Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 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/>. |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 20 | #include "response.hpp" |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 21 | |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 22 | namespace ndn { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 23 | namespace ndns { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 25 | Response::Response() |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 26 | : m_freshness(time::milliseconds(3600)) |
| 27 | , m_contentType(Query::QUERY_DNS) |
| 28 | , m_responseType(NDNS_Resp) |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 29 | { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 32 | Response::~Response() |
| 33 | { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 34 | } |
| 35 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 36 | template<bool T> |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 37 | size_t Response::wireEncode(EncodingImpl<T> & block) const |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 38 | { |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 39 | size_t totalLength = 0; |
| 40 | std::vector<RR>::size_type lenOfRR = m_rrs.size(); |
| 41 | RR rr; |
| 42 | Block btemp; |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 43 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 44 | //totalLength += m_rrs[0].wireEncode(block); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 45 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 46 | 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 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 51 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 52 | totalLength += prependNonNegativeIntegerBlock(block, |
| 53 | ndn::ndns::tlv::ResponseNumberOfRRData, lenOfRR); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 54 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 55 | totalLength += block.prependVarNumber(totalLength); |
| 56 | totalLength += block.prependVarNumber(ndn::ndns::tlv::ResponseContentBlob); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 57 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 58 | totalLength += prependNonNegativeIntegerBlock(block, |
| 59 | ndn::ndns::tlv::ResponseFressness, m_freshness.count()); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 60 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 61 | 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()); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 64 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 65 | totalLength += m_queryName.wireEncode(block); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 66 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 67 | totalLength += block.prependVarNumber(totalLength); |
| 68 | totalLength += block.prependVarNumber(Tlv::Content); |
| 69 | return totalLength; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 70 | } |
| 71 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 72 | const Block& |
| 73 | Response::wireEncode() const |
| 74 | { |
| 75 | |
| 76 | if (m_wire.hasWire()) |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 77 | return m_wire; |
| 78 | EncodingEstimator estimator; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 79 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 80 | 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; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 86 | } |
| 87 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 88 | void Response::wireDecode(const Block& wire) |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 89 | { |
| 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 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 100 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 101 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 102 | if (it != m_wire.elements_end() && it->type() == ndn::Tlv::Name) { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 103 | m_queryName.wireDecode(*it); |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 104 | it++; |
| 105 | } else { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 106 | throw Tlv::Error("not the ndn::Tlv::Name type"); |
| 107 | } |
| 108 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 109 | if (it != m_wire.elements_end() |
| 110 | && it->type() == ndn::ndns::tlv::ResponseType) { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 111 | std::string temp = std::string(reinterpret_cast<const char*>(it->value()), |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 112 | it->value_size()); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 113 | m_responseType = Response::toResponseType(temp); |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 114 | it++; |
| 115 | } else { |
| 116 | throw Tlv::Error("not the ndn::ndns::tlv::ReponseType type"); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 117 | } |
| 118 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 119 | 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 | } |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 151 | } |
| 152 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 153 | it++; |
| 154 | } else { |
| 155 | throw Tlv::Error("not the ndn::ndns::tlv::ResponseContentBlob type"); |
| 156 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 157 | |
| 158 | } |
| 159 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 160 | void Response::fromData(const Data& data) |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 161 | { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 162 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 163 | m_queryName = data.getName(); |
| 164 | m_freshness = data.getFreshnessPeriod(); |
| 165 | m_contentType = Query::QueryType(data.getContentType()); |
| 166 | this->wireDecode(data.getContent()); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 167 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 168 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 169 | void Response::fromData(const Name& name, const Data& data) |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 170 | { |
| 171 | fromData(data); |
| 172 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 173 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 174 | Data Response::toData() const |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 175 | { |
| 176 | Data data; |
| 177 | data.setName(m_queryName); |
| 178 | data.setFreshnessPeriod(this->m_freshness); |
| 179 | data.setContentType(m_contentType); |
| 180 | data.setContent(this->wireEncode()); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 181 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 182 | return data; |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 185 | } // namespace ndns |
| 186 | } // namespace ndn |