1 // Copyright (C) 2010-2015 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <iostream>
10 #include <string>
11 
12 #include <util/buffer.h>
13 #include <dns/messagerenderer.h>
14 #include <dns/name.h>
15 #include <dns/question.h>
16 #include <dns/rrclass.h>
17 #include <dns/rrtype.h>
18 
19 using namespace std;
20 using namespace isc::util;
21 
22 namespace isc {
23 namespace dns {
Question(InputBuffer & buffer)24 Question::Question(InputBuffer& buffer) :
25     name_(buffer), rrtype_(0), rrclass_(0)
26 {
27     // In theory, we could perform this in the member initialization list,
28     // and it would be a little bit more efficient.  We don't do this, however,
29     // because the initialization ordering is crucial (type must be first)
30     // and the ordering in the initialization list depends on the appearance
31     // order of member variables.  It's fragile to rely on such an implicit
32     // dependency, so we make the initialization order explicit.
33     rrtype_ = RRType(buffer);
34     rrclass_ = RRClass(buffer);
35 }
36 
37 std::string
toText(bool newline) const38 Question::toText(bool newline) const {
39     std::string r(name_.toText() + " " + rrclass_.toText() + " " +
40                   rrtype_.toText());
41     if (newline) {
42         r.append("\n");
43     }
44 
45     return (r);
46 }
47 
48 unsigned int
toWire(OutputBuffer & buffer) const49 Question::toWire(OutputBuffer& buffer) const {
50     name_.toWire(buffer);
51     rrtype_.toWire(buffer);
52     rrclass_.toWire(buffer);    // number of "entries", which is always 1
53 
54     return (1);
55 }
56 
57 unsigned int
toWire(AbstractMessageRenderer & renderer) const58 Question::toWire(AbstractMessageRenderer& renderer) const {
59     const size_t pos0 = renderer.getLength();
60 
61     renderer.writeName(name_);
62     rrtype_.toWire(renderer);
63     rrclass_.toWire(renderer);
64 
65     // Make sure the renderer has a room for the question
66     if (renderer.getLength() > renderer.getLengthLimit()) {
67         renderer.trim(renderer.getLength() - pos0);
68         renderer.setTruncated();
69         return (0);
70     }
71 
72     return (1);                 // number of "entries"
73 }
74 
75 ostream&
operator <<(std::ostream & os,const Question & question)76 operator<<(std::ostream& os, const Question& question) {
77     os << question.toText();
78     return (os);
79 }
80 }
81 }
82