00001 #include "test_ethernet_src.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_src_data{
00011 unsigned char EthernetSrc[6];
00012 } EthernetSrcData;
00013
00014 #define DEBUG
00015
00016
00017 int EthernetDecoderID;
00018
00019
00020
00021
00022 int TestEthernetSrc(int PacketSlot, TestNode* Nodes){
00023 unsigned char ESrc[6];
00024 EthernetSrcData* data;
00025 EthernetData* EData;
00026 TestNode* Node;
00027 int i;
00028 PacketRec* p;
00029
00030 #ifdef DEBUGPATH
00031 printf("In TestEthernetSrc\n");
00032 #endif
00033
00034 #ifdef DEBUG
00035 printf("Testing Ethernet Src\n");
00036 #endif
00037
00038 p=&Globals.Packets[PacketSlot];
00039
00040
00041
00042 for (i=p->NumDecoderData; i>=0;i--){
00043 if (p->DecoderInfo[i].DecoderID==EthernetDecoderID){
00044 EData=(EthernetData*)p->DecoderInfo[i].Data;
00045 ESrc[0]=EData->Header->SrcMac[0];
00046 ESrc[1]=EData->Header->SrcMac[1];
00047 ESrc[2]=EData->Header->SrcMac[2];
00048 ESrc[3]=EData->Header->SrcMac[3];
00049 ESrc[4]=EData->Header->SrcMac[4];
00050 ESrc[5]=EData->Header->SrcMac[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=(EthernetSrcData*)Node->Data;
00078 if (memcmp(data->EthernetSrc,ESrc,6)!=0){
00079 #ifdef DEBUGMATCH
00080 printf("Ethernet Src %02x:%02x:%02x:%02x:%02x:%02x does not match test value %02x:%02x:%02x:%02x:%02x:%02x\n",
00081 ESrc[0],ESrc[1],ESrc[2],ESrc[3],ESrc[4],ESrc[5],
00082 data->EthernetSrc[0],
00083 data->EthernetSrc[1],
00084 data->EthernetSrc[2],
00085 data->EthernetSrc[3],
00086 data->EthernetSrc[4],
00087 data->EthernetSrc[5]
00088 );
00089 #endif
00090 SetRuleInactive(PacketSlot, Node->RuleID);
00091 }
00092 #ifdef DEBUGMATCH
00093 else{
00094 printf("Ethernet Src 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
00119
00120 int EthernetSrcAddNode(int TestID, int RuleID, char* Args){
00121 int i;
00122 EthernetSrcData* data;
00123 unsigned char byte;
00124 char* next_byte;
00125 char* next_delim;
00126
00127 #ifdef DEBUGPATH
00128 printf("In EthernetSrcAddNode\n");
00129 #endif
00130
00131 #ifdef DEBUG
00132 printf("Addding a Node with args %s\n",Args);
00133 #endif
00134
00135 data=calloc(sizeof(EthernetSrcData),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->EthernetSrc[i]=byte;
00147 }
00148
00149 return TestAddNode(TestID, RuleID, (void*)data);
00150 }
00151
00152
00153
00154
00155 int InitTestEthernetSrc(){
00156 int TestID;
00157
00158 #ifdef DEBUGPATH
00159 printf("In InitTestInterfaceName\n");
00160 #endif
00161
00162 TestID=CreateTest("EthernetSrc");
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, "src");
00171 Globals.Tests[TestID].AddNode=EthernetSrcAddNode;
00172 Globals.Tests[TestID].TestFunc=TestEthernetSrc;
00173
00174 EthernetDecoderID=GetDecoderByName("Ethernet");
00175
00176 return TRUE;
00177 }