Kea 3.3.0
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;
20using namespace isc::util::str;
21
22namespace isc {
23namespace radius {
24
25string
26canonize(const string& hexdump) {
27 string result(hexdump);
28 for (char& c : result) {
29 switch (c) {
30 case 'A':
31 case 'B':
32 case 'C':
33 case 'D':
34 case 'E':
35 case 'F':
36 // Lower case
37 c += 'a' - 'A';
38 break;
39 case ':':
40 // Canonical format use - as the separator.
41 c = '-';
42 break;
43 default:
44 break;
45 }
46 }
47 return (result);
48}
49
50vector<uint8_t>
51pop0(const ClientIdPtr& client_id) {
52 vector<uint8_t> content = client_id->getClientId();
53 if ((content.size() > 1) && (content[0] == 0)) {
54 content.erase(content.begin());
55 }
56 return (content);
57}
58
59vector<uint8_t>
60pop0(const DuidPtr& duid) {
61 vector<uint8_t> content = duid->getDuid();
62 if ((content[0] == 0) && (content[1] == 0)) {
63 content.erase(content.begin(), content.begin() + 2);
64 }
65 return (content);
66}
67
68string
69toPrintable(const vector<uint8_t>& content) {
70 if (content.empty()) {
71 return ("");
72 }
73 if (str::isPrintable(content)) {
74 string repr;
75 repr.resize(content.size());
76 memmove(&repr[0], &content[0], repr.size());
77 return (repr);
78 } else {
79 return (dumpAsHex(content));
80 }
81}
82
83vector<uint8_t>
84extractDuid(const ClientIdPtr& client_id, bool& extracted) {
85 vector<uint8_t> content = client_id->getClientId();
86 extracted = false;
87 if ((content.size() > 5) && (content[0] == CLIENT_ID_OPTION_TYPE_DUID)) {
88 extracted = true;
89 content.erase(content.begin(), content.begin() + 5);
90 }
91 // Not an error condition but likely to be unused anyway...
92 return (content);
93}
94
95} // end of namespace isc::radius
96} // 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.
vector< uint8_t > pop0(const ClientIdPtr &client_id)
Pop leading zero in a DHCPv4 client-id.
string dumpAsHex(const uint8_t *data, size_t length)
Dumps a buffer of bytes as a string of hexadecimal digits.
Definition str.cc:330
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.