Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 4 | * |
| 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 | |
| 23 | namespace ndn { |
| 24 | namespace ndns { |
| 25 | |
| 26 | Response::Response() |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 27 | : m_contentType(NDNS_BLOB) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 28 | , m_freshnessPeriod(DEFAULT_RR_FRESHNESS_PERIOD) |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 29 | , m_appContent(makeEmptyBlock(ndn::tlv::Content)) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 30 | { |
| 31 | } |
| 32 | |
| 33 | Response::Response(const Name& zone, const name::Component& queryType) |
| 34 | : m_zone(zone) |
| 35 | , m_queryType(queryType) |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 36 | , m_contentType(NDNS_BLOB) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 37 | , m_freshnessPeriod(DEFAULT_RR_FRESHNESS_PERIOD) |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 38 | , m_appContent(makeEmptyBlock(ndn::tlv::Content)) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 42 | template<encoding::Tag T> |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 43 | size_t |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 44 | Response::wireEncode(EncodingImpl<T>& block) const |
| 45 | { |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 46 | if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 47 | // Raw application content |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 48 | return block.prependBlock(m_appContent); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Content :: = CONTENT-TYPE TLV-LENGTH |
| 52 | // Block* |
| 53 | |
| 54 | size_t totalLength = 0; |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 55 | for (auto iter = m_rrs.rbegin(); iter != m_rrs.rend(); ++iter) { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 56 | totalLength += block.prependBlock(*iter); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | totalLength += block.prependVarNumber(totalLength); |
| 60 | totalLength += block.prependVarNumber(::ndn::tlv::Content); |
| 61 | |
| 62 | return totalLength; |
| 63 | } |
| 64 | |
| 65 | const Block |
| 66 | Response::wireEncode() const |
| 67 | { |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 68 | if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 69 | return m_appContent; |
| 70 | } |
| 71 | |
| 72 | EncodingEstimator estimator; |
| 73 | size_t estimatedSize = wireEncode(estimator); |
| 74 | EncodingBuffer buffer(estimatedSize, 0); |
| 75 | wireEncode(buffer); |
| 76 | return buffer.block(); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Response::wireDecode(const Block& wire) |
| 81 | { |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 82 | if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 83 | m_appContent = wire; |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | wire.parse(); |
| 88 | |
| 89 | Block::element_const_iterator iter = wire.elements().begin(); |
| 90 | for (; iter != wire.elements().end(); ++iter) { |
| 91 | m_rrs.push_back(*iter); |
| 92 | } |
| 93 | } |
| 94 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 95 | std::pair<Name, Name> |
| 96 | Response::wireDecodeDoe(const Block& wire) |
| 97 | { |
| 98 | wire.parse(); |
| 99 | if (wire.elements().size() != 2) { |
| 100 | BOOST_THROW_EXCEPTION(Error("Unexpected number of components while decoding DOE record")); |
| 101 | } |
| 102 | return std::make_pair(Name(wire.elements().front()), Name(wire.elements().back())); |
| 103 | } |
| 104 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 105 | bool |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 106 | Response::fromData(const Name& zone, const Data& data) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 107 | { |
| 108 | label::MatchResult re; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 109 | if (!matchName(data, zone, re)) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 110 | return false; |
| 111 | |
| 112 | m_rrLabel = re.rrLabel; |
| 113 | m_rrType = re.rrType; |
| 114 | m_version = re.version; |
| 115 | |
| 116 | m_zone = zone; |
| 117 | size_t len = zone.size(); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 118 | m_queryType = data.getName().get(len); |
| 119 | |
| 120 | MetaInfo info = data.getMetaInfo(); |
| 121 | |
| 122 | m_freshnessPeriod = time::duration_cast<time::seconds>(info.getFreshnessPeriod()); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 123 | m_contentType = NdnsContentType(data.getContentType()); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 124 | |
| 125 | wireDecode(data.getContent()); |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | shared_ptr<Data> |
| 131 | Response::toData() |
| 132 | { |
| 133 | Name name; |
| 134 | name.append(m_zone) |
| 135 | .append(m_queryType) |
| 136 | .append(m_rrLabel) |
| 137 | .append(m_rrType); |
| 138 | |
| 139 | if (m_version.empty()) { |
| 140 | name.appendVersion(); |
| 141 | m_version = name.get(-1); |
| 142 | } |
| 143 | else { |
| 144 | name.append(m_version); |
| 145 | } |
| 146 | |
| 147 | shared_ptr<Data> data = make_shared<Data>(name); |
| 148 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 149 | if (m_contentType != NDNS_BLOB && m_contentType != NDNS_KEY) { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 150 | data->setContent(this->wireEncode()); |
| 151 | } |
| 152 | else { |
| 153 | data->setContent(m_appContent); |
| 154 | } |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 155 | data->setFreshnessPeriod(m_freshnessPeriod); |
| 156 | data->setContentType(m_contentType); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 157 | |
| 158 | return data; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | Response& |
| 163 | Response::addRr(const Block& rr) |
| 164 | { |
| 165 | this->m_rrs.push_back(rr); |
| 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | Response& |
| 170 | Response::addRr(const std::string& rr) |
| 171 | { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 172 | return this->addRr(makeBinaryBlock(ndns::tlv::RrData, rr.c_str(), rr.size())); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | bool |
| 176 | Response::removeRr(const Block& rr) |
| 177 | { |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 178 | for (auto iter = m_rrs.begin(); iter != m_rrs.end(); ++iter) { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 179 | if (*iter == rr) { |
| 180 | m_rrs.erase(iter); |
| 181 | return true; |
| 182 | } |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | Response::setAppContent(const Block& block) |
| 189 | { |
| 190 | if (block.type() != ndn::tlv::Content) { |
| 191 | m_appContent = Block(ndn::tlv::Content, block); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 192 | } |
| 193 | else { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 194 | m_appContent = block; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 195 | } |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 196 | |
| 197 | m_appContent.encode(); // this is a must |
| 198 | } |
| 199 | |
| 200 | |
| 201 | bool |
| 202 | Response::operator==(const Response& other) const |
| 203 | { |
| 204 | bool tmp = (getZone() == other.getZone() && |
| 205 | getQueryType() == other.getQueryType() && getRrLabel() == other.getRrLabel() && |
| 206 | getRrType() == other.getRrType() && getVersion() == other.getVersion() && |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 207 | getContentType() == other.getContentType()); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 208 | |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 209 | if (!tmp) |
| 210 | return false; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 211 | |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 212 | if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) |
| 213 | return getAppContent() == other.getAppContent(); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 214 | else |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 215 | return getRrs() == other.getRrs(); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | std::ostream& |
| 219 | operator<<(std::ostream& os, const Response& response) |
| 220 | { |
| 221 | os << "Response: zone=" << response.getZone() |
| 222 | << " queryType=" << response.getQueryType() |
| 223 | << " rrLabel=" << response.getRrLabel() |
| 224 | << " rrType=" << response.getRrType() |
| 225 | << " version=" << response.getVersion() |
| 226 | << " freshnessPeriod=" << response.getFreshnessPeriod() |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 227 | << " NdnsContentType=" << response.getContentType(); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 228 | if (response.getContentType() == NDNS_BLOB |
| 229 | || response.getContentType() == NDNS_KEY) { |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 230 | if (response.getAppContent().isValid()) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 231 | os << " appContentSize=" << response.getAppContent().size(); |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 232 | else |
| 233 | os << " appContent=NULL"; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 234 | } |
| 235 | else { |
| 236 | os << " rrs.size=" << response.getRrs().size(); |
| 237 | } |
| 238 | return os; |
| 239 | } |
Davide Pesavento | 1bff1b2 | 2020-06-08 18:46:05 -0400 | [diff] [blame^] | 240 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 241 | } // namespace ndns |
| 242 | } // namespace ndn |