/
seas.c
759 lines (705 loc) · 20 KB
/
seas.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
* Copyright (C) 2006-2007 VozTelecom Sistemas S.L
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>/*memset*/
#include <errno.h>/*errno*/
#include <unistd.h>/*close(),pipe,fork,pid_t*/
#include <sys/wait.h>/*wait*/
#include <signal.h>/*SIGINT,etc*/
#include "../../sr_module.h"
#include "../../ip_addr.h" /*ip_addr,hostent2ip_addr*/
#include "../../tags.h" /*init_tags*/
#include "../../socket_info.h" /*get_first_socket()*/
#include "../../resolve.h" /*resolvehost*/
#include "../../mem/mem.h" /*pkg_malloc*/
#include "../../mem/shm_mem.h" /*shm_malloc*/
#include "../../dprint.h" /*LM_**/
#include "../../error.h" /*ser_error*/
#include "../tm/tm_load.h" /*load_tm_api*/
#include "../tm/h_table.h" /*cell*/
#include "../tm/t_lookup.h" /*T_UNDEFINED*/
#include "encode_msg.h" /*encode_msg*/
#include "seas.h"
#include "seas_action.h"
#include "event_dispatcher.h"
#include "statistics.h"/*pstart_stats_server*/
#include "ha.h"
#include "cluster.h"
/* Exported Functions */
static int w_as_relay_t(struct sip_msg *msg, char *as_name, char *foo);
static int w_as_relay_sl(struct sip_msg *msg, char *as_name, char *foo);
/* Local functions */
static int seas_init(void);
static int seas_child_init(int rank);
static int seas_exit();
static int fixup_as_relay(void** param, int param_no);
/*utility functions*/
static void seas_init_tags();
static inline int is_e2e_ack(struct cell *t,struct sip_msg *msg);
char seas_tags[TOTAG_VALUE_LEN+1];
char *seas_tag_suffix;
char whoami[MAX_WHOAMI_LEN];
int is_dispatcher=0;
extern int sig_flag;
static char *seas_listen_socket=0;
static char *seas_stats_socket=0;
struct ip_addr *seas_listen_ip=0;
unsigned short seas_listen_port=0;
struct as_entry *as_table=0;
struct as_entry *as_list=0;
int write_pipe=0;
int read_pipe=0;
struct seas_functions seas_f;
static cmd_export_t cmds[]=
{
{"as_relay_t", (cmd_function)w_as_relay_t, 1, fixup_as_relay,
0, REQUEST_ROUTE},
{"as_relay_sl", (cmd_function)w_as_relay_sl, 1, fixup_as_relay,
0, REQUEST_ROUTE},
{0,0,0,0,0,0}
};
static param_export_t params[]=
{
{"listen_sockets",STR_PARAM, &seas_listen_socket},
{"stats_socket", STR_PARAM, &seas_stats_socket},
{"jain_ping", STR_PARAM, &jain_ping_config},
{"servlet_ping", STR_PARAM, &servlet_ping_config},
{"clusters", STR_PARAM, &cluster_cfg},
{0,0,0}
};
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_DEFAULT, "tm", DEP_ABORT },
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{ NULL, NULL },
},
};
struct module_exports exports=
{
"seas",
MOD_TYPE_DEFAULT,/* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS,
&deps, /* OpenSIPS module dependencies */
cmds,
0,
params,
0,
0,
0,
0, /* extra processes */
seas_init, /* module initialization function */
(response_function) 0,
(destroy_function) seas_exit, /* module exit function */
(child_init_function) seas_child_init /* per-child init function */
};
static int fixup_as_relay(void** param, int param_no)
{
int len;
char *parameter;
struct as_entry **entry,*tmp;
parameter=(char *)(*param);
if (param_no!=1)
return 0;
len=strlen(parameter);
for (entry=&as_list;*entry;entry=&((*entry)->next)) {
if (len== (*entry)->name.len &&
!memcmp((*entry)->name.s,parameter,len)) {
pkg_free(*param);
*param=*entry;
return 1;
}
}
if (!(*entry)) {
if (!(*entry=(struct as_entry *)shm_malloc(sizeof(struct as_entry)))) {
LM_ERR("no more shm_mem\n");
goto error;
}
memset(*entry,0,sizeof(struct as_entry));
if(!((*entry)->name.s=shm_malloc(len))){
LM_ERR("no more share mem\n");
goto error;
}
(*entry)->name.len=len;
memcpy((*entry)->name.s,parameter,len);
(*entry)->u.as.name=(*entry)->name;
(*entry)->u.as.event_fd=(*entry)->u.as.action_fd=-1;
(*entry)->type=AS_TYPE;
pkg_free(*param);
*param=*entry;
}
for (tmp=as_list;tmp;tmp=tmp->next)
LM_DBG("%.*s\n",tmp->name.len,tmp->name.s);
return 1;
error:
return -1;
}
/**
* Sets up signal handlers
*/
void seas_sighandler(int signo)
{
struct as_entry *as;
if(is_dispatcher)
sig_flag=signo;
switch(signo){
case SIGPIPE:
if(is_dispatcher)
return;
LM_INFO("%s exiting\n",whoami);
if(my_as->u.as.ac_buffer.s){
pkg_free(my_as->u.as.ac_buffer.s);
my_as->u.as.ac_buffer.s=0;
}
if(my_as->u.as.action_fd!=-1){
close(my_as->u.as.action_fd);
my_as->u.as.action_fd=-1;
}
exit(0);
break;
case SIGCHLD:
LM_INFO("Child stopped or terminated\n");
break;
case SIGUSR1:
case SIGUSR2:
LM_DBG("Memory status (pkg):\n");
#ifdef PKG_MALLOC
pkg_status();
#endif
break;
case SIGINT:
case SIGTERM:
LM_INFO("INFO: signal %d received\n",signo);
#ifdef PKG_MALLOC
pkg_status();
#endif
if(is_dispatcher){
for (as=as_list;as;as=as->next) {
if(as->type==AS_TYPE && as->connected)
kill(as->u.as.action_pid,signo);
}
while(wait(0) > 0);
exit(0);
}else{
LM_INFO("%s exiting\n",whoami);
if(my_as && my_as->u.as.ac_buffer.s)
pkg_free(my_as->u.as.ac_buffer.s);
if(my_as && my_as->u.as.action_fd!=-1)
close(my_as->u.as.action_fd);
exit(0);
}
break;
}
}
/**
* wrapper for the AS transaction-stateful relay script function.
*
*/
static int w_as_relay_t(struct sip_msg *msg, char *entry, char *foo)
{
as_msg_p my_as_ev;
int new_tran,ret=0,len;
char *buffer,processor_id;
struct cell *mycel;
struct as_entry *as;
static str msg500={"Server Internal Error!",sizeof("Server Internal Error!")-1};
buffer=(char*)0;
my_as_ev=(as_msg_p)0;
/**
* returns <0 on error
* 1 if (new transaction was created) or if (ACK for locally replied 200 with totag) or if (ACK for code>=300)
* 0 if it was a retransmission
*/
new_tran = seas_f.tmb.t_newtran(msg);
if(new_tran<0) {
ret = new_tran;
goto done;
}
/*retransmission: script processing should be stopped*/
if (new_tran==0 && !(msg->REQ_METHOD==METHOD_ACK)){
ret = 0;
goto done;
}
as=(struct as_entry *)entry;
if(!as->connected){
LM_ERR("app server %.*s not connected\n",as->name.len,as->name.s);
goto error;
}
if(as->type==AS_TYPE){
if((processor_id=get_processor_id(&msg->rcv,&(as->u.as)))<0){
LM_ERR("no processor found for packet with dst port:%d\n",msg->rcv.dst_port);
goto error;
}
}else if(as->type==CLUSTER_TYPE){
LM_ERR("clustering not fully implemented\n");
return 0;
}else{
LM_ERR("unknown type of as (neither cluster nor as)\n");
return -1;
}
LM_DBG("as found ! (%.*s) processor id = %d\n",as->name.len,as->name.s,processor_id);
if(new_tran==1 && msg->REQ_METHOD==METHOD_ACK){
/* core should forward statelessly (says t_newtran)*/
LM_DBG("forwarding statelessly !!!\n");
if(!(buffer=create_as_event_sl(msg,processor_id,&len,0))){
LM_ERR("create_as_event_sl() unable to create event code\n");
goto error;
}
}else if(!(buffer=create_as_event_t(seas_f.tmb.t_gett(),msg,processor_id,&len,0))){
LM_ERR("unable to create event code\n");
goto error;
}
if(!(my_as_ev=shm_malloc(sizeof(as_msg_t)))){
LM_ERR("Out of shared mem!\n");
goto error;
}
my_as_ev->msg = buffer;
my_as_ev->as = as;
my_as_ev->type = T_REQ_IN;
my_as_ev->len = len;
my_as_ev->transaction=seas_f.tmb.t_gett(); /*does not refcount*/
if(use_stats && new_tran>0)
as_relay_stat(seas_f.tmb.t_gett());
again:
ret=write(write_pipe,&my_as_ev,sizeof(as_msg_p));
if(ret==-1){
if(errno==EINTR)
goto again;
else if(errno==EPIPE){
LM_ERR("SEAS Event Dispatcher has closed the pipe. Invalidating it !\n");
goto error;
/** TODO handle this correctly !!!*/
}
}
seas_f.tmb.t_setkr(REQ_FWDED);
ret=0;
done:
return ret;
error:
mycel=seas_f.tmb.t_gett();
if(mycel && mycel!=T_UNDEFINED){
if(!seas_f.tmb.t_reply(msg,500,&msg500)){
LM_ERR("t_reply (500)\n");
}
}
if(my_as_ev)
shm_free(my_as_ev);
if(buffer)
shm_free(buffer);
return ret;
}
/**
* wrapper for the AS stateless relay script function.
*
*/
static int w_as_relay_sl(struct sip_msg *msg, char *as_name, char *foo)
{
as_msg_p my_as_ev=0;
int ret=0,len;
char *buffer=0,processor_id;
struct as_entry *as;
as=(struct as_entry *)as_name;
if(as->type==AS_TYPE){
if((processor_id=get_processor_id(&msg->rcv,&(as->u.as)))<0){
LM_ERR("no processor found for packet with dst port:%d\n",msg->rcv.dst_port);
goto error;
}
}else if (as->type==CLUSTER_TYPE) {
LM_ERR("clustering not fully implemented\n");
goto error;
}else{
LM_ERR("unknown type of as\n");
goto error;
}
LM_DBG("as found ! (%.*s) processor id = %d\n",as->name.len,as->name.s,processor_id);
if(!(buffer=create_as_event_sl(msg,processor_id,&len,0))){
LM_ERR("unable to create event code\n");
goto error;
}
if(!(my_as_ev=shm_malloc(sizeof(as_msg_t))))
goto error;
my_as_ev->msg = buffer;
my_as_ev->as = as;
my_as_ev->type = SL_REQ_IN;
my_as_ev->len = len;
my_as_ev->transaction=seas_f.tmb.t_gett(); /*does not refcount*/
if(use_stats)
as_relay_stat(seas_f.tmb.t_gett());
again:
ret=write(write_pipe,&my_as_ev,sizeof(as_msg_p));
if(ret==-1){
if(errno==EINTR)
goto again;
else if(errno==EPIPE){
LM_ERR("SEAS Event Dispatcher has closed the pipe. Invalidating it !\n");
return -2;
/** TODO handle this correctly !!!*/
}
}
//this shouln't be here, because it will remove the transaction from memory, but
//if transaction isn't unref'ed iw will be released anyway at t_unref if kr (killreason)==0
// a wait timer will be put to run with WT_TIME_OUT (5 seconds, within which the AS should respond)
// this is a bug !!! I think this is why we lose calls at high load !!
//t_release(msg, 0, 0);
seas_f.tmb.t_setkr(REQ_FWDED);
ret=0;
return ret;
error:
if(my_as_ev)
shm_free(my_as_ev);
if(buffer)
shm_free(buffer);
return ret;
}
/**
* creates an as_event in shared memory and returns its address or NULL if error.
* event_length(4) UNSIGNED INT includes the length 4 bytes itself
* type(1),
* flags(4),
* transport(1).
* src_ip_len(1),
* src_ip(4 or 16),
* dst_ip_len(1),
* dst_ip(4 or 16),
* src_port(2),
* dst_port(2),
* hash index(4),
* label(4),
* [cancelled hash_index,label]
*
*/
char * create_as_event_t(struct cell *t,struct sip_msg *msg,char processor_id,int *evt_len,int flags)
{
unsigned int i,hash_index,label;
unsigned short int port;
unsigned int k,len;
char *buffer=NULL;
struct cell *originalT;
originalT=0;
if(!(buffer=shm_malloc(ENCODED_MSG_SIZE))){
LM_ERR("Out Of Memory !!\n");
return 0;
}
*evt_len=0;
if(t){
hash_index=t->hash_index;
label=t->label;
}else{
/**seas_f.tmb.t_get_trans_ident(msg,&hash_index,&label); this is bad, because it ref-counts !!!*/
LM_ERR("no transaction provided...\n");
goto error;
}
k=4;
/*type*/
buffer[k++]=(unsigned char)T_REQ_IN;
/*processor_id*/
buffer[k++]=(unsigned char)processor_id;
/*flags*/
if(is_e2e_ack(t,msg)){
flags|=E2E_ACK;
}else if(msg->REQ_METHOD==METHOD_CANCEL){
LM_DBG("new CANCEL\n");
originalT=seas_f.tmb.t_lookup_original_t(msg);
if(!originalT || originalT==T_UNDEFINED){
/** we dont even pass the unknown CANCEL to JAIN*/
LM_WARN("CANCEL does not match any existing transaction!!\n");
goto error;
}else{
flags|=CANCEL_FOUND;
//seas_f.tmb.unref_cell(originalT);
}
LM_DBG("Cancelling transaction !!\n");
}
flags=htonl(flags);
memcpy(buffer+k,&flags,4);
k+=4;
/*protocol should be UDP,TCP,TLS or whatever*/
buffer[k++]=(unsigned char)msg->rcv.proto;
/*src ip len + src ip*/
len=msg->rcv.src_ip.len;
buffer[k++]=(unsigned char)len;
memcpy(buffer+k,&(msg->rcv.src_ip.u),len);
k+=len;
/*dst ip len + dst ip*/
len=msg->rcv.dst_ip.len;
buffer[k++]=(unsigned char)len;
memcpy(buffer+k,&(msg->rcv.dst_ip.u),len);
k+=len;
/*src port */
port=htons(msg->rcv.src_port);
memcpy(buffer+k,&port,2);
k+=2;
/*dst port */
port=htons(msg->rcv.dst_port);
memcpy(buffer+k,&port,2);
k+=2;
/*hash_index*/
i=htonl(hash_index);
memcpy(buffer+k,&i,4);
k+=4;
/*label (is the collision slot in the hash-table)*/
i=htonl(label);
memcpy(buffer+k,&i,4);
k+=4;
if(msg->REQ_METHOD==METHOD_CANCEL && originalT){
LM_DBG("Cancelled transaction: Hash_Index=%d, Label=%d\n",originalT->hash_index,originalT->label);
/*hash_index*/
i=htonl(originalT->hash_index);
memcpy(buffer+k,&i,4);
k+=4;
/*label (is the collision slot in the hash-table)*/
i=htonl(originalT->label);
memcpy(buffer+k,&i,4);
k+=4;
}
/*length of event (hdr+payload-4), copied at the beginning*/
if(encode_msg(msg,buffer+k,ENCODED_MSG_SIZE-k)<0){
LM_ERR("Unable to encode msg\n");
goto error;
}
i = GET_PAY_SIZE(buffer+k);
k+=i;
*evt_len=k;
k=htonl(k);
memcpy(buffer,&k,4);
return buffer;
error:
if(buffer)
shm_free(buffer);
return 0;
}
/**
* creates an as_event in shared memory and returns its address or NULL if error.
* event_length(4) UNSIGNED INT includes the length 4 bytes itself
* type(1),
* processor_id(4),
* flags(4),
* transport(1).
* src_ip_len(1),
* src_ip(4 or 16),
* dst_ip_len(1),
* dst_ip(4 or 16),
* src_port(2),
* dst_port(2),
*
*/
char * create_as_event_sl(struct sip_msg *msg,char processor_id,int *evt_len,int flags)
{
unsigned int i;
unsigned short int port;
unsigned int k,len;
char *buffer=NULL;
if(!(buffer=shm_malloc(ENCODED_MSG_SIZE))){
LM_ERR("create_as_event_t Out Of Memory !!\n");
return 0;
}
*evt_len=0;
/*leave 4 bytes for event length*/
k=4;
/*type*/
buffer[k++]=(unsigned char)SL_REQ_IN;
/*processor_id*/
buffer[k++]=(unsigned char)processor_id;
/*flags*/
flags=htonl(flags);
memcpy(buffer+k,&flags,4);
k+=4;
/*protocol should be UDP,TCP,TLS or whatever*/
buffer[k++]=(unsigned char)msg->rcv.proto;
/*src ip len + src ip*/
len=msg->rcv.src_ip.len;
buffer[k++]=(unsigned char)len;
memcpy(buffer+k,&(msg->rcv.src_ip.u),len);
k+=len;
/*dst ip len + dst ip*/
len=msg->rcv.dst_ip.len;
buffer[k++]=(unsigned char)len;
memcpy(buffer+k,&(msg->rcv.dst_ip.u),len);
k+=len;
/*src port */
port=htons(msg->rcv.src_port);
memcpy(buffer+k,&port,2);
k+=2;
/*dst port */
port=htons(msg->rcv.dst_port);
memcpy(buffer+k,&port,2);
k+=2;
/*length of event (hdr+payload-4), copied at the beginning*/
if(encode_msg(msg,buffer+k,ENCODED_MSG_SIZE-k)<0){
LM_ERR("Unable to encode msg\n");
goto error;
}
i = GET_PAY_SIZE(buffer+k);
k+=i;
*evt_len=k;
k=htonl(k);
memcpy(buffer,&k,4);
return buffer;
error:
if(buffer)
shm_free(buffer);
return 0;
}
static inline int is_e2e_ack(struct cell *t,struct sip_msg *msg)
{
if(msg->REQ_METHOD != METHOD_ACK)
return 0;
if (t->uas.status<300)
return 1;
return 0;
}
/** Initializes seas module. It first parses the listen_sockets parameter
* which has the form "ip_address[:port]", creates the pipe to
* communicate with the dispatcher.
*/
static int seas_init(void)
{
char *p,*port;
struct hostent *he;
struct socket_info *si;
int c_pipe[2],mierr,i;
/** Populate seas_functions*/
if (load_tm_api(&seas_f.tmb)!=0) {
LM_ERR( "can't load TM API\n");
return -1;
}
if(!(seas_f.t_check_orig_trans = find_export("t_check_trans", 0, 0))){
LM_ERR( "Seas requires transaction module (t_check_trans not found)\n");
return -1;
}
/** Populate seas_functions*/
c_pipe[0]=c_pipe[1]=-1;
p=seas_listen_socket;
port=(char *)0;
seas_listen_port=5080;
/*if the seas_listen_socket configuration string is empty, use default values*/
if(p==NULL || *p==0){
si=get_first_socket();
seas_listen_ip=&si->address;
} else {/*if config string is not empty, then try to find host first, and maybe port..*/
while(*p){
if(*p == ':'){
*p=0;
port=p+1;
break;
}
p++;
}
if(!(he=resolvehost(seas_listen_socket,0)))
goto error;
if(!(seas_listen_ip=pkg_malloc(sizeof(struct ip_addr))))
goto error;
hostent2ip_addr(seas_listen_ip, he, 0);
if(port!=(char *)0 && (seas_listen_port=str2s(port,strlen(port),&mierr))==0){
LM_ERR("invalid port %s \n",port);
goto error;
}
}
memset(unc_as_t,0,2*MAX_UNC_AS_NR*sizeof(struct unc_as));//useless because unc_as_t is in bss?
if (pipe(c_pipe)==-1) {
LM_ERR("cannot create pipe!\n");
goto error;
}
read_pipe=c_pipe[0];
write_pipe=c_pipe[1];
seas_init_tags();
if(0>start_stats_server(seas_stats_socket))
goto error;
if(0>prepare_ha())
goto error;
if(0>parse_cluster_cfg())
goto error;
return 0;
error:
for(i=0;i<2;i++)
if(c_pipe[i]!=-1)
close(c_pipe[i]);
if(seas_listen_ip!=0)
pkg_free(seas_listen_ip);
if(use_stats)
stop_stats_server();
return -1;
}
/**Initializes SEAS to-tags
*/
static void seas_init_tags(void)
{
init_tags(seas_tags, &seas_tag_suffix,"VozTele-Seas/tags",'-');
LM_DBG("seas_init_tags, seas_tags=%s\n",seas_tags);
}
/**
* This function initializes each one of the processes spawn by the server.
* the rank is 1 only when the main process is being initialized, so in that
* case the function spawns the SEAS process to handle as_events triggered
* from the other SER processes (executing the script).
* the new process created, then goes into dispatcher_main_loop(), where
* it reads() the pipe waiting for events produced by other SER processes.
*/
static int seas_child_init(int rank)
{
int pid;
/* only the child 1 will execute this */
if (rank != 1){
/* only dispatcher needs to read from the pipe, so close reading fd*/
close(read_pipe);
return 0;
}
if ((pid=fork())<0) {
LM_ERR("forking failed\n");
return -1;
}
if (!pid) {
/*dispatcher child. we leave writing end open so that new childs spawned
* by event dispatcher can also write to pipe.. */
/* close(write_pipe); */
return dispatcher_main_loop();
}
return 0;
}
/* this should close the sockets open to any of the application servers, and
* send them an EOF event or something that signals that SER is beeing shutdown,
* so they could do their cleanup, etc.
*/
static int seas_exit(void)
{
if( seas_listen_ip!=NULL && seas_listen_ip!=&(get_first_socket()->address))
pkg_free(seas_listen_ip);
return 0;
}
/**
* search within a given AS, if any of the registered processors is bound
* to the receive_info structure passed. If there is one, it returns its
* identifier (number between 0 and 128), otherwise it returns -1;
*/
char get_processor_id(struct receive_info *rcv,as_p as)
{
int i;
for(i=0;i<MAX_BINDS;i++){
if(as->bound_processor[i]!=0 &&
(rcv->dst_ip.len == as->binds[i]->address.len) &&
(rcv->dst_ip.af==as->binds[i]->address.af) &&
(!memcmp(rcv->dst_ip.u.addr,as->binds[i]->address.u.addr,rcv->dst_ip.len))/* &&
(rcv->dst_port==as->binds[i].dst_port) &&
(rcv->proto==as->binds[i].proto)*/)
return as->bound_processor[i];
}
return -1;
}