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