Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

sp_tcp_win_check.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 /* $Id$ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include <sys/types.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #ifdef HAVE_STRINGS_H
00029 #include <strings.h>
00030 #endif
00031 #include <ctype.h>
00032 
00033 #include "rules.h"
00034 #include "decode.h"
00035 #include "plugbase.h"
00036 #include "parser.h"
00037 #include "util.h"
00038 #include "debug.h"
00039 #include "plugin_enum.h"
00040 
00041 
00042 typedef struct _TcpWinData
00043 {
00044     u_int16_t tcp_win;
00045     u_int8_t not_flag;
00046 
00047 } TcpWinData;
00048 
00049 void TcpWinCheckInit(char *, OptTreeNode *, int);
00050 void ParseTcpWin(char *, OptTreeNode *);
00051 int TcpWinCheckEq(Packet *, struct _OptTreeNode *, OptFpList *);
00052 
00053 
00054 
00055 
00056 /****************************************************************************
00057  * 
00058  * Function: SetupTcpWinCheck()
00059  *
00060  * Purpose: Associate the window keyword with TcpWinCheckInit
00061  *
00062  * Arguments: None.
00063  *
00064  * Returns: void function
00065  *
00066  ****************************************************************************/
00067 void SetupTcpWinCheck(void)
00068 {
00069     /* map the keyword to an initialization/processing function */
00070     RegisterPlugin("window", TcpWinCheckInit);
00071 }
00072 
00073 
00074 /****************************************************************************
00075  * 
00076  * Function: TcpWinCheckInit(char *, OptTreeNode *)
00077  *
00078  * Purpose: Setup the window data struct and link the function into option
00079  *          function pointer list
00080  *
00081  * Arguments: data => rule arguments/data
00082  *            otn => pointer to the current rule option list node
00083  *
00084  * Returns: void function
00085  *
00086  ****************************************************************************/
00087 void TcpWinCheckInit(char *data, OptTreeNode *otn, int protocol)
00088 {
00089     if(protocol != IPPROTO_TCP)
00090     {
00091         FatalError("%s(%d): TCP Options on non-TCP rule\n", 
00092                    file_name, file_line);
00093     }
00094 
00095     /* multiple declaration check */ 
00096     if(otn->ds_list[PLUGIN_TCP_WIN_CHECK])
00097     {
00098         FatalError("%s(%d): Multiple TCP window options in rule\n", file_name,
00099                 file_line);
00100     }
00101         
00102     /* allocate the data structure and attach it to the
00103        rule's data struct list */
00104     otn->ds_list[PLUGIN_TCP_WIN_CHECK] = (TcpWinData *)
00105             SnortAlloc(sizeof(TcpWinData));
00106 
00107     /* this is where the keyword arguments are processed and placed into the 
00108        rule option's data structure */
00109     ParseTcpWin(data, otn);
00110 
00111     /* finally, attach the option's detection function to the rule's 
00112        detect function pointer list */
00113     AddOptFuncToList(TcpWinCheckEq, otn);
00114 }
00115 
00116 
00117 
00118 /****************************************************************************
00119  * 
00120  * Function: ParseTcpWin(char *, OptTreeNode *)
00121  *
00122  * Purpose: Convert the tos option argument to data and plug it into the 
00123  *          data structure
00124  *
00125  * Arguments: data => argument data
00126  *            otn => pointer to the current rule's OTN
00127  *
00128  * Returns: void function
00129  *
00130  ****************************************************************************/
00131 void ParseTcpWin(char *data, OptTreeNode *otn)
00132 {
00133     TcpWinData *ds_ptr;  /* data struct pointer */
00134     u_int16_t win_size;
00135 
00136     /* set the ds pointer to make it easier to reference the option's
00137        particular data struct */
00138     ds_ptr = otn->ds_list[PLUGIN_TCP_WIN_CHECK];
00139 
00140     /* get rid of any whitespace */
00141     while(isspace((int)*data))
00142     {
00143         data++;
00144     }
00145 
00146     if(data[0] == '!')
00147     {
00148         ds_ptr->not_flag = 1;
00149     }
00150 
00151     if(index(data, (int) 'x') == NULL && index(data, (int)'X') == NULL)
00152     {
00153         win_size = atoi(data);
00154     }
00155     else
00156     {
00157         if(index(data,(int)'x'))
00158         {
00159             win_size = (u_int16_t) strtol((index(data, (int)'x')+1), NULL, 16);
00160         }
00161         else
00162         {
00163             win_size = (u_int16_t) strtol((index(data, (int)'X')+1), NULL, 16);
00164         }
00165     }
00166 
00167     ds_ptr->tcp_win = htons(win_size);
00168 
00169 #ifdef DEBUG
00170     printf("TCP Window set to 0x%X\n", ds_ptr->tcp_win);
00171 #endif
00172 
00173 }
00174 
00175 
00176 /****************************************************************************
00177  * 
00178  * Function: TcpWinCheckEq(char *, OptTreeNode *)
00179  *
00180  * Purpose: Test the TCP header's window to see if its value is equal to the
00181  *          value in the rule.  
00182  *
00183  * Arguments: data => argument data
00184  *            otn => pointer to the current rule's OTN
00185  *
00186  * Returns: void function
00187  *
00188  ****************************************************************************/
00189 int TcpWinCheckEq(Packet *p, struct _OptTreeNode *otn, OptFpList *fp_list)
00190 {
00191     if(!p->tcph)
00192         return 0; /* if error occured while ip header
00193                    * was processed, return 0 automagically.
00194                    */
00195 
00196     if((((TcpWinData *)otn->ds_list[PLUGIN_TCP_WIN_CHECK])->tcp_win == p->tcph->th_win) ^ (((TcpWinData *)otn->ds_list[PLUGIN_TCP_WIN_CHECK])->not_flag))
00197     {
00198         /* call the next function in the function list recursively */
00199         return fp_list->next->OptTestFunc(p, otn, fp_list->next);
00200     }
00201 #ifdef DEBUG
00202     else
00203     {
00204         /* you can put debug comments here or not */
00205         DebugMessage(DEBUG_PLUGIN,"No match\n");
00206     }
00207 #endif
00208 
00209     /* if the test isn't successful, return 0 */
00210     return 0;
00211 }

Generated on Sun May 14 14:51:13 2006 by  doxygen 1.4.2