xorp

io_ip_socket.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-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 
00021 #ifndef __FEA_DATA_PLANE_IO_IO_IP_SOCKET_HH__
00022 #define __FEA_DATA_PLANE_IO_IO_IP_SOCKET_HH__
00023 
00024 //
00025 // I/O IP raw communication support.
00026 //
00027 // The mechanism is UNIX raw sockets.
00028 //
00029 
00030 #include "libxorp/xorp.h"
00031 #include "libxorp/eventloop.hh"
00032 
00033 #ifdef HAVE_SYS_UIO_H
00034 #include <sys/uio.h>
00035 #endif
00036 
00037 #include "fea/io_ip.hh"
00038 
00039 // Shall we use one socket per interface for receiving?  Defaulting
00040 // to enabled for Linux (its the only thing that supports SO_BINDTODEVICE it seems.)
00041 #ifdef SO_BINDTODEVICE
00042 #define USE_SOCKET_PER_IFACE
00043 #endif
00044 
00045 
00052 class IoIpSocket : public IoIp, public IfTreeListener {
00053 public:
00064     IoIpSocket(FeaDataPlaneManager& fea_data_plane_manager,
00065            const IfTree& iftree, int family, uint8_t ip_protocol);
00066 
00070     virtual ~IoIpSocket();
00071 
00078     int     start(string& error_msg);
00079 
00086     int     stop(string& error_msg);
00087 
00096     int     set_multicast_ttl(int ttl, string& error_msg);
00097 
00109     int     enable_multicast_loopback(bool is_enabled, string& error_msg);
00110 
00121     int     set_default_multicast_interface(const string& if_name,
00122                         const string& vif_name,
00123                         string& error_msg);
00124 
00134     int     join_multicast_group(const string& if_name,
00135                      const string& vif_name,
00136                      const IPvX& group,
00137                      string& error_msg);
00138     
00148     int     leave_multicast_group(const string& if_name,
00149                       const string& vif_name,
00150                       const IPvX& group,
00151                       string& error_msg);
00152 
00179     int     send_packet(const string&   if_name,
00180                 const string&   vif_name,
00181                 const IPvX&     src_address,
00182                 const IPvX&     dst_address,
00183                 int32_t     ip_ttl,
00184                 int32_t     ip_tos,
00185                 bool        ip_router_alert,
00186                 bool        ip_internet_control,
00187                 const vector<uint8_t>& ext_headers_type,
00188                 const vector<vector<uint8_t> >& ext_headers_payload,
00189                 const vector<uint8_t>& payload,
00190                 string&     error_msg);
00191 
00198     XorpFd* findExistingInputSocket(const string& if_name, const string& vif_name);
00199 
00204     XorpFd* mcast_protocol_fd_in();
00205 
00207     void notifyDeletingIface(const string& ifname);
00208     void notifyErasingIface(const string& ifname) { notifyDeletingIface(ifname); }
00209 
00211     void notifyDeletingVif(const string& ifname, const string& vifname);
00212     void notifyErasingVif(const string& ifname, const string& vifname) {
00213     notifyDeletingVif(ifname, vifname);
00214     }
00215 
00216 
00217 private:
00227     int     open_proto_sockets(string& error_msg);
00228 
00229 
00231     int initializeInputSocket(XorpFd* rv, string& error_msg);
00232 
00236     XorpFd* findOrCreateInputSocket(const string& if_name, const string& vif_name,
00237                     string& error_msg);
00238 
00245     int     close_proto_sockets(string& error_msg);
00246 
00250     int cleanupXorpFd(XorpFd* fd);
00251 
00252 
00271     int     enable_ip_hdr_include(bool is_enabled, string& error_msg);
00272 
00285     int enable_recv_pktinfo(XorpFd* input_fd, bool is_enabled, string& error_msg);
00286 
00296     void    proto_socket_read(XorpFd fd, IoEventType type);
00297 
00308     int     proto_socket_transmit(const IfTreeInterface* ifp,
00309                       const IfTreeVif*  vifp,
00310                       const IPvX&   src_address,
00311                       const IPvX&   dst_address,
00312                       string&       error_msg);
00313 
00314     // Private state
00315     //XorpFd    _proto_socket_in;    // The socket to receive protocol message
00316     // The key is "if_name vif_name"
00317     map<string, XorpFd*> _proto_sockets_in;
00318 #ifdef USE_SOCKET_PER_IFACE
00319     XorpFd        _mcast_proto_socket_in;
00320 #endif
00321     XorpFd  _proto_socket_out;   // The socket to end protocol message
00322     bool    _is_ip_hdr_included; // True if IP header is included on send
00323     uint16_t    _ip_id;          // IPv4 Header ID
00324 
00325     uint8_t*    _rcvbuf;    // Data buffer for receiving
00326     uint8_t*    _sndbuf;    // Data buffer for sending
00327     uint8_t*    _rcvcmsgbuf;    // Control recv info (IPv6 only)
00328     uint8_t*    _sndcmsgbuf;    // Control send info (IPv6 only)
00329 
00330     struct iovec    _rcviov[1]; // The scatter/gatter array for receiving
00331     struct iovec    _sndiov[1]; // The scatter/gatter array for sending
00332 
00333 #ifndef HOST_OS_WINDOWS
00334     struct msghdr   _rcvmh; // The msghdr structure used by recvmsg()
00335     struct msghdr   _sndmh; // The msghdr structure used by sendmsg()
00336 #endif // ! HOST_OS_WINDOWS
00337 
00338     struct sockaddr_in  _from4; // The source addr of recvmsg() msg (IPv4)
00339     struct sockaddr_in  _to4;   // The dest.  addr of sendmsg() msg (IPv4)
00340 #ifdef HAVE_IPV6
00341     struct sockaddr_in6 _from6; // The source addr of recvmsg() msg (IPv6)
00342     struct sockaddr_in6 _to6;   // The dest.  addr of sendmsg() msg (IPv6)
00343 #endif
00344 };
00345 
00346 #endif // __FEA_DATA_PLANE_IO_IO_IP_SOCKET_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations