blob: 2f4da51ce41dcce60a19aa00a7b2867257036da1 [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)
shockjiang99ad3892014-08-03 14:56:13 -070025 : m_step(IterativeQuery::NSQuery)
26 , m_tryNum(0)
27 , m_tryMax(2)
28 , m_query(query)
29 , m_finishedLabelNum(0)
30 , m_lastFinishedLabelNum(0)
31 , m_rrLabelLen(1)
32 , m_authZoneIndex(0)
shockjiang85312022014-07-27 23:22:16 -070033{
34}
35
shockjiang99ad3892014-08-03 14:56:13 -070036bool IterativeQuery::doTimeout()
shockjiang85312022014-07-27 23:22:16 -070037{
38 abort();
39 return false;
40}
41
shockjiang99ad3892014-08-03 14:56:13 -070042void IterativeQuery::abort()
43 {
44 std::cout<<"Abort the Resolving"<<std::endl;
45 std::cout << (*this);
46 std::cout << std::endl;
47 }
shockjiang85312022014-07-27 23:22:16 -070048
shockjiang99ad3892014-08-03 14:56:13 -070049void IterativeQuery::doData(Data& data)
shockjiang85312022014-07-27 23:22:16 -070050{
shockjiang99ad3892014-08-03 14:56:13 -070051 std::cout << "[* -> *] resolve Data: " << data.getName().toUri() << std::endl;
shockjiang85312022014-07-27 23:22:16 -070052 Response re;
53 re.fromData(data);
shockjiang99ad3892014-08-03 14:56:13 -070054 std::cout << re << std::endl;
shockjiang85312022014-07-27 23:22:16 -070055
shockjiang99ad3892014-08-03 14:56:13 -070056 if (re.getResponseType() == Response::UNKNOWN) {
57 std::cout << "[* !! *] unknown content type and exit";
shockjiang85312022014-07-27 23:22:16 -070058 m_step = Abort;
59 abort();
60 return;
shockjiang99ad3892014-08-03 14:56:13 -070061 } else if (re.getResponseType() == Response::NDNS_Nack) {
62 if (m_step == NSQuery) {
63 //In fact, there are two different situations
64 //1st: /net/ndnsim/DNS/www/NS is nacked
65 //2st: /net/DNS/ndnsim/www/NS is nacked
66 m_step = RRQuery;
shockjiang85312022014-07-27 23:22:16 -070067
shockjiang99ad3892014-08-03 14:56:13 -070068 if (m_query.getRrType() == RR::NDNCERT && m_rrLabelLen == 1) {
69 //here working for KSK and NDNCERT when get a Nack
70 //e.g., /net/ndnsim/ksk-1, ksk-1 returns nack, but it should query /net
71
72 Name dstLabel = m_query.getRrLabel();
73 Name label = dstLabel.getSubName(m_finishedLabelNum, m_rrLabelLen);
74 if (boost::starts_with(label.toUri(), "/ksk") || boost::starts_with(label.toUri(), "/KSK")) {
75 m_finishedLabelNum = m_lastFinishedLabelNum;
76 }
77
shockjiang85312022014-07-27 23:22:16 -070078 }
shockjiang99ad3892014-08-03 14:56:13 -070079 } else if (m_step == RRQuery) {
80 m_step = AnswerStub;
81 }
shockjiang85312022014-07-27 23:22:16 -070082
shockjiang99ad3892014-08-03 14:56:13 -070083 m_lastResponse = re;
84 } else if (re.getResponseType() == Response::NDNS_Auth) { // need more specific info
85 m_rrLabelLen += 1;
86 } else if (re.getResponseType() == Response::NDNS_Resp) { // get the intermediate answer
shockjiang85312022014-07-27 23:22:16 -070087 if (m_step == NSQuery) {
88 //do nothing, step NSQuery
shockjiang99ad3892014-08-03 14:56:13 -070089 m_lastFinishedLabelNum = m_finishedLabelNum;
shockjiang85312022014-07-27 23:22:16 -070090 m_finishedLabelNum += m_rrLabelLen;
91 m_rrLabelLen = 1;
92 m_authZoneIndex = 0;
93 m_lastResponse = re;
94
shockjiang99ad3892014-08-03 14:56:13 -070095 } else if (m_step == RRQuery) { // final resolver gets result back
shockjiang85312022014-07-27 23:22:16 -070096 m_step = AnswerStub;
97 m_lastResponse = re;
98 }
99
shockjiang99ad3892014-08-03 14:56:13 -0700100 std::cout << "get RRs: " << m_lastResponse.getStringRRs() << std::endl;
shockjiang85312022014-07-27 23:22:16 -0700101 }
102
103}
104
shockjiang99ad3892014-08-03 14:56:13 -0700105const Interest IterativeQuery::toLatestInterest()
shockjiang85312022014-07-27 23:22:16 -0700106{
107 Query query = Query();
108 Name dstLabel = m_query.getRrLabel();
109
110 Name authZone = dstLabel.getPrefix(m_finishedLabelNum);
111
112 Name label;
113 if (m_step == RRQuery) {
114 label = dstLabel.getSubName(m_finishedLabelNum);
shockjiang99ad3892014-08-03 14:56:13 -0700115 } else {
shockjiang85312022014-07-27 23:22:16 -0700116 label = dstLabel.getSubName(m_finishedLabelNum, m_rrLabelLen);
117 }
118 query.setAuthorityZone(authZone);
119 query.setRrLabel(label);
120
shockjiang99ad3892014-08-03 14:56:13 -0700121 if (m_step == NSQuery) {
shockjiang85312022014-07-27 23:22:16 -0700122 query.setRrType(RR::NS);
123 query.setQueryType(Query::QUERY_DNS);
shockjiang99ad3892014-08-03 14:56:13 -0700124 } else if (m_step == RRQuery) {
shockjiang85312022014-07-27 23:22:16 -0700125 query.setRrType(m_query.getRrType());
shockjiang99ad3892014-08-03 14:56:13 -0700126 if (m_query.getRrType() == RR::NDNCERT) {
127 query.setQueryType(Query::QUERY_KEY);
128 query.setQueryType(Query::QUERY_DNS);
129 } else {
130 query.setQueryType(Query::QUERY_DNS);
131 }
132
133 } else if (m_step == AnswerStub) {
shockjiang85312022014-07-27 23:22:16 -0700134 query.setRrType(m_query.getRrType());
135 query.setQueryType(Query::QUERY_DNS);
136 }
137 Interest interest = query.toInterest();
138 //m_lastInterest = interest;
139
140 return interest;
141}
142
143} /* namespace ndns */
144} /* namespace ndn */