tests/test_icmp_code.c

Go to the documentation of this file.
00001 #include "test_icmp_code.h"
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include "../decoders/decode_icmp.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 icmp_code_data{
00013         NumList*                codes;
00014 } ICMPCodeData;
00015 
00016 //#define DEBUG
00017 //#define DEBUGMATCH
00018 
00019 int ICMPDecoderID;
00020 
00021 /******************************************
00022 * Apply the Test
00023 ******************************************/
00024 int TestICMPCode(int PacketSlot, TestNode* Nodes){
00025         unsigned char           ICMPCode;
00026         ICMPCodeData*           data;
00027         ICMPData*                       IData;
00028         TestNode*                       Node;
00029         int                                     i;
00030         PacketRec*                      p;
00031 
00032 #ifdef DEBUGPATH
00033         printf("In TestICMPCode\n");
00034 #endif
00035 
00036 #ifdef DEBUG
00037         printf("Testing ICMP Code\n");
00038 #endif  
00039         
00040         p=&Globals.Packets[PacketSlot];
00041         
00042         if (!Nodes) return FALSE;
00043         
00044         /*get the dst out of the ip header*/
00045         /*todo: make this more efficient*/
00046         for (i=p->NumDecoderData; i>=0;i--){
00047                 if (p->DecoderInfo[i].DecoderID==ICMPDecoderID){
00048                         IData=(ICMPData*)p->DecoderInfo[i].Data;
00049                         ICMPCode=IData->Header->code;
00050                         break;
00051                 }
00052         }
00053         
00054         if (i==-1){
00055 #ifdef DEBUG    
00056                 printf("Couldn't find the icmp header\n");
00057 #endif          
00058                 return FALSE;
00059         }
00060 
00061 #ifdef DEBUGMATCH
00062         printf("**************************************\n");
00063         printf("Before applying icmp code tests\n");
00064         for (i=0;i<Globals.NumRules;i++)
00065         if (RuleIsActive(p,i))
00066                 printf("Rule %i is active\n",i);
00067         else
00068                 printf("Rule %i is inactive\n",i);
00069         printf("**************************************\n");
00070 #endif  
00071         
00072         Node=Nodes;
00073         while(Node){
00074                 if (RuleIsActive(PacketSlot, Node->RuleID)){
00075                         data=(ICMPCodeData*)Node->Data;
00076                         if (!IsInList(data->codes, ICMPCode)){
00077 #ifdef DEBUGMATCH
00078                                 printf("ICMP Code %u doesn't match %u", data->ICMPCode, ICMPCode);
00079 #endif                  
00080                                 SetRuleInactive(PacketSlot, Node->RuleID);
00081                         }
00082 #ifdef DEBUGMATCH                       
00083                         else{
00084                                 printf("IP Dst Matches\n");
00085                         }
00086                 }else{
00087                         printf("Rule is inactive\n");
00088 #endif                  
00089                 }
00090                 Node=Node->Next;
00091         }
00092         
00093 #ifdef DEBUGMATCH
00094         printf("**************************************\n");
00095         for (i=0;i<Globals.NumRules;i++)
00096         if (RuleIsActive(p,i))
00097                 printf("Rule %i is active\n",i);
00098         else
00099                 printf("Rule %i is inactive\n",i);
00100         printf("**************************************\n");
00101 #endif  
00102                 
00103         return TRUE;
00104 }
00105 
00106 /******************************************
00107 * Add a rule node to this test
00108 ******************************************/
00109 int ICMPCodeAddNode(int TestID, int RuleID, char* Args){
00110         ICMPCodeData*           data;
00111 
00112 #ifdef DEBUGPATH
00113         printf("In ICMPCodeAddNode\n");
00114 #endif
00115 
00116 #ifdef DEBUG
00117         printf("Addding a Node with args %s\n",Args);
00118 #endif
00119 
00120         data=calloc(sizeof(ICMPCodeData),1);    
00121         data->codes=InitNumList(LIST_TYPE_NORMAL);
00122         
00123         if (!AddRangesString(data->codes, Args, NULL, 0)){
00124                 printf("Couldn't add data\n");
00125                 free(data);
00126                 return FALSE;
00127         }
00128 
00129         
00130         return TestAddNode(TestID, RuleID, (void*)data);
00131 }
00132 
00133 /****************************************
00134 * Set up the test of the ICMP Code Field
00135 *****************************************/
00136 int InitTestICMPCode(){
00137         int     TestID;
00138 
00139 #ifdef DEBUGPATH
00140         printf("In InitTestICMPCode\n");
00141 #endif
00142 
00143         TestID=CreateTest("ICMPCode");
00144         if (TestID==TEST_NONE) return FALSE;
00145         
00146         if (!BindTestToDecoder(TestID, "ICMP")){
00147                 printf("Failed to Bind to ICMP\n");
00148                 return FALSE;
00149         } 
00150         
00151         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "code");
00152         Globals.Tests[TestID].AddNode=ICMPCodeAddNode;
00153         Globals.Tests[TestID].TestFunc=TestICMPCode;
00154         
00155         ICMPDecoderID=GetDecoderByName("ICMP");
00156 
00157         return TRUE;
00158 }

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