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