xref: /third_party/musl/src/network/sendmsg.c (revision 570af302)
1570af302Sopenharmony_ci#include <sys/socket.h>
2570af302Sopenharmony_ci#include <limits.h>
3570af302Sopenharmony_ci#include <string.h>
4570af302Sopenharmony_ci#include <errno.h>
5570af302Sopenharmony_ci#include "syscall.h"
6570af302Sopenharmony_ci
7570af302Sopenharmony_cissize_t sendmsg(int fd, const struct msghdr *msg, int flags)
8570af302Sopenharmony_ci{
9570af302Sopenharmony_ci#if LONG_MAX > INT_MAX
10570af302Sopenharmony_ci	struct msghdr h;
11570af302Sopenharmony_ci	/* Kernels before 2.6.38 set SCM_MAX_FD to 255, allocate enough
12570af302Sopenharmony_ci	 * space to support an SCM_RIGHTS ancillary message with 255 fds.
13570af302Sopenharmony_ci	 * Kernels since 2.6.38 set SCM_MAX_FD to 253. */
14570af302Sopenharmony_ci	struct cmsghdr chbuf[CMSG_SPACE(255*sizeof(int))/sizeof(struct cmsghdr)+1], *c;
15570af302Sopenharmony_ci	if (msg) {
16570af302Sopenharmony_ci		h = *msg;
17570af302Sopenharmony_ci		h.__pad1 = h.__pad2 = 0;
18570af302Sopenharmony_ci		msg = &h;
19570af302Sopenharmony_ci		if (h.msg_controllen) {
20570af302Sopenharmony_ci			if (h.msg_controllen > sizeof chbuf) {
21570af302Sopenharmony_ci				errno = ENOMEM;
22570af302Sopenharmony_ci				return -1;
23570af302Sopenharmony_ci			}
24570af302Sopenharmony_ci			memcpy(chbuf, h.msg_control, h.msg_controllen);
25570af302Sopenharmony_ci			h.msg_control = chbuf;
26570af302Sopenharmony_ci			for (c=CMSG_FIRSTHDR(&h); c; c=CMSG_NXTHDR(&h,c))
27570af302Sopenharmony_ci				c->__pad1 = 0;
28570af302Sopenharmony_ci		}
29570af302Sopenharmony_ci	}
30570af302Sopenharmony_ci#endif
31570af302Sopenharmony_ci	return socketcall_cp(sendmsg, fd, msg, flags, 0, 0, 0);
32570af302Sopenharmony_ci}
33