summaryrefslogtreecommitdiffstats
path: root/src/lib/dns/rdata/generic/hinfo_13.cc
blob: cb22107a6774d816836fca194547cbda5cea2fd6 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (C) 2011  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 <config.h>

#include <string>

#include <boost/lexical_cast.hpp>

#include <exceptions/exceptions.h>

#include <dns/name.h>
#include <dns/messagerenderer.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>

using namespace std;
using namespace boost;
using namespace isc::util;

// BEGIN_ISC_NAMESPACE
// BEGIN_RDATA_NAMESPACE


HINFO::HINFO(const string& hinfo_str) {
    string::const_iterator input_iterator = hinfo_str.begin();
    cpu_ = getNextCharacterString(hinfo_str, input_iterator);

    skipLeftSpaces(hinfo_str, input_iterator);

    os_ = getNextCharacterString(hinfo_str, input_iterator);
}

HINFO::HINFO(InputBuffer& buffer, size_t rdata_len) {
    cpu_ = getNextCharacterString(buffer, rdata_len);
    os_ = getNextCharacterString(buffer, rdata_len);
}

HINFO::HINFO(const HINFO& source):
    Rdata(), cpu_(source.cpu_), os_(source.os_)
{
}

std::string
HINFO::toText() const {
    string result;
    result += "\"";
    result += cpu_;
    result += "\" \"";
    result += os_;
    result += "\"";
    return result;
}

void
HINFO::toWire(OutputBuffer& buffer) const {
    buffer.writeUint8(cpu_.size());
    buffer.writeData(cpu_.c_str(), cpu_.size());

    buffer.writeUint8(os_.size());
    buffer.writeData(os_.c_str(), os_.size());
}

void
HINFO::toWire(AbstractMessageRenderer& renderer) const {
    renderer.writeUint8(cpu_.size());
    renderer.writeData(cpu_.c_str(), cpu_.size());

    renderer.writeUint8(os_.size());
    renderer.writeData(os_.c_str(), os_.size());
}

int
HINFO::compare(const Rdata& other) const {
    const HINFO& other_hinfo = dynamic_cast<const HINFO&>(other);

    if (cpu_ < other_hinfo.cpu_) {
        return (-1);
    } else if (cpu_ > other_hinfo.cpu_) {
        return (1);
    }

    if (os_ < other_hinfo.os_) {
        return (-1);
    } else if (os_ > other_hinfo.os_) {
        return (1);
    }

    return (0);
}

const std::string&
HINFO::getCPU() const {
    return cpu_;
}

const std::string&
HINFO::getOS() const {
    return os_;
}

void
HINFO::skipLeftSpaces(const std::string& input_str,
                      std::string::const_iterator& input_iterator)
{
    if (input_iterator >= input_str.end()) {
        isc_throw(InvalidRdataText,
                  "Invalid HINFO text format, field is missing.");
    }

    if (!isspace(*input_iterator)) {
        isc_throw(InvalidRdataText,
            "Invalid HINFO text format, fields are not separated by space.");
    }
    // Skip white spaces
    while (input_iterator < input_str.end() && isspace(*input_iterator)) {
        ++input_iterator;
    }
}

std::string
HINFO::getNextCharacterString(const std::string& input_str,
                              std::string::const_iterator& input_iterator)
{
    string result;

    // If the input string only contains white-spaces, it is an invalid
    // <character-string>
    if (input_iterator >= input_str.end()) {
        isc_throw(InvalidRdataText, "Invalid HINFO text format, \
                  <character-string> field is missing.");
    }

    // Whether the <character-string> is separated with double quotes (")
    bool quotes_separated = (*input_iterator == '"');

    if (quotes_separated) {
        ++input_iterator;
    }

    while(input_iterator < input_str.end()){
        if (quotes_separated) {
            // If the <character-string> is seperated with quotes symbol and
            // another quotes symbol is encountered, it is the end of the
            // <character-string>
            if (*input_iterator == '"') {
                ++input_iterator;
                break;
            }
        } else if (*input_iterator == ' ') {
            // If the <character-string> is not seperated with quotes symbol,
            // it is seperated with <space> char
            break;
        }

        result.push_back(*input_iterator);

        ++input_iterator;
    }

    if (result.size() > MAX_CHARSTRING_LEN) {
        isc_throw(CharStringTooLong, "HINFO <character-string> is too long");
    }

    return (result);
}

std::string
HINFO::getNextCharacterString(InputBuffer& buffer, size_t len) {
    uint8_t str_len = buffer.readUint8();

    size_t pos = buffer.getPosition();
    if (len - pos < str_len) {
        isc_throw(InvalidRdataLength, "Invalid HINFO string length");
    }

    uint8_t buf[MAX_CHARSTRING_LEN];
    buffer.readData(buf, str_len);
    return (string(buf, buf + str_len));
}

// END_RDATA_NAMESPACE
// END_ISC_NAMESPACE