blob: 5417c8c37f21ff760290d07f035badd33fbe555c [file] [log] [blame]
Shock Jiang895bc1b2014-10-01 20:00:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Davide Pesavento98026122022-03-14 22:00:03 -04003 * Copyright (c) 2014-2022, Regents of the University of California.
Shock Jiang895bc1b2014-10-01 20:00:58 -07004 *
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 Jiang895bc1b2014-10-01 20:00:58 -070027#include <ndn-cxx/data.hpp>
Shock Jiang895bc1b2014-10-01 20:00:58 -070028#include <ndn-cxx/meta-info.hpp>
29
Shock Jiang895bc1b2014-10-01 20:00:58 -070030namespace ndn {
31namespace ndns {
32
33/**
Shock Jiangcde28712014-10-19 21:17:20 -070034 * @brief Default life time of resource record
Shock Jiang895bc1b2014-10-01 20:00:58 -070035 */
Davide Pesavento38fd3982022-04-18 22:22:02 -040036inline constexpr time::seconds DEFAULT_RR_FRESHNESS_PERIOD = 3600_s;
Shock Jiang895bc1b2014-10-01 20:00:58 -070037
38/**
39 * @brief NDNS Response abstraction. Response is used on client side,
40 * while Rrset is used on server side, and Data packet is used during transmission in network.
41 */
42class Response
43{
44public:
Yumin Xia55a7cc42017-05-14 18:43:34 -070045 class Error : public ndn::tlv::Error
46 {
47 public:
48 using ndn::tlv::Error::Error;
49 };
50
Shock Jiang895bc1b2014-10-01 20:00:58 -070051 Response();
52
53 Response(const Name& zone, const name::Component& queryType);
54
55 /**
56 * @brief fill the attributes from Data packet.
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080057 *
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080058 * @param zone NDNS zone name
Yumin Xia6343c5b2016-10-20 15:45:50 -070059 * @param data Data.getName() must the same zone as its prefix,
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080060 * otherwise it's undefined behavior
Shock Jiang895bc1b2014-10-01 20:00:58 -070061 * @return false if Data.getName() does not follow the structure of NDNS Response without
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080062 * changing any attributes, otherwise return true and fill the attributes
Shock Jiang895bc1b2014-10-01 20:00:58 -070063 */
64 bool
Yumin Xia6343c5b2016-10-20 15:45:50 -070065 fromData(const Name& zone, const Data& data);
Shock Jiang895bc1b2014-10-01 20:00:58 -070066
67 shared_ptr<Data>
68 toData();
69
70 Response&
71 addRr(const Block& rr);
72
73 /**
74 * @brief add Block which contains string information and its tlv type is ndns::tlv::RrData
Alexander Afanasyevdf2e9392016-03-10 11:50:53 -080075 *
76 * @return Response that is service level information, the encoding level abstraction,
77 * i.e., Block is not very convenient.
Shock Jiang895bc1b2014-10-01 20:00:58 -070078 */
79 Response&
80 addRr(const std::string& rr);
81
82 bool
83 removeRr(const Block& rr);
84
85 bool
86 operator==(const Response& other) const;
87
88 bool
89 operator!=(const Response& other) const
90 {
91 return !(*this == other);
92 }
93
94 /**
95 * @brief encode the app-level data
96 */
Davide Pesavento98026122022-03-14 22:00:03 -040097 Block
Shock Jiang895bc1b2014-10-01 20:00:58 -070098 wireEncode() const;
99
100 /**
101 * @brief decode the app-level data
102 */
103 void
104 wireDecode(const Block& wire);
105
106 /**
107 * @brief encode app-level data
108 */
Yumin Xia2c509c22017-02-09 14:37:36 -0800109 template<encoding::Tag T>
Shock Jiang895bc1b2014-10-01 20:00:58 -0700110 size_t
Davide Pesavento98026122022-03-14 22:00:03 -0400111 wireEncode(EncodingImpl<T>& encoder) const;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700112
Yumin Xia55a7cc42017-05-14 18:43:34 -0700113 static std::pair<Name, Name>
114 wireDecodeDoe(const Block& wire);
115
Davide Pesavento98026122022-03-14 22:00:03 -0400116public: // getter and setter
Shock Jiang895bc1b2014-10-01 20:00:58 -0700117 const Name&
118 getZone() const
119 {
120 return m_zone;
121 }
122
123 void
124 setZone(const Name& zone)
125 {
126 m_zone = zone;
127 }
128
129 const name::Component&
130 getQueryType() const
131 {
132 return m_queryType;
133 }
134
135 void
136 setQueryType(const name::Component& queryType)
137 {
138 m_queryType = queryType;
139 }
140
141 const Name&
142 getRrLabel() const
143 {
144 return m_rrLabel;
145 }
146
147 void
148 setRrLabel(const Name& rrLabel)
149 {
150 m_rrLabel = rrLabel;
151 }
152
153 void
154 setRrType(const name::Component& rrType)
155 {
156 m_rrType = rrType;
157 }
158
159 const name::Component&
160 getRrType() const
161 {
162 return m_rrType;
163 }
164
165 void
166 setVersion(const name::Component& version)
167 {
168 m_version = version;
169 }
170
171 const name::Component&
172 getVersion() const
173 {
174 return m_version;
175 }
176
177 void
Yumin Xiaa484ba72016-11-10 20:40:12 -0800178 setContentType(NdnsContentType contentType)
Shock Jiang895bc1b2014-10-01 20:00:58 -0700179 {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800180 m_contentType = contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700181 }
182
Yumin Xiaa484ba72016-11-10 20:40:12 -0800183 NdnsContentType
184 getContentType() const
Shock Jiang895bc1b2014-10-01 20:00:58 -0700185 {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800186 return m_contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700187 }
188
189 const Block&
190 getAppContent() const
191 {
192 return m_appContent;
193 }
194
195 void
196 setAppContent(const Block& block);
197
198 const std::vector<Block>&
199 getRrs() const
200 {
201 return m_rrs;
202 }
203
204 void
205 setRrs(const std::vector<Block>& rrs)
206 {
207 m_rrs = rrs;
208 }
209
210 time::seconds
211 getFreshnessPeriod() const
212 {
213 return m_freshnessPeriod;
214 }
215
216 void
217 setFreshnessPeriod(time::seconds freshnessPeriod)
218 {
219 m_freshnessPeriod = freshnessPeriod;
220 }
221
222private:
223 Name m_zone;
224 name::Component m_queryType;
225 Name m_rrLabel;
226 name::Component m_rrType;
227 name::Component m_version;
228
Yumin Xiaa484ba72016-11-10 20:40:12 -0800229 NdnsContentType m_contentType;
Shock Jiang895bc1b2014-10-01 20:00:58 -0700230 time::seconds m_freshnessPeriod;
231
232 /**
233 * @brief App content. Be valid only for NDNS-NULL Response
234 */
235 Block m_appContent;
236
237 /**
238 * @brief Content of Resource Record. Be valid only when this is not a NDNS-NULL Response
239 */
240 std::vector<Block> m_rrs;
241};
242
243std::ostream&
244operator<<(std::ostream& os, const Response& response);
245
246} // namespace ndns
247} // namespace ndn
248
249#endif // NDNS_CLIENTS_RESPONSE_HPP