Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 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_RR_HPP |
| 21 | #define NDNS_RR_HPP |
| 22 | |
| 23 | #include "ndns-tlv.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/encoding/block.hpp> |
| 26 | #include <ndn-cxx/interest.hpp> |
| 27 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
| 29 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 33 | class RR |
| 34 | { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 35 | public: |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 36 | |
| 37 | enum RRType |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 38 | { |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 39 | NS, |
| 40 | TXT, |
| 41 | FH, |
| 42 | A, |
| 43 | AAAA, |
| 44 | NDNCERT, |
| 45 | UNKNOWN |
| 46 | }; |
| 47 | static std::string toString(const RRType& type) |
| 48 | { |
| 49 | std::string str; |
| 50 | |
| 51 | switch (type) { |
| 52 | case NS: |
| 53 | str = "NS"; |
| 54 | break; |
| 55 | case TXT: |
| 56 | str = "TXT"; |
| 57 | break; |
| 58 | case FH: |
| 59 | str = "FH"; |
| 60 | break; |
| 61 | case A: |
| 62 | str = "A"; |
| 63 | break; |
| 64 | case AAAA: |
| 65 | str = "AAAA"; |
| 66 | break; |
| 67 | case NDNCERT: |
| 68 | str = "NDNCERT"; |
| 69 | break; |
| 70 | default: |
| 71 | str = "UNKNOWN"; |
| 72 | break; |
| 73 | } |
| 74 | return str; |
| 75 | } |
| 76 | |
| 77 | static RRType toRRType(const std::string& str) |
| 78 | { |
| 79 | RRType atype; |
| 80 | if (str == "NS") { |
| 81 | atype = NS; |
| 82 | } else if (str == "TXT") { |
| 83 | atype = TXT; |
| 84 | } else if (str == "FH") { |
| 85 | atype = FH; |
| 86 | } else if (str == "A") { |
| 87 | atype = A; |
| 88 | } else if (str == "AAAA") { |
| 89 | atype = AAAA; |
| 90 | } else if (str == "NDNCERT") { |
| 91 | atype = NDNCERT; |
| 92 | } |
| 93 | else { |
| 94 | atype = UNKNOWN; |
| 95 | } |
| 96 | return atype; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 99 | RR(); |
| 100 | virtual ~RR(); |
| 101 | |
| 102 | const std::string& |
| 103 | getRrdata() const |
| 104 | { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 106 | return m_rrData; |
| 107 | } |
| 108 | |
| 109 | void setRrdata(const std::string& rrdata) |
| 110 | { |
| 111 | this->m_rrData = rrdata; |
| 112 | } |
| 113 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 114 | public: |
| 115 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 116 | uint32_t getId() const |
| 117 | { |
| 118 | return m_id; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 119 | } |
| 120 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 121 | void setId(uint32_t id) |
| 122 | { |
| 123 | m_id = id; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 124 | } |
| 125 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 126 | const Block& getWire() const |
| 127 | { |
| 128 | return m_wire; |
| 129 | } |
| 130 | |
| 131 | void setWire(const Block& wire) |
| 132 | { |
| 133 | m_wire = wire; |
| 134 | } |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 135 | |
| 136 | inline bool operator==(const RR& rr) const |
| 137 | { |
| 138 | if (this->getRrdata() == rr.getRrdata()) |
| 139 | return true; |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 144 | template<bool T> |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 145 | inline size_t wireEncode(EncodingImpl<T> & block) const |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 146 | { |
| 147 | size_t totalLength = 0; |
| 148 | const std::string& msg = this->getRrdata(); |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 149 | totalLength += prependByteArrayBlock(block, ndn::ndns::tlv::RRDataSub2, |
| 150 | reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size()); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 151 | |
| 152 | totalLength += prependNonNegativeIntegerBlock(block, |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 153 | ndn::ndns::tlv::RRDataSub1, this->getId()); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 154 | |
| 155 | totalLength += block.prependVarNumber(totalLength); |
| 156 | totalLength += block.prependVarNumber(ndn::ndns::tlv::RRData); |
| 157 | //std::cout<<"call rr.h wireEncode"<<std::endl; |
| 158 | return totalLength; |
| 159 | } |
| 160 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 161 | const Block& |
| 162 | wireEncode() const; |
| 163 | |
| 164 | void |
| 165 | wireDecode(const Block& wire); |
| 166 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 167 | //inline std::ostream& operator<<(std::ostream& os, const RR& rr); |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 168 | |
| 169 | private: |
| 170 | uint32_t m_id; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 171 | //unsigned long m_id; |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 172 | std::string m_rrData; |
| 173 | |
| 174 | mutable Block m_wire; |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 175 | }; |
| 176 | //class RR |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 177 | |
| 178 | inline std::ostream& |
| 179 | operator<<(std::ostream& os, const RR& rr) |
| 180 | { |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame^] | 181 | os << "RR: Id=" << rr.getId() << " Data=" << rr.getRrdata(); |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 182 | return os; |
| 183 | } |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 184 | |
| 185 | } // namespace ndns |
| 186 | } // namespace ndn |
| 187 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame] | 188 | #endif // NDNS_RR_HPP |