3 * - address-family specific code
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.)
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 3, or (at your option)
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.
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.
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
43 * General address-family operations.
46 #define SIN(cnst, sa) ((void)(sa)->sa_family, (cnst struct sockaddr_in *)(sa))
47 #define SIN6(cnst, sa) ((void)(sa)->sa_family, (cnst struct sockaddr_in6 *)(sa))
49 static void unknown_af(int af) NONRETURNING;
50 static void unknown_af(int af) {
51 fprintf(stderr, "ADNS INTERNAL: unknown address family %d\n", af);
56 * SOCKADDR_IN_IN6(CNST, struct sockaddr *sa, SIN, {
57 * // struct sockaddr_in *const SIN; // implicitly
60 * // struct sockaddr_in6 *const SIN6; // implicitly
64 * SOCKADDR_IN_IN6_PAIR(CNST, struct sockaddr *sa, SINA,
65 * struct sockaddr *sb, SINB, {
66 * // struct sockaddr_in *const SINA; // implicitly
67 * // struct sockaddr_in *const SINB; // implicitly
70 * // struct sockaddr_in6 *const SINA6; // implicitly
71 * // struct sockaddr_in6 *const SINB6; // implicitly
75 * SOCKADDR_IN_IN6_OTHER(CNST, struct sockaddr *sa, SIN, { in }, { in6 }, {
76 * code for other address family
79 * AF_IN_IN6_OTHER(af, { in }, { in6 }, { other })
81 * Executes the first or second block according to the AF in sa. CNST
82 * may be `const' or empty. For _PAIR, sa and sb must be same AF.
84 * All except _OTHER handle unknown AFs with unknown_af.
86 * Code blocks may not contain , outside parens.
88 #define AF_IN_IN6_OTHER(af, for_inet, for_inet6, other) \
89 if ((af) == AF_INET) { \
91 } else if ((af) == AF_INET6) { \
96 #define SOCKADDR_IN_IN6_OTHER(cnst, sa, sin, for_inet, for_inet6, other) \
97 AF_IN_IN6_OTHER((sa)->sa_family, { \
98 cnst struct sockaddr_in *const sin = SIN(cnst,(sa)); \
101 cnst struct sockaddr_in6 *const sin##6 = SIN6(cnst,(sa)); \
106 #define SOCKADDR_IN_IN6(cnst, sa, sin, for_inet, for_inet6) \
107 SOCKADDR_IN_IN6_OTHER(cnst, sa, sin, for_inet, for_inet6, { \
108 unknown_af((sa)->sa_family); \
110 #define SOCKADDR_IN_IN6_PAIR(cnst, sa, sina, sb, sinb, for_inet, for_inet6) \
112 assert((sa)->sa_family == (sb)->sa_family); \
113 SOCKADDR_IN_IN6(cnst, sa, sina, { \
114 cnst struct sockaddr_in *const sinb = SIN(cnst,(sb)); \
117 cnst struct sockaddr_in6 *const sinb##6 = SIN6(cnst,(sb)); \
122 int adns__addrs_equal_raw(const struct sockaddr *a,
123 int bf, const void *b) {
124 if (a->sa_family != bf) return 0;
126 SOCKADDR_IN_IN6(const, a, sin, {
127 return sin->sin_addr.s_addr == ((const struct in_addr*)b)->s_addr;
129 return !memcmp(&sin6->sin6_addr, b, sizeof(struct in6_addr));
133 int adns__addrs_equal(const adns_sockaddr *a, const adns_sockaddr *b) {
134 return adns__addrs_equal_raw(&a->sa, b->sa.sa_family,
135 adns__sockaddr_addr(&b->sa));
138 int adns__sockaddrs_equal(const struct sockaddr *sa,
139 const struct sockaddr *sb) {
140 if (!adns__addrs_equal_raw(sa, sb->sa_family, adns__sockaddr_addr(sb)))
142 SOCKADDR_IN_IN6_PAIR(const, sa, sina, sb, sinb, {
143 return sina->sin_port == sinb->sin_port;
145 return sina6->sin6_port == sinb6->sin6_port &&
146 sina6->sin6_scope_id == sinb6->sin6_scope_id;
150 int adns__addr_width(int af) {
151 AF_IN_IN6_OTHER(af, {
160 void adns__prefix_mask(adns_sockaddr *sa, int len) {
161 SOCKADDR_IN_IN6(, &sa->sa, sin, {
163 sin->sin_addr.s_addr= htonl(!len ? 0 : 0xffffffff << (32-len));
167 unsigned char *m= sin6->sin6_addr.s6_addr;
170 if (j) m[i++]= (0xff << (8-j)) & 0xff;
171 memset(m+i, 0, 16-i);
175 int adns__guess_prefix_length(const adns_sockaddr *sa) {
176 SOCKADDR_IN_IN6(const, &sa->sa, sin, {
177 unsigned a= (ntohl(sin->sin_addr.s_addr) >> 24) & 0xff;
178 if (a < 128) return 8;
179 else if (a < 192) return 16;
180 else if (a < 224) return 24;
188 int adns__addr_matches(int af, const void *addr,
189 const adns_sockaddr *base, const adns_sockaddr *mask)
191 if (af != base->sa.sa_family) return 0;
192 SOCKADDR_IN_IN6_PAIR(const, &base->sa, sbase, &mask->sa, smask, {
193 const struct in_addr *v4 = addr;
194 return (v4->s_addr & smask->sin_addr.s_addr)
195 == sbase->sin_addr.s_addr;
199 const char *b= sbase6->sin6_addr.s6_addr;
200 const char *m= smask6->sin6_addr.s6_addr;
201 for (i = 0; i < 16; i++)
202 if ((a[i] & m[i]) != b[i]) return 0;
207 const void *adns__sockaddr_addr(const struct sockaddr *sa) {
208 SOCKADDR_IN_IN6(const, sa, sin, {
209 return &sin->sin_addr;
211 return &sin6->sin6_addr;
215 void adns__addr_inject(const void *a, adns_sockaddr *sa) {
216 SOCKADDR_IN_IN6( , &sa->sa, sin, {
217 memcpy(&sin->sin_addr, a, sizeof(sin->sin_addr));
219 memcpy(&sin6->sin6_addr, a, sizeof(sin6->sin6_addr));
224 * addr2text and text2addr
227 #define ADDRFAM_DEBUG
229 static void af_debug_func(const char *fmt, ...) {
233 vfprintf(stderr,fmt,al);
237 # define af_debug(fmt,...) \
238 (af_debug_func("%s: " fmt "\n", __func__, __VA_ARGS__))
240 # define af_debug(fmt,...) ((void)("" fmt "", __VA_ARGS__))
243 static bool addrtext_our_errno(int e) {
251 static bool addrtext_scope_use_ifname(const struct sockaddr *sa) {
252 const struct in6_addr *in6= &SIN6(const,sa)->sin6_addr;
254 IN6_IS_ADDR_LINKLOCAL(in6) ||
255 IN6_IS_ADDR_MC_LINKLOCAL(in6);
258 int adns_text2addr(const char *text, uint16_t port, adns_queryflags flags,
259 struct sockaddr *sa, socklen_t *salen_io) {
261 char copybuf[INET6_ADDRSTRLEN];
262 const char *parse=text;
263 const char *scopestr=0;
268 #define INVAL(how) do{ \
269 af_debug("invalid: %s: `%s'", how, text); \
273 #define AFCORE(INETx,SINx,sinx) \
275 dst = &SINx(,sa)->sinx##_addr; \
276 portp = &SINx(,sa)->sinx##_port; \
277 needlen= sizeof(*SINx(,sa));
279 if (!strchr(text, ':')) { /* INET */
281 AFCORE(INET,SIN,sin);
285 AFCORE(INET6,SIN6,sin6);
287 const char *percent= strchr(text, '%');
289 ptrdiff_t lhslen = percent - text;
290 if (lhslen >= INET6_ADDRSTRLEN) INVAL("scoped addr lhs too long");
291 memcpy(copybuf, text, lhslen);
297 af_debug("will parse scoped addr `%s' %% `%s'", parse, scopestr);
304 if (scopestr && (flags & adns_qf_addrlit_scope_forbid))
305 INVAL("scoped addr but _scope_forbid");
307 if (*salen_io < needlen) {
312 memset(sa, 0, needlen);
315 *portp = htons(port);
317 if (af == AF_INET && !(flags & adns_qf_addrlit_ipv4_quadonly)) {
318 /* we have to use inet_aton to deal with non-dotted-quad literals */
319 int r= inet_aton(parse,&SIN(,sa)->sin_addr);
320 if (!r) INVAL("inet_aton rejected");
322 int r= inet_pton(af,parse,dst);
323 if (!r) INVAL("inet_pton rejected");
330 unsigned long scope= strtoul(scopestr,&ep,10);
331 if (errno==ERANGE) INVAL("numeric scope id too large for unsigned long");
334 if (scope > ~(uint32_t)0)
335 INVAL("numeric scope id too large for uint32_t");
337 if (flags & adns_qf_addrlit_scope_numeric)
338 INVAL("non-numeric scope but _scope_numeric");
339 if (!addrtext_scope_use_ifname(sa)) {
340 af_debug("cannot convert non-numeric scope"
341 " in non-link-local addr `%s'", text);
345 scope= if_nametoindex(scopestr);
347 /* RFC3493 says "No errors are defined". It's not clear
348 * whether that is supposed to mean if_nametoindex "can't
349 * fail" (other than by the supplied name not being that of an
350 * interface) which seems unrealistic, or that it conflates
351 * all its errors together by failing to set errno, or simply
352 * that they didn't bother to document the errors.
354 * glibc, FreeBSD and OpenBSD all set errno (to ENXIO when
355 * appropriate). See Debian bug #749349.
357 * We attempt to deal with this by clearing errno to start
358 * with, and then perhaps mapping the results. */
359 af_debug("if_nametoindex rejected scope name (errno=%s)",
363 } else if (addrtext_our_errno(errno)) {
364 /* we use these for other purposes, urgh. */
365 perror("adns: adns_text2addr: if_nametoindex"
366 " failed with unexpected error");
372 if (scope > ~(uint32_t)0) {
373 fprintf(stderr,"adns: adns_text2addr: if_nametoindex"
374 " returned an interface index >=2^32 which will not fit"
375 " in sockaddr_in6.sin6_scope_id");
381 SIN6(,sa)->sin6_scope_id= scope;
382 } /* if (scopestr) */
388 int adns_addr2text(const struct sockaddr *sa, adns_queryflags flags,
389 char *buffer, int *buflen_io, int *port_r) {
393 if (*buflen_io < ADNS_ADDR2TEXT_BUFLEN) {
394 *buflen_io = ADNS_ADDR2TEXT_BUFLEN;
398 SOCKADDR_IN_IN6_OTHER(const, sa, sin, {
399 src= &sin->sin_addr; port= sin->sin_port;
401 src= &sin6->sin6_addr; port= sin6->sin6_port;
406 const char *ok= inet_ntop(sa->sa_family, src, buffer, *buflen_io);
409 if (sa->sa_family == AF_INET6) {
410 uint32_t scope = SIN6(const,sa)->sin6_scope_id;
412 if (flags & adns_qf_addrlit_scope_forbid)
414 int scopeoffset = strlen(buffer);
415 int remain = *buflen_io - scopeoffset;
416 char *scopeptr = buffer + scopeoffset;
417 assert(remain >= IF_NAMESIZE+1/*%*/);
418 *scopeptr++= '%'; remain--;
420 af_debug("will print scoped addr `%.*s' %% %"PRIu32"",
421 scopeoffset,buffer, scope);
422 if (scope <= UINT_MAX /* so we can pass it to if_indextoname */
423 && !(flags & adns_qf_addrlit_scope_numeric)
424 && addrtext_scope_use_ifname(sa)) {
425 parsedname = if_indextoname(scope, scopeptr);
427 af_debug("if_indextoname rejected scope (errno=%s)",
430 /* fair enough, show it as a number then */
431 } else if (addrtext_our_errno(errno)) {
432 /* we use these for other purposes, urgh. */
433 perror("adns: adns_addr2text: if_indextoname"
434 " failed with unexpected error");
442 int r = snprintf(scopeptr, remain,
444 assert(r < *buflen_io - scopeoffset);
446 af_debug("printed scoped addr `%s'", buffer);
450 if (port_r) *port_r= ntohs(port);
454 char *adns__sockaddr_ntoa(const struct sockaddr *sa, char *buf) {
456 int len= ADNS_ADDR2TEXT_BUFLEN;
458 err= adns_addr2text(sa, 0, buf, &len, 0);
460 err= adns_addr2text(sa, adns_qf_addrlit_scope_numeric, buf, &len, 0);
466 * Reverse-domain parsing and construction.
469 int adns__make_reverse_domain(const struct sockaddr *sa, const char *zone,
470 char **buf_io, size_t bufsz,
476 const unsigned char *ap;
479 AF_IN_IN6_OTHER(sa->sa_family, {
481 if (!zone) zone= "in-addr.arpa";
484 if (!zone) zone= "ip6.arpa";
489 req += strlen(zone) + 1;
493 p= malloc(req); if (!p) return errno;
498 SOCKADDR_IN_IN6(const, sa, sin, {
499 aa= ntohl(sin->sin_addr.s_addr);
500 for (i=0; i<4; i++) {
501 p += sprintf(p, "%d", (int)(aa & 0xff));
506 ap= sin6->sin6_addr.s6_addr + 16;
507 for (i=0; i<16; i++) {
509 for (j=0; j<2; j++) {
511 *p++= (y < 10) ? y + '0' : y - 10 + 'a';
523 #define REVPARSE_P_L(labnum) \
524 const char *p= dgram + rps->labstart[labnum]; \
525 int l= rps->lablen[labnum]
527 * REVPARSE_P_L(int labnum);
530 * const struct revparse_state *rps;
532 * const char *p; // start of label labnum in dgram
533 * int l; // length of label in dgram
536 static bool revparse_check_tail(struct revparse_state *rps,
537 const char *dgram, int nlabels,
538 int bodylen, const char *inarpa) {
541 if (nlabels != bodylen+2) return 0;
542 for (i=0; i<2; i++) {
543 REVPARSE_P_L(bodylen+i);
544 const char *want= !i ? inarpa : "arpa";
545 if (!adns__labels_equal(p,l, want,strlen(want))) return 0;
550 static bool revparse_atoi(const char *p, int l, int base,
551 unsigned max, unsigned *v_r) {
553 if (l>1 && p[0]=='0') return 0;
557 int c= ctype_toupper(*p++);
558 if ('0'<=c && c<='9') tv = c-'0';
559 else if ('A'<=c && c<='Z') tv = c-'A'+10;
561 if (tv >= base) return 0;
570 static bool revparse_inet(struct revparse_state *rps,
571 const char *dgram, int nlabels,
572 adns_rrtype *rrtype_r, adns_sockaddr *addr_r) {
573 if (!revparse_check_tail(rps,dgram,nlabels,4,"in-addr")) return 0;
577 for (i=3; i>=0; i--) {
580 if (!revparse_atoi(p,l,10,255,&v)) return 0;
585 addr_r->inet.sin_family= AF_INET;
586 addr_r->inet.sin_addr.s_addr= htonl(a);
590 static bool revparse_inet6(struct revparse_state *rps,
591 const char *dgram, int nlabels,
592 adns_rrtype *rrtype_r, adns_sockaddr *addr_r) {
593 if (!revparse_check_tail(rps,dgram,nlabels,32,"ip6")) return 0;
596 memset(addr_r,0,sizeof(*addr_r));
597 unsigned char *a= addr_r->inet6.sin6_addr.s6_addr+16;
598 for (i=0; i<32; ) { /* i incremented in inner loop */
600 for (j=0; j<2; j++, i++) {
603 if (!revparse_atoi(p,l,16,15,&v)) return 0;
609 *rrtype_r= adns_r_aaaa;
610 addr_r->inet.sin_family= AF_INET6;
614 bool adns__revparse_label(struct revparse_state *rps, int labnum,
615 const char *dgram, int labstart, int lablen) {
616 if (labnum >= MAXREVLABELS)
619 assert(labstart <= 65535);
620 assert(lablen <= 255);
621 rps->labstart[labnum] = labstart;
622 rps->lablen[labnum] = lablen;
626 bool adns__revparse_done(struct revparse_state *rps,
627 const char *dgram, int nlabels,
628 adns_rrtype *rrtype_r, adns_sockaddr *addr_r) {
630 revparse_inet(rps,dgram,nlabels,rrtype_r,addr_r) ||
631 revparse_inet6(rps,dgram,nlabels,rrtype_r,addr_r);