Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / host / lib / ethernet.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2007,2008 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef INCLUDED_USRP2_ETHERNET_H
20 #define INCLUDED_USRP2_ETHERNET_H
21
22 #include <string>
23 #include <vector>
24 #include <eth_common.h>
25
26 namespace usrp2 {
27
28   class pktfilter;
29   
30   /*!
31    * \brief Read and write ethernet frames.
32    *
33    * This provides a low level interface to hardware that communicates
34    * via raw (non-IP) ethernet frames.
35    */
36   class ethernet {
37     int   d_fd;
38     uint8_t d_mac[6];
39     
40   public:
41     ethernet ();
42     ~ethernet ();
43     
44     static const int MAX_PKTLEN = 1512;
45     static const int MIN_PKTLEN = 64;
46     
47     /*!
48      * \param ifname ethernet interface name, e.g., "eth0"
49      * \param protocol is the ethertype protocol number in network order.
50      *    Use 0 to receive all protocols.
51      */
52     bool open (std::string ifname, int protocol);
53     
54     bool close ();
55     
56     /*!
57      * \brief attach packet filter to socket to restrict which packets read sees.
58      * \param pf        the packet filter
59      */
60     bool attach_pktfilter (pktfilter *pf);
61     
62     /*!
63      * \brief return 6 byte string containing our MAC address
64      */
65     const uint8_t *mac () const { return d_mac; }
66     
67     /*!
68      * \brief Return file descriptor associated with socket.
69      */
70     int fd () const { return d_fd; }
71     
72     /*!
73      * \brief Read packet from interface.
74      *
75      * \param buf               where to put the packet
76      * \param buflen    maximum length of packet in bytes (should be >= 1528)
77      *
78      * \returns number of bytes read or -1 if trouble.
79      *
80      * Returned packet includes 14-byte ethhdr
81      */
82     int read_packet (void *buf, int buflen);
83     
84     /*!
85      * \brief Read packet from interface, but don't block waiting
86      *
87      * \param buf               where to put the packet
88      * \param buflen    maximum length of packet in bytes (should be >= 1528)
89      *
90      * \returns number of bytes read, -1 if trouble or 0 if nothing available.
91      *
92      * Returned packet includes 14-byte ethhdr
93      */
94     int read_packet_dont_block (void *buf, int buflen);
95     
96     /*
97      * \brief Write ethernet packet to interface.
98      *
99      * \param buf               the packet to write
100      * \param buflen    length of packet in bytes
101      *
102      * \returns number of bytes written or -1 if trouble.
103      *
104      * Packet must begin with 14-byte ethhdr, but does not include the FCS.
105      */
106     int write_packet (const void *buf, int buflen);
107
108     /*
109      * \brief Write ethernet packet to interface.
110      *
111      * \param iov       scatter/gather array
112      * \param iovlen    number of elements in iov
113      *
114      * \returns number of bytes written or -1 if trouble.
115      *
116      * Packet must begin with 14-byte ethhdr, but does not include the FCS.
117      */
118     int write_packetv (const eth_iovec *iov, size_t iovlen);
119
120   };
121   
122 } // namespace usrp2
123
124 #endif /* INCLUDED_USRP2_ETHERNET_H */