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
25public:
26 DuplicateIfaceName(const char* file, size_t line, const char* what) :
27 isc::Exception(file, line, what) { }
28};
29
32public:
33 InvalidIfaceName(const char* file, size_t line, const char* what) :
34 isc::Exception(file, line, what) { }
35};
36
38class NoSuchIface : public Exception {
39public:
40 NoSuchIface(const char* file, size_t line, const char* what) :
41 isc::Exception(file, line, what) { }
42};
43
46public:
47 DuplicateAddress(const char* file, size_t line, const char* what) :
48 isc::Exception(file, line, what) { }
49};
50
53class NoSuchAddress : public Exception {
54public:
55 NoSuchAddress(const char* file, size_t line, const char* what) :
56 isc::Exception(file, line, what) { }
57};
58
62public:
63 InvalidSocketType(const char* file, size_t line, const char* what) :
64 isc::Exception(file, line, what) { }
65};
66
132public:
133
141
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
250 return (socket_type_);
251 }
252
254 std::string socketTypeToText() const;
255
259 void setOutboundIface(const OutboundIface& outbound_iface);
260
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
325 return (service_socket_require_all_);
326 }
327
331 void setServiceSocketsRetryWaitTime(uint32_t interval) {
332 service_sockets_retry_wait_time_ = interval;
333 }
334
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
353 return (service_sockets_max_retries_);
354 }
355
360 return (reconnect_ctl_);
361 }
362
366
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
522typedef boost::shared_ptr<CfgIface> CfgIfacePtr;
523
525typedef boost::shared_ptr<const CfgIface> ConstCfgIfacePtr;
526
527}
528}
529
530#endif // CFG_IFACE_H
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static const char * ALL_IFACES_KEYWORD
Keyword used to enable all interfaces.
Definition cfg_iface.h:155
void closeSockets() const
Convenience function which closes all open sockets.
Definition cfg_iface.cc:38
std::string socketTypeToText() const
Returns the socket type in the textual format.
Definition cfg_iface.cc:342
void update(CfgIface &other)
Update runtime mutable members.
Definition cfg_iface.cc:626
void setReDetect(bool re_detect)
Set the re-detect flag.
Definition cfg_iface.h:310
void setServiceSocketsRetryWaitTime(uint32_t interval)
Set the socket service binding retry interval between attempts.
Definition cfg_iface.h:331
void reset()
Puts the interface configuration into default state.
Definition cfg_iface.cc:302
uint32_t getServiceSocketsRetryWaitTime() const
Indicates the socket service binding retry interval between attempts.
Definition cfg_iface.h:338
OutboundIface
Indicates how outbound interface is selected for relayed traffic.
Definition cfg_iface.h:143
@ USE_ROUTING
Server uses routing to determine the right interface to send response.
Definition cfg_iface.h:148
@ SAME_AS_INBOUND
Server sends responses over the same interface on which queries are received.
Definition cfg_iface.h:146
util::ReconnectCtlPtr getReconnectCtl() const
Get the reconnect controller.
Definition cfg_iface.h:359
OutboundIface getOutboundIface() const
Returns outbound interface selection mode.
Definition cfg_iface.cc:372
CfgIface()
Constructor.
Definition cfg_iface.cc:30
SocketType getSocketType() const
Returns DHCP socket type used by the server.
Definition cfg_iface.h:249
bool getServiceSocketsRequireAll() const
Indicates that Kea must successfully bind all socket services on init.
Definition cfg_iface.h:324
bool operator!=(const CfgIface &other) const
Inequality operator.
Definition cfg_iface.h:298
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
static OutboundIface textToOutboundIface(const std::string &txt)
Converts text to outbound interface selection mode.
Definition cfg_iface.cc:390
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
bool equals(const CfgIface &other) const
Compares two CfgIface objects for equality.
Definition cfg_iface.cc:43
SocketType
Socket type used by the DHCPv4 server.
Definition cfg_iface.h:135
@ SOCKET_UDP
Datagram socket, i.e. IP/UDP socket.
Definition cfg_iface.h:139
@ SOCKET_RAW
Raw socket, used for direct DHCPv4 traffic.
Definition cfg_iface.h:137
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition cfg_iface.cc:578
void setServiceSocketsRequireAll(bool require_all)
Set flag that Kea must successfully bind all socket services on init.
Definition cfg_iface.h:317
std::string outboundTypeToText() const
Returns outbound interface selection mode as string.
Definition cfg_iface.cc:377
void setOutboundIface(const OutboundIface &outbound_iface)
Sets outbound interface selection mode.
Definition cfg_iface.cc:404
void setServiceSocketsMaxRetries(uint32_t max_retries)
Set a maximum number of service sockets bind attempts.
Definition cfg_iface.h:345
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
bool operator==(const CfgIface &other) const
Equality operator.
Definition cfg_iface.h:289
static OpenSocketsFailedCallback open_sockets_failed_callback_
Optional callback function to invoke if all retries of the opening sockets fail.
Definition cfg_iface.h:369
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
std::function< void(util::ReconnectCtlPtr)> OpenSocketsFailedCallback
Represents a callback invoked if all retries of the opening sockets fail.
Definition cfg_iface.h:365
uint32_t getServiceSocketsMaxRetries() const
Indicates the maximum number of service sockets bind attempts.
Definition cfg_iface.h:352
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
DuplicateAddress(const char *file, size_t line, const char *what)
Definition cfg_iface.h:47
DuplicateIfaceName(const char *file, size_t line, const char *what)
Definition cfg_iface.h:26
Represents a single network interface.
Definition iface_mgr.h:136
InvalidIfaceName(const char *file, size_t line, const char *what)
Definition cfg_iface.h:33
InvalidSocketType(const char *file, size_t line, const char *what)
Definition cfg_iface.h:63
NoSuchAddress(const char *file, size_t line, const char *what)
Definition cfg_iface.h:55
NoSuchIface(const char *file, size_t line, const char *what)
Definition cfg_iface.h:40
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
boost::shared_ptr< const CfgIface > ConstCfgIfacePtr
A pointer to the const CfgIface.
Definition cfg_iface.h:525
boost::shared_ptr< CfgIface > CfgIfacePtr
A pointer to the CfgIface .
Definition cfg_iface.h:522
boost::shared_ptr< ReconnectCtl > ReconnectCtlPtr
Pointer to an instance of ReconnectCtl.
Defines the logger used by the top-level component of kea-lfc.
Abstract class for configuration Cfg_* classes.
Base class for user context.