changeset 187:258932879f8b

sip-manual-out: rework for internal RTP handling, using themwi-rtp-mgr
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 16 Mar 2023 23:46:17 -0800
parents 068fce34e565
children 6aecee01cf0a
files Makefile sip-manual-out/Makefile sip-manual-out/dummy_rtp.c sip-manual-out/main.c sip-manual-out/old/dummy_rtp.c sip-manual-out/rtp.c
diffstat 6 files changed, 147 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Mar 12 08:41:10 2023 -0800
+++ b/Makefile	Thu Mar 16 23:46:17 2023 -0800
@@ -12,7 +12,7 @@
 mtctest:	libnumdb libutil
 rtp-mgr:	libutil
 sip-in:		libnumdb libsip libutil
-sip-manual-out:	libsip libutil
+sip-manual-out:	librtpalloc libsip libutil
 sip-out:	liboutrt libsip libutil
 utils:		libnumdb librtpalloc libsip libutil
 
--- a/sip-manual-out/Makefile	Sun Mar 12 08:41:10 2023 -0800
+++ b/sip-manual-out/Makefile	Thu Mar 16 23:46:17 2023 -0800
@@ -1,9 +1,9 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	sip-manual-out
-OBJS=	bye_in.o disc_cmd.o dummy_rtp.o main.o readconf.o reinvite.o sip_log.o \
+OBJS=	bye_in.o disc_cmd.o main.o readconf.o reinvite.o rtp.o sip_log.o \
 	sip_udp.o uac.o uas.o
-LIBS=	../libsip/libsip.a ../libutil/libutil.a
+LIBS=	../libsip/libsip.a ../librtpalloc/librtpalloc.a ../libutil/libutil.a
 INSTBIN=/usr/local/bin
 
 all:	${PROG}
--- a/sip-manual-out/dummy_rtp.c	Sun Mar 12 08:41:10 2023 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,66 +0,0 @@
-/*
- * In this module we implement the code that connects to themwi-mgw
- * and obtains a dummy PSTN-side RTP endpoint.
- */
-
-#include <sys/types.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 "../include/tmgw_ctrl.h"
-#include "../include/tmgw_const.h"
-
-struct sockaddr_in dummy_rtp_endp;
-
-static char tmgw_socket_pathname[] = "/var/gsm/tmgw_socket";
-
-obtain_dummy_rtp()
-{
-	struct sockaddr_un sa;
-	unsigned sa_len;
-	int fd, rc;
-	struct tmgw_ctrl_req req;
-	struct tmgw_ctrl_resp resp;
-
-	fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
-	if (fd < 0) {
-		perror("socket(AF_UNIX, SOCK_SEQPACKET, 0)");
-		exit(1);
-	}
-	fill_sockaddr_un(tmgw_socket_pathname, &sa, &sa_len);
-	rc = connect(fd, (struct sockaddr *) &sa, sa_len);
-	if (rc < 0) {
-		perror(tmgw_socket_pathname);
-		exit(1);
-	}
-	bzero(&req, sizeof req);
-	req.opcode = TMGW_CTRL_OP_CRCX;
-	req.ep_id = TMGW_EP_TYPE_DUMMY_PSTN;
-	rc = send(fd, &req, sizeof req, 0);
-	if (rc < 0) {
-		perror("send to TMGW socket");
-		exit(1);
-	}
-	rc = recv(fd, &resp, sizeof resp, 0);
-	if (rc < 0) {
-		perror("recv from TMGW socket");
-		exit(1);
-	}
-	if (rc != sizeof resp) {
-		fprintf(stderr,
-	"error: response packet from TMGW has wrong length (%d bytes)\n",
-			rc);
-		exit(1);
-	}
-	if (resp.res != TMGW_RESP_OK) {
-		fprintf(stderr, "TMGW CRCX returned error %u\n", resp.res);
-		exit(1);
-	}
-	bcopy(&resp.pstn_addr, &dummy_rtp_endp, sizeof(struct sockaddr_in));
-	return(0);
-}
--- a/sip-manual-out/main.c	Sun Mar 12 08:41:10 2023 -0800
+++ b/sip-manual-out/main.c	Thu Mar 16 23:46:17 2023 -0800
@@ -20,7 +20,8 @@
 extern struct in_addr sip_bind_ip, sip_dest_ip;
 extern unsigned sip_bind_port, sip_dest_port;
 extern char sip_dest_domain[];
-extern struct sockaddr_in dummy_rtp_endp;
+extern struct sockaddr_in rtp_local_addr;
+extern int rtp_udp_fd, rtcp_udp_fd;
 
 struct sockaddr_in sip_dest_sin;
 char from_uri[128], to_uri[128], call_id[128];
@@ -55,8 +56,8 @@
 	if (rc < 0)
 		goto msg_size_err;
 	bzero(&sdp, sizeof sdp);
-	sdp.conn_ip = dummy_rtp_endp.sin_addr;
-	sdp.conn_port = ntohs(dummy_rtp_endp.sin_port);
+	sdp.conn_ip = rtp_local_addr.sin_addr;
+	sdp.conn_port = ntohs(rtp_local_addr.sin_port);
 	if (pcma_codec_force)
 		sdp.codec_mask = SDP_CODEC_MASK_PCMA;
 	else {
@@ -111,12 +112,12 @@
 		goto usage;
 	read_config_file(argv[optind]);
 	open_sip_udp_socket();
-	obtain_dummy_rtp();
+	obtain_rtp_endp();
 	sip_dest_sin.sin_family = AF_INET;
 	sip_dest_sin.sin_addr = sip_dest_ip;
 	sip_dest_sin.sin_port = htons(sip_dest_port);
 	sprintf(from_uri, "<sip:%s@%s>;tag=out%u", argv[optind+1],
-		inet_ntoa(sip_bind_ip), ntohs(dummy_rtp_endp.sin_port));
+		inet_ntoa(sip_bind_ip), ntohs(rtp_local_addr.sin_port));
 	sprintf(to_uri, "sip:%s@%s", argv[optind+2], sip_dest_domain);
 	if (logfile) {
 		rc = open_sip_log_file(logfile);
@@ -129,20 +130,27 @@
 	char **argv;
 {
 	fd_set fds;
-	int rc;
+	int rc, max_fd;
 
 	preliminary_proc(argc, argv);
 	gettimeofday(&cur_event_time, 0);
 	sprintf(call_id, "%08u_%u@%s",
 		(unsigned)(cur_event_time.tv_sec % 100000000),
-		ntohs(dummy_rtp_endp.sin_port), inet_ntoa(sip_bind_ip));
+		ntohs(rtp_local_addr.sin_port), inet_ntoa(sip_bind_ip));
 	send_invite_req();
 	/* main select loop */
+	max_fd = sip_socket;
+	if (rtp_udp_fd > max_fd)
+		max_fd = rtp_udp_fd;
+	if (rtcp_udp_fd > max_fd)
+		max_fd = rtcp_udp_fd;
 	for (;;) {
 		FD_ZERO(&fds);
 		FD_SET(0, &fds);
 		FD_SET(sip_socket, &fds);
-		rc = select(sip_socket+1, &fds, 0, 0, 0);
+		FD_SET(rtp_udp_fd, &fds);
+		FD_SET(rtcp_udp_fd, &fds);
+		rc = select(max_fd+1, &fds, 0, 0, 0);
 		if (rc < 0) {
 			if (errno == EINTR)
 				continue;
@@ -154,5 +162,9 @@
 			select_stdin();
 		if (FD_ISSET(sip_socket, &fds))
 			sip_socket_select();
+		if (FD_ISSET(rtp_udp_fd, &fds))
+			rtp_rx_select();
+		if (FD_ISSET(rtcp_udp_fd, &fds))
+			rtcp_rx_select();
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-manual-out/old/dummy_rtp.c	Thu Mar 16 23:46:17 2023 -0800
@@ -0,0 +1,66 @@
+/*
+ * In this module we implement the code that connects to themwi-mgw
+ * and obtains a dummy PSTN-side RTP endpoint.
+ */
+
+#include <sys/types.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 "../include/tmgw_ctrl.h"
+#include "../include/tmgw_const.h"
+
+struct sockaddr_in dummy_rtp_endp;
+
+static char tmgw_socket_pathname[] = "/var/gsm/tmgw_socket";
+
+obtain_dummy_rtp()
+{
+	struct sockaddr_un sa;
+	unsigned sa_len;
+	int fd, rc;
+	struct tmgw_ctrl_req req;
+	struct tmgw_ctrl_resp resp;
+
+	fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+	if (fd < 0) {
+		perror("socket(AF_UNIX, SOCK_SEQPACKET, 0)");
+		exit(1);
+	}
+	fill_sockaddr_un(tmgw_socket_pathname, &sa, &sa_len);
+	rc = connect(fd, (struct sockaddr *) &sa, sa_len);
+	if (rc < 0) {
+		perror(tmgw_socket_pathname);
+		exit(1);
+	}
+	bzero(&req, sizeof req);
+	req.opcode = TMGW_CTRL_OP_CRCX;
+	req.ep_id = TMGW_EP_TYPE_DUMMY_PSTN;
+	rc = send(fd, &req, sizeof req, 0);
+	if (rc < 0) {
+		perror("send to TMGW socket");
+		exit(1);
+	}
+	rc = recv(fd, &resp, sizeof resp, 0);
+	if (rc < 0) {
+		perror("recv from TMGW socket");
+		exit(1);
+	}
+	if (rc != sizeof resp) {
+		fprintf(stderr,
+	"error: response packet from TMGW has wrong length (%d bytes)\n",
+			rc);
+		exit(1);
+	}
+	if (resp.res != TMGW_RESP_OK) {
+		fprintf(stderr, "TMGW CRCX returned error %u\n", resp.res);
+		exit(1);
+	}
+	bcopy(&resp.pstn_addr, &dummy_rtp_endp, sizeof(struct sockaddr_in));
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sip-manual-out/rtp.c	Thu Mar 16 23:46:17 2023 -0800
@@ -0,0 +1,58 @@
+/*
+ * In this module we implement our RTP handling: obtaining a PSTN-side
+ * RTP endpoint from themwi-rtp-mgr, then handling read select on RTP
+ * and RTCP UDP sockets.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "../include/tmgw_const.h"
+#include "../librtpalloc/rtp_alloc_simple.h"
+
+struct sockaddr_in rtp_local_addr;
+int rtp_udp_fd, rtcp_udp_fd;
+
+static int got_some_rtp, got_some_rtcp;
+
+void
+obtain_rtp_endp()
+{
+	int rc;
+	struct rtp_alloc_simple res;
+
+	rc = rtp_alloc_simple(TMGW_EP_TYPE_PSTN_ONLY, &res);
+	if (rc < 0)
+		exit(1);	/* error msg already printed */
+	bcopy(&res.pstn_addr, &rtp_local_addr, sizeof(struct sockaddr_in));
+	rtp_udp_fd = res.pstn_rtp_fd;
+	rtcp_udp_fd = res.pstn_rtcp_fd;
+}
+
+void
+rtp_rx_select()
+{
+	u_char buf[512];
+
+	recv(rtp_udp_fd, buf, sizeof buf, 0);
+	if (!got_some_rtp) {
+		printf("Got some RTP\n");
+		got_some_rtp = 1;
+	}
+}
+
+void
+rtcp_rx_select()
+{
+	u_char buf[512];
+
+	recv(rtcp_udp_fd, buf, sizeof buf, 0);
+	if (!got_some_rtcp) {
+		printf("Got some RTCP\n");
+		got_some_rtcp = 1;
+	}
+}