query.cc 4.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (C) 2010  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.

15
#include <util/buffer.h>
16 17 18
#include <dns/name.h>
#include <dns/rrset.h>
#include <dns/message.h>
19

20
#include <cc/data.h>
21

22
#include <datasrc/query.h>
23

JINMEI Tatuya's avatar
JINMEI Tatuya committed
24 25
using namespace isc::dns;

26
namespace isc {
Evan Hunt's avatar
Evan Hunt committed
27
namespace datasrc {
28

Evan Hunt's avatar
Evan Hunt committed
29
QueryTask::QueryTask(const Query& qry, const isc::dns::Name& n,
30 31
                     const isc::dns::RRType& t,
                     const isc::dns::Message::Section sect) :
Evan Hunt's avatar
Evan Hunt committed
32 33
    q(qry), qname(n), qclass(qry.qclass()), qtype(t), section(sect),
    op(AUTH_QUERY), state(GETANSWER), flags(0)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
34 35
{}

Evan Hunt's avatar
Evan Hunt committed
36
QueryTask::QueryTask(const Query& qry, const isc::dns::Name& n, 
37 38
                     const isc::dns::RRType& t,
                     const isc::dns::Message::Section sect,
JINMEI Tatuya's avatar
JINMEI Tatuya committed
39
                     const Op o) :
Evan Hunt's avatar
Evan Hunt committed
40 41
    q(qry), qname(n), qclass(qry.qclass()), qtype(t), section(sect), op(o),
    state(GETANSWER), flags(0)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
42 43
{}

Evan Hunt's avatar
Evan Hunt committed
44
QueryTask::QueryTask(const Query& qry, const isc::dns::Name& n,
45 46
                     const isc::dns::RRType& t,
                     const isc::dns::Message::Section sect,
JINMEI Tatuya's avatar
JINMEI Tatuya committed
47
                     const State st) :
Evan Hunt's avatar
Evan Hunt committed
48 49
    q(qry), qname(n), qclass(qry.qclass()), qtype(t), section(sect),
    op(AUTH_QUERY), state(st), flags(0)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
50 51
{}

Evan Hunt's avatar
Evan Hunt committed
52
QueryTask::QueryTask(const Query& qry, const isc::dns::Name& n,
53 54
                     const isc::dns::RRType& t,
                     const isc::dns::Message::Section sect,
JINMEI Tatuya's avatar
JINMEI Tatuya committed
55
                     const Op o, const State st) :
Evan Hunt's avatar
Evan Hunt committed
56 57
    q(qry), qname(n), qclass(qry.qclass()), qtype(t), section(sect), op(o),
    state(st), flags(0) 
JINMEI Tatuya's avatar
JINMEI Tatuya committed
58 59
{}

Evan Hunt's avatar
Evan Hunt committed
60
QueryTask::QueryTask(const Query& qry, const isc::dns::Name& n, 
JINMEI Tatuya's avatar
JINMEI Tatuya committed
61
                     const isc::dns::RRType& t, const Op o) :
Evan Hunt's avatar
Evan Hunt committed
62
    q(qry), qname(n), qclass(qry.qclass()), qtype(t),
63
    section(Message::SECTION_ANSWER), op(o), state(GETANSWER), flags(0)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
64 65
{
    if (op != SIMPLE_QUERY) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
66
        isc_throw(Unexpected, "invalid constructor for this task operation");
JINMEI Tatuya's avatar
JINMEI Tatuya committed
67 68 69 70
    }
}

// A referral query doesn't need to specify section, state, or type.
Evan Hunt's avatar
Evan Hunt committed
71 72
QueryTask::QueryTask(const Query& qry, const isc::dns::Name& n, const Op o) :
    q(qry), qname(n), qclass(qry.qclass()), qtype(RRType::ANY()),
73
    section(Message::SECTION_ANSWER), op(o), state(GETANSWER), flags(0)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
74 75
{
    if (op != REF_QUERY) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
76
        isc_throw(Unexpected, "invalid constructor for this task operation");
JINMEI Tatuya's avatar
JINMEI Tatuya committed
77 78 79
    }
}

Evan Hunt's avatar
Evan Hunt committed
80
QueryTask::QueryTask(const Query& qry, const isc::dns::Name& n,
81
                     const isc::dns::Message::Section sect, const Op o,
JINMEI Tatuya's avatar
JINMEI Tatuya committed
82
                     const State st) :
Evan Hunt's avatar
Evan Hunt committed
83 84
        q(qry), qname(n), qclass(qry.qclass()), qtype(RRType::ANY()),
        section(sect), op(o), state(st), flags(0)
JINMEI Tatuya's avatar
JINMEI Tatuya committed
85 86
{
    if (op != GLUE_QUERY && op != NOGLUE_QUERY) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
87
        isc_throw(Unexpected, "invalid constructor for this task operation");
JINMEI Tatuya's avatar
JINMEI Tatuya committed
88 89 90
    }
}

Evan Hunt's avatar
Evan Hunt committed
91 92
QueryTask::~QueryTask() {}

Evan Hunt's avatar
Evan Hunt committed
93
Query::Query(Message& m, HotCache& c, bool dnssec) :
94
    status_(PENDING), qname_(NULL), qclass_(NULL), qtype_(NULL),
Evan Hunt's avatar
Evan Hunt committed
95
    cache_(&c), message_(&m), want_additional_(true), want_dnssec_(dnssec)
96 97
{
    // Check message formatting
98
    if (message_->getRRCount(Message::SECTION_QUESTION) != 1) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
99
        isc_throw(Unexpected, "malformed message: too many questions");
100 101 102 103 104 105 106
    }

    // Populate the query task queue with the initial question
    QuestionPtr question = *message_->beginQuestion();
    qname_ = &question->getName();
    qclass_ = &question->getClass();
    qtype_ = &question->getType();
107
    restarts_ = 0;
108

Evan Hunt's avatar
Evan Hunt committed
109
    querytasks_.push(QueryTaskPtr(new QueryTask(*this, *qname_, *qtype_,
110
                                                Message::SECTION_ANSWER)));
111 112
}

Evan Hunt's avatar
Evan Hunt committed
113 114
Query::~Query() {}

115 116
}
}