implement sendmmsg and recvmmsg
[musl] / src / network / sendmmsg.c
1 #define _GNU_SOURCE
2 #include <sys/socket.h>
3 #include <limits.h>
4 #include <errno.h>
5 #include "syscall.h"
6
7 int sendmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags)
8 {
9 #if LONG_MAX > INT_MAX
10         /* Can't use the syscall directly because the kernel has the wrong
11          * idea for the types of msg_iovlen, msg_controllen, and cmsg_len,
12          * and the cmsg blocks cannot be modified in-place. */
13         int i;
14         if (vlen > IOV_MAX) vlen = IOV_MAX; /* This matches the kernel. */
15         for (i=0; i<vlen; i++) {
16                 /* As an unfortunate inconsistency, the sendmmsg API uses
17                  * unsigned int for the resulting msg_len, despite sendmsg
18                  * returning ssize_t. However Linux limits the total bytes
19                  * sent by sendmsg to INT_MAX, so the assignment is safe. */
20                 ssize_t r = sendmsg(fd, &msgvec[i].msg_hdr, flags);
21                 if (r < 0) goto error;
22                 msgvec[i].msg_len = r;
23         }
24 error:
25         return i ? i : -1;
26 #else
27         return syscall_cp(SYS_sendmmsg, fd, msgvec, vlen, flags);
28 #endif
29 }