| 1 |
/* HEADER Testing inet_pton() and inet_ntop() */ |
| 2 |
|
| 3 |
int i; |
| 4 |
char str[128]; |
| 5 |
struct in_addr in_addr; |
| 6 |
struct in6_addr in6_addr; |
| 7 |
|
| 8 |
static const struct { const char* s; unsigned char b[4]; } in_testdata[] = { |
| 9 |
{ "0.0.0.0", { 0, 0, 0, 0 } }, |
| 10 |
{ "1.2.3.4", { 1, 2, 3, 4 } }, |
| 11 |
{ "255.255.255.255", { 255, 255, 255, 255 } }, |
| 12 |
}; |
| 13 |
|
| 14 |
static const struct { const char* s; unsigned char b[16]; } in6_testdata[] = { |
| 15 |
{ "::", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, }, |
| 16 |
{ "::1", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, }, |
| 17 |
{ "1::", { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, }, |
| 18 |
{ "1::1", { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, }, |
| 19 |
}; |
| 20 |
|
| 21 |
for (i = 0; i < sizeof(in_testdata)/sizeof(in_testdata[0]); ++i) { |
| 22 |
const unsigned char *const b = in_testdata[i].b; |
| 23 |
|
| 24 |
OKF(inet_pton(AF_INET, in_testdata[i].s, &in_addr) == 1, |
| 25 |
("IPv4 inet_pton(%s)", in_testdata[i].s)); |
| 26 |
OK(ntohl(in_addr.s_addr) |
| 27 |
== ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]), |
| 28 |
"inet_pton() value"); |
| 29 |
OK(inet_ntop(AF_INET, &in_addr, str, sizeof(str)) == str, |
| 30 |
"IPv4 inet_ntop()"); |
| 31 |
OKF(strcmp(in_testdata[i].s, str) == 0, |
| 32 |
("%s =?= %s", in_testdata[i].s, str)); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
for (i = 0; i < sizeof(in6_testdata)/sizeof(in6_testdata[0]); ++i) { |
| 37 |
const unsigned char *const b = in6_testdata[i].b; |
| 38 |
const unsigned char *const r = (void *)&in6_addr; |
| 39 |
int result, j; |
| 40 |
|
| 41 |
result = inet_pton(AF_INET6, in6_testdata[i].s, &in6_addr); |
| 42 |
OKF(result == 1, |
| 43 |
("IPv6 inet_pton(%s) -> %d; " |
| 44 |
" %02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 45 |
":%02x%02x:%02x%02x:%02x%02x:%02x%02x", |
| 46 |
in6_testdata[i].s, result, |
| 47 |
r[0], r[1], r[ 2], r[ 3], r[ 4], r[ 5], r[ 6], r[ 7], |
| 48 |
r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15] |
| 49 |
)); |
| 50 |
for (j = 0; j < 16; ++j) |
| 51 |
printf("%02x ", b[j]); |
| 52 |
printf("\n"); |
| 53 |
for (j = 0; j < 16; ++j) |
| 54 |
OKF(r[j] == b[j], |
| 55 |
("IPv6 inet_pton() value (%#02x =?= %#02x)", r[j], b[j])); |
| 56 |
OK(inet_ntop(AF_INET6, &in6_addr, str, sizeof(str)) == str, |
| 57 |
"IPv6 inet_ntop()"); |
| 58 |
OKF(strcmp(in6_testdata[i].s, str) == 0, |
| 59 |
("%s =?= %s", in6_testdata[i].s, str)); |
| 60 |
} |