tests/test_ip_ttl.c

Go to the documentation of this file.
00001 #include "test_ip_ttl.h"
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include "../decoders/decode_ip.h"
00006 #include "../packets/packet.h"
00007 #include <arpa/inet.h>
00008 #include "../engine/num_list.h"
00009 #include "../engine/bits.h"
00010 
00011 extern GlobalVars       Globals;
00012 
00013 typedef struct ip_ttl_data{
00014         NumList*                                TTLs;
00015         unsigned char                   RuleBits[MAX_RULES/8];
00016         struct ip_ttl_data*     Next;
00017 } IPTTLData;
00018 
00019 //#define DEBUG
00020 //#define DEBUGMATCH
00021 
00022 int IPDecoderID;
00023 IPTTLData*      IPTTLHead;
00024 
00025 /******************************************
00026 * Apply the Test with collapsed rules
00027 ******************************************/
00028 int TestIPTTL(int PacketSlot, TestNode* Nodes){
00029         unsigned char           IPTTL;
00030         IPTTLData*                      t;
00031         IPData*                         IData;
00032         int                                     i;
00033         PacketRec*                      p;
00034 
00035 #ifdef DEBUGPATH
00036         printf("In TestIPTTL\n");
00037 #endif
00038 
00039 #ifdef DEBUG
00040         printf("Testing IP TTL\n");
00041 #endif  
00042         
00043         if (!Nodes) return FALSE;
00044         
00045         p=&Globals.Packets[PacketSlot];
00046         
00047         /*get the ttl out of the ip header*/
00048         if (!GetDataByID(PacketSlot, IPDecoderID, (void**)&IData)){
00049                 printf("Failed to get IP header data\n");
00050                 return FALSE;
00051         }
00052 
00053         IPTTL=IData->Header->ttl;
00054         
00055         if (i==-1){
00056 #ifdef DEBUG    
00057                 printf("Couldn't find the ip header\n");
00058 #endif          
00059                 return FALSE;
00060         }
00061 
00062 #ifdef DEBUGMATCH
00063         printf("**************************************\n");
00064         printf("Before applying ip ttl tests\n");
00065         for (i=0;i<Globals.NumRules;i++)
00066         if (RuleIsActive(PacketSlot,i))
00067                 printf("Rule %i is active\n",i);
00068         else
00069                 printf("Rule %i is inactive\n",i);
00070         printf("**************************************\n");
00071 #endif  
00072         
00073         t=IPTTLHead;
00074         while (t){
00075                 if (!IsInList(t->TTLs, IPTTL)){
00076                         /*mark these rules as inactive*/
00077                         NotAndBitFields(p->RuleBits, t->RuleBits, p->RuleBits, Globals.NumRules);
00078                 }
00079                 t=t->Next;
00080         }
00081                 
00082 #ifdef DEBUGMATCH
00083         printf("**************************************\n");
00084         for (i=0;i<Globals.NumRules;i++)
00085         if (RuleIsActive(PacketSlot,i))
00086                 printf("Rule %i is active\n",i);
00087         else
00088                 printf("Rule %i is inactive\n",i);
00089         printf("**************************************\n");
00090 #endif  
00091                 
00092         return TRUE;
00093 }
00094 
00095 /******************************************
00096 * Add a rule node to this test
00097 ******************************************/
00098 int IPTTLAddNode(int TestID, int RuleID, char* Args){
00099         IPTTLData*                      data;
00100         IPTTLData*                      t;
00101         IPTTLData*                      last;
00102 #ifdef DEBUG    
00103         int                                     i;
00104 #endif
00105 
00106 #ifdef DEBUGPATH
00107         printf("In IPTTLAddNode\n");
00108 #endif
00109 
00110 #ifdef DEBUG
00111         printf("Addding a Node with args %s\n",Args);
00112 #endif
00113 
00114         data=calloc(sizeof(IPTTLData),1);
00115         
00116         /*set up the number list*/
00117         data->TTLs=InitNumList(LIST_TYPE_NORMAL);
00118         if (!AddIPRanges(data->TTLs, Args)){
00119                 free(data);
00120                 data=NULL;
00121                 return FALSE;
00122         }
00123         
00124         /*check to see if this is a duplicate*/
00125         if (!IPTTLHead){
00126 #ifdef DEBUG
00127                 printf("First IP Dest\n");
00128 #endif  
00129                 IPTTLHead=data;
00130                 SetBit(data->RuleBits, Globals.NumRules, RuleID, 1);
00131                 return TestAddNode(TestID, RuleID, (void*)data);
00132         }else{
00133                 t=IPTTLHead;
00134                 last=t;
00135                 while (t){
00136                         if (NumListCompare(data->TTLs, t->TTLs)){
00137 #ifdef DEBUG
00138                                 printf("This is a duplicate\n");
00139 #endif                  
00140                                 DestroyNumList(data->TTLs);
00141                                 free(data);
00142                                 data=NULL;
00143                                 SetBit(t->RuleBits, Globals.NumRules, RuleID, 1);
00144 #ifdef DEBUG1
00145                                 for (i=0;i<Globals.NumRules+1;i++)
00146                                 if (GetBit(t->RuleBits, Globals.NumRules, i))
00147                                 printf("Bit %i is set\n",i);
00148 #endif                          
00149                                 return TestAddNode(TestID, RuleID, (void*)t);           
00150                         }
00151                         
00152                         last=t;
00153                         t=t->Next;
00154                 }
00155                 
00156 #ifdef DEBUG
00157                 printf("This is a new one\n");
00158 #endif          
00159                 last->Next=data;
00160                 SetBit(data->RuleBits, Globals.NumRules, RuleID, 1);
00161                 return TestAddNode(TestID, RuleID, (void*)data);                
00162         }
00163 }
00164 
00165 /****************************************
00166 * Set up the test of the IP TTL Field
00167 *****************************************/
00168 int InitTestIPTTL(){
00169         int     TestID;
00170 
00171 #ifdef DEBUGPATH
00172         printf("In InitTestIPTTL\n");
00173 #endif
00174 
00175         IPTTLHead=NULL;
00176 
00177         TestID=CreateTest("IPTTL");
00178         if (TestID==TEST_NONE) return FALSE;
00179         
00180         if (!BindTestToDecoder(TestID, "IP")){
00181                 printf("Failed to Bind to IP\n");
00182                 return FALSE;
00183         } 
00184         
00185         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "ttl");
00186         Globals.Tests[TestID].AddNode=IPTTLAddNode;
00187         Globals.Tests[TestID].TestFunc=TestIPTTL;
00188         
00189         IPDecoderID=GetDecoderByName("IP");
00190 
00191         return TRUE;
00192 }

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2