changeset 95:f280328e7e2e

themwi-mgw: initial implementation of PSTN to GSM forwarding
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 25 Sep 2022 19:17:44 -0800
parents 2c22b40408fb
children f24bbfd23c9d
files include/tmgw_const.h mgw/Makefile mgw/ctrl_sock.c mgw/dlcx.c mgw/g711_decode.c mgw/int_defs.h mgw/main.c mgw/mdcx.c mgw/pstn2gsm.c mgw/readconf.c mgw/struct.h
diffstat 11 files changed, 275 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/include/tmgw_const.h	Sat Sep 24 15:14:50 2022 -0800
+++ b/include/tmgw_const.h	Sun Sep 25 19:17:44 2022 -0800
@@ -16,6 +16,9 @@
 #define	TMGW_FWD_MODE_SENDONLY		2
 #define	TMGW_FWD_MODE_SENDRECV		3
 
+#define	TMGW_FWD_ENABLE_PSTN2GSM	1
+#define	TMGW_FWD_ENABLE_GSM2PSTN	2
+
 #define	GSM_TCHF_FRAME			0x0300
 #define	GSM_TCHF_FRAME_EFR		0x0301
 #define	GSM_TCHH_FRAME			0x0302
--- a/mgw/Makefile	Sat Sep 24 15:14:50 2022 -0800
+++ b/mgw/Makefile	Sun Sep 25 19:17:44 2022 -0800
@@ -1,14 +1,15 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	themwi-mgw
-OBJS=	crcx.o ctrl_prot.o ctrl_sock.o dlcx.o main.o mdcx.o readconf.o udpsink.o
+OBJS=	crcx.o ctrl_prot.o ctrl_sock.o dlcx.o g711_decode.o main.o mdcx.o \
+	pstn2gsm.o readconf.o udpsink.o
 LIBS=	../libutil/libutil.a
 INSTBIN=/usr/local/bin
 
 all:	${PROG}
 
 ${PROG}: ${OBJS} ${LIBS}
-	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS} -lgsm
 
 install:
 	install -c -o bin -g bin -m 755 ${PROG} ${INSTBIN}
--- a/mgw/ctrl_sock.c	Sat Sep 24 15:14:50 2022 -0800
+++ b/mgw/ctrl_sock.c	Sun Sep 25 19:17:44 2022 -0800
@@ -9,6 +9,7 @@
 #include <sys/un.h>
 #include <netinet/in.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
--- a/mgw/dlcx.c	Sat Sep 24 15:14:50 2022 -0800
+++ b/mgw/dlcx.c	Sun Sep 25 19:17:44 2022 -0800
@@ -83,6 +83,10 @@
 			free_rtp_end(&ep->rtp_gsm);
 		if (ep->ep_type & TMGW_EP_HAS_PSTN_SOCK)
 			free_rtp_end(&ep->rtp_pstn);
+		if (ep->gsm_encoder_state)
+			free(ep->gsm_encoder_state);
+		if (ep->gsm_decoder_state)
+			free(ep->gsm_decoder_state);
 		free(ep);
 	}
 	delq = 0;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mgw/g711_decode.c	Sun Sep 25 19:17:44 2022 -0800
@@ -0,0 +1,76 @@
+/*
+ * This module contains decoding tables for G.711 PCMU and PCMA,
+ * lifted from the source from libgsm companion 'toast' utility.
+ */
+
+#include <stdint.h>
+
+uint16_t pcmu_decode_table[256] = {
+	33280, 34308, 35336, 36364, 37393, 38421, 39449, 40477,
+	41505, 42534, 43562, 44590, 45618, 46647, 47675, 48703,
+	49474, 49988, 50503, 51017, 51531, 52045, 52559, 53073,
+	53587, 54101, 54616, 55130, 55644, 56158, 56672, 57186,
+	57572, 57829, 58086, 58343, 58600, 58857, 59114, 59371,
+	59628, 59885, 60142, 60399, 60656, 60913, 61171, 61428,
+	61620, 61749, 61877, 62006, 62134, 62263, 62392, 62520,
+	62649, 62777, 62906, 63034, 63163, 63291, 63420, 63548,
+	63645, 63709, 63773, 63838, 63902, 63966, 64030, 64095,
+	64159, 64223, 64287, 64352, 64416, 64480, 64544, 64609,
+	64657, 64689, 64721, 64753, 64785, 64818, 64850, 64882,
+	64914, 64946, 64978, 65010, 65042, 65075, 65107, 65139,
+	65163, 65179, 65195, 65211, 65227, 65243, 65259, 65275,
+	65291, 65308, 65324, 65340, 65356, 65372, 65388, 65404,
+	65416, 65424, 65432, 65440, 65448, 65456, 65464, 65472,
+	65480, 65488, 65496, 65504, 65512, 65520, 65528,     0,
+	32256, 31228, 30200, 29172, 28143, 27115, 26087, 25059,
+	24031, 23002, 21974, 20946, 19918, 18889, 17861, 16833,
+	16062, 15548, 15033, 14519, 14005, 13491, 12977, 12463,
+	11949, 11435, 10920, 10406,  9892,  9378,  8864,  8350,
+	 7964,  7707,  7450,  7193,  6936,  6679,  6422,  6165,
+	 5908,  5651,  5394,  5137,  4880,  4623,  4365,  4108,
+	 3916,  3787,  3659,  3530,  3402,  3273,  3144,  3016,
+	 2887,  2759,  2630,  2502,  2373,  2245,  2116,  1988,
+	 1891,  1827,  1763,  1698,  1634,  1570,  1506,  1441,
+	 1377,  1313,  1249,  1184,  1120,  1056,   992,   927,
+	  879,   847,   815,   783,   751,   718,   686,   654,
+	  622,   590,   558,   526,   494,   461,   429,   397,
+	  373,   357,   341,   325,   309,   293,   277,   261,
+	  245,   228,   212,   196,   180,   164,   148,   132,
+	  120,   112,   104,    96,    88,    80,    72,    64,
+	   56,    48,    40,    32,    24,    16,    8,      0
+};
+
+uint16_t pcma_decode_table[256] = {
+ 60032, 60288, 59520, 59776, 61056, 61312, 60544, 60800,
+ 57984, 58240, 57472, 57728, 59008, 59264, 58496, 58752,
+ 62784, 62912, 62528, 62656, 63296, 63424, 63040, 63168,
+ 61760, 61888, 61504, 61632, 62272, 62400, 62016, 62144,
+ 43520, 44544, 41472, 42496, 47616, 48640, 45568, 46592,
+ 35328, 36352, 33280, 34304, 39424, 40448, 37376, 38400,
+ 54528, 55040, 53504, 54016, 56576, 57088, 55552, 56064,
+ 50432, 50944, 49408, 49920, 52480, 52992, 51456, 51968,
+ 65192, 65208, 65160, 65176, 65256, 65272, 65224, 65240,
+ 65064, 65080, 65032, 65048, 65128, 65144, 65096, 65112,
+ 65448, 65464, 65416, 65432, 65512, 65528, 65480, 65496,
+ 65320, 65336, 65288, 65304, 65384, 65400, 65352, 65368,
+ 64160, 64224, 64032, 64096, 64416, 64480, 64288, 64352,
+ 63648, 63712, 63520, 63584, 63904, 63968, 63776, 63840,
+ 64848, 64880, 64784, 64816, 64976, 65008, 64912, 64944,
+ 64592, 64624, 64528, 64560, 64720, 64752, 64656, 64688,
+  5504,  5248,  6016,  5760,  4480,  4224,  4992,  4736,
+  7552,  7296,  8064,  7808,  6528,  6272,  7040,  6784,
+  2752,  2624,  3008,  2880,  2240,  2112,  2496,  2368,
+  3776,  3648,  4032,  3904,  3264,  3136,  3520,  3392,
+ 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
+ 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
+ 11008, 10496, 12032, 11520,  8960,  8448,  9984,  9472,
+ 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
+   344,   328,   376,   360,   280,   264,   312,   296,
+   472,   456,   504,   488,   408,   392,   440,   424,
+    88,    72,   120,   104,    24,     8,    56,    40,
+   216,   200,   248,   232,   152,   136,   184,   168,
+  1376,  1312,  1504,  1440,  1120,  1056,  1248,  1184,
+  1888,  1824,  2016,  1952,  1632,  1568,  1760,  1696,
+   688,   656,   752,   720,   560,   528,   624,   592,
+   944,   912,  1008,   976,   816,   784,   880,   848
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mgw/int_defs.h	Sun Sep 25 19:17:44 2022 -0800
@@ -0,0 +1,19 @@
+/*
+ * This header file holds miscellaneous internal definitions for themwi-mgw.
+ */
+
+#define	RTP_PACKET_SIZE_PSTN	172
+#define	RTP_PACKET_SIZE_GSM_FR	45
+#define	RTP_PACKET_SIZE_GSM_EFR	43
+#define	RTP_MAX_PAYLOAD		160
+
+struct rtp_packet {
+	uint8_t		v_p_x_cc;
+	uint8_t		m_pt;
+	uint16_t	seq;
+	uint32_t	tstamp;
+	uint32_t	ssrc;
+	uint8_t		payload[RTP_MAX_PAYLOAD];
+};
+
+#define	SAMPLES_PER_FRAME	160
--- a/mgw/main.c	Sat Sep 24 15:14:50 2022 -0800
+++ b/mgw/main.c	Sun Sep 25 19:17:44 2022 -0800
@@ -5,6 +5,7 @@
 #include <sys/types.h>
 #include <sys/errno.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
--- a/mgw/mdcx.c	Sat Sep 24 15:14:50 2022 -0800
+++ b/mgw/mdcx.c	Sun Sep 25 19:17:44 2022 -0800
@@ -22,6 +22,8 @@
 	struct tmgw_ctrl_req *req;
 	struct tmgw_ctrl_resp *resp;
 {
+	int rc;
+
 	if (req->setup_mask & TMGW_CTRL_MASK_GSM_CONN) {
 		if (ep->ep_type != TMGW_EP_TYPE_GATEWAY) {
 			resp->res = TMGW_RESP_ERR_PROT;
@@ -31,6 +33,10 @@
 			resp->res = TMGW_RESP_ERR_PARAM;
 			return(-1);
 		}
+		if (req->gsm_payload_type > 0x7F) {
+			resp->res = TMGW_RESP_ERR_PARAM;
+			return(-1);
+		}
 		switch (req->gsm_payload_msg_type) {
 		case GSM_TCHF_FRAME:
 			break;
@@ -43,6 +49,11 @@
 			resp->res = TMGW_RESP_ERR_PARAM;
 			return(-1);
 		}
+		if (ep->gsm_payload_msg_type &&
+		    ep->gsm_payload_msg_type != req->gsm_payload_msg_type) {
+			resp->res = TMGW_RESP_ERR_PROT;
+			return(-1);
+		}
 		bcopy(&req->gsm_addr, &ep->rtp_gsm.remote_addr,
 			sizeof(struct sockaddr_in));
 		ep->gsm_payload_type = req->gsm_payload_type;
@@ -76,6 +87,14 @@
 			resp->res = TMGW_RESP_ERR_PROT;
 			return(-1);
 		}
+		if ((req->fwd_mode & TMGW_FWD_ENABLE_PSTN2GSM) &&
+		    !(ep->fwd_mode & TMGW_FWD_ENABLE_PSTN2GSM)) {
+			rc = pstn2gsm_init(ep);
+			if (rc != TMGW_RESP_OK) {
+				resp->res = rc;
+				return(-1);
+			}
+		}
 		ep->fwd_mode = req->fwd_mode;
 	}
 	return(0);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mgw/pstn2gsm.c	Sun Sep 25 19:17:44 2022 -0800
@@ -0,0 +1,140 @@
+/*
+ * In this module we implement our RTP gateway function
+ * in the PSTN to GSM direction.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.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 <gsm.h>	/* libgsm dependency */
+#include "../include/tmgw_ctrl.h"
+#include "../include/tmgw_const.h"
+#include "struct.h"
+#include "select.h"
+#include "int_defs.h"
+
+extern uint16_t pcmu_decode_table[256];
+extern uint16_t pcma_decode_table[256];
+
+#define	ERR_WRONG_UDP_SRC	0x0001
+#define	ERR_BAD_RTP_PACKET	0x0002
+#define	ERR_SSRC_CHANGE		0x0004
+#define	ERR_SEQ_BREAK		0x0008
+#define	ERR_TSTAMP_BREAK	0x0010
+
+void
+pstn2gsm_rtp_in(in_fd, ep)
+	struct endpoint *ep;
+{
+	struct rtp_packet pkt;
+	struct sockaddr_in sin_from;
+	socklen_t addrlen;
+	uint16_t *pcm_dec_table;
+	int16_t seq_delta;
+	int32_t ts_delta;
+	int16_t pcm_samples[SAMPLES_PER_FRAME];
+	unsigned n;
+	int rc, m_out;
+
+	addrlen = sizeof(struct sockaddr_in);
+	rc = recvfrom(in_fd, &pkt, sizeof pkt, 0,
+			(struct sockaddr *) &sin_from, &addrlen);
+	if (rc < 0)
+		return;
+	if (sin_from.sin_addr.s_addr != ep->rtp_pstn.remote_addr.sin_addr.s_addr
+	    || sin_from.sin_port != ep->rtp_pstn.remote_addr.sin_port) {
+		if (!(ep->p2g_err_flags & ERR_WRONG_UDP_SRC)) {
+			syslog(LOG_ERR,
+				"PSTN RTP ep got UDP packet from wrong source");
+			ep->p2g_err_flags |= ERR_WRONG_UDP_SRC;
+		}
+		return;
+	}
+	if (rc != RTP_PACKET_SIZE_PSTN) {
+bad_rtp_pkt:	if (!(ep->p2g_err_flags & ERR_BAD_RTP_PACKET)) {
+			syslog(LOG_ERR, "Rx bad RTP packet on PSTN side");
+			ep->p2g_err_flags |= ERR_BAD_RTP_PACKET;
+		}
+		return;
+	}
+	if (pkt.v_p_x_cc != 0x80)
+		goto bad_rtp_pkt;
+	switch (pkt.m_pt & 0x7F) {
+	case PSTN_CODEC_PCMU:
+		pcm_dec_table = pcmu_decode_table;
+		break;
+	case PSTN_CODEC_PCMA:
+		pcm_dec_table = pcma_decode_table;
+		break;
+	default:
+		goto bad_rtp_pkt;
+	}
+	if (ep->p2g_state && pkt.ssrc != ep->p2g_ssrc) {
+		if (!(ep->p2g_err_flags & ERR_SSRC_CHANGE)) {
+			syslog(LOG_ERR, "PSTN RTP stream changed SSRC");
+			ep->p2g_err_flags |= ERR_SSRC_CHANGE;
+		}
+		ep->p2g_state = 0;
+	}
+	if (ep->p2g_state) {
+		seq_delta = ntohs(pkt.seq) - ep->p2g_last_seq;
+		ts_delta = ntohl(pkt.tstamp) - ep->p2g_last_ts;
+		if (seq_delta <= 0)
+			return;	/* discard old or duplicate */
+		if (seq_delta != 1) {
+			if (!(ep->p2g_err_flags & ERR_SEQ_BREAK)) {
+				syslog(LOG_ERR, "PSTN RTP stream seq break");
+				ep->p2g_err_flags |= ERR_SEQ_BREAK;
+			}
+			m_out = 1;
+		} else if (ts_delta != SAMPLES_PER_FRAME) {
+			if (!(ep->p2g_err_flags & ERR_TSTAMP_BREAK)) {
+				syslog(LOG_ERR, "PSTN RTP stream tstamp break");
+				ep->p2g_err_flags |= ERR_TSTAMP_BREAK;
+			}
+			m_out = 1;
+		} else
+			m_out = 0;
+	} else
+		m_out = 1;
+	ep->p2g_state = 1;
+	ep->p2g_ssrc = pkt.ssrc;
+	ep->p2g_last_ts = htonl(pkt.tstamp);
+	ep->p2g_last_seq = htons(pkt.seq);
+	/* actual transcoding and forwarding */
+	for (n = 0; n < SAMPLES_PER_FRAME; n++)
+		pcm_samples[n] = pcm_dec_table[pkt.payload[n]];
+	pkt.m_pt = ep->gsm_payload_type;
+	if (m_out)
+		pkt.m_pt |= 0x80;
+	switch (ep->gsm_payload_msg_type) {
+	case GSM_TCHF_FRAME:
+		gsm_encode(ep->gsm_encoder_state, pcm_samples, pkt.payload);
+		n = RTP_PACKET_SIZE_GSM_FR;
+		break;
+	}
+	addrlen = sizeof(struct sockaddr_in);
+	sendto(ep->rtp_gsm.rtp_fd, &pkt, n, 0,
+		(struct sockaddr *) &ep->rtp_gsm.remote_addr, addrlen);
+}
+
+pstn2gsm_init(ep)
+	struct endpoint *ep;
+{
+	switch (ep->gsm_payload_msg_type) {
+	case GSM_TCHF_FRAME:
+		ep->gsm_encoder_state = gsm_create();
+		if (!ep->gsm_encoder_state)
+			return TMGW_RESP_ERR_RSRC;
+		break;
+	}
+	select_handlers[ep->rtp_pstn.rtp_fd] = pstn2gsm_rtp_in;
+	return TMGW_RESP_OK;
+}
--- a/mgw/readconf.c	Sat Sep 24 15:14:50 2022 -0800
+++ b/mgw/readconf.c	Sun Sep 25 19:17:44 2022 -0800
@@ -10,6 +10,7 @@
 #include <arpa/inet.h>
 #include <ctype.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
--- a/mgw/struct.h	Sat Sep 24 15:14:50 2022 -0800
+++ b/mgw/struct.h	Sun Sep 25 19:17:44 2022 -0800
@@ -25,6 +25,14 @@
 	unsigned	gsm_payload_msg_type;
 	unsigned	pstn_payload_type;
 	unsigned	fwd_mode;
+	/* gateway functionality */
+	void		*gsm_encoder_state;
+	void		*gsm_decoder_state;
+	int		p2g_state;
+	uint32_t	p2g_ssrc;
+	uint32_t	p2g_last_ts;
+	uint16_t	p2g_last_seq;
+	int		p2g_err_flags;
 	/* linked list management */
 	unsigned	ep_id;
 	struct endpoint	*next;