xorp

io_tcpudp_socket.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/data_plane/io/io_tcpudp_socket.hh,v 1.15 2008/10/11 04:20:19 pavlin Exp $
00021 
00022 #ifndef __FEA_DATA_PLANE_IO_IO_TCPUDP_SOCKET_HH__
00023 #define __FEA_DATA_PLANE_IO_IO_TCPUDP_SOCKET_HH__
00024 
00025 //
00026 // I/O TCP/UDP communication support.
00027 //
00028 // The mechanism is UNIX TCP/UDP sockets.
00029 //
00030 
00031 #include "libxorp/xorp.h"
00032 #include "libxorp/eventloop.hh"
00033 #include "libxorp/asyncio.hh"
00034 
00035 #include "fea/io_tcpudp.hh"
00036 
00037 class AsyncFileWriter;
00038 
00039 
00043 class IoTcpUdpSocket : public IoTcpUdp {
00044 public:
00055     IoTcpUdpSocket(FeaDataPlaneManager& fea_data_plane_manager,
00056            const IfTree& iftree, int family, bool is_tcp);
00057 
00061     virtual ~IoTcpUdpSocket();
00062 
00069     int     start(string& error_msg);
00070 
00077     int     stop(string& error_msg);
00078 
00085     int tcp_open(string& error_msg);
00086 
00093     int udp_open(string& error_msg);
00094 
00103     int tcp_open_and_bind(const IPvX& local_addr, uint16_t local_port,
00104               string& error_msg);
00105 
00114     int udp_open_and_bind(const IPvX& local_addr, uint16_t local_port, const string& local_dev,
00115               int reuse, string& error_msg);
00116 
00128     int udp_open_bind_join(const IPvX& local_addr, uint16_t local_port,
00129                const IPvX& mcast_addr, uint8_t ttl, bool reuse,
00130                string& error_msg);
00131 
00142     int tcp_open_bind_connect(const IPvX& local_addr, uint16_t local_port,
00143                   const IPvX& remote_addr, uint16_t remote_port,
00144                   string& error_msg);
00145 
00156     int udp_open_bind_connect(const IPvX& local_addr, uint16_t local_port,
00157                   const IPvX& remote_addr, uint16_t remote_port,
00158                   string& error_msg);
00159 
00174     int udp_open_bind_broadcast(const string& ifname, const string& vifname,
00175                     uint16_t local_port, uint16_t remote_port,
00176                     bool reuse, bool limited, bool connected,
00177                     string& error_msg);
00178 
00187     int bind(const IPvX& local_addr, uint16_t local_port, string& error_msg);
00188 
00197     int udp_join_group(const IPvX& mcast_addr, const IPvX& join_if_addr,
00198                string& error_msg);
00199 
00208     int udp_leave_group(const IPvX& mcast_addr, const IPvX& leave_if_addr,
00209             string& error_msg);
00210 
00217     int close(string& error_msg);
00218 
00229     int tcp_listen(uint32_t backlog, string& error_msg);
00230 
00237     int udp_enable_recv(string& error_msg);
00238 
00246     int send(const vector<uint8_t>& data, string& error_msg);
00247 
00260     int send_to(const IPvX& remote_addr, uint16_t remote_port,
00261         const vector<uint8_t>& data, string& error_msg);
00262 
00272     int send_from_multicast_if(const IPvX& group_addr, uint16_t group_port,
00273                    const IPvX& ifaddr, const vector<uint8_t>& data,
00274                    string& error_msg);
00275 
00293     int set_socket_option(const string& optname, uint32_t optval,
00294               string& error_msg);
00295 
00305     int set_socket_option(const string& optname, const string& optval,
00306               string& error_msg);
00307 
00316     int accept_connection(bool is_accepted, string& error_msg);
00317 
00318 protected:
00324     void set_socket_fd(XorpFd fd) { _socket_fd = fd; }
00325 
00326 private:
00333     int enable_data_recv(string& error_msg);
00334 
00345     int enable_recv_pktinfo(bool is_enabled, string& error_msg);
00346 
00353     void accept_io_cb(XorpFd fd, IoEventType io_event_type);
00354 
00361     void connect_io_cb(XorpFd fd, IoEventType io_event_type);
00362 
00369     void data_io_cb(XorpFd fd, IoEventType io_event_type);
00370 
00380     void send_completed_cb(AsyncFileWriter::Event   event,
00381                const uint8_t*       buffer,
00382                size_t           buffer_bytes,
00383                size_t           offset);
00384 
00393     void disconnect_io_cb(XorpFd fd, IoEventType io_event_type);
00394 
00395     XorpFd  _socket_fd;
00396     IPvX    _peer_address;      // Peer address (valid for TCP only)
00397     uint16_t    _peer_port;     // Peer port (valid for TCP only)
00398     AsyncFileWriter* _async_writer; // Async writer for sending data
00399 
00400     bool        _limited_broadcast_enabled; // true if this is an IPv4/UDP
00401                                             // broadcast socket
00402     IPvX        _network_broadcast_address; // valid at time of creation for
00403                                             // IPv4/UDP broadcasts only
00404 };
00405 
00406 #endif // __FEA_DATA_PLANE_IO_IO_TCPUDP_SOCKET_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations