diff librtpalloc/rtpmgr_resp.c @ 184:f8c40090a0a8

librtpalloc: new library for talking to themwi-rtp-mgr
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Mar 2023 23:48:14 -0800
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/librtpalloc/rtpmgr_resp.c	Sat Mar 11 23:48:14 2023 -0800
@@ -0,0 +1,66 @@
+/*
+ * Here we implement the collect_rtpmgr_resp() function,
+ * which is a wrapper around the mess of recvmsg
+ * with file descriptor passing.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdint.h>
+#include <string.h>
+#include <strings.h>
+#include "../include/rtp_alloc.h"
+#include "rtpmgr_resp.h"
+
+collect_rtpmgr_resp(ctrl_fd, recv_flags, out)
+	struct rtp_alloc_resp_wrap *out;
+{
+	int rc;
+	struct iovec iov;
+	struct msghdr msg;
+	union {
+		char buf[CMSG_SPACE(sizeof(int) * 4)];
+		struct cmsghdr align;
+	} cmsgu;
+	struct cmsghdr *cmsg;
+
+	iov.iov_base = &out->resp;
+	iov.iov_len = sizeof(struct rtp_alloc_resp);
+	bzero(&msg, sizeof msg);
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = cmsgu.buf;
+	msg.msg_controllen = CMSG_SPACE(sizeof(int) * 4);
+	rc = recvmsg(ctrl_fd, &msg, recv_flags);
+	if (rc < 0)
+		return rc;
+	out->resp_len = rc;
+	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+		if (cmsg->cmsg_level == SOL_SOCKET &&
+		    cmsg->cmsg_type == SCM_RIGHTS)
+			break;
+	}
+	if (cmsg) {
+		switch (cmsg->cmsg_len) {
+		case CMSG_LEN(sizeof(int)):
+			out->num_fd = 1;
+			break;
+		case CMSG_LEN(sizeof(int) * 2):
+			out->num_fd = 2;
+			break;
+		case CMSG_LEN(sizeof(int) * 3):
+			out->num_fd = 3;
+			break;
+		case CMSG_LEN(sizeof(int) * 4):
+			out->num_fd = 4;
+			break;
+		default:
+			out->num_fd = 0;
+		}
+		if (out->num_fd)
+			bcopy(CMSG_DATA(cmsg), out->fd_buf,
+				sizeof(int) * out->num_fd);
+	} else
+		out->num_fd = 0;
+	return 0;
+}