Yingdi Yu | 20e3a6e | 2014-05-26 23:16:10 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 The University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
| 21 | **/ |
| 22 | |
| 23 | #ifndef NLSR_VALIDATOR_HPP |
| 24 | #define NLSR_VALIDATOR_HPP |
| 25 | |
| 26 | #include <ndn-cxx/security/validator-config.hpp> |
| 27 | |
| 28 | namespace nlsr { |
| 29 | |
| 30 | class Validator : public ndn::ValidatorConfig |
| 31 | { |
| 32 | public: |
| 33 | class Error : public ndn::ValidatorConfig::Error |
| 34 | { |
| 35 | public: |
| 36 | explicit |
| 37 | Error(const std::string& what) |
| 38 | : ndn::ValidatorConfig::Error(what) |
| 39 | { |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | explicit |
| 44 | Validator(ndn::Face& face, |
| 45 | const ndn::Name broadcastPrefix, |
| 46 | const ndn::shared_ptr<ndn::CertificateCache>& cache, |
| 47 | const int stepLimit = 10) |
| 48 | : ndn::ValidatorConfig(face, cache, stepLimit) |
| 49 | , m_broadcastPrefix(broadcastPrefix) |
| 50 | { |
| 51 | m_broadcastPrefix.append("KEYS"); |
| 52 | } |
| 53 | |
| 54 | virtual |
| 55 | ~Validator() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | const ndn::Name& |
| 60 | getBroadcastPrefix() |
| 61 | { |
| 62 | return m_broadcastPrefix; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | setBroadcastPrefix(const ndn::Name& broadcastPrefix) |
| 67 | { |
| 68 | m_broadcastPrefix = broadcastPrefix; |
| 69 | } |
| 70 | |
| 71 | protected: |
| 72 | typedef std::vector<ndn::shared_ptr<ndn::ValidationRequest> > NextSteps; |
| 73 | |
| 74 | virtual void |
| 75 | checkPolicy(const ndn::Data& data, |
| 76 | int nSteps, |
| 77 | const ndn::OnDataValidated& onValidated, |
| 78 | const ndn::OnDataValidationFailed& onValidationFailed, |
| 79 | NextSteps& nextSteps) |
| 80 | { |
| 81 | ndn::ValidatorConfig::checkPolicy(data, nSteps, |
| 82 | onValidated, onValidationFailed, |
| 83 | nextSteps); |
| 84 | |
| 85 | for (NextSteps::iterator it = nextSteps.begin(); it != nextSteps.end(); it++) |
| 86 | { |
| 87 | ndn::Name broadcastName = m_broadcastPrefix; |
| 88 | broadcastName.append((*it)->m_interest.getName()); |
| 89 | |
| 90 | (*it)->m_interest.setName(broadcastName); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | virtual void |
| 95 | checkPolicy(const ndn::Interest& interest, |
| 96 | int nSteps, |
| 97 | const ndn::OnInterestValidated& onValidated, |
| 98 | const ndn::OnInterestValidationFailed& onValidationFailed, |
| 99 | NextSteps& nextSteps) |
| 100 | { |
| 101 | ndn::ValidatorConfig::checkPolicy(interest, nSteps, |
| 102 | onValidated, onValidationFailed, |
| 103 | nextSteps); |
| 104 | |
| 105 | for (NextSteps::iterator it = nextSteps.begin(); it != nextSteps.end(); it++) |
| 106 | { |
| 107 | ndn::Name broadcastName = m_broadcastPrefix; |
| 108 | broadcastName.append((*it)->m_interest.getName()); |
| 109 | |
| 110 | (*it)->m_interest.setName(broadcastName); |
| 111 | } |
| 112 | |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | onData(const ndn::Interest& interest, |
| 117 | const ndn::Data& data, |
| 118 | const ndn::shared_ptr<ndn::ValidationRequest>& nextStep) |
| 119 | { |
| 120 | |
| 121 | ndn::shared_ptr<ndn::Data> internalData = ndn::make_shared<ndn::Data>(); |
| 122 | internalData->wireDecode(data.getContent().blockFromValue()); |
| 123 | validate(*internalData, |
| 124 | nextStep->m_onValidated, nextStep->m_onDataValidated, nextStep->m_nSteps); |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | ndn::Name m_broadcastPrefix; |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | } // namespace nlsr |
| 133 | |
| 134 | #endif // NLSR_VALIDATOR_HPP |