xorp

port_io.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2009 XORP, Inc.
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/rip/port_io.hh,v 1.16 2008/10/02 21:58:17 bms Exp $
00021 
00022 #ifndef __RIP_PORT_IO_HH__
00023 #define __RIP_PORT_IO_HH__
00024 
00025 #include "constants.hh"
00026 #include "libxorp/ipv4.hh"
00027 #include "libxorp/ipv6.hh"
00028 
00029 template <typename A>
00030 class PortIOUserBase;
00031 
00038 template <typename A>
00039 class PortIOBase
00040 {
00041 public:
00042     typedef A           Addr;
00043     typedef PortIOUserBase<A>   PortIOUser;
00044 public:
00048     PortIOBase(PortIOUser&  user,
00049            const string&    ifname,
00050            const string&    vifname,
00051            const Addr&  address,
00052            bool     enabled = true);
00053 
00054     virtual ~PortIOBase();
00055 
00073     virtual bool send(const Addr&       dst_addr,
00074               uint16_t          dst_port,
00075               const vector<uint8_t>&    rip_packet) = 0;
00076 
00081     virtual bool pending() const = 0;
00082 
00086     const string& ifname() const { return _ifname; }
00087 
00091     const string& vifname() const { return _vifname; }
00092 
00096     const Addr& address() const { return _addr; }
00097 
00101     size_t max_route_entries_per_packet() const { return _max_rte_pp; }
00102 
00107     bool set_max_route_entries_per_packet(size_t max_entries);
00108 
00112     bool enabled() const { return _en; }
00113 
00122     void set_enabled(bool en);
00123 
00124 protected:
00125     PortIOUser& _user;
00126     string  _ifname;
00127     string  _vifname;
00128     Addr    _addr;
00129     size_t  _max_rte_pp;
00130     bool    _en;
00131 };
00132 
00133 
00137 template <typename A>
00138 class PortIOUserBase {
00139 public:
00140     typedef PortIOBase<A> PortIO;
00141 public:
00142     PortIOUserBase() : _pio(0) {}
00143 
00144     virtual ~PortIOUserBase();
00145 
00146     virtual void port_io_send_completion(bool       success) = 0;
00147 
00148     virtual void port_io_receive(const A&   src_addr,
00149                  uint16_t   src_port,
00150                  const uint8_t* rip_packet,
00151                  size_t     rip_packet_bytes) = 0;
00152 
00153     virtual void port_io_enabled_change(bool en) = 0;
00154 
00155     bool set_io_handler(PortIO* pio, bool set_owner);
00156 
00157     PortIO* io_handler();
00158 
00159     const PortIO* io_handler() const;
00160 
00161     bool port_io_enabled() const;
00162 
00163 protected:
00164     PortIO* _pio;
00165     bool    _pio_owner;
00166 };
00167 
00168 
00169 // ----------------------------------------------------------------------------
00170 // Inline PortIOBase Methods
00171 //
00172 
00173 template <typename A>
00174 PortIOBase<A>::PortIOBase(PortIOUser&   user,
00175               const string& ifname,
00176               const string& vifname,
00177               const Addr&   addr,
00178               bool      en)
00179     : _user(user), _ifname(ifname), _vifname(vifname), _addr(addr),
00180       _max_rte_pp(RIPv2_ROUTES_PER_PACKET), _en(en)
00181 {}
00182 
00183 template <typename A>
00184 PortIOBase<A>::~PortIOBase()
00185 {}
00186 
00187 template <>
00188 inline bool
00189 PortIOBase<IPv4>::set_max_route_entries_per_packet(size_t)
00190 {
00191     return false;
00192 }
00193 
00194 template <>
00195 inline bool
00196 PortIOBase<IPv6>::set_max_route_entries_per_packet(size_t max_entries)
00197 {
00198     _max_rte_pp = max_entries;
00199     return true;
00200 }
00201 
00202 template <typename A>
00203 inline void
00204 PortIOBase<A>::set_enabled(bool en)
00205 {
00206     if (en != _en) {
00207     _en = en;
00208     _user.port_io_enabled_change(en);
00209     }
00210 }
00211 
00212 
00213 // ----------------------------------------------------------------------------
00214 // Inline PortIOUserBase Methods
00215 //
00216 
00217 template <typename A>
00218 PortIOUserBase<A>::~PortIOUserBase()
00219 {
00220     if (_pio && _pio_owner)
00221     delete _pio;
00222     _pio = 0;
00223 }
00224 
00225 template <typename A>
00226 inline bool
00227 PortIOUserBase<A>::set_io_handler(PortIO* pio, bool set_owner)
00228 {
00229     if (_pio == 0) {
00230     _pio = pio;
00231     _pio_owner = set_owner;
00232     return true;
00233     }
00234     return false;
00235 }
00236 
00237 template <typename A>
00238 inline PortIOBase<A>*
00239 PortIOUserBase<A>::io_handler()
00240 {
00241     return _pio;
00242 }
00243 
00244 template <typename A>
00245 inline const PortIOBase<A>*
00246 PortIOUserBase<A>::io_handler() const
00247 {
00248     return _pio;
00249 }
00250 
00251 template <typename A>
00252 inline bool
00253 PortIOUserBase<A>::port_io_enabled() const
00254 {
00255     if (_pio)
00256     return _pio->enabled();
00257     return false;
00258 }
00259 
00260 #endif // __RIP_PEER_IO_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations