tests/test_ip_src.c

Go to the documentation of this file.
00001 #include "test_ip_src.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 <netinet/in.h> 
00009 #include "../engine/num_list.h"
00010 
00011 extern GlobalVars       Globals;
00012 
00013 typedef struct ip_src_data{
00014         NumList*                                IPS;
00015         unsigned char                   RuleBits[MAX_RULES/8];
00016         struct ip_src_data*     Next;
00017 } IPSrcData;
00018 
00019 //#define DEBUG
00020 //#define DEBUGMATCH
00021 
00022 int IPDecoderID;
00023 IPSrcData* IPSrcHead;
00024 
00025 /******************************************
00026 * Apply the Test
00027 ******************************************/
00028 int TestIPSrc(int PacketSlot, TestNode* Nodes){
00029         unsigned long           IPSrc;
00030         IPSrcData*                      t;
00031         IPData*                         IData;
00032         int                                     i;
00033         PacketRec*                      p;
00034 
00035 #ifdef DEBUGPATH
00036         printf("In TestIPSrc\n");
00037 #endif
00038 
00039 #ifdef DEBUG
00040         printf("Testing IP Src\n");
00041 #endif  
00042         
00043         p=&Globals.Packets[PacketSlot];
00044         
00045         if (!Nodes) return FALSE;
00046         
00047         /*get the src 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         IPSrc=ntohl(IData->Header->saddr);
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 interface name tests\n");
00065         for (i=0;i<Globals.NumRules;i++)
00066         if (RuleIsActive(p,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=IPSrcHead;
00074         while (t){
00075                 if (!IsInList(t->IPS, IPSrc)){
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         printf("After applying interface name tests\n");
00085         for (i=0;i<Globals.NumRules;i++)
00086         if (RuleIsActive(p,i))
00087                 printf("Rule %i is active\n",i);
00088         else
00089                 printf("Rule %i is inactive\n",i);
00090         printf("**************************************\n");
00091 #endif  
00092                 
00093         return TRUE;
00094 }
00095 
00096 /******************************************
00097 * Add a rule node to this test
00098 ******************************************/
00099 int IPSrcAddNode(int TestID, int RuleID, char* Args){
00100         IPSrcData*                      data;
00101         IPSrcData*                      t;
00102         IPSrcData*                      last;
00103 #ifdef DEBUG    
00104         int                                     i;
00105 #endif
00106 
00107 #ifdef DEBUGPATH
00108         printf("In IPSrcAddNode\n");
00109 #endif
00110 
00111 #ifdef DEBUG
00112         printf("Addding a Node with args %s\n",Args);
00113 #endif
00114 
00115         data=calloc(sizeof(IPSrcData),1);       
00116         
00117         /*set up the number list*/
00118         data->IPS=InitNumList(LIST_TYPE_NORMAL);
00119         if (!AddIPRanges(data->IPS, Args)){
00120                 free(data);
00121                 data=NULL;
00122                 return FALSE;
00123         }
00124         
00125         /*check to see if this is a duplicate*/
00126         if (!IPSrcHead){
00127 #ifdef DEBUG
00128                 printf("First IP Source\n");
00129 #endif  
00130                 IPSrcHead=data;
00131                 SetBit(data->RuleBits, Globals.NumRules, RuleID, 1);
00132         return TestAddNode(TestID, RuleID, (void*)data);
00133         }else{
00134                 t=IPSrcHead;
00135                 last=t;
00136                 while (t){
00137                         if (NumListCompare(data->IPS, t->IPS)){
00138 #ifdef DEBUG
00139                                 printf("This is a duplicate\n");
00140 #endif                  
00141                                 DestroyNumList(data->IPS);
00142                                 free(data);
00143                                 data=NULL;
00144                                 SetBit(t->RuleBits, Globals.NumRules, RuleID, 1);
00145 #ifdef DEBUG1
00146                                 for (i=0;i<Globals.NumRules+1;i++)
00147                                 if (GetBit(t->RuleBits, Globals.NumRules, i))
00148                                 printf("Bit %i is set\n",i);
00149 #endif                          
00150                                 return TestAddNode(TestID, RuleID, (void*)t);           
00151                         }
00152                         
00153                         last=t;
00154                         t=t->Next;
00155                 }
00156                 
00157 #ifdef DEBUG
00158                 printf("This is a new one\n");
00159 #endif          
00160                 last->Next=data;
00161                 SetBit(data->RuleBits, Globals.NumRules, RuleID, 1);
00162                 return TestAddNode(TestID, RuleID, (void*)data);                
00163         }
00164 }
00165 
00166 /****************************************
00167 * Set up the test of the IP Src Field
00168 *****************************************/
00169 int InitTestIPSrc(){
00170         int     TestID;
00171 
00172 #ifdef DEBUGPATH
00173         printf("In InitTestIPSrc\n");
00174 #endif
00175 
00176         IPSrcHead=NULL;
00177 
00178         TestID=CreateTest("IPSrc");
00179         if (TestID==TEST_NONE) return FALSE;
00180         
00181         if (!BindTestToDecoder(TestID, "IP")){
00182                 printf("Failed to Bind to IP\n");
00183                 return FALSE;
00184         } 
00185         
00186         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "src");
00187         Globals.Tests[TestID].AddNode=IPSrcAddNode;
00188         Globals.Tests[TestID].TestFunc=TestIPSrc;
00189         
00190         IPDecoderID=GetDecoderByName("IP");
00191 
00192         return TRUE;
00193 }

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