tests/test_udp_regex.c

Go to the documentation of this file.
00001 #include "test_udp_regex.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 
00009 extern GlobalVars       Globals;
00010 
00011 typedef struct udp_regexp_data{
00012         unsigned char   udp_content[MAX_CONTENT_LEN];
00013         regex_t            *re;
00014 } UDPRegExpData;
00015 
00016 //#define DEBUG
00017 //#define DEBUGMATCH
00018 
00019 int UDPDecoderID;
00020 
00021 
00022 /******************************************
00023 * Apply the Test
00024 ******************************************/
00025 int TestUDPRegExp(int PacketSlot, TestNode* Nodes){
00026         PacketRec* p;
00027         TestNode* Node;
00028         UDPRegExpData* data;
00029         int result;
00030         int i;
00031 
00032 #ifdef DEBUGPATH
00033         printf("In TestUDPRegExp\n");
00034 #endif
00035 
00036 #ifdef DEBUG
00037         printf("Testing UDP RegExp\n");
00038 #endif  
00039 
00040         p=&Globals.Packets[PacketSlot];
00041         
00042         if (!Nodes) return FALSE;
00043         
00044 #ifdef DEBUGMATCH
00045         printf("**************************************\n");
00046         printf("Before applying udp regexp tests\n");
00047         for (i=0;i<Globals.NumRules;i++)
00048         if (RuleIsActive(PacketSlot,i))
00049                 printf("Rule %i is active\n",i);
00050         else
00051                 printf("Rule %i is inactive\n",i);
00052         printf("**************************************\n");
00053 #endif  
00054         Node=Nodes;
00055 
00056         while (Node) {
00057          
00058           if (RuleIsActive(PacketSlot, Node->RuleID)) {
00059                 regex_t   re;
00060 
00061                 data=(UDPRegExpData*)Node->Data;
00062                 result=0;
00063 
00064                 //regfree(&re);
00065 
00066                 result = match(p->RawPacket+p->BeginData, data->re);
00067 
00068                 if (result != 0)
00069                         SetRuleInactive(PacketSlot, Node->RuleID);
00070 
00071           }
00072 
00073                 Node=Node->Next;
00074         
00075         }
00076 
00077         
00078 #ifdef DEBUGMATCH
00079         printf("**************************************\n");
00080         for (i=0;i<Globals.NumRules;i++)
00081         if (RuleIsActive(PacketSlot,i))
00082                 printf("Rule %i is active\n",i);
00083         else
00084                 printf("Rule %i is inactive\n",i);
00085         printf("**************************************\n");
00086 #endif  
00087                 
00088         return TRUE;
00089 }
00090 
00091 /******************************************
00092 * Add a rule node to this tets
00093 ******************************************/
00094 int UDPRegExpAddNode(int TestID, int RuleID, char* Args){
00095         UDPRegExpData* data;
00096         int status;
00097 
00098 #ifdef DEBUGPATH
00099         printf("In UDPRegExpAddNode\n");
00100 #endif
00101 
00102 #ifdef DEBUG
00103         printf("Adding a Node with args %s\n",Args);
00104 #endif
00105 
00106         data=calloc(sizeof(UDPRegExpData),1);
00107         data->re=calloc(sizeof(regex_t),1);
00108         snprintf(data->udp_content, MAX_CONTENT_LEN, "%s", Args);
00109         
00110         if((status=regcomp( data->re, data->udp_content, REG_EXTENDED)) != 0)
00111         return(status);
00112         
00113         //data = regular expression
00114         return TestAddNode(TestID, RuleID, (void*)data); 
00115 }
00116 
00117 /****************************************
00118 * Set up the test of the UDP RE
00119 *****************************************/
00120 int InitTestUDPRegExp(){
00121         int TestID;
00122 
00123 #ifdef DEBUGPATH
00124         printf("In InitTestUDPRegExp\n");
00125 #endif
00126 
00127         TestID=CreateTest("UDPRegExp");
00128         if (TestID==TEST_NONE) return FALSE;
00129         
00130         if (!BindTestToDecoder(TestID, "UDP")){
00131                 printf("Failed to Bind to UDP\n");
00132                 return FALSE;
00133         } 
00134         
00135         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "regex");
00136         Globals.Tests[TestID].AddNode=UDPRegExpAddNode;
00137         Globals.Tests[TestID].TestFunc=TestUDPRegExp;
00138 
00139         
00140         UDPDecoderID=GetDecoderByName("UDP");
00141 
00142         return TRUE;
00143 }

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