Modifying the return value of cfg_set* functions, in order to make
[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  */
39
40
41 #include <string.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/utsname.h>
47 #include <stdio.h>
48
49 #include <sys/ioctl.h>
50 #include <net/if.h>
51 #ifdef HAVE_SYS_SOCKIO_H
52 #include <sys/sockio.h>
53 #endif
54
55 #include "globals.h"
56 #include "socket_info.h"
57 #include "dprint.h"
58 #include "mem/mem.h"
59 #include "ut.h"
60 #include "resolve.h"
61 #include "name_alias.h"
62
63
64
65 /* list manip. functions (internal use only) */
66
67
68 /* append */
69 #define sock_listadd(head, el) \
70         do{\
71                 if (*(head)==0) *(head)=(el); \
72                 else{ \
73                         for((el)->next=*(head); (el)->next->next;\
74                                         (el)->next=(el)->next->next); \
75                         (el)->next->next=(el); \
76                         (el)->prev=(el)->next; \
77                         (el)->next=0; \
78                 }\
79         }while(0)
80
81
82 /* insert after "after" */
83 #define sock_listins(el, after) \
84         do{ \
85                 if ((after)){\
86                         (el)->next=(after)->next; \
87                         if ((after)->next) (after)->next->prev=(el); \
88                         (after)->next=(el); \
89                         (el)->prev=(after); \
90                 }else{ /* after==0 = list head */ \
91                         (after)=(el); \
92                         (el)->next=(el)->prev=0; \
93                 }\
94         }while(0)
95
96
97 #define sock_listrm(head, el) \
98         do {\
99                 if (*(head)==(el)) *(head)=(el)->next; \
100                 if ((el)->next) (el)->next->prev=(el)->prev; \
101                 if ((el)->prev) (el)->prev->next=(el)->next; \
102         }while(0)
103
104
105
106 /* another helper function, it just creates a socket_info struct */
107 static inline struct socket_info* new_sock_info(        char* name,
108                                                                 unsigned short port, unsigned short proto,
109                                                                 enum si_flags flags)
110 {
111         struct socket_info* si;
112         
113         si=(struct socket_info*) pkg_malloc(sizeof(struct socket_info));
114         if (si==0) goto error;
115         memset(si, 0, sizeof(struct socket_info));
116         si->socket=-1;
117         si->name.len=strlen(name);
118         si->name.s=(char*)pkg_malloc(si->name.len+1); /* include \0 */
119         if (si->name.s==0) goto error;
120         memcpy(si->name.s, name, si->name.len+1);
121         /* set port & proto */
122         si->port_no=port;
123         si->proto=proto;
124         si->flags=flags;
125         return si;
126 error:
127         LOG(L_ERR, "ERROR: new_sock_info: memory allocation error\n");
128         if (si) pkg_free(si);
129         return 0;
130 }
131
132
133
134 /*  delete a socket_info struct */
135 static void free_sock_info(struct socket_info* si)
136 {
137         if(si){
138                 if(si->name.s) pkg_free(si->name.s);
139                 if(si->address_str.s) pkg_free(si->address_str.s);
140                 if(si->port_no_str.s) pkg_free(si->port_no_str.s);
141         }
142 }
143
144
145
146 static char* get_proto_name(unsigned short proto)
147 {
148         switch(proto){
149                 case PROTO_NONE:
150                         return "*";
151                 case PROTO_UDP:
152                         return "udp";
153 #ifdef USE_TCP
154                 case PROTO_TCP:
155                         return "tcp";
156 #endif
157 #ifdef USE_TLS
158                 case PROTO_TLS:
159                         return "tls";
160 #endif
161                 default:
162                         return "unknown";
163         }
164 }
165
166
167 static struct socket_info** get_sock_info_list(unsigned short proto)
168 {
169         
170         switch(proto){
171                 case PROTO_UDP:
172                         return &udp_listen;
173                         break;
174 #ifdef USE_TCP
175                 case PROTO_TCP:
176                         return &tcp_listen;
177                         break;
178 #endif
179 #ifdef USE_TLS
180                 case PROTO_TLS:
181                         return &tls_listen;
182                         break;
183 #endif
184                 default:
185                         LOG(L_CRIT, "BUG: get_sock_info_list: invalid proto %d\n", proto);
186         }
187         return 0;
188 }
189
190
191
192 /* checks if the proto: host:port is one of the address we listen on
193  * and returns the corresponding socket_info structure.
194  * if port==0, the  port number is ignored
195  * if proto==0 (PROTO_NONE) the protocol is ignored
196  * returns  0 if not found
197  * WARNING: uses str2ip6 so it will overwrite any previous
198  *  unsaved result of this function (static buffer)
199  */
200 struct socket_info* grep_sock_info(str* host, unsigned short port,
201                                                                                                 unsigned short proto)
202 {
203         char* hname;
204         int h_len;
205         struct socket_info* si;
206         struct socket_info** list;
207         unsigned short c_proto;
208 #ifdef USE_IPV6
209         struct ip_addr* ip6;
210 #endif
211         h_len=host->len;
212         hname=host->s;
213 #ifdef USE_IPV6
214         if ((h_len>2)&&((*hname)=='[')&&(hname[h_len-1]==']')){
215                 /* ipv6 reference, skip [] */
216                 hname++;
217                 h_len-=2;
218         }
219 #endif
220         c_proto=proto?proto:PROTO_UDP;
221         do{
222                 /* get the proper sock_list */
223                 if (c_proto==PROTO_NONE)
224                         list=&udp_listen;
225                 else
226                         list=get_sock_info_list(c_proto);
227         
228                 if (list==0){
229                         LOG(L_WARN, "WARNING: grep_sock_info: "
230                                                 "unknown proto %d\n", c_proto);
231                         goto not_found; /* false */
232                 }
233                 for (si=*list; si; si=si->next){
234                         DBG("grep_sock_info - checking if host==us: %d==%d && "
235                                         " [%.*s] == [%.*s]\n", 
236                                                 h_len,
237                                                 si->name.len,
238                                                 h_len, hname,
239                                                 si->name.len, si->name.s
240                                 );
241                         if (port) {
242                                 DBG("grep_sock_info - checking if port %d matches port %d\n", 
243                                                 si->port_no, port);
244                                 if (si->port_no!=port) {
245                                         continue;
246                                 }
247                         }
248                         if ( (h_len==si->name.len) && 
249                                 (strncasecmp(hname, si->name.s,
250                                                  si->name.len)==0) /*slower*/)
251                                 /* comp. must be case insensitive, host names
252                                  * can be written in mixed case, it will also match
253                                  * ipv6 addresses if we are lucky*/
254                                 goto found;
255                 /* check if host == ip address */
256 #ifdef USE_IPV6
257                         /* ipv6 case is uglier, host can be [3ffe::1] */
258                         ip6=str2ip6(host);
259                         if (ip6){
260                                 if (ip_addr_cmp(ip6, &si->address))
261                                         goto found; /* match */
262                                 else
263                                         continue; /* no match, but this is an ipv6 address
264                                                                  so no point in trying ipv4 */
265                         }
266 #endif
267                         /* ipv4 */
268                         if (    (!(si->flags&SI_IS_IP)) &&
269                                         (h_len==si->address_str.len) && 
270                                 (memcmp(hname, si->address_str.s, 
271                                                                         si->address_str.len)==0)
272                                 )
273                                 goto found;
274                 }
275         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
276 not_found:
277         return 0;
278 found:
279         return si;
280 }
281
282
283
284 /* checks if the proto: ip:port is one of the address we listen on
285  * and returns the corresponding socket_info structure.
286  * (same as grep_socket_info, but use ip addr instead)
287  * if port==0, the  port number is ignored
288  * if proto==0 (PROTO_NONE) the protocol is ignored
289  * returns  0 if not found
290  * WARNING: uses str2ip6 so it will overwrite any previous
291  *  unsaved result of this function (static buffer)
292  */
293 struct socket_info* find_si(struct ip_addr* ip, unsigned short port,
294                                                                                                 unsigned short proto)
295 {
296         struct socket_info* si;
297         struct socket_info** list;
298         unsigned short c_proto;
299         
300         c_proto=proto?proto:PROTO_UDP;
301         do{
302                 /* get the proper sock_list */
303                 if (c_proto==PROTO_NONE)
304                         list=&udp_listen;
305                 else
306                         list=get_sock_info_list(c_proto);
307         
308                 if (list==0){
309                         LOG(L_WARN, "WARNING: grep_sock_info: "
310                                                 "unknown proto %d\n", c_proto);
311                         goto not_found; /* false */
312                 }
313                 for (si=*list; si; si=si->next){
314                         if (port) {
315                                 if (si->port_no!=port) {
316                                         continue;
317                                 }
318                         }
319                         if (ip_addr_cmp(ip, &si->address))
320                                 goto found;
321                 }
322         }while( (proto==0) && (c_proto=next_proto(c_proto)) );
323 not_found:
324         return 0;
325 found:
326         return si;
327 }
328
329
330
331 /* adds a new sock_info structure to the corresponding list
332  * return  0 on success, -1 on error */
333 int new_sock2list(char* name, unsigned short port, unsigned short proto,
334                                                 enum si_flags flags, struct socket_info** list)
335 {
336         struct socket_info* si;
337         
338         si=new_sock_info(name, port, proto, flags);
339         if (si==0){
340                 LOG(L_ERR, "ERROR: add_listen_iface: new_sock_info failed\n");
341                 goto error;
342         }
343         sock_listadd(list, si);
344         return 0;
345 error:
346         return -1;
347 }
348
349
350
351 /* adds a sock_info structure to the corresponding proto list
352  * return  0 on success, -1 on error */
353 int add_listen_iface(char* name, unsigned short port, unsigned short proto,
354                                                 enum si_flags flags)
355 {
356         struct socket_info** list;
357         unsigned short c_proto;
358         
359         c_proto=(proto)?proto:PROTO_UDP;
360         do{
361                 list=get_sock_info_list(c_proto);
362                 if (list==0){
363                         LOG(L_ERR, "ERROR: add_listen_iface: get_sock_info_list failed\n");
364                         goto error;
365                 }
366                 if (port==0){ /* use default port */
367                         port=
368 #ifdef USE_TLS
369                                 ((c_proto)==PROTO_TLS)?tls_port_no:
370 #endif
371                                 port_no;
372                 }
373 #ifdef USE_TLS
374                 else if ((c_proto==PROTO_TLS) && (proto==0)){
375                         /* -l  ip:port => on udp:ip:port; tcp:ip:port and tls:ip:port+1? */
376                         port++;
377                 }
378 #endif
379                 if (new_sock2list(name, port, c_proto, flags, list)<0){
380                         LOG(L_ERR, "ERROR: add_listen_iface: new_sock2list failed\n");
381                         goto error;
382                 }
383         }while( (proto==0) && (c_proto=next_proto(c_proto)));
384         return 0;
385 error:
386         return -1;
387 }
388
389
390
391 /* add all family type addresses of interface if_name to the socket_info array
392  * if if_name==0, adds all addresses on all interfaces
393  * WARNING: it only works with ipv6 addresses on FreeBSD
394  * return: -1 on error, 0 on success
395  */
396 int add_interfaces(char* if_name, int family, unsigned short port,
397                                         unsigned short proto,
398                                         struct socket_info** list)
399 {
400         struct ifconf ifc;
401         struct ifreq ifr;
402         struct ifreq ifrcopy;
403         char*  last;
404         char* p;
405         int size;
406         int lastlen;
407         int s;
408         char* tmp;
409         struct ip_addr addr;
410         int ret;
411         enum si_flags flags;
412         
413 #ifdef HAVE_SOCKADDR_SA_LEN
414         #ifndef MAX
415                 #define MAX(a,b) ( ((a)>(b))?(a):(b))
416         #endif
417 #endif
418         /* ipv4 or ipv6 only*/
419         flags=SI_NONE;
420         s=socket(family, SOCK_DGRAM, 0);
421         ret=-1;
422         lastlen=0;
423         ifc.ifc_req=0;
424         for (size=100; ; size*=2){
425                 ifc.ifc_len=size*sizeof(struct ifreq);
426                 ifc.ifc_req=(struct ifreq*) pkg_malloc(size*sizeof(struct ifreq));
427                 if (ifc.ifc_req==0){
428                         LOG(L_ERR, "ERROR: add_interfaces: memory allocation failure\n");
429                         goto error;
430                 }
431                 if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
432                         if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
433                         LOG(L_ERR, "ERROR: add_interfaces: ioctl failed: %s\n",
434                                         strerror(errno));
435                         goto error;
436                 }
437                 if  ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
438                                                                                                                    len not changed*/
439                 lastlen=ifc.ifc_len;
440                 /* try a bigger array*/
441                 pkg_free(ifc.ifc_req);
442         }
443         
444         last=(char*)ifc.ifc_req+ifc.ifc_len;
445         for(p=(char*)ifc.ifc_req; p<last;
446                         p+=
447                         #ifdef __OS_linux
448                                 sizeof(ifr) /* works on x86_64 too */
449                         #else
450                                 (sizeof(ifr.ifr_name)+
451                                 #ifdef  HAVE_SOCKADDR_SA_LEN
452                                         MAX(ifr.ifr_addr.sa_len, sizeof(struct sockaddr))
453                                 #else
454                                         ( (ifr.ifr_addr.sa_family==AF_INET)?
455                                                 sizeof(struct sockaddr_in):
456                                                 ((ifr.ifr_addr.sa_family==AF_INET6)?
457                                                 sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
458                                 #endif
459                                 )
460                         #endif
461                 )
462         {
463                 /* copy contents into ifr structure
464                  * warning: it might be longer (e.g. ipv6 address) */
465                 memcpy(&ifr, p, sizeof(ifr));
466                 if (ifr.ifr_addr.sa_family!=family){
467                         /*printf("strange family %d skipping...\n",
468                                         ifr->ifr_addr.sa_family);*/
469                         continue;
470                 }
471                 
472                 /*get flags*/
473                 ifrcopy=ifr;
474                 if (ioctl(s, SIOCGIFFLAGS,  &ifrcopy)!=-1){ /* ignore errors */
475                         /* ignore down ifs only if listening on all of them*/
476                         if (if_name==0){ 
477                                 /* if if not up, skip it*/
478                                 if (!(ifrcopy.ifr_flags & IFF_UP)) continue;
479                         }
480                 }
481                 
482                 
483                 
484                 if ((if_name==0)||
485                         (strncmp(if_name, ifr.ifr_name, sizeof(ifr.ifr_name))==0)){
486                         
487                         /*add address*/
488                         sockaddr2ip_addr(&addr, 
489                                         (struct sockaddr*)(p+(long)&((struct ifreq*)0)->ifr_addr));
490                         if ((tmp=ip_addr2a(&addr))==0) goto error;
491                         /* check if loopback */
492                         if (ifrcopy.ifr_flags & IFF_LOOPBACK) 
493                                 flags|=SI_IS_LO;
494                         /* add it to one of the lists */
495                         if (new_sock2list(tmp, port, proto, flags, list)!=0){
496                                 LOG(L_ERR, "ERROR: add_interfaces: new_sock2list failed\n");
497                                 goto error;
498                         }
499                         ret=0;
500                 }
501                         /*
502                         printf("%s:\n", ifr->ifr_name);
503                         printf("        ");
504                         print_sockaddr(&(ifr->ifr_addr));
505                         printf("        ");
506                         ls_ifflags(ifr->ifr_name, family, options);
507                         printf("\n");*/
508         }
509         pkg_free(ifc.ifc_req); /*clean up*/
510         close(s);
511         return  ret;
512 error:
513         if (ifc.ifc_req) pkg_free(ifc.ifc_req);
514         close(s);
515         return -1;
516 }
517
518
519
520 /* fixes a socket list => resolve addresses, 
521  * interface names, fills missing members, remove duplicates
522  * fills type_flags if not null with SOCKET_T_IPV4 and/or SOCKET_T_IPV6*/
523 static int fix_socket_list(struct socket_info **list, int* type_flags)
524 {
525         struct socket_info* si;
526         struct socket_info* l;
527         struct socket_info* next;
528         char* tmp;
529         int len;
530         struct hostent* he;
531         char** h;
532         
533         if (type_flags)
534                 *type_flags=0;
535         /* try to change all the interface names into addresses
536          *  --ugly hack */
537         for (si=*list;si;){
538                 next=si->next;
539                 if (add_interfaces(si->name.s, AF_INET, si->port_no,
540                                                         si->proto, list)!=-1){
541                         /* success => remove current entry (shift the entire array)*/
542                         sock_listrm(list, si);
543                         free_sock_info(si);
544                 }
545                 si=next;
546         }
547         /* get ips & fill the port numbers*/
548 #ifdef EXTRA_DEBUG
549         DBG("Listening on \n");
550 #endif
551         for (si=*list;si;si=si->next){
552                 /* fix port number, port_no should be !=0 here */
553                 if (si->port_no==0){
554 #ifdef USE_TLS
555                         si->port_no= (si->proto==PROTO_TLS)?tls_port_no:port_no;
556 #else
557                         si->port_no= port_no;
558 #endif
559                 }
560                 tmp=int2str(si->port_no, &len);
561                 if (len>=MAX_PORT_LEN){
562                         LOG(L_ERR, "ERROR: fix_socket_list: bad port number: %d\n", 
563                                                 si->port_no);
564                         goto error;
565                 }
566                 si->port_no_str.s=(char*)pkg_malloc(len+1);
567                 if (si->port_no_str.s==0){
568                         LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
569                         goto error;
570                 }
571                 strncpy(si->port_no_str.s, tmp, len+1);
572                 si->port_no_str.len=len;
573                 
574                 /* get "official hostnames", all the aliases etc. */
575                 he=resolvehost(si->name.s);
576                 if (he==0){
577                         LOG(L_ERR, "ERROR: fix_socket_list: could not resolve %s\n",
578                                         si->name.s);
579                         goto error;
580                 }
581                 /* check if we got the official name */
582                 if (strcasecmp(he->h_name, si->name.s)!=0){
583                         if (add_alias(si->name.s, si->name.len,
584                                                         si->port_no, si->proto)<0){
585                                 LOG(L_ERR, "ERROR: fix_socket_list: add_alias failed\n");
586                         }
587                         /* change the official name */
588                         pkg_free(si->name.s);
589                         si->name.s=(char*)pkg_malloc(strlen(he->h_name)+1);
590                         if (si->name.s==0){
591                                 LOG(L_ERR,  "ERROR: fix_socket_list: out of memory.\n");
592                                 goto error;
593                         }
594                         si->name.len=strlen(he->h_name);
595                         strncpy(si->name.s, he->h_name, si->name.len+1);
596                 }
597                 /* add the aliases*/
598                 for(h=he->h_aliases; h && *h; h++)
599                         if (add_alias(*h, strlen(*h), si->port_no, si->proto)<0){
600                                 LOG(L_ERR, "ERROR: fix_socket_list: add_alias failed\n");
601                         }
602                 hostent2ip_addr(&si->address, he, 0); /*convert to ip_addr 
603                                                                                                                  format*/
604                 if (type_flags){
605                         *type_flags|=(si->address.af==AF_INET)?SOCKET_T_IPV4:SOCKET_T_IPV6;
606                 }
607                 if ((tmp=ip_addr2a(&si->address))==0) goto error;
608                 si->address_str.s=(char*)pkg_malloc(strlen(tmp)+1);
609                 if (si->address_str.s==0){
610                         LOG(L_ERR, "ERROR: fix_socket_list: out of memory.\n");
611                         goto error;
612                 }
613                 strncpy(si->address_str.s, tmp, strlen(tmp)+1);
614                 /* set is_ip (1 if name is an ip address, 0 otherwise) */
615                 si->address_str.len=strlen(tmp);
616                 if      (       (si->address_str.len==si->name.len)&&
617                                 (strncasecmp(si->address_str.s, si->name.s,
618                                                                 si->address_str.len)==0)
619                         ){
620                                 si->flags|=SI_IS_IP;
621                                 /* do rev. DNS on it (for aliases)*/
622                                 he=rev_resolvehost(&si->address);
623                                 if (he==0){
624                                         LOG(L_WARN, "WARNING: fix_socket_list:"
625                                                         " could not rev. resolve %s\n",
626                                                         si->name.s);
627                                 }else{
628                                         /* add the aliases*/
629                                         if (add_alias(he->h_name, strlen(he->h_name),
630                                                                         si->port_no, si->proto)<0){
631                                                 LOG(L_ERR, "ERROR: fix_socket_list:"
632                                                                         "add_alias failed\n");
633                                         }
634                                         for(h=he->h_aliases; h && *h; h++)
635                                                 if (add_alias(*h,strlen(*h),si->port_no,si->proto)<0){
636                                                         LOG(L_ERR, "ERROR: fix_socket_list:"
637                                                                         " add_alias failed\n");
638                                                 }
639                                 }
640                 }
641
642 #ifdef USE_MCAST
643                      /* Check if it is an multicast address and
644                       * set the flag if so
645                       */
646                 if (is_mcast(&si->address)) {
647                         si->flags |= SI_IS_MCAST;
648                 }
649 #endif /* USE_MCAST */
650                 
651                 /* check if INADDR_ANY */
652                 if (ip_addr_any(&si->address))
653                         si->flags|=SI_IS_ANY;
654
655 #ifdef EXTRA_DEBUG
656                 printf("              %.*s [%s]:%s%s\n", si->name.len, 
657                                 si->name.s, si->address_str.s, si->port_no_str.s,
658                                 si->flags & SI_IS_MCAST ? " mcast" : "");
659 #endif
660         }
661         /* removing duplicate addresses*/
662         for (si=*list;si; si=si->next){
663                 for (l=si->next;l;){
664                         next=l->next;
665                         if ((si->port_no==l->port_no) &&
666                                 (si->address.af==l->address.af) &&
667                                 (memcmp(si->address.u.addr, l->address.u.addr, si->address.len)
668                                         == 0)
669                                 ){
670 #ifdef EXTRA_DEBUG
671                                 printf("removing duplicate %s [%s] ==  %s [%s]\n",
672                                                 si->name.s, si->address_str.s,
673                                                  l->name.s, l->address_str.s);
674 #endif
675                                 /* add the name to the alias list*/
676                                 if ((!(l->flags& SI_IS_IP)) && (
677                                                 (l->name.len!=si->name.len)||
678                                                 (strncmp(l->name.s, si->name.s, si->name.len)!=0))
679                                         )
680                                         add_alias(l->name.s, l->name.len, l->port_no, l->proto);
681                                                 
682                                 /* remove l*/
683                                 sock_listrm(list, l);
684                                 free_sock_info(l);
685                         }
686                         l=next;
687                 }
688         }
689
690 #ifdef USE_MCAST
691              /* Remove invalid multicast entries */
692         si=*list;
693         while(si){
694                 if ((si->flags & SI_IS_MCAST) && 
695                     ((si->proto == PROTO_TCP)
696 #ifdef USE_TLS
697                     || (si->proto == PROTO_TLS)
698 #endif /* USE_TLS */
699                     )){
700                         LOG(L_WARN, "WARNING: removing entry %s:%s [%s]:%s\n",
701                             get_proto_name(si->proto), si->name.s, 
702                             si->address_str.s, si->port_no_str.s);
703                         l = si;
704                         si=si->next;
705                         sock_listrm(list, l);
706                         free_sock_info(l);
707                 } else {
708                         si=si->next;
709                 }
710         }
711 #endif /* USE_MCAST */
712
713         return 0;
714 error:
715         return -1;
716 }
717
718
719
720 /* fix all 3 socket lists, fills socket_types if non-null
721  * return 0 on success, -1 on error */
722 int fix_all_socket_lists(int* socket_types)
723 {
724         struct utsname myname;
725         int flags;
726         
727         if (socket_types)
728                 *socket_types=0;
729         
730         if ((udp_listen==0)
731 #ifdef USE_TCP
732                         && (tcp_listen==0)
733 #ifdef USE_TLS
734                         && (tls_listen==0)
735 #endif
736 #endif
737                 ){
738                 /* get all listening ipv4 interfaces */
739                 if (add_interfaces(0, AF_INET, 0,  PROTO_UDP, &udp_listen)==0){
740                         /* if ok, try to add the others too */
741 #ifdef USE_TCP
742                         if (!tcp_disable){
743                                 if (add_interfaces(0, AF_INET, 0,  PROTO_TCP, &tcp_listen)!=0)
744                                         goto error;
745 #ifdef USE_TLS
746                                 if (!tls_disable){
747                                         if (add_interfaces(0, AF_INET, 0, PROTO_TLS,
748                                                                 &tls_listen)!=0)
749                                         goto error;
750                                 }
751 #endif
752                         }
753 #endif
754                 }else{
755                         /* if error fall back to get hostname */
756                         /* get our address, only the first one */
757                         if (uname (&myname) <0){
758                                 LOG(L_ERR, "ERROR: fix_all_socket_lists: cannot determine"
759                                                 " hostname, try -l address\n");
760                                 goto error;
761                         }
762                         if (add_listen_iface(myname.nodename, 0, 0, 0)!=0){
763                                 LOG(L_ERR, "ERROR: fix_all_socket_lists: add_listen_iface "
764                                                 "failed \n");
765                                 goto error;
766                         }
767                 }
768         }
769         if (fix_socket_list(&udp_listen, &flags)!=0){
770                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
771                                 " udp failed\n");
772                 goto error;
773         }
774         if (flags && socket_types){
775                 *socket_types|=flags|SOCKET_T_UDP;
776         }
777 #ifdef USE_TCP
778         if (!tcp_disable && (fix_socket_list(&tcp_listen, &flags)!=0)){
779                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
780                                 " tcp failed\n");
781                 goto error;
782         }
783         if (flags && socket_types){
784                 *socket_types|=flags|SOCKET_T_TCP;
785         }
786 #ifdef USE_TLS
787         if (!tls_disable && (fix_socket_list(&tls_listen, &flags)!=0)){
788                 LOG(L_ERR, "ERROR: fix_all_socket_lists: fix_socket_list"
789                                 " tls failed\n");
790                 goto error;
791         }
792         if (flags && socket_types){
793                 *socket_types|=flags|SOCKET_T_TLS;
794         }
795 #endif
796 #endif
797         if ((udp_listen==0)
798 #ifdef USE_TCP
799                         && (tcp_listen==0)
800 #ifdef USE_TLS
801                         && (tls_listen==0)
802 #endif
803 #endif
804                 ){
805                 LOG(L_ERR, "ERROR: fix_all_socket_lists: no listening sockets\n");
806                 goto error;
807         }
808         return 0;
809 error:
810         return -1;
811 }
812
813
814
815 void print_all_socket_lists()
816 {
817         struct socket_info *si;
818         struct socket_info** list;
819         unsigned short proto;
820         
821         
822         proto=PROTO_UDP;
823         do{
824                 list=get_sock_info_list(proto);
825                 for(si=list?*list:0; si; si=si->next){
826                         printf("             %s: %s [%s]:%s%s\n", get_proto_name(proto),
827                                si->name.s, si->address_str.s, si->port_no_str.s, 
828                                si->flags & SI_IS_MCAST ? " mcast" : "");
829                 }
830         }while((proto=next_proto(proto)));
831 }
832
833
834 void print_aliases()
835 {
836         struct host_alias* a;
837
838         for(a=aliases; a; a=a->next) 
839                 if (a->port)
840                         printf("             %s: %.*s:%d\n", get_proto_name(a->proto), 
841                                         a->alias.len, a->alias.s, a->port);
842                 else
843                         printf("             %s: %.*s:*\n", get_proto_name(a->proto), 
844                                         a->alias.len, a->alias.s);
845 }