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