1 // inet_pton and inet_ntop tests with roundtrip check
7 static int digit(int c)
10 if (c>9) c-='a'-'0'-10;
14 static void tobin(char *d, char *s)
17 for (i=0; s[2*i]; i++) d[i] = digit(s[2*i])*16+digit(s[2*i+1]);
20 static void tohex(char *d, char *s, int n)
23 for (i=0; i<n; i++) sprintf(d+2*i, "%02x", (unsigned char)s[i]);
26 #define T(af,src,ret,hex) do{\
31 r=inet_pton(af,src,binaddr); \
33 t_error("inet_pton("#af", "#src", addr) returned %d, want %d\n", r, ret); \
35 tohex(hexaddr,binaddr,af==AF_INET?4:16); \
36 if (strcmp(hexaddr,hex)) \
37 t_error("inet_pton("#af", "#src", addr) got addr %s, want %s\n", hexaddr, hex); \
39 if (inet_ntop(af,binaddr,txtaddr,sizeof txtaddr)!=txtaddr) \
40 t_error("inet_ntop("#af", <"#hex">, buf, size) did not return buf\n"); \
41 if (af==AF_INET && strcmp(txtaddr,src)) \
42 t_error("inet_ntop("#af", <"#hex">, buf, size) got %s, want %s\n", txtaddr, src); \
43 if (af!=AF_INET6) break; \
44 if (inet_pton(af,txtaddr,binaddr)!=1) \
45 t_error("inet_ntop("#af", <"#hex">, buf, size) got %s, it is rejected by inet_pton\n", txtaddr); \
46 tohex(hexaddr,binaddr,16); \
47 if (strcmp(hexaddr,hex)) \
48 t_error("inet_ntop("#af", <"#hex">, buf, size) got %s that is %s, want %s\n", txtaddr, hexaddr, hex); \
49 if (strncmp(hex,"00000000000000000000ffff",24)==0 && !strchr(txtaddr,'.')) \
50 t_error("inet_ntop("#af", <"#hex">, buf, size) got %s, should be ipv4 mapped\n", txtaddr); \
55 T(AF_INET+AF_INET6, "0.0.0.0", -1, "")
56 T(AF_INET, "0.0.0.0", 1, "00000000")
57 T(AF_INET, "127.0.0.1", 1, "7f000001")
58 T(AF_INET, "255.255.255.255", 1, "ffffffff")
59 T(AF_INET, "1.2.3.", 0, "")
60 T(AF_INET, "1.2.3.4.5", 0, "")
61 T(AF_INET, ".1.2.3", 0, "")
62 T(AF_INET, "1.2.03.4", 0, "")
63 T(AF_INET, "1.2.3.a", 0, "")
64 T(AF_INET, "1.256.2.3", 0, "")
65 T(AF_INET, "1.2.4294967296.3", 0, "")
66 T(AF_INET, "1.2.-4294967295.3", 0, "")
67 T(AF_INET, "1.2. 3.4", 0, "")
68 T(AF_INET6, ":", 0, "")
69 T(AF_INET6, "::", 1, "00000000000000000000000000000000")
70 T(AF_INET6, "::1", 1, "00000000000000000000000000000001")
71 T(AF_INET6, ":::", 0, "")
72 T(AF_INET6, "192.168.1.1", 0, "")
73 T(AF_INET6, ":192.168.1.1", 0, "")
74 T(AF_INET6, "::192.168.1.1", 1, "000000000000000000000000c0a80101")
75 T(AF_INET6, "0:0:0:0:0:0:192.168.1.1", 1, "000000000000000000000000c0a80101")
76 T(AF_INET6, "0:0::0:0:0:192.168.1.1", 1, "000000000000000000000000c0a80101")
77 T(AF_INET6, "::012.34.56.78", 0, "")
78 T(AF_INET6, ":ffff:192.168.1.1", 0, "")
79 T(AF_INET6, "::ffff:192.168.1.1", 1, "00000000000000000000ffffc0a80101")
80 T(AF_INET6, ".192.168.1.1", 0, "")
81 T(AF_INET6, ":.192.168.1.1", 0, "")
82 T(AF_INET6, "a:0b:00c:000d:0000e:f::", 1, "000a000b000c000d000e000f00000000")
83 T(AF_INET6, "a:b::c:d:e:f", 1, "000a000b00000000000c000d000e000f")
84 T(AF_INET6, "ffff:c0a8:5e4", 0, "")
85 T(AF_INET6, ":ffff:c0a8:5e4", 0, "")
86 T(AF_INET6, "0:0:0:0:0:ffff:c0a8:5e4", 1, "00000000000000000000ffffc0a805e4")
87 T(AF_INET6, "0:0:0:0:ffff:c0a8:5e4", 0, "")
88 T(AF_INET6, "0::ffff:c0a8:5e4", 1, "00000000000000000000ffffc0a805e4")
89 T(AF_INET6, "::0::ffff:c0a8:5e4", 0, "")
90 T(AF_INET6, "c0a8", 0, "")