summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcp/option6_addrlst.cc
blob: a617e5bd6fcd9e7de66edc13429462cc9ced938e (plain)
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) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>

#include <asiolink/io_address.h>
#include <dhcp/dhcp6.h>
#include <dhcp/libdhcp++.h>
#include <dhcp/option6_addrlst.h>
#include <exceptions/exceptions.h>
#include <util/io_utilities.h>

#include <sstream>

#include <arpa/inet.h>
#include <stdint.h>

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

namespace isc {
namespace dhcp {

Option6AddrLst::Option6AddrLst(uint16_t type, const AddressContainer& addrs)
    : Option(V6, type), addrs_(addrs) {
}

Option6AddrLst::Option6AddrLst(uint16_t type, const isc::asiolink::IOAddress& addr)
    : Option(V6, type), addrs_(1,addr) {
}

Option6AddrLst::Option6AddrLst(uint16_t type, OptionBufferConstIter begin,
                               OptionBufferConstIter end)
    : Option(V6, type) {
    unpack(begin, end);
}

OptionPtr
Option6AddrLst::clone() const {
    return (cloneInternal<Option6AddrLst>());
}

void
Option6AddrLst::setAddress(const isc::asiolink::IOAddress& addr) {
    if (!addr.isV6()) {
        isc_throw(BadValue, "Can't store non-IPv6 address in Option6AddrLst option");
    }

    addrs_.clear();
    addrs_.push_back(addr);
}

void
Option6AddrLst::setAddresses(const AddressContainer& addrs) {
    addrs_ = addrs;
}

void Option6AddrLst::pack(isc::util::OutputBuffer& buf, bool) const {
    buf.writeUint16(type_);

    // len() returns complete option length.
    // len field contains length without 4-byte option header
    buf.writeUint16(len() - getHeaderLen());

    for (auto const& addr : addrs_) {
        if (!addr.isV6()) {
            isc_throw(isc::BadValue, addr.toText()
                      << " is not an IPv6 address");
        }
        // If an address is IPv6 address it should have assumed
        // length of V6ADDRESS_LEN.
        buf.writeData(&addr.toBytes()[0], V6ADDRESS_LEN);
    }
}

void Option6AddrLst::unpack(OptionBufferConstIter begin,
                            OptionBufferConstIter end) {
    if ((distance(begin, end) % V6ADDRESS_LEN) != 0) {
        isc_throw(OutOfRange, "Option " << type_
                  << " malformed: len=" << distance(begin, end)
                  << " is not divisible by 16.");
    }
    while (begin != end) {
        addrs_.push_back(IOAddress::fromBytes(AF_INET6, &(*begin)));
        begin += V6ADDRESS_LEN;
    }
}

std::string Option6AddrLst::toText(int indent) const {
    stringstream output;
    output << headerToText(indent) << ":";

    for (auto const& addr : addrs_) {
        output << " " << addr;
    }
    return (output.str());
}

uint16_t Option6AddrLst::len() const {
    return (OPTION6_HDR_LEN + addrs_.size() * V6ADDRESS_LEN);
}

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