view librtpalloc/recvmsg_wrap.c @ 8:7e2b24fc097b

add README and INSTALL
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 27 May 2024 22:09:00 +0000
parents 60b512a868b4
children
line wrap: on
line source

/*
 * 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;
}