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 Copyright Ian Jackson
25 * and contributors (see the file INSTALL for full details).
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 3, or (at your option)
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software Foundation.
41 #include <sys/types.h>
42 #include <sys/socket.h>
54 #define STRING(x) STRING2(x)
59 static void hex(const void *data_v, int l) {
60 const uint8_t *data= data_v;
63 printf("%02x",data[i]);
66 static void dump(const char *pfx, struct sockaddr *sa, socklen_t salen) {
71 for (i=0; i<salen; i++)
72 printf("%02x",((const uint8_t*)sa)[i]);
74 printf(" %d ", sa->sa_family);
76 switch (sa->sa_family) {
78 const struct sockaddr_in *s = (const void*)sa;
79 printf(".port=%d .addr=%08"PRIx32"",
81 (uint32_t)ntohl(s->sin_addr.s_addr));
85 const struct sockaddr_in6 *s = (const void*)sa;
86 printf(".port=%d .flowinfo=%08"PRIx32" .scope_id=%08"PRIx32" .addr=",
88 (uint32_t)ntohl(s->sin6_flowinfo),
89 (uint32_t)ntohl(s->sin6_scope_id));
90 hex(&s->sin6_addr, sizeof(s->sin6_addr));
97 static void dotest(const char *input) {
101 struct addrinfo *air=0;
102 char ourbuf[ADNS_ADDR2TEXT_BUFLEN];
103 char theirbuf[ADNS_ADDR2TEXT_BUFLEN];
105 memset(&ours,0,sizeof(ours));
107 socklen= sizeof(ours);
108 int our_r= adns_text2addr(input,PORT,0,&ours.sa,&socklen);
110 memset(&aip,0,sizeof(aip));
111 aip.ai_flags= AI_NUMERICHOST|AI_NUMERICSERV;
112 aip.ai_socktype= SOCK_DGRAM;
113 aip.ai_protocol= IPPROTO_UDP;
115 int libc_r= getaddrinfo(input,STRING(PORT),&aip,&air);
116 printf("`%s': us %s; libc %s, air=%p",
117 input, strerror(our_r), libc_r ? gai_strerror(libc_r) : "0", air);
119 printf(" .family=%d .socklen=%ld .addr=%p .next=%p",
120 air->ai_family, (long)air->ai_addrlen, air->ai_addr, air->ai_next);
123 if (libc_r==EAI_NONAME && !air) {
124 if (strchr(input,'%') && (our_r==ENOSYS || our_r==ENXIO)) {
125 printf(" bad-scope");
128 if (strchr(input,'%') && our_r==ENOSYS) {
129 printf(" bad-scope");
139 #define FAIL do{ printf(" | FAIL\n"); fails++; }while(0)
140 #define WANT(x) if (!(x)) { printf(" not %s",STRING(x)); FAIL; return; } else;
147 if (air->ai_addrlen!=socklen || memcmp(&ours,air->ai_addr,socklen)) {
150 dump("ours",&ours.sa,socklen);
151 dump("libc",air->ai_addr,air->ai_addrlen);
157 int ourbuflen= sizeof(ourbuf);
159 our_r= adns_addr2text(&ours.sa,0, ourbuf,&ourbuflen, &ourport);
161 printf(" us %s",strerror(our_r));
163 printf(" `%s'",ourbuf);
165 size_t theirbuflen= sizeof(theirbuf);
166 libc_r= getnameinfo(&ours.sa,socklen, theirbuf,theirbuflen, 0,0,
167 NI_NUMERICHOST|NI_NUMERICSERV);
168 printf("; libc %s", libc_r ? gai_strerror(libc_r) : "0");
170 printf(" `%s'",theirbuf);
177 if (strcmp(ourbuf,theirbuf)) {
187 int main(int argc, char **argv) {
189 while ((arg= *++argv)) {