blob: 5ef4d2b3281c5ae79f8e7bcd45914e43b59dc398 [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 */
Alexander Afanasyevab430ee2014-08-05 21:11:25 -070019
shockjiang85312022014-07-27 23:22:16 -070020#include "iterative-query.hpp"
21
22namespace ndn {
23namespace ndns {
24
25IterativeQuery::IterativeQuery(const Query& query)
shockjiang99ad3892014-08-03 14:56:13 -070026 : m_step(IterativeQuery::NSQuery)
27 , m_tryNum(0)
28 , m_tryMax(2)
29 , m_query(query)
30 , m_finishedLabelNum(0)
31 , m_lastFinishedLabelNum(0)
32 , m_rrLabelLen(1)
33 , m_authZoneIndex(0)
shockjiang85312022014-07-27 23:22:16 -070034{
35}
36
shockjiang99ad3892014-08-03 14:56:13 -070037bool IterativeQuery::doTimeout()
shockjiang85312022014-07-27 23:22:16 -070038{
39 abort();
40 return false;
41}
42
shockjiang99ad3892014-08-03 14:56:13 -070043void IterativeQuery::abort()
44 {
45 std::cout<<"Abort the Resolving"<<std::endl;
46 std::cout << (*this);
47 std::cout << std::endl;
48 }
shockjiang85312022014-07-27 23:22:16 -070049
shockjiang99ad3892014-08-03 14:56:13 -070050void IterativeQuery::doData(Data& data)
shockjiang85312022014-07-27 23:22:16 -070051{
shockjiang99ad3892014-08-03 14:56:13 -070052 std::cout << "[* -> *] resolve Data: " << data.getName().toUri() << std::endl;
shockjiang85312022014-07-27 23:22:16 -070053 Response re;
54 re.fromData(data);
shockjiang99ad3892014-08-03 14:56:13 -070055 std::cout << re << std::endl;
shockjiang85312022014-07-27 23:22:16 -070056
shockjiang99ad3892014-08-03 14:56:13 -070057 if (re.getResponseType() == Response::UNKNOWN) {
58 std::cout << "[* !! *] unknown content type and exit";
shockjiang85312022014-07-27 23:22:16 -070059 m_step = Abort;
60 abort();
61 return;
shockjiang99ad3892014-08-03 14:56:13 -070062 } else if (re.getResponseType() == Response::NDNS_Nack) {
63 if (m_step == NSQuery) {
64 //In fact, there are two different situations
65 //1st: /net/ndnsim/DNS/www/NS is nacked
66 //2st: /net/DNS/ndnsim/www/NS is nacked
67 m_step = RRQuery;
shockjiang85312022014-07-27 23:22:16 -070068
shockjiang99ad3892014-08-03 14:56:13 -070069 if (m_query.getRrType() == RR::NDNCERT && m_rrLabelLen == 1) {
70 //here working for KSK and NDNCERT when get a Nack
71 //e.g., /net/ndnsim/ksk-1, ksk-1 returns nack, but it should query /net
72
73 Name dstLabel = m_query.getRrLabel();
74 Name label = dstLabel.getSubName(m_finishedLabelNum, m_rrLabelLen);
75 if (boost::starts_with(label.toUri(), "/ksk") || boost::starts_with(label.toUri(), "/KSK")) {
76 m_finishedLabelNum = m_lastFinishedLabelNum;
77 }
78
shockjiang85312022014-07-27 23:22:16 -070079 }
shockjiang99ad3892014-08-03 14:56:13 -070080 } else if (m_step == RRQuery) {
81 m_step = AnswerStub;
82 }
shockjiang85312022014-07-27 23:22:16 -070083
shockjiang99ad3892014-08-03 14:56:13 -070084 m_lastResponse = re;
85 } else if (re.getResponseType() == Response::NDNS_Auth) { // need more specific info
86 m_rrLabelLen += 1;
87 } else if (re.getResponseType() == Response::NDNS_Resp) { // get the intermediate answer
shockjiang85312022014-07-27 23:22:16 -070088 if (m_step == NSQuery) {
89 //do nothing, step NSQuery
shockjiang99ad3892014-08-03 14:56:13 -070090 m_lastFinishedLabelNum = m_finishedLabelNum;
shockjiang85312022014-07-27 23:22:16 -070091 m_finishedLabelNum += m_rrLabelLen;
92 m_rrLabelLen = 1;
93 m_authZoneIndex = 0;
94 m_lastResponse = re;
95
shockjiang99ad3892014-08-03 14:56:13 -070096 } else if (m_step == RRQuery) { // final resolver gets result back
shockjiang85312022014-07-27 23:22:16 -070097 m_step = AnswerStub;
98 m_lastResponse = re;
99 }
100
shockjiang99ad3892014-08-03 14:56:13 -0700101 std::cout << "get RRs: " << m_lastResponse.getStringRRs() << std::endl;
shockjiang85312022014-07-27 23:22:16 -0700102 }
103
104}
105
shockjiang99ad3892014-08-03 14:56:13 -0700106const Interest IterativeQuery::toLatestInterest()
shockjiang85312022014-07-27 23:22:16 -0700107{
108 Query query = Query();
109 Name dstLabel = m_query.getRrLabel();
110
111 Name authZone = dstLabel.getPrefix(m_finishedLabelNum);
112
113 Name label;
114 if (m_step == RRQuery) {
115 label = dstLabel.getSubName(m_finishedLabelNum);
shockjiang99ad3892014-08-03 14:56:13 -0700116 } else {
shockjiang85312022014-07-27 23:22:16 -0700117 label = dstLabel.getSubName(m_finishedLabelNum, m_rrLabelLen);
118 }
119 query.setAuthorityZone(authZone);
120 query.setRrLabel(label);
121
shockjiang99ad3892014-08-03 14:56:13 -0700122 if (m_step == NSQuery) {
shockjiang85312022014-07-27 23:22:16 -0700123 query.setRrType(RR::NS);
124 query.setQueryType(Query::QUERY_DNS);
shockjiang99ad3892014-08-03 14:56:13 -0700125 } else if (m_step == RRQuery) {
shockjiang85312022014-07-27 23:22:16 -0700126 query.setRrType(m_query.getRrType());
shockjiang99ad3892014-08-03 14:56:13 -0700127 if (m_query.getRrType() == RR::NDNCERT) {
128 query.setQueryType(Query::QUERY_KEY);
129 query.setQueryType(Query::QUERY_DNS);
130 } else {
131 query.setQueryType(Query::QUERY_DNS);
132 }
133
134 } else if (m_step == AnswerStub) {
shockjiang85312022014-07-27 23:22:16 -0700135 query.setRrType(m_query.getRrType());
136 query.setQueryType(Query::QUERY_DNS);
137 }
138 Interest interest = query.toInterest();
139 //m_lastInterest = interest;
140
141 return interest;
142}
143
144} /* namespace ndns */
145} /* namespace ndn */