blob: fc14346fd26befb9db5127444b63e3ff5ee7209c [file] [log] [blame]
Shock Jiang895bc1b2014-10-01 20:00:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi767f35c2016-07-23 01:54:42 +00003 * Copyright (c) 2014-2016, 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()
27 : m_ndnsType(NDNS_RAW)
28 , 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)
36 , m_ndnsType(NDNS_RAW)
37 , 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
42template<bool T>
43inline size_t
44Response::wireEncode(EncodingImpl<T>& block) const
45{
46 if (m_ndnsType == NDNS_RAW) {
47 // 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{
69 if (m_ndnsType == NDNS_RAW) {
70 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{
83 if (m_ndnsType == NDNS_RAW) {
84 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());
114 const Block* block = info.findAppMetaInfo(tlv::NdnsType);
115 if (block != 0)
116 m_ndnsType = static_cast<NdnsType>(readNonNegativeInteger(*block));
117
118 wireDecode(data.getContent());
119 return true;
120}
121
122
123shared_ptr<Data>
124Response::toData()
125{
126 Name name;
127 name.append(m_zone)
128 .append(m_queryType)
129 .append(m_rrLabel)
130 .append(m_rrType);
131
132 if (m_version.empty()) {
133 name.appendVersion();
134 m_version = name.get(-1);
135 }
136 else {
137 name.append(m_version);
138 }
139
140 shared_ptr<Data> data = make_shared<Data>(name);
141
142 MetaInfo info;
143 info.setFreshnessPeriod(m_freshnessPeriod);
144
145 if (m_ndnsType != NDNS_RAW) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000146 info.addAppMetaInfo(makeNonNegativeIntegerBlock(ndns::tlv::NdnsType, m_ndnsType));
Shock Jiang895bc1b2014-10-01 20:00:58 -0700147 data->setContent(this->wireEncode());
148 }
149 else {
150 data->setContent(m_appContent);
151 }
152 data->setMetaInfo(info);
153
154 return data;
155}
156
157
158Response&
159Response::addRr(const Block& rr)
160{
161 this->m_rrs.push_back(rr);
162 return *this;
163}
164
165Response&
166Response::addRr(const std::string& rr)
167{
Junxiao Shi767f35c2016-07-23 01:54:42 +0000168 return this->addRr(makeBinaryBlock(ndns::tlv::RrData, rr.c_str(), rr.size()));
Shock Jiang895bc1b2014-10-01 20:00:58 -0700169}
170
171bool
172Response::removeRr(const Block& rr)
173{
174 for (std::vector<Block>::iterator iter = m_rrs.begin(); iter != m_rrs.end(); ++iter) {
175 if (*iter == rr) {
176 m_rrs.erase(iter);
177 return true;
178 }
179 }
180 return false;
181}
182
183void
184Response::setAppContent(const Block& block)
185{
186 if (block.type() != ndn::tlv::Content) {
187 m_appContent = Block(ndn::tlv::Content, block);
188 } else
189 m_appContent = block;
190
191 m_appContent.encode(); // this is a must
192}
193
194
195bool
196Response::operator==(const Response& other) const
197{
198 bool tmp = (getZone() == other.getZone() &&
199 getQueryType() == other.getQueryType() && getRrLabel() == other.getRrLabel() &&
200 getRrType() == other.getRrType() && getVersion() == other.getVersion() &&
201 getNdnsType() == other.getNdnsType());
202
203 if (tmp == false)
204 return tmp;
205
206 if (m_ndnsType == NDNS_RAW) {
207 return tmp && (getAppContent() == other.getAppContent());
208 }
209 else
210 return tmp && getRrs() == other.getRrs();
211}
212
213std::ostream&
214operator<<(std::ostream& os, const Response& response)
215{
216 os << "Response: zone=" << response.getZone()
217 << " queryType=" << response.getQueryType()
218 << " rrLabel=" << response.getRrLabel()
219 << " rrType=" << response.getRrType()
220 << " version=" << response.getVersion()
221 << " freshnessPeriod=" << response.getFreshnessPeriod()
222 << " ndnsType=" << response.getNdnsType();
223 if (response.getNdnsType() == NDNS_RAW) {
224 if (response.getAppContent().empty())
225 os << " appContent=NULL";
226 else
227 os << " appContentSize=" << response.getAppContent().size();
228 }
229 else {
230 os << " rrs.size=" << response.getRrs().size();
231 }
232 return os;
233}
234} // namespace ndns
235} // namespace ndn