tests/test_udp_src.c

Go to the documentation of this file.
00001 #include "test_udp_src.h"
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include "../decoders/decode_udp.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 udp_src_data{
00014         NumList*                                Ports;
00015         unsigned char                   RuleBits[MAX_RULES/8];
00016         struct udp_src_data*    Next;
00017 } UDPSrcData;
00018 
00019 //#define DEBUG
00020 //#define DEBUGMATCH
00021 
00022 int UDPDecoderID;
00023 UDPSrcData*     UDPSrcHead;
00024 
00025 
00026 /******************************************
00027 * Apply the Test with collapsed rules
00028 ******************************************/
00029 int TestUDPSrc(int PacketSlot, TestNode* Nodes){
00030         unsigned short          UDPSrc;
00031         UDPSrcData*                     t;
00032         UDPData*                        TData;
00033         int                                     i;
00034         PacketRec*                      p;
00035 
00036 #ifdef DEBUGPATH
00037         printf("In TestUDPSrc\n");
00038 #endif
00039 
00040 #ifdef DEBUG
00041         printf("Testing UDP Src\n");
00042 #endif  
00043         
00044         if (!Nodes) return FALSE;
00045         
00046         p=&Globals.Packets[PacketSlot];
00047         
00048         /*get the src out of the udp header*/
00049         if (!GetDataByID(PacketSlot, UDPDecoderID, (void**)&TData)){
00050                 printf("Failed to get UDP header data\n");
00051                 return FALSE;
00052         }
00053 
00054         UDPSrc=ntohs(TData->Header->dest);
00055         
00056         if (i==-1){
00057 #ifdef DEBUG    
00058                 printf("Couldn't find the udp header\n");
00059 #endif          
00060                 return FALSE;
00061         }
00062 
00063 #ifdef DEBUGMATCH
00064         printf("**************************************\n");
00065         printf("Before applying udp src tests\n");
00066         for (i=0;i<Globals.NumRules;i++)
00067         if (RuleIsActive(PacketSlot,i))
00068                 printf("Rule %i is active\n",i);
00069         else
00070                 printf("Rule %i is inactive\n",i);
00071         printf("**************************************\n");
00072 #endif  
00073         
00074         t=UDPSrcHead;
00075         while (t){
00076                 if (!IsInList(t->Ports, UDPSrc)){
00077                         /*mark these rules as inactive*/
00078                         NotAndBitFields(p->RuleBits, t->RuleBits, p->RuleBits, Globals.NumRules);
00079                 }
00080                 t=t->Next;
00081         }
00082                 
00083 #ifdef DEBUGMATCH
00084         printf("**************************************\n");
00085         for (i=0;i<Globals.NumRules;i++)
00086         if (RuleIsActive(PacketSlot,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 UDPSrcAddNode(int TestID, int RuleID, char* Args){
00100         UDPSrcData*                     data;
00101         UDPSrcData*                     t;
00102         UDPSrcData*                     last;
00103 
00104 #ifdef DEBUGPATH
00105         printf("In UDPSrcAddNode\n");
00106 #endif
00107 
00108 #ifdef DEBUG
00109         printf("Addding a Node with args %s\n",Args);
00110 #endif
00111 
00112         data=calloc(sizeof(UDPSrcData),1);
00113         
00114         /*set up the number list*/
00115         data->Ports=InitNumList(LIST_TYPE_NORMAL);
00116         if (!AddRangesString(data->Ports, Args, NULL, 0)){
00117                 free(data);
00118                 data=NULL;
00119                 return FALSE;
00120         }
00121         
00122         /*check to see if this is a duplicate*/
00123         if (!UDPSrcHead){
00124 #ifdef DEBUG
00125                 printf("First UDP Dest\n");
00126 #endif  
00127                 UDPSrcHead=data;
00128                 SetBit(data->RuleBits, Globals.NumRules, RuleID, 1);
00129                 return TestAddNode(TestID, RuleID, (void*)data);
00130         }else{
00131                 t=UDPSrcHead;
00132                 last=t;
00133                 while (t){
00134                         if (NumListCompare(data->Ports, t->Ports)){
00135 #ifdef DEBUG
00136                                 printf("This is a duplicate\n");
00137 #endif                  
00138                                 DestroyNumList(data->Ports);
00139                                 free(data);
00140                                 data=NULL;
00141                                 SetBit(t->RuleBits, Globals.NumRules, RuleID, 1);
00142 #ifdef DEBUG
00143                                 for (i=0;i<Globals.NumRules+1;i++)
00144                                 if (GetBit(t->RuleBits, Globals.NumRules, i))
00145                                 printf("Bit %i is set\n",i);
00146 #endif                          
00147                                 return TestAddNode(TestID, RuleID, (void*)t);           
00148                         }
00149                         
00150                         last=t;
00151                         t=t->Next;
00152                 }
00153                 
00154 #ifdef DEBUG
00155                 printf("This is a new one\n");
00156 #endif          
00157                 last->Next=data;
00158                 SetBit(data->RuleBits, Globals.NumRules, RuleID, 1);
00159                 return TestAddNode(TestID, RuleID, (void*)data);                
00160         }
00161 }
00162 
00163 /****************************************
00164 * Set up the test of the UDP Src Field
00165 *****************************************/
00166 int InitTestUDPSrc(){
00167         int     TestID;
00168 
00169 #ifdef DEBUGPATH
00170         printf("In InitTestUDPSrc\n");
00171 #endif
00172 
00173         UDPSrcHead=NULL;
00174 
00175         TestID=CreateTest("UDPSrc");
00176         if (TestID==TEST_NONE) return FALSE;
00177         
00178         if (!BindTestToDecoder(TestID, "UDP")){
00179                 printf("Failed to Bind to UDP\n");
00180                 return FALSE;
00181         } 
00182         
00183         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "src");
00184         Globals.Tests[TestID].AddNode=UDPSrcAddNode;
00185         Globals.Tests[TestID].TestFunc=TestUDPSrc;
00186         
00187         UDPDecoderID=GetDecoderByName("UDP");
00188 
00189         return TRUE;
00190 }

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