Kea  2.3.5-git
adaptor_host.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2022 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 #include <config.h>
8 
9 #include <util/encode/hex.h>
10 #include <util/strutil.h>
11 #include <yang/adaptor_host.h>
12 
13 #include <iomanip>
14 #include <sstream>
15 #include <vector>
16 
17 using namespace std;
18 using namespace isc::data;
19 using namespace isc::util;
20 
21 namespace isc {
22 namespace yang {
23 
24 const string
25 AdaptorHost::STD_CHARACTERS =
26  "0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.@_";
27 
28 void
29 AdaptorHost::quoteIdentifier(ElementPtr host) {
30  ConstElementPtr flex_id = host->get("flex-id");
31  if (!flex_id) {
32  return;
33  }
34  const string& id = flex_id->stringValue();
35  // Empty is allowed.
36  if (id.empty()) {
37  return;
38  }
39  // No special and no not printable characters?
40  if (id.find_first_not_of(STD_CHARACTERS) == string::npos) {
41  return;
42  }
43  // Quoted identifier?
44  vector<uint8_t> binary = str::quotedStringToBinary(id);
45  if (binary.empty()) {
46  binary.assign(id.begin(), id.end());
47  }
48  // Convert in hexadecimal (from DUID::toText()).
49  stringstream tmp;
50  tmp << hex;
51  bool delim = false;
52  for (vector<uint8_t>::const_iterator it = binary.begin();
53  it != binary.end(); ++it) {
54  if (delim) {
55  tmp << ":";
56  }
57  tmp << setw(2) << setfill('0') << static_cast<unsigned int>(*it);
58  delim = true;
59  }
60  host->set("flex-id", Element::create(tmp.str()));
61 }
62 
63 } // namespace yang
64 } // namespace isc
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
STL namespace.
Definition: edns.h:19
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
std::vector< uint8_t > quotedStringToBinary(const std::string &quoted_string)
Converts a string in quotes into vector.
Definition: strutil.cc:196
Defines the logger used by the top-level component of kea-lfc.