Kea 3.1.1
radius_utils.cc
Go to the documentation of this file.
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
16using namespace std;
17using namespace isc;
18using namespace isc::dhcp;
19using namespace isc::util;
20
21namespace isc {
22namespace radius {
23
24string
25canonize(const string& hexdump) {
26 string result(hexdump);
27 for (char& c : result) {
28 switch (c) {
29 case 'A':
30 case 'B':
31 case 'C':
32 case 'D':
33 case 'E':
34 case 'F':
35 // Lower case
36 c += 'a' - 'A';
37 break;
38 case ':':
39 // Canonical format use - as the separator.
40 c = '-';
41 break;
42 default:
43 break;
44 }
45 }
46 return (result);
47}
48
49vector<uint8_t>
50pop0(const ClientIdPtr& client_id) {
51 vector<uint8_t> content = client_id->getClientId();
52 if ((content.size() > 1) && (content[0] == 0)) {
53 content.erase(content.begin());
54 }
55 return (content);
56}
57
58vector<uint8_t>
59pop0(const DuidPtr& duid) {
60 vector<uint8_t> content = duid->getDuid();
61 if ((content[0] == 0) && (content[1] == 0)) {
62 content.erase(content.begin(), content.begin() + 2);
63 }
64 return (content);
65}
66
67string
68toPrintable(const vector<uint8_t>& content) {
69 if (content.empty()) {
70 return ("");
71 }
72 if (str::isPrintable(content)) {
73 string repr;
74 repr.resize(content.size());
75 memmove(&repr[0], &content[0], repr.size());
76 return (repr);
77 } else {
78 return (toHex(content));
79 }
80}
81
82string
83toHex(const vector<uint8_t>& content) {
84 ostringstream repr;
85 repr << hex;
86 bool delim = false;
87 for (const unsigned char& ch : content) {
88 if (delim) {
89 repr << ":";
90 }
91 repr << setw(2) << setfill('0') << static_cast<unsigned>(ch);
92 delim = true;
93 }
94 return (repr.str());
95}
96
97vector<uint8_t>
98extractDuid(const ClientIdPtr& client_id, bool& extracted) {
99 vector<uint8_t> content = client_id->getClientId();
100 extracted = false;
101 if ((content.size() > 5) && (content[0] == CLIENT_ID_OPTION_TYPE_DUID)) {
102 extracted = true;
103 content.erase(content.begin(), content.begin() + 5);
104 }
105 // Not an error condition but likely to be unused anyway...
106 return (content);
107}
108
109} // end of namespace isc::radius
110} // end of namespace isc
boost::shared_ptr< DUID > DuidPtr
Definition duid.h:136
boost::shared_ptr< ClientId > ClientIdPtr
Shared pointer to a Client ID.
Definition duid.h:216
vector< uint8_t > extractDuid(const ClientIdPtr &client_id, bool &extracted)
Extract the duid from a RFC 4361 compliant DHCPv4 client ID.
string canonize(const string &hexdump)
Canonize hardware address textual representation.
string toPrintable(const vector< uint8_t > &content)
Return printable textual representation of a vector.
string toHex(const vector< uint8_t > &content)
Return hexadecimal textual representation of a vector.
vector< uint8_t > pop0(const ClientIdPtr &client_id)
Pop leading zero in a DHCPv4 client-id.
bool isPrintable(const string &content)
Check if a string is printable.
Definition str.cc:310
Defines the logger used by the top-level component of kea-lfc.