Kea 3.3.3
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 <cc/cfg_to_element.h>
12#include <cc/user_context.h>
13#include <dhcp/iface_mgr.h>
14#include <util/reconnect_ctl.h>
15
16#include <cstdint>
17#include <map>
18#include <set>
19#include <string>
20
21#include <boost/shared_ptr.hpp>
22
23namespace isc {
24namespace dhcp {
25
28public:
29 DuplicateIfaceName(const char* file, size_t line, const char* what) :
30 isc::Exception(file, line, what) { }
31};
32
35public:
36 InvalidIfaceName(const char* file, size_t line, const char* what) :
37 isc::Exception(file, line, what) { }
38};
39
41class NoSuchIface : public Exception {
42public:
43 NoSuchIface(const char* file, size_t line, const char* what) :
44 isc::Exception(file, line, what) { }
45};
46
49public:
50 DuplicateAddress(const char* file, size_t line, const char* what) :
51 isc::Exception(file, line, what) { }
52};
53
56class NoSuchAddress : public Exception {
57public:
58 NoSuchAddress(const char* file, size_t line, const char* what) :
59 isc::Exception(file, line, what) { }
60};
61
65public:
66 InvalidSocketType(const char* file, size_t line, const char* what) :
67 isc::Exception(file, line, what) { }
68};
69
135public:
136
138 enum SocketType : uint16_t {
143 };
144
153
158 static const char* ALL_IFACES_KEYWORD;
159
161 CfgIface();
162
165 void closeSockets() const;
166
172 bool equals(const CfgIface& other) const;
173
187 void openSockets(const uint16_t family, const uint16_t port,
188 const bool use_bcast = true);
189
193 void reset();
194
211 void use(const uint16_t family, const std::string& iface_name);
212
219 void update(CfgIface& other);
220
233 void useSocketType(const uint16_t family, const SocketType& socket_type);
234
248 void useSocketType(const uint16_t family,
249 const std::string& socket_type_name);
250
253 return (socket_type_);
254 }
255
257 std::string socketTypeToText() const;
258
262 void setOutboundIface(const OutboundIface& outbound_iface);
263
268
272 std::string outboundTypeToText() const;
273
278 static OutboundIface textToOutboundIface(const std::string& txt);
279
285 SocketType textToSocketType(const std::string& socket_type_name) const;
286
292 bool operator==(const CfgIface& other) const {
293 return (equals(other));
294 }
295
301 bool operator!=(const CfgIface& other) const {
302 return (!equals(other));
303 }
304
308 virtual isc::data::ElementPtr toElement() const;
309
313 void setReDetect(bool re_detect) {
314 re_detect_ = re_detect;
315 }
316
320 void setServiceSocketsRequireAll(bool require_all) {
321 service_socket_require_all_ = require_all;
322 }
323
328 return (service_socket_require_all_);
329 }
330
334 void setServiceSocketsRetryWaitTime(uint32_t interval) {
335 service_sockets_retry_wait_time_ = interval;
336 }
337
342 return (service_sockets_retry_wait_time_);
343 }
344
348 void setServiceSocketsMaxRetries(uint32_t max_retries) {
349 service_sockets_max_retries_ = max_retries;
350 }
351
356 return (service_sockets_max_retries_);
357 }
358
363 return (reconnect_ctl_);
364 }
365
369
373
382 void triggerOpenSocketsWithRetry(const uint16_t family, const uint16_t port,
383 const bool use_bcast = true);
384
385private:
386
399 static bool multipleAddressesPerInterfaceActive();
400
412 void setState(const uint16_t family, const bool inactive,
413 const bool loopback_inactive) const;
414
425 void setIfaceAddrsState(const uint16_t family, const bool active,
426 Iface& iface) const;
427
436 static void socketOpenErrorHandler(const std::string& errmsg);
437
454 static std::pair<bool, bool> openSocketsForFamily(const uint16_t family,
455 const uint16_t port,
456 const bool can_use_bcast,
457 const bool skip_opened);
458
464 util::ReconnectCtlPtr makeReconnectCtl() const;
465
480 bool openSocketsWithRetry(util::ReconnectCtlPtr reconnect_ctl,
481 const uint16_t family, const uint16_t port,
482 const bool can_use_bcast, bool skip_opened) const;
483
485 typedef std::set<std::string> IfaceSet;
486
488 IfaceSet iface_set_;
489
492 typedef std::multimap<std::string, asiolink::IOAddress> ExplicitAddressMap;
493
496 ExplicitAddressMap address_map_;
497
500 bool wildcard_used_;
501
503 SocketType socket_type_;
504
506 bool re_detect_;
507
509 bool service_socket_require_all_;
510
512 uint32_t service_sockets_retry_wait_time_;
513
515 uint32_t service_sockets_max_retries_;
516
518 OutboundIface outbound_iface_;
519
521 util::ReconnectCtlPtr reconnect_ctl_;
522};
523
525typedef boost::shared_ptr<CfgIface> CfgIfacePtr;
526
528typedef boost::shared_ptr<const CfgIface> ConstCfgIfacePtr;
529
530}
531}
532
533#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:158
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:313
void setServiceSocketsRetryWaitTime(uint32_t interval)
Set the socket service binding retry interval between attempts.
Definition cfg_iface.h:334
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:341
util::ReconnectCtlPtr getReconnectCtl() const
Get the reconnect controller.
Definition cfg_iface.h:362
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:252
SocketType
Socket type used by the DHCPv4 server.
Definition cfg_iface.h:138
@ SOCKET_UDP
Datagram socket, i.e. IP/UDP socket.
Definition cfg_iface.h:142
@ SOCKET_RAW
Raw socket, used for direct DHCPv4 traffic.
Definition cfg_iface.h:140
bool getServiceSocketsRequireAll() const
Indicates that Kea must successfully bind all socket services on init.
Definition cfg_iface.h:327
bool operator!=(const CfgIface &other) const
Inequality operator.
Definition cfg_iface.h:301
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
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:320
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:348
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
OutboundIface
Indicates how outbound interface is selected for relayed traffic.
Definition cfg_iface.h:146
@ USE_ROUTING
Server uses routing to determine the right interface to send response.
Definition cfg_iface.h:151
@ SAME_AS_INBOUND
Server sends responses over the same interface on which queries are received.
Definition cfg_iface.h:149
bool operator==(const CfgIface &other) const
Equality operator.
Definition cfg_iface.h:292
static OpenSocketsFailedCallback open_sockets_failed_callback_
Optional callback function to invoke if all retries of the opening sockets fail.
Definition cfg_iface.h:372
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:368
uint32_t getServiceSocketsMaxRetries() const
Indicates the maximum number of service sockets bind attempts.
Definition cfg_iface.h:355
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:50
DuplicateIfaceName(const char *file, size_t line, const char *what)
Definition cfg_iface.h:29
Represents a single network interface.
Definition iface_mgr.h:136
InvalidIfaceName(const char *file, size_t line, const char *what)
Definition cfg_iface.h:36
InvalidSocketType(const char *file, size_t line, const char *what)
Definition cfg_iface.h:66
NoSuchAddress(const char *file, size_t line, const char *what)
Definition cfg_iface.h:58
NoSuchIface(const char *file, size_t line, const char *what)
Definition cfg_iface.h:43
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:528
boost::shared_ptr< CfgIface > CfgIfacePtr
A pointer to the CfgIface .
Definition cfg_iface.h:525
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.