"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/util.c" between
dnsmasq-2.80.tar.gz and dnsmasq-2.81.tar.xz

About: Dnsmasq is a lightweight caching DNS forwarder and DHCP server.

util.c  (dnsmasq-2.80):util.c  (dnsmasq-2.81.tar.xz)
/* dnsmasq is Copyright (c) 2000-2018 Simon Kelley /* dnsmasq is Copyright (c) 2000-2020 Simon Kelley
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991, or the Free Software Foundation; version 2 dated June, 1991, or
(at your option) version 3 dated 29 June, 2007. (at your option) version 3 dated 29 June, 2007.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
skipping to change at line 32 skipping to change at line 32
#ifdef HAVE_BROKEN_RTC #ifdef HAVE_BROKEN_RTC
#include <sys/times.h> #include <sys/times.h>
#endif #endif
#if defined(HAVE_LIBIDN2) #if defined(HAVE_LIBIDN2)
#include <idn2.h> #include <idn2.h>
#elif defined(HAVE_IDN) #elif defined(HAVE_IDN)
#include <idna.h> #include <idna.h>
#endif #endif
#ifdef HAVE_LINUX_NETWORK
#include <sys/utsname.h>
#endif
/* SURF random number generator */ /* SURF random number generator */
static u32 seed[32]; static u32 seed[32];
static u32 in[12]; static u32 in[12];
static u32 out[8]; static u32 out[8];
static int outleft = 0; static int outleft = 0;
void rand_init() void rand_init()
{ {
int fd = open(RANDFILE, O_RDONLY); int fd = open(RANDFILE, O_RDONLY);
skipping to change at line 322 skipping to change at line 326
} }
int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2) int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2)
{ {
if (s1->sa.sa_family == s2->sa.sa_family) if (s1->sa.sa_family == s2->sa.sa_family)
{ {
if (s1->sa.sa_family == AF_INET && if (s1->sa.sa_family == AF_INET &&
s1->in.sin_port == s2->in.sin_port && s1->in.sin_port == s2->in.sin_port &&
s1->in.sin_addr.s_addr == s2->in.sin_addr.s_addr) s1->in.sin_addr.s_addr == s2->in.sin_addr.s_addr)
return 1; return 1;
#ifdef HAVE_IPV6
if (s1->sa.sa_family == AF_INET6 && if (s1->sa.sa_family == AF_INET6 &&
s1->in6.sin6_port == s2->in6.sin6_port && s1->in6.sin6_port == s2->in6.sin6_port &&
s1->in6.sin6_scope_id == s2->in6.sin6_scope_id && s1->in6.sin6_scope_id == s2->in6.sin6_scope_id &&
IN6_ARE_ADDR_EQUAL(&s1->in6.sin6_addr, &s2->in6.sin6_addr)) IN6_ARE_ADDR_EQUAL(&s1->in6.sin6_addr, &s2->in6.sin6_addr))
return 1; return 1;
#endif
} }
return 0; return 0;
} }
int sa_len(union mysockaddr *addr) int sa_len(union mysockaddr *addr)
{ {
#ifdef HAVE_SOCKADDR_SA_LEN #ifdef HAVE_SOCKADDR_SA_LEN
return addr->sa.sa_len; return addr->sa.sa_len;
#else #else
#ifdef HAVE_IPV6
if (addr->sa.sa_family == AF_INET6) if (addr->sa.sa_family == AF_INET6)
return sizeof(addr->in6); return sizeof(addr->in6);
else else
#endif
return sizeof(addr->in); return sizeof(addr->in);
#endif #endif
} }
/* don't use strcasecmp and friends here - they may be messed up by LOCALE */ /* don't use strcasecmp and friends here - they may be messed up by LOCALE */
int hostname_isequal(const char *a, const char *b) int hostname_isequal(const char *a, const char *b)
{ {
unsigned int c1, c2; unsigned int c1, c2;
do { do {
skipping to change at line 438 skipping to change at line 439
} }
return 32 - zero_count; return 32 - zero_count;
} }
int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask) int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask)
{ {
return (a.s_addr & mask.s_addr) == (b.s_addr & mask.s_addr); return (a.s_addr & mask.s_addr) == (b.s_addr & mask.s_addr);
} }
#ifdef HAVE_IPV6
int is_same_net6(struct in6_addr *a, struct in6_addr *b, int prefixlen) int is_same_net6(struct in6_addr *a, struct in6_addr *b, int prefixlen)
{ {
int pfbytes = prefixlen >> 3; int pfbytes = prefixlen >> 3;
int pfbits = prefixlen & 7; int pfbits = prefixlen & 7;
if (memcmp(&a->s6_addr, &b->s6_addr, pfbytes) != 0) if (memcmp(&a->s6_addr, &b->s6_addr, pfbytes) != 0)
return 0; return 0;
if (pfbits == 0 || if (pfbits == 0 ||
(a->s6_addr[pfbytes] >> (8 - pfbits) == b->s6_addr[pfbytes] >> (8 - pfbits ))) (a->s6_addr[pfbytes] >> (8 - pfbits) == b->s6_addr[pfbytes] >> (8 - pfbits )))
skipping to change at line 477 skipping to change at line 477
{ {
int i; int i;
for (i = 15; i >= 8; i--) for (i = 15; i >= 8; i--)
{ {
addr->s6_addr[i] = host; addr->s6_addr[i] = host;
host = host >> 8; host = host >> 8;
} }
} }
#endif
/* returns port number from address */ /* returns port number from address */
int prettyprint_addr(union mysockaddr *addr, char *buf) int prettyprint_addr(union mysockaddr *addr, char *buf)
{ {
int port = 0; int port = 0;
#ifdef HAVE_IPV6
if (addr->sa.sa_family == AF_INET) if (addr->sa.sa_family == AF_INET)
{ {
inet_ntop(AF_INET, &addr->in.sin_addr, buf, ADDRSTRLEN); inet_ntop(AF_INET, &addr->in.sin_addr, buf, ADDRSTRLEN);
port = ntohs(addr->in.sin_port); port = ntohs(addr->in.sin_port);
} }
else if (addr->sa.sa_family == AF_INET6) else if (addr->sa.sa_family == AF_INET6)
{ {
char name[IF_NAMESIZE]; char name[IF_NAMESIZE];
inet_ntop(AF_INET6, &addr->in6.sin6_addr, buf, ADDRSTRLEN); inet_ntop(AF_INET6, &addr->in6.sin6_addr, buf, ADDRSTRLEN);
if (addr->in6.sin6_scope_id != 0 && if (addr->in6.sin6_scope_id != 0 &&
if_indextoname(addr->in6.sin6_scope_id, name) && if_indextoname(addr->in6.sin6_scope_id, name) &&
strlen(buf) + strlen(name) + 2 <= ADDRSTRLEN) strlen(buf) + strlen(name) + 2 <= ADDRSTRLEN)
{ {
strcat(buf, "%"); strcat(buf, "%");
strcat(buf, name); strcat(buf, name);
} }
port = ntohs(addr->in6.sin6_port); port = ntohs(addr->in6.sin6_port);
} }
#else
strcpy(buf, inet_ntoa(addr->in.sin_addr));
port = ntohs(addr->in.sin_port);
#endif
return port; return port;
} }
void prettyprint_time(char *buf, unsigned int t) void prettyprint_time(char *buf, unsigned int t)
{ {
if (t == 0xffffffff) if (t == 0xffffffff)
sprintf(buf, _("infinite")); sprintf(buf, _("infinite"));
else else
{ {
skipping to change at line 534 skipping to change at line 527
if ((x = t%60)) if ((x = t%60))
p += sprintf(&buf[p], "%us", x); p += sprintf(&buf[p], "%us", x);
} }
} }
/* in may equal out, when maxlen may be -1 (No max len). /* in may equal out, when maxlen may be -1 (No max len).
Return -1 for extraneous no-hex chars found. */ Return -1 for extraneous no-hex chars found. */
int parse_hex(char *in, unsigned char *out, int maxlen, int parse_hex(char *in, unsigned char *out, int maxlen,
unsigned int *wildcard_mask, int *mac_type) unsigned int *wildcard_mask, int *mac_type)
{ {
int mask = 0, i = 0; int done = 0, mask = 0, i = 0;
char *r; char *r;
if (mac_type) if (mac_type)
*mac_type = 0; *mac_type = 0;
while (maxlen == -1 || i < maxlen) while (!done && (maxlen == -1 || i < maxlen))
{ {
for (r = in; *r != 0 && *r != ':' && *r != '-' && *r != ' '; r++) for (r = in; *r != 0 && *r != ':' && *r != '-' && *r != ' '; r++)
if (*r != '*' && !isxdigit((unsigned char)*r)) if (*r != '*' && !isxdigit((unsigned char)*r))
return -1; return -1;
if (*r == 0) if (*r == 0)
maxlen = i; done = 1;
if (r != in ) if (r != in )
{ {
if (*r == '-' && i == 0 && mac_type) if (*r == '-' && i == 0 && mac_type)
{ {
*r = 0; *r = 0;
*mac_type = strtol(in, NULL, 16); *mac_type = strtol(in, NULL, 16);
mac_type = NULL; mac_type = NULL;
} }
else else
skipping to change at line 715 skipping to change at line 708
} while (retry_send(n) || errno == ENOMEM || errno == ENOBUFS); } while (retry_send(n) || errno == ENOMEM || errno == ENOBUFS);
if (errno != 0) if (errno != 0)
return 0; return 0;
} }
return 1; return 1;
} }
/* close all fds except STDIN, STDOUT and STDERR, spare1, spare2 and spare3 */
void close_fds(long max_fd, int spare1, int spare2, int spare3)
{
/* On Linux, use the /proc/ filesystem to find which files
are actually open, rather than iterate over the whole space,
for efficiency reasons. If this fails we drop back to the dumb code. */
#ifdef HAVE_LINUX_NETWORK
DIR *d;
if ((d = opendir("/proc/self/fd")))
{
struct dirent *de;
while ((de = readdir(d)))
{
long fd;
char *e = NULL;
errno = 0;
fd = strtol(de->d_name, &e, 10);
if (errno != 0 || !e || *e || fd == dirfd(d) ||
fd == STDOUT_FILENO || fd == STDERR_FILENO || fd == STDIN_FILENO ||
fd == spare1 || fd == spare2 || fd == spare3)
continue;
close(fd);
}
closedir(d);
return;
}
#endif
/* fallback, dumb code. */
for (max_fd--; max_fd >= 0; max_fd--)
if (max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO && max_fd != STDIN_FI
LENO &&
max_fd != spare1 && max_fd != spare2 && max_fd != spare3)
close(max_fd);
}
/* Basically match a string value against a wildcard pattern. */ /* Basically match a string value against a wildcard pattern. */
int wildcard_match(const char* wildcard, const char* match) int wildcard_match(const char* wildcard, const char* match)
{ {
while (*wildcard && *match) while (*wildcard && *match)
{ {
if (*wildcard == '*') if (*wildcard == '*')
return 1; return 1;
if (*wildcard != *match) if (*wildcard != *match)
return 0; return 0;
skipping to change at line 751 skipping to change at line 785
if (*wildcard != *match) if (*wildcard != *match)
return 0; return 0;
++wildcard; ++wildcard;
++match; ++match;
--num; --num;
} }
return (!num) || (*wildcard == *match); return (!num) || (*wildcard == *match);
} }
#ifdef HAVE_LINUX_NETWORK
int kernel_version(void)
{
struct utsname utsname;
int version;
char *split;
if (uname(&utsname) < 0)
die(_("failed to find kernel version: %s"), NULL, EC_MISC);
split = strtok(utsname.release, ".");
version = (split ? atoi(split) : 0);
split = strtok(NULL, ".");
version = version * 256 + (split ? atoi(split) : 0);
split = strtok(NULL, ".");
return version * 256 + (split ? atoi(split) : 0);
}
#endif
 End of changes. 15 change blocks. 
16 lines changed or deleted 51 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)