blob: 989634a8c0eafcc1cf6a638237091b6cebb86e73 [file] [log] [blame]
shockjiang85312022014-07-27 23:22:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19#include "iterative-query.hpp"
20
21namespace ndn {
22namespace ndns {
23
24IterativeQuery::IterativeQuery(const Query& query)
25: m_step(IterativeQuery::NSQuery)
26, m_tryNum(0)
27, m_tryMax(2)
28, m_query(query)
29, m_finishedLabelNum(0)
30, m_rrLabelLen(1)
31, m_authZoneIndex(0)
32{
33}
34
35
36bool
37IterativeQuery::doTimeout()
38{
39 abort();
40 return false;
41}
42
43void
44IterativeQuery::abort(){
45 std::cout<<(*this);
46 std::cout<<std::endl;
47}
48
49void
50IterativeQuery::doData(Data& data)
51{
52 std::cout<<"[* -> *] resolve Data: "<<data.getName().toUri()<<std::endl;
53 Response re;
54 re.fromData(data);
55 std::cout<<re<<std::endl;
56
57 if (re.getResponseType() == Response::UNKNOWN)
58 {
59 std::cout<<"[* !! *] unknown content type and exit";
60 m_step = Abort;
61 abort();
62 return;
63 }
64 else if (re.getResponseType() == Response::NDNS_Nack)
65 {
66 if (m_step == NSQuery) {
67 //In fact, there are two different situations
68 //1st: /net/ndnsim/DNS/www/NS is nacked
69 //2st: /net/DNS/ndnsim/www/NS is nacked
70 m_step = RRQuery;
71
72 } else if (m_step == RRQuery)
73 {
74 m_step = AnswerStub;
75 }
76 m_lastResponse = re;
77 }
78 else if (re.getResponseType() == Response::NDNS_Auth)
79 { // need more specific info
80 m_rrLabelLen += 1;
81
82 }
83 else if (re.getResponseType() == Response::NDNS_Resp)
84 {// get the intermediate answer
85 if (m_step == NSQuery) {
86 //do nothing, step NSQuery
87 m_finishedLabelNum += m_rrLabelLen;
88 m_rrLabelLen = 1;
89 m_authZoneIndex = 0;
90 m_lastResponse = re;
91
92 } else if (m_step == RRQuery)
93 { // final resolver gets result back
94 m_step = AnswerStub;
95 m_lastResponse = re;
96 }
97
98 std::cout<<"get RRs: "<<m_lastResponse.getStringRRs()<<std::endl;
99 }
100
101}
102
103const Interest
104IterativeQuery::toLatestInterest()
105{
106 Query query = Query();
107 Name dstLabel = m_query.getRrLabel();
108
109 Name authZone = dstLabel.getPrefix(m_finishedLabelNum);
110
111 Name label;
112 if (m_step == RRQuery) {
113 label = dstLabel.getSubName(m_finishedLabelNum);
114 }
115 else {
116 label = dstLabel.getSubName(m_finishedLabelNum, m_rrLabelLen);
117 }
118 query.setAuthorityZone(authZone);
119 query.setRrLabel(label);
120
121 if (m_step == NSQuery)
122 {
123 query.setRrType(RR::NS);
124 query.setQueryType(Query::QUERY_DNS);
125 } else if (m_step == RRQuery)
126 {
127 query.setRrType(m_query.getRrType());
128 query.setQueryType(Query::QUERY_DNS);
129 } else if (m_step == AnswerStub)
130 {
131 query.setRrType(m_query.getRrType());
132 query.setQueryType(Query::QUERY_DNS);
133 }
134 Interest interest = query.toInterest();
135 //m_lastInterest = interest;
136
137 return interest;
138}
139
140} /* namespace ndns */
141} /* namespace ndn */