xorp

io_tcpudp.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2007-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/io_tcpudp.hh,v 1.12 2008/10/02 21:56:48 bms Exp $
00021 
00022 
00023 #ifndef __FEA_IO_TCPUDP_HH__
00024 #define __FEA_IO_TCPUDP_HH__
00025 
00026 
00027 
00028 #include "fea_data_plane_manager.hh"
00029 
00030 //
00031 // I/O TCP/UDP communication API.
00032 //
00033 
00034 
00035 class EventLoop;
00036 class IfTree;
00037 class IfTreeInterface;
00038 class IfTreeVif;
00039 class IoTcpUdpManager;
00040 class IoTcpUdpReceiver;
00041 class IPvX;
00042 
00043 
00047 class IoTcpUdp {
00048 public:
00059     IoTcpUdp(FeaDataPlaneManager& fea_data_plane_manager, const IfTree& iftree,
00060          int family, bool is_tcp);
00061 
00065     virtual ~IoTcpUdp();
00066 
00072     IoTcpUdpManager& io_tcpudp_manager() { return _io_tcpudp_manager; }
00073 
00079     FeaDataPlaneManager& fea_data_plane_manager() { return _fea_data_plane_manager; }
00080 
00086     virtual bool is_running() const { return _is_running; }
00087 
00093     EventLoop& eventloop() { return (_eventloop); }
00094 
00100     const IfTree& iftree() const { return (_iftree); }
00101 
00107     virtual int family() const { return (_family); }
00108 
00114     virtual bool is_tcp() const { return (_is_tcp); }
00115 
00121     IoTcpUdpReceiver* io_tcpudp_receiver() { return (_io_tcpudp_receiver); }
00122 
00128     virtual void register_io_tcpudp_receiver(IoTcpUdpReceiver* io_tcpudp_receiver);
00129 
00133     virtual void unregister_io_tcpudp_receiver();
00134 
00141     virtual int start(string& error_msg) = 0;
00142 
00149     virtual int stop(string& error_msg) = 0;
00150 
00157     virtual int tcp_open(string& error_msg) = 0;
00158 
00165     virtual int udp_open(string& error_msg) = 0;
00166 
00175     virtual int tcp_open_and_bind(const IPvX& local_addr, uint16_t local_port,
00176                   string& error_msg) = 0;
00177 
00186     virtual int udp_open_and_bind(const IPvX& local_addr, uint16_t local_port, const string& local_dev,
00187                   int reuse, string& error_msg) = 0;
00188 
00200     virtual int udp_open_bind_join(const IPvX& local_addr, uint16_t local_port,
00201                    const IPvX& mcast_addr, uint8_t ttl,
00202                    bool reuse, string& error_msg) = 0;
00203 
00214     virtual int tcp_open_bind_connect(const IPvX& local_addr,
00215                       uint16_t local_port,
00216                       const IPvX& remote_addr,
00217                       uint16_t remote_port,
00218                       string& error_msg) = 0;
00219 
00230     virtual int udp_open_bind_connect(const IPvX& local_addr,
00231                       uint16_t local_port,
00232                       const IPvX& remote_addr,
00233                       uint16_t remote_port,
00234                       string& error_msg) = 0;
00235 
00250     virtual int udp_open_bind_broadcast(const string& ifname,
00251                         const string& vifname,
00252                         uint16_t local_port,
00253                         uint16_t remote_port,
00254                         bool reuse,
00255                         bool limited,
00256                         bool connected,
00257                         string& error_msg) = 0;
00258 
00267     virtual int bind(const IPvX& local_addr, uint16_t local_port,
00268              string& error_msg) = 0;
00269 
00278     virtual int udp_join_group(const IPvX& mcast_addr,
00279                    const IPvX& join_if_addr,
00280                    string& error_msg) = 0;
00281 
00290     virtual int udp_leave_group(const IPvX& mcast_addr,
00291                 const IPvX& leave_if_addr,
00292                 string& error_msg) = 0;
00293 
00300     virtual int close(string& error_msg) = 0;
00301 
00312     virtual int tcp_listen(uint32_t backlog, string& error_msg) = 0;
00313 
00324     virtual int udp_enable_recv(string& error_msg) = 0;
00325 
00333     virtual int send(const vector<uint8_t>& data, string& error_msg) = 0;
00334 
00347     virtual int send_to(const IPvX& remote_addr, uint16_t remote_port,
00348             const vector<uint8_t>& data, string& error_msg) = 0;
00349 
00359     virtual int send_from_multicast_if(const IPvX& group_addr,
00360                        uint16_t group_port,
00361                        const IPvX& ifaddr,
00362                        const vector<uint8_t>& data,
00363                        string& error_msg) = 0;
00364 
00382     virtual int set_socket_option(const string& optname, uint32_t optval,
00383                   string& error_msg) = 0;
00384 
00394     virtual int set_socket_option(const string& optname,
00395                                   const string& optval,
00396                   string& error_msg) = 0;
00397 
00406     virtual int accept_connection(bool is_accepted, string& error_msg) = 0;
00407 
00408 protected:
00420     virtual void recv_event(const string&       if_name,
00421                 const string&       vif_name,
00422                 const IPvX&         src_host,
00423                 uint16_t            src_port,
00424                 const vector<uint8_t>&  data);
00425 
00435     virtual void inbound_connect_event(const IPvX&  src_host,
00436                        uint16_t     src_port,
00437                        IoTcpUdp*    new_io_tcpudp);
00438 
00444     virtual void outgoing_connect_event();
00445 
00453     virtual void error_event(const string&      error,
00454                  bool           fatal);
00455 
00463     virtual void disconnect_event();
00464 
00465     // Misc other state
00466     bool    _is_running;
00467 
00468 private:
00469     IoTcpUdpManager&    _io_tcpudp_manager; // The I/O TCP/UDP manager
00470     FeaDataPlaneManager& _fea_data_plane_manager; // The data plane manager
00471     EventLoop&      _eventloop;     // The event loop to use
00472     const IfTree&   _iftree;        // The interface tree to use
00473     const int       _family;        // The address family
00474     const bool      _is_tcp;    // If true, this is TCP, otherwise UDP
00475     IoTcpUdpReceiver*   _io_tcpudp_receiver;    // The registered receiver
00476 };
00477 
00486 class IoTcpUdpReceiver {
00487 public:
00491     IoTcpUdpReceiver() {}
00492 
00496     virtual ~IoTcpUdpReceiver() {}
00497 
00509     virtual void recv_event(const string&       if_name,
00510                 const string&       vif_name,
00511                 const IPvX&         src_host,
00512                 uint16_t            src_port,
00513                 const vector<uint8_t>&  data) = 0;
00514 
00524     virtual void inbound_connect_event(const IPvX&  src_host,
00525                        uint16_t     src_port,
00526                        IoTcpUdp*    Xnew_io_tcpudp) = 0;
00527 
00533     virtual void outgoing_connect_event() = 0;
00534 
00542     virtual void error_event(const string&      error,
00543                  bool           fatal) = 0;
00544 
00552     virtual void disconnect_event() = 0;
00553 };
00554 
00555 #endif // __FEA_IO_TCPUDP_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations