xorp

topology.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8 sw=4:
00003 
00004 // Copyright (c) 2001-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 // 
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/contrib/olsr/topology.hh,v 1.3 2008/10/02 21:56:36 bms Exp $
00022 
00023 #ifndef __OLSR_TOPOLOGY_HH__
00024 #define __OLSR_TOPOLOGY_HH__
00025 
00026 class TopologyManager;
00027 
00034 class MidEntry {
00035 public:
00036     MidEntry(EventLoop& ev, TopologyManager* parent,
00037          const OlsrTypes::MidEntryID id, const IPv4& iface_addr,
00038          const IPv4& main_addr, const uint16_t distance,
00039          const TimeVal& vtime)
00040      : _ev(ev), _parent(parent), _id(id),
00041        _iface_addr(iface_addr), _main_addr(main_addr),
00042        _distance(distance)
00043     {
00044     update_timer(vtime);
00045     }
00046 
00047     inline OlsrTypes::MidEntryID id() const { return _id; }
00048     inline IPv4 iface_addr() const { return _iface_addr; }
00049     inline IPv4 main_addr() const { return _main_addr; }
00050     inline uint16_t distance() const { return _distance; }
00051 
00052     inline void set_distance(const uint16_t distance) {
00053     _distance = distance;
00054     }
00055 
00060     inline TimeVal time_remaining() const {
00061     TimeVal tv;
00062     _expiry_timer.time_remaining(tv);
00063     return tv;
00064     }
00065 
00071     void update_timer(const TimeVal& vtime);
00072 
00076     void event_dead();
00077 
00078 private:
00079     EventLoop&      _ev;
00080     TopologyManager*    _parent;
00081     OlsrTypes::MidEntryID       _id;
00082     IPv4        _iface_addr;
00083     IPv4        _main_addr;
00084     uint16_t        _distance;
00085     XorpTimer       _expiry_timer;
00086 };
00087 
00091 class TopologyEntry {
00092 public:
00093     TopologyEntry(EventLoop& ev, TopologyManager* parent,
00094           OlsrTypes::TopologyID id,
00095           const IPv4& dest, const IPv4& lasthop,
00096           const uint16_t distance,
00097           const uint16_t seqno,
00098           const TimeVal& vtime)
00099      : _ev(ev), _parent(parent), _id(id), _destination(dest),
00100        _lasthop(lasthop), _distance(distance), _seqno(seqno)
00101     {
00102     update_timer(vtime);
00103     }
00104 
00105     inline OlsrTypes::TopologyID id() const { return _id; }
00106     inline IPv4 destination() const { return _destination; }
00107     inline IPv4 lasthop() const { return _lasthop; }
00108     inline uint16_t distance() const { return _distance; }
00109     inline uint16_t seqno() const { return _seqno; }
00110 
00111     inline void set_distance(const uint16_t d) { _distance = d; }
00112 
00117     inline TimeVal time_remaining() const {
00118     TimeVal tv;
00119     _expiry_timer.time_remaining(tv);
00120     return tv;
00121     }
00122 
00128     void update_timer(const TimeVal& vtime);
00129 
00133     void event_dead();
00134 
00135 private:
00136     EventLoop&          _ev;
00137     TopologyManager*        _parent;
00138 
00142     OlsrTypes::TopologyID   _id;
00143 
00147     IPv4    _destination;   // T_dest_addr
00148 
00152     IPv4    _lasthop;   // T_last_addr
00153 
00158     uint16_t    _distance;  // Not named -- Section 10, 3.1
00159 
00163     uint16_t    _seqno;     // T_seq
00164 
00168     XorpTimer   _expiry_timer;  // T_time
00169 };
00170 
00175 class TopologyManager {
00176   public:
00177     TopologyManager(Olsr& olsr, EventLoop& eventloop,
00178             FaceManager& fm, Neighborhood& nh);
00179     ~TopologyManager();
00180 
00181     inline RouteManager* route_manager() { return _rm; }
00182 
00183     void set_route_manager(RouteManager* rm) { _rm = rm; }
00184 
00185     //
00186     // TC entries.
00187     //
00188 
00203     void update_tc_entry(const IPv4& dest_addr,
00204              const IPv4& origin_addr,
00205              const uint16_t distance,
00206              const uint16_t ansn,
00207              const TimeVal& vtime,
00208              bool& is_created)
00209     throw(BadTopologyEntry);
00210 
00222     OlsrTypes::TopologyID add_tc_entry(const IPv4& dest_addr,
00223                        const IPv4& origin_addr,
00224                        const uint16_t distance,
00225                        const uint16_t ansn,
00226                        const TimeVal& expiry_time)
00227     throw(BadTopologyEntry);
00228 
00237     bool delete_tc_entry(const OlsrTypes::TopologyID tcid);
00238 
00242     void clear_tc_entries();
00243 
00255     bool apply_tc_ansn(const uint16_t ansn, const IPv4& origin_addr);
00256 
00268     OlsrTypes::TopologyID get_topologyid(const IPv4& dest_addr,
00269                      const IPv4& lasthop_addr)
00270     throw(BadTopologyEntry);
00271 
00281     const TopologyEntry* get_topology_entry_by_id(
00282     const OlsrTypes::TopologyID tcid) const
00283     throw(BadTopologyEntry);
00284 
00292     void get_topology_list(list<OlsrTypes::TopologyID>& tclist) const;
00293 
00308     vector<IPv4> get_tc_neighbor_set(const IPv4& origin_addr, uint16_t& ansn)
00309     throw(BadTopologyEntry);
00310 
00311     /*
00312      * Look up the distance of a TC entry.
00313      * Used by protocol simulator.
00314      *
00315      * @param origin_addr the address of the node originating this TC entry.
00316      * @param neighbor_addr the address of the destination node.
00317      * @return the number of hops to reach neighbor_addr via origin_addr.
00318      * @throw BadTopologyEntry if the entry does not exist.
00319      */
00320     uint16_t get_tc_distance(const IPv4& origin_addr, const IPv4& dest_addr)
00321     throw(BadTopologyEntry);
00322 
00330     size_t get_tc_lasthop_count_by_dest(const IPv4& dest_addr);
00331 
00338     size_t tc_node_count() const;
00339 
00340     //
00341     // MID entries.
00342     //
00343 
00358     void update_mid_entry(const IPv4& main_addr, const IPv4& iface_addr,
00359                   const uint16_t distance, const TimeVal& vtime,
00360               bool& is_mid_created)
00361     throw(BadMidEntry);
00362 
00377     void add_mid_entry(const IPv4& main_addr, const IPv4& iface_addr,
00378                const uint16_t distance, const TimeVal& vtime)
00379     throw(BadMidEntry);
00380 
00387     bool delete_mid_entry(const OlsrTypes::MidEntryID mid_id);
00388 
00392     void clear_mid_entries();
00393 
00402     vector<IPv4> get_mid_addresses(const IPv4& main_addr);
00403 
00414     uint16_t get_mid_address_distance(const IPv4& main_addr,
00415                       const IPv4& iface_addr)
00416     throw(BadMidEntry);
00417 
00429     IPv4 get_main_addr_of_mid(const IPv4& mid_addr)
00430     throw(BadMidEntry);
00431 
00438     size_t mid_node_count() const;
00439 
00449     const MidEntry* get_mid_entry_by_id(
00450     const OlsrTypes::MidEntryID midid) const
00451     throw(BadTopologyEntry);
00452 
00460     void get_mid_list(list<OlsrTypes::MidEntryID>& midlist) const;
00461 
00462 
00463     //
00464     // RouteManager interaction.
00465     //
00466 
00480     void push_topology();
00481 
00482     //
00483     // Event handlers.
00484     //
00485 
00496     bool event_receive_tc(Message* msg,
00497     const IPv4& remote_addr, const IPv4& local_addr);
00498 
00504     void event_tc_dead(OlsrTypes::TopologyID tcid);
00505 
00516     bool event_receive_mid(Message* msg,
00517     const IPv4& remote_addr, const IPv4& local_addr);
00518 
00522     void event_mid_dead(const OlsrTypes::MidEntryID mid_id);
00523 
00524 protected:
00534     void update_tc_distance(TopologyEntry* tc, uint16_t distance);
00535 
00546     void assert_tc_distance_is_unique(const OlsrTypes::TopologyID tcid)
00547     throw(BadTopologyEntry);
00548 
00563     void assert_tc_ansn_is_identical(const IPv4& origin_addr)
00564     throw(BadTopologyEntry);
00565 
00566 private:
00567     Olsr&       _olsr;
00568     EventLoop&      _eventloop;
00569     FaceManager&    _fm;
00570     Neighborhood&   _nh;
00571     RouteManager*   _rm;
00572 
00573     OlsrTypes::MidEntryID   _next_mid_id;
00574     OlsrTypes::TopologyID   _next_tcid;
00575 
00579     typedef map<OlsrTypes::MidEntryID, MidEntry*> MidIdMap;
00580     MidIdMap _mids;
00581 
00586     typedef multimap<IPv4, OlsrTypes::MidEntryID> MidAddrMap;
00587     MidAddrMap _mid_addr;
00588 
00592     typedef map<OlsrTypes::TopologyID, TopologyEntry*> TcIdMap;
00593     TcIdMap _topology;
00594 
00600     typedef multimap<uint16_t, OlsrTypes::TopologyID> TcDistanceMap;
00601     TcDistanceMap _tc_distances;
00602 
00607     typedef multimap<IPv4, OlsrTypes::TopologyID> TcDestMap;
00608     TcDestMap _tc_destinations;
00609 
00614     typedef multimap<IPv4, OlsrTypes::TopologyID> TcLasthopMap;
00615     TcLasthopMap _tc_lasthops;
00616 
00622     typedef map<IPv4, uint16_t>     TcFinalSeqMap;
00623     TcFinalSeqMap _tc_final_seqnos;
00624 };
00625 
00626 #endif // __OLSR_TOPOLOGY_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations