forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket_info.c
1382 lines (1227 loc) · 38.4 KB
/
socket_info.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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* 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
*
*
* This file contains code that initializes and handles ser listen addresses
* lists (struct socket_info). It is used mainly on startup.
*
* History:
* --------
* 2003-10-22 created by andrei
* 2004-10-10 added grep_sock_info (andrei)
* 2004-11-08 added find_si (andrei)
* 2007-01-11 auto_aliases option added (bogdan)
*/
/*!
* \file
* \brief Find & manage listen addresses
*/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#include "str.h"
#include "globals.h"
#include "socket_info.h"
#include "dprint.h"
#include "mem/mem.h"
#include "ut.h"
#include "pt_scaling.h"
#include "resolve.h"
#include "name_alias.h"
#include "net/trans.h"
#ifdef __OS_linux
#include <features.h> /* for GLIBC version testing */
#if defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2, 4)
#include <ifaddrs.h>
#define HAVE_IFADDRS
#endif
#endif
#endif
#define MAX_PROC_BUFFER 256
/* list manip. functions (internal use only) */
/* append */
#define sock_listadd(head, el) \
do{\
if (*(head)==0) *(head)=(el); \
else{ \
for((el)->next=*(head); (el)->next->next;\
(el)->next=(el)->next->next); \
(el)->next->next=(el); \
(el)->prev=(el)->next; \
(el)->next=0; \
}\
}while(0)
/* insert after "after" */
#define sock_listins(el, after) \
do{ \
if ((after)){\
(el)->next=(after)->next; \
if ((after)->next) (after)->next->prev=(el); \
(after)->next=(el); \
(el)->prev=(after); \
}else{ /* after==0 = list head */ \
(after)=(el); \
(el)->next=(el)->prev=0; \
}\
}while(0)
#define sock_listrm(head, el) \
do {\
if (*(head)==(el)) *(head)=(el)->next; \
if ((el)->next) (el)->next->prev=(el)->prev; \
if ((el)->prev) (el)->prev->next=(el)->next; \
}while(0)
/* another helper function, it just creates a socket_info struct */
struct socket_info_full* new_sock_info( struct socket_id *sid)
{
struct socket_info_full *sif;
struct socket_info *si;
sif=(struct socket_info_full*) pkg_malloc(sizeof(*sif));
if (sif==NULL) goto error;
memset(sif, 0, sizeof(*sif));
si = &sif->socket_info;
si->socket=-1;
si->last_real_ports = &sif->last_real_ports;
if (sid->name) {
si->name.len=strlen(sid->name);
si->name.s=(char*)pkg_malloc(si->name.len+1); /* include \0 */
if (si->name.s==0) goto error;
memcpy(si->name.s, sid->name, si->name.len+1);
}
/* set port & proto */
si->port_no=sid->port;
si->proto=sid->proto;
si->flags=sid->flags;
/* advertised socket information */
/* Make sure the adv_sock_string is initialized, because if there is
* no adv_sock_name, no other code will initialize it!
*/
si->adv_sock_str.s=NULL;
si->adv_sock_str.len=0;
si->adv_port = 0; /* Here to help grep_sock_info along. */
if(sid->adv_name) {
si->adv_name_str.len=strlen(sid->adv_name);
si->adv_name_str.s=(char *)pkg_malloc(si->adv_name_str.len+1);
if (si->adv_name_str.s==0) goto error;
memcpy(si->adv_name_str.s, sid->adv_name, si->adv_name_str.len+1);
if (!sid->adv_port) sid->adv_port=si->port_no ;
si->adv_port_str.s=pkg_malloc(10);
if (si->adv_port_str.s==0) goto error;
si->adv_port_str.len=snprintf(si->adv_port_str.s, 10, "%hu",
(unsigned short)sid->adv_port);
si->adv_port = sid->adv_port;
}
/* store the tag info too */
if (sid->tag) {
si->tag.len = strlen(sid->tag);
si->tag.s=(char*)pkg_malloc(si->tag.len+1); /* include \0 */
if (si->tag.s==0) goto error;
memcpy(si->tag.s, sid->tag, si->tag.len+1);
}
if (si->proto!=PROTO_UDP && si->proto!=PROTO_SCTP &&
si->proto!=PROTO_HEP_UDP) {
if (sid->workers)
LM_WARN("number of workers per non UDP-based <%.*s> listener not "
"supported -> ignoring...\n", si->name.len, si->name.s);
if (sid->auto_scaling_profile)
LM_WARN("auto-scaling for non UDP-based <%.*s> listener not "
"supported -> ignoring...\n", si->name.len, si->name.s);
} else {
if (sid->workers)
si->workers = sid->workers;
if (sid->auto_scaling_profile) {
si->s_profile = get_scaling_profile(sid->auto_scaling_profile);
if (si->s_profile==NULL) {
LM_WARN("scaling profile <%s> in listener <%.*s> not defined "
"-> ignoring it...\n", sid->auto_scaling_profile,
si->name.len, si->name.s);
} else {
auto_scaling_enabled = 1;
}
} else if (udp_auto_scaling_profile) {
si->s_profile = get_scaling_profile(udp_auto_scaling_profile);
if (si->s_profile==NULL) {
LM_WARN("scaling profile <%s> in udp_workers not defined "
"-> ignoring it...\n", udp_auto_scaling_profile);
} else {
auto_scaling_enabled = 1;
}
}
}
return sif;
error:
LM_ERR("pkg memory allocation error\n");
if (sif) pkg_free(sif);
return 0;
}
/* delete a socket_info struct */
static void free_sock_info(struct socket_info_full* sif)
{
if(sif){
struct socket_info *si = &sif->socket_info;
if(si->name.s) pkg_free(si->name.s);
if(si->tag.s) pkg_free(si->tag.s);
if(si->sock_str.s) pkg_free(si->sock_str.s);
if(si->address_str.s) pkg_free(si->address_str.s);
if(si->port_no_str.s) pkg_free(si->port_no_str.s);
if(si->adv_name_str.s) pkg_free(si->adv_name_str.s);
if(si->adv_port_str.s) pkg_free(si->adv_port_str.s);
if(si->adv_sock_str.s) pkg_free(si->adv_sock_str.s);
if(si->tag_sock_str.s) pkg_free(si->tag_sock_str.s);
}
}
/* checks if the proto: host:port is one of the address we listen on
* and returns the corresponding socket_info structure.
* if port==0, the port number is ignored
* if proto==0 (PROTO_NONE) the protocol is ignored
* returns 0 if not found
* WARNING: uses str2ip6 so it will overwrite any previous
* unsaved result of this function (static buffer)
*/
const struct socket_info* grep_sock_info_ext(str* host, unsigned short port,
unsigned short proto, int check_tags)
{
char* hname;
int h_len;
const struct socket_info* si = NULL;
struct socket_info_full* sif;
struct socket_info_full ** list;
unsigned short c_proto;
struct ip_addr* ip6;
h_len=host->len;
hname=host->s;
if ((h_len>2)&&((*hname)=='[')&&(hname[h_len-1]==']')){
/* ipv6 reference, skip [] */
hname++;
h_len-=2;
}
c_proto=proto?proto:PROTO_UDP;
do{
/* "proto" is all the time valid here */
list=get_sock_info_list(c_proto);
if (list==0){
LM_WARN("unknown proto %d\n", c_proto);
goto not_found; /* false */
}
for (sif=*list; sif; sif=sif->next){
si = &sif->socket_info;
LM_DBG("checking if host==us: %d==%d && "
" [%.*s] == [%.*s]\n",
h_len,
si->name.len,
h_len, hname,
si->name.len, si->name.s
);
if (check_tags && port==0 && si->tag.s && h_len==si->tag.len &&
strncasecmp(hname, si->tag.s, si->tag.len)==0 )
goto found;
if (port) {
LM_DBG("checking if port %d matches port %d\n",
si->port_no, port);
if ((si->port_no != 0 || protos[c_proto].default_port != port) &&
(si->port_no == 0 || si->port_no != port) &&
si->adv_port!=port) {
continue;
}
}
if ( (h_len==si->name.len) &&
(strncasecmp(hname, si->name.s,
si->name.len)==0) /*slower*/)
/* comp. must be case insensitive, host names
* can be written in mixed case, it will also match
* ipv6 addresses if we are lucky*/
goto found;
/* Check if the adv. name of this socket matches */
if ( (h_len==si->adv_name_str.len) &&
(strncasecmp(hname, si->adv_name_str.s,
si->adv_name_str.len)==0) /*slower*/)
/* comp. must be case insensitive, host names
* can be in mixed case, it will also match
* ipv6 addresses if we are lucky*/
goto found;
/* if no advertised is specified on the interface, we should check
* if it is the global address */
if (!si->adv_name_str.len && default_global_address->s &&
h_len == default_global_address->len &&
(strncasecmp(hname, default_global_address->s,
default_global_address->len)==0) /*slower*/)
/* this might match sockets that are not supposed to
* match, when using multiple listeners for the same
* protocol; but in that case the default_global_address
* concept is broken, since there is no way to choose
* the right socket */
goto found;
/* check if host == ip address */
/* ipv6 case is uglier, host can be [3ffe::1] */
ip6=str2ip6(host);
if (ip6){
if (ip_addr_cmp(ip6, &si->address))
goto found; /* match */
else
if (si->adv_name_str.len && ip_addr_cmp(ip6,&si->adv_address))
goto found;
else
continue; /* no match, but this is an ipv6 address
so no point in trying ipv4 */
}
/* ipv4 */
if ( (!(si->flags&SI_IS_IP)) &&
(h_len==si->address_str.len) &&
(memcmp(hname, si->address_str.s,
si->address_str.len)==0)
)
goto found;
}
}while( (proto==0) && (c_proto=next_proto(c_proto)) );
not_found:
return 0;
found:
return si;
}
/* checks if the proto: ip:port is one of the address we listen on
* and returns the corresponding socket_info structure.
* (same as grep_socket_info, but use ip addr instead)
* if port==0, the port number is ignored
* if proto==0 (PROTO_NONE) the protocol is ignored
* returns 0 if not found
* WARNING: uses str2ip6 so it will overwrite any previous
* unsaved result of this function (static buffer)
*/
const struct socket_info* find_si(const struct ip_addr* ip, unsigned short port,
unsigned short proto)
{
const struct socket_info* si = NULL;
struct socket_info_full* sif;
struct socket_info_full** list;
unsigned short c_proto;
c_proto=proto?proto:PROTO_UDP;
do{
/* get the proper sock_list */
list=get_sock_info_list(c_proto);
if (list==0){
LM_WARN("unknown proto %d\n", c_proto);
goto not_found; /* false */
}
for (sif=*list; sif; sif=sif->next){
si = &sif->socket_info;
if (port) {
if (si->port_no!=port) {
continue;
}
}
if (ip_addr_cmp(ip, &si->address) || ip_addr_cmp(ip, &si->adv_address))
goto found;
}
}while( (proto==0) && (c_proto=next_proto(c_proto)) );
not_found:
return 0;
found:
return si;
}
/* parses the specified `spec` and returns an associated
* socket_info*, if it could be found */
const struct socket_info* parse_sock_info(str *addr)
{
int port, proto;
str host;
if (!addr || !addr->s)
return NULL;
if (parse_phostport(addr->s, addr->len, &host.s, &host.len,
&port, &proto) != 0) {
return NULL;
}
return grep_internal_sock_info(&host, (unsigned short) port,
(unsigned short) proto);
}
/* adds a new sock_info structure to the corresponding list
* return 0 on success, -1 on error */
int new_sock2list(struct socket_id *sid, struct socket_info_full** list)
{
struct socket_info_full* si;
si=new_sock_info(sid);
if (si==0){
LM_ERR("new_sock_info failed\n");
goto error;
}
sock_listadd(list, si);
return 0;
error:
return -1;
}
void push_sock2list(struct socket_info_full *si)
{
sock_listadd(&protos[si->socket_info.proto].listeners, si);
}
/* add all family type addresses of interface if_name to the socket_info array
* WARNING: it only works with ipv6 addresses on FreeBSD
* return: -1 on error, 0 on success
*/
static int expand_interface(const struct socket_info *si, struct socket_info_full** list)
{
int ret = -1;
struct ip_addr addr;
struct socket_id sid;
sid.port = si->port_no;
sid.proto = si->proto;
sid.workers = si->workers;
sid.auto_scaling_profile = si->s_profile?si->s_profile->name:NULL;
sid.adv_port = si->adv_port;
sid.adv_name = si->adv_name_str.s; /* it is NULL terminated */
sid.tag = si->tag.s; /* it is NULL terminated */
#ifdef HAVE_IFADDRS
/* use the getifaddrs interface to get all the interfaces */
struct ifaddrs *addrs;
struct ifaddrs *it;
if (getifaddrs(&addrs) != 0) {
LM_ERR("cannot get interfaces list: %s(%d)\n", strerror(errno), errno);
return -1;
}
for (it = addrs; it; it = it->ifa_next) {
if (!it->ifa_addr)
continue;
if (si->name.len == 0 || (strcmp(si->name.s, it->ifa_name) == 0)) {
if (it->ifa_addr->sa_family != AF_INET &&
it->ifa_addr->sa_family != AF_INET6)
continue;
/*
* if it is ipv6, and there was no explicit interface specified,
* make sure we don't add any "scoped" interface
*/
if (it->ifa_addr->sa_family == AF_INET6 &&
(((struct sockaddr_in6 *)(void *)it->ifa_addr)->sin6_scope_id != 0))
continue;
sockaddr2ip_addr(&addr, it->ifa_addr);
if ((sid.name = ip_addr2a(&addr)) == 0)
goto end;
sid.flags = si->flags;
if (it->ifa_flags & IFF_LOOPBACK)
sid.flags |= SI_IS_LO;
if (new_sock2list(&sid, list) != 0) {
LM_ERR("clone_sock2list failed\n");
goto end;
}
ret = 0;
}
}
end:
freeifaddrs(addrs);
return ret;
#else
struct ifconf ifc;
struct ifreq ifr;
struct ifreq ifrcopy;
char* last;
char* p;
int size;
int lastlen;
int s;
#ifdef HAVE_SOCKADDR_SA_LEN
#ifndef MAX
#define MAX(a,b) ( ((a)>(b))?(a):(b))
#endif
#endif
/* ipv4 or ipv6 only*/
s=socket(AF_INET, SOCK_DGRAM, 0);
lastlen=0;
ifc.ifc_req=0;
for (size=100; ; size*=2){
ifc.ifc_len=size*sizeof(struct ifreq);
ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
if (ifc.ifc_req==0){
LM_ERR("memory allocation failure\n");
goto error;
}
if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
if(errno==EBADF) goto error; /* invalid descriptor => no such ifs*/
LM_ERR("ioctl failed: %s\n", strerror(errno));
goto error;
}
if ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
len not changed*/
lastlen=ifc.ifc_len;
/* try a bigger array*/
pkg_free(ifc.ifc_req);
}
last=(char*)ifc.ifc_req+ifc.ifc_len;
for(p=(char*)ifc.ifc_req; p<last;
p+=
#ifdef __OS_linux
sizeof(ifr) /* works on x86_64 too */
#else
(sizeof(ifr.ifr_name)+
#ifdef HAVE_SOCKADDR_SA_LEN
MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
#else
( (ifr.ifr_addr.sa_family==AF_INET)?
sizeof(struct sockaddr_in):
((ifr.ifr_addr.sa_family==AF_INET6)?
sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
#endif
)
#endif
)
{
/* copy contents into ifr structure
* warning: it might be longer (e.g. ipv6 address) */
memcpy(&ifr, p, sizeof(ifr));
if (ifr.ifr_addr.sa_family!=AF_INET){
/*printf("strange family %d skipping...\n",
ifr->ifr_addr.sa_family);*/
continue;
}
/*get flags*/
ifrcopy=ifr;
if (ioctl(s, SIOCGIFFLAGS, &ifrcopy)!=-1){ /* ignore errors */
/* ignore down ifs only if listening on all of them*/
if (si->name.len==0){
/* if if not up, skip it*/
if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
}
}
if (si->name.len == 0 ||
strncmp(si->name.s, ifr.ifr_name, sizeof(ifr.ifr_name))==0){
/*add address*/
sockaddr2ip_addr(&addr,
(struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
if ((sid.name=ip_addr2a(&addr))==0) goto error;
sid.flags = si->flags;
/* check if loopback */
if (ifrcopy.ifr_flags & IFF_LOOPBACK)
sid.flags|=SI_IS_LO;
/* add it to one of the lists */
if (new_sock2list(&sid, list) != 0) {
LM_ERR("clone_sock2list failed\n");
goto error;
}
ret=0;
}
/*
printf("%s:\n", ifr->ifr_name);
printf(" ");
print_sockaddr(&(ifr->ifr_addr));
printf(" ");
ls_ifflags(ifr->ifr_name, family, options);
printf("\n");*/
}
pkg_free(ifc.ifc_req); /*clean up*/
close(s);
return ret;
error:
if (ifc.ifc_req) pkg_free(ifc.ifc_req);
if (s >= 0)
close(s);
return -1;
#endif
}
#define STR_IMATCH(str, buf) ((str).len==strlen(buf) && strncasecmp(buf, (str).s, (str).len)==0)
/* fixes a socket list => resolve addresses,
* interface names, fills missing members, remove duplicates */
int fix_socket_list(struct socket_info_full **list)
{
struct socket_info_full* sif;
struct socket_info* si;
struct socket_info_full* l;
struct socket_info_full* next;
char* tmp;
int len;
struct hostent* he;
char** h;
/* try to change all the interface names into addresses
* --ugly hack */
for (sif=*list;sif;){
next=sif->next;
si = &sif->socket_info;
// fix the SI_IS_LO flag for sockets specified by IP/hostname as expand_interface
// below will only do it for sockets specified using the network interface name
if (STR_IMATCH(si->name, "localhost") ||
STR_IMATCH(si->name, "127.0.0.1") ||
STR_IMATCH(si->name, "0:0:0:0:0:0:0:1") || STR_IMATCH(si->name, "::1")) {
si->flags |= SI_IS_LO;
}
if (expand_interface(si, list)!=-1){
/* success => remove current entry (shift the entire array)*/
sock_listrm(list, sif);
free_sock_info(sif);
}
sif=next;
}
/* get ips & fill the port numbers*/
#ifdef EXTRA_DEBUG
LM_DBG("listening on \n");
#endif
for (sif=*list;sif;sif=sif->next){
si = &sif->socket_info;
/* fix the number of processes per interface */
if (!si->workers && is_udp_based_proto(si->proto))
si->workers = udp_workers_no;
if (si->port_no==0)
si->port_no= protos[si->proto].default_port;
tmp=int2str(si->port_no, &len);
if (len>=MAX_PORT_LEN){
LM_ERR("bad port number: %d\n", si->port_no);
goto error;
}
si->port_no_str.s=(char*)pkg_malloc(len+1);
if (si->port_no_str.s==0){
LM_ERR("out of pkg memory.\n");
goto error;
}
memcpy(si->port_no_str.s, tmp, len+1);
si->port_no_str.len=len;
/* get "official hostnames", all the aliases etc. */
he=resolvehost(si->name.s,0);
if (he==0){
LM_ERR("could not resolve %s\n", si->name.s);
goto error;
}
/* check if we got the official name */
if (strcasecmp(he->h_name, si->name.s)!=0){
if (auto_aliases && add_alias(si->name.s, si->name.len,
si->port_no, si->proto)<0){
LM_ERR("add_alias failed\n");
}
/* change the official name */
pkg_free(si->name.s);
si->name.s=(char*)pkg_malloc(strlen(he->h_name)+1);
if (si->name.s==0){
LM_ERR("out of pkg memory.\n");
goto error;
}
si->name.len=strlen(he->h_name);
memcpy(si->name.s, he->h_name, si->name.len+1);
}
/* add the aliases*/
if (auto_aliases) {
for(h=he->h_aliases; h && *h; h++)
if (add_alias(*h, strlen(*h), si->port_no, si->proto)<0){
LM_ERR("add_alias failed\n");
}
}
hostent2ip_addr(&si->address, he, 0); /*convert to ip_addr
format*/
if ((tmp=ip_addr2a(&si->address))==0) goto error;
if (si->address.af == AF_INET6) {
si->address_str.s=(char*)pkg_malloc(strlen(tmp)+1+2);
if (si->address_str.s==0){
LM_ERR("out of pkg memory.\n");
goto error;
}
si->address_str.s[0] = '[';
memcpy( si->address_str.s+1 , tmp, strlen(tmp));
si->address_str.s[1+strlen(tmp)] = ']';
si->address_str.s[2+strlen(tmp)] = '\0';
si->address_str.len=strlen(tmp) + 2;
} else {
si->address_str.s=(char*)pkg_malloc(strlen(tmp)+1);
if (si->address_str.s==0){
LM_ERR("out of pkg memory.\n");
goto error;
}
memcpy(si->address_str.s, tmp, strlen(tmp)+1);
si->address_str.len=strlen(tmp);
}
/* set is_ip (1 if name is an ip address, 0 otherwise) */
if ( auto_aliases && (si->address_str.len==si->name.len) &&
(strncasecmp(si->address_str.s, si->name.s,
si->address_str.len)==0)
){
si->flags|=SI_IS_IP;
/* do rev. DNS on it (for aliases)*/
he=rev_resolvehost(&si->address);
if (he==0){
LM_WARN("could not rev. resolve %s\n", si->name.s);
}else{
/* add the aliases*/
if (add_alias(he->h_name, strlen(he->h_name),
si->port_no, si->proto)<0){
LM_ERR("add_alias failed\n");
}
for(h=he->h_aliases; h && *h; h++)
if (add_alias(*h,strlen(*h),si->port_no,si->proto)<0){
LM_ERR(" add_alias failed\n");
}
}
}
/* Now build an ip_addr structure for the adv_name, if there is one
* so that find_si can find it later easily. Doing this so that
* we can force_send_socket() on an advertised name. Generally there
* is little interest in dealing with an advertised name as anything
* other than an opaque string that we blindly put into the SIP
* message.
*/
if(si->adv_name_str.len) {
/* If adv_name_str is already an IP, this is kinda foolish cus it
* converts it to ip_addr, then to he, then here we go back to
* ip_addr, but it's either that, or we duplicate the logic to
* check for an ip address here, and still we might have to call
* resolvehost().
*/
he=resolvehost(si->adv_name_str.s,0);
if (he==0){
LM_ERR("ERROR: fix_socket_list: could not resolve "
"advertised name %s\n", si->adv_name_str.s);
goto error;
}
hostent2ip_addr(&si->adv_address, he, 0); /*convert to ip_addr */
if (si->adv_address.af == AF_INET6 /* translates to IPv6 */
&& str2ip6(&si->adv_name_str)!=NULL /* it's an actual IP */
&& si->adv_name_str.s[0]!='[' ) /* not enclosed */
{
tmp = pkg_malloc( si->adv_name_str.len +2 );
if (tmp==NULL) {
LM_ERR("failed to convert advertized IPv6 "
"to enclosed format\n");
goto error;
}
tmp[0] = '[';
memcpy( tmp+1, si->adv_name_str.s, si->adv_name_str.len);
tmp[si->adv_name_str.len+1] = ']';
pkg_free(si->adv_name_str.s);
si->adv_name_str.s = tmp;
si->adv_name_str.len += 2;
}
/* build and set string encoding for the adv socket info
* This is usefful for the usrloc module when it's generating
* or updating the socket on a location record, so we'll generate
* it up front just like the regular sock_str so we don't have
* to worry about it later.
*/
tmp = socket2str( si, 0, &si->adv_sock_str.len, 1);
if (tmp==0) {
LM_ERR("ERROR: fix_socket_list: failed to convert "
"socket to string (adv)\n");
goto error;
}
si->adv_sock_str.s=(char*)pkg_malloc(si->adv_sock_str.len);
if (si->adv_sock_str.s==0) {
LM_ERR("ERROR: fix_socket_list: out of memory.\n");
goto error;
}
memcpy(si->adv_sock_str.s, tmp, si->adv_sock_str.len);
}
if (si->tag.len) {
/* build and set string encoding for the tagged socket info */
tmp = socket2str( si, 0, &si->tag_sock_str.len, 2);
if (tmp==0) {
LM_ERR("failed to convert tag socket to string\n");
goto error;
}
si->tag_sock_str.s=(char*)pkg_malloc(si->tag_sock_str.len);
if (si->tag_sock_str.s==0) {
LM_ERR("out of pkg memory.\n");
goto error;
}
memcpy(si->tag_sock_str.s, tmp, si->tag_sock_str.len);
}
/* build and set string encoding for the real socket info */
tmp = socket2str( si, 0, &si->sock_str.len, 0);
if (tmp==0) {
LM_ERR("failed to convert socket to string\n");
goto error;
}
si->sock_str.s=(char*)pkg_malloc(si->sock_str.len);
if (si->sock_str.s==0) {
LM_ERR("out of pkg memory.\n");
goto error;
}
memcpy(si->sock_str.s, tmp, si->sock_str.len);
#ifdef USE_MCAST
/* Check if it is an multicast address and
* set the flag if so
*/
if (is_mcast(&si->address)) {
si->flags |= SI_IS_MCAST;
}
#endif /* USE_MCAST */
#ifdef EXTRA_DEBUG
printf(" %.*s [%s]:%s%s%s\n", si->name.len,
si->name.s, si->address_str.s, si->port_no_str.s,
si->flags & SI_IS_MCAST ? " mcast" : "",
is_anycast(si) ? " anycast" : "");
#endif
}
/* removing duplicate addresses*/
for (sif=*list;sif; sif=sif->next){
for (l=sif->next;l;){
next=l->next;
si = &sif->socket_info;
const struct socket_info *sl = &l->socket_info;
if ((si->port_no==sl->port_no) &&
(si->address.af==sl->address.af) &&
(memcmp(si->address.u.addr, sl->address.u.addr, si->address.len)
== 0)
){
#ifdef EXTRA_DEBUG
printf("removing duplicate %s [%s] == %s [%s]\n",
si->name.s, si->address_str.s,
sl->name.s, sl->address_str.s);
#endif
/* add the name to the alias list*/
if ((!(sl->flags& SI_IS_IP)) && (
(sl->name.len!=si->name.len)||
(strncmp(sl->name.s, si->name.s, si->name.len)!=0))
)
if (add_alias(sl->name.s,sl->name.len,sl->port_no,sl->proto)<0)
LM_ERR(" add_alias failed\n");
/* remove l*/
sock_listrm(list, l);
free_sock_info(l);
}
l=next;
}
}
#ifdef USE_MCAST
/* Remove invalid multicast entries */
sif=*list;
while(sif){
si = &sif->socket_info;
if ((si->flags & SI_IS_MCAST) &&
(si->proto != PROTO_UDP)
){
LM_WARN("removing entry %s:%s [%s]:%s\n",
get_proto_name(si->proto), si->name.s,
si->address_str.s, si->port_no_str.s);
l = sif;
sif=sif->next;
sock_listrm(list, l);
free_sock_info(l);
} else {
sif=sif->next;
}
}
#endif /* USE_MCAST */
return 0;
error:
return -1;
}
/*
* This function will retrieve a list of all ip addresses and ports that
* OpenSIPS is listening on, with respect to the transport protocol specified
* with 'protocol'.
*
* The first parameter, ipList, is a pointer to a pointer. It will be assigned
* a new block of memory holding the IP Addresses and ports being listened to
* with respect to 'protocol'. The array maps a 2D array into a 1 dimensional
* space, and is layed out as follows:
*
* The first NUM_IP_OCTETS indices will be the IP address, and the next index
* the port. So if NUM_IP_OCTETS is equal to 4 and there are two IP addresses
* found, then:
*
* - ipList[0] will be the first octet of the first ip address
* - ipList[3] will be the last octet of the first ip address.
* - iplist[4] will be the port of the first ip address
* -
* - iplist[5] will be the first octet of the first ip address,
* - and so on.
*
* The function will return the number of sockets which were found. This can
* be used to index into ipList.
*
* NOTE: This function assigns a block of memory equal to:
*
* returnedValue * (NUM_IP_OCTETS + 1) * sizeof(int);
*
* Therefore it is CRUCIAL that you free ipList when you are done with
* its contents, to avoid a nasty memory leak.
*/
int get_socket_list_from_proto(unsigned int **ipList, int protocol) {
struct socket_info_full *sif;
struct socket_info_full ** list;
int num_ip_octets = 4;
int numberOfSockets = 0;
int currentRow = 0;
/* I hate to use #ifdefs, but this is necessary because of the way
* get_sock_info_list() is defined. */
if (protocol == PROTO_TCP)
{
return 0;
}
if (protocol == PROTO_TLS)
{
return 0;
}
/* Retrieve the list of sockets with respect to the given protocol. */
list=get_sock_info_list(protocol);
/* Find out how many sockets are in the list. We need to know this so
* we can malloc an array to assign to ipList. */
for(sif=list?*list:0; sif; sif=sif->next){
/* We only support IPV4 at this point. */
if (sif->socket_info.address.af == AF_INET) {
numberOfSockets++;
}
}
/* There are no open sockets with respect to the given protocol. */
if (numberOfSockets == 0)
{
return 0;
}
*ipList = pkg_malloc(numberOfSockets *
(num_ip_octets + 1) * (int)sizeof(int));
/* We couldn't allocate memory for the IP List. So all we can do is
* fail. */
if (*ipList == NULL) {
LM_ERR("no more pkg memory");
return 0;
}
/* We need to search the list again. So find the front of the list. */
list=get_sock_info_list(protocol);
/* Extract out the IP Addresses and ports. */
for(sif=list?*list:0; sif; sif=sif->next){
const struct socket_info *si = &sif->socket_info;
/* We currently only support IPV4. */
if (si->address.af != AF_INET) {
continue;
}
(*ipList)[currentRow*(num_ip_octets + 1) ] =
si->address.u.addr[0];
(*ipList)[currentRow*(num_ip_octets + 1)+1] =
si->address.u.addr[1];
(*ipList)[currentRow*(num_ip_octets + 1)+2] =