# HG changeset patch # User Mychaela Falconia # Date 1716842481 0 # Node ID 60b512a868b4b5bfd25f17a5066e91cb8ed8877d # Parent 247f4bbde24ca86cb7ec559d02f69a0e6998c7a2 librtpalloc: port of recvmsg wrapper module diff -r 247f4bbde24c -r 60b512a868b4 include/rtp_alloc_resp.h --- /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 + +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); diff -r 247f4bbde24c -r 60b512a868b4 librtpalloc/Makefile --- /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 diff -r 247f4bbde24c -r 60b512a868b4 librtpalloc/recvmsg_wrap.c --- /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 +#include +#include +#include +#include + +#include +#include + +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; +}