Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

ipobj.c

Go to the documentation of this file.
00001 /*
00002 
00003         ipobj.c
00004 
00005         IP address encapsulation interface
00006 
00007         This module provides encapsulation of single IP ADDRESSes as
00008         objects, and collections of IP ADDRESSes as objects
00009 
00010 
00011 */
00012 
00013 #ifdef HAVE_CONFIG_H
00014 #include "config.h"
00015 #endif
00016 
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <string.h>
00020 
00021 #ifndef WIN32
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #include <netinet/in.h>
00025 #include <arpa/inet.h>
00026 #endif
00027 
00028 #include <ctype.h>
00029 
00030 
00031 #include "ipobj.h"
00032 
00033 /*
00034         UITLITY SUPPORT
00035 */
00036 int  ip_familysize( int family )  /* Use with stack allocated structures */
00037 {
00038     if( family == IPV4_FAMILY ) return IPV4_LEN;
00039     if( family == IPV6_FAMILY ) return IPV6_LEN;
00040     return 0;
00041 }
00042 
00043 int ip4_sprintx( char * s, int slen, void * ip4 )
00044 {
00045      char stmp[256];
00046      int  rc;
00047      unsigned char * ip = (unsigned char *) ip4;
00048 
00049      rc = snprintf(stmp,sizeof(stmp),"%d.%d.%d.%d",ip[3],ip[2],ip[1],ip[0]);
00050 
00051      if( rc <= 0 ) return -1;
00052 
00053      if( (rc+1) > slen )
00054           return -1;
00055 
00056      strcpy(s,stmp);
00057      
00058      return 0;
00059 }
00060 int ip6_sprintx( char * s, int slen, void * ip6 )
00061 {
00062      char stmp[256];
00063      int  rc;
00064      unsigned short * ps = (unsigned short*) ip6;
00065 
00066      rc = snprintf(stmp,sizeof(stmp),"%.1x:%.1x:%.1x:%.1x:%.1x:%.1x:%.1x:%.1x",
00067              ps[7],ps[6],ps[5],ps[4],ps[3],ps[2],ps[1],ps[0]);
00068 
00069      if( rc <= 0 ) return -1;
00070 
00071      if( (rc+1) > slen )
00072           return -1;
00073 
00074      strcpy(s,stmp);
00075      
00076      return 0;
00077 }
00078 
00079 
00080 int ip_sprint( char * s, int slen, IPADDRESS * p )
00081 {
00082      if( p->family == IPV4_FAMILY )
00083      {
00084         if( ip4_sprintx( s, slen, p->ip ) )
00085             return -1;  
00086 
00087         return 0;
00088      }     
00089      else if( p->family == IPV6_FAMILY )
00090      {
00091         if( ip6_sprintx( s, slen, p->ip ) )
00092             return -1;  
00093 
00094         return 0;
00095      }     
00096      return -1;
00097 }
00098 
00099 int ip_fprint( FILE * fp, IPADDRESS * p )
00100 {
00101      int  stat;
00102      char s[256];
00103 
00104      stat = ip_sprint( s, sizeof(s), p );
00105 
00106      if( stat )
00107          return stat;
00108 
00109      fprintf(fp,"%s",s);
00110 
00111      return 0;
00112 }
00113 
00114 /*
00115         INIT FAMILY FOR IP ADDRESS
00116 */
00117 static 
00118 void ip_init ( IPADDRESS * p , int family )  /* Use with stack allocated structures */
00119 {
00120    if( p )
00121    {
00122        p->family = family;
00123    }
00124 }
00125 
00126 /*
00127   ALLOCATE/CREATE IP ADDRESS
00128 */
00129 IPADDRESS * ip_new ( int family )   /* Dynamic allocation */
00130 {
00131    IPADDRESS * p = NULL;
00132 
00133    if( family == IPV4_FAMILY )
00134    {
00135      p = malloc( sizeof(IPADDRESS) + IPV4_LEN - 1 );
00136      ip_init( p, family );
00137    }
00138    else if( family == IPV6_FAMILY )
00139    {
00140      p = malloc( sizeof(IPADDRESS) + IPV6_LEN - 1 );
00141      ip_init( p, family );
00142    }
00143    return p;
00144 }
00145 
00146 /*
00147         FREE IP ADDRESS
00148 */
00149 void ip_free ( IPADDRESS * p )
00150 {
00151      if( p )
00152          free( p );
00153 }
00154 /*
00155         Get Address Family
00156 */
00157 int ip_family( IPADDRESS * p )
00158 {
00159      return p->family;
00160 }
00161 
00162 /*
00163         Get Address size - in bytes
00164 */
00165 int ip_size( IPADDRESS * p )
00166 {
00167     return ip_familysize( p->family ) ;
00168 }
00169 
00170 /*
00171         SET IP ADDRESS
00172 */
00173 int ip_set( IPADDRESS * ia, void * ip, int family )
00174 {
00175      if( !ia ) return -1;
00176 
00177      if( ia->family != family ) return -1;
00178            
00179      if(      family == IPV4_FAMILY ) memcpy(ia->ip,ip,IPV4_LEN);
00180      else if( family == IPV6_FAMILY ) memcpy(ia->ip,ip,IPV6_LEN);
00181 
00182      return 0;
00183 }
00184 
00185 
00186 /*
00187         GET IP ADDRESS
00188 */
00189 int ip_get( IPADDRESS * ia, void * ip, int family )
00190 {
00191      if( !ia ) return -1;
00192 
00193      if( ia->family != family )
00194          return -1;
00195 
00196      if(      family == IPV4_FAMILY ) memcpy(ip,ia->ip,IPV4_LEN);
00197      else if( family == IPV6_FAMILY ) memcpy(ip,ia->ip,IPV6_LEN);
00198 
00199      return 0;
00200 }
00201 
00202 
00203 /*
00204         TEST IP ADDRESS
00205 */
00206 int ip_equal( IPADDRESS * ia, void * ip, int family )
00207 {
00208      if( !ia ) return -1;
00209 
00210      if( ia->family != family )
00211          return 0;
00212 
00213      if( ia->family == IPV4_FAMILY )
00214      {
00215          if( memcmp(ip,ia->ip,IPV4_LEN) == 0 ) 
00216              return 1;
00217      }
00218      else if( ia->family == IPV4_FAMILY )
00219      {
00220          if( memcmp(ip,ia->ip,IPV6_LEN) == 0 ) 
00221              return 1;
00222      }
00223      return 0;
00224 }
00225 
00226 int ip_eq( IPADDRESS * ia, IPADDRESS * ib )
00227 {
00228      if( !ia ) return -1;
00229      if( !ib ) return -1;
00230 
00231      if( ia->family != ib->family )
00232          return 0; /* nope */
00233 
00234      if( ia->family == IPV4_FAMILY )
00235      {
00236          if( memcmp(ib->ip,ia->ip,IPV4_LEN) == 0 ) 
00237              return 1;
00238      }
00239      else if( ia->family ==  IPV6_FAMILY )
00240      {
00241          if( memcmp(ib->ip,ia->ip,IPV6_LEN) == 0 ) 
00242              return 1;
00243      }
00244      return 0;
00245 }
00246 
00247 
00248 /*
00249 
00250 
00251    IP COLLECTION INTERFACE
00252 
00253    
00254    Snort Accepts:
00255 
00256         IP-Address              192.168.1.1
00257         IP-Address/MaskBits     192.168.1.0/24
00258         IP-Address/Mask         192.168.1.0/255.255.255.0
00259 
00260    
00261    These can all be handled via the CIDR block notation : IP/MaskBits
00262 
00263    We use collections (lists) of cidr blocks to represent address blocks
00264    and indivdual addresses.    
00265 
00266    For a single IPAddress the implied Mask is 32 bits,or 255.255.255.255, or 0xffffffff, or -1.
00267 
00268 */
00269 
00270 static 
00271 void ipset_init( IPSET * ipc )
00272 {
00273    if( ipc )
00274    {
00275      ipc->family = IPV4_FAMILY;  
00276      sflist_init( &ipc->cidr_list );
00277    }
00278 }
00279 static 
00280 void ipset6_init( IPSET * ipc )
00281 {
00282    if( ipc )
00283    {
00284      ipc->family = IPV6_FAMILY;  
00285      sflist_init( &ipc->cidr_list );
00286    }
00287 }
00288 
00289 IPSET * ipset_new( int family )
00290 {
00291    IPSET * p = (IPSET *)malloc( sizeof(IPSET));
00292 
00293    if( family == IPV4_FAMILY )
00294    {
00295      ipset_init( p );
00296    }
00297    else
00298    {
00299      ipset6_init( p );
00300    }
00301    
00302    return p;
00303 }
00304 
00305 IPSET * ipset_copy( IPSET *ipsp )
00306 {
00307    int family;
00308    IPSET * newset = NULL;
00309    CIDRBLOCK *cbp;
00310    CIDRBLOCK6 *cbp6;
00311 
00312    if(ipsp)
00313    {
00314        family = ipset_family( ipsp );
00315        newset = ipset_new(family) ;
00316    
00317        if( family == IPV4_FAMILY )
00318        {
00319            for(cbp =(CIDRBLOCK*)sflist_first( &ipsp->cidr_list );
00320                cbp !=NULL;
00321                cbp =(CIDRBLOCK*)sflist_next( &ipsp->cidr_list ) )
00322            {
00323                ipset_add(newset, &cbp->ip, &cbp->mask, cbp->notflag, family);
00324            }
00325            
00326        }
00327        else
00328        {
00329            for(cbp6 =(CIDRBLOCK6*)sflist_first( &ipsp->cidr_list );
00330                cbp6 !=NULL;
00331                cbp6 =(CIDRBLOCK6*)sflist_next( &ipsp->cidr_list ) )
00332            {
00333                ipset_add(newset, &cbp6->ip, &cbp6->mask, cbp6->notflag, family);
00334            }
00335 
00336        }
00337    }
00338 
00339    return newset;
00340 }
00341 
00342 
00343 
00344 
00345 
00346 void ipset_free( IPSET * ipc )
00347 {
00348    if( ipc )
00349    {
00350      sflist_free( &ipc->cidr_list );
00351      free( ipc );
00352    }
00353 }
00354 int ipset_family( IPSET * ipset )
00355 {
00356     return ipset->family;       
00357 }
00358 /* 
00359         The user must know what kind of address he's adding, 
00360         and the family of the IPSET
00361 */
00362 int ipset_add( IPSET * ipc, void * vip, void * vmask, int notflag , int family )
00363 {
00364 
00365     if( !ipc ) return -1;
00366 
00367     if( ipc->family != family )
00368     {
00369         return -1;
00370     }
00371 
00372     if( ipc->family == IPV4_FAMILY )
00373     {
00374         unsigned * ip=(unsigned*)vip;
00375         unsigned * mask=(unsigned*)vmask;
00376         CIDRBLOCK *p = (CIDRBLOCK*)malloc( sizeof(CIDRBLOCK) );
00377         if(!p) return -1;
00378 
00379         p->mask    = *mask;
00380         p->ip      = *ip & *mask;
00381         p->notflag = notflag;
00382 
00383         if( notflag )sflist_add_head( &ipc->cidr_list, p ); // test NOT items 1st
00384         else         sflist_add_tail( &ipc->cidr_list, p );
00385     }
00386     else if( ipc->family == IPV6_FAMILY )
00387     {
00388         int i;
00389         unsigned short * ips = (unsigned short *)vip;
00390         CIDRBLOCK6 *p6 = (CIDRBLOCK6*)malloc( sizeof(CIDRBLOCK6) );
00391         if(!p6) return -1;
00392             
00393         memcpy(p6->mask,vmask,IPV6_LEN);
00394 
00395         for(i=0;i<8;i++)
00396         {
00397             p6->ip[i] = (unsigned short)(ips[i] & p6->mask[i]);
00398         }
00399 
00400         p6->notflag = notflag;
00401 
00402         if( notflag ) sflist_add_head( &ipc->cidr_list, p6 ); // always test NOT items 1st
00403         else          sflist_add_tail( &ipc->cidr_list, p6 );
00404     }
00405     else return -1;
00406 
00407     return 0;
00408 }
00409 
00410 int ipset_contains( IPSET * ipc, void * ip, int family )
00411 {
00412     if( !ipc ) return 0;
00413 
00414     if( ipc->family != family )
00415     {
00416         return 0;
00417     }
00418 
00419     if( ipc->family == IPV4_FAMILY )
00420     {
00421         CIDRBLOCK * p;
00422         unsigned  * ipu = (unsigned*)ip;
00423 
00424         for(p =(CIDRBLOCK*)sflist_first( &ipc->cidr_list ); 
00425             p!=0;
00426             p =(CIDRBLOCK*)sflist_next( &ipc->cidr_list ) )
00427         {
00428             if( (p->mask & (*ipu)) == p->ip )
00429             {
00430                 if( p->notflag ) return 0;
00431                 return 1;
00432             }
00433         }
00434     }
00435     else if( ipc->family == IPV6_FAMILY )
00436     {
00437         CIDRBLOCK6     * p;
00438         unsigned short * ips = (unsigned short *)ip;
00439         unsigned short   mip[8];
00440 
00441 
00442         for(p = (CIDRBLOCK6*)sflist_first( &ipc->cidr_list );
00443             p!= 0;
00444             p = (CIDRBLOCK6*)sflist_next( &ipc->cidr_list ) )
00445         {
00446            
00447            mip[0] = (unsigned short)(p->mask[0] & ips[0]);
00448            mip[1] = (unsigned short)(p->mask[1] & ips[1]);
00449            mip[2] = (unsigned short)(p->mask[2] & ips[2]);
00450            mip[3] = (unsigned short)(p->mask[3] & ips[3]);
00451            mip[4] = (unsigned short)(p->mask[4] & ips[4]);
00452            mip[5] = (unsigned short)(p->mask[5] & ips[5]);
00453            mip[6] = (unsigned short)(p->mask[6] & ips[6]);
00454            mip[7] = (unsigned short)(p->mask[7] & ips[7]);
00455 
00456            if( memcmp(mip,p->ip,IPV6_LEN) == 0 )
00457            {
00458                if( p->notflag ) return 0;
00459                 return 1;
00460            }
00461         }
00462     }
00463     else return -1;
00464 
00465 
00466     return 0;
00467 }
00468 
00469 
00470 int ipset_print( IPSET * ipc )
00471 {
00472     char ip_str[80], mask_str[80];
00473 
00474     if( !ipc ) return 0;
00475 
00476     if( ipc->family == IPV4_FAMILY )
00477     {
00478         CIDRBLOCK * p;
00479 
00480         printf("IPSET-IPV4\n");
00481 
00482         for(p =(CIDRBLOCK*)sflist_first( &ipc->cidr_list );
00483             p!=0;
00484             p =(CIDRBLOCK*)sflist_next( &ipc->cidr_list ) )
00485         {
00486            ip4_sprintx(ip_str,  80, &p->ip);
00487            ip4_sprintx(mask_str,80, &p->mask);
00488 
00489            if( p->notflag )
00490                printf("CIDR BLOCK: !%s / %s\n", ip_str,mask_str);
00491            else
00492                printf("CIDR BLOCK: %s / %s\n",  ip_str,mask_str);
00493         }
00494     }
00495     else if( ipc->family == IPV6_FAMILY )
00496     {
00497         CIDRBLOCK6 * p;
00498 
00499         printf("IPSET-IPV6\n");
00500 
00501         for(p =(CIDRBLOCK6*)sflist_first( &ipc->cidr_list );
00502             p!=0;
00503             p =(CIDRBLOCK6*)sflist_next( &ipc->cidr_list ) )
00504         {
00505            ip6_sprintx(ip_str,  80,p->ip);
00506            ip6_sprintx(mask_str,80,p->mask);
00507 
00508            if( p->notflag )
00509                printf("CIDR BLOCK: !%s / %s\n", ip_str,mask_str);
00510            else
00511                printf("CIDR BLOCK: %s / %s\n",  ip_str,mask_str);
00512         }
00513     }
00514     else return -1;
00515 
00516 
00517     return 0;
00518 }
00519 
00520 /* parsing functions to help make life a bit easier */
00521 
00522 /** 
00523  * Break an IP4 Address down into its components 
00524  * 
00525  * @param ipstr string to parse
00526  * @param use network order for return values (defaults to host order)
00527  * @param not_flag return value if the ip is negated
00528  * @param host ipv4 host argument
00529  * @param mask ipv4 mask argument
00530  * 
00531  * @return 0 on sucess, else failure parsing the address
00532  * @retval -3 \0 encountered prematurely
00533  * @retval -2 strdup failed
00534  * @retval -1 null argument
00535  * @retval -4 out of range for CIDR notation
00536  */
00537 
00538 int ip4_parse(char *ipstr, int network_order, int *not_flag, unsigned *host, unsigned *mask)
00539 {
00540     char *saved, *s_copy, *maskptr;
00541     struct in_addr addrstuff;
00542     
00543     if(!ipstr || !not_flag || !host || !mask)
00544         return -1;
00545 
00546     if(*ipstr == '\0')
00547         return -3;
00548 
00549     saved = s_copy = strdup(ipstr);
00550     
00551     if(!s_copy)
00552     {
00553         return -2;
00554     }
00555     else
00556     {
00557         while(isspace((int)*s_copy))
00558             s_copy++;
00559 
00560         if(*s_copy == '\0')
00561         {
00562             free(saved);
00563             return -3;
00564         }
00565 
00566         if(*s_copy == '!')
00567         {
00568             *not_flag = 1;
00569             s_copy++;
00570 
00571             if(*s_copy == '\0')
00572             {
00573                 free(saved);
00574                 return -3;
00575             }
00576         }
00577         else
00578         {
00579             *not_flag = 0;
00580         }
00581         
00582         maskptr = strstr(s_copy, "/");
00583         
00584         if(!maskptr)
00585         {
00586             /* assume this is a host */
00587             *mask = 0xFFFFFFFF;
00588         }
00589         else
00590         {
00591             *maskptr = '\0';
00592             maskptr++;
00593         }
00594 
00595         if(!strcmp(s_copy, "0") || !strcmp(s_copy, "0.0.0.0"))
00596         {
00597             *host = 0;
00598         }
00599         else if((addrstuff.s_addr = inet_addr(s_copy)) == -1)
00600         {
00601             if(!strncmp(s_copy, "255.255.255.255", 15))
00602             {
00603                 addrstuff.s_addr = INADDR_BROADCAST;
00604             }
00605             else
00606             {
00607                 /* invalid ip address! */
00608                 free(saved);
00609                 return -3;
00610             }
00611         }
00612         else
00613         {
00614             *host = ntohl(addrstuff.s_addr);
00615         }            
00616         
00617         if(maskptr)
00618         {
00619             if(maskptr == '\0')
00620             {
00621                 /* /\0 is the representation */
00622                 free(saved);
00623                 return -3;
00624             }
00625 
00626             if(strstr(maskptr, "."))
00627             {
00628                 if(!strcmp(maskptr, "0") || !strcmp(maskptr, "0.0.0.0"))
00629                 {
00630                     *mask = 0;
00631                 }
00632                 else if((addrstuff.s_addr = inet_addr(maskptr)) == -1)
00633                 {
00634                     if(!strncmp(maskptr, "255.255.255.255", 15))
00635                     {
00636                         addrstuff.s_addr = INADDR_BROADCAST;
00637                     }
00638                     else
00639                     {
00640                         /* invalid ip address! */
00641                         free(saved);
00642                         return -3;
00643                     }
00644                 }
00645                 else
00646                 {
00647                     memcpy(mask, &addrstuff.s_addr, sizeof(unsigned));
00648                 }           
00649             }
00650             else
00651             {
00652                 int blocksize = atoi(maskptr);
00653                 int i;
00654 
00655                 if(blocksize == 0)
00656                 {
00657                     *mask = 0;
00658                 }
00659                 else if(blocksize < 1 || blocksize > 32)
00660                 {
00661                     free(saved);
00662                     return -4;
00663                 }
00664                 else
00665                 {
00666                     *mask = 0;
00667                     for(i=0;i<blocksize;i++)
00668                     {
00669                         (*mask) |= (1 << 31) >> i;
00670                     }
00671                 }
00672             }
00673         }
00674     }
00675 
00676     /* convert the arguments by default */
00677     if(network_order)
00678     {
00679         *mask = htonl(*mask);
00680         *host = htonl(*host);   
00681     }
00682     
00683     free(saved);
00684     return 0;
00685 }
00686 
00687 int ip4_setparse(IPSET *ipset, char *ipstr)
00688 {
00689     char *s_copy, *saved, *endp;
00690     int parse_count = 0;
00691     int set_not_flag = 0;
00692     int done = 0;
00693 
00694     if(!ipset || !ipstr)
00695         return -1;
00696 
00697     while(isspace((int)*ipstr) || (*ipstr == '['))
00698         ipstr++;
00699     
00700     if(*ipstr == '\0')
00701         return -3;
00702 
00703     endp = saved = s_copy = strdup(ipstr);
00704 
00705     if(!s_copy)
00706         return -2;
00707 
00708     if(*s_copy == '!')
00709         set_not_flag = 1; /* global not flag for the set */
00710 
00711     while(*s_copy != '\0' && !done)
00712     {
00713         unsigned host, mask;
00714         int      item_not_flag;
00715         
00716         while((*endp != '\0') && (*endp != ',') && (*endp != ']'))
00717         {
00718             endp++;
00719         }
00720 
00721         switch(*endp)
00722         {
00723         case '\0':
00724         case ']':
00725             done = 1;
00726             /* last cases -- fall through */
00727         case ',':
00728             if(*endp != '\0')
00729             {
00730                 *endp = '\0';
00731             }
00732             
00733             if(ip4_parse(s_copy, 0, &item_not_flag, &host, &mask) != 0)
00734             {
00735                 free(saved);
00736                 return -5;
00737             }
00738 
00739             if(ipset_add(ipset, &host, &mask,
00740                          (item_not_flag ^ set_not_flag), IPV4_FAMILY) != 0)
00741             {
00742                 free(saved);
00743                 return -6;
00744             }
00745             else
00746             {
00747                 endp++;
00748                 s_copy = endp;
00749                 parse_count++;
00750             }
00751             break;
00752         default:
00753             printf("ip4_setparse: unknown switch condition conditon: %c\n", *endp);
00754             exit(1);
00755         }
00756     }
00757 
00758     
00759     free(saved);
00760 
00761     if(!parse_count)
00762         return -7;
00763 
00764     return 0;
00765 }
00766 
00767 #ifdef MAIN_IP
00768 
00769 #include <time.h>
00770 
00771 #ifndef WIN32
00772 #define rand   random
00773 #define srand srandom
00774 #endif
00775 
00776 #define MAXIP 100     
00777 
00778 #include "sflsq.c"
00779 
00780 void test_ip4_parsing(void)
00781 {
00782     unsigned host, mask, not_flag;
00783     char **curip;
00784     int ret;
00785     IPADDRESS *adp;                
00786     char *ips[] = { "138.26.1.24",
00787                     "1.1.1.1",
00788                     "1.1.1.1/16",
00789                     "1.1.1.1/255.255.255.255",
00790                     "z/24",
00791                     "0/0",
00792                     "0.0.0.0/0.0.0.0",
00793                     "0.0.0.0/0.0.2.0",
00794                     NULL };
00795 
00796     for(curip = ips; curip[0] != NULL; curip++)
00797     {
00798         /* network byte order stuff */
00799         if((ret = ip4_parse(curip[0], 1, &not_flag, &host, &mask)) != 0)
00800         {
00801             fprintf(stderr, "Unable to parse %s with ret %d\n", curip[0], ret);
00802         }
00803         else
00804         {            
00805             printf("%c", not_flag ? '!' : ' ');            
00806             printf("%s/", inet_ntoa(*(struct in_addr *) &host));
00807             printf("%s", inet_ntoa(*(struct in_addr *) &mask));
00808             printf(" parsed successfully!\n");
00809         }
00810 
00811         /* host byte order stuff */
00812         if((ret = ip4_parse(curip[0], 0, &not_flag, &host, &mask)) != 0)
00813         {
00814             fprintf(stderr, "Unable to parse %s with ret %d\n", curip[0], ret);
00815         }
00816         else
00817         {
00818             adp = ip_new(IPV4_FAMILY);
00819             ip_set(adp, &host, IPV4_FAMILY);
00820             ip_fprint(stdout, adp);
00821             fprintf(stdout, "*****************\n");
00822             ip_free(adp);            
00823         }
00824     }
00825 
00826     return;
00827 }
00828 
00829 void test_ip4set_parsing(void)
00830 {
00831     char **curip;
00832     int ret;
00833     IPADDRESS *adp;
00834     int not_flag;
00835     int host;
00836     int mask;
00837     char *ips[] = { "12.24.24.1/32,!24.24.24.1",
00838                     "[0.0.0.0/0.0.2.0,241.242.241.22]",
00839                     "138.26.1.24",
00840                     "1.1.1.1",
00841                     "1.1.1.1/16",
00842                     "1.1.1.1/255.255.255.255",
00843                     "z/24",
00844                     "0/0",
00845                     "0.0.0.0/0.0.0.0",
00846                     "0.0.0.0/0.0.2.0",                    
00847                     NULL };
00848 
00849     for(curip = ips; curip[0] != NULL; curip++)
00850     {
00851         IPSET *ipset = ipset_new(IPV4_FAMILY);
00852         
00853         /* network byte order stuff */
00854         if((ret = ip4_setparse(ipset, curip[0])) != 0)
00855         {
00856             ipset_free(ipset);
00857             fprintf(stderr, "Unable to parse %s with ret %d\n", curip[0], ret);
00858         }
00859         else
00860         {
00861             printf("-[%s]\n ", curip[0]);
00862             ipset_print(ipset);
00863             printf("---------------------\n ");
00864         }
00865     }
00866 
00867     return;
00868 }
00869 
00870 //  -----------------------------
00871 void test_ip()
00872 {
00873      int            i,k;
00874      IPADDRESS    * ipa[MAXIP];
00875      unsigned       ipaddress,ipx;
00876      unsigned short ipaddress6[8], ipx6[8];
00877 
00878      printf("IPADDRESS testing\n");
00879 
00880      srand( time(0) );
00881 
00882      for(i=0;i<MAXIP;i++)
00883      {
00884          if( i % 2 )
00885          {
00886              ipa[i]= ip_new(IPV4_FAMILY);
00887              ipaddress = rand() * rand();
00888              ip_set( ipa[i], &ipaddress, IPV4_FAMILY  );
00889 
00890              if( !ip_equal(ipa[i],&ipaddress, IPV4_FAMILY ) )
00891                  printf("error with ip_equal\n");
00892 
00893              ip_get( ipa[i], &ipx, IPV4_FAMILY );
00894                if( ipx != ipaddress )
00895                  printf("error with ip_get\n");
00896 
00897          }
00898          else
00899          {
00900              ipa[i]= ip_new(IPV6_FAMILY);
00901 
00902              for(k=0;k<8;k++) ipaddress6[k] = rand() % (1<<16); 
00903 
00904              ip_set( ipa[i], ipaddress6, IPV6_FAMILY  );
00905 
00906              if( !ip_equal(ipa[i],&ipaddress6, IPV6_FAMILY ) )
00907                  printf("error with ip6_equal\n");
00908 
00909              ip_get( ipa[i], ipx6, IPV6_FAMILY  );
00910 
00911              for(k=0;k<8;k++)
00912                if( ipx6[k] != ipaddress6[k] )
00913                   printf("error with ip6_get\n");
00914 
00915          }
00916 
00917          printf("[%d] ",i);
00918          ip_fprint(stdout,ipa[i]);
00919          printf("\n");
00920      }
00921 
00922      printf("IP testing completed\n");
00923 }
00924 
00925 
00926 
00927 //  -----------------------------
00928 void test_ipset()
00929 {
00930      int      i,k;
00931      IPSET  * ipset, * ipset6;
00932      IPSET  * ipset_copyp, * ipset6_copyp;
00933      
00934      unsigned ipaddress, mask;
00935      unsigned short mask6[8];
00936      unsigned short ipaddress6[8];
00937 
00938      printf("IPSET testing\n");
00939 
00940      ipset  = ipset_new(IPV4_FAMILY);
00941      ipset6 = ipset_new(IPV6_FAMILY);
00942 
00943      srand( time(0) );
00944 
00945      for(i=0;i<MAXIP;i++)
00946      {
00947          if( i % 2 )
00948          {
00949              ipaddress = rand() * rand();
00950              mask = 0xffffff00;
00951 
00952              ipset_add( ipset, &ipaddress, &mask, 0, IPV4_FAMILY ); //class C cidr blocks
00953 
00954              if( !ipset_contains( ipset, &ipaddress, IPV4_FAMILY ) )
00955                  printf("error with ipset_contains\n");
00956          }
00957          else
00958          {
00959              for(k=0;k<8;k++) ipaddress6[k] = rand() % (1<<16); 
00960 
00961              for(k=0;k<8;k++) mask6[k] = 0xffff;
00962 
00963              ipset_add( ipset6, ipaddress6, mask6, 0, IPV6_FAMILY );
00964 
00965              if( !ipset_contains( ipset6, &ipaddress6, IPV6_FAMILY ) )
00966                  printf("error with ipset6_contains\n");
00967          }
00968 
00969      }
00970 
00971      ipset_copyp = ipset_copy( ipset );
00972      ipset6_copyp = ipset_copy( ipset6 );
00973      
00974 
00975      printf("-----IP SET-----\n");
00976      ipset_print( ipset );
00977      printf("\n");
00978 
00979      printf("-----IP SET6-----\n");
00980      ipset_print( ipset6 );
00981      printf("\n");
00982 
00983      printf("-----IP SET COPY -----\n");
00984      ipset_print( ipset_copyp );
00985      printf("\n");
00986 
00987      printf("-----IP SET6 COPY -----\n");
00988      ipset_print( ipset6_copyp );
00989      printf("\n");
00990 
00991      printf("IP set testing completed\n");
00992 }
00993 
00994 //  -----------------------------
00995 int main( int argc, char ** argv )
00996 {
00997   printf("ipobj \n");
00998   
00999   test_ip();
01000 
01001   test_ipset();
01002 
01003   test_ip4_parsing();
01004 
01005   test_ip4set_parsing();
01006 
01007   printf("normal pgm completion\n");
01008 
01009   return 0;
01010 }
01011 
01012 #endif
01013 

Generated on Sun May 14 14:51:18 2006 by  doxygen 1.4.2