Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2 | |
| 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 Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 8 | 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 Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 12 | |
| 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 Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 17 | /* The SURF random number generator was taken from djbdns-1.05, by |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 18 | Daniel J Bernstein, which is public domain. */ |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 19 | |
| 20 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 21 | #include "dnsmasq.h" |
| 22 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 23 | #ifdef HAVE_BROKEN_RTC |
| 24 | #include <sys/times.h> |
| 25 | #endif |
| 26 | |
Simon Kelley | 43cdf1c | 2017-05-21 22:12:44 +0100 | [diff] [blame] | 27 | #if defined(HAVE_LIBIDN2) |
Petr Menšík | d203af4 | 2017-05-10 21:41:57 +0100 | [diff] [blame] | 28 | #include <idn2.h> |
Simon Kelley | 43cdf1c | 2017-05-21 22:12:44 +0100 | [diff] [blame] | 29 | #elif defined(HAVE_IDN) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 30 | #include <idna.h> |
| 31 | #endif |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 32 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 33 | /* SURF random number generator */ |
| 34 | |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 35 | static u32 seed[32]; |
| 36 | static u32 in[12]; |
| 37 | static u32 out[8]; |
Simon Kelley | 6586e83 | 2013-11-07 14:20:13 +0000 | [diff] [blame] | 38 | static int outleft = 0; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 39 | |
| 40 | void 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 | |
| 55 | static void surf(void) |
| 56 | { |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 57 | u32 t[12]; u32 x; u32 sum = 0; |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 58 | 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 | |
| 74 | unsigned short rand16(void) |
| 75 | { |
Simon Kelley | 6586e83 | 2013-11-07 14:20:13 +0000 | [diff] [blame] | 76 | 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 Kelley | b5ea1cc | 2014-07-29 16:34:14 +0100 | [diff] [blame] | 86 | u32 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 Kelley | 6586e83 | 2013-11-07 14:20:13 +0000 | [diff] [blame] | 98 | u64 rand64(void) |
| 99 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 100 | static int outleft = 0; |
| 101 | |
Simon Kelley | 6586e83 | 2013-11-07 14:20:13 +0000 | [diff] [blame] | 102 | 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 Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 110 | |
Simon Kelley | 6586e83 | 2013-11-07 14:20:13 +0000 | [diff] [blame] | 111 | return (u64)out[outleft+1] + (((u64)out[outleft]) << 32); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 114 | /* returns 2 if names is OK but contains one or more underscores */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 115 | static int check_name(char *in) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 116 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 117 | /* remove trailing . |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 118 | also fail empty string and label > 63 chars */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 119 | size_t dotgap = 0, l = strlen(in); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 120 | char c; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 121 | int nowhite = 0; |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 122 | int hasuscore = 0; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 123 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 124 | if (l == 0 || l > MAXDNAME) return 0; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 125 | |
| 126 | if (in[l-1] == '.') |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 127 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 128 | in[l-1] = 0; |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 129 | nowhite = 1; |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 130 | } |
Simon Kelley | 0fc2f31 | 2014-01-08 10:26:58 +0000 | [diff] [blame] | 131 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 132 | for (; (c = *in); in++) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 133 | { |
| 134 | if (c == '.') |
| 135 | dotgap = 0; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 136 | else if (++dotgap > MAXLABEL) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 137 | return 0; |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 138 | else if (isascii((unsigned char)c) && iscntrl((unsigned char)c)) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 139 | /* iscntrl only gives expected results for ascii */ |
| 140 | return 0; |
Simon Kelley | 43cdf1c | 2017-05-21 22:12:44 +0100 | [diff] [blame] | 141 | #if !defined(HAVE_IDN) && !defined(HAVE_LIBIDN2) |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 142 | else if (!isascii((unsigned char)c)) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 143 | return 0; |
| 144 | #endif |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 145 | else if (c != ' ') |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 146 | { |
| 147 | nowhite = 1; |
| 148 | if (c == '_') |
| 149 | hasuscore = 1; |
| 150 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 151 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 152 | |
| 153 | if (!nowhite) |
| 154 | return 0; |
| 155 | |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 156 | return hasuscore ? 2 : 1; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 157 | } |
| 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. */ |
| 163 | int legal_hostname(char *name) |
| 164 | { |
| 165 | char c; |
Simon Kelley | 7abb69b | 2013-04-29 10:52:16 +0100 | [diff] [blame] | 166 | int first; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 167 | |
| 168 | if (!check_name(name)) |
| 169 | return 0; |
| 170 | |
Simon Kelley | 7abb69b | 2013-04-29 10:52:16 +0100 | [diff] [blame] | 171 | for (first = 1; (c = *name); name++, first = 0) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 172 | /* check for legal char a-z A-Z 0-9 - _ . */ |
| 173 | { |
| 174 | if ((c >= 'A' && c <= 'Z') || |
Kyle Mestery | d859ca2 | 2013-07-24 13:11:58 +0100 | [diff] [blame] | 175 | (c >= 'a' && c <= 'z') || |
| 176 | (c >= '0' && c <= '9')) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 177 | continue; |
Simon Kelley | 7abb69b | 2013-04-29 10:52:16 +0100 | [diff] [blame] | 178 | |
Kyle Mestery | d859ca2 | 2013-07-24 13:11:58 +0100 | [diff] [blame] | 179 | if (!first && (c == '-' || c == '_')) |
Simon Kelley | 7abb69b | 2013-04-29 10:52:16 +0100 | [diff] [blame] | 180 | continue; |
Kyle Mestery | d859ca2 | 2013-07-24 13:11:58 +0100 | [diff] [blame] | 181 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 182 | /* end of hostname part */ |
| 183 | if (c == '.') |
| 184 | return 1; |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | char *canonicalise(char *in, int *nomem) |
| 193 | { |
| 194 | char *ret = NULL; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 195 | int rc; |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 196 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 197 | if (nomem) |
| 198 | *nomem = 0; |
| 199 | |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 200 | if (!(rc = check_name(in))) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 201 | return NULL; |
| 202 | |
Simon Kelley | 09ce307 | 2017-09-25 16:53:55 +0100 | [diff] [blame] | 203 | #if defined(HAVE_LIBIDN2) && (!defined(IDN2_VERSION_NUMBER) || IDN2_VERSION_NUMBER < 0x02000003) |
| 204 | /* older libidn2 strips underscores, so don't do IDN processing |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 205 | if the name has an underscore (check_name() returned 2) */ |
| 206 | if (rc != 2) |
Simon Kelley | 09ce307 | 2017-09-25 16:53:55 +0100 | [diff] [blame] | 207 | #endif |
| 208 | #if defined(HAVE_IDN) || defined(HAVE_LIBIDN2) |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 209 | { |
Simon Kelley | 09ce307 | 2017-09-25 16:53:55 +0100 | [diff] [blame] | 210 | # ifdef HAVE_LIBIDN2 |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 211 | 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 Kelley | 09ce307 | 2017-09-25 16:53:55 +0100 | [diff] [blame] | 214 | # else |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 215 | rc = idna_to_ascii_lz(in, &ret, 0); |
Simon Kelley | 09ce307 | 2017-09-25 16:53:55 +0100 | [diff] [blame] | 216 | # endif |
Simon Kelley | 69a815a | 2017-07-08 21:20:16 +0100 | [diff] [blame] | 217 | 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 Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 235 | if ((ret = whine_malloc(strlen(in)+1))) |
| 236 | strcpy(ret, in); |
| 237 | else if (nomem) |
| 238 | *nomem = 1; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 239 | |
| 240 | return ret; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 243 | unsigned char *do_rfc1035_name(unsigned char *p, char *sval, char *limit) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 244 | { |
| 245 | int j; |
| 246 | |
| 247 | while (sval && *sval) |
| 248 | { |
| 249 | unsigned char *cp = p++; |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 250 | |
| 251 | if (limit && p > (unsigned char*)limit) |
| 252 | return NULL; |
| 253 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 254 | for (j = 0; *sval && (*sval != '.'); sval++, j++) |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 255 | { |
Simon Kelley | 0549c73 | 2017-09-25 18:17:11 +0100 | [diff] [blame] | 256 | if (limit && p + 1 > (unsigned char*)limit) |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 257 | return NULL; |
| 258 | |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 259 | #ifdef HAVE_DNSSEC |
| 260 | if (option_bool(OPT_DNSSEC_VALID) && *sval == NAME_ESCAPE) |
Simon Kelley | b8f1655 | 2015-04-22 21:14:31 +0100 | [diff] [blame] | 261 | *p++ = (*(++sval))-1; |
Simon Kelley | cbe379a | 2015-04-21 22:57:06 +0100 | [diff] [blame] | 262 | else |
| 263 | #endif |
| 264 | *p++ = *sval; |
| 265 | } |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 266 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 267 | *cp = j; |
| 268 | if (*sval) |
| 269 | sval++; |
| 270 | } |
Simon Kelley | c366717 | 2017-10-13 23:26:29 +0100 | [diff] [blame] | 271 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 272 | return p; |
| 273 | } |
| 274 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 275 | /* for use during startup */ |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 276 | void *safe_malloc(size_t size) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 277 | { |
Simon Kelley | d6dce53 | 2016-07-11 21:34:31 +0100 | [diff] [blame] | 278 | void *ret = calloc(1, size); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 279 | |
| 280 | if (!ret) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 281 | die(_("could not get memory"), NULL, EC_NOMEM); |
Simon Kelley | d6dce53 | 2016-07-11 21:34:31 +0100 | [diff] [blame] | 282 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 283 | return ret; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 284 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 285 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 286 | void 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 Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 294 | void *whine_malloc(size_t size) |
| 295 | { |
Simon Kelley | d6dce53 | 2016-07-11 21:34:31 +0100 | [diff] [blame] | 296 | void *ret = calloc(1, size); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 297 | |
| 298 | if (!ret) |
| 299 | my_syslog(LOG_ERR, _("failed to allocate %d bytes"), (int) size); |
Simon Kelley | d55f81f | 2016-07-06 21:33:56 +0100 | [diff] [blame] | 300 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 301 | return ret; |
| 302 | } |
| 303 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 304 | int 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 Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 310 | s1->in.sin_addr.s_addr == s2->in.sin_addr.s_addr) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 311 | return 1; |
| 312 | #ifdef HAVE_IPV6 |
| 313 | if (s1->sa.sa_family == AF_INET6 && |
| 314 | s1->in6.sin6_port == s2->in6.sin6_port && |
Simon Kelley | 3934155 | 2015-01-18 22:11:10 +0000 | [diff] [blame] | 315 | s1->in6.sin6_scope_id == s2->in6.sin6_scope_id && |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 316 | IN6_ARE_ADDR_EQUAL(&s1->in6.sin6_addr, &s2->in6.sin6_addr)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 317 | return 1; |
| 318 | #endif |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | int 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 Kelley | c99df93 | 2012-10-12 13:39:04 +0100 | [diff] [blame] | 338 | int hostname_isequal(const char *a, const char *b) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 339 | { |
| 340 | unsigned int c1, c2; |
| 341 | |
| 342 | do { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 343 | c1 = (unsigned char) *a++; |
| 344 | c2 = (unsigned char) *b++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 345 | |
| 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 Kelley | b637d78 | 2016-12-13 16:44:11 +0000 | [diff] [blame] | 357 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 358 | time_t dnsmasq_time(void) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 359 | { |
| 360 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 361 | 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 Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 368 | #else |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 369 | return time(NULL); |
| 370 | #endif |
| 371 | } |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 372 | |
Lung-Pin Chang | dc8a1b1 | 2014-07-02 10:48:05 +0800 | [diff] [blame] | 373 | int netmask_length(struct in_addr mask) |
| 374 | { |
| 375 | int zero_count = 0; |
| 376 | |
Simon Kelley | 6d8e8ac | 2014-07-13 22:12:45 +0100 | [diff] [blame] | 377 | while (0x0 == (mask.s_addr & 0x1) && zero_count < 32) |
| 378 | { |
| 379 | mask.s_addr >>= 1; |
| 380 | zero_count++; |
| 381 | } |
| 382 | |
Lung-Pin Chang | dc8a1b1 | 2014-07-02 10:48:05 +0800 | [diff] [blame] | 383 | return 32 - zero_count; |
| 384 | } |
| 385 | |
Simon Kelley | a84fa1d | 2004-04-23 22:21:21 +0100 | [diff] [blame] | 386 | int 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 Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 390 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 391 | #ifdef HAVE_IPV6 |
| 392 | int 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 Kelley | c64b7f6 | 2012-05-18 10:19:59 +0100 | [diff] [blame] | 401 | (a->s6_addr[pfbytes] >> (8 - pfbits) == b->s6_addr[pfbytes] >> (8 - pfbits))) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 402 | return 1; |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 407 | /* return least significant 64 bits if IPv6 address */ |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 408 | u64 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 | |
| 419 | void 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 Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 430 | #endif |
| 431 | |
| 432 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 433 | /* returns port number from address */ |
| 434 | int 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 Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 446 | char name[IF_NAMESIZE]; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 447 | inet_ntop(AF_INET6, &addr->in6.sin6_addr, buf, ADDRSTRLEN); |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 448 | 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 Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 455 | 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 Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 465 | void prettyprint_time(char *buf, unsigned int t) |
| 466 | { |
| 467 | if (t == 0xffffffff) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 468 | sprintf(buf, _("infinite")); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 469 | else |
| 470 | { |
| 471 | unsigned int x, p = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 472 | if ((x = t/86400)) |
Rosen Penev | cbd29e5 | 2017-06-27 22:29:51 +0100 | [diff] [blame] | 473 | p += sprintf(&buf[p], "%ud", x); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 474 | if ((x = (t/3600)%24)) |
Rosen Penev | cbd29e5 | 2017-06-27 22:29:51 +0100 | [diff] [blame] | 475 | p += sprintf(&buf[p], "%uh", x); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 476 | if ((x = (t/60)%60)) |
Rosen Penev | cbd29e5 | 2017-06-27 22:29:51 +0100 | [diff] [blame] | 477 | p += sprintf(&buf[p], "%um", x); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 478 | if ((x = t%60)) |
Rosen Penev | cbd29e5 | 2017-06-27 22:29:51 +0100 | [diff] [blame] | 479 | p += sprintf(&buf[p], "%us", x); |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 484 | /* in may equal out, when maxlen may be -1 (No max len). |
| 485 | Return -1 for extraneous no-hex chars found. */ |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 486 | int parse_hex(char *in, unsigned char *out, int maxlen, |
| 487 | unsigned int *wildcard_mask, int *mac_type) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 488 | { |
| 489 | int mask = 0, i = 0; |
| 490 | char *r; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 491 | |
| 492 | if (mac_type) |
| 493 | *mac_type = 0; |
| 494 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 495 | while (maxlen == -1 || i < maxlen) |
| 496 | { |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 497 | for (r = in; *r != 0 && *r != ':' && *r != '-' && *r != ' '; r++) |
Simon Kelley | 572b41e | 2011-02-18 18:11:18 +0000 | [diff] [blame] | 498 | if (*r != '*' && !isxdigit((unsigned char)*r)) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 499 | return -1; |
| 500 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 501 | if (*r == 0) |
| 502 | maxlen = i; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 503 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 504 | if (r != in ) |
| 505 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 506 | if (*r == '-' && i == 0 && mac_type) |
| 507 | { |
| 508 | *r = 0; |
| 509 | *mac_type = strtol(in, NULL, 16); |
| 510 | mac_type = NULL; |
| 511 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 512 | else |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 513 | { |
| 514 | *r = 0; |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 515 | if (strcmp(in, "*") == 0) |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 516 | { |
| 517 | mask = (mask << 1) | 1; |
| 518 | i++; |
| 519 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 520 | else |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 521 | { |
| 522 | int j, bytes = (1 + (r - in))/2; |
| 523 | for (j = 0; j < bytes; j++) |
| 524 | { |
Vladislav Grishenko | 50db349 | 2013-11-26 11:09:31 +0000 | [diff] [blame] | 525 | char sav = sav; |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 526 | if (j < bytes - 1) |
| 527 | { |
| 528 | sav = in[(j+1)*2]; |
| 529 | in[(j+1)*2] = 0; |
| 530 | } |
Simon Kelley | ae3154a | 2017-01-01 22:59:46 +0000 | [diff] [blame] | 531 | /* checks above allow mix of hexdigit and *, which |
| 532 | is illegal. */ |
| 533 | if (strchr(&in[j*2], '*')) |
| 534 | return -1; |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 535 | out[i] = strtol(&in[j*2], NULL, 16); |
| 536 | mask = mask << 1; |
Simon Kelley | 2282787 | 2017-05-08 21:39:04 +0100 | [diff] [blame] | 537 | if (++i == maxlen) |
Simon Kelley | 5614413 | 2017-05-03 22:54:09 +0100 | [diff] [blame] | 538 | break; |
Simon Kelley | 9f7f3b1 | 2012-05-28 21:39:57 +0100 | [diff] [blame] | 539 | if (j < bytes - 1) |
| 540 | in[(j+1)*2] = sav; |
| 541 | } |
| 542 | } |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 543 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 544 | } |
| 545 | in = r+1; |
| 546 | } |
| 547 | |
| 548 | if (wildcard_mask) |
| 549 | *wildcard_mask = mask; |
| 550 | |
| 551 | return i; |
| 552 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 553 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 554 | /* return 0 for no match, or (no matched octets) + 1 */ |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 555 | int memcmp_masked(unsigned char *a, unsigned char *b, int len, unsigned int mask) |
| 556 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 557 | 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 Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* _note_ may copy buffer */ |
| 570 | int expand_buf(struct iovec *iov, size_t size) |
| 571 | { |
| 572 | void *new; |
| 573 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 574 | if (size <= (size_t)iov->iov_len) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 575 | return 1; |
| 576 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 577 | if (!(new = whine_malloc(size))) |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 578 | { |
| 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 Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 594 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 595 | char *print_mac(char *buff, unsigned char *mac, int len) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 596 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 597 | char *p = buff; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 598 | 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 Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 606 | return buff; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 607 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 608 | |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 609 | /* rc is return from sendto and friends. |
| 610 | Return 1 if we should retry. |
| 611 | Set errno to zero if we succeeded. */ |
| 612 | int retry_send(ssize_t rc) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 613 | { |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 614 | 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 Kelley | 5782649 | 2014-09-18 22:08:58 +0100 | [diff] [blame] | 624 | /* 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 Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 629 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 630 | { |
| 631 | waiter.tv_sec = 0; |
| 632 | waiter.tv_nsec = 10000; |
| 633 | nanosleep(&waiter, NULL); |
Simon Kelley | 5782649 | 2014-09-18 22:08:58 +0100 | [diff] [blame] | 634 | if (retries++ < 1000) |
| 635 | return 1; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 636 | } |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 637 | |
| 638 | retries = 0; |
| 639 | |
| 640 | if (errno == EINTR) |
| 641 | return 1; |
| 642 | |
| 643 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 644 | } |
| 645 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 646 | int 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 Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 652 | 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 Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 662 | |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 663 | if (errno != 0) |
| 664 | return 0; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 665 | } |
Simon Kelley | ff841eb | 2015-03-11 21:36:30 +0000 | [diff] [blame] | 666 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 667 | return 1; |
| 668 | } |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 669 | |
Simon Kelley | 49333cb | 2013-03-15 20:30:51 +0000 | [diff] [blame] | 670 | /* Basically match a string value against a wildcard pattern. */ |
| 671 | int 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 Jerram | 70772c9 | 2014-06-11 21:22:40 +0100 | [diff] [blame] | 687 | |
| 688 | /* The same but comparing a maximum of NUM characters, like strncmp. */ |
| 689 | int 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 | } |