xorp

io_link_pcap.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2007-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 // 
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/fea/data_plane/io/io_link_pcap.hh,v 1.15 2008/10/26 23:25:08 pavlin Exp $
00022 
00023 #ifndef __FEA_DATA_PLANE_IO_IO_LINK_PCAP_HH__
00024 #define __FEA_DATA_PLANE_IO_IO_LINK_PCAP_HH__
00025 
00026 //
00027 // I/O link raw communication support.
00028 //
00029 // The mechanism is pcap(3).
00030 //
00031 
00032 #include "libxorp/xorp.h"
00033 #include "libxorp/eventloop.hh"
00034 
00035 #ifdef HAVE_PCAP_H
00036 #include <pcap.h>
00037 #endif
00038 
00039 #include "fea/io_link.hh"
00040 
00041 #ifndef HAVE_PCAP_H
00042 typedef void pcap_t;
00043 #endif
00044 
00045 
00052 class IoLinkPcap : public IoLink {
00053 public:
00067     IoLinkPcap(FeaDataPlaneManager& fea_data_plane_manager,
00068            const IfTree& iftree, const string& if_name,
00069            const string& vif_name, uint16_t ether_type,
00070            const string& filter_program);
00071 
00075     virtual ~IoLinkPcap();
00076 
00083     int     start(string& error_msg);
00084 
00091     int     stop(string& error_msg);
00092 
00100     int     join_multicast_group(const Mac& group, string& error_msg);
00101     
00109     int     leave_multicast_group(const Mac& group, string& error_msg);
00110 
00121     int     send_packet(const Mac&      src_address,
00122                 const Mac&      dst_address,
00123                 uint16_t        ether_type,
00124                 const vector<uint8_t>& payload,
00125                 string&     error_msg);
00126 
00127 private:
00134     int     open_pcap_access(string& error_msg);
00135     
00142     int     close_pcap_access(string& error_msg);
00143 
00152     int     join_leave_multicast_group(bool is_join, const Mac& group,
00153                        string& error_msg);
00154 
00163     void    ioevent_read_cb(XorpFd fd, IoEventType type);
00164 
00169     void    recv_data();
00170 
00177     int     reopen_pcap_access(string& error_msg);
00178 
00179     // Private state
00180     XorpFd  _packet_fd; // The file descriptor to send and recv packets
00181     pcap_t* _pcap;      // The pcap descriptor to send and recv packets
00182     int     _datalink_type; // The pcap-defined DLT_* data link type
00183     char*   _pcap_errbuf;   // The pcap buffer for error messages
00184     int     _multicast_sock; // The socket to join L2 multicast groups
00185 
00186     XorpTask    _recv_data_task; // Task for receiving pending data
00187 };
00188 
00189 #endif // __FEA_DATA_PLANE_IO_IO_LINK_PCAP_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations