Branch data Line data Source code
1 : : // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #include <string>
16 : : #include <sstream>
17 : : #include <ostream>
18 : :
19 : : #include <exceptions/exceptions.h>
20 : :
21 : : #include <dns/rcode.h>
22 : :
23 : : using namespace std;
24 : :
25 : : namespace isc {
26 : : namespace dns {
27 : : namespace {
28 : : // This diagram shows the wire-format representation of the 12-bit extended
29 : : // form RCODEs and its relationship with implementation specific parameters.
30 : : //
31 : : // 0 3 11 15
32 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 : : // |UNUSED | EXTENDED-RCODE | RCODE |
34 : : // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 : : // <= EXTRCODE_SHIFT (4 bits)
36 : : const unsigned int EXTRCODE_SHIFT = 4;
37 : : const unsigned int RCODE_MASK = 0x000f;
38 : :
39 : :
40 : : // EDNS-extended RCODEs are 12-bit unsigned integers. 0xfff is the highest.
41 : : const uint16_t MAX_RCODE = 0xfff;
42 : :
43 : : const char* const rcodetext[] = {
44 : : "NOERROR",
45 : : "FORMERR",
46 : : "SERVFAIL",
47 : : "NXDOMAIN",
48 : : "NOTIMP",
49 : : "REFUSED",
50 : : "YXDOMAIN",
51 : : "YXRRSET",
52 : : "NXRRSET",
53 : : "NOTAUTH",
54 : : "NOTZONE",
55 : : "RESERVED11",
56 : : "RESERVED12",
57 : : "RESERVED13",
58 : : "RESERVED14",
59 : : "RESERVED15",
60 : : "BADVERS"
61 : : };
62 : : }
63 : :
64 : 3625 : Rcode::Rcode(const uint16_t code) : code_(code) {
65 [ + + ]: 3625 : if (code_ > MAX_RCODE) {
66 [ + - ]: 8 : isc_throw(OutOfRange, "Rcode is too large to construct");
67 : : }
68 : 3621 : }
69 : :
70 : 43 : Rcode::Rcode(const uint8_t code, const uint8_t extended_code) :
71 : 43 : code_((extended_code << EXTRCODE_SHIFT) | (code & RCODE_MASK))
72 : : {
73 [ + + ]: 43 : if (code > RCODE_MASK) {
74 [ + - ]: 6 : isc_throw(OutOfRange,
75 : : "Base Rcode is too large to construct: "
76 : : << static_cast<unsigned int>(code));
77 : : }
78 : 41 : }
79 : :
80 : : uint8_t
81 : 920 : Rcode::getExtendedCode() const {
82 : 920 : return (code_ >> EXTRCODE_SHIFT);
83 : : }
84 : :
85 : : string
86 : 545 : Rcode::toText() const {
87 [ + + ]: 545 : if (code_ < sizeof(rcodetext) / sizeof (const char*)) {
88 : 541 : return (rcodetext[code_]);
89 : : }
90 : :
91 : 549 : ostringstream oss;
92 : 4 : oss << code_;
93 : : return (oss.str());
94 : : }
95 : :
96 : : ostream&
97 : 6 : operator<<(std::ostream& os, const Rcode& rcode) {
98 [ + - ]: 6 : return (os << rcode.toText());
99 : : }
100 : : }
101 : 12 : }
|