tests/test_icmp_type.c

Go to the documentation of this file.
00001 #include "test_icmp_type.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_type_data{
00013         NumList*        types;
00014 } ICMPTypeData;
00015 
00016 //#define DEBUG
00017 //#define DEBUGMATCH
00018 
00019 int ICMPDecoderID;
00020 
00021 /******************************************
00022 * Apply the Test
00023 ******************************************/
00024 int TestICMPType(int PacketSlot, TestNode* Nodes){
00025         unsigned char           ICMPType;
00026         ICMPTypeData*           data;
00027         ICMPData*                       IData;
00028         TestNode*                       Node;
00029         int                                     i;
00030         PacketRec*                      p;
00031 
00032 #ifdef DEBUGPATH
00033         printf("In TestICMPType\n");
00034 #endif
00035 
00036 #ifdef DEBUG
00037         printf("Testing ICMP Type\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                         ICMPType=IData->Header->type;
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 type 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=(ICMPTypeData*)Node->Data;
00076                         if (!IsInList(data->types, ICMPType)){
00077 #ifdef DEBUGMATCH
00078                                 printf("ICMP Type %u doesn't match %u\n", data->icmp_type, ICMPType);
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 ICMPTypeAddNode(int TestID, int RuleID, char* Args){
00110         ICMPTypeData*           data;
00111         NumAlias                        Aliases[2];
00112 
00113 #ifdef DEBUGPATH
00114         printf("In ICMPTypeAddNode\n");
00115 #endif
00116 
00117 #ifdef DEBUG
00118         printf("Addding a Node with args %s\n",Args);
00119 #endif
00120 
00121         sprintf(Aliases[0].Alias, "Echo");
00122         Aliases[0].Num=ICMP_TYPE_ECHO;
00123         sprintf(Aliases[1].Alias, "EchoReply");
00124         Aliases[1].Num=ICMP_TYPE_ECHOREPLY;
00125 
00126         data=calloc(sizeof(ICMPTypeData),1);            
00127         data->types=InitNumList(LIST_TYPE_NORMAL);
00128         
00129         if (!AddRangesString(data->types, Args, Aliases, 2)){
00130                 printf("Couldn't add data\n");
00131                 free(data);
00132                 return FALSE;
00133         }
00134 
00135         return TestAddNode(TestID, RuleID, (void*)data);
00136 }
00137 
00138 /****************************************
00139 * Set up the test of the ICMP Type Field
00140 *****************************************/
00141 int InitTestICMPType(){
00142         int     TestID;
00143 
00144 #ifdef DEBUGPATH
00145         printf("In InitTestICMPType\n");
00146 #endif
00147 
00148         TestID=CreateTest("ICMPType");
00149         if (TestID==TEST_NONE) return FALSE;
00150         
00151         if (!BindTestToDecoder(TestID, "ICMP")){
00152                 printf("Failed to Bind to ICMP\n");
00153                 return FALSE;
00154         } 
00155         
00156         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "type");
00157         Globals.Tests[TestID].AddNode=ICMPTypeAddNode;
00158         Globals.Tests[TestID].TestFunc=TestICMPType;
00159         
00160         ICMPDecoderID=GetDecoderByName("ICMP");
00161 
00162         return TRUE;
00163 }

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