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