routes/route_sip.c

Go to the documentation of this file.
00001 /***************************************************
00002 * Sends all packets with a particular SIP out a 
00003 * particular interface
00004 ****************************************************/
00005 
00006 #include "route_sip.h"
00007 #include <string.h>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include "../packets/packet.h"
00011 #include "../engine/num_list.h"
00012 #include "../decoders/decode.h"
00013 #include "../decoders/decode_ip.h"
00014 #include "../decoders/decode_arp.h"
00015 #include <arpa/inet.h>
00016 #include <netinet/in.h>
00017 #include "../routes/route_sip.h"
00018 #ifdef _SOLARIS_
00019 #include <strings.h>
00020 #endif
00021 
00022 int                     IPDecoderID;
00023 int                     ARPDecoderID;
00024 NumList*        SInterfaces[MAX_INTERFACES];
00025 
00026 //#define DEBUG
00027 
00028 extern GlobalVars       Globals;
00029 
00030 /*******************************************
00031 * Given a SIP, send it out the interface
00032 *******************************************/
00033 int RouteSIP(int PacketSlot){
00034         ARPData*                AData;
00035         IPData*                 IData;
00036         PacketRec*              p;
00037         int                             i;
00038         int                             Timeout;
00039         unsigned int    IP;
00040 
00041 #ifdef DEBUGPATH
00042         printf("In RouteSIP\n");
00043 #endif
00044 
00045         p=&Globals.Packets[PacketSlot];
00046         
00047         if (GetDataByID(PacketSlot, IPDecoderID, (void**)&IData)){
00048 #ifdef DEBUG
00049                 printf("%s->",inet_ntoa(*(struct in_addr*)&IData->Header->saddr));
00050                 printf("%s\n",inet_ntoa(*(struct in_addr*)&IData->Header->daddr));
00051                 printf("This packet is from %i(%s)\n",p->InterfaceNum, Globals.Interfaces[p->InterfaceNum].Name);
00052 #endif
00053                 IP=ntohl(IData->Header->saddr);
00054         }else if (GetDataByID(PacketSlot, ARPDecoderID, (void**)&AData)){
00055                 if (AData->Header->Operation!=ARP_OP_REPLY){
00056 #ifdef DEBUG
00057                         printf("Wasn't an arp reply\n");
00058 #endif          
00059                         return ROUTE_RESULT_CONTINUE;
00060                 }
00061                 
00062                 IP=ntohl(*(unsigned int*)&AData->EthernetARPHeader->SenderIP[0]);
00063 #ifdef DEBUG
00064                 printf("Arp Reply from %s\n",inet_ntoa(*(struct in_addr*)&IP));
00065 #endif          
00066         }else{
00067 #ifdef DEBUG
00068                 printf("This isn't an ip packet or an arp packet\n");
00069 #endif          
00070                 return ROUTE_RESULT_CONTINUE;
00071         }
00072 
00073 
00074         /*TODO: Make this more efficient*/
00075 /*replacing logic with a time list*/
00076 /*
00077         for (i=0;i<Globals.NumInterfaces;i++){
00078                 if (SInterfaces[i]){
00079                         if (IsInListUser(SInterfaces[i], IP,(void**)&Timeout )){
00080                                 if (Timeout!=0 && Timeout<Globals.Packets[PacketSlot].tv.tv_sec){
00081 #ifdef DEBUG
00082                                         printf("This route has expired\n");
00083 #endif                          
00084                                         RemoveFromList(SInterfaces[i], ntohl(IData->Header->saddr));
00085                                         return ROUTE_RESULT_CONTINUE;
00086                                 }else{
00087 #ifdef DEBUG
00088                                         printf("Found a SIP route for this packet.  Going out interface %i(%s)\n",i,Globals.Interfaces[i].Name);
00089 #endif  
00090                                         p->TargetInterface=i;
00091                                         return ROUTE_RESULT_DONE;
00092                                 }
00093                         }
00094                 }
00095         }
00096 */      
00097 
00098 #ifdef DEBUG
00099         printf("There isn't a route set for this SIP\n");
00100 #endif  
00101         return ROUTE_RESULT_CONTINUE;
00102 }
00103 
00104 /*********************************
00105 * Add some members to the sip list
00106 * First arg is the interface
00107 * followed by the dest IP's
00108 **********************************/
00109 int RouteSIPAddNode(int RouteID, char* Args){
00110         int             i;
00111         char*   sp;
00112         int             InterfaceNum;
00113         
00114 #ifdef DEBUGPATH
00115         printf("In RouteSIPAddNode\n");
00116 #endif
00117         
00118 #ifdef DEBUG    
00119         printf("Adding with args %s\n",Args);
00120 #endif
00121 
00122         /*first pop off the interface*/
00123         sp=strchr(Args, ' ');
00124         if (!sp){
00125                 printf("Expected Interface Name\nFormat: SIP(<interface> <sip>,<sip>,.....)\n");
00126                 return FALSE;
00127         }
00128         
00129         *sp=0x00;
00130         sp++;
00131         while (*sp==' ') sp++;
00132         if (sp==0x00){
00133                 printf("Expected Destination IP\nFormat: SIP(<interface> <sip>,<sip>,.....)\n");
00134                 return FALSE;   
00135         }
00136         
00137         /*see if that interface exists*/
00138 #ifdef DEBUG
00139         printf("Searching for interface \"%s\"\n",Args);
00140 #endif  
00141         for (i=0;i<Globals.NumInterfaces;i++){
00142                 if (strcasecmp(Args, Globals.Interfaces[i].Name)==0){
00143                         InterfaceNum=i;
00144                         break;
00145                 }
00146         }
00147 
00148         if (i==Globals.NumInterfaces){
00149                 printf("Unknown Interface \"%s\"\n",Args);
00150                 return FALSE;
00151         }
00152         
00153         if (!SInterfaces[InterfaceNum]){
00154                 SInterfaces[InterfaceNum]=InitNumList(LIST_TYPE_NORMAL);
00155         }
00156         
00157         if (!AddIPRanges(SInterfaces[InterfaceNum], sp)){
00158                 printf("Failed to parse IP list \"%s\"\n",sp);
00159                 return FALSE;
00160         }
00161                 
00162         return TRUE;
00163 }
00164 
00165 /****************************************
00166 * Set up everything to do sip routing
00167 ****************************************/
00168 int InitRouteSIP(){
00169         int RouteID;
00170         
00171 #ifdef DEBUGPATH
00172         printf("In InitRoutSIP\n");
00173 #endif  
00174 
00175         bzero(SInterfaces, sizeof(NumList*) * MAX_INTERFACES);
00176         
00177         if ( (RouteID=CreateRoute("SIP"))==ROUTE_NONE){
00178                 printf("Couldn't create route SIP\n");
00179                 return FALSE;
00180         }
00181         
00182         Globals.Routes[RouteID].RouteFunc=RouteSIP;
00183         Globals.Routes[RouteID].AddNode=RouteSIPAddNode;
00184         
00185         if ( (IPDecoderID=GetDecoderByName("IP"))==DECODER_NONE){
00186                 printf("Couldn't find the IP Deoder\n");
00187                 return FALSE;
00188         }
00189 
00190         if ( (ARPDecoderID=GetDecoderByName("ARP"))==DECODER_NONE){
00191                 printf("Couldn't find the ARP Deoder\n");
00192                 return FALSE;
00193         }
00194         
00195         return TRUE;
00196 }
00197 
00198 /************************************************
00199 * Add a temporary/permanent SIP route
00200 *************************************************/
00201 int RouteSIPAdd(unsigned int SIP, int Interface, long UntilTime){
00202 #ifdef DEBUGPATH
00203         printf("In RouteSIPAdd\n");
00204 #endif
00205         
00206 #ifdef DEBUG
00207         printf("Rerouting %s\n", inet_ntoa(*(struct in_addr*)&SIP));
00208 #endif  
00209         
00210         if (!SInterfaces[Interface]){
00211                 SInterfaces[Interface]=InitNumList(LIST_TYPE_NORMAL);
00212 #ifdef DEBUG            
00213                 printf("This is the first for this interface\n");
00214 #endif          
00215                 return FALSE;
00216         }
00217         
00218         if (IsInList(SInterfaces[Interface], htonl(SIP))){
00219 #ifdef DEBUG
00220                 printf("This IP was already rerouted\n");
00221 #endif  
00222                 return TRUE;
00223         }
00224         
00225 /*replacing with a time list*/
00226 /*      if (!AddRangeUser(SInterfaces[Interface], htonl(SIP), htonl(SIP), (void*)UntilTime)){
00227                 printf("Couldn't add route\n");
00228                 return FALSE;
00229         }
00230 */
00231         /*mark route_sip as active in case it's not*/
00232         Globals.Routes[GetRouteByName("SIP")].Active=TRUE;
00233 
00234         return TRUE;
00235 }

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