summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcp/option6_ia.cc
blob: 2b414903da4b932248dc48dd41871f3fdf101881 (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
111
112
113
114
115
116
117
118
119
120
// Copyright (C) 2011-2022 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 <dhcp/dhcp6.h>
#include <dhcp/libdhcp++.h>
#include <dhcp/option6_ia.h>
#include <dhcp/option_space.h>
#include <exceptions/exceptions.h>
#include <util/io_utilities.h>

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

using namespace std;
using namespace isc::util;

namespace isc {
namespace dhcp {

Option6IA::Option6IA(uint16_t type, uint32_t iaid)
    :Option(Option::V6, type), iaid_(iaid), t1_(0), t2_(0) {

    // IA_TA has different layout than IA_NA and IA_PD. We can't use this class
    if (type == D6O_IA_TA) {
        isc_throw(BadValue, "Can't use Option6IA for IA_TA as it has "
                  "a different layout");
    }

    setEncapsulatedSpace(DHCP6_OPTION_SPACE);
}

Option6IA::Option6IA(uint16_t type, OptionBufferConstIter begin,
                     OptionBufferConstIter end)
    :Option(Option::V6, type) {

    // IA_TA has different layout than IA_NA and IA_PD. We can't use this class
    if (type == D6O_IA_TA) {
        isc_throw(BadValue, "Can't use Option6IA for IA_TA as it has "
                  "a different layout");
    }

    setEncapsulatedSpace(DHCP6_OPTION_SPACE);

    unpack(begin, end);
}

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

void Option6IA::pack(isc::util::OutputBuffer& buf, bool) const {
    buf.writeUint16(type_);
    buf.writeUint16(len() - OPTION6_HDR_LEN);
    buf.writeUint32(iaid_);
    buf.writeUint32(t1_);
    buf.writeUint32(t2_);

    packOptions(buf);
}

void Option6IA::unpack(OptionBufferConstIter begin,
                       OptionBufferConstIter end) {
    // IA_NA and IA_PD have 12 bytes content (iaid, t1, t2 fields)
    // followed by 0 or more sub-options.
    if (distance(begin, end) < OPTION6_IA_LEN) {
        isc_throw(OutOfRange, "Option " << type_ << " truncated");
    }
    iaid_ = readUint32(&(*begin), distance(begin, end));
    begin += sizeof(uint32_t);
    t1_ = readUint32(&(*begin), distance(begin, end));
    begin += sizeof(uint32_t);

    t2_ = readUint32(&(*begin), distance(begin, end));
    begin += sizeof(uint32_t);

    unpackOptions(OptionBuffer(begin, end));
}

std::string Option6IA::toText(int indent) const {
    stringstream output;

    switch(getType()) {
    case D6O_IA_NA:
        output << headerToText(indent, "IA_NA");
        break;
    case D6O_IA_PD:
        output << headerToText(indent, "IA_PD");
        break;
    default:
        output << headerToText(indent);
    }

    output << ": iaid=" << iaid_ << ", t1=" << t1_ << ", t2=" << t2_
           << suboptionsToText(indent + 2);

    return (output.str());
}

uint16_t Option6IA::len() const {

    uint16_t length = OPTION6_HDR_LEN /*header (4)*/ +
        OPTION6_IA_LEN  /* option content (12) */;

    // length of all suboptions
    for (OptionCollection::const_iterator it = options_.begin();
         it != options_.end();
         ++it) {
        length += (*it).second->len();
    }
    return (length);
}

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