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 | #ifndef NDNS_CLIENTS_RESPONSE_HPP |
| 21 | #define NDNS_CLIENTS_RESPONSE_HPP |
| 22 | |
| 23 | #include "ndns-tlv.hpp" |
| 24 | #include "ndns-enum.hpp" |
| 25 | #include "ndns-label.hpp" |
| 26 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/data.hpp> |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/meta-info.hpp> |
| 29 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | |
| 33 | /** |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 34 | * @brief Default life time of resource record |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 35 | */ |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 36 | const time::seconds DEFAULT_RR_FRESHNESS_PERIOD = 3600_s; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | /** |
| 40 | * @brief NDNS Response abstraction. Response is used on client side, |
| 41 | * while Rrset is used on server side, and Data packet is used during transmission in network. |
| 42 | */ |
| 43 | class Response |
| 44 | { |
| 45 | public: |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 46 | class Error : public ndn::tlv::Error |
| 47 | { |
| 48 | public: |
| 49 | using ndn::tlv::Error::Error; |
| 50 | }; |
| 51 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 52 | Response(); |
| 53 | |
| 54 | Response(const Name& zone, const name::Component& queryType); |
| 55 | |
| 56 | /** |
| 57 | * @brief fill the attributes from Data packet. |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 58 | * |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 59 | * @param zone NDNS zone name |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 60 | * @param data Data.getName() must the same zone as its prefix, |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 61 | * otherwise it's undefined behavior |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 62 | * @return false if Data.getName() does not follow the structure of NDNS Response without |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 63 | * changing any attributes, otherwise return true and fill the attributes |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 64 | */ |
| 65 | bool |
Yumin Xia | 6343c5b | 2016-10-20 15:45:50 -0700 | [diff] [blame] | 66 | fromData(const Name& zone, const Data& data); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 67 | |
| 68 | shared_ptr<Data> |
| 69 | toData(); |
| 70 | |
| 71 | Response& |
| 72 | addRr(const Block& rr); |
| 73 | |
| 74 | /** |
| 75 | * @brief add Block which contains string information and its tlv type is ndns::tlv::RrData |
Alexander Afanasyev | df2e939 | 2016-03-10 11:50:53 -0800 | [diff] [blame] | 76 | * |
| 77 | * @return Response that is service level information, the encoding level abstraction, |
| 78 | * i.e., Block is not very convenient. |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 79 | */ |
| 80 | Response& |
| 81 | addRr(const std::string& rr); |
| 82 | |
| 83 | bool |
| 84 | removeRr(const Block& rr); |
| 85 | |
| 86 | bool |
| 87 | operator==(const Response& other) const; |
| 88 | |
| 89 | bool |
| 90 | operator!=(const Response& other) const |
| 91 | { |
| 92 | return !(*this == other); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @brief encode the app-level data |
| 97 | */ |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 98 | Block |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 99 | wireEncode() const; |
| 100 | |
| 101 | /** |
| 102 | * @brief decode the app-level data |
| 103 | */ |
| 104 | void |
| 105 | wireDecode(const Block& wire); |
| 106 | |
| 107 | /** |
| 108 | * @brief encode app-level data |
| 109 | */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 110 | template<encoding::Tag T> |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 111 | size_t |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 112 | wireEncode(EncodingImpl<T>& encoder) const; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 113 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 114 | static std::pair<Name, Name> |
| 115 | wireDecodeDoe(const Block& wire); |
| 116 | |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 117 | public: // getter and setter |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 118 | const Name& |
| 119 | getZone() const |
| 120 | { |
| 121 | return m_zone; |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | setZone(const Name& zone) |
| 126 | { |
| 127 | m_zone = zone; |
| 128 | } |
| 129 | |
| 130 | const name::Component& |
| 131 | getQueryType() const |
| 132 | { |
| 133 | return m_queryType; |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | setQueryType(const name::Component& queryType) |
| 138 | { |
| 139 | m_queryType = queryType; |
| 140 | } |
| 141 | |
| 142 | const Name& |
| 143 | getRrLabel() const |
| 144 | { |
| 145 | return m_rrLabel; |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | setRrLabel(const Name& rrLabel) |
| 150 | { |
| 151 | m_rrLabel = rrLabel; |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | setRrType(const name::Component& rrType) |
| 156 | { |
| 157 | m_rrType = rrType; |
| 158 | } |
| 159 | |
| 160 | const name::Component& |
| 161 | getRrType() const |
| 162 | { |
| 163 | return m_rrType; |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | setVersion(const name::Component& version) |
| 168 | { |
| 169 | m_version = version; |
| 170 | } |
| 171 | |
| 172 | const name::Component& |
| 173 | getVersion() const |
| 174 | { |
| 175 | return m_version; |
| 176 | } |
| 177 | |
| 178 | void |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 179 | setContentType(NdnsContentType contentType) |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 180 | { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 181 | m_contentType = contentType; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 184 | NdnsContentType |
| 185 | getContentType() const |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 186 | { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 187 | return m_contentType; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | const Block& |
| 191 | getAppContent() const |
| 192 | { |
| 193 | return m_appContent; |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | setAppContent(const Block& block); |
| 198 | |
| 199 | const std::vector<Block>& |
| 200 | getRrs() const |
| 201 | { |
| 202 | return m_rrs; |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | setRrs(const std::vector<Block>& rrs) |
| 207 | { |
| 208 | m_rrs = rrs; |
| 209 | } |
| 210 | |
| 211 | time::seconds |
| 212 | getFreshnessPeriod() const |
| 213 | { |
| 214 | return m_freshnessPeriod; |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | setFreshnessPeriod(time::seconds freshnessPeriod) |
| 219 | { |
| 220 | m_freshnessPeriod = freshnessPeriod; |
| 221 | } |
| 222 | |
| 223 | private: |
| 224 | Name m_zone; |
| 225 | name::Component m_queryType; |
| 226 | Name m_rrLabel; |
| 227 | name::Component m_rrType; |
| 228 | name::Component m_version; |
| 229 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 230 | NdnsContentType m_contentType; |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 231 | time::seconds m_freshnessPeriod; |
| 232 | |
| 233 | /** |
| 234 | * @brief App content. Be valid only for NDNS-NULL Response |
| 235 | */ |
| 236 | Block m_appContent; |
| 237 | |
| 238 | /** |
| 239 | * @brief Content of Resource Record. Be valid only when this is not a NDNS-NULL Response |
| 240 | */ |
| 241 | std::vector<Block> m_rrs; |
| 242 | }; |
| 243 | |
| 244 | std::ostream& |
| 245 | operator<<(std::ostream& os, const Response& response); |
| 246 | |
| 247 | } // namespace ndns |
| 248 | } // namespace ndn |
| 249 | |
| 250 | #endif // NDNS_CLIENTS_RESPONSE_HPP |