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
// 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_parsers.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cc/dhcp_config_error.h>
#include <dhcpsrv/parsers/option_data_parser.h>
#include <boost/pointer_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/lexical_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::dhcp;
using namespace std;

namespace isc {
namespace host_cache {

/// @brief Defaults for Host Cache configuration.
const SimpleDefaults HCConfigParser::HOST_CACHE_DEFAULTS = {
    { "maximum",       Element::integer, "0" }
};

/// @todo: Remove this duplicated code (see trac #5578)
void
HCConfigParser::parse(HostCache& hcref, const ConstElementPtr& config) {
    ElementPtr mutable_cfg;
    if (config) {
        mutable_cfg = boost::const_pointer_cast<Element>(config);
    } else {
        mutable_cfg = Element::createMap();
    }

    if (!mutable_cfg || (mutable_cfg->getType() != Element::map)) {
        isc_throw(ConfigError, "Host Cache config is not a map");
    }

    try {
        setDefaults(mutable_cfg, HOST_CACHE_DEFAULTS);

        int64_t maximum = getInteger(mutable_cfg, "maximum");
        if (maximum < 0) {
            isc_throw(ConfigError, "Host Cache maximum parameter is negative");
        }
        if (maximum > MAXIMUM) {
            isc_throw(ConfigError, "Host Cache maximum parameter is too large "
                      << "(" << maximum << " > " << MAXIMUM << ")");
        }
        hcref.setMaximum(static_cast<size_t>(maximum));
    } catch (const ConfigError&) {
        throw;
    } catch (const exception& ex) {
        isc_throw(ConfigError, ex.what());
    }
}

HostPtr
HCEntryParser::parse(const ConstElementPtr& entry) {
    try {
        // Scan critical parameters
        string identifier;
        string identifier_name;
        string hostname;
        ConstElementPtr user_context;
        auto& map = entry->mapValue();<--- Variable 'map' can be declared as reference to const
        for (auto const& element : map) {
            if ((element.first == "hw-address") ||
                (element.first == "duid") ||
                (element.first == "circuit-id") ||
                (element.first == "client-id") ||
                (element.first == "flex-id")) {
                if (!identifier_name.empty()) {
                    isc_throw(ConfigError, "the '" << element.first
                              << "' and '" << identifier_name
                              << "' are mutually exclusive");
                }
                identifier = element.second->stringValue();
                identifier_name = element.first;
                continue;
            }

            if (element.first == "hostname") {
                hostname = element.second->stringValue();
                continue;
            }

            if (element.first == "user-context") {
                user_context = element.second;
                continue;
            }
        }

        // Host identifier is a must.
        if (identifier_name.empty()) {
            isc_throw(ConfigError, "no identifier");
        }

        // Create a host object from the basic parameters we already parsed.
        HostPtr host;
        host.reset(new Host(identifier, identifier_name,
                            SubnetID(SUBNET_ID_UNUSED),
                            SubnetID(SUBNET_ID_UNUSED),
                            IOAddress("0.0.0.0"), hostname));
        // Add user context
        if (user_context) {
            host->setContext(user_context);
        }

        // Scan other parameters
        for (auto const& element : map) {
            if ((element.first == "hw-address") ||
                (element.first == "duid") ||
                (element.first == "circuit-id") ||
                (element.first == "client-id") ||
                (element.first == "flex-id") ||
                (element.first == "hostname") ||
                (element.first == "user-context")) {
                continue;
            }

            if (element.first == "subnet-id4") {
                host->setIPv4SubnetID(element.second->intValue());
                continue;
            }

            if (element.first == "subnet-id6") {
                host->setIPv6SubnetID(element.second->intValue());
                continue;
            }

            if (element.first == "option-data4") {
                CfgOptionPtr cfg_option = host->getCfgOption4();
                OptionDataListParser parser(AF_INET);
                parser.parse(cfg_option, element.second);
                continue;
            }

            if (element.first == "option-data6") {
                CfgOptionPtr cfg_option = host->getCfgOption6();
                OptionDataListParser parser(AF_INET6);
                parser.parse(cfg_option, element.second);
                continue;
            }

            if (element.first == "ip-address") {
                host->setIPv4Reservation(IOAddress(element.second->stringValue()));
                continue;
            }

            if (element.first == "next-server") {
                host->setNextServer(IOAddress(element.second->stringValue()));
                continue;
            }

            if (element.first == "server-hostname") {
                host->setServerHostname(element.second->stringValue());
                continue;
            }

            if (element.first == "boot-file-name") {
                host->setBootFileName(element.second->stringValue());
                continue;
            }

            if (element.first == "client-classes4") {
                auto const& classes = element.second->listValue();
                for (auto const& cclass : classes) {
                    host->addClientClass4(cclass->stringValue());
                }
                continue;
            }

            if (element.first == "client-classes6") {
                auto const& classes = element.second->listValue();
                for (auto const& cclass : classes) {
                    host->addClientClass6(cclass->stringValue());
                }
                continue;
            }

            if (element.first == "ip-addresses") {
                auto const& addresses = element.second->listValue();
                for (auto const& addr : addresses) {
                  string address = addr->stringValue();
                  host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_NA,
                                                   IOAddress(address),
                                                   128));
                }
                continue;
            }

            if (element.first == "prefixes") {
                auto& prefixes = element.second->listValue();<--- Variable 'prefixes' can be declared as reference to const
                for (auto const& pref : prefixes) {
                    string prefix = pref->stringValue();
                    uint8_t len = 128;
                    size_t pos  = prefix.find('/');
                    if (pos == string::npos) {
                        isc_throw(ConfigError, "bad prefix (no /)");
                    } else if (pos > prefix.length() - 1) {
                        isc_throw(ConfigError, "bad prefix (end /)");
                    }
                    try {
                        string preflen = prefix.substr(pos + 1);
                        len = boost::lexical_cast<unsigned>(preflen);
                    } catch (const boost::bad_lexical_cast&) {
                        isc_throw(ConfigError, "bad prefix length");
                    }
                    prefix.erase(pos);

                    host->addReservation(IPv6Resrv(IPv6Resrv::TYPE_PD,
                                                   IOAddress(prefix),
                                                   len));
                }
                continue;
            }

            if (element.first == "host-id") {
                int64_t hid = element.second->intValue();
                host->setHostId(static_cast<uint64_t>(hid));
                continue;
            }

            if (element.first == "negative") {
                host->setNegative(element.second->boolValue());
                continue;
            }

            isc_throw(ConfigError, "unknown parameter: " << element.first);
        }
        return (host);

    } catch (const ConfigError&) {
        throw;
    } catch (const exception& ex) {
        isc_throw(ConfigError, ex.what());
    }
}

} // end of namespace isc::host_cache
} // end of namespace isc