pkg: fix wrong package name, closes FS#148, reported from Andrew Pogrebennyk
[sip-router] / socket_info.c
1 /* $Id$
2  *
3  * find & manage listen addresses 
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License 
25  * along with this program; if not, write to the Free Software 
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28 /*
29  * This file contains code that initializes and handles ser listen addresses
30  * lists (struct socket_info). It is used mainly on startup.
31  * 
32  * History:
33  * --------
34  *  2003-10-22  created by andrei
35  *  2004-10-10  added grep_sock_info (andrei)
36  *  2004-11-08  added find_si (andrei)
37  *  2007-08-23  added detection for INADDR_ANY types of sockets (andrei)
38  *  2008-08-08  sctp support (andrei)
39  *  2008-08-15  support for handling sctp multihomed sockets (andrei)
40  *  2008-10-15  fixed protocol list iteration when some protocols are
41  *               compile time disabled (andrei)
42  */
43
44
45 /*!
46  * \file
47  * \brief SIP-router core :: 
48  * \ingroup core
49  * Module: \ref core
50  */
51
52 #include <string.h>
53 #include <errno.h>
54 #include <unistd.h>
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/utsname.h>
58 #include <stdio.h>
59
60 #include <sys/ioctl.h>
61 #include <net/if.h>
62 #ifdef HAVE_SYS_SOCKIO_H
63 #include <sys/sockio.h>
64 #endif
65
66 #include "globals.h"
67 #include "socket_info.h"
68 #include "dprint.h"
69 #include "mem/mem.h"
70 #include "ut.h"
71 #include "resolve.h"
72 #include "name_alias.h"
73
74
75
76 /* list manip. functions (internal use only) */
77
78
79 /* append */
80 #define sock_listadd(head, el) \
81         do{\
82                 if (*(head)==0) *(head)=(el); \
83                 else{ \
84                         for((el)->next=*(head); (el)->next->next;\
85                                         (el)->next=(el)->next->next); \
86                         (el)->next->next=(el); \
87                         (el)->prev=(el)->next; \
88                         (el)->next=0; \
89                 }\
90         }while(0)
91
92
93 /* insert after "after" */
94 #define sock_listins(el, after) \
95         do{ \
96                 if ((after)){\
97                         (el)->next=(after)->next; \
98                         if ((after)->next) (after)->next->prev=(el); \
99                         (after)->next=(el); \
100                         (el)->prev=(after); \
101                 }else{ /* after==0 = list head */ \
102                         (after)=(el); \
103                         (el)->next=(el)->prev=0; \
104                 }\
105         }while(0)
106
107
108 #define sock_listrm(head, el) \
109         do {\
110                 if (*(head)==(el)) *(head)=(el)->next; \
111                 if ((el)->next) (el)->next->prev=(el)->prev; \
112                 if ((el)->prev) (el)->prev->next=(el)->next; \
113         }while(0)
114
115
116 #define addr_info_listadd sock_listadd
117 #define addr_info_listins sock_listins
118 #define addr_info_listrm sock_listrm
119
120 inline static void addr_info_list_ins_lst(struct addr_info* lst,
121                                                                                 struct addr_info* after)
122 {
123         struct addr_info* l;
124         struct addr_info* n;
125         
126         if (lst){
127                 n=after->next;
128                 after->next=lst;
129                 lst->prev=after;
130                 if (n){
131                         for(l=lst; l->next; l=l->next);
132                         l->next=n;
133                         n->prev=l;
134                 }
135         }
136 }
137
138
139 /* protocol order, filled by init_proto_order() */
140 enum sip_protos nxt_proto[PROTO_LAST+1]=
141 { PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_SCTP, 0 };
142
143
144
145 /* another helper function, it just fills a struct addr_info
146  * returns: 0 on success, -1 on error*/
147 static int init_addr_info(struct addr_info* a,
148                                                                 char* name, enum si_flags flags)
149 {
150
151         memset(a, 0, sizeof(*a));
152         a->name.len=strlen(name);
153         a->name.s=pkg_malloc(a->name.len+1); /* include \0 */
154         if (a->name.s==0) goto error;
155         memcpy(a->name.s, name, a->name.len+1);
156         a->flags=flags;
157         return 0;
158 error:
159         LOG(L_ERR, "ERROR: init_addr_info: memory allocation error\n");
160         return -1;
161 }
162
163
164
165 /* returns 0 on error, new addr_info_lst element on success */
166 static inline struct addr_info* new_addr_info(char* name, 
167                                                                                                         enum si_flags gf)
168 {
169         struct addr_info* al;
170         
171         al=pkg_malloc(sizeof(*al));
172         if (al==0) goto error;
173         al->next=0;
174         al->prev=0;
175         if (init_addr_info(al, name, gf)!=0) goto error;
176         return al;
177 error:
178         LOG(L_ERR, "ERROR: new_addr_info: memory allocation error\n");
179         if (al){
180                 if (al->name.s) pkg_free(al->name.s);
181                 pkg_free(al);
182         }
183         return 0;
184 }
185
186
187
188 static inline void free_addr_info(struct addr_info* a)
189 {
190         if (a){
191                 if (a->name.s){
192                         pkg_free(a->name.s);
193                         a->name.s=0;
194                 }
195                 pkg_free(a);
196         }
197 }
198
199
200
201 static inline void free_addr_info_lst(struct addr_info** lst)
202 {
203         struct addr_info* a;
204         struct addr_info* tmp;
205         
206         a=*lst;
207         while(a){
208                 tmp=a;
209                 a=a->next;
210                 free_addr_info(tmp);
211         }
212 }
213
214
215
216 /* adds a new add_info_lst element to the corresponding list
217  * returns 0 on success, -1 on error */
218 static int new_addr_info2list(char* name, enum si_flags f,
219                                                                 struct addr_info** l)
220 {
221         struct addr_info * al;
222         
223         al=new_addr_info(name, f);
224         if (al==0) goto error;
225         addr_info_listadd(l, al);
226         return 0;
227 error:
228         return -1;
229 }
230
231
232
233 /* another helper function, it just creates a socket_info struct */
234 static inline struct socket_info* new_sock_info(        char* name,
235                                                                 struct name_lst* addr_l,
236                                                                 unsigned short port, unsigned short proto,
237                                                                 enum si_flags flags)
238 {
239         struct socket_info* si;
240         struct name_lst* n;
241         
242         si=(struct socket_info*) pkg_malloc(sizeof(struct socket_info));
243         if (si==0) goto error;
244         memset(si, 0, sizeof(struct socket_info));
245         si->socket=-1;
246         si->name.len=strlen(name);
247         si->name.s=(char*)pkg_malloc(si->name.len+1); /* include \0 */
248         if (si->name.s==0) goto error;
249         memcpy(si->name.s, name, si->name.len+1);
250         /* set port & proto */
251         si->port_no=port;
252         si->proto=proto;
253         si->flags=flags;
254         si->addr_info_lst=0;
255         for (n=addr_l; n; n=n->next){
256                 if (new_addr_info2list(n->name, n->flags, &si->addr_info_lst)!=0){
257                         LOG(L_ERR, "ERROR: new_sockk_info:new_addr_info2list failed\n");
258                         goto error;
259                 }
260         }
261         return si;
262 error:
263         LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
264         if (si) pkg_free(si);
265         return 0;
266 }
267
268
269
270 /*  delete a socket_info struct */
271 static void free_sock_info(struct socket_info* si)
272 {
273         if(si){
274                 if(si->name.s) pkg_free(si->name.s);
275                 if(si->address_str.s) pkg_free(si->address_str.s);
276                 if(si->port_no_str.s) pkg_free(si->port_no_str.s);
277                 if (si->addr_info_lst) free_addr_info_lst(&si->addr_info_lst);
278                 if(si->sock_str.s) pkg_free(si->sock_str.s);
279         }
280 }
281
282
283
284 static char* get_proto_name(unsigned short proto)
285 {
286         switch(proto){
287                 case PROTO_NONE:
288                         return "*";
289                 case PROTO_UDP:
290                         return "udp";
291 #ifdef USE_TCP
292                 case PROTO_TCP:
293                         return "tcp";
294 #endif
295 #ifdef USE_TLS
296                 case PROTO_TLS:
297                         return "tls";
298 #endif
299 #ifdef USE_SCTP
300                 case PROTO_SCTP:
301                         return "sctp";
302 #endif
303                 default:
304                         return "unknown";
305         }
306 }
307
308 /** Convert socket to its textual representation.
309  *
310  * This function converts the transport protocol, the IP address and the port
311  * number in a comma delimited string of form proto:ip:port. The resulting
312  * string is NOT zero terminated
313  *
314  * @param s is a pointer to the destination memory buffer
315  * @param len is a pointer to an integer variable. Initially the variable
316  *        should contain the size of the buffer in s. The value of the variable
317  *        will be changed to the length of the resulting string on success and
318  *        to the desired size of the destination buffer if it is too small
319  * @param si is a pointer to the socket_info structure to be printed
320  * @return -1 on error and 0 on success
321  */
322 int socket2str(char* s, int* len, struct socket_info* si)
323 {
324         str proto;
325         int l;
326         
327         proto.s = get_proto_name(si->proto);
328         proto.len = strlen(proto.s);
329         
330         l = proto.len + si->address_str.len + si->port_no_str.len + 2;
331
332         if(si->address.af==AF_INET6)
333                 l += 2;
334         
335         if (*len < l) {
336                 ERR("socket2str: Destionation buffer too short\n");
337                 *len = l;
338                 return -1;
339         }
340         
341         memcpy(s, proto.s, proto.len);
342         s += proto.len;
343         *s = ':'; s++;
344         if(si->address.af==AF_INET6) {
345                 *s = '['; s++;
346         }
347         memcpy(s, si->address_str.s, si->address_str.len);
348         s += si->address_str.len;
349         if(si->address.af==AF_INET6) {
350                 *s = ']'; s++;
351         }
352         *s = ':'; s++;
353         memcpy(s, si->port_no_str.s, si->port_no_str.len);
354         s += si->port_no_str.len;
355
356         *len = l;
357         return 0;
358 }
359
360
361
362 /* Fill si->sock_str with string representing the socket_info structure,
363  * format of the string is 'proto:address:port'. Returns 0 on success and
364  * negative number on failure.
365  */
366 static int fix_sock_str(struct socket_info* si)
367 {
368         int len = MAX_SOCKET_STR;
369
370         if (si->sock_str.s) pkg_free(si->sock_str.s);
371         
372         si->sock_str.s = pkg_malloc(len + 1);
373         if (si->sock_str.s == NULL) {
374                 ERR("fix_sock_str: No memory left\n");
375                 return -1;
376         }
377         if (socket2str(si->sock_str.s, &len, si) < 0) {
378                 BUG("fix_sock_str: Error in socket2str\n");
379                 return -1;
380         }
381         si->sock_str.s[len] = '\0';
382         si->sock_str.len = len;
383         return 0;
384 }
385
386
387 /* returns 0 if support for the protocol is not compiled or if proto is 
388    invalid */
389 struct socket_info** get_sock_info_list(unsigned short proto)
390 {
391         
392         switch(proto){
393                 case PROTO_UDP:
394                         return &udp_listen;
395                         break;
396                 case PROTO_TCP:
397 #ifdef USE_TCP
398                         return &tcp_listen;
399 #endif
400                         break;
401                 case PROTO_TLS:
402 #ifdef USE_TLS
403                         return &tls_listen;
404 #endif
405                         break;
406                 case PROTO_SCTP:
407 #ifdef USE_SCTP
408                         return &sctp_listen;
409 #endif
410                         break;
411                 default:
412                         LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
413         }
414         return 0;
415 }
416
417
418 /* helper function for grep_sock_info
419  * params:
420  *  host - hostname to compare with
421  *  name - official name
422  *  addr_str - name's resolved ip address converted to string
423  *  ip_addr - name's ip address 
424  *  flags - set to SI_IS_IP if name contains an IP
425  *
426  * returns 0 if host matches, -1 if not */
427 inline static int si_hname_cmp(str* host, str* name, str* addr_str, 
428                                                                 struct ip_addr* ip_addr, int flags)
429 {
430 #ifdef USE_IPV6
431         struct ip_addr* ip6;
432 #endif
433         
434         if ( (host->len==name->len) && 
435                 (strncasecmp(host->s, name->s, name->len)==0) /*slower*/)
436                 /* comp. must be case insensitive, host names
437                  * can be written in mixed case, it will also match
438                  * ipv6 addresses if we are lucky*/
439                 goto found;
440         /* check if host == ip address */
441 #ifdef USE_IPV6
442         /* ipv6 case is uglier, host can be [3ffe::1] */
443         ip6=str2ip6(host);
444         if (ip6){
445                 if (ip_addr_cmp(ip6, ip_addr))
446                         goto found; /* match */
447                 else
448                         return -1; /* no match, but this is an ipv6 address
449                                                  so no point in trying ipv4 */
450         }
451 #endif
452         /* ipv4 */
453         if ( (!(flags&SI_IS_IP)) && (host->len==addr_str->len) && 
454                         (memcmp(host->s, addr_str->s, addr_str->len)==0) )
455                 goto found;
456         return -1;
457 found:
458         return 0;
459 }
460
461
462 /* checks if the proto: host:port is one of the address we listen on
463  * and returns the corresponding socket_info structure.
464  * if port==0, the  port number is ignored
465  * if proto==0 (PROTO_NONE) the protocol is ignored
466  * returns  0 if not found
467  * WARNING: uses str2ip6 so it will overwrite any previous
468  *  unsaved result of this function (static buffer)
469  */
470 struct socket_info* grep_sock_info(str* host, unsigned short port,
471                                                                                                 unsigned short proto)
472 {
473         str hname;
474         struct socket_info* si;
475         struct socket_info** list;
476         struct addr_info* ai;
477         unsigned short c_proto;
478         
479         hname=*host;
480 #ifdef USE_IPV6
481         if ((hname.len>2)&&((*hname.s)=='[')&&(hname.s[hname.len-1]==']')){
482                 /* ipv6 reference, skip [] */
483                 hname.s++;
484                 hname.len-=2;
485         }
486 #endif
487         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
488         do{
489                 /* get the proper sock_list */
490                 list=get_sock_info_list(c_proto);
491         
492                 if (list==0) /* disabled or unknown protocol */
493                         continue;
494                 for (si=*list; si; si=si->next){
495                         DBG("grep_sock_info - checking if host==us: %d==%d && "
496                                         " [%.*s] == [%.*s]\n", 
497                                                 hname.len,
498                                                 si->name.len,
499                                                 hname.len, hname.s,
500                                                 si->name.len, si->name.s
501                                 );
502                         if (port) {
503                                 DBG("grep_sock_info - checking if port %d matches port %d\n", 
504                                                 si->port_no, port);
505                                 if (si->port_no!=port) {
506                                         continue;
507                                 }
508                         }
509                         if (si_hname_cmp(&hname, &si->name, &si->address_str, 
510                                                                 &si->address, si->flags)==0)
511                                 goto found;
512                         /* try among the extra addresses */
513                         for (ai=si->addr_info_lst; ai; ai=ai->next)
514                                 if (si_hname_cmp(&hname, &ai->name, &ai->address_str, 
515                                                                         &ai->address, ai->flags)==0)
516                                         goto found;
517                 }
518         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
519 /* not_found: */
520         return 0;
521 found:
522         return si;
523 }
524
525 /* checks if the proto:port is one of the ports we listen on
526  * and returns the corresponding socket_info structure.
527  * if proto==0 (PROTO_NONE) the protocol is ignored
528  * returns  0 if not found
529  */
530 struct socket_info* grep_sock_info_by_port(unsigned short port, 
531                                                                                         unsigned short proto)
532 {
533         struct socket_info* si;
534         struct socket_info** list;
535         unsigned short c_proto;
536
537         if (!port) {
538                 goto not_found;
539         }
540         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
541         do{
542                 /* get the proper sock_list */
543                 list=get_sock_info_list(c_proto);
544         
545                 if (list==0) /* disabled or unknown protocol */
546                         continue;
547                 
548                 for (si=*list; si; si=si->next){
549                         DBG("grep_sock_info_by_port - checking if port %d matches"
550                                         " port %d\n", si->port_no, port);
551                         if (si->port_no==port) {
552                                 goto found;
553                         }
554                 }
555         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
556 not_found:
557         return 0;
558 found:
559         return si;
560 }
561
562
563
564 /* checks if the proto: ip:port is one of the address we listen on
565  * and returns the corresponding socket_info structure.
566  * (same as grep_socket_info, but use ip addr instead)
567  * if port==0, the  port number is ignored
568  * if proto==0 (PROTO_NONE) the protocol is ignored
569  * returns  0 if not found
570  * WARNING: uses str2ip6 so it will overwrite any previous
571  *  unsaved result of this function (static buffer)
572  */
573 struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
574                                                                                                 unsigned short proto)
575 {
576         struct socket_info* si;
577         struct socket_info** list;
578         struct addr_info* ai;
579         unsigned short c_proto;
580         
581         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
582         do{
583                 /* get the proper sock_list */
584                 list=get_sock_info_list(c_proto);
585         
586                 if (list==0) /* disabled or unknown protocol */
587                         continue;
588                 
589                 for (si=*list; si; si=si->next){
590                         if (port) {
591                                 if (si->port_no!=port) {
592                                         continue;
593                                 }
594                         }
595                         if (ip_addr_cmp(ip, &si->address))
596                                 goto found;
597                         for (ai=si->addr_info_lst; ai; ai=ai->next)
598                                 if (ip_addr_cmp(ip, &ai->address))
599                                         goto found;
600                 }
601         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
602 /* not_found: */
603         return 0;
604 found:
605         return si;
606 }
607
608
609
610 /* append a new sock_info structure to the corresponding list
611  * return  new sock info on success, 0 on error */
612 static struct socket_info* new_sock2list(char* name, struct name_lst* addr_l,
613                                                                         unsigned short port,
614                                                                         unsigned short proto, enum si_flags flags,
615                                                                         struct socket_info** list)
616 {
617         struct socket_info* si;
618         
619         si=new_sock_info(name, addr_l, port, proto, flags);
620         if (si==0){
621                 LOG(L_ERR, "ERROR: new_sock2list: new_sock_info failed\n");
622                 goto error;
623         }
624         sock_listadd(list, si);
625         return si;
626 error:
627         return 0;
628 }
629
630
631
632 /* adds a new sock_info structure immediately after "after"
633  * return  new sock info on success, 0 on error */
634 static struct socket_info* new_sock2list_after(char* name,
635                                                                         struct name_lst* addr_l,
636                                                                         unsigned short port,
637                                                                         unsigned short proto, enum si_flags flags,
638                                                                         struct socket_info* after)
639 {
640         struct socket_info* si;
641         
642         si=new_sock_info(name, addr_l, port, proto, flags);
643         if (si==0){
644                 LOG(L_ERR, "ERROR: new_sock2list_after: new_sock_info failed\n");
645                 goto error;
646         }
647         sock_listins(si, after);
648         return si;
649 error:
650         return 0;
651 }
652
653
654
655 /* adds a sock_info structure to the corresponding proto list
656  * return  0 on success, -1 on error */
657 int add_listen_iface(char* name, struct name_lst* addr_l,
658                                                 unsigned short port, unsigned short proto,
659                                                 enum si_flags flags)
660 {
661         struct socket_info** list;
662         unsigned short c_proto;
663         struct name_lst* a_l;
664         unsigned short c_port;
665         
666         c_proto=(proto!=PROTO_NONE)?proto:PROTO_UDP;
667         do{
668                 list=get_sock_info_list(c_proto);
669                 if (list==0) /* disabled or unknown protocol */
670                         continue;
671                 
672                 if (port==0){ /* use default port */
673                         c_port=
674 #ifdef USE_TLS
675                                 ((c_proto)==PROTO_TLS)?tls_port_no:
676 #endif
677                                 port_no;
678                 }
679 #ifdef USE_TLS
680                 else if ((c_proto==PROTO_TLS) && (proto==0)){
681                         /* -l  ip:port => on udp:ip:port; tcp:ip:port and tls:ip:port+1?*/
682                         c_port=port+1;
683                 }
684 #endif
685                 else{
686                         c_port=port;
687                 }
688                 if (c_proto!=PROTO_SCTP){
689                         if (new_sock2list(name, 0, c_port, c_proto,
690                                                                 flags & ~SI_IS_MHOMED, list)==0){
691                                 LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
692                                 goto error;
693                         }
694                         /* add the other addresses in the list as separate sockets
695                          * since only SCTP can bind to multiple addresses */
696                         for (a_l=addr_l; a_l; a_l=a_l->next){
697                                 if (new_sock2list(a_l->name, 0, c_port, 
698                                                                         c_proto, flags & ~SI_IS_MHOMED, list)==0){
699                                         LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list"
700                                                                 " failed\n");
701                                         goto error;
702                                 }
703                         }
704                 }else{
705                         if (new_sock2list(name, addr_l, c_port, c_proto, flags, list)==0){
706                                 LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
707                                 goto error;
708                         }
709                 }
710         }while( (proto==0) && (c_proto=next_proto(c_proto)));
711         return 0;
712 error:
713         return -1;
714 }
715
716
717
718 /* add all family type addresses of interface if_name to the socket_info array
719  * if if_name==0, adds all addresses on all interfaces
720  * WARNING: it only works with ipv6 addresses on FreeBSD
721  * return: -1 on error, 0 on success
722  */
723 int add_interfaces(char* if_name, int family, unsigned short port,
724                                         unsigned short proto,
725                                         struct addr_info** ai_l)
726 {
727         struct ifconf ifc;
728         struct ifreq ifr;
729         struct ifreq ifrcopy;
730         char*  last;
731         char* p;
732         int size;
733         int lastlen;
734         int s;
735         char* tmp;
736         struct ip_addr addr;
737         int ret;
738         enum si_flags flags;
739         
740 #ifdef HAVE_SOCKADDR_SA_LEN
741         #ifndef MAX
742                 #define MAX(a,b) ( ((a)>(b))?(a):(b))
743         #endif
744 #endif
745         /* ipv4 or ipv6 only*/
746         flags=SI_NONE;
747         s=socket(family, SOCK_DGRAM, 0);
748         ret=-1;
749         lastlen=0;
750         ifc.ifc_req=0;
751         for (size=100; ; size*=2){
752                 ifc.ifc_len=size*sizeof(struct ifreq);
753                 ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
754                 if (ifc.ifc_req==0){
755                         LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
756                         goto error;
757                 }
758                 if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
759                         if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
760                         LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
761                                         strerror(errno));
762                         goto error;
763                 }
764                 if  ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
765                                                                                                                    len not changed*/
766                 lastlen=ifc.ifc_len;
767                 /* try a bigger array*/
768                 pkg_free(ifc.ifc_req);
769         }
770         
771         last=(char*)ifc.ifc_req+ifc.ifc_len;
772         for(p=(char*)ifc.ifc_req; p<last;
773                         p+=
774                         #ifdef __OS_linux
775                                 sizeof(ifr) /* works on x86_64 too */
776                         #else
777                                 (sizeof(ifr.ifr_name)+
778                                 #ifdef  HAVE_SOCKADDR_SA_LEN
779                                         MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
780                                 #else
781                                         ( (ifr.ifr_addr.sa_family==AF_INET)?
782                                                 sizeof(struct sockaddr_in):
783                                         #ifdef USE_IPV6
784                                                 ((ifr.ifr_addr.sa_family==AF_INET6)?
785                                                 sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
786                                         #else /* USE_IPV6 */
787                                                 sizeof(struct sockaddr) )
788                                         #endif /* USE_IPV6 */
789                                 #endif
790                                 )
791                         #endif
792                 )
793         {
794                 /* copy contents into ifr structure
795                  * warning: it might be longer (e.g. ipv6 address) */
796                 memcpy(&ifr, p, sizeof(ifr));
797                 if (ifr.ifr_addr.sa_family!=family){
798                         /*printf("strange family %d skipping...\n",
799                                         ifr->ifr_addr.sa_family);*/
800                         continue;
801                 }
802                 
803                 /*get flags*/
804                 ifrcopy=ifr;
805                 if (ioctl(s, SIOCGIFFLAGS,  &ifrcopy)!=-1){ /* ignore errors */
806                         /* ignore down ifs only if listening on all of them*/
807                         if (if_name==0){ 
808                                 /* if if not up, skip it*/
809                                 if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
810                         }
811                 }
812                 
813                 
814                 
815                 if ((if_name==0)||
816                         (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
817                         
818                         /*add address*/
819                         sockaddr2ip_addr(&addr, 
820                                         (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
821                         if ((tmp=ip_addr2a(&addr))==0) goto error;
822                         /* check if loopback */
823                         if (ifrcopy.ifr_flags & IFF_LOOPBACK) 
824                                 flags|=SI_IS_LO;
825                         /* save the info */
826                         if (new_addr_info2list(tmp, flags, ai_l)!=0){
827                                 LOG(L_ERR, "ERROR: add_interfaces: "
828                                                 "new_addr_info2list failed\n");
829                                 goto error;
830                         }
831                         ret=0;
832                 }
833                         /*
834                         printf("%s:\n", ifr->ifr_name);
835                         printf("        ");
836                         print_sockaddr(&(ifr->ifr_addr));
837                         printf("        ");
838                         ls_ifflags(ifr->ifr_name, family, options);
839                         printf("\n");*/
840         }
841         pkg_free(ifc.ifc_req); /*clean up*/
842         close(s);
843         return  ret;
844 error:
845         if (ifc.ifc_req) pkg_free(ifc.ifc_req);
846         close(s);
847         return -1;
848 }
849
850
851
852 /* internal helper function: resolve host names and add aliases
853  * name is a value result parameter: it should contain the hostname that
854  * will be used to fill all the other members, including name itself
855  * in some situation (name->s should be a 0 terminated pkg_malloc'ed string)
856  * return 0 on success and -1 on error */
857 static int fix_hostname(str* name, struct ip_addr* address, str* address_str,
858                                                 enum si_flags* flags, int* type_flags,
859                                                 struct socket_info* s)
860 {
861         struct hostent* he;
862         char* tmp;
863         char** h;
864         
865         /* get "official hostnames", all the aliases etc. */
866         he=resolvehost(name->s);
867         if (he==0){
868                 LOG(L_ERR, "ERROR: fix_hostname: could not resolve %s\n", name->s);
869                 goto error;
870         }
871         /* check if we got the official name */
872         if (strcasecmp(he->h_name, name->s)!=0){
873                 if (sr_auto_aliases && 
874                                 add_alias(name->s, name->len, s->port_no, s->proto)<0){
875                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
876                 }
877                 /* change the official name */
878                 pkg_free(name->s);
879                 name->s=(char*)pkg_malloc(strlen(he->h_name)+1);
880                 if (name->s==0){
881                         LOG(L_ERR,  "ERROR: fix_hostname: out of memory.\n");
882                         goto error;
883                 }
884                 name->len=strlen(he->h_name);
885                 strncpy(name->s, he->h_name, name->len+1);
886         }
887         /* add the aliases*/
888         for(h=he->h_aliases; sr_auto_aliases && h && *h; h++)
889                 if (add_alias(*h, strlen(*h), s->port_no, s->proto)<0){
890                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
891                 }
892         hostent2ip_addr(address, he, 0); /*convert to ip_addr format*/
893         if (type_flags){
894                 *type_flags|=(address->af==AF_INET)?SOCKET_T_IPV4:SOCKET_T_IPV6;
895         }
896         if ((tmp=ip_addr2a(address))==0) goto error;
897         address_str->s=pkg_malloc(strlen(tmp)+1);
898         if (address_str->s==0){
899                 LOG(L_ERR, "ERROR: fix_hostname: out of memory.\n");
900                 goto error;
901         }
902         strncpy(address_str->s, tmp, strlen(tmp)+1);
903         /* set is_ip (1 if name is an ip address, 0 otherwise) */
904         address_str->len=strlen(tmp);
905         if (sr_auto_aliases && (address_str->len==name->len) &&
906                 (strncasecmp(address_str->s, name->s, address_str->len)==0)){
907                 *flags|=SI_IS_IP;
908                 /* do rev. DNS on it (for aliases)*/
909                 he=rev_resolvehost(address);
910                 if (he==0){
911                         LOG(L_WARN, "WARNING: fix_hostname: could not rev. resolve %s\n",
912                                         name->s);
913                 }else{
914                         /* add the aliases*/
915                         if (add_alias(he->h_name, strlen(he->h_name), s->port_no,
916                                                         s->proto)<0){
917                                 LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
918                         }
919                         for(h=he->h_aliases; h && *h; h++)
920                                 if (add_alias(*h, strlen(*h), s->port_no, s->proto) < 0){
921                                         LOG(L_ERR, "ERROR: fix_hostname: add_alias failed\n");
922                                 }
923                 }
924         }
925         
926 #ifdef USE_MCAST
927         /* Check if it is an multicast address and
928          * set the flag if so
929          */
930         if (is_mcast(address)){
931                 *flags |= SI_IS_MCAST;
932         }
933 #endif /* USE_MCAST */
934         
935         /* check if INADDR_ANY */
936         if (ip_addr_any(address))
937                 *flags|=SI_IS_ANY;
938         else if (ip_addr_loopback(address)) /* check for loopback */
939                 *flags|=SI_IS_LO;
940         
941         return 0;
942 error:
943         return -1;
944 }
945
946
947
948 /* append new elements to a socket_info list after "list"
949  * each element is created  from addr_info_lst + port, protocol and flags
950  * return 0 on succes, -1 on error
951  */
952 static int addr_info_to_si_lst(struct addr_info* ai_lst, unsigned short port,
953                                                                 char proto, enum si_flags flags,
954                                                                 struct socket_info** list)
955 {
956         struct addr_info* ail;
957         
958         for (ail=ai_lst; ail; ail=ail->next){
959                 if(new_sock2list(ail->name.s, 0, port, proto, ail->flags | flags,
960                                                         list)==0)
961                         return -1;
962         }
963         return 0;
964 }
965
966
967
968 /* insert new elements to a socket_info list after "el", 
969  * each element is created from addr_info_lst + port, * protocol and flags
970  * return 0 on succes, -1 on error
971  */
972 static int addr_info_to_si_lst_after(struct addr_info* ai_lst,
973                                                                                 unsigned short port,
974                                                                                 char proto, enum si_flags flags,
975                                                                                 struct socket_info* el)
976 {
977         struct addr_info* ail;
978         struct socket_info* new_si;
979         
980         for (ail=ai_lst; ail; ail=ail->next){
981                 if((new_si=new_sock2list_after(ail->name.s, 0, port, proto,
982                                                                 ail->flags | flags, el))==0)
983                         return -1;
984                 el=new_si;
985         }
986         return 0;
987 }
988
989
990
991 /* fixes a socket list => resolve addresses, 
992  * interface names, fills missing members, remove duplicates
993  * fills type_flags if not null with SOCKET_T_IPV4 and/or SOCKET_T_IPV6*/
994 static int fix_socket_list(struct socket_info **list, int* type_flags)
995 {
996         struct socket_info* si;
997         struct socket_info* new_si;
998         struct socket_info* l;
999         struct socket_info* next;
1000         struct socket_info* next_si;
1001         struct socket_info* del_si;
1002         struct socket_info* keep_si;
1003         char* tmp;
1004         int len;
1005         struct addr_info* ai_lst;
1006         struct addr_info* ail;
1007         struct addr_info* tmp_ail;
1008         struct addr_info* tmp_ail_next;
1009         struct addr_info* ail_next;
1010
1011         if (type_flags)
1012                 *type_flags=0;
1013         /* try to change all the interface names into addresses
1014          *  --ugly hack */
1015         for (si=*list;si;){
1016                 next=si->next;
1017                 ai_lst=0;
1018                 if (add_interfaces(si->name.s, AF_INET, si->port_no,
1019                                                         si->proto, &ai_lst)!=-1){
1020                         if (si->flags & SI_IS_MHOMED){
1021                                 if((new_si=new_sock2list_after(ai_lst->name.s, 0, si->port_no,
1022                                                                                         si->proto,
1023                                                                                         ai_lst->flags|si->flags, si))==0)
1024                                         break;
1025                                 ail=ai_lst;
1026                                 ai_lst=ai_lst->next;
1027                                 free_addr_info(ail); /* free the first elem. */
1028                                 if (ai_lst){
1029                                         ai_lst->prev=0;
1030                                         /* find the end */
1031                                         for (ail=ai_lst; ail->next; ail=ail->next);
1032                                         /* add the mh list after the last position in ai_lst */
1033                                         addr_info_list_ins_lst(si->addr_info_lst, ail);
1034                                         new_si->addr_info_lst=ai_lst;
1035                                         si->addr_info_lst=0; /* detached and moved to new_si */
1036                                         ail=ail->next; /* ail== old si->addr_info_lst */
1037                                 }else{
1038                                         ail=si->addr_info_lst;
1039                                         new_si->addr_info_lst=ail;
1040                                         si->addr_info_lst=0; /* detached and moved to new_si */
1041                                 }
1042                                 
1043                         }else{
1044                                 /* add all addr. as separate  interfaces */
1045                                 if (addr_info_to_si_lst_after(ai_lst, si->port_no, si->proto,
1046                                                                                                 si->flags, si)!=0)
1047                                         goto error;
1048                                 /* ai_lst not needed anymore */
1049                                 free_addr_info_lst(&ai_lst);
1050                                 ail=0;
1051                                 new_si=0;
1052                         }
1053                         /* success => remove current entry (shift the entire array)*/
1054                         sock_listrm(list, si);
1055                         free_sock_info(si);
1056                 }else{
1057                         new_si=si;
1058                         ail=si->addr_info_lst;
1059                 }
1060                 
1061                 if (ail){
1062                         if (new_si && (new_si->flags & SI_IS_MHOMED)){
1063                                 ai_lst=0;
1064                                 for (; ail;){
1065                                         ail_next=ail->next;
1066                                         if (add_interfaces(ail->name.s, AF_INET, new_si->port_no,
1067                                                                                         new_si->proto, &ai_lst)!=-1){
1068                                                 /* add the resolved list after the current position */
1069                                                 addr_info_list_ins_lst(ai_lst, ail);
1070                                                 /* success, remove the current entity */
1071                                                 addr_info_listrm(&new_si->addr_info_lst, ail);
1072                                                 free_addr_info(ail);
1073                                                 ai_lst=0;
1074                                         }
1075                                         ail=ail_next;
1076                                 }
1077                         }
1078                 }
1079                 si=next;
1080         }
1081         /* get ips & fill the port numbers*/
1082 #ifdef EXTRA_DEBUG
1083         DBG("Listening on \n");
1084 #endif
1085         for (si=*list;si;si=si->next){
1086                 /* fix port number, port_no should be !=0 here */
1087                 if (si->port_no==0){
1088 #ifdef USE_TLS
1089                         si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
1090 #else
1091                         si->port_no= port_no;
1092 #endif
1093                 }
1094                 tmp=int2str(si->port_no, &len);
1095                 if (len>=MAX_PORT_LEN){
1096                         LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n", 
1097                                                 si->port_no);
1098                         goto error;
1099                 }
1100                 si->port_no_str.s=(char*)pkg_malloc(len+1);
1101                 if (si->port_no_str.s==0){
1102                         LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
1103                         goto error;
1104                 }
1105                 strncpy(si->port_no_str.s, tmp, len+1);
1106                 si->port_no_str.len=len;
1107                 
1108                 if (fix_hostname(&si->name, &si->address, &si->address_str,
1109                                                 &si->flags, type_flags, si) !=0 )
1110                         goto error;
1111                 /* fix hostnames in mh addresses */
1112                 for (ail=si->addr_info_lst; ail; ail=ail->next){
1113                         if (fix_hostname(&ail->name, &ail->address, &ail->address_str,
1114                                                 &ail->flags, type_flags, si) !=0 )
1115                                 goto error;
1116                 }
1117
1118                 if (fix_sock_str(si) < 0) goto error;
1119                 
1120 #ifdef EXTRA_DEBUG
1121                 printf("              %.*s [%s]:%s%s\n", si->name.len, 
1122                                 si->name.s, si->address_str.s, si->port_no_str.s,
1123                                 si->flags & SI_IS_MCAST ? " mcast" : "");
1124 #endif
1125         }
1126         /* removing duplicate addresses*/
1127         for (si=*list;si; ){
1128                 next_si=si->next;
1129                 for (l=si->next;l;){
1130                         next=l->next;
1131                         if ((si->port_no==l->port_no) &&
1132                                 (si->address.af==l->address.af) &&
1133                                 (memcmp(si->address.u.addr, l->address.u.addr,
1134                                                 si->address.len) == 0)
1135                                 ){
1136                                 /* remove the socket with no  extra addresses.,
1137                                  * if both of them have extra addresses, remove one of them
1138                                  * and merge the extra addresses into the other */
1139                                 if (l->addr_info_lst==0){
1140                                         del_si=l;
1141                                         keep_si=si;
1142                                 }else if (si->addr_info_lst==0){
1143                                         del_si=si;
1144                                         keep_si=l;
1145                                 }else{
1146                                         /* move l->addr_info_lst to si->addr_info_lst */
1147                                         /* find last elem */
1148                                         for (ail=si->addr_info_lst; ail->next; ail=ail->next);
1149                                         /* add the l list after the last position in si lst */
1150                                         addr_info_list_ins_lst(l->addr_info_lst, ail);
1151                                         l->addr_info_lst=0; /* detached */
1152                                         del_si=l; /* l will be removed */
1153                                         keep_si=l;
1154                                 }
1155 #ifdef EXTRA_DEBUG
1156                                 printf("removing duplicate %s [%s] ==  %s [%s]\n",
1157                                                 keep_si->name.s, keep_si->address_str.s,
1158                                                  del_si->name.s, del_si->address_str.s);
1159 #endif
1160                                 /* add the name to the alias list*/
1161                                 if ((!(del_si->flags& SI_IS_IP)) && (
1162                                                 (del_si->name.len!=keep_si->name.len)||
1163                                                 (strncmp(del_si->name.s, keep_si->name.s,
1164                                                                  del_si->name.len)!=0))
1165                                         )
1166                                         add_alias(del_si->name.s, del_si->name.len,
1167                                                                 l->port_no, l->proto);
1168                                 /* make sure next_si doesn't point to del_si */
1169                                 if (del_si==next_si)
1170                                         next_si=next_si->next;
1171                                 /* remove del_si*/
1172                                 sock_listrm(list, del_si);
1173                                 free_sock_info(del_si);
1174                         }
1175                         l=next;
1176                 }
1177                 si=next_si;
1178         }
1179         /* check for duplicates in extra_addresses */
1180         for (si=*list;si; si=si->next){
1181                 /* check  for & remove internal duplicates: */
1182                 for (ail=si->addr_info_lst; ail;){
1183                         ail_next=ail->next;
1184                         /* 1. check if the extra addresses contain a duplicate for the 
1185                          * main  one */
1186                         if ((ail->address.af==si->address.af) &&
1187                                 (memcmp(ail->address.u.addr, si->address.u.addr,
1188                                                         ail->address.len) == 0)){
1189                                 /* add the name to the alias list*/
1190                                 if ((!(ail->flags& SI_IS_IP)) && (
1191                                         (ail->name.len!=si->name.len)||
1192                                         (strncmp(ail->name.s, si->name.s, ail->name.len)!=0)))
1193                                         add_alias(ail->name.s, ail->name.len, si->port_no,
1194                                                                 si->proto);
1195                                         /* remove ail*/
1196                                 addr_info_listrm(&si->addr_info_lst, ail);
1197                                 free_addr_info(ail);
1198                                 ail=ail_next;
1199                                 continue;
1200                         }
1201                         /* 2. check if the extra addresses contain a duplicates for 
1202                          *  other addresses in the same list */
1203                         for (tmp_ail=ail->next; tmp_ail;){
1204                                 tmp_ail_next=tmp_ail->next;
1205                                 if ((ail->address.af==tmp_ail->address.af) &&
1206                                         (memcmp(ail->address.u.addr, tmp_ail->address.u.addr,
1207                                                         ail->address.len) == 0)){
1208                                         /* add the name to the alias list*/
1209                                         if ((!(tmp_ail->flags& SI_IS_IP)) && (
1210                                                 (ail->name.len!=tmp_ail->name.len)||
1211                                                 (strncmp(ail->name.s, tmp_ail->name.s,
1212                                                                                 tmp_ail->name.len)!=0))
1213                                                 )
1214                                                 add_alias(tmp_ail->name.s, tmp_ail->name.len,
1215                                                                         si->port_no, si->proto);
1216                                                 /* remove tmp_ail*/
1217                                         addr_info_listrm(&si->addr_info_lst, tmp_ail);
1218                                         free_addr_info(tmp_ail);
1219                                 }
1220                                 tmp_ail=tmp_ail_next;
1221                         }
1222                         ail=ail_next;
1223                 }
1224                 /* check for duplicates between extra addresses (e.g. sctp MH)
1225                  * and other main addresses, on conflict remove the corresponding
1226                  * extra addresses (another possible solution would be to join
1227                  * the 2 si entries into one). */
1228                 for (ail=si->addr_info_lst; ail;){
1229                         ail_next=ail->next;
1230                         for (l=*list;l; l=l->next){
1231                                 if (l==si) continue;
1232                                 if (si->port_no==l->port_no){
1233                                         if ((ail->address.af==l->address.af) &&
1234                                                 (memcmp(ail->address.u.addr, l->address.u.addr,
1235                                                                                 ail->address.len) == 0)){
1236                                                 /* add the name to the alias list*/
1237                                                 if ((!(ail->flags& SI_IS_IP)) && (
1238                                                         (ail->name.len!=l->name.len)||
1239                                                         (strncmp(ail->name.s, l->name.s, l->name.len)!=0))
1240                                                         )
1241                                                         add_alias(ail->name.s, ail->name.len,
1242                                                                                 l->port_no, l->proto);
1243                                                 /* remove ail*/
1244                                                 addr_info_listrm(&si->addr_info_lst, ail);
1245                                                 free_addr_info(ail);
1246                                                 break;
1247                                         }
1248                                         /* check for duplicates with other  extra addresses
1249                                          * lists */
1250                                         for (tmp_ail=l->addr_info_lst; tmp_ail; ){
1251                                                 tmp_ail_next=tmp_ail->next;
1252                                                 if ((ail->address.af==tmp_ail->address.af) &&
1253                                                         (memcmp(ail->address.u.addr,
1254                                                                         tmp_ail->address.u.addr,
1255                                                                         ail->address.len) == 0)){
1256                                                         /* add the name to the alias list*/
1257                                                         if ((!(tmp_ail->flags& SI_IS_IP)) && (
1258                                                                         (ail->name.len!=tmp_ail->name.len)||
1259                                                                         (strncmp(ail->name.s, tmp_ail->name.s,
1260                                                                                         tmp_ail->name.len)!=0))
1261                                                                 )
1262                                                                 add_alias(tmp_ail->name.s, tmp_ail->name.len,
1263                                                                                 l->port_no, l->proto);
1264                                                         /* remove tmp_ail*/
1265                                                         addr_info_listrm(&l->addr_info_lst, tmp_ail);
1266                                                         free_addr_info(tmp_ail);
1267                                                 }
1268                                                 tmp_ail=tmp_ail_next;
1269                                         }
1270                                 }
1271                         }
1272                         ail=ail_next;
1273                 }
1274         }
1275
1276 #ifdef USE_MCAST
1277              /* Remove invalid multicast entries */
1278         si=*list;
1279         while(si){
1280                 if ((si->proto == PROTO_TCP)
1281 #ifdef USE_TLS
1282                     || (si->proto == PROTO_TLS)
1283 #endif /* USE_TLS */
1284 #ifdef USE_SCTP
1285                         || (si->proto == PROTO_SCTP)
1286 #endif
1287                         ){
1288                         if (si->flags & SI_IS_MCAST){
1289                                 LOG(L_WARN, "WARNING: removing entry %s:%s [%s]:%s\n",
1290                                         get_proto_name(si->proto), si->name.s, 
1291                                         si->address_str.s, si->port_no_str.s);
1292                                 l = si;
1293                                 si=si->next;
1294                                 sock_listrm(list, l);
1295                                 free_sock_info(l);
1296                         }else{
1297                                 ail=si->addr_info_lst;
1298                                 while(ail){
1299                                         if (ail->flags & SI_IS_MCAST){
1300                                                 LOG(L_WARN, "WARNING: removing mh entry %s:%s"
1301                                                                 " [%s]:%s\n",
1302                                                                 get_proto_name(si->proto), ail->name.s, 
1303                                                                 ail->address_str.s, si->port_no_str.s);
1304                                                 tmp_ail=ail;
1305                                                 ail=ail->next;
1306                                                 addr_info_listrm(&si->addr_info_lst, tmp_ail);
1307                                                 free_addr_info(tmp_ail);
1308                                         }else{
1309                                                 ail=ail->next;
1310                                         }
1311                                 }
1312                                 si=si->next;
1313                         }
1314                 } else {
1315                         si=si->next;
1316                 }
1317         }
1318 #endif /* USE_MCAST */
1319
1320         return 0;
1321 error:
1322         return -1;
1323 }
1324
1325 int socket_types = 0;
1326
1327 /* fix all 3 socket lists, fills socket_types if non-null
1328  * return 0 on success, -1 on error */
1329 int fix_all_socket_lists()
1330 {
1331         struct utsname myname;
1332         int flags;
1333         struct addr_info* ai_lst;
1334         
1335         ai_lst=0;
1336         
1337         if ((udp_listen==0)
1338 #ifdef USE_TCP
1339                         && (tcp_listen==0)
1340 #ifdef USE_TLS
1341                         && (tls_listen==0)
1342 #endif
1343 #endif
1344 #ifdef USE_SCTP
1345                         && (sctp_listen==0)
1346 #endif
1347                 ){
1348                 /* get all listening ipv4 interfaces */
1349                 if ((add_interfaces(0, AF_INET, 0,  PROTO_UDP, &ai_lst)==0) &&
1350                         (addr_info_to_si_lst(ai_lst, 0, PROTO_UDP, 0, &udp_listen)==0)){
1351                         free_addr_info_lst(&ai_lst);
1352                         ai_lst=0;
1353                         /* if ok, try to add the others too */
1354 #ifdef USE_TCP
1355                         if (!tcp_disable){
1356                                 if ((add_interfaces(0, AF_INET, 0,  PROTO_TCP, &ai_lst)!=0) ||
1357                                         (addr_info_to_si_lst(ai_lst, 0, PROTO_TCP, 0,
1358                                                                                                                 &tcp_listen)!=0))
1359                                         goto error;
1360                                 free_addr_info_lst(&ai_lst);
1361                                 ai_lst=0;
1362 #ifdef USE_TLS
1363                                 if (!tls_disable){
1364                                         if ((add_interfaces(0, AF_INET, 0, PROTO_TLS,
1365                                                                                 &ai_lst)!=0) ||
1366                                                 (addr_info_to_si_lst(ai_lst, 0, PROTO_TLS, 0,
1367                                                                                                                 &tls_listen)!=0))
1368                                                 goto error;
1369                                 }
1370                                 free_addr_info_lst(&ai_lst);
1371                                 ai_lst=0;
1372 #endif
1373                         }
1374 #endif
1375 #ifdef USE_SCTP
1376                         if (!sctp_disable){
1377                                 if ((add_interfaces(0, AF_INET, 0,  PROTO_SCTP, &ai_lst)!=0)||
1378                                         (addr_info_to_si_lst(ai_lst, 0, PROTO_SCTP, 0,
1379                                                                                                                 &sctp_listen)!=0))
1380                                         goto error;
1381                                 free_addr_info_lst(&ai_lst);
1382                                 ai_lst=0;
1383                         }
1384 #endif /* USE_SCTP */
1385                 }else{
1386                         /* if error fall back to get hostname */
1387                         /* get our address, only the first one */
1388                         if (uname (&myname) <0){
1389                                 LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
1390                                                 " hostname, try -l address\n");
1391                                 goto error;
1392                         }
1393                         if (add_listen_iface(myname.nodename, 0, 0, 0, 0)!=0){
1394                                 LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
1395                                                 "failed \n");
1396                                 goto error;
1397                         }
1398                 }
1399         }
1400         flags=0;
1401         if (fix_socket_list(&udp_listen, &flags)!=0){
1402                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1403                                 " udp failed\n");
1404                 goto error;
1405         }
1406         if (flags){
1407                 socket_types|=flags|SOCKET_T_UDP;
1408         }
1409 #ifdef USE_TCP
1410         flags=0;
1411         if (!tcp_disable && (fix_socket_list(&tcp_listen, &flags)!=0)){
1412                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1413                                 " tcp failed\n");
1414                 goto error;
1415         }
1416         if (flags){
1417                 socket_types|=flags|SOCKET_T_TCP;
1418         }
1419 #ifdef USE_TLS
1420         flags=0;
1421         if (!tls_disable && (fix_socket_list(&tls_listen, &flags)!=0)){
1422                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1423                                 " tls failed\n");
1424                 goto error;
1425         }
1426         if (flags){
1427                 socket_types|=flags|SOCKET_T_TLS;
1428         }
1429 #endif
1430 #endif
1431 #ifdef USE_SCTP
1432         flags=0;
1433         if (!sctp_disable && (fix_socket_list(&sctp_listen, &flags)!=0)){
1434                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
1435                                 " sctp failed\n");
1436                 goto error;
1437         }
1438         if (flags){
1439                 socket_types|=flags|SOCKET_T_SCTP;
1440         }
1441 #endif /* USE_SCTP */
1442         if ((udp_listen==0)
1443 #ifdef USE_TCP
1444                         && (tcp_listen==0)
1445 #ifdef USE_TLS
1446                         && (tls_listen==0)
1447 #endif
1448 #endif
1449 #ifdef USE_SCTP
1450                         && (sctp_listen==0)
1451 #endif
1452                 ){
1453                 LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
1454                 goto error;
1455         }
1456         return 0;
1457 error:
1458         if (ai_lst) free_addr_info_lst(&ai_lst);
1459         return -1;
1460 }
1461
1462
1463
1464 void print_all_socket_lists()
1465 {
1466         struct socket_info *si;
1467         struct socket_info** list;
1468         struct addr_info* ai;
1469         unsigned short proto;
1470         
1471         
1472         proto=PROTO_UDP;
1473         do{
1474                 list=get_sock_info_list(proto);
1475                 for(si=list?*list:0; si; si=si->next){
1476                         if (si->addr_info_lst){
1477                                 printf("             %s: (%s",
1478                                                 get_proto_name(proto),
1479                                                 si->address_str.s);
1480                                 for (ai=si->addr_info_lst; ai; ai=ai->next)
1481                                         printf(", %s", ai->address_str.s);
1482                                 printf("):%s%s%s\n",
1483                                                 si->port_no_str.s, 
1484                                                 si->flags & SI_IS_MCAST ? " mcast" : "",
1485                                                 si->flags & SI_IS_MHOMED? " mhomed" : "");
1486                         }else{
1487                                 printf("             %s: %s",
1488                                                 get_proto_name(proto),
1489                                                 si->name.s);
1490                                 if (!si->flags & SI_IS_IP)
1491                                         printf(" [%s]", si->address_str.s);
1492                                 printf( ":%s%s%s\n",
1493                                                 si->port_no_str.s, 
1494                                                 si->flags & SI_IS_MCAST ? " mcast" : "",
1495                                                 si->flags & SI_IS_MHOMED? " mhomed" : "");
1496                         }
1497                 }
1498         }while((proto=next_proto(proto)));
1499 }
1500
1501
1502 void print_aliases()
1503 {
1504         struct host_alias* a;
1505
1506         for(a=aliases; a; a=a->next) 
1507                 if (a->port)
1508                         printf("             %s: %.*s:%d\n", get_proto_name(a->proto), 
1509                                         a->alias.len, a->alias.s, a->port);
1510                 else
1511                         printf("             %s: %.*s:*\n", get_proto_name(a->proto), 
1512                                         a->alias.len, a->alias.s);
1513 }
1514
1515
1516
1517 void init_proto_order()
1518 {
1519         int r;
1520         
1521         /* fix proto list  (remove disabled protocols)*/
1522 #ifdef USE_TCP
1523         if (tcp_disable)
1524 #endif
1525                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
1526                         if (nxt_proto[r]==PROTO_TCP)
1527                                 nxt_proto[r]=nxt_proto[PROTO_TCP];
1528                 }
1529 #ifdef USE_TCP
1530 #ifdef USE_TLS
1531         if (tls_disable || tcp_disable)
1532 #endif
1533 #endif
1534                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
1535                         if (nxt_proto[r]==PROTO_TLS)
1536                                 nxt_proto[r]=nxt_proto[PROTO_TLS];
1537                 }
1538 #ifdef USE_SCTP
1539         if (sctp_disable)
1540 #endif
1541                 for(r=PROTO_NONE; r<=PROTO_LAST; r++){
1542                         if (nxt_proto[r]==PROTO_SCTP)
1543                                 nxt_proto[r]=nxt_proto[PROTO_SCTP];
1544                 }
1545 }
1546
1547