blob: c326bd665e99f1dd1bbec244069e96da0d0bf85f [file] [log] [blame]
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
shockjiange9c1ab92014-07-21 12:02:52 -07004 *
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -07005 * 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/>.
shockjiange9c1ab92014-07-21 12:02:52 -070018 */
19
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070020#include "response.hpp"
shockjiange9c1ab92014-07-21 12:02:52 -070021
shockjiange9c1ab92014-07-21 12:02:52 -070022namespace ndn {
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070023namespace ndns {
shockjiange9c1ab92014-07-21 12:02:52 -070024
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070025Response::Response()
shockjiang99ad3892014-08-03 14:56:13 -070026 : m_freshness(time::milliseconds(3600))
27 , m_contentType(Query::QUERY_DNS)
28 , m_responseType(NDNS_Resp)
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070029{
shockjiange9c1ab92014-07-21 12:02:52 -070030}
31
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070032Response::~Response()
33{
shockjiange9c1ab92014-07-21 12:02:52 -070034}
35
shockjianga5ae48c2014-07-27 23:21:41 -070036template<bool T>
shockjiang99ad3892014-08-03 14:56:13 -070037size_t Response::wireEncode(EncodingImpl<T> & block) const
shockjiange9c1ab92014-07-21 12:02:52 -070038{
shockjiang99ad3892014-08-03 14:56:13 -070039 size_t totalLength = 0;
40 std::vector<RR>::size_type lenOfRR = m_rrs.size();
41 RR rr;
42 Block btemp;
shockjiange9c1ab92014-07-21 12:02:52 -070043
shockjiang99ad3892014-08-03 14:56:13 -070044 //totalLength += m_rrs[0].wireEncode(block);
shockjiange9c1ab92014-07-21 12:02:52 -070045
shockjiang99ad3892014-08-03 14:56:13 -070046 for (std::vector<RR>::size_type i = 0; i < lenOfRR; i++) {
47 rr = m_rrs[lenOfRR - i - 1];
48 totalLength += rr.wireEncode(block);
49 //std::cout<<"totalLenght="<<totalLength<<std::endl;
50 }
shockjiange9c1ab92014-07-21 12:02:52 -070051
shockjiang99ad3892014-08-03 14:56:13 -070052 totalLength += prependNonNegativeIntegerBlock(block,
53 ndn::ndns::tlv::ResponseNumberOfRRData, lenOfRR);
shockjiange9c1ab92014-07-21 12:02:52 -070054
shockjiang99ad3892014-08-03 14:56:13 -070055 totalLength += block.prependVarNumber(totalLength);
56 totalLength += block.prependVarNumber(ndn::ndns::tlv::ResponseContentBlob);
shockjianga5ae48c2014-07-27 23:21:41 -070057
shockjiang99ad3892014-08-03 14:56:13 -070058 totalLength += prependNonNegativeIntegerBlock(block,
59 ndn::ndns::tlv::ResponseFressness, m_freshness.count());
shockjianga5ae48c2014-07-27 23:21:41 -070060
shockjiang99ad3892014-08-03 14:56:13 -070061 std::string msg = Response::toString(m_responseType);
62 totalLength += prependByteArrayBlock(block, ndn::ndns::tlv::ResponseType,
63 reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size());
shockjianga5ae48c2014-07-27 23:21:41 -070064
shockjiang99ad3892014-08-03 14:56:13 -070065 totalLength += m_queryName.wireEncode(block);
shockjianga5ae48c2014-07-27 23:21:41 -070066
shockjiang99ad3892014-08-03 14:56:13 -070067 totalLength += block.prependVarNumber(totalLength);
68 totalLength += block.prependVarNumber(Tlv::Content);
69 return totalLength;
shockjianga5ae48c2014-07-27 23:21:41 -070070}
71
shockjianga5ae48c2014-07-27 23:21:41 -070072const Block&
73Response::wireEncode() const
74{
75
76 if (m_wire.hasWire())
shockjiang99ad3892014-08-03 14:56:13 -070077 return m_wire;
78 EncodingEstimator estimator;
shockjianga5ae48c2014-07-27 23:21:41 -070079
shockjiang99ad3892014-08-03 14:56:13 -070080 size_t estimatedSize = wireEncode(estimator);
81 //std::cout<< typeid( this).name()<<" Instance estimatedsize="<<estimatedSize<<std::endl;
82 EncodingBuffer buffer(estimatedSize, 0);
83 wireEncode(buffer);
84 m_wire = buffer.block();
85 return m_wire;
shockjianga5ae48c2014-07-27 23:21:41 -070086}
87
shockjiang99ad3892014-08-03 14:56:13 -070088void Response::wireDecode(const Block& wire)
shockjianga5ae48c2014-07-27 23:21:41 -070089{
90 if (!wire.hasWire()) {
91 throw Tlv::Error("The supplied block does not contain wire format");
92 }
93
94 //if (wire.type() != ndn::ndns::tlv::ResponseContentBlob)
95 // throw Tlv::Error("Unexpected TLV type when decoding Content");
96
97 m_wire = wire;
98 m_wire.parse();
99
shockjianga5ae48c2014-07-27 23:21:41 -0700100 Block::element_const_iterator it = m_wire.elements_begin();
101
shockjiang99ad3892014-08-03 14:56:13 -0700102 if (it != m_wire.elements_end() && it->type() == ndn::Tlv::Name) {
shockjianga5ae48c2014-07-27 23:21:41 -0700103 m_queryName.wireDecode(*it);
shockjiang99ad3892014-08-03 14:56:13 -0700104 it++;
105 } else {
shockjianga5ae48c2014-07-27 23:21:41 -0700106 throw Tlv::Error("not the ndn::Tlv::Name type");
107 }
108
shockjiang99ad3892014-08-03 14:56:13 -0700109 if (it != m_wire.elements_end()
110 && it->type() == ndn::ndns::tlv::ResponseType) {
shockjianga5ae48c2014-07-27 23:21:41 -0700111 std::string temp = std::string(reinterpret_cast<const char*>(it->value()),
shockjiang99ad3892014-08-03 14:56:13 -0700112 it->value_size());
shockjianga5ae48c2014-07-27 23:21:41 -0700113 m_responseType = Response::toResponseType(temp);
shockjiang99ad3892014-08-03 14:56:13 -0700114 it++;
115 } else {
116 throw Tlv::Error("not the ndn::ndns::tlv::ReponseType type");
shockjianga5ae48c2014-07-27 23:21:41 -0700117 }
118
shockjiang99ad3892014-08-03 14:56:13 -0700119 if (it != m_wire.elements_end()
120 && it->type() == ndn::ndns::tlv::ResponseFressness) {
121 m_freshness = time::milliseconds(readNonNegativeInteger(*it));
122 it++;
123 } else {
124 throw Tlv::Error("not the ndn::ndns::tlv::ReponseFreshness type");
125 }
126
127 if (it != m_wire.elements_end()
128 && it->type() == ndn::ndns::tlv::ResponseContentBlob) {
129 //Block b2 = it->value();/* to check */
130 Block b2 = *it;/* to check */
131
132 b2.parse();
133 Block::element_const_iterator it2 = b2.elements_begin();
134 size_t rrlen = 0;
135 if (it2 != b2.elements_end()
136 && it2->type() == ndn::ndns::tlv::ResponseNumberOfRRData) {
137 rrlen = readNonNegativeInteger(*it2);
138 it2++;
139 } else {
140 throw Tlv::Error("not the ndn::ndns::tlv::ResponseNumberOfRRData type");
141 }
142 for (size_t i = 0; i < rrlen; i++) {
143 if (it2 != b2.elements_end() && it2->type() == ndn::ndns::tlv::RRData) {
144 RR rr;
145 rr.wireDecode(*it2);
146 this->m_rrs.push_back(rr);
147 it2++;
148 } else {
149 throw Tlv::Error("not the ndn::ndns::tlv::RRData type");
150 }
shockjianga5ae48c2014-07-27 23:21:41 -0700151 }
152
shockjiang99ad3892014-08-03 14:56:13 -0700153 it++;
154 } else {
155 throw Tlv::Error("not the ndn::ndns::tlv::ResponseContentBlob type");
156 }
shockjiange9c1ab92014-07-21 12:02:52 -0700157
158}
159
shockjiang99ad3892014-08-03 14:56:13 -0700160void Response::fromData(const Data& data)
shockjiange9c1ab92014-07-21 12:02:52 -0700161{
shockjiange9c1ab92014-07-21 12:02:52 -0700162
shockjiang99ad3892014-08-03 14:56:13 -0700163 m_queryName = data.getName();
164 m_freshness = data.getFreshnessPeriod();
165 m_contentType = Query::QueryType(data.getContentType());
166 this->wireDecode(data.getContent());
shockjianga5ae48c2014-07-27 23:21:41 -0700167}
shockjiange9c1ab92014-07-21 12:02:52 -0700168
shockjiang99ad3892014-08-03 14:56:13 -0700169void Response::fromData(const Name& name, const Data& data)
shockjianga5ae48c2014-07-27 23:21:41 -0700170{
171 fromData(data);
172}
shockjiange9c1ab92014-07-21 12:02:52 -0700173
shockjiang99ad3892014-08-03 14:56:13 -0700174Data Response::toData() const
shockjianga5ae48c2014-07-27 23:21:41 -0700175{
176 Data data;
177 data.setName(m_queryName);
178 data.setFreshnessPeriod(this->m_freshness);
179 data.setContentType(m_contentType);
180 data.setContent(this->wireEncode());
shockjiange9c1ab92014-07-21 12:02:52 -0700181
shockjianga5ae48c2014-07-27 23:21:41 -0700182 return data;
shockjiange9c1ab92014-07-21 12:02:52 -0700183}
184
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700185} // namespace ndns
186} // namespace ndn