blob: 3da172b08b460f35edfe133298769b589e7f4dff [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/*
Davide Pesavento1bff1b22020-06-08 18:46:05 -04003 * Copyright (c) 2014-2020, 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)
Davide Pesavento1bff1b22020-06-08 18:46:05 -040029 , m_appContent(makeEmptyBlock(ndn::tlv::Content))
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)
Davide Pesavento1bff1b22020-06-08 18:46:05 -040038 , m_appContent(makeEmptyBlock(ndn::tlv::Content))
Shock Jiang895bc1b2014-10-01 20:00:58 -070039{
40}
41
Yumin Xia2c509c22017-02-09 14:37:36 -080042template<encoding::Tag T>
Davide Pesavento1bff1b22020-06-08 18:46:05 -040043size_t
Shock Jiang895bc1b2014-10-01 20:00:58 -070044Response::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;
Davide Pesavento1bff1b22020-06-08 18:46:05 -040055 for (auto iter = m_rrs.rbegin(); iter != m_rrs.rend(); ++iter) {
Junxiao Shi767f35c2016-07-23 01:54:42 +000056 totalLength += block.prependBlock(*iter);
Shock Jiang895bc1b2014-10-01 20:00:58 -070057 }
58
59 totalLength += block.prependVarNumber(totalLength);
60 totalLength += block.prependVarNumber(::ndn::tlv::Content);
61
62 return totalLength;
63}
64
65const Block
66Response::wireEncode() const
67{
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080068 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -070069 return m_appContent;
70 }
71
72 EncodingEstimator estimator;
73 size_t estimatedSize = wireEncode(estimator);
74 EncodingBuffer buffer(estimatedSize, 0);
75 wireEncode(buffer);
76 return buffer.block();
77}
78
79void
80Response::wireDecode(const Block& wire)
81{
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080082 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -070083 m_appContent = wire;
84 return;
85 }
86
87 wire.parse();
88
89 Block::element_const_iterator iter = wire.elements().begin();
90 for (; iter != wire.elements().end(); ++iter) {
91 m_rrs.push_back(*iter);
92 }
93}
94
Yumin Xia55a7cc42017-05-14 18:43:34 -070095std::pair<Name, Name>
96Response::wireDecodeDoe(const Block& wire)
97{
98 wire.parse();
99 if (wire.elements().size() != 2) {
100 BOOST_THROW_EXCEPTION(Error("Unexpected number of components while decoding DOE record"));
101 }
102 return std::make_pair(Name(wire.elements().front()), Name(wire.elements().back()));
103}
104
Shock Jiang895bc1b2014-10-01 20:00:58 -0700105bool
Yumin Xia6343c5b2016-10-20 15:45:50 -0700106Response::fromData(const Name& zone, const Data& data)
Shock Jiang895bc1b2014-10-01 20:00:58 -0700107{
108 label::MatchResult re;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700109 if (!matchName(data, zone, re))
Shock Jiang895bc1b2014-10-01 20:00:58 -0700110 return false;
111
112 m_rrLabel = re.rrLabel;
113 m_rrType = re.rrType;
114 m_version = re.version;
115
116 m_zone = zone;
117 size_t len = zone.size();
Shock Jiang895bc1b2014-10-01 20:00:58 -0700118 m_queryType = data.getName().get(len);
119
120 MetaInfo info = data.getMetaInfo();
121
122 m_freshnessPeriod = time::duration_cast<time::seconds>(info.getFreshnessPeriod());
Yumin Xiaa484ba72016-11-10 20:40:12 -0800123 m_contentType = NdnsContentType(data.getContentType());
Shock Jiang895bc1b2014-10-01 20:00:58 -0700124
125 wireDecode(data.getContent());
126 return true;
127}
128
129
130shared_ptr<Data>
131Response::toData()
132{
133 Name name;
134 name.append(m_zone)
135 .append(m_queryType)
136 .append(m_rrLabel)
137 .append(m_rrType);
138
139 if (m_version.empty()) {
140 name.appendVersion();
141 m_version = name.get(-1);
142 }
143 else {
144 name.append(m_version);
145 }
146
147 shared_ptr<Data> data = make_shared<Data>(name);
148
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800149 if (m_contentType != NDNS_BLOB && m_contentType != NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700150 data->setContent(this->wireEncode());
151 }
152 else {
153 data->setContent(m_appContent);
154 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800155 data->setFreshnessPeriod(m_freshnessPeriod);
156 data->setContentType(m_contentType);
Shock Jiang895bc1b2014-10-01 20:00:58 -0700157
158 return data;
159}
160
161
162Response&
163Response::addRr(const Block& rr)
164{
165 this->m_rrs.push_back(rr);
166 return *this;
167}
168
169Response&
170Response::addRr(const std::string& rr)
171{
Junxiao Shi767f35c2016-07-23 01:54:42 +0000172 return this->addRr(makeBinaryBlock(ndns::tlv::RrData, rr.c_str(), rr.size()));
Shock Jiang895bc1b2014-10-01 20:00:58 -0700173}
174
175bool
176Response::removeRr(const Block& rr)
177{
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400178 for (auto iter = m_rrs.begin(); iter != m_rrs.end(); ++iter) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700179 if (*iter == rr) {
180 m_rrs.erase(iter);
181 return true;
182 }
183 }
184 return false;
185}
186
187void
188Response::setAppContent(const Block& block)
189{
190 if (block.type() != ndn::tlv::Content) {
191 m_appContent = Block(ndn::tlv::Content, block);
Yumin Xia2c509c22017-02-09 14:37:36 -0800192 }
193 else {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700194 m_appContent = block;
Yumin Xia2c509c22017-02-09 14:37:36 -0800195 }
Shock Jiang895bc1b2014-10-01 20:00:58 -0700196
197 m_appContent.encode(); // this is a must
198}
199
200
201bool
202Response::operator==(const Response& other) const
203{
204 bool tmp = (getZone() == other.getZone() &&
205 getQueryType() == other.getQueryType() && getRrLabel() == other.getRrLabel() &&
206 getRrType() == other.getRrType() && getVersion() == other.getVersion() &&
Yumin Xiaa484ba72016-11-10 20:40:12 -0800207 getContentType() == other.getContentType());
Shock Jiang895bc1b2014-10-01 20:00:58 -0700208
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400209 if (!tmp)
210 return false;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700211
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400212 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY)
213 return getAppContent() == other.getAppContent();
Shock Jiang895bc1b2014-10-01 20:00:58 -0700214 else
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400215 return getRrs() == other.getRrs();
Shock Jiang895bc1b2014-10-01 20:00:58 -0700216}
217
218std::ostream&
219operator<<(std::ostream& os, const Response& response)
220{
221 os << "Response: zone=" << response.getZone()
222 << " queryType=" << response.getQueryType()
223 << " rrLabel=" << response.getRrLabel()
224 << " rrType=" << response.getRrType()
225 << " version=" << response.getVersion()
226 << " freshnessPeriod=" << response.getFreshnessPeriod()
Yumin Xiaa484ba72016-11-10 20:40:12 -0800227 << " NdnsContentType=" << response.getContentType();
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800228 if (response.getContentType() == NDNS_BLOB
229 || response.getContentType() == NDNS_KEY) {
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400230 if (response.getAppContent().isValid())
Shock Jiang895bc1b2014-10-01 20:00:58 -0700231 os << " appContentSize=" << response.getAppContent().size();
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400232 else
233 os << " appContent=NULL";
Shock Jiang895bc1b2014-10-01 20:00:58 -0700234 }
235 else {
236 os << " rrs.size=" << response.getRrs().size();
237 }
238 return os;
239}
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400240
Shock Jiang895bc1b2014-10-01 20:00:58 -0700241} // namespace ndns
242} // namespace ndn