File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_n2.c
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Mon Apr 15 10:42:26 2024 UTC (6 months, 2 weeks ago) by misho
Branches: MAIN
CVS tags: elwix6_6, elwix6_5, elwix6_4, elwix6_3, elwix6_2, elwix6_1, elwix5_12, HEAD, ELWIX6_5, ELWIX6_4, ELWIX6_2, ELWIX6_1, ELWIX6_0, ELWIX5_11
Version 5.11

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

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