1ccddfd958789a183df8bea9e432c3e3c23eef68
[sip-router] / socket_info.c
1
2 /* $Id$
3  *
4  * find & manage listen addresses 
5  *
6  * Copyright (C) 2001-2003 FhG Fokus
7  *
8  * This file is part of ser, a free SIP server.
9  *
10  * ser is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version
14  *
15  * For a license to use the ser software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * ser is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License 
26  * along with this program; if not, write to the Free Software 
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29 /*
30  * This file contains code that initializes and handles ser listen addresses
31  * lists (struct socket_info). It is used mainly on startup.
32  * 
33  * History:
34  * --------
35  *  2003-10-22  created by andrei
36  *  2004-10-10  added grep_sock_info (andrei)
37  *  2004-11-08  added find_si (andrei)
38  *  2007-08-23  added detection for INADDR_ANY types of sockets (andrei)
39  *  2008-08-08  sctp support (andrei)
40  *  2008-08-15  support for handling sctp multihomed sockets (andrei)
41  *  2008-10-15  fixed protocol list iteration when some protocols are
42  *               compile time disabled (andrei)
43  */
44
45
46 /*!
47  * \file
48  * \brief SIP-router core :: 
49  * \ingroup core
50  * Module: \ref core
51  */
52
53 #include <string.h>
54 #include <errno.h>
55 #include <unistd.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/utsname.h>
59 #include <stdio.h>
60
61 #include <sys/ioctl.h>
62 #include <net/if.h>
63 #ifdef HAVE_SYS_SOCKIO_H
64 #include <sys/sockio.h>
65 #endif
66
67 #include "globals.h"
68 #include "socket_info.h"
69 #include "dprint.h"
70 #include "mem/mem.h"
71 #include "ut.h"
72 #include "resolve.h"
73 #include "name_alias.h"
74
75
76
77 /* list manip. functions (internal use only) */
78
79
80 /* append */
81 #define sock_listadd(head, el) \
82         do{\
83                 if (*(head)==0) *(head)=(el); \
84                 else{ \
85                         for((el)->next=*(head); (el)->next->next;\
86                                         (el)->next=(el)->next->next); \
87                         (el)->next->next=(el); \
88                         (el)->prev=(el)->next; \
89                         (el)->next=0; \
90                 }\
91         }while(0)
92
93
94 /* insert after "after" */
95 #define sock_listins(el, after) \
96         do{ \
97                 if ((after)){\
98                         (el)->next=(after)->next; \
99                         if ((after)->next) (after)->next->prev=(el); \
100                         (after)->next=(el); \
101                         (el)->prev=(after); \
102                 }else{ /* after==0 = list head */ \
103                         (after)=(el); \
104                         (el)->next=(el)->prev=0; \
105                 }\
106         }while(0)
107
108
109 #define sock_listrm(head, el) \
110         do {\
111                 if (*(head)==(el)) *(head)=(el)->next; \
112                 if ((el)->next) (el)->next->prev=(el)->prev; \
113                 if ((el)->prev) (el)->prev->next=(el)->next; \
114         }while(0)
115
116
117 #define addr_info_listadd sock_listadd
118 #define addr_info_listins sock_listins
119 #define addr_info_listrm sock_listrm
120
121 inline static void addr_info_list_ins_lst(struct addr_info* lst,
122                                                                                 struct addr_info* after)
123 {
124         struct addr_info* l;
125         struct addr_info* n;
126         
127         if (lst){
128                 n=after->next;
129                 after->next=lst;
130                 lst->prev=after;
131                 if (n){
132                         for(l=lst; l->next; l=l->next);
133                         l->next=n;
134                         n->prev=l;
135                 }
136         }
137 }
138
139
140 /* protocol order, filled by init_proto_order() */
141 enum sip_protos nxt_proto[PROTO_LAST+1]=
142 { PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP, 0 };
143
144
145
146 /* another helper function, it just fills a struct addr_info
147  * returns: 0 on success, -1 on error*/
148 static int init_addr_info(struct addr_info* a,
149                                                                 char* name, enum si_flags flags)
150 {
151
152         memset(a, 0, sizeof(*a));
153         a->name.len=strlen(name);
154         a->name.s=pkg_malloc(a->name.len+1); /* include \0 */
155         if (a->name.s==0) goto error;
156         memcpy(a->name.s, name, a->name.len+1);
157         a->flags=flags;
158         return 0;
159 error:
160         LOG(L_ERR, "ERROR: init_addr_info: memory allocation error\n");
161         return -1;
162 }
163
164
165
166 /* returns 0 on error, new addr_info_lst element on success */
167 static inline struct addr_info* new_addr_info(char* name, 
168                                                                                                         enum si_flags gf)
169 {
170         struct addr_info* al;
171         
172         al=pkg_malloc(sizeof(*al));
173         if (al==0) goto error;
174         al->next=0;
175         al->prev=0;
176         if (init_addr_info(al, name, gf)!=0) goto error;
177         return al;
178 error:
179         LOG(L_ERR, "ERROR: new_addr_info: memory allocation error\n");
180         if (al){
181                 if (al->name.s) pkg_free(al->name.s);
182                 pkg_free(al);
183         }
184         return 0;
185 }
186
187
188
189 static inline void free_addr_info(struct addr_info* a)
190 {
191         if (a){
192                 if (a->name.s){
193                         pkg_free(a->name.s);
194                         a->name.s=0;
195                 }
196                 pkg_free(a);
197         }
198 }
199
200
201
202 static inline void free_addr_info_lst(struct addr_info** lst)
203 {
204         struct addr_info* a;
205         struct addr_info* tmp;
206         
207         a=*lst;
208         while(a){
209                 tmp=a;
210                 a=a->next;
211                 free_addr_info(tmp);
212         }
213 }
214
215
216
217 /* adds a new add_info_lst element to the corresponding list
218  * returns 0 on success, -1 on error */
219 static int new_addr_info2list(char* name, enum si_flags f,
220                                                                 struct addr_info** l)
221 {
222         struct addr_info * al;
223         
224         al=new_addr_info(name, f);
225         if (al==0) goto error;
226         addr_info_listadd(l, al);
227         return 0;
228 error:
229         return -1;
230 }
231
232
233
234 /* another helper function, it just creates a socket_info struct */
235 static inline struct socket_info* new_sock_info(        char* name,
236                                                                 struct name_lst* addr_l,
237                                                                 unsigned short port, unsigned short proto,
238                                                                 char *usename, unsigned short useport,
239                                                                 enum si_flags flags)
240 {
241         struct socket_info* si;
242         struct name_lst* n;
243         struct hostent* he;
244         char *p;
245         
246         si=(struct socket_info*) pkg_malloc(sizeof(struct socket_info));
247         if (si==0) goto error;
248         memset(si, 0, sizeof(struct socket_info));
249         si->socket=-1;
250         si->name.len=strlen(name);
251         si->name.s=(char*)pkg_malloc(si->name.len+1); /* include \0 */
252         if (si->name.s==0) goto error;
253         memcpy(si->name.s, name, si->name.len+1);
254         /* set port & proto */
255         si->port_no=port;
256         si->proto=proto;
257         si->flags=flags;
258         si->addr_info_lst=0;
259         for (n=addr_l; n; n=n->next){
260                 if (new_addr_info2list(n->name, n->flags, &si->addr_info_lst)!=0){
261                         LOG(L_ERR, "ERROR: new_sockk_info:new_addr_info2list failed\n");
262                         goto error;
263                 }
264         }
265         if(usename!=NULL)
266         {
267                 si->useinfo.name.len=strlen(usename);
268                 si->useinfo.name.s=(char*)pkg_malloc(si->useinfo.name.len+1);
269                 if (si->useinfo.name.s==0)
270                         goto error;
271                 strcpy(si->useinfo.name.s, usename);
272                 if(usename[0]=='[' && usename[si->useinfo.name.len-1]==']')
273                 {
274                         si->useinfo.address_str.len = si->useinfo.name.len - 2;
275                         p = si->useinfo.name.s + 1;
276                 } else {
277                         si->useinfo.address_str.len = si->useinfo.name.len;
278                         p = si->useinfo.name.s;
279                 }
280                 si->useinfo.address_str.s=(char*)pkg_malloc(si->useinfo.address_str.len+1);
281                 if(si->useinfo.address_str.s==NULL)
282                         goto error;
283                 strncpy(si->useinfo.address_str.s, p, si->useinfo.address_str.len);
284                 si->useinfo.address_str.s[si->useinfo.address_str.len] = '\0';
285
286                 p = int2str(useport, &si->useinfo.port_no_str.len);
287                 if(p==NULL)
288                         goto error;
289                 si->useinfo.port_no_str.s=(char*)pkg_malloc(si->useinfo.port_no_str.len+1);
290                 if(si->useinfo.port_no_str.s==NULL)
291                         goto error;
292                 strcpy(si->useinfo.port_no_str.s, p);
293                 si->useinfo.port_no = useport;
294
295                 he=resolvehost(si->useinfo.name.s);
296                 if (he==0){
297                         LM_ERR(" unable to resolve advertised name %s\n", si->useinfo.name.s);
298                         goto error;
299                 }
300                 hostent2ip_addr(&si->useinfo.address, he, 0);
301         }
302         return si;
303 error:
304         LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
305         if (si) {
306                 if(si->name.s)
307                         pkg_free(si->name.s);
308                 pkg_free(si);
309         }
310         return 0;
311 }
312
313
314
315 /*  delete a socket_info struct */
316 static void free_sock_info(struct socket_info* si)
317 {
318         if(si){
319                 if(si->name.s) pkg_free(si->name.s);
320                 if(si->address_str.s) pkg_free(si->address_str.s);
321                 if(si->port_no_str.s) pkg_free(si->port_no_str.s);
322                 if(si->addr_info_lst) free_addr_info_lst(&si->addr_info_lst);
323                 if(si->sock_str.s) pkg_free(si->sock_str.s);
324                 if(si->useinfo.name.s) pkg_free(si->useinfo.name.s);
325                 if(si->useinfo.port_no_str.s) pkg_free(si->useinfo.port_no_str.s);
326                 if(si->useinfo.sock_str.s) pkg_free(si->useinfo.sock_str.s);
327         }
328 }
329
330
331
332 static char* get_valid_proto_name(unsigned short proto)
333 {
334         switch(proto){
335                 case PROTO_NONE:
336                         return "*";
337                 case PROTO_UDP:
338                         return "udp";
339 #ifdef USE_TCP
340                 case PROTO_TCP:
341                         return "tcp";
342 #endif
343 #ifdef USE_TLS
344                 case PROTO_TLS:
345                         return "tls";
346 #endif
347 #ifdef USE_SCTP
348                 case PROTO_SCTP:
349                         return "sctp";
350 #endif
351                 default:
352                         return "unknown";
353         }
354 }
355
356 /** Convert socket to its textual representation.
357  *
358  * This function converts the transport protocol, the IP address and the port
359  * number in a comma delimited string of form proto:ip:port. The resulting
360  * string is NOT zero terminated
361  *
362  * @param s is a pointer to the destination memory buffer
363  * @param len is a pointer to an integer variable. Initially the variable
364  *        should contain the size of the buffer in s. The value of the variable
365  *        will be changed to the length of the resulting string on success and
366  *        to the desired size of the destination buffer if it is too small
367  * @param si is a pointer to the socket_info structure to be printed
368  * @return -1 on error and 0 on success
369  */
370 int socket2str(char* s, int* len, struct socket_info* si)
371 {
372         return socketinfo2str(s, len, si, 0);
373 }
374
375 int socketinfo2str(char* s, int* len, struct socket_info* si, int mode)
376 {
377         str proto;
378         int l;
379         
380         proto.s = get_valid_proto_name(si->proto);
381         proto.len = strlen(proto.s);
382         
383         if(mode==1)
384                 l = proto.len + si->useinfo.name.len + si->useinfo.port_no_str.len + 2;
385         else
386                 l = proto.len + si->address_str.len + si->port_no_str.len + 2;
387
388         if(si->address.af==AF_INET6)
389                 l += 2;
390         
391         if (*len < l) {
392                 LM_ERR("Destionation buffer too short\n");
393                 *len = l;
394                 return -1;
395         }
396         
397         memcpy(s, proto.s, proto.len);
398         s += proto.len;
399         *s = ':'; s++;
400         if(mode==1){
401                 memcpy(s, si->useinfo.name.s, si->useinfo.name.len);
402                 s += si->useinfo.name.len;
403                 *s = ':'; s++;
404                 memcpy(s, si->useinfo.port_no_str.s, si->useinfo.port_no_str.len);
405                 s += si->useinfo.port_no_str.len;
406         } else {
407                 if(si->address.af==AF_INET6) {
408                         *s = '['; s++;
409                 }
410                 memcpy(s, si->address_str.s, si->address_str.len);
411                 s += si->address_str.len;
412                 if(si->address.af==AF_INET6) {
413                         *s = ']'; s++;
414                 }
415                 *s = ':'; s++;
416                 memcpy(s, si->port_no_str.s, si->port_no_str.len);
417                 s += si->port_no_str.len;
418         }
419
420         *len = l;
421         return 0;
422 }
423
424
425
426 /* Fill si->sock_str with string representing the socket_info structure,
427  * format of the string is 'proto:address:port'. Returns 0 on success and
428  * negative number on failure.
429  */
430 static int fix_sock_str(struct socket_info* si)
431 {
432         int len = MAX_SOCKET_STR;
433
434         if (si->sock_str.s) pkg_free(si->sock_str.s);
435         
436         si->sock_str.s = pkg_malloc(len + 1);
437         if (si->sock_str.s == NULL) {
438                 ERR("fix_sock_str: No memory left\n");
439                 return -1;
440         }
441         if (socketinfo2str(si->sock_str.s, &len, si, 0) < 0) {
442                 BUG("fix_sock_str: Error in socket to str\n");
443                 return -1;
444         }
445         si->sock_str.s[len] = '\0';
446         si->sock_str.len = len;
447         if(si->useinfo.name.s!=NULL)
448         {
449                 len = MAX_SOCKET_STR;
450
451                 if (si->useinfo.sock_str.s) pkg_free(si->useinfo.sock_str.s);
452
453                 si->useinfo.sock_str.s = pkg_malloc(len + 1);
454                 if (si->useinfo.sock_str.s == NULL) {
455                         ERR("fix_sock_str: No memory left\n");
456                         return -1;
457                 }
458                 if (socketinfo2str(si->useinfo.sock_str.s, &len, si, 1) < 0) {
459                         BUG("fix_sock_str: Error in socket to str\n");
460                         return -1;
461                 }
462                 si->useinfo.sock_str.s[len] = '\0';
463                 si->useinfo.sock_str.len = len;
464         }
465         return 0;
466 }
467
468
469 /* returns 0 if support for the protocol is not compiled or if proto is 
470    invalid */
471 struct socket_info** get_sock_info_list(unsigned short proto)
472 {
473         
474         switch(proto){
475                 case PROTO_UDP:
476                         return &udp_listen;
477                         break;
478                 case PROTO_TCP:
479 #ifdef USE_TCP
480                         return &tcp_listen;
481 #endif
482                         break;
483                 case PROTO_TLS:
484 #ifdef USE_TLS
485                         return &tls_listen;
486 #endif
487                         break;
488                 case PROTO_SCTP:
489 #ifdef USE_SCTP
490                         return &sctp_listen;
491 #endif
492                         break;
493                 default:
494                         LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
495         }
496         return 0;
497 }
498
499
500 /* helper function for grep_sock_info
501  * params:
502  *  host - hostname to compare with
503  *  name - official name
504  *  addr_str - name's resolved ip address converted to string
505  *  ip_addr - name's ip address 
506  *  flags - set to SI_IS_IP if name contains an IP
507  *
508  * returns 0 if host matches, -1 if not */
509 inline static int si_hname_cmp(str* host, str* name, str* addr_str, 
510                                                                 struct ip_addr* ip_addr, int flags)
511 {
512 #ifdef USE_IPV6
513         struct ip_addr* ip6;
514 #endif
515         
516         if ( (host->len==name->len) && 
517                 (strncasecmp(host->s, name->s, name->len)==0) /*slower*/)
518                 /* comp. must be case insensitive, host names
519                  * can be written in mixed case, it will also match
520                  * ipv6 addresses if we are lucky*/
521                 goto found;
522         /* check if host == ip address */
523 #ifdef USE_IPV6
524         /* ipv6 case is uglier, host can be [3ffe::1] */
525         ip6=str2ip6(host);
526         if (ip6){
527                 if (ip_addr_cmp(ip6, ip_addr))
528                         goto found; /* match */
529                 else
530                         return -1; /* no match, but this is an ipv6 address
531                                                  so no point in trying ipv4 */
532         }
533 #endif
534         /* ipv4 */
535         if ( (!(flags&SI_IS_IP)) && (host->len==addr_str->len) && 
536                         (memcmp(host->s, addr_str->s, addr_str->len)==0) )
537                 goto found;
538         return -1;
539 found:
540         return 0;
541 }
542
543
544 /* checks if the proto: host:port is one of the address we listen on
545  * and returns the corresponding socket_info structure.
546  * if port==0, the  port number is ignored
547  * if proto==0 (PROTO_NONE) the protocol is ignored
548  * returns  0 if not found
549  * WARNING: uses str2ip6 so it will overwrite any previous
550  *  unsaved result of this function (static buffer)
551  */
552 struct socket_info* grep_sock_info(str* host, unsigned short port,
553                                                                                                 unsigned short proto)
554 {
555         str hname;
556         struct socket_info* si;
557         struct socket_info** list;
558         struct addr_info* ai;
559         unsigned short c_proto;
560         
561         hname=*host;
562 #ifdef USE_IPV6
563         if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
564                 /* ipv6 reference, skip [] */
565                 hname.s++;
566                 hname.len-=2;
567         }
568 #endif
569         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
570         do{
571                 /* get the proper sock_list */
572                 list=get_sock_info_list(c_proto);
573         
574                 if (list==0) /* disabled or unknown protocol */
575                         continue;
576                 for (si=*list; si; si=si->next){
577                         DBG("grep_sock_info - checking if host==us: %d==%d &&"
578                                         " [%.*s] == [%.*s]\n",
579                                                 hname.len,
580                                                 si->name.len,
581                                                 hname.len, hname.s,
582                                                 si->name.len, si->name.s
583                                 );
584                         if (port) {
585                                 DBG("grep_sock_info - checking if port %d (advertise %d)"
586                                                 " matches port %d\n",
587                                                 si->port_no, si->useinfo.port_no, port);
588                                 if (si->port_no!=port && si->useinfo.port_no!=port) {
589                                         continue;
590                                 }
591                         }
592                         if (si_hname_cmp(&hname, &si->name, &si->address_str, 
593                                                                 &si->address, si->flags)==0)
594                                 goto found;
595                         if(si->useinfo.name.s!=NULL)
596                         {
597                                 DBG("grep_sock_info - checking advertise if host==us:"
598                                                 " %d==%d && [%.*s] == [%.*s]\n",
599                                                 hname.len,
600                                                 si->useinfo.name.len,
601                                                 hname.len, hname.s,
602                                                 si->useinfo.name.len, si->useinfo.name.s
603                                 );
604                                 if (si_hname_cmp(&hname, &si->useinfo.name,
605                                                         &si->useinfo.address_str, &si->useinfo.address,
606                                                         si->flags)==0)
607                                         goto found;
608                         }
609                         /* try among the extra addresses */
610                         for (ai=si->addr_info_lst; ai; ai=ai->next)
611                                 if (si_hname_cmp(&hname, &ai->name, &ai->address_str, 
612                                                                         &ai->address, ai->flags)==0)
613                                         goto found;
614                 }
615         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
616 /* not_found: */
617         return 0;
618 found:
619         return si;
620 }
621
622 /* checks if the proto:port is one of the ports we listen on
623  * and returns the corresponding socket_info structure.
624  * if proto==0 (PROTO_NONE) the protocol is ignored
625  * returns  0 if not found
626  */
627 struct socket_info* grep_sock_info_by_port(unsigned short port, 
628                                                                                         unsigned short proto)
629 {
630         struct socket_info* si;
631         struct socket_info** list;
632         unsigned short c_proto;
633
634         if (!port) {
635                 goto not_found;
636         }
637         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
638         do{
639                 /* get the proper sock_list */
640                 list=get_sock_info_list(c_proto);
641         
642                 if (list==0) /* disabled or unknown protocol */
643                         continue;
644                 
645                 for (si=*list; si; si=si->next){
646                         DBG("grep_sock_info_by_port - checking if port %d matches"
647                                         " port %d\n", si->port_no, port);
648                         if (si->port_no==port) {
649                                 goto found;
650                         }
651                 }
652         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
653 not_found:
654         return 0;
655 found:
656         return si;
657 }
658
659
660
661 /* checks if the proto: ip:port is one of the address we listen on
662  * and returns the corresponding socket_info structure.
663  * (same as grep_socket_info, but use ip addr instead)
664  * if port==0, the  port number is ignored
665  * if proto==0 (PROTO_NONE) the protocol is ignored
666  * returns  0 if not found
667  * WARNING: uses str2ip6 so it will overwrite any previous
668  *  unsaved result of this function (static buffer)
669  */
670 struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
671                                                                                                 unsigned short proto)
672 {
673         struct socket_info* si;
674         struct socket_info** list;
675         struct addr_info* ai;
676         unsigned short c_proto;
677         
678         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
679         do{
680                 /* get the proper sock_list */
681                 list=get_sock_info_list(c_proto);
682         
683                 if (list==0) /* disabled or unknown protocol */
684                         continue;
685                 
686                 for (si=*list; si; si=si->next){
687                         if (port) {
688                                 if (si->port_no!=port) {
689                                         continue;
690                                 }
691                         }
692                         if (ip_addr_cmp(ip, &si->address)
693                                         || ip_addr_cmp(ip, &si->useinfo.address))
694                                 goto found;
695                         for (ai=si->addr_info_lst; ai; ai=ai->next)
696                                 if (ip_addr_cmp(ip, &ai->address))
697                                         goto found;
698                 }
699         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
700 /* not_found: */
701         return 0;
702 found:
703         return si;
704 }
705
706
707
708 /* append a new sock_info structure to the corresponding list
709  * return  new sock info on success, 0 on error */
710 static struct socket_info* new_sock2list(char* name, struct name_lst* addr_l,
711                                                                         unsigned short port,
712                                                                         unsigned short proto,
713                                                                         char *usename, unsigned short useport,
714                                                                         enum si_flags flags,
715                                                                         struct socket_info** list)
716 {
717         struct socket_info* si;
718         
719         si=new_sock_info(name, addr_l, port, proto, usename, useport, flags);
720         if (si==0){
721                 LOG(L_ERR, "ERROR: new_sock2list: new_sock_info failed\n");
722                 goto error;
723         }
724         if(socket_workers>0) {
725                 si->workers = socket_workers;
726                 socket_workers = 0;
727         }
728         sock_listadd(list, si);
729         return si;
730 error:
731         return 0;
732 }
733
734
735
736 /* adds a new sock_info structure immediately after "after"
737  * return  new sock info on success, 0 on error */
738 static struct socket_info* new_sock2list_after(char* name,
739                                                                         struct name_lst* addr_l,
740                                                                         unsigned short port,
741                                                                         unsigned short proto,
742                                                                         char *usename,
743                                                                         unsigned short useport,
744                                                                         enum si_flags flags,
745                                                                         struct socket_info* after)
746 {
747         struct socket_info* si;
748         
749         si=new_sock_info(name, addr_l, port, proto, usename, useport, flags);
750         if (si==0){
751                 LOG(L_ERR, "ERROR: new_sock2list_after: new_sock_info failed\n");
752                 goto error;
753         }
754         sock_listins(si, after);
755         return si;
756 error:
757         return 0;
758 }
759
760
761
762 /* adds a sock_info structure to the corresponding proto list
763  * return  0 on success, -1 on error */
764 int add_listen_advertise_iface(char* name, struct name_lst* addr_l,
765                                                 unsigned short port, unsigned short proto,
766                                                 char *usename, unsigned short useport,
767                                                 enum si_flags flags)
768 {
769         struct socket_info** list;
770         unsigned short c_proto;
771         struct name_lst* a_l;
772         unsigned short c_port;
773         
774         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
775         do{
776                 list=get_sock_info_list(c_proto);
777                 if (list==0) /* disabled or unknown protocol */
778                         continue;
779                 
780                 if (port==0){ /* use default port */
781                         c_port=
782 #ifdef USE_TLS
783                                 ((c_proto)==PROTO_TLS)?tls_port_no:
784 #endif
785                                 port_no;
786                 }
787 #ifdef USE_TLS
788                 else if ((c_proto==PROTO_TLS) && (proto==0)){
789                         /* -l  ip:port => on udp:ip:port; tcp:ip:port and tls:ip:port+1?*/
790                         c_port=port+1;
791                 }
792 #endif
793                 else{
794                         c_port=port;
795                 }
796                 if (c_proto!=PROTO_SCTP){
797                         if (new_sock2list(name, 0, c_port, c_proto, usename, useport,
798                                                                 flags & ~SI_IS_MHOMED, list)==0){
799                                 LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
800                                 goto error;
801                         }
802                         /* add the other addresses in the list as separate sockets
803                          * since only SCTP can bind to multiple addresses */
804                         for (a_l=addr_l; a_l; a_l=a_l->next){
805                                 if (new_sock2list(a_l->name, 0, c_port, 
806                                                                         c_proto, usename, useport,
807                                                                         flags & ~SI_IS_MHOMED, list)==0){
808                                         LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list"
809                                                                 " failed\n");
810                                         goto error;
811                                 }
812                         }
813                 }else{
814                         if (new_sock2list(name, addr_l, c_port, c_proto, usename, useport,
815                                                 flags, list)==0){
816                                 LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
817                                 goto error;
818                         }
819                 }
820         }while( (proto==0) && (c_proto=next_proto(c_proto)));
821         return 0;
822 error:
823         return -1;
824 }
825
826 /* adds a sock_info structure to the corresponding proto list
827  * return  0 on success, -1 on error */
828 int add_listen_iface(char* name, struct name_lst* addr_l,
829                                                 unsigned short port, unsigned short proto,
830                                                 enum si_flags flags)
831 {
832         return add_listen_advertise_iface(name, addr_l, port, proto, 0, 0, flags);
833 }
834 #ifdef __OS_linux
835
836 #include "linux/netlink.h"
837 #include "linux/rtnetlink.h"
838 #include "arpa/inet.h"
839
840
841 #define MAX_IF_LEN 64
842 struct idx
843 {
844         struct idx *    next;
845         int             family;
846         unsigned        ifa_flags;
847         char            addr[MAX_IF_LEN];
848
849 };
850
851 struct idxlist{
852         struct idx*     addresses;
853         int             index;
854         char            name[MAX_IF_LEN];
855         unsigned        flags;
856 };
857
858 #define MAX_IFACE_NO 32
859
860 static struct idxlist *ifaces = NULL;
861 static int seq = 0;
862
863 #define SADDR(s) ((struct sockaddr_in*)s)->sin_addr.s_addr
864
865 #define NLMSG_TAIL(nmsg) \
866         ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
867
868 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
869               int alen)
870 {
871         int len = RTA_LENGTH(alen);
872         struct rtattr *rta;
873
874         if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
875                 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
876                 return -1;
877         }
878         rta = NLMSG_TAIL(n);
879         rta->rta_type = type;
880         rta->rta_len = len;
881         memcpy(RTA_DATA(rta), data, alen);
882         n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
883         return 0;
884 }
885
886
887
888 static int nl_bound_sock(void)
889 {
890         int sock;
891         struct sockaddr_nl la;
892
893         sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
894         if(sock <= 0){
895                 LM_ERR("could not create NETLINK sock to get interface list");
896                 goto error;
897         }
898
899         /* bind NETLINK socket to pid */
900         bzero(&la, sizeof(la));
901         la.nl_family = AF_NETLINK;
902         la.nl_pad = 0;
903         la.nl_pid = getpid();
904         la.nl_groups = 0;
905         if ( bind(sock, (struct sockaddr*) &la, sizeof(la)) < 0){
906                 LM_ERR("could not bind NETLINK sock to sockaddr_nl\n");
907                 goto error;
908         }
909
910         return sock;
911 error:
912         if(sock > 0) close(sock);
913         return -1;
914 }
915
916 #define fill_nl_req(req, type, family) do {\
917         memset(&req, 0, sizeof(req));\
918         req.nlh.nlmsg_len = sizeof(req);\
919         req.nlh.nlmsg_type = type;\
920         req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST|NLM_F_DUMP;\
921         req.nlh.nlmsg_pid = getpid();\
922         req.nlh.nlmsg_seq = seq++;\
923         req.g.rtgen_family = family;\
924         } while(0);
925
926         
927 static int get_flags(int family){
928         struct {
929                 struct nlmsghdr nlh;
930                 struct rtgenmsg g;
931         } req;
932         int rtn = 0;
933         struct nlmsghdr*  nlp;
934         struct ifinfomsg *ifi;
935         char buf[8192];
936         char *p = buf;
937         int nll = 0;
938         int nl_sock = 0;
939
940         fill_nl_req(req, RTM_GETLINK, AF_INET);
941
942         if((nl_sock = nl_bound_sock()) < 0) return -1;
943
944         if(send(nl_sock, (void*)&req, sizeof(req), 0) < 0)
945         {
946                 LM_ERR("error sending NETLINK request\n");
947                 goto error;
948         }
949
950         while(1) {
951                 rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
952                 nlp = (struct nlmsghdr *) p;
953                 if(nlp->nlmsg_type == NLMSG_DONE){
954                         LM_DBG("done\n");
955                          break;
956                 }
957                 if(nlp->nlmsg_type == NLMSG_ERROR){
958                          LM_DBG("Error on message to netlink");
959                          break;
960                 }
961                 p += rtn;
962
963                 nll += rtn;
964         }
965
966         nlp = (struct nlmsghdr *) buf;
967         for(;NLMSG_OK(nlp, nll);nlp=NLMSG_NEXT(nlp, nll)){
968                 ifi = NLMSG_DATA(nlp);
969
970                 if (nlp->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
971                         goto error;
972
973                 LM_ERR("Interface with index %d has flags %d\n", ifi->ifi_index, ifi->ifi_flags);
974                 if(ifaces == NULL){
975                         LM_ERR("get_flags must not be called on empty interface list");
976                         goto error;
977                 }
978                 if(ifi->ifi_index >= MAX_IFACE_NO){
979                         LM_ERR("invalid network interface index returned %d", ifi->ifi_index);
980                         goto error;
981                 }
982                 ifaces[ifi->ifi_index].flags = ifi->ifi_flags;
983         }
984
985         if(nl_sock>0) close(nl_sock);
986         return 0;
987
988 error:
989         if(nl_sock>0) close(nl_sock);
990         return -1;
991 }
992
993 static int build_iface_list(void)
994 {
995         struct {
996                 struct nlmsghdr nlh;
997                 struct rtgenmsg g;
998         } req;
999
1000         int seq = 0;
1001         int rtn = 0;
1002         struct nlmsghdr*  nlp;
1003         struct ifaddrmsg *ifi;
1004         int rtl;
1005         char buf[8192];
1006         char *p = buf;
1007         int nll = 0;
1008         struct rtattr * rtap;
1009         int index, i;
1010         struct idx* entry;
1011         struct idx* tmp;
1012         int nl_sock = 0;
1013         int families[] = {AF_INET, AF_INET6};
1014         char name[MAX_IF_LEN];
1015         int is_link_local = 0;
1016
1017         if(ifaces == NULL){
1018                 if((ifaces = (struct idxlist*)pkg_malloc(MAX_IFACE_NO*sizeof(struct idxlist))) == NULL){
1019                         LM_ERR("No more pkg memory\n");
1020                         return -1;
1021                 }
1022                 memset(ifaces, 0, sizeof(struct idxlist)*MAX_IFACE_NO);
1023         }
1024
1025         /* bind netlink socket */
1026         if((nl_sock = nl_bound_sock()) < 0) return -1;
1027
1028         for (i = 0 ; i < sizeof(families)/sizeof(int); i++) {
1029                 fill_nl_req(req, RTM_GETADDR, families[i]);
1030
1031                 if(send(nl_sock, (void*)&req, sizeof(req), 0) < 0){
1032                         LM_ERR("error sending NETLINK request\n");
1033                         goto error;
1034                 };
1035
1036                 memset(buf, 0, sizeof(buf));
1037                 nll = 0;
1038                 p = buf;
1039                 while(1) {
1040                         rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
1041                         LM_DBG("received %d byles \n", rtn);
1042                         nlp = (struct nlmsghdr *) p;
1043                         if(nlp->nlmsg_type == NLMSG_DONE){
1044                                 LM_DBG("done receiving netlink info \n");
1045                                  break;
1046                         }
1047                         if(nlp->nlmsg_type == NLMSG_ERROR){
1048                                  LM_ERR("Error on message to netlink");
1049                                  break;
1050                         }
1051                         p += rtn;
1052
1053                         nll += rtn;
1054                 }
1055
1056                 nlp = (struct nlmsghdr *) buf;
1057                 for(;NLMSG_OK(nlp, nll);nlp=NLMSG_NEXT(nlp, nll)){
1058                         ifi = NLMSG_DATA(nlp);
1059
1060                         if (nlp->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
1061                                 continue;
1062                         // init all the strings
1063                         // inner loop: loop thru all the attributes of
1064                         // one route entry
1065                         rtap = (struct rtattr *) IFA_RTA(ifi);
1066
1067                         rtl = IFA_PAYLOAD(nlp);
1068
1069                         index = ifi->ifa_index;
1070                         if(index >= MAX_IFACE_NO){
1071                                 LM_ERR("Invalid interface index returned: %d\n", index);
1072                                 goto error;
1073                         }
1074
1075                         entry = (struct idx*)pkg_malloc(sizeof(struct idx));
1076                         if(entry == 0)
1077                         {
1078                                 LM_ERR("could not allocate memory\n");
1079                                 goto error;
1080                         }
1081
1082                         entry->next = 0;
1083                         entry->family = families[i];
1084                         entry->ifa_flags = ifi->ifa_flags;
1085                         is_link_local = 0;
1086
1087                         for(;RTA_OK(rtap, rtl);rtap=RTA_NEXT(rtap,rtl)){
1088                                 switch(rtap->rta_type){
1089                                         case IFA_ADDRESS:
1090                                                 if((*(int*)RTA_DATA(rtap))== htons(0xfe80)){
1091                                                         LM_DBG("Link Local Address, ignoring ...\n");
1092                                                         is_link_local = 1;
1093                                                         break;
1094                                                 }
1095                                                 inet_ntop(families[i], RTA_DATA(rtap), entry->addr, MAX_IF_LEN);
1096                                                 LM_DBG("iface <IFA_ADDRESS> addr is  %s\n", entry->addr);
1097                                                 break;
1098                                         case IFA_LOCAL:
1099                                                 if((*(int*)RTA_DATA(rtap))== htons(0xfe80)){
1100                                                         LM_DBG("Link Local Address, ignoring ...\n");
1101                                                         is_link_local = 1;
1102                                                 }
1103                                                 inet_ntop(families[i], RTA_DATA(rtap), entry->addr, MAX_IF_LEN);
1104                                                 LM_DBG("iface <IFA_LOCAL> addr is %s\n", entry->addr);
1105                                                 break;
1106                                         case IFA_LABEL:
1107                                                 LM_DBG("iface name is %s\n", (char*)RTA_DATA(rtap));
1108                                                 strncpy(name, (char*)RTA_DATA(rtap), MAX_IF_LEN);
1109                                                 break;
1110                                         case IFA_BROADCAST:
1111                                         case IFA_ANYCAST:
1112                                         case IFA_UNSPEC:
1113                                         case IFA_CACHEINFO:
1114                                         default:
1115                                                 break;
1116                                 }
1117                         }
1118                         if(is_link_local) continue;    /* link local addresses are not bindable */
1119
1120                         if(strlen(ifaces[index].name)==0)
1121                                 strncpy(ifaces[index].name, name, MAX_IF_LEN);
1122
1123                         ifaces[index].index = index;
1124
1125                         if(ifaces[index].addresses == 0 )
1126                                 ifaces[index].addresses = entry;
1127                         else {
1128                                 for(tmp = ifaces[index].addresses; tmp->next ; tmp = tmp->next)/*empty*/;
1129                                 tmp->next = entry;
1130                         }
1131                 }
1132         }
1133         if(nl_sock>0) close(nl_sock);
1134         /* the socket should be closed so we can bind again */
1135         for(i = 0; i < sizeof(families)/sizeof(int); i++){
1136                 /* get device flags */
1137                 get_flags(families[i]); /* AF_INET or AF_INET6 */
1138         }
1139
1140         return 0;
1141 error:
1142         if(nl_sock>0) close(nl_sock);
1143         return -1;
1144
1145 }
1146 /* add all family type addresses of interface if_to the socket_info array
1147  * if if_name==0, adds all addresses on all interfaces
1148  * uses RTNETLINK sockets to get addresses on the present interface on LINUX
1149  * return: -1 on error, 0 on success
1150  */
1151 int add_interfaces_via_netlink(char* if_name, int family, unsigned short port,
1152                                         unsigned short proto,
1153                                         struct addr_info** ai_l)
1154 {
1155         int i;
1156         struct idx* tmp;
1157         enum si_flags flags;
1158
1159         if(ifaces == NULL && (build_iface_list()!=0)){
1160                 LM_ERR("Could not get network interface list\n");
1161                 return -1;
1162         }
1163
1164         flags=SI_NONE;
1165         for(i=0; i< MAX_IFACE_NO; ++i){
1166                 if(ifaces[i].addresses == NULL) continue; /* not present/configured */
1167                 if ((if_name==0)||
1168                         (strncmp(if_name, ifaces[i].name, strlen(ifaces[i].name))==0)){
1169
1170                         /* check if iface is up */
1171                         //if(! (ifaces[i].flags & IFF_UP) ) continue;
1172
1173                         for(tmp = ifaces[i].addresses; tmp; tmp = tmp->next){
1174                                 LM_DBG("\t in add_iface_via_netlink Name %s Address %s\n", ifaces[i].name, tmp->addr);
1175                                 /* match family */
1176                                 if (family == tmp->family){
1177                                         /* check if loopback */
1178                                         if (ifaces[i].flags & IFF_LOOPBACK){
1179                                                 LM_DBG("INTERFACE %s is loopback", ifaces[i].name);
1180                                                 flags|=SI_IS_LO;
1181                                         }
1182                                         /* save the info */
1183                                         if (new_addr_info2list(tmp->addr, flags, ai_l)!=0){
1184                                                 LOG(L_ERR, "ERROR: add_interfaces: "
1185                                                         "new_addr_info2list failed\n");
1186                                                 goto error;
1187                                         }
1188                                 }
1189                         }
1190                 }
1191         }
1192         return 0;
1193 error:
1194         return -1;
1195 }
1196 #endif /* __OS_linux */
1197
1198 /* add all family type addresses of interface if_name to the socket_info array
1199  * if if_name==0, adds all addresses on all interfaces
1200  * WARNING: it only works with ipv6 addresses on FreeBSD
1201  * return: -1 on error, 0 on success
1202  */
1203 int add_interfaces(char* if_name, int family, unsigned short port,
1204                                         unsigned short proto,
1205                                         struct addr_info** ai_l)
1206 {
1207         struct ifconf ifc;
1208         struct ifreq ifr;
1209         struct ifreq ifrcopy;
1210         char*  last;
1211         char* p;
1212         int size;
1213         int lastlen;
1214         int s;
1215         char* tmp;
1216         struct ip_addr addr;
1217         int ret;
1218         enum si_flags flags;
1219
1220 #ifdef HAVE_SOCKADDR_SA_LEN
1221         #ifndef MAX
1222                 #define MAX(a,b) ( ((a)>(b))?(a):(b))
1223         #endif
1224 #endif
1225         /* ipv4 or ipv6 only*/
1226         flags=SI_NONE;
1227         s=socket(family, SOCK_DGRAM, 0);
1228         ret=-1;
1229         lastlen=0;
1230         ifc.ifc_req=0;
1231         for (size=100; ; size*=2){
1232                 ifc.ifc_len=size*sizeof(struct ifreq);
1233                 ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
1234                 if (ifc.ifc_req==0){
1235                         LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
1236                         goto error;
1237                 }
1238                 if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
1239                         if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
1240                         LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
1241                                         strerror(errno));
1242                         goto error;
1243                 }
1244                 if  ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
1245                                                                                                                    len not changed*/
1246                 lastlen=ifc.ifc_len;
1247                 /* try a bigger array*/
1248                 pkg_free(ifc.ifc_req);
1249         }
1250         
1251         last=(char*)ifc.ifc_req+ifc.ifc_len;
1252         for(p=(char*)ifc.ifc_req; p<last;
1253                         p+=
1254                         #ifdef __OS_linux
1255                                 sizeof(ifr) /* works on x86_64 too */
1256                         #else
1257                                 (sizeof(ifr.ifr_name)+
1258                                 #ifdef  HAVE_SOCKADDR_SA_LEN
1259                                         MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
1260                                 #else
1261                                         ( (ifr.ifr_addr.sa_family==AF_INET)?
1262                                                 sizeof(struct sockaddr_in):
1263                                         #ifdef USE_IPV6
1264                                                 ((ifr.ifr_addr.sa_family==AF_INET6)?
1265                                                 sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
1266                                         #else /* USE_IPV6 */
1267                                                 sizeof(struct sockaddr) )
1268                                         #endif /* USE_IPV6 */
1269                                 #endif
1270                                 )
1271                         #endif
1272                 )
1273         {
1274                 /* copy contents into ifr structure
1275                  * warning: it might be longer (e.g. ipv6 address) */
1276                 memcpy(&ifr, p, sizeof(ifr));
1277                 if (ifr.ifr_addr.sa_family!=family){
1278                         /*printf("strange family %d skipping...\n",
1279                                         ifr->ifr_addr.sa_family);*/
1280                         continue;
1281                 }
1282                 
1283                 /*get flags*/
1284                 ifrcopy=ifr;
1285                 if (ioctl(s, SIOCGIFFLAGS,  &ifrcopy)!=-1){ /* ignore errors */
1286                         /* ignore down ifs only if listening on all of them*/
1287                         if (if_name==0){ 
1288                                 /* if if not up, skip it*/
1289                                 if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
1290                         }
1291                 }
1292                 
1293                 
1294                 
1295                 if ((if_name==0)||
1296                         (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
1297                         
1298                         /*add address*/
1299                         sockaddr2ip_addr(&addr, 
1300                                         (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
1301                         if ((tmp=ip_addr2a(&addr))==0) goto error;
1302                         /* check if loopback */
1303                         if (ifrcopy.ifr_flags & IFF_LOOPBACK) 
1304                                 flags|=SI_IS_LO;
1305                         /* save the info */
1306                         if (new_addr_info2list(tmp, flags, ai_l)!=0){
1307                                 LOG(L_ERR, "ERROR: add_interfaces: "
1308                                                 "new_addr_info2list failed\n");
1309                                 goto error;
1310                         }
1311                         ret=0;
1312                 }
1313                         /*
1314                         printf("%s:\n", ifr->ifr_name);
1315                         printf("        ");
1316                         print_sockaddr(&(ifr->ifr_addr));
1317                         printf("        ");
1318                         ls_ifflags(ifr->ifr_name, family, options);
1319                         printf("\n");*/
1320         }
1321         pkg_free(ifc.ifc_req); /*clean up*/
1322         close(s);
1323         return  ret;
1324 error:
1325         if (ifc.ifc_req) pkg_free(ifc.ifc_req);
1326         close(s);
1327         return -1;
1328 }
1329
1330
1331
1332 /* internal helper function: resolve host names and add aliases
1333  * name is a value result parameter: it should contain the hostname that
1334  * will be used to fill all the other members, including name itself
1335  * in some situation (name->s should be a 0 terminated pkg_malloc'ed string)
1336  * return 0 on success and -1 on error */
1337 static int fix_hostname(str* name, struct ip_addr* address, str* address_str,
1338                                                 enum si_flags* flags, int* type_flags,
1339                                                 struct socket_info* s)
1340 {
1341         struct hostent* he;
1342         char* tmp;
1343         char** h;
1344         
1345         /* get "official hostnames", all the aliases etc. */
1346         he=resolvehost(name->s);
1347         if (he==0){
1348                 LOG(L_ERR, "ERROR: fix_hostname: could not resolve %s\n", name->s);
1349                 goto error;
1350         }
1351         /* check if we got the official name */
1352         if (strcasecmp(he->h_name, name->s)!=0){
1353                 if (sr_auto_aliases && 
1354                                 add_alias(name->s, name->len, s->port_no, s->proto)<0){
1355                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
1356                 }
1357                 /* change the official name */
1358                 pkg_free(name->s);
1359                 name->s=(char*)pkg_malloc(strlen(he->h_name)+1);
1360                 if (name->s==0){
1361                         LOG(L_ERR,  "ERROR: fix_hostname: out of memory.\n");
1362                         goto error;
1363                 }
1364                 name->len=strlen(he->h_name);
1365                 strncpy(name->s, he->h_name, name->len+1);
1366         }
1367         /* add the aliases*/
1368         for(h=he->h_aliases; sr_auto_aliases && h && *h; h++)
1369                 if (add_alias(*h, strlen(*h), s->port_no, s->proto)<0){
1370                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
1371                 }
1372         hostent2ip_addr(address, he, 0); /*convert to ip_addr format*/
1373         if (type_flags){
1374                 *type_flags|=(address->af==AF_INET)?SOCKET_T_IPV4:SOCKET_T_IPV6;
1375         }
1376         if ((tmp=ip_addr2a(address))==0) goto error;
1377         address_str->s=pkg_malloc(strlen(tmp)+1);
1378         if (address_str->s==0){
1379                 LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
1380                 goto error;
1381         }
1382         strncpy(address_str->s, tmp, strlen(tmp)+1);
1383         /* set is_ip (1 if name is an ip address, 0 otherwise) */
1384         address_str->len=strlen(tmp);
1385         if (sr_auto_aliases && (address_str->len==name->len) &&
1386                 (strncasecmp(address_str->s, name->s, address_str->len)==0)){
1387                 *flags|=SI_IS_IP;
1388                 /* do rev. DNS on it (for aliases)*/
1389                 he=rev_resolvehost(address);
1390                 if (he==0){
1391                         LOG(L_WARN, "WARNING: fix_hostname: could not rev. resolve %s\n",
1392                                         name->s);
1393                 }else{
1394                         /* add the aliases*/
1395                         if (add_alias(he->h_name, strlen(he->h_name), s->port_no,
1396                                                         s->proto)<0){
1397                                 LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
1398                         }
1399                         for(h=he->h_aliases; h && *h; h++)
1400                                 if (add_alias(*h, strlen(*h), s->port_no, s->proto) < 0){
1401                                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
1402                                 }
1403                 }
1404         }
1405         
1406 #ifdef USE_MCAST
1407         /* Check if it is an multicast address and
1408          * set the flag if so
1409          */
1410         if (is_mcast(address)){
1411                 *flags |= SI_IS_MCAST;
1412         }
1413 #endif /* USE_MCAST */
1414         
1415         /* check if INADDR_ANY */
1416         if (ip_addr_any(address))
1417                 *flags|=SI_IS_ANY;
1418         else if (ip_addr_loopback(address)) /* check for loopback */
1419                 *flags|=SI_IS_LO;
1420         
1421         return 0;
1422 error:
1423         return -1;
1424 }
1425
1426
1427
1428 /* append new elements to a socket_info list after "list"
1429  * each element is created  from addr_info_lst + port, protocol and flags
1430  * return 0 on succes, -1 on error
1431  */
1432 static int addr_info_to_si_lst(struct addr_info* ai_lst, unsigned short port,
1433                                                                 char proto, enum si_flags flags,
1434                                                                 struct socket_info** list)
1435 {
1436         struct addr_info* ail;
1437         
1438         for (ail=ai_lst; ail; ail=ail->next){
1439                 if(new_sock2list(ail->name.s, 0, port, proto, 0, 0,
1440                                         ail->flags | flags, list)==0)
1441                         return -1;
1442         }
1443         return 0;
1444 }
1445
1446
1447
1448 /* insert new elements to a socket_info list after "el", 
1449  * each element is created from addr_info_lst + port, * protocol and flags
1450  * return 0 on succes, -1 on error
1451  */
1452 static int addr_info_to_si_lst_after(struct addr_info* ai_lst,
1453                                                                                 unsigned short port,
1454                                                                                 char proto, enum si_flags flags,
1455                                                                                 struct socket_info* el)
1456 {
1457         struct addr_info* ail;
1458         struct socket_info* new_si;
1459         
1460         for (ail=ai_lst; ail; ail=ail->next){
1461                 if((new_si=new_sock2list_after(ail->name.s, 0, port, proto,
1462                                                                 0, 0, ail->flags | flags, el))==0)
1463                         return -1;
1464                 el=new_si;
1465         }
1466         return 0;
1467 }
1468
1469
1470
1471 /* fixes a socket list => resolve addresses, 
1472  * interface names, fills missing members, remove duplicates
1473  * fills type_flags if not null with SOCKET_T_IPV4 and/or SOCKET_T_IPV6*/
1474 static int fix_socket_list(struct socket_info **list, int* type_flags)
1475 {
1476         struct socket_info* si;
1477         struct socket_info* new_si;
1478         struct socket_info* l;
1479         struct socket_info* next;
1480         struct socket_info* next_si;
1481         struct socket_info* del_si;
1482         struct socket_info* keep_si;
1483         char* tmp;
1484         int len;
1485         struct addr_info* ai_lst;
1486         struct addr_info* ail;
1487         struct addr_info* tmp_ail;
1488         struct addr_info* tmp_ail_next;
1489         struct addr_info* ail_next;
1490
1491         if (type_flags)
1492                 *type_flags=0;
1493         /* try to change all the interface names into addresses
1494          *  --ugly hack */
1495         for (si=*list;si;){
1496                 next=si->next;
1497                 ai_lst=0;
1498                 if (add_interfaces(si->name.s, AF_INET, si->port_no,
1499                                                         si->proto, &ai_lst)!=-1){
1500                         if (si->flags & SI_IS_MHOMED){
1501                                 if((new_si=new_sock2list_after(ai_lst->name.s, 0, si->port_no,
1502                                                                                         si->proto, si->useinfo.name.s,
1503                                                                                         si->useinfo.port_no,
1504                                                                                         ai_lst->flags|si->flags, si))==0)
1505                                         break;
1506                                 ail=ai_lst;
1507                                 ai_lst=ai_lst->next;
1508                                 free_addr_info(ail); /* free the first elem. */
1509                                 if (ai_lst){
1510                                         ai_lst->prev=0;
1511                                         /* find the end */
1512                                         for (ail=ai_lst; ail->next; ail=ail->next);
1513                                         /* add the mh list after the last position in ai_lst */
1514                                         addr_info_list_ins_lst(si->addr_info_lst, ail);
1515                                         new_si->addr_info_lst=ai_lst;
1516                                         si->addr_info_lst=0; /* detached and moved to new_si */
1517                                         ail=ail->next; /* ail== old si->addr_info_lst */
1518                                 }else{
1519                                         ail=si->addr_info_lst;
1520                                         new_si->addr_info_lst=ail;
1521                                         si->addr_info_lst=0; /* detached and moved to new_si */
1522                                 }
1523                                 
1524                         }else{
1525                                 /* add all addr. as separate  interfaces */
1526                                 if (addr_info_to_si_lst_after(ai_lst, si->port_no, si->proto,
1527                                                                                                 si->flags, si)!=0)
1528                                         goto error;
1529                                 /* ai_lst not needed anymore */
1530                                 free_addr_info_lst(&ai_lst);
1531                                 ail=0;
1532                                 new_si=0;
1533                         }
1534                         /* success => remove current entry (shift the entire array)*/
1535                         sock_listrm(list, si);
1536                         free_sock_info(si);
1537                 }else{
1538                         new_si=si;
1539                         ail=si->addr_info_lst;
1540                 }
1541                 
1542                 if (ail){
1543                         if (new_si && (new_si->flags & SI_IS_MHOMED)){
1544                                 ai_lst=0;
1545                                 for (; ail;){
1546                                         ail_next=ail->next;
1547                                         if (add_interfaces(ail->name.s, AF_INET, new_si->port_no,
1548                                                                                         new_si->proto, &ai_lst)!=-1){
1549                                                 /* add the resolved list after the current position */
1550                                                 addr_info_list_ins_lst(ai_lst, ail);
1551                                                 /* success, remove the current entity */
1552                                                 addr_info_listrm(&new_si->addr_info_lst, ail);
1553                                                 free_addr_info(ail);
1554                                                 ai_lst=0;
1555                                         }
1556                                         ail=ail_next;
1557                                 }
1558                         }
1559                 }
1560                 si=next;
1561         }
1562         /* get ips & fill the port numbers*/
1563 #ifdef EXTRA_DEBUG
1564         DBG("Listening on \n");
1565 #endif
1566         for (si=*list;si;si=si->next){
1567                 /* fix port number, port_no should be !=0 here */
1568                 if (si->port_no==0){
1569 #ifdef USE_TLS
1570                         si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
1571 #else
1572                         si->port_no= port_no;
1573 #endif
1574                 }
1575                 tmp=int2str(si->port_no, &len);
1576                 if (len>=MAX_PORT_LEN){
1577                         LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n", 
1578                                                 si->port_no);
1579                         goto error;
1580                 }
1581                 si->port_no_str.s=(char*)pkg_malloc(len+1);
1582                 if (si->port_no_str.s==0){
1583                         LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
1584                         goto error;
1585                 }
1586                 strncpy(si->port_no_str.s, tmp, len+1);
1587                 si->port_no_str.len=len;
1588                 
1589                 if (fix_hostname(&si->name, &si->address, &si->address_str,
1590                                                 &si->flags, type_flags, si) !=0 )
1591                         goto error;
1592                 /* fix hostnames in mh addresses */
1593                 for (ail=si->addr_info_lst; ail; ail=ail->next){
1594                         if (fix_hostname(&ail->name, &ail->address, &ail->address_str,
1595                                                 &ail->flags, type_flags, si) !=0 )
1596                                 goto error;
1597                 }
1598
1599                 if (fix_sock_str(si) < 0) goto error;
1600                 
1601 #ifdef EXTRA_DEBUG
1602                 printf("              %.*s [%s]:%s%s\n", si->name.len, 
1603                                 si->name.s, si->address_str.s, si->port_no_str.s,
1604                                 si->flags & SI_IS_MCAST ? " mcast" : "");
1605 #endif
1606         }
1607         /* removing duplicate addresses*/
1608         for (si=*list;si; ){
1609                 next_si=si->next;
1610                 for (l=si->next;l;){
1611                         next=l->next;
1612                         if ((si->port_no==l->port_no) &&
1613                                 (si->address.af==l->address.af) &&
1614                                 (memcmp(si->address.u.addr, l->address.u.addr,
1615                                                 si->address.len) == 0)
1616                                 ){
1617                                 /* remove the socket with no  extra addresses.,
1618                                  * if both of them have extra addresses, remove one of them
1619                                  * and merge the extra addresses into the other */
1620                                 if (l->addr_info_lst==0){
1621                                         del_si=l;
1622                                         keep_si=si;
1623                                 }else if (si->addr_info_lst==0){
1624                                         del_si=si;
1625                                         keep_si=l;
1626                                 }else{
1627                                         /* move l->addr_info_lst to si->addr_info_lst */
1628                                         /* find last elem */
1629                                         for (ail=si->addr_info_lst; ail->next; ail=ail->next);
1630                                         /* add the l list after the last position in si lst */
1631                                         addr_info_list_ins_lst(l->addr_info_lst, ail);
1632                                         l->addr_info_lst=0; /* detached */
1633                                         del_si=l; /* l will be removed */
1634                                         keep_si=l;
1635                                 }
1636 #ifdef EXTRA_DEBUG
1637                                 printf("removing duplicate %s [%s] ==  %s [%s]\n",
1638                                                 keep_si->name.s, keep_si->address_str.s,
1639                                                  del_si->name.s, del_si->address_str.s);
1640 #endif
1641                                 /* add the name to the alias list*/
1642                                 if ((!(del_si->flags& SI_IS_IP)) && (
1643                                                 (del_si->name.len!=keep_si->name.len)||
1644                                                 (strncmp(del_si->name.s, keep_si->name.s,
1645                                                                  del_si->name.len)!=0))
1646                                         )
1647                                         add_alias(del_si->name.s, del_si->name.len,
1648                                                                 l->port_no, l->proto);
1649                                 /* make sure next_si doesn't point to del_si */
1650                                 if (del_si==next_si)
1651                                         next_si=next_si->next;
1652                                 /* remove del_si*/
1653                                 sock_listrm(list, del_si);
1654                                 free_sock_info(del_si);
1655                         }
1656                         l=next;
1657                 }
1658                 si=next_si;
1659         }
1660         /* check for duplicates in extra_addresses */
1661         for (si=*list;si; si=si->next){
1662                 /* check  for & remove internal duplicates: */
1663                 for (ail=si->addr_info_lst; ail;){
1664                         ail_next=ail->next;
1665                         /* 1. check if the extra addresses contain a duplicate for the 
1666                          * main  one */
1667                         if ((ail->address.af==si->address.af) &&
1668                                 (memcmp(ail->address.u.addr, si->address.u.addr,
1669                                                         ail->address.len) == 0)){
1670                                 /* add the name to the alias list*/
1671                                 if ((!(ail->flags& SI_IS_IP)) && (
1672                                         (ail->name.len!=si->name.len)||
1673                                         (strncmp(ail->name.s, si->name.s, ail->name.len)!=0)))
1674                                         add_alias(ail->name.s, ail->name.len, si->port_no,
1675                                                                 si->proto);
1676                                         /* remove ail*/
1677                                 addr_info_listrm(&si->addr_info_lst, ail);
1678                                 free_addr_info(ail);
1679                                 ail=ail_next;
1680                                 continue;
1681                         }
1682                         /* 2. check if the extra addresses contain a duplicates for 
1683                          *  other addresses in the same list */
1684                         for (tmp_ail=ail->next; tmp_ail;){
1685                                 tmp_ail_next=tmp_ail->next;
1686                                 if ((ail->address.af==tmp_ail->address.af) &&
1687                                         (memcmp(ail->address.u.addr, tmp_ail->address.u.addr,
1688                                                         ail->address.len) == 0)){
1689                                         /* add the name to the alias list*/
1690                                         if ((!(tmp_ail->flags& SI_IS_IP)) && (
1691                                                 (ail->name.len!=tmp_ail->name.len)||
1692                                                 (strncmp(ail->name.s, tmp_ail->name.s,
1693                                                                                 tmp_ail->name.len)!=0))
1694                                                 )
1695                                                 add_alias(tmp_ail->name.s, tmp_ail->name.len,
1696                                                                         si->port_no, si->proto);
1697                                                 /* remove tmp_ail*/
1698                                         addr_info_listrm(&si->addr_info_lst, tmp_ail);
1699                                         free_addr_info(tmp_ail);
1700                                 }
1701                                 tmp_ail=tmp_ail_next;
1702                         }
1703                         ail=ail_next;
1704                 }
1705                 /* check for duplicates between extra addresses (e.g. sctp MH)
1706                  * and other main addresses, on conflict remove the corresponding
1707                  * extra addresses (another possible solution would be to join
1708                  * the 2 si entries into one). */
1709                 for (ail=si->addr_info_lst; ail;){
1710                         ail_next=ail->next;
1711                         for (l=*list;l; l=l->next){
1712                                 if (l==si) continue;
1713                                 if (si->port_no==l->port_no){
1714                                         if ((ail->address.af==l->address.af) &&
1715                                                 (memcmp(ail->address.u.addr, l->address.u.addr,
1716                                                                                 ail->address.len) == 0)){
1717                                                 /* add the name to the alias list*/
1718                                                 if ((!(ail->flags& SI_IS_IP)) && (
1719                                                         (ail->name.len!=l->name.len)||
1720                                                         (strncmp(ail->name.s, l->name.s, l->name.len)!=0))
1721                                                         )
1722                                                         add_alias(ail->name.s, ail->name.len,
1723                                                                                 l->port_no, l->proto);
1724                                                 /* remove ail*/
1725                                                 addr_info_listrm(&si->addr_info_lst, ail);
1726                                                 free_addr_info(ail);
1727                                                 break;
1728                                         }
1729                                         /* check for duplicates with other  extra addresses
1730                                          * lists */
1731                                         for (tmp_ail=l->addr_info_lst; tmp_ail; ){
1732                                                 tmp_ail_next=tmp_ail->next;
1733                                                 if ((ail->address.af==tmp_ail->address.af) &&
1734                                                         (memcmp(ail->address.u.addr,
1735                                                                         tmp_ail->address.u.addr,
1736                                                                         ail->address.len) == 0)){
1737                                                         /* add the name to the alias list*/
1738                                                         if ((!(tmp_ail->flags& SI_IS_IP)) && (
1739                                                                         (ail->name.len!=tmp_ail->name.len)||
1740                                                                         (strncmp(ail->name.s, tmp_ail->name.s,
1741                                                                                         tmp_ail->name.len)!=0))
1742                                                                 )
1743                                                                 add_alias(tmp_ail->name.s, tmp_ail->name.len,
1744                                                                                 l->port_no, l->proto);
1745                                                         /* remove tmp_ail*/
1746                                                         addr_info_listrm(&l->addr_info_lst, tmp_ail);
1747                                                         free_addr_info(tmp_ail);
1748                                                 }
1749                                                 tmp_ail=tmp_ail_next;
1750                                         }
1751                                 }
1752                         }
1753                         ail=ail_next;
1754                 }
1755         }
1756
1757 #ifdef USE_MCAST
1758              /* Remove invalid multicast entries */
1759         si=*list;
1760         while(si){
1761                 if ((si->proto == PROTO_TCP)
1762 #ifdef USE_TLS
1763                     || (si->proto == PROTO_TLS)
1764 #endif /* USE_TLS */
1765 #ifdef USE_SCTP
1766                         || (si->proto == PROTO_SCTP)
1767 #endif
1768                         ){
1769                         if (si->flags & SI_IS_MCAST){
1770                                 LOG(L_WARN, "WARNING: removing entry %s:%s [%s]:%s\n",
1771                                         get_valid_proto_name(si->proto), si->name.s, 
1772                                         si->address_str.s, si->port_no_str.s);
1773                                 l = si;
1774                                 si=si->next;
1775                                 sock_listrm(list, l);
1776                                 free_sock_info(l);
1777                         }else{
1778                                 ail=si->addr_info_lst;
1779                                 while(ail){
1780                                         if (ail->flags & SI_IS_MCAST){
1781                                                 LOG(L_WARN, "WARNING: removing mh entry %s:%s"
1782                                                                 " [%s]:%s\n",
1783                                                                 get_valid_proto_name(si->proto), ail->name.s, 
1784                                                                 ail->address_str.s, si->port_no_str.s);
1785                                                 tmp_ail=ail;
1786                                                 ail=ail->next;
1787                                                 addr_info_listrm(&si->addr_info_lst, tmp_ail);
1788                                                 free_addr_info(tmp_ail);
1789                                         }else{
1790                                                 ail=ail->next;
1791                                         }
1792                                 }
1793                                 si=si->next;
1794                         }
1795                 } else {
1796                         si=si->next;
1797                 }
1798         }
1799 #endif /* USE_MCAST */
1800
1801         return 0;
1802 error:
1803         return -1;
1804 }
1805
1806 int socket_types = 0;
1807
1808 /* fix all 3 socket lists, fills socket_types if non-null
1809  * return 0 on success, -1 on error */
1810 int fix_all_socket_lists()
1811 {
1812         struct utsname myname;
1813         int flags;
1814         struct addr_info* ai_lst;
1815         
1816         ai_lst=0;
1817         
1818         if ((udp_listen==0)
1819 #ifdef USE_TCP
1820                         && (tcp_listen==0)
1821 #ifdef USE_TLS
1822                         && (tls_listen==0)
1823 #endif
1824 #endif
1825 #ifdef USE_SCTP
1826                         && (sctp_listen==0)
1827 #endif
1828                 ){
1829                 /* get all listening ipv4/ipv6 interfaces */
1830                 if ( ( (add_interfaces(0, AF_INET, 0,  PROTO_UDP, &ai_lst)==0)
1831 #ifdef USE_IPV6
1832 #ifdef __OS_linux
1833                 &&  (!auto_bind_ipv6 || add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_UDP, &ai_lst) == 0)
1834 #else
1835                 && ( !auto_bind_ipv6 || add_interfaces(0, AF_INET6, 0,  PROTO_UDP, &ai_lst) !=0 ) /* add_interface does not work for IPv6 on Linux */
1836 #endif /* __OS_linux */
1837 #endif /* USE_IPV6 */
1838                          ) && (addr_info_to_si_lst(ai_lst, 0, PROTO_UDP, 0, &udp_listen)==0)){
1839                         free_addr_info_lst(&ai_lst);
1840                         ai_lst=0;
1841                         /* if ok, try to add the others too */
1842 #ifdef USE_TCP
1843                         if (!tcp_disable){
1844                                 if ( ((add_interfaces(0, AF_INET, 0,  PROTO_TCP, &ai_lst)!=0)
1845 #ifdef USE_IPV6
1846 #ifdef __OS_linux
1847                                 || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_TCP, &ai_lst) != 0)
1848 #else
1849                                 || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0,  PROTO_TCP, &ai_lst) !=0 )
1850 #endif /* __OS_linux */
1851 #endif /* USE_IPV6 */
1852                                 ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_TCP, 0,
1853                                                                                                                 &tcp_listen)!=0))
1854                                         goto error;
1855                                 free_addr_info_lst(&ai_lst);
1856                                 ai_lst=0;
1857 #ifdef USE_TLS
1858                                 if (!tls_disable){
1859                                         if (((add_interfaces(0, AF_INET, 0, PROTO_TLS,
1860                                                                                 &ai_lst)!=0)
1861 #ifdef USE_IPV6
1862 #ifdef __OS_linux
1863                                 || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_TLS, &ai_lst) != 0)
1864 #else
1865                                 || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0,  PROTO_TLS, &ai_lst)!=0)
1866 #endif /* __OS_linux */
1867 #endif /* USE_IPV6 */
1868                                         ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_TLS, 0,
1869                                                                                                                 &tls_listen)!=0))
1870                                                 goto error;
1871                                 }
1872                                 free_addr_info_lst(&ai_lst);
1873                                 ai_lst=0;
1874 #endif
1875                         }
1876 #endif
1877 #ifdef USE_SCTP
1878                         if (!sctp_disable){
1879                                 if (((add_interfaces(0, AF_INET, 0,  PROTO_SCTP, &ai_lst)!=0)
1880 #ifdef USE_IPV6
1881 #ifdef __OS_linux
1882                                 || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_SCTP, &ai_lst) != 0)
1883 #else
1884                                 || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0,  PROTO_SCTP, &ai_lst) != 0)
1885 #endif /* __OS_linux */
1886 #endif /* USE_IPV6 */
1887                                         ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_SCTP, 0,
1888                                                                                         &sctp_listen)!=0))
1889                                         goto error;
1890                                 free_addr_info_lst(&ai_lst);
1891                                 ai_lst=0;
1892                         }
1893 #endif /* USE_SCTP */
1894                 }else{
1895                         /* if error fall back to get hostname */
1896                         /* get our address, only the first one */
1897                         if (uname (&myname) <0){
1898                                 LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
1899                                                 " hostname, try -l address\n");
1900                                 goto error;
1901                         }
1902                         if (add_listen_iface(myname.nodename, 0, 0, 0, 0)!=0){
1903                                 LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
1904                                                 "failed \n");
1905                                 goto error;
1906                         }
1907                 }
1908         }
1909         flags=0;
1910         if (fix_socket_list(&udp_listen, &flags)!=0){
1911                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1912                                 " udp failed\n");
1913                 goto error;
1914         }
1915         if (flags){
1916                 socket_types|=flags|SOCKET_T_UDP;
1917         }
1918 #ifdef USE_TCP
1919         flags=0;
1920         if (!tcp_disable && (fix_socket_list(&tcp_listen, &flags)!=0)){
1921                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1922                                 " tcp failed\n");
1923                 goto error;
1924         }
1925         if (flags){
1926                 socket_types|=flags|SOCKET_T_TCP;
1927         }
1928 #ifdef USE_TLS
1929         flags=0;
1930         if (!tls_disable && (fix_socket_list(&tls_listen, &flags)!=0)){
1931                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1932                                 " tls failed\n");
1933                 goto error;
1934         }
1935         if (flags){
1936                 socket_types|=flags|SOCKET_T_TLS;
1937         }
1938 #endif
1939 #endif
1940 #ifdef USE_SCTP
1941         flags=0;
1942         if (!sctp_disable && (fix_socket_list(&sctp_listen, &flags)!=0)){
1943                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1944                                 " sctp failed\n");
1945                 goto error;
1946         }
1947         if (flags){
1948                 socket_types|=flags|SOCKET_T_SCTP;
1949         }
1950 #endif /* USE_SCTP */
1951         if ((udp_listen==0)
1952 #ifdef USE_TCP
1953                         && (tcp_listen==0)
1954 #ifdef USE_TLS
1955                         && (tls_listen==0)
1956 #endif
1957 #endif
1958 #ifdef USE_SCTP
1959                         && (sctp_listen==0)
1960 #endif
1961                 ){
1962                 LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
1963                 goto error;
1964         }
1965         return 0;
1966 error:
1967         if (ai_lst) free_addr_info_lst(&ai_lst);
1968         return -1;
1969 }
1970
1971
1972
1973 void print_all_socket_lists()
1974 {
1975         struct socket_info *si;
1976         struct socket_info** list;
1977         struct addr_info* ai;
1978         unsigned short proto;
1979         
1980         
1981         proto=PROTO_UDP;
1982         do{
1983                 list=get_sock_info_list(proto);
1984                 for(si=list?*list:0; si; si=si->next){
1985                         if (si->addr_info_lst){
1986                                 printf("             %s: (%s",
1987                                                 get_valid_proto_name(proto),
1988                                                 si->address_str.s);
1989                                 for (ai=si->addr_info_lst; ai; ai=ai->next)
1990                                         printf(", %s", ai->address_str.s);
1991                                 printf("):%s%s%s\n",
1992                                                 si->port_no_str.s, 
1993                                                 si->flags & SI_IS_MCAST ? " mcast" : "",
1994                                                 si->flags & SI_IS_MHOMED? " mhomed" : "");
1995                         }else{
1996                                 printf("             %s: %s",
1997                                                 get_valid_proto_name(proto),
1998                                                 si->name.s);
1999                                 if (!si->flags & SI_IS_IP)
2000                                         printf(" [%s]", si->address_str.s);
2001                                 printf( ":%s%s%s\n",
2002                                                 si->port_no_str.s, 
2003                                                 si->flags & SI_IS_MCAST ? " mcast" : "",
2004                                                 si->flags & SI_IS_MHOMED? " mhomed" : "");
2005                         }
2006                 }
2007         }while((proto=next_proto(proto)));
2008 }
2009
2010
2011 void print_aliases()
2012 {
2013         struct host_alias* a;
2014
2015         for(a=aliases; a; a=a->next) 
2016                 if (a->port)
2017                         printf("             %s: %.*s:%d\n", get_valid_proto_name(a->proto), 
2018                                         a->alias.len, a->alias.s, a->port);
2019                 else
2020                         printf("             %s: %.*s:*\n", get_valid_proto_name(a->proto), 
2021                                         a->alias.len, a->alias.s);
2022 }
2023
2024
2025
2026 void init_proto_order()
2027 {
2028         int r;
2029         
2030         /* fix proto list  (remove disabled protocols)*/
2031 #ifdef USE_TCP
2032         if (tcp_disable)
2033 #endif
2034                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
2035                         if (nxt_proto[r]==PROTO_TCP)
2036                                 nxt_proto[r]=nxt_proto[PROTO_TCP];
2037                 }
2038 #ifdef USE_TCP
2039 #ifdef USE_TLS
2040         if (tls_disable || tcp_disable)
2041 #endif
2042 #endif
2043                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
2044                         if (nxt_proto[r]==PROTO_TLS)
2045                                 nxt_proto[r]=nxt_proto[PROTO_TLS];
2046                 }
2047 #ifdef USE_SCTP
2048         if (sctp_disable)
2049 #endif
2050                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
2051                         if (nxt_proto[r]==PROTO_SCTP)
2052                                 nxt_proto[r]=nxt_proto[PROTO_SCTP];
2053                 }
2054 }
2055
2056