src/routing/olsr/olsr-agent-impl.h
changeset 4364 579bbfe8bb65
parent 4363 55ccad74d7cc
child 4365 f71fd5bfc4ad
equal deleted inserted replaced
4363:55ccad74d7cc 4364:579bbfe8bb65
     1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2004 Francisco J. Ros 
       
     4  * Copyright (c) 2007 INESC Porto
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Authors: Francisco J. Ros  <fjrm@dif.um.es>
       
    20  *          Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
       
    21  */
       
    22 
       
    23 
       
    24 #ifndef __OLSR_AGENT_IMPL_H__
       
    25 #define __OLSR_AGENT_IMPL_H__
       
    26 
       
    27 #include "olsr-header.h"
       
    28 #include "olsr-state.h"
       
    29 #include "olsr-repositories.h"
       
    30 
       
    31 #include "ns3/object.h"
       
    32 #include "ns3/packet.h"
       
    33 #include "ns3/node.h"
       
    34 #include "ns3/socket.h"
       
    35 #include "ns3/event-garbage-collector.h"
       
    36 #include "ns3/timer.h"
       
    37 #include "ns3/traced-callback.h"
       
    38 #include "ns3/ipv4.h"
       
    39 
       
    40 #include <vector>
       
    41 #include <map>
       
    42 
       
    43 
       
    44 namespace ns3 {
       
    45 namespace olsr {
       
    46 
       
    47 
       
    48 /// An %OLSR's routing table entry.
       
    49 struct RoutingTableEntry
       
    50 {
       
    51   Ipv4Address destAddr;	///< Address of the destination node.
       
    52   Ipv4Address nextAddr;	///< Address of the next hop.
       
    53   uint32_t interface; ///< Interface index
       
    54   uint32_t distance; ///< Distance in hops to the destination.
       
    55 
       
    56   RoutingTableEntry () : // default values
       
    57     destAddr (), nextAddr (),
       
    58     interface (0), distance (0) {};
       
    59 };
       
    60 
       
    61 
       
    62 class AgentImpl : public Ipv4RoutingProtocol
       
    63 {
       
    64 public:
       
    65   static TypeId GetTypeId (void);
       
    66 
       
    67   AgentImpl ();
       
    68   virtual ~AgentImpl ();
       
    69 
       
    70   void SetNode (Ptr<Node> node);
       
    71 
       
    72   void Start ();
       
    73   void SetMainInterface (uint32_t interface);
       
    74 
       
    75 private:
       
    76   std::map<Ipv4Address, RoutingTableEntry> m_table; ///< Data structure for the routing table.
       
    77 
       
    78   EventGarbageCollector m_events;
       
    79 
       
    80   /// Address of the routing agent.
       
    81   Ipv4Address m_routingAgentAddr;
       
    82 	
       
    83   /// Packets sequence number counter.
       
    84   uint16_t m_packetSequenceNumber;
       
    85   /// Messages sequence number counter.
       
    86   uint16_t m_messageSequenceNumber;
       
    87   /// Advertised Neighbor Set sequence number.
       
    88   uint16_t m_ansn;
       
    89   
       
    90   /// HELLO messages' emission interval.
       
    91   Time m_helloInterval;
       
    92   /// TC messages' emission interval.
       
    93   Time m_tcInterval;
       
    94   /// MID messages' emission interval.
       
    95   Time m_midInterval;
       
    96   /// Willingness for forwarding packets on behalf of other nodes.
       
    97   uint8_t m_willingness;
       
    98 	
       
    99   /// Internal state with all needed data structs.
       
   100   OlsrState m_state;
       
   101 
       
   102   Ptr<Ipv4> m_ipv4;
       
   103 	
       
   104 private:
       
   105 
       
   106   void Clear ();
       
   107   uint32_t GetSize () const { return m_table.size (); }
       
   108   std::vector<RoutingTableEntry> GetEntries () const;
       
   109   void RemoveEntry (const Ipv4Address &dest);
       
   110   void AddEntry (const Ipv4Address &dest,
       
   111                  const Ipv4Address &next,
       
   112                  uint32_t interface,
       
   113                  uint32_t distance);
       
   114   void AddEntry (const Ipv4Address &dest,
       
   115                  const Ipv4Address &next,
       
   116                  const Ipv4Address &interfaceAddress,
       
   117                  uint32_t distance);
       
   118   bool Lookup (const Ipv4Address &dest,
       
   119                RoutingTableEntry &outEntry) const;
       
   120   bool FindSendEntry (const RoutingTableEntry &entry,
       
   121                       RoutingTableEntry &outEntry) const;
       
   122 
       
   123   // From Ipv4RoutingProtocol
       
   124   virtual bool RequestRoute (uint32_t ifIndex,
       
   125                              const Ipv4Header &ipHeader,
       
   126                              Ptr<Packet> packet,
       
   127                              RouteReplyCallback routeReply);
       
   128   virtual bool RequestIfIndex (Ipv4Address destination, 
       
   129                                uint32_t& ifIndex);
       
   130 
       
   131 
       
   132   void DoDispose ();
       
   133 
       
   134   void SendPacket (Ptr<Packet> packet, const MessageList &containedMessages);
       
   135 	
       
   136   /// Increments packet sequence number and returns the new value.
       
   137   inline uint16_t GetPacketSequenceNumber ();
       
   138   /// Increments message sequence number and returns the new value.
       
   139   inline uint16_t GetMessageSequenceNumber ();
       
   140 	
       
   141   void RecvOlsr (Ptr<Socket> socket);
       
   142 
       
   143   void MprComputation ();
       
   144   void RoutingTableComputation ();
       
   145   Ipv4Address GetMainAddress (Ipv4Address iface_addr) const;
       
   146 
       
   147   // Timer handlers
       
   148   Timer m_helloTimer;
       
   149   void HelloTimerExpire ();
       
   150   
       
   151   Timer m_tcTimer;
       
   152   void TcTimerExpire ();
       
   153 
       
   154   Timer m_midTimer;
       
   155   void MidTimerExpire ();
       
   156 
       
   157   void DupTupleTimerExpire (Ipv4Address address, uint16_t sequenceNumber);
       
   158   bool m_linkTupleTimerFirstTime;
       
   159   void LinkTupleTimerExpire (Ipv4Address neighborIfaceAddr);
       
   160   void Nb2hopTupleTimerExpire (Ipv4Address neighborMainAddr, Ipv4Address twoHopNeighborAddr);
       
   161   void MprSelTupleTimerExpire (Ipv4Address mainAddr);
       
   162   void TopologyTupleTimerExpire (Ipv4Address destAddr, Ipv4Address lastAddr);
       
   163   void IfaceAssocTupleTimerExpire (Ipv4Address ifaceAddr);
       
   164 
       
   165   void IncrementAnsn ();
       
   166 
       
   167   /// A list of pending messages which are buffered awaiting for being sent.
       
   168   olsr::MessageList m_queuedMessages;
       
   169   Timer m_queuedMessagesTimer; // timer for throttling outgoing messages
       
   170 
       
   171   void ForwardDefault (olsr::MessageHeader olsrMessage,
       
   172                        DuplicateTuple *duplicated,
       
   173                        const Ipv4Address &localIface,
       
   174                        const Ipv4Address &senderAddress);
       
   175   void QueueMessage (const olsr::MessageHeader &message, Time delay);
       
   176   void SendQueuedMessages ();
       
   177   void SendHello ();
       
   178   void SendTc ();
       
   179   void SendMid ();
       
   180 
       
   181   void NeighborLoss (const LinkTuple &tuple);
       
   182   void AddDuplicateTuple (const DuplicateTuple &tuple);
       
   183   void RemoveDuplicateTuple (const DuplicateTuple &tuple);
       
   184   void LinkTupleAdded (const LinkTuple &tuple, uint8_t willingness);
       
   185   void RemoveLinkTuple (const LinkTuple &tuple);
       
   186   void LinkTupleUpdated (const LinkTuple &tuple, uint8_t willingness);
       
   187   void AddNeighborTuple (const NeighborTuple &tuple);
       
   188   void RemoveNeighborTuple (const NeighborTuple &tuple);
       
   189   void AddTwoHopNeighborTuple (const TwoHopNeighborTuple &tuple);
       
   190   void RemoveTwoHopNeighborTuple (const TwoHopNeighborTuple &tuple);
       
   191   void AddMprSelectorTuple (const MprSelectorTuple  &tuple);
       
   192   void RemoveMprSelectorTuple (const MprSelectorTuple &tuple);
       
   193   void AddTopologyTuple (const TopologyTuple &tuple);
       
   194   void RemoveTopologyTuple (const TopologyTuple &tuple);
       
   195   void AddIfaceAssocTuple (const IfaceAssocTuple &tuple);
       
   196   void RemoveIfaceAssocTuple (const IfaceAssocTuple &tuple);
       
   197 
       
   198   void ProcessHello (const olsr::MessageHeader &msg,
       
   199                      const Ipv4Address &receiverIface,
       
   200                      const Ipv4Address &senderIface);
       
   201   void ProcessTc (const olsr::MessageHeader &msg,
       
   202                   const Ipv4Address &senderIface);
       
   203   void ProcessMid (const olsr::MessageHeader &msg,
       
   204                    const Ipv4Address &senderIface);
       
   205 
       
   206   void LinkSensing (const olsr::MessageHeader &msg,
       
   207                     const olsr::MessageHeader::Hello &hello,
       
   208                     const Ipv4Address &receiverIface,
       
   209                     const Ipv4Address &sender_iface);
       
   210   void PopulateNeighborSet (const olsr::MessageHeader &msg,
       
   211                             const olsr::MessageHeader::Hello &hello);
       
   212   void PopulateTwoHopNeighborSet (const olsr::MessageHeader &msg,
       
   213                                   const olsr::MessageHeader::Hello &hello);
       
   214   void PopulateMprSelectorSet (const olsr::MessageHeader &msg,
       
   215                                const olsr::MessageHeader::Hello &hello);
       
   216 
       
   217   int Degree (NeighborTuple const &tuple);
       
   218 
       
   219   Ipv4Address m_mainAddress;
       
   220 
       
   221   // One socket per interface, each bound to that interface's address
       
   222   // (reason: for OLSR Link Sensing we need to know on which interface
       
   223   // HELLO messages arrive)
       
   224   std::map< Ptr<Socket>, Ipv4Address > m_socketAddresses;
       
   225 
       
   226   TracedCallback <const PacketHeader &,
       
   227                   const MessageList &> m_rxPacketTrace;
       
   228   TracedCallback <const PacketHeader &,
       
   229                   const MessageList &> m_txPacketTrace;
       
   230   TracedCallback <uint32_t> m_routingTableChanged;
       
   231 
       
   232 };
       
   233 
       
   234 }} // namespace ns3
       
   235 
       
   236 #endif