changeset 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 3962d9345a09
children 857d78c58f56
files Makefile librtpalloc/Makefile librtpalloc/rtp_alloc_simple.c librtpalloc/rtp_alloc_simple.h librtpalloc/rtpmgr_resp.c librtpalloc/rtpmgr_resp.h
diffstat 6 files changed, 229 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sat Mar 11 21:48:23 2023 -0800
+++ b/Makefile	Sat Mar 11 23:48:14 2023 -0800
@@ -2,7 +2,7 @@
 CFLAGS=	-O2
 
 PROGDIR=mgw mncc mtctest rtp-mgr sip-in sip-manual-out sip-out utils
-LIBDIR=	libnumdb liboutrt libsip libutil
+LIBDIR=	libnumdb liboutrt librtpalloc libsip libutil
 SUBDIR=	${PROGDIR} ${LIBDIR}
 
 all:	${SUBDIR}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/librtpalloc/Makefile	Sat Mar 11 23:48:14 2023 -0800
@@ -0,0 +1,13 @@
+CC=	gcc
+CFLAGS=	-O2
+OBJS=	rtp_alloc_simple.o rtpmgr_resp.o
+LIB=	librtpalloc.a
+
+all:	${LIB}
+
+${LIB}:	${OBJS}
+	ar rcu $@ ${OBJS}
+	ranlib $@
+
+clean:
+	rm -f *.[oa] errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/librtpalloc/rtp_alloc_simple.c	Sat Mar 11 23:48:14 2023 -0800
@@ -0,0 +1,124 @@
+/*
+ * The library function implemented in this C module provides a
+ * simple interface for obtaining a single RTP endpoint from
+ * themwi-rtp-mgr.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "../include/tmgw_const.h"
+#include "../include/rtp_alloc.h"
+#include "rtpmgr_resp.h"
+#include "rtp_alloc_simple.h"
+
+static char ctrl_socket_pathname[] = "/var/gsm/rtp_alloc_socket";
+
+static void
+close_fds(resp)
+	struct rtp_alloc_resp_wrap *resp;
+{
+	unsigned n;
+
+	for (n = 0; n < resp->num_fd; n++)
+		close(resp->fd_buf[n]);
+}
+
+rtp_alloc_simple(ep_type, out)
+	int ep_type;
+	struct rtp_alloc_simple *out;
+{
+	struct sockaddr_un sa;
+	unsigned sa_len;
+	int ctrl_fd, rc;
+	struct rtp_alloc_req req;
+	struct rtp_alloc_resp_wrap resp;
+	unsigned expect_num_fd;
+
+	switch (ep_type) {
+	case TMGW_EP_TYPE_GSM_ONLY:
+	case TMGW_EP_TYPE_PSTN_ONLY:
+		expect_num_fd = 2;
+		break;
+	case TMGW_EP_TYPE_GATEWAY:
+		expect_num_fd = 4;
+		break;
+	default:
+		fprintf(stderr,
+			"rtp_alloc_simple() error: unknown EP type %d\n",
+			ep_type);
+		return(-1);
+	}
+	ctrl_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+	if (ctrl_fd < 0) {
+		perror("socket(AF_UNIX, SOCK_SEQPACKET, 0)");
+		return(-1);
+	}
+	fill_sockaddr_un(ctrl_socket_pathname, &sa, &sa_len);
+	rc = connect(ctrl_fd, (struct sockaddr *) &sa, sa_len);
+	if (rc < 0) {
+		perror(ctrl_socket_pathname);
+		close(ctrl_fd);
+		return(-1);
+	}
+	bzero(&req, sizeof req);
+	req.ep_type = ep_type;
+	rc = send(ctrl_fd, &req, sizeof req, 0);
+	if (rc < 0) {
+		perror("send to RTP allocator socket");
+		close(ctrl_fd);
+		return(-1);
+	}
+	rc = collect_rtpmgr_resp(ctrl_fd, 0, &resp);
+	if (rc < 0) {
+		perror("recvmsg from RTP allocator socket");
+		close(ctrl_fd);
+		return(-1);
+	}
+	close(ctrl_fd);
+	if (resp.resp_len != sizeof(struct rtp_alloc_resp)) {
+		fprintf(stderr,
+"error: response packet from themwi-rtp-mgr has wrong length (%u bytes)\n",
+			resp.resp_len);
+		close_fds(&resp);
+		return(-1);
+	}
+	if (resp.resp.res != RTP_ALLOC_OK) {
+		fprintf(stderr, "themwi-rtp-mgr returned error %u\n",
+			resp.resp.res);
+		close_fds(&resp);
+		return(-1);
+	}
+	if (resp.num_fd != expect_num_fd) {
+		fprintf(stderr,
+"error: themwi-rtp-mgr returned %u descriptors instead of expected %u\n",
+			resp.num_fd, expect_num_fd);
+		close_fds(&resp);
+		return(-1);
+	}
+	switch (ep_type) {
+	case TMGW_EP_TYPE_GSM_ONLY:
+		out->gsm_rtp_fd = resp.fd_buf[0];
+		out->gsm_rtcp_fd = resp.fd_buf[1];
+		break;
+	case TMGW_EP_TYPE_PSTN_ONLY:
+		out->pstn_rtp_fd = resp.fd_buf[0];
+		out->pstn_rtcp_fd = resp.fd_buf[1];
+		break;
+	case TMGW_EP_TYPE_GATEWAY:
+		out->gsm_rtp_fd = resp.fd_buf[0];
+		out->gsm_rtcp_fd = resp.fd_buf[1];
+		out->pstn_rtp_fd = resp.fd_buf[2];
+		out->pstn_rtcp_fd = resp.fd_buf[3];
+	}
+	bcopy(&resp.resp.gsm_addr, &out->gsm_addr,
+		sizeof(struct sockaddr_storage));
+	bcopy(&resp.resp.pstn_addr, &out->pstn_addr,
+		sizeof(struct sockaddr_storage));
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/librtpalloc/rtp_alloc_simple.h	Sat Mar 11 23:48:14 2023 -0800
@@ -0,0 +1,13 @@
+/*
+ * This header file defines the library interface for "simple"
+ * RTP endpoint allocation.
+ */
+
+struct rtp_alloc_simple {
+	struct sockaddr_storage gsm_addr;
+	int	gsm_rtp_fd;
+	int	gsm_rtcp_fd;
+	struct sockaddr_storage pstn_addr;
+	int	pstn_rtp_fd;
+	int	pstn_rtcp_fd;
+};
--- /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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/librtpalloc/rtpmgr_resp.h	Sat Mar 11 23:48:14 2023 -0800
@@ -0,0 +1,12 @@
+/*
+ * The structure defined in this header file is returned by the
+ * collect_rtpmgr_resp() function, which is a wrapper around
+ * the mess of recvmsg with file descriptor passing.
+ */
+
+struct rtp_alloc_resp_wrap {
+	struct rtp_alloc_resp resp;
+	unsigned	resp_len;
+	int		fd_buf[4];
+	unsigned	num_fd;
+};