tests/test_ip_proto.c

Go to the documentation of this file.
00001 #include "test_ip_proto.h"
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include "../decoders/decode_ip.h"
00006 #include "../packets/packet.h"
00007 #include <arpa/inet.h>
00008 #include "../engine/num_list.h"
00009 
00010 #define IP_PROTO_ICMP   1
00011 #define IP_PROTO_IGMP   2
00012 #define IP_PROTO_TCP    6
00013 #define IP_PROTO_UDP    17
00014 #define IP_PROTO_PIM    0x67
00015 #define IP_PROTO_OSPF   0x59
00016 
00017 extern GlobalVars       Globals;
00018 
00019 typedef struct ip_proto_data{
00020         NumList*        Protos;
00021 } IPProtoData;
00022 
00023 //#define DEBUG
00024 //#define DEBUGMATCH
00025 
00026 int IPDecoderID;
00027 
00028 /******************************************
00029 * Apply the Test
00030 ******************************************/
00031 int TestIPProto(int PacketSlot, TestNode* Nodes){
00032         unsigned char           IPProto;
00033         IPProtoData*            data;
00034         IPData*                         IData;
00035         TestNode*                       Node;
00036         int                                     i;
00037         PacketRec*                      p;
00038 
00039 #ifdef DEBUGPATH
00040         printf("In TestIPProto\n");
00041 #endif
00042 
00043 #ifdef DEBUG
00044         printf("Testing IP Proto\n");
00045 #endif  
00046         
00047         p=&Globals.Packets[PacketSlot];
00048         
00049         if (!Nodes) return FALSE;
00050         
00051         /*get the proto out of the ip header*/
00052         /*todo: make this more efficient*/
00053         for (i=p->NumDecoderData; i>=0;i--){
00054                 if (p->DecoderInfo[i].DecoderID==IPDecoderID){
00055                         IData=(IPData*)p->DecoderInfo[i].Data;
00056                         IPProto=IData->Header->protocol;
00057                         break;
00058                 }
00059         }
00060         
00061         if (i==-1){
00062 #ifdef DEBUG    
00063                 printf("Couldn't find the ip header\n");
00064 #endif          
00065                 return FALSE;
00066         }
00067 
00068 #ifdef DEBUGMATCH
00069         printf("**************************************\n");
00070         printf("Before applying interface name tests\n");
00071         for (i=0;i<Globals.NumRules;i++)
00072         if (RuleIsActive(p,i))
00073                 printf("Rule %i is active\n",i);
00074         else
00075                 printf("Rule %i is inactive\n",i);
00076         printf("**************************************\n");
00077 #endif  
00078         
00079         Node=Nodes;
00080         while(Node){
00081                 if (RuleIsActive(PacketSlot, Node->RuleID)){
00082                         data=(IPProtoData*)Node->Data;
00083                         if (!IsInList(data->Protos, IPProto)){
00084 #ifdef DEBUGMATCH
00085                                 printf("IP Proto %u ",data->IPProto);
00086                                 printf("does not match %u\n",IPProto);
00087 #endif                  
00088                                 SetRuleInactive(PacketSlot, Node->RuleID);
00089                         }
00090 #ifdef DEBUGMATCH                       
00091                         else{
00092                                 printf("IP Proto Matches\n");
00093                         }
00094                 }else{
00095                         printf("Rule is inactive\n");
00096 #endif                  
00097                 }
00098                 Node=Node->Next;
00099         }
00100         
00101 #ifdef DEBUGMATCH
00102         printf("**************************************\n");
00103         printf("After applying interface name tests\n");
00104         for (i=0;i<Globals.NumRules;i++)
00105         if (RuleIsActive(p,i))
00106                 printf("Rule %i is active\n",i);
00107         else
00108                 printf("Rule %i is inactive\n",i);
00109         printf("**************************************\n");
00110 #endif  
00111                 
00112         return TRUE;
00113 }
00114 
00115 /******************************************
00116 * Add a rule node to this test
00117 ******************************************/
00118 int IPProtoAddNode(int TestID, int RuleID, char* Args){
00119         IPProtoData*            data;
00120         NumAlias                        Aliases[6];
00121 
00122 #ifdef DEBUGPATH
00123         printf("In IPProtoAddNode\n");
00124 #endif
00125 
00126 #ifdef DEBUG
00127         printf("Addding a Node with args %s\n",Args);
00128 #endif
00129         
00130         snprintf(Aliases[0].Alias,512,"TCP");
00131         Aliases[0].Num=IP_PROTO_TCP;
00132         snprintf(Aliases[1].Alias,512,"UDP");
00133         Aliases[1].Num=IP_PROTO_UDP;
00134         snprintf(Aliases[2].Alias,512,"ICMP");
00135         Aliases[2].Num=IP_PROTO_ICMP;
00136         snprintf(Aliases[3].Alias,512,"IGMP");
00137         Aliases[3].Num=IP_PROTO_IGMP;
00138         snprintf(Aliases[4].Alias,512,"PIM");
00139         Aliases[4].Num=IP_PROTO_PIM;
00140         snprintf(Aliases[5].Alias,512,"OSPF");
00141         Aliases[5].Num=IP_PROTO_OSPF;
00142 
00143         data=calloc(sizeof(IPProtoData),1);     
00144         
00145         data->Protos=InitNumList(LIST_TYPE_NORMAL);
00146         
00147         if (!AddRangesString(data->Protos, Args, Aliases, 6)){
00148                 printf("Couldn't add data\n");
00149                 free(data);
00150                 return FALSE;
00151         }
00152         
00153         return TestAddNode(TestID, RuleID, (void*)data);
00154 }
00155 
00156 /****************************************
00157 * Set up the test of the IP Proto Field
00158 *****************************************/
00159 int InitTestIPProto(){
00160         int     TestID;
00161 
00162 #ifdef DEBUGPATH
00163         printf("In InitTestIPProto\n");
00164 #endif
00165 
00166         TestID=CreateTest("IPProto");
00167         if (TestID==TEST_NONE) return FALSE;
00168         
00169         if (!BindTestToDecoder(TestID, "IP")){
00170                 printf("Failed to Bind to IP\n");
00171                 return FALSE;
00172         } 
00173         
00174         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "proto");
00175         Globals.Tests[TestID].AddNode=IPProtoAddNode;
00176         Globals.Tests[TestID].TestFunc=TestIPProto;
00177         
00178         IPDecoderID=GetDecoderByName("IP");
00179 
00180         return TRUE;
00181 }

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