blob: a23bc3c8fede6f880ad5ac4b06b447c107704b7c [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/*
3 * Copyright (c) 2014-2017, 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
96bool
Yumin Xia6343c5b2016-10-20 15:45:50 -070097Response::fromData(const Name& zone, const Data& data)
Shock Jiang895bc1b2014-10-01 20:00:58 -070098{
99 label::MatchResult re;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700100 if (!matchName(data, zone, re))
Shock Jiang895bc1b2014-10-01 20:00:58 -0700101 return false;
102
103 m_rrLabel = re.rrLabel;
104 m_rrType = re.rrType;
105 m_version = re.version;
106
107 m_zone = zone;
108 size_t len = zone.size();
Shock Jiang895bc1b2014-10-01 20:00:58 -0700109 m_queryType = data.getName().get(len);
110
111 MetaInfo info = data.getMetaInfo();
112
113 m_freshnessPeriod = time::duration_cast<time::seconds>(info.getFreshnessPeriod());
Yumin Xiaa484ba72016-11-10 20:40:12 -0800114 m_contentType = NdnsContentType(data.getContentType());
Shock Jiang895bc1b2014-10-01 20:00:58 -0700115
116 wireDecode(data.getContent());
117 return true;
118}
119
120
121shared_ptr<Data>
122Response::toData()
123{
124 Name name;
125 name.append(m_zone)
126 .append(m_queryType)
127 .append(m_rrLabel)
128 .append(m_rrType);
129
130 if (m_version.empty()) {
131 name.appendVersion();
132 m_version = name.get(-1);
133 }
134 else {
135 name.append(m_version);
136 }
137
138 shared_ptr<Data> data = make_shared<Data>(name);
139
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800140 if (m_contentType != NDNS_BLOB && m_contentType != NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700141 data->setContent(this->wireEncode());
142 }
143 else {
144 data->setContent(m_appContent);
145 }
Yumin Xiaa484ba72016-11-10 20:40:12 -0800146 data->setFreshnessPeriod(m_freshnessPeriod);
147 data->setContentType(m_contentType);
Shock Jiang895bc1b2014-10-01 20:00:58 -0700148
149 return data;
150}
151
152
153Response&
154Response::addRr(const Block& rr)
155{
156 this->m_rrs.push_back(rr);
157 return *this;
158}
159
160Response&
161Response::addRr(const std::string& rr)
162{
Junxiao Shi767f35c2016-07-23 01:54:42 +0000163 return this->addRr(makeBinaryBlock(ndns::tlv::RrData, rr.c_str(), rr.size()));
Shock Jiang895bc1b2014-10-01 20:00:58 -0700164}
165
166bool
167Response::removeRr(const Block& rr)
168{
169 for (std::vector<Block>::iterator iter = m_rrs.begin(); iter != m_rrs.end(); ++iter) {
170 if (*iter == rr) {
171 m_rrs.erase(iter);
172 return true;
173 }
174 }
175 return false;
176}
177
178void
179Response::setAppContent(const Block& block)
180{
181 if (block.type() != ndn::tlv::Content) {
182 m_appContent = Block(ndn::tlv::Content, block);
Yumin Xia2c509c22017-02-09 14:37:36 -0800183 }
184 else {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700185 m_appContent = block;
Yumin Xia2c509c22017-02-09 14:37:36 -0800186 }
Shock Jiang895bc1b2014-10-01 20:00:58 -0700187
188 m_appContent.encode(); // this is a must
189}
190
191
192bool
193Response::operator==(const Response& other) const
194{
195 bool tmp = (getZone() == other.getZone() &&
196 getQueryType() == other.getQueryType() && getRrLabel() == other.getRrLabel() &&
197 getRrType() == other.getRrType() && getVersion() == other.getVersion() &&
Yumin Xiaa484ba72016-11-10 20:40:12 -0800198 getContentType() == other.getContentType());
Shock Jiang895bc1b2014-10-01 20:00:58 -0700199
200 if (tmp == false)
201 return tmp;
202
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800203 if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700204 return tmp && (getAppContent() == other.getAppContent());
205 }
206 else
207 return tmp && getRrs() == other.getRrs();
208}
209
210std::ostream&
211operator<<(std::ostream& os, const Response& response)
212{
213 os << "Response: zone=" << response.getZone()
214 << " queryType=" << response.getQueryType()
215 << " rrLabel=" << response.getRrLabel()
216 << " rrType=" << response.getRrType()
217 << " version=" << response.getVersion()
218 << " freshnessPeriod=" << response.getFreshnessPeriod()
Yumin Xiaa484ba72016-11-10 20:40:12 -0800219 << " NdnsContentType=" << response.getContentType();
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800220 if (response.getContentType() == NDNS_BLOB
221 || response.getContentType() == NDNS_KEY) {
Shock Jiang895bc1b2014-10-01 20:00:58 -0700222 if (response.getAppContent().empty())
223 os << " appContent=NULL";
224 else
225 os << " appContentSize=" << response.getAppContent().size();
226 }
227 else {
228 os << " rrs.size=" << response.getRrs().size();
229 }
230 return os;
231}
232} // namespace ndns
233} // namespace ndn