xorp

io_tcpudp_manager.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_manager.hh,v 1.13 2008/10/02 21:56:49 bms Exp $
00021 
00022 #ifndef __FEA_IO_TCPUDP_MANAGER_HH__
00023 #define __FEA_IO_TCPUDP_MANAGER_HH__
00024 
00025 
00026 
00027 
00028 
00029 #include "libxorp/ipvx.hh"
00030 
00031 #include "fea_io.hh"
00032 #include "io_tcpudp.hh"
00033 
00034 class FeaNode;
00035 class FeaDataPlaneManager;
00036 
00037 
00041 class IoTcpUdpComm :
00042     public NONCOPYABLE,
00043     public IoTcpUdpReceiver
00044 {
00045 public:
00049     class JoinedMulticastGroup {
00050     public:
00051     JoinedMulticastGroup(const IPvX& interface_address,
00052                  const IPvX& group_address)
00053         : _interface_address(interface_address),
00054           _group_address(group_address)
00055     {}
00056 #ifdef XORP_USE_USTL
00057     JoinedMulticastGroup() { }
00058 #endif
00059     virtual ~JoinedMulticastGroup() {}
00060 
00061     const IPvX& interface_address() const { return _interface_address; }
00062     const IPvX& group_address() const { return _group_address; }
00063 
00071     bool operator<(const JoinedMulticastGroup& other) const {
00072         if (_interface_address != other._interface_address)
00073         return (_interface_address < other._interface_address);
00074         return (_group_address < other._group_address);
00075     }
00076 
00084     bool operator==(const JoinedMulticastGroup& other) const {
00085         return ((_interface_address == other._interface_address)
00086             && (_group_address == other._group_address));
00087     }
00088 
00094     void add_receiver(const string& receiver_name) {
00095         _receivers.insert(receiver_name);
00096     }
00097 
00103     void delete_receiver(const string& receiver_name) {
00104         _receivers.erase(receiver_name);
00105     }
00106 
00110     bool empty() const { return _receivers.empty(); }
00111 
00112     private:
00113     IPvX        _interface_address;
00114     IPvX        _group_address;
00115     set<string> _receivers;
00116     };
00117 
00129     IoTcpUdpComm(IoTcpUdpManager& io_tcpudp_manager, const IfTree& iftree,
00130          int family, bool is_tcp, const string& creator);
00131 
00146     IoTcpUdpComm(IoTcpUdpManager& io_tcpudp_manager, const IfTree& iftree,
00147          int family, bool is_tcp, const string& creator,
00148          const string& listener_sockid, const IPvX& peer_host,
00149          uint16_t peer_port);
00150 
00154     virtual ~IoTcpUdpComm();
00155 
00159     void allocate_io_tcpudp_plugins();
00160 
00164     void deallocate_io_tcpudp_plugins();
00165 
00171     void allocate_io_tcpudp_plugin(FeaDataPlaneManager* fea_data_plane_manager);
00172 
00178     void deallocate_io_tcpudp_plugin(FeaDataPlaneManager* fea_data_plane_manager);
00179 
00185     void add_plugin(IoTcpUdp* new_io_tcpudp);
00186 
00190     void start_io_tcpudp_plugins();
00191 
00195     void stop_io_tcpudp_plugins();
00196 
00205     int tcp_open(string& sockid, string& error_msg);
00206 
00215     int udp_open(string& sockid, string& error_msg);
00216 
00227     int tcp_open_and_bind(const IPvX& local_addr, uint16_t local_port,
00228               string& sockid, string& error_msg);
00229 
00240     int udp_open_and_bind(const IPvX& local_addr, uint16_t local_port,
00241               const string& local_dev, int reuse, string& sockid, string& error_msg);
00242 
00256     int udp_open_bind_join(const IPvX& local_addr, uint16_t local_port,
00257                const IPvX& mcast_addr, uint8_t ttl, bool reuse,
00258                string& sockid, string& error_msg);
00259 
00272     int tcp_open_bind_connect(const IPvX& local_addr, uint16_t local_port,
00273                   const IPvX& remote_addr, uint16_t remote_port,
00274                   string& sockid, string& error_msg);
00275 
00288     int udp_open_bind_connect(const IPvX& local_addr, uint16_t local_port,
00289                   const IPvX& remote_addr, uint16_t remote_port,
00290                   string& sockid, string& error_msg);
00291 
00308     int udp_open_bind_broadcast(const string& ifname, const string& vifname,
00309                     uint16_t local_port, uint16_t remote_port,
00310                     bool reuse, bool limited, bool connected,
00311                     string& sockid, string& error_msg);
00312 
00321     int bind(const IPvX& local_addr, uint16_t local_port, string& error_msg);
00322 
00331     int udp_join_group(const IPvX& mcast_addr, const IPvX& join_if_addr,
00332                string& error_msg);
00333 
00342     int udp_leave_group(const IPvX& mcast_addr, const IPvX& leave_if_addr,
00343             string& error_msg);
00344 
00351     int close(string& error_msg);
00352 
00363     int tcp_listen(uint32_t backlog, string& error_msg);
00364 
00371     int udp_enable_recv(string& error_msg);
00372 
00380     int send(const vector<uint8_t>& data, string& error_msg);
00381 
00394     int send_to(const IPvX& remote_addr, uint16_t remote_port,
00395         const vector<uint8_t>& data, string& error_msg);
00396 
00409     int send_from_multicast_if(const IPvX& group_addr, uint16_t group_port,
00410                    const IPvX& ifaddr, const vector<uint8_t>& data,
00411                    string& error_msg);
00412 
00433     int set_socket_option(const string& optname, uint32_t optval,
00434               string& error_msg);
00435 
00448     int set_socket_option(const string& optname, const string& optval,
00449               string& error_msg);
00450 
00459     int accept_connection(bool is_accepted, string& error_msg);
00460 
00472     void recv_event(const string&       if_name,
00473             const string&       vif_name,
00474             const IPvX&         src_host,
00475             uint16_t            src_port,
00476             const vector<uint8_t>&  data);
00477 
00487     void inbound_connect_event(const IPvX&  src_host,
00488                    uint16_t     src_port,
00489                    IoTcpUdp*    new_io_tcpudp);
00490 
00496     void outgoing_connect_event();
00497 
00505     void error_event(const string&      error,
00506              bool           fatal);
00507 
00515     void disconnect_event();
00516 
00522     const string& creator() const { return (_creator); }
00523 
00529     const string& sockid() const { return (_sockid); }
00530 
00536     const string& listener_sockid() const { return (_listener_sockid); }
00537 
00543     const IPvX& peer_host() const { return (_peer_host); }
00544 
00550     uint16_t peer_port() const { return (_peer_port); }
00551 
00552 private:
00553     IoTcpUdpComm(const IoTcpUdpComm&);          // Not implemented.
00554     IoTcpUdpComm& operator=(const IoTcpUdpComm&);   // Not implemented.
00555 
00556     IoTcpUdpManager&        _io_tcpudp_manager;
00557     const IfTree&       _iftree;
00558     const int           _family;
00559     const bool          _is_tcp;
00560     const string        _creator;
00561     const string        _sockid;
00562 
00563     // State for connected entries
00564     const string        _listener_sockid;
00565     const IPvX          _peer_host;
00566     const uint16_t      _peer_port;
00567 
00568     typedef list<pair<FeaDataPlaneManager*, IoTcpUdp*> >IoTcpUdpPlugins;
00569     IoTcpUdpPlugins     _io_tcpudp_plugins;
00570 
00571     typedef map<JoinedMulticastGroup, JoinedMulticastGroup> JoinedGroupsTable;
00572     JoinedGroupsTable       _joined_groups_table;
00573 };
00574 
00579 class IoTcpUdpManagerReceiver {
00580 public:
00584     virtual ~IoTcpUdpManagerReceiver() {}
00585 
00599     virtual void recv_event(const string&       receiver_name,
00600                 const string&       sockid,
00601                 const string&       if_name,
00602                 const string&       vif_name,
00603                 const IPvX&         src_host,
00604                 uint16_t            src_port,
00605                 const vector<uint8_t>&  data) = 0;
00606 
00618     virtual void inbound_connect_event(const string&    receiver_name,
00619                        const string&    sockid,
00620                        const IPvX&  src_host,
00621                        uint16_t     src_port,
00622                        const string&    new_sockid) = 0;
00623 
00634     virtual void outgoing_connect_event(int     family,
00635                     const string&   receiver_name,
00636                     const string&   sockid) = 0;
00637 
00649     virtual void error_event(int            family,
00650                  const string&      receiver_name,
00651                  const string&      sockid,
00652                  const string&      error,
00653                  bool           fatal) = 0;
00654 
00667     virtual void disconnect_event(int           family,
00668                   const string&     receiver_name,
00669                   const string&     sockid) = 0;
00670 };
00671 
00675 class IoTcpUdpManager : public IoTcpUdpManagerReceiver,
00676             public InstanceWatcher {
00677 public:
00681     IoTcpUdpManager(FeaNode& fea_node, const IfTree& iftree);
00682 
00686     virtual ~IoTcpUdpManager();
00687 
00699     int tcp_open(int family, const string& creator, string& sockid,
00700          string& error_msg);
00701 
00713     int udp_open(int family, const string& creator, string& sockid,
00714          string& error_msg);
00715 
00729     int tcp_open_and_bind(int family, const string& creator,
00730               const IPvX& local_addr, uint16_t local_port,
00731               string& sockid, string& error_msg);
00732 
00746     int udp_open_and_bind(int family, const string& creator,
00747               const IPvX& local_addr, uint16_t local_port,
00748               const string& local_dev, int reuse,
00749               string& sockid, string& error_msg);
00750 
00767     int udp_open_bind_join(int family, const string& creator,
00768                const IPvX& local_addr, uint16_t local_port,
00769                const IPvX& mcast_addr, uint8_t ttl, bool reuse,
00770                string& sockid, string& error_msg);
00771 
00787     int tcp_open_bind_connect(int family, const string& creator,
00788                   const IPvX& local_addr, uint16_t local_port,
00789                   const IPvX& remote_addr, uint16_t remote_port,
00790                   string& sockid, string& error_msg);
00791 
00807     int udp_open_bind_connect(int family, const string& creator,
00808                   const IPvX& local_addr, uint16_t local_port,
00809                   const IPvX& remote_addr, uint16_t remote_port,
00810                   string& sockid, string& error_msg);
00811 
00831     int udp_open_bind_broadcast(int family, const string& creator,
00832                     const string& ifname, const string& vifname,
00833                     uint16_t local_port, uint16_t remote_port,
00834                     bool reuse, bool limited, bool connected,
00835                     string& sockid, string& error_msg);
00836 
00848     int bind(int family, const string& sockid, const IPvX& local_addr,
00849          uint16_t local_port, string& error_msg);
00850 
00862     int udp_join_group(int family, const string& sockid,
00863                const IPvX& mcast_addr, const IPvX& join_if_addr,
00864                string& error_msg);
00865 
00877     int udp_leave_group(int family, const string& sockid,
00878             const IPvX& mcast_addr, const IPvX& leave_if_addr,
00879             string& error_msg);
00880 
00890     int close(int family, const string& sockid, string& error_msg);
00891 
00905     int tcp_listen(int family, const string& sockid, uint32_t backlog,
00906            string& error_msg);
00907 
00918     int udp_enable_recv(int family, const string& sockid, string& error_msg);
00919 
00930     int send(int family, const string& sockid, const vector<uint8_t>& data,
00931          string& error_msg);
00932 
00948     int send_to(int family, const string& sockid, const IPvX& remote_addr,
00949         uint16_t remote_port, const vector<uint8_t>& data,
00950         string& error_msg);
00951 
00964     int send_from_multicast_if(int family, const string& sockid,
00965                    const IPvX& group_addr, uint16_t group_port,
00966                    const IPvX& ifaddr, const vector<uint8_t>& data,
00967                    string& error_msg);
00968 
00989     int set_socket_option(int family, const string& sockid,
00990               const string& optname, uint32_t optval,
00991               string& error_msg);
00992 
01005     int set_socket_option(int family, const string& sockid,
01006               const string& optname, const string& optval,
01007               string& error_msg);
01008 
01020     int accept_connection(int family, const string& sockid, bool is_accepted,
01021               string& error_msg);
01022 
01036     void recv_event(const string&       receiver_name,
01037             const string&       sockid,
01038             const string&       if_name,
01039             const string&       vif_name,
01040             const IPvX&         src_host,
01041             uint16_t            src_port,
01042             const vector<uint8_t>&  data);
01043 
01055     void inbound_connect_event(const string&    receiver_name,
01056                    const string&    sockid,
01057                    const IPvX&  src_host,
01058                    uint16_t     src_port,
01059                    const string&    new_sockid);
01060 
01071     void outgoing_connect_event(int     family,
01072                 const string&   receiver_name,
01073                 const string&   sockid);
01074 
01086     void error_event(int            family,
01087              const string&      receiver_name,
01088              const string&      sockid,
01089              const string&      error,
01090              bool           fatal);
01091 
01104     void disconnect_event(int           family,
01105               const string&     receiver_name,
01106               const string&     sockid);
01107 
01113     void instance_birth(const string& instance_name);
01114 
01120     void instance_death(const string& instance_name);
01121 
01126     void set_io_tcpudp_manager_receiver(IoTcpUdpManagerReceiver* v) {
01127     _io_tcpudp_manager_receiver = v;
01128     }
01129 
01135     const IfTree&   iftree() const { return _iftree; }
01136 
01145     int register_data_plane_manager(FeaDataPlaneManager* fea_data_plane_manager,
01146                     bool is_exclusive);
01147 
01154     int unregister_data_plane_manager(FeaDataPlaneManager* fea_data_plane_manager);
01155 
01161     list<FeaDataPlaneManager*>& fea_data_plane_managers() {
01162     return _fea_data_plane_managers;
01163     }
01164 
01178     IoTcpUdpComm* connect_io_tcpudp_comm(int family,
01179                      bool is_tcp,
01180                      const string& creator,
01181                      const string& listener_sockid,
01182                      const IPvX& peer_host,
01183                      uint16_t peer_port,
01184                      IoTcpUdp* new_io_tcpudp);
01185 
01186 private:
01187     typedef map<string, IoTcpUdpComm*> CommTable;
01188 
01196     IoTcpUdpComm*   find_io_tcpudp_comm(int family, const string& sockid,
01197                         string& error_msg);
01207     IoTcpUdpComm*   open_io_tcpudp_comm(int family, bool is_tcp,
01208                         const string& creator,
01209                         bool allocate_plugins = true);
01210 
01217     void        delete_io_tcpudp_comm(int family,
01218                           const string& sockid);
01219 
01226     CommTable&      comm_table_by_family(int family);
01227 
01234     void erase_comm_handlers_by_creator(int family, const string& creator);
01235 
01243     bool has_comm_handler_by_creator(const string& creator) const;
01244 
01251     bool        is_my_address(const IPvX& local_addr) const;
01252 
01253     FeaNode&        _fea_node;
01254     EventLoop&      _eventloop;
01255     const IfTree&   _iftree;
01256 
01257     // Collection of IP communication handlers keyed by sockid.
01258     CommTable       _comm_table4;
01259     CommTable       _comm_table6;
01260 
01261     IoTcpUdpManagerReceiver* _io_tcpudp_manager_receiver;
01262 
01263     list<FeaDataPlaneManager*> _fea_data_plane_managers;
01264 };
01265 
01266 #endif // __FEA_IO_TCPUDP_MANAGER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations