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 | a5ae48c | 2014-07-27 23:21:41 -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> |
| 37 | size_t |
| 38 | Response::wireEncode(EncodingImpl<T> & block) const |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 39 | { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 40 | size_t totalLength = 0; |
| 41 | std::vector<RR>::size_type lenOfRR = m_rrs.size(); |
| 42 | RR rr; |
| 43 | Block btemp; |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 44 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 45 | //totalLength += m_rrs[0].wireEncode(block); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 46 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 47 | for (std::vector<RR>::size_type i=0; i<lenOfRR; i++) |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 48 | { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 49 | rr = m_rrs[lenOfRR-i-1]; |
| 50 | totalLength += rr.wireEncode(block); |
| 51 | //std::cout<<"totalLenght="<<totalLength<<std::endl; |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 52 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 53 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 54 | totalLength += prependNonNegativeIntegerBlock(block, |
| 55 | ndn::ndns::tlv::ResponseNumberOfRRData, |
| 56 | lenOfRR); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 57 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 58 | 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 | |
| 82 | const Block& |
| 83 | Response::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 | |
| 99 | void |
| 100 | Response::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 | |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 184 | |
| 185 | } |
| 186 | |
| 187 | void |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 188 | Response::fromData(const Data& data) |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 189 | { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 190 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 191 | m_queryName = data.getName(); |
| 192 | m_freshness = data.getFreshnessPeriod(); |
| 193 | m_contentType = Query::QueryType(data.getContentType()); |
| 194 | this->wireDecode(data.getContent()); |
| 195 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 196 | |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 197 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 198 | void |
| 199 | Response::fromData(const Name& name, const Data& data) |
| 200 | { |
| 201 | fromData(data); |
| 202 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 203 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 204 | Data |
| 205 | Response::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()); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 212 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame^] | 213 | return data; |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 217 | } // namespace ndns |
| 218 | } // namespace ndn |