Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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() |
| 27 | : m_ndnsType(NDNS_RAW) |
| 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) |
| 36 | , m_ndnsType(NDNS_RAW) |
| 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 | |
| 42 | template<bool T> |
| 43 | inline size_t |
| 44 | Response::wireEncode(EncodingImpl<T>& block) const |
| 45 | { |
| 46 | if (m_ndnsType == NDNS_RAW) { |
| 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 | { |
| 69 | if (m_ndnsType == NDNS_RAW) { |
| 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 | { |
| 83 | if (m_ndnsType == NDNS_RAW) { |
| 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 |
| 97 | Response::fromData(const Name& hint, const Name& zone, const Data& data) |
| 98 | { |
| 99 | label::MatchResult re; |
| 100 | if (!matchName(data, hint, zone, re)) |
| 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(); |
| 109 | if (!hint.empty()) |
| 110 | len += hint.size() + 1; |
| 111 | m_queryType = data.getName().get(len); |
| 112 | |
| 113 | MetaInfo info = data.getMetaInfo(); |
| 114 | |
| 115 | m_freshnessPeriod = time::duration_cast<time::seconds>(info.getFreshnessPeriod()); |
| 116 | const Block* block = info.findAppMetaInfo(tlv::NdnsType); |
| 117 | if (block != 0) |
| 118 | m_ndnsType = static_cast<NdnsType>(readNonNegativeInteger(*block)); |
| 119 | |
| 120 | wireDecode(data.getContent()); |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | shared_ptr<Data> |
| 126 | Response::toData() |
| 127 | { |
| 128 | Name name; |
| 129 | name.append(m_zone) |
| 130 | .append(m_queryType) |
| 131 | .append(m_rrLabel) |
| 132 | .append(m_rrType); |
| 133 | |
| 134 | if (m_version.empty()) { |
| 135 | name.appendVersion(); |
| 136 | m_version = name.get(-1); |
| 137 | } |
| 138 | else { |
| 139 | name.append(m_version); |
| 140 | } |
| 141 | |
| 142 | shared_ptr<Data> data = make_shared<Data>(name); |
| 143 | |
| 144 | MetaInfo info; |
| 145 | info.setFreshnessPeriod(m_freshnessPeriod); |
| 146 | |
| 147 | if (m_ndnsType != NDNS_RAW) { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 148 | info.addAppMetaInfo(makeNonNegativeIntegerBlock(ndns::tlv::NdnsType, m_ndnsType)); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 149 | data->setContent(this->wireEncode()); |
| 150 | } |
| 151 | else { |
| 152 | data->setContent(m_appContent); |
| 153 | } |
| 154 | data->setMetaInfo(info); |
| 155 | |
| 156 | return data; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | Response& |
| 161 | Response::addRr(const Block& rr) |
| 162 | { |
| 163 | this->m_rrs.push_back(rr); |
| 164 | return *this; |
| 165 | } |
| 166 | |
| 167 | Response& |
| 168 | Response::addRr(const std::string& rr) |
| 169 | { |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 170 | return this->addRr(makeBinaryBlock(ndns::tlv::RrData, rr.c_str(), rr.size())); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool |
| 174 | Response::removeRr(const Block& rr) |
| 175 | { |
| 176 | for (std::vector<Block>::iterator iter = m_rrs.begin(); iter != m_rrs.end(); ++iter) { |
| 177 | if (*iter == rr) { |
| 178 | m_rrs.erase(iter); |
| 179 | return true; |
| 180 | } |
| 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | Response::setAppContent(const Block& block) |
| 187 | { |
| 188 | if (block.type() != ndn::tlv::Content) { |
| 189 | m_appContent = Block(ndn::tlv::Content, block); |
| 190 | } else |
| 191 | m_appContent = block; |
| 192 | |
| 193 | m_appContent.encode(); // this is a must |
| 194 | } |
| 195 | |
| 196 | |
| 197 | bool |
| 198 | Response::operator==(const Response& other) const |
| 199 | { |
| 200 | bool tmp = (getZone() == other.getZone() && |
| 201 | getQueryType() == other.getQueryType() && getRrLabel() == other.getRrLabel() && |
| 202 | getRrType() == other.getRrType() && getVersion() == other.getVersion() && |
| 203 | getNdnsType() == other.getNdnsType()); |
| 204 | |
| 205 | if (tmp == false) |
| 206 | return tmp; |
| 207 | |
| 208 | if (m_ndnsType == NDNS_RAW) { |
| 209 | return tmp && (getAppContent() == other.getAppContent()); |
| 210 | } |
| 211 | else |
| 212 | return tmp && getRrs() == other.getRrs(); |
| 213 | } |
| 214 | |
| 215 | std::ostream& |
| 216 | operator<<(std::ostream& os, const Response& response) |
| 217 | { |
| 218 | os << "Response: zone=" << response.getZone() |
| 219 | << " queryType=" << response.getQueryType() |
| 220 | << " rrLabel=" << response.getRrLabel() |
| 221 | << " rrType=" << response.getRrType() |
| 222 | << " version=" << response.getVersion() |
| 223 | << " freshnessPeriod=" << response.getFreshnessPeriod() |
| 224 | << " ndnsType=" << response.getNdnsType(); |
| 225 | if (response.getNdnsType() == NDNS_RAW) { |
| 226 | if (response.getAppContent().empty()) |
| 227 | os << " appContent=NULL"; |
| 228 | else |
| 229 | os << " appContentSize=" << response.getAppContent().size(); |
| 230 | } |
| 231 | else { |
| 232 | os << " rrs.size=" << response.getRrs().size(); |
| 233 | } |
| 234 | return os; |
| 235 | } |
| 236 | } // namespace ndns |
| 237 | } // namespace ndn |