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
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027 | // Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.
#include <config.h>
#include <host_cache.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <host_cache_impl.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <host_cache_parsers.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <host_cache_log.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <database/db_exceptions.h>
#include <util/encode/encode.h>
#include <util/multi_threading_mgr.h>
#include <util/str.h>
#include <string><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <fstream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace std;
using namespace isc::asiolink;
using namespace isc::config;
using namespace isc::data;
using namespace isc::db;
using namespace isc::dhcp;
using namespace isc::util;
namespace isc {
namespace host_cache {
HostCache::HostCache() : impl_(new HostCacheImpl()), mutex_(new std::mutex) {
}
HostCache::~HostCache() {
impl_.reset();
}
void HostCache::configure(const ConstElementPtr&config) {
HCConfigParser parser;
parser.parse(*this, config);
}
void HostCache::setMaximum(size_t maximum) {
MultiThreadingLock lock(*mutex_);
impl_->setMaximum(maximum);
}
size_t HostCache::getMaximum() const {
MultiThreadingLock lock(*mutex_);
return (impl_->getMaximum());
}
ConstHostCollection
HostCache::getAll(const Host::IdentifierType& /*identifier_type*/,
const uint8_t* /*identifier_begin*/,
const size_t /*identifier_len*/) const {
return (ConstHostCollection());
}
ConstHostCollection
HostCache::getAll4(const dhcp::SubnetID& /*subnet_id*/) const {
return (ConstHostCollection());
}
ConstHostCollection
HostCache::getAll6(const dhcp::SubnetID& /*subnet_id*/) const {
return (ConstHostCollection());
}
ConstHostCollection
HostCache::getAllbyHostname(const std::string& /*hostname*/) const {
return (ConstHostCollection());
}
ConstHostCollection
HostCache::getAllbyHostname4(const std::string& /*hostname*/,
const dhcp::SubnetID& /*subnet_id*/) const {
return (ConstHostCollection());
}
ConstHostCollection
HostCache::getAllbyHostname6(const std::string& /*hostname*/,
const dhcp::SubnetID& /*subnet_id*/) const {
return (ConstHostCollection());
}
dhcp::ConstHostCollection
HostCache::getPage4(const dhcp::SubnetID& /*subnet_id*/,
size_t& /*source_index*/,
uint64_t /*lower_host_id*/,
const dhcp::HostPageSize& /*page_size*/) const {
return (ConstHostCollection());
}
dhcp::ConstHostCollection
HostCache::getPage6(const dhcp::SubnetID& /*subnet_id*/,
size_t& /*source_index*/,
uint64_t /*lower_host_id*/,
const dhcp::HostPageSize& /*page_size*/) const {
return (ConstHostCollection());
}
dhcp::ConstHostCollection
HostCache::getPage4(size_t& /*source_index*/,
uint64_t /*lower_host_id*/,
const dhcp:: HostPageSize& /*page_size*/) const {
return (ConstHostCollection());
}
dhcp::ConstHostCollection
HostCache::getPage6(size_t& /*source_index*/,
uint64_t /*lower_host_id*/,
const dhcp::HostPageSize& /*page_size*/) const {
return (ConstHostCollection());
}
ConstHostCollection
HostCache::getAll4(const asiolink::IOAddress& /*address*/) const {
return (ConstHostCollection());
}
ConstHostPtr
HostCache::get4(const dhcp::SubnetID& subnet_id,
const dhcp::Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin,
const size_t identifier_len) const {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_TRACE,
HOST_CACHE_GET_ONE_SUBNET_ID_IDENTIFIER)
.arg("IPv4")
.arg(subnet_id)
.arg(Host::getIdentifierAsText(identifier_type, identifier_begin,
identifier_len));
ConstHostPtr host;
{
MultiThreadingLock lock(*mutex_);
host = impl_->get4(subnet_id, identifier_type,
identifier_begin, identifier_len);
}
if (host) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_GET_ONE_SUBNET_ID_IDENTIFIER_HOST)
.arg(subnet_id)
.arg(Host::getIdentifierAsText(identifier_type, identifier_begin,
identifier_len))
.arg(host->toText());
}
return (host);
}
ConstHostPtr
HostCache::get4(const dhcp::SubnetID& subnet_id,
const asiolink::IOAddress& address) const {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_TRACE,
HOST_CACHE_GET_ONE_SUBNET_ID_ADDRESS4)
.arg(subnet_id)
.arg(address.toText());
// Must not specify address other than IPv4.
if (!address.isV4()) {
return (ConstHostPtr());
}
ConstHostPtr host;
{
MultiThreadingLock lock(*mutex_);
host = impl_->get4(subnet_id, address);
}
if (host) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_GET_ONE_SUBNET_ID_ADDRESS4_HOST)
.arg(subnet_id)
.arg(address.toText())
.arg(host->toText());
}
return (host);
}
ConstHostCollection
HostCache::getAll4(const dhcp::SubnetID& /*subnet_id*/,
const asiolink::IOAddress& /*address*/) const {
return (ConstHostCollection());
}
ConstHostPtr
HostCache::get6(const dhcp::SubnetID& subnet_id,
const dhcp::Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin,
const size_t identifier_len) const {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_TRACE,
HOST_CACHE_GET_ONE_SUBNET_ID_IDENTIFIER)
.arg("IPv6")
.arg(subnet_id)
.arg(Host::getIdentifierAsText(identifier_type, identifier_begin,
identifier_len));
ConstHostPtr host;
{
MultiThreadingLock lock(*mutex_);
host = impl_->get6(subnet_id, identifier_type,
identifier_begin, identifier_len);
}
if (host) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_GET_ONE_SUBNET_ID_IDENTIFIER_HOST)
.arg(subnet_id)
.arg(Host::getIdentifierAsText(identifier_type, identifier_begin,
identifier_len))
.arg(host->toText());
}
return (host);
}
ConstHostPtr
HostCache::get6(const asiolink::IOAddress& prefix,
const uint8_t prefix_len) const {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_TRACE,
HOST_CACHE_GET_ONE_PREFIX)
.arg(prefix.toText())
.arg(static_cast<int>(prefix_len));
ConstHostPtr host;
{
MultiThreadingLock lock(*mutex_);
host = impl_->get6(prefix, prefix_len);
}
if (host) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_GET_ONE_PREFIX_HOST)
.arg(prefix.toText())
.arg(static_cast<int>(prefix_len))
.arg(host->toText());
}
return (host);
}
ConstHostPtr
HostCache::get6(const dhcp::SubnetID& subnet_id,
const asiolink::IOAddress& address) const {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_TRACE,
HOST_CACHE_GET_ONE_SUBNET_ID_ADDRESS6)
.arg(subnet_id)
.arg(address.toText());
// Must not specify address other than IPv6.
if (!address.isV6()) {
return (ConstHostPtr());
}
ConstHostPtr host;
{
MultiThreadingLock lock(*mutex_);
host = impl_->get6(subnet_id, address);
}
if (host) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_GET_ONE_SUBNET_ID_ADDRESS6_HOST)
.arg(subnet_id)
.arg(address.toText())
.arg(host->toText());
}
return (host);
}
ConstHostCollection
HostCache::getAll6(const dhcp::SubnetID& /*subnet_id*/,
const asiolink::IOAddress& /*address*/) const {
return (ConstHostCollection());
}
ConstHostCollection
HostCache::getAll6(const IOAddress& /*address*/) const {
return (ConstHostCollection());
}
void
HostCache::add(const HostPtr& host) {
if (!host) {
return;
}
// At least one subnet ID must be used.
if (host->getIPv4SubnetID() == SUBNET_ID_UNUSED &&
host->getIPv6SubnetID() == SUBNET_ID_UNUSED) {
return;
}
bool ret;
{
MultiThreadingLock lock(*mutex_);
ret = impl_->add(host);
}
if (ret) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS, HOST_CACHE_ADD)
.arg(host->toText());
} else {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_ADD_DUPLICATE)
.arg(host->toText());
isc_throw(DuplicateEntry, "Host cache duplicate entry error");
}
}
bool
HostCache::del(const SubnetID& subnet_id, const IOAddress& addr) {
string txt;
if (addr.isV4()) {
{
MultiThreadingLock lock(*mutex_);
txt = impl_->del4(subnet_id, addr);
}
if (!txt.empty()) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_DEL_SUBNET_ID_ADDRESS4)
.arg(subnet_id)
.arg(addr.toText())
.arg(txt);
}
} else if (addr.isV6()) {
{
MultiThreadingLock lock(*mutex_);
txt = impl_->del6(subnet_id, addr);
}
if (!txt.empty()) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_DEL_SUBNET_ID_ADDRESS6)
.arg(subnet_id)
.arg(addr.toText())
.arg(txt);
}
}
return (false);
}
bool
HostCache::del4(const SubnetID& subnet_id,
const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin,
const size_t identifier_len) {
string txt;
{
MultiThreadingLock lock(*mutex_);
txt = impl_->del4(subnet_id, identifier_type,
identifier_begin, identifier_len);
}
if (!txt.empty()) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_DEL_SUBNET_ID_IDENTIFIER4)
.arg(subnet_id)
.arg(Host::getIdentifierAsText(identifier_type,
identifier_begin,
identifier_len))
.arg(txt);
}
return (false);
}
bool
HostCache::del6(const SubnetID& subnet_id,
const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin,
const size_t identifier_len) {
string txt;
{
MultiThreadingLock lock(*mutex_);
txt = impl_->del6(subnet_id, identifier_type,
identifier_begin, identifier_len);
}
if (!txt.empty()) {
LOG_DEBUG(host_cache_logger, HOST_CACHE_DBG_RESULTS,
HOST_CACHE_DEL_SUBNET_ID_IDENTIFIER6)
.arg(subnet_id)
.arg(Host::getIdentifierAsText(identifier_type,
identifier_begin,
identifier_len))
.arg(txt);
}
return (false);
}
void
HostCache::update(HostPtr const& host) {
MultiThreadingLock lock(*mutex_);
impl_->update(host);
}
bool
HostCache::setIPReservationsUnique(const bool unique) {
// This backend does not support the mode in which multiple reservations
// for the same IP address are created. If selecting this mode is
// attempted this function returns false to indicate that this is
// not allowed.
return (unique);
}
ElementPtr
HostCache::toElement() const {
MultiThreadingLock lock(*mutex_);
return (impl_->toElement());
}
size_t
HostCache::insert(const ConstHostPtr& host, bool overwrite) {
MultiThreadingLock lock(*mutex_);
return (impl_->insert(host, overwrite));
}
bool
HostCache::remove(const HostPtr& host) {
MultiThreadingLock lock(*mutex_);
return (impl_->remove(host));
}
void
HostCache::flush(size_t count) {
MultiThreadingLock lock(*mutex_);
if (count == 0) {
impl_->clear();
} else {
impl_->flush(count);
}
}
size_t
HostCache::size() const {
MultiThreadingLock lock(*mutex_);
return (impl_->size());
}
size_t
HostCache::capacity() const {
MultiThreadingLock lock(*mutex_);
return (impl_->capacity());
}
ConstHostCollection
HostCache::get(const Host::IdentifierType& identifier_type,
const uint8_t* identifier_begin,
const size_t identifier_len) const {
MultiThreadingLock lock(*mutex_);
return (impl_->get(identifier_type, identifier_begin, identifier_len));
}
int
HostCache::cacheSizeHandler(hooks::CalloutHandle& handle) {
size_t count;
try {
extractCommand(handle);
MultiThreadingLock lock(*mutex_);
count = impl_->size();
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_SIZE_FAILED)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_SIZE)
.arg(count);
ostringstream msg;
msg << count << " entries.";
ElementPtr result = Element::createMap();
result->set("size", Element::create(static_cast<int64_t>(count)));
ConstElementPtr response =
createAnswer(CONTROL_RESULT_SUCCESS, msg.str(), result);
setResponse(handle, response);
return (0);
}
int
HostCache::cacheClearHandler(hooks::CalloutHandle& handle) {
MultiThreadingCriticalSection cs;
try {
extractCommand(handle);
impl_->clear();
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_CLEAR_FAILED)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_CLEAR);
setSuccessResponse(handle, "Cache cleared.");
return (0);
}
int
HostCache::cacheFlushHandler(hooks::CalloutHandle& handle) {
MultiThreadingCriticalSection cs;
size_t how_many = 0;<--- The scope of the variable 'how_many' can be reduced. [+]The scope of the variable 'how_many' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
size_t before = impl_->size();
string txt = "(missing parameters)";
try {
extractCommand(handle);
if (cmd_args_) {
txt = cmd_args_->str();
}
if (!cmd_args_) {
isc_throw(BadValue, "no parameters specified for the command");
}
int64_t val = cmd_args_->intValue();
if (val == 0) {
isc_throw(BadValue, "invalid (0) parameter: "
"please use cache-clear command");
}
if (val < 0) {
isc_throw(BadValue, "invalid (<0) parameter");
}
if (val > HCConfigParser::MAXIMUM) {
val = HCConfigParser::MAXIMUM + 1;
}
how_many = static_cast<size_t>(val);
impl_->flush(how_many);
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_FLUSH_FAILED)
.arg(txt)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_FLUSH);
ostringstream msg;
msg << "Cache flushed (" << before - impl_->size() << " entries removed).";
setSuccessResponse(handle, msg.str());
return (0);
}
int
HostCache::cacheGetHandler(hooks::CalloutHandle& handle) {
MultiThreadingCriticalSection cs;
ConstElementPtr dump;
size_t count = 0;
try {
extractCommand(handle);
dump = impl_->toElement();
count = dump->size();
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_GET_FAILED)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_GET)
.arg(count);
ostringstream msg;
msg << count << " entries returned.";
ConstElementPtr response =
createAnswer(count > 0 ? CONTROL_RESULT_SUCCESS : CONTROL_RESULT_EMPTY,
msg.str(), dump);
setResponse(handle, response);
return (0);
}
int
HostCache::cacheGetByIdHandler(hooks::CalloutHandle& handle) {
Host::IdentifierType id_type;
vector<uint8_t> ident;
string txt = "(missing parameters)";<--- The scope of the variable 'txt' can be reduced. [+]The scope of the variable 'txt' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
ElementPtr result = Element::createList();
size_t count = 0;
try {
extractCommand(handle);
if (cmd_args_) {
txt = cmd_args_->str();
}
if (!cmd_args_) {
isc_throw(BadValue, "no parameters specified for the command");
}
if (cmd_args_->getType() != Element::map) {
isc_throw(BadValue, "invalid (not a map) parameter");
}
// Take parameters.
string ident_txt;
unsigned have_ident = 0;
auto& map = cmd_args_->mapValue();<--- Variable 'map' can be declared as reference to const
for (auto const& cfg : map) {
if (cfg.first == "hw-address") {
id_type = Host::IDENT_HWADDR;
++have_ident;
ident_txt = cfg.second->stringValue();
} else if (cfg.first == "duid") {
id_type= Host::IDENT_DUID;
++have_ident;
ident_txt = cfg.second->stringValue();
} else if (cfg.first == "circuit-id") {
id_type= Host::IDENT_CIRCUIT_ID;
++have_ident;
ident_txt = cfg.second->stringValue();
} else if (cfg.first == "client-id") {
id_type= Host::IDENT_CLIENT_ID;
++have_ident;
ident_txt = cfg.second->stringValue();
} else if (cfg.first == "flex-id") {
id_type= Host::IDENT_FLEX;
++have_ident;
ident_txt = cfg.second->stringValue();
} else {
isc_throw(BadValue,
"unknown parameter '" << cfg.first << "'");
}
}
// Decode parameters.
if (have_ident == 0) {
isc_throw(BadValue, "an identifier is required");
}
if (have_ident > 1) {
isc_throw(BadValue, "only one identifier can be specified");
}
if (ident_txt.empty()) {
isc_throw(BadValue, "invalid (empty) identifier");
}
try {
ident = util::str::quotedStringToBinary(ident_txt);
if (ident.empty()) {
util::str::decodeFormattedHexString(ident_txt, ident);
}
} catch (...) {
isc_throw(BadValue, "invalid identifier '" << ident_txt << "'");
}
// Get the entry list and build the result.
ConstHostCollection hosts;
{
MultiThreadingLock lock(*mutex_);
hosts = impl_->get(id_type, &ident[0], ident.size());
}
for (auto const& host : hosts) {
result->add(host_cache::toElement(host));
}
count = result->size();
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_GET_BY_ID_FAILED)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_GET_BY_ID)
.arg(count);
ostringstream msg;
msg << count << " entries returned.";
ConstElementPtr response =
createAnswer(count ? CONTROL_RESULT_SUCCESS : CONTROL_RESULT_EMPTY,
msg.str(), result);
setResponse(handle, response);
return (0);
}
int
HostCache::cacheInsertHandler(hooks::CalloutHandle& handle) {
string filename;<--- Unused variable: filename
size_t count = 0;
size_t overwritten = 0;
string txt = "(missing parameters)";
try {
extractCommand(handle);
if (cmd_args_) {
txt = cmd_args_->str();
}
if (!cmd_args_) {
isc_throw(BadValue, "no parameters specified for the command");
}
if (cmd_args_->getType() == Element::map) {
HCEntryParser parser;
HostPtr entry = parser.parse(cmd_args_);
++count;
try {
MultiThreadingLock lock(*mutex_);
overwritten += impl_->insert(entry, true);
} catch (const std::exception& ex) {
isc_throw(BadValue,
"Insert host (" << entry->toText()
<< ") failed: " << ex.what());
}
} else {
HCEntryListParser parser;
HostCollection entries = parser.parse(cmd_args_);
for (auto const& entry : entries) {
++count;
try {
MultiThreadingLock lock(*mutex_);
overwritten += impl_->insert(entry, true);
} catch (const std::exception& ex) {
isc_throw(BadValue, "Insert failed at " << count
<< " host (" << entry->toText()
<< "): " << ex.what());
}
}
}
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_INSERT_FAILED)
.arg(txt)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_INSERT)
.arg(count)
.arg(overwritten);
ostringstream msg;
msg << count << " entries inserted (" << overwritten
<< " overwritten by more recent entries).";
setSuccessResponse(handle, msg.str());
return (0);
}
int
HostCache::cacheWriteHandler(hooks::CalloutHandle& handle) {
MultiThreadingCriticalSection cs;
ConstElementPtr dump;
string filename;
size_t count = 0;
string txt = "(missing parameters)";
try {
extractCommand(handle);
if (cmd_args_) {
txt = cmd_args_->str();
}
if (!cmd_args_) {
isc_throw(BadValue, "no parameters specified for the command");
}
if (cmd_args_->getType() != Element::string) {
isc_throw(BadValue, "invalid (not a string) parameter");
}
filename = cmd_args_->stringValue();
if (filename.empty()) {
isc_throw(BadValue, "invalid (empty string) parameter");
}
ofstream out(filename, ios::trunc);
if (!out.good()) {
isc_throw(Unexpected, "Unable to open file '" << filename
<< "' for writing.");
}
dump = impl_->toElement();
count = dump->size();
prettyPrint(dump, out);
out.close();
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_WRITE_FAILED)
.arg(txt)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_WRITE)
.arg(count);
ostringstream msg;
msg << count << " entries dumped to '" << filename << "'.";
setSuccessResponse(handle, msg.str());
return (0);
}
int
HostCache::cacheLoadHandler(hooks::CalloutHandle& handle) {
MultiThreadingCriticalSection cs;
string filename;
size_t count = 0;
size_t overwritten = 0;
string txt = "(missing parameters)";
try {
extractCommand(handle);
if (cmd_args_) {
txt = cmd_args_->str();
}
if (!cmd_args_) {
isc_throw(BadValue, "no parameters specified for the command");
}
if (cmd_args_->getType() != Element::string) {
isc_throw(BadValue, "invalid (not a string) parameter");
}
filename = cmd_args_->stringValue();
if (filename.empty()) {
isc_throw(BadValue, "invalid (empty string) parameter");
}
ConstElementPtr json = Element::fromJSONFile(filename);
if (!json) {
isc_throw(Unexpected, "No entries found.");
}
HCEntryListParser parser;
HostCollection entries = parser.parse(json);
for (auto const& entry : entries) {
++count;
try {
overwritten += impl_->insert(entry, true);
} catch (const std::exception& ex) {
isc_throw(BadValue, "Insert failed at " << count
<< " host ( " << entry->toText()
<< "): " << ex.what());
}
}
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_LOAD_FAILED)
.arg(txt)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_LOAD)
.arg(count)
.arg(overwritten);
ostringstream msg;
msg << count << " entries loaded from '" << filename << "' ("
<< overwritten << " overwritten by more recent entries).";
setSuccessResponse(handle, msg.str());
return (0);
}
int
HostCache::cacheRemoveHandler(hooks::CalloutHandle& handle) {
uint16_t family = AF_UNIX;
SubnetID subnet_id;
string addr_txt;<--- The scope of the variable 'addr_txt' can be reduced. [+]The scope of the variable 'addr_txt' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
Host::IdentifierType id_type;
vector<uint8_t> ident;
string txt = "(missing parameters)";
string del_txt;
try {
extractCommand(handle);
if (cmd_args_) {
txt = cmd_args_->str();
}
if (!cmd_args_) {
isc_throw(BadValue, "no parameters specified for the command");
}
if (cmd_args_->getType() != Element::map) {
isc_throw(BadValue, "invalid (not a map) parameter");
}
// Take parameters.
string ident_txt;
bool have_ident = false;
bool valid = true;
auto& map = cmd_args_->mapValue();<--- Variable 'map' can be declared as reference to const
for (auto const& cfg : map) {
if (cfg.first == "subnet-id") {
if (family != AF_UNIX) {
valid = false;
}
family = AF_UNSPEC;
subnet_id = static_cast<uint32_t>(cfg.second->intValue());
} else if (cfg.first == "subnet-id4") {
if (family != AF_UNIX) {
valid = false;
}
family = AF_INET;
subnet_id = static_cast<uint32_t>(cfg.second->intValue());
} else if (cfg.first == "subnet-id6") {
if (family != AF_UNIX) {
valid = false;
}
family = AF_INET6;
subnet_id = static_cast<uint32_t>(cfg.second->intValue());
} else if (cfg.first == "ip-address") {
addr_txt = cfg.second->stringValue();
} else if (cfg.first == "hw-address") {
id_type = Host::IDENT_HWADDR;
if (have_ident) {
valid = false;
}
have_ident = true;
ident_txt = cfg.second->stringValue();
} else if (cfg.first == "duid") {
id_type= Host::IDENT_DUID;
if (have_ident) {
valid = false;
}
have_ident = true;
ident_txt = cfg.second->stringValue();
} else if (cfg.first == "circuit-id") {
id_type= Host::IDENT_CIRCUIT_ID;
if (have_ident) {
valid = false;
}
have_ident = true;
ident_txt = cfg.second->stringValue();
} else if (cfg.first == "client-id") {
id_type= Host::IDENT_CLIENT_ID;
if (have_ident) {
valid = false;
}
have_ident = true;
ident_txt = cfg.second->stringValue();
} else if (cfg.first == "flex-id") {
id_type= Host::IDENT_FLEX;
if (have_ident) {
valid = false;
}
have_ident = true;
ident_txt = cfg.second->stringValue();
} else {
isc_throw(BadValue,
"unknown parameter '" << cfg.first << "'");
}
}
// Decode parameters.
IOAddress addr(0);
if (valid && !addr_txt.empty()) {
if (have_ident) {
valid = false;
}
if (family == AF_UNIX) {
isc_throw(BadValue, "subnet-id is required");
}
addr = IOAddress(addr_txt);
if (addr.isV4()) {
if (family == AF_INET6) {
valid = false;
}
if (addr.isV4Zero()) {
isc_throw(BadValue, "invalid ip-address '0.0.0.0'");
}
family = AF_INET;
} else if (addr.isV6()) {
if (family == AF_INET) {
valid = false;
}
if (addr.isV6Zero()) {
isc_throw(BadValue, "invalid ip-address '::'");
}
family = AF_INET6;
} else {
isc_throw(Unexpected,
"invalid ip-address '" << addr_txt << "'");
}
} else if (valid) {
if (!have_ident) {
isc_throw(BadValue,
"either ip-address or an identifier is required");
}
if (ident_txt.empty()) {
isc_throw(BadValue, "invalid (empty) identifier");
}
if ((family != AF_INET) && (family != AF_INET6)) {
isc_throw(BadValue,
"either subnet-id4 or subnet-id6 is required");
}
try {
ident = util::str::quotedStringToBinary(ident_txt);
if (ident.empty()) {
util::str::decodeFormattedHexString(ident_txt, ident);
}
} catch (...) {
isc_throw(BadValue,
"invalid identifier '" << ident_txt << "'");
}
}
// Something is wrong?
if (!valid) {
isc_throw(BadValue, "inconsistent parameters");
}
// Call del*().
MultiThreadingLock lock(*mutex_);
if (!addr_txt.empty()) {
if (family == AF_INET) {
del_txt = impl_->del4(subnet_id, addr);
} else {
del_txt = impl_->del6(subnet_id, addr);
}
} else if (family == AF_INET) {
del_txt = impl_->del4(subnet_id, id_type, &ident[0], ident.size());
} else {
del_txt = impl_->del6(subnet_id, id_type, &ident[0], ident.size());
}
} catch (const std::exception& ex) {
LOG_ERROR(host_cache_logger, HOST_CACHE_COMMAND_REMOVE_FAILED)
.arg(txt)
.arg(ex.what());
setErrorResponse(handle, ex.what());
return (1);
}
LOG_INFO(host_cache_logger, HOST_CACHE_COMMAND_REMOVE)
.arg(txt);
if (!del_txt.empty()) {
setSuccessResponse(handle, "Host removed.");
} else {
setErrorResponse(handle, "Host not removed (not found).",
CONTROL_RESULT_EMPTY);
}
return (0);
}
} // end of namespace isc::host_cache
} // end of namespace isc
|