00001
00012 #include "test_tcp_listcontent.h"
00013 #include <stdio.h>
00014 #include <string.h>
00015 #include <stdlib.h>
00016 #include "../decoders/decode_tcp.h"
00017 #include "../packets/packet.h"
00018 #include "../engine/jtree.h"
00019 #include "../engine/parse_config.h"
00020 #include <arpa/inet.h>
00021
00022 extern GlobalVars Globals;
00023
00024 typedef struct tcp_content_data{
00025 unsigned char tcp_content[MAX_CONTENT_LEN];
00026 } TCPListContentData;
00027
00028
00029
00030
00031 int TCPDecoderID;
00032 JTree TCPListContentTree;
00033
00042 int TestTCPListContent(int PacketSlot, TestNode* Nodes){
00043 PacketRec* p;
00044 #ifdef DEBUGMATCH
00045 int i;
00046 #endif
00047
00048 #ifdef DEBUGPATH
00049 printf("In TestTCPListContent\n");
00050 #endif
00051
00052 #ifdef DEBUG
00053 printf("Testing TCP ListContent\n");
00054 #endif
00055
00056 p=&Globals.Packets[PacketSlot];
00057
00058 if (!Nodes) return FALSE;
00059
00060 #ifdef DEBUGMATCH
00061 printf("**************************************\n");
00062 printf("Before applying tcp no case tests\n");
00063 for (i=0;i<Globals.NumRules;i++)
00064 if (RuleIsActive(PacketSlot,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 MatchStrings(&TCPListContentTree, p->RuleBits, p->RawPacket+p->BeginData, p->PacketLen - p->BeginData);
00072
00073 #ifdef DEBUGMATCH
00074 printf("**************************************\n");
00075 for (i=0;i<Globals.NumRules;i++)
00076 if (RuleIsActive(PacketSlot,i))
00077 printf("Rule %i is active\n",i);
00078 else
00079 printf("Rule %i is inactive\n",i);
00080 printf("**************************************\n");
00081 #endif
00082
00083 return TRUE;
00084 }
00085
00095 int TCPListContentAddNode(int TestID, int RuleID, char* Args){
00096 TCPListContentData* data;
00097 FILE* listf;
00098 char LineBuff[10240];
00099
00100 #ifdef DEBUGPATH
00101 printf("In TCPListContentAddNode\n");
00102 #endif
00103
00104 listf = fopen(Args, "r");
00105 if (!listf){
00106 printf("Couldn't open file %s for 'tcp listcontent' test\n", Args);
00107 return FALSE;
00108 }
00109
00110 #ifdef DEBUG
00111 printf("Adding a Node with args from file %s\n", Args);
00112 #endif
00113
00114
00115
00116 data=NULL;
00117
00118 while (GetLine(listf, LineBuff, 10240)) {
00119 #ifdef DEBUG
00120 printf("Adding: %s\n", LineBuff);
00121 #endif
00122
00123 if (!AddStringJTree(&TCPListContentTree, LineBuff, strlen(LineBuff), RuleID)) {
00124 printf("Failed to add to tree\n");
00125
00126
00127 return FALSE;
00128 }
00129 }
00130
00131 fclose(listf);
00132
00133 return TestAddNode(TestID, RuleID, (void*)data);
00134 }
00135
00142 int TestTCPListContentFinishedSetup(){
00143 #ifdef DEBUGPATH
00144 printf("In TestTCPListContentFinishedSetup\n");
00145 #endif
00146
00147 return FinalizeJTree(&TCPListContentTree);
00148 }
00149
00150
00157 int InitTestTCPListContent() {
00158 int TestID;
00159
00160 #ifdef DEBUGPATH
00161 printf("In InitTestTCPListContent\n");
00162 #endif
00163
00164 InitJTree(&TCPListContentTree, FALSE);
00165
00166 TestID=CreateTest("TCPListContent");
00167 if (TestID==TEST_NONE) return FALSE;
00168
00169 if (!BindTestToDecoder(TestID, "TCP")){
00170 printf("Failed to Bind to TCP\n");
00171 return FALSE;
00172 }
00173
00174 snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "listcontent");
00175 Globals.Tests[TestID].AddNode=TCPListContentAddNode;
00176 Globals.Tests[TestID].TestFunc=TestTCPListContent;
00177 Globals.Tests[TestID].FinishedSetup=TestTCPListContentFinishedSetup;
00178
00179 TCPDecoderID=GetDecoderByName("TCP");
00180
00181 return TRUE;
00182 }