changeset 3:60b512a868b4

librtpalloc: port of recvmsg wrapper module
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 27 May 2024 20:41:21 +0000
parents 247f4bbde24c
children 764bbf72392f
files include/rtp_alloc_resp.h librtpalloc/Makefile librtpalloc/recvmsg_wrap.c
diffstat 3 files changed, 105 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/rtp_alloc_resp.h	Mon May 27 20:41:21 2024 +0000
@@ -0,0 +1,20 @@
+/*
+ * This header file declares the interface to the librtpalloc module
+ * that provides a wrapper around the recvmsg(2) call retrieving
+ * the response "packet" (structure plus UNIX access rights passing)
+ * from themwi-rtp-mgr.
+ */
+
+#pragma once
+
+#include <themwi/rtp/rtp_alloc_if.h>
+
+struct rtp_alloc_resp_wrap {
+	struct rtp_alloc_resp resp;
+	unsigned	resp_len;
+	int		fd_buf[4];
+	unsigned	num_fd;
+};
+
+int collect_rtpmgr_resp(int ctrl_fd, int recv_flags,
+			struct rtp_alloc_resp_wrap *out);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/librtpalloc/Makefile	Mon May 27 20:41:21 2024 +0000
@@ -0,0 +1,18 @@
+CPPFLAGS=-I../build-inc
+OBJS=	recvmsg_wrap.o
+LIB=	librtpalloc.a
+
+include ../config.defs
+
+all:	${LIB}
+
+${LIB}:	${OBJS}
+	ar rcu $@ ${OBJS}
+	ranlib $@
+
+install:
+	mkdir -p ${DESTDIR}${libdir}
+	install -c -m 644 ${LIB} ${DESTDIR}${libdir}
+
+clean:
+	rm -f *.[oa] errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/librtpalloc/recvmsg_wrap.c	Mon May 27 20:41:21 2024 +0000
@@ -0,0 +1,67 @@
+/*
+ * 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 <themwi/rtp/rtp_alloc_if.h>
+#include <themwi/rtp/rtp_alloc_resp.h>
+
+int collect_rtpmgr_resp(int ctrl_fd, int recv_flags,
+			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;
+}