Add a server-side listener to the SMB transport layer (Refs #1068) - #1069
Open
p0dalirius wants to merge 1 commit into
Open
Add a server-side listener to the SMB transport layer (Refs #1068)#1069p0dalirius wants to merge 1 commit into
p0dalirius wants to merge 1 commit into
Conversation
The SMB transport layer had only a client side: Transport exposes Connect, and neither implementation could be built from an accepted connection, so nothing in the tree could listen for SMB. Add the accept side, keeping both transports interchangeable above it: - tcp.NewTCPTransportFromConn and nbt.NewNBTTransportFromConn adopt an already-established connection, so the server side of a connection reuses the existing framing, length limits and read-deadline handling. - nbns.DecodeSessionServiceName decodes a second-level-encoded NetBIOS name, the inverse of EncodeSessionServiceName. It returns the byte count consumed so a caller can decode the CALLED name and then the CALLING name that follows it, and it walks scope labels rather than assuming the default scope, bounding each by the 63-byte limit. - nbt.AcceptSession completes the RFC 1002 4.3 handshake on an accepted connection: it reads the SESSION REQUEST, decodes both names, and answers a POSITIVE SESSION RESPONSE (0x82, LENGTH 0) or a NEGATIVE SESSION RESPONSE (0x83, LENGTH 1) carrying the RFC 1002 4.3.4 error code. A CALLED name addressed to a service other than the server service (0x20) and a name the endpoint does not serve are refused separately, and a wildcard CALLED name such as the "*SMBSERVER" convention is always served, mirroring what EstablishSession expects to receive and retry against. - transport.Listener with ListenTCP and ListenNBT yields a Transport whose transport-level handshake, where the transport has one, is already complete. A connection that fails the NetBIOS handshake is logged and discarded inside Accept instead of being surfaced, so one bad client cannot take the listener down. An address with no port binds the dialect default: 445 for Direct TCP, 139 for NetBIOS. Tests cover the name codec round trip across name lengths, suffixes and a scoped name, and reject each malformed encoding; the handshake against the client side of the same implementation, the wildcard and any-name policies, and refusal of an unserved name, a wrong service suffix and malformed frames, asserting the exact response bytes; and, for each listener, a bidirectional SMB-message round trip, that a failed handshake leaves the listener serving the next client, that Close unblocks Accept, and that a portless address lands on the default port.
p0dalirius
force-pushed
the
enhancement-smb1-server-transport
branch
from
August 24, 2026 13:46
4ce6c10 to
b570fe4
Compare
This was referenced Aug 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Commit 1 of 12 for #1068. The SMB transport layer had only a client side:
TransportexposesConnect, and neither implementation could be built from an accepted connection, so nothing in the tree could listen for SMB.grep -rl 'net.Listen|\.Accept()'matchednetbios/nbns,netbios/nbdgmandllmnr/server, and nothing undernetwork/smb/.This adds the accept side, keeping both transports interchangeable above it so the server layers in later commits do not care which one a connection arrived on.
Changes
tcp.NewTCPTransportFromConnandnbt.NewNBTTransportFromConnadopt an already-established connection, so the server side reuses the existing framing,MaxDirectTCPPayloadSizelimit and read-deadline handling rather than duplicating them.nbns.DecodeSessionServiceNamedecodes a second-level-encoded NetBIOS name — the inverse of the existingEncodeSessionServiceName. It returns the number of bytes consumed so a caller can decode the CALLED name and then the CALLING name that follows it, and it walks scope labels rather than assuming the default scope, bounding each by the 63-byte limit.nbt.AcceptSessioncompletes the RFC 1002 §4.3 handshake on an accepted connection, the counterpart ofEstablishSession. A CALLED name addressed to a service other than the server service (0x20) and a name the endpoint does not serve are refused separately, and a wildcard CALLED name such as the*SMBSERVERconvention is always served — mirroring whatEstablishSessionexpects to receive and retry against.transport.Listener, withListenTCPandListenNBT, yields aTransportwhose transport-level handshake (where the transport has one) is already complete. A connection that fails the NetBIOS handshake is logged and discarded insideAcceptrather than surfaced as an error, so one bad client cannot take the listener down. An address with no port binds the dialect default: 445 for Direct TCP, 139 for NetBIOS.No existing behaviour changes: the additions are new symbols, and the client-side paths are untouched.
Wire format
Cross-checked against RFC 1002 §4.3:
The error codes emitted (
0x80not listening on called name,0x82called name not present,0x8Funspecified) are the existingnetbios.NEGATIVE_SESSION_*constants.Testing
go build ./...,go vet ./...andgo test ./...are green across the repository.New tests:
*SMBSERVERknown answer; the two-names-in-one-buffer case the SESSION REQUEST needs; and rejection of a bad length byte, a bad encoding character, an unterminated name, a truncated scope label and an oversize scope label.Closeunblocks a blockedAccept; and that a portless address lands on the default port.Notes
The remaining eleven commits for #1068 build on this: the server skeleton and dispatch loop come next.