actions/action_route_sip.c

Go to the documentation of this file.
00001 #include "action_route_sip.h"
00002 #include <stdio.h>
00003 #include "../engine/message.h"
00004 #include "../decoders/decode_ip.h"
00005 #include "../routes/route_sip.h"
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #ifdef _SOLARIS_
00009 #include <strings.h>
00010 #endif
00011 #include <netinet/in.h>
00012 #include <arpa/inet.h>
00013 
00014 #define DEBUG
00015 
00016 typedef struct action_route_sip{
00017         int             Interface;
00018         int                     Timeout;
00019         int                     MaxPerSec;
00020         NumList*        LocalList;
00021 } ActionRouteSIPRec;
00022 
00023 extern GlobalVars       Globals;
00024 int IPDecoderID;
00025 
00026 /******************************************
00027 * Parse the args for this action
00028 ******************************************/
00029 void* RouteSIPParseArgs(char* Args){
00030         ActionRouteSIPRec*      data;
00031         char*                           c;
00032         char*                           c2;
00033         int                                     i;
00034         int                                     InterfaceNum;
00035 
00036 #ifdef DEBUGPATH
00037         printf("In RouteSIPParseArgs\n");
00038 #endif
00039 
00040 #ifdef DEBUG
00041         printf("Parsing args for action_route_sip\n");
00042 #endif  
00043 
00044         /*interface is the first arg*/
00045         c=strchr(Args, ',');
00046         if (!c){
00047                 printf("Expected ,\n");
00048                 return NULL;
00049         }
00050         
00051         *c=0x00;
00052         c++;
00053         
00054         for (i=0;i<Globals.NumInterfaces;i++){
00055                 if (strcasecmp(Globals.Interfaces[i].Name, Args)==0){
00056 #ifdef DEBUG
00057                         printf("Interface set to %i(%s)\n",i, Globals.Interfaces[i].Name);
00058 #endif
00059                         InterfaceNum=i;
00060                         break;
00061                 }
00062         }
00063         
00064         if (i==Globals.NumInterfaces){
00065                 printf("Error: \"%s\" is not an interface name\n",Args);
00066                 return NULL;
00067         }
00068         
00069         /*timeout is the second arg*/ 
00070         data=(ActionRouteSIPRec*)calloc(sizeof(ActionRouteSIPRec),1);
00071         data->Interface=InterfaceNum;
00072         data->Timeout=atoi(c);
00073         data->LocalList=InitNumList(LIST_TYPE_NORMAL);
00074         data->MaxPerSec=0;
00075         
00076 #ifdef DEBUG
00077         printf("Timout set to %i\n",data->Timeout);
00078 #endif  
00079                 
00080         /*Number of seconds per drop is third*/
00081         c2=strchr(c+1, ',');
00082         if (!c2){
00083                 printf("Expected ,\n");
00084                 return NULL;
00085         }
00086         
00087         *c2=0x00;
00088         c2++;
00089         
00090         data->MaxPerSec=atoi(c2);
00091         
00092 #ifdef DEBUG
00093         printf("Limiting to %i Reroutes/Sec\n",data->MaxPerSec);
00094 #endif  
00095 
00096         c2=strchr(c2+1, ',');
00097         if (!c2){
00098                 printf("Expected ,\n");
00099                 return NULL;
00100         }
00101         
00102         *c2=0x00;
00103         c2++;
00104         while (*c2==' ') c2++;
00105 
00106         /*Last is a list of non-routable*/
00107         if (!AddIPRanges(data->LocalList, c2)){
00108                 printf("Couldn't understand local IP's (%s)\n",c2);
00109                 free(data);
00110                 return NULL;
00111         }
00112         
00113         
00114         return data;
00115 }
00116 
00117 
00118 /******************************************
00119 * handle informational messages
00120 ******************************************/
00121 int RouteSIPMessage(char* Message, void* Data){
00122 
00123 #ifdef DEBUGPATH
00124         printf("In RouteSIPMessage\n");
00125 #endif
00126 
00127         return TRUE;
00128 }
00129 
00130 /******************************************
00131 * Reroute this IP
00132 ******************************************/
00133 int RouteSIPAction(int RuleNum, int PacketSlot, void* Data){
00134         ActionRouteSIPRec*      data;
00135         PacketRec*                      p;
00136         IPData*                         IP;
00137         
00138 #ifdef DEBUGPATH
00139         printf("In RouteSIPAction\n");
00140 #endif
00141 
00142 #ifdef DEBUG
00143         printf("Applying an SIP action\n");
00144 #endif
00145 
00146         if (!Data){
00147 #ifdef DEBUG
00148                 printf("I must have somewhere to route to\n");
00149 #endif  
00150                 return FALSE;
00151         }
00152                 
00153         if (!GetDataByID(PacketSlot, IPDecoderID, (void**)&IP)){
00154                 printf("This packet has no IP header\n");
00155                 return FALSE;
00156         }
00157         
00158         p=&Globals.Packets[PacketSlot];
00159         data=(ActionRouteSIPRec*)Data;
00160 
00161         if (IsInList(data->LocalList, ntohl(IP->Header->saddr))){
00162 #ifdef DEBUG
00163                 printf("This is in the local list\n");
00164 #endif  
00165                 return FALSE;
00166         }
00167                         
00168         /*check to see if we're too fast*/
00169                         
00170                         
00171         return RouteSIPAdd(IP->Header->saddr, data->Interface, Globals.Packets[PacketSlot].tv.tv_sec+data->Timeout);
00172 }
00173 
00174 /********************************
00175 * Set up the sip routing stuff
00176 ********************************/
00177 int InitActionRouteSIP(){
00178         int ActionID;
00179 
00180 #ifdef DEBUGPATH
00181         printf("In InitActionRouteSIP\n");
00182 #endif
00183 
00184         ActionID=CreateAction("route sip");
00185         if (ActionID==ACTION_NONE){
00186 #ifdef DEBUG
00187                 printf("Couldn't allocation action route sip\n");
00188 #endif  
00189                 return FALSE;
00190         }
00191         
00192         Globals.ActionItems[ActionID].ActionFunc=RouteSIPAction;
00193         Globals.ActionItems[ActionID].MessageFunc=RouteSIPMessage;
00194         Globals.ActionItems[ActionID].ParseArgs=RouteSIPParseArgs;
00195 
00196         IPDecoderID=GetDecoderByName("IP");
00197 
00198         return TRUE;
00199 }

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