00001 #include "test_ip_dst.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 #include "../engine/bits.h"
00010
00011 extern GlobalVars Globals;
00012
00013 typedef struct ip_dst_data{
00014 NumList* IPS;
00015 unsigned char RuleBits[MAX_RULES/8];
00016 struct ip_dst_data* Next;
00017 } IPDstData;
00018
00019
00020
00021
00022 int IPDecoderID;
00023 IPDstData* IPDstHead;
00024
00025
00026
00027
00028 int TestIPDst(int PacketSlot, TestNode* Nodes){
00029 unsigned long IPDst;
00030 IPDstData* t;
00031 IPData* IData;
00032 int i;
00033 PacketRec* p;
00034
00035 DEBUGPATH;
00036
00037 if (!Nodes) return FALSE;
00038
00039 p=&Globals.Packets[PacketSlot];
00040
00041
00042 if (!GetDataByID(PacketSlot, IPDecoderID, (void**)&IData)){
00043 printf("Failed to get IP header data\n");
00044 return FALSE;
00045 }
00046
00047 IPDst=ntohl(IData->Header->daddr);
00048
00049 if (i==-1){
00050 #ifdef DEBUG
00051 printf("Couldn't find the ip header\n");
00052 #endif
00053 return FALSE;
00054 }
00055
00056 #ifdef DEBUG
00057 printf("%s->",inet_ntoa(*(struct in_addr*)&IData->Header->saddr));
00058 printf("%s\n",inet_ntoa(*(struct in_addr*)&IData->Header->daddr));
00059 printf("As int %u\n",IData->Header->daddr);
00060 printf("Host order %u\n",ntohl(IData->Header->daddr));
00061 #endif
00062
00063 #ifdef DEBUGMATCH
00064 printf("**************************************\n");
00065 printf("Before applying ip dst tests\n");
00066 for (i=0;i<Globals.NumRules;i++)
00067 if (RuleIsActive(PacketSlot,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 t=IPDstHead;
00075 while (t){
00076 if (!IsInList(t->IPS, IPDst)){
00077
00078 NotAndBitFields(p->RuleBits, t->RuleBits, p->RuleBits, Globals.NumRules);
00079 }
00080 t=t->Next;
00081 }
00082
00083 #ifdef DEBUGMATCH
00084 printf("**************************************\n");
00085 for (i=0;i<Globals.NumRules;i++)
00086 if (RuleIsActive(PacketSlot,i))
00087 printf("Rule %i is active\n",i);
00088 else
00089 printf("Rule %i is inactive\n",i);
00090 printf("**************************************\n");
00091 #endif
00092
00093 return TRUE;
00094 }
00095
00096
00097
00098
00099 int IPDstAddNode(int TestID, int RuleID, char* Args){
00100 IPDstData* data;
00101 IPDstData* t;
00102 IPDstData* last;
00103 #ifdef DEBUG
00104 int i;
00105 #endif
00106
00107 DEBUGPATH;
00108
00109 data=calloc(sizeof(IPDstData),1);
00110
00111
00112 data->IPS=InitNumList(LIST_TYPE_NORMAL);
00113 if (!AddIPRanges(data->IPS, Args)){
00114 free(data);
00115 data=NULL;
00116 return FALSE;
00117 }
00118
00119
00120 if (!IPDstHead){
00121 #ifdef DEBUG
00122 printf("First IP Dest\n");
00123 #endif
00124 IPDstHead=data;
00125 SetBit(data->RuleBits, Globals.NumRules, RuleID, 1);
00126 return TestAddNode(TestID, RuleID, (void*)data);
00127 }else{
00128 t=IPDstHead;
00129 last=t;
00130 while (t){
00131 if (NumListCompare(data->IPS, t->IPS)){
00132 #ifdef DEBUG
00133 printf("This is a duplicate\n");
00134 #endif
00135 DestroyNumList(data->IPS);
00136 free(data);
00137 data=NULL;
00138 SetBit(t->RuleBits, Globals.NumRules, RuleID, 1);
00139 #ifdef DEBUG1
00140 for (i=0;i<Globals.NumRules+1;i++)
00141 if (GetBit(t->RuleBits, Globals.NumRules, i))
00142 printf("Bit %i is set\n",i);
00143 #endif
00144 return TestAddNode(TestID, RuleID, (void*)t);
00145 }
00146
00147 last=t;
00148 t=t->Next;
00149 }
00150
00151 #ifdef DEBUG
00152 printf("This is a new one\n");
00153 #endif
00154 last->Next=data;
00155 SetBit(data->RuleBits, Globals.NumRules, RuleID, 1);
00156 return TestAddNode(TestID, RuleID, (void*)data);
00157 }
00158 }
00159
00160
00161
00162
00163 int InitTestIPDst(){
00164 int TestID;
00165
00166 #ifdef DEBUGPATH
00167 printf("In InitTestIPDst\n");
00168 #endif
00169
00170 IPDstHead=NULL;
00171
00172 TestID=CreateTest("IPDst");
00173 if (TestID==TEST_NONE) return FALSE;
00174
00175 if (!BindTestToDecoder(TestID, "IP")){
00176 printf("Failed to Bind to IP\n");
00177 return FALSE;
00178 }
00179
00180 snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "dst");
00181 Globals.Tests[TestID].AddNode=IPDstAddNode;
00182 Globals.Tests[TestID].TestFunc=TestIPDst;
00183
00184 IPDecoderID=GetDecoderByName("IP");
00185
00186 return TRUE;
00187 }