blob: 922d4c785f258ca84862ffa7d6257f449d8620b3 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu0a312e52015-07-22 13:14:53 -07003 * Copyright (c) 2014-2015, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * 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 Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * 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 Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * 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 Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * 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 Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
21
22#include "get-query-processor.hpp"
23#include "encoding/pib-encoding.hpp"
24#include <boost/lexical_cast.hpp>
25
26namespace ndn {
27namespace pib {
28
29const Name GetQueryProcessor::PIB_PREFIX("/localhost/pib");
30const size_t GetQueryProcessor::GET_QUERY_LENGTH = 5;
31
32GetQueryProcessor::GetQueryProcessor(PibDb& db)
33 : m_db(db)
34{
35}
36
37std::pair<bool, Block>
38GetQueryProcessor::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
79std::pair<bool, Block>
80GetQueryProcessor::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
91std::pair<bool, Block>
92GetQueryProcessor::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
109std::pair<bool, Block>
110GetQueryProcessor::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
122std::pair<bool, Block>
123GetQueryProcessor::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