# HG changeset patch # User Tom Henderson # Date 1279919709 25200 # Node ID 7e2827d11afaab7cb3162921ceb10c983e38a2e7 # Parent dde6fcc000955c4cd95b4cd33a22bf84f72d2d30 Allow AODV to use /32 interface addresses diff -r dde6fcc00095 -r 7e2827d11afa src/routing/aodv/aodv-routing-protocol.cc --- a/src/routing/aodv/aodv-routing-protocol.cc Fri Jul 23 13:31:02 2010 -0700 +++ b/src/routing/aodv/aodv-routing-protocol.cc Fri Jul 23 14:15:09 2010 -0700 @@ -269,7 +269,7 @@ RoutingProtocol::RouteOutput (Ptr p, const Ipv4Header &header, Ptr oif, Socket::SocketErrno &sockerr) { - NS_LOG_FUNCTION (this << header.GetDestination ()); + NS_LOG_FUNCTION (this << header << (oif? oif->GetIfIndex () : 0)); if (! p) { return LoopbackRoute (header); // later @@ -370,12 +370,8 @@ { Ipv4InterfaceAddress iface = j->second; if (m_ipv4->GetInterfaceForAddress (iface.GetLocal ()) == iif) - if (dst == iface.GetBroadcast ()) + if (dst == iface.GetBroadcast () || dst.IsBroadcast ()) { - if (!EnableBroadcast) - { - return true; - } if (m_dpd.IsDuplicate (p, header)) { NS_LOG_DEBUG ("Duplicated packet " << p->GetUid () << " from " << origin << ". Drop."); @@ -385,6 +381,10 @@ NS_LOG_LOGIC ("Broadcast local delivery to " << iface.GetLocal ()); Ptr packet = p->Copy (); lcb (p, header, iif); + if (!EnableBroadcast) + { + return true; + } if (header.GetTtl () > 1) { NS_LOG_LOGIC ("Forward broadcast. TTL " << (uint16_t) header.GetTtl ()); @@ -528,8 +528,9 @@ UdpSocketFactory::GetTypeId ()); NS_ASSERT (socket != 0); socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvAodv, this)); - socket->Bind (InetSocketAddress (iface.GetLocal (), AODV_PORT)); - socket->Connect (InetSocketAddress (iface.GetBroadcast (), AODV_PORT)); + socket->BindToNetDevice (l3->GetNetDevice (i)); + socket->Bind (InetSocketAddress (Ipv4Address::GetAny (), AODV_PORT)); + socket->SetAllowBroadcast (true); socket->SetAttribute ("IpTtl", UintegerValue (1)); m_socketAddresses.insert (std::make_pair (socket, iface)); @@ -607,8 +608,10 @@ UdpSocketFactory::GetTypeId ()); NS_ASSERT (socket != 0); socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvAodv,this)); - socket->Bind (InetSocketAddress (iface.GetLocal (), AODV_PORT)); - socket->Connect (InetSocketAddress (iface.GetBroadcast (), AODV_PORT)); + socket->BindToNetDevice (l3->GetNetDevice (i)); + // Bind to any IP address so that broadcasts can be received + socket->Bind (InetSocketAddress (Ipv4Address::GetAny(), AODV_PORT)); + socket->SetAllowBroadcast (true); m_socketAddresses.insert (std::make_pair (socket, iface)); // Add local broadcast record to the routing table @@ -644,8 +647,9 @@ UdpSocketFactory::GetTypeId ()); NS_ASSERT (socket != 0); socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvAodv, this)); - socket->Bind (InetSocketAddress (iface.GetLocal (), AODV_PORT)); - socket->Connect (InetSocketAddress (iface.GetBroadcast (), AODV_PORT)); + // Bind to any IP address so that broadcasts can be received + socket->Bind (InetSocketAddress (Ipv4Address::GetAny(), AODV_PORT)); + socket->SetAllowBroadcast (true); m_socketAddresses.insert (std::make_pair (socket, iface)); // Add local broadcast record to the routing table @@ -692,7 +696,7 @@ NS_ASSERT (m_lo != 0); Ptr rt = Create (); rt->SetDestination (hdr.GetDestination ()); - rt->SetSource (hdr.GetSource ()); + rt->SetSource (Ipv4Address ("127.0.0.1")); rt->SetGateway (Ipv4Address ("127.0.0.1")); rt->SetOutputDevice (m_lo); return rt; @@ -762,7 +766,17 @@ packet->AddHeader (rreqHeader); TypeHeader tHeader (AODVTYPE_RREQ); packet->AddHeader (tHeader); - socket->Send (packet); + // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise + Ipv4Address destination; + if (iface.GetMask () == Ipv4Mask::GetOnes ()) + { + destination = Ipv4Address ("255.255.255.255"); + } + else + { + destination = iface.GetBroadcast (); + } + socket->SendTo (packet, 0, InetSocketAddress (destination, AODV_PORT)); } ScheduleRreqRetry (dst); if (EnableHello) @@ -989,7 +1003,17 @@ packet->AddHeader (rreqHeader); TypeHeader tHeader (AODVTYPE_RREQ); packet->AddHeader (tHeader); - socket->Send (packet); + // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise + Ipv4Address destination; + if (iface.GetMask () == Ipv4Mask::GetOnes ()) + { + destination = Ipv4Address ("255.255.255.255"); + } + else + { + destination = iface.GetBroadcast (); + } + socket->SendTo (packet, 0, InetSocketAddress (destination, AODV_PORT)); } if (EnableHello) @@ -1401,7 +1425,17 @@ packet->AddHeader (helloHeader); TypeHeader tHeader (AODVTYPE_RREP); packet->AddHeader (tHeader); - socket->Send (packet); + // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise + Ipv4Address destination; + if (iface.GetMask () == Ipv4Mask::GetOnes ()) + { + destination = Ipv4Address ("255.255.255.255"); + } + else + { + destination = iface.GetBroadcast (); + } + socket->SendTo (packet, 0, InetSocketAddress (destination, AODV_PORT)); } } @@ -1504,7 +1538,17 @@ Ipv4InterfaceAddress iface = i->second; NS_ASSERT (socket); NS_LOG_LOGIC ("Broadcast RERR message from interface " << iface.GetLocal()); - socket->Send (packet); + // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise + Ipv4Address destination; + if (iface.GetMask () == Ipv4Mask::GetOnes ()) + { + destination = Ipv4Address ("255.255.255.255"); + } + else + { + destination = iface.GetBroadcast (); + } + socket->SendTo (packet, 0, InetSocketAddress (destination, AODV_PORT)); } } } @@ -1550,7 +1594,17 @@ Ptr socket = FindSocketWithInterfaceAddress (*i); NS_ASSERT (socket); NS_LOG_LOGIC ("Broadcast RERR message from interface " << i->GetLocal()); - socket->Send (packet); + // Send to all-hosts broadcast if on /32 addr, subnet-directed otherwise + Ipv4Address destination; + if (i->GetMask () == Ipv4Mask::GetOnes ()) + { + destination = Ipv4Address ("255.255.255.255"); + } + else + { + destination = i->GetBroadcast (); + } + socket->SendTo (packet, 0, InetSocketAddress (destination, AODV_PORT)); } }