blob: cb13dda54d714cab9aacea68283e8e4c0caa4531 [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()
shockjianga5ae48c2014-07-27 23:21:41 -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>
37size_t
38Response::wireEncode(EncodingImpl<T> & block) const
shockjiange9c1ab92014-07-21 12:02:52 -070039{
shockjianga5ae48c2014-07-27 23:21:41 -070040 size_t totalLength = 0;
41 std::vector<RR>::size_type lenOfRR = m_rrs.size();
42 RR rr;
43 Block btemp;
shockjiange9c1ab92014-07-21 12:02:52 -070044
shockjianga5ae48c2014-07-27 23:21:41 -070045 //totalLength += m_rrs[0].wireEncode(block);
shockjiange9c1ab92014-07-21 12:02:52 -070046
shockjianga5ae48c2014-07-27 23:21:41 -070047 for (std::vector<RR>::size_type i=0; i<lenOfRR; i++)
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070048 {
shockjianga5ae48c2014-07-27 23:21:41 -070049 rr = m_rrs[lenOfRR-i-1];
50 totalLength += rr.wireEncode(block);
51 //std::cout<<"totalLenght="<<totalLength<<std::endl;
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -070052 }
shockjiange9c1ab92014-07-21 12:02:52 -070053
shockjianga5ae48c2014-07-27 23:21:41 -070054 totalLength += prependNonNegativeIntegerBlock(block,
55 ndn::ndns::tlv::ResponseNumberOfRRData,
56 lenOfRR);
shockjiange9c1ab92014-07-21 12:02:52 -070057
shockjianga5ae48c2014-07-27 23:21:41 -070058 totalLength += block.prependVarNumber(totalLength);
59 totalLength += block.prependVarNumber(ndn::ndns::tlv::ResponseContentBlob);
60
61 totalLength += prependNonNegativeIntegerBlock(block,
62 ndn::ndns::tlv::ResponseFressness,
63 m_freshness.count()
64 );
65
66 std::string msg = Response::toString(m_responseType);
67 totalLength += prependByteArrayBlock(block,
68 ndn::ndns::tlv::ResponseType,
69 reinterpret_cast<const uint8_t*>(msg.c_str()),
70 msg.size()
71 );
72
73 totalLength += m_queryName.wireEncode(block);
74
75
76 totalLength += block.prependVarNumber(totalLength);
77 totalLength += block.prependVarNumber(Tlv::Content);
78 return totalLength;
79}
80
81
82const Block&
83Response::wireEncode() const
84{
85
86 if (m_wire.hasWire())
87 return m_wire;
88 EncodingEstimator estimator;
89
90 size_t estimatedSize = wireEncode(estimator);
91 //std::cout<< typeid( this).name()<<" Instance estimatedsize="<<estimatedSize<<std::endl;
92 EncodingBuffer buffer(estimatedSize, 0);
93 wireEncode(buffer);
94 m_wire = buffer.block();
95 return m_wire;
96}
97
98
99void
100Response::wireDecode(const Block& wire)
101{
102 if (!wire.hasWire()) {
103 throw Tlv::Error("The supplied block does not contain wire format");
104 }
105
106 //if (wire.type() != ndn::ndns::tlv::ResponseContentBlob)
107 // throw Tlv::Error("Unexpected TLV type when decoding Content");
108
109 m_wire = wire;
110 m_wire.parse();
111
112
113 Block::element_const_iterator it = m_wire.elements_begin();
114
115 if (it != m_wire.elements_end() && it->type() == ndn::Tlv::Name)
116 {
117 m_queryName.wireDecode(*it);
118 it ++;
119 } else
120 {
121 throw Tlv::Error("not the ndn::Tlv::Name type");
122 }
123
124 if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::ResponseType)
125 {
126 std::string temp = std::string(reinterpret_cast<const char*>(it->value()),
127 it->value_size());
128 m_responseType = Response::toResponseType(temp);
129 it ++;
130 } else
131 {
132 throw Tlv::Error("not the ndn::ndns::tlv::ReponseType type");
133 }
134
135 if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::ResponseFressness)
136 {
137 m_freshness = time::milliseconds(readNonNegativeInteger(*it));
138 it ++;
139 } else
140 {
141 throw Tlv::Error("not the ndn::ndns::tlv::ReponseFreshness type");
142 }
143
144
145 if (it != m_wire.elements_end() && it->type() == ndn::ndns::tlv::ResponseContentBlob)
146 {
147 //Block b2 = it->value();/* to check */
148 Block b2 = *it;/* to check */
149
150 b2.parse();
151 Block::element_const_iterator it2 = b2.elements_begin();
152 size_t rrlen = 0;
153 if (it2 != b2.elements_end() && it2->type() == ndn::ndns::tlv::ResponseNumberOfRRData)
154 {
155 rrlen = readNonNegativeInteger(*it2);
156 it2 ++;
157 } else
158 {
159 throw Tlv::Error("not the ndn::ndns::tlv::ResponseNumberOfRRData type");
160 }
161 for (size_t i=0; i<rrlen; i++)
162 {
163 if (it2 != b2.elements_end() &&
164 it2->type() == ndn::ndns::tlv::RRData)
165 {
166 RR rr;
167 rr.wireDecode(*it2);
168 this->m_rrs.push_back(rr);
169 it2 ++;
170 } else
171 {
172 throw Tlv::Error("not the ndn::ndns::tlv::RRData type");
173 }
174 }
175
176
177 it ++;
178 } else
179 {
180 throw Tlv::Error("not the ndn::ndns::tlv::ResponseContentBlob type");
181 }
182
183
shockjiange9c1ab92014-07-21 12:02:52 -0700184
185}
186
187void
shockjianga5ae48c2014-07-27 23:21:41 -0700188Response::fromData(const Data& data)
shockjiange9c1ab92014-07-21 12:02:52 -0700189{
shockjiange9c1ab92014-07-21 12:02:52 -0700190
shockjianga5ae48c2014-07-27 23:21:41 -0700191 m_queryName = data.getName();
192 m_freshness = data.getFreshnessPeriod();
193 m_contentType = Query::QueryType(data.getContentType());
194 this->wireDecode(data.getContent());
195}
shockjiange9c1ab92014-07-21 12:02:52 -0700196
shockjiange9c1ab92014-07-21 12:02:52 -0700197
shockjianga5ae48c2014-07-27 23:21:41 -0700198void
199Response::fromData(const Name& name, const Data& data)
200{
201 fromData(data);
202}
shockjiange9c1ab92014-07-21 12:02:52 -0700203
shockjianga5ae48c2014-07-27 23:21:41 -0700204Data
205Response::toData() const
206{
207 Data data;
208 data.setName(m_queryName);
209 data.setFreshnessPeriod(this->m_freshness);
210 data.setContentType(m_contentType);
211 data.setContent(this->wireEncode());
shockjiange9c1ab92014-07-21 12:02:52 -0700212
shockjianga5ae48c2014-07-27 23:21:41 -0700213 return data;
shockjiange9c1ab92014-07-21 12:02:52 -0700214}
215
216
Alexander Afanasyev6e3d1ac2014-07-21 12:47:49 -0700217} // namespace ndns
218} // namespace ndn