tests/test_udp_dst.c

Go to the documentation of this file.
00001 #include "test_udp_dst.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 
00010 extern GlobalVars       Globals;
00011 
00012 typedef struct udp_dst_data{
00013         NumList*                Ports;
00014 } UDPDstData;
00015 
00016 //#define DEBUG
00017 //#define DEBUGMATCH
00018 
00019 int UDPDecoderID;
00020 
00021 /******************************************
00022 * Apply the Test
00023 ******************************************/
00024 int TestUDPDst(int PacketSlot, TestNode* Nodes){
00025         unsigned short          UDPDst;
00026         UDPDstData*                     data;
00027         UDPData*                        TData;
00028         TestNode*                       Node;
00029         PacketRec*                      p;
00030 
00031 #ifdef DEBUGPATH
00032         printf("In TestUDPDst\n");
00033 #endif
00034 
00035 #ifdef DEBUG
00036         printf("Testing UDP Dst\n");
00037 #endif  
00038         
00039         if (!Nodes) return FALSE;
00040         
00041         p=&Globals.Packets[PacketSlot];
00042         
00043         /*get the dst out of the udp header*/
00044         /*todo: make this more efficient*/
00045         if (!GetDataByID(PacketSlot, UDPDecoderID, (void**)&TData)){
00046 #ifdef DEBUG
00047                 printf("This isn't a udp packet\n");
00048 #endif  
00049                 return FALSE;
00050         }
00051         
00052         UDPDst=ntohs(TData->Header->dest);
00053         
00054 #ifdef DEBUGMATCH
00055         printf("**************************************\n");
00056         printf("Before applying udp dst tests\n");
00057         for (i=0;i<Globals.NumRules;i++)
00058         if (RuleIsActive(p,i))
00059                 printf("Rule %i is active\n",i);
00060         else
00061                 printf("Rule %i is inactive\n",i);
00062         printf("**************************************\n");
00063 #endif  
00064         
00065         Node=Nodes;
00066         while(Node){
00067                 if (RuleIsActive(PacketSlot, Node->RuleID)){
00068                         data=(UDPDstData*)Node->Data;
00069                         if (!IsInList(data->Ports, UDPDst)){
00070 #ifdef DEBUGMATCH
00071                                 printf("UDP Dst %u doesn't match\n", UDPDst);
00072 #endif                  
00073                                 SetRuleInactive(PacketSlot, Node->RuleID);
00074                         }
00075 #ifdef DEBUGMATCH                       
00076                         else{
00077                                 printf("UDP Dst Matches\n");
00078                         }
00079                 }else{
00080                         printf("Rule is inactive\n");
00081 #endif                  
00082                 }
00083                 Node=Node->Next;
00084         }
00085         
00086 #ifdef DEBUGMATCH
00087         printf("**************************************\n");
00088         for (i=0;i<Globals.NumRules;i++)
00089         if (RuleIsActive(p,i))
00090                 printf("Rule %i is active\n",i);
00091         else
00092                 printf("Rule %i is inactive\n",i);
00093         printf("**************************************\n");
00094 #endif  
00095                 
00096         return TRUE;
00097 }
00098 
00099 /******************************************
00100 * Add a rule node to this test
00101 ******************************************/
00102 int UDPDstAddNode(int TestID, int RuleID, char* Args){
00103         UDPDstData*                     data;
00104 
00105 #ifdef DEBUGPATH
00106         printf("In UDPDstAddNode\n");
00107 #endif
00108 
00109 #ifdef DEBUG
00110         printf("Adding a Node with args %s\n",Args);
00111 #endif
00112 
00113         data=calloc(sizeof(UDPDstData),1);      
00114         
00115         /*set up the number list*/
00116         data->Ports=InitNumList(LIST_TYPE_NORMAL);
00117         if (!AddRangesString(data->Ports, Args, NULL, 0)){
00118                 free(data);
00119                 data=NULL;
00120                 return FALSE;
00121         }
00122                         
00123         return TestAddNode(TestID, RuleID, (void*)data);
00124 }
00125 
00126 /****************************************
00127 * Set up the test of the UDP Dst Field
00128 *****************************************/
00129 int InitTestUDPDst(){
00130         int     TestID;
00131 
00132 #ifdef DEBUGPATH
00133         printf("In InitTestUDPDst\n");
00134 #endif
00135 
00136         TestID=CreateTest("UDPDst");
00137         if (TestID==TEST_NONE) return FALSE;
00138         
00139         if (!BindTestToDecoder(TestID, "UDP")){
00140                 printf("Failed to Bind to UDP\n");
00141                 return FALSE;
00142         } 
00143         
00144         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "dst");
00145         Globals.Tests[TestID].AddNode=UDPDstAddNode;
00146         Globals.Tests[TestID].TestFunc=TestUDPDst;
00147         
00148         UDPDecoderID=GetDecoderByName("UDP");
00149 
00150         return TRUE;
00151 }

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