blob: 532bc16d96ee4a47bdc018c98860b017b4b6a23a [file] [log] [blame]
Simon Kelleyd1ced3a2018-01-01 22:18:03 +00001/* dnsmasq is Copyright (c) 2000-2018 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 {
249 unsigned char *cp = p++;
Simon Kelleyc3667172017-10-13 23:26:29 +0100250
251 if (limit && p > (unsigned char*)limit)
252 return NULL;
253
Simon Kelley3d8df262005-08-29 12:19:27 +0100254 for (j = 0; *sval && (*sval != '.'); sval++, j++)
Simon Kelleycbe379a2015-04-21 22:57:06 +0100255 {
Simon Kelley0549c732017-09-25 18:17:11 +0100256 if (limit && p + 1 > (unsigned char*)limit)
Simon Kelleyc3667172017-10-13 23:26:29 +0100257 return NULL;
258
Simon Kelleycbe379a2015-04-21 22:57:06 +0100259#ifdef HAVE_DNSSEC
260 if (option_bool(OPT_DNSSEC_VALID) && *sval == NAME_ESCAPE)
Simon Kelleyb8f16552015-04-22 21:14:31 +0100261 *p++ = (*(++sval))-1;
Simon Kelleycbe379a2015-04-21 22:57:06 +0100262 else
263#endif
264 *p++ = *sval;
265 }
Simon Kelleyc3667172017-10-13 23:26:29 +0100266
Simon Kelley3d8df262005-08-29 12:19:27 +0100267 *cp = j;
268 if (*sval)
269 sval++;
270 }
Simon Kelleyc3667172017-10-13 23:26:29 +0100271
Simon Kelley3d8df262005-08-29 12:19:27 +0100272 return p;
273}
274
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000275/* for use during startup */
Simon Kelley0a852542005-03-23 20:28:59 +0000276void *safe_malloc(size_t size)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000277{
Simon Kelleyd6dce532016-07-11 21:34:31 +0100278 void *ret = calloc(1, size);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000279
280 if (!ret)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100281 die(_("could not get memory"), NULL, EC_NOMEM);
Simon Kelleyd6dce532016-07-11 21:34:31 +0100282
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000283 return ret;
Simon Kelley3d8df262005-08-29 12:19:27 +0100284}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000285
Simon Kelley1a6bca82008-07-11 11:11:42 +0100286void safe_pipe(int *fd, int read_noblock)
287{
288 if (pipe(fd) == -1 ||
289 !fix_fd(fd[1]) ||
290 (read_noblock && !fix_fd(fd[0])))
291 die(_("cannot create pipe: %s"), NULL, EC_MISC);
292}
293
Simon Kelley5aabfc72007-08-29 11:24:47 +0100294void *whine_malloc(size_t size)
295{
Simon Kelleyd6dce532016-07-11 21:34:31 +0100296 void *ret = calloc(1, size);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100297
298 if (!ret)
299 my_syslog(LOG_ERR, _("failed to allocate %d bytes"), (int) size);
Simon Kelleyd55f81f2016-07-06 21:33:56 +0100300
Simon Kelley5aabfc72007-08-29 11:24:47 +0100301 return ret;
302}
303
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000304int sockaddr_isequal(union mysockaddr *s1, union mysockaddr *s2)
305{
306 if (s1->sa.sa_family == s2->sa.sa_family)
307 {
308 if (s1->sa.sa_family == AF_INET &&
309 s1->in.sin_port == s2->in.sin_port &&
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100310 s1->in.sin_addr.s_addr == s2->in.sin_addr.s_addr)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000311 return 1;
312#ifdef HAVE_IPV6
313 if (s1->sa.sa_family == AF_INET6 &&
314 s1->in6.sin6_port == s2->in6.sin6_port &&
Simon Kelley39341552015-01-18 22:11:10 +0000315 s1->in6.sin6_scope_id == s2->in6.sin6_scope_id &&
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100316 IN6_ARE_ADDR_EQUAL(&s1->in6.sin6_addr, &s2->in6.sin6_addr))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000317 return 1;
318#endif
319 }
320 return 0;
321}
322
323int sa_len(union mysockaddr *addr)
324{
325#ifdef HAVE_SOCKADDR_SA_LEN
326 return addr->sa.sa_len;
327#else
328#ifdef HAVE_IPV6
329 if (addr->sa.sa_family == AF_INET6)
330 return sizeof(addr->in6);
331 else
332#endif
333 return sizeof(addr->in);
334#endif
335}
336
337/* don't use strcasecmp and friends here - they may be messed up by LOCALE */
Simon Kelleyc99df932012-10-12 13:39:04 +0100338int hostname_isequal(const char *a, const char *b)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000339{
340 unsigned int c1, c2;
341
342 do {
Simon Kelley3d8df262005-08-29 12:19:27 +0100343 c1 = (unsigned char) *a++;
344 c2 = (unsigned char) *b++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000345
346 if (c1 >= 'A' && c1 <= 'Z')
347 c1 += 'a' - 'A';
348 if (c2 >= 'A' && c2 <= 'Z')
349 c2 += 'a' - 'A';
350
351 if (c1 != c2)
352 return 0;
353 } while (c1);
354
355 return 1;
356}
Simon Kelleyb637d782016-12-13 16:44:11 +0000357
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100358time_t dnsmasq_time(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000359{
360#ifdef HAVE_BROKEN_RTC
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100361 struct tms dummy;
362 static long tps = 0;
363
364 if (tps == 0)
365 tps = sysconf(_SC_CLK_TCK);
366
367 return (time_t)(times(&dummy)/tps);
Simon Kelley44a2a312004-03-10 20:04:35 +0000368#else
Simon Kelley44a2a312004-03-10 20:04:35 +0000369 return time(NULL);
370#endif
371}
Simon Kelleya84fa1d2004-04-23 22:21:21 +0100372
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800373int netmask_length(struct in_addr mask)
374{
375 int zero_count = 0;
376
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100377 while (0x0 == (mask.s_addr & 0x1) && zero_count < 32)
378 {
379 mask.s_addr >>= 1;
380 zero_count++;
381 }
382
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800383 return 32 - zero_count;
384}
385
Simon Kelleya84fa1d2004-04-23 22:21:21 +0100386int is_same_net(struct in_addr a, struct in_addr b, struct in_addr mask)
387{
388 return (a.s_addr & mask.s_addr) == (b.s_addr & mask.s_addr);
389}
Simon Kelleyfd9fa482004-10-21 20:24:00 +0100390
Simon Kelleyc72daea2012-01-05 21:33:27 +0000391#ifdef HAVE_IPV6
392int is_same_net6(struct in6_addr *a, struct in6_addr *b, int prefixlen)
393{
394 int pfbytes = prefixlen >> 3;
395 int pfbits = prefixlen & 7;
396
397 if (memcmp(&a->s6_addr, &b->s6_addr, pfbytes) != 0)
398 return 0;
399
400 if (pfbits == 0 ||
Simon Kelleyc64b7f62012-05-18 10:19:59 +0100401 (a->s6_addr[pfbytes] >> (8 - pfbits) == b->s6_addr[pfbytes] >> (8 - pfbits)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000402 return 1;
403
404 return 0;
405}
406
Josh Soref730c6742017-02-06 16:14:04 +0000407/* return least significant 64 bits if IPv6 address */
Simon Kelley52b92f42012-01-22 16:05:15 +0000408u64 addr6part(struct in6_addr *addr)
409{
410 int i;
411 u64 ret = 0;
412
413 for (i = 8; i < 16; i++)
414 ret = (ret << 8) + addr->s6_addr[i];
415
416 return ret;
417}
418
419void setaddr6part(struct in6_addr *addr, u64 host)
420{
421 int i;
422
423 for (i = 15; i >= 8; i--)
424 {
425 addr->s6_addr[i] = host;
426 host = host >> 8;
427 }
428}
429
Simon Kelleyc72daea2012-01-05 21:33:27 +0000430#endif
431
432
Simon Kelley3d8df262005-08-29 12:19:27 +0100433/* returns port number from address */
434int prettyprint_addr(union mysockaddr *addr, char *buf)
435{
436 int port = 0;
437
438#ifdef HAVE_IPV6
439 if (addr->sa.sa_family == AF_INET)
440 {
441 inet_ntop(AF_INET, &addr->in.sin_addr, buf, ADDRSTRLEN);
442 port = ntohs(addr->in.sin_port);
443 }
444 else if (addr->sa.sa_family == AF_INET6)
445 {
Simon Kelley7de060b2011-08-26 17:24:52 +0100446 char name[IF_NAMESIZE];
Simon Kelley3d8df262005-08-29 12:19:27 +0100447 inet_ntop(AF_INET6, &addr->in6.sin6_addr, buf, ADDRSTRLEN);
Simon Kelley7de060b2011-08-26 17:24:52 +0100448 if (addr->in6.sin6_scope_id != 0 &&
449 if_indextoname(addr->in6.sin6_scope_id, name) &&
450 strlen(buf) + strlen(name) + 2 <= ADDRSTRLEN)
451 {
452 strcat(buf, "%");
453 strcat(buf, name);
454 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100455 port = ntohs(addr->in6.sin6_port);
456 }
457#else
458 strcpy(buf, inet_ntoa(addr->in.sin_addr));
459 port = ntohs(addr->in.sin_port);
460#endif
461
462 return port;
463}
464
Simon Kelley0a852542005-03-23 20:28:59 +0000465void prettyprint_time(char *buf, unsigned int t)
466{
467 if (t == 0xffffffff)
Simon Kelleyb8187c82005-11-26 21:46:27 +0000468 sprintf(buf, _("infinite"));
Simon Kelley0a852542005-03-23 20:28:59 +0000469 else
470 {
471 unsigned int x, p = 0;
Simon Kelley3d8df262005-08-29 12:19:27 +0100472 if ((x = t/86400))
Rosen Penevcbd29e52017-06-27 22:29:51 +0100473 p += sprintf(&buf[p], "%ud", x);
Simon Kelley3d8df262005-08-29 12:19:27 +0100474 if ((x = (t/3600)%24))
Rosen Penevcbd29e52017-06-27 22:29:51 +0100475 p += sprintf(&buf[p], "%uh", x);
Simon Kelley0a852542005-03-23 20:28:59 +0000476 if ((x = (t/60)%60))
Rosen Penevcbd29e52017-06-27 22:29:51 +0100477 p += sprintf(&buf[p], "%um", x);
Simon Kelley0a852542005-03-23 20:28:59 +0000478 if ((x = t%60))
Rosen Penevcbd29e52017-06-27 22:29:51 +0100479 p += sprintf(&buf[p], "%us", x);
Simon Kelley0a852542005-03-23 20:28:59 +0000480 }
481}
482
483
Simon Kelley28866e92011-02-14 20:19:14 +0000484/* in may equal out, when maxlen may be -1 (No max len).
485 Return -1 for extraneous no-hex chars found. */
Simon Kelleycdeda282006-03-16 20:16:06 +0000486int parse_hex(char *in, unsigned char *out, int maxlen,
487 unsigned int *wildcard_mask, int *mac_type)
Simon Kelley0a852542005-03-23 20:28:59 +0000488{
489 int mask = 0, i = 0;
490 char *r;
Simon Kelleycdeda282006-03-16 20:16:06 +0000491
492 if (mac_type)
493 *mac_type = 0;
494
Simon Kelley0a852542005-03-23 20:28:59 +0000495 while (maxlen == -1 || i < maxlen)
496 {
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100497 for (r = in; *r != 0 && *r != ':' && *r != '-' && *r != ' '; r++)
Simon Kelley572b41e2011-02-18 18:11:18 +0000498 if (*r != '*' && !isxdigit((unsigned char)*r))
Simon Kelley28866e92011-02-14 20:19:14 +0000499 return -1;
500
Simon Kelley0a852542005-03-23 20:28:59 +0000501 if (*r == 0)
502 maxlen = i;
Simon Kelleycdeda282006-03-16 20:16:06 +0000503
Simon Kelley0a852542005-03-23 20:28:59 +0000504 if (r != in )
505 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000506 if (*r == '-' && i == 0 && mac_type)
507 {
508 *r = 0;
509 *mac_type = strtol(in, NULL, 16);
510 mac_type = NULL;
511 }
Simon Kelley0a852542005-03-23 20:28:59 +0000512 else
Simon Kelleycdeda282006-03-16 20:16:06 +0000513 {
514 *r = 0;
Simon Kelleycdeda282006-03-16 20:16:06 +0000515 if (strcmp(in, "*") == 0)
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100516 {
517 mask = (mask << 1) | 1;
518 i++;
519 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000520 else
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100521 {
522 int j, bytes = (1 + (r - in))/2;
523 for (j = 0; j < bytes; j++)
524 {
Vladislav Grishenko50db3492013-11-26 11:09:31 +0000525 char sav = sav;
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100526 if (j < bytes - 1)
527 {
528 sav = in[(j+1)*2];
529 in[(j+1)*2] = 0;
530 }
Simon Kelleyae3154a2017-01-01 22:59:46 +0000531 /* checks above allow mix of hexdigit and *, which
532 is illegal. */
533 if (strchr(&in[j*2], '*'))
534 return -1;
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100535 out[i] = strtol(&in[j*2], NULL, 16);
536 mask = mask << 1;
Simon Kelley22827872017-05-08 21:39:04 +0100537 if (++i == maxlen)
Simon Kelley56144132017-05-03 22:54:09 +0100538 break;
Simon Kelley9f7f3b12012-05-28 21:39:57 +0100539 if (j < bytes - 1)
540 in[(j+1)*2] = sav;
541 }
542 }
Simon Kelleycdeda282006-03-16 20:16:06 +0000543 }
Simon Kelley0a852542005-03-23 20:28:59 +0000544 }
545 in = r+1;
546 }
547
548 if (wildcard_mask)
549 *wildcard_mask = mask;
550
551 return i;
552}
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100553
Simon Kelley7622fc02009-06-04 20:32:05 +0100554/* return 0 for no match, or (no matched octets) + 1 */
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100555int memcmp_masked(unsigned char *a, unsigned char *b, int len, unsigned int mask)
556{
Simon Kelley7622fc02009-06-04 20:32:05 +0100557 int i, count;
558 for (count = 1, i = len - 1; i >= 0; i--, mask = mask >> 1)
559 if (!(mask & 1))
560 {
561 if (a[i] == b[i])
562 count++;
563 else
564 return 0;
565 }
566 return count;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100567}
568
569/* _note_ may copy buffer */
570int expand_buf(struct iovec *iov, size_t size)
571{
572 void *new;
573
Simon Kelley824af852008-02-12 20:43:05 +0000574 if (size <= (size_t)iov->iov_len)
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100575 return 1;
576
Simon Kelley5aabfc72007-08-29 11:24:47 +0100577 if (!(new = whine_malloc(size)))
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100578 {
579 errno = ENOMEM;
580 return 0;
581 }
582
583 if (iov->iov_base)
584 {
585 memcpy(new, iov->iov_base, iov->iov_len);
586 free(iov->iov_base);
587 }
588
589 iov->iov_base = new;
590 iov->iov_len = size;
591
592 return 1;
593}
Simon Kelley7cebd202006-05-06 14:13:33 +0100594
Simon Kelley5aabfc72007-08-29 11:24:47 +0100595char *print_mac(char *buff, unsigned char *mac, int len)
Simon Kelley7cebd202006-05-06 14:13:33 +0100596{
Simon Kelley5aabfc72007-08-29 11:24:47 +0100597 char *p = buff;
Simon Kelley7cebd202006-05-06 14:13:33 +0100598 int i;
599
600 if (len == 0)
601 sprintf(p, "<null>");
602 else
603 for (i = 0; i < len; i++)
604 p += sprintf(p, "%.2x%s", mac[i], (i == len - 1) ? "" : ":");
605
Simon Kelley5aabfc72007-08-29 11:24:47 +0100606 return buff;
Simon Kelley7cebd202006-05-06 14:13:33 +0100607}
Simon Kelley16972692006-10-16 20:04:18 +0100608
Simon Kelleyff841eb2015-03-11 21:36:30 +0000609/* rc is return from sendto and friends.
610 Return 1 if we should retry.
611 Set errno to zero if we succeeded. */
612int retry_send(ssize_t rc)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100613{
Simon Kelleyff841eb2015-03-11 21:36:30 +0000614 static int retries = 0;
615 struct timespec waiter;
616
617 if (rc != -1)
618 {
619 retries = 0;
620 errno = 0;
621 return 0;
622 }
623
Simon Kelley57826492014-09-18 22:08:58 +0100624 /* Linux kernels can return EAGAIN in perpetuity when calling
625 sendmsg() and the relevant interface has gone. Here we loop
626 retrying in EAGAIN for 1 second max, to avoid this hanging
627 dnsmasq. */
628
Simon Kelleyff841eb2015-03-11 21:36:30 +0000629 if (errno == EAGAIN || errno == EWOULDBLOCK)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100630 {
631 waiter.tv_sec = 0;
632 waiter.tv_nsec = 10000;
633 nanosleep(&waiter, NULL);
Simon Kelley57826492014-09-18 22:08:58 +0100634 if (retries++ < 1000)
635 return 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100636 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000637
638 retries = 0;
639
640 if (errno == EINTR)
641 return 1;
642
643 return 0;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100644}
645
Simon Kelley16972692006-10-16 20:04:18 +0100646int read_write(int fd, unsigned char *packet, int size, int rw)
647{
648 ssize_t n, done;
649
650 for (done = 0; done < size; done += n)
651 {
Simon Kelleyff841eb2015-03-11 21:36:30 +0000652 do {
653 if (rw)
654 n = read(fd, &packet[done], (size_t)(size - done));
655 else
656 n = write(fd, &packet[done], (size_t)(size - done));
657
658 if (n == 0)
659 return 0;
660
661 } while (retry_send(n) || errno == ENOMEM || errno == ENOBUFS);
Simon Kelley16972692006-10-16 20:04:18 +0100662
Simon Kelleyff841eb2015-03-11 21:36:30 +0000663 if (errno != 0)
664 return 0;
Simon Kelley16972692006-10-16 20:04:18 +0100665 }
Simon Kelleyff841eb2015-03-11 21:36:30 +0000666
Simon Kelley16972692006-10-16 20:04:18 +0100667 return 1;
668}
Simon Kelley1a6bca82008-07-11 11:11:42 +0100669
Simon Kelley49333cb2013-03-15 20:30:51 +0000670/* Basically match a string value against a wildcard pattern. */
671int wildcard_match(const char* wildcard, const char* match)
672{
673 while (*wildcard && *match)
674 {
675 if (*wildcard == '*')
676 return 1;
677
678 if (*wildcard != *match)
679 return 0;
680
681 ++wildcard;
682 ++match;
683 }
684
685 return *wildcard == *match;
686}
Neil Jerram70772c92014-06-11 21:22:40 +0100687
688/* The same but comparing a maximum of NUM characters, like strncmp. */
689int wildcard_matchn(const char* wildcard, const char* match, int num)
690{
691 while (*wildcard && *match && num)
692 {
693 if (*wildcard == '*')
694 return 1;
695
696 if (*wildcard != *match)
697 return 0;
698
699 ++wildcard;
700 ++match;
701 --num;
702 }
703
704 return (!num) || (*wildcard == *match);
705}