annotate mgw/mdcx.c @ 103:3b3f07b112f3

mgw: implement GSM to PSTN forwarding
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 26 Sep 2022 20:46:19 -0800
parents 1579ec0e9fb9
children f062c32a5116
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>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <netinet/in.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #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
9 #include <stdint.h>
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <syslog.h>
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "../include/tmgw_ctrl.h"
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "../include/tmgw_const.h"
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #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
17 #include "int_defs.h"
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 extern struct endpoint *find_ep_by_id();
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
100
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
21 static void
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
22 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
23 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
24 {
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
25 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
26 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
27 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
28 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
29 return;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
30 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
31 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
32 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
33 return;
1579ec0e9fb9 mgw internal code: set GSM side RTP packet size in one place
Mychaela Falconia <falcon@freecalypso.org>
parents: 99
diff changeset
34 }
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
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
37 mdcx_operation(ep, req)
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 struct endpoint *ep;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 struct tmgw_ctrl_req *req;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 {
95
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
41 int rc;
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
42
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 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
44 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
45 return TMGW_RESP_ERR_PROT;
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
46 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
47 return TMGW_RESP_ERR_PARAM;
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
48 if (req->gsm_payload_type > 0x7F)
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
49 return TMGW_RESP_ERR_PARAM;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
50 switch (req->gsm_payload_msg_type) {
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
51 case GSM_TCHF_FRAME:
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
52 break;
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
53 case GSM_TCHF_FRAME_EFR:
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
54 case GSM_TCHH_FRAME:
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
55 case GSM_TCH_FRAME_AMR:
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
56 return TMGW_RESP_ERR_NOTSUP;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
57 default:
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
58 return TMGW_RESP_ERR_PARAM;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
59 }
95
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
60 if (ep->gsm_payload_msg_type &&
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
61 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
62 return TMGW_RESP_ERR_PROT;
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 bcopy(&req->gsm_addr, &ep->rtp_gsm.remote_addr,
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 sizeof(struct sockaddr_in));
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 ep->gsm_payload_type = req->gsm_payload_type;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 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
67 gsm_vars_init(ep);
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 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
70 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
71 return TMGW_RESP_ERR_PROT;
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
72 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
73 return TMGW_RESP_ERR_PARAM;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
74 switch (req->pstn_payload_type) {
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
75 case PSTN_CODEC_PCMU:
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
76 case PSTN_CODEC_PCMA:
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
77 break;
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
78 default:
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
79 return TMGW_RESP_ERR_PARAM;
34
7dae2bae56a1 themwi-mgw: implement MDCX parameter validation
Mychaela Falconia <falcon@freecalypso.org>
parents: 32
diff changeset
80 }
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 bcopy(&req->pstn_addr, &ep->rtp_pstn.remote_addr,
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 sizeof(struct sockaddr_in));
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 ep->pstn_payload_type = req->pstn_payload_type;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (req->setup_mask & TMGW_CTRL_MASK_FWD_MODE) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 if (ep->ep_type != TMGW_EP_TYPE_GATEWAY ||
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 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
88 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
89 return TMGW_RESP_ERR_PROT;
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 100
diff changeset
90 if ((req->fwd_mode & TMGW_FWD_ENABLE_GSM2PSTN) &&
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 100
diff changeset
91 !(ep->fwd_mode & TMGW_FWD_ENABLE_GSM2PSTN)) {
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;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 100
diff changeset
95 }
95
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
96 if ((req->fwd_mode & TMGW_FWD_ENABLE_PSTN2GSM) &&
f280328e7e2e themwi-mgw: initial implementation of PSTN to GSM forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents: 38
diff changeset
97 !(ep->fwd_mode & TMGW_FWD_ENABLE_PSTN2GSM)) {
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 int rc;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 ep = find_ep_by_id(conn, req->ep_id);
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 if (!ep) {
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 resp->res = TMGW_RESP_ERR_PROT;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 return;
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 }
99
738be11ac432 mgw internal code: simplify MDCX error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
121 resp->res = mdcx_operation(ep, req);
32
b3f74df7b808 beginning of themwi-mgw
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 }