option4_addrlst.cc 4.15 KB
Newer Older
1
// Copyright (C) 2011-2012 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// 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 <asiolink/io_address.h>
#include <dhcp/option4_addrlst.h>
17 18 19 20 21 22 23 24 25
#include <exceptions/exceptions.h>
#include <util/io_utilities.h>

#include <iomanip>
#include <sstream>

#include <arpa/inet.h>
#include <stdint.h>
#include <string.h>
26 27 28 29 30

using namespace std;
using namespace isc::util;
using namespace isc::asiolink;

31 32 33
namespace isc {
namespace dhcp {

34 35 36 37 38 39 40 41 42 43 44
Option4AddrLst::Option4AddrLst(uint8_t type)
    :Option(V4, type) {
}

Option4AddrLst::Option4AddrLst(uint8_t type, const AddressContainer& addrs)
    :Option(V4, type) {
    setAddresses(addrs);
    // don't set addrs_ directly. setAddresses() will do additional checks.
}


45 46
Option4AddrLst::Option4AddrLst(uint8_t type, OptionBufferConstIter first,
                               OptionBufferConstIter last)
47
    :Option(V4, type) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
48
    if ( (distance(first, last) % V4ADDRESS_LEN) ) {
49
        isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_
Tomek Mrugalski's avatar
Tomek Mrugalski committed
50
                  << " has invalid length=" << distance(first, last)
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
                  << ", must be divisible by 4.");
    }

    while (first != last) {
        const uint8_t* ptr = &(*first);
        addAddress(IOAddress(readUint32(ptr)));
        first += V4ADDRESS_LEN;
    }
}

Option4AddrLst::Option4AddrLst(uint8_t type, const IOAddress& addr)
    :Option(V4, type) {
    setAddress(addr);
}

void
Option4AddrLst::pack4(isc::util::OutputBuffer& buf) {

    if (addrs_.size() * V4ADDRESS_LEN > 255) {
        isc_throw(OutOfRange, "DHCPv4 Option4AddrLst " << type_ << " is too big."
                  << "At most 255 bytes are supported.");
        /// TODO Larger options can be stored as separate instances
        /// of DHCPv4 options. Clients MUST concatenate them.
        /// Fortunately, there are no such large options used today.
    }

    buf.writeUint8(type_);
    buf.writeUint8(len() - getHeaderLen());

    AddressContainer::const_iterator addr = addrs_.begin();

    while (addr != addrs_.end()) {
        buf.writeUint32(*addr);
Tomek Mrugalski's avatar
Tomek Mrugalski committed
84
        ++addr;
85 86 87 88
    }
}

void Option4AddrLst::setAddress(const isc::asiolink::IOAddress& addr) {
89
    if (!addr.isV4()) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        isc_throw(BadValue, "Can't store non-IPv4 address in "
                  << "Option4AddrLst option");
    }
    addrs_.clear();
    addAddress(addr);
}

void Option4AddrLst::setAddresses(const AddressContainer& addrs) {

    // Do not copy it as a whole. addAddress() does sanity checks.
    // i.e. throw if someone tries to set IPv6 address.
    addrs_.clear();
    for (AddressContainer::const_iterator addr = addrs.begin();
         addr != addrs.end(); ++addr) {
        addAddress(*addr);
    }
}


void Option4AddrLst::addAddress(const isc::asiolink::IOAddress& addr) {
110
    if (!addr.isV4()) {
111 112 113 114 115 116
        isc_throw(BadValue, "Can't store non-IPv4 address in "
                  << "Option4AddrLst option");
    }
    addrs_.push_back(addr);
}

Tomek Mrugalski's avatar
Tomek Mrugalski committed
117
uint16_t Option4AddrLst::len() {
118 119 120 121 122 123 124 125

    // Returns length of the complete option (option header + data length)
    return (getHeaderLen() + addrs_.size() * V4ADDRESS_LEN);
}

std::string Option4AddrLst::toText(int indent /* =0 */ ) {
    std::stringstream tmp;

Tomek Mrugalski's avatar
Tomek Mrugalski committed
126
    for (int i = 0; i < indent; i++) {
127
        tmp << " ";
Tomek Mrugalski's avatar
Tomek Mrugalski committed
128
    }
129

Tomek Mrugalski's avatar
Tomek Mrugalski committed
130
    tmp << "type=" << type_ << ", len=" << len()-getHeaderLen() << ":";
131 132 133

    for (AddressContainer::const_iterator addr = addrs_.begin();
         addr != addrs_.end(); ++addr) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
134
        tmp << " " << (*addr);
135 136 137 138
    }

    return tmp.str();
}
139 140 141

} // end of isc::dhcp namespace
} // end of isc namespace