blob: 4aedf73a81fd5e23d64c0fd28b6610c696035b8c [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 Pesavento98026122022-03-14 22:00:03 -04003 * Copyright (c) 2014-2022, 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
Davide Pesavento98026122022-03-14 22:00:03 -040023#include <ndn-cxx/encoding/block-helpers.hpp>
24
Shock Jiang895bc1b2014-10-01 20:00:58 -070025namespace ndn {
26namespace ndns {
27
28Response::Response()
Yumin Xiaa484ba72016-11-10 20:40:12 -080029 : m_contentType(NDNS_BLOB)
Shock Jiang895bc1b2014-10-01 20:00:58 -070030 , m_freshnessPeriod(DEFAULT_RR_FRESHNESS_PERIOD)
Davide Pesavento1bff1b22020-06-08 18:46:05 -040031 , m_appContent(makeEmptyBlock(ndn::tlv::Content))
Shock Jiang895bc1b2014-10-01 20:00:58 -070032{
33}
34
35Response::Response(const Name& zone, const name::Component& queryType)
36 : m_zone(zone)
37 , m_queryType(queryType)
Yumin Xiaa484ba72016-11-10 20:40:12 -080038 , m_contentType(NDNS_BLOB)
Shock Jiang895bc1b2014-10-01 20:00:58 -070039 , m_freshnessPeriod(DEFAULT_RR_FRESHNESS_PERIOD)
Davide Pesavento1bff1b22020-06-08 18:46:05 -040040 , m_appContent(makeEmptyBlock(ndn::tlv::Content))
Shock Jiang895bc1b2014-10-01 20:00:58 -070041{
42}
43
Davide Pesavento98026122022-03-14 22:00:03 -040044template<encoding::Tag TAG>
Davide Pesavento1bff1b22020-06-08 18:46:05 -040045size_t
Davide Pesavento98026122022-03-14 22:00:03 -040046Response::wireEncode(EncodingImpl<TAG>& encoder) const
Shock Jiang895bc1b2014-10-01 20:00:58 -070047{
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080048 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -070049 // Raw application content
Davide Pesavento98026122022-03-14 22:00:03 -040050 return prependBlock(encoder, m_appContent);
Shock Jiang895bc1b2014-10-01 20:00:58 -070051 }
52
53 // Content :: = CONTENT-TYPE TLV-LENGTH
54 // Block*
55
56 size_t totalLength = 0;
Davide Pesavento1bff1b22020-06-08 18:46:05 -040057 for (auto iter = m_rrs.rbegin(); iter != m_rrs.rend(); ++iter) {
Davide Pesavento98026122022-03-14 22:00:03 -040058 totalLength += prependBlock(encoder, *iter);
Shock Jiang895bc1b2014-10-01 20:00:58 -070059 }
60
Davide Pesavento98026122022-03-14 22:00:03 -040061 totalLength += encoder.prependVarNumber(totalLength);
62 totalLength += encoder.prependVarNumber(ndn::tlv::Content);
Shock Jiang895bc1b2014-10-01 20:00:58 -070063 return totalLength;
64}
65
Davide Pesavento98026122022-03-14 22:00:03 -040066Block
Shock Jiang895bc1b2014-10-01 20:00:58 -070067Response::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
Davide Pesavento98026122022-03-14 22:00:03 -040090 auto iter = wire.elements().begin();
Shock Jiang895bc1b2014-10-01 20:00:58 -070091 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) {
Davide Pesavento948c50c2020-12-26 21:30:45 -0500101 NDN_THROW(Error("Unexpected number of elements while decoding DOE record"));
Yumin Xia55a7cc42017-05-14 18:43:34 -0700102 }
Davide Pesavento98026122022-03-14 22:00:03 -0400103 return {Name(wire.elements().front()), Name(wire.elements().back())};
Yumin Xia55a7cc42017-05-14 18:43:34 -0700104}
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{
Davide Pesavento38fd3982022-04-18 22:22:02 -0400166 m_rrs.push_back(rr);
Shock Jiang895bc1b2014-10-01 20:00:58 -0700167 return *this;
168}
169
170Response&
171Response::addRr(const std::string& rr)
172{
Davide Pesavento38fd3982022-04-18 22:22:02 -0400173 return addRr(makeStringBlock(ndns::tlv::RrData, rr));
Shock Jiang895bc1b2014-10-01 20:00:58 -0700174}
175
176bool
177Response::removeRr(const Block& rr)
178{
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400179 for (auto iter = m_rrs.begin(); iter != m_rrs.end(); ++iter) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700180 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
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400210 if (!tmp)
211 return false;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700212
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400213 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY)
214 return getAppContent() == other.getAppContent();
Shock Jiang895bc1b2014-10-01 20:00:58 -0700215 else
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400216 return getRrs() == other.getRrs();
Shock Jiang895bc1b2014-10-01 20:00:58 -0700217}
218
219std::ostream&
220operator<<(std::ostream& os, const Response& response)
221{
222 os << "Response: zone=" << response.getZone()
223 << " queryType=" << response.getQueryType()
224 << " rrLabel=" << response.getRrLabel()
225 << " rrType=" << response.getRrType()
226 << " version=" << response.getVersion()
227 << " freshnessPeriod=" << response.getFreshnessPeriod()
Yumin Xiaa484ba72016-11-10 20:40:12 -0800228 << " NdnsContentType=" << response.getContentType();
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800229 if (response.getContentType() == NDNS_BLOB
230 || response.getContentType() == NDNS_KEY) {
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400231 if (response.getAppContent().isValid())
Shock Jiang895bc1b2014-10-01 20:00:58 -0700232 os << " appContentSize=" << response.getAppContent().size();
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400233 else
234 os << " appContent=NULL";
Shock Jiang895bc1b2014-10-01 20:00:58 -0700235 }
236 else {
237 os << " rrs.size=" << response.getRrs().size();
238 }
239 return os;
240}
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400241
Shock Jiang895bc1b2014-10-01 20:00:58 -0700242} // namespace ndns
243} // namespace ndn