tests/test_ethernet_dst.c

Go to the documentation of this file.
00001 #include "test_ethernet_dst.h"
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include "../decoders/decode_ethernet.h"
00006 #include "../packets/packet.h"
00007 
00008 extern GlobalVars       Globals;
00009 
00010 typedef struct ethernet_dst_data{
00011         unsigned char   EthernetDst[6];
00012 } EthernetDstData;
00013 
00014 //#define DEBUG
00015 //#define DEBUGMATCH
00016 
00017 int EthernetDecoderID;
00018 
00019 /******************************************
00020 * Apply the Test
00021 ******************************************/
00022 int TestEthernetDst(int PacketSlot, TestNode* Nodes){
00023         unsigned char           EDst[6];
00024         EthernetDstData*        data;
00025         EthernetData*           EData;
00026         TestNode*                       Node;
00027         int                                     i;
00028         PacketRec*                      p;
00029 
00030 #ifdef DEBUGPATH
00031         printf("In TestEthernetDst\n");
00032 #endif
00033 
00034 #ifdef DEBUG
00035         printf("Testing Ethernet Dst\n");
00036 #endif  
00037         
00038         p=&Globals.Packets[PacketSlot];
00039         
00040         /*get the dst out of the ethernet header*/
00041         /*todo: make this more efficient*/
00042         for (i=p->NumDecoderData; i>=0;i--){
00043                 if (p->DecoderInfo[i].DecoderID==EthernetDecoderID){
00044                         EData=(EthernetData*)p->DecoderInfo[i].Data;
00045                         EDst[0]=EData->Header->DstMac[0];
00046                         EDst[1]=EData->Header->DstMac[1];
00047                         EDst[2]=EData->Header->DstMac[2];
00048                         EDst[3]=EData->Header->DstMac[3];
00049                         EDst[4]=EData->Header->DstMac[4];
00050                         EDst[5]=EData->Header->DstMac[5];
00051                         break;
00052                 }
00053         }
00054         
00055         if (i==-1){
00056 #ifdef DEBUG    
00057                 printf("Couldn't find the ethernet header\n");
00058 #endif          
00059                 return FALSE;
00060         }
00061 
00062 #ifdef DEBUGMATCH
00063         printf("\n\n"); 
00064         printf("**************************************\n");
00065         printf("Before applying interface name tests\n");
00066         for (i=0;i<Globals.NumRules;i++)
00067         if (RuleIsActive(p,i))
00068                 printf("Rule %i is active\n",i);
00069         else
00070                 printf("Rule %i is inactive\n",i);
00071         printf("**************************************\n");
00072 #endif  
00073         
00074         Node=Nodes;
00075         while(Node){
00076                 if (RuleIsActive(PacketSlot, Node->RuleID)){
00077                         data=(EthernetDstData*)Node->Data;
00078                         if (memcmp(data->EthernetDst,EDst,6)!=0){
00079 #ifdef DEBUGMATCH
00080                                 printf("Ethernet Dst %02x:%02x:%02x:%02x:%02x:%02x does not match test value %02x:%02x:%02x:%02x:%02x:%02x\n",
00081                                         EDst[0],EDst[1],EDst[2],EDst[3],EDst[4],EDst[5],
00082                                         data->EthernetDst[0],
00083                                         data->EthernetDst[1],
00084                                         data->EthernetDst[2],
00085                                         data->EthernetDst[3],
00086                                         data->EthernetDst[4],
00087                                         data->EthernetDst[5]
00088                                         );
00089 #endif                  
00090                                 SetRuleInactive(PacketSlot, Node->RuleID);
00091                         }
00092 #ifdef DEBUGMATCH                       
00093                         else{
00094                                 printf("Ethernet Dst Matches\n");
00095                         }
00096                 }else{
00097                         printf("Rule is inactive\n");
00098 #endif                  
00099                 }
00100                 Node=Node->Next;
00101         }
00102         
00103 #ifdef DEBUGMATCH
00104         printf("**************************************\n");
00105         printf("After applying interface name tests\n");
00106         for (i=0;i<Globals.NumRules;i++)
00107         if (RuleIsActive(p,i))
00108                 printf("Rule %i is active\n",i);
00109         else
00110                 printf("Rule %i is inactive\n",i);
00111         printf("**************************************\n");
00112 #endif  
00113                 
00114         return TRUE;
00115 }
00116 
00117 /******************************************
00118 * Add a rule node to this test
00119 ******************************************/
00120 int EthernetDstAddNode(int TestID, int RuleID, char* Args){
00121         int                             i;
00122         EthernetDstData*        data;
00123         unsigned char           byte;
00124         char*                           next_byte;
00125         char*                           next_delim;
00126         
00127 #ifdef DEBUGPATH
00128         printf("In EthernetDstAddNode\n");
00129 #endif
00130 
00131 #ifdef DEBUG
00132         printf("Addding a Node with args %s\n",Args);
00133 #endif
00134 
00135         data=calloc(sizeof(EthernetDstData),1); 
00136         
00137         for (i=0;i<6;i++){
00138                 next_delim=&Args[(i*3)+2];
00139                 if ((*next_delim !=':') && (*next_delim!=0x00)){
00140                         printf("Expected :\n");
00141                         return FALSE;
00142                 }
00143                 *next_delim=0x00;
00144                 next_byte=&Args[i*3];
00145                 byte=strtoul(next_byte, NULL, 16);
00146                 data->EthernetDst[i]=byte;
00147         }               
00148         
00149         return TestAddNode(TestID, RuleID, (void*)data);
00150 }
00151 
00152 /****************************************
00153 * Set up the test of the Ethernet Dst Field
00154 *****************************************/
00155 int InitTestEthernetDst(){
00156         int     TestID;
00157 
00158 #ifdef DEBUGPATH
00159         printf("In InitTestInterfaceName\n");
00160 #endif
00161 
00162         TestID=CreateTest("EthernetDst");
00163         if (TestID==TEST_NONE) return FALSE;
00164         
00165         if (!BindTestToDecoder(TestID, "Ethernet")){
00166                 printf("Failed to Bind to Ethernet\n");
00167                 return FALSE;
00168         } 
00169         
00170         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "dst");
00171         Globals.Tests[TestID].AddNode=EthernetDstAddNode;
00172         Globals.Tests[TestID].TestFunc=TestEthernetDst;
00173         
00174         EthernetDecoderID=GetDecoderByName("Ethernet");
00175 
00176         return TRUE;
00177 }

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