Skip to content
Snippets Groups Projects
Commit ddd780e6 authored by tho's avatar tho
Browse files

Socket/Protocols/Raw/PacketSocketHandle: added promisc() method

parent 01c477ee
No related branches found
No related tags found
No related merge requests found
...@@ -76,6 +76,7 @@ namespace { ...@@ -76,6 +76,7 @@ namespace {
void do_mc(int fd, std::string const & interface, senf::MACAddress address, bool add) void do_mc(int fd, std::string const & interface, senf::MACAddress address, bool add)
{ {
struct packet_mreq mreq; struct packet_mreq mreq;
::memset(&mreq, 0, sizeof(mreq));
mreq.mr_ifindex = ::if_nametoindex(interface.c_str()); mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
if (mreq.mr_ifindex == 0) if (mreq.mr_ifindex == 0)
throw senf::SystemException(EINVAL); throw senf::SystemException(EINVAL);
...@@ -83,25 +84,39 @@ namespace { ...@@ -83,25 +84,39 @@ namespace {
mreq.mr_alen = 6; mreq.mr_alen = 6;
std::copy(address.begin(), address.end(), &mreq.mr_address[0]); std::copy(address.begin(), address.end(), &mreq.mr_address[0]);
if (::setsockopt(fd, SOL_PACKET, if (::setsockopt(fd, SOL_PACKET,
add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP, add ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
&mreq, sizeof(mreq)) < 0) &mreq, sizeof(mreq)) < 0)
throw senf::SystemException(); throw senf::SystemException();
} }
} }
prefix_ void senf::PacketSocketProtocol::mcAdd(std::string const & interface, prefix_ void senf::PacketSocketProtocol::mcAdd(std::string const & interface,
MACAddress const & address) MACAddress const & address)
const const
{ {
do_mc(fd(),interface,address,true); do_mc(fd(), interface, address, true);
} }
prefix_ void senf::PacketSocketProtocol::mcDrop(std::string const & interface, prefix_ void senf::PacketSocketProtocol::mcDrop(std::string const & interface,
MACAddress const & address) MACAddress const & address)
const const
{ {
do_mc(fd(),interface,address,false); do_mc(fd(), interface, address, false);
}
prefix_ void senf::PacketSocketProtocol::promisc(std::string const & interface, bool mode)
const
{
struct packet_mreq mreq;
::memset(&mreq, 0, sizeof(mreq));
mreq.mr_ifindex = ::if_nametoindex(interface.c_str());
if (mreq.mr_ifindex == 0)
throw senf::SystemException(EINVAL);
mreq.mr_type = PACKET_MR_PROMISC;
if (::setsockopt(fd(), SOL_PACKET,
mode ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP,
&mreq, sizeof(mreq)) < 0)
throw senf::SystemException();
} }
///////////////////////////////cc.e//////////////////////////////////////// ///////////////////////////////cc.e////////////////////////////////////////
......
...@@ -122,6 +122,9 @@ namespace senf { ...@@ -122,6 +122,9 @@ namespace senf {
///< Disable reception of a multicast group ///< Disable reception of a multicast group
/**< \see \ref mcAdd() */ /**< \see \ref mcAdd() */
void promisc(std::string const & interface, bool mode) const;
///< enable/disable promiscuous mode
///@} ///@}
///\name Abstract Interface Implementation ///\name Abstract Interface Implementation
......
...@@ -56,10 +56,13 @@ BOOST_AUTO_UNIT_TEST(packetSocketHandle) ...@@ -56,10 +56,13 @@ BOOST_AUTO_UNIT_TEST(packetSocketHandle)
// How am I supposed to test read and write .. grmpf .. // How am I supposed to test read and write .. grmpf ..
SENF_CHECK_NO_THROW( sock.protocol().mcAdd( SENF_CHECK_NO_THROW( sock.protocol().mcAdd(
"eth0",senf::MACAddress::from_string("01-02-03-04-05-06")) ); "eth0", senf::MACAddress::from_string("01-02-03-04-05-06")) );
SENF_CHECK_NO_THROW( sock.protocol().mcDrop( SENF_CHECK_NO_THROW( sock.protocol().mcDrop(
"eth0",senf::MACAddress::from_string("01-02-03-04-05-06")) ); "eth0", senf::MACAddress::from_string("01-02-03-04-05-06")) );
SENF_CHECK_NO_THROW( sock.protocol().promisc( "eth0", true) );
SENF_CHECK_NO_THROW( sock.protocol().promisc( "eth0", false));
SENF_CHECK_NO_THROW( sock.protocol().available() ); SENF_CHECK_NO_THROW( sock.protocol().available() );
BOOST_CHECK( ! sock.eof() ); BOOST_CHECK( ! sock.eof() );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment