--- libelwix/example/test_n2.c 2013/06/03 22:49:13 1.2.2.2 +++ libelwix/example/test_n2.c 2020/05/27 15:01:46 1.5.26.2 @@ -12,27 +12,42 @@ main(int argc, char **argv) struct in_addr a; netaddr_t n, *n0; inaddr_t t; + ether_addr_t *ea, na; + char str[BUFSIZ]; + int res; + a.s_addr = E_CIDRMASK(4); + printf("/4 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); a.s_addr = E_CIDRMASK(8); - printf("/8 = %08X %08x\n", a.s_addr, htonl(a.s_addr)); + printf("/8 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); a.s_addr = E_CIDRMASK(24); - printf("/24 = %08X %08x\n", a.s_addr, htonl(a.s_addr)); + printf("/24 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); a.s_addr = E_CIDRMASK(26); - printf("/26 = %08X %08x\n", a.s_addr, htonl(a.s_addr)); + printf("/26 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); + a.s_addr = E_CIDRMASK(28); + printf("/28 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); + a.s_addr = E_CIDRMASK(29); + printf("/29 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); a.s_addr = E_CIDRMASK(30); - printf("/30 = %08X %08x\n", a.s_addr, htonl(a.s_addr)); + printf("/30 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); + a.s_addr = E_CIDRMASK(31); + printf("/31 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); + a.s_addr = E_CIDRMASK(32); + printf("/32 = %08X %08x\n", a.s_addr, ntohl(a.s_addr)); memset(&n, 0, sizeof n); n.addr.sin.sin_family = AF_INET; n.addr.sin.sin_addr.s_addr = inet_addr("10.10.10.1"); - n.mask.in.s_addr = E_CIDRMASK(24); + n.mask.in.s_addr = E_CIDRMASK(26); t.in.s_addr = inet_addr("10.10.10.42"); - printf("10.10.10.42 is in network 10.10.10.0/24? %d\n", e_innet(&n, &t)); + printf("10.10.10.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t)); + t.in.s_addr = inet_addr("10.10.10.242"); + printf("10.10.10.242 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t)); t.in.s_addr = inet_addr("10.10.0.42"); - printf("10.10.0.42 is in network 10.10.10.0/24? %d\n", e_innet(&n, &t)); + printf("10.10.0.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t)); t.in.s_addr = inet_addr("172.16.0.42"); - printf("172.16.0.42 is in network 10.10.10.0/24? %d\n", e_innet(&n, &t)); + printf("172.16.0.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t)); n0 = e_getnet("1.1.1.1/12"); if (n0) { @@ -44,23 +59,79 @@ main(int argc, char **argv) printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr)); e_free(n0); } + n0 = e_getnet("2.2.2.2/32"); + if (n0) { + printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr)); + e_free(n0); + } n0 = e_getnet("elwix.org/29"); if (n0) { printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr)); e_free(n0); } + memset(&n, 0, sizeof n); + printf("ret=%d should be -1\n", e_network("4.4.4.4", NULL)); + printf("ret=%d should be 1\n", e_network("4.4.4.4", &n)); + printf("ret=%d should be 0\n", e_network("4.4.4.5/32", &n)); + printf("4.4.4.5/32 mask=0x%X addr=%s\n", ntohl(n.mask.in.s_addr), inet_ntoa(n.addr.sin.sin_addr)); + printf("ret=%d should be 0\n", e_network("4.4.4.5/30", &n)); + printf("4.4.4.5/30 mask=0x%X addr=%s\n", ntohl(n.mask.in.s_addr), inet_ntoa(n.addr.sin.sin_addr)); + printf("ret=%d should be 0\n", e_network("4.4.4.4/24", &n)); + printf("4.4.4.4/24 mask=0x%X addr=%s\n", ntohl(n.mask.in.s_addr), inet_ntoa(n.addr.sin.sin_addr)); + printf("ret=%d should be 0\n", e_network("253.4.4.4/4", &n)); + printf("253.4.4.4/8 mask=0x%X addr=%s\n", ntohl(n.mask.in.s_addr), inet_ntoa(n.addr.sin.sin_addr)); + printf("ret=%d should be 0\n", e_network("4.4.4.4/0", &n)); + printf("4.4.4.4/0 mask=0x%X addr=%s\n", ntohl(n.mask.in.s_addr), inet_ntoa(n.addr.sin.sin_addr)); + printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/128", &n)); + inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str); + printf("ipv6/128 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str); + printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/126", &n)); + inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str); + printf("ipv6/126 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str); + printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/112", &n)); + inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str); + printf("ipv6/112 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str); + printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/64", &n)); + inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str); + printf("ipv6/64 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str); + printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/4", &n)); + inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str); + printf("ipv6/4 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str); + printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/0", &n)); + inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str); + printf("ipv6/0 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), + ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str); + if (argc < 2) return 0; AIT_SET_DATA(val, "shits hit the fan! --- shits with Merudia ...\n", 47); printf("GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val)); AIT_SET_DATA(val, argv[1], strlen(argv[1]) + 1); + printf("+GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val)); + ait_freeVar(&val); AIT_SET_DATA(val, argv[1], strlen(argv[1]) + 1); printf("GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val)); ait_freeVar(&val); - ait_freeVar(&val); if (!e_gethostbyname(argv[1], 1234, &addr)) { printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError()); @@ -73,7 +144,20 @@ main(int argc, char **argv) } printf("Address is %s port is %u\n", AIT_GET_STR(&v), e_n2port(&addr)); - AIT_FREE_VAL(&v); + + if (argc < 3) + return 0; + + val = ait_allocVar(); + AIT_SET_STR(val, argv[2]); + printf("addr=%s\n", (argc > 3) ? argv[3] : NULL); + e_ether_aton((argc > 3) ? argv[3] : NULL, &na); + if ((ea = e_ether_addr(AIT_GET_STR(val), (argc > 3) ? &na : NULL))) { + printf("MAC address of iface=%s is %s\n", AIT_GET_STR(val), e_ether_ntoa(ea, str, sizeof str)); + e_free(ea); + } + ait_freeVar(&val); + return 0; }