Skip to content

Commit de4e1be

Browse files
committed
feat: Add MULTI_CONN support option on server (see #4)
1 parent 5a1141f commit de4e1be

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

pkg/protocol/negotiation.go

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const (
2424
NEGOTIATION_TYPE_INFO_NAME = uint16(1)
2525
NEGOTIATION_TYPE_INFO_DESCRIPTION = uint16(2)
2626
NEGOTIATION_TYPE_INFO_BLOCKSIZE = uint16(3)
27+
28+
NEGOTIATION_REPLY_FLAGS_HAS_FLAGS = uint16((1 << 0))
29+
NEGOTIATION_REPLY_FLAGS_CAN_MULTI_CONN = uint16((1 << 8))
2730
)
2831

2932
type NegotiationNewstyleHeader struct {

pkg/server/nbd.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818
)
1919

2020
const (
21-
DefaultMaximumRequestSize = 32 * 1024 * 1024 // Support for a 32M maximum packet size is expected: https://sourceforge.net/p/nbd/mailman/message/35081223/
21+
defaultMaximumRequestSize = 32 * 1024 * 1024 // Support for a 32M maximum packet size is expected: https://sourceforge.net/p/nbd/mailman/message/35081223/
2222
)
2323

2424
type Export struct {
@@ -36,12 +36,14 @@ type Options struct {
3636
MaximumBlockSize uint32
3737

3838
MaximumRequestSize int
39+
SupportsMultiConn bool
3940
}
4041

4142
func Handle(conn net.Conn, exports []*Export, options *Options) error {
4243
if options == nil {
4344
options = &Options{
44-
ReadOnly: false,
45+
ReadOnly: false,
46+
SupportsMultiConn: true,
4547
}
4648
}
4749

@@ -54,11 +56,11 @@ func Handle(conn net.Conn, exports []*Export, options *Options) error {
5456
}
5557

5658
if options.MaximumBlockSize == 0 {
57-
options.MaximumBlockSize = DefaultMaximumRequestSize
59+
options.MaximumBlockSize = defaultMaximumRequestSize
5860
}
5961

6062
if options.MaximumRequestSize == 0 {
61-
options.MaximumRequestSize = DefaultMaximumRequestSize
63+
options.MaximumRequestSize = defaultMaximumRequestSize
6264
}
6365

6466
// Negotiation
@@ -145,11 +147,16 @@ n:
145147
}
146148

147149
{
150+
transmissionFlags := uint16(0)
151+
if options.SupportsMultiConn {
152+
transmissionFlags = protocol.NEGOTIATION_REPLY_FLAGS_HAS_FLAGS | protocol.NEGOTIATION_REPLY_FLAGS_CAN_MULTI_CONN
153+
}
154+
148155
info := &bytes.Buffer{}
149156
if err := binary.Write(info, binary.BigEndian, protocol.NegotiationReplyInfo{
150157
Type: protocol.NEGOTIATION_TYPE_INFO_EXPORT,
151158
Size: uint64(size),
152-
TransmissionFlags: 0,
159+
TransmissionFlags: transmissionFlags,
153160
}); err != nil {
154161
return err
155162
}
@@ -335,7 +342,7 @@ n:
335342
}
336343

337344
length := requestHeader.Length
338-
if length > DefaultMaximumRequestSize {
345+
if length > defaultMaximumRequestSize {
339346
return ErrInvalidRequestSize
340347
}
341348

0 commit comments

Comments
 (0)