blob: 9299703c6d30aac851980b245dc95a8394323598 [file] [log] [blame]
Simon Kelleyaff33962015-01-31 20:13:40 +00001/* dnsmasq is Copyright (c) 2000-2015 Simon Kelley
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002
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
Simon Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley9e4abcb2004-01-22 19:47:41 +00008 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.
Simon Kelley73a08a22009-02-05 20:28:08 +000012
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/>.
Simon Kelley9e4abcb2004-01-22 19:47:41 +000015*/
16
Simon Kelley1a6bca82008-07-11 11:11:42 +010017/* The SURF random number generator was taken from djbdns-1.05, by
Simon Kelley7622fc02009-06-04 20:32:05 +010018 Daniel J Bernstein, which is public domain. */
Simon Kelley1a6bca82008-07-11 11:11:42 +010019
20
Simon Kelley9e4abcb2004-01-22 19:47:41 +000021#include "dnsmasq.h"
22
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010023#ifdef HAVE_BROKEN_RTC
24#include <sys/times.h>
25#endif
26
Simon Kelley572b41e2011-02-18 18:11:18 +000027#if defined(LOCALEDIR) || defined(HAVE_IDN)
Simon Kelley1f15b812009-10-13 17:49:32 +010028#include <idna.h>
29#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +010030
Simon Kelley1a6bca82008-07-11 11:11:42 +010031/* SURF random number generator */
32
Simon Kelley572b41e2011-02-18 18:11:18 +000033static u32 seed[32];
34static u32 in[12];
35static u32 out[8];
Simon Kelley6586e832013-11-07 14:20:13 +000036static int outleft = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +010037
38void rand_init()
39{
40 int fd = open(RANDFILE, O_RDONLY);
41
42 if (fd == -1 ||
43 !read_write(fd, (unsigned char *)&seed, sizeof(seed), 1) ||
44 !read_write(fd, (unsigned char *)&in, sizeof(in), 1))
45 die(_("failed to seed the random number generator: %s"), NULL, EC_MISC);
46
47 close(fd);
48}
49
50#define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
51#define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
52
53static void surf(void)
54{
Simon Kelley572b41e2011-02-18 18:11:18 +000055 u32 t[12]; u32 x; u32 sum = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +010056 int r; int i; int loop;
57
58 for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
59 for (i = 0;i < 8;++i) out[i] = seed[24 + i];
60 x = t[11];
61 for (loop = 0;loop < 2;++loop) {
62 for (r = 0;r < 16;++r) {
63 sum += 0x9e3779b9;
64 MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
65 MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
66 MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
67 }
68 for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
69 }
70}
71
72unsigned short rand16(void)
73{
Simon Kelley6586e832013-11-07 14:20:13 +000074 if (!outleft)
75 {
76 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
77 surf();
78 outleft = 8;
79 }
80
81 return (unsigned short) out[--outleft];
82}
83
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +010084u32 rand32(void)
85{
86 if (!outleft)
87 {
88 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
89 surf();
90 outleft = 8;
91 }
92
93 return out[--outleft];
94}
95
Simon Kelley6586e832013-11-07 14:20:13 +000096u64 rand64(void)
97{
Simon Kelley1a6bca82008-07-11 11:11:42 +010098 static int outleft = 0;
99
Simon Kelley6586e832013-11-07 14:20:13 +0000100 if (outleft < 2)
101 {
102 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
103 surf();
104 outleft = 8;
105 }
106
107 outleft -= 2;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100108
Simon Kelley6586e832013-11-07 14:20:13 +0000109 return (u64)out[outleft+1] + (((u64)out[outleft]) << 32);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100110}
111
Simon Kelley1f15b812009-10-13 17:49:32 +0100112static int check_name(char *in)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000113{
Simon Kelley1f15b812009-10-13 17:49:32 +0100114 /* remove trailing .
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000115 also fail empty string and label > 63 chars */
Simon Kelley1f15b812009-10-13 17:49:32 +0100116 size_t dotgap = 0, l = strlen(in);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000117 char c;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100118 int nowhite = 0;
Simon Kelley1f15b812009-10-13 17:49:32 +0100119
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000120 if (l == 0 || l > MAXDNAME) return 0;
Simon Kelley1f15b812009-10-13 17:49:32 +0100121
122 if (in[l-1] == '.')
Simon Kelleya2226412004-05-13 20:27:08 +0100123 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100124 in[l-1] = 0;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000125 nowhite = 1;
Simon Kelleya2226412004-05-13 20:27:08 +0100126 }
Simon Kelley0fc2f312014-01-08 10:26:58 +0000127
Simon Kelley1f15b812009-10-13 17:49:32 +0100128 for (; (c = *in); in++)
Simon Kelley3d8df262005-08-29 12:19:27 +0100129 {
130 if (c == '.')
131 dotgap = 0;
Simon Kelley1f15b812009-10-13 17:49:32 +0100132 else if (++dotgap > MAXLABEL)
Simon Kelley3d8df262005-08-29 12:19:27 +0100133 return 0;
Simon Kelley572b41e2011-02-18 18:11:18 +0000134 else if (isascii((unsigned char)c) && iscntrl((unsigned char)c))
Simon Kelley1f15b812009-10-13 17:49:32 +0100135 /* iscntrl only gives expected results for ascii */
136 return 0;
Simon Kelley572b41e2011-02-18 18:11:18 +0000137#if !defined(LOCALEDIR) && !defined(HAVE_IDN)
138 else if (!isascii((unsigned char)c))
Simon Kelley1f15b812009-10-13 17:49:32 +0100139 return 0;
140#endif
Simon Kelley5aabfc72007-08-29 11:24:47 +0100141 else if (c != ' ')
142 nowhite = 1;
Simon Kelley3d8df262005-08-29 12:19:27 +0100143 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100144
145 if (!nowhite)
146 return 0;
147
148 return 1;
149}
150
151/* Hostnames have a more limited valid charset than domain names
152 so check for legal char a-z A-Z 0-9 - _
153 Note that this may receive a FQDN, so only check the first label
154 for the tighter criteria. */
155int legal_hostname(char *name)
156{
157 char c;
Simon Kelley7abb69b2013-04-29 10:52:16 +0100158 int first;
Simon Kelley1f15b812009-10-13 17:49:32 +0100159
160 if (!check_name(name))
161 return 0;
162
Simon Kelley7abb69b2013-04-29 10:52:16 +0100163 for (first = 1; (c = *name); name++, first = 0)
Simon Kelley1f15b812009-10-13 17:49:32 +0100164 /* check for legal char a-z A-Z 0-9 - _ . */
165 {
166 if ((c >= 'A' && c <= 'Z') ||
Kyle Mesteryd859ca22013-07-24 13:11:58 +0100167 (c >= 'a' && c <= 'z') ||
168 (c >= '0' && c <= '9'))
Simon Kelley1f15b812009-10-13 17:49:32 +0100169 continue;
Simon Kelley7abb69b2013-04-29 10:52:16 +0100170
Kyle Mesteryd859ca22013-07-24 13:11:58 +0100171 if (!first && (c == '-' || c == '_'))
Simon Kelley7abb69b2013-04-29 10:52:16 +0100172 continue;
Kyle Mesteryd859ca22013-07-24 13:11:58 +0100173
Simon Kelley1f15b812009-10-13 17:49:32 +0100174 /* end of hostname part */
175 if (c == '.')
176 return 1;
177
178 return 0;
179 }
180
181 return 1;
182}
183
184char *canonicalise(char *in, int *nomem)
185{
186 char *ret = NULL;
Simon Kelley572b41e2011-02-18 18:11:18 +0000187#if defined(LOCALEDIR) || defined(HAVE_IDN)
Simon Kelley1f15b812009-10-13 17:49:32 +0100188 int rc;
189#endif
190
191 if (nomem)
192 *nomem = 0;
193
194 if (!check_name(in))
195 return NULL;
196
Simon Kelley572b41e2011-02-18 18:11:18 +0000197#if defined(LOCALEDIR) || defined(HAVE_IDN)
Simon Kelley1f15b812009-10-13 17:49:32 +0100198 if ((rc = idna_to_ascii_lz(in, &ret, 0)) != IDNA_SUCCESS)
199 {
200 if (ret)
201 free(ret);
202
203 if (nomem && (rc == IDNA_MALLOC_ERROR || rc == IDNA_DLOPEN_ERROR))
204 {
205 my_syslog(LOG_ERR, _("failed to allocate memory"));
206 *nomem = 1;
207 }
208
209 return NULL;
210 }
211#else
212 if ((ret = whine_malloc(strlen(in)+1)))
213 strcpy(ret, in);
214 else if (nomem)
215 *nomem = 1;
216#endif
217
218 return ret;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000219}
220
Simon Kelley3d8df262005-08-29 12:19:27 +0100221unsigned char *do_rfc1035_name(unsigned char *p, char *sval)
222{
223 int j;
224
225 while (sval && *sval)
226 {
227 unsigned char *cp = p++;
228 for (j = 0; *sval && (*sval != '.'); sval++, j++)
Simon Kelleycbe379a2015-04-21 22:57:06 +0100229 {
230#ifdef HAVE_DNSSEC
231 if (option_bool(OPT_DNSSEC_VALID) && *sval == NAME_ESCAPE)
Simon Kelleyb8f16552015-04-22 21:14:31 +0100232 *p++ = (*(++sval))-1;
Simon Kelleycbe379a2015-04-21 22:57:06 +0100233 else
234#endif
235 *p++ = *sval;
236 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100237 *cp = j;
238 if (*sval)
239 sval++;
240 }
241 return p;
242}
243
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000244/* for use during startup */
Simon Kelley0a852542005-03-23 20:28:59 +0000245void *safe_malloc(size_t size)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000246{
247 void *ret = malloc(size);
248
249 if (!ret)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100250 die(_("could not get memory"), NULL, EC_NOMEM);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000251
252 return ret;
Simon Kelley3d8df262005-08-29 12:19:27 +0100253}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000254
Simon Kelley1a6bca82008-07-11 11:11:42 +0100255void safe_pipe(int *fd, int read_noblock)
256{
257 if (pipe(fd) == -1 ||
258 !fix_fd(fd[1]) ||
259 (read_noblock && !fix_fd(fd[0])))
260 die(_("cannot create pipe: %s"), NULL, EC_MISC);
261}
262
Simon Kelley5aabfc72007-08-29 11:24:47 +0100263void *whine_malloc(size_t size)
264{
265 void *ret = malloc(size);
266
267 if (!ret)
268 my_syslog(LOG_ERR, _("failed to allocate %d bytes"), (int) size);
269
270 return ret;
271}
272
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000273int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2)
274{
275 if (s1->sa.sa_family == s2->sa.sa_family)
276 {
277 if (s1->sa.sa_family == AF_INET &&
278 s1->in.sin_port == s2->in.sin_port &&
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100279 s1->in.sin_addr.s_addr == s2->in.sin_addr.s_addr)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000280 return 1;
281#ifdef HAVE_IPV6
282 if (s1->sa.sa_family == AF_INET6 &&
283 s1->in6.sin6_port == s2->in6.sin6_port &&
Simon Kelley39341552015-01-18 22:11:10 +0000284 s1->in6.sin6_scope_id == s2->in6.sin6_scope_id &&
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100285 IN6_ARE_ADDR_EQUAL(&s1->in6.sin6_addr, &s2->in6.sin6_addr))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000286 return 1;
287#endif
288 }
289 return 0;
290}
291
292int sa_len(union mysockaddr *addr)
293{
294#ifdef HAVE_SOCKADDR_SA_LEN
295 return addr->sa.sa_len;
296#else
297#ifdef HAVE_IPV6
298 if (addr->sa.sa_family == AF_INET6)
299 return sizeof(addr->in6);
300 else
301#endif
302 return sizeof(addr->in);
303#endif
304}
305
306/* don't use strcasecmp and friends here - they may be messed up by LOCALE */
Simon Kelleyc99df932012-10-12 13:39:04 +0100307int hostname_isequal(const char *a, const char *b)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000308{
309 unsigned int c1, c2;
310
311 do {
Simon Kelley3d8df262005-08-29 12:19:27 +0100312 c1 = (unsigned char) *a++;
313 c2 = (unsigned char) *b++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000314
315 if (c1 >= 'A' && c1 <= 'Z')
316 c1 += 'a' - 'A';
317 if (c2 >= 'A' && c2 <= 'Z')
318 c2 += 'a' - 'A';
319
320 if (c1 != c2)
321 return 0;
322 } while (c1);
323
324 return 1;
325}
326
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100327time_t dnsmasq_time(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000328{
329#ifdef HAVE_BROKEN_RTC
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100330 struct tms dummy;
331 static long tps = 0;
332
333 if (tps == 0)
334 tps = sysconf(_SC_CLK_TCK);
335
336 return (time_t)(times(&dummy)/tps);
Simon Kelley44a2a312004-03-10 20:04:35 +0000337#else
Simon Kelley44a2a312004-03-10 20:04:35 +0000338 return time(NULL);
339#endif
340}
Simon Kelleya84fa1d2004-04-23 22:21:21 +0100341
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800342int netmask_length(struct in_addr mask)
343{
344 int zero_count = 0;
345
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100346 while (0x0 == (mask.s_addr & 0x1) && zero_count < 32)
347 {
348 mask.s_addr >>= 1;
349 zero_count++;
350 }
351
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800352 return 32 - zero_count;
353}
354
Simon Kelleya84fa1d2004-04-23 22:21:21 +0100355int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask)
356{
357 return (a.s_addr & mask.s_addr) == (b.s_addr & mask.s_addr);
358}
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100359
Simon Kelleyc72daea2012-01-05 21:33:27 +0000360#ifdef HAVE_IPV6
361int is_same_net6(struct in6_addr *a, struct in6_addr *b, int prefixlen)
362{
363 int pfbytes = prefixlen >> 3;
364 int pfbits = prefixlen & 7;
365
366 if (memcmp(&a->s6_addr, &b->s6_addr, pfbytes) != 0)
367 return 0;
368
369 if (pfbits == 0 ||
Simon Kelleyc64b7f62012-05-18 10:19:59 +0100370 (a->s6_addr[pfbytes] >> (8 - pfbits) == b->s6_addr[pfbytes] >> (8 - pfbits)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000371 return 1;
372
373 return 0;
374}
375
Simon Kelley52b92f42012-01-22 16:05:15 +0000376/* return least signigicant 64 bits if IPv6 address */
377u64 addr6part(struct in6_addr *addr)
378{
379 int i;
380 u64 ret = 0;
381
382 for (i = 8; i < 16; i++)
383 ret = (ret << 8) + addr->s6_addr[i];
384
385 return ret;
386}
387
388void setaddr6part(struct in6_addr *addr, u64 host)
389{
390 int i;
391
392 for (i = 15; i >= 8; i--)
393 {
394 addr->s6_addr[i] = host;
395 host = host >> 8;
396 }
397}
398
Simon Kelleyc72daea2012-01-05 21:33:27 +0000399#endif
400
401
Simon Kelley3d8df262005-08-29 12:19:27 +0100402/* returns port number from address */
403int prettyprint_addr(union mysockaddr *addr, char *buf)
404{
405 int port = 0;
406
407#ifdef HAVE_IPV6
408 if (addr->sa.sa_family == AF_INET)
409 {
410 inet_ntop(AF_INET, &addr->in.sin_addr, buf, ADDRSTRLEN);
411 port = ntohs(addr->in.sin_port);
412 }
413 else if (addr->sa.sa_family == AF_INET6)
414 {
Simon Kelley7de060b2011-08-26 17:24:52 +0100415 char name[IF_NAMESIZE];
Simon Kelley3d8df262005-08-29 12:19:27 +0100416 inet_ntop(AF_INET6, &addr->in6.sin6_addr, buf, ADDRSTRLEN);
Simon Kelley7de060b2011-08-26 17:24:52 +0100417 if (addr->in6.sin6_scope_id != 0 &&
418 if_indextoname(addr->in6.sin6_scope_id, name) &&
419 strlen(buf) + strlen(name) + 2 <= ADDRSTRLEN)
420 {
421 strcat(buf, "%");
422 strcat(buf, name);
423 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100424 port = ntohs(addr->in6.sin6_port);
425 }
426#else
427 strcpy(buf, inet_ntoa(addr->in.sin_addr));
428 port = ntohs(addr->in.sin_port);
429#endif
430
431 return port;
432}
433
Simon Kelley0a852542005-03-23 20:28:59 +0000434void prettyprint_time(char *buf, unsigned int t)
435{
436 if (t == 0xffffffff)
Simon Kelleyb8187c82005-11-26 21:46:27 +0000437 sprintf(buf, _("infinite"));
Simon Kelley0a852542005-03-23 20:28:59 +0000438 else
439 {
440 unsigned int x, p = 0;
Simon Kelley3d8df262005-08-29 12:19:27 +0100441 if ((x = t/86400))
442 p += sprintf(&buf[p], "%dd", x);
443 if ((x = (t/3600)%24))
Simon Kelley0a852542005-03-23 20:28:59 +0000444 p += sprintf(&buf[p], "%dh", x);
445 if ((x = (t/60)%60))
446 p += sprintf(&buf[p], "%dm", x);
447 if ((x = t%60))
448 p += sprintf(&buf[p], "%ds", x);
449 }
450}
451
452
Simon Kelley28866e92011-02-14 20:19:14 +0000453/* in may equal out, when maxlen may be -1 (No max len).
454 Return -1 for extraneous no-hex chars found. */
Simon Kelleycdeda282006-03-16 20:16:06 +0000455int parse_hex(char *in, unsigned char *out, int maxlen,
456 unsigned int *wildcard_mask, int *mac_type)
Simon Kelley0a852542005-03-23 20:28:59 +0000457{
458 int mask = 0, i = 0;
459 char *r;
Simon Kelleycdeda282006-03-16 20:16:06 +0000460
461 if (mac_type)
462 *mac_type = 0;
463
Simon Kelley0a852542005-03-23 20:28:59 +0000464 while (maxlen == -1 || i < maxlen)
465 {
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100466 for (r = in; *r != 0 && *r != ':' && *r != '-' && *r != ' '; r++)
Simon Kelley572b41e2011-02-18 18:11:18 +0000467 if (*r != '*' && !isxdigit((unsigned char)*r))
Simon Kelley28866e92011-02-14 20:19:14 +0000468 return -1;
469
Simon Kelley0a852542005-03-23 20:28:59 +0000470 if (*r == 0)
471 maxlen = i;
Simon Kelleycdeda282006-03-16 20:16:06 +0000472
Simon Kelley0a852542005-03-23 20:28:59 +0000473 if (r != in )
474 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000475 if (*r == '-' && i == 0 && mac_type)
476 {
477 *r = 0;
478 *mac_type = strtol(in, NULL, 16);
479 mac_type = NULL;
480 }
Simon Kelley0a852542005-03-23 20:28:59 +0000481 else
Simon Kelleycdeda282006-03-16 20:16:06 +0000482 {
483 *r = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +0000484 if (strcmp(in, "*") == 0)
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100485 {
486 mask = (mask << 1) | 1;
487 i++;
488 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000489 else
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100490 {
491 int j, bytes = (1 + (r - in))/2;
492 for (j = 0; j < bytes; j++)
493 {
Vladislav Grishenko50db3492013-11-26 11:09:31 +0000494 char sav = sav;
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100495 if (j < bytes - 1)
496 {
497 sav = in[(j+1)*2];
498 in[(j+1)*2] = 0;
499 }
500 out[i] = strtol(&in[j*2], NULL, 16);
501 mask = mask << 1;
502 i++;
503 if (j < bytes - 1)
504 in[(j+1)*2] = sav;
505 }
506 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000507 }
Simon Kelley0a852542005-03-23 20:28:59 +0000508 }
509 in = r+1;
510 }
511
512 if (wildcard_mask)
513 *wildcard_mask = mask;
514
515 return i;
516}
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100517
Simon Kelley7622fc02009-06-04 20:32:05 +0100518/* return 0 for no match, or (no matched octets) + 1 */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100519int memcmp_masked(unsigned char *a, unsigned char *b, int len, unsigned int mask)
520{
Simon Kelley7622fc02009-06-04 20:32:05 +0100521 int i, count;
522 for (count = 1, i = len - 1; i >= 0; i--, mask = mask >> 1)
523 if (!(mask & 1))
524 {
525 if (a[i] == b[i])
526 count++;
527 else
528 return 0;
529 }
530 return count;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100531}
532
533/* _note_ may copy buffer */
534int expand_buf(struct iovec *iov, size_t size)
535{
536 void *new;
537
Simon Kelley824af852008-02-12 20:43:05 +0000538 if (size <= (size_t)iov->iov_len)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100539 return 1;
540
Simon Kelley5aabfc72007-08-29 11:24:47 +0100541 if (!(new = whine_malloc(size)))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100542 {
543 errno = ENOMEM;
544 return 0;
545 }
546
547 if (iov->iov_base)
548 {
549 memcpy(new, iov->iov_base, iov->iov_len);
550 free(iov->iov_base);
551 }
552
553 iov->iov_base = new;
554 iov->iov_len = size;
555
556 return 1;
557}
Simon Kelley7cebd202006-05-06 14:13:33 +0100558
Simon Kelley5aabfc72007-08-29 11:24:47 +0100559char *print_mac(char *buff, unsigned char *mac, int len)
Simon Kelley7cebd202006-05-06 14:13:33 +0100560{
Simon Kelley5aabfc72007-08-29 11:24:47 +0100561 char *p = buff;
Simon Kelley7cebd202006-05-06 14:13:33 +0100562 int i;
563
564 if (len == 0)
565 sprintf(p, "<null>");
566 else
567 for (i = 0; i < len; i++)
568 p += sprintf(p, "%.2x%s", mac[i], (i == len - 1) ? "" : ":");
569
Simon Kelley5aabfc72007-08-29 11:24:47 +0100570 return buff;
Simon Kelley7cebd202006-05-06 14:13:33 +0100571}
Simon Kelley16972692006-10-16 20:04:18 +0100572
573void bump_maxfd(int fd, int *max)
574{
575 if (fd > *max)
576 *max = fd;
577}
578
Simon Kelleyff841eb2015-03-11 21:36:30 +0000579/* rc is return from sendto and friends.
580 Return 1 if we should retry.
581 Set errno to zero if we succeeded. */
582int retry_send(ssize_t rc)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100583{
Simon Kelleyff841eb2015-03-11 21:36:30 +0000584 static int retries = 0;
585 struct timespec waiter;
586
587 if (rc != -1)
588 {
589 retries = 0;
590 errno = 0;
591 return 0;
592 }
593
Simon Kelley57826492014-09-18 22:08:58 +0100594 /* Linux kernels can return EAGAIN in perpetuity when calling
595 sendmsg() and the relevant interface has gone. Here we loop
596 retrying in EAGAIN for 1 second max, to avoid this hanging
597 dnsmasq. */
598
Simon Kelleyff841eb2015-03-11 21:36:30 +0000599 if (errno == EAGAIN || errno == EWOULDBLOCK)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100600 {
601 waiter.tv_sec = 0;
602 waiter.tv_nsec = 10000;
603 nanosleep(&waiter, NULL);
Simon Kelley57826492014-09-18 22:08:58 +0100604 if (retries++ < 1000)
605 return 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100606 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000607
608 retries = 0;
609
610 if (errno == EINTR)
611 return 1;
612
613 return 0;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100614}
615
Simon Kelley16972692006-10-16 20:04:18 +0100616int read_write(int fd, unsigned char *packet, int size, int rw)
617{
618 ssize_t n, done;
619
620 for (done = 0; done < size; done += n)
621 {
Simon Kelleyff841eb2015-03-11 21:36:30 +0000622 do {
623 if (rw)
624 n = read(fd, &packet[done], (size_t)(size - done));
625 else
626 n = write(fd, &packet[done], (size_t)(size - done));
627
628 if (n == 0)
629 return 0;
630
631 } while (retry_send(n) || errno == ENOMEM || errno == ENOBUFS);
Simon Kelley16972692006-10-16 20:04:18 +0100632
Simon Kelleyff841eb2015-03-11 21:36:30 +0000633 if (errno != 0)
634 return 0;
Simon Kelley16972692006-10-16 20:04:18 +0100635 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000636
Simon Kelley16972692006-10-16 20:04:18 +0100637 return 1;
638}
Simon Kelley1a6bca82008-07-11 11:11:42 +0100639
Simon Kelley49333cb2013-03-15 20:30:51 +0000640/* Basically match a string value against a wildcard pattern. */
641int wildcard_match(const char* wildcard, const char* match)
642{
643 while (*wildcard && *match)
644 {
645 if (*wildcard == '*')
646 return 1;
647
648 if (*wildcard != *match)
649 return 0;
650
651 ++wildcard;
652 ++match;
653 }
654
655 return *wildcard == *match;
656}
Neil Jerram70772c92014-06-11 21:22:40 +0100657
658/* The same but comparing a maximum of NUM characters, like strncmp. */
659int wildcard_matchn(const char* wildcard, const char* match, int num)
660{
661 while (*wildcard && *match && num)
662 {
663 if (*wildcard == '*')
664 return 1;
665
666 if (*wildcard != *match)
667 return 0;
668
669 ++wildcard;
670 ++match;
671 --num;
672 }
673
674 return (!num) || (*wildcard == *match);
675}