From 385c1a7d48ebe018bcde205f8bd4506403dc889a Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Tue, 18 Aug 2026 02:41:02 -0700 Subject: [PATCH] [Windows] Implement sendmmsg (#3699) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Motivation: `CNIOWindows_sendmmsg` was a stub that asserted and then called `abort()`, so any code path reaching it took the whole process down: ``` Assertion failed: !"sendmmsg not implemented", file Sources\CNIOWindows\shim.c, line 26 ``` ### Modifications: Winsock has no `sendmmsg`, so emulate it by sending the messages one at a time — exactly what the Darwin shim does for the same reason. Error semantics match the Darwin implementation: a failure on the first message is reported to the caller. ### Result: Further progress on windows. Windows-only; no other platform is affected. --------- Co-authored-by: Fabian Fett --- Sources/CNIOWindows/include/CNIOWindows.h | 2 - Sources/CNIOWindows/shim.c | 6 -- Sources/NIOPosix/BSDSocketAPIWindows.swift | 77 +++++++++++++++++----- 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/Sources/CNIOWindows/include/CNIOWindows.h b/Sources/CNIOWindows/include/CNIOWindows.h index 3ed8611c3..fe8883b61 100644 --- a/Sources/CNIOWindows/include/CNIOWindows.h +++ b/Sources/CNIOWindows/include/CNIOWindows.h @@ -93,8 +93,6 @@ NIO(sendto)(SOCKET s, const void *buf, int len, int flags, const SOCKADDR *to, return sendto(s, buf, len, flags, to, tolen); } -int NIO(sendmmsg)(SOCKET s, NIO(mmsghdr) *msgvec, unsigned int vlen, int flags); - int NIO(recvmmsg)(SOCKET s, NIO(mmsghdr) *msgvec, unsigned int vlen, int flags, struct timespec *timeout); diff --git a/Sources/CNIOWindows/shim.c b/Sources/CNIOWindows/shim.c index 02c483c91..58c338913 100644 --- a/Sources/CNIOWindows/shim.c +++ b/Sources/CNIOWindows/shim.c @@ -21,12 +21,6 @@ #include #include -int CNIOWindows_sendmmsg(SOCKET s, CNIOWindows_mmsghdr *msgvec, unsigned int vlen, - int flags) { - assert(!"sendmmsg not implemented"); - abort(); -} - int CNIOWindows_recvmmsg(SOCKET s, CNIOWindows_mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout) { diff --git a/Sources/NIOPosix/BSDSocketAPIWindows.swift b/Sources/NIOPosix/BSDSocketAPIWindows.swift index 0000caa84..02390bc0a 100644 --- a/Sources/NIOPosix/BSDSocketAPIWindows.swift +++ b/Sources/NIOPosix/BSDSocketAPIWindows.swift @@ -346,6 +346,31 @@ extension NIOBSDSocket { msgHdr lpMsg: UnsafePointer, flags dwFlags: CInt ) throws -> IOResult { + let WSASendMsg = try Self.lookupWSASendMsg(socket: Handle) + + let lpMsg: LPWSAMSG = UnsafeMutablePointer(mutating: lpMsg) + var NumberOfBytesSent: DWORD = 0 + // FIXME(compnerd) is the socket guaranteed to not be overlapped? + if WSASendMsg( + Handle, + lpMsg, + DWORD(dwFlags), + &NumberOfBytesSent, + nil, + nil + ) == SOCKET_ERROR { + throw IOError(winsock: WSAGetLastError(), reason: "sendmsg") + } + return .processed(size_t(NumberOfBytesSent)) + } + + /// Resolves the `WSASendMsg` extension function for the given socket. + /// + /// Winsock does not export `WSASendMsg` for direct linking, it has to be looked up per + /// socket through `WSAIoctl`. + private static func lookupWSASendMsg( + socket Handle: NIOBSDSocket.Handle + ) throws -> LPFN_WSASENDMSG { // TODO(compnerd) see comment above var InBuffer = WSAID_WSASENDMSG var pfnWSASendMsg: LPFN_WSASENDMSG? @@ -370,21 +395,7 @@ extension NIOBSDSocket { reason: "sendmsg" ) } - - let lpMsg: LPWSAMSG = UnsafeMutablePointer(mutating: lpMsg) - var NumberOfBytesSent: DWORD = 0 - // FIXME(compnerd) is the socket guaranteed to not be overlapped? - if WSASendMsg( - Handle, - lpMsg, - DWORD(dwFlags), - &NumberOfBytesSent, - nil, - nil - ) == SOCKET_ERROR { - throw IOError(winsock: WSAGetLastError(), reason: "sendmsg") - } - return .processed(size_t(NumberOfBytesSent)) + return WSASendMsg } @inline(never) @@ -475,7 +486,41 @@ extension NIOBSDSocket { ) throws -> IOResult { - .processed(Int(CNIOWindows_sendmmsg(socket, msgvec, vlen, flags))) + // Winsock has no `sendmmsg`, so send the messages one at a time, in the same way the + // Darwin shim does. The `WSASendMsg` extension function is resolved once for the whole + // batch rather than once per message. + guard vlen > 0 else { + return .processed(0) + } + let WSASendMsg = try Self.lookupWSASendMsg(socket: socket) + + for i in 0.. 0 else { + // Nothing was sent at all. Report a blocking socket as such, which is what + // the POSIX implementation's `syscall(blocking: true)` wrapper does, and + // report anything else as an error. + if error == WSAEWOULDBLOCK { + return .wouldBlock(0) + } + throw IOError(winsock: error, reason: "sendmmsg") + } + // Earlier messages were sent successfully, so report a short send and leave the + // caller to retry the rest, as the Darwin shim does. + return .processed(i) + } + + msgvec[i].msg_len = CUnsignedInt(dwNumberOfBytesSent) + } + + return .processed(Int(vlen)) } // NOTE: this should return a `ssize_t`, however, that is not a standard