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 | /* |
| 3 | * Copyright (c) 2014-2017, 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) |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 29 | , m_appContent(makeBinaryBlock(ndn::tlv::Content, reinterpret_cast<const uint8_t*>(0), 0)) |
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) |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 38 | , m_appContent(makeBinaryBlock(ndn::tlv::Content, reinterpret_cast<const uint8_t*>(0), 0)) |
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> |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 43 | inline size_t |
| 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; |
| 55 | for (std::vector<Block>::const_reverse_iterator iter = m_rrs.rbegin(); |
| 56 | iter != m_rrs.rend(); ++iter) { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 57 | totalLength += block.prependBlock(*iter); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | totalLength += block.prependVarNumber(totalLength); |
| 61 | totalLength += block.prependVarNumber(::ndn::tlv::Content); |
| 62 | |
| 63 | return totalLength; |
| 64 | } |
| 65 | |
| 66 | const Block |
| 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 | |
| 90 | Block::element_const_iterator iter = wire.elements().begin(); |
| 91 | for (; iter != wire.elements().end(); ++iter) { |
| 92 | m_rrs.push_back(*iter); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | bool |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 97 | Response::fromData(const Name& zone, const Data& data) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 98 | { |
| 99 | label::MatchResult re; |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 100 | if (!matchName(data, zone, re)) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 101 | return false; |
| 102 | |
| 103 | m_rrLabel = re.rrLabel; |
| 104 | m_rrType = re.rrType; |
| 105 | m_version = re.version; |
| 106 | |
| 107 | m_zone = zone; |
| 108 | size_t len = zone.size(); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 109 | m_queryType = data.getName().get(len); |
| 110 | |
| 111 | MetaInfo info = data.getMetaInfo(); |
| 112 | |
| 113 | m_freshnessPeriod = time::duration_cast<time::seconds>(info.getFreshnessPeriod()); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 114 | m_contentType = NdnsContentType(data.getContentType()); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 115 | |
| 116 | wireDecode(data.getContent()); |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | shared_ptr<Data> |
| 122 | Response::toData() |
| 123 | { |
| 124 | Name name; |
| 125 | name.append(m_zone) |
| 126 | .append(m_queryType) |
| 127 | .append(m_rrLabel) |
| 128 | .append(m_rrType); |
| 129 | |
| 130 | if (m_version.empty()) { |
| 131 | name.appendVersion(); |
| 132 | m_version = name.get(-1); |
| 133 | } |
| 134 | else { |
| 135 | name.append(m_version); |
| 136 | } |
| 137 | |
| 138 | shared_ptr<Data> data = make_shared<Data>(name); |
| 139 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 140 | if (m_contentType != NDNS_BLOB && m_contentType != NDNS_KEY) { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 141 | data->setContent(this->wireEncode()); |
| 142 | } |
| 143 | else { |
| 144 | data->setContent(m_appContent); |
| 145 | } |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 146 | data->setFreshnessPeriod(m_freshnessPeriod); |
| 147 | data->setContentType(m_contentType); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 148 | |
| 149 | return data; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | Response& |
| 154 | Response::addRr(const Block& rr) |
| 155 | { |
| 156 | this->m_rrs.push_back(rr); |
| 157 | return *this; |
| 158 | } |
| 159 | |
| 160 | Response& |
| 161 | Response::addRr(const std::string& rr) |
| 162 | { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 163 | return this->addRr(makeBinaryBlock(ndns::tlv::RrData, rr.c_str(), rr.size())); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool |
| 167 | Response::removeRr(const Block& rr) |
| 168 | { |
| 169 | for (std::vector<Block>::iterator iter = m_rrs.begin(); iter != m_rrs.end(); ++iter) { |
| 170 | if (*iter == rr) { |
| 171 | m_rrs.erase(iter); |
| 172 | return true; |
| 173 | } |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | Response::setAppContent(const Block& block) |
| 180 | { |
| 181 | if (block.type() != ndn::tlv::Content) { |
| 182 | m_appContent = Block(ndn::tlv::Content, block); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 183 | } |
| 184 | else { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 185 | m_appContent = block; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 186 | } |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 187 | |
| 188 | m_appContent.encode(); // this is a must |
| 189 | } |
| 190 | |
| 191 | |
| 192 | bool |
| 193 | Response::operator==(const Response& other) const |
| 194 | { |
| 195 | bool tmp = (getZone() == other.getZone() && |
| 196 | getQueryType() == other.getQueryType() && getRrLabel() == other.getRrLabel() && |
| 197 | getRrType() == other.getRrType() && getVersion() == other.getVersion() && |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 198 | getContentType() == other.getContentType()); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 199 | |
| 200 | if (tmp == false) |
| 201 | return tmp; |
| 202 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 203 | if (m_contentType == NDNS_BLOB || m_contentType == NDNS_KEY) { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 204 | return tmp && (getAppContent() == other.getAppContent()); |
| 205 | } |
| 206 | else |
| 207 | return tmp && getRrs() == other.getRrs(); |
| 208 | } |
| 209 | |
| 210 | std::ostream& |
| 211 | operator<<(std::ostream& os, const Response& response) |
| 212 | { |
| 213 | os << "Response: zone=" << response.getZone() |
| 214 | << " queryType=" << response.getQueryType() |
| 215 | << " rrLabel=" << response.getRrLabel() |
| 216 | << " rrType=" << response.getRrType() |
| 217 | << " version=" << response.getVersion() |
| 218 | << " freshnessPeriod=" << response.getFreshnessPeriod() |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 219 | << " NdnsContentType=" << response.getContentType(); |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 220 | if (response.getContentType() == NDNS_BLOB |
| 221 | || response.getContentType() == NDNS_KEY) { |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 222 | if (response.getAppContent().empty()) |
| 223 | os << " appContent=NULL"; |
| 224 | else |
| 225 | os << " appContentSize=" << response.getAppContent().size(); |
| 226 | } |
| 227 | else { |
| 228 | os << " rrs.size=" << response.getRrs().size(); |
| 229 | } |
| 230 | return os; |
| 231 | } |
| 232 | } // namespace ndns |
| 233 | } // namespace ndn |