00001 #include "test_tcp_regex.h"
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <stdlib.h>
00005 #include "../decoders/decode_tcp.h"
00006 #include "../packets/packet.h"
00007 #include <arpa/inet.h>
00008
00009 extern GlobalVars Globals;
00010
00011 typedef struct tcp_regexp_data{
00012 unsigned char tcp_content[MAX_CONTENT_LEN];
00013 regex_t *re;
00014 } TCPRegExpData;
00015
00016
00017
00018
00019 int TCPDecoderID;
00020
00021
00022
00023
00024
00025 int TestTCPRegExp(int PacketSlot, TestNode* Nodes){
00026 PacketRec* p;
00027 TestNode* Node;
00028 TCPRegExpData* data;
00029 int result;
00030 int i;
00031
00032 #ifdef DEBUGPATH
00033 printf("In TestTCPRegExp\n");
00034 #endif
00035
00036 #ifdef DEBUG
00037 printf("Testing TCP RegExp\n");
00038 #endif
00039
00040 p=&Globals.Packets[PacketSlot];
00041
00042 if (!Nodes) return FALSE;
00043
00044 #ifdef DEBUGMATCH
00045 printf("**************************************\n");
00046 printf("Before applying tcp regexp tests\n");
00047 for (i=0;i<Globals.NumRules;i++)
00048 if (RuleIsActive(PacketSlot,i))
00049 printf("Rule %i is active\n",i);
00050 else
00051 printf("Rule %i is inactive\n",i);
00052 printf("**************************************\n");
00053 #endif
00054 Node=Nodes;
00055
00056 while (Node) {
00057
00058 if (RuleIsActive(PacketSlot, Node->RuleID)) {
00059 regex_t re;
00060
00061 data=(TCPRegExpData*)Node->Data;
00062 result=0;
00063
00064
00065
00066 result = match(p->RawPacket+p->BeginData, data->re);
00067
00068 if (result != 0)
00069 SetRuleInactive(PacketSlot, Node->RuleID);
00070
00071 }
00072
00073 Node=Node->Next;
00074
00075 }
00076
00077
00078 #ifdef DEBUGMATCH
00079 printf("**************************************\n");
00080 for (i=0;i<Globals.NumRules;i++)
00081 if (RuleIsActive(PacketSlot,i))
00082 printf("Rule %i is active\n",i);
00083 else
00084 printf("Rule %i is inactive\n",i);
00085 printf("**************************************\n");
00086 #endif
00087
00088 return TRUE;
00089 }
00090
00091
00092
00093
00094 int TCPRegExpAddNode(int TestID, int RuleID, char* Args){
00095 TCPRegExpData* data;
00096 int status;
00097
00098 #ifdef DEBUGPATH
00099 printf("In TCPRegExpAddNode\n");
00100 #endif
00101
00102 #ifdef DEBUG
00103 printf("Adding a Node with args %s\n",Args);
00104 #endif
00105
00106 data=calloc(sizeof(TCPRegExpData),1);
00107 data->re=calloc(sizeof(regex_t),1);
00108 snprintf(data->tcp_content, MAX_CONTENT_LEN, "%s", Args);
00109
00110 if((status=regcomp( data->re, data->tcp_content, REG_EXTENDED)) != 0)
00111 return(status);
00112
00113
00114 return TestAddNode(TestID, RuleID, (void*)data);
00115 }
00116
00117
00118
00119
00120 int InitTestTCPRegExp(){
00121 int TestID;
00122
00123 #ifdef DEBUGPATH
00124 printf("In InitTestTCPRegExp\n");
00125 #endif
00126
00127 TestID=CreateTest("TCPRegExp");
00128 if (TestID==TEST_NONE) return FALSE;
00129
00130 if (!BindTestToDecoder(TestID, "TCP")){
00131 printf("Failed to Bind to TCP\n");
00132 return FALSE;
00133 }
00134
00135 snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "regex");
00136 Globals.Tests[TestID].AddNode=TCPRegExpAddNode;
00137 Globals.Tests[TestID].TestFunc=TestTCPRegExp;
00138
00139
00140 TCPDecoderID=GetDecoderByName("TCP");
00141
00142 return TRUE;
00143 }