annotate mgw/mdcx.c @ 188:6aecee01cf0a

sip-manual-out: add RTP stream continuity analysis
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 17 Mar 2023 01:14:57 -0800
parents c985c33baeac
children 0047c4c08d9e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we implement our MDCX operation.
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/socket.h>
127
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents: 103
diff changeset
7 #include <sys/time.h>
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <netinet/in.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
38
020ba624bdd8 mgw source: #include <stdlib.h> in files using tmgw_ctrl.h
Mychaela Falconia <falcon@freecalypso.org>
parents: 34
diff changeset
10 #include <stdint.h>
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <strings.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <syslog.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "../include/tmgw_ctrl.h"
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include "../include/tmgw_const.h"
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include "struct.h"
100
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
18 #include "int_defs.h"
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 extern struct endpoint *find_ep_by_id();
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
100
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
22 static void
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
23 gsm_vars_init(ep)
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
24 struct endpoint *ep;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
25 {
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
26 switch (ep->gsm_payload_msg_type) {
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
27 case GSM_TCHF_FRAME:
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
28 ep->gsm_rtp_pkt_size = RTP_PACKET_SIZE_GSM_FR;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
29 ep->gsm_payload_magic = 0xD0;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
30 return;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
31 case GSM_TCHF_FRAME_EFR:
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
32 ep->gsm_rtp_pkt_size = RTP_PACKET_SIZE_GSM_EFR;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
33 ep->gsm_payload_magic = 0xC0;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
34 return;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
35 }
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
36 }
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
37
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
38 mdcx_operation(ep, req)
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 struct endpoint *ep;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 struct tmgw_ctrl_req *req;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 {
95
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
42 int rc;
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
43
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (req->setup_mask & TMGW_CTRL_MASK_GSM_CONN) {
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
45 if (ep->ep_type != TMGW_EP_TYPE_GATEWAY)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
46 return TMGW_RESP_ERR_PROT;
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
47 if (req->gsm_addr.ss_family != AF_INET)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
48 return TMGW_RESP_ERR_PARAM;
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
49 if (req->gsm_payload_type > 0x7F)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
50 return TMGW_RESP_ERR_PARAM;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
51 switch (req->gsm_payload_msg_type) {
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
52 case GSM_TCHF_FRAME:
174
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
53 case GSM_TCHF_FRAME_EFR:
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
54 break;
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
55 case GSM_TCHH_FRAME:
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
56 case GSM_TCH_FRAME_AMR:
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
57 return TMGW_RESP_ERR_NOTSUP;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
58 default:
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
59 return TMGW_RESP_ERR_PARAM;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
60 }
95
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
61 if (ep->gsm_payload_msg_type &&
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
62 ep->gsm_payload_msg_type != req->gsm_payload_msg_type)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
63 return TMGW_RESP_ERR_PROT;
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 bcopy(&req->gsm_addr, &ep->rtp_gsm.remote_addr,
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 sizeof(struct sockaddr_in));
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 ep->gsm_payload_type = req->gsm_payload_type;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 ep->gsm_payload_msg_type = req->gsm_payload_msg_type;
100
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
68 gsm_vars_init(ep);
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (req->setup_mask & TMGW_CTRL_MASK_PSTN_CONN) {
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
71 if (ep->ep_type != TMGW_EP_TYPE_GATEWAY)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
72 return TMGW_RESP_ERR_PROT;
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
73 if (req->pstn_addr.ss_family != AF_INET)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
74 return TMGW_RESP_ERR_PARAM;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
75 switch (req->pstn_payload_type) {
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
76 case PSTN_CODEC_PCMU:
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
77 case PSTN_CODEC_PCMA:
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
78 break;
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
79 default:
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
80 return TMGW_RESP_ERR_PARAM;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
81 }
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 bcopy(&req->pstn_addr, &ep->rtp_pstn.remote_addr,
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 sizeof(struct sockaddr_in));
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 ep->pstn_payload_type = req->pstn_payload_type;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 if (req->setup_mask & TMGW_CTRL_MASK_FWD_MODE) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (ep->ep_type != TMGW_EP_TYPE_GATEWAY ||
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 ep->rtp_gsm.remote_addr.sin_family != AF_INET ||
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
89 ep->rtp_pstn.remote_addr.sin_family != AF_INET)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
90 return TMGW_RESP_ERR_PROT;
127
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents: 103
diff changeset
91 if (req->fwd_mode & TMGW_FWD_ENABLE_GSM2PSTN) {
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 100
diff changeset
92 rc = gsm2pstn_init(ep);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 100
diff changeset
93 if (rc != TMGW_RESP_OK)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 100
diff changeset
94 return rc;
170
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
95 } else
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
96 ep->dtmf_sample_ptr = 0;
127
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents: 103
diff changeset
97 if (req->fwd_mode & TMGW_FWD_ENABLE_PSTN2GSM) {
95
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
98 rc = pstn2gsm_init(ep);
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
99 if (rc != TMGW_RESP_OK)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
100 return rc;
95
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
101 }
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 ep->fwd_mode = req->fwd_mode;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 }
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
104 return TMGW_RESP_OK;
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 void
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 process_mdcx(conn, req, resp)
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 struct ctrl_conn *conn;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 struct tmgw_ctrl_req *req;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 struct tmgw_ctrl_resp *resp;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 struct endpoint *ep;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 ep = find_ep_by_id(conn, req->ep_id);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 if (!ep) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 resp->res = TMGW_RESP_ERR_PROT;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 return;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 }
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
120 resp->res = mdcx_operation(ep, req);
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 }