xorp

rt_tab_register.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/rib/rt_tab_register.hh,v 1.21 2008/10/02 21:58:13 bms Exp $
00021 
00022 #ifndef __RIB_RT_TAB_REGISTER_HH__
00023 #define __RIB_RT_TAB_REGISTER_HH__
00024 
00025 
00026 
00027 
00028 #include "libxorp/debug.h"
00029 
00030 #include "rt_tab_base.hh"
00031 
00032 
00033 class RegisterServer;
00034 
00039 class ModuleData {
00040 public:
00047     ModuleData(const string& modulename)
00048     : _modulename(modulename), _is_set(false)   { }
00049 
00050     ModuleData() { }
00051 
00055     const string& name() const          { return _modulename;   }
00056 
00060     bool is_set() const             { return _is_set;   }
00061 
00066     void set() const                { _is_set = true;   }
00067 
00072     void clear() const              { _is_set = false;  }
00073 
00077     string str() const              {
00078         string s;
00079         s = _modulename + (_is_set ? " (SET)" : " (cleared)");
00080         return s;
00081     }
00082 
00094     bool operator==(const ModuleData& other) const {
00095     return name() == other.name();
00096     }
00097 
00108     bool operator<(const ModuleData& other) const {
00109     return name() < other.name();
00110     }
00111 
00112 private:
00113     string      _modulename;
00114     mutable bool    _is_set;
00115 };
00116 
00138 template <class A>
00139 class RouteRegister {
00140 public:
00150     RouteRegister(const IPNet<A>& valid_subnet, 
00151           const IPRouteEntry<A>* route,
00152           const ModuleData& module)
00153     : _valid_subnet(valid_subnet), _route(route) {
00154     _modules[module.name()] = module;
00155     }
00156 
00160     ~RouteRegister() {
00161     _route = reinterpret_cast<const IPRouteEntry<A>* >(0xbad);
00162     }
00163 
00173     int add_registrant(const ModuleData& module) {
00174     debug_msg("add_registrant: Module: %s\n", module.str().c_str());
00175     if (_modules.find(module.name()) != _modules.end())
00176         return XORP_ERROR;
00177     _modules[module.name()] = module;
00178     return XORP_OK;
00179     }
00180 
00190     int delete_registrant(const ModuleData& module);
00191 
00197     void mark_modules() const {
00198     map<string, ModuleData>::const_iterator i;
00199     for (i = _modules.begin(); i != _modules.end(); ++i)
00200         i->second.set();
00201     }
00202 
00206     int size() const            { return _modules.size(); }
00207 
00212     const IPNet<A>& valid_subnet() const { return _valid_subnet; }
00213 
00217     const IPRouteEntry<A>* route() const { return _route; }
00218 
00223     list<string> module_names() const {
00224     list<string> names;
00225     map<string, ModuleData>::const_iterator i;
00226     for (i = _modules.begin(); i != _modules.end(); ++i)
00227         names.push_back(i->second.name());
00228     return names;
00229     }
00230 
00234     string str() const;
00235 
00236 private:
00237     map<string, ModuleData> _modules;
00238 
00239     // _net duplicates the storage of this in the RegisterTable map -
00240     // not very efficient
00241     IPNet<A> _valid_subnet;
00242 
00243     // _valid_subnet is the subset of _route->net() that this
00244     // registration is valid for
00245     const IPRouteEntry<A>* _route;
00246 };
00247 
00248 
00262 template<class A>
00263 class RegisterTable : public RouteTable<A> {
00264 public:
00283     RegisterTable(const string& tablename, RegisterServer& register_server,
00284           bool multicast);
00285 
00289     ~RegisterTable()
00290     {}
00291 
00302     int add_route(const IPRouteEntry<A>& route, RouteTable<A>* caller);
00303 
00314     int delete_route(const IPRouteEntry<A>* route, RouteTable<A>* caller);
00315 
00320     const IPRouteEntry<A>* lookup_route(const IPNet<A>& net) const {
00321         return _parent->lookup_route(net);
00322     }
00323 
00328     const IPRouteEntry<A>* lookup_route(const A& addr) const {
00329         return _parent->lookup_route(addr);
00330     }
00331 
00337     RouteRange<A>* lookup_route_range(const A& addr) const {
00338         return _parent->lookup_route_range(addr);
00339     }
00340 
00348     void replumb(RouteTable<A>* old_parent, RouteTable<A>* new_parent);
00349 
00353     RouteTable<A>* parent()         { return _parent;   }
00354 
00358     string str() const;
00359 
00364     void print();
00365 
00366     // Stuff specific to a Register Table
00367 
00381     RouteRegister<A>* register_route_range(const A& addr,
00382                        const string& module);
00383 
00394     int deregister_route_range(const IPNet<A>& subnet, const string& module);
00395 
00399     TableType type() const  { return REGISTER_TABLE; }
00400 
00405     void flush();
00406     
00407 private:
00408     RouteRegister<A>* add_registration(const IPNet<A>& net,
00409                        const IPRouteEntry<A>* route,
00410                        const string& module);
00411     int delete_registration(const IPNet<A>& net, const string& module);
00412     int notify_relevant_modules(bool add,
00413                 const IPRouteEntry<A>& changed_route);
00414     int find_matches(const IPRouteEntry<A>& route);
00415 
00416     void notify_invalidated(typename Trie<A, RouteRegister<A>* >::iterator trie_iter);
00417     void notify_route_changed(typename Trie<A, RouteRegister<A>* >::iterator trie_iter,
00418                   const IPRouteEntry<A>& changed_route);
00419 
00420     map<string, ModuleData> _module_names;
00421     Trie<A, RouteRegister<A>* >     _ipregistry;
00422     RouteTable<A>*          _parent;
00423     RegisterServer&         _register_server;
00424     bool                _multicast;  // true if a multicast rib
00425 };
00426 
00427 #endif // __RIB_RT_TAB_REGISTER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations