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