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),
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) {
86 const bool can_use_bcast = use_bcast && (socket_type_ ==
SOCKET_RAW);
88 reconnect_ctl_ = makeReconnectCtl();
89 auto sopen = openSocketsWithRetry(reconnect_ctl_, family, port, can_use_bcast,
false);
100 const bool use_bcast) {
103 const bool can_use_bcast = use_bcast && (socket_type_ ==
SOCKET_RAW);
105 reconnect_ctl_->resetRetries();
107 openSocketsWithRetry(reconnect_ctl_, family, port, can_use_bcast,
true);
111CfgIface::openSocketsForFamily(
const uint16_t family,
const uint16_t port,
112 const bool can_use_bcast,
const bool skip_opened) {
113 bool no_errors =
true;
117 auto error_callback = [&no_errors](
const std::string& errmsg) {
118 socketOpenErrorHandler(errmsg);
123 if (family == AF_INET) {
125 error_callback, skip_opened);
132 return (std::make_pair(sopen, no_errors));
137 std::string timer_name =
"SocketReopenTimer";
139 auto on_fail_action = OnFailAction::SERVE_RETRY_CONTINUE;
141 on_fail_action = OnFailAction::SERVE_RETRY_EXIT;
145 auto reconnect_ctl = boost::make_shared<ReconnectCtl>(
"Socket", timer_name,
150 return (reconnect_ctl);
155 const uint16_t family,
const uint16_t port,
156 const bool can_use_bcast,
bool skip_opened)
const {
157 MultiThreadingCriticalSection cs;
159 ReceiverCriticalSection rcs;
171 bool loopback_used =
false;
172 if ((family == AF_INET6) || (socket_type_ ==
SOCKET_UDP)) {
174 for (
auto const& iface_name : iface_set_) {
176 if (iface && iface->flag_loopback_) {
177 loopback_used =
true;
181 for (
auto const& unicast : address_map_) {
183 if (iface && iface->flag_loopback_) {
184 loopback_used =
true;
196 setState(family, !wildcard_used_, !loopback_used);
200 if (!wildcard_used_) {
201 for (
auto const& iface_name : iface_set_) {
210 "fail to open socket on interface '"
211 << iface_name <<
"' as this interface doesn't"
214 if (family == AF_INET) {
215 iface->inactive4_ =
false;
216 setIfaceAddrsState(family,
true, *iface);
219 iface->inactive6_ =
false;
226 for (
auto const& unicast : address_map_) {
230 "fail to open unicast socket on interface '"
231 << unicast.first <<
"' as this interface doesn't"
234 if (family == AF_INET6) {
235 iface->addUnicast(unicast.second);
236 iface->inactive6_ =
false;
239 iface->setActive(unicast.second,
true);
240 iface->inactive4_ =
false;
249 if (family == AF_INET && can_use_bcast &&
250 CfgIface::multipleAddressesPerInterfaceActive()) {
255 bool is_initial_call =
false;
257 is_initial_call = (reconnect_ctl->retriesLeft() == reconnect_ctl->maxRetries());
259 auto result_pair = CfgIface::openSocketsForFamily(family, port, can_use_bcast, !is_initial_call);
260 bool sopen = result_pair.first;
261 bool has_errors = !result_pair.second;
263 auto timer_name = reconnect_ctl->timerName();
271 if (has_errors && reconnect_ctl->retriesLeft() > 0) {
273 reconnect_ctl->checkRetries();
277 std::bind(&CfgIface::openSocketsWithRetry,
279 reconnect_ctl, family,
280 port, can_use_bcast,
false),
281 reconnect_ctl->retryInterval(),
303 wildcard_used_ =
false;
305 address_map_.clear();
310CfgIface::setState(
const uint16_t family,
const bool inactive,
311 const bool loopback_inactive)
const {
313 bool iface_inactive = iface->flag_loopback_ ? loopback_inactive : inactive;
314 if (family == AF_INET) {
315 iface->inactive4_ = iface_inactive;
317 iface->inactive6_ = iface_inactive;
321 setIfaceAddrsState(family, !inactive, *iface);
326CfgIface::setIfaceAddrsState(
const uint16_t family,
const bool active,
327 Iface& iface)
const {
329 for (
auto const& addr : iface.getAddresses()) {
330 if (addr.get().getFamily() == family) {
331 iface.setActive(addr.get(), active);
337CfgIface::socketOpenErrorHandler(
const std::string& errmsg) {
343 switch (socket_type_) {
359 if (socket_type_name ==
"udp") {
362 }
else if (socket_type_name ==
"raw") {
367 << socket_type_name <<
"'");
373 return (outbound_iface_);
378 switch (outbound_iface_) {
380 return (
"same-as-inbound");
382 return (
"use-routing");
391 if (txt ==
"same-as-inbound") {
394 }
else if (txt ==
"use-routing") {
405 outbound_iface_ = outbound_iface;
416 size_t pos = iface_name.find(
"/");
418 std::string addr_str;
420 if (pos == std::string::npos) {
424 "empty interface name used in configuration");
429 <<
"' doesn't exist in the system");
432 }
else if (wildcard_used_) {
439 wildcard_used_ =
true;
453 "empty interface name specified in the"
454 " interface configuration");
458 if (addr_str.empty()) {
460 "empty address specified in the interface"
461 <<
" configuration");
469 <<
"' must not be used in conjunction with an"
478 <<
"' doesn't exist in the system");
487 if (family == AF_INET6) {
491 " a valid IPv6 unicast address");
498 .arg(addr.
toText()).arg(name);
504 " a valid IPv4 address");
509 if (!iface->hasAddress(addr)) {
511 "interface '" << name <<
"' doesn't have address '"
512 << addr <<
"' assigned");
518 if ((family == AF_INET) && (iface_set_.find(iface->getName()) != iface_set_.end())) {
520 <<
"' has already been selected");
524 std::pair<const std::string, IOAddress> iface_address_tuple(name, addr);
525 if (std::find(address_map_.begin(), address_map_.end(),
526 iface_address_tuple) != address_map_.end()) {
528 << addr <<
"' for interface '" << name <<
"' "
529 "because this address is already selected");
532 if (family == AF_INET6) {
534 .arg(addr.
toText()).arg(name);
538 .arg(addr.
toText()).arg(name);
540 address_map_.insert(std::pair<std::string, IOAddress>(name, addr));
548 if ((iface_set_.find(name) != iface_set_.end()) ||
549 ((family == AF_INET) && address_map_.count(name) > 0)) {
551 <<
"' has already been specified");
555 iface_set_.insert(name);
562 if (family != AF_INET) {
564 " the DHCPv6 server");
566 socket_type_ = socket_type;
573 const std::string& socket_type_name) {
586 if (wildcard_used_) {
589 for (
auto const& iface : iface_set_) {
592 for (
auto const& address : address_map_) {
593 std::string spec = address.first +
"/" + address.second.toText();
596 result->set(
"interfaces", ifaces);
612 if (service_socket_require_all_) {
613 result->set(
"service-sockets-require-all",
Element::create(service_socket_require_all_));
616 if (service_sockets_max_retries_ != 0) {
617 result->set(
"service-sockets-max-retries",
Element::create(
static_cast<int>(service_sockets_max_retries_)));
619 result->set(
"service-sockets-retry-wait-time",
Element::create(
static_cast<int>(service_sockets_retry_wait_time_)));
627 iface_set_ = other.iface_set_;
628 address_map_ = other.address_map_;
629 wildcard_used_ = other.wildcard_used_;
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())
Create a NullElement.
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.
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 update(CfgIface &other)
Update runtime mutable members.
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.
void triggerOpenSocketsWithRetry(const uint16_t family, const uint16_t port, const bool use_bcast=true)
Trigger the reopen sockets with retry mechanism.
Exception thrown when duplicated address specified.
Exception thrown when duplicated interface names specified.
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.
void initializeFDEventHandler()
Initialize the FD event handler;.
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.
Represents a single network interface.
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.
#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 &input)
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.