ad6fe9baa2b6e85637ba76fc3e240566f19facfd
[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                 case PROTO_WS:
480 #ifdef USE_TCP
481                         return &tcp_listen;
482 #endif
483                         break;
484                 case PROTO_TLS:
485 #ifdef USE_TLS
486                         return &tls_listen;
487 #endif
488                         break;
489                 case PROTO_SCTP:
490 #ifdef USE_SCTP
491                         return &sctp_listen;
492 #endif
493                         break;
494                 default:
495                         LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
496         }
497         return 0;
498 }
499
500
501 /* helper function for grep_sock_info
502  * params:
503  *  host - hostname to compare with
504  *  name - official name
505  *  addr_str - name's resolved ip address converted to string
506  *  ip_addr - name's ip address 
507  *  flags - set to SI_IS_IP if name contains an IP
508  *
509  * returns 0 if host matches, -1 if not */
510 inline static int si_hname_cmp(str* host, str* name, str* addr_str, 
511                                                                 struct ip_addr* ip_addr, int flags)
512 {
513 #ifdef USE_IPV6
514         struct ip_addr* ip6;
515 #endif
516         
517         if ( (host->len==name->len) && 
518                 (strncasecmp(host->s, name->s, name->len)==0) /*slower*/)
519                 /* comp. must be case insensitive, host names
520                  * can be written in mixed case, it will also match
521                  * ipv6 addresses if we are lucky*/
522                 goto found;
523         /* check if host == ip address */
524 #ifdef USE_IPV6
525         /* ipv6 case is uglier, host can be [3ffe::1] */
526         ip6=str2ip6(host);
527         if (ip6){
528                 if (ip_addr_cmp(ip6, ip_addr))
529                         goto found; /* match */
530                 else
531                         return -1; /* no match, but this is an ipv6 address
532                                                  so no point in trying ipv4 */
533         }
534 #endif
535         /* ipv4 */
536         if ( (!(flags&SI_IS_IP)) && (host->len==addr_str->len) && 
537                         (memcmp(host->s, addr_str->s, addr_str->len)==0) )
538                 goto found;
539         return -1;
540 found:
541         return 0;
542 }
543
544
545 /* checks if the proto: host:port is one of the address we listen on
546  * and returns the corresponding socket_info structure.
547  * if port==0, the  port number is ignored
548  * if proto==0 (PROTO_NONE) the protocol is ignored
549  * returns  0 if not found
550  * WARNING: uses str2ip6 so it will overwrite any previous
551  *  unsaved result of this function (static buffer)
552  */
553 struct socket_info* grep_sock_info(str* host, unsigned short port,
554                                                                                                 unsigned short proto)
555 {
556         str hname;
557         struct socket_info* si;
558         struct socket_info** list;
559         struct addr_info* ai;
560         unsigned short c_proto;
561         
562         hname=*host;
563 #ifdef USE_IPV6
564         if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
565                 /* ipv6 reference, skip [] */
566                 hname.s++;
567                 hname.len-=2;
568         }
569 #endif
570         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
571         do{
572                 /* get the proper sock_list */
573                 list=get_sock_info_list(c_proto);
574         
575                 if (list==0) /* disabled or unknown protocol */
576                         continue;
577                 for (si=*list; si; si=si->next){
578                         DBG("grep_sock_info - checking if host==us: %d==%d &&"
579                                         " [%.*s] == [%.*s]\n",
580                                                 hname.len,
581                                                 si->name.len,
582                                                 hname.len, hname.s,
583                                                 si->name.len, si->name.s
584                                 );
585                         if (port) {
586                                 DBG("grep_sock_info - checking if port %d (advertise %d)"
587                                                 " matches port %d\n",
588                                                 si->port_no, si->useinfo.port_no, port);
589                                 if (si->port_no!=port && si->useinfo.port_no!=port) {
590                                         continue;
591                                 }
592                         }
593                         if (si_hname_cmp(&hname, &si->name, &si->address_str, 
594                                                                 &si->address, si->flags)==0)
595                                 goto found;
596                         if(si->useinfo.name.s!=NULL)
597                         {
598                                 DBG("grep_sock_info - checking advertise if host==us:"
599                                                 " %d==%d && [%.*s] == [%.*s]\n",
600                                                 hname.len,
601                                                 si->useinfo.name.len,
602                                                 hname.len, hname.s,
603                                                 si->useinfo.name.len, si->useinfo.name.s
604                                 );
605                                 if (si_hname_cmp(&hname, &si->useinfo.name,
606                                                         &si->useinfo.address_str, &si->useinfo.address,
607                                                         si->flags)==0)
608                                         goto found;
609                         }
610                         /* try among the extra addresses */
611                         for (ai=si->addr_info_lst; ai; ai=ai->next)
612                                 if (si_hname_cmp(&hname, &ai->name, &ai->address_str, 
613                                                                         &ai->address, ai->flags)==0)
614                                         goto found;
615                 }
616         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
617 /* not_found: */
618         return 0;
619 found:
620         return si;
621 }
622
623 /* checks if the proto:port is one of the ports we listen on
624  * and returns the corresponding socket_info structure.
625  * if proto==0 (PROTO_NONE) the protocol is ignored
626  * returns  0 if not found
627  */
628 struct socket_info* grep_sock_info_by_port(unsigned short port, 
629                                                                                         unsigned short proto)
630 {
631         struct socket_info* si;
632         struct socket_info** list;
633         unsigned short c_proto;
634
635         if (!port) {
636                 goto not_found;
637         }
638         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
639         do{
640                 /* get the proper sock_list */
641                 list=get_sock_info_list(c_proto);
642         
643                 if (list==0) /* disabled or unknown protocol */
644                         continue;
645                 
646                 for (si=*list; si; si=si->next){
647                         DBG("grep_sock_info_by_port - checking if port %d matches"
648                                         " port %d\n", si->port_no, port);
649                         if (si->port_no==port) {
650                                 goto found;
651                         }
652                 }
653         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
654 not_found:
655         return 0;
656 found:
657         return si;
658 }
659
660
661
662 /* checks if the proto: ip:port is one of the address we listen on
663  * and returns the corresponding socket_info structure.
664  * (same as grep_socket_info, but use ip addr instead)
665  * if port==0, the  port number is ignored
666  * if proto==0 (PROTO_NONE) the protocol is ignored
667  * returns  0 if not found
668  * WARNING: uses str2ip6 so it will overwrite any previous
669  *  unsaved result of this function (static buffer)
670  */
671 struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
672                                                                                                 unsigned short proto)
673 {
674         struct socket_info* si;
675         struct socket_info** list;
676         struct addr_info* ai;
677         unsigned short c_proto;
678         
679         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
680         do{
681                 /* get the proper sock_list */
682                 list=get_sock_info_list(c_proto);
683         
684                 if (list==0) /* disabled or unknown protocol */
685                         continue;
686                 
687                 for (si=*list; si; si=si->next){
688                         if (port) {
689                                 if (si->port_no!=port) {
690                                         continue;
691                                 }
692                         }
693                         if (ip_addr_cmp(ip, &si->address)
694                                         || ip_addr_cmp(ip, &si->useinfo.address))
695                                 goto found;
696                         for (ai=si->addr_info_lst; ai; ai=ai->next)
697                                 if (ip_addr_cmp(ip, &ai->address))
698                                         goto found;
699                 }
700         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
701 /* not_found: */
702         return 0;
703 found:
704         return si;
705 }
706
707
708
709 /* append a new sock_info structure to the corresponding list
710  * return  new sock info on success, 0 on error */
711 static struct socket_info* new_sock2list(char* name, struct name_lst* addr_l,
712                                                                         unsigned short port,
713                                                                         unsigned short proto,
714                                                                         char *usename, unsigned short useport,
715                                                                         enum si_flags flags,
716                                                                         struct socket_info** list)
717 {
718         struct socket_info* si;
719         
720         si=new_sock_info(name, addr_l, port, proto, usename, useport, flags);
721         if (si==0){
722                 LOG(L_ERR, "ERROR: new_sock2list: new_sock_info failed\n");
723                 goto error;
724         }
725         if(socket_workers>0) {
726                 si->workers = socket_workers;
727                 socket_workers = 0;
728         }
729         sock_listadd(list, si);
730         return si;
731 error:
732         return 0;
733 }
734
735
736
737 /* adds a new sock_info structure immediately after "after"
738  * return  new sock info on success, 0 on error */
739 static struct socket_info* new_sock2list_after(char* name,
740                                                                         struct name_lst* addr_l,
741                                                                         unsigned short port,
742                                                                         unsigned short proto,
743                                                                         char *usename,
744                                                                         unsigned short useport,
745                                                                         enum si_flags flags,
746                                                                         struct socket_info* after)
747 {
748         struct socket_info* si;
749         
750         si=new_sock_info(name, addr_l, port, proto, usename, useport, flags);
751         if (si==0){
752                 LOG(L_ERR, "ERROR: new_sock2list_after: new_sock_info failed\n");
753                 goto error;
754         }
755         sock_listins(si, after);
756         return si;
757 error:
758         return 0;
759 }
760
761
762
763 /* adds a sock_info structure to the corresponding proto list
764  * return  0 on success, -1 on error */
765 int add_listen_advertise_iface(char* name, struct name_lst* addr_l,
766                                                 unsigned short port, unsigned short proto,
767                                                 char *usename, unsigned short useport,
768                                                 enum si_flags flags)
769 {
770         struct socket_info** list;
771         unsigned short c_proto;
772         struct name_lst* a_l;
773         unsigned short c_port;
774         
775         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
776         do{
777                 list=get_sock_info_list(c_proto);
778                 if (list==0) /* disabled or unknown protocol */
779                         continue;
780                 
781                 if (port==0){ /* use default port */
782                         c_port=
783 #ifdef USE_TLS
784                                 ((c_proto)==PROTO_TLS)?tls_port_no:
785 #endif
786                                 port_no;
787                 }
788 #ifdef USE_TLS
789                 else if ((c_proto==PROTO_TLS) && (proto==0)){
790                         /* -l  ip:port => on udp:ip:port; tcp:ip:port and tls:ip:port+1?*/
791                         c_port=port+1;
792                 }
793 #endif
794                 else{
795                         c_port=port;
796                 }
797                 if (c_proto!=PROTO_SCTP){
798                         if (new_sock2list(name, 0, c_port, c_proto, usename, useport,
799                                                                 flags & ~SI_IS_MHOMED, list)==0){
800                                 LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
801                                 goto error;
802                         }
803                         /* add the other addresses in the list as separate sockets
804                          * since only SCTP can bind to multiple addresses */
805                         for (a_l=addr_l; a_l; a_l=a_l->next){
806                                 if (new_sock2list(a_l->name, 0, c_port, 
807                                                                         c_proto, usename, useport,
808                                                                         flags & ~SI_IS_MHOMED, list)==0){
809                                         LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list"
810                                                                 " failed\n");
811                                         goto error;
812                                 }
813                         }
814                 }else{
815                         if (new_sock2list(name, addr_l, c_port, c_proto, usename, useport,
816                                                 flags, list)==0){
817                                 LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
818                                 goto error;
819                         }
820                 }
821         }while( (proto==0) && (c_proto=next_proto(c_proto)));
822         return 0;
823 error:
824         return -1;
825 }
826
827 /* adds a sock_info structure to the corresponding proto list
828  * return  0 on success, -1 on error */
829 int add_listen_iface(char* name, struct name_lst* addr_l,
830                                                 unsigned short port, unsigned short proto,
831                                                 enum si_flags flags)
832 {
833         return add_listen_advertise_iface(name, addr_l, port, proto, 0, 0, flags);
834 }
835 #ifdef __OS_linux
836
837 #include "linux/netlink.h"
838 #include "linux/rtnetlink.h"
839 #include "arpa/inet.h"
840
841
842 #define MAX_IF_LEN 64
843 struct idx
844 {
845         struct idx *    next;
846         int             family;
847         unsigned        ifa_flags;
848         char            addr[MAX_IF_LEN];
849
850 };
851
852 struct idxlist{
853         struct idx*     addresses;
854         int             index;
855         char            name[MAX_IF_LEN];
856         unsigned        flags;
857 };
858
859 #define MAX_IFACE_NO 32
860
861 static struct idxlist *ifaces = NULL;
862 static int seq = 0;
863
864 #define SADDR(s) ((struct sockaddr_in*)s)->sin_addr.s_addr
865
866 #define NLMSG_TAIL(nmsg) \
867         ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
868
869 int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
870               int alen)
871 {
872         int len = RTA_LENGTH(alen);
873         struct rtattr *rta;
874
875         if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
876                 fprintf(stderr, "addattr_l ERROR: message exceeded bound of %d\n",maxlen);
877                 return -1;
878         }
879         rta = NLMSG_TAIL(n);
880         rta->rta_type = type;
881         rta->rta_len = len;
882         memcpy(RTA_DATA(rta), data, alen);
883         n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
884         return 0;
885 }
886
887
888
889 static int nl_bound_sock(void)
890 {
891         int sock;
892         struct sockaddr_nl la;
893
894         sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
895         if(sock <= 0){
896                 LM_ERR("could not create NETLINK sock to get interface list");
897                 goto error;
898         }
899
900         /* bind NETLINK socket to pid */
901         bzero(&la, sizeof(la));
902         la.nl_family = AF_NETLINK;
903         la.nl_pad = 0;
904         la.nl_pid = getpid();
905         la.nl_groups = 0;
906         if ( bind(sock, (struct sockaddr*) &la, sizeof(la)) < 0){
907                 LM_ERR("could not bind NETLINK sock to sockaddr_nl\n");
908                 goto error;
909         }
910
911         return sock;
912 error:
913         if(sock > 0) close(sock);
914         return -1;
915 }
916
917 #define fill_nl_req(req, type, family) do {\
918         memset(&req, 0, sizeof(req));\
919         req.nlh.nlmsg_len = sizeof(req);\
920         req.nlh.nlmsg_type = type;\
921         req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST|NLM_F_DUMP;\
922         req.nlh.nlmsg_pid = getpid();\
923         req.nlh.nlmsg_seq = seq++;\
924         req.g.rtgen_family = family;\
925         } while(0);
926
927         
928 static int get_flags(int family){
929         struct {
930                 struct nlmsghdr nlh;
931                 struct rtgenmsg g;
932         } req;
933         int rtn = 0;
934         struct nlmsghdr*  nlp;
935         struct ifinfomsg *ifi;
936         char buf[8192];
937         char *p = buf;
938         int nll = 0;
939         int nl_sock = 0;
940
941         fill_nl_req(req, RTM_GETLINK, AF_INET);
942
943         if((nl_sock = nl_bound_sock()) < 0) return -1;
944
945         if(send(nl_sock, (void*)&req, sizeof(req), 0) < 0)
946         {
947                 LM_ERR("error sending NETLINK request\n");
948                 goto error;
949         }
950
951         while(1) {
952                 rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
953                 nlp = (struct nlmsghdr *) p;
954                 if(nlp->nlmsg_type == NLMSG_DONE){
955                         LM_DBG("done\n");
956                          break;
957                 }
958                 if(nlp->nlmsg_type == NLMSG_ERROR){
959                          LM_DBG("Error on message to netlink");
960                          break;
961                 }
962                 p += rtn;
963
964                 nll += rtn;
965         }
966
967         nlp = (struct nlmsghdr *) buf;
968         for(;NLMSG_OK(nlp, nll);nlp=NLMSG_NEXT(nlp, nll)){
969                 ifi = NLMSG_DATA(nlp);
970
971                 if (nlp->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
972                         goto error;
973
974                 LM_ERR("Interface with index %d has flags %d\n", ifi->ifi_index, ifi->ifi_flags);
975                 if(ifaces == NULL){
976                         LM_ERR("get_flags must not be called on empty interface list");
977                         goto error;
978                 }
979                 if(ifi->ifi_index >= MAX_IFACE_NO){
980                         LM_ERR("invalid network interface index returned %d", ifi->ifi_index);
981                         goto error;
982                 }
983                 ifaces[ifi->ifi_index].flags = ifi->ifi_flags;
984         }
985
986         if(nl_sock>0) close(nl_sock);
987         return 0;
988
989 error:
990         if(nl_sock>0) close(nl_sock);
991         return -1;
992 }
993
994 static int build_iface_list(void)
995 {
996         struct {
997                 struct nlmsghdr nlh;
998                 struct rtgenmsg g;
999         } req;
1000
1001         int seq = 0;
1002         int rtn = 0;
1003         struct nlmsghdr*  nlp;
1004         struct ifaddrmsg *ifi;
1005         int rtl;
1006         char buf[8192];
1007         char *p = buf;
1008         int nll = 0;
1009         struct rtattr * rtap;
1010         int index, i;
1011         struct idx* entry;
1012         struct idx* tmp;
1013         int nl_sock = 0;
1014         int families[] = {AF_INET, AF_INET6};
1015         char name[MAX_IF_LEN];
1016         int is_link_local = 0;
1017
1018         if(ifaces == NULL){
1019                 if((ifaces = (struct idxlist*)pkg_malloc(MAX_IFACE_NO*sizeof(struct idxlist))) == NULL){
1020                         LM_ERR("No more pkg memory\n");
1021                         return -1;
1022                 }
1023                 memset(ifaces, 0, sizeof(struct idxlist)*MAX_IFACE_NO);
1024         }
1025
1026         /* bind netlink socket */
1027         if((nl_sock = nl_bound_sock()) < 0) return -1;
1028
1029         for (i = 0 ; i < sizeof(families)/sizeof(int); i++) {
1030                 fill_nl_req(req, RTM_GETADDR, families[i]);
1031
1032                 if(send(nl_sock, (void*)&req, sizeof(req), 0) < 0){
1033                         LM_ERR("error sending NETLINK request\n");
1034                         goto error;
1035                 };
1036
1037                 memset(buf, 0, sizeof(buf));
1038                 nll = 0;
1039                 p = buf;
1040                 while(1) {
1041                         rtn = recv(nl_sock, p, sizeof(buf) - nll, 0);
1042                         LM_DBG("received %d byles \n", rtn);
1043                         nlp = (struct nlmsghdr *) p;
1044                         if(nlp->nlmsg_type == NLMSG_DONE){
1045                                 LM_DBG("done receiving netlink info \n");
1046                                  break;
1047                         }
1048                         if(nlp->nlmsg_type == NLMSG_ERROR){
1049                                  LM_ERR("Error on message to netlink");
1050                                  break;
1051                         }
1052                         p += rtn;
1053
1054                         nll += rtn;
1055                 }
1056
1057                 nlp = (struct nlmsghdr *) buf;
1058                 for(;NLMSG_OK(nlp, nll);nlp=NLMSG_NEXT(nlp, nll)){
1059                         ifi = NLMSG_DATA(nlp);
1060
1061                         if (nlp->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
1062                                 continue;
1063                         // init all the strings
1064                         // inner loop: loop thru all the attributes of
1065                         // one route entry
1066                         rtap = (struct rtattr *) IFA_RTA(ifi);
1067
1068                         rtl = IFA_PAYLOAD(nlp);
1069
1070                         index = ifi->ifa_index;
1071                         if(index >= MAX_IFACE_NO){
1072                                 LM_ERR("Invalid interface index returned: %d\n", index);
1073                                 goto error;
1074                         }
1075
1076                         entry = (struct idx*)pkg_malloc(sizeof(struct idx));
1077                         if(entry == 0)
1078                         {
1079                                 LM_ERR("could not allocate memory\n");
1080                                 goto error;
1081                         }
1082
1083                         entry->next = 0;
1084                         entry->family = families[i];
1085                         entry->ifa_flags = ifi->ifa_flags;
1086                         is_link_local = 0;
1087
1088                         for(;RTA_OK(rtap, rtl);rtap=RTA_NEXT(rtap,rtl)){
1089                                 switch(rtap->rta_type){
1090                                         case IFA_ADDRESS:
1091                                                 if((*(int*)RTA_DATA(rtap))== htons(0xfe80)){
1092                                                         LM_DBG("Link Local Address, ignoring ...\n");
1093                                                         is_link_local = 1;
1094                                                         break;
1095                                                 }
1096                                                 inet_ntop(families[i], RTA_DATA(rtap), entry->addr, MAX_IF_LEN);
1097                                                 LM_DBG("iface <IFA_ADDRESS> addr is  %s\n", entry->addr);
1098                                                 break;
1099                                         case IFA_LOCAL:
1100                                                 if((*(int*)RTA_DATA(rtap))== htons(0xfe80)){
1101                                                         LM_DBG("Link Local Address, ignoring ...\n");
1102                                                         is_link_local = 1;
1103                                                 }
1104                                                 inet_ntop(families[i], RTA_DATA(rtap), entry->addr, MAX_IF_LEN);
1105                                                 LM_DBG("iface <IFA_LOCAL> addr is %s\n", entry->addr);
1106                                                 break;
1107                                         case IFA_LABEL:
1108                                                 LM_DBG("iface name is %s\n", (char*)RTA_DATA(rtap));
1109                                                 strncpy(name, (char*)RTA_DATA(rtap), MAX_IF_LEN);
1110                                                 break;
1111                                         case IFA_BROADCAST:
1112                                         case IFA_ANYCAST:
1113                                         case IFA_UNSPEC:
1114                                         case IFA_CACHEINFO:
1115                                         default:
1116                                                 break;
1117                                 }
1118                         }
1119                         if(is_link_local) continue;    /* link local addresses are not bindable */
1120
1121                         if(strlen(ifaces[index].name)==0)
1122                                 strncpy(ifaces[index].name, name, MAX_IF_LEN);
1123
1124                         ifaces[index].index = index;
1125
1126                         if(ifaces[index].addresses == 0 )
1127                                 ifaces[index].addresses = entry;
1128                         else {
1129                                 for(tmp = ifaces[index].addresses; tmp->next ; tmp = tmp->next)/*empty*/;
1130                                 tmp->next = entry;
1131                         }
1132                 }
1133         }
1134         if(nl_sock>0) close(nl_sock);
1135         /* the socket should be closed so we can bind again */
1136         for(i = 0; i < sizeof(families)/sizeof(int); i++){
1137                 /* get device flags */
1138                 get_flags(families[i]); /* AF_INET or AF_INET6 */
1139         }
1140
1141         return 0;
1142 error:
1143         if(nl_sock>0) close(nl_sock);
1144         return -1;
1145
1146 }
1147 /* add all family type addresses of interface if_to the socket_info array
1148  * if if_name==0, adds all addresses on all interfaces
1149  * uses RTNETLINK sockets to get addresses on the present interface on LINUX
1150  * return: -1 on error, 0 on success
1151  */
1152 int add_interfaces_via_netlink(char* if_name, int family, unsigned short port,
1153                                         unsigned short proto,
1154                                         struct addr_info** ai_l)
1155 {
1156         int i;
1157         struct idx* tmp;
1158         enum si_flags flags;
1159
1160         if(ifaces == NULL && (build_iface_list()!=0)){
1161                 LM_ERR("Could not get network interface list\n");
1162                 return -1;
1163         }
1164
1165         flags=SI_NONE;
1166         for(i=0; i< MAX_IFACE_NO; ++i){
1167                 if(ifaces[i].addresses == NULL) continue; /* not present/configured */
1168                 if ((if_name==0)||
1169                         (strncmp(if_name, ifaces[i].name, strlen(ifaces[i].name))==0)){
1170
1171                         /* check if iface is up */
1172                         //if(! (ifaces[i].flags & IFF_UP) ) continue;
1173
1174                         for(tmp = ifaces[i].addresses; tmp; tmp = tmp->next){
1175                                 LM_DBG("\t in add_iface_via_netlink Name %s Address %s\n", ifaces[i].name, tmp->addr);
1176                                 /* match family */
1177                                 if (family == tmp->family){
1178                                         /* check if loopback */
1179                                         if (ifaces[i].flags & IFF_LOOPBACK){
1180                                                 LM_DBG("INTERFACE %s is loopback", ifaces[i].name);
1181                                                 flags|=SI_IS_LO;
1182                                         }
1183                                         /* save the info */
1184                                         if (new_addr_info2list(tmp->addr, flags, ai_l)!=0){
1185                                                 LOG(L_ERR, "ERROR: add_interfaces: "
1186                                                         "new_addr_info2list failed\n");
1187                                                 goto error;
1188                                         }
1189                                 }
1190                         }
1191                 }
1192         }
1193         return 0;
1194 error:
1195         return -1;
1196 }
1197 #endif /* __OS_linux */
1198
1199 /* add all family type addresses of interface if_name to the socket_info array
1200  * if if_name==0, adds all addresses on all interfaces
1201  * WARNING: it only works with ipv6 addresses on FreeBSD
1202  * return: -1 on error, 0 on success
1203  */
1204 int add_interfaces(char* if_name, int family, unsigned short port,
1205                                         unsigned short proto,
1206                                         struct addr_info** ai_l)
1207 {
1208         struct ifconf ifc;
1209         struct ifreq ifr;
1210         struct ifreq ifrcopy;
1211         char*  last;
1212         char* p;
1213         int size;
1214         int lastlen;
1215         int s;
1216         char* tmp;
1217         struct ip_addr addr;
1218         int ret;
1219         enum si_flags flags;
1220
1221 #ifdef HAVE_SOCKADDR_SA_LEN
1222         #ifndef MAX
1223                 #define MAX(a,b) ( ((a)>(b))?(a):(b))
1224         #endif
1225 #endif
1226         /* ipv4 or ipv6 only*/
1227         flags=SI_NONE;
1228         s=socket(family, SOCK_DGRAM, 0);
1229         ret=-1;
1230         lastlen=0;
1231         ifc.ifc_req=0;
1232         for (size=100; ; size*=2){
1233                 ifc.ifc_len=size*sizeof(struct ifreq);
1234                 ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
1235                 if (ifc.ifc_req==0){
1236                         LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
1237                         goto error;
1238                 }
1239                 if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
1240                         if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
1241                         LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
1242                                         strerror(errno));
1243                         goto error;
1244                 }
1245                 if  ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
1246                                                                                                                    len not changed*/
1247                 lastlen=ifc.ifc_len;
1248                 /* try a bigger array*/
1249                 pkg_free(ifc.ifc_req);
1250         }
1251         
1252         last=(char*)ifc.ifc_req+ifc.ifc_len;
1253         for(p=(char*)ifc.ifc_req; p<last;
1254                         p+=
1255                         #ifdef __OS_linux
1256                                 sizeof(ifr) /* works on x86_64 too */
1257                         #else
1258                                 (sizeof(ifr.ifr_name)+
1259                                 #ifdef  HAVE_SOCKADDR_SA_LEN
1260                                         MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
1261                                 #else
1262                                         ( (ifr.ifr_addr.sa_family==AF_INET)?
1263                                                 sizeof(struct sockaddr_in):
1264                                         #ifdef USE_IPV6
1265                                                 ((ifr.ifr_addr.sa_family==AF_INET6)?
1266                                                 sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
1267                                         #else /* USE_IPV6 */
1268                                                 sizeof(struct sockaddr) )
1269                                         #endif /* USE_IPV6 */
1270                                 #endif
1271                                 )
1272                         #endif
1273                 )
1274         {
1275                 /* copy contents into ifr structure
1276                  * warning: it might be longer (e.g. ipv6 address) */
1277                 memcpy(&ifr, p, sizeof(ifr));
1278                 if (ifr.ifr_addr.sa_family!=family){
1279                         /*printf("strange family %d skipping...\n",
1280                                         ifr->ifr_addr.sa_family);*/
1281                         continue;
1282                 }
1283                 
1284                 /*get flags*/
1285                 ifrcopy=ifr;
1286                 if (ioctl(s, SIOCGIFFLAGS,  &ifrcopy)!=-1){ /* ignore errors */
1287                         /* ignore down ifs only if listening on all of them*/
1288                         if (if_name==0){ 
1289                                 /* if if not up, skip it*/
1290                                 if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
1291                         }
1292                 }
1293                 
1294                 
1295                 
1296                 if ((if_name==0)||
1297                         (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
1298                         
1299                         /*add address*/
1300                         sockaddr2ip_addr(&addr, 
1301                                         (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
1302                         if ((tmp=ip_addr2a(&addr))==0) goto error;
1303                         /* check if loopback */
1304                         if (ifrcopy.ifr_flags & IFF_LOOPBACK) 
1305                                 flags|=SI_IS_LO;
1306                         /* save the info */
1307                         if (new_addr_info2list(tmp, flags, ai_l)!=0){
1308                                 LOG(L_ERR, "ERROR: add_interfaces: "
1309                                                 "new_addr_info2list failed\n");
1310                                 goto error;
1311                         }
1312                         ret=0;
1313                 }
1314                         /*
1315                         printf("%s:\n", ifr->ifr_name);
1316                         printf("        ");
1317                         print_sockaddr(&(ifr->ifr_addr));
1318                         printf("        ");
1319                         ls_ifflags(ifr->ifr_name, family, options);
1320                         printf("\n");*/
1321         }
1322         pkg_free(ifc.ifc_req); /*clean up*/
1323         close(s);
1324         return  ret;
1325 error:
1326         if (ifc.ifc_req) pkg_free(ifc.ifc_req);
1327         close(s);
1328         return -1;
1329 }
1330
1331
1332
1333 /* internal helper function: resolve host names and add aliases
1334  * name is a value result parameter: it should contain the hostname that
1335  * will be used to fill all the other members, including name itself
1336  * in some situation (name->s should be a 0 terminated pkg_malloc'ed string)
1337  * return 0 on success and -1 on error */
1338 static int fix_hostname(str* name, struct ip_addr* address, str* address_str,
1339                                                 enum si_flags* flags, int* type_flags,
1340                                                 struct socket_info* s)
1341 {
1342         struct hostent* he;
1343         char* tmp;
1344         char** h;
1345         
1346         /* get "official hostnames", all the aliases etc. */
1347         he=resolvehost(name->s);
1348         if (he==0){
1349                 LOG(L_ERR, "ERROR: fix_hostname: could not resolve %s\n", name->s);
1350                 goto error;
1351         }
1352         /* check if we got the official name */
1353         if (strcasecmp(he->h_name, name->s)!=0){
1354                 if (sr_auto_aliases && 
1355                                 add_alias(name->s, name->len, s->port_no, s->proto)<0){
1356                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
1357                 }
1358                 /* change the official name */
1359                 pkg_free(name->s);
1360                 name->s=(char*)pkg_malloc(strlen(he->h_name)+1);
1361                 if (name->s==0){
1362                         LOG(L_ERR,  "ERROR: fix_hostname: out of memory.\n");
1363                         goto error;
1364                 }
1365                 name->len=strlen(he->h_name);
1366                 strncpy(name->s, he->h_name, name->len+1);
1367         }
1368         /* add the aliases*/
1369         for(h=he->h_aliases; sr_auto_aliases && h && *h; h++)
1370                 if (add_alias(*h, strlen(*h), s->port_no, s->proto)<0){
1371                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
1372                 }
1373         hostent2ip_addr(address, he, 0); /*convert to ip_addr format*/
1374         if (type_flags){
1375                 *type_flags|=(address->af==AF_INET)?SOCKET_T_IPV4:SOCKET_T_IPV6;
1376         }
1377         if ((tmp=ip_addr2a(address))==0) goto error;
1378         address_str->s=pkg_malloc(strlen(tmp)+1);
1379         if (address_str->s==0){
1380                 LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
1381                 goto error;
1382         }
1383         strncpy(address_str->s, tmp, strlen(tmp)+1);
1384         /* set is_ip (1 if name is an ip address, 0 otherwise) */
1385         address_str->len=strlen(tmp);
1386         if (sr_auto_aliases && (address_str->len==name->len) &&
1387                 (strncasecmp(address_str->s, name->s, address_str->len)==0)){
1388                 *flags|=SI_IS_IP;
1389                 /* do rev. DNS on it (for aliases)*/
1390                 he=rev_resolvehost(address);
1391                 if (he==0){
1392                         LOG(L_WARN, "WARNING: fix_hostname: could not rev. resolve %s\n",
1393                                         name->s);
1394                 }else{
1395                         /* add the aliases*/
1396                         if (add_alias(he->h_name, strlen(he->h_name), s->port_no,
1397                                                         s->proto)<0){
1398                                 LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
1399                         }
1400                         for(h=he->h_aliases; h && *h; h++)
1401                                 if (add_alias(*h, strlen(*h), s->port_no, s->proto) < 0){
1402                                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
1403                                 }
1404                 }
1405         }
1406         
1407 #ifdef USE_MCAST
1408         /* Check if it is an multicast address and
1409          * set the flag if so
1410          */
1411         if (is_mcast(address)){
1412                 *flags |= SI_IS_MCAST;
1413         }
1414 #endif /* USE_MCAST */
1415         
1416         /* check if INADDR_ANY */
1417         if (ip_addr_any(address))
1418                 *flags|=SI_IS_ANY;
1419         else if (ip_addr_loopback(address)) /* check for loopback */
1420                 *flags|=SI_IS_LO;
1421         
1422         return 0;
1423 error:
1424         return -1;
1425 }
1426
1427
1428
1429 /* append new elements to a socket_info list after "list"
1430  * each element is created  from addr_info_lst + port, protocol and flags
1431  * return 0 on succes, -1 on error
1432  */
1433 static int addr_info_to_si_lst(struct addr_info* ai_lst, unsigned short port,
1434                                                                 char proto, enum si_flags flags,
1435                                                                 struct socket_info** list)
1436 {
1437         struct addr_info* ail;
1438         
1439         for (ail=ai_lst; ail; ail=ail->next){
1440                 if(new_sock2list(ail->name.s, 0, port, proto, 0, 0,
1441                                         ail->flags | flags, list)==0)
1442                         return -1;
1443         }
1444         return 0;
1445 }
1446
1447
1448
1449 /* insert new elements to a socket_info list after "el", 
1450  * each element is created from addr_info_lst + port, * protocol and flags
1451  * return 0 on succes, -1 on error
1452  */
1453 static int addr_info_to_si_lst_after(struct addr_info* ai_lst,
1454                                                                                 unsigned short port,
1455                                                                                 char proto, enum si_flags flags,
1456                                                                                 struct socket_info* el)
1457 {
1458         struct addr_info* ail;
1459         struct socket_info* new_si;
1460         
1461         for (ail=ai_lst; ail; ail=ail->next){
1462                 if((new_si=new_sock2list_after(ail->name.s, 0, port, proto,
1463                                                                 0, 0, ail->flags | flags, el))==0)
1464                         return -1;
1465                 el=new_si;
1466         }
1467         return 0;
1468 }
1469
1470
1471
1472 /* fixes a socket list => resolve addresses, 
1473  * interface names, fills missing members, remove duplicates
1474  * fills type_flags if not null with SOCKET_T_IPV4 and/or SOCKET_T_IPV6*/
1475 static int fix_socket_list(struct socket_info **list, int* type_flags)
1476 {
1477         struct socket_info* si;
1478         struct socket_info* new_si;
1479         struct socket_info* l;
1480         struct socket_info* next;
1481         struct socket_info* next_si;
1482         struct socket_info* del_si;
1483         struct socket_info* keep_si;
1484         char* tmp;
1485         int len;
1486         struct addr_info* ai_lst;
1487         struct addr_info* ail;
1488         struct addr_info* tmp_ail;
1489         struct addr_info* tmp_ail_next;
1490         struct addr_info* ail_next;
1491
1492         if (type_flags)
1493                 *type_flags=0;
1494         /* try to change all the interface names into addresses
1495          *  --ugly hack */
1496         for (si=*list;si;){
1497                 next=si->next;
1498                 ai_lst=0;
1499                 if (add_interfaces(si->name.s, AF_INET, si->port_no,
1500                                                         si->proto, &ai_lst)!=-1){
1501                         if (si->flags & SI_IS_MHOMED){
1502                                 if((new_si=new_sock2list_after(ai_lst->name.s, 0, si->port_no,
1503                                                                                         si->proto, si->useinfo.name.s,
1504                                                                                         si->useinfo.port_no,
1505                                                                                         ai_lst->flags|si->flags, si))==0)
1506                                         break;
1507                                 ail=ai_lst;
1508                                 ai_lst=ai_lst->next;
1509                                 free_addr_info(ail); /* free the first elem. */
1510                                 if (ai_lst){
1511                                         ai_lst->prev=0;
1512                                         /* find the end */
1513                                         for (ail=ai_lst; ail->next; ail=ail->next);
1514                                         /* add the mh list after the last position in ai_lst */
1515                                         addr_info_list_ins_lst(si->addr_info_lst, ail);
1516                                         new_si->addr_info_lst=ai_lst;
1517                                         si->addr_info_lst=0; /* detached and moved to new_si */
1518                                         ail=ail->next; /* ail== old si->addr_info_lst */
1519                                 }else{
1520                                         ail=si->addr_info_lst;
1521                                         new_si->addr_info_lst=ail;
1522                                         si->addr_info_lst=0; /* detached and moved to new_si */
1523                                 }
1524                                 
1525                         }else{
1526                                 /* add all addr. as separate  interfaces */
1527                                 if (addr_info_to_si_lst_after(ai_lst, si->port_no, si->proto,
1528                                                                                                 si->flags, si)!=0)
1529                                         goto error;
1530                                 /* ai_lst not needed anymore */
1531                                 free_addr_info_lst(&ai_lst);
1532                                 ail=0;
1533                                 new_si=0;
1534                         }
1535                         /* success => remove current entry (shift the entire array)*/
1536                         sock_listrm(list, si);
1537                         free_sock_info(si);
1538                 }else{
1539                         new_si=si;
1540                         ail=si->addr_info_lst;
1541                 }
1542                 
1543                 if (ail){
1544                         if (new_si && (new_si->flags & SI_IS_MHOMED)){
1545                                 ai_lst=0;
1546                                 for (; ail;){
1547                                         ail_next=ail->next;
1548                                         if (add_interfaces(ail->name.s, AF_INET, new_si->port_no,
1549                                                                                         new_si->proto, &ai_lst)!=-1){
1550                                                 /* add the resolved list after the current position */
1551                                                 addr_info_list_ins_lst(ai_lst, ail);
1552                                                 /* success, remove the current entity */
1553                                                 addr_info_listrm(&new_si->addr_info_lst, ail);
1554                                                 free_addr_info(ail);
1555                                                 ai_lst=0;
1556                                         }
1557                                         ail=ail_next;
1558                                 }
1559                         }
1560                 }
1561                 si=next;
1562         }
1563         /* get ips & fill the port numbers*/
1564 #ifdef EXTRA_DEBUG
1565         DBG("Listening on \n");
1566 #endif
1567         for (si=*list;si;si=si->next){
1568                 /* fix port number, port_no should be !=0 here */
1569                 if (si->port_no==0){
1570 #ifdef USE_TLS
1571                         si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
1572 #else
1573                         si->port_no= port_no;
1574 #endif
1575                 }
1576                 tmp=int2str(si->port_no, &len);
1577                 if (len>=MAX_PORT_LEN){
1578                         LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n", 
1579                                                 si->port_no);
1580                         goto error;
1581                 }
1582                 si->port_no_str.s=(char*)pkg_malloc(len+1);
1583                 if (si->port_no_str.s==0){
1584                         LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
1585                         goto error;
1586                 }
1587                 strncpy(si->port_no_str.s, tmp, len+1);
1588                 si->port_no_str.len=len;
1589                 
1590                 if (fix_hostname(&si->name, &si->address, &si->address_str,
1591                                                 &si->flags, type_flags, si) !=0 )
1592                         goto error;
1593                 /* fix hostnames in mh addresses */
1594                 for (ail=si->addr_info_lst; ail; ail=ail->next){
1595                         if (fix_hostname(&ail->name, &ail->address, &ail->address_str,
1596                                                 &ail->flags, type_flags, si) !=0 )
1597                                 goto error;
1598                 }
1599
1600                 if (fix_sock_str(si) < 0) goto error;
1601                 
1602 #ifdef EXTRA_DEBUG
1603                 printf("              %.*s [%s]:%s%s\n", si->name.len, 
1604                                 si->name.s, si->address_str.s, si->port_no_str.s,
1605                                 si->flags & SI_IS_MCAST ? " mcast" : "");
1606 #endif
1607         }
1608         /* removing duplicate addresses*/
1609         for (si=*list;si; ){
1610                 next_si=si->next;
1611                 for (l=si->next;l;){
1612                         next=l->next;
1613                         if ((si->port_no==l->port_no) &&
1614                                 (si->address.af==l->address.af) &&
1615                                 (memcmp(si->address.u.addr, l->address.u.addr,
1616                                                 si->address.len) == 0)
1617                                 ){
1618                                 /* remove the socket with no  extra addresses.,
1619                                  * if both of them have extra addresses, remove one of them
1620                                  * and merge the extra addresses into the other */
1621                                 if (l->addr_info_lst==0){
1622                                         del_si=l;
1623                                         keep_si=si;
1624                                 }else if (si->addr_info_lst==0){
1625                                         del_si=si;
1626                                         keep_si=l;
1627                                 }else{
1628                                         /* move l->addr_info_lst to si->addr_info_lst */
1629                                         /* find last elem */
1630                                         for (ail=si->addr_info_lst; ail->next; ail=ail->next);
1631                                         /* add the l list after the last position in si lst */
1632                                         addr_info_list_ins_lst(l->addr_info_lst, ail);
1633                                         l->addr_info_lst=0; /* detached */
1634                                         del_si=l; /* l will be removed */
1635                                         keep_si=l;
1636                                 }
1637 #ifdef EXTRA_DEBUG
1638                                 printf("removing duplicate %s [%s] ==  %s [%s]\n",
1639                                                 keep_si->name.s, keep_si->address_str.s,
1640                                                  del_si->name.s, del_si->address_str.s);
1641 #endif
1642                                 /* add the name to the alias list*/
1643                                 if ((!(del_si->flags& SI_IS_IP)) && (
1644                                                 (del_si->name.len!=keep_si->name.len)||
1645                                                 (strncmp(del_si->name.s, keep_si->name.s,
1646                                                                  del_si->name.len)!=0))
1647                                         )
1648                                         add_alias(del_si->name.s, del_si->name.len,
1649                                                                 l->port_no, l->proto);
1650                                 /* make sure next_si doesn't point to del_si */
1651                                 if (del_si==next_si)
1652                                         next_si=next_si->next;
1653                                 /* remove del_si*/
1654                                 sock_listrm(list, del_si);
1655                                 free_sock_info(del_si);
1656                         }
1657                         l=next;
1658                 }
1659                 si=next_si;
1660         }
1661         /* check for duplicates in extra_addresses */
1662         for (si=*list;si; si=si->next){
1663                 /* check  for & remove internal duplicates: */
1664                 for (ail=si->addr_info_lst; ail;){
1665                         ail_next=ail->next;
1666                         /* 1. check if the extra addresses contain a duplicate for the 
1667                          * main  one */
1668                         if ((ail->address.af==si->address.af) &&
1669                                 (memcmp(ail->address.u.addr, si->address.u.addr,
1670                                                         ail->address.len) == 0)){
1671                                 /* add the name to the alias list*/
1672                                 if ((!(ail->flags& SI_IS_IP)) && (
1673                                         (ail->name.len!=si->name.len)||
1674                                         (strncmp(ail->name.s, si->name.s, ail->name.len)!=0)))
1675                                         add_alias(ail->name.s, ail->name.len, si->port_no,
1676                                                                 si->proto);
1677                                         /* remove ail*/
1678                                 addr_info_listrm(&si->addr_info_lst, ail);
1679                                 free_addr_info(ail);
1680                                 ail=ail_next;
1681                                 continue;
1682                         }
1683                         /* 2. check if the extra addresses contain a duplicates for 
1684                          *  other addresses in the same list */
1685                         for (tmp_ail=ail->next; tmp_ail;){
1686                                 tmp_ail_next=tmp_ail->next;
1687                                 if ((ail->address.af==tmp_ail->address.af) &&
1688                                         (memcmp(ail->address.u.addr, tmp_ail->address.u.addr,
1689                                                         ail->address.len) == 0)){
1690                                         /* add the name to the alias list*/
1691                                         if ((!(tmp_ail->flags& SI_IS_IP)) && (
1692                                                 (ail->name.len!=tmp_ail->name.len)||
1693                                                 (strncmp(ail->name.s, tmp_ail->name.s,
1694                                                                                 tmp_ail->name.len)!=0))
1695                                                 )
1696                                                 add_alias(tmp_ail->name.s, tmp_ail->name.len,
1697                                                                         si->port_no, si->proto);
1698                                                 /* remove tmp_ail*/
1699                                         addr_info_listrm(&si->addr_info_lst, tmp_ail);
1700                                         free_addr_info(tmp_ail);
1701                                 }
1702                                 tmp_ail=tmp_ail_next;
1703                         }
1704                         ail=ail_next;
1705                 }
1706                 /* check for duplicates between extra addresses (e.g. sctp MH)
1707                  * and other main addresses, on conflict remove the corresponding
1708                  * extra addresses (another possible solution would be to join
1709                  * the 2 si entries into one). */
1710                 for (ail=si->addr_info_lst; ail;){
1711                         ail_next=ail->next;
1712                         for (l=*list;l; l=l->next){
1713                                 if (l==si) continue;
1714                                 if (si->port_no==l->port_no){
1715                                         if ((ail->address.af==l->address.af) &&
1716                                                 (memcmp(ail->address.u.addr, l->address.u.addr,
1717                                                                                 ail->address.len) == 0)){
1718                                                 /* add the name to the alias list*/
1719                                                 if ((!(ail->flags& SI_IS_IP)) && (
1720                                                         (ail->name.len!=l->name.len)||
1721                                                         (strncmp(ail->name.s, l->name.s, l->name.len)!=0))
1722                                                         )
1723                                                         add_alias(ail->name.s, ail->name.len,
1724                                                                                 l->port_no, l->proto);
1725                                                 /* remove ail*/
1726                                                 addr_info_listrm(&si->addr_info_lst, ail);
1727                                                 free_addr_info(ail);
1728                                                 break;
1729                                         }
1730                                         /* check for duplicates with other  extra addresses
1731                                          * lists */
1732                                         for (tmp_ail=l->addr_info_lst; tmp_ail; ){
1733                                                 tmp_ail_next=tmp_ail->next;
1734                                                 if ((ail->address.af==tmp_ail->address.af) &&
1735                                                         (memcmp(ail->address.u.addr,
1736                                                                         tmp_ail->address.u.addr,
1737                                                                         ail->address.len) == 0)){
1738                                                         /* add the name to the alias list*/
1739                                                         if ((!(tmp_ail->flags& SI_IS_IP)) && (
1740                                                                         (ail->name.len!=tmp_ail->name.len)||
1741                                                                         (strncmp(ail->name.s, tmp_ail->name.s,
1742                                                                                         tmp_ail->name.len)!=0))
1743                                                                 )
1744                                                                 add_alias(tmp_ail->name.s, tmp_ail->name.len,
1745                                                                                 l->port_no, l->proto);
1746                                                         /* remove tmp_ail*/
1747                                                         addr_info_listrm(&l->addr_info_lst, tmp_ail);
1748                                                         free_addr_info(tmp_ail);
1749                                                 }
1750                                                 tmp_ail=tmp_ail_next;
1751                                         }
1752                                 }
1753                         }
1754                         ail=ail_next;
1755                 }
1756         }
1757
1758 #ifdef USE_MCAST
1759              /* Remove invalid multicast entries */
1760         si=*list;
1761         while(si){
1762                 if ((si->proto == PROTO_TCP)
1763 #ifdef USE_TLS
1764                     || (si->proto == PROTO_TLS)
1765 #endif /* USE_TLS */
1766 #ifdef USE_SCTP
1767                         || (si->proto == PROTO_SCTP)
1768 #endif
1769                         ){
1770                         if (si->flags & SI_IS_MCAST){
1771                                 LOG(L_WARN, "WARNING: removing entry %s:%s [%s]:%s\n",
1772                                         get_valid_proto_name(si->proto), si->name.s, 
1773                                         si->address_str.s, si->port_no_str.s);
1774                                 l = si;
1775                                 si=si->next;
1776                                 sock_listrm(list, l);
1777                                 free_sock_info(l);
1778                         }else{
1779                                 ail=si->addr_info_lst;
1780                                 while(ail){
1781                                         if (ail->flags & SI_IS_MCAST){
1782                                                 LOG(L_WARN, "WARNING: removing mh entry %s:%s"
1783                                                                 " [%s]:%s\n",
1784                                                                 get_valid_proto_name(si->proto), ail->name.s, 
1785                                                                 ail->address_str.s, si->port_no_str.s);
1786                                                 tmp_ail=ail;
1787                                                 ail=ail->next;
1788                                                 addr_info_listrm(&si->addr_info_lst, tmp_ail);
1789                                                 free_addr_info(tmp_ail);
1790                                         }else{
1791                                                 ail=ail->next;
1792                                         }
1793                                 }
1794                                 si=si->next;
1795                         }
1796                 } else {
1797                         si=si->next;
1798                 }
1799         }
1800 #endif /* USE_MCAST */
1801
1802         return 0;
1803 error:
1804         return -1;
1805 }
1806
1807 int socket_types = 0;
1808
1809 /* fix all 3 socket lists, fills socket_types if non-null
1810  * return 0 on success, -1 on error */
1811 int fix_all_socket_lists()
1812 {
1813         struct utsname myname;
1814         int flags;
1815         struct addr_info* ai_lst;
1816         
1817         ai_lst=0;
1818         
1819         if ((udp_listen==0)
1820 #ifdef USE_TCP
1821                         && (tcp_listen==0)
1822 #ifdef USE_TLS
1823                         && (tls_listen==0)
1824 #endif
1825 #endif
1826 #ifdef USE_SCTP
1827                         && (sctp_listen==0)
1828 #endif
1829                 ){
1830                 /* get all listening ipv4/ipv6 interfaces */
1831                 if ( ( (add_interfaces(0, AF_INET, 0,  PROTO_UDP, &ai_lst)==0)
1832 #ifdef USE_IPV6
1833 #ifdef __OS_linux
1834                 &&  (!auto_bind_ipv6 || add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_UDP, &ai_lst) == 0)
1835 #else
1836                 && ( !auto_bind_ipv6 || add_interfaces(0, AF_INET6, 0,  PROTO_UDP, &ai_lst) !=0 ) /* add_interface does not work for IPv6 on Linux */
1837 #endif /* __OS_linux */
1838 #endif /* USE_IPV6 */
1839                          ) && (addr_info_to_si_lst(ai_lst, 0, PROTO_UDP, 0, &udp_listen)==0)){
1840                         free_addr_info_lst(&ai_lst);
1841                         ai_lst=0;
1842                         /* if ok, try to add the others too */
1843 #ifdef USE_TCP
1844                         if (!tcp_disable){
1845                                 if ( ((add_interfaces(0, AF_INET, 0,  PROTO_TCP, &ai_lst)!=0)
1846 #ifdef USE_IPV6
1847 #ifdef __OS_linux
1848                                 || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_TCP, &ai_lst) != 0)
1849 #else
1850                                 || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0,  PROTO_TCP, &ai_lst) !=0 )
1851 #endif /* __OS_linux */
1852 #endif /* USE_IPV6 */
1853                                 ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_TCP, 0,
1854                                                                                                                 &tcp_listen)!=0))
1855                                         goto error;
1856                                 free_addr_info_lst(&ai_lst);
1857                                 ai_lst=0;
1858 #ifdef USE_TLS
1859                                 if (!tls_disable){
1860                                         if (((add_interfaces(0, AF_INET, 0, PROTO_TLS,
1861                                                                                 &ai_lst)!=0)
1862 #ifdef USE_IPV6
1863 #ifdef __OS_linux
1864                                 || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_TLS, &ai_lst) != 0)
1865 #else
1866                                 || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0,  PROTO_TLS, &ai_lst)!=0)
1867 #endif /* __OS_linux */
1868 #endif /* USE_IPV6 */
1869                                         ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_TLS, 0,
1870                                                                                                                 &tls_listen)!=0))
1871                                                 goto error;
1872                                 }
1873                                 free_addr_info_lst(&ai_lst);
1874                                 ai_lst=0;
1875 #endif
1876                         }
1877 #endif
1878 #ifdef USE_SCTP
1879                         if (!sctp_disable){
1880                                 if (((add_interfaces(0, AF_INET, 0,  PROTO_SCTP, &ai_lst)!=0)
1881 #ifdef USE_IPV6
1882 #ifdef __OS_linux
1883                                 || (auto_bind_ipv6 && add_interfaces_via_netlink(0, AF_INET6, 0, PROTO_SCTP, &ai_lst) != 0)
1884 #else
1885                                 || (auto_bind_ipv6 && add_interfaces(0, AF_INET6, 0,  PROTO_SCTP, &ai_lst) != 0)
1886 #endif /* __OS_linux */
1887 #endif /* USE_IPV6 */
1888                                         ) || (addr_info_to_si_lst(ai_lst, 0, PROTO_SCTP, 0,
1889                                                                                         &sctp_listen)!=0))
1890                                         goto error;
1891                                 free_addr_info_lst(&ai_lst);
1892                                 ai_lst=0;
1893                         }
1894 #endif /* USE_SCTP */
1895                 }else{
1896                         /* if error fall back to get hostname */
1897                         /* get our address, only the first one */
1898                         if (uname (&myname) <0){
1899                                 LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
1900                                                 " hostname, try -l address\n");
1901                                 goto error;
1902                         }
1903                         if (add_listen_iface(myname.nodename, 0, 0, 0, 0)!=0){
1904                                 LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
1905                                                 "failed \n");
1906                                 goto error;
1907                         }
1908                 }
1909         }
1910         flags=0;
1911         if (fix_socket_list(&udp_listen, &flags)!=0){
1912                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1913                                 " udp failed\n");
1914                 goto error;
1915         }
1916         if (flags){
1917                 socket_types|=flags|SOCKET_T_UDP;
1918         }
1919 #ifdef USE_TCP
1920         flags=0;
1921         if (!tcp_disable && (fix_socket_list(&tcp_listen, &flags)!=0)){
1922                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1923                                 " tcp failed\n");
1924                 goto error;
1925         }
1926         if (flags){
1927                 socket_types|=flags|SOCKET_T_TCP;
1928         }
1929 #ifdef USE_TLS
1930         flags=0;
1931         if (!tls_disable && (fix_socket_list(&tls_listen, &flags)!=0)){
1932                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1933                                 " tls failed\n");
1934                 goto error;
1935         }
1936         if (flags){
1937                 socket_types|=flags|SOCKET_T_TLS;
1938         }
1939 #endif
1940 #endif
1941 #ifdef USE_SCTP
1942         flags=0;
1943         if (!sctp_disable && (fix_socket_list(&sctp_listen, &flags)!=0)){
1944                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1945                                 " sctp failed\n");
1946                 goto error;
1947         }
1948         if (flags){
1949                 socket_types|=flags|SOCKET_T_SCTP;
1950         }
1951 #endif /* USE_SCTP */
1952         if ((udp_listen==0)
1953 #ifdef USE_TCP
1954                         && (tcp_listen==0)
1955 #ifdef USE_TLS
1956                         && (tls_listen==0)
1957 #endif
1958 #endif
1959 #ifdef USE_SCTP
1960                         && (sctp_listen==0)
1961 #endif
1962                 ){
1963                 LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
1964                 goto error;
1965         }
1966         return 0;
1967 error:
1968         if (ai_lst) free_addr_info_lst(&ai_lst);
1969         return -1;
1970 }
1971
1972
1973
1974 void print_all_socket_lists()
1975 {
1976         struct socket_info *si;
1977         struct socket_info** list;
1978         struct addr_info* ai;
1979         unsigned short proto;
1980         
1981         
1982         proto=PROTO_UDP;
1983         do{
1984                 list=get_sock_info_list(proto);
1985                 for(si=list?*list:0; si; si=si->next){
1986                         if (si->addr_info_lst){
1987                                 printf("             %s: (%s",
1988                                                 get_valid_proto_name(proto),
1989                                                 si->address_str.s);
1990                                 for (ai=si->addr_info_lst; ai; ai=ai->next)
1991                                         printf(", %s", ai->address_str.s);
1992                                 printf("):%s%s%s\n",
1993                                                 si->port_no_str.s, 
1994                                                 si->flags & SI_IS_MCAST ? " mcast" : "",
1995                                                 si->flags & SI_IS_MHOMED? " mhomed" : "");
1996                         }else{
1997                                 printf("             %s: %s",
1998                                                 get_valid_proto_name(proto),
1999                                                 si->name.s);
2000                                 if (!si->flags & SI_IS_IP)
2001                                         printf(" [%s]", si->address_str.s);
2002                                 printf( ":%s%s%s\n",
2003                                                 si->port_no_str.s, 
2004                                                 si->flags & SI_IS_MCAST ? " mcast" : "",
2005                                                 si->flags & SI_IS_MHOMED? " mhomed" : "");
2006                         }
2007                 }
2008         }while((proto=next_proto(proto)));
2009 }
2010
2011
2012 void print_aliases()
2013 {
2014         struct host_alias* a;
2015
2016         for(a=aliases; a; a=a->next) 
2017                 if (a->port)
2018                         printf("             %s: %.*s:%d\n", get_valid_proto_name(a->proto), 
2019                                         a->alias.len, a->alias.s, a->port);
2020                 else
2021                         printf("             %s: %.*s:*\n", get_valid_proto_name(a->proto), 
2022                                         a->alias.len, a->alias.s);
2023 }
2024
2025
2026
2027 void init_proto_order()
2028 {
2029         int r;
2030         
2031         /* fix proto list  (remove disabled protocols)*/
2032 #ifdef USE_TCP
2033         if (tcp_disable)
2034 #endif
2035                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
2036                         if (nxt_proto[r]==PROTO_TCP)
2037                                 nxt_proto[r]=nxt_proto[PROTO_TCP];
2038                 }
2039 #ifdef USE_TCP
2040 #ifdef USE_TLS
2041         if (tls_disable || tcp_disable)
2042 #endif
2043 #endif
2044                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
2045                         if (nxt_proto[r]==PROTO_TLS)
2046                                 nxt_proto[r]=nxt_proto[PROTO_TLS];
2047                 }
2048 #ifdef USE_SCTP
2049         if (sctp_disable)
2050 #endif
2051                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
2052                         if (nxt_proto[r]==PROTO_SCTP)
2053                                 nxt_proto[r]=nxt_proto[PROTO_SCTP];
2054                 }
2055 }
2056
2057