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