5 ./addrtext_s fe80::1%wlanx
6 ./addrtext_s fe80::1%wlan0
7 ./addrtext_s fe80::1%23
9 ./addrtext_s 2001:ba8:1e3::%wlan0
10 ./addrtext_s 2001:ba8:1e3::%23
11 ./addrtext_s 2001:ba8:1e3::%1 # normally lo
12 ./addrtext_s 127.0.0.1x
13 ./addrtext_s 172.18.45.6
21 * - test program for address<->string conversion, not part of the library
24 * This file is part of adns, which is
25 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
26 * Copyright (C) 1999-2000,2003,2006 Tony Finch
27 * Copyright (C) 1991 Massachusetts Institute of Technology
28 * (See the file INSTALL for full details.)
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 3, or (at your option)
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software Foundation.
44 #include <sys/types.h>
45 #include <sys/socket.h>
57 #define STRING(x) STRING2(x)
62 static void hex(const void *data_v, int l) {
63 const uint8_t *data= data_v;
66 printf("%02x",data[i]);
69 static void dump(const char *pfx, struct sockaddr *sa, socklen_t salen) {
74 for (i=0; i<salen; i++)
75 printf("%02x",((const uint8_t*)sa)[i]);
77 printf(" %d ", sa->sa_family);
79 switch (sa->sa_family) {
81 const struct sockaddr_in *s = (const void*)sa;
82 printf(".port=%d .addr=%08"PRIx32"",
84 (uint32_t)ntohl(s->sin_addr.s_addr));
88 const struct sockaddr_in6 *s = (const void*)sa;
89 printf(".port=%d .flowinfo=%08"PRIx32" .scope_id=%08"PRIx32" .addr=",
91 (uint32_t)ntohl(s->sin6_flowinfo),
92 (uint32_t)ntohl(s->sin6_scope_id));
93 hex(&s->sin6_addr, sizeof(s->sin6_addr));
100 static void dotest(const char *input) {
104 struct addrinfo *air=0;
105 char ourbuf[ADNS_ADDR2TEXT_BUFLEN];
106 char theirbuf[ADNS_ADDR2TEXT_BUFLEN];
108 memset(&ours,0,sizeof(ours));
110 socklen= sizeof(ours);
111 int our_r= adns_text2addr(input,PORT,0,&ours.sa,&socklen);
113 memset(&aip,0,sizeof(aip));
114 aip.ai_flags= AI_NUMERICHOST|AI_NUMERICSERV;
115 aip.ai_socktype= SOCK_DGRAM;
116 aip.ai_protocol= IPPROTO_UDP;
118 int libc_r= getaddrinfo(input,STRING(PORT),&aip,&air);
119 printf("`%s': us %s; libc %s, air=%p",
120 input, strerror(our_r), libc_r ? gai_strerror(libc_r) : "0", air);
122 printf(" .family=%d .socklen=%ld .addr=%p .next=%p",
123 air->ai_family, (long)air->ai_addrlen, air->ai_addr, air->ai_next);
126 if (libc_r==EAI_NONAME && !air) {
127 if (strchr(input,'%') && (our_r==ENOSYS || our_r==ENXIO)) {
128 printf(" bad-scope");
131 if (strchr(input,'%') && our_r==ENOSYS) {
132 printf(" bad-scope");
142 #define FAIL do{ printf(" | FAIL\n"); fails++; }while(0)
143 #define WANT(x) if (!(x)) { printf(" not %s",STRING(x)); FAIL; return; } else;
150 if (air->ai_addrlen!=socklen || memcmp(&ours,air->ai_addr,socklen)) {
153 dump("ours",&ours.sa,socklen);
154 dump("libc",air->ai_addr,air->ai_addrlen);
160 int ourbuflen= sizeof(ourbuf);
162 our_r= adns_addr2text(&ours.sa,0, ourbuf,&ourbuflen, &ourport);
164 printf(" us %s",strerror(our_r));
166 printf(" `%s'",ourbuf);
168 size_t theirbuflen= sizeof(theirbuf);
169 libc_r= getnameinfo(&ours.sa,socklen, theirbuf,theirbuflen, 0,0,
170 NI_NUMERICHOST|NI_NUMERICSERV);
171 printf("; libc %s", libc_r ? gai_strerror(libc_r) : "0");
173 printf(" `%s'",theirbuf);
180 if (strcmp(ourbuf,theirbuf)) {
190 int main(int argc, char **argv) {
192 while ((arg= *++argv)) {