1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
|
// Copyright (C) 2017-2019 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef NETWORK_H
#define NETWORK_H
#include <asiolink/io_address.h>
#include <cc/cfg_to_element.h>
#include <cc/data.h>
#include <cc/element_value.h>
#include <cc/stamped_element.h>
#include <cc/user_context.h>
#include <dhcp/classify.h>
#include <dhcp/option.h>
#include <dhcpsrv/cfg_option.h>
#include <dhcpsrv/cfg_4o6.h>
#include <dhcpsrv/triplet.h>
#include <util/optional.h>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <cstdint>
#include <functional>
#include <string>
namespace isc {
namespace data {
/// @brief The @c ElementValue specialization for IOAddress.
template<>
class ElementValue<asiolink::IOAddress> {
public:
/// @brief Function operator extracting an @c Element value as
/// string.
///
/// @param el Element holding a value to be extracted.
asiolink::IOAddress operator()(ConstElementPtr el) const {
return (asiolink::IOAddress(el->stringValue()));
}
};
} // end of namespace isc::data
} // end of namespace isc
namespace isc {
namespace dhcp {
/// List of IOAddresses
typedef std::vector<isc::asiolink::IOAddress> IOAddressList;
class Network;
/// @brief Pointer to the @ref Network object.
typedef boost::shared_ptr<Network> NetworkPtr;
/// @brief Weak pointer to the @ref Network object.
typedef boost::weak_ptr<Network> WeakNetworkPtr;
/// @brief Callback function for @c Network that retrieves globally
/// configured parameters.
typedef std::function<data::ConstElementPtr()> FetchNetworkGlobalsFn;
/// @brief Common interface representing a network to which the DHCP clients
/// are connected.
///
/// The most common type of network, in Kea's terminology, is a subnet. The
/// @ref Subnet derives from this class. Another types of objects implementing
/// this interface are @ref SharedNetwork4 and @ref SharedNetwork6 objects.
/// They group multiple subnets together to provide means for
/// extending available address pools (a single client may obtain IP
/// address from any of the pools belonging to subnets in the shared
/// network), or for selecting a subnet on a given link, depending on the
/// class of the client (e.g. cable network case: different subnet is
/// selected for cable modems, different one for routers).
///
/// The subnets and shared networks share many data structures, e.g. DHCP
/// options, local interface name, address manipulation methods. Both subnets
/// and shared networks derive from this class to provide the common
/// functionality.
///
/// The DHCP server configuration is complex because many parameters may
/// be specified at different levels of hierarchy. The lower level values,
/// e.g. subnet specific values, take precedence over upper level values,
/// e.g. shared network specific ones. For historical reasons, the DHCP
/// servers expect that the appropriate values are inherited from the
/// upper configuration levels to the lower configuration levels upon
/// the reconfiguration. For example: if a user didn't specify
/// valid-lifetime for a subnet, calling @c Subnet4::getValid() should
/// result in returning a global value of valid-lifetime. In the early
/// Kea days it was achieved by the configuration parsers which would
/// explicitly assign the global valid lifetime to the @c Subnet4
/// instances for which the subnet specific value was not provided. This
/// approach has a major benefit that it is fast. However, it makes
/// the subnets tightly dependent on the global values (and shared
/// network specific values). Modification of the global value must
/// result in modification of this value in all subnets for which
/// there is no explicit value provided. This issue became a serious
/// road block during the implementation of the Configuration Backend.
///
/// The @c Network object has been modified to address the problem of
/// inheritance of global, shared network specific and subnet specific
/// parameters in a generic way, at the same time minimizing the need to
/// change the existing server logic.
///
/// The @c Network object now holds the pointer to the "parent" @c Network
/// object. Thus subnets which belong to a shared network will have
/// that shared network as its parent. Stand-alone subnets, will have
/// no parent.
///
/// The general idea is that the accessor functions of the network
/// will first check if the accessed value is specified or not (that
/// is handled by @c util::Optional object). If the value is specified
/// it is returned. Otherwise, the object will check if there is a
/// parent object it belongs to and will call the appropriate method
/// of that object. If the value is present it is returned. Otherwise
/// the global value is returned.
///
/// Accessing global values from the @c Network object is troublesome.
/// There is no uniform way to access those values. For example, the
/// given network may be in a staging or current configuration and
/// it really has no means to know in which of the two it belongs.
/// In fact, an attempt to pass the pointer to the @c SrvConfig object
/// would cause a circular dependency between the @c Network and the
/// @c SrvConfig. Even if it was possible and the @c Network had
/// access to the specific @c SrvConfig instance, it doesn't handle
/// the cases when the @c SrvConfig instance was modified.
///
/// To deal with the problem of accessing the global parameters in a
/// flexible manner, we elected to use an optional callback function
/// which can be associated with the @c Network object. This callback
/// implements the logic to retrieve global parameters and return them
/// in a well known form, so as the @c Network accessors can use them.
class Network : public virtual isc::data::StampedElement,
public virtual isc::data::UserContext,
public isc::data::CfgToElement {
public:
/// @brief Holds optional information about relay.
///
/// In some cases it is beneficial to have additional information about
/// a relay configured in the subnet. For now, the structure holds only
/// IP addresses, but there may potentially be additional parameters added
/// later, e.g. relay interface-id or relay-id.
class RelayInfo {
public:
/// @brief Adds an address to the list of addresses
///
/// @param addr address to add
/// @throw BadValue if the address is already in the list
void addAddress(const asiolink::IOAddress& addr);
/// @brief Returns const reference to the list of addresses
///
/// @return const reference to the list of addresses
const IOAddressList& getAddresses() const;
/// @brief Indicates whether or not the address list has entries
///
/// @return True if the address list is not empty
bool hasAddresses() const;
/// @brief Checks the address list for the given address
///
/// @return True if the address is found in the address list
bool containsAddress(const asiolink::IOAddress& addr) const;
private:
/// @brief List of relay IP addresses
IOAddressList addresses_;
};
/// @brief Specifies allowed host reservation mode.
///
typedef enum {
/// None - host reservation is disabled. No reservation types
/// are allowed.
HR_DISABLED,
/// Only out-of-pool reservations is allowed. This mode
/// allows AllocEngine to skip reservation checks when
/// dealing with with addresses that are in pool.
HR_OUT_OF_POOL,
/// Only global reservations are allowed. This mode
/// instructs AllocEngine to only look at global reservations.
HR_GLOBAL,
/// Both out-of-pool and in-pool reservations are allowed. This is the
/// most flexible mode, where sysadmin have biggest liberty. However,
/// there is a non-trivial performance penalty for it, as the
/// AllocEngine code has to check whether there are reservations, even
/// when dealing with reservations from within the dynamic pools.
/// @todo - should ALL include global?
HR_ALL
} HRMode;
/// @brief Inheritance "mode" used when fetching an optional @c Network
/// parameter.
///
/// The following modes are currently supported:
/// - NONE: no inheritance is used, the network specific value is returned
/// regardless if it is specified or not.
/// - PARENT_NETWORK: parent network specific value is returned or unspecified
/// if the parent network doesn't exist.
/// - GLOBAL: global specific value is returned.
/// - ALL: inheritance is used on all levels: network specific value takes
/// precedence over parent specific value over the global value.
enum class Inheritance {
NONE,
PARENT_NETWORK,
GLOBAL,
ALL
};
/// Pointer to the RelayInfo structure
typedef boost::shared_ptr<Network::RelayInfo> RelayInfoPtr;
/// @brief Constructor.
Network()
: iface_name_(), client_class_(), t1_(), t2_(), valid_(),
host_reservation_mode_(HR_ALL, true), cfg_option_(new CfgOption()),
calculate_tee_times_(), t1_percent_(), t2_percent_() {
}
/// @brief Virtual destructor.
///
/// Does nothing at the moment.
virtual ~Network() { };
/// @brief Sets the optional callback function used to fetch globally
/// configured parameters.
///
/// @param fetch_globals_fn Pointer to the function.
void setFetchGlobalsFn(FetchNetworkGlobalsFn fetch_globals_fn) {
fetch_globals_fn_ = fetch_globals_fn;
}
/// @brief Checks if the network is associated with a function used to
/// fetch globally configured parameters.
///
/// @return true if it is associated, false otherwise.
bool hasFetchGlobalsFn() const {
return (static_cast<bool>(fetch_globals_fn_));
}
/// @brief Sets local name of the interface for which this network is
/// selected.
///
/// If the interface is specified, the server will use the network
/// associated with this local interface to allocate IP addresses and
/// other resources to a client.
///
/// @param iface_name Interface name.
void setIface(const util::Optional<std::string>& iface_name) {
iface_name_ = iface_name;
}
/// @brief Returns name of the local interface for which this network is
/// selected.
///
/// @param inheritance inheritance mode to be used.
/// @return Interface name as text.
util::Optional<std::string>
getIface(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network>(&Network::getIface, iface_name_,
inheritance, "interface"));
};
/// @brief Sets information about relay
///
/// In some situations where there are shared subnets (i.e. two different
/// subnets are available on the same physical link), there is only one
/// relay that handles incoming requests from clients. In such a case,
/// the usual subnet selection criteria based on relay belonging to the
/// subnet being selected are no longer sufficient and we need to explicitly
/// specify a relay. One notable example of such uncommon, but valid
/// scenario is a cable network, where there is only one CMTS (one relay),
/// but there are 2 distinct subnets behind it: one for cable modems
/// and another one for CPEs and other user equipment behind modems.
/// From manageability perspective, it is essential that modems get addresses
/// from different subnet, so users won't tinker with their modems.
///
/// Setting this parameter is not needed in most deployments.
/// This structure holds IP address only for now, but it is expected to
/// be extended in the future.
///
/// @param relay structure that contains relay information
void setRelayInfo(const RelayInfo& relay) {
relay_ = relay;
}
/// @brief Returns const reference to relay information
///
/// @note The returned reference is only valid as long as the object
/// returned it is valid.
///
/// @return const reference to the relay information
const RelayInfo& getRelayInfo() const {
return (relay_);
}
/// @brief Adds an address to the list addresses in the network's relay info
///
/// @param addr address of the relay
/// @throw BadValue if the address is already in the list
void addRelayAddress(const asiolink::IOAddress& addr);
/// @brief Returns the list of relay addresses from the network's relay info
///
/// @return const reference to the list of addresses
const IOAddressList& getRelayAddresses() const;
/// @brief Indicates if network's relay info has relay addresses
///
/// @return True the relay list is not empty, false otherwise
bool hasRelays() const;
/// @brief Tests if the network's relay info contains the given address
///
/// @param address address to search for in the relay list
/// @return True if a relay with the given address is found, false otherwise
bool hasRelayAddress(const asiolink::IOAddress& address) const;
/// @brief Checks whether this network supports client that belongs to
/// specified classes.
///
/// This method checks whether a client that belongs to given classes can
/// use this network. For example, if this class is reserved for client
/// class "foo" and the client belongs to classes "foo", "bar" and "baz",
/// it is supported. On the other hand, client belonging to classes
/// "foobar" and "zyxxy" is not supported.
///
/// @note: changed the planned white and black lists idea to a simple
/// client class name.
///
/// @param client_classes list of all classes the client belongs to
/// @return true if client can be supported, false otherwise
virtual bool
clientSupported(const isc::dhcp::ClientClasses& client_classes) const;
/// @brief Sets the supported class to class class_name
///
/// @param class_name client class to be supported by this network
void allowClientClass(const isc::dhcp::ClientClass& class_name);
/// @brief Adds class class_name to classes required to be evaluated.
///
/// @param class_name client class required to be evaluated
void requireClientClass(const isc::dhcp::ClientClass& class_name);
/// @brief Returns classes which are required to be evaluated
const ClientClasses& getRequiredClasses() const;
/// @brief returns the client class
///
/// @note The returned reference is only valid as long as the object
/// returned it is valid.
///
/// @param inheritance inheritance mode to be used.
/// @return client class @ref client_class_
util::Optional<ClientClass>
getClientClass(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network>(&Network::getClientClass, client_class_,
inheritance));
}
/// @brief Return valid-lifetime for addresses in that prefix
///
/// @param inheritance inheritance mode to be used.
Triplet<uint32_t> getValid(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network>(&Network::getValid, valid_, inheritance,
"valid-lifetime"));
}
/// @brief Sets new valid lifetime for a network.
///
/// @param valid New valid lifetime in seconds.
void setValid(const Triplet<uint32_t>& valid) {
valid_ = valid;
}
/// @brief Returns T1 (renew timer), expressed in seconds
///
/// @param inheritance inheritance mode to be used.
Triplet<uint32_t> getT1(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network>(&Network::getT1, t1_, inheritance, "renew-timer"));
}
/// @brief Sets new renew timer for a network.
///
/// @param t1 New renew timer value in seconds.
void setT1(const Triplet<uint32_t>& t1) {
t1_ = t1;
}
/// @brief Returns T2 (rebind timer), expressed in seconds
///
/// @param inheritance inheritance mode to be used.
Triplet<uint32_t> getT2(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network>(&Network::getT2, t2_, inheritance, "rebind-timer"));
}
/// @brief Sets new rebind timer for a network.
///
/// @param t2 New rebind timer value in seconds.
void setT2(const Triplet<uint32_t>& t2) {
t2_ = t2;
}
/// @brief Specifies what type of Host Reservations are supported.
///
/// Host reservations may be either in-pool (they reserve an address that
/// is in the dynamic pool) or out-of-pool (they reserve an address that is
/// not in the dynamic pool). HR may also be completely disabled for
/// performance reasons.
///
/// @param inheritance inheritance mode to be used.
/// @return Host reservation mode enabled.
util::Optional<HRMode>
getHostReservationMode(const Inheritance& inheritance = Inheritance::ALL) const {
// Inheritance for host reservations is a little different than for other
// parameters. The reservation at the global level is given as a string.
// Thus we call getProperty here without a global name to check if the
// host reservation mode is specified on network level only.
const util::Optional<HRMode>& hr_mode = getProperty<Network>(&Network::getHostReservationMode,
host_reservation_mode_,
inheritance);
// If HR mode is not specified at network level we need this special
// case code to handle conversion of the globally configured HR
// mode to an enum.
if (hr_mode.unspecified() && (inheritance != Inheritance::NONE) &&
(inheritance != Inheritance::PARENT_NETWORK)) {
// Get global reservation mode.
util::Optional<std::string> hr_mode_name;
hr_mode_name = getGlobalProperty(hr_mode_name, "reservation-mode");
if (!hr_mode_name.unspecified()) {
try {
// If the HR mode is globally configured, let's convert it from
// a string to enum.
return (hrModeFromString(hr_mode_name.get()));
} catch (...) {
// This should not really happen because the configuration
// parser should have already verified the globally configured
// reservation mode. However, we want to be 100% sure that this
// method doesn't throw. Let's just return unspecified.
return (hr_mode);
}
}
}
return (hr_mode);
}
/// @brief Sets host reservation mode.
///
/// See @ref getHostReservationMode for details.
///
/// @param mode mode to be set
void setHostReservationMode(const util::Optional<HRMode>& mode) {
host_reservation_mode_ = mode;
}
/// @brief Attempts to convert text representation to HRMode enum.
///
/// Allowed values are "disabled", "off" (alias for disabled),
/// "out-of-pool" and "all". See @c Network::HRMode for their exact meaning.
///
/// @param hr_mode_name Host Reservation mode in the textual form.
///
/// @throw BadValue if the text cannot be converted.
///
/// @return one of allowed HRMode values
static HRMode hrModeFromString(const std::string& hr_mode_name);
/// @brief Returns pointer to the option data configuration for this network.
CfgOptionPtr getCfgOption() {
return (cfg_option_);
}
/// @brief Returns const pointer to the option data configuration for this
/// network.
ConstCfgOptionPtr getCfgOption() const {
return (cfg_option_);
}
/// @brief Returns whether or not T1/T2 calculation is enabled.
///
/// @param inheritance inheritance mode to be used.
util::Optional<bool>
getCalculateTeeTimes(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network>(&Network::getCalculateTeeTimes,
calculate_tee_times_,
inheritance,
"calculate-tee-times"));
}
/// @brief Sets whether or not T1/T2 calculation is enabled.
///
/// @param calculate_tee_times new value of enabled/disabled.
void setCalculateTeeTimes(const util::Optional<bool>& calculate_tee_times) {
calculate_tee_times_ = calculate_tee_times;
}
/// @brief Returns percentage to use when calculating the T1 (renew timer).
///
/// @param inheritance inheritance mode to be used.
util::Optional<double>
getT1Percent(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network>(&Network::getT1Percent, t1_percent_,
inheritance, "t1-percent"));
}
/// @brief Sets new precentage for calculating T1 (renew timer).
///
/// @param t1_percent New percentage to use.
void setT1Percent(const util::Optional<double>& t1_percent) {
t1_percent_ = t1_percent;
}
/// @brief Returns percentage to use when calculating the T2 (rebind timer).
///
/// @param inheritance inheritance mode to be used.
util::Optional<double>
getT2Percent(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network>(&Network::getT2Percent, t2_percent_,
inheritance, "t2-percent"));
}
/// @brief Sets new precentage for calculating T2 (rebind timer).
///
/// @param t2_percent New percentage to use.
void setT2Percent(const util::Optional<double>& t2_percent) {
t2_percent_ = t2_percent;
}
/// @brief Unparses network object.
///
/// @return A pointer to unparsed network configuration.
virtual data::ElementPtr toElement() const;
protected:
/// @brief Returns a value of global configuration parameter with
/// a given name.
///
/// If the @c ferch_globals_fn_ function is non-null, this method will
/// invoke this function to retrieve a global value having the given
/// name. Typically, this method is invoked by @c getProperty when
/// network specific value of the parameter is not found. In some cases
/// it may be called by other methods. One such example is the
/// @c getHostReservationMode which needs to call @c getGlobalProperty
/// explicitly to convert the global host reservation mode value from
/// a string to an enum.
///
/// @tparam ReturnType Type of the returned value, e.g.
/// @c Optional<std::string>.
///
/// @param property Value to be returned when it is specified or when
/// no global value is found.
/// @param global_name Name of the global parameter which value should
/// be returned
///
/// @return Optional value fetched from the global level or the value
/// of @c property.
template<typename ReturnType>
ReturnType getGlobalProperty(ReturnType property,
const std::string& global_name) const {
if (!global_name.empty() && fetch_globals_fn_) {
data::ConstElementPtr globals = fetch_globals_fn_();
if (globals && (globals->getType() == data::Element::map)) {
data::ConstElementPtr global_param = globals->get(global_name);
if (global_param) {
// If there is a global parameter, convert it to the
// optional value of the given type and return.
return (data::ElementValue<typename ReturnType::ValueType>()(global_param));
}
}
}
return (property);
}
/// @brief Returns a value associated with a network using inheritance.
///
/// This template method provides a generic mechanism to retrieve a
/// network parameter using inheritance. It is called from public
/// accessor methods which return an @c OptionalValue or @c Triplet.
///
/// @tparam BaseType Type of this instance, e.g. @c Network, @c Network4
/// etc, which exposes a method to be called.
/// @tparam ReturnType Type of the returned value, e.g.
/// @c Optional<std::string>.
///
/// @param MethodPointer Pointer to the method of the base class which
/// should be called on the parent network instance (typically on
/// @c SharedNetwork4 or @c SharedNetwork6) to fetch the parent specific
/// value if the value is unspecified for this instance.
/// @param property Value to be returned when it is specified or when
/// no explicit value is specified on upper inheritance levels.
/// @param inheritance inheritance mode to be used.
/// @param global_name Optional name of the global parameter which value
/// should be returned if the given parameter is not specified on network
/// level. This value is empty by default, which indicates that the
/// global value for the given parameter is not supported and shouldn't
/// be fetched.
///
/// @return Optional value fetched from this instance level, parent
/// network level or global level
template<typename BaseType, typename ReturnType>
ReturnType getProperty(ReturnType(BaseType::*MethodPointer)(const Inheritance&) const,
ReturnType property,
const Inheritance& inheritance,
const std::string& global_name = "") const {
// If no inheritance is to be used, return the value for this
// network regardless if it is specified or not.
if (inheritance == Inheritance::NONE) {
return (property);
} else if (inheritance == Inheritance::PARENT_NETWORK) {
ReturnType parent_property;
// Check if this instance has a parent network.
auto parent = boost::dynamic_pointer_cast<BaseType>(parent_network_.lock());
if (parent) {
parent_property = ((*parent).*MethodPointer)(Network::Inheritance::NONE);
}
return (parent_property);
// If global value requested, return it.
} else if (inheritance == Inheritance::GLOBAL) {
return (getGlobalProperty(ReturnType(), global_name));
}
// We use inheritance and the value is not specified on the network level.
// Hence, we need to get the parent network specific value or global value.
if (property.unspecified()) {
// Check if this instance has a parent network.
auto parent = boost::dynamic_pointer_cast<BaseType>(parent_network_.lock());
// If the parent network exists, let's fetch the parent specific
// value.
if (parent) {
// We're using inheritance so ask for the parent specific network
// and return it only if it is specified.
auto parent_property = ((*parent).*MethodPointer)(inheritance);
if (!parent_property.unspecified()) {
return (parent_property);
}
}
// The value is not specified on network level. If the value
// can be specified on global level and there is a callback
// that returns the global values, try to find this parameter
// at the global scope.
return (getGlobalProperty(property, global_name));
}
// We haven't found the value at any level, so return the unspecified.
return (property);
}
/// @brief Returns option pointer associated with a network using inheritance.
///
/// This template method provides a generic mechanism to retrieve a
/// network parameter using inheritance. It is called from public
/// accessor methods which return an @c OptionPtr.
///
/// @tparam BaseType Type of this instance, e.g. @c Network, @c Network4
/// etc, which exposes a method to be called.
///
/// @param MethodPointer Pointer to the method of the base class which
/// should be called on the parent network instance (typically on
/// @c SharedNetwork4 or @c SharedNetwork6) to fetch the parent specific
/// value if the value is unspecified for this instance.
/// @param property the value to return when inheritance mode is NONE, or
/// when the mode is PARENT_NETWORK and the property has not been specified
/// by a parent network.
/// @param inheritance inheritance mode to be used.
///
/// @return Option pointer fetched from this instance level or parent
/// network level.
template<typename BaseType>
OptionPtr
getOptionProperty(OptionPtr(BaseType::*MethodPointer)(const Inheritance& inheritance) const,
OptionPtr property,
const Inheritance& inheritance) const {
if (inheritance == Network::Inheritance::NONE) {
return (property);
} else if (inheritance == Network::Inheritance::PARENT_NETWORK) {
OptionPtr parent_property;
// Check if this instance has a parent network.
auto parent = boost::dynamic_pointer_cast<BaseType>(parent_network_.lock());
// If the parent network exists, let's fetch the parent specific
// value.
if (parent) {
parent_property = ((*parent).*MethodPointer)(Network::Inheritance::NONE);
}
return (parent_property);
} else if (inheritance == Network::Inheritance::GLOBAL) {
return (OptionPtr());
}
// We use inheritance and the value is not specified on the network level.
// Hence, we need to get the parent network specific value.
if (!property) {
// Check if this instance has a parent network.
auto parent = boost::dynamic_pointer_cast<BaseType>(parent_network_.lock());
if (parent) {
// We're using inheritance so ask for the parent specific network
// and return it only if it is specified.
OptionPtr parent_property = (((*parent).*MethodPointer)(inheritance));
if (parent_property) {
return (parent_property);
}
}
}
// We haven't found the value at any level, so return the unspecified.
return (property);
}
/// @brief Holds interface name for which this network is selected.
util::Optional<std::string> iface_name_;
/// @brief Relay information
///
/// See @ref RelayInfo for detailed description.
RelayInfo relay_;
/// @brief Optional definition of a client class
///
/// If defined, only clients belonging to that class will be allowed to use
/// this particular network. The default value for this is an empty string,
/// which means that any client is allowed, regardless of its class.
util::Optional<ClientClass> client_class_;
/// @brief Required classes
///
/// If the network is selected these classes will be added to the
/// incoming packet and their evaluation will be required.
ClientClasses required_classes_;
/// @brief a Triplet (min/default/max) holding allowed renew timer values
Triplet<uint32_t> t1_;
/// @brief a Triplet (min/default/max) holding allowed rebind timer values
Triplet<uint32_t> t2_;
/// @brief a Triplet (min/default/max) holding allowed valid lifetime values
Triplet<uint32_t> valid_;
/// @brief Specifies host reservation mode
///
/// See @ref HRMode type for details.
util::Optional<HRMode> host_reservation_mode_;
/// @brief Pointer to the option data configuration for this subnet.
CfgOptionPtr cfg_option_;
/// @brief Enables calculation of T1 and T2 timers
util::Optional<bool> calculate_tee_times_;
/// @brief Percentage of the lease lifetime to use when calculating T1 timer
util::Optional<double> t1_percent_;
/// @brief Percentage of the lease lifetime to use when calculating T2 timer
util::Optional<double> t2_percent_;
/// @brief Pointer to another network that this network belongs to.
///
/// The most common case is that this instance is a subnet which belongs
/// to a shared network and the @c parent_network_ points to the shared
/// network object. If the network instance (subnet) doesn't belong to
/// a shared network this pointer is null.
WeakNetworkPtr parent_network_;
/// @brief Pointer to the optional callback used to fetch globally
/// configured parameters inherited to the @c Network object.
FetchNetworkGlobalsFn fetch_globals_fn_;
};
/// @brief Specialization of the @ref Network object for DHCPv4 case.
class Network4 : public virtual Network {
public:
/// @brief Constructor.
Network4()
: Network(), match_client_id_(true, true), authoritative_(),
siaddr_(), sname_(), filename_() {
}
/// @brief Returns the flag indicating if the client identifiers should
/// be used to identify the client's lease.
///
/// @param inheritance inheritance mode to be used.
/// @return true if client identifiers should be used, false otherwise.
util::Optional<bool>
getMatchClientId(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network4>(&Network4::getMatchClientId,
match_client_id_,
inheritance,
"match-client-id"));
}
/// @brief Sets the flag indicating if the client identifier should be
/// used to identify the client's lease.
///
/// @param match If this value is true, the client identifiers are not
/// used for lease lookup.
void setMatchClientId(const util::Optional<bool>& match) {
match_client_id_ = match;
}
/// @brief Returns the flag indicating if requests for unknown IP addresses
/// should be rejected with DHCPNAK instead of ignored.
///
/// @param inheritance inheritance mode to be used.w
/// @return true if requests for unknown IP addresses should be rejected,
/// false otherwise.
util::Optional<bool>
getAuthoritative(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network4>(&Network4::getAuthoritative, authoritative_,
inheritance, "authoritative"));
}
/// @brief Sets the flag indicating if requests for unknown IP addresses
/// should be rejected with DHCPNAK instead of ignored.
///
/// @param authoritative If this value is true, the requests for unknown IP
/// addresses will be rejected with DHCPNAK messages
void setAuthoritative(const util::Optional<bool>& authoritative) {
authoritative_ = authoritative;
}
/// @brief Sets siaddr for the network.
///
/// Will be used for siaddr field (the next server) that typically is used
/// as TFTP server. If not specified, the default value of 0.0.0.0 is
/// used.
void setSiaddr(const util::Optional<asiolink::IOAddress>& siaddr);
/// @brief Returns siaddr for this network.
///
/// @return siaddr value
util::Optional<asiolink::IOAddress>
getSiaddr(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network4>(&Network4::getSiaddr, siaddr_,
inheritance, "next-server"));
}
/// @brief Sets server hostname for the network.
///
/// Will be used for server hostname field (may be empty if not defined)
void setSname(const util::Optional<std::string>& sname);
/// @brief Returns server hostname for this network.
///
/// @param inheritance inheritance mode to be used.
/// @return server hostname value
util::Optional<std::string>
getSname(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network4>(&Network4::getSname, sname_,
inheritance, "server-hostname"));
}
/// @brief Sets boot file name for the network.
///
/// Will be used for boot file name (may be empty if not defined)
void setFilename(const util::Optional<std::string>& filename);
/// @brief Returns boot file name for this subnet
///
/// @param inheritance inheritance mode to be used.
/// @return boot file name value
util::Optional<std::string>
getFilename(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network4>(&Network4::getFilename, filename_,
inheritance, "boot-file-name"));
}
/// @brief Unparses network object.
///
/// @return A pointer to unparsed network configuration.
virtual data::ElementPtr toElement() const;
/// @brief Returns binary representation of the dhcp-server-identifier option (54).
///
/// @return Server identifier option as IPv4 address. Zero IPv4 address
/// indicates that server identifier hasn't been specified.
virtual asiolink::IOAddress getServerId() const;
private:
/// @brief Should server use client identifiers for client lease
/// lookup.
util::Optional<bool> match_client_id_;
/// @brief Should requests for unknown IP addresses be rejected.
util::Optional<bool> authoritative_;
/// @brief siaddr value for this subnet
util::Optional<asiolink::IOAddress> siaddr_;
/// @brief server hostname for this subnet
util::Optional<std::string> sname_;
/// @brief boot file name for this subnet
util::Optional<std::string> filename_;
};
/// @brief Specialization of the @ref Network object for DHCPv6 case.
class Network6 : public virtual Network {
public:
/// @brief Constructor.
Network6()
: Network(), preferred_(), interface_id_(), rapid_commit_() {
}
/// @brief Returns preferred lifetime (in seconds)
///
/// @param inheritance inheritance mode to be used.
/// @return a triplet with preferred lifetime
Triplet<uint32_t>
getPreferred(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network6>(&Network6::getPreferred, preferred_,
inheritance, "preferred-lifetime"));
}
/// @brief Sets new preferred lifetime for a network.
///
/// @param preferred New preferred lifetime in seconds.
void setPreferred(const Triplet<uint32_t>& preferred) {
preferred_ = preferred;
}
/// @brief Returns interface-id value (if specified)
///
/// @param inheritance inheritance mode to be used.
/// @return interface-id option (if defined)
OptionPtr getInterfaceId(const Inheritance& inheritance = Inheritance::ALL) const {
return (getOptionProperty<Network6>(&Network6::getInterfaceId, interface_id_,
inheritance));
}
/// @brief sets interface-id option (if defined)
///
/// @param ifaceid pointer to interface-id option
void setInterfaceId(const OptionPtr& ifaceid) {
interface_id_ = ifaceid;
}
/// @brief Returns boolean value indicating that the Rapid Commit option
/// is supported or unsupported for the subnet.
///
/// @param inheritance inheritance mode to be used.
/// @return true if the Rapid Commit option is supported, false otherwise.
util::Optional<bool>
getRapidCommit(const Inheritance& inheritance = Inheritance::ALL) const {
return (getProperty<Network6>(&Network6::getRapidCommit, rapid_commit_,
inheritance, "rapid-commit"));
}
/// @brief Enables or disables Rapid Commit option support for the subnet.
///
/// @param rapid_commit A boolean value indicating that the Rapid Commit
/// option support is enabled (if true), or disabled (if false).
void setRapidCommit(const util::Optional<bool>& rapid_commit) {
rapid_commit_ = rapid_commit;
};
/// @brief Unparses network object.
///
/// @return A pointer to unparsed network configuration.
virtual data::ElementPtr toElement() const;
private:
/// @brief a triplet with preferred lifetime (in seconds)
Triplet<uint32_t> preferred_;
/// @brief specifies optional interface-id
OptionPtr interface_id_;
/// @brief A flag indicating if Rapid Commit option is supported
/// for this network.
///
/// It's default value is false, which indicates that the Rapid
/// Commit is disabled for the subnet.
util::Optional<bool> rapid_commit_;
};
} // end of namespace isc::dhcp
} // end of namespace isc
#endif // NETWORK_H
|