tests/ip/test_ip.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- ip_print_prefix
- okif_txt
- okif
- main
1 #include <iproutines.h>
2 #include "bitmask.h"
3 #include <inet6def.h>
4
5
6
7
8 /*+
9 This is a hook function for use with g_list_foreach, to print a list
10 of prefixes
11 +*/
12
13 void ip_print_prefix(void *dataptr, void *junk) {
/* [<][>][^][v][top][bottom][index][help] */
14 char ascpref[IP_PREFSTR_MAX];
15 ip_prefix_t *binpref=dataptr;
16
17 IP_pref_b2a( binpref, ascpref, IP_PREFSTR_MAX );
18 printf ("\tprefix: %s\n", ascpref);
19 }
20
21 /**************************************************************************/
22
23 int okif_txt( int conditiontrue, char *string )
/* [<][>][^][v][top][bottom][index][help] */
24 {
25
26 if( conditiontrue ) {
27 printf(".OK.\t");
28 } else {
29 printf("**failed**");
30 }
31 printf("%s\n",string);
32
33 return conditiontrue;
34 }
35
36 #define okif(a) okif_txt(a, #a)
/* [<][>][^][v][top][bottom][index][help] */
37
38
39 /**************************************************************************/
40
41 int main(void)
/* [<][>][^][v][top][bottom][index][help] */
42 {
43 ip_addr_t myaddr;
44 ip_prefix_t mypref;
45 ip_range_t myrange;
46 char buf[255];
47
48 /*sleep(60);*/
49
50 printf("\ttesting IP_pref and IP_addr functions ...\n");
51 printf("\tcorrect input ...");
52
53 IP_pref_e2b(&mypref, "123.21.12.1/7");
54 IP_pref_b2a(&mypref, buf, 32);
55 okif( strcmp(buf, "122.0.0.0/7") == 0 );
56
57
58 printf("\ttricky input ...");
59 IP_addr_e2b(&myaddr, "123.21.12.7 - ");
60 IP_addr_b2a(&myaddr, buf, 32);
61 okif( strcmp(buf, "123.21.12.7") == 0 );
62
63
64 printf("\ttesting IP_rang functions ...\n");
65
66 IP_rang_e2b( &myrange, "193.232.213.12 - 193.232.213.91 ");
67 IP_addr_b2a( &(myrange.begin), buf, 32);
68 okif( strcmp(buf, "193.232.213.12") == 0 );
69
70 IP_addr_b2a( &(myrange.end), buf, 32);
71 okif( strcmp(buf, "193.232.213.91") == 0 );
72
73
74 printf("\ttesting IP_addr_bit functions ...\n");
75 IP_addr_e2b(&myaddr, "195.21.12.1");
76 IP_addr_b2a( &myaddr, buf, 32);
77 printf("\tfor address %s (%08x, %u)\n", buf,
78 myaddr.words[0], myaddr.words[0]);
79
80 /*{ int i;
81 for(i=0;i<32;i++) {
82 printf("%2d \t%d\n", i, IP_addr_bit_get(&myaddr, i));
83 }
84 }
85 */
86
87 {
88 extern void ip_print_prefix(void *dataptr, void *junk);
89 GList *preflist = NULL;
90 unsigned mask = IP_rang_decomp(&myrange, &preflist);
91
92 g_list_foreach( preflist, ip_print_prefix, NULL );
93 okif_txt(mask == 0x3c, "IP_rang_decomp ...");
94 }
95
96 IP_revd_e2b(&mypref, "65.81.213.in-addr.arpa");
97 IP_pref_b2a(&mypref, buf, 32);
98 okif_txt( strcmp(buf, "213.81.65.0/24") == 0 ,"inaddr.arpa conversion ...");
99
100 okif_txt( IP_revd_a2b(&mypref, "0-31.81.213.in-addr.arpa") == IP_OK,
101 "RFC2317 phase 1");
102 IP_pref_b2a(&mypref, buf, 32);
103 okif_txt( strcmp(buf, "213.81.0.0/16") == 0 ,"RFC2317 phase 2");
104
105
106 okif_txt( IP_revd_e2b(&mypref, "0.8.0.6.0.1.0.0.2.ip6.int") == IP_OK,
107 "ip6.int conversion");
108 IP_pref_b2a(&mypref, buf, 128);
109 okif_txt( strcmp(buf, "2001:608::/36") == 0 ,"ip6.int back to prefix");
110
111 okif_txt( IP_revd_e2b(&mypref, "8.0.6.0.1.0.0.2.8.0.6.0.1.0.0.2.8.0.6.0.1.0.0.2.8.0.6.0.1.0.0.2.ip6.int") == IP_OK,
112 "128 bit ip6.int conversion");
113 IP_pref_b2a(&mypref, buf, 128);
114 okif_txt( strcmp(buf, "2001:608:2001:608:2001:608:2001:608/128") == 0 ,"ip6.int back to prefix");
115
116 okif_txt( IP_revd_e2b(&mypref, "213.in-a") == IP_NOREVD, "rev.dom bad input");
117
118 okif_txt( IP_revd_e2b(&mypref, "65.81.213.IN-ADDR.arpa") == IP_OK, "capitals");
119 IP_pref_b2a(&mypref, buf, 32);
120 okif_txt( strcmp(buf, "213.81.65.0/24") == 0 ,"inaddr.arpa conversion ...");
121
122
123 okif_txt( IP_pref_b2v4_len(&mypref) == 24, "pref_v4_len ...");
124
125 okif_txt( IP_addr_b2v4_addr(& (mypref.ip)) == 3578872064U, "addr_v4_addr ...");
126
127 okif_txt( IP_pref_b2v4_addr(&mypref) == 3578872064U, "pref_v4_addr ..." );
128
129 okif_txt( IP_pref_v4_mk( &mypref, 3578872064U, 20 ) == IP_OK
130 && IP_pref_b2a( &mypref, buf, 32) == IP_OK
131 && strcmp(buf, "213.81.64.0/20") == 0, "pref_v4_make ..." );
132
133 okif_txt( IP_addr_e2b(&myaddr,"2001::A5D4:d8B1 ") == IP_OK
134 && IP_addr_b2a(&myaddr, buf, 128) == IP_OK
135 && strcmp(buf,"2001::a5d4:d8b1") == 0, "ipv6 address conversion ...");
136
137 okif_txt( IP_pref_e2b(&mypref," 2001::a5d4:d8B1/69") == IP_OK
138 && IP_pref_b2a(&mypref, buf, 128) == IP_OK
139 && strcmp(buf,"2001::/69") == 0, "ipv6 prefix conversion ..." );
140
141 okif_txt( IP_rang_e2b( &myrange, "2001:a5d4:d8BC:: - 2001:a5d4:d8ff::")
142 == IP_OK
143 && IP_rang_b2a(&myrange, buf, 255) == IP_OK
144 && strcmp(buf, "2001:a5d4:d8bc:: - 2001:a5d4:d8ff::") == 0,
145 "IPv6 range conversion ...");
146
147 okif_txt( IP_addr_f2b_v6(&myaddr, "1234567890", "987654321122") == IP_OK
148 && IP_addr_b2v6_hi(&myaddr) == 1234567890LL
149 && IP_addr_b2v6_lo(&myaddr) == 987654321122LL,
150 "IP_v6 f2b address conversion");
151
152 okif_txt( IP_pref_f2b_v6(&mypref, "123456789012345",
153 "987654321123456","127") == IP_OK
154 && IP_pref_b2v6_len(&mypref) == 127
155 && IP_addr_b2v6_hi(& (mypref.ip)) == 123456789012345LL
156 && IP_addr_b2v6_lo(& (mypref.ip)) == 987654321123456LL,
157 "IP_v6 f2b prefix conversion");
158
159
160 IP_pref_e2b(&mypref, "2001:567:87FF::/48");
161 IP_pref_b2a(&mypref, buf, 32);
162 okif_txt( strcmp(buf, "2001:567:87ff::/48") == 0 ,"IPv6 pref conversion ...");
163
164 {
165 char dst[16];
166 inet_pton(AF_INET6, "2001:567:87FF::", &dst);
167 inet_ntop(AF_INET6, &dst, buf, 32);
168 }
169
170 return 0;
171 }
172