-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgw.c
430 lines (308 loc) · 10.1 KB
/
gw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "gw.h"
#include "slap.h"
#include "mgcp.h"
#include "logging.h"
static GHashTable *gwtable;
static GList *callfreelist=NULL;
static void gw_call_put(struct call_s *call) {
logwrite(LOG_DEBUG, "returning call to freelist %p", call);
callfreelist=g_list_concat(&call->list, callfreelist);
}
static struct call_s *gw_call_get(void ) {
struct call_s *call;
GList *first;
first=g_list_first(callfreelist);
if (!first) {
call=g_slice_new0(struct call_s);
call->list.data=call;
logwrite(LOG_DEBUG, "returning newly allocated call %p", call);
return call;
}
callfreelist=g_list_remove_link(callfreelist, first);
logwrite(LOG_DEBUG, "returning call from freelist %p %p", first->data, first);
return first->data;
}
void gw_slot_create(struct gateway_s *gw, int slot) {
logwrite(LOG_DEBUG, "Createing slot on %s slot %d", gw->name, slot);
}
void gw_ds1_create(struct gateway_s *gw, int slot, int ds1) {
logwrite(LOG_DEBUG, "Createing ds1 on %s slot %d ds1 %d", gw->name, slot, ds1);
if (!gw->slot[slot].ds1[ds1])
gw->slot[slot].ds1[ds1]=calloc(1, sizeof(struct ds1_s));
}
struct ds1_s *gw_ds1_get(struct gateway_s *gw, int slot, int ds1) {
return gw->slot[slot].ds1[ds1];
}
struct ds0_s *gw_ds0_get(struct gateway_s *gw, int slot, int ds1no, int ds0no) {
struct ds1_s *ds1=gw_ds1_get(gw, slot, ds1no);
if (!ds1)
return NULL;
return &ds1->ds0[ds0no];
}
void gw_ds0_set_status(struct gateway_s *gw, int slot, int span, int chan, int status) {
struct ds1_s *ds1=gw_ds1_get(gw, slot, span);
if (!ds1)
return;
ds1->ds0[chan].status=status;
}
void gw_ds1_set_status(struct gateway_s *gw, int slot, int span, int status) {
struct ds1_s *ds1=gw_ds1_get(gw, slot, span);
if (!ds1)
return;
if (ds1->status != DS1_STATUS_UP) {
if (status == DS1_STATUS_UP) {
mgcp_send_rsip_span(gw, slot, span, MGCP_RSIP_RESTART, 0);
}
} else if (ds1->status == DS1_STATUS_UP) {
if (status != DS1_STATUS_UP) {
mgcp_send_rsip_span(gw, slot, span, MGCP_RSIP_FORCED, 0);
}
}
ds1->status=status;
}
void gw_slot_set_status(struct gateway_s *gw, int slot, int status) {
int i;
if (status == 2) {
for(i=0;i<3;i++)
gw_ds1_set_status(gw, slot, i, DS1_STATUS_UNKNOWN);
}
gw->slot[slot].status=status;
}
void gw_set_status(struct gateway_s *gw, int status) {
if (gw->status != GW_STATUS_AVAIL)
if (status == GW_STATUS_AVAIL)
mgcp_send_rsip_gw(gw, MGCP_RSIP_RESTART, 5);
if (gw->status == GW_STATUS_AVAIL)
if (status != GW_STATUS_AVAIL)
mgcp_send_rsip_gw(gw, MGCP_RSIP_FORCED, 0);
gw->status=status;
}
int gw_connected(struct gateway_s *gw) {
return (gw->status == GW_STATUS_AVAIL);
}
int gw_ds0_idle(struct ds0_s *ds0) {
return (ds0->status == DS0_IDLE);
}
/* Okay - this is tricky - we need a callid for SLAP and
for MGCP for later reference.
MGCP has a HEX string and SLAP uses a 15 bit identifier
with an identifier of 0 beeing illegal.
*/
int gw_callid_next(struct gateway_s *gw) {
/* Loop until we find a non used call id ...
most likely the next */
while(42) {
gw->callid++;
gw->callid&=0x7fff;
if (gw->callid == 0)
gw->callid++;
if (!g_hash_table_lookup(gw->calltable, &gw->callid))
break;
}
/* FIXME: Check for existing callid */
return gw->callid;
}
static void gw_call_delete(int fd, short event, void *arg) {
struct call_s *call=arg;
if (call->status != CALL_IDLE) {
logwrite(LOG_ERROR, "Call delete called with non idle CALL gateway %s callid %d",
call->ep.gw->name, call->callid);
}
g_hash_table_remove(call->ep.gw->calltable, &call->callid);
gw_call_put(call);
}
void gw_call_deltimer_start(struct call_s *call) {
call->tv.tv_sec=CALL_TIMER_DEL;
call->tv.tv_usec=0;
evtimer_del(&call->timer);
evtimer_set(&call->timer, &gw_call_delete, call);
evtimer_add(&call->timer, &call->tv);
}
static void gw_call_drop(int fd, short event, void *arg) {
struct call_s *call=arg;
switch(call->status) {
case(CALL_IDLE):
/* If the call went idle something weird happend. We should
not trigger as the idle setting process should cancel this
timer and schedule a droptimer
*/
logwrite(LOG_ERROR, "droptimer triggered - call was idle gw %s callid %d",
call->ep.gw->name, call->callid);
gw_call_deltimer_start(call);
break;
case(CALL_DROP_WAIT_MGCP):
/* We simply reset the state - SLAP took care for itself */
logwrite(LOG_ERROR, "droptimer triggered waiting for MGCP - gw %s callid %d",
call->ep.gw->name, call->callid);
gw_mgcp_call_drop_ack(call->ep.gw, call->callid);
break;
case(CALL_DROP_WAIT_SLAP):
/* We send out an 200 dropped to MGCP and hope SLAP has just forgotten
to send us a message.
*/
logwrite(LOG_ERROR, "droptimer triggered waiting for SLAP - gw %s callid %d",
call->ep.gw->name, call->callid);
gw_slap_call_drop_ack(call->ep.gw, call->callid);
break;
}
}
void gw_call_droptimer_start(struct call_s *call) {
call->tv.tv_sec=CALL_TIMER_DROP;
call->tv.tv_usec=0;
evtimer_del(&call->timer);
evtimer_set(&call->timer, &gw_call_drop, call);
evtimer_add(&call->timer, &call->tv);
}
void gw_slap_call_drop_req(struct gateway_s *gw, int callid) {
struct call_s *call;
logwrite(LOG_ERROR, "Call drop from SLAP - callid %d gw %s", callid, gw->name);
call=g_hash_table_lookup(gw->calltable, &callid);
if (!call) {
logwrite(LOG_ERROR, "Unknown callid in deny from SLAP - callid %d gw %s", callid, gw->name);
return;
}
call->status=CALL_DROP_WAIT_MGCP;
mgcp_call_drop_req(&call->ep, callid);
gw_call_droptimer_start(call);
}
void gw_slap_call_drop_ack(struct gateway_s *gw, int callid) {
struct call_s *call;
logwrite(LOG_DEBUG, "Call drop ack from SLAP - callid %d gw %s", callid, gw->name);
call=g_hash_table_lookup(gw->calltable, &callid);
if (!call) {
logwrite(LOG_ERROR, "Unknown callid in dropack from SLAP - callid %d gw %s", callid, gw->name);
return;
}
mgcp_call_drop_ack(&call->ep, call->mgcpmsgid, callid);
call->status=CALL_IDLE;
call->ds0->status=DS0_IDLE;
gw_call_deltimer_start(call);
call->ds0->call=NULL;
}
void gw_mgcp_call_drop_ack(struct gateway_s *gw, int connid) {
struct call_s *call;
call=g_hash_table_lookup(gw->calltable, &connid);
if (!call) {
logwrite(LOG_ERROR, "Could not find ConnectionID %x from %s",
connid, gw->name);
return;
}
slap_call_drop_ack(gw, call->ep.slot, call->ep.span, call->ep.chan, connid);
call->status=CALL_IDLE;
call->ds0->status=DS0_IDLE;
gw_call_deltimer_start(call);
call->ds0->call=NULL;
}
void gw_mgcp_call_drop_req(struct endpoint_s *ep, int mgcpmsgid, int connid) {
struct call_s *call;
if (connid) {
call=g_hash_table_lookup(ep->gw->calltable, &connid);
if (!call) {
logwrite(LOG_ERROR, "Could not find ConnectionID %x from %s", connid, ep->gw->name);
/* We dont even know this endpoint - simply acknowledge */
mgcp_call_drop_ack(ep, mgcpmsgid, connid);
return;
}
} else {
struct ds0_s *ds0;
ds0=gw_ds0_get(ep->gw, ep->slot, ep->span, ep->chan);
if (!ds0) {
logwrite(LOG_ERROR, "Could not find DS0 from endpoint %s %d %d %d",
ep->gw->name, ep->slot, ep->span, ep->chan);
/* We dont even know this endpoint - simply acknowledge */
mgcp_call_drop_ack(ep, mgcpmsgid, connid);
return;
}
if (!ds0->call) {
logwrite(LOG_ERROR, "No call on DS0 from endpoint %s %d %d %d",
ep->gw->name, ep->slot, ep->span, ep->chan);
/* No call - simply acknowledge */
mgcp_call_drop_ack(ep, mgcpmsgid, connid);
return;
}
call=ds0->call;
}
call->mgcpmsgid=mgcpmsgid;
call->status=CALL_DROP_WAIT_SLAP;
slap_call_drop_req(call->ep.gw, call->ep.slot, call->ep.span, call->ep.chan, call->callid);
gw_call_droptimer_start(call);
}
void gw_slap_call_proceed(struct gateway_s *gw, int callid) {
struct call_s *call;
logwrite(LOG_DEBUG, "Call proceed from SLAP - callid %d gw %s", callid, gw->name);
call=g_hash_table_lookup(gw->calltable, &callid);
if (!call) {
logwrite(LOG_ERROR, "Unknown callid in proceed from SLAP - callid %d gw %s", callid, gw->name);
return;
}
call->status=CALL_ESTABLISHED;
mgcp_call_proceed(&call->ep, call->mgcpmsgid, callid);
}
int gw_mgcp_call_setup(struct endpoint_s *ep, int mgcpmsgid,
char *anumber, char *bnumber, int bearer) {
struct ds0_s *ds0=gw_ds0_get(ep->gw, ep->slot, ep->span, ep->chan);
struct call_s *call=gw_call_get();
#if 0
if (!gw_ds0_idle(ds0)) {
mgcp_send_busy(ep, mgcpmsgid);
return 0;
}
#endif
call->ds0=ds0;
ds0->call=call;
call->status=CALL_INCOMING;
ds0->status=DS0_BUSY;
memcpy(&call->ep, ep, sizeof(struct endpoint_s));
strncpy(call->anumber, anumber, NUMBER_MAX_SIZE);
strncpy(call->bnumber, bnumber, NUMBER_MAX_SIZE);
call->callid=gw_callid_next(ep->gw);
call->bearertype=bearer;
/* Store msgid for later status response messages */
call->mgcpmsgid=mgcpmsgid;
g_hash_table_insert(ep->gw->calltable, &call->callid, call);
slap_call_incoming(ep->gw, ep->slot, ep->span, ep->chan,
bearer, call->anumber, call->bnumber, call->callid);
return call->callid;
}
void gw_slap_call_deny(struct gateway_s *gw, int callid) {
struct call_s *call;
logwrite(LOG_ERROR, "Call deny from SLAP - callid %d gw %s", callid, gw->name);
call=g_hash_table_lookup(gw->calltable, &callid);
if (!call) {
logwrite(LOG_ERROR, "Unknown callid in deny from SLAP - callid %d gw %s", callid, gw->name);
return;
}
mgcp_call_deny(&call->ep, call->mgcpmsgid, callid);
call->status=CALL_IDLE;
call->ds0->status=DS0_IDLE;
gw_call_deltimer_start(call);
call->ds0->call=NULL;
}
struct gateway_s *gw_lookup(char *name) {
return g_hash_table_lookup(gwtable, name);
}
struct gateway_s *gw_create(char *name) {
struct gateway_s *gw;
logwrite(LOG_INFO, "Created gateway with name %s", name);
gw=calloc(1, sizeof(struct gateway_s));
strncpy(gw->name, name, sizeof(gw->name));
g_hash_table_insert(gwtable, gw->name, gw);
gw->calltable=g_hash_table_new(g_int_hash, g_int_equal);
slap_init_gateway(gw);
mgcp_init_gateway(gw);
return gw;
}
struct gateway_s *gw_lookup_or_create(char *name) {
struct gateway_s *gw;
gw=gw_lookup(name);
if (gw)
return gw;
return gw_create(name);
}
int gw_init(void ) {
gwtable=g_hash_table_new(g_str_hash, g_str_equal);
return 0;
}