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 "default-query-processor.hpp" |
| 23 | #include "encoding/pib-encoding.hpp" |
| 24 | |
| 25 | #include <boost/lexical_cast.hpp> |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace pib { |
| 29 | |
| 30 | using std::string; |
| 31 | |
| 32 | const size_t DefaultQueryProcessor::DEFAULT_QUERY_LENGTH = 5; |
| 33 | |
| 34 | DefaultQueryProcessor::DefaultQueryProcessor(PibDb& db) |
| 35 | : m_db(db) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | std::pair<bool, Block> |
| 40 | DefaultQueryProcessor::operator()(const Interest& interest) |
| 41 | { |
| 42 | const Name& interestName = interest.getName(); |
| 43 | |
| 44 | // handle pib query: /localhost/pib/[UserName]/default/param |
| 45 | if (interestName.size() != DEFAULT_QUERY_LENGTH) { |
| 46 | // malformed interest, discard |
| 47 | return std::make_pair(false, Block()); |
| 48 | } |
| 49 | |
| 50 | DefaultParam param; |
| 51 | |
| 52 | try { |
| 53 | param.wireDecode(interestName.get(OFFSET_PARAM).blockFromValue()); |
| 54 | } |
| 55 | catch (const tlv::Error& e) { |
| 56 | PibError error(ERR_WRONG_PARAM, "error in parsing param: " + string(e.what())); |
| 57 | return std::make_pair(true, error.wireEncode()); |
| 58 | } |
| 59 | |
| 60 | switch (param.getTargetType()) { |
| 61 | case TYPE_ID: |
| 62 | return processDefaultIdQuery(param); |
| 63 | case TYPE_KEY: |
| 64 | return processDefaultKeyQuery(param); |
| 65 | case TYPE_CERT: |
| 66 | return processDefaultCertQuery(param); |
| 67 | default: |
| 68 | { |
| 69 | PibError error(ERR_WRONG_PARAM, |
| 70 | "target type is not supported: " + |
| 71 | boost::lexical_cast<string>(param.getTargetType())); |
| 72 | return std::make_pair(true, error.wireEncode()); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | std::pair<bool, Block> |
| 78 | DefaultQueryProcessor::processDefaultIdQuery(const DefaultParam& param) |
| 79 | { |
| 80 | if (param.getOriginType() == TYPE_USER) { |
| 81 | try { |
| 82 | PibIdentity result(m_db.getDefaultIdentity()); |
| 83 | return std::make_pair(true, result.wireEncode()); |
| 84 | } |
| 85 | catch (PibDb::Error&) { |
| 86 | PibError error(ERR_NO_DEFAULT_ID, "default identity does not exist"); |
| 87 | return std::make_pair(true, error.wireEncode()); |
| 88 | } |
| 89 | } |
| 90 | else { |
| 91 | PibError error(ERR_WRONG_PARAM, |
| 92 | "origin type of id target must be USER(0), but gets: " + |
| 93 | boost::lexical_cast<string>(param.getOriginType())); |
| 94 | return std::make_pair(true, error.wireEncode()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | std::pair<bool, Block> |
| 99 | DefaultQueryProcessor::processDefaultKeyQuery(const DefaultParam& param) |
| 100 | { |
| 101 | Name identity; |
| 102 | if (param.getOriginType() == TYPE_ID) |
| 103 | identity = param.getOriginName(); |
| 104 | else if (param.getOriginType() == TYPE_USER) { |
| 105 | try { |
| 106 | identity = m_db.getDefaultIdentity(); |
| 107 | } |
| 108 | catch (PibDb::Error&) { |
| 109 | PibError error(ERR_NO_DEFAULT_ID, "default identity does not exist"); |
| 110 | return std::make_pair(true, error.wireEncode()); |
| 111 | } |
| 112 | } |
| 113 | else { |
| 114 | PibError error(ERR_WRONG_PARAM, |
| 115 | "origin type of key target must be ID(1) or USER(0), but gets: " + |
| 116 | boost::lexical_cast<string>(param.getOriginType())); |
| 117 | return std::make_pair(true, error.wireEncode()); |
| 118 | } |
| 119 | |
| 120 | try { |
| 121 | Name keyName = m_db.getDefaultKeyNameOfIdentity(identity); |
| 122 | shared_ptr<PublicKey> key = m_db.getKey(keyName); |
| 123 | |
| 124 | if (key == nullptr) { |
| 125 | PibError error(ERR_NO_DEFAULT_KEY, "default key does not exist"); |
| 126 | return std::make_pair(true, error.wireEncode()); |
| 127 | } |
| 128 | |
| 129 | PibPublicKey result(keyName, *key); |
| 130 | return std::make_pair(true, result.wireEncode()); |
| 131 | } |
| 132 | catch (PibDb::Error& e) { |
| 133 | PibError error(ERR_NO_DEFAULT_KEY, |
| 134 | "default key does not exist: " + string(e.what())); |
| 135 | return std::make_pair(true, error.wireEncode()); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | std::pair<bool, Block> |
| 140 | DefaultQueryProcessor::processDefaultCertQuery(const DefaultParam& param) |
| 141 | { |
| 142 | Name identity; |
| 143 | if (param.getOriginType() == TYPE_USER) { |
| 144 | try { |
| 145 | identity = m_db.getDefaultIdentity(); |
| 146 | } |
| 147 | catch (PibDb::Error&) { |
| 148 | PibError error(ERR_NO_DEFAULT_ID, "default identity does not exist"); |
| 149 | return std::make_pair(true, error.wireEncode()); |
| 150 | } |
| 151 | } |
| 152 | else if (param.getOriginType() == TYPE_ID) |
| 153 | identity = param.getOriginName(); |
| 154 | else if (param.getOriginType() != TYPE_KEY) { |
| 155 | PibError error(ERR_WRONG_PARAM, |
| 156 | "origin type of cert target must be KEY(2), ID(1) or USER(0), but gets: " + |
| 157 | boost::lexical_cast<string>(param.getOriginType())); |
| 158 | return std::make_pair(true, error.wireEncode()); |
| 159 | } |
| 160 | |
| 161 | Name keyName; |
| 162 | if (param.getOriginType() == TYPE_KEY) { |
| 163 | keyName = param.getOriginName(); |
| 164 | if (keyName.size() < 1) { |
| 165 | PibError error(ERR_WRONG_PARAM, |
| 166 | "key name must contain key id component"); |
| 167 | return std::make_pair(true, error.wireEncode()); |
| 168 | } |
| 169 | } |
| 170 | else { |
| 171 | try { |
| 172 | keyName = m_db.getDefaultKeyNameOfIdentity(identity); |
| 173 | } |
| 174 | catch (PibDb::Error&) { |
| 175 | PibError error(ERR_NO_DEFAULT_KEY, "default key does not exist"); |
| 176 | return std::make_pair(true, error.wireEncode()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | try { |
| 181 | Name certName = m_db.getDefaultCertNameOfKey(keyName); |
| 182 | shared_ptr<IdentityCertificate> certificate = m_db.getCertificate(certName); |
| 183 | |
| 184 | if (certificate == nullptr) { |
| 185 | PibError error (ERR_NO_DEFAULT_CERT, "default cert does not exist"); |
| 186 | return std::make_pair(true, error.wireEncode()); |
| 187 | } |
| 188 | |
| 189 | PibCertificate result(*certificate); |
| 190 | return std::make_pair(true, result.wireEncode()); |
| 191 | } |
| 192 | catch (PibDb::Error&) { |
| 193 | PibError error(ERR_NO_DEFAULT_CERT, "default cert does not exist"); |
| 194 | return std::make_pair(true, error.wireEncode()); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | } // namespace pib |
| 199 | } // namespace ndn |