00001 #include "test_ip_check.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
00011 extern GlobalVars Globals;
00012
00013 typedef struct ip_check_data{
00014 NumList* Checks;
00015 } IPCheckData;
00016
00017
00018
00019
00020 int IPDecoderID;
00021
00022
00023
00024
00025 int TestIPCheck(int PacketSlot, TestNode* Nodes){
00026 unsigned short IPCheck;
00027 IPCheckData* data;
00028 IPData* IData;
00029 TestNode* Node;
00030 int i;
00031 PacketRec* p;
00032
00033 #ifdef DEBUGPATH
00034 printf("In TestIPCheck\n");
00035 #endif
00036
00037 #ifdef DEBUG
00038 printf("Testing IP Check\n");
00039 #endif
00040
00041 p=&Globals.Packets[PacketSlot];
00042
00043 if (!Nodes) return FALSE;
00044
00045
00046
00047 for (i=p->NumDecoderData; i>=0;i--){
00048 if (p->DecoderInfo[i].DecoderID==IPDecoderID){
00049 IData=(IPData*)p->DecoderInfo[i].Data;
00050 IPCheck=IData->Header->check;
00051 break;
00052 }
00053 }
00054
00055 if (i==-1){
00056 #ifdef DEBUG
00057 printf("Couldn't find the ip header\n");
00058 #endif
00059 return FALSE;
00060 }
00061
00062 #ifdef DEBUGMATCH
00063 printf("**************************************\n");
00064 printf("Before applying interface name tests\n");
00065 for (i=0;i<Globals.NumRules;i++)
00066 if (RuleIsActive(p,i))
00067 printf("Rule %i is active\n",i);
00068 else
00069 printf("Rule %i is inactive\n",i);
00070 printf("**************************************\n");
00071 #endif
00072
00073 Node=Nodes;
00074 while(Node){
00075 if (RuleIsActive(PacketSlot, Node->RuleID)){
00076 data=(IPCheckData*)Node->Data;
00077 if (!IsInList(data->Checks, IPCheck)){
00078 SetRuleInactive(PacketSlot, Node->RuleID);
00079 }
00080 #ifdef DEBUGMATCH
00081 else{
00082 printf("IP Chck Matches\n");
00083 }
00084 }else{
00085 printf("Rule is inactive\n");
00086 #endif
00087 }
00088 Node=Node->Next;
00089 }
00090
00091 #ifdef DEBUGMATCH
00092 printf("**************************************\n");
00093 printf("After applying interface name tests\n");
00094 for (i=0;i<Globals.NumRules;i++)
00095 if (RuleIsActive(p,i))
00096 printf("Rule %i is active\n",i);
00097 else
00098 printf("Rule %i is inactive\n",i);
00099 printf("**************************************\n");
00100 #endif
00101
00102 return TRUE;
00103 }
00104
00105
00106
00107
00108 int IPCheckAddNode(int TestID, int RuleID, char* Args){
00109 IPCheckData* data;
00110
00111 #ifdef DEBUGPATH
00112 printf("In IPCheckAddNode\n");
00113 #endif
00114
00115 #ifdef DEBUG
00116 printf("Addding a Node with args %s\n",Args);
00117 #endif
00118
00119
00120 data=calloc(sizeof(IPCheckData),1);
00121
00122 data->Checks=InitNumList(LIST_TYPE_NORMAL);
00123
00124 if (!AddRangesString(data->Checks, Args, NULL, 0)){
00125 printf("Couldn't add data\n");
00126 free(data);
00127 return FALSE;
00128 }
00129
00130 return TestAddNode(TestID, RuleID, (void*)data);
00131 }
00132
00133
00134
00135
00136 int InitTestIPCheck(){
00137 int TestID;
00138
00139 #ifdef DEBUGPATH
00140 printf("In InitTestIPCheck\n");
00141 #endif
00142
00143 TestID=CreateTest("IPCheck");
00144 if (TestID==TEST_NONE) return FALSE;
00145
00146 if (!BindTestToDecoder(TestID, "IP")){
00147 printf("Failed to Bind to IP\n");
00148 return FALSE;
00149 }
00150
00151 snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "check");
00152 Globals.Tests[TestID].AddNode=IPCheckAddNode;
00153 Globals.Tests[TestID].TestFunc=TestIPCheck;
00154
00155 IPDecoderID=GetDecoderByName("IP");
00156
00157 return TRUE;
00158 }