xorp

fte.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/fea/fte.hh,v 1.26 2008/10/02 21:56:47 bms Exp $
00021 
00022 #ifndef __FEA_FTE_HH__
00023 #define __FEA_FTE_HH__
00024 
00025 #include "libxorp/ipv4.hh"
00026 #include "libxorp/ipv6.hh"
00027 #include "libxorp/ipvx.hh"
00028 #include "libxorp/ipv4net.hh"
00029 #include "libxorp/ipv6net.hh"
00030 #include "libxorp/ipvxnet.hh"
00031 
00032 
00038 template<typename A, typename N>
00039 class Fte {
00040 public:
00041     Fte() { zero(); }
00042     explicit Fte(int family) : _net(family), _nexthop(family) { zero(); }
00043     Fte(const N&    net,
00044     const A&    nexthop,
00045     const string&   ifname,
00046     const string&   vifname,
00047     uint32_t    metric,
00048     uint32_t    admin_distance,
00049     bool        xorp_route)
00050     : _net(net), _nexthop(nexthop), _ifname(ifname), _vifname(vifname),
00051       _metric(metric), _admin_distance(admin_distance),
00052       _xorp_route(xorp_route), _is_deleted(false), _is_unresolved(false),
00053       _is_connected_route(false) {}
00054     Fte(const N& net)
00055     : _net(net), _nexthop(A::ZERO(net.af())),
00056       _metric(0), _admin_distance(0),
00057       _xorp_route(false), _is_deleted(false), _is_unresolved(false),
00058       _is_connected_route(false) {}
00059 
00060     const N&    net() const     { return _net; }
00061     const A&    nexthop() const     { return _nexthop; }
00062     const string& ifname() const    { return _ifname; }
00063     const string& vifname() const   { return _vifname; }
00064     uint32_t    metric() const      { return _metric; }
00065     uint32_t    admin_distance() const  { return _admin_distance; }
00066     bool    xorp_route() const  { return _xorp_route; }
00067     bool    is_deleted() const  { return _is_deleted; }
00068     void    mark_deleted()      { _is_deleted = true; }
00069     bool    is_unresolved() const   { return _is_unresolved; }
00070     void    mark_unresolved()   { _is_unresolved = true; }
00071     bool    is_connected_route() const { return _is_connected_route; }
00072     void    mark_connected_route()  { _is_connected_route = true; }
00073 
00077     void zero() {
00078     _net = N(A::ZERO(_net.af()), 0);
00079     _nexthop = A::ZERO(_nexthop.af());
00080     _ifname.erase();
00081     _vifname.erase();
00082     _metric = 0;
00083     _admin_distance = 0;
00084     _xorp_route = false;
00085     _is_deleted = false;
00086     _is_unresolved = false;
00087     _is_connected_route = false;
00088     }
00089 
00093     bool is_host_route() const {
00094     return _net.prefix_len() == _net.masked_addr().addr_bitlen();
00095     }
00096 
00100     string str() const {
00101     return c_format("net = %s nexthop = %s ifname = %s vifname = %s "
00102             "metric = %u admin_distance = %u xorp_route = %s "
00103             "is_deleted = %s is_unresolved = %s "
00104             "is_connected_route = %s",
00105             _net.str().c_str(), _nexthop.str().c_str(),
00106             _ifname.c_str(), _vifname.c_str(),
00107             XORP_UINT_CAST(_metric),
00108             XORP_UINT_CAST(_admin_distance),
00109             bool_c_str(_xorp_route),
00110             bool_c_str(_is_deleted),
00111             bool_c_str(_is_unresolved),
00112             bool_c_str(_is_connected_route));
00113     }
00114 
00115 private:
00116     N       _net;           // Network
00117     A       _nexthop;       // Nexthop address
00118     string  _ifname;        // Interface name
00119     string  _vifname;       // Virtual interface name
00120     uint32_t    _metric;        // Route metric
00121     uint32_t    _admin_distance;    // Route admin distance
00122     bool    _xorp_route;        // This route was installed by XORP
00123     bool    _is_deleted;        // True if the entry was deleted
00124     bool    _is_unresolved;     // True if the entry is actually
00125                     // a notification of an unresolved
00126                     // route to the destination.
00127     bool    _is_connected_route;    // True if this is a route for
00128                     // directly-connected subnet.
00129 };
00130 
00131 typedef Fte<IPv4, IPv4Net> Fte4;
00132 typedef Fte<IPv6, IPv6Net> Fte6;
00133 typedef Fte<IPvX, IPvXNet> BaseFteX;
00134 
00135 class FteX : public BaseFteX {
00136 public:
00148     FteX(const IPvXNet& net,
00149      const IPvX&    nexthop,
00150      const string&  ifname,
00151      const string&  vifname,
00152      uint32_t   metric,
00153      uint32_t   admin_distance,
00154      bool       xorp_route)
00155     : BaseFteX(net,
00156            nexthop,
00157            ifname,
00158            vifname,
00159            metric,
00160            admin_distance,
00161            xorp_route) {}
00162 
00166     explicit FteX(int family) : BaseFteX(family) {}
00167 
00168 #ifdef XORP_USE_USTL
00169     FteX() : BaseFteX(AF_INET) {}
00170 #endif
00171 
00175     FteX(const Fte4& fte4)
00176     : BaseFteX(IPvXNet(fte4.net()),
00177            IPvX(fte4.nexthop()),
00178            fte4.ifname(),
00179            fte4.vifname(),
00180            fte4.metric(),
00181            fte4.admin_distance(),
00182            fte4.xorp_route()) {
00183     if (fte4.is_deleted())
00184         mark_deleted();
00185     if (fte4.is_unresolved())
00186         mark_unresolved();
00187     if (fte4.is_connected_route())
00188         mark_connected_route();
00189     }
00190 
00194     FteX(const Fte6& fte6)
00195     : BaseFteX(IPvXNet(fte6.net()),
00196            IPvX(fte6.nexthop()),
00197            fte6.ifname(),
00198            fte6.vifname(),
00199            fte6.metric(),
00200            fte6.admin_distance(),
00201            fte6.xorp_route()) {
00202     if (fte6.is_deleted())
00203         mark_deleted();
00204     if (fte6.is_unresolved())
00205         mark_unresolved();
00206     if (fte6.is_connected_route())
00207         mark_connected_route();
00208     }
00209 
00215     Fte4 get_fte4() const throw (InvalidCast) {
00216     Fte4 fte4(net().get_ipv4net(),
00217           nexthop().get_ipv4(),
00218           ifname(),
00219           vifname(),
00220           metric(),
00221           admin_distance(),
00222           xorp_route());
00223     if (is_deleted())
00224         fte4.mark_deleted();
00225     if (is_unresolved())
00226         fte4.mark_unresolved();
00227     if (is_connected_route())
00228         fte4.mark_connected_route();
00229     return fte4;
00230     }
00231 
00237     Fte6 get_fte6() const throw (InvalidCast) {
00238     Fte6 fte6(net().get_ipv6net(),
00239           nexthop().get_ipv6(),
00240           ifname(),
00241           vifname(),
00242           metric(),
00243           admin_distance(),
00244           xorp_route());
00245     if (is_deleted())
00246         fte6.mark_deleted();
00247     if (is_unresolved())
00248         fte6.mark_unresolved();
00249     if (is_connected_route())
00250         fte6.mark_connected_route();
00251     return fte6;
00252     }
00253 };
00254 
00255 #endif  // __FEA_FTE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations