blob: 2a1d44840cc44d0369fe3005c9756af989f53d7c [file] [log] [blame]
Simon Kelley50ca8552017-06-24 22:36:43 +01001/* dnsmasq is Copyright (c) 2000-2017 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 Kelley43cdf1c2017-05-21 22:12:44 +010027#if defined(HAVE_LIBIDN2)
Petr Menšíkd203af42017-05-10 21:41:57 +010028#include <idn2.h>
Simon Kelley43cdf1c2017-05-21 22:12:44 +010029#elif defined(HAVE_IDN)
Simon Kelley1f15b812009-10-13 17:49:32 +010030#include <idna.h>
31#endif
Simon Kelley1a6bca82008-07-11 11:11:42 +010032
Simon Kelley1a6bca82008-07-11 11:11:42 +010033/* SURF random number generator */
34
Simon Kelley572b41e2011-02-18 18:11:18 +000035static u32 seed[32];
36static u32 in[12];
37static u32 out[8];
Simon Kelley6586e832013-11-07 14:20:13 +000038static int outleft = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +010039
40void rand_init()
41{
42 int fd = open(RANDFILE, O_RDONLY);
43
44 if (fd == -1 ||
45 !read_write(fd, (unsigned char *)&seed, sizeof(seed), 1) ||
46 !read_write(fd, (unsigned char *)&in, sizeof(in), 1))
47 die(_("failed to seed the random number generator: %s"), NULL, EC_MISC);
48
49 close(fd);
50}
51
52#define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
53#define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
54
55static void surf(void)
56{
Simon Kelley572b41e2011-02-18 18:11:18 +000057 u32 t[12]; u32 x; u32 sum = 0;
Simon Kelley1a6bca82008-07-11 11:11:42 +010058 int r; int i; int loop;
59
60 for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
61 for (i = 0;i < 8;++i) out[i] = seed[24 + i];
62 x = t[11];
63 for (loop = 0;loop < 2;++loop) {
64 for (r = 0;r < 16;++r) {
65 sum += 0x9e3779b9;
66 MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
67 MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
68 MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
69 }
70 for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
71 }
72}
73
74unsigned short rand16(void)
75{
Simon Kelley6586e832013-11-07 14:20:13 +000076 if (!outleft)
77 {
78 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
79 surf();
80 outleft = 8;
81 }
82
83 return (unsigned short) out[--outleft];
84}
85
Simon Kelleyb5ea1cc2014-07-29 16:34:14 +010086u32 rand32(void)
87{
88 if (!outleft)
89 {
90 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
91 surf();
92 outleft = 8;
93 }
94
95 return out[--outleft];
96}
97
Simon Kelley6586e832013-11-07 14:20:13 +000098u64 rand64(void)
99{
Simon Kelley1a6bca82008-07-11 11:11:42 +0100100 static int outleft = 0;
101
Simon Kelley6586e832013-11-07 14:20:13 +0000102 if (outleft < 2)
103 {
104 if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
105 surf();
106 outleft = 8;
107 }
108
109 outleft -= 2;
Simon Kelley1a6bca82008-07-11 11:11:42 +0100110
Simon Kelley6586e832013-11-07 14:20:13 +0000111 return (u64)out[outleft+1] + (((u64)out[outleft]) << 32);
Simon Kelley1a6bca82008-07-11 11:11:42 +0100112}
113
Simon Kelley69a815a2017-07-08 21:20:16 +0100114/* returns 2 if names is OK but contains one or more underscores */
Simon Kelley1f15b812009-10-13 17:49:32 +0100115static int check_name(char *in)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000116{
Simon Kelley1f15b812009-10-13 17:49:32 +0100117 /* remove trailing .
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000118 also fail empty string and label > 63 chars */
Simon Kelley1f15b812009-10-13 17:49:32 +0100119 size_t dotgap = 0, l = strlen(in);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000120 char c;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100121 int nowhite = 0;
Simon Kelley69a815a2017-07-08 21:20:16 +0100122 int hasuscore = 0;
Simon Kelley1f15b812009-10-13 17:49:32 +0100123
Simon Kelleyf6b7dc42005-01-23 12:06:08 +0000124 if (l == 0 || l > MAXDNAME) return 0;
Simon Kelley1f15b812009-10-13 17:49:32 +0100125
126 if (in[l-1] == '.')
Simon Kelleya2226412004-05-13 20:27:08 +0100127 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100128 in[l-1] = 0;
Simon Kelley0fc2f312014-01-08 10:26:58 +0000129 nowhite = 1;
Simon Kelleya2226412004-05-13 20:27:08 +0100130 }
Simon Kelley0fc2f312014-01-08 10:26:58 +0000131
Simon Kelley1f15b812009-10-13 17:49:32 +0100132 for (; (c = *in); in++)
Simon Kelley3d8df262005-08-29 12:19:27 +0100133 {
134 if (c == '.')
135 dotgap = 0;
Simon Kelley1f15b812009-10-13 17:49:32 +0100136 else if (++dotgap > MAXLABEL)
Simon Kelley3d8df262005-08-29 12:19:27 +0100137 return 0;
Simon Kelley572b41e2011-02-18 18:11:18 +0000138 else if (isascii((unsigned char)c) && iscntrl((unsigned char)c))
Simon Kelley1f15b812009-10-13 17:49:32 +0100139 /* iscntrl only gives expected results for ascii */
140 return 0;
Simon Kelley43cdf1c2017-05-21 22:12:44 +0100141#if !defined(HAVE_IDN) && !defined(HAVE_LIBIDN2)
Simon Kelley572b41e2011-02-18 18:11:18 +0000142 else if (!isascii((unsigned char)c))
Simon Kelley1f15b812009-10-13 17:49:32 +0100143 return 0;
144#endif
Simon Kelley5aabfc72007-08-29 11:24:47 +0100145 else if (c != ' ')
Simon Kelley69a815a2017-07-08 21:20:16 +0100146 {
147 nowhite = 1;
148 if (c == '_')
149 hasuscore = 1;
150 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100151 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100152
153 if (!nowhite)
154 return 0;
155
Simon Kelley69a815a2017-07-08 21:20:16 +0100156 return hasuscore ? 2 : 1;
Simon Kelley1f15b812009-10-13 17:49:32 +0100157}
158
159/* Hostnames have a more limited valid charset than domain names
160 so check for legal char a-z A-Z 0-9 - _
161 Note that this may receive a FQDN, so only check the first label
162 for the tighter criteria. */
163int legal_hostname(char *name)
164{
165 char c;
Simon Kelley7abb69b2013-04-29 10:52:16 +0100166 int first;
Simon Kelley1f15b812009-10-13 17:49:32 +0100167
168 if (!check_name(name))
169 return 0;
170
Simon Kelley7abb69b2013-04-29 10:52:16 +0100171 for (first = 1; (c = *name); name++, first = 0)
Simon Kelley1f15b812009-10-13 17:49:32 +0100172 /* check for legal char a-z A-Z 0-9 - _ . */
173 {
174 if ((c >= 'A' && c <= 'Z') ||
Kyle Mesteryd859ca22013-07-24 13:11:58 +0100175 (c >= 'a' && c <= 'z') ||
176 (c >= '0' && c <= '9'))
Simon Kelley1f15b812009-10-13 17:49:32 +0100177 continue;
Simon Kelley7abb69b2013-04-29 10:52:16 +0100178
Kyle Mesteryd859ca22013-07-24 13:11:58 +0100179 if (!first && (c == '-' || c == '_'))
Simon Kelley7abb69b2013-04-29 10:52:16 +0100180 continue;
Kyle Mesteryd859ca22013-07-24 13:11:58 +0100181
Simon Kelley1f15b812009-10-13 17:49:32 +0100182 /* end of hostname part */
183 if (c == '.')
184 return 1;
185
186 return 0;
187 }
188
189 return 1;
190}
191
192char *canonicalise(char *in, int *nomem)
193{
194 char *ret = NULL;
Simon Kelley1f15b812009-10-13 17:49:32 +0100195 int rc;
Simon Kelley69a815a2017-07-08 21:20:16 +0100196
Simon Kelley1f15b812009-10-13 17:49:32 +0100197 if (nomem)
198 *nomem = 0;
199
Simon Kelley69a815a2017-07-08 21:20:16 +0100200 if (!(rc = check_name(in)))
Simon Kelley1f15b812009-10-13 17:49:32 +0100201 return NULL;
202
Simon Kelley09ce3072017-09-25 16:53:55 +0100203#if defined(HAVE_LIBIDN2) && (!defined(IDN2_VERSION_NUMBER) || IDN2_VERSION_NUMBER < 0x02000003)
204 /* older libidn2 strips underscores, so don't do IDN processing
Simon Kelley69a815a2017-07-08 21:20:16 +0100205 if the name has an underscore (check_name() returned 2) */
206 if (rc != 2)
Simon Kelley09ce3072017-09-25 16:53:55 +0100207#endif
208#if defined(HAVE_IDN) || defined(HAVE_LIBIDN2)
Simon Kelley1f15b812009-10-13 17:49:32 +0100209 {
Simon Kelley09ce3072017-09-25 16:53:55 +0100210# ifdef HAVE_LIBIDN2
Simon Kelley69a815a2017-07-08 21:20:16 +0100211 rc = idn2_to_ascii_lz(in, &ret, IDN2_NONTRANSITIONAL);
212 if (rc == IDN2_DISALLOWED)
213 rc = idn2_to_ascii_lz(in, &ret, IDN2_TRANSITIONAL);
Simon Kelley09ce3072017-09-25 16:53:55 +0100214# else
Simon Kelley69a815a2017-07-08 21:20:16 +0100215 rc = idna_to_ascii_lz(in, &ret, 0);
Simon Kelley09ce3072017-09-25 16:53:55 +0100216# endif
Simon Kelley69a815a2017-07-08 21:20:16 +0100217 if (rc != IDNA_SUCCESS)
218 {
219 if (ret)
220 free(ret);
221
222 if (nomem && (rc == IDNA_MALLOC_ERROR || rc == IDNA_DLOPEN_ERROR))
223 {
224 my_syslog(LOG_ERR, _("failed to allocate memory"));
225 *nomem = 1;
226 }
227
228 return NULL;
229 }
230
231 return ret;
232 }
233#endif
234
Simon Kelley1f15b812009-10-13 17:49:32 +0100235 if ((ret = whine_malloc(strlen(in)+1)))
236 strcpy(ret, in);
237 else if (nomem)
238 *nomem = 1;
Simon Kelley1f15b812009-10-13 17:49:32 +0100239
240 return ret;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000241}
242
Simon Kelley0549c732017-09-25 18:17:11 +0100243unsigned char *do_rfc1035_name(unsigned char *p, char *sval, char *limit)
Simon Kelley3d8df262005-08-29 12:19:27 +0100244{
245 int j;
246
247 while (sval && *sval)
248 {
Simon Kelley0549c732017-09-25 18:17:11 +0100249 if (limit && p + 1 > (unsigned char*)limit)
250 return p;
251
Simon Kelley3d8df262005-08-29 12:19:27 +0100252 unsigned char *cp = p++;
253 for (j = 0; *sval && (*sval != '.'); sval++, j++)
Simon Kelleycbe379a2015-04-21 22:57:06 +0100254 {
Simon Kelley0549c732017-09-25 18:17:11 +0100255 if (limit && p + 1 > (unsigned char*)limit)
256 return p;
Simon Kelleycbe379a2015-04-21 22:57:06 +0100257#ifdef HAVE_DNSSEC
258 if (option_bool(OPT_DNSSEC_VALID) && *sval == NAME_ESCAPE)
Simon Kelleyb8f16552015-04-22 21:14:31 +0100259 *p++ = (*(++sval))-1;
Simon Kelleycbe379a2015-04-21 22:57:06 +0100260 else
261#endif
262 *p++ = *sval;
263 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100264 *cp = j;
265 if (*sval)
266 sval++;
267 }
268 return p;
269}
270
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000271/* for use during startup */
Simon Kelley0a852542005-03-23 20:28:59 +0000272void *safe_malloc(size_t size)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000273{
Simon Kelleyd6dce532016-07-11 21:34:31 +0100274 void *ret = calloc(1, size);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000275
276 if (!ret)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100277 die(_("could not get memory"), NULL, EC_NOMEM);
Simon Kelleyd6dce532016-07-11 21:34:31 +0100278
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000279 return ret;
Simon Kelley3d8df262005-08-29 12:19:27 +0100280}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000281
Simon Kelley1a6bca82008-07-11 11:11:42 +0100282void safe_pipe(int *fd, int read_noblock)
283{
284 if (pipe(fd) == -1 ||
285 !fix_fd(fd[1]) ||
286 (read_noblock && !fix_fd(fd[0])))
287 die(_("cannot create pipe: %s"), NULL, EC_MISC);
288}
289
Simon Kelley5aabfc72007-08-29 11:24:47 +0100290void *whine_malloc(size_t size)
291{
Simon Kelleyd6dce532016-07-11 21:34:31 +0100292 void *ret = calloc(1, size);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100293
294 if (!ret)
295 my_syslog(LOG_ERR, _("failed to allocate %d bytes"), (int) size);
Simon Kelleyd55f81f2016-07-06 21:33:56 +0100296
Simon Kelley5aabfc72007-08-29 11:24:47 +0100297 return ret;
298}
299
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000300int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2)
301{
302 if (s1->sa.sa_family == s2->sa.sa_family)
303 {
304 if (s1->sa.sa_family == AF_INET &&
305 s1->in.sin_port == s2->in.sin_port &&
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100306 s1->in.sin_addr.s_addr == s2->in.sin_addr.s_addr)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000307 return 1;
308#ifdef HAVE_IPV6
309 if (s1->sa.sa_family == AF_INET6 &&
310 s1->in6.sin6_port == s2->in6.sin6_port &&
Simon Kelley39341552015-01-18 22:11:10 +0000311 s1->in6.sin6_scope_id == s2->in6.sin6_scope_id &&
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100312 IN6_ARE_ADDR_EQUAL(&s1->in6.sin6_addr, &s2->in6.sin6_addr))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000313 return 1;
314#endif
315 }
316 return 0;
317}
318
319int sa_len(union mysockaddr *addr)
320{
321#ifdef HAVE_SOCKADDR_SA_LEN
322 return addr->sa.sa_len;
323#else
324#ifdef HAVE_IPV6
325 if (addr->sa.sa_family == AF_INET6)
326 return sizeof(addr->in6);
327 else
328#endif
329 return sizeof(addr->in);
330#endif
331}
332
333/* don't use strcasecmp and friends here - they may be messed up by LOCALE */
Simon Kelleyc99df932012-10-12 13:39:04 +0100334int hostname_isequal(const char *a, const char *b)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000335{
336 unsigned int c1, c2;
337
338 do {
Simon Kelley3d8df262005-08-29 12:19:27 +0100339 c1 = (unsigned char) *a++;
340 c2 = (unsigned char) *b++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000341
342 if (c1 >= 'A' && c1 <= 'Z')
343 c1 += 'a' - 'A';
344 if (c2 >= 'A' && c2 <= 'Z')
345 c2 += 'a' - 'A';
346
347 if (c1 != c2)
348 return 0;
349 } while (c1);
350
351 return 1;
352}
Simon Kelleyb637d782016-12-13 16:44:11 +0000353
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100354time_t dnsmasq_time(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000355{
356#ifdef HAVE_BROKEN_RTC
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100357 struct tms dummy;
358 static long tps = 0;
359
360 if (tps == 0)
361 tps = sysconf(_SC_CLK_TCK);
362
363 return (time_t)(times(&dummy)/tps);
Simon Kelley44a2a312004-03-10 20:04:35 +0000364#else
Simon Kelley44a2a312004-03-10 20:04:35 +0000365 return time(NULL);
366#endif
367}
Simon Kelleya84fa1d2004-04-23 22:21:21 +0100368
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800369int netmask_length(struct in_addr mask)
370{
371 int zero_count = 0;
372
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100373 while (0x0 == (mask.s_addr & 0x1) && zero_count < 32)
374 {
375 mask.s_addr >>= 1;
376 zero_count++;
377 }
378
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800379 return 32 - zero_count;
380}
381
Simon Kelleya84fa1d2004-04-23 22:21:21 +0100382int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask)
383{
384 return (a.s_addr & mask.s_addr) == (b.s_addr & mask.s_addr);
385}
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100386
Simon Kelleyc72daea2012-01-05 21:33:27 +0000387#ifdef HAVE_IPV6
388int is_same_net6(struct in6_addr *a, struct in6_addr *b, int prefixlen)
389{
390 int pfbytes = prefixlen >> 3;
391 int pfbits = prefixlen & 7;
392
393 if (memcmp(&a->s6_addr, &b->s6_addr, pfbytes) != 0)
394 return 0;
395
396 if (pfbits == 0 ||
Simon Kelleyc64b7f62012-05-18 10:19:59 +0100397 (a->s6_addr[pfbytes] >> (8 - pfbits) == b->s6_addr[pfbytes] >> (8 - pfbits)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000398 return 1;
399
400 return 0;
401}
402
Josh Soref730c6742017-02-06 16:14:04 +0000403/* return least significant 64 bits if IPv6 address */
Simon Kelley52b92f42012-01-22 16:05:15 +0000404u64 addr6part(struct in6_addr *addr)
405{
406 int i;
407 u64 ret = 0;
408
409 for (i = 8; i < 16; i++)
410 ret = (ret << 8) + addr->s6_addr[i];
411
412 return ret;
413}
414
415void setaddr6part(struct in6_addr *addr, u64 host)
416{
417 int i;
418
419 for (i = 15; i >= 8; i--)
420 {
421 addr->s6_addr[i] = host;
422 host = host >> 8;
423 }
424}
425
Simon Kelleyc72daea2012-01-05 21:33:27 +0000426#endif
427
428
Simon Kelley3d8df262005-08-29 12:19:27 +0100429/* returns port number from address */
430int prettyprint_addr(union mysockaddr *addr, char *buf)
431{
432 int port = 0;
433
434#ifdef HAVE_IPV6
435 if (addr->sa.sa_family == AF_INET)
436 {
437 inet_ntop(AF_INET, &addr->in.sin_addr, buf, ADDRSTRLEN);
438 port = ntohs(addr->in.sin_port);
439 }
440 else if (addr->sa.sa_family == AF_INET6)
441 {
Simon Kelley7de060b2011-08-26 17:24:52 +0100442 char name[IF_NAMESIZE];
Simon Kelley3d8df262005-08-29 12:19:27 +0100443 inet_ntop(AF_INET6, &addr->in6.sin6_addr, buf, ADDRSTRLEN);
Simon Kelley7de060b2011-08-26 17:24:52 +0100444 if (addr->in6.sin6_scope_id != 0 &&
445 if_indextoname(addr->in6.sin6_scope_id, name) &&
446 strlen(buf) + strlen(name) + 2 <= ADDRSTRLEN)
447 {
448 strcat(buf, "%");
449 strcat(buf, name);
450 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100451 port = ntohs(addr->in6.sin6_port);
452 }
453#else
454 strcpy(buf, inet_ntoa(addr->in.sin_addr));
455 port = ntohs(addr->in.sin_port);
456#endif
457
458 return port;
459}
460
Simon Kelley0a852542005-03-23 20:28:59 +0000461void prettyprint_time(char *buf, unsigned int t)
462{
463 if (t == 0xffffffff)
Simon Kelleyb8187c82005-11-26 21:46:27 +0000464 sprintf(buf, _("infinite"));
Simon Kelley0a852542005-03-23 20:28:59 +0000465 else
466 {
467 unsigned int x, p = 0;
Simon Kelley3d8df262005-08-29 12:19:27 +0100468 if ((x = t/86400))
Rosen Penevcbd29e52017-06-27 22:29:51 +0100469 p += sprintf(&buf[p], "%ud", x);
Simon Kelley3d8df262005-08-29 12:19:27 +0100470 if ((x = (t/3600)%24))
Rosen Penevcbd29e52017-06-27 22:29:51 +0100471 p += sprintf(&buf[p], "%uh", x);
Simon Kelley0a852542005-03-23 20:28:59 +0000472 if ((x = (t/60)%60))
Rosen Penevcbd29e52017-06-27 22:29:51 +0100473 p += sprintf(&buf[p], "%um", x);
Simon Kelley0a852542005-03-23 20:28:59 +0000474 if ((x = t%60))
Rosen Penevcbd29e52017-06-27 22:29:51 +0100475 p += sprintf(&buf[p], "%us", x);
Simon Kelley0a852542005-03-23 20:28:59 +0000476 }
477}
478
479
Simon Kelley28866e92011-02-14 20:19:14 +0000480/* in may equal out, when maxlen may be -1 (No max len).
481 Return -1 for extraneous no-hex chars found. */
Simon Kelleycdeda282006-03-16 20:16:06 +0000482int parse_hex(char *in, unsigned char *out, int maxlen,
483 unsigned int *wildcard_mask, int *mac_type)
Simon Kelley0a852542005-03-23 20:28:59 +0000484{
485 int mask = 0, i = 0;
486 char *r;
Simon Kelleycdeda282006-03-16 20:16:06 +0000487
488 if (mac_type)
489 *mac_type = 0;
490
Simon Kelley0a852542005-03-23 20:28:59 +0000491 while (maxlen == -1 || i < maxlen)
492 {
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100493 for (r = in; *r != 0 && *r != ':' && *r != '-' && *r != ' '; r++)
Simon Kelley572b41e2011-02-18 18:11:18 +0000494 if (*r != '*' && !isxdigit((unsigned char)*r))
Simon Kelley28866e92011-02-14 20:19:14 +0000495 return -1;
496
Simon Kelley0a852542005-03-23 20:28:59 +0000497 if (*r == 0)
498 maxlen = i;
Simon Kelleycdeda282006-03-16 20:16:06 +0000499
Simon Kelley0a852542005-03-23 20:28:59 +0000500 if (r != in )
501 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000502 if (*r == '-' && i == 0 && mac_type)
503 {
504 *r = 0;
505 *mac_type = strtol(in, NULL, 16);
506 mac_type = NULL;
507 }
Simon Kelley0a852542005-03-23 20:28:59 +0000508 else
Simon Kelleycdeda282006-03-16 20:16:06 +0000509 {
510 *r = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +0000511 if (strcmp(in, "*") == 0)
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100512 {
513 mask = (mask << 1) | 1;
514 i++;
515 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000516 else
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100517 {
518 int j, bytes = (1 + (r - in))/2;
519 for (j = 0; j < bytes; j++)
520 {
Vladislav Grishenko50db3492013-11-26 11:09:31 +0000521 char sav = sav;
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100522 if (j < bytes - 1)
523 {
524 sav = in[(j+1)*2];
525 in[(j+1)*2] = 0;
526 }
Simon Kelleyae3154a2017-01-01 22:59:46 +0000527 /* checks above allow mix of hexdigit and *, which
528 is illegal. */
529 if (strchr(&in[j*2], '*'))
530 return -1;
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100531 out[i] = strtol(&in[j*2], NULL, 16);
532 mask = mask << 1;
Simon Kelley22827872017-05-08 21:39:04 +0100533 if (++i == maxlen)
Simon Kelley56144132017-05-03 22:54:09 +0100534 break;
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100535 if (j < bytes - 1)
536 in[(j+1)*2] = sav;
537 }
538 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000539 }
Simon Kelley0a852542005-03-23 20:28:59 +0000540 }
541 in = r+1;
542 }
543
544 if (wildcard_mask)
545 *wildcard_mask = mask;
546
547 return i;
548}
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100549
Simon Kelley7622fc02009-06-04 20:32:05 +0100550/* return 0 for no match, or (no matched octets) + 1 */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100551int memcmp_masked(unsigned char *a, unsigned char *b, int len, unsigned int mask)
552{
Simon Kelley7622fc02009-06-04 20:32:05 +0100553 int i, count;
554 for (count = 1, i = len - 1; i >= 0; i--, mask = mask >> 1)
555 if (!(mask & 1))
556 {
557 if (a[i] == b[i])
558 count++;
559 else
560 return 0;
561 }
562 return count;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100563}
564
565/* _note_ may copy buffer */
566int expand_buf(struct iovec *iov, size_t size)
567{
568 void *new;
569
Simon Kelley824af852008-02-12 20:43:05 +0000570 if (size <= (size_t)iov->iov_len)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100571 return 1;
572
Simon Kelley5aabfc72007-08-29 11:24:47 +0100573 if (!(new = whine_malloc(size)))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100574 {
575 errno = ENOMEM;
576 return 0;
577 }
578
579 if (iov->iov_base)
580 {
581 memcpy(new, iov->iov_base, iov->iov_len);
582 free(iov->iov_base);
583 }
584
585 iov->iov_base = new;
586 iov->iov_len = size;
587
588 return 1;
589}
Simon Kelley7cebd202006-05-06 14:13:33 +0100590
Simon Kelley5aabfc72007-08-29 11:24:47 +0100591char *print_mac(char *buff, unsigned char *mac, int len)
Simon Kelley7cebd202006-05-06 14:13:33 +0100592{
Simon Kelley5aabfc72007-08-29 11:24:47 +0100593 char *p = buff;
Simon Kelley7cebd202006-05-06 14:13:33 +0100594 int i;
595
596 if (len == 0)
597 sprintf(p, "<null>");
598 else
599 for (i = 0; i < len; i++)
600 p += sprintf(p, "%.2x%s", mac[i], (i == len - 1) ? "" : ":");
601
Simon Kelley5aabfc72007-08-29 11:24:47 +0100602 return buff;
Simon Kelley7cebd202006-05-06 14:13:33 +0100603}
Simon Kelley16972692006-10-16 20:04:18 +0100604
Simon Kelleyff841eb2015-03-11 21:36:30 +0000605/* rc is return from sendto and friends.
606 Return 1 if we should retry.
607 Set errno to zero if we succeeded. */
608int retry_send(ssize_t rc)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100609{
Simon Kelleyff841eb2015-03-11 21:36:30 +0000610 static int retries = 0;
611 struct timespec waiter;
612
613 if (rc != -1)
614 {
615 retries = 0;
616 errno = 0;
617 return 0;
618 }
619
Simon Kelley57826492014-09-18 22:08:58 +0100620 /* Linux kernels can return EAGAIN in perpetuity when calling
621 sendmsg() and the relevant interface has gone. Here we loop
622 retrying in EAGAIN for 1 second max, to avoid this hanging
623 dnsmasq. */
624
Simon Kelleyff841eb2015-03-11 21:36:30 +0000625 if (errno == EAGAIN || errno == EWOULDBLOCK)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100626 {
627 waiter.tv_sec = 0;
628 waiter.tv_nsec = 10000;
629 nanosleep(&waiter, NULL);
Simon Kelley57826492014-09-18 22:08:58 +0100630 if (retries++ < 1000)
631 return 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100632 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000633
634 retries = 0;
635
636 if (errno == EINTR)
637 return 1;
638
639 return 0;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100640}
641
Simon Kelley16972692006-10-16 20:04:18 +0100642int read_write(int fd, unsigned char *packet, int size, int rw)
643{
644 ssize_t n, done;
645
646 for (done = 0; done < size; done += n)
647 {
Simon Kelleyff841eb2015-03-11 21:36:30 +0000648 do {
649 if (rw)
650 n = read(fd, &packet[done], (size_t)(size - done));
651 else
652 n = write(fd, &packet[done], (size_t)(size - done));
653
654 if (n == 0)
655 return 0;
656
657 } while (retry_send(n) || errno == ENOMEM || errno == ENOBUFS);
Simon Kelley16972692006-10-16 20:04:18 +0100658
Simon Kelleyff841eb2015-03-11 21:36:30 +0000659 if (errno != 0)
660 return 0;
Simon Kelley16972692006-10-16 20:04:18 +0100661 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000662
Simon Kelley16972692006-10-16 20:04:18 +0100663 return 1;
664}
Simon Kelley1a6bca82008-07-11 11:11:42 +0100665
Simon Kelley49333cb2013-03-15 20:30:51 +0000666/* Basically match a string value against a wildcard pattern. */
667int wildcard_match(const char* wildcard, const char* match)
668{
669 while (*wildcard && *match)
670 {
671 if (*wildcard == '*')
672 return 1;
673
674 if (*wildcard != *match)
675 return 0;
676
677 ++wildcard;
678 ++match;
679 }
680
681 return *wildcard == *match;
682}
Neil Jerram70772c92014-06-11 21:22:40 +0100683
684/* The same but comparing a maximum of NUM characters, like strncmp. */
685int wildcard_matchn(const char* wildcard, const char* match, int num)
686{
687 while (*wildcard && *match && num)
688 {
689 if (*wildcard == '*')
690 return 1;
691
692 if (*wildcard != *match)
693 return 0;
694
695 ++wildcard;
696 ++match;
697 --num;
698 }
699
700 return (!num) || (*wildcard == *match);
701}