3 * Copyright 2005,2007,2008 Free Software Foundation, Inc.
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.
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.
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/>.
23 #include "eth_buffer.h"
25 #include "pktfilter.h"
26 #include <open_usrp2_socket.h>
32 //#include <features.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <net/ethernet.h>
39 #include <netinet/in.h>
41 #include <linux/types.h>
42 #include <netpacket/packet.h>
43 #include <linux/filter.h> // packet filter
48 open_packet_socket (std::string ifname, int protocol)
52 protocol = htons(ETH_P_ALL);
54 int fd = socket (PF_PACKET, SOCK_RAW, protocol);
56 int fd = usrp2::open_usrp2_socket();
60 fprintf (stderr, "%s: socket: %s\n", ifname.c_str(), strerror (errno));
64 // get interface index
66 memset (&ifr, 0, sizeof(ifr));
67 strncpy (ifr.ifr_name, ifname.c_str(), sizeof (ifr.ifr_name));
68 int res = ioctl (fd, SIOCGIFINDEX, &ifr);
71 fprintf (stderr, "%s: SIOCGIFINDEX: %s\n", ifname.c_str(), strerror(errno));
74 int ifindex = ifr.ifr_ifindex;
76 // bind to the specified interface
78 memset (&sa, 0, sizeof (sa));
79 sa.sll_family = AF_PACKET;
80 sa.sll_protocol = protocol;
81 sa.sll_ifindex = ifindex;
82 res = bind (fd, (struct sockaddr *)&sa, sizeof (sa));
85 fprintf (stderr, "%s: bind: %s\n", ifname.c_str(), strerror(errno));
92 extract_mac_addr (unsigned char *mac, const unsigned char *hwaddr)
95 for (i = 0; i < 6; i++)
99 for (int j = 0; j < 14; j++){
100 if (hwaddr[j] != 0xff){
101 mac[i++] = hwaddr[j];
109 get_mac_addr (std::string ifname, int fd, unsigned char *mac)
112 memset (&ifr, 0, sizeof(ifr));
113 strncpy (ifr.ifr_name, ifname.c_str(), sizeof (ifr.ifr_name));
114 int res = ioctl (fd, SIOCGIFHWADDR, &ifr);
116 fprintf (stderr, "%s: SIOCGIFHWADDR: %s\n", ifname.c_str(), strerror(errno));
121 for (unsigned i = 0; i < sizeof (ifr.ifr_hwaddr.sa_data); i++)
122 fprintf (stderr, "%02x", ifr.ifr_hwaddr.sa_data[i]);
123 fprintf (stderr, "\n");
126 extract_mac_addr (mac, (unsigned char *)ifr.ifr_hwaddr.sa_data);
130 ethernet::ethernet ()
133 memset (d_mac, 0, sizeof (d_mac));
136 ethernet::~ethernet ()
142 ethernet::open (std::string ifname, int protocol)
145 fprintf (stderr, "ethernet: already open\n");
148 if ((d_fd = open_packet_socket (ifname, protocol)) == -1){
151 get_mac_addr (ifname, d_fd, d_mac);
166 ethernet::read_packet (void *buf, int buflen)
168 int len = recvfrom (d_fd, buf, buflen, 0, (sockaddr *) 0, 0);
173 ethernet::read_packet_dont_block (void *buf, int buflen)
175 int len = recvfrom (d_fd, buf, buflen, MSG_DONTWAIT, 0, 0);
176 if (len == -1 && errno == EAGAIN)
183 ethernet::write_packet (const void *buf, int buflen)
185 int retval = send (d_fd, buf, buflen, 0);
188 return write_packet (buf, buflen);
190 perror ("ethernet:write_packet: send");
197 ethernet::write_packetv(const eth_iovec *iov, size_t iovlen)
200 memset(&mh, 0, sizeof(mh));
201 mh.msg_iov = const_cast<eth_iovec*>(iov);
202 mh.msg_iovlen = iovlen;
204 int retval = sendmsg(d_fd, &mh, 0);
207 return write_packetv(iov, iovlen);
209 perror("ethernet:write_packetv: send");
216 ethernet::attach_pktfilter (pktfilter *pf)
218 struct sock_fprog filter;
219 filter.len = pf->d_len;
220 filter.filter = pf->d_inst;
222 int r = setsockopt (d_fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof (filter));
224 perror ("ethernet:attach: SO_ATTACH_FILTER");