Commit | Line | Data |
---|---|---|
9136cf0c MW |
1 | /* |
2 | * addrfam.c | |
3 | * - address-family specific code | |
4 | */ | |
5 | /* | |
6 | * This file is part of adns, which is | |
7 | * Copyright (C) 1997-2000,2003,2006 Ian Jackson | |
8 | * Copyright (C) 1999-2000,2003,2006 Tony Finch | |
9 | * Copyright (C) 1991 Massachusetts Institute of Technology | |
10 | * (See the file INSTALL for full details.) | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2, or (at your option) | |
15 | * any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; if not, write to the Free Software Foundation, | |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | */ | |
26 | ||
27 | #include <stdlib.h> | |
28 | #include <errno.h> | |
29 | #include <limits.h> | |
30 | #include <unistd.h> | |
b0f1ed64 IJ |
31 | #include <inttypes.h> |
32 | #include <stddef.h> | |
1a9d7092 | 33 | #include <stdbool.h> |
9136cf0c MW |
34 | |
35 | #include <sys/types.h> | |
36 | #include <netdb.h> | |
37 | #include <sys/socket.h> | |
38 | #include <netinet/in.h> | |
39 | #include <arpa/inet.h> | |
b0f1ed64 IJ |
40 | #include <netinet/in.h> |
41 | #include <net/if.h> | |
9136cf0c MW |
42 | |
43 | #include "internal.h" | |
44 | ||
45 | /* | |
6480e2df | 46 | * General address-family operations. |
9136cf0c MW |
47 | */ |
48 | ||
49 | #define SIN(sa) ((struct sockaddr_in *)(sa)) | |
705b9b15 | 50 | #define CSIN(sa) ((const struct sockaddr_in *)(sa)) |
9136cf0c | 51 | |
6480e2df MW |
52 | #define SIN6(sa) ((struct sockaddr_in6 *)(sa)) |
53 | #define CSIN6(sa) ((const struct sockaddr_in6 *)(sa)) | |
54 | ||
55 | /* This gadget (thanks, Richard Kettlewell) makes sure that we handle the | |
56 | * same set of address families in each switch. */ | |
57 | #define AF_CASES(pre) \ | |
58 | case AF_INET: goto pre##_inet; \ | |
59 | case AF_INET6: goto pre##_inet6 | |
60 | ||
61 | static void unknown_af(int af) { | |
62 | fprintf(stderr, "ADNS INTERNAL: unknown address family %d\n", af); | |
63 | abort(); | |
64 | } | |
9136cf0c | 65 | |
6480e2df | 66 | int adns__af_supported_p(int af) |
705b9b15 | 67 | { |
6480e2df MW |
68 | switch (af) { |
69 | AF_CASES(af); | |
70 | af_inet: af_inet6: return 1; | |
71 | default: return 0; | |
72 | } | |
705b9b15 MW |
73 | } |
74 | ||
6480e2df MW |
75 | int adns__sockaddr_equal_p(const struct sockaddr *sa, |
76 | const struct sockaddr *sb) | |
77 | { | |
78 | if (sa->sa_family != sb->sa_family) return 0; | |
79 | switch (sa->sa_family) { | |
80 | AF_CASES(af); | |
81 | af_inet: { | |
82 | const struct sockaddr_in *sina = CSIN(sa), *sinb = CSIN(sb); | |
83 | return (sina->sin_addr.s_addr == sinb->sin_addr.s_addr && | |
84 | sina->sin_port == sinb->sin_port); | |
85 | } | |
86 | af_inet6: { | |
87 | /* Don't check the flowlabel. That's apparently useful for routing | |
88 | * performance, but doesn't affect the address in any important | |
89 | * respect. | |
90 | */ | |
91 | const struct sockaddr_in6 *sin6a = CSIN6(sa), *sin6b = CSIN6(sb); | |
92 | return (memcmp(sin6a->sin6_addr.s6_addr, | |
93 | sin6b->sin6_addr.s6_addr, | |
94 | sizeof(sin6a->sin6_addr.s6_addr)) == 0 && | |
95 | sin6a->sin6_port == sin6b->sin6_port && | |
96 | sin6a->sin6_scope_id == sin6b->sin6_scope_id); | |
97 | } | |
98 | default: | |
99 | unknown_af(sa->sa_family); | |
100 | return -1; | |
101 | } | |
102 | } | |
9136cf0c | 103 | |
6480e2df | 104 | int adns__gen_pton(const char *p, int *af_r, union gen_addr *addr_r) |
9136cf0c | 105 | { |
6480e2df MW |
106 | static const int aflist[] = { AF_INET6, AF_INET }; |
107 | int i, rc; | |
9136cf0c | 108 | |
6480e2df MW |
109 | for (i = 0; i < sizeof(aflist)/sizeof(*aflist); i++) { |
110 | rc = inet_pton(aflist[i], p, addr_r); | |
111 | assert(rc >= 0); | |
112 | if (rc) { *af_r = aflist[i]; return 1; } | |
113 | } | |
114 | return 0; | |
9136cf0c MW |
115 | } |
116 | ||
6480e2df | 117 | int adns__addr_width(int af) |
8a53cf7f | 118 | { |
6480e2df MW |
119 | switch (af) { |
120 | AF_CASES(af); | |
121 | af_inet: return 32; | |
122 | af_inet6: return 128; | |
123 | default: unknown_af(af); return -1; | |
8a53cf7f | 124 | } |
8a53cf7f MW |
125 | } |
126 | ||
6480e2df | 127 | void adns__prefix_mask(int af, int len, union gen_addr *mask_r) |
8a53cf7f | 128 | { |
6480e2df MW |
129 | switch (af) { |
130 | AF_CASES(af); | |
131 | af_inet: | |
132 | assert(len <= 32); | |
133 | mask_r->v4.s_addr = htonl(!len ? 0 : 0xffffffff << (32 - len)); | |
134 | break; | |
135 | af_inet6: { | |
136 | int i = len/8, j = len%8; | |
137 | unsigned char *m = mask_r->v6.s6_addr; | |
138 | ||
139 | assert(len <= 128); | |
140 | memset(m, 0xff, i); | |
141 | if (j) m[i++] = (0xff << (8-j)) & 0xff; | |
142 | memset(m+i, 0, 16-i); | |
143 | } break; | |
144 | default: | |
145 | unknown_af(af); | |
146 | break; | |
147 | } | |
8a53cf7f MW |
148 | } |
149 | ||
6480e2df | 150 | int adns__guess_prefix_length(int af, const union gen_addr *addr) |
8a53cf7f | 151 | { |
6480e2df MW |
152 | switch (af) { |
153 | AF_CASES(af); | |
154 | af_inet: { | |
155 | unsigned a = (ntohl(addr->v4.s_addr) >> 24) & 0xff; | |
156 | ||
157 | if (a < 128) return 8; | |
158 | else if (a < 192) return 16; | |
159 | else if (a < 224) return 24; | |
160 | else return -1; | |
161 | } break; | |
162 | af_inet6: | |
163 | return 64; | |
164 | default: | |
165 | unknown_af(af); | |
166 | return -1; | |
167 | } | |
168 | } | |
8a53cf7f | 169 | |
6480e2df MW |
170 | int adns__addr_match_p(int addraf, const union gen_addr *addr, |
171 | int netaf, const union gen_addr *base, | |
172 | const union gen_addr *mask) | |
173 | { | |
174 | if (addraf != netaf) return 0; | |
175 | switch (addraf) { | |
176 | AF_CASES(af); | |
177 | af_inet: | |
178 | return (addr->v4.s_addr & mask->v4.s_addr) == base->v4.s_addr; | |
179 | af_inet6: { | |
180 | int i; | |
181 | const char *a = addr->v6.s6_addr; | |
182 | const char *b = base->v6.s6_addr; | |
183 | const char *m = mask->v6.s6_addr; | |
184 | ||
185 | for (i = 0; i < 16; i++) | |
186 | if ((a[i] & m[i]) != b[i]) return 0; | |
187 | return 1; | |
188 | } break; | |
189 | default: | |
190 | unknown_af(addraf); | |
191 | return -1; | |
8a53cf7f | 192 | } |
8a53cf7f MW |
193 | } |
194 | ||
6480e2df MW |
195 | const void *adns__sockaddr_to_inaddr(const struct sockaddr *sa) |
196 | { | |
197 | switch (sa->sa_family) { | |
198 | AF_CASES(af); | |
199 | af_inet: return &CSIN(sa)->sin_addr; | |
200 | af_inet6: return &CSIN6(sa)->sin6_addr; | |
201 | default: unknown_af(sa->sa_family); return 0; | |
202 | } | |
203 | } | |
9136cf0c MW |
204 | |
205 | /* | |
504877d1 IJ |
206 | * addr2text and text2addr |
207 | */ | |
208 | ||
b0f1ed64 IJ |
209 | #define ADDRFAM_DEBUG |
210 | #ifdef ADDRFAM_DEBUG | |
211 | # define af_debug(fmt,...) \ | |
212 | (fprintf(stderr, "%s: " fmt "\n", __func__, __VA_ARGS__)) | |
213 | #else | |
214 | # define af_debug(fmt,...) ((void)("" fmt "", __VA_ARGS__)) | |
215 | #endif | |
216 | ||
1a9d7092 IJ |
217 | static bool addrtext_our_errno(int e) { |
218 | return | |
219 | e==EAFNOSUPPORT || | |
220 | e==EINVAL || | |
221 | e==ENOSPC || | |
222 | e==ENOSYS; | |
223 | } | |
224 | ||
3121b6a6 IJ |
225 | static bool addrtext_scope_use_ifname(const struct sockaddr *sa) { |
226 | const struct in6_addr *in6= &CSIN6(sa)->sin6_addr; | |
227 | return | |
228 | IN6_IS_ADDR_LINKLOCAL(in6) || | |
229 | IN6_IS_ADDR_MC_LINKLOCAL(in6); | |
230 | } | |
231 | ||
b0f1ed64 | 232 | int adns_text2addr(const char *addr, uint16_t port, struct sockaddr *sa, |
504877d1 IJ |
233 | socklen_t *salen /* set if OK or ENOSPC */) { |
234 | int af; | |
235 | char copybuf[INET6_ADDRSTRLEN]; | |
b0f1ed64 IJ |
236 | const char *parse=addr; |
237 | const char *scopestr=0; | |
238 | socklen_t needlen; | |
239 | void *dst; | |
240 | uint16_t *portp; | |
241 | ||
242 | #define INVAL(how) do{ \ | |
243 | af_debug("invalid: %s: `%s'", how, addr); \ | |
244 | return EINVAL; \ | |
245 | }while(0) | |
504877d1 | 246 | |
b0f1ed64 IJ |
247 | #define AFCORE(INETx,SINx,sinx) \ |
248 | af= AF_##INETx; \ | |
249 | dst = &SINx(sa)->sinx##_addr; \ | |
250 | portp = &SINx(sa)->sinx##_port; \ | |
251 | needlen= sizeof(*SINx(sa)); | |
252 | ||
62c62d61 IJ |
253 | if (!strchr(addr, ':')) { /* INET */ |
254 | ||
b0f1ed64 IJ |
255 | AFCORE(INET,SIN,sin); |
256 | ||
257 | } else { /* INET6 */ | |
258 | ||
259 | AFCORE(INET6,SIN6,sin6); | |
260 | ||
261 | const char *percent= strchr(addr, '%'); | |
504877d1 IJ |
262 | if (percent) { |
263 | ptrdiff_t lhslen = percent - addr; | |
b0f1ed64 IJ |
264 | if (lhslen >= INET6_ADDRSTRLEN) INVAL("scoped addr lhs too long"); |
265 | memcpy(copybuf, addr, lhslen); | |
266 | copybuf[lhslen]= 0; | |
504877d1 | 267 | |
b0f1ed64 IJ |
268 | parse= copybuf; |
269 | scopestr= percent+1; | |
504877d1 | 270 | |
b0f1ed64 IJ |
271 | af_debug("will parse scoped address `%s' %% `%s'", parse, scopestr); |
272 | } | |
504877d1 | 273 | |
504877d1 IJ |
274 | } |
275 | ||
62c62d61 IJ |
276 | #undef AFCORE |
277 | ||
b0f1ed64 IJ |
278 | if (*salen < needlen) { |
279 | *salen = needlen; | |
280 | return ENOSPC; | |
281 | } | |
282 | *salen = needlen; | |
283 | ||
284 | memset(sa, 0, needlen); | |
76af9c49 IJ |
285 | |
286 | sa->sa_family= af; | |
b0f1ed64 IJ |
287 | *portp = htons(port); |
288 | ||
289 | int r= inet_pton(af,parse,dst); | |
290 | if (!r) INVAL("inet_pton rejected"); | |
291 | if (r<0) { | |
292 | af_debug("inet_pton failed on `%s'", parse); | |
293 | return errno; | |
294 | } | |
295 | ||
296 | if (scopestr) { | |
297 | errno=0; | |
298 | char *ep; | |
299 | unsigned long scope= strtoul(scopestr,&ep,10); | |
300 | if (errno==ERANGE) INVAL("numeric scope id too large for unsigned long"); | |
301 | assert(!errno); | |
302 | if (!*ep) { | |
303 | if (scope > ~(uint32_t)0) | |
304 | INVAL("numeric scope id too large for uint32_t"); | |
305 | } else { /* !!*ep */ | |
3121b6a6 | 306 | if (!addrtext_scope_use_ifname(sa)) { |
b0f1ed64 IJ |
307 | af_debug("cannot convert non-numeric scope" |
308 | " in non-link-local addr `%s'", addr); | |
309 | return ENOSYS; | |
310 | } | |
311 | errno= 0; | |
312 | scope= if_nametoindex(scopestr); | |
313 | if (!scope) { | |
314 | /* RFC3493 says "No errors are defined". It's not clear | |
315 | * whether that is supposed to mean if_nametoindex "can't | |
316 | * fail" (other than by the supplied name not being that of an | |
317 | * interface) which seems unrealistic, or that it conflates | |
318 | * all its errors together by failing to set errno, or simply | |
319 | * that they didn't bother to document the errors. | |
320 | * | |
321 | * glibc, FreeBSD and OpenBSD all set errno (to ENXIO when | |
322 | * appropriate). See Debian bug #749349. | |
323 | * | |
324 | * We attempt to deal with this by clearing errno to start | |
325 | * with, and then perhaps mapping the results. */ | |
326 | af_debug("if_nametoindex rejected scope name (errno=%s)", | |
327 | strerror(errno)); | |
328 | if (errno==0) { | |
329 | return ENXIO; | |
1a9d7092 | 330 | } else if (addrtext_our_errno(errno)) { |
b0f1ed64 IJ |
331 | /* we use these for other purposes, urgh. */ |
332 | perror("adns: adns_text2addr: if_nametoindex" | |
333 | " failed with unexpected error"); | |
334 | return EIO; | |
335 | } else { | |
336 | return errno; | |
337 | } | |
338 | } else { /* ix>0 */ | |
339 | if (scope > ~(uint32_t)0) { | |
340 | fprintf(stderr,"adns: adns_text2addr: if_nametoindex" | |
02fd91f8 | 341 | " returned an interface index >=2^32 which will not fit" |
b0f1ed64 IJ |
342 | " in sockaddr_in6.sin6_scope_id"); |
343 | return EIO; | |
344 | } | |
345 | } | |
346 | } /* else; !!*ep */ | |
347 | ||
76af9c49 | 348 | SIN6(sa)->sin6_scope_id= scope; |
b0f1ed64 IJ |
349 | } /* if (scopestr) */ |
350 | ||
351 | return 0; | |
352 | } | |
504877d1 | 353 | |
bfd4cc54 IJ |
354 | int adns_addr2text(const struct sockaddr *sa, |
355 | char *addr_buffer, int *addr_buflen, | |
356 | int *port_r) { | |
357 | const void *src; | |
358 | int port; | |
359 | ||
360 | if (*addr_buflen < ADNS_ADDR2TEXT_BUFLEN) { | |
361 | *addr_buflen = ADNS_ADDR2TEXT_BUFLEN; | |
362 | return ENOSPC; | |
363 | } | |
364 | ||
365 | switch (sa->sa_family) { | |
366 | AF_CASES(af); | |
367 | af_inet: src= &CSIN(sa)->sin_addr; port= CSIN(sa)->sin_port; break; | |
368 | af_inet6: src= &CSIN6(sa)->sin6_addr; port= CSIN6(sa)->sin6_port; break; | |
369 | default: return EAFNOSUPPORT; | |
370 | } | |
371 | ||
372 | const char *ok= inet_ntop(sa->sa_family, src, addr_buffer, *addr_buflen); | |
373 | assert(ok); | |
374 | ||
375 | if (sa->sa_family == AF_INET6) { | |
376 | uint32_t scope = CSIN6(sa)->sin6_scope_id; | |
377 | if (scope) { | |
bfd4cc54 IJ |
378 | int scopeoffset = strlen(addr_buffer); |
379 | int remain = *addr_buflen - scopeoffset; | |
1a9d7092 IJ |
380 | char *scopeptr = addr_buffer + scopeoffset; |
381 | assert(remain >= IF_NAMESIZE+1/*%*/); | |
382 | *scopeptr++= '%'; remain--; | |
3121b6a6 IJ |
383 | bool parsedname = 0; |
384 | if (scope <= UINT_MAX /* so we can pass it to if_indextoname */ | |
385 | && addrtext_scope_use_ifname(sa)) { | |
386 | parsedname = if_indextoname(scope, scopeptr); | |
387 | if (!parsedname) { | |
388 | if (errno==ENXIO) { | |
389 | /* fair enough, show it as a number then */ | |
390 | } else if (addrtext_our_errno(errno)) { | |
391 | /* we use these for other purposes, urgh. */ | |
392 | perror("adns: adns_addr2text: if_indextoname" | |
393 | " failed with unexpected error"); | |
394 | return EIO; | |
395 | } else { | |
396 | return errno; | |
397 | } | |
1a9d7092 | 398 | } |
3121b6a6 IJ |
399 | } |
400 | if (!parsedname) { | |
1a9d7092 IJ |
401 | int r = snprintf(scopeptr, remain, |
402 | "%"PRIu32"", scope); | |
403 | assert(r < *addr_buflen - scopeoffset); | |
404 | } | |
bfd4cc54 IJ |
405 | af_debug("printed scoped address `%s'", addr_buffer); |
406 | } | |
407 | } | |
408 | ||
409 | if (port_r) *port_r= ntohs(port); | |
410 | return 0; | |
411 | } | |
412 | ||
504877d1 | 413 | /* |
6480e2df | 414 | * Reverse-domain parsing and construction. |
9136cf0c MW |
415 | */ |
416 | ||
6480e2df MW |
417 | int adns__make_reverse_domain(const struct sockaddr *sa, |
418 | const char *zone, | |
419 | char **buf_io, size_t bufsz, | |
420 | char **buf_free_r) | |
421 | { | |
422 | size_t req; | |
423 | char *p; | |
424 | unsigned c, y; | |
425 | unsigned long aa; | |
426 | const unsigned char *ap; | |
427 | int i, j; | |
9136cf0c | 428 | |
6480e2df MW |
429 | switch (sa->sa_family) { |
430 | AF_CASES(af); | |
431 | af_inet: | |
432 | req = 4 * 4; | |
433 | if (!zone) zone = "in-addr.arpa"; | |
434 | break; | |
435 | af_inet6: | |
436 | req = 2 * 32; | |
437 | if (!zone) zone = "ip6.arpa"; | |
438 | break; | |
439 | default: | |
440 | return ENOSYS; | |
441 | } | |
9136cf0c | 442 | |
6480e2df MW |
443 | req += strlen(zone) + 1; |
444 | if (req <= bufsz) | |
445 | p = *buf_io; | |
446 | else { | |
447 | p = malloc(req); if (!p) return errno; | |
448 | *buf_free_r = p; | |
449 | } | |
705b9b15 | 450 | |
6480e2df MW |
451 | *buf_io = p; |
452 | switch (sa->sa_family) { | |
453 | AF_CASES(bf); | |
454 | bf_inet: | |
455 | aa = ntohl(CSIN(sa)->sin_addr.s_addr); | |
456 | for (i = 0; i < 4; i++) { | |
457 | p += sprintf(p, "%d", (int)(aa & 0xff)); | |
458 | *p++ = '.'; | |
459 | aa >>= 8; | |
460 | } | |
461 | break; | |
462 | bf_inet6: | |
463 | ap = CSIN6(sa)->sin6_addr.s6_addr + 16; | |
464 | for (i = 0; i < 16; i++) { | |
465 | c = *--ap; | |
466 | for (j = 0; j < 2; j++) { | |
467 | y = c & 0xf; | |
468 | if (y < 10) *p++ = y + '0'; | |
469 | else *p++ = y - 10 + 'a'; | |
470 | c >>= 4; | |
471 | *p++ = '.'; | |
472 | } | |
473 | } | |
474 | break; | |
475 | default: | |
476 | unknown_af(sa->sa_family); | |
477 | } | |
9136cf0c | 478 | |
6480e2df MW |
479 | strcpy(p, zone); |
480 | return 0; | |
9136cf0c MW |
481 | } |
482 | ||
9136cf0c | 483 | |
6480e2df | 484 | static int inet_rev_parsecomp(const char *p, size_t n) |
9136cf0c | 485 | { |
6480e2df MW |
486 | int i = 0; |
487 | if (n > 3) return -1; | |
9136cf0c | 488 | |
6480e2df MW |
489 | while (n--) { |
490 | if ('0' <= *p && *p <= '9') i = 10*i + *p++ - '0'; | |
491 | else return -1; | |
492 | } | |
493 | return i; | |
494 | } | |
495 | ||
496 | static void inet_rev_mkaddr(union gen_addr *addr, const byte *ipv) | |
497 | { | |
498 | addr->v4.s_addr = htonl((ipv[3]<<24) | (ipv[2]<<16) | | |
499 | (ipv[1]<<8) | (ipv[0])); | |
9136cf0c MW |
500 | } |
501 | ||
8a53cf7f MW |
502 | static int inet6_rev_parsecomp(const char *p, size_t n) |
503 | { | |
504 | if (n != 1) return -1; | |
505 | else if ('0' <= *p && *p <= '9') return *p - '0'; | |
506 | else if ('a' <= *p && *p <= 'f') return *p - 'a' + 10; | |
507 | else if ('A' <= *p && *p <= 'F') return *p - 'a' + 10; | |
508 | else return -1; | |
509 | } | |
510 | ||
511 | static void inet6_rev_mkaddr(union gen_addr *addr, const byte *ipv) | |
512 | { | |
513 | unsigned char *a = addr->v6.s6_addr; | |
514 | int i; | |
515 | ||
516 | for (i = 0; i < 16; i++) | |
517 | a[i] = (ipv[31-2*i] << 4) | (ipv[30-2*i] << 0); | |
518 | } | |
519 | ||
6480e2df MW |
520 | static const struct revparse_domain { |
521 | int af; /* address family */ | |
522 | int nrevlab; /* n of reverse-address labels */ | |
523 | adns_rrtype rrtype; /* forward-lookup type */ | |
524 | ||
525 | int (*rev_parsecomp)(const char *p, size_t n); | |
526 | /* parse a single component from a label; return the integer value, or -1 | |
527 | * if it was unintelligible. | |
528 | */ | |
529 | ||
530 | void (*rev_mkaddr)(union gen_addr *addr, const byte *ipv); | |
531 | /* write out the parsed address from a vector of parsed components */ | |
532 | ||
533 | const char *const tail[3]; /* tail label names */ | |
534 | } revparse_domains[NREVDOMAINS] = { | |
535 | { AF_INET, 4, adns_r_a, inet_rev_parsecomp, inet_rev_mkaddr, | |
536 | { DNS_INADDR_ARPA, 0 } }, | |
537 | { AF_INET6, 32, adns_r_aaaa, inet6_rev_parsecomp, inet6_rev_mkaddr, | |
538 | { DNS_IP6_ARPA, 0 } }, | |
539 | }; | |
8a53cf7f | 540 | |
6480e2df MW |
541 | #define REVDOMAIN_MAP(rps, labnum) \ |
542 | ((labnum) ? (rps)->map : (1 << NREVDOMAINS) - 1) | |
543 | ||
544 | int adns__revparse_label(struct revparse_state *rps, int labnum, | |
545 | const char *label, int lablen) | |
546 | { | |
547 | unsigned f = REVDOMAIN_MAP(rps, labnum); | |
548 | const struct revparse_domain *rpd; | |
549 | const char *tp; | |
550 | unsigned d; | |
551 | int i, ac; | |
552 | ||
553 | for (rpd=revparse_domains, i=0, d=1; i<NREVDOMAINS; rpd++, i++, d <<= 1) { | |
554 | if (!(f & d)) continue; | |
555 | if (labnum >= rpd->nrevlab) { | |
556 | tp = rpd->tail[labnum - rpd->nrevlab]; | |
557 | if (!tp || strncmp(label, tp, lablen) != 0 || tp[lablen]) | |
558 | goto mismatch; | |
559 | } else { | |
560 | ac = rpd->rev_parsecomp(label, lablen); | |
561 | if (ac < 0) goto mismatch; | |
562 | assert(labnum < sizeof(rps->ipv[i])); | |
563 | rps->ipv[i][labnum] = ac; | |
8a53cf7f | 564 | } |
6480e2df MW |
565 | continue; |
566 | ||
567 | mismatch: | |
568 | f &= ~d; | |
569 | if (!f) return -1; | |
8a53cf7f | 570 | } |
6480e2df MW |
571 | |
572 | rps->map = f; | |
573 | return 0; | |
8a53cf7f MW |
574 | } |
575 | ||
6480e2df MW |
576 | int adns__revparse_done(struct revparse_state *rps, int nlabels, |
577 | adns_rrtype *rrtype_r, struct af_addr *addr_r) | |
578 | { | |
579 | unsigned f = REVDOMAIN_MAP(rps, nlabels); | |
580 | const struct revparse_domain *rpd; | |
581 | unsigned d; | |
582 | int i, found = -1; | |
583 | ||
584 | for (rpd=revparse_domains, i=0, d=1; i<NREVDOMAINS; rpd++, i++, d <<= 1) { | |
585 | if (!(f & d)) continue; | |
586 | if (nlabels >= rpd->nrevlab && !rpd->tail[nlabels - rpd->nrevlab]) | |
587 | { found = i; continue; } | |
588 | f &= ~d; | |
589 | if (!f) return -1; | |
590 | } | |
591 | assert(found >= 0); assert(f == (1 << found)); | |
592 | ||
593 | rpd = &revparse_domains[found]; | |
594 | *rrtype_r = rpd->rrtype; | |
595 | addr_r->af = rpd->af; | |
596 | rpd->rev_mkaddr(&addr_r->addr, rps->ipv[found]); | |
597 | return 0; | |
598 | } |