Commit | Line | Data |
---|---|---|
ddf8d57f 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> | |
b3209391 IJ |
31 | #include <inttypes.h> |
32 | #include <stddef.h> | |
33 | #include <stdbool.h> | |
ddf8d57f MW |
34 | |
35 | #include <sys/types.h> | |
36 | #include <sys/socket.h> | |
37 | #include <netinet/in.h> | |
38 | #include <arpa/inet.h> | |
b3209391 | 39 | #include <net/if.h> |
ddf8d57f MW |
40 | |
41 | #include "internal.h" | |
42 | ||
43 | /* | |
44 | * General address-family operations. | |
45 | */ | |
46 | ||
279af118 IJ |
47 | #define SIN(cnst, sa) ((void)(sa)->sa_family, (cnst struct sockaddr_in *)(sa)) |
48 | #define SIN6(cnst, sa) ((void)(sa)->sa_family, (cnst struct sockaddr_in6 *)(sa)) | |
ddf8d57f | 49 | |
b190a826 | 50 | static void unknown_af(int af) NONRETURNING; |
ddf8d57f MW |
51 | static void unknown_af(int af) { |
52 | fprintf(stderr, "ADNS INTERNAL: unknown address family %d\n", af); | |
53 | abort(); | |
54 | } | |
55 | ||
63240761 IJ |
56 | /* |
57 | * SOCKADDR_IN_IN6(CNST, struct sockaddr *sa, SIN, { | |
58 | * // struct sockaddr_in *const SIN; // implicitly | |
59 | * code for inet; | |
60 | * }, { | |
61 | * // struct sockaddr_in6 *const SIN6; // implicitly | |
62 | * code for inet6; | |
63 | * }) | |
64 | * | |
65 | * SOCKADDR_IN_IN6_PAIR(CNST, struct sockaddr *sa, SINA, | |
66 | * struct sockaddr *sb, SINB, { | |
67 | * // struct sockaddr_in *const SINA; // implicitly | |
68 | * // struct sockaddr_in *const SINB; // implicitly | |
69 | * code for inet; | |
70 | * },{ | |
71 | * // struct sockaddr_in6 *const SINA6; // implicitly | |
72 | * // struct sockaddr_in6 *const SINB6; // implicitly | |
73 | * code for inet6; | |
74 | * }); | |
75 | * | |
76 | * SOCKADDR_IN_IN6_OTHER(CNST, struct sockaddr *sa, SIN, { in }, { in6 }, { | |
77 | * code for other address family | |
78 | * }) | |
79 | * | |
80 | * AF_IN_IN6_OTHER(af, { in }, { in6 }, { other }) | |
81 | * | |
82 | * Executes the first or second block according to the AF in sa. CNST | |
83 | * may be `const' or empty. For _PAIR, sa and sb must be same AF. | |
84 | * | |
85 | * All except _OTHER handle unknown AFs with unknown_af. | |
86 | * | |
87 | * Code blocks may not contain , outside parens. | |
88 | */ | |
89 | #define AF_IN_IN6_OTHER(af, for_inet, for_inet6, other) \ | |
90 | if ((af) == AF_INET) { \ | |
91 | for_inet \ | |
92 | } else if ((af) == AF_INET6) { \ | |
93 | for_inet6 \ | |
94 | } else { \ | |
95 | other \ | |
96 | } | |
97 | #define SOCKADDR_IN_IN6_OTHER(cnst, sa, sin, for_inet, for_inet6, other) \ | |
279af118 IJ |
98 | AF_IN_IN6_OTHER((sa)->sa_family, { \ |
99 | cnst struct sockaddr_in *const sin = SIN(cnst,(sa)); \ | |
63240761 IJ |
100 | for_inet \ |
101 | }, { \ | |
279af118 | 102 | cnst struct sockaddr_in6 *const sin##6 = SIN6(cnst,(sa)); \ |
63240761 IJ |
103 | for_inet6 \ |
104 | }, \ | |
105 | other \ | |
106 | ) | |
107 | #define SOCKADDR_IN_IN6(cnst, sa, sin, for_inet, for_inet6) \ | |
108 | SOCKADDR_IN_IN6_OTHER(cnst, sa, sin, for_inet, for_inet6, { \ | |
109 | unknown_af((sa)->sa_family); \ | |
110 | }) | |
111 | #define SOCKADDR_IN_IN6_PAIR(cnst, sa, sina, sb, sinb, for_inet, for_inet6) \ | |
112 | do{ \ | |
113 | assert((sa)->sa_family == (sb)->sa_family); \ | |
114 | SOCKADDR_IN_IN6(cnst, sa, sina, { \ | |
279af118 | 115 | cnst struct sockaddr_in *const sinb = SIN(cnst,(sb)); \ |
63240761 | 116 | for_inet \ |
279af118 IJ |
117 | }, { \ |
118 | cnst struct sockaddr_in6 *const sinb##6 = SIN6(cnst,(sb)); \ | |
63240761 IJ |
119 | for_inet6 \ |
120 | }); \ | |
121 | }while(0) | |
122 | ||
9ee3a72c IJ |
123 | int adns__addrs_equal_raw(const struct sockaddr *a, |
124 | int bf, const void *b) { | |
125 | if (a->sa_family != bf) return 0; | |
63240761 IJ |
126 | |
127 | SOCKADDR_IN_IN6(const, a, sin, { | |
128 | return sin->sin_addr.s_addr == ((const struct in_addr*)b)->s_addr; | |
129 | }, { | |
130 | return !memcmp(&sin6->sin6_addr, b, sizeof(struct in6_addr)); | |
131 | }); | |
ddf8d57f MW |
132 | } |
133 | ||
9ee3a72c IJ |
134 | int adns__addrs_equal(const adns_sockaddr *a, const adns_sockaddr *b) { |
135 | return adns__addrs_equal_raw(&a->sa, b->sa.sa_family, | |
136 | adns__sockaddr_addr(&b->sa)); | |
137 | } | |
138 | ||
139 | int adns__sockaddrs_equal(const struct sockaddr *sa, | |
140 | const struct sockaddr *sb) { | |
141 | if (!adns__addrs_equal_raw(sa, sb->sa_family, adns__sockaddr_addr(sb))) | |
142 | return 0; | |
63240761 IJ |
143 | SOCKADDR_IN_IN6_PAIR(const, sa, sina, sb, sinb, { |
144 | return sina->sin_port == sinb->sin_port; | |
145 | }, { | |
146 | return sina6->sin6_port == sinb6->sin6_port && | |
147 | sina6->sin6_scope_id == sinb6->sin6_scope_id; | |
148 | }); | |
ddf8d57f MW |
149 | } |
150 | ||
151 | int adns__addr_width(int af) { | |
63240761 IJ |
152 | AF_IN_IN6_OTHER(af, { |
153 | return 32; | |
154 | }, { | |
155 | return 128; | |
156 | }, { | |
157 | unknown_af(af); | |
158 | }); | |
ddf8d57f MW |
159 | } |
160 | ||
9ee3a72c | 161 | void adns__prefix_mask(adns_sockaddr *sa, int len) { |
63240761 IJ |
162 | SOCKADDR_IN_IN6(, &sa->sa, sin, { |
163 | assert(len <= 32); | |
164 | sin->sin_addr.s_addr= htonl(!len ? 0 : 0xffffffff << (32-len)); | |
165 | }, { | |
166 | int i= len/8; | |
167 | int j= len%8; | |
168 | unsigned char *m= sin6->sin6_addr.s6_addr; | |
169 | assert(len <= 128); | |
170 | memset(m, 0xff, i); | |
171 | if (j) m[i++]= (0xff << (8-j)) & 0xff; | |
172 | memset(m+i, 0, 16-i); | |
173 | }); | |
ddf8d57f MW |
174 | } |
175 | ||
9ee3a72c | 176 | int adns__guess_prefix_length(const adns_sockaddr *sa) { |
63240761 IJ |
177 | SOCKADDR_IN_IN6(const, &sa->sa, sin, { |
178 | unsigned a= (ntohl(sin->sin_addr.s_addr) >> 24) & 0xff; | |
179 | if (a < 128) return 8; | |
180 | else if (a < 192) return 16; | |
181 | else if (a < 224) return 24; | |
182 | else return -1; | |
183 | }, { | |
184 | (void)sin6; | |
185 | return 64; | |
186 | }); | |
ddf8d57f MW |
187 | } |
188 | ||
9ee3a72c IJ |
189 | int adns__addr_matches(int af, const void *addr, |
190 | const adns_sockaddr *base, const adns_sockaddr *mask) | |
ddf8d57f | 191 | { |
9ee3a72c | 192 | if (af != base->sa.sa_family) return 0; |
63240761 IJ |
193 | SOCKADDR_IN_IN6_PAIR(const, &base->sa, sbase, &mask->sa, smask, { |
194 | const struct in_addr *v4 = addr; | |
195 | return (v4->s_addr & smask->sin_addr.s_addr) | |
196 | == sbase->sin_addr.s_addr; | |
197 | }, { | |
198 | int i; | |
199 | const char *a= addr; | |
200 | const char *b= sbase6->sin6_addr.s6_addr; | |
201 | const char *m= smask6->sin6_addr.s6_addr; | |
202 | for (i = 0; i < 16; i++) | |
203 | if ((a[i] & m[i]) != b[i]) return 0; | |
204 | return 1; | |
205 | }); | |
ddf8d57f MW |
206 | } |
207 | ||
9ee3a72c | 208 | const void *adns__sockaddr_addr(const struct sockaddr *sa) { |
63240761 IJ |
209 | SOCKADDR_IN_IN6(const, sa, sin, { |
210 | return &sin->sin_addr; | |
211 | }, { | |
212 | return &sin6->sin6_addr; | |
213 | }); | |
ddf8d57f MW |
214 | } |
215 | ||
9ee3a72c | 216 | void adns__addr_inject(const void *a, adns_sockaddr *sa) { |
63240761 IJ |
217 | SOCKADDR_IN_IN6( , &sa->sa, sin, { |
218 | memcpy(&sin->sin_addr, a, sizeof(sin->sin_addr)); | |
219 | }, { | |
220 | memcpy(&sin6->sin6_addr, a, sizeof(sin6->sin6_addr)); | |
221 | }); | |
ddf8d57f | 222 | } |
b3209391 IJ |
223 | |
224 | /* | |
225 | * addr2text and text2addr | |
226 | */ | |
227 | ||
228 | #define ADDRFAM_DEBUG | |
229 | #ifdef ADDRFAM_DEBUG | |
230 | static void af_debug_func(const char *fmt, ...) { | |
231 | int esave= errno; | |
232 | va_list al; | |
233 | va_start(al,fmt); | |
234 | vfprintf(stderr,fmt,al); | |
235 | va_end(al); | |
236 | errno= esave; | |
237 | } | |
238 | # define af_debug(fmt,...) \ | |
239 | (af_debug_func("%s: " fmt "\n", __func__, __VA_ARGS__)) | |
240 | #else | |
241 | # define af_debug(fmt,...) ((void)("" fmt "", __VA_ARGS__)) | |
242 | #endif | |
243 | ||
244 | static bool addrtext_our_errno(int e) { | |
245 | return | |
246 | e==EAFNOSUPPORT || | |
247 | e==EINVAL || | |
248 | e==ENOSPC || | |
249 | e==ENOSYS; | |
250 | } | |
251 | ||
252 | static bool addrtext_scope_use_ifname(const struct sockaddr *sa) { | |
279af118 | 253 | const struct in6_addr *in6= &SIN6(const,sa)->sin6_addr; |
b3209391 IJ |
254 | return |
255 | IN6_IS_ADDR_LINKLOCAL(in6) || | |
256 | IN6_IS_ADDR_MC_LINKLOCAL(in6); | |
257 | } | |
258 | ||
259 | int adns_text2addr(const char *text, uint16_t port, adns_queryflags flags, | |
260 | struct sockaddr *sa, socklen_t *salen_io) { | |
261 | int af; | |
262 | char copybuf[INET6_ADDRSTRLEN]; | |
263 | const char *parse=text; | |
264 | const char *scopestr=0; | |
265 | socklen_t needlen; | |
266 | void *dst; | |
267 | uint16_t *portp; | |
268 | ||
269 | #define INVAL(how) do{ \ | |
270 | af_debug("invalid: %s: `%s'", how, text); \ | |
271 | return EINVAL; \ | |
272 | }while(0) | |
273 | ||
274 | #define AFCORE(INETx,SINx,sinx) \ | |
275 | af= AF_##INETx; \ | |
279af118 IJ |
276 | dst = &SINx(,sa)->sinx##_addr; \ |
277 | portp = &SINx(,sa)->sinx##_port; \ | |
278 | needlen= sizeof(*SINx(,sa)); | |
b3209391 IJ |
279 | |
280 | if (!strchr(text, ':')) { /* INET */ | |
281 | ||
282 | AFCORE(INET,SIN,sin); | |
283 | ||
284 | } else { /* INET6 */ | |
285 | ||
286 | AFCORE(INET6,SIN6,sin6); | |
287 | ||
288 | const char *percent= strchr(text, '%'); | |
289 | if (percent) { | |
290 | ptrdiff_t lhslen = percent - text; | |
291 | if (lhslen >= INET6_ADDRSTRLEN) INVAL("scoped addr lhs too long"); | |
292 | memcpy(copybuf, text, lhslen); | |
293 | copybuf[lhslen]= 0; | |
294 | ||
295 | parse= copybuf; | |
296 | scopestr= percent+1; | |
297 | ||
298 | af_debug("will parse scoped addr `%s' %% `%s'", parse, scopestr); | |
299 | } | |
300 | ||
301 | } | |
302 | ||
303 | #undef AFCORE | |
304 | ||
305 | if (scopestr && (flags & adns_qf_addrlit_scope_forbid)) | |
306 | INVAL("scoped addr but _scope_forbid"); | |
307 | ||
308 | if (*salen_io < needlen) { | |
309 | *salen_io = needlen; | |
310 | return ENOSPC; | |
311 | } | |
312 | ||
313 | memset(sa, 0, needlen); | |
314 | ||
315 | sa->sa_family= af; | |
316 | *portp = htons(port); | |
317 | ||
318 | if (af == AF_INET && !(flags & adns_qf_addrlit_ipv4_quadonly)) { | |
319 | /* we have to use inet_aton to deal with non-dotted-quad literals */ | |
279af118 | 320 | int r= inet_aton(parse,&SIN(,sa)->sin_addr); |
b3209391 IJ |
321 | if (!r) INVAL("inet_aton rejected"); |
322 | } else { | |
323 | int r= inet_pton(af,parse,dst); | |
324 | if (!r) INVAL("inet_pton rejected"); | |
325 | assert(r>0); | |
326 | } | |
327 | ||
328 | if (scopestr) { | |
329 | errno=0; | |
330 | char *ep; | |
331 | unsigned long scope= strtoul(scopestr,&ep,10); | |
332 | if (errno==ERANGE) INVAL("numeric scope id too large for unsigned long"); | |
333 | assert(!errno); | |
334 | if (!*ep) { | |
335 | if (scope > ~(uint32_t)0) | |
336 | INVAL("numeric scope id too large for uint32_t"); | |
337 | } else { /* !!*ep */ | |
338 | if (flags & adns_qf_addrlit_scope_numeric) | |
339 | INVAL("non-numeric scope but _scope_numeric"); | |
340 | if (!addrtext_scope_use_ifname(sa)) { | |
341 | af_debug("cannot convert non-numeric scope" | |
342 | " in non-link-local addr `%s'", text); | |
343 | return ENOSYS; | |
344 | } | |
345 | errno= 0; | |
346 | scope= if_nametoindex(scopestr); | |
347 | if (!scope) { | |
348 | /* RFC3493 says "No errors are defined". It's not clear | |
349 | * whether that is supposed to mean if_nametoindex "can't | |
350 | * fail" (other than by the supplied name not being that of an | |
351 | * interface) which seems unrealistic, or that it conflates | |
352 | * all its errors together by failing to set errno, or simply | |
353 | * that they didn't bother to document the errors. | |
354 | * | |
355 | * glibc, FreeBSD and OpenBSD all set errno (to ENXIO when | |
356 | * appropriate). See Debian bug #749349. | |
357 | * | |
358 | * We attempt to deal with this by clearing errno to start | |
359 | * with, and then perhaps mapping the results. */ | |
360 | af_debug("if_nametoindex rejected scope name (errno=%s)", | |
361 | strerror(errno)); | |
362 | if (errno==0) { | |
363 | return ENXIO; | |
364 | } else if (addrtext_our_errno(errno)) { | |
365 | /* we use these for other purposes, urgh. */ | |
366 | perror("adns: adns_text2addr: if_nametoindex" | |
367 | " failed with unexpected error"); | |
368 | return EIO; | |
369 | } else { | |
370 | return errno; | |
371 | } | |
372 | } else { /* ix>0 */ | |
373 | if (scope > ~(uint32_t)0) { | |
374 | fprintf(stderr,"adns: adns_text2addr: if_nametoindex" | |
375 | " returned an interface index >=2^32 which will not fit" | |
376 | " in sockaddr_in6.sin6_scope_id"); | |
377 | return EIO; | |
378 | } | |
379 | } | |
380 | } /* else; !!*ep */ | |
381 | ||
279af118 | 382 | SIN6(,sa)->sin6_scope_id= scope; |
b3209391 IJ |
383 | } /* if (scopestr) */ |
384 | ||
385 | *salen_io = needlen; | |
386 | return 0; | |
387 | } | |
388 | ||
389 | int adns_addr2text(const struct sockaddr *sa, adns_queryflags flags, | |
390 | char *buffer, int *buflen_io, int *port_r) { | |
391 | const void *src; | |
392 | int port; | |
393 | ||
394 | if (*buflen_io < ADNS_ADDR2TEXT_BUFLEN) { | |
395 | *buflen_io = ADNS_ADDR2TEXT_BUFLEN; | |
396 | return ENOSPC; | |
397 | } | |
398 | ||
63240761 IJ |
399 | SOCKADDR_IN_IN6_OTHER(const, sa, sin, { |
400 | src= &sin->sin_addr; port= sin->sin_port; | |
401 | }, { | |
402 | src= &sin6->sin6_addr; port= sin6->sin6_port; | |
403 | }, { | |
404 | return EAFNOSUPPORT; | |
405 | }); | |
b3209391 IJ |
406 | |
407 | const char *ok= inet_ntop(sa->sa_family, src, buffer, *buflen_io); | |
408 | assert(ok); | |
409 | ||
410 | if (sa->sa_family == AF_INET6) { | |
279af118 | 411 | uint32_t scope = SIN6(const,sa)->sin6_scope_id; |
b3209391 IJ |
412 | if (scope) { |
413 | if (flags & adns_qf_addrlit_scope_forbid) | |
414 | return EINVAL; | |
415 | int scopeoffset = strlen(buffer); | |
416 | int remain = *buflen_io - scopeoffset; | |
417 | char *scopeptr = buffer + scopeoffset; | |
418 | assert(remain >= IF_NAMESIZE+1/*%*/); | |
419 | *scopeptr++= '%'; remain--; | |
420 | bool parsedname = 0; | |
eca4e30a MW |
421 | af_debug("will print scoped addr `%.*s' %% %"PRIu32"", |
422 | scopeoffset,buffer, scope); | |
b3209391 IJ |
423 | if (scope <= UINT_MAX /* so we can pass it to if_indextoname */ |
424 | && !(flags & adns_qf_addrlit_scope_numeric) | |
425 | && addrtext_scope_use_ifname(sa)) { | |
426 | parsedname = if_indextoname(scope, scopeptr); | |
427 | if (!parsedname) { | |
428 | af_debug("if_indextoname rejected scope (errno=%s)", | |
429 | strerror(errno)); | |
430 | if (errno==ENXIO) { | |
431 | /* fair enough, show it as a number then */ | |
432 | } else if (addrtext_our_errno(errno)) { | |
433 | /* we use these for other purposes, urgh. */ | |
434 | perror("adns: adns_addr2text: if_indextoname" | |
435 | " failed with unexpected error"); | |
436 | return EIO; | |
437 | } else { | |
438 | return errno; | |
439 | } | |
440 | } | |
441 | } | |
442 | if (!parsedname) { | |
443 | int r = snprintf(scopeptr, remain, | |
444 | "%"PRIu32"", scope); | |
445 | assert(r < *buflen_io - scopeoffset); | |
446 | } | |
447 | af_debug("printed scoped addr `%s'", buffer); | |
448 | } | |
449 | } | |
450 | ||
451 | if (port_r) *port_r= ntohs(port); | |
452 | return 0; | |
453 | } | |
844e3556 MW |
454 | |
455 | char *adns__sockaddr_ntoa(const struct sockaddr *sa, char *buf) { | |
456 | int err; | |
457 | int len= ADNS_ADDR2TEXT_BUFLEN; | |
458 | ||
459 | err= adns_addr2text(sa, 0, buf, &len, 0); | |
460 | if (err == EIO) | |
461 | err= adns_addr2text(sa, adns_qf_addrlit_scope_numeric, buf, &len, 0); | |
462 | assert(!err); | |
463 | return buf; | |
464 | } | |
60b40c59 MW |
465 | |
466 | /* | |
467 | * Reverse-domain parsing and construction. | |
468 | */ | |
469 | ||
470 | int adns__make_reverse_domain(const struct sockaddr *sa, const char *zone, | |
471 | char **buf_io, size_t bufsz, | |
472 | char **buf_free_r) { | |
473 | size_t req; | |
474 | char *p; | |
475 | unsigned c, y; | |
476 | unsigned long aa; | |
477 | const unsigned char *ap; | |
478 | int i, j; | |
479 | ||
63240761 IJ |
480 | AF_IN_IN6_OTHER(sa->sa_family, { |
481 | req= 4 * 4; | |
482 | if (!zone) zone= "in-addr.arpa"; | |
483 | }, { | |
484 | req = 2 * 32; | |
485 | if (!zone) zone= "ip6.arpa"; | |
486 | }, { | |
487 | return ENOSYS; | |
488 | }); | |
60b40c59 MW |
489 | |
490 | req += strlen(zone) + 1; | |
491 | if (req <= bufsz) | |
492 | p= *buf_io; | |
493 | else { | |
494 | p= malloc(req); if (!p) return errno; | |
495 | *buf_free_r = p; | |
496 | } | |
497 | ||
498 | *buf_io= p; | |
63240761 IJ |
499 | SOCKADDR_IN_IN6(const, sa, sin, { |
500 | aa= ntohl(sin->sin_addr.s_addr); | |
501 | for (i=0; i<4; i++) { | |
502 | p += sprintf(p, "%d", (int)(aa & 0xff)); | |
60b40c59 | 503 | *p++= '.'; |
63240761 | 504 | aa >>= 8; |
60b40c59 | 505 | } |
63240761 IJ |
506 | }, { |
507 | ap= sin6->sin6_addr.s6_addr + 16; | |
508 | for (i=0; i<16; i++) { | |
509 | c= *--ap; | |
510 | for (j=0; j<2; j++) { | |
511 | y= c & 0xf; | |
512 | *p++= (y < 10) ? y + '0' : y - 10 + 'a'; | |
513 | c >>= 4; | |
514 | *p++= '.'; | |
515 | } | |
516 | } | |
517 | }); | |
60b40c59 MW |
518 | |
519 | strcpy(p, zone); | |
520 | return 0; | |
521 | } | |
522 | ||
523 | ||
524 | static int inet_rev_parsecomp(const char *p, size_t n) { | |
525 | int i= 0; | |
526 | if (n > 3) return -1; | |
527 | ||
528 | while (n--) { | |
529 | if ('0' <= *p && *p <= '9') i= 10*i + *p++ - '0'; | |
530 | else return -1; | |
531 | } | |
532 | return i; | |
533 | } | |
534 | ||
9ee3a72c IJ |
535 | static void inet_rev_mkaddr(adns_sockaddr *addr, const byte *ipv) { |
536 | struct in_addr *v4 = &addr->inet.sin_addr; | |
537 | v4->s_addr= htonl((ipv[3]<<24) | (ipv[2]<<16) | | |
538 | (ipv[1]<<8) | (ipv[0])); | |
60b40c59 MW |
539 | } |
540 | ||
541 | static int inet6_rev_parsecomp(const char *p, size_t n) { | |
542 | if (n != 1) return -1; | |
543 | else if ('0' <= *p && *p <= '9') return *p - '0'; | |
544 | else if ('a' <= *p && *p <= 'f') return *p - 'a' + 10; | |
545 | else if ('A' <= *p && *p <= 'F') return *p - 'a' + 10; | |
546 | else return -1; | |
547 | } | |
548 | ||
9ee3a72c IJ |
549 | static void inet6_rev_mkaddr(adns_sockaddr *addr, const byte *ipv) { |
550 | struct in6_addr *v6 = &addr->inet6.sin6_addr; | |
551 | unsigned char *a= v6->s6_addr; | |
60b40c59 MW |
552 | int i; |
553 | ||
554 | for (i=0; i<16; i++) | |
555 | a[i]= (ipv[31-2*i] << 4) | (ipv[30-2*i] << 0); | |
556 | } | |
557 | ||
558 | static const struct revparse_domain { | |
559 | int af; /* address family */ | |
560 | int nrevlab; /* n of reverse-address labels */ | |
561 | adns_rrtype rrtype; /* forward-lookup type */ | |
562 | ||
563 | int (*rev_parsecomp)(const char *p, size_t n); | |
564 | /* parse a single component from a label; return the integer value, or -1 | |
565 | * if it was unintelligible. | |
566 | */ | |
567 | ||
9ee3a72c IJ |
568 | void (*rev_mkaddr)(adns_sockaddr *addr, const byte *ipv); |
569 | /* write out the parsed protocol address from a vector of parsed components */ | |
60b40c59 MW |
570 | |
571 | const char *const tail[3]; /* tail label names */ | |
572 | } revparse_domains[NREVDOMAINS] = { | |
573 | { AF_INET, 4, adns_r_a, inet_rev_parsecomp, inet_rev_mkaddr, | |
574 | { DNS_INADDR_ARPA, 0 } }, | |
575 | { AF_INET6, 32, adns_r_aaaa, inet6_rev_parsecomp, inet6_rev_mkaddr, | |
576 | { DNS_IP6_ARPA, 0 } }, | |
577 | }; | |
578 | ||
579 | #define REVDOMAIN_MAP(rps, labnum) \ | |
580 | ((labnum) ? (rps)->map : (1 << NREVDOMAINS) - 1) | |
581 | ||
582 | int adns__revparse_label(struct revparse_state *rps, int labnum, | |
583 | const char *label, int lablen) { | |
584 | unsigned f= REVDOMAIN_MAP(rps, labnum); | |
585 | const struct revparse_domain *rpd; | |
586 | const char *tp; | |
587 | unsigned d; | |
588 | int i, ac; | |
589 | ||
590 | for (rpd=revparse_domains, i=0, d=1; i<NREVDOMAINS; rpd++, i++, d <<= 1) { | |
591 | if (!(f & d)) continue; | |
592 | if (labnum >= rpd->nrevlab) { | |
593 | tp = rpd->tail[labnum - rpd->nrevlab]; | |
594 | if (!tp || strncmp(label, tp, lablen) != 0 || tp[lablen]) | |
595 | goto mismatch; | |
596 | } else { | |
597 | ac= rpd->rev_parsecomp(label, lablen); | |
598 | if (ac < 0) goto mismatch; | |
599 | assert(labnum < sizeof(rps->ipv[i])); | |
600 | rps->ipv[i][labnum]= ac; | |
601 | } | |
602 | continue; | |
603 | ||
604 | mismatch: | |
605 | f &= ~d; | |
606 | if (!f) return -1; | |
607 | } | |
608 | ||
609 | rps->map= f; | |
610 | return 0; | |
611 | } | |
612 | ||
613 | int adns__revparse_done(struct revparse_state *rps, int nlabels, | |
9ee3a72c | 614 | adns_rrtype *rrtype_r, adns_sockaddr *addr_r) { |
60b40c59 MW |
615 | unsigned f= REVDOMAIN_MAP(rps, nlabels); |
616 | const struct revparse_domain *rpd; | |
617 | unsigned d; | |
618 | int i, found= -1; | |
619 | ||
620 | for (rpd=revparse_domains, i=0, d=1; i<NREVDOMAINS; rpd++, i++, d <<= 1) { | |
621 | if (!(f & d)) continue; | |
622 | if (nlabels >= rpd->nrevlab && !rpd->tail[nlabels - rpd->nrevlab]) | |
623 | { found = i; continue; } | |
624 | f &= ~d; | |
625 | if (!f) return -1; | |
626 | } | |
627 | assert(found >= 0); assert(f == (1 << found)); | |
628 | ||
629 | rpd= &revparse_domains[found]; | |
630 | *rrtype_r= rpd->rrtype; | |
9ee3a72c IJ |
631 | addr_r->sa.sa_family= rpd->af; |
632 | rpd->rev_mkaddr(addr_r, rps->ipv[found]); | |
60b40c59 MW |
633 | return 0; |
634 | } |