Commit e7a2eaf0 authored by Marcin Siodelski's avatar Marcin Siodelski
Browse files

[3252] Use error handler macro for IfaceMgr::openSocket4.

parent 58da9f45
......@@ -396,7 +396,6 @@ IfaceMgr::openSockets4(const uint16_t port, const bool use_bcast,
continue;
}
int sock = -1;
// If selected interface is broadcast capable set appropriate
// options on the socket so as it can receive and send broadcast
// messages.
......@@ -406,22 +405,24 @@ IfaceMgr::openSockets4(const uint16_t port, const bool use_bcast,
// bind to INADDR_ANY address but we can do it only once. Thus,
// if one socket has been bound we can't do it any further.
if (!bind_to_device && bcast_num > 0) {
handleSocketConfigError("SO_BINDTODEVICE socket option is"
" not supported on this OS;"
" therefore, DHCP server can only"
" listen broadcast traffic on a"
" single interface",
error_handler);
ifacemgr_error(SocketConfigError, error_handler,
"SO_BINDTODEVICE socket option is"
" not supported on this OS;"
" therefore, DHCP server can only"
" listen broadcast traffic on a"
" single interface");
continue;
} else {
try {
// We haven't open any broadcast sockets yet, so we can
// open at least one more.
sock = openSocket(iface->getName(), *addr, port,
true, true);
openSocket(iface->getName(), *addr, port, true, true);
} catch (const Exception& ex) {
handleSocketConfigError(ex.what(), error_handler);
ifacemgr_error(SocketConfigError, error_handler,
"failed to open socket on interface "
<< iface->getName() << ", reason: "
<< ex.what());
continue;
}
......@@ -436,22 +437,17 @@ IfaceMgr::openSockets4(const uint16_t port, const bool use_bcast,
} else {
try {
// Not broadcast capable, do not set broadcast flags.
sock = openSocket(iface->getName(), *addr, port,
false, false);
openSocket(iface->getName(), *addr, port, false, false);
} catch (const Exception& ex) {
handleSocketConfigError(ex.what(), error_handler);
ifacemgr_error(SocketConfigError, error_handler,
"failed to open socket on interface "
<< iface->getName() << ", reason: "
<< ex.what());
continue;
}
}
if (sock < 0) {
const char* errstr = strerror(errno);
handleSocketConfigError(std::string("failed to open IPv4 socket,"
" reason:") + errstr,
error_handler);
} else {
++count;
}
++count;
}
}
......@@ -551,7 +547,7 @@ IfaceMgr::openSockets6(const uint16_t port,
iface->delSocket(sock);
ifacemgr_error(SocketConfigError, error_handler,
"Failed to open multicast socket on"
" interface " << iface->getFullName()
" interface " << iface->getName()
<< ", reason: " << ex.what());
continue;
}
......@@ -562,20 +558,6 @@ IfaceMgr::openSockets6(const uint16_t port,
return (count > 0);
}
void
IfaceMgr::handleSocketConfigError(const std::string& errmsg,
IfaceMgrErrorMsgCallback handler) {
// If error handler is installed, we don't want to throw an exception, but
// rather call this handler.
if (handler != NULL) {
handler(errmsg);
} else {
isc_throw(SocketConfigError, errmsg);
}
}
void
IfaceMgr::printIfaces(std::ostream& out /*= std::cout*/) {
for (IfaceCollection::const_iterator iface=ifaces_.begin();
......
......@@ -955,23 +955,6 @@ private:
getLocalAddress(const isc::asiolink::IOAddress& remote_addr,
const uint16_t port);
/// @brief Handles an error which occurs during configuration of a socket.
///
/// If the handler callback is specified (non-NULL), this handler is
/// called and the specified error message is passed to it. If the
/// handler is not specified, the @c isc::dhcpSocketConfigError exception
/// is thrown with the specified message.
///
/// This function should be called to handle errors which occur during
/// socket opening, binding or configuration (e.g. setting socket options
/// etc).
///
/// @param errmsg An error message to be passed to a handlder function or
/// to the @c isc::dhcp::SocketConfigError exception.
/// @param handler An error handler function or NULL.
void handleSocketConfigError(const std::string& errmsg,
IfaceMgrErrorMsgCallback handler);
/// @brief Checks if there is at least one socket of the specified family
/// open.
///
......
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