GNU Radio 3.4.2 C++ API
|
00001 /* -*- c++ -*- */ 00002 /* 00003 * Copyright 2005,2007,2008 Free Software Foundation, Inc. 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 as published by 00007 * the Free Software Foundation, either version 3 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 #ifndef INCLUDED_USRP2_ETHERNET_H 00020 #define INCLUDED_USRP2_ETHERNET_H 00021 00022 #include <string> 00023 #include <vector> 00024 #include <eth_common.h> 00025 00026 namespace usrp2 { 00027 00028 class pktfilter; 00029 00030 /*! 00031 * \brief Read and write ethernet frames. 00032 * 00033 * This provides a low level interface to hardware that communicates 00034 * via raw (non-IP) ethernet frames. 00035 */ 00036 class ethernet { 00037 int d_fd; 00038 uint8_t d_mac[6]; 00039 00040 public: 00041 ethernet (); 00042 ~ethernet (); 00043 00044 static const int MAX_PKTLEN = 1512; 00045 static const int MIN_PKTLEN = 64; 00046 00047 /*! 00048 * \param ifname ethernet interface name, e.g., "eth0" 00049 * \param protocol is the ethertype protocol number in network order. 00050 * Use 0 to receive all protocols. 00051 */ 00052 bool open (std::string ifname, int protocol); 00053 00054 bool close (); 00055 00056 /*! 00057 * \brief attach packet filter to socket to restrict which packets read sees. 00058 * \param pf the packet filter 00059 */ 00060 bool attach_pktfilter (pktfilter *pf); 00061 00062 /*! 00063 * \brief return 6 byte string containing our MAC address 00064 */ 00065 const uint8_t *mac () const { return d_mac; } 00066 00067 /*! 00068 * \brief Return file descriptor associated with socket. 00069 */ 00070 int fd () const { return d_fd; } 00071 00072 /*! 00073 * \brief Read packet from interface. 00074 * 00075 * \param buf where to put the packet 00076 * \param buflen maximum length of packet in bytes (should be >= 1528) 00077 * 00078 * \returns number of bytes read or -1 if trouble. 00079 * 00080 * Returned packet includes 14-byte ethhdr 00081 */ 00082 int read_packet (void *buf, int buflen); 00083 00084 /*! 00085 * \brief Read packet from interface, but don't block waiting 00086 * 00087 * \param buf where to put the packet 00088 * \param buflen maximum length of packet in bytes (should be >= 1528) 00089 * 00090 * \returns number of bytes read, -1 if trouble or 0 if nothing available. 00091 * 00092 * Returned packet includes 14-byte ethhdr 00093 */ 00094 int read_packet_dont_block (void *buf, int buflen); 00095 00096 /* 00097 * \brief Write ethernet packet to interface. 00098 * 00099 * \param buf the packet to write 00100 * \param buflen length of packet in bytes 00101 * 00102 * \returns number of bytes written or -1 if trouble. 00103 * 00104 * Packet must begin with 14-byte ethhdr, but does not include the FCS. 00105 */ 00106 int write_packet (const void *buf, int buflen); 00107 00108 /* 00109 * \brief Write ethernet packet to interface. 00110 * 00111 * \param iov scatter/gather array 00112 * \param iovlen number of elements in iov 00113 * 00114 * \returns number of bytes written or -1 if trouble. 00115 * 00116 * Packet must begin with 14-byte ethhdr, but does not include the FCS. 00117 */ 00118 int write_packetv (const eth_iovec *iov, size_t iovlen); 00119 00120 }; 00121 00122 } // namespace usrp2 00123 00124 #endif /* INCLUDED_USRP2_ETHERNET_H */