Files
swift-nio/Sources/NIOPosix
Victor DebrayandGeorge Barnett c1ccdbcc95 Add channel options for a vsock socket's local and remote addresses (#3689)
Add channel options to read a vsock socket's local and remote addresses

### Motivation:

NIOPosix supports vsock via `VsockAddress`, `ServerBootstrap.bind(to:)`
and `ClientBootstrap.connect(to:)`, but `Channel.localAddress` and
`Channel.remoteAddress` are always `nil` on vsock channels because
`NIOCore.SocketAddress` has no vsock representation.

Two things are unreachable as a result:

- A server accepting a vsock connection cannot tell which context
connected. The peer's context ID is the only identity a vsock connection
carries, and it is supplied by the kernel rather than the peer, which
makes it a natural basis for authorization.
- A listener bound to `VsockAddress/Port/any` cannot learn the port the
kernel assigned, so callers are forced to hard-code ports and race for
them.

`ChannelOptions.localVsockContextID` already exists for the first half
of this problem, but it reports only the context ID, not the port.

Adding a `.vsock` case to `SocketAddress` would fix both, but it is
source-breaking for every exhaustive switch over that enum, in NIO and
downstream.

### Modifications:

- Add `ChannelOptions.Types.LocalVsockAddress` and `RemoteVsockAddress`,
both get-only with `Value == VsockAddress`, alongside the existing
`LocalVsockContextID`.
- Add `BaseSocket.getLocalVsockAddress()` and `getRemoteVsockAddress()`,
sharing a helper that reads into a `sockaddr_storage` via
`withMutableSockAddr` and `convert()`, mirroring `BaseSocket.get_addr`.
On a non-vsock socket `getsockname`/`getpeername` fill a different
`sockaddr`, and reinterpreting those bytes would yield a context ID
derived from unrelated data such as a peer's IP address and port, so the
helper checks the address family and throws
`SocketAddressError.unsupported` on a mismatch.
- Service both options in `SocketChannel.getOption0`, and
`LocalVsockAddress` in `ServerSocketChannel.getOption0`.
`RemoteVsockAddress` is not serviced on `ServerSocketChannel`: a
listener has no peer.
- Tests: both helpers reject non-vsock sockets; over vsock loopback the
client's peer port matches the port the listener bound and the accepted
channel reports the same CID the client saw; a `Port/any` listener
reports a concrete port and a CID of `.any`, matching the channel
option. The loopback tests are gated on `System.supportsVsockLoopback`
in the same way as `testGetLocalCID` and `testEchoVsock`, so they run in
the `vsock-tests` CI job and skip elsewhere.

This is purely additive: two new option types, four new static
accessors, and new arms in two internal switches. No existing
declaration changes.

### Result:

A vsock server can identify the connecting context, and a wildcard-bound
listener can report its port:

```swift
let peer = try await channel.getOption(.remoteVsockAddress).get()
peer.cid  // the connecting context's CID

let local = try await listener.getOption(.localVsockAddress).get()
local.port  // the port a `Port/any` bind was given
```

Reading either option on a channel that isn't backed by a vsock socket
throws `SocketAddressError.unsupported`.

Co-authored-by: George Barnett <gbarnett@apple.com>
2026-09-15 07:01:23 +00:00
..
2026-07-07 15:46:28 +01:00
2026-07-07 15:46:28 +01:00
2026-07-07 15:46:28 +01:00