File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_n2.c
Revision 1.4.20.1: download - view: text, annotated - select for diffs - revision graph
Wed Apr 4 01:22:14 2018 UTC (6 years, 7 months ago) by misho
Branches: elwix4_10
Diff to: branchpoint 1.4: preferred, unified
minor changes in inaddr_t structure

    1: #include <stdio.h>
    2: #include <string.h>
    3: #include <elwix.h>
    4: #include <netinet/in.h>
    5: 
    6: 
    7: int
    8: main(int argc, char **argv)
    9: {
   10: 	sockaddr_t addr = { 0 };
   11: 	ait_val_t v, *val = NULL;
   12: 	struct in_addr a;
   13: 	netaddr_t n, *n0;
   14: 	inaddr_t t;
   15: 	ether_addr_t *ea, na;
   16: 	char str[BUFSIZ];
   17: 	int res;
   18: 
   19: 	a.s_addr = E_CIDRMASK(8);
   20: 	printf("/8 = %08X %08x\n", a.s_addr, htonl(a.s_addr));
   21: 	a.s_addr = E_CIDRMASK(24);
   22: 	printf("/24 = %08X %08x\n", a.s_addr, htonl(a.s_addr));
   23: 	a.s_addr = E_CIDRMASK(26);
   24: 	printf("/26 = %08X %08x\n", a.s_addr, htonl(a.s_addr));
   25: 	a.s_addr = E_CIDRMASK(30);
   26: 	printf("/30 = %08X %08x\n", a.s_addr, htonl(a.s_addr));
   27: 
   28: 	memset(&n, 0, sizeof n);
   29: 	n.addr.sin.sin_family = AF_INET;
   30: 	n.addr.sin.sin_addr.s_addr = inet_addr("10.10.10.1");
   31: 	n.mask.in.s_addr = E_CIDRMASK(26);
   32: 
   33: 	t.in.s_addr = inet_addr("10.10.10.42");
   34: 	printf("10.10.10.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
   35: 	t.in.s_addr = inet_addr("10.10.10.242");
   36: 	printf("10.10.10.242 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
   37: 	t.in.s_addr = inet_addr("10.10.0.42");
   38: 	printf("10.10.0.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
   39: 	t.in.s_addr = inet_addr("172.16.0.42");
   40: 	printf("172.16.0.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
   41: 
   42: 	n0 = e_getnet("1.1.1.1/12");
   43: 	if (n0) {
   44: 		printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr));
   45: 		e_free(n0);
   46: 	}
   47: 	n0 = e_getnet("2.2.2.2");
   48: 	if (n0) {
   49: 		printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr));
   50: 		e_free(n0);
   51: 	}
   52: 	n0 = e_getnet("elwix.org/29");
   53: 	if (n0) {
   54: 		printf("addr=%s/%x\n", inet_ntoa(n0->addr.sin.sin_addr), ntohl(n0->mask.in.s_addr));
   55: 		e_free(n0);
   56: 	}
   57: 
   58: 	if (argc < 2)
   59: 		return 0;
   60: 
   61: 	AIT_SET_DATA(val, "shits hit the fan! --- shits with Merudia ...\n", 47);
   62: 	printf("GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
   63: 	AIT_SET_DATA(val, argv[1], strlen(argv[1]) + 1);
   64: 	printf("+GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
   65: 	ait_freeVar(&val);
   66: 
   67: 	AIT_SET_DATA(val, argv[1], strlen(argv[1]) + 1);
   68: 	printf("GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
   69: 	ait_freeVar(&val);
   70: 
   71: 	if (!e_gethostbyname(argv[1], 1234, &addr)) {
   72: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   73: 		return 1;
   74: 	}
   75: 
   76: 	if (!e_n2addr(&addr, &v)) {
   77: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   78: 		return 2;
   79: 	}
   80: 
   81: 	printf("Address is %s port is %u\n", AIT_GET_STR(&v), e_n2port(&addr));
   82: 	AIT_FREE_VAL(&v);
   83: 
   84: 	if (argc < 3)
   85: 		return 0;
   86: 
   87: 	val = ait_allocVar();
   88: 	AIT_SET_STR(val, argv[2]);
   89: 	printf("addr=%s\n", (argc > 3) ? argv[3] : NULL);
   90: 	e_ether_aton((argc > 3) ? argv[3] : NULL, &na);
   91: 	if ((ea = e_ether_addr(AIT_GET_STR(val), (argc > 3) ? &na : NULL))) {
   92: 		printf("MAC address of iface=%s is %s\n", AIT_GET_STR(val), e_ether_ntoa(ea, str, sizeof str));
   93: 		e_free(ea);
   94: 	}
   95: 	ait_freeVar(&val);
   96: 
   97: 	return 0;
   98: }

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