|
Packit Service |
b38f0b |
/* HEADER Testing inet_pton() and inet_ntop() */
|
|
Packit Service |
b38f0b |
|
|
Packit Service |
b38f0b |
int i;
|
|
Packit Service |
b38f0b |
char str[128];
|
|
Packit Service |
b38f0b |
struct in_addr in_addr;
|
|
Packit Service |
b38f0b |
struct in6_addr in6_addr;
|
|
Packit Service |
b38f0b |
|
|
Packit Service |
b38f0b |
static const struct { const char* s; unsigned char b[4]; } in_testdata[] = {
|
|
Packit Service |
b38f0b |
{ "0.0.0.0", { 0, 0, 0, 0 } },
|
|
Packit Service |
b38f0b |
{ "1.2.3.4", { 1, 2, 3, 4 } },
|
|
Packit Service |
b38f0b |
{ "255.255.255.255", { 255, 255, 255, 255 } },
|
|
Packit Service |
b38f0b |
};
|
|
Packit Service |
b38f0b |
|
|
Packit Service |
b38f0b |
static const struct { const char* s; unsigned char b[16]; } in6_testdata[] = {
|
|
Packit Service |
b38f0b |
{ "::", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, },
|
|
Packit Service |
b38f0b |
{ "::1", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, },
|
|
Packit Service |
b38f0b |
{ "1::", { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, },
|
|
Packit Service |
b38f0b |
{ "1::1", { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, },
|
|
Packit Service |
b38f0b |
};
|
|
Packit Service |
b38f0b |
|
|
Packit Service |
b38f0b |
for (i = 0; i < sizeof(in_testdata)/sizeof(in_testdata[0]); ++i) {
|
|
Packit Service |
b38f0b |
const unsigned char *const b = in_testdata[i].b;
|
|
Packit Service |
b38f0b |
|
|
Packit Service |
b38f0b |
OKF(inet_pton(AF_INET, in_testdata[i].s, &in_addr) == 1,
|
|
Packit Service |
b38f0b |
("IPv4 inet_pton(%s)", in_testdata[i].s));
|
|
Packit Service |
b38f0b |
OK(ntohl(in_addr.s_addr)
|
|
Packit Service |
b38f0b |
== ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]),
|
|
Packit Service |
b38f0b |
"inet_pton() value");
|
|
Packit Service |
b38f0b |
OK(inet_ntop(AF_INET, &in_addr, str, sizeof(str)) == str,
|
|
Packit Service |
b38f0b |
"IPv4 inet_ntop()");
|
|
Packit Service |
b38f0b |
OKF(strcmp(in_testdata[i].s, str) == 0,
|
|
Packit Service |
b38f0b |
("%s =?= %s", in_testdata[i].s, str));
|
|
Packit Service |
b38f0b |
}
|
|
Packit Service |
b38f0b |
|
|
Packit Service |
b38f0b |
|
|
Packit Service |
b38f0b |
for (i = 0; i < sizeof(in6_testdata)/sizeof(in6_testdata[0]); ++i) {
|
|
Packit Service |
b38f0b |
const unsigned char *const b = in6_testdata[i].b;
|
|
Packit Service |
b38f0b |
const unsigned char *const r = (void *)&in6_addr;
|
|
Packit Service |
b38f0b |
int result, j;
|
|
Packit Service |
b38f0b |
|
|
Packit Service |
b38f0b |
result = inet_pton(AF_INET6, in6_testdata[i].s, &in6_addr);
|
|
Packit Service |
b38f0b |
OKF(result == 1,
|
|
Packit Service |
b38f0b |
("IPv6 inet_pton(%s) -> %d; "
|
|
Packit Service |
b38f0b |
" %02x%02x:%02x%02x:%02x%02x:%02x%02x"
|
|
Packit Service |
b38f0b |
":%02x%02x:%02x%02x:%02x%02x:%02x%02x",
|
|
Packit Service |
b38f0b |
in6_testdata[i].s, result,
|
|
Packit Service |
b38f0b |
r[0], r[1], r[ 2], r[ 3], r[ 4], r[ 5], r[ 6], r[ 7],
|
|
Packit Service |
b38f0b |
r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]
|
|
Packit Service |
b38f0b |
));
|
|
Packit Service |
b38f0b |
for (j = 0; j < 16; ++j)
|
|
Packit Service |
b38f0b |
printf("%02x ", b[j]);
|
|
Packit Service |
b38f0b |
printf("\n");
|
|
Packit Service |
b38f0b |
for (j = 0; j < 16; ++j)
|
|
Packit Service |
b38f0b |
OKF(r[j] == b[j],
|
|
Packit Service |
b38f0b |
("IPv6 inet_pton() value (%#02x =?= %#02x)", r[j], b[j]));
|
|
Packit Service |
b38f0b |
OK(inet_ntop(AF_INET6, &in6_addr, str, sizeof(str)) == str,
|
|
Packit Service |
b38f0b |
"IPv6 inet_ntop()");
|
|
Packit Service |
b38f0b |
OKF(strcmp(in6_testdata[i].s, str) == 0,
|
|
Packit Service |
b38f0b |
("%s =?= %s", in6_testdata[i].s, str));
|
|
Packit Service |
b38f0b |
}
|