blob: 5f223d3c6d0596064f84dd8157f8d35cb4fbe58e [file] [log] [blame]
Shock Jiang895bc1b2014-10-01 20:00:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Yumin Xia55a7cc42017-05-14 18:43:34 -07003 * Copyright (c) 2014-2018, Regents of the University of California.
Shock Jiang895bc1b2014-10-01 20:00:58 -07004 *
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
20#include "response.hpp"
21#include "logger.hpp"
22
23namespace ndn {
24namespace ndns {
25
26Response::Response()
Yumin Xiaa484ba72016-11-10 20:40:12 -080027 : m_contentType(NDNS_BLOB)
Shock Jiang895bc1b2014-10-01 20:00:58 -070028 , m_freshnessPeriod(DEFAULT_RR_FRESHNESS_PERIOD)
Junxiao Shi767f35c2016-07-23 01:54:42 +000029 , m_appContent(makeBinaryBlock(ndn::tlv::Content, reinterpret_cast<const uint8_t*>(0), 0))
Shock Jiang895bc1b2014-10-01 20:00:58 -070030{
31}
32
33Response::Response(const Name& zone, const name::Component& queryType)
34 : m_zone(zone)
35 , m_queryType(queryType)
Yumin Xiaa484ba72016-11-10 20:40:12 -080036 , m_contentType(NDNS_BLOB)
Shock Jiang895bc1b2014-10-01 20:00:58 -070037 , m_freshnessPeriod(DEFAULT_RR_FRESHNESS_PERIOD)
Junxiao Shi767f35c2016-07-23 01:54:42 +000038 , m_appContent(makeBinaryBlock(ndn::tlv::Content, reinterpret_cast<const uint8_t*>(0), 0))
Shock Jiang895bc1b2014-10-01 20:00:58 -070039{
40}
41
Yumin Xia2c509c22017-02-09 14:37:36 -080042template<encoding::Tag T>
Shock Jiang895bc1b2014-10-01 20:00:58 -070043inline size_t
44Response::wireEncode(EncodingImpl<T>& block) const
45{
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080046 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -070047 // Raw application content
Junxiao Shi767f35c2016-07-23 01:54:42 +000048 return block.prependBlock(m_appContent);
Shock Jiang895bc1b2014-10-01 20:00:58 -070049 }
50
51 // Content :: = CONTENT-TYPE TLV-LENGTH
52 // Block*
53
54 size_t totalLength = 0;
55 for (std::vector<Block>::const_reverse_iterator iter = m_rrs.rbegin();
56 iter != m_rrs.rend(); ++iter) {
Junxiao Shi767f35c2016-07-23 01:54:42 +000057 totalLength += block.prependBlock(*iter);
Shock Jiang895bc1b2014-10-01 20:00:58 -070058 }
59
60 totalLength += block.prependVarNumber(totalLength);
61 totalLength += block.prependVarNumber(::ndn::tlv::Content);
62
63 return totalLength;
64}
65
66const Block
67Response::wireEncode() const
68{
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080069 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -070070 return m_appContent;
71 }
72
73 EncodingEstimator estimator;
74 size_t estimatedSize = wireEncode(estimator);
75 EncodingBuffer buffer(estimatedSize, 0);
76 wireEncode(buffer);
77 return buffer.block();
78}
79
80void
81Response::wireDecode(const Block& wire)
82{
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080083 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -070084 m_appContent = wire;
85 return;
86 }
87
88 wire.parse();
89
90 Block::element_const_iterator iter = wire.elements().begin();
91 for (; iter != wire.elements().end(); ++iter) {
92 m_rrs.push_back(*iter);
93 }
94}
95
Yumin Xia55a7cc42017-05-14 18:43:34 -070096std::pair<Name, Name>
97Response::wireDecodeDoe(const Block& wire)
98{
99 wire.parse();
100 if (wire.elements().size() != 2) {
101 BOOST_THROW_EXCEPTION(Error("Unexpected number of components while decoding DOE record"));
102 }
103 return std::make_pair(Name(wire.elements().front()), Name(wire.elements().back()));
104}
105
Shock Jiang895bc1b2014-10-01 20:00:58 -0700106bool
Yumin Xia6343c5b2016-10-20 15:45:50 -0700107Response::fromData(const Name& zone, const Data& data)
Shock Jiang895bc1b2014-10-01 20:00:58 -0700108{
109 label::MatchResult re;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700110 if (!matchName(data, zone, re))
Shock Jiang895bc1b2014-10-01 20:00:58 -0700111 return false;
112
113 m_rrLabel = re.rrLabel;
114 m_rrType = re.rrType;
115 m_version = re.version;
116
117 m_zone = zone;
118 size_t len = zone.size();
Shock Jiang895bc1b2014-10-01 20:00:58 -0700119 m_queryType = data.getName().get(len);
120
121 MetaInfo info = data.getMetaInfo();
122
123 m_freshnessPeriod = time::duration_cast<time::seconds>(info.getFreshnessPeriod());
Yumin Xiaa484ba72016-11-10 20:40:12 -0800124 m_contentType = NdnsContentType(data.getContentType());
Shock Jiang895bc1b2014-10-01 20:00:58 -0700125
126 wireDecode(data.getContent());
127 return true;
128}
129
130
131shared_ptr<Data>
132Response::toData()
133{
134 Name name;
135 name.append(m_zone)
136 .append(m_queryType)
137 .append(m_rrLabel)
138 .append(m_rrType);
139
140 if (m_version.empty()) {
141 name.appendVersion();
142 m_version = name.get(-1);
143 }
144 else {
145 name.append(m_version);
146 }
147
148 shared_ptr<Data> data = make_shared<Data>(name);
149
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800150 if (m_contentType != NDNS_BLOB && m_contentType != NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700151 data->setContent(this->wireEncode());
152 }
153 else {
154 data->setContent(m_appContent);
155 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800156 data->setFreshnessPeriod(m_freshnessPeriod);
157 data->setContentType(m_contentType);
Shock Jiang895bc1b2014-10-01 20:00:58 -0700158
159 return data;
160}
161
162
163Response&
164Response::addRr(const Block& rr)
165{
166 this->m_rrs.push_back(rr);
167 return *this;
168}
169
170Response&
171Response::addRr(const std::string& rr)
172{
Junxiao Shi767f35c2016-07-23 01:54:42 +0000173 return this->addRr(makeBinaryBlock(ndns::tlv::RrData, rr.c_str(), rr.size()));
Shock Jiang895bc1b2014-10-01 20:00:58 -0700174}
175
176bool
177Response::removeRr(const Block& rr)
178{
179 for (std::vector<Block>::iterator iter = m_rrs.begin(); iter != m_rrs.end(); ++iter) {
180 if (*iter == rr) {
181 m_rrs.erase(iter);
182 return true;
183 }
184 }
185 return false;
186}
187
188void
189Response::setAppContent(const Block& block)
190{
191 if (block.type() != ndn::tlv::Content) {
192 m_appContent = Block(ndn::tlv::Content, block);
Yumin Xia2c509c22017-02-09 14:37:36 -0800193 }
194 else {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700195 m_appContent = block;
Yumin Xia2c509c22017-02-09 14:37:36 -0800196 }
Shock Jiang895bc1b2014-10-01 20:00:58 -0700197
198 m_appContent.encode(); // this is a must
199}
200
201
202bool
203Response::operator==(const Response& other) const
204{
205 bool tmp = (getZone() == other.getZone() &&
206 getQueryType() == other.getQueryType() && getRrLabel() == other.getRrLabel() &&
207 getRrType() == other.getRrType() && getVersion() == other.getVersion() &&
Yumin Xiaa484ba72016-11-10 20:40:12 -0800208 getContentType() == other.getContentType());
Shock Jiang895bc1b2014-10-01 20:00:58 -0700209
210 if (tmp == false)
211 return tmp;
212
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800213 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700214 return tmp && (getAppContent() == other.getAppContent());
215 }
216 else
217 return tmp && getRrs() == other.getRrs();
218}
219
220std::ostream&
221operator<<(std::ostream& os, const Response& response)
222{
223 os << "Response: zone=" << response.getZone()
224 << " queryType=" << response.getQueryType()
225 << " rrLabel=" << response.getRrLabel()
226 << " rrType=" << response.getRrType()
227 << " version=" << response.getVersion()
228 << " freshnessPeriod=" << response.getFreshnessPeriod()
Yumin Xiaa484ba72016-11-10 20:40:12 -0800229 << " NdnsContentType=" << response.getContentType();
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800230 if (response.getContentType() == NDNS_BLOB
231 || response.getContentType() == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700232 if (response.getAppContent().empty())
233 os << " appContent=NULL";
234 else
235 os << " appContentSize=" << response.getAppContent().size();
236 }
237 else {
238 os << " rrs.size=" << response.getRrs().size();
239 }
240 return os;
241}
242} // namespace ndns
243} // namespace ndn