diff --git a/src/routing/aodv/aodv-routing-protocol.cc b/src/routing/aodv/aodv-routing-protocol.cc new file mode 100644 index 000000000..e08ceb31d --- /dev/null +++ b/src/routing/aodv/aodv-routing-protocol.cc @@ -0,0 +1,194 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 1997, 1998 Carnegie Mellon University. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Authors: The AODV code developed by the CMU/MONARCH group was optimized and + * tuned by Samir Das and Mahesh Marina, University of Cincinnati. + * The work was partially done in Sun Microsystems. + * + * Ported to ns-3 by Elena Borovkova + */ +#include "aodv-routing-protocol.h" +#include "ns3/socket-factory.h" +#include "ns3/udp-socket-factory.h" +#include "ns3/simulator.h" +#include "ns3/log.h" +#include "ns3/random-variable.h" +#include "ns3/inet-socket-address.h" +#include "ns3/ipv4-routing-protocol.h" +#include "ns3/ipv4-route.h" +#include "ns3/boolean.h" +#include "ns3/uinteger.h" +#include "ns3/enum.h" +#include "ns3/trace-source-accessor.h" +#include "ns3/ipv4-header.h" + +/// UDP Port for AODV control traffic +#define AODV_PORT 654 + +NS_LOG_COMPONENT_DEFINE ("AodvRoutingProtocol"); + +namespace ns3 +{ +namespace aodv +{ +NS_OBJECT_ENSURE_REGISTERED (RoutingProtocol); + +RoutingProtocol::RoutingProtocol() +{ +} + +TypeId +RoutingProtocol::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::aodv::RoutingProtocol") + .SetParent () + .AddConstructor () + ; + return tid; +} + +RoutingProtocol::~RoutingProtocol() +{ +} + +void +RoutingProtocol::DoDispose () +{ + m_ipv4 = 0; + for (std::map< Ptr, Ipv4Address >::iterator iter = m_socketAddresses.begin (); + iter != m_socketAddresses.end (); iter++) + { + iter->first->Close (); + } + m_socketAddresses.clear (); + Ipv4RoutingProtocol::DoDispose (); +} + +Ptr +RoutingProtocol::RouteOutput (Ptr p, const Ipv4Header &header, uint32_t oif, Socket::SocketErrno &sockerr) +{ + Ptr rtentry; + sockerr = Socket::ERROR_NOROUTETOHOST; + // TODO resolve route + return rtentry; +} + +bool +RoutingProtocol::RouteInput (Ptr p, const Ipv4Header &header, Ptr idev, + UnicastForwardCallback ucb, MulticastForwardCallback mcb, + LocalDeliverCallback lcb, ErrorCallback ecb) +{ + // TODO + return false; +} + +void +RoutingProtocol::SetIpv4 (Ptr ipv4) +{ + NS_ASSERT (ipv4 != 0); + NS_ASSERT (m_ipv4 == 0); + /* + m_helloTimer.SetFunction (&RoutingProtocol::HelloTimerExpire, this); + */ + + m_ipv4 = ipv4; + Simulator::ScheduleNow (&RoutingProtocol::Start, this); +} + +void +RoutingProtocol::Start () +{ + // Open UDP sockets for control traffic on each IP interface + const Ipv4Address loopback ("127.0.0.1"); + for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++) + { + Ipv4Address addr = m_ipv4->GetAddress (i, 0).GetLocal (); + if (addr == loopback) + continue; + + // Create a socket to listen only on this interface + Ptr socket = Socket::CreateSocket (GetObject (), + UdpSocketFactory::GetTypeId()); + socket->SetRecvCallback (MakeCallback (&RoutingProtocol::RecvAodv, this)); + if (socket->Bind (InetSocketAddress (addr, AODV_PORT))) + { + NS_FATAL_ERROR ("Failed to bind() AODV receive socket"); + } + socket->Connect (InetSocketAddress (Ipv4Address (0xffffffff), AODV_PORT)); + m_socketAddresses.insert(std::make_pair(socket, addr)); + } +} + +void +RoutingProtocol::NotifyInterfaceUp (uint32_t interface) +{ + // TODO +} + +void +RoutingProtocol::NotifyInterfaceDown (uint32_t interface) +{ + // TODO +} + +void +RoutingProtocol::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) +{ +} + +void +RoutingProtocol::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) +{ +} + +void +RoutingProtocol::RecvAodv (Ptr socket) +{ + Ptr packet; + Address sourceAddress; + packet = socket->RecvFrom (sourceAddress); + + InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom (sourceAddress); + Ipv4Address senderIfaceAddr = inetSourceAddr.GetIpv4 (); + Ipv4Address receiverIfaceAddr = m_socketAddresses[socket]; + + NS_ASSERT (receiverIfaceAddr != Ipv4Address ()); + NS_LOG_DEBUG ("AODV node " << this << " received a AODV packet from " << senderIfaceAddr << " to " << receiverIfaceAddr); + NS_ASSERT (inetSourceAddr.GetPort () == AODV_PORT); + + // TODO check packet type and call RecvSmth +} + +void +RoutingProtocol::RecvRequest (Ptr p) +{ + // TODO +} + +void +RoutingProtocol::RecvReply (Ptr p) +{ + // TODO +} + +void +RoutingProtocol::RecvError (Ptr p) +{ + // TODO +} + +}} diff --git a/src/routing/aodv/aodv-routing-protocol.h b/src/routing/aodv/aodv-routing-protocol.h new file mode 100644 index 000000000..c80933ab3 --- /dev/null +++ b/src/routing/aodv/aodv-routing-protocol.h @@ -0,0 +1,88 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 1997, 1998 Carnegie Mellon University. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Authors: The AODV code developed by the CMU/MONARCH group was optimized and + * tuned by Samir Das and Mahesh Marina, University of Cincinnati. + * The work was partially done in Sun Microsystems. + * + * Ported to ns-3 by Elena Borovkova + */ +#ifndef AODVROUTINGPROTOCOL_H_ +#define AODVROUTINGPROTOCOL_H_ +#include "ns3/object.h" +#include "ns3/packet.h" +#include "ns3/node.h" +#include "ns3/socket.h" +#include "ns3/timer.h" +#include "ns3/ipv4.h" +#include "ns3/ipv4-routing-protocol.h" +#include + +namespace ns3 +{ +namespace aodv +{ + +/** + * \ingroup aodv + * \brief AODV routing protocol + */ +class RoutingProtocol : public Ipv4RoutingProtocol +{ +public: + static TypeId GetTypeId (void); + + RoutingProtocol(); + virtual ~RoutingProtocol(); + virtual void DoDispose (); + + ///\name From Ipv4RoutingProtocol + //\{ + Ptr RouteOutput (Ptr p, const Ipv4Header &header, uint32_t oif, Socket::SocketErrno &sockerr); + bool RouteInput (Ptr p, const Ipv4Header &header, Ptr idev, + UnicastForwardCallback ucb, MulticastForwardCallback mcb, + LocalDeliverCallback lcb, ErrorCallback ecb); + virtual void NotifyInterfaceUp (uint32_t interface); + virtual void NotifyInterfaceDown (uint32_t interface); + virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address); + virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address); + virtual void SetIpv4 (Ptr ipv4); + //\} + +private: + /// IP protocol + Ptr m_ipv4; + /// UDP socket per each IP interface, map socket -> iface + std::map< Ptr, Ipv4Address > m_socketAddresses; + +private: + /// Start protocol operation + void Start (); + + /// Receive and process control packet + void RecvAodv (Ptr s); + /// Receive RREQ + void RecvRequest (Ptr p); + /// Receive RREP + void RecvReply (Ptr p); + /// Receive RERR + void RecvError (Ptr p); +}; + +} +} +#endif /* AODVROUTINGPROTOCOL_H_ */ diff --git a/src/routing/aodv/wscript b/src/routing/aodv/wscript index ca71cb610..65d9ff118 100644 --- a/src/routing/aodv/wscript +++ b/src/routing/aodv/wscript @@ -7,13 +7,12 @@ def build(bld): 'aodv-rtable.cc', 'aodv-rqueue.cc', 'aodv-packet.cc', + 'aodv-routing-protocol.cc', ] headers = bld.new_task_gen('ns3header') headers.module = 'aodv' headers.source = [ - 'aodv-rtable.h', - 'aodv-rqueue.h', - 'aodv-packet.h', + 'aodv-routing-protocol.h', ]