xorp

vertex.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
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/ospf/vertex.hh,v 1.18 2008/10/02 21:57:50 bms Exp $
00022 
00023 #ifndef __OSPF_VERTEX_HH__
00024 #define __OSPF_VERTEX_HH__
00025 
00030 class Vertex {
00031  public:
00032     Vertex() : _origin(false), _nexthop_id(OspfTypes::UNUSED_INTERFACE_ID)
00033     {}
00034 
00035     bool operator<(const Vertex& other) const {
00036     XLOG_ASSERT(get_version() == other.get_version());
00037     switch(_version) {
00038     case OspfTypes::V2:
00039         if (_nodeid == other.get_nodeid())
00040         return _t < other.get_type();
00041         break;
00042     case OspfTypes::V3:
00043         if (_nodeid == other.get_nodeid() && _t != other.get_type())
00044         return _t < other.get_type();
00045         switch(_t) {
00046         case OspfTypes::Router:
00047         break;
00048         case OspfTypes::Network:
00049         if (_nodeid == other.get_nodeid())
00050             return _interface_id < other.get_interface_id();
00051         break;
00052         }
00053         break;
00054     }
00055     return _nodeid < other.get_nodeid();
00056     }
00057 
00058     bool operator==(const Vertex& other) const {
00059     XLOG_ASSERT(get_version() == other.get_version());
00060     return _nodeid == other.get_nodeid() && _t ==  other.get_type();
00061     }
00062 
00063     void set_version(OspfTypes::Version v) {
00064     _version = v;
00065     }
00066 
00067     OspfTypes::Version get_version() const {
00068     return _version;
00069     }
00070 
00071     void set_type(OspfTypes::VertexType t) {
00072     _t = t;
00073     }
00074 
00075     OspfTypes::VertexType get_type() const {
00076     return _t;
00077     }
00078 
00079     void set_nodeid(uint32_t nodeid) {
00080     _nodeid = nodeid;
00081     }
00082 
00083     uint32_t get_nodeid() const {
00084     return _nodeid;
00085     }
00086 
00091     void set_lsa(Lsa::LsaRef lsar) {
00092     XLOG_ASSERT(OspfTypes::V2 == get_version());
00093     XLOG_ASSERT(0 == _lsars.size());
00094     _lsars.push_back(lsar);
00095     }
00096 
00101     Lsa::LsaRef get_lsa() const {
00102     XLOG_ASSERT(OspfTypes::V2 == get_version());
00103     XLOG_ASSERT(1 == _lsars.size());
00104     return *(_lsars.begin());
00105     }
00106 
00111     list<Lsa::LsaRef>& get_lsas() {
00112     XLOG_ASSERT(OspfTypes::V3 == get_version());
00113     return _lsars;
00114     }
00115 
00116     void set_interface_id(uint32_t interface_id) {
00117     XLOG_ASSERT(OspfTypes::V3 == get_version());
00118     _interface_id = interface_id;
00119     }
00120 
00121     uint32_t get_interface_id() const {
00122     XLOG_ASSERT(OspfTypes::V3 == get_version());
00123     return _interface_id;
00124     }
00125 
00126     void set_origin(bool origin) {
00127     _origin = origin;
00128     }
00129 
00130     bool get_origin() const {
00131     return _origin;
00132     }
00133 
00134     void set_address(IPv4 address) {
00135     XLOG_ASSERT(OspfTypes::V2 == get_version());
00136     _address_ipv4 = address;
00137     }
00138 
00139     IPv4 get_address_ipv4() const {
00140     XLOG_ASSERT(OspfTypes::V2 == get_version());
00141     return _address_ipv4;
00142     }
00143 
00144     void set_address(IPv6 address) {
00145     XLOG_ASSERT(OspfTypes::V3 == get_version());
00146     _address_ipv6 = address;
00147     }
00148 
00149     IPv6 get_address_ipv6() const {
00150     XLOG_ASSERT(OspfTypes::V3 == get_version());
00151     return _address_ipv6;
00152     }
00153 
00154     void set_nexthop_id(uint32_t nexthop_id) {
00155     XLOG_ASSERT(OspfTypes::V3 == get_version());
00156     _nexthop_id = nexthop_id;
00157     }
00158 
00159     uint32_t get_nexthop_id() const {
00160     XLOG_ASSERT(OspfTypes::V3 == get_version());
00161     return _nexthop_id;
00162     }
00163 
00164     string str() const {
00165     string output;
00166     switch(_version) {
00167     case OspfTypes::V2:
00168         output = "OSPFv2";
00169         if (_origin)
00170         output += "(Origin)";
00171         switch(_t) {
00172         case OspfTypes::Router:
00173         output += " Router";
00174         break;
00175         case OspfTypes::Network:
00176         output += " Network";
00177         break;
00178         }
00179         output += c_format(" %s(%#x) %s(%#x)", pr_id(_nodeid).c_str(), 
00180                    _nodeid, cstring(_address_ipv4),
00181                    _address_ipv4.addr());
00182         break;
00183     case OspfTypes::V3:
00184         output = "OSPFv3";
00185         if (_origin)
00186         output += "(Origin)";
00187         switch(_t) {
00188         case OspfTypes::Router:
00189         output += c_format(" Router %s(%#x)", pr_id(_nodeid).c_str(),
00190                    _nodeid);
00191         break;
00192         case OspfTypes::Network:
00193         output += c_format(" Transit %s(%#x) %u", 
00194                    pr_id(_nodeid).c_str(), _nodeid,
00195                    _interface_id);
00196         break;
00197         }
00198         output += c_format(" %s", cstring(_address_ipv6));
00199         break;
00200     }
00201     return output;
00202     }
00203 
00204  private:
00205     /*const */OspfTypes::Version _version;
00206     
00207     OspfTypes::VertexType _t;   // Router or Network (Transit in OSPFv3) 
00208     uint32_t _nodeid;
00209     uint32_t _interface_id; // OSPFv3 Only
00210 
00211     bool _origin;       // Is this the vertex of the router.
00212 
00213     // The address of the Vertex that should be used as the nexthop by
00214     // the origin.
00215     IPv4 _address_ipv4;     
00216     IPv6 _address_ipv6;
00217 
00218     // If this vertex is directly connected to the origin this is the
00219     // interface ID that should be used with the nexthop.
00220     uint32_t _nexthop_id;
00221 
00222     list<Lsa::LsaRef> _lsars;
00223 
00224     // RFC 2328 Section 16.1.  Calculating the shortest-path tree for an area:
00225     // Vertex (node) ID
00226     //     A 32-bit number which together with the vertex type (router
00227     //     or network) uniquely identifies the vertex.  For router
00228     //     vertices the Vertex ID is the router's OSPF Router ID.  For
00229     //     network vertices, it is the IP address of the network's
00230     //     Designated Router.
00231 
00232     // RFC 2470 Section 3.8.1.Calculating the shortest path tree for an area:
00233     // o  The Vertex ID for a router is the OSPF Router ID. The Vertex ID
00234     //    for a transit network is a combination of the Interface ID and
00235     //    OSPF Router ID of the network's Designated Router.
00236 };
00237 
00238 #endif // __OSPF_VERTEX_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations