21namespace ph = std::placeholders;
31 : wildcard_used_(false), socket_type_(SOCKET_RAW), re_detect_(false),
32 service_socket_require_all_(false), service_sockets_retry_wait_time_(5000),
33 service_sockets_max_retries_(0),
34 outbound_iface_(SAME_AS_INBOUND) {
44 return (iface_set_ == other.iface_set_ &&
45 address_map_ == other.address_map_ &&
46 wildcard_used_ == other.wildcard_used_ &&
47 socket_type_ == other.socket_type_);
51CfgIface::multipleAddressesPerInterfaceActive() {
53 if (iface->countActive4() > 1) {
62 const bool use_bcast) {
69 bool loopback_used_ =
false;
70 if ((family == AF_INET6) || (socket_type_ ==
SOCKET_UDP)) {
72 for (IfaceSet::const_iterator iface_name = iface_set_.begin();
73 iface_name != iface_set_.end(); ++iface_name) {
75 if (iface && iface->flag_loopback_) {
76 loopback_used_ =
true;
80 for (ExplicitAddressMap::const_iterator unicast = address_map_.begin();
81 unicast != address_map_.end(); ++unicast) {
83 if (iface && iface->flag_loopback_) {
84 loopback_used_ =
true;
93 setState(family, !wildcard_used_, !loopback_used_);
114 if (!wildcard_used_) {
115 for (IfaceSet::const_iterator iface_name = iface_set_.begin();
116 iface_name != iface_set_.end(); ++iface_name) {
124 "fail to open socket on interface '"
125 << *iface_name <<
"' as this interface doesn't"
128 }
else if (family == AF_INET) {
129 iface->inactive4_ =
false;
130 setIfaceAddrsState(family,
true, *iface);
133 iface->inactive6_ =
false;
140 for (ExplicitAddressMap::const_iterator unicast = address_map_.begin();
141 unicast != address_map_.end(); ++unicast) {
145 "fail to open unicast socket on interface '"
146 << unicast->first <<
"' as this interface doesn't"
149 if (family == AF_INET6) {
150 iface->addUnicast(unicast->second);
151 iface->inactive6_ =
false;
154 iface->setActive(unicast->second,
true);
155 iface->inactive4_ =
false;
161 const bool can_use_bcast = use_bcast && (socket_type_ ==
SOCKET_RAW);
168 if (family == AF_INET && can_use_bcast && multipleAddressesPerInterfaceActive()) {
172 reconnect_ctl_ = makeReconnectCtl();
173 auto sopen = openSocketsWithRetry(reconnect_ctl_, family, port, can_use_bcast);
183CfgIface::openSocketsForFamily(
const uint16_t family,
const uint16_t port,
184 const bool can_use_bcast,
const bool skip_opened) {
185 bool no_errors =
true;
189 auto error_callback = [&no_errors](
const std::string& errmsg) {
190 socketOpenErrorHandler(errmsg);
197 if (family == AF_INET) {
199 error_callback, skip_opened);
206 return (std::make_pair(sopen, no_errors));
211 std::string timer_name =
"SocketReopenTimer";
213 auto on_fail_action = OnFailAction::SERVE_RETRY_CONTINUE;
215 on_fail_action = OnFailAction::SERVE_RETRY_EXIT;
219 auto reconnect_ctl = boost::make_shared<ReconnectCtl>(
"Socket", timer_name,
224 return (reconnect_ctl);
229 const uint16_t family,
const uint16_t port,
230 const bool can_use_bcast) {
234 bool is_initial_call = (reconnect_ctl->retriesLeft() == reconnect_ctl->maxRetries());
235 auto result_pair = openSocketsForFamily(family, port, can_use_bcast, !is_initial_call);
236 bool sopen = result_pair.first;
237 bool has_errors = !result_pair.second;
239 auto timer_name = reconnect_ctl->timerName();
247 if (has_errors && reconnect_ctl->retriesLeft() > 0) {
249 reconnect_ctl->checkRetries();
253 std::bind(&CfgIface::openSocketsWithRetry,
254 reconnect_ctl, family,
255 port, can_use_bcast),
256 reconnect_ctl->retryInterval(),
278 wildcard_used_ =
false;
280 address_map_.clear();
285CfgIface::setState(
const uint16_t family,
const bool inactive,
286 const bool loopback_inactive)
const {
288 bool iface_inactive = iface->flag_loopback_ ? loopback_inactive : inactive;
289 if (family == AF_INET) {
290 iface->inactive4_ = iface_inactive;
292 iface->inactive6_ = iface_inactive;
296 setIfaceAddrsState(family, !inactive, *iface);
301CfgIface::setIfaceAddrsState(
const uint16_t family,
const bool active,
302 Iface& iface)
const {
305 if (addr.get().getFamily() == family) {
306 iface.setActive(addr.get(), active);
312CfgIface::socketOpenErrorHandler(
const std::string& errmsg) {
318 switch (socket_type_) {
334 if (socket_type_name ==
"udp") {
337 }
else if (socket_type_name ==
"raw") {
342 << socket_type_name <<
"'");
348 return (outbound_iface_);
353 switch (outbound_iface_) {
355 return (
"same-as-inbound");
357 return (
"use-routing");
366 if (txt ==
"same-as-inbound") {
369 }
else if (txt ==
"use-routing") {
380 outbound_iface_ = outbound_iface;
391 size_t pos = iface_name.find(
"/");
393 std::string addr_str;
395 if (pos == std::string::npos) {
399 "empty interface name used in configuration");
404 <<
"' doesn't exist in the system");
407 }
else if (wildcard_used_) {
414 wildcard_used_ =
true;
428 "empty interface name specified in the"
429 " interface configuration");
433 if (addr_str.empty()) {
435 "empty address specified in the interface"
436 <<
" configuration");
444 <<
"' must not be used in conjunction with an"
453 <<
"' doesn't exist in the system");
462 if (family == AF_INET6) {
466 " a valid IPv6 unicast address");
473 .arg(addr.
toText()).arg(name);
479 " a valid IPv4 address");
484 if (!iface->hasAddress(addr)) {
486 "interface '" << name <<
"' doesn't have address '"
487 << addr <<
"' assigned");
493 if ((family == AF_INET) && (iface_set_.find(iface->getName()) != iface_set_.end())) {
495 <<
"' has already been selected");
499 std::pair<const std::string, IOAddress> iface_address_tuple(name, addr);
500 if (std::find(address_map_.begin(), address_map_.end(),
501 iface_address_tuple) != address_map_.end()) {
503 << addr <<
"' for interface '" << name <<
"' "
504 "because this address is already selected");
507 if (family == AF_INET6) {
509 .arg(addr.
toText()).arg(name);
513 .arg(addr.
toText()).arg(name);
515 address_map_.insert(std::pair<std::string, IOAddress>(name, addr));
523 if ((iface_set_.find(name) != iface_set_.end()) ||
524 ((family == AF_INET) && address_map_.count(name) > 0)) {
526 <<
"' has already been specified");
531 if (addr_str.empty()) {
534 iface_set_.insert(name);
541 if (family != AF_INET) {
543 " the DHCPv6 server");
545 socket_type_ = socket_type;
552 const std::string& socket_type_name) {
565 if (wildcard_used_) {
568 for (IfaceSet::const_iterator iface = iface_set_.cbegin();
569 iface != iface_set_.cend(); ++iface) {
572 for (ExplicitAddressMap::const_iterator address = address_map_.cbegin();
573 address != address_map_.cend(); ++address) {
574 std::string spec = address->first +
"/" + address->second.toText();
577 result->set(
"interfaces", ifaces);
593 if (service_socket_require_all_) {
594 result->set(
"service-sockets-require-all",
Element::create(service_socket_require_all_));
597 if (service_sockets_max_retries_ != 0) {
598 result->set(
"service-sockets-max-retries",
Element::create(
static_cast<int>(service_sockets_max_retries_)));
600 result->set(
"service-sockets-retry-wait-time",
Element::create(
static_cast<int>(service_sockets_retry_wait_time_)));
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when an unexpected error condition occurs.
The IOAddress class represents an IP addresses (version agnostic)
std::string toText() const
Convert the address to a string.
bool isV6() const
Convenience function to check for an IPv6 address.
bool isV4() const
Convenience function to check for an IPv4 address.
bool isV6Multicast() const
checks whether and address is IPv6 and is multicast
bool isV6LinkLocal() const
checks whether and address is IPv6 and is link-local
static ElementPtr create(const Position &pos=ZERO_POSITION())
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Represents selection of interfaces for DHCP server.
static const char * ALL_IFACES_KEYWORD
Keyword used to enable all interfaces.
void closeSockets() const
Convenience function which closes all open sockets.
std::string socketTypeToText() const
Returns the socket type in the textual format.
void reset()
Puts the interface configuration into default state.
uint32_t getServiceSocketsRetryWaitTime() const
Indicates the socket service binding retry interval between attempts.
OutboundIface
Indicates how outbound interface is selected for relayed traffic.
@ USE_ROUTING
Server uses routing to determine the right interface to send response.
@ SAME_AS_INBOUND
Server sends responses over the same interface on which queries are received.
OutboundIface getOutboundIface() const
Returns outbound interface selection mode.
bool getServiceSocketsRequireAll() const
Indicates that Kea must successfully bind all socket services on init.
void openSockets(const uint16_t family, const uint16_t port, const bool use_bcast=true)
Tries to open sockets on selected interfaces.
static OutboundIface textToOutboundIface(const std::string &txt)
Converts text to outbound interface selection mode.
void use(const uint16_t family, const std::string &iface_name)
Select interface to be used to receive DHCP traffic.
bool equals(const CfgIface &other) const
Compares two CfgIface objects for equality.
SocketType
Socket type used by the DHCPv4 server.
@ SOCKET_UDP
Datagram socket, i.e. IP/UDP socket.
@ SOCKET_RAW
Raw socket, used for direct DHCPv4 traffic.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
std::string outboundTypeToText() const
Returns outbound interface selection mode as string.
void setOutboundIface(const OutboundIface &outbound_iface)
Sets outbound interface selection mode.
SocketType textToSocketType(const std::string &socket_type_name) const
Converts the socket type in the textual format to the type represented by the SocketType.
static OpenSocketsFailedCallback open_sockets_failed_callback_
Optional callback function to invoke if all retries of the opening sockets fail.
void useSocketType(const uint16_t family, const SocketType &socket_type)
Sets the specified socket type to be used by the server.
std::function< void(util::ReconnectCtlPtr)> OpenSocketsFailedCallback
Represents a callback invoked if all retries of the opening sockets fail.
uint32_t getServiceSocketsMaxRetries() const
Indicates the maximum number of service sockets bind attempts.
Exception thrown when duplicated address specified.
Exception thrown when duplicated interface names specified.
Handles network interfaces, transmission and reception.
bool openSockets4(const uint16_t port=DHCP4_SERVER_PORT, const bool use_bcast=true, IfaceMgrErrorMsgCallback error_handler=0, const bool skip_opened=false)
Opens IPv4 sockets on detected interfaces.
IfacePtr getIface(const unsigned int ifindex)
Returns interface specified interface index.
bool openSockets6(const uint16_t port=DHCP6_SERVER_PORT, IfaceMgrErrorMsgCallback error_handler=0, const bool skip_opened=false)
Opens IPv6 sockets on detected interfaces.
void detectIfaces(bool update_only=false)
Detects network interfaces.
void clearUnicasts()
Clears unicast addresses on all interfaces.
static IfaceMgr & instance()
IfaceMgr is a singleton class.
bool isDirectResponseSupported() const
Check if packet be sent directly to the client having no address.
void closeSockets()
Closes all open sockets.
void setMatchingPacketFilter(const bool direct_response_desired=false)
Set Packet Filter object to handle send/receive packets.
void setAllowLoopBack(const bool allow_loopback)
Allows or disallows the loopback interface.
util::Optional< asiolink::IOAddress > Address
Address type.
Exception thrown when specified interface name is invalid.
Exception thrown when invalid socket type has been specified for the given family.
Exception thrown when specified unicast address is not assigned to the interface specified.
Exception thrown when specified interface doesn't exist in a system.
static const TimerMgrPtr & instance()
Returns pointer to the sole instance of the TimerMgr.
RAII class creating a critical section.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
boost::shared_ptr< Element > ElementPtr
isc::log::Logger dhcpsrv_logger("dhcpsrv")
DHCP server library Logger.
const isc::log::MessageID DHCPSRV_CFGMGR_ADD_IFACE
boost::shared_ptr< Iface > IfacePtr
Type definition for the pointer to an Iface object.
const isc::log::MessageID DHCPSRV_CFGMGR_USE_ADDRESS
const isc::log::MessageID DHCPSRV_MULTIPLE_RAW_SOCKETS_PER_IFACE
const isc::log::MessageID DHCPSRV_CFGMGR_SOCKET_TYPE_SELECT
const isc::log::MessageID DHCPSRV_CFGMGR_ALL_IFACES_ACTIVE
const isc::log::MessageID DHCPSRV_CFGMGR_UNICAST_LINK_LOCAL
const isc::log::MessageID DHCPSRV_NO_SOCKETS_OPEN
const isc::log::MessageID DHCPSRV_OPEN_SOCKET_FAIL
const int DHCPSRV_DBG_TRACE
DHCP server library logging levels.
const isc::log::MessageID DHCPSRV_CFGMGR_SOCKET_RAW_UNSUPPORTED
const isc::log::MessageID DHCPSRV_CFGMGR_USE_UNICAST
string trim(const string &instring)
Trim Leading and Trailing Spaces.
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-lfc.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.