tests/ip/test_ip.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following functions.
  1. ip_print_prefix
  2. okif_txt
  3. 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)
  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   okif_txt( IP_revd_e2b(&mypref, "0.e.7.0.1.0.0.2.ip6.arpa") == IP_OK, 
 106             "ip6.arpa conversion");
 107   IP_pref_b2a(&mypref, buf, 128);
 108   okif_txt( strcmp(buf, "2001:7e0::/32")  == 0 ,"ip6.arpa back to prefix");
 109 
 110   okif_txt( IP_revd_e2b(&mypref, "1.8.0.6.0.1.0.0.2.ip6.int") == IP_OK, 
 111             "ip6.int conversion");
 112   IP_pref_b2a(&mypref, buf, 128);
 113   okif_txt( strcmp(buf, "2001:608:1000::/36")  == 0 ,"ip6.int back to prefix");
 114 
 115   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, 
 116             "128 bit ip6.int conversion");
 117   IP_pref_b2a(&mypref, buf, 128);
 118   okif_txt( strcmp(buf, "2001:608:2001:608:2001:608:2001:608/128")  == 0 ,"128 bit ip6.int back to prefix");
 119 
 120   {
 121       int len;
 122       const char addr[] = "2af136082af136082af136082af13608";
 123       char arpa_buf[128];
 124       char prefix_buf[128];
 125       char tmp_buf[128];
 126       int i;
 127 
 128       for (len=1; len<=32; len++) {
 129           arpa_buf[0] = '\0';
 130           for (i=len-1; i>=0; i--) {
 131               tmp_buf[0] = addr[i]; tmp_buf[1] = '.'; tmp_buf[2] = '\0';
 132               strcat(arpa_buf, tmp_buf);
 133           }
 134           strcat(arpa_buf, "ip6.arpa");
 135 
 136           prefix_buf[0] = '\0';
 137           i = 0;
 138           while (i<len) {
 139               tmp_buf[0] = addr[i]; tmp_buf[1] = '\0';
 140               strcat(prefix_buf, tmp_buf);
 141               i++;
 142               if ((i % 4) == 0) {
 143                   strcat(prefix_buf, ":");
 144               }
 145           }
 146           if (i % 4) {
 147               while (i % 4) {
 148                   strcat(prefix_buf, "0");
 149                   i++;
 150               }
 151               strcat(prefix_buf, ":");
 152           }
 153           if (i == 28) {
 154               strcat(prefix_buf, "0/");
 155           } else if (i == 32) {
 156               prefix_buf[strlen(prefix_buf)-1] = '/';
 157           } else {
 158               strcat(prefix_buf, ":/");
 159           }
 160           sprintf(tmp_buf, "%d", len * 4);
 161           strcat(prefix_buf, tmp_buf);
 162 
 163           sprintf(tmp_buf, "%d bit ip6.arpa conversion", len * 4);
 164           okif_txt(IP_revd_e2b(&mypref, arpa_buf)==IP_OK, tmp_buf);
 165           sprintf(tmp_buf, "%d bit ip6.arpa back to prefix", len * 4);
 166           IP_pref_b2a(&mypref, buf, 128);
 167           okif_txt( strcmp(buf, prefix_buf)  == 0 , tmp_buf);
 168       }
 169   }
 170   
 171   okif_txt( IP_revd_e2b(&mypref, "213.in-a") == IP_NOREVD, "rev.dom bad input"); 
 172 
 173   okif_txt( IP_revd_e2b(&mypref, "65.81.213.IN-ADDR.arpa") == IP_OK, "capitals");  
 174   IP_pref_b2a(&mypref, buf, 32);
 175   okif_txt( strcmp(buf, "213.81.65.0/24") == 0 ,"inaddr.arpa conversion ...");
 176  
 177 
 178   okif_txt( IP_pref_b2v4_len(&mypref) == 24, "pref_v4_len  ...");
 179     
 180   okif_txt( IP_addr_b2v4_addr(& (mypref.ip)) == 3578872064U, "addr_v4_addr  ...");
 181   
 182   okif_txt( IP_pref_b2v4_addr(&mypref) == 3578872064U, "pref_v4_addr  ..." );
 183 
 184   okif_txt(  IP_pref_v4_mk(  &mypref, 3578872064U, 20 ) == IP_OK
 185          && IP_pref_b2a( &mypref, buf, 32) == IP_OK
 186          && strcmp(buf, "213.81.64.0/20") == 0, "pref_v4_make  ..." );
 187   
 188   okif_txt( IP_addr_e2b(&myaddr,"2001::A5D4:d8B1 ") == IP_OK
 189         && IP_addr_b2a(&myaddr, buf, 128) == IP_OK
 190         && strcmp(buf,"2001::a5d4:d8b1") == 0, "ipv6 address conversion ..."); 
 191  
 192   okif_txt( IP_pref_e2b(&mypref," 2001::a5d4:d8B1/69") == IP_OK
 193         && IP_pref_b2a(&mypref, buf, 128) == IP_OK
 194         && strcmp(buf,"2001::/69") == 0, "ipv6 prefix conversion ..." );
 195   
 196   okif_txt( IP_rang_e2b( &myrange, "2001:a5d4:d8BC:: - 2001:a5d4:d8ff::") 
 197                     == IP_OK
 198         && IP_rang_b2a(&myrange,  buf, 255) == IP_OK
 199         && strcmp(buf, "2001:a5d4:d8bc:: - 2001:a5d4:d8ff::") == 0,
 200         "IPv6 range conversion ...");
 201 
 202   okif_txt( IP_addr_f2b_v6(&myaddr, "1234567890", "987654321122") == IP_OK
 203             && IP_addr_b2v6_hi(&myaddr) == 1234567890LL
 204             && IP_addr_b2v6_lo(&myaddr) == 987654321122LL,
 205             "IP_v6 f2b address conversion");
 206   
 207    okif_txt( IP_pref_f2b_v6(&mypref, "123456789012345", 
 208                                      "987654321123456","127") == IP_OK
 209              && IP_pref_b2v6_len(&mypref) == 127
 210              && IP_addr_b2v6_hi(& (mypref.ip)) == 123456789012345LL
 211              && IP_addr_b2v6_lo(& (mypref.ip)) == 987654321123456LL,
 212              "IP_v6 f2b prefix conversion");
 213   
 214 
 215   IP_pref_e2b(&mypref, "2001:567:87FF::/48");  
 216   IP_pref_b2a(&mypref, buf, 32);
 217   okif_txt( strcmp(buf, "2001:567:87ff::/48") == 0 ,"IPv6 pref conversion ...");
 218 
 219   {
 220   char dst[16];
 221   inet_pton(AF_INET6, "2001:567:87FF::", &dst);
 222   inet_ntop(AF_INET6, &dst, buf, 32);
 223   }
 224  
 225   return 0; 
 226 } 
 227  

/* [<][>][^][v][top][bottom][index][help] */