File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_n2.c
Revision 1.6.30.1: download - view: text, annotated - select for diffs - revision graph
Mon Apr 15 09:50:57 2024 UTC (6 months, 2 weeks ago) by misho
Branches: elwix5_11
Diff to: branchpoint 1.6: preferred, unified
UT patch

    1: #include <stdio.h>
    2: #include <string.h>
    3: #include <elwix.h>
    4: #include <netinet/in.h>
    5: 
    6: #ifdef __linux__
    7: 	#define __u6_addr __in6_u
    8: #endif
    9: 
   10: int
   11: main(int argc, char **argv)
   12: {
   13: 	sockaddr_t addr = { 0 };
   14: 	ait_val_t v, *val = NULL;
   15: 	struct in_addr a;
   16: 	netaddr_t n, *n0;
   17: 	inaddr_t t;
   18: 	ether_addr_t *ea, na;
   19: 	char str[BUFSIZ];
   20: 	int res;
   21: 
   22: 	a.s_addr = E_CIDRMASK(4);
   23: 	printf("/4 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   24: 	a.s_addr = E_CIDRMASK(8);
   25: 	printf("/8 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   26: 	a.s_addr = E_CIDRMASK(24);
   27: 	printf("/24 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   28: 	a.s_addr = E_CIDRMASK(26);
   29: 	printf("/26 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   30: 	a.s_addr = E_CIDRMASK(28);
   31: 	printf("/28 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   32: 	a.s_addr = E_CIDRMASK(29);
   33: 	printf("/29 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   34: 	a.s_addr = E_CIDRMASK(30);
   35: 	printf("/30 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   36: 	a.s_addr = E_CIDRMASK(31);
   37: 	printf("/31 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   38: 	a.s_addr = E_CIDRMASK(32);
   39: 	printf("/32 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
   40: 
   41: 	memset(&n, 0, sizeof n);
   42: 	n.addr.sin.sin_family = AF_INET;
   43: 	n.addr.sin.sin_addr.s_addr = inet_addr("10.10.10.1");
   44: 	n.mask.in.s_addr = E_CIDRMASK(26);
   45: 
   46: 	t.in.s_addr = inet_addr("10.10.10.42");
   47: 	printf("10.10.10.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
   48: 	t.in.s_addr = inet_addr("10.10.10.242");
   49: 	printf("10.10.10.242 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
   50: 	t.in.s_addr = inet_addr("10.10.0.42");
   51: 	printf("10.10.0.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
   52: 	t.in.s_addr = inet_addr("172.16.0.42");
   53: 	printf("172.16.0.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
   54: 
   55: 	n0 = e_getnet("1.1.1.1/12");
   56: 	if (n0) {
   57: 		printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr));
   58: 		e_free(n0);
   59: 	}
   60: 	n0 = e_getnet("2.2.2.2");
   61: 	if (n0) {
   62: 		printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr));
   63: 		e_free(n0);
   64: 	}
   65: 	n0 = e_getnet("2.2.2.2/32");
   66: 	if (n0) {
   67: 		printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr));
   68: 		e_free(n0);
   69: 	}
   70: 	n0 = e_getnet("elwix.org/29");
   71: 	if (n0) {
   72: 		printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr));
   73: 		e_free(n0);
   74: 	}
   75: 
   76: 	memset(&n, 0, sizeof n);
   77: 	printf("ret=%d should be -1\n", e_network("4.4.4.4", NULL));
   78: 	printf("ret=%d should be 1\n", e_network("4.4.4.4", &n));
   79: 	printf("ret=%d should be 0\n", e_network("4.4.4.5/32", &n));
   80: 	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));
   81: 	printf("ret=%d should be 0\n", e_network("4.4.4.5/30", &n));
   82: 	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));
   83: 	printf("ret=%d should be 0\n", e_network("4.4.4.4/24", &n));
   84: 	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));
   85: 	printf("ret=%d should be 0\n", e_network("253.4.4.4/4", &n));
   86: 	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));
   87: 	printf("ret=%d should be 0\n", e_network("4.4.4.4/0", &n));
   88: 	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));
   89: 	printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/128", &n));
   90: 	inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
   91: 	printf("ipv6/128 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), 
   92: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), 
   93: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), 
   94: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
   95: 	printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/126", &n));
   96: 	inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
   97: 	printf("ipv6/126 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), 
   98: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), 
   99: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), 
  100: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
  101: 	printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/112", &n));
  102: 	inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
  103: 	printf("ipv6/112 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), 
  104: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), 
  105: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), 
  106: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
  107: 	printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/64", &n));
  108: 	inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
  109: 	printf("ipv6/64 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), 
  110: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), 
  111: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), 
  112: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
  113: 	printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/4", &n));
  114: 	inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
  115: 	printf("ipv6/4 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), 
  116: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), 
  117: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), 
  118: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
  119: 	printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/0", &n));
  120: 	inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
  121: 	printf("ipv6/0 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]), 
  122: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]), 
  123: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]), 
  124: 		ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
  125: 
  126: 	if (argc < 2)
  127: 		return 0;
  128: 
  129: 	AIT_SET_DATA(val, "shits hit the fan! --- shits with Merudia ...\n", 47);
  130: 	printf("GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
  131: 	AIT_SET_DATA(val, argv[1], strlen(argv[1]) + 1);
  132: 	printf("+GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
  133: 	ait_freeVar(&val);
  134: 
  135: 	AIT_SET_DATA(val, argv[1], strlen(argv[1]) + 1);
  136: 	printf("GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
  137: 	ait_freeVar(&val);
  138: 
  139: 	if (!e_gethostbyname(argv[1], 1234, &addr)) {
  140: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
  141: 		return 1;
  142: 	}
  143: 
  144: 	if (!e_n2addr(&addr, &v)) {
  145: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
  146: 		return 2;
  147: 	}
  148: 
  149: 	printf("Address is %s port is %u\n", AIT_GET_STR(&v), e_n2port(&addr));
  150: 	AIT_FREE_VAL(&v);
  151: 
  152: 	if (argc < 3)
  153: 		return 0;
  154: 
  155: 	val = ait_allocVar();
  156: 	AIT_SET_STR(val, argv[2]);
  157: 	printf("addr=%s\n", (argc > 3) ? argv[3] : NULL);
  158: 	e_ether_aton((argc > 3) ? argv[3] : NULL, &na);
  159: 	if ((ea = e_ether_addr(AIT_GET_STR(val), (argc > 3) ? &na : NULL))) {
  160: 		printf("MAC address of iface=%s is %s\n", AIT_GET_STR(val), e_ether_ntoa(ea, str, sizeof str));
  161: 		e_free(ea);
  162: 	}
  163: 	ait_freeVar(&val);
  164: 
  165: 	return 0;
  166: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>