From 54b0dc09ca78fd052acfcbadb515a11a3e5347e8 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Tue, 19 Aug 2025 12:37:12 +0200 Subject: [PATCH] [Windows] Make `SocketChannel.swift` compile (#3348) - vsock isn't supported on windows. - creating `in_addr` needs conditional compiling --- Sources/NIOPosix/SocketChannel.swift | 21 ++++++++++++++++++++- Sources/NIOPosix/VsockAddress.swift | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Sources/NIOPosix/SocketChannel.swift b/Sources/NIOPosix/SocketChannel.swift index fd51c008c..504c2ad83 100644 --- a/Sources/NIOPosix/SocketChannel.swift +++ b/Sources/NIOPosix/SocketChannel.swift @@ -121,7 +121,11 @@ final class SocketChannel: BaseStreamSocketChannel, @unchecked Sendable case _ as ChannelOptions.Types.ConnectTimeoutOption: return connectTimeout as! Option.Value case _ as ChannelOptions.Types.LocalVsockContextID: + #if os(Windows) + fallthrough + #else return try self.socket.getLocalVsockContextID() as! Option.Value + #endif default: return try super.getOption0(option) } @@ -276,7 +280,11 @@ final class ServerSocketChannel: BaseSocketChannel, @unchecked Sen case _ as ChannelOptions.Types.BacklogOption: return backlog as! Option.Value case _ as ChannelOptions.Types.LocalVsockContextID: + #if os(Windows) + fallthrough + #else return try self.socket.getLocalVsockContextID() as! Option.Value + #endif default: return try super.getOption0(option) } @@ -311,8 +319,13 @@ final class ServerSocketChannel: BaseSocketChannel, @unchecked Sen switch target { case .socketAddress(let address): try socket.bind(to: address) + #if os(Windows) + case .vsockAddress: + fatalError(vsockUnimplemented) + #else case .vsockAddress(let address): try socket.bind(to: address) + #endif } self.updateCachedAddressesFromSocket(updateRemote: false) try self.socket.listen(backlog: backlog) @@ -1185,10 +1198,16 @@ extension DatagramChannel: MulticastChannel { ) try self.socket.setOption(level: .ip, name: operation.optionName(level: .ip), value: multicastRequest) case (.v4(let groupAddress), .none): + #if os(Windows) + var addr = in_addr() + addr.S_un.S_addr = INADDR_ANY + #else + let addr = in_addr(s_addr: INADDR_ANY) + #endif // IPv4 binding without target interface. let multicastRequest = ip_mreq( imr_multiaddr: groupAddress.address.sin_addr, - imr_interface: in_addr(s_addr: INADDR_ANY) + imr_interface: addr ) try self.socket.setOption(level: .ip, name: operation.optionName(level: .ip), value: multicastRequest) case (.v6(let groupAddress), .some(.v6)): diff --git a/Sources/NIOPosix/VsockAddress.swift b/Sources/NIOPosix/VsockAddress.swift index 75d53d223..c8ca97441 100644 --- a/Sources/NIOPosix/VsockAddress.swift +++ b/Sources/NIOPosix/VsockAddress.swift @@ -24,7 +24,7 @@ import CNIODarwin #endif import CNIOLinux #endif -private let vsockUnimplemented = "VSOCK support is not implemented for this platform" +let vsockUnimplemented = "VSOCK support is not implemented for this platform" // MARK: - Public API that's available on all platforms.