File:
[ELWIX - Embedded LightWeight unIX -] /
libelwix /
example /
test_n2.c
Revision
1.6:
download - view:
text,
annotated -
select for diffs -
revision graph
Wed May 27 15:03:28 2020 UTC (4 years, 5 months ago) by
misho
Branches:
MAIN
CVS tags:
elwix5_9,
elwix5_8,
elwix5_7,
elwix5_6,
elwix5_5,
elwix5_4,
elwix5_3,
elwix5_2,
elwix5_11,
elwix5_10,
elwix5_1,
elwix5_0,
elwix4_26,
elwix4_25,
elwix4_24,
HEAD,
ELWIX5_9,
ELWIX5_8,
ELWIX5_7,
ELWIX5_6,
ELWIX5_5,
ELWIX5_4,
ELWIX5_3,
ELWIX5_2,
ELWIX5_10,
ELWIX5_1,
ELWIX5_0,
ELWIX4_26,
ELWIX4_25,
ELWIX4_24,
ELWIX4_23
ver 4.23
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(4);
20: printf("/4 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
21: a.s_addr = E_CIDRMASK(8);
22: printf("/8 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
23: a.s_addr = E_CIDRMASK(24);
24: printf("/24 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
25: a.s_addr = E_CIDRMASK(26);
26: printf("/26 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
27: a.s_addr = E_CIDRMASK(28);
28: printf("/28 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
29: a.s_addr = E_CIDRMASK(29);
30: printf("/29 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
31: a.s_addr = E_CIDRMASK(30);
32: printf("/30 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
33: a.s_addr = E_CIDRMASK(31);
34: printf("/31 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
35: a.s_addr = E_CIDRMASK(32);
36: printf("/32 = %08X %08x\n", a.s_addr, ntohl(a.s_addr));
37:
38: memset(&n, 0, sizeof n);
39: n.addr.sin.sin_family = AF_INET;
40: n.addr.sin.sin_addr.s_addr = inet_addr("10.10.10.1");
41: n.mask.in.s_addr = E_CIDRMASK(26);
42:
43: t.in.s_addr = inet_addr("10.10.10.42");
44: printf("10.10.10.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
45: t.in.s_addr = inet_addr("10.10.10.242");
46: printf("10.10.10.242 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
47: t.in.s_addr = inet_addr("10.10.0.42");
48: printf("10.10.0.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
49: t.in.s_addr = inet_addr("172.16.0.42");
50: printf("172.16.0.42 is in network 10.10.10.0/26? %d\n", e_innet(&n, &t));
51:
52: n0 = e_getnet("1.1.1.1/12");
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: n0 = e_getnet("2.2.2.2");
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/32");
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("elwix.org/29");
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:
73: memset(&n, 0, sizeof n);
74: printf("ret=%d should be -1\n", e_network("4.4.4.4", NULL));
75: printf("ret=%d should be 1\n", e_network("4.4.4.4", &n));
76: printf("ret=%d should be 0\n", e_network("4.4.4.5/32", &n));
77: 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));
78: printf("ret=%d should be 0\n", e_network("4.4.4.5/30", &n));
79: 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));
80: printf("ret=%d should be 0\n", e_network("4.4.4.4/24", &n));
81: 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));
82: printf("ret=%d should be 0\n", e_network("253.4.4.4/4", &n));
83: 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));
84: printf("ret=%d should be 0\n", e_network("4.4.4.4/0", &n));
85: 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));
86: printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/128", &n));
87: inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
88: printf("ipv6/128 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]),
89: ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]),
90: ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]),
91: ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
92: printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/126", &n));
93: inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
94: printf("ipv6/126 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]),
95: ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]),
96: ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]),
97: ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
98: printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/112", &n));
99: inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
100: printf("ipv6/112 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]),
101: ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]),
102: ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]),
103: ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
104: printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/64", &n));
105: inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
106: printf("ipv6/64 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]),
107: ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]),
108: ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]),
109: ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
110: printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/4", &n));
111: inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
112: printf("ipv6/4 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]),
113: ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]),
114: ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]),
115: ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
116: printf("ret=%d should be 0\n", e_network("2a01:9980:b:20::deba/0", &n));
117: inet_ntop(AF_INET6, &n.addr.sin6.sin6_addr, str, sizeof str);
118: printf("ipv6/0 mask=0x%X,0x%X,0x%X,0x%X addr=%s\n", ntohl(n.mask.in6.__u6_addr.__u6_addr32[0]),
119: ntohl(n.mask.in6.__u6_addr.__u6_addr32[1]),
120: ntohl(n.mask.in6.__u6_addr.__u6_addr32[2]),
121: ntohl(n.mask.in6.__u6_addr.__u6_addr32[3]), str);
122:
123: if (argc < 2)
124: return 0;
125:
126: AIT_SET_DATA(val, "shits hit the fan! --- shits with Merudia ...\n", 47);
127: printf("GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
128: AIT_SET_DATA(val, argv[1], strlen(argv[1]) + 1);
129: printf("+GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
130: ait_freeVar(&val);
131:
132: AIT_SET_DATA(val, argv[1], strlen(argv[1]) + 1);
133: printf("GET DATA = %p %s t=%d l=%d\n", val, AIT_GET_DATA(val), AIT_TYPE(val), AIT_LEN(val));
134: ait_freeVar(&val);
135:
136: if (!e_gethostbyname(argv[1], 1234, &addr)) {
137: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
138: return 1;
139: }
140:
141: if (!e_n2addr(&addr, &v)) {
142: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
143: return 2;
144: }
145:
146: printf("Address is %s port is %u\n", AIT_GET_STR(&v), e_n2port(&addr));
147: AIT_FREE_VAL(&v);
148:
149: if (argc < 3)
150: return 0;
151:
152: val = ait_allocVar();
153: AIT_SET_STR(val, argv[2]);
154: printf("addr=%s\n", (argc > 3) ? argv[3] : NULL);
155: e_ether_aton((argc > 3) ? argv[3] : NULL, &na);
156: if ((ea = e_ether_addr(AIT_GET_STR(val), (argc > 3) ? &na : NULL))) {
157: printf("MAC address of iface=%s is %s\n", AIT_GET_STR(val), e_ether_ntoa(ea, str, sizeof str));
158: e_free(ea);
159: }
160: ait_freeVar(&val);
161:
162: return 0;
163: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>