xorp

finder_tcp.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-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxipc/finder_tcp.hh,v 1.25 2008/10/02 21:57:20 bms Exp $
00023 
00024 #ifndef __LIBXIPC_FINDER_TCP_HH__
00025 #define __LIBXIPC_FINDER_TCP_HH__
00026 
00027 #include "libxorp/xorp.h"
00028 #include "libxorp/asyncio.hh"
00029 #include "libxorp/eventloop.hh"
00030 #include "libxorp/exceptions.hh"
00031 #include "libxorp/ipv4.hh"
00032 #include "libxorp/ipv4net.hh"
00033 #include "libxorp/ref_ptr.hh"
00034 
00035 #ifdef HAVE_SYS_UIO_H
00036 #include <sys/uio.h>
00037 #endif
00038 
00039 
00040 #define MAX_XRL_INPUT_SIZE  65536   // maximum total XRL input buffer
00041                     // size on the network.
00042 
00043 class FinderTcpBase :
00044     public NONCOPYABLE
00045 {
00046 public:
00047     FinderTcpBase(EventLoop& e, XorpFd fd);
00048 
00049     virtual ~FinderTcpBase();
00050 
00061     virtual bool read_event(int        errval,
00062                 const uint8_t* data,
00063                 uint32_t       data_bytes) = 0;
00064 
00069     void set_read_enabled(bool en);
00070 
00075     bool read_enabled() const;
00076 
00088     bool write_data(const uint8_t* data, uint32_t data_bytes);
00089 
00090     bool write_data(const iovec* iov, uint32_t iovcnt);
00091 
00098     virtual void write_event(int        errval,
00099                  const uint8_t* data,
00100                  uint32_t       data_bytes) = 0;
00101 
00106     virtual void close_event();
00107 
00108     virtual void error_event();
00109 
00110     void close();
00111     bool closed() const;
00112 
00113 protected:
00114     void read_callback(AsyncFileOperator::Event,
00115                const uint8_t*, size_t, size_t);
00116     void write_callback(AsyncFileOperator::Event,
00117             const uint8_t*, size_t, size_t);
00118 
00119 protected:
00120     XorpFd _sock;
00121     vector<uint8_t> _input_buffer;
00122 
00123     AsyncFileReader _reader;
00124     AsyncFileWriter _writer;
00125 
00126     uint32_t _isize;    // input buffer size as received.
00127     uint32_t _osize;    // output buffer size as advertised.
00128 };
00129 
00130 class FinderTcpListenerBase :
00131     public NONCOPYABLE
00132 {
00133 public:
00134     typedef vector<IPv4> AddrList;
00135     typedef vector<IPv4Net> NetList;
00136 
00137 public:
00138     FinderTcpListenerBase(EventLoop&    e,
00139               IPv4      iface,
00140               uint16_t  port,
00141               bool      en = true)
00142     throw (InvalidAddress, InvalidPort);
00143 
00144     virtual ~FinderTcpListenerBase();
00145 
00155     virtual bool connection_event(XorpFd fd) = 0;
00156 
00160     bool enabled() const;
00161 
00165     void set_enabled(bool en);
00166 
00170     IPv4 address() const        { return _addr; }
00171 
00175     uint16_t port() const       { return _port; }
00176 
00177 protected:
00182     void connect_hook(XorpFd fd, IoEventType type);
00183 
00184     EventLoop& eventloop() const { return _e; }
00185 
00186 protected:
00187     EventLoop&  _e;
00188     XorpFd  _lsock;
00189     bool    _en;
00190 
00191     IPv4    _addr;
00192     uint16_t    _port;
00193 
00194     AddrList    _ok_addrs;
00195     NetList _ok_nets;
00196 };
00197 
00198 #endif // __LIBXIPC_FINDER_TCP_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations