/src/kea/src/hooks/dhcp/radius/radius_utils.cc
Line | Count | Source |
1 | | // Copyright (C) 2020-2025 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/str.h> |
10 | | #include <dhcp/dhcp4.h> |
11 | | #include <radius_utils.h> |
12 | | #include <cctype> |
13 | | #include <iomanip> |
14 | | #include <sstream> |
15 | | |
16 | | using namespace std; |
17 | | using namespace isc; |
18 | | using namespace isc::dhcp; |
19 | | using namespace isc::util; |
20 | | |
21 | | namespace isc { |
22 | | namespace radius { |
23 | | |
24 | | string |
25 | 0 | canonize(const string& hexdump) { |
26 | 0 | string result(hexdump); |
27 | 0 | for (char& c : result) { |
28 | 0 | switch (c) { |
29 | 0 | case 'A': |
30 | 0 | case 'B': |
31 | 0 | case 'C': |
32 | 0 | case 'D': |
33 | 0 | case 'E': |
34 | 0 | case 'F': |
35 | | // Lower case |
36 | 0 | c += 'a' - 'A'; |
37 | 0 | break; |
38 | 0 | case ':': |
39 | | // Canonical format use - as the separator. |
40 | 0 | c = '-'; |
41 | 0 | break; |
42 | 0 | default: |
43 | 0 | break; |
44 | 0 | } |
45 | 0 | } |
46 | 0 | return (result); |
47 | 0 | } |
48 | | |
49 | | vector<uint8_t> |
50 | 0 | pop0(const ClientIdPtr& client_id) { |
51 | 0 | vector<uint8_t> content = client_id->getClientId(); |
52 | 0 | if ((content.size() > 1) && (content[0] == 0)) { |
53 | 0 | content.erase(content.begin()); |
54 | 0 | } |
55 | 0 | return (content); |
56 | 0 | } |
57 | | |
58 | | vector<uint8_t> |
59 | 0 | pop0(const DuidPtr& duid) { |
60 | 0 | vector<uint8_t> content = duid->getDuid(); |
61 | 0 | if ((content[0] == 0) && (content[1] == 0)) { |
62 | 0 | content.erase(content.begin(), content.begin() + 2); |
63 | 0 | } |
64 | 0 | return (content); |
65 | 0 | } |
66 | | |
67 | | string |
68 | 0 | toPrintable(const vector<uint8_t>& content) { |
69 | 0 | if (content.empty()) { |
70 | 0 | return (""); |
71 | 0 | } |
72 | 0 | if (str::isPrintable(content)) { |
73 | 0 | string repr; |
74 | 0 | repr.resize(content.size()); |
75 | 0 | memmove(&repr[0], &content[0], repr.size()); |
76 | 0 | return (repr); |
77 | 0 | } else { |
78 | 0 | return (toHex(content)); |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | string |
83 | 0 | toHex(const vector<uint8_t>& content) { |
84 | 0 | ostringstream repr; |
85 | 0 | repr << hex; |
86 | 0 | bool delim = false; |
87 | 0 | for (const unsigned char& ch : content) { |
88 | 0 | if (delim) { |
89 | 0 | repr << ":"; |
90 | 0 | } |
91 | 0 | repr << setw(2) << setfill('0') << static_cast<unsigned>(ch); |
92 | 0 | delim = true; |
93 | 0 | } |
94 | 0 | return (repr.str()); |
95 | 0 | } |
96 | | |
97 | | vector<uint8_t> |
98 | 0 | extractDuid(const ClientIdPtr& client_id, bool& extracted) { |
99 | 0 | vector<uint8_t> content = client_id->getClientId(); |
100 | 0 | extracted = false; |
101 | 0 | if ((content.size() > 5) && (content[0] == CLIENT_ID_OPTION_TYPE_DUID)) { |
102 | 0 | extracted = true; |
103 | 0 | content.erase(content.begin(), content.begin() + 5); |
104 | 0 | } |
105 | | // Not an error condition but likely to be unused anyway... |
106 | 0 | return (content); |
107 | 0 | } |
108 | | |
109 | | } // end of namespace isc::radius |
110 | | } // end of namespace isc |