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 2, 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,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <netinet/in.h>
46 * General address-family operations.
49 #define SIN(sa) ((struct sockaddr_in *)(sa))
50 #define CSIN(sa) ((const struct sockaddr_in *)(sa))
52 #define SIN6(sa) ((struct sockaddr_in6 *)(sa))
53 #define CSIN6(sa) ((const struct sockaddr_in6 *)(sa))
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
61 static void unknown_af(int af
) {
62 fprintf(stderr
, "ADNS INTERNAL: unknown address family %d\n", af
);
66 int adns__af_supported_p(int af
)
70 af_inet
: af_inet6
: return 1;
75 int adns__sockaddr_equal_p(const struct sockaddr
*sa
,
76 const struct sockaddr
*sb
)
78 if (sa
->sa_family
!= sb
->sa_family
) return 0;
79 switch (sa
->sa_family
) {
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
);
87 /* Don't check the flowlabel. That's apparently useful for routing
88 * performance, but doesn't affect the address in any important
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
);
99 unknown_af(sa
->sa_family
);
104 int adns__gen_pton(const char *p
, int *af_r
, union gen_addr
*addr_r
)
106 static const int aflist
[] = { AF_INET6
, AF_INET
};
109 for (i
= 0; i
< sizeof(aflist
)/sizeof(*aflist
); i
++) {
110 rc
= inet_pton(aflist
[i
], p
, addr_r
);
112 if (rc
) { *af_r
= aflist
[i
]; return 1; }
117 int adns__addr_width(int af
)
122 af_inet6
: return 128;
123 default: unknown_af(af
); return -1;
127 void adns__prefix_mask(int af
, int len
, union gen_addr
*mask_r
)
133 mask_r
->v4
.s_addr
= htonl(!len ?
0 : 0xffffffff << (32 - len
));
136 int i
= len
/8, j
= len
%8;
137 unsigned char *m
= mask_r
->v6
.s6_addr
;
141 if (j
) m
[i
++] = (0xff << (8-j
)) & 0xff;
142 memset(m
+i
, 0, 16-i
);
150 int adns__guess_prefix_length(int af
, const union gen_addr
*addr
)
155 unsigned a
= (ntohl(addr
->v4
.s_addr
) >> 24) & 0xff;
157 if (a
< 128) return 8;
158 else if (a
< 192) return 16;
159 else if (a
< 224) return 24;
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
)
174 if (addraf
!= netaf
) return 0;
178 return (addr
->v4
.s_addr
& mask
->v4
.s_addr
) == base
->v4
.s_addr
;
181 const char *a
= addr
->v6
.s6_addr
;
182 const char *b
= base
->v6
.s6_addr
;
183 const char *m
= mask
->v6
.s6_addr
;
185 for (i
= 0; i
< 16; i
++)
186 if ((a
[i
] & m
[i
]) != b
[i
]) return 0;
195 const void *adns__sockaddr_to_inaddr(const struct sockaddr
*sa
)
197 switch (sa
->sa_family
) {
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;
206 * addr2text and text2addr
209 #define ADDRFAM_DEBUG
211 static void af_debug_func(const char *fmt
, ...) {
215 vfprintf(stderr
,fmt
,al
);
219 # define af_debug(fmt,...) \
220 (af_debug_func("%s: " fmt "\n", __func__, __VA_ARGS__))
222 # define af_debug(fmt,...) ((void)("" fmt "", __VA_ARGS__))
225 static bool addrtext_our_errno(int e
) {
233 static bool addrtext_scope_use_ifname(const struct sockaddr
*sa
) {
234 const struct in6_addr
*in6
= &CSIN6(sa
)->sin6_addr
;
236 IN6_IS_ADDR_LINKLOCAL(in6
) ||
237 IN6_IS_ADDR_MC_LINKLOCAL(in6
);
240 int adns_text2addr(const char *text
, uint16_t port
, adns_queryflags flags
,
241 struct sockaddr
*sa
, socklen_t
*salen_io
) {
243 char copybuf
[INET6_ADDRSTRLEN
];
244 const char *parse
=text
;
245 const char *scopestr
=0;
250 #define INVAL(how) do{ \
251 af_debug("invalid: %s: `%s'", how, text); \
255 #define AFCORE(INETx,SINx,sinx) \
257 dst = &SINx(sa)->sinx##_addr; \
258 portp = &SINx(sa)->sinx##_port; \
259 needlen= sizeof(*SINx(sa));
261 if (!strchr(text
, ':')) { /* INET */
263 AFCORE(INET
,SIN
,sin
);
267 AFCORE(INET6
,SIN6
,sin6
);
269 const char *percent
= strchr(text
, '%');
271 ptrdiff_t lhslen
= percent
- text
;
272 if (lhslen
>= INET6_ADDRSTRLEN
) INVAL("scoped addr lhs too long");
273 memcpy(copybuf
, text
, lhslen
);
279 af_debug("will parse scoped addr `%s' %% `%s'", parse
, scopestr
);
286 if (scopestr
&& (flags
& adns_qf_addrlit_scope_forbid
))
287 INVAL("scoped addr but _scope_forbid");
289 if (*salen_io
< needlen
) {
294 memset(sa
, 0, needlen
);
297 *portp
= htons(port
);
299 int r
= inet_pton(af
,parse
,dst
);
300 if (!r
) INVAL("inet_pton rejected");
302 af_debug("inet_pton failed on `%s'", parse
);
309 unsigned long scope
= strtoul(scopestr
,&ep
,10);
310 if (errno
==ERANGE
) INVAL("numeric scope id too large for unsigned long");
313 if (scope
> ~(uint32_t)0)
314 INVAL("numeric scope id too large for uint32_t");
316 if (flags
& adns_qf_addrlit_scope_numeric
)
317 INVAL("non-numeric scope but _scope_numeric");
318 if (!addrtext_scope_use_ifname(sa
)) {
319 af_debug("cannot convert non-numeric scope"
320 " in non-link-local addr `%s'", text
);
324 scope
= if_nametoindex(scopestr
);
326 /* RFC3493 says "No errors are defined". It's not clear
327 * whether that is supposed to mean if_nametoindex "can't
328 * fail" (other than by the supplied name not being that of an
329 * interface) which seems unrealistic, or that it conflates
330 * all its errors together by failing to set errno, or simply
331 * that they didn't bother to document the errors.
333 * glibc, FreeBSD and OpenBSD all set errno (to ENXIO when
334 * appropriate). See Debian bug #749349.
336 * We attempt to deal with this by clearing errno to start
337 * with, and then perhaps mapping the results. */
338 af_debug("if_nametoindex rejected scope name (errno=%s)",
342 } else if (addrtext_our_errno(errno
)) {
343 /* we use these for other purposes, urgh. */
344 perror("adns: adns_text2addr: if_nametoindex"
345 " failed with unexpected error");
351 if (scope
> ~(uint32_t)0) {
352 fprintf(stderr
,"adns: adns_text2addr: if_nametoindex"
353 " returned an interface index >=2^32 which will not fit"
354 " in sockaddr_in6.sin6_scope_id");
360 SIN6(sa
)->sin6_scope_id
= scope
;
361 } /* if (scopestr) */
367 int adns_addr2text(const struct sockaddr
*sa
, adns_queryflags flags
,
368 char *buffer
, int *buflen_io
, int *port_r
) {
372 if (*buflen_io
< ADNS_ADDR2TEXT_BUFLEN
) {
373 *buflen_io
= ADNS_ADDR2TEXT_BUFLEN
;
377 switch (sa
->sa_family
) {
379 af_inet
: src
= &CSIN(sa
)->sin_addr
; port
= CSIN(sa
)->sin_port
; break;
380 af_inet6
: src
= &CSIN6(sa
)->sin6_addr
; port
= CSIN6(sa
)->sin6_port
; break;
381 default: return EAFNOSUPPORT
;
384 const char *ok
= inet_ntop(sa
->sa_family
, src
, buffer
, *buflen_io
);
387 if (sa
->sa_family
== AF_INET6
) {
388 uint32_t scope
= CSIN6(sa
)->sin6_scope_id
;
390 if (flags
& adns_qf_addrlit_scope_forbid
)
392 int scopeoffset
= strlen(buffer
);
393 int remain
= *buflen_io
- scopeoffset
;
394 char *scopeptr
= buffer
+ scopeoffset
;
395 assert(remain
>= IF_NAMESIZE
+1/*%*/);
396 *scopeptr
++= '%'; remain
--;
398 af_debug("will print scoped addr %s %% %"PRIu32
"", buffer
, scope
);
399 if (scope
<= UINT_MAX
/* so we can pass it to if_indextoname */
400 && !(flags
& adns_qf_addrlit_scope_numeric
)
401 && addrtext_scope_use_ifname(sa
)) {
402 parsedname
= if_indextoname(scope
, scopeptr
);
404 af_debug("if_indextoname rejected scope (errno=%s)",
407 /* fair enough, show it as a number then */
408 } else if (addrtext_our_errno(errno
)) {
409 /* we use these for other purposes, urgh. */
410 perror("adns: adns_addr2text: if_indextoname"
411 " failed with unexpected error");
419 int r
= snprintf(scopeptr
, remain
,
421 assert(r
< *buflen_io
- scopeoffset
);
423 af_debug("printed scoped addr `%s'", buffer
);
427 if (port_r
) *port_r
= ntohs(port
);
432 * Reverse-domain parsing and construction.
435 int adns__make_reverse_domain(const struct sockaddr
*sa
,
437 char **buf_io
, size_t bufsz
,
444 const unsigned char *ap
;
447 switch (sa
->sa_family
) {
451 if (!zone
) zone
= "in-addr.arpa";
455 if (!zone
) zone
= "ip6.arpa";
461 req
+= strlen(zone
) + 1;
465 p
= malloc(req
); if (!p
) return errno
;
470 switch (sa
->sa_family
) {
473 aa
= ntohl(CSIN(sa
)->sin_addr
.s_addr
);
474 for (i
= 0; i
< 4; i
++) {
475 p
+= sprintf(p
, "%d", (int)(aa
& 0xff));
481 ap
= CSIN6(sa
)->sin6_addr
.s6_addr
+ 16;
482 for (i
= 0; i
< 16; i
++) {
484 for (j
= 0; j
< 2; j
++) {
486 if (y
< 10) *p
++ = y
+ '0';
487 else *p
++ = y
- 10 + 'a';
494 unknown_af(sa
->sa_family
);
502 static int inet_rev_parsecomp(const char *p
, size_t n
)
505 if (n
> 3) return -1;
508 if ('0' <= *p
&& *p
<= '9') i
= 10*i
+ *p
++ - '0';
514 static void inet_rev_mkaddr(union gen_addr
*addr
, const byte
*ipv
)
516 addr
->v4
.s_addr
= htonl((ipv
[3]<<24) | (ipv
[2]<<16) |
517 (ipv
[1]<<8) | (ipv
[0]));
520 static int inet6_rev_parsecomp(const char *p
, size_t n
)
522 if (n
!= 1) return -1;
523 else if ('0' <= *p
&& *p
<= '9') return *p
- '0';
524 else if ('a' <= *p
&& *p
<= 'f') return *p
- 'a' + 10;
525 else if ('A' <= *p
&& *p
<= 'F') return *p
- 'a' + 10;
529 static void inet6_rev_mkaddr(union gen_addr
*addr
, const byte
*ipv
)
531 unsigned char *a
= addr
->v6
.s6_addr
;
534 for (i
= 0; i
< 16; i
++)
535 a
[i
] = (ipv
[31-2*i
] << 4) | (ipv
[30-2*i
] << 0);
538 static const struct revparse_domain
{
539 int af
; /* address family */
540 int nrevlab
; /* n of reverse-address labels */
541 adns_rrtype rrtype
; /* forward-lookup type */
543 int (*rev_parsecomp
)(const char *p
, size_t n
);
544 /* parse a single component from a label; return the integer value, or -1
545 * if it was unintelligible.
548 void (*rev_mkaddr
)(union gen_addr
*addr
, const byte
*ipv
);
549 /* write out the parsed address from a vector of parsed components */
551 const char *const tail
[3]; /* tail label names */
552 } revparse_domains
[NREVDOMAINS
] = {
553 { AF_INET
, 4, adns_r_a
, inet_rev_parsecomp
, inet_rev_mkaddr
,
554 { DNS_INADDR_ARPA
, 0 } },
555 { AF_INET6
, 32, adns_r_aaaa
, inet6_rev_parsecomp
, inet6_rev_mkaddr
,
556 { DNS_IP6_ARPA
, 0 } },
559 #define REVDOMAIN_MAP(rps, labnum) \
560 ((labnum) ? (rps)->map : (1 << NREVDOMAINS) - 1)
562 int adns__revparse_label(struct revparse_state
*rps
, int labnum
,
563 const char *label
, int lablen
)
565 unsigned f
= REVDOMAIN_MAP(rps
, labnum
);
566 const struct revparse_domain
*rpd
;
571 for (rpd
=revparse_domains
, i
=0, d
=1; i
<NREVDOMAINS
; rpd
++, i
++, d
<<= 1) {
572 if (!(f
& d
)) continue;
573 if (labnum
>= rpd
->nrevlab
) {
574 tp
= rpd
->tail
[labnum
- rpd
->nrevlab
];
575 if (!tp
|| strncmp(label
, tp
, lablen
) != 0 || tp
[lablen
])
578 ac
= rpd
->rev_parsecomp(label
, lablen
);
579 if (ac
< 0) goto mismatch
;
580 assert(labnum
< sizeof(rps
->ipv
[i
]));
581 rps
->ipv
[i
][labnum
] = ac
;
594 int adns__revparse_done(struct revparse_state
*rps
, int nlabels
,
595 adns_rrtype
*rrtype_r
, struct af_addr
*addr_r
)
597 unsigned f
= REVDOMAIN_MAP(rps
, nlabels
);
598 const struct revparse_domain
*rpd
;
602 for (rpd
=revparse_domains
, i
=0, d
=1; i
<NREVDOMAINS
; rpd
++, i
++, d
<<= 1) {
603 if (!(f
& d
)) continue;
604 if (nlabels
>= rpd
->nrevlab
&& !rpd
->tail
[nlabels
- rpd
->nrevlab
])
605 { found
= i
; continue; }
609 assert(found
>= 0); assert(f
== (1 << found
));
611 rpd
= &revparse_domains
[found
];
612 *rrtype_r
= rpd
->rrtype
;
613 addr_r
->af
= rpd
->af
;
614 rpd
->rev_mkaddr(&addr_r
->addr
, rps
->ipv
[found
]);