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. |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 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/>. |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 20 | #include "query.hpp" |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 21 | |
| 22 | namespace ndn { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 23 | namespace ndns { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 24 | |
| 25 | Query::Query() |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 26 | : m_interestLifetime(time::milliseconds(4000)) |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 27 | { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | Query::~Query() { |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | |
| 34 | void |
| 35 | Query::fromWire(const Name &name, const Interest &interest) |
| 36 | { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 37 | Name interestName; |
| 38 | interestName = interest.getName(); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 40 | int qtflag = -1; |
| 41 | size_t len = interestName.size(); |
| 42 | for (size_t i=0; i<len; i++) |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 43 | { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 44 | std::string comp = interestName.get(i).toUri(); |
| 45 | if (comp == toString(QUERY_DNS) || comp == toString(QUERY_DNS_R)) |
| 46 | { |
| 47 | qtflag = i; |
| 48 | break; |
| 49 | } |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 50 | }//for |
| 51 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 52 | if (qtflag == -1) |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 53 | { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 54 | std::cerr << "There is no QueryType in the Interest Name: " << interestName << std::endl; |
| 55 | return; |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 56 | } |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 57 | this->m_queryType = toQueryType(interestName.get(qtflag).toUri()); |
| 58 | this->m_rrType = toRRType(interestName.get(len-1).toUri()); |
| 59 | this->m_authorityZone = interestName.getPrefix(qtflag); //the DNS/DNS-R is not included |
| 60 | this->m_interestLifetime = interest.getInterestLifetime(); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 61 | |
| 62 | } |
| 63 | |
| 64 | template<bool T> |
| 65 | size_t |
| 66 | Query::wireEncode(EncodingImpl<T>& block) const |
| 67 | { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 68 | size_t totalLength = 0; |
| 69 | totalLength += 0; |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | size_t totalLength = prependByteArrayBlock(block, tlv::Bytes, |
| 73 | m_key.get().buf(), m_key.get().size()); |
| 74 | totalLength += m_keyName.wireEncode(block); |
| 75 | totalLength += block.prependVarNumber(totalLength); |
| 76 | totalLength += block.prependVarNumber(tlv::PublicKey); |
| 77 | |
| 78 | return totalLength; |
| 79 | } |
| 80 | |
| 81 | Interest |
| 82 | Query::toWire() const |
| 83 | { |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 84 | Name name = this->m_authorityZone; |
| 85 | name.append(toString(this->m_queryType)); |
| 86 | name.append(this->m_rrLabel); |
| 87 | name.append(toString(this->m_rrType)); |
| 88 | Selectors selector; |
| 89 | //selector.setMustBeFresh(true); |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 91 | Interest interest = Interest(name, selector, -1, this->m_interestLifetime); |
| 92 | return interest; |
shockjiang | e9c1ab9 | 2014-07-21 12:02:52 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Alexander Afanasyev | 6e3d1ac | 2014-07-21 12:47:49 -0700 | [diff] [blame^] | 95 | } // namespace ndns |
| 96 | } // namespace ndn |