-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
net: support AF_UNIX paths in net.BoundSocket #64399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guybedford
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
guybedford:net-boundsocket-pipe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+322
−13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -380,19 +380,35 @@ const kBoundSource = Symbol('kBoundSource'); | |
| // Server/Socket. | ||
| const kBoundSocketConsume = Symbol('kBoundSocketConsume'); | ||
|
|
||
| // A role-neutral wrapper over a synchronously bound libuv TCP handle: bound to | ||
| // a local address but neither listening nor connecting until adopted by exactly | ||
| // one Server (server.listen) or Socket (new net.Socket({ handle })). Adoption | ||
| // transfers ownership; an un-adopted handle must be closed by the caller. | ||
| // bind(2) is non-blocking, so binding happens inline and errors throw | ||
| // synchronously. host must be a numeric IP literal; no DNS is performed. | ||
| // Internal: read a pipe BoundSocket's bound path before adoption (undefined for | ||
| // a TCP BoundSocket). | ||
| const kBoundSocketPath = Symbol('kBoundSocketPath'); | ||
|
|
||
| // The source path of an adopted, bound client pipe, surfaced as localAddress. | ||
| const kBoundPath = Symbol('kBoundPath'); | ||
|
|
||
| const isLinux = process.platform === 'linux'; | ||
|
|
||
| // A role-neutral wrapper over a synchronously bound libuv handle: bound to a | ||
| // local address (a numeric IP literal for TCP, or a filesystem/abstract path | ||
| // for a unix-domain socket via { path }) but neither listening nor connecting | ||
| // until adopted by exactly one Server (server.listen) or Socket | ||
| // (new net.Socket({ handle })). Adoption transfers ownership; an un-adopted | ||
| // handle must be closed by the caller. bind(2) is non-blocking, so binding | ||
| // happens inline and errors throw synchronously. No DNS is performed. | ||
| class BoundSocket { | ||
| #handle; | ||
| #address = {}; | ||
| #path; | ||
|
|
||
| constructor(options = kEmptyObject) { | ||
| validateObject(options, 'options'); | ||
|
|
||
| if (options.path !== undefined) { | ||
| this.#bindPipe(options); | ||
| return; | ||
| } | ||
|
|
||
| const port = validatePort(options.port ?? 0, 'options.port'); | ||
|
|
||
| const ipv6Only = options.ipv6Only ?? false; | ||
|
|
@@ -443,13 +459,47 @@ class BoundSocket { | |
| this.#handle = handle; | ||
| } | ||
|
|
||
| // The kernel-assigned local address, resolved at construction; reflects the | ||
| // OS-assigned ephemeral port when the bind requested port 0. | ||
| // Bind a named unix-domain socket (or Windows named pipe). A leading '\0' | ||
| // selects the Linux abstract namespace. path is mutually exclusive with the | ||
| // TCP options; uv_pipe_bind is synchronous so conflicts throw here. | ||
| #bindPipe(options) { | ||
| const { path } = options; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, you're destructing |
||
| if (options.host !== undefined || options.port !== undefined || | ||
| options.ipv6Only !== undefined || options.reusePort !== undefined) { | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'options', options, | ||
| 'path is mutually exclusive with host, port, ipv6Only, and reusePort'); | ||
| } | ||
| validateString(path, 'options.path'); | ||
| if (path[0] === '\0' && !isLinux) { | ||
| throw new ERR_INVALID_ARG_VALUE( | ||
| 'options.path', path, | ||
| 'abstract socket paths are only supported on Linux'); | ||
| } | ||
|
|
||
| const handle = new Pipe(PipeConstants.SOCKET); | ||
| const err = handle.bind(path); | ||
| if (err) { | ||
| handle.close(); | ||
| throw new ErrnoException(err, 'bind'); | ||
| } | ||
|
|
||
| this.#handle = handle; | ||
| this.#path = path; | ||
| } | ||
|
|
||
| // The bound local endpoint: an { address, family, port } object for TCP, or | ||
| // the path string for a pipe (matching net.Server.address()). Resolved at | ||
| // construction; a TCP bind of port 0 reflects the OS-assigned port. | ||
| address() { | ||
| if (this.#handle === null) { | ||
| throw new ERR_SOCKET_HANDLE_ADOPTED(); | ||
| } | ||
| return this.#address; | ||
| return this.#path ?? this.#address; | ||
| } | ||
|
|
||
| get [kBoundSocketPath]() { | ||
| return this.#path; | ||
| } | ||
|
|
||
| // The underlying OS file descriptor, or -1 where sockets have none (Windows). | ||
|
|
@@ -488,6 +538,13 @@ class BoundSocket { | |
| this.#handle = null; | ||
| return handle; | ||
| } | ||
|
|
||
| // Reports whether this is a pipe (unix-domain) bind rather than TCP. Its mere | ||
| // presence on the prototype ('isPipe' in net.BoundSocket.prototype) is the | ||
| // capability signal that this build honors { path } instead of a TCP port. | ||
| get isPipe() { | ||
| return this.#path !== undefined; | ||
| } | ||
| } | ||
|
|
||
| function Socket(options) { | ||
|
|
@@ -555,9 +612,24 @@ function Socket(options) { | |
| let boundNotConnected = false; | ||
| if (options.handle) { | ||
| if (options.handle instanceof BoundSocket) { | ||
| const boundPath = options.handle[kBoundSocketPath]; | ||
| this._handle = options.handle[kBoundSocketConsume](); | ||
| this[kBoundSource] = true; | ||
| boundNotConnected = true; | ||
| // A bound client pipe owns a source path; surface it as localAddress. | ||
| if (boundPath !== undefined) { | ||
| this[kBoundPath] = boundPath; | ||
| // uv_pipe_bind() only assigns the fd; it does not open the stream, so a | ||
| // later connect() would leave the handle without its readable/writable | ||
| // flags and unusable. Re-open the already-bound fd (idempotent, sets the | ||
| // flags) so the adopted client pipe connects to a working stream. | ||
| if (this._handle.fd >= 0) { | ||
| const err = this._handle.open(this._handle.fd); | ||
| if (err) { | ||
| throw new ErrnoException(err, 'open'); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| this._handle = options.handle; // private | ||
| } | ||
|
|
@@ -1120,6 +1192,11 @@ protoGetter('remotePort', function remotePort() { | |
|
|
||
|
|
||
| Socket.prototype._getsockname = function() { | ||
| // An adopted bound client pipe has no handle getsockname; its source path was | ||
| // captured at adoption and stays authoritative across the connect reset. | ||
| if (this[kBoundPath] !== undefined) { | ||
| return { address: this[kBoundPath] }; | ||
| } | ||
| if (!this._handle || !this._handle.getsockname) { | ||
| return {}; | ||
| } else if (!this._sockname) { | ||
|
|
@@ -1478,7 +1555,13 @@ Socket.prototype.connect = function(...args) { | |
| } | ||
|
|
||
| const { path } = options; | ||
| const pipe = !!path; | ||
| // An adopted BoundSocket handle already fixes the transport; trust its type | ||
| // rather than inferring pipe-ness from a path option on the connect call. | ||
| // Once destroyed the adopted handle is gone (its reservation released), so | ||
| // fall back to the path option, as for other pre-existing handles (e.g. a | ||
| // TLSWrap) that are not transport handles. | ||
| const pipe = this[kBoundSource] && this._handle ? | ||
| this._handle instanceof Pipe : !!path; | ||
| debug('pipe', pipe, path); | ||
|
|
||
| if (!this._handle) { | ||
|
|
@@ -2425,7 +2508,12 @@ Server.prototype.listen = function(...args) { | |
| boundSocket = options.handle; | ||
| } | ||
| if (boundSocket !== null) { | ||
| const boundPath = boundSocket[kBoundSocketPath]; | ||
| this._handle = boundSocket[kBoundSocketConsume](); | ||
| // A pipe-backed handle reports its path via Server.address(). | ||
| if (boundPath !== undefined) { | ||
| this._pipeName = boundPath; | ||
| } | ||
| this[async_id_symbol] = this._handle.getAsyncId(); | ||
| this._listeningId++; | ||
| listenInCluster(this, null, -1, -1, backlogFromArgs, undefined, true); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the
`host`/`port`suggest`host`or`port`here.