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
4
5
6 /*+
7 This is a hook function for use with g_list_foreach, to print a list
8 of prefixes
9 +*/
10
11 void ip_print_prefix(void *dataptr, void *junk) {
/* [<][>][^][v][top][bottom][index][help] */
12 char ascpref[IP_PREFSTR_MAX];
13 ip_prefix_t *binpref=dataptr;
14
15 IP_pref_b2a( binpref, ascpref, IP_PREFSTR_MAX );
16 printf ("\tprefix: %s\n", ascpref);
17 }
18
19 /**************************************************************************/
20
21 int okif_txt( int conditiontrue, char *string )
/* [<][>][^][v][top][bottom][index][help] */
22 {
23
24 if( conditiontrue ) {
25 printf(".OK.\t");
26 } else {
27 printf("**failed**");
28 }
29 printf("%s\n",string);
30
31 return conditiontrue;
32 }
33
34 #define okif(a) okif_txt(a, #a)
/* [<][>][^][v][top][bottom][index][help] */
35
36
37 /**************************************************************************/
38
39 int main(void)
/* [<][>][^][v][top][bottom][index][help] */
40 {
41 ip_addr_t myaddr;
42 ip_prefix_t mypref;
43 ip_range_t myrange;
44 char buf[255];
45
46 /*sleep(60);*/
47
48 printf("\ttesting IP_pref and IP_addr functions ...\n");
49 printf("\tcorrect input ...");
50
51 IP_pref_e2b(&mypref, "123.21.12.1/7");
52 IP_pref_b2a(&mypref, buf, 32);
53 okif( strcmp(buf, "122.0.0.0/7") == 0 );
54
55
56 printf("\ttricky input ...");
57 IP_addr_e2b(&myaddr, "123.21.12.7 - ");
58 IP_addr_b2a(&myaddr, buf, 32);
59 okif( strcmp(buf, "123.21.12.7") == 0 );
60
61
62 printf("\ttesting IP_rang functions ...\n");
63
64 IP_rang_e2b( &myrange, "193.232.213.12 - 193.232.213.91 ");
65 IP_addr_b2a( &(myrange.begin), buf, 32);
66 okif( strcmp(buf, "193.232.213.12") == 0 );
67
68 IP_addr_b2a( &(myrange.end), buf, 32);
69 okif( strcmp(buf, "193.232.213.91") == 0 );
70
71
72 printf("\ttesting IP_addr_bit functions ...\n");
73 IP_addr_e2b(&myaddr, "195.21.12.1");
74 IP_addr_b2a( &myaddr, buf, 32);
75 printf("\tfor address %s (%08x, %u)\n", buf,
76 myaddr.words[0], myaddr.words[0]);
77
78 /*{ int i;
79 for(i=0;i<32;i++) {
80 printf("%2d \t%d\n", i, IP_addr_bit_get(&myaddr, i));
81 }
82 }
83 */
84
85 {
86 extern void ip_print_prefix(void *dataptr, void *junk);
87 GList *preflist = NULL;
88 unsigned mask = IP_rang_decomp(&myrange, &preflist);
89
90 g_list_foreach( preflist, ip_print_prefix, NULL );
91 okif_txt(mask == 0x3c, "IP_rang_decomp ...");
92 }
93
94
95 IP_revd_e2b(&mypref, "65.81.213.in-addr.arpa");
96 IP_pref_b2a(&mypref, buf, 32);
97 okif_txt( strcmp(buf, "213.81.65.0/24") == 0 ,"inaddr.arpa conversion ...");
98
99 okif_txt( IP_pref_b2v4_len(&mypref) == 24, "pref_v4_len ...");
100
101 okif_txt( IP_addr_b2v4_addr(& (mypref.ip)) == 3578872064U, "addr_v4_addr ...");
102
103 okif_txt( IP_pref_b2v4_addr(&mypref) == 3578872064U, "pref_v4_addr ..." );
104
105 okif_txt( IP_pref_v4_mk( &mypref, 3578872064U, 20 ) == IP_OK
106 && IP_pref_b2a( &mypref, buf, 32) == IP_OK
107 && strcmp(buf, "213.81.64.0/20") == 0, "pref_v4_make ..." );
108
109 okif_txt( IP_addr_e2b(&myaddr,"2001::A5D4:d8B1 ") == IP_OK
110 && IP_addr_b2a(&myaddr, buf, 128) == IP_OK
111 && strcmp(buf,"2001::a5d4:d8b1") == 0, "ipv6 address conversion ...");
112
113 okif_txt( IP_pref_e2b(&mypref," 2001::a5d4:d8B1/69") == IP_OK
114 && IP_pref_b2a(&mypref, buf, 128) == IP_OK
115 && strcmp(buf,"2001::/69") == 0, "ipv6 prefix conversion ..." );
116
117 okif_txt( IP_rang_e2b( &myrange, "2001:a5d4:d8BC:: - 2001:a5d4:d8ff::")
118 == IP_OK
119 && IP_rang_b2a(&myrange, buf, 255) == IP_OK
120 && strcmp(buf, "2001:a5d4:d8bc:: - 2001:a5d4:d8ff::") == 0,
121 "IPv6 range conversion ...");
122
123 okif_txt( IP_addr_f2b_v6(&myaddr, "1234567890", "987654321122") == IP_OK
124 && IP_addr_b2v6_hi(&myaddr) == 1234567890LL
125 && IP_addr_b2v6_lo(&myaddr) == 987654321122LL,
126 "IP_v6 f2b address conversion");
127
128 okif_txt( IP_pref_f2b_v6(&mypref, "123456789012345",
129 "987654321123456","127") == IP_OK
130 && IP_pref_b2v6_len(&mypref) == 127
131 && IP_addr_b2v6_hi(& (mypref.ip)) == 123456789012345LL
132 && IP_addr_b2v6_lo(& (mypref.ip)) == 987654321123456LL,
133 "IP_v6 f2b prefix conversion");
134
135
136
137
138 return 0;
139 }
140