summaryrefslogtreecommitdiff
path: root/usrp2/host/lib/ethernet.h
blob: 24624f441afb96197eb2bd306f39ee57b3790663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* -*- c++ -*- */
/*
 * Copyright 2005,2007,2008 Free Software Foundation, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_USRP2_ETHERNET_H
#define INCLUDED_USRP2_ETHERNET_H

#include <string>
#include <vector>
#include <eth_common.h>

namespace usrp2 {

  class pktfilter;
  
  /*!
   * \brief Read and write ethernet frames.
   *
   * This provides a low level interface to hardware that communicates
   * via raw (non-IP) ethernet frames.
   */
  class ethernet {
    int	  d_fd;
    uint8_t d_mac[6];
    
  public:
    ethernet ();
    ~ethernet ();
    
    static const int MAX_PKTLEN = 1512;
    static const int MIN_PKTLEN = 64;
    
    /*!
     * \param ifname ethernet interface name, e.g., "eth0"
     * \param protocol is the ethertype protocol number in network order.
     *    Use 0 to receive all protocols.
     */
    bool open (std::string ifname, int protocol);
    
    bool close ();
    
    /*!
     * \brief attach packet filter to socket to restrict which packets read sees.
     * \param pf	the packet filter
     */
    bool attach_pktfilter (pktfilter *pf);
    
    /*!
     * \brief return 6 byte string containing our MAC address
     */
    const uint8_t *mac () const { return d_mac; }
    
    /*!
     * \brief Return file descriptor associated with socket.
     */
    int fd () const { return d_fd; }
    
    /*!
     * \brief Read packet from interface.
     *
     * \param buf		where to put the packet
     * \param buflen	maximum length of packet in bytes (should be >= 1528)
     *
     * \returns number of bytes read or -1 if trouble.
     *
     * Returned packet includes 14-byte ethhdr
     */
    int read_packet (void *buf, int buflen);
    
    /*!
     * \brief Read packet from interface, but don't block waiting
     *
     * \param buf		where to put the packet
     * \param buflen	maximum length of packet in bytes (should be >= 1528)
     *
     * \returns number of bytes read, -1 if trouble or 0 if nothing available.
     *
     * Returned packet includes 14-byte ethhdr
     */
    int read_packet_dont_block (void *buf, int buflen);
    
    /*
     * \brief Write ethernet packet to interface.
     *
     * \param buf		the packet to write
     * \param buflen	length of packet in bytes
     *
     * \returns number of bytes written or -1 if trouble.
     *
     * Packet must begin with 14-byte ethhdr, but does not include the FCS.
     */
    int write_packet (const void *buf, int buflen);

    /*
     * \brief Write ethernet packet to interface.
     *
     * \param iov	scatter/gather array
     * \param iovlen	number of elements in iov
     *
     * \returns number of bytes written or -1 if trouble.
     *
     * Packet must begin with 14-byte ethhdr, but does not include the FCS.
     */
    int write_packetv (const eth_iovec *iov, size_t iovlen);

  };
  
} // namespace usrp2

#endif /* INCLUDED_USRP2_ETHERNET_H */