[Windows] Implement sendmmsg (#3699)

### 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 <fabianfett@apple.com>
This commit is contained in:
Jake PetroulesandFabian Fett authored and GitHub committed 2026-08-18 11:41:02 +02:00
1 parent 48119dbbd2
commit 385c1a7d48
3 files changed
+61 -24

No files matched your search

@@ -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);
-6
View File
@@ -21,12 +21,6 @@
#include <stdio.h>
#include <winbase.h>
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) {
+61 -16
View File
@@ -346,6 +346,31 @@ extension NIOBSDSocket {
msgHdr lpMsg: UnsafePointer<msghdr>,
flags dwFlags: CInt
) throws -> IOResult<size_t> {
let WSASendMsg = try Self.lookupWSASendMsg(socket: Handle)
let lpMsg: LPWSAMSG = UnsafeMutablePointer<WSAMSG>(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<WSAMSG>(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<Int>
{
.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..<Int(vlen) {
var dwNumberOfBytesSent: DWORD = 0
// FIXME(compnerd) is the socket guaranteed to not be overlapped?
let iResult = withUnsafeMutablePointer(to: &msgvec[i].msg_hdr) { lpMsg in
WSASendMsg(socket, lpMsg, DWORD(flags), &dwNumberOfBytesSent, nil, nil)
}
if iResult == SOCKET_ERROR {
let error = WSAGetLastError()
guard i > 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