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 (7 months, 1 week 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

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <elwix.h>

#ifdef __linux__
	#define __u6_addr __in6_u
#endif

int
main(int argc, char **argv)
{
	sockaddr_t addr = { 0 };
	ait_val_t v, *val = NULL;
	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, ntohl(a.s_addr));
	a.s_addr = E_CIDRMASK(24);
	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, 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, 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(26);

	t.in.s_addr = inet_addr("10.10.10.42");
	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/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/26? %d\n", e_innet(&n, &t));

	n0 = e_getnet("1.1.1.1/12");
	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("2.2.2.2");
	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("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);

	if (!e_gethostbyname(argv[1], 1234, &addr)) {
		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
		return 1;
	}

	if (!e_n2addr(&addr, &v)) {
		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
		return 2;
	}

	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;
}

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