21 #include <sys/types.h> 22 #include <sys/socket.h> 23 #include <linux/if_packet.h> 25 #include <net/ethernet.h> 26 #include <netinet/in.h> 34 void do_mc(
int fd, std::string
const & interface,
senf::MACAddress address,
bool add)
36 struct ::packet_mreq mreq;
37 ::memset(&mreq, 0,
sizeof(mreq));
38 mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
39 if (mreq.mr_ifindex == 0)
41 mreq.mr_type = PACKET_MR_MULTICAST;
43 std::copy(address.begin(), address.end(), &mreq.mr_address[0]);
44 if (::setsockopt(fd, SOL_PACKET,
45 add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
46 &mreq,
sizeof(mreq)) < 0)
55 do_mc(
fd(), interface, address,
true);
62 do_mc(
fd(), interface, address,
false);
68 struct packet_mreq mreq;
69 ::memset(&mreq, 0,
sizeof(mreq));
70 mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
71 if (mreq.mr_ifindex == 0)
73 mreq.mr_type = PACKET_MR_PROMISC;
74 if (::setsockopt(
fd(), SOL_PACKET,
75 mode ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
76 &mreq,
sizeof(mreq)) < 0)
83 struct ::tpacket_stats stats;
84 ::socklen_t len =
sizeof(stats);
85 if (::getsockopt(
fd(), SOL_PACKET, PACKET_STATISTICS,
86 reinterpret_cast<char *>(&stats), &len) < 0)
88 return stats.tp_drops;
95 #ifndef PACKET_STATISTICS_TX 96 # define PACKET_STATISTICS_TX 31 98 unsigned wrongFormat (0);
99 ::socklen_t len =
sizeof(wrongFormat);
101 reinterpret_cast<char *>(&wrongFormat), &len) < 0)
115 int socktype = SOCK_RAW;
117 socktype = SOCK_DGRAM;
119 protocol = ETH_P_ALL;
120 int sock = ::socket(PF_PACKET, socktype | SOCK_CLOEXEC, htons(protocol));
129 if (!
fh().readable())
131 ssize_t l = ::recv(
fd(),0,0,MSG_PEEK | MSG_TRUNC);
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
unsigned txWrongFormat() const
packets dropped by kernel due to wrong format (too large) since last call
unsigned rxQueueDropped() const
packets dropped by kernel since last call
int fd() const
Get file descriptor.
unsigned available() const
Return (maximum) number of bytes available for reading without < blocking.
void mcDrop(std::string const &interface, MACAddress const &address) const
Disable reception of a multicast group.
void init_packetSocket(SocketType type, int protocol) const
FileHandle fh() const
Get a FileHandle for this instance.
LinuxPacketSocketProtocol public header.
void promisc(std::string const &interface, bool mode) const
enable/disable promiscuous mode
void mcAdd(std::string const &interface, MACAddress const &address) const
Enable reception of a multicast group.
#define PACKET_STATISTICS_TX
bool eof() const
Check for end-of-file condition.