mirror of
https://github.com/pi-hole/dnsmasq.git
synced 2025-12-19 18:28:25 +00:00
import of dnsmasq-2.59.tar.gz
This commit is contained in:
105
src/network.c
105
src/network.c
@@ -187,7 +187,7 @@ int iface_check(int family, struct all_addr *addr, char *name, int *indexp)
|
||||
}
|
||||
|
||||
static int iface_allowed(struct irec **irecp, int if_index,
|
||||
union mysockaddr *addr, struct in_addr netmask)
|
||||
union mysockaddr *addr, struct in_addr netmask, int dad)
|
||||
{
|
||||
struct irec *iface;
|
||||
int fd, mtu = 0, loopback;
|
||||
@@ -202,8 +202,11 @@ static int iface_allowed(struct irec **irecp, int if_index,
|
||||
we call this routine multiple times. */
|
||||
for (iface = *irecp; iface; iface = iface->next)
|
||||
if (sockaddr_isequal(&iface->addr, addr))
|
||||
return 1;
|
||||
|
||||
{
|
||||
iface->dad = dad;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1 ||
|
||||
!indextoname(fd, if_index, ifr.ifr_name) ||
|
||||
ioctl(fd, SIOCGIFFLAGS, &ifr) == -1)
|
||||
@@ -283,6 +286,8 @@ static int iface_allowed(struct irec **irecp, int if_index,
|
||||
iface->netmask = netmask;
|
||||
iface->tftp_ok = tftp_ok;
|
||||
iface->mtu = mtu;
|
||||
iface->dad = dad;
|
||||
iface->done = 0;
|
||||
if ((iface->name = whine_malloc(strlen(ifr.ifr_name)+1)))
|
||||
strcpy(iface->name, ifr.ifr_name);
|
||||
iface->next = *irecp;
|
||||
@@ -296,7 +301,7 @@ static int iface_allowed(struct irec **irecp, int if_index,
|
||||
|
||||
#ifdef HAVE_IPV6
|
||||
static int iface_allowed_v6(struct in6_addr *local,
|
||||
int scope, int if_index, void *vparam)
|
||||
int scope, int if_index, int dad, void *vparam)
|
||||
{
|
||||
union mysockaddr addr;
|
||||
struct in_addr netmask; /* dummy */
|
||||
@@ -312,7 +317,7 @@ static int iface_allowed_v6(struct in6_addr *local,
|
||||
addr.in6.sin6_port = htons(daemon->port);
|
||||
addr.in6.sin6_scope_id = scope;
|
||||
|
||||
return iface_allowed((struct irec **)vparam, if_index, &addr, netmask);
|
||||
return iface_allowed((struct irec **)vparam, if_index, &addr, netmask, dad);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -330,7 +335,7 @@ static int iface_allowed_v4(struct in_addr local, int if_index,
|
||||
addr.in.sin_addr = local;
|
||||
addr.in.sin_port = htons(daemon->port);
|
||||
|
||||
return iface_allowed((struct irec **)vparam, if_index, &addr, netmask);
|
||||
return iface_allowed((struct irec **)vparam, if_index, &addr, netmask, 0);
|
||||
}
|
||||
|
||||
int enumerate_interfaces(void)
|
||||
@@ -355,13 +360,10 @@ int fix_fd(int fd)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int make_sock(union mysockaddr *addr, int type)
|
||||
static int make_sock(union mysockaddr *addr, int type, int dienow)
|
||||
{
|
||||
int family = addr->sa.sa_family;
|
||||
int fd, rc, opt = 1;
|
||||
#ifdef HAVE_IPV6
|
||||
static int dad_count = 0;
|
||||
#endif
|
||||
|
||||
if ((fd = socket(family, type, 0)) == -1)
|
||||
{
|
||||
@@ -374,11 +376,16 @@ static int make_sock(union mysockaddr *addr, int type)
|
||||
return -1;
|
||||
|
||||
err:
|
||||
port = prettyprint_addr(addr, daemon->namebuff);
|
||||
if (!option_bool(OPT_NOWILD))
|
||||
sprintf(daemon->namebuff, "port %d", port);
|
||||
die(_("failed to create listening socket for %s: %s"),
|
||||
daemon->namebuff, EC_BADNET);
|
||||
if (dienow)
|
||||
{
|
||||
port = prettyprint_addr(addr, daemon->namebuff);
|
||||
if (!option_bool(OPT_NOWILD))
|
||||
sprintf(daemon->namebuff, "port %d", port);
|
||||
die(_("failed to create listening socket for %s: %s"),
|
||||
daemon->namebuff, EC_BADNET);
|
||||
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1 || !fix_fd(fd))
|
||||
@@ -389,27 +396,7 @@ static int make_sock(union mysockaddr *addr, int type)
|
||||
goto err;
|
||||
#endif
|
||||
|
||||
while (1)
|
||||
{
|
||||
if ((rc = bind(fd, (struct sockaddr *)addr, sa_len(addr))) != -1)
|
||||
break;
|
||||
|
||||
#ifdef HAVE_IPV6
|
||||
/* An interface may have an IPv6 address which is still undergoing DAD.
|
||||
If so, the bind will fail until the DAD completes, so we try over 20 seconds
|
||||
before failing. */
|
||||
if (family == AF_INET6 &&
|
||||
(errno == ENODEV || errno == EADDRNOTAVAIL) &&
|
||||
dad_count++ < DAD_WAIT)
|
||||
{
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
if (rc == -1)
|
||||
if ((rc = bind(fd, (struct sockaddr *)addr, sa_len(addr))) == -1)
|
||||
goto err;
|
||||
|
||||
if (type == SOCK_STREAM)
|
||||
@@ -462,15 +449,15 @@ static int make_sock(union mysockaddr *addr, int type)
|
||||
return fd;
|
||||
}
|
||||
|
||||
static struct listener *create_listeners(union mysockaddr *addr, int do_tftp)
|
||||
static struct listener *create_listeners(union mysockaddr *addr, int do_tftp, int dienow)
|
||||
{
|
||||
struct listener *l = NULL;
|
||||
int fd = -1, tcpfd = -1, tftpfd = -1;
|
||||
|
||||
if (daemon->port != 0)
|
||||
{
|
||||
fd = make_sock(addr, SOCK_DGRAM);
|
||||
tcpfd = make_sock(addr, SOCK_STREAM);
|
||||
fd = make_sock(addr, SOCK_DGRAM, dienow);
|
||||
tcpfd = make_sock(addr, SOCK_STREAM, dienow);
|
||||
}
|
||||
|
||||
#ifdef HAVE_TFTP
|
||||
@@ -481,7 +468,7 @@ static struct listener *create_listeners(union mysockaddr *addr, int do_tftp)
|
||||
/* port must be restored to DNS port for TCP code */
|
||||
short save = addr->in.sin_port;
|
||||
addr->in.sin_port = htons(TFTP_PORT);
|
||||
tftpfd = make_sock(addr, SOCK_DGRAM);
|
||||
tftpfd = make_sock(addr, SOCK_DGRAM, dienow);
|
||||
addr->in.sin_port = save;
|
||||
}
|
||||
# ifdef HAVE_IPV6
|
||||
@@ -489,7 +476,7 @@ static struct listener *create_listeners(union mysockaddr *addr, int do_tftp)
|
||||
{
|
||||
short save = addr->in6.sin6_port;
|
||||
addr->in6.sin6_port = htons(TFTP_PORT);
|
||||
tftpfd = make_sock(addr, SOCK_DGRAM);
|
||||
tftpfd = make_sock(addr, SOCK_DGRAM, dienow);
|
||||
addr->in6.sin6_port = save;
|
||||
}
|
||||
# endif
|
||||
@@ -509,7 +496,7 @@ static struct listener *create_listeners(union mysockaddr *addr, int do_tftp)
|
||||
return l;
|
||||
}
|
||||
|
||||
struct listener *create_wildcard_listeners(void)
|
||||
void create_wildcard_listeners(void)
|
||||
{
|
||||
union mysockaddr addr;
|
||||
struct listener *l;
|
||||
@@ -523,7 +510,7 @@ struct listener *create_wildcard_listeners(void)
|
||||
addr.in.sin_addr.s_addr = INADDR_ANY;
|
||||
addr.in.sin_port = htons(daemon->port);
|
||||
|
||||
l = create_listeners(&addr, tftp_enabled);
|
||||
l = create_listeners(&addr, tftp_enabled, 1);
|
||||
|
||||
#ifdef HAVE_IPV6
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
@@ -535,31 +522,41 @@ struct listener *create_wildcard_listeners(void)
|
||||
addr.in6.sin6_port = htons(daemon->port);
|
||||
|
||||
if (l)
|
||||
l->next = create_listeners(&addr, tftp_enabled);
|
||||
l->next = create_listeners(&addr, tftp_enabled, 1);
|
||||
else
|
||||
l = create_listeners(&addr, tftp_enabled);
|
||||
l = create_listeners(&addr, tftp_enabled, 1);
|
||||
#endif
|
||||
|
||||
return l;
|
||||
daemon->listeners = l;
|
||||
}
|
||||
|
||||
struct listener *create_bound_listeners(void)
|
||||
void create_bound_listeners(int dienow)
|
||||
{
|
||||
struct listener *new, *listeners = NULL;
|
||||
struct listener *new;
|
||||
struct irec *iface;
|
||||
|
||||
for (iface = daemon->interfaces; iface; iface = iface->next)
|
||||
if ((new = create_listeners(&iface->addr, iface->tftp_ok)))
|
||||
if (!iface->done && !iface->dad &&
|
||||
(new = create_listeners(&iface->addr, iface->tftp_ok, dienow)))
|
||||
{
|
||||
new->iface = iface;
|
||||
new->next = listeners;
|
||||
listeners = new;
|
||||
new->next = daemon->listeners;
|
||||
daemon->listeners = new;
|
||||
iface->done = 1;
|
||||
}
|
||||
|
||||
return listeners;
|
||||
}
|
||||
|
||||
|
||||
int is_dad_listeners(void)
|
||||
{
|
||||
struct irec *iface;
|
||||
|
||||
if (option_bool(OPT_NOWILD))
|
||||
for (iface = daemon->interfaces; iface; iface = iface->next)
|
||||
if (iface->dad && !iface->done)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
/* return a UDP socket bound to a random port, have to cope with straying into
|
||||
occupied port nos and reserved ones. */
|
||||
int random_sock(int family)
|
||||
|
||||
Reference in New Issue
Block a user