FreeCalypso > hg > themwi-interim
comparison mncc/call_setup.c @ 2:053f04687106
mncc: initial import from old ThemWi
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 08 Jun 2024 23:12:12 +0000 |
parents | |
children | 847690ea7f4a |
comparison
equal
deleted
inserted
replaced
1:b161dbfffdaa | 2:053f04687106 |
---|---|
1 /* | |
2 * In this module we implement setup of new calls: either new MO calls | |
3 * coming from GSM or new MT calls coming from a ThemWi call socket. | |
4 */ | |
5 | |
6 #include <sys/types.h> | |
7 #include <sys/socket.h> | |
8 #include <stdio.h> | |
9 #include <stdint.h> | |
10 #include <stdlib.h> | |
11 #include <string.h> | |
12 #include <strings.h> | |
13 #include <syslog.h> | |
14 #include "../include/mncc.h" | |
15 #include "../include/gsm48_const.h" | |
16 #include "../include/number_db_v2.h" | |
17 #include "../libnumdb2/lookup_func.h" | |
18 #include "struct.h" | |
19 #include "gsm_call.h" | |
20 | |
21 preen_msc_provided_number(nums) | |
22 struct gsm_mncc_number *nums; | |
23 { | |
24 int len; | |
25 | |
26 len = grok_number_string(nums->number, 0); | |
27 switch (len) { | |
28 case 4: | |
29 nums->type = GSM48_TON_NET_SPEC; | |
30 nums->plan = GSM48_NPI_PRIVATE; | |
31 break; | |
32 case 11: | |
33 if (nums->number[0] != '1') | |
34 return(0); | |
35 nums->type = GSM48_TON_INTERNATIONAL; | |
36 nums->plan = GSM48_NPI_ISDN_E164; | |
37 break; | |
38 default: | |
39 return(0); | |
40 } | |
41 nums->screen = GSM48_SCRN_NETWORK; | |
42 return(1); | |
43 } | |
44 | |
45 void | |
46 reject_mo_call(callref, cause_loc, cause_val) | |
47 uint32_t callref; | |
48 { | |
49 struct gsm_mncc msg; | |
50 | |
51 bzero(&msg, sizeof(struct gsm_mncc)); | |
52 msg.msg_type = MNCC_REJ_REQ; | |
53 msg.callref = callref; | |
54 mncc_set_cause(&msg, cause_loc, cause_val); | |
55 send_mncc_to_gsm(&msg, sizeof(struct gsm_mncc)); | |
56 } | |
57 | |
58 void | |
59 process_mo_call_setup(msg) | |
60 struct gsm_mncc *msg; | |
61 { | |
62 struct gsm_call *call; | |
63 struct owned_number_rec *own; | |
64 struct short_number_rec *snum; | |
65 int is_nanp, is_itn, is_local; | |
66 | |
67 if (preen_msc_provided_number(&msg->calling)) | |
68 msg->fields |= MNCC_F_CALLING; | |
69 else | |
70 msg->fields &= ~MNCC_F_CALLING; | |
71 if (!(msg->fields & MNCC_F_CALLED)) { | |
72 syslog(LOG_ERR, "rejecting MO call 0x%x: no called number", | |
73 msg->callref); | |
74 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
75 GSM48_CC_CAUSE_INVAL_MAND_INF); | |
76 return; | |
77 } | |
78 call = create_gsm_call(msg->callref); | |
79 if (!call) { | |
80 syslog(LOG_ERR, "rejecting MO call 0x%x: no memory for call", | |
81 msg->callref); | |
82 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
83 GSM48_CC_CAUSE_RESOURCE_UNAVAIL); | |
84 return; | |
85 } | |
86 /* route based on destination address */ | |
87 refresh_number_db(); | |
88 is_nanp = is_itn = 0; | |
89 switch (grok_number_string(msg->called.number, 0)) { | |
90 case 4: | |
91 if (msg->called.type != GSM48_TON_UNKNOWN && | |
92 msg->called.type != GSM48_TON_NET_SPEC && | |
93 msg->called.type != GSM48_TON_SHORT_CODE) | |
94 break; | |
95 if (msg->called.plan != GSM48_NPI_UNKNOWN && | |
96 msg->called.plan != GSM48_NPI_ISDN_E164 && | |
97 msg->called.plan != GSM48_NPI_PRIVATE) | |
98 break; | |
99 snum = numdb_lookup_short(msg->called.number); | |
100 if (!snum) { | |
101 syslog(LOG_ERR, | |
102 "rejecting MO call 0x%x: unassigned short number", | |
103 msg->callref); | |
104 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
105 GSM48_CC_CAUSE_UNASSIGNED_NR); | |
106 call->gc_flag = 1; | |
107 return; | |
108 } | |
109 switch (snum->short_num_type) { | |
110 case SHORT_NUM_TYPE_ABBREV: | |
111 is_nanp = 1; | |
112 msg->called.type = GSM48_TON_INTERNATIONAL; | |
113 msg->called.plan = GSM48_NPI_ISDN_E164; | |
114 sprintf(msg->called.number, "1%03u%03u%04u", | |
115 snum->fullnum_prefix[0], | |
116 snum->fullnum_prefix[1], snum->short_num); | |
117 break; | |
118 case SHORT_NUM_TYPE_ITN: | |
119 is_itn = 1; | |
120 msg->called.type = GSM48_TON_NET_SPEC; | |
121 msg->called.plan = GSM48_NPI_PRIVATE; | |
122 break; | |
123 case SHORT_NUM_TYPE_TEST_SINK: | |
124 syslog(LOG_ERR, | |
125 "rejecting MO call 0x%x: test sink not implemented", | |
126 msg->callref); | |
127 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
128 GSM48_CC_CAUSE_DEST_OOO); | |
129 call->gc_flag = 1; | |
130 return; | |
131 default: | |
132 syslog(LOG_ERR, | |
133 "rejecting MO call 0x%x: unknown short number type 0x%02X", | |
134 msg->callref, snum->short_num_type); | |
135 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
136 GSM48_CC_CAUSE_DEST_OOO); | |
137 call->gc_flag = 1; | |
138 return; | |
139 } | |
140 break; | |
141 case 10: | |
142 if (msg->called.type != GSM48_TON_UNKNOWN && | |
143 msg->called.type != GSM48_TON_NATIONAL) | |
144 break; | |
145 if (msg->called.plan != GSM48_NPI_UNKNOWN && | |
146 msg->called.plan != GSM48_NPI_ISDN_E164 && | |
147 msg->called.plan != GSM48_NPI_NATIONAL) | |
148 break; | |
149 if (!is_nanp_valid_prefix(msg->called.number)) { | |
150 syslog(LOG_ERR, | |
151 "rejecting MO call 0x%x: invalid NANP number", | |
152 msg->callref); | |
153 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
154 GSM48_CC_CAUSE_INV_NR_FORMAT); | |
155 call->gc_flag = 1; | |
156 return; | |
157 } | |
158 is_nanp = 1; | |
159 /* canonicalize to international format */ | |
160 bcopy(msg->called.number, msg->called.number+1, 11); | |
161 msg->called.number[0] = '1'; | |
162 msg->called.type = GSM48_TON_INTERNATIONAL; | |
163 msg->called.plan = GSM48_NPI_ISDN_E164; | |
164 break; | |
165 case 11: | |
166 if (msg->called.type != GSM48_TON_UNKNOWN && | |
167 msg->called.type != GSM48_TON_INTERNATIONAL) | |
168 break; | |
169 if (msg->called.plan != GSM48_NPI_UNKNOWN && | |
170 msg->called.plan != GSM48_NPI_ISDN_E164) | |
171 break; | |
172 if (msg->called.number[0] != '1') | |
173 break; | |
174 if (!is_nanp_valid_prefix(msg->called.number+1)) { | |
175 syslog(LOG_ERR, | |
176 "rejecting MO call 0x%x: invalid NANP number", | |
177 msg->callref); | |
178 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
179 GSM48_CC_CAUSE_INV_NR_FORMAT); | |
180 call->gc_flag = 1; | |
181 return; | |
182 } | |
183 is_nanp = 1; | |
184 /* canonicalize to international format */ | |
185 msg->called.type = GSM48_TON_INTERNATIONAL; | |
186 msg->called.plan = GSM48_NPI_ISDN_E164; | |
187 break; | |
188 } | |
189 is_local = is_itn; | |
190 if (is_nanp && (own = numdb_lookup_nanp(msg->called.number+1))) { | |
191 is_local = 1; | |
192 switch (own->usage & NUMBER_USAGE_MASK) { | |
193 case NUMBER_USAGE_TYPE_GSM_SUB: | |
194 break; | |
195 case NUMBER_USAGE_TYPE_ALIAS: | |
196 sprintf(msg->called.number, "1%03u%03u%04u", | |
197 own->remap[0], own->remap[1], own->remap[2]); | |
198 break; | |
199 default: | |
200 syslog(LOG_ERR, | |
201 "rejecting MO call 0x%x: unassigned owned NANP", | |
202 msg->callref); | |
203 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
204 GSM48_CC_CAUSE_UNASSIGNED_NR); | |
205 call->gc_flag = 1; | |
206 return; | |
207 } | |
208 } | |
209 /* weed out attempts to call yourself */ | |
210 if (is_local && !strcmp(msg->calling.number, msg->called.number)) { | |
211 syslog(LOG_ERR, "rejecting MO call 0x%x: call to self", | |
212 msg->callref); | |
213 reject_mo_call(msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
214 GSM48_CC_CAUSE_INCOMPAT_DEST); | |
215 call->gc_flag = 1; | |
216 return; | |
217 } | |
218 /* actually route the call */ | |
219 if (is_local) | |
220 internal_switch_mo_setup(call, msg); | |
221 else | |
222 outbound_mo_setup(call, msg); | |
223 } | |
224 | |
225 static void | |
226 reject_mt_call(conn, callref, cause_loc, cause_val) | |
227 struct socket_conn *conn; | |
228 uint32_t callref; | |
229 { | |
230 struct gsm_mncc msg; | |
231 | |
232 bzero(&msg, sizeof(struct gsm_mncc)); | |
233 msg.msg_type = MNCC_REJ_REQ; | |
234 msg.callref = callref; | |
235 mncc_set_cause(&msg, cause_loc, cause_val); | |
236 mncc_signal_to_socket_nocall(conn, &msg); | |
237 } | |
238 | |
239 void | |
240 process_ext_mtcall_setup(conn, msg) | |
241 struct socket_conn *conn; | |
242 struct gsm_mncc *msg; | |
243 { | |
244 struct gsm_call *call; | |
245 | |
246 if (!(msg->fields & MNCC_F_CALLED) && !msg->imsi[0]) { | |
247 syslog(LOG_ERR, "rejecting ext MT: no called number"); | |
248 reject_mt_call(conn, msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
249 GSM48_CC_CAUSE_INVAL_MAND_INF); | |
250 return; | |
251 } | |
252 call = create_new_mt_call(); | |
253 if (!call) { | |
254 syslog(LOG_ERR, "rejecting ext MT: no memory for call"); | |
255 reject_mt_call(conn, msg->callref, GSM48_CAUSE_LOC_PRN_S_LU, | |
256 GSM48_CC_CAUSE_RESOURCE_UNAVAIL); | |
257 return; | |
258 } | |
259 call->socket = conn; | |
260 call->socket_ref = msg->callref; | |
261 conn->ncalls++; | |
262 syslog(LOG_DEBUG, "mapped socket callref 0x%x to GSM callref 0x%x", | |
263 msg->callref, call->callref); | |
264 /* forward to GSM MNCC interface */ | |
265 msg->callref = call->callref; | |
266 send_mncc_to_gsm(msg, sizeof(struct gsm_mncc)); | |
267 } | |
268 | |
269 preen_connected_number(msg) | |
270 struct gsm_mncc *msg; | |
271 { | |
272 if (preen_msc_provided_number(&msg->connected)) | |
273 msg->fields |= MNCC_F_CONNECTED; | |
274 else | |
275 msg->fields &= ~MNCC_F_CONNECTED; | |
276 } |