Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 4 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 8 | * ndn-tools 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. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 11 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 12 | * ndn-tools 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. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 15 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 18 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "get-query-processor.hpp" |
| 23 | #include "encoding/pib-encoding.hpp" |
| 24 | #include <boost/lexical_cast.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace pib { |
| 28 | |
| 29 | const Name GetQueryProcessor::PIB_PREFIX("/localhost/pib"); |
| 30 | const size_t GetQueryProcessor::GET_QUERY_LENGTH = 5; |
| 31 | |
| 32 | GetQueryProcessor::GetQueryProcessor(PibDb& db) |
| 33 | : m_db(db) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | std::pair<bool, Block> |
| 38 | GetQueryProcessor::operator()(const Interest& interest) |
| 39 | { |
| 40 | const Name& interestName = interest.getName(); |
| 41 | |
| 42 | // handle pib query: /localhost/pib/[UserName]/get/param |
| 43 | if (interestName.size() != GET_QUERY_LENGTH) { |
| 44 | // malformed interest, discard |
| 45 | return std::make_pair(false, Block()); |
| 46 | } |
| 47 | |
| 48 | GetParam param; |
| 49 | |
| 50 | try { |
| 51 | param.wireDecode(interestName.get(OFFSET_PARAM).blockFromValue()); |
| 52 | } |
| 53 | catch (const tlv::Error& e) { |
| 54 | PibError error(ERR_WRONG_PARAM, "error in parsing param: " + std::string(e.what())); |
| 55 | return std::make_pair(true, error.wireEncode()); |
| 56 | } |
| 57 | |
| 58 | switch (param.getTargetType()) { |
| 59 | case TYPE_ID: |
| 60 | return processGetIdQuery(param); |
| 61 | case TYPE_KEY: |
| 62 | return processGetKeyQuery(param); |
| 63 | case TYPE_CERT: |
| 64 | return processGetCertQuery(param); |
| 65 | case TYPE_USER: |
| 66 | if (interest.getName().get(2).toUri() != m_db.getOwnerName()) |
| 67 | return std::make_pair(false, Block()); |
| 68 | else |
| 69 | return processGetUserQuery(param); |
| 70 | default: |
| 71 | { |
| 72 | PibError error(ERR_WRONG_PARAM, "requested type is not supported:" + |
| 73 | boost::lexical_cast<std::string>(param.getTargetType())); |
| 74 | return std::make_pair(true, error.wireEncode()); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | std::pair<bool, Block> |
| 80 | GetQueryProcessor::processGetIdQuery(const GetParam& param) |
| 81 | { |
| 82 | if (!m_db.hasIdentity(param.getTargetName())) { |
| 83 | PibError error(ERR_NON_EXISTING_ID, "requested id does not exist"); |
| 84 | return std::make_pair(true, error.wireEncode()); |
| 85 | } |
| 86 | |
| 87 | PibIdentity result(param.getTargetName()); |
| 88 | return std::make_pair(true, result.wireEncode()); |
| 89 | } |
| 90 | |
| 91 | std::pair<bool, Block> |
| 92 | GetQueryProcessor::processGetKeyQuery(const GetParam& param) |
| 93 | { |
| 94 | if (param.getTargetName().empty()) { |
| 95 | PibError error(ERR_WRONG_PARAM, "key name does not have id-component"); |
| 96 | return std::make_pair(true, error.wireEncode()); |
| 97 | } |
| 98 | |
| 99 | shared_ptr<PublicKey> key = m_db.getKey(param.getTargetName()); |
| 100 | if (key == nullptr) { |
| 101 | PibError error(ERR_NON_EXISTING_KEY, "requested key does not exist"); |
| 102 | return std::make_pair(true, error.wireEncode()); |
| 103 | } |
| 104 | |
| 105 | PibPublicKey result(param.getTargetName(), *key); |
| 106 | return std::make_pair(true, result.wireEncode()); |
| 107 | } |
| 108 | |
| 109 | std::pair<bool, Block> |
| 110 | GetQueryProcessor::processGetCertQuery(const GetParam& param) |
| 111 | { |
| 112 | shared_ptr<IdentityCertificate> certificate = m_db.getCertificate(param.getTargetName()); |
| 113 | if (certificate == nullptr) { |
| 114 | PibError error(ERR_NON_EXISTING_CERT, "requested certificate does not exist"); |
| 115 | return std::make_pair(true, error.wireEncode()); |
| 116 | } |
| 117 | |
| 118 | PibCertificate result(*certificate); |
| 119 | return std::make_pair(true, result.wireEncode()); |
| 120 | } |
| 121 | |
| 122 | std::pair<bool, Block> |
| 123 | GetQueryProcessor::processGetUserQuery(const GetParam& param) |
| 124 | { |
| 125 | PibUser result; |
| 126 | result.setMgmtCert(*m_db.getMgmtCertificate()); |
| 127 | result.setTpmLocator(m_db.getTpmLocator()); |
| 128 | return std::make_pair(true, result.wireEncode()); |
| 129 | } |
| 130 | |
| 131 | } // namespace pib |
| 132 | } // namespace ndn |