1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <util/str.h>
#include <dhcp/dhcp4.h>
#include <radius_utils.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cctype><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iomanip><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::dhcp;
using namespace isc::util;

namespace isc {
namespace radius {

string
canonize(const string& hexdump) {
    string result(hexdump);
    for (char& c : result) {
        switch (c) {
        case 'A':
        case 'B':
        case 'C':
        case 'D':
        case 'E':
        case 'F':
            // Lower case
            c += 'a' - 'A';
            break;
        case ':':
            // Canonical format use - as the separator.
            c = '-';
            break;
        default:
            break;
        }
    }
    return (result);
}

vector<uint8_t>
pop0(const ClientIdPtr& client_id) {
    vector<uint8_t> content = client_id->getClientId();
    if ((content.size() > 1) && (content[0] == 0)) {
        content.erase(content.begin());
    }
    return (content);
}

vector<uint8_t>
pop0(const DuidPtr& duid) {
    vector<uint8_t> content = duid->getDuid();
    if ((content[0] == 0) && (content[1] == 0)) {
        content.erase(content.begin(), content.begin() + 2);
    }
    return (content);
}

string
toPrintable(const vector<uint8_t>& content) {
    if (content.empty()) {
        return ("");
    }
    if (str::isPrintable(content)) {
        string repr;
        repr.resize(content.size());
        memmove(&repr[0], &content[0], repr.size());
        return (repr);
    } else {
        return (toHex(content));
    }
}

string
toHex(const vector<uint8_t>& content) {
    ostringstream repr;
    repr << hex;
    bool delim = false;
    for (const unsigned char& ch : content) {
        if (delim) {
            repr << ":";
        }
        repr << setw(2) << setfill('0') << static_cast<unsigned>(ch);
        delim = true;
    }
    return (repr.str());
}

vector<uint8_t>
extractDuid(const ClientIdPtr& client_id, bool& extracted) {
    vector<uint8_t> content = client_id->getClientId();
    extracted = false;
    if ((content.size() > 5) && (content[0] == CLIENT_ID_OPTION_TYPE_DUID)) {
        extracted = true;
        content.erase(content.begin(), content.begin() + 5);
    }
    // Not an error condition but likely to be unused anyway...
    return (content);
}

} // end of namespace isc::radius
} // end of namespace isc