tests/test_ethernet_type.c

Go to the documentation of this file.
00001 #include "test_ethernet_type.h"
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include <netinet/in.h>
00006 #include "../decoders/decode_ethernet.h"
00007 #include "../packets/packet.h"
00008 #include "../engine/num_list.h"
00009 
00010 extern GlobalVars       Globals;
00011 
00012 typedef struct ethernet_type_data{
00013         NumList*                Types;
00014 } EthernetTypeData;
00015 
00016 //#define DEBUG
00017 //#define DEBUGMATCH
00018 
00019 int EthernetDecoderID;
00020 
00021 /******************************************
00022 * Apply the Test
00023 ******************************************/
00024 int TestEthernetType(int PacketSlot, TestNode* Nodes){
00025         unsigned short          EType;
00026         EthernetTypeData*       data;
00027         EthernetData*           EData;
00028         TestNode*                       Node;
00029         int                                     i;
00030         PacketRec*                      p;
00031 
00032 #ifdef DEBUGPATH
00033         printf("In TestEthernetType\n");
00034 #endif
00035 
00036 #ifdef DEBUG
00037         printf("Testing Ethernet type\n");
00038 #endif  
00039         
00040         p=&Globals.Packets[PacketSlot];
00041         
00042         /*get the type out of the ethernet header*/
00043         /*todo: make this more efficient*/
00044         for (i=p->NumDecoderData; i>=0;i--){
00045                 if (p->DecoderInfo[i].DecoderID==EthernetDecoderID){
00046                         EData=(EthernetData*)p->DecoderInfo[i].Data;
00047                         EType=ntohs(EData->Header->Type);
00048                         break;
00049                 }
00050         }
00051         
00052         if (i==-1){
00053 #ifdef DEBUG    
00054                 printf("Couldn't find the ethernet header\n");
00055 #endif          
00056                 return FALSE;
00057         }
00058 
00059 #ifdef DEBUGMATCH
00060         printf("\n\n"); 
00061         printf("**************************************\n");
00062         printf("Before applying interface name tests\n");
00063         for (i=0;i<Globals.NumRules;i++)
00064         if (RuleIsActive(p,i))
00065                 printf("Rule %i is active\n",i);
00066         else
00067                 printf("Rule %i is inactive\n",i);
00068         printf("**************************************\n");
00069 #endif  
00070         
00071         Node=Nodes;
00072         while(Node){
00073                 if (RuleIsActive(PacketSlot, Node->RuleID)){
00074                         data=(EthernetTypeData*)Node->Data;
00075                         if (!IsInList(data->Types,EType)){
00076 #ifdef DEBUGMATCH
00077                                 printf("Ethernet Type %04x does not match test value %04x\n",EType, data->EthernetType);
00078 #endif                  
00079                                 SetRuleInactive(PacketSlot, Node->RuleID);
00080                         }
00081 #ifdef DEBUGMATCH                       
00082                         else{
00083                                 printf("Ethernet Type Matches\n");
00084                         }
00085                 }else{
00086                         printf("Rule is inactive\n");
00087 #endif                  
00088                 }
00089                 Node=Node->Next;
00090         }
00091         
00092 #ifdef DEBUGMATCH
00093         printf("**************************************\n");
00094         printf("After applying interface name tests\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 EthernetTypeAddNode(int TestID, int RuleID, char* Args){
00110         EthernetTypeData*       data;
00111         NumAlias                        Aliases[2];
00112         
00113 #ifdef DEBUGPATH
00114         printf("In EthernetTypeAddNode\n");
00115 #endif
00116 
00117 #ifdef DEBUG
00118         printf("Addding Node with args %s\n",Args);
00119 #endif
00120 
00121         sprintf(Aliases[0].Alias, "IP");
00122         Aliases[0].Num=ETHERNET_TYPE_IP;
00123         sprintf(Aliases[1].Alias, "ARP");
00124         Aliases[1].Num=ETHERNET_TYPE_ARP;
00125 
00126 
00127         data=calloc(sizeof(EthernetTypeData),1);
00128         data->Types=InitNumList(LIST_TYPE_NORMAL);
00129 
00130         if (!AddRangesString(data->Types, Args, Aliases, 2)){
00131                 printf("Couldn't add data\n");
00132                 free(data);
00133                 return FALSE;
00134         }
00135         
00136         return TestAddNode(TestID, RuleID, (void*)data);
00137 }
00138 
00139 /****************************************
00140 * Set up the test of the Ethernet Type Field
00141 *****************************************/
00142 int InitTestEthernetType(){
00143         int     TestID;
00144 
00145 #ifdef DEBUGPATH
00146         printf("In InitTestInterfaceName\n");
00147 #endif
00148 
00149         TestID=CreateTest("EthernetType");
00150         if (TestID==TEST_NONE) return FALSE;
00151         
00152         if (!BindTestToDecoder(TestID, "Ethernet")){
00153                 printf("Failed to Bind to Ethernet\n");
00154                 return FALSE;
00155         } 
00156         
00157         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "type");
00158         Globals.Tests[TestID].AddNode=EthernetTypeAddNode;
00159         Globals.Tests[TestID].TestFunc=TestEthernetType;
00160         
00161         EthernetDecoderID=GetDecoderByName("Ethernet");
00162 
00163         return TRUE;
00164 }

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