NETWORK ATTACKS FRAMEWORK  1.0.0
A NETwork Attacks framework. Making network attacks impact evaluation easier!
NA_locality.cc
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * Copyright (C) 2001 Uppsala University & Ericsson AB.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  * Authors: Erik Nordström, <erik.nordstrom@it.uu.se>
00020  *
00021  *****************************************************************************/
00022 #define NS_PORT
00023 #define OMNETPP
00024 
00025 #ifdef NS_PORT
00026 #ifndef OMNETPP
00027 #include "ns/aodv-uu.h"
00028 #else
00029 #include "../NA_aodv_uu_omnet.h"
00030 #include "IPv4InterfaceData.h"
00031 #endif
00032 #else
00033 #include <netdb.h>
00034 extern int h_errno;
00035 
00036 #include "NA_locality.h"
00037 #include "NA_defs_aodv.h"
00038 #include "NA_debug_aodv.h"
00039 
00040 extern int gw_prefix;
00041 #endif
00042 
00043 
00044 int NS_CLASS locality(struct in_addr dest, unsigned int ifindex)
00045 {
00046 
00047 #ifndef NS_PORT
00048     if (gw_prefix)
00049     {
00050         if ((dest.s_addr & DEV_IFINDEX(ifindex).netmask.s_addr) ==
00051                 (DEV_IFINDEX(ifindex).ipaddr.s_addr & DEV_IFINDEX(ifindex).netmask.
00052                  s_addr))
00053             return HOST_ADHOC;
00054         else
00055             return HOST_INET;
00056 
00057     }
00058     else
00059     {
00060         struct hostent *hent;
00061 
00062         hent = gethostbyaddr(&dest, sizeof(struct in_addr), AF_INET);
00063 
00064         if (!hent)
00065         {
00066             switch (h_errno)
00067             {
00068             case HOST_NOT_FOUND:
00069                 DEBUG(LOG_DEBUG, 0, "RREQ for Non-Internet dest %s",
00070                       ip_to_str(dest));
00071                 return HOST_UNKNOWN;
00072             default:
00073                 DEBUG(LOG_DEBUG, 0, "Unknown DNS error");
00074                 break;
00075 
00076             }
00077         }
00078         else
00079             return HOST_INET;
00080     }
00081 #else
00082 #ifndef OMNETPP
00083     char *dstnet = Address::getInstance().get_subnetaddr(dest.s_addr);
00084     char *subnet =
00085         Address::getInstance().get_subnetaddr(DEV_NR(NS_DEV_NR).ipaddr.s_addr);
00086     DEBUG(LOG_DEBUG, 0, "myaddr=%d, dest=%d dstnet=%s subnet=%s",
00087           DEV_NR(NS_DEV_NR).ipaddr.s_addr, dest.s_addr, dstnet, subnet);
00088     if (subnet != NULL)
00089     {
00090         if (dstnet != NULL)
00091         {
00092             if (strcmp(dstnet, subnet) != 0)
00093             {
00094                 delete[]dstnet;
00095                 return HOST_INET;
00096             }
00097             delete[]dstnet;
00098         }
00099         delete[]subnet;
00100     }
00101     assert(dstnet == NULL);
00102     return HOST_UNKNOWN;
00103 #else
00104     InterfaceEntry *   ie;
00105     ie = getInterfaceEntry (ifindex);
00106     struct in_addr dstnet;
00107     struct in_addr subnet;
00108     struct in_addr interface;
00109 
00110     int masklen = ie->ipv4Data()->getNetmask().getNetmaskLength();
00111     dstnet.s_addr = ManetAddress(dest.s_addr); dstnet.s_addr.setPrefix(masklen);   // dstnet.s_addr = dest.s_addr & mask.s_addr;
00112     interface.s_addr = ManetAddress(ie->ipv4Data()->getIPAddress());
00113     subnet.s_addr = interface.s_addr; subnet.s_addr.setPrefix(masklen);    //interface.s_addr & mask.s_addr;
00114     if (!subnet.s_addr.isUnspecified())
00115     {
00116         if (!dstnet.s_addr.isUnspecified())
00117         {
00118             if (subnet.s_addr==dstnet.s_addr)
00119                 return HOST_ADHOC;
00120             else
00121                 return HOST_INET;
00122         }
00123     }
00124     return HOST_UNKNOWN;
00125 #endif
00126 #endif
00127     return HOST_UNKNOWN;
00128 }
00129 
 All Classes Files Functions Variables Typedefs Enumerator Defines