changeset 179:b79d6334f543

themwi-rtp-mgr: RTP port allocation split out of themwi-mgw
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Mar 2023 20:19:14 -0800
parents b259e2722485
children 565477d07418
files .hgignore include/rtp_alloc.h rtp-mgr/Makefile rtp-mgr/alloc.c rtp-mgr/ctrl_prot.c rtp-mgr/ctrl_sock.c rtp-mgr/main.c rtp-mgr/readconf.c rtp-mgr/select.h rtp-mgr/struct.h
diffstat 10 files changed, 583 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Mar 09 13:08:31 2023 -0800
+++ b/.hgignore	Sat Mar 11 20:19:14 2023 -0800
@@ -8,6 +8,8 @@
 
 ^mtctest/themwi-test-mtc$
 
+^rtp-mgr/themwi-rtp-mgr$
+
 ^sip-in/themwi-sip-in$
 
 ^sip-manual-out/sip-manual-out$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/rtp_alloc.h	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,20 @@
+/*
+ * This header file defines the ad hoc control interface
+ * to themwi-rtp-mgr over a dedicated local socket.
+ */
+
+struct rtp_alloc_req {
+	uint32_t	transact_ref;
+	uint32_t	ep_type;
+};
+
+struct rtp_alloc_resp {
+	uint32_t	transact_ref;
+	uint32_t	res;
+	struct sockaddr_storage gsm_addr;
+	struct sockaddr_storage pstn_addr;
+};
+
+#define	RTP_ALLOC_OK		0
+#define	RTP_ALLOC_ERR_PARAM	1
+#define	RTP_ALLOC_ERR_RSRC	2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp-mgr/Makefile	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,17 @@
+CC=	gcc
+CFLAGS=	-O2
+PROG=	themwi-rtp-mgr
+OBJS=	alloc.o ctrl_prot.o ctrl_sock.o main.o readconf.o
+LIBS=	../libutil/libutil.a
+INSTBIN=/usr/local/bin
+
+all:	${PROG}
+
+${PROG}: ${OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS}
+
+install:
+	install -c -o bin -g bin -m 755 ${PROG} ${INSTBIN}
+
+clean:
+	rm -f *.o ${PROG} errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp-mgr/alloc.c	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,66 @@
+/*
+ * In this module we implement our RTP port allocation operation.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <syslog.h>
+#include <unistd.h>
+#include "../include/tmgw_const.h"
+#include "struct.h"
+#include "select.h"
+
+get_rtp_port_pair(roe, brc)
+	struct rtp_one_end *roe;
+	struct bind_range_cfg *brc;
+{
+	struct sockaddr_in sin;
+	unsigned tries, rtp_port;
+	int rc;
+
+	sin.sin_family = AF_INET;
+	sin.sin_addr = brc->bind_ip;
+	for (tries = brc->port_tries; tries; tries--) {
+		rtp_port = brc->port_next;
+		brc->port_next += 2;
+		if (brc->port_next >= brc->port_range_end)
+			brc->port_next = brc->port_range_start;
+		sin.sin_port = htons(rtp_port);
+		roe->rtp_fd = socket(AF_INET, SOCK_DGRAM, 0);
+		if (roe->rtp_fd < 0) {
+			syslog(LOG_CRIT, "socket(AF_INET, SOCK_DGRAM, 0): %m");
+			return(-1);
+		}
+		rc = bind(roe->rtp_fd, (struct sockaddr *) &sin, sizeof sin);
+		if (rc < 0) {
+			close(roe->rtp_fd);
+			continue;
+		}
+		bcopy(&sin, &roe->bound_addr, sizeof(struct sockaddr_in));
+		sin.sin_port = htons(rtp_port+1);
+		roe->rtcp_fd = socket(AF_INET, SOCK_DGRAM, 0);
+		if (roe->rtcp_fd < 0) {
+			syslog(LOG_CRIT, "socket(AF_INET, SOCK_DGRAM, 0): %m");
+			close(roe->rtp_fd);
+			return(-1);
+		}
+		rc = bind(roe->rtcp_fd, (struct sockaddr *) &sin, sizeof sin);
+		if (rc < 0) {
+			close(roe->rtp_fd);
+			close(roe->rtcp_fd);
+			continue;
+		}
+		/* all good! */
+		return(0);
+	}
+	/* couldn't find a free port pair */
+	return(-1);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp-mgr/ctrl_prot.c	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,130 @@
+/*
+ * In this module we implement our control socket protocol.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <syslog.h>
+#include <unistd.h>
+#include "../include/tmgw_const.h"
+#include "../include/rtp_alloc.h"
+#include "struct.h"
+#include "select.h"
+
+extern struct bind_range_cfg bind_range_gsm, bind_range_pstn;
+
+void
+free_rtp_end(roe)
+	struct rtp_one_end *roe;
+{
+	close(roe->rtp_fd);
+	close(roe->rtcp_fd);
+}
+
+void
+ctrl_message_handler(fd)
+{
+	struct rtp_alloc_req req;
+	struct rtp_alloc_resp resp;
+	struct rtp_one_end rtp_gsm, rtp_pstn;
+	struct iovec iov;
+	struct msghdr msg;
+	int fd_out[4], num_fd, *fd_bufptr;
+	union {
+		char buf[CMSG_SPACE(sizeof fd_out)];
+		struct cmsghdr align;
+	} cmsgu;
+	struct cmsghdr *cmsg;
+	int rc;
+
+	/* receive request */
+	rc = recv(fd, &req, sizeof req, 0);
+	if (rc < sizeof req) {
+		syslog(LOG_DEBUG, "ctrl connection closing");
+		close(fd);
+		FD_CLR(fd, &select_for_read);
+		return;
+	}
+	/* start preparing response */
+	bzero(&resp, sizeof resp);
+	resp.transact_ref = req.transact_ref;
+	switch (req.ep_type) {
+	case TMGW_EP_TYPE_DUMMY_GSM:
+	case TMGW_EP_TYPE_DUMMY_PSTN:
+	case TMGW_EP_TYPE_GATEWAY:
+		break;
+	default:
+		resp.res = RTP_ALLOC_ERR_PARAM;
+error_resp:	send(fd, &resp, sizeof resp, 0);
+		return;
+	}
+	/* allocate resources */
+	if (req.ep_type & TMGW_EP_HAS_GSM_SOCK) {
+		rc = get_rtp_port_pair(&rtp_gsm, &bind_range_gsm);
+		if (rc < 0) {
+			syslog(LOG_ERR,
+				"unable to get local port pair on GSM side");
+			resp.res = RTP_ALLOC_ERR_RSRC;
+			goto error_resp;
+		}
+	}
+	if (req.ep_type & TMGW_EP_HAS_PSTN_SOCK) {
+		rc = get_rtp_port_pair(&rtp_pstn, &bind_range_pstn);
+		if (rc < 0) {
+			syslog(LOG_ERR,
+				"unable to get local port pair on PSTN side");
+			if (req.ep_type & TMGW_EP_HAS_GSM_SOCK)
+				free_rtp_end(&rtp_gsm);
+			resp.res = RTP_ALLOC_ERR_RSRC;
+			goto error_resp;
+		}
+	}
+	/* finish ordinary body of response */
+	resp.res = RTP_ALLOC_OK;
+	if (req.ep_type & TMGW_EP_HAS_GSM_SOCK)
+		bcopy(&rtp_gsm.bound_addr, &resp.gsm_addr,
+			sizeof(struct sockaddr_in));
+	if (req.ep_type & TMGW_EP_HAS_PSTN_SOCK)
+		bcopy(&rtp_pstn.bound_addr, &resp.pstn_addr,
+			sizeof(struct sockaddr_in));
+	iov.iov_base = &resp;
+	iov.iov_len = sizeof resp;
+	/* file descriptor passing voodoo */
+	switch (req.ep_type) {
+	case TMGW_EP_TYPE_DUMMY_GSM:
+		num_fd = 2;
+		fd_out[0] = rtp_gsm.rtp_fd;
+		fd_out[1] = rtp_gsm.rtcp_fd;
+		break;
+	case TMGW_EP_TYPE_DUMMY_PSTN:
+		num_fd = 2;
+		fd_out[0] = rtp_pstn.rtp_fd;
+		fd_out[1] = rtp_pstn.rtcp_fd;
+		break;
+	case TMGW_EP_TYPE_GATEWAY:
+		num_fd = 4;
+		fd_out[0] = rtp_gsm.rtp_fd;
+		fd_out[1] = rtp_gsm.rtcp_fd;
+		fd_out[2] = rtp_pstn.rtp_fd;
+		fd_out[3] = rtp_pstn.rtcp_fd;
+	}
+	bzero(&msg, sizeof msg);
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = cmsgu.buf;
+	msg.msg_controllen = CMSG_SPACE(sizeof(int) * num_fd);
+	cmsg = CMSG_FIRSTHDR(&msg);
+	cmsg->cmsg_level = SOL_SOCKET;
+	cmsg->cmsg_type = SCM_RIGHTS;
+	cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fd);
+	fd_bufptr = (int *) CMSG_DATA(cmsg);
+	bcopy(fd_out, fd_bufptr, sizeof(int) * num_fd);
+	sendmsg(fd, &msg, 0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp-mgr/ctrl_sock.c	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,72 @@
+/*
+ * In this module we implement the logic of listening on the RTP
+ * allocator control socket and accepting control connections.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <syslog.h>
+#include <unistd.h>
+#include "struct.h"
+#include "select.h"
+
+static char ctrl_socket_pathname[] = "/var/gsm/rtp_alloc_socket";
+
+extern void ctrl_message_handler();
+
+void
+ctrlsock_accept_handler(listener_fd)
+{
+	struct sockaddr_un sa;
+	socklen_t sa_len;
+	int conn_fd;
+
+	sa_len = sizeof sa;
+	conn_fd = accept(listener_fd, (struct sockaddr *) &sa, &sa_len);
+	if (conn_fd < 0) {
+		syslog(LOG_CRIT, "accept on UNIX socket: %m");
+		exit(1);
+	}
+	update_max_fd(conn_fd);
+	FD_SET(conn_fd, &select_for_read);
+	select_handlers[conn_fd] = ctrl_message_handler;
+	syslog(LOG_DEBUG, "accepted ctrl connection");
+}
+
+create_ctrl_socket()
+{
+	struct sockaddr_un sa;
+	unsigned sa_len;
+	int fd, rc;
+
+	fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+	if (fd < 0) {
+		syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
+		return(-1);
+	}
+	unlink(ctrl_socket_pathname);
+	fill_sockaddr_un(ctrl_socket_pathname, &sa, &sa_len);
+	rc = bind(fd, (struct sockaddr *) &sa, sa_len);
+	if (rc < 0) {
+		syslog(LOG_ERR, "bind to %s: %m", ctrl_socket_pathname);
+		return(-1);
+	}
+	rc = listen(fd, 3);
+	if (rc < 0) {
+		syslog(LOG_CRIT, "listen on UNIX socket: %m");
+		return(-1);
+	}
+	chmod(ctrl_socket_pathname, 0775);
+	update_max_fd(fd);
+	FD_SET(fd, &select_for_read);
+	select_handlers[fd] = ctrlsock_accept_handler;
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp-mgr/main.c	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,65 @@
+/*
+ * Main module for themwi-rtp-mgr.
+ */
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <signal.h>
+#include <syslog.h>
+#include <unistd.h>
+
+fd_set select_for_read;
+void (*select_handlers[FD_SETSIZE])();
+void *select_data[FD_SETSIZE];
+
+static int max_fd;
+
+update_max_fd(newfd)
+{
+	if (newfd >= FD_SETSIZE) {
+		syslog(LOG_CRIT, "FATAL: file descriptor %d >= FD_SETSIZE",
+			newfd);
+		exit(1);
+	}
+	if (newfd > max_fd)
+		max_fd = newfd;
+}
+
+main(argc, argv)
+	char **argv;
+{
+	fd_set fds;
+	int cc, i;
+
+	openlog("themwi-rtp-mgr", 0, LOG_LOCAL5);
+	read_config_file();
+	if (create_ctrl_socket() < 0) {
+		fprintf(stderr,
+			"error creating RTP allocator control socket\n");
+		exit(1);
+	}
+	signal(SIGPIPE, SIG_IGN);
+	/* main select loop */
+	for (;;) {
+		bcopy(&select_for_read, &fds, sizeof(fd_set));
+		cc = select(max_fd+1, &fds, 0, 0, 0);
+		if (cc < 0) {
+			if (errno == EINTR)
+				continue;
+			syslog(LOG_CRIT, "select: %m");
+			exit(1);
+		}
+		for (i = 0; cc && i <= max_fd; i++) {
+			if (FD_ISSET(i, &fds)) {
+				select_handlers[i](i, select_data[i]);
+				cc--;
+			}
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp-mgr/readconf.c	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,189 @@
+/*
+ * In this module we implement the reading of /var/gsm/themwi-rtp.cfg:
+ * we parse and save the configured IP address and port range for each
+ * of our two sides, GSM and PSTN.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "struct.h"
+
+struct bind_range_cfg bind_range_gsm, bind_range_pstn;
+
+static char config_file_pathname[] = "/var/gsm/themwi-rtp.cfg";
+
+struct parse_state {
+	int lineno;
+	int set_mask;
+};
+
+static void
+handle_bind_ip(st, kw, brc, line)
+	struct parse_state *st;
+	char *kw, *line;
+	struct bind_range_cfg *brc;
+{
+	char *cp, *np;
+
+	for (cp = line; isspace(*cp); cp++)
+		;
+	if (*cp == '\0' || *cp == '#') {
+inv_syntax:	fprintf(stderr,
+			"%s line %d: %s setting requires one argument\n",
+			config_file_pathname, st->lineno, kw);
+		exit(1);
+	}
+	for (np = cp; *cp && !isspace(*cp); cp++)
+		;
+	if (*cp)
+		*cp++ = '\0';
+	while (isspace(*cp))
+		cp++;
+	if (*cp != '\0' && *cp != '#')
+		goto inv_syntax;
+	brc->bind_ip.s_addr = inet_addr(np);
+	if (brc->bind_ip.s_addr == INADDR_NONE) {
+		fprintf(stderr,
+			"%s line %d: invalid IP address argument \"%s\"\n",
+			config_file_pathname, st->lineno, np);
+		exit(1);
+	}
+}
+
+static void
+handle_port_range(st, kw, brc, line)
+	struct parse_state *st;
+	char *kw, *line;
+	struct bind_range_cfg *brc;
+{
+	char *cp, *np1, *np2;
+
+	for (cp = line; isspace(*cp); cp++)
+		;
+	if (!isdigit(*cp)) {
+inv_syntax:	fprintf(stderr,
+		"%s line %d: %s setting requires two numeric arguments\n",
+			config_file_pathname, st->lineno, kw);
+		exit(1);
+	}
+	for (np1 = cp; isdigit(*cp); cp++)
+		;
+	if (!isspace(*cp))
+		goto inv_syntax;
+	while (isspace(*cp))
+		cp++;
+	if (!isdigit(*cp))
+		goto inv_syntax;
+	for (np2 = cp; isdigit(*cp); cp++)
+		;
+	if (*cp && !isspace(*cp))
+		goto inv_syntax;
+	while (isspace(*cp))
+		cp++;
+	if (*cp != '\0' && *cp != '#')
+		goto inv_syntax;
+	brc->port_range_start = atoi(np1);
+	brc->port_range_end = atoi(np2);
+	if (brc->port_range_start & 1) {
+		fprintf(stderr, "%s line %d: start port must be even\n",
+			config_file_pathname, st->lineno);
+		exit(1);
+	}
+	if (!(brc->port_range_end & 1)) {
+		fprintf(stderr, "%s line %d: end port must be odd\n",
+			config_file_pathname, st->lineno);
+		exit(1);
+	}
+	if (brc->port_range_end <= brc->port_range_start) {
+		fprintf(stderr,
+		"%s line %d: end port must be greater than start port\n",
+			config_file_pathname, st->lineno);
+		exit(1);
+	}
+	brc->port_next = brc->port_range_start;
+	brc->port_tries = (brc->port_range_end - brc->port_range_start + 1) / 2;
+}
+
+static void
+process_line(st, line)
+	struct parse_state *st;
+	char *line;
+{
+	char *cp, *np;
+	void (*handler)();
+	struct bind_range_cfg *ipside;
+	int set_id;
+
+	if (!index(line, '\n')) {
+		fprintf(stderr, "%s line %d: too long or missing newline\n",
+			config_file_pathname, st->lineno);
+		exit(1);
+	}
+	for (cp = line; isspace(*cp); cp++)
+		;
+	if (*cp == '\0' || *cp == '#')
+		return;
+	for (np = cp; *cp && !isspace(*cp); cp++)
+		;
+	if (*cp)
+		*cp++ = '\0';
+	if (!strcmp(np, "gsm-ip-addr")) {
+		handler = handle_bind_ip;
+		ipside = &bind_range_gsm;
+		set_id = 1;
+	} else if (!strcmp(np, "gsm-port-range")) {
+		handler = handle_port_range;
+		ipside = &bind_range_gsm;
+		set_id = 2;
+	} else if (!strcmp(np, "pstn-ip-addr")) {
+		handler = handle_bind_ip;
+		ipside = &bind_range_pstn;
+		set_id = 4;
+	} else if (!strcmp(np, "pstn-port-range")) {
+		handler = handle_port_range;
+		ipside = &bind_range_pstn;
+		set_id = 8;
+	} else {
+		fprintf(stderr, "%s line %d: non-understood keyword \"%s\"\n",
+			config_file_pathname, st->lineno, np);
+		exit(1);
+	}
+	if (st->set_mask & set_id) {
+		fprintf(stderr, "%s line %d: duplicate %s setting\n",
+			config_file_pathname, st->lineno, np);
+		exit(1);
+	}
+	handler(st, np, ipside, cp);
+	st->set_mask |= set_id;
+}
+
+read_config_file()
+{
+	FILE *inf;
+	struct parse_state pst;
+	char linebuf[256];
+
+	inf = fopen(config_file_pathname, "r");
+	if (!inf) {
+		perror(config_file_pathname);
+		exit(1);
+	}
+	pst.set_mask = 0;
+	for (pst.lineno = 1; fgets(linebuf, sizeof linebuf, inf); pst.lineno++)
+		process_line(&pst, linebuf);
+	fclose(inf);
+	if (pst.set_mask != 15) {
+		fprintf(stderr, "error: %s did not set all required settings\n",
+			config_file_pathname);
+		exit(1);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp-mgr/select.h	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,5 @@
+/* extern declarations for select loop global variables */
+
+extern fd_set select_for_read;
+extern void (*select_handlers[])();
+extern void *select_data[];
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtp-mgr/struct.h	Sat Mar 11 20:19:14 2023 -0800
@@ -0,0 +1,17 @@
+/*
+ * This header file defines internal data structures for themwi-rtp-mgr.
+ */
+
+struct bind_range_cfg {
+	struct in_addr	bind_ip;
+	unsigned	port_range_start;
+	unsigned	port_range_end;
+	unsigned	port_next;
+	unsigned	port_tries;
+};
+
+struct rtp_one_end {
+	int	rtp_fd;
+	int	rtcp_fd;
+	struct sockaddr_in bound_addr;
+};