|
Szabolcs Nagy |
c5b587 |
// inet_addr, inet_ntoa, inet_pton and inet_ntop tests with roundtrip check
|
|
Szabolcs Nagy |
c5b587 |
#include <errno.h>
|
|
Szabolcs Nagy |
088f2d |
#include <string.h>
|
|
Szabolcs Nagy |
088f2d |
#include <stdio.h>
|
|
Szabolcs Nagy |
088f2d |
#include <arpa/inet.h>
|
|
Szabolcs Nagy |
088f2d |
#include "test.h"
|
|
Szabolcs Nagy |
088f2d |
|
|
Szabolcs Nagy |
088f2d |
static int digit(int c)
|
|
Szabolcs Nagy |
088f2d |
{
|
|
Szabolcs Nagy |
088f2d |
c-='0';
|
|
Szabolcs Nagy |
088f2d |
if (c>9) c-='a'-'0'-10;
|
|
Szabolcs Nagy |
088f2d |
return c;
|
|
Szabolcs Nagy |
088f2d |
}
|
|
Szabolcs Nagy |
088f2d |
|
|
Szabolcs Nagy |
c5b587 |
static void tobin(void *d, char *s)
|
|
Szabolcs Nagy |
088f2d |
{
|
|
Szabolcs Nagy |
088f2d |
int i;
|
|
Szabolcs Nagy |
c5b587 |
unsigned char *p = d;
|
|
Szabolcs Nagy |
c5b587 |
for (i=0; s[2*i]; i++) p[i] = digit(s[2*i])*16+digit(s[2*i+1]);
|
|
Szabolcs Nagy |
088f2d |
}
|
|
Szabolcs Nagy |
088f2d |
|
|
Szabolcs Nagy |
c5b587 |
static void tohex(char *d, void *s, int n)
|
|
Szabolcs Nagy |
088f2d |
{
|
|
Szabolcs Nagy |
088f2d |
int i;
|
|
Szabolcs Nagy |
c5b587 |
unsigned char *p = s;
|
|
Szabolcs Nagy |
c5b587 |
for (i=0; i
|
|
Szabolcs Nagy |
088f2d |
}
|
|
Szabolcs Nagy |
088f2d |
|
|
Szabolcs Nagy |
c5b587 |
#define V6(src,ret,hex) do{\
|
|
Szabolcs Nagy |
088f2d |
int r; \
|
|
Szabolcs Nagy |
088f2d |
char binaddr[16]; \
|
|
Szabolcs Nagy |
088f2d |
char hexaddr[40]; \
|
|
Szabolcs Nagy |
088f2d |
char txtaddr[60]; \
|
|
Szabolcs Nagy |
c5b587 |
\
|
|
Szabolcs Nagy |
c5b587 |
r=inet_pton(AF_INET6,src,binaddr); \
|
|
Szabolcs Nagy |
088f2d |
if (r!=ret) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_pton(AF_INET6, "#src", addr) returned %d, want %d\n", r, ret); \
|
|
Szabolcs Nagy |
088f2d |
if (ret!=1) break; \
|
|
Szabolcs Nagy |
c5b587 |
tohex(hexaddr,binaddr,16); \
|
|
Szabolcs Nagy |
088f2d |
if (strcmp(hexaddr,hex)) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_pton(AF_INET6, "#src", addr) got addr %s, want %s\n", hexaddr, hex); \
|
|
Szabolcs Nagy |
c5b587 |
\
|
|
Szabolcs Nagy |
088f2d |
tobin(binaddr,hex); \
|
|
Szabolcs Nagy |
c5b587 |
if (inet_ntop(AF_INET6,binaddr,txtaddr,sizeof txtaddr)!=txtaddr) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_ntop(AF_INET6, <"#hex">, buf, size) did not return buf\n"); \
|
|
Szabolcs Nagy |
c5b587 |
if (inet_pton(AF_INET6,txtaddr,binaddr)!=1) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_ntop(AF_INET6, <"#hex">, buf, size) got %s, it is rejected by inet_pton\n", txtaddr); \
|
|
Szabolcs Nagy |
088f2d |
tohex(hexaddr,binaddr,16); \
|
|
Szabolcs Nagy |
088f2d |
if (strcmp(hexaddr,hex)) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_ntop(AF_INET6, <"#hex">, buf, size) got %s that is %s, want %s\n", txtaddr, hexaddr, hex); \
|
|
Szabolcs Nagy |
088f2d |
if (strncmp(hex,"00000000000000000000ffff",24)==0 && !strchr(txtaddr,'.')) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_ntop(AF_INET6, <"#hex">, buf, size) got %s, should be ipv4 mapped\n", txtaddr); \
|
|
Szabolcs Nagy |
c5b587 |
}while(0);
|
|
Szabolcs Nagy |
c5b587 |
|
|
Szabolcs Nagy |
c5b587 |
// ret and hex are the results of inet_pton and inet_addr respectively
|
|
Szabolcs Nagy |
c5b587 |
#define V4(src,ret,hex) do{\
|
|
Szabolcs Nagy |
c5b587 |
int r; \
|
|
Szabolcs Nagy |
c5b587 |
uint32_t a; \
|
|
Szabolcs Nagy |
c5b587 |
struct in_addr in; \
|
|
Szabolcs Nagy |
c5b587 |
char buf[20]; \
|
|
Szabolcs Nagy |
c5b587 |
char *p; \
|
|
Szabolcs Nagy |
c5b587 |
\
|
|
Szabolcs Nagy |
c5b587 |
a=inet_addr(src); \
|
|
Szabolcs Nagy |
c5b587 |
tohex(buf,&a,4); \
|
|
Szabolcs Nagy |
c5b587 |
if (strcmp(buf,hex)) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_addr("#src") returned %s, want %s\n", buf, hex); \
|
|
Szabolcs Nagy |
c5b587 |
\
|
|
Szabolcs Nagy |
c5b587 |
r=inet_pton(AF_INET,src,&a); \
|
|
Szabolcs Nagy |
c5b587 |
if (r!=ret) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_pton(AF_INET, "#src", addr) returned %d, want %d\n", r, ret); \
|
|
Szabolcs Nagy |
c5b587 |
\
|
|
Szabolcs Nagy |
c5b587 |
if (ret!=1) break; \
|
|
Szabolcs Nagy |
c5b587 |
\
|
|
Szabolcs Nagy |
c5b587 |
tohex(buf,&a,4); \
|
|
Szabolcs Nagy |
c5b587 |
if (strcmp(buf,hex)) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_pton(AF_INET, "#src", addr) got addr %s, want %s\n", buf, hex); \
|
|
Szabolcs Nagy |
c5b587 |
\
|
|
Szabolcs Nagy |
c5b587 |
tobin(&a,hex); \
|
|
Szabolcs Nagy |
c5b587 |
if (inet_ntop(AF_INET,&a,buf,sizeof buf)!=buf) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_ntop(AF_INET, <"#hex">, buf, size) did not return buf\n"); \
|
|
Szabolcs Nagy |
c5b587 |
if (strcmp(buf,src)) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_ntop(AF_INET, <"#hex">, buf, size) got %s, want %s\n", buf, src); \
|
|
Szabolcs Nagy |
c5b587 |
\
|
|
Szabolcs Nagy |
c5b587 |
in.s_addr = a; \
|
|
Szabolcs Nagy |
c5b587 |
p=inet_ntoa(in); \
|
|
Szabolcs Nagy |
c5b587 |
if (strcmp(p,src)) \
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_ntoa(<"#hex">) returned %s, want %s\n", p, src); \
|
|
Szabolcs Nagy |
088f2d |
}while(0);
|
|
Szabolcs Nagy |
088f2d |
|
|
Szabolcs Nagy |
088f2d |
int main(void)
|
|
Szabolcs Nagy |
088f2d |
{
|
|
Szabolcs Nagy |
c5b587 |
|
|
Szabolcs Nagy |
c5b587 |
// errors
|
|
Szabolcs Nagy |
c5b587 |
if (inet_pton(12345, "", 0) != -1 || errno != EAFNOSUPPORT)
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_pton(12345,,) should fail with EAFNOSUPPORT, got %s\n", strerror(errno));
|
|
Szabolcs Nagy |
c5b587 |
errno=0;
|
|
Szabolcs Nagy |
1571e8 |
if (inet_ntop(AF_INET,"xxxx","",0) != 0 || errno != ENOSPC)
|
|
Szabolcs Nagy |
c5b587 |
t_error("inet_ntop(,,0,0) should fail with ENOSPC, got %s\n", strerror(errno));
|
|
Szabolcs Nagy |
c5b587 |
errno=0;
|
|
Szabolcs Nagy |
c5b587 |
|
|
Szabolcs Nagy |
c5b587 |
// dotted-decimal notation
|
|
Szabolcs Nagy |
c5b587 |
V4("0.0.0.0", 1, "00000000")
|
|
Szabolcs Nagy |
c5b587 |
V4("127.0.0.1", 1, "7f000001")
|
|
Szabolcs Nagy |
c5b587 |
V4("10.0.128.31", 1, "0a00801f")
|
|
Szabolcs Nagy |
c5b587 |
V4("255.255.255.255", 1, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
|
|
Szabolcs Nagy |
c5b587 |
// numbers-and-dots notation, but not dotted-decimal
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.03.4", 0, "01020304")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.0x33.4", 0, "01023304")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.0XAB.4", 0, "0102ab04")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.0xabcd", 0, "0102abcd")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.0xabcdef", 0, "01abcdef")
|
|
Szabolcs Nagy |
c5b587 |
V4("00377.0x0ff.65534", 0, "fffffffe")
|
|
Szabolcs Nagy |
c5b587 |
|
|
Szabolcs Nagy |
c5b587 |
// invalid
|
|
Szabolcs Nagy |
c5b587 |
V4(".1.2.3", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
V4("1..2.3", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.3.", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.3.4.5", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.3.a", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.256.2.3", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.4294967296.3", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2.-4294967295.3", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
V4("1.2. 3.4", 0, "ffffffff")
|
|
Szabolcs Nagy |
c5b587 |
|
|
Szabolcs Nagy |
c5b587 |
// ipv6
|
|
Szabolcs Nagy |
c5b587 |
V6(":", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6("::", 1, "00000000000000000000000000000000")
|
|
Szabolcs Nagy |
c5b587 |
V6("::1", 1, "00000000000000000000000000000001")
|
|
Szabolcs Nagy |
c5b587 |
V6(":::", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6("192.168.1.1", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6(":192.168.1.1", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6("::192.168.1.1", 1, "000000000000000000000000c0a80101")
|
|
Szabolcs Nagy |
c5b587 |
V6("0:0:0:0:0:0:192.168.1.1", 1, "000000000000000000000000c0a80101")
|
|
Szabolcs Nagy |
c5b587 |
V6("0:0::0:0:0:192.168.1.1", 1, "000000000000000000000000c0a80101")
|
|
Szabolcs Nagy |
c5b587 |
V6("::012.34.56.78", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6(":ffff:192.168.1.1", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6("::ffff:192.168.1.1", 1, "00000000000000000000ffffc0a80101")
|
|
Szabolcs Nagy |
c5b587 |
V6(".192.168.1.1", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6(":.192.168.1.1", 0, "")
|
|
Szabolcs Nagy |
2ac662 |
V6("a:0b:00c:000d:E:F::", 1, "000a000b000c000d000e000f00000000")
|
|
Szabolcs Nagy |
2ac662 |
V6("a:0b:00c:000d:0000e:f::", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6("a:b::c:d:e:f", 1, "000a000b00000000000c000d000e000f")
|
|
Szabolcs Nagy |
c5b587 |
V6("ffff:c0a8:5e4", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6(":ffff:c0a8:5e4", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6("0:0:0:0:0:ffff:c0a8:5e4", 1, "00000000000000000000ffffc0a805e4")
|
|
Szabolcs Nagy |
c5b587 |
V6("0:0:0:0:ffff:c0a8:5e4", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6("0::ffff:c0a8:5e4", 1, "00000000000000000000ffffc0a805e4")
|
|
Szabolcs Nagy |
c5b587 |
V6("::0::ffff:c0a8:5e4", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
V6("c0a8", 0, "")
|
|
Szabolcs Nagy |
c5b587 |
|
|
Szabolcs Nagy |
088f2d |
return t_status;
|
|
Szabolcs Nagy |
088f2d |
}
|