duid.cc 3.75 KB
Newer Older
1
// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

15
#include <dhcp/duid.h>
16
#include <exceptions/exceptions.h>
17
#include <util/io_utilities.h>
18

19
#include <iomanip>
20 21 22 23
#include <sstream>
#include <vector>

#include <stdint.h>
24 25 26 27 28 29 30 31

namespace isc {
namespace dhcp {

DUID::DUID(const std::vector<uint8_t>& duid) {
    if (duid.size() > MAX_DUID_LEN) {
        isc_throw(OutOfRange, "DUID too large");
    }
32 33 34 35
    if (duid.empty()) {
        isc_throw(OutOfRange, "Empty DUIDs are not allowed");
    }
    duid_ = duid;
36 37
}

38
DUID::DUID(const uint8_t* data, size_t len) {
39 40 41
    if (len > MAX_DUID_LEN) {
        isc_throw(OutOfRange, "DUID too large");
    }
42 43 44
    if (len == 0) {
        isc_throw(OutOfRange, "Empty DUIDs/Client-ids not allowed");
    }
45 46 47 48

    duid_ = std::vector<uint8_t>(data, data + len);
}

49
const std::vector<uint8_t>& DUID::getDuid() const {
50
    return (duid_);
51 52 53 54 55 56 57 58 59 60 61 62 63 64
}

DUID::DUIDType DUID::getType() const {
    if (duid_.size() < 2) {
        return (DUID_UNKNOWN);
    }
    uint16_t type = (duid_[0] << 8) + duid_[1];
    if (type < DUID_MAX) {
        return (static_cast<DUID::DUIDType>(type));
    } else {
        return (DUID_UNKNOWN);
    }
}

65 66
std::string DUID::toText() const {
    std::stringstream tmp;
67
    tmp << std::hex;
68 69 70 71 72 73
    bool delim = false;
    for (std::vector<uint8_t>::const_iterator it = duid_.begin();
         it != duid_.end(); ++it) {
        if (delim) {
            tmp << ":";
        }
74
        tmp << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(*it);
75 76 77 78 79
        delim = true;
    }
    return (tmp.str());
}

80
bool DUID::operator==(const DUID& other) const {
81 82 83
    return (this->duid_ == other.duid_);
}

84
bool DUID::operator!=(const DUID& other) const {
85 86 87
    return (this->duid_ != other.duid_);
}

88
// Constructor based on vector<uint8_t>
89
ClientId::ClientId(const std::vector<uint8_t>& clientid)
90
    : DUID(clientid) {
91 92 93 94
    if (clientid.size() < MIN_CLIENT_ID_LEN) {
        isc_throw(OutOfRange, "client-id is too short (" << clientid.size()
                  << "), at least 2 is required");
    }
95 96
}

97
// Constructor based on C-style data
98
ClientId::ClientId(const uint8_t *clientid, size_t len)
99
    : DUID(clientid, len) {
100 101 102 103
    if (len < MIN_CLIENT_ID_LEN) {
        isc_throw(OutOfRange, "client-id is too short (" << len
                  << "), at least 2 is required");
    }
104 105
}

106
// Returns a copy of client-id data
107
const std::vector<uint8_t>& ClientId::getClientId() const {
108
    return (duid_);
109 110
}

111 112 113 114 115 116 117 118 119 120
// Returns the Client ID in text form
std::string ClientId::toText() const {

    // As DUID is a private base class of ClientId, we can't access
    // its public toText() method through inheritance: instead we
    // need the interface of a ClientId::toText() that calls the
    // equivalent method in the base class.
    return (DUID::toText());
}

121 122
// Compares two client-ids
bool ClientId::operator==(const ClientId& other) const {
123 124 125
    return (this->duid_ == other.duid_);
}

126 127
// Compares two client-ids
bool ClientId::operator!=(const ClientId& other) const {
128 129 130
    return (this->duid_ != other.duid_);
}

131 132
}; // end of isc::dhcp namespace
}; // end of isc namespace