Kea: cfg_iface.h Source File

| Kea 3.2.0-git

cfg_iface.h
Go to the documentation of this file.
1// Copyright (C) 2014-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef CFG_IFACE_H
8#define CFG_IFACE_H
9
10#include <asiolink/io_address.h>
11#include <dhcp/iface_mgr.h>
12#include <util/reconnect_ctl.h>
13#include <cc/cfg_to_element.h>
14#include <cc/user_context.h>
15#include <boost/shared_ptr.hpp>
16#include <map>
17#include <set>
18#include <string>
19
20namespace isc {
21namespace dhcp {
22
24 class DuplicateIfaceName : public Exception {
25public:
26 DuplicateIfaceName(const char* file, size_t line, const char* what) :
27 isc::Exception(file, line, what) { }
28};
29
31 class InvalidIfaceName : public Exception {
32public:
33 InvalidIfaceName(const char* file, size_t line, const char* what) :
34 isc::Exception(file, line, what) { }
35};
36
38 class NoSuchIface : public Exception {
39public:
40 NoSuchIface(const char* file, size_t line, const char* what) :
41 isc::Exception(file, line, what) { }
42};
43
45 class DuplicateAddress : public Exception {
46public:
47 DuplicateAddress(const char* file, size_t line, const char* what) :
48 isc::Exception(file, line, what) { }
49};
50
53 class NoSuchAddress : public Exception {
54public:
55 NoSuchAddress(const char* file, size_t line, const char* what) :
56 isc::Exception(file, line, what) { }
57};
58
61 class InvalidSocketType : public Exception {
62public:
63 InvalidSocketType(const char* file, size_t line, const char* what) :
64 isc::Exception(file, line, what) { }
65};
66
131 class CfgIface : public isc::data::UserContext, public isc::data::CfgToElement {
132public:
133
135 enum SocketType {
137 SOCKET_RAW,
139 SOCKET_UDP
140 };
141
143 enum OutboundIface {
146 SAME_AS_INBOUND,
148 USE_ROUTING
149 };
150
155 static const char* ALL_IFACES_KEYWORD;
156
158 CfgIface();
159
162 void closeSockets() const;
163
169 bool equals(const CfgIface& other) const;
170
184 void openSockets(const uint16_t family, const uint16_t port,
185 const bool use_bcast = true);
186
190 void reset();
191
208 void use(const uint16_t family, const std::string& iface_name);
209
216 void update(CfgIface& other);
217
230 void useSocketType(const uint16_t family, const SocketType& socket_type);
231
245 void useSocketType(const uint16_t family,
246 const std::string& socket_type_name);
247
249 SocketType getSocketType() const {
250 return (socket_type_);
251 }
252
254 std::string socketTypeToText() const;
255
259 void setOutboundIface(const OutboundIface& outbound_iface);
260
264 OutboundIface getOutboundIface() const;
265
269 std::string outboundTypeToText() const;
270
275 static OutboundIface textToOutboundIface(const std::string& txt);
276
282 SocketType textToSocketType(const std::string& socket_type_name) const;
283
289 bool operator==(const CfgIface& other) const {
290 return (equals(other));
291 }
292
298 bool operator!=(const CfgIface& other) const {
299 return (!equals(other));
300 }
301
305 virtual isc::data::ElementPtr toElement() const;
306
310 void setReDetect(bool re_detect) {
311 re_detect_ = re_detect;
312 }
313
317 void setServiceSocketsRequireAll(bool require_all) {
318 service_socket_require_all_ = require_all;
319 }
320
324 bool getServiceSocketsRequireAll() const {
325 return (service_socket_require_all_);
326 }
327
331 void setServiceSocketsRetryWaitTime(uint32_t interval) {
332 service_sockets_retry_wait_time_ = interval;
333 }
334
338 uint32_t getServiceSocketsRetryWaitTime() const {
339 return (service_sockets_retry_wait_time_);
340 }
341
345 void setServiceSocketsMaxRetries(uint32_t max_retries) {
346 service_sockets_max_retries_ = max_retries;
347 }
348
352 uint32_t getServiceSocketsMaxRetries() const {
353 return (service_sockets_max_retries_);
354 }
355
359 util::ReconnectCtlPtr getReconnectCtl() const {
360 return (reconnect_ctl_);
361 }
362
365 typedef std::function<void(util::ReconnectCtlPtr)> OpenSocketsFailedCallback;
366
369 static OpenSocketsFailedCallback open_sockets_failed_callback_;
370
379 void triggerOpenSocketsWithRetry(const uint16_t family, const uint16_t port,
380 const bool use_bcast = true);
381
382private:
383
396 static bool multipleAddressesPerInterfaceActive();
397
409 void setState(const uint16_t family, const bool inactive,
410 const bool loopback_inactive) const;
411
422 void setIfaceAddrsState(const uint16_t family, const bool active,
423 Iface& iface) const;
424
433 static void socketOpenErrorHandler(const std::string& errmsg);
434
451 static std::pair<bool, bool> openSocketsForFamily(const uint16_t family,
452 const uint16_t port,
453 const bool can_use_bcast,
454 const bool skip_opened);
455
461 util::ReconnectCtlPtr makeReconnectCtl() const;
462
477 bool openSocketsWithRetry(util::ReconnectCtlPtr reconnect_ctl,
478 const uint16_t family, const uint16_t port,
479 const bool can_use_bcast, bool skip_opened) const;
480
482 typedef std::set<std::string> IfaceSet;
483
485 IfaceSet iface_set_;
486
489 typedef std::multimap<std::string, asiolink::IOAddress> ExplicitAddressMap;
490
493 ExplicitAddressMap address_map_;
494
497 bool wildcard_used_;
498
500 SocketType socket_type_;
501
503 bool re_detect_;
504
506 bool service_socket_require_all_;
507
509 uint32_t service_sockets_retry_wait_time_;
510
512 uint32_t service_sockets_max_retries_;
513
515 OutboundIface outbound_iface_;
516
518 util::ReconnectCtlPtr reconnect_ctl_;
519};
520
522 typedef boost::shared_ptr<CfgIface> CfgIfacePtr;
523
525 typedef boost::shared_ptr<const CfgIface> ConstCfgIfacePtr;
526
527}
528}
529
530#endif // CFG_IFACE_H
cfg_to_element.h
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::dhcp::CfgIface::ALL_IFACES_KEYWORD
static const char * ALL_IFACES_KEYWORD
Keyword used to enable all interfaces.
Definition cfg_iface.h:155
isc::dhcp::CfgIface::closeSockets
void closeSockets() const
Convenience function which closes all open sockets.
Definition cfg_iface.cc:38
isc::dhcp::CfgIface::socketTypeToText
std::string socketTypeToText() const
Returns the socket type in the textual format.
Definition cfg_iface.cc:342
isc::dhcp::CfgIface::update
void update(CfgIface &other)
Update runtime mutable members.
Definition cfg_iface.cc:626
isc::dhcp::CfgIface::setReDetect
void setReDetect(bool re_detect)
Set the re-detect flag.
Definition cfg_iface.h:310
isc::dhcp::CfgIface::setServiceSocketsRetryWaitTime
void setServiceSocketsRetryWaitTime(uint32_t interval)
Set the socket service binding retry interval between attempts.
Definition cfg_iface.h:331
isc::dhcp::CfgIface::reset
void reset()
Puts the interface configuration into default state.
Definition cfg_iface.cc:302
isc::dhcp::CfgIface::getServiceSocketsRetryWaitTime
uint32_t getServiceSocketsRetryWaitTime() const
Indicates the socket service binding retry interval between attempts.
Definition cfg_iface.h:338
isc::dhcp::CfgIface::OutboundIface
OutboundIface
Indicates how outbound interface is selected for relayed traffic.
Definition cfg_iface.h:143
isc::dhcp::CfgIface::USE_ROUTING
@ USE_ROUTING
Server uses routing to determine the right interface to send response.
Definition cfg_iface.h:148
isc::dhcp::CfgIface::SAME_AS_INBOUND
@ SAME_AS_INBOUND
Server sends responses over the same interface on which queries are received.
Definition cfg_iface.h:146
isc::dhcp::CfgIface::getReconnectCtl
util::ReconnectCtlPtr getReconnectCtl() const
Get the reconnect controller.
Definition cfg_iface.h:359
isc::dhcp::CfgIface::getOutboundIface
OutboundIface getOutboundIface() const
Returns outbound interface selection mode.
Definition cfg_iface.cc:372
isc::dhcp::CfgIface::CfgIface
CfgIface()
Constructor.
Definition cfg_iface.cc:30
isc::dhcp::CfgIface::getSocketType
SocketType getSocketType() const
Returns DHCP socket type used by the server.
Definition cfg_iface.h:249
isc::dhcp::CfgIface::getServiceSocketsRequireAll
bool getServiceSocketsRequireAll() const
Indicates that Kea must successfully bind all socket services on init.
Definition cfg_iface.h:324
isc::dhcp::CfgIface::operator!=
bool operator!=(const CfgIface &other) const
Inequality operator.
Definition cfg_iface.h:298
isc::dhcp::CfgIface::openSockets
void openSockets(const uint16_t family, const uint16_t port, const bool use_bcast=true)
Tries to open sockets on selected interfaces.
Definition cfg_iface.cc:61
isc::dhcp::CfgIface::textToOutboundIface
static OutboundIface textToOutboundIface(const std::string &txt)
Converts text to outbound interface selection mode.
Definition cfg_iface.cc:390
isc::dhcp::CfgIface::use
void use(const uint16_t family, const std::string &iface_name)
Select interface to be used to receive DHCP traffic.
Definition cfg_iface.cc:409
isc::dhcp::CfgIface::equals
bool equals(const CfgIface &other) const
Compares two CfgIface objects for equality.
Definition cfg_iface.cc:43
isc::dhcp::CfgIface::SocketType
SocketType
Socket type used by the DHCPv4 server.
Definition cfg_iface.h:135
isc::dhcp::CfgIface::SOCKET_UDP
@ SOCKET_UDP
Datagram socket, i.e. IP/UDP socket.
Definition cfg_iface.h:139
isc::dhcp::CfgIface::SOCKET_RAW
@ SOCKET_RAW
Raw socket, used for direct DHCPv4 traffic.
Definition cfg_iface.h:137
isc::dhcp::CfgIface::toElement
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition cfg_iface.cc:578
isc::dhcp::CfgIface::setServiceSocketsRequireAll
void setServiceSocketsRequireAll(bool require_all)
Set flag that Kea must successfully bind all socket services on init.
Definition cfg_iface.h:317
isc::dhcp::CfgIface::outboundTypeToText
std::string outboundTypeToText() const
Returns outbound interface selection mode as string.
Definition cfg_iface.cc:377
isc::dhcp::CfgIface::setOutboundIface
void setOutboundIface(const OutboundIface &outbound_iface)
Sets outbound interface selection mode.
Definition cfg_iface.cc:404
isc::dhcp::CfgIface::setServiceSocketsMaxRetries
void setServiceSocketsMaxRetries(uint32_t max_retries)
Set a maximum number of service sockets bind attempts.
Definition cfg_iface.h:345
isc::dhcp::CfgIface::textToSocketType
SocketType textToSocketType(const std::string &socket_type_name) const
Converts the socket type in the textual format to the type represented by the SocketType.
Definition cfg_iface.cc:358
isc::dhcp::CfgIface::operator==
bool operator==(const CfgIface &other) const
Equality operator.
Definition cfg_iface.h:289
isc::dhcp::CfgIface::open_sockets_failed_callback_
static OpenSocketsFailedCallback open_sockets_failed_callback_
Optional callback function to invoke if all retries of the opening sockets fail.
Definition cfg_iface.h:369
isc::dhcp::CfgIface::useSocketType
void useSocketType(const uint16_t family, const SocketType &socket_type)
Sets the specified socket type to be used by the server.
Definition cfg_iface.cc:560
isc::dhcp::CfgIface::OpenSocketsFailedCallback
std::function< void(util::ReconnectCtlPtr)> OpenSocketsFailedCallback
Represents a callback invoked if all retries of the opening sockets fail.
Definition cfg_iface.h:365
isc::dhcp::CfgIface::getServiceSocketsMaxRetries
uint32_t getServiceSocketsMaxRetries() const
Indicates the maximum number of service sockets bind attempts.
Definition cfg_iface.h:352
isc::dhcp::CfgIface::triggerOpenSocketsWithRetry
void triggerOpenSocketsWithRetry(const uint16_t family, const uint16_t port, const bool use_bcast=true)
Trigger the reopen sockets with retry mechanism.
Definition cfg_iface.cc:99
isc::dhcp::DuplicateAddress::DuplicateAddress
DuplicateAddress(const char *file, size_t line, const char *what)
Definition cfg_iface.h:47
isc::dhcp::DuplicateIfaceName::DuplicateIfaceName
DuplicateIfaceName(const char *file, size_t line, const char *what)
Definition cfg_iface.h:26
isc::dhcp::Iface
Represents a single network interface.
Definition iface_mgr.h:136
isc::dhcp::InvalidIfaceName::InvalidIfaceName
InvalidIfaceName(const char *file, size_t line, const char *what)
Definition cfg_iface.h:33
isc::dhcp::InvalidSocketType::InvalidSocketType
InvalidSocketType(const char *file, size_t line, const char *what)
Definition cfg_iface.h:63
isc::dhcp::NoSuchAddress::NoSuchAddress
NoSuchAddress(const char *file, size_t line, const char *what)
Definition cfg_iface.h:55
isc::dhcp::NoSuchIface::NoSuchIface
NoSuchIface(const char *file, size_t line, const char *what)
Definition cfg_iface.h:40
iface_mgr.h
io_address.h
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::dhcp
Definition dhcp4/client_handler.cc:20
isc::dhcp::ConstCfgIfacePtr
boost::shared_ptr< const CfgIface > ConstCfgIfacePtr
A pointer to the const CfgIface.
Definition cfg_iface.h:525
isc::dhcp::CfgIfacePtr
boost::shared_ptr< CfgIface > CfgIfacePtr
A pointer to the CfgIface .
Definition cfg_iface.h:522
isc::util::file
Definition filesystem.cc:29
isc::util::ReconnectCtlPtr
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Definition reconnect_ctl.h:153
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
reconnect_ctl.h
isc::data::CfgToElement
Abstract class for configuration Cfg_* classes.
Definition cfg_to_element.h:29
isc::data::UserContext
Base class for user context.
Definition user_context.h:22
user_context.h