comparison librtpalloc/recvmsg_wrap.c @ 3:60b512a868b4

librtpalloc: port of recvmsg wrapper module
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 27 May 2024 20:41:21 +0000
parents
children
comparison
equal deleted inserted replaced
2:247f4bbde24c 3:60b512a868b4
1 /*
2 * Here we implement the collect_rtpmgr_resp() function,
3 * which is a wrapper around the mess of recvmsg
4 * with file descriptor passing.
5 */
6
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include <strings.h>
12
13 #include <themwi/rtp/rtp_alloc_if.h>
14 #include <themwi/rtp/rtp_alloc_resp.h>
15
16 int collect_rtpmgr_resp(int ctrl_fd, int recv_flags,
17 struct rtp_alloc_resp_wrap *out)
18 {
19 int rc;
20 struct iovec iov;
21 struct msghdr msg;
22 union {
23 char buf[CMSG_SPACE(sizeof(int) * 4)];
24 struct cmsghdr align;
25 } cmsgu;
26 struct cmsghdr *cmsg;
27
28 iov.iov_base = &out->resp;
29 iov.iov_len = sizeof(struct rtp_alloc_resp);
30 bzero(&msg, sizeof msg);
31 msg.msg_iov = &iov;
32 msg.msg_iovlen = 1;
33 msg.msg_control = cmsgu.buf;
34 msg.msg_controllen = CMSG_SPACE(sizeof(int) * 4);
35 rc = recvmsg(ctrl_fd, &msg, recv_flags);
36 if (rc < 0)
37 return rc;
38 out->resp_len = rc;
39 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
40 if (cmsg->cmsg_level == SOL_SOCKET &&
41 cmsg->cmsg_type == SCM_RIGHTS)
42 break;
43 }
44 if (cmsg) {
45 switch (cmsg->cmsg_len) {
46 case CMSG_LEN(sizeof(int)):
47 out->num_fd = 1;
48 break;
49 case CMSG_LEN(sizeof(int) * 2):
50 out->num_fd = 2;
51 break;
52 case CMSG_LEN(sizeof(int) * 3):
53 out->num_fd = 3;
54 break;
55 case CMSG_LEN(sizeof(int) * 4):
56 out->num_fd = 4;
57 break;
58 default:
59 out->num_fd = 0;
60 }
61 if (out->num_fd)
62 bcopy(CMSG_DATA(cmsg), out->fd_buf,
63 sizeof(int) * out->num_fd);
64 } else
65 out->num_fd = 0;
66 return 0;
67 }