Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "probe.hpp" |
Zhiyi Zhang | 61c43a6 | 2020-09-28 12:56:01 -0700 | [diff] [blame^] | 22 | |
| 23 | #include <boost/throw_exception.hpp> |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/encoding/tlv.hpp> |
| 25 | |
Zhiyi Zhang | 61c43a6 | 2020-09-28 12:56:01 -0700 | [diff] [blame^] | 26 | #include "../logging.hpp" |
| 27 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace ndncert { |
| 30 | |
| 31 | // For Client |
| 32 | std::vector<std::string> |
| 33 | PROBE::parseProbeComponents(const std::string& probe) |
| 34 | { |
| 35 | std::vector<std::string> components; |
| 36 | std::string delimiter = ":"; |
| 37 | size_t last = 0; |
| 38 | size_t next = 0; |
| 39 | while ((next = probe.find(delimiter, last)) != std::string::npos) { |
| 40 | components.push_back(probe.substr(last, next - last)); |
| 41 | last = next + 1; |
| 42 | } |
| 43 | components.push_back(probe.substr(last)); |
| 44 | return components; |
| 45 | } |
| 46 | |
| 47 | Block |
| 48 | PROBE::encodeApplicationParametersFromProbeInfo(const ClientCaItem& ca, const std::string& probeInfo) |
| 49 | { |
| 50 | auto content = makeEmptyBlock(tlv::ApplicationParameters); |
| 51 | |
| 52 | std::vector<std::string> fields = parseProbeComponents(ca.m_probe); |
Zhiyi Zhang | 61c43a6 | 2020-09-28 12:56:01 -0700 | [diff] [blame^] | 53 | std::vector<std::string> arguments = parseProbeComponents(probeInfo); |
| 54 | ; |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 55 | |
| 56 | if (arguments.size() != fields.size()) { |
Zhiyi Zhang | 61c43a6 | 2020-09-28 12:56:01 -0700 | [diff] [blame^] | 57 | BOOST_THROW_EXCEPTION(std::runtime_error("Error in genProbeRequestJson: argument list does not match field list in the config file.")); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | for (size_t i = 0; i < fields.size(); ++i) { |
Zhiyi Zhang | 61c43a6 | 2020-09-28 12:56:01 -0700 | [diff] [blame^] | 61 | content.push_back( |
| 62 | makeStringBlock(tlv_parameter_key, fields.at(i))); |
| 63 | content.push_back( |
| 64 | makeStringBlock(tlv_parameter_value, arguments.at(i))); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 65 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 66 | content.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 67 | return content; |
| 68 | } |
| 69 | |
| 70 | // For CA |
| 71 | Block |
| 72 | PROBE::encodeDataContent(const Name& identifier, const std::string& m_probe, const Block& parameterTLV) |
| 73 | { |
| 74 | std::vector<std::string> fields; |
| 75 | std::string delimiter = ":"; |
| 76 | size_t last = 0; |
| 77 | size_t next = 0; |
| 78 | while ((next = m_probe.find(delimiter, last)) != std::string::npos) { |
| 79 | fields.push_back(m_probe.substr(last, next - last)); |
| 80 | last = next + 1; |
| 81 | } |
| 82 | fields.push_back(m_probe.substr(last)); |
| 83 | |
| 84 | Block content = makeEmptyBlock(tlv::Content); |
Zhiyi Zhang | 61c43a6 | 2020-09-28 12:56:01 -0700 | [diff] [blame^] | 85 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 86 | // TODO: Currently have no mechanism to utilize the given params to determine name |
| 87 | //for (size_t i = 0; i < fields.size(); ++i) { |
| 88 | // root.put(fields.at(i), parameterJson.get(fields.at(i), "")); |
| 89 | //} |
| 90 | |
| 91 | content.push_back(makeNestedBlock(tlv_probe_response, identifier)); |
| 92 | |
| 93 | // TODO: Must be determined based on CA config |
| 94 | content.push_back(makeEmptyBlock(tlv_allow_longer_name)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 95 | content.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 96 | return content; |
| 97 | } |
| 98 | |
| 99 | } // namespace ndncert |
| 100 | } // namespace ndn |