tests/test_tcp_src.c

Go to the documentation of this file.
00001 #include "test_tcp_src.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 #include "../engine/num_list.h"
00009 
00010 extern GlobalVars       Globals;
00011 
00012 typedef struct tcp_src_data{
00013         NumList*                Ports;
00014 } TCPSrcData;
00015 
00016 //#define DEBUG
00017 //#define DEBUGMATCH
00018 
00019 int TCPDecoderID;
00020 
00021 /******************************************
00022 * Apply the Test
00023 ******************************************/
00024 int TestTCPSrc(int PacketSlot, TestNode* Nodes){
00025         unsigned short          TCPSrc;
00026         TCPSrcData*                     data;
00027         TCPData*                        TData;
00028         TestNode*                       Node;
00029         int                                     i;
00030         PacketRec*                      p;
00031 
00032 #ifdef DEBUGPATH
00033         printf("In TestTCPSrc\n");
00034 #endif
00035 
00036 #ifdef DEBUG
00037         printf("Testing TCP Src\n");
00038 #endif  
00039         
00040         p=&Globals.Packets[PacketSlot];
00041         
00042         if (!Nodes) return FALSE;
00043         
00044         /*get the src out of the tcp header*/
00045         /*todo: make this more efficient*/
00046         for (i=p->NumDecoderData; i>=0;i--){
00047                 if (p->DecoderInfo[i].DecoderID==TCPDecoderID){
00048                         TData=(TCPData*)p->DecoderInfo[i].Data;
00049                         TCPSrc=ntohs(TData->Header->source);
00050                         break;
00051                 }
00052         }
00053         
00054         if (i==-1){
00055 #ifdef DEBUG    
00056                 printf("Couldn't find the tcp header\n");
00057 #endif          
00058                 return FALSE;
00059         }
00060 
00061 #ifdef DEBUGMATCH
00062         printf("**************************************\n");
00063         printf("Before applying tcp src tests\n");
00064         for (i=0;i<Globals.NumRules;i++)
00065         if (RuleIsActive(p,i))
00066                 printf("Rule %i is active\n",i);
00067         else
00068                 printf("Rule %i is inactive\n",i);
00069         printf("**************************************\n");
00070 #endif  
00071         
00072         Node=Nodes;
00073         while(Node){
00074                 if (RuleIsActive(PacketSlot, Node->RuleID)){
00075                         data=(TCPSrcData*)Node->Data;
00076                         if (!IsInList(data->Ports, TCPSrc)){
00077 #ifdef DEBUGMATCH
00078                                 printf("TCP Src %u doesn't match %u\n", data->tcp_src, TCPSrc);
00079 #endif                  
00080                                 SetRuleInactive(PacketSlot, Node->RuleID);
00081                         }
00082 #ifdef DEBUGMATCH                       
00083                         else{
00084                                 printf("TCP Src Matches\n");
00085                         }
00086                 }else{
00087                         printf("Rule is inactive\n");
00088 #endif                  
00089                 }
00090                 Node=Node->Next;
00091         }
00092         
00093 #ifdef DEBUGMATCH
00094         printf("**************************************\n");
00095         for (i=0;i<Globals.NumRules;i++)
00096         if (RuleIsActive(p,i))
00097                 printf("Rule %i is active\n",i);
00098         else
00099                 printf("Rule %i is inactive\n",i);
00100         printf("**************************************\n");
00101 #endif  
00102                 
00103         return TRUE;
00104 }
00105 
00106 /******************************************
00107 * Add a rule node to this test
00108 ******************************************/
00109 int TCPSrcAddNode(int TestID, int RuleID, char* Args){
00110         TCPSrcData*                     data;
00111 
00112 #ifdef DEBUGPATH
00113         printf("In TCPSrcAddNode\n");
00114 #endif
00115 
00116 #ifdef DEBUG
00117         printf("Addding a Node with args %s\n",Args);
00118 #endif
00119 
00120         data=calloc(sizeof(TCPSrcData),1);
00121 
00122         /*set up the number list*/
00123         data->Ports=InitNumList(LIST_TYPE_NORMAL);
00124         if (!AddRangesString(data->Ports, Args, NULL, 0)){
00125                 free(data);
00126                 data=NULL;
00127                 return FALSE;
00128         }
00129         
00130         return TestAddNode(TestID, RuleID, (void*)data);
00131 }
00132 
00133 /****************************************
00134 * Set up the test of the TCP Src Field
00135 *****************************************/
00136 int InitTestTCPSrc(){
00137         int     TestID;
00138 
00139 #ifdef DEBUGPATH
00140         printf("In InitTestTCPSrc\n");
00141 #endif
00142 
00143         TestID=CreateTest("TCPSrc");
00144         if (TestID==TEST_NONE) return FALSE;
00145         
00146         if (!BindTestToDecoder(TestID, "TCP")){
00147                 printf("Failed to Bind to TCP\n");
00148                 return FALSE;
00149         } 
00150         
00151         snprintf(Globals.Tests[TestID].ShortName, MAX_NAME_LEN, "src");
00152         Globals.Tests[TestID].AddNode=TCPSrcAddNode;
00153         Globals.Tests[TestID].TestFunc=TestTCPSrc;
00154         
00155         TCPDecoderID=GetDecoderByName("TCP");
00156 
00157         return TRUE;
00158 }

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2