tests/ip/test_ip.c

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

FUNCTIONS

This source file includes following functions.
  1. ip_print_prefix
  2. okif_txt
  3. okif
  4. main

#include <iproutines.h>
#include "bitmask.h"



/*+
  This is a hook function for use with g_list_foreach, to print a list
  of prefixes 
+*/

void ip_print_prefix(void *dataptr, void *junk) {
/* [<][>][^][v][top][bottom][index][help] */
  char ascpref[IP_PREFSTR_MAX];
  ip_prefix_t *binpref=dataptr;
  
  IP_pref_b2a( binpref, ascpref, IP_PREFSTR_MAX );
  printf ("\tprefix: %s\n", ascpref);
}

/**************************************************************************/

int okif_txt( int conditiontrue, char *string ) 
/* [<][>][^][v][top][bottom][index][help] */
{
  
  if( conditiontrue ) {
    printf(".OK.\t");
  } else {
    printf("**failed**");
  }
  printf("%s\n",string);

  return conditiontrue;
}

#define okif(a) okif_txt(a, #a)
/* [<][>][^][v][top][bottom][index][help] */


/**************************************************************************/

int main(void)
/* [<][>][^][v][top][bottom][index][help] */
{
  ip_addr_t   myaddr;
  ip_prefix_t mypref;
  ip_range_t  myrange;
  char buf[255];

  /*sleep(60);*/

  printf("\ttesting IP_pref and IP_addr functions ...\n");
  printf("\tcorrect input ...");

  IP_pref_e2b(&mypref, "123.21.12.1/7");
  IP_pref_b2a(&mypref, buf, 32);
  okif( strcmp(buf, "122.0.0.0/7") == 0 );
  

  printf("\ttricky input ...");
  IP_addr_e2b(&myaddr, "123.21.12.7   - ");
  IP_addr_b2a(&myaddr, buf, 32);
  okif( strcmp(buf, "123.21.12.7") == 0 );


  printf("\ttesting IP_rang functions ...\n");
  
  IP_rang_e2b( &myrange, "193.232.213.12 - 193.232.213.91 ");
  IP_addr_b2a( &(myrange.begin), buf, 32);
  okif( strcmp(buf, "193.232.213.12") == 0 );

  IP_addr_b2a( &(myrange.end), buf, 32);
  okif( strcmp(buf, "193.232.213.91") == 0 );


  printf("\ttesting IP_addr_bit functions ...\n");
  IP_addr_e2b(&myaddr, "195.21.12.1");
  IP_addr_b2a( &myaddr, buf, 32);
  printf("\tfor address %s (%08x, %u)\n", buf, 
         myaddr.words[0], myaddr.words[0]);     

  /*{ int i;
    for(i=0;i<32;i++) {
    printf("%2d \t%d\n", i, IP_addr_bit_get(&myaddr, i));
    }
    }
  */
 
  {
    extern void ip_print_prefix(void *dataptr, void *junk);
    GList *preflist = NULL;
    unsigned mask = IP_rang_decomp(&myrange, &preflist);
 
    g_list_foreach( preflist, ip_print_prefix, NULL ); 
    okif_txt(mask == 0x3c, "IP_rang_decomp ...");
  }

  
  IP_revd_e2b(&mypref, "65.81.213.in-addr.arpa");  
  IP_pref_b2a(&mypref, buf, 32);
  okif_txt( strcmp(buf, "213.81.65.0/24") == 0 ,"inaddr.arpa conversion ...");

  okif_txt( IP_pref_b2v4_len(&mypref) == 24, "pref_v4_len  ...");
    
  okif_txt( IP_addr_b2v4_addr(& (mypref.ip)) == 3578872064U, "addr_v4_addr  ...");
  
  okif_txt( IP_pref_b2v4_addr(&mypref) == 3578872064U, "pref_v4_addr  ..." );

  okif_txt(  IP_pref_v4_mk(  &mypref, 3578872064U, 20 ) == IP_OK
         && IP_pref_b2a( &mypref, buf, 32) == IP_OK
         && strcmp(buf, "213.81.64.0/20") == 0, "pref_v4_make  ..." );
  
  okif_txt( IP_addr_e2b(&myaddr,"2001::A5D4:d8B1 ") == IP_OK
        && IP_addr_b2a(&myaddr, buf, 128) == IP_OK
        && strcmp(buf,"2001::a5d4:d8b1") == 0, "ipv6 address conversion ..."); 
 
  okif_txt( IP_pref_e2b(&mypref," 2001::a5d4:d8B1/69") == IP_OK
        && IP_pref_b2a(&mypref, buf, 128) == IP_OK
        && strcmp(buf,"2001::/69") == 0, "ipv6 prefix conversion ..." );
  
  okif_txt( IP_rang_e2b( &myrange, "2001:a5d4:d8BC:: - 2001:a5d4:d8ff::") 
                    == IP_OK
        && IP_rang_b2a(&myrange,  buf, 255) == IP_OK
        && strcmp(buf, "2001:a5d4:d8bc:: - 2001:a5d4:d8ff::") == 0,
        "IPv6 range conversion ...");

  okif_txt( IP_addr_f2b_v6(&myaddr, "1234567890", "987654321122") == IP_OK
            && IP_addr_b2v6_hi(&myaddr) == 1234567890LL
            && IP_addr_b2v6_lo(&myaddr) == 987654321122LL,
            "IP_v6 f2b address conversion");
  
   okif_txt( IP_pref_f2b_v6(&mypref, "123456789012345", 
                                     "987654321123456","127") == IP_OK
             && IP_pref_b2v6_len(&mypref) == 127
             && IP_addr_b2v6_hi(& (mypref.ip)) == 123456789012345LL
             && IP_addr_b2v6_lo(& (mypref.ip)) == 987654321123456LL,
             "IP_v6 f2b prefix conversion");
  


 
  return 0; 
} 
 

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