pkt.cc 3.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
//
// 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.

#include <utility>
#include <dhcp/pkt.h>

namespace isc {
namespace dhcp {

void
Pkt::addOption(const OptionPtr& opt) {
23
    options_.insert(std::pair<int, OptionPtr>(opt->getType(), opt));
24 25 26 27 28 29 30 31
}

OptionPtr
Pkt::getOption(uint16_t type) const {
    OptionCollection::const_iterator x = options_.find(type);
    if (x != options_.end()) {
        return (*x).second;
    }
32
    return (OptionPtr()); // NULL
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
}

bool
Pkt::delOption(uint16_t type) {
    isc::dhcp::OptionCollection::iterator x = options_.find(type);
    if (x!=options_.end()) {
        options_.erase(x);
        return (true); // delete successful
    }
    return (false); // can't find option to be deleted
}

bool
Pkt::inClass(const std::string& client_class) {
    return (classes_.find(client_class) != classes_.end());
}

void
Pkt::addClass(const std::string& client_class) {
    if (classes_.find(client_class) == classes_.end()) {
        classes_.insert(client_class);
    }
}

void
Pkt::updateTimestamp() {
    timestamp_ = boost::posix_time::microsec_clock::universal_time();
}

void Pkt::repack() {
63 64 65
    if (!data_.empty()) {
        buffer_out_.writeData(&data_[0], data_.size());
    }
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
void
Pkt::setRemoteHWAddr(const uint8_t htype, const uint8_t hlen,
                      const std::vector<uint8_t>& mac_addr) {
    setHWAddrMember(htype, hlen, mac_addr, remote_hwaddr_);
}

void
Pkt::setRemoteHWAddr(const HWAddrPtr& addr) {
    if (!addr) {
        isc_throw(BadValue, "Setting remote HW address to NULL is"
                  << " forbidden.");
    }
    remote_hwaddr_ = addr;
}

void
Pkt::setHWAddrMember(const uint8_t htype, const uint8_t,
                      const std::vector<uint8_t>& mac_addr,
                      HWAddrPtr& hw_addr) {

    hw_addr.reset(new HWAddr(mac_addr, htype));
}

91
HWAddrPtr
92
Pkt::getMAC(uint32_t hw_addr_src) {
93
    HWAddrPtr mac;
94
    if (hw_addr_src & MAC_SOURCE_RAW) {
95 96 97
        mac = getRemoteHWAddr();
        if (mac) {
            return (mac);
98 99 100 101
        } else if (hw_addr_src == MAC_SOURCE_RAW) {
            // If we're interested only in RAW sockets, no bother trying
            // other options.
            return (HWAddrPtr());
102 103 104 105 106 107 108 109 110
        }
    }

    /// @todo: add other MAC acquisition methods here

    // Ok, none of the methods were suitable. Return NULL.
    return (HWAddrPtr());
}

111 112
};
};