]>
git.ipfire.org Git - people/ms/dnsmasq.git/blob - src/network.c
1 /* dnsmasq is Copyright (c) 2000-2012 Simon Kelley
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifdef HAVE_LINUX_NETWORK
21 int indextoname(int fd
, int index
, char *name
)
28 ifr
.ifr_ifindex
= index
;
29 if (ioctl(fd
, SIOCGIFNAME
, &ifr
) == -1)
32 strncpy(name
, ifr
.ifr_name
, IF_NAMESIZE
);
38 #elif defined(HAVE_SOLARIS_NETWORK)
42 #ifndef LIFC_UNDER_IPMP
43 # define LIFC_UNDER_IPMP 0
46 int indextoname(int fd
, int index
, char *name
)
50 int numifs
, bufsize
, i
;
57 if (getzoneid() == GLOBAL_ZONEID
)
59 if (!if_indextoname(index
, name
))
64 lifc_flags
= LIFC_NOXMIT
| LIFC_TEMPORARY
| LIFC_ALLZONES
| LIFC_UNDER_IPMP
;
65 lifn
.lifn_family
= AF_UNSPEC
;
66 lifn
.lifn_flags
= lifc_flags
;
67 if (ioctl(fd
, SIOCGLIFNUM
, &lifn
) < 0)
70 numifs
= lifn
.lifn_count
;
71 bufsize
= numifs
* sizeof(struct lifreq
);
73 lifc
.lifc_family
= AF_UNSPEC
;
74 lifc
.lifc_flags
= lifc_flags
;
75 lifc
.lifc_len
= bufsize
;
76 lifc
.lifc_buf
= alloca(bufsize
);
78 if (ioctl(fd
, SIOCGLIFCONF
, &lifc
) < 0)
81 lifrp
= lifc
.lifc_req
;
82 for (i
= lifc
.lifc_len
/ sizeof(struct lifreq
); i
; i
--, lifrp
++)
85 strncpy(lifr
.lifr_name
, lifrp
->lifr_name
, IF_NAMESIZE
);
86 if (ioctl(fd
, SIOCGLIFINDEX
, &lifr
) < 0)
89 if (lifr
.lifr_index
== index
) {
90 strncpy(name
, lifr
.lifr_name
, IF_NAMESIZE
);
100 int indextoname(int fd
, int index
, char *name
)
102 if (index
== 0 || !if_indextoname(index
, name
))
110 int iface_check(int family
, struct all_addr
*addr
, char *name
)
115 /* Note: have to check all and not bail out early, so that we set the
118 if (daemon
->if_names
|| daemon
->if_addrs
)
121 struct dhcp_context
*range
;
127 for (range
= daemon
->dhcp
; range
; range
= range
->next
)
128 if (range
->interface
&& strcmp(range
->interface
, name
) == 0)
132 for (tmp
= daemon
->if_names
; tmp
; tmp
= tmp
->next
)
133 if (tmp
->name
&& (strcmp(tmp
->name
, name
) == 0))
136 for (tmp
= daemon
->if_addrs
; tmp
; tmp
= tmp
->next
)
137 if (tmp
->addr
.sa
.sa_family
== family
)
139 if (family
== AF_INET
&&
140 tmp
->addr
.in
.sin_addr
.s_addr
== addr
->addr
.addr4
.s_addr
)
143 else if (family
== AF_INET6
&&
144 IN6_ARE_ADDR_EQUAL(&tmp
->addr
.in6
.sin6_addr
,
151 for (tmp
= daemon
->if_except
; tmp
; tmp
= tmp
->next
)
152 if (tmp
->name
&& (strcmp(tmp
->name
, name
) == 0))
158 static int iface_allowed(struct irec
**irecp
, int if_index
,
159 union mysockaddr
*addr
, struct in_addr netmask
, int dad
)
162 int fd
, mtu
= 0, loopback
;
164 int tftp_ok
= daemon
->tftp_unlimited
;
169 struct interface_list
*ir
= NULL
;
171 /* check whether the interface IP has been added already
172 we call this routine multiple times. */
173 for (iface
= *irecp
; iface
; iface
= iface
->next
)
174 if (sockaddr_isequal(&iface
->addr
, addr
))
180 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) == -1 ||
181 !indextoname(fd
, if_index
, ifr
.ifr_name
) ||
182 ioctl(fd
, SIOCGIFFLAGS
, &ifr
) == -1)
193 loopback
= ifr
.ifr_flags
& IFF_LOOPBACK
;
198 if (ioctl(fd
, SIOCGIFMTU
, &ifr
) != -1)
203 /* If we are restricting the set of interfaces to use, make
204 sure that loopback interfaces are in that set. */
205 if (daemon
->if_names
&& loopback
)
208 for (lo
= daemon
->if_names
; lo
; lo
= lo
->next
)
209 if (lo
->name
&& strcmp(lo
->name
, ifr
.ifr_name
) == 0)
213 (lo
= whine_malloc(sizeof(struct iname
))) &&
214 (lo
->name
= whine_malloc(strlen(ifr
.ifr_name
)+1)))
216 strcpy(lo
->name
, ifr
.ifr_name
);
218 lo
->next
= daemon
->if_names
;
219 daemon
->if_names
= lo
;
224 /* implement wierd TFTP service rules */
225 for (ir
= daemon
->tftp_interfaces
; ir
; ir
= ir
->next
)
226 if (strcmp(ir
->interface
, ifr
.ifr_name
) == 0)
235 if (addr
->sa
.sa_family
== AF_INET
&&
236 !iface_check(AF_INET
, (struct all_addr
*)&addr
->in
.sin_addr
, ifr
.ifr_name
))
240 for (tmp
= daemon
->dhcp_except
; tmp
; tmp
= tmp
->next
)
241 if (tmp
->name
&& (strcmp(tmp
->name
, ifr
.ifr_name
) == 0))
249 if (addr
->sa
.sa_family
== AF_INET6
&&
250 !iface_check(AF_INET6
, (struct all_addr
*)&addr
->in6
.sin6_addr
, ifr
.ifr_name
))
256 if ((iface
= whine_malloc(sizeof(struct irec
))))
259 iface
->netmask
= netmask
;
260 iface
->tftp_ok
= tftp_ok
;
261 iface
->dhcp_ok
= dhcp_ok
;
265 if ((iface
->name
= whine_malloc(strlen(ifr
.ifr_name
)+1)))
267 strcpy(iface
->name
, ifr
.ifr_name
);
268 iface
->next
= *irecp
;
280 static int iface_allowed_v6(struct in6_addr
*local
, int prefix
,
281 int scope
, int if_index
, int dad
, void *vparam
)
283 union mysockaddr addr
;
284 struct in_addr netmask
; /* dummy */
287 (void)prefix
; /* warning */
288 (void)scope
; /* warning */
290 memset(&addr
, 0, sizeof(addr
));
291 #ifdef HAVE_SOCKADDR_SA_LEN
292 addr
.in6
.sin6_len
= sizeof(addr
.in6
);
294 addr
.in6
.sin6_family
= AF_INET6
;
295 addr
.in6
.sin6_addr
= *local
;
296 addr
.in6
.sin6_port
= htons(daemon
->port
);
297 addr
.in6
.sin6_scope_id
= if_index
;
299 return iface_allowed((struct irec
**)vparam
, if_index
, &addr
, netmask
, dad
);
303 static int iface_allowed_v4(struct in_addr local
, int if_index
,
304 struct in_addr netmask
, struct in_addr broadcast
, void *vparam
)
306 union mysockaddr addr
;
308 memset(&addr
, 0, sizeof(addr
));
309 #ifdef HAVE_SOCKADDR_SA_LEN
310 addr
.in
.sin_len
= sizeof(addr
.in
);
312 addr
.in
.sin_family
= AF_INET
;
313 addr
.in
.sin_addr
= broadcast
; /* warning */
314 addr
.in
.sin_addr
= local
;
315 addr
.in
.sin_port
= htons(daemon
->port
);
317 return iface_allowed((struct irec
**)vparam
, if_index
, &addr
, netmask
, 0);
320 int enumerate_interfaces(void)
323 if (!iface_enumerate(AF_INET6
, &daemon
->interfaces
, iface_allowed_v6
))
327 return iface_enumerate(AF_INET
, &daemon
->interfaces
, iface_allowed_v4
);
330 /* set NONBLOCK bit on fd: See Stevens 16.6 */
335 if ((flags
= fcntl(fd
, F_GETFL
)) == -1 ||
336 fcntl(fd
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
342 static int make_sock(union mysockaddr
*addr
, int type
, int dienow
)
344 int family
= addr
->sa
.sa_family
;
347 if ((fd
= socket(family
, type
, 0)) == -1)
352 /* No error if the kernel just doesn't support this IP flavour */
353 if (errno
== EPROTONOSUPPORT
||
354 errno
== EAFNOSUPPORT
||
359 port
= prettyprint_addr(addr
, daemon
->addrbuff
);
360 if (!option_bool(OPT_NOWILD
) && !option_bool(OPT_CLEVERBIND
))
361 sprintf(daemon
->addrbuff
, "port %d", port
);
362 s
= _("failed to create listening socket for %s: %s");
365 die(s
, daemon
->addrbuff
, EC_BADNET
);
367 my_syslog(LOG_ERR
, s
, daemon
->addrbuff
, strerror(errno
));
371 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof(opt
)) == -1 || !fix_fd(fd
))
375 if (family
== AF_INET6
&& setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, &opt
, sizeof(opt
)) == -1)
379 if ((rc
= bind(fd
, (struct sockaddr
*)addr
, sa_len(addr
))) == -1)
382 if (type
== SOCK_STREAM
)
384 if (listen(fd
, 5) == -1)
387 else if (!option_bool(OPT_NOWILD
))
389 if (family
== AF_INET
)
391 #if defined(HAVE_LINUX_NETWORK)
392 if (setsockopt(fd
, IPPROTO_IP
, IP_PKTINFO
, &opt
, sizeof(opt
)) == -1)
394 #elif defined(IP_RECVDSTADDR) && defined(IP_RECVIF)
395 if (setsockopt(fd
, IPPROTO_IP
, IP_RECVDSTADDR
, &opt
, sizeof(opt
)) == -1 ||
396 setsockopt(fd
, IPPROTO_IP
, IP_RECVIF
, &opt
, sizeof(opt
)) == -1)
401 else if (!set_ipv6pktinfo(fd
))
410 int set_ipv6pktinfo(int fd
)
414 /* The API changed around Linux 2.6.14 but the old ABI is still supported:
415 handle all combinations of headers and kernel.
416 OpenWrt note that this fixes the problem addressed by your very broken patch. */
417 daemon
->v6pktinfo
= IPV6_PKTINFO
;
419 #ifdef IPV6_RECVPKTINFO
420 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &opt
, sizeof(opt
)) != -1)
422 # ifdef IPV6_2292PKTINFO
423 else if (errno
== ENOPROTOOPT
&& setsockopt(fd
, IPPROTO_IPV6
, IPV6_2292PKTINFO
, &opt
, sizeof(opt
)) != -1)
425 daemon
->v6pktinfo
= IPV6_2292PKTINFO
;
430 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_PKTINFO
, &opt
, sizeof(opt
)) != -1)
438 static struct listener
*create_listeners(union mysockaddr
*addr
, int do_tftp
, int dienow
)
440 struct listener
*l
= NULL
;
441 int fd
= -1, tcpfd
= -1, tftpfd
= -1;
443 if (daemon
->port
!= 0)
445 fd
= make_sock(addr
, SOCK_DGRAM
, dienow
);
446 tcpfd
= make_sock(addr
, SOCK_STREAM
, dienow
);
452 if (addr
->sa
.sa_family
== AF_INET
)
454 /* port must be restored to DNS port for TCP code */
455 short save
= addr
->in
.sin_port
;
456 addr
->in
.sin_port
= htons(TFTP_PORT
);
457 tftpfd
= make_sock(addr
, SOCK_DGRAM
, dienow
);
458 addr
->in
.sin_port
= save
;
463 short save
= addr
->in6
.sin6_port
;
464 addr
->in6
.sin6_port
= htons(TFTP_PORT
);
465 tftpfd
= make_sock(addr
, SOCK_DGRAM
, dienow
);
466 addr
->in6
.sin6_port
= save
;
472 if (fd
!= -1 || tcpfd
!= -1 || tftpfd
!= -1)
474 l
= safe_malloc(sizeof(struct listener
));
476 l
->family
= addr
->sa
.sa_family
;
485 void create_wildcard_listeners(void)
487 union mysockaddr addr
;
489 int tftp_enabled
= daemon
->tftp_unlimited
|| daemon
->tftp_interfaces
;
491 memset(&addr
, 0, sizeof(addr
));
492 #ifdef HAVE_SOCKADDR_SA_LEN
493 addr
.in
.sin_len
= sizeof(addr
.in
);
495 addr
.in
.sin_family
= AF_INET
;
496 addr
.in
.sin_addr
.s_addr
= INADDR_ANY
;
497 addr
.in
.sin_port
= htons(daemon
->port
);
499 l
= create_listeners(&addr
, tftp_enabled
, 1);
502 memset(&addr
, 0, sizeof(addr
));
503 # ifdef HAVE_SOCKADDR_SA_LEN
504 addr
.in6
.sin6_len
= sizeof(addr
.in6
);
506 addr
.in6
.sin6_family
= AF_INET6
;
507 addr
.in6
.sin6_addr
= in6addr_any
;
508 addr
.in6
.sin6_port
= htons(daemon
->port
);
511 l
->next
= create_listeners(&addr
, tftp_enabled
, 1);
513 l
= create_listeners(&addr
, tftp_enabled
, 1);
516 daemon
->listeners
= l
;
519 void create_bound_listeners(int dienow
)
521 struct listener
*new;
523 struct iname
*if_tmp
;
525 for (iface
= daemon
->interfaces
; iface
; iface
= iface
->next
)
526 if (!iface
->done
&& !iface
->dad
&&
527 (new = create_listeners(&iface
->addr
, iface
->tftp_ok
, dienow
)))
530 new->next
= daemon
->listeners
;
531 daemon
->listeners
= new;
535 /* Check for --listen-address options that haven't been used because there's
536 no interface with a matching address. These may be valid: eg it's possible
537 to listen on 127.0.1.1 even if the loopback interface is 127.0.0.1
539 If the address isn't valid the bind() will fail and we'll die().
541 The resulting listeners have the ->iface field NULL, and this has to be
542 handled by the DNS and TFTP code. It disables --localise-queries processing
543 (no netmask) and some MTU login the tftp code. */
545 for (if_tmp
= daemon
->if_addrs
; if_tmp
; if_tmp
= if_tmp
->next
)
547 (new = create_listeners(&if_tmp
->addr
, daemon
->tftp_unlimited
, dienow
)))
550 new->next
= daemon
->listeners
;
551 daemon
->listeners
= new;
555 int is_dad_listeners(void)
559 if (option_bool(OPT_NOWILD
))
560 for (iface
= daemon
->interfaces
; iface
; iface
= iface
->next
)
561 if (iface
->dad
&& !iface
->done
)
566 /* return a UDP socket bound to a random port, have to cope with straying into
567 occupied port nos and reserved ones. */
568 int random_sock(int family
)
572 if ((fd
= socket(family
, SOCK_DGRAM
, 0)) != -1)
574 union mysockaddr addr
;
575 unsigned int ports_avail
= 65536u - (unsigned short)daemon
->min_port
;
576 int tries
= ports_avail
< 30 ? 3 * ports_avail
: 100;
578 memset(&addr
, 0, sizeof(addr
));
579 addr
.sa
.sa_family
= family
;
581 /* don't loop forever if all ports in use. */
586 unsigned short port
= rand16();
588 if (daemon
->min_port
!= 0)
589 port
= htons(daemon
->min_port
+ (port
% ((unsigned short)ports_avail
)));
591 if (family
== AF_INET
)
593 addr
.in
.sin_addr
.s_addr
= INADDR_ANY
;
594 addr
.in
.sin_port
= port
;
595 #ifdef HAVE_SOCKADDR_SA_LEN
596 addr
.in
.sin_len
= sizeof(struct sockaddr_in
);
602 addr
.in6
.sin6_addr
= in6addr_any
;
603 addr
.in6
.sin6_port
= port
;
604 #ifdef HAVE_SOCKADDR_SA_LEN
605 addr
.in6
.sin6_len
= sizeof(struct sockaddr_in6
);
610 if (bind(fd
, (struct sockaddr
*)&addr
, sa_len(&addr
)) == 0)
613 if (errno
!= EADDRINUSE
&& errno
!= EACCES
)
624 int local_bind(int fd
, union mysockaddr
*addr
, char *intname
, int is_tcp
)
626 union mysockaddr addr_copy
= *addr
;
628 /* cannot set source _port_ for TCP connections. */
631 if (addr_copy
.sa
.sa_family
== AF_INET
)
632 addr_copy
.in
.sin_port
= 0;
635 addr_copy
.in6
.sin6_port
= 0;
639 if (bind(fd
, (struct sockaddr
*)&addr_copy
, sa_len(&addr_copy
)) == -1)
642 #if defined(SO_BINDTODEVICE)
643 if (intname
[0] != 0 &&
644 setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, intname
, IF_NAMESIZE
) == -1)
651 static struct serverfd
*allocate_sfd(union mysockaddr
*addr
, char *intname
)
653 struct serverfd
*sfd
;
656 /* when using random ports, servers which would otherwise use
657 the INADDR_ANY/port0 socket have sfd set to NULL */
658 if (!daemon
->osport
&& intname
[0] == 0)
662 if (addr
->sa
.sa_family
== AF_INET
&&
663 addr
->in
.sin_addr
.s_addr
== INADDR_ANY
&&
664 addr
->in
.sin_port
== htons(0))
668 if (addr
->sa
.sa_family
== AF_INET6
&&
669 memcmp(&addr
->in6
.sin6_addr
, &in6addr_any
, sizeof(in6addr_any
)) == 0 &&
670 addr
->in6
.sin6_port
== htons(0))
675 /* may have a suitable one already */
676 for (sfd
= daemon
->sfds
; sfd
; sfd
= sfd
->next
)
677 if (sockaddr_isequal(&sfd
->source_addr
, addr
) &&
678 strcmp(intname
, sfd
->interface
) == 0)
681 /* need to make a new one. */
682 errno
= ENOMEM
; /* in case malloc fails. */
683 if (!(sfd
= whine_malloc(sizeof(struct serverfd
))))
686 if ((sfd
->fd
= socket(addr
->sa
.sa_family
, SOCK_DGRAM
, 0)) == -1)
692 if (!local_bind(sfd
->fd
, addr
, intname
, 0) || !fix_fd(sfd
->fd
))
694 errsave
= errno
; /* save error from bind. */
701 strcpy(sfd
->interface
, intname
);
702 sfd
->source_addr
= *addr
;
703 sfd
->next
= daemon
->sfds
;
708 /* create upstream sockets during startup, before root is dropped which may be needed
709 this allows query_port to be a low port and interface binding */
710 void pre_allocate_sfds(void)
714 if (daemon
->query_port
!= 0)
716 union mysockaddr addr
;
717 memset(&addr
, 0, sizeof(addr
));
718 addr
.in
.sin_family
= AF_INET
;
719 addr
.in
.sin_addr
.s_addr
= INADDR_ANY
;
720 addr
.in
.sin_port
= htons(daemon
->query_port
);
721 #ifdef HAVE_SOCKADDR_SA_LEN
722 addr
.in
.sin_len
= sizeof(struct sockaddr_in
);
724 allocate_sfd(&addr
, "");
726 memset(&addr
, 0, sizeof(addr
));
727 addr
.in6
.sin6_family
= AF_INET6
;
728 addr
.in6
.sin6_addr
= in6addr_any
;
729 addr
.in6
.sin6_port
= htons(daemon
->query_port
);
730 #ifdef HAVE_SOCKADDR_SA_LEN
731 addr
.in6
.sin6_len
= sizeof(struct sockaddr_in6
);
733 allocate_sfd(&addr
, "");
737 for (srv
= daemon
->servers
; srv
; srv
= srv
->next
)
738 if (!(srv
->flags
& (SERV_LITERAL_ADDRESS
| SERV_NO_ADDR
| SERV_USE_RESOLV
| SERV_NO_REBIND
)) &&
739 !allocate_sfd(&srv
->source_addr
, srv
->interface
) &&
741 option_bool(OPT_NOWILD
))
743 prettyprint_addr(&srv
->source_addr
, daemon
->namebuff
);
744 if (srv
->interface
[0] != 0)
746 strcat(daemon
->namebuff
, " ");
747 strcat(daemon
->namebuff
, srv
->interface
);
749 die(_("failed to bind server socket for %s: %s"),
750 daemon
->namebuff
, EC_BADNET
);
755 void check_servers(void)
758 struct server
*new, *tmp
, *ret
= NULL
;
761 /* interface may be new since startup */
762 if (!option_bool(OPT_NOWILD
))
763 enumerate_interfaces();
765 for (new = daemon
->servers
; new; new = tmp
)
769 if (!(new->flags
& (SERV_LITERAL_ADDRESS
| SERV_NO_ADDR
| SERV_USE_RESOLV
| SERV_NO_REBIND
)))
771 port
= prettyprint_addr(&new->addr
, daemon
->namebuff
);
773 /* 0.0.0.0 is nothing, the stack treats it like 127.0.0.1 */
774 if (new->addr
.sa
.sa_family
== AF_INET
&&
775 new->addr
.in
.sin_addr
.s_addr
== 0)
781 for (iface
= daemon
->interfaces
; iface
; iface
= iface
->next
)
782 if (sockaddr_isequal(&new->addr
, &iface
->addr
))
786 my_syslog(LOG_WARNING
, _("ignoring nameserver %s - local interface"), daemon
->namebuff
);
791 /* Do we need a socket set? */
793 !(new->sfd
= allocate_sfd(&new->source_addr
, new->interface
)) &&
796 my_syslog(LOG_WARNING
,
797 _("ignoring nameserver %s - cannot make/bind socket: %s"),
798 daemon
->namebuff
, strerror(errno
));
804 /* reverse order - gets it right. */
808 if (!(new->flags
& SERV_NO_REBIND
))
810 if (new->flags
& (SERV_HAS_DOMAIN
| SERV_FOR_NODOTS
| SERV_USE_RESOLV
))
813 if (!(new->flags
& SERV_HAS_DOMAIN
))
814 s1
= _("unqualified"), s2
= _("names");
815 else if (strlen(new->domain
) == 0)
816 s1
= _("default"), s2
= "";
818 s1
= _("domain"), s2
= new->domain
;
820 if (new->flags
& SERV_NO_ADDR
)
821 my_syslog(LOG_INFO
, _("using local addresses only for %s %s"), s1
, s2
);
822 else if (new->flags
& SERV_USE_RESOLV
)
823 my_syslog(LOG_INFO
, _("using standard nameservers for %s %s"), s1
, s2
);
824 else if (!(new->flags
& SERV_LITERAL_ADDRESS
))
825 my_syslog(LOG_INFO
, _("using nameserver %s#%d for %s %s"), daemon
->namebuff
, port
, s1
, s2
);
827 else if (new->interface
[0] != 0)
828 my_syslog(LOG_INFO
, _("using nameserver %s#%d(via %s)"), daemon
->namebuff
, port
, new->interface
);
830 my_syslog(LOG_INFO
, _("using nameserver %s#%d"), daemon
->namebuff
, port
);
834 daemon
->servers
= ret
;
837 /* Return zero if no servers found, in that case we keep polling.
838 This is a protection against an update-time/write race on resolv.conf */
839 int reload_servers(char *fname
)
843 struct server
*old_servers
= NULL
;
844 struct server
*new_servers
= NULL
;
848 /* buff happens to be MAXDNAME long... */
849 if (!(f
= fopen(fname
, "r")))
851 my_syslog(LOG_ERR
, _("failed to read %s: %s"), fname
, strerror(errno
));
855 /* move old servers to free list - we can reuse the memory
856 and not risk malloc if there are the same or fewer new servers.
857 Servers which were specced on the command line go to the new list. */
858 for (serv
= daemon
->servers
; serv
;)
860 struct server
*tmp
= serv
->next
;
861 if (serv
->flags
& SERV_FROM_RESOLV
)
863 serv
->next
= old_servers
;
865 /* forward table rules reference servers, so have to blow them away */
870 serv
->next
= new_servers
;
876 while ((line
= fgets(daemon
->namebuff
, MAXDNAME
, f
)))
878 union mysockaddr addr
, source_addr
;
879 char *token
= strtok(line
, " \t\n\r");
883 if (strcmp(token
, "nameserver") != 0 && strcmp(token
, "server") != 0)
885 if (!(token
= strtok(NULL
, " \t\n\r")))
888 memset(&addr
, 0, sizeof(addr
));
889 memset(&source_addr
, 0, sizeof(source_addr
));
891 if ((addr
.in
.sin_addr
.s_addr
= inet_addr(token
)) != (in_addr_t
) -1)
893 #ifdef HAVE_SOCKADDR_SA_LEN
894 source_addr
.in
.sin_len
= addr
.in
.sin_len
= sizeof(source_addr
.in
);
896 source_addr
.in
.sin_family
= addr
.in
.sin_family
= AF_INET
;
897 addr
.in
.sin_port
= htons(NAMESERVER_PORT
);
898 source_addr
.in
.sin_addr
.s_addr
= INADDR_ANY
;
899 source_addr
.in
.sin_port
= htons(daemon
->query_port
);
905 char *scope_id
= strchr(token
, '%');
910 scope_index
= if_nametoindex(scope_id
);
913 if (inet_pton(AF_INET6
, token
, &addr
.in6
.sin6_addr
) > 0)
915 #ifdef HAVE_SOCKADDR_SA_LEN
916 source_addr
.in6
.sin6_len
= addr
.in6
.sin6_len
= sizeof(source_addr
.in6
);
918 source_addr
.in6
.sin6_family
= addr
.in6
.sin6_family
= AF_INET6
;
919 source_addr
.in6
.sin6_flowinfo
= addr
.in6
.sin6_flowinfo
= 0;
920 addr
.in6
.sin6_port
= htons(NAMESERVER_PORT
);
921 addr
.in6
.sin6_scope_id
= scope_index
;
922 source_addr
.in6
.sin6_addr
= in6addr_any
;
923 source_addr
.in6
.sin6_port
= htons(daemon
->query_port
);
924 source_addr
.in6
.sin6_scope_id
= 0;
937 old_servers
= old_servers
->next
;
939 else if (!(serv
= whine_malloc(sizeof (struct server
))))
942 /* this list is reverse ordered:
943 it gets reversed again in check_servers */
944 serv
->next
= new_servers
;
947 serv
->source_addr
= source_addr
;
949 serv
->interface
[0] = 0;
951 serv
->flags
= SERV_FROM_RESOLV
;
952 serv
->queries
= serv
->failed_queries
= 0;
956 /* Free any memory not used. */
959 struct server
*tmp
= old_servers
->next
;
964 daemon
->servers
= new_servers
;
971 /* Use an IPv4 listener socket for ioctling */
972 struct in_addr
get_ifaddr(char *intr
)
976 struct sockaddr_in ret
;
978 ret
.sin_addr
.s_addr
= -1;
980 for (l
= daemon
->listeners
;
981 l
&& (l
->family
!= AF_INET
|| l
->fd
== -1);
984 strncpy(ifr
.ifr_name
, intr
, IF_NAMESIZE
);
985 ifr
.ifr_addr
.sa_family
= AF_INET
;
987 if (l
&& ioctl(l
->fd
, SIOCGIFADDR
, &ifr
) != -1)
988 memcpy(&ret
, &ifr
.ifr_addr
, sizeof(ret
));