Commit 0119b50f authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[2765] Applied patch for DHCPv4 silently failing when DHCP port in use.

parent e03adbe6
......@@ -103,9 +103,29 @@ namespace isc {
namespace dhcp {
int
PktFilterLPF::openSocket(const Iface& iface, const isc::asiolink::IOAddress&,
PktFilterLPF::openSocket(const Iface& iface,
const isc::asiolink::IOAddress& addr,
const uint16_t port, const bool,
const bool) {
// Let's check if a socket is already in use
int sock_check = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_check < 0) {
isc_throw(SocketConfigError, "Failed to create dgram socket");
}
struct sockaddr_in addr4;
memset(& addr4, 0, sizeof(addr4));
addr4.sin_family = AF_INET;
addr4.sin_addr.s_addr = htonl(addr);
addr4.sin_port = htons(port);
if (bind(sock_check, (struct sockaddr *)& addr4, sizeof(addr4)) < 0) {
// We return negative, the proper error message will be displayed
// by the IfaceMgr ...
close(sock_check);
return (-1);
}
close(sock_check);
int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sock < 0) {
......
......@@ -986,6 +986,40 @@ TEST_F(IfaceMgrTest, setMatchingPacketFilter) {
EXPECT_TRUE(iface_mgr->isDirectResponseSupported());
}
TEST_F(IfaceMgrTest, checkPacketFilterLPFSocket) {
IOAddress loAddr("127.0.0.1");
int socket1 = 0, socket2 = 0;
// Create two instances of IfaceMgr.
boost::scoped_ptr<NakedIfaceMgr> iface_mgr1(new NakedIfaceMgr());
ASSERT_TRUE(iface_mgr1);
boost::scoped_ptr<NakedIfaceMgr> iface_mgr2(new NakedIfaceMgr());
ASSERT_TRUE(iface_mgr2);
// Let IfaceMgr figure out which Packet Filter to use when
// direct response capability is not desired. It should pick
// PktFilterInet.
EXPECT_NO_THROW(iface_mgr1->setMatchingPacketFilter(false));
// Let's open a loopback socket with handy unpriviliged port number
socket1 = iface_mgr1->openSocket(LOOPBACK, loAddr,
DHCP4_SERVER_PORT + 10000);
EXPECT_GE(socket1, 0);
// Then the second use PkFilterLPF mode
EXPECT_NO_THROW(iface_mgr2->setMatchingPacketFilter(true));
// This socket opening attempt should not return positive value
// The first socket already opened same port
EXPECT_NO_THROW(
socket2 = iface_mgr2->openSocket(LOOPBACK, loAddr,
DHCP4_SERVER_PORT + 10000);
);
EXPECT_LE(socket2, 0);
close(socket2);
close(socket1);
}
#else
// This non-Linux specific test checks whether it is possible to use
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment