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 | // Copyright (C) 2015-2024 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/.
#include <config.h>
#include <dhcp/duid_factory.h>
#include <dhcpsrv/cfg_duid.h>
#include <util/encode/encode.h>
#include <util/str.h>
#include <iostream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc;
using namespace isc::data;
using namespace isc::util::encode;
using namespace isc::util::str;
namespace isc {
namespace dhcp {
CfgDUID::CfgDUID()
: type_(DUID::DUID_LLT), identifier_(), htype_(0), time_(0),
enterprise_id_(0), persist_(true) {
}
void
CfgDUID::setIdentifier(const std::string& identifier_as_hex) {
// Remove whitespaces.
const std::string identifier = trim(identifier_as_hex);
// Temporary result of parsing.
std::vector<uint8_t> binary;<--- The scope of the variable 'binary' can be reduced. [+]The scope of the variable 'binary' 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.
if (!identifier.empty()) {
try {
// Decode identifier specified as a string of hexadecimal digits.
decodeHex(identifier, binary);
// All went ok, so let's replace identifier with a new value.
identifier_.swap(binary);
} catch (const std::exception& ex) {
isc_throw(BadValue, "identifier specified in the DUID"
" configuration '" << identifier
<< "' is not a valid string of hexadecimal digits");
}
} else {
// If specified identifier is empty, clear our internal identifier.
identifier_.clear();
}
}
DuidPtr
CfgDUID::create(const std::string& duid_file_path) {
// Forget the current DUID.
current_duid_.reset();
// Use DUID factory to create a DUID instance.
DUIDFactory factory(persist() ? duid_file_path : "");
switch (getType()) {
case DUID::DUID_LLT:
factory.createLLT(getHType(), getTime(), getIdentifier());
break;
case DUID::DUID_EN:
factory.createEN(getEnterpriseId(), getIdentifier());
break;
case DUID::DUID_LL:
factory.createLL(getHType(), getIdentifier());
break;
default:
// This should actually never happen.
isc_throw(Unexpected, "invalid DUID type used " << getType()
<< " to create a new DUID");
}
// Save the newly created DUID.
current_duid_ = factory.get();
// Return generated DUID.
return (current_duid_);
}
ElementPtr
CfgDUID::toElement() const {
ElementPtr result = Element::createMap();
// Set user context
contextToElement(result);
// The type item is required
std::string duid_type = "LLT";
switch (type_) {
case DUID::DUID_LLT:
break;
case DUID::DUID_EN:
duid_type = "EN";
break;
case DUID::DUID_LL:
duid_type = "LL";
break;
default:
isc_throw(ToElementError, "invalid DUID type: " << getType());
break;
}
result->set("type", Element::create(duid_type));
// Set the identifier
result->set("identifier",
Element::create(util::encode::encodeHex(identifier_)));
// Set the hardware type
result->set("htype", Element::create(htype_));
// Set the time
result->set("time", Element::create(static_cast<long long>(time_)));
// Set the enterprise id
result->set("enterprise-id",
Element::create(static_cast<long long>(enterprise_id_)));
// Set the persistence flag
result->set("persist", Element::create(persist_));
return (result);
}
}
}
|