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 | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 22 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 23 | namespace ndn { |
| 24 | namespace ndncert { |
| 25 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 26 | Block |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 27 | PROBE::encodeApplicationParameters(std::vector<std::tuple<std::string, std::string>>&& parameters) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 28 | { |
| 29 | auto content = makeEmptyBlock(tlv::ApplicationParameters); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 30 | for (size_t i = 0; i < parameters.size(); ++i) { |
| 31 | content.push_back(makeStringBlock(tlv_parameter_key, std::get<0>(parameters[i]))); |
| 32 | content.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(parameters[i]))); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 33 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 34 | content.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 35 | return content; |
| 36 | } |
| 37 | |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 38 | std::vector<std::tuple<std::string, std::string>> |
| 39 | PROBE::decodeApplicationParameters(const Block& block) |
| 40 | { |
| 41 | std::vector<std::tuple<std::string, std::string>> result; |
| 42 | block.parse(); |
| 43 | for (size_t i = 0; i < block.elements().size() - 1; ++i) { |
| 44 | if (block.elements().at(i).type() == tlv_parameter_key && block.elements().at(i + 1).type() == tlv_parameter_value) { |
| 45 | result.push_back(std::make_tuple(readString(block.elements().at(i)), readString(block.elements().at(i + 1)))); |
| 46 | } |
| 47 | } |
| 48 | return result; |
| 49 | } |
| 50 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 51 | Block |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame^] | 52 | PROBE::encodeDataContent(const std::vector<Name>& identifiers, boost::optional<size_t> maxSuffixLength, |
| 53 | boost::optional<std::vector<std::shared_ptr<security::v2::Certificate>>> redirectionItems) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 54 | { |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 55 | Block content = makeEmptyBlock(tlv::Content); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 56 | for (const auto& name : identifiers) { |
| 57 | content.push_back(makeNestedBlock(tlv_probe_response, name)); |
| 58 | } |
| 59 | if (maxSuffixLength) { |
| 60 | content.push_back(makeNonNegativeIntegerBlock(tlv_max_suffix_length, *maxSuffixLength)); |
| 61 | } |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame^] | 62 | if (redirectionItems) { |
| 63 | for (const auto& item : *redirectionItems) { |
| 64 | content.push_back(makeNestedBlock(tlv_probe_redirect, item->getFullName())); |
| 65 | } |
| 66 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 67 | content.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 68 | return content; |
| 69 | } |
| 70 | |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame^] | 71 | void |
| 72 | PROBE::decodeDataContent(const Block& block, |
| 73 | std::vector<Name>& availableNames, |
| 74 | std::vector<Name>& availableRedirection) |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 75 | { |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 76 | block.parse(); |
| 77 | for (const auto& item : block.elements()) { |
| 78 | if (item.type() == tlv_probe_response) { |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame^] | 79 | availableNames.push_back(Name(item.blockFromValue())); |
| 80 | } |
| 81 | if (item.type() == tlv_probe_redirect) { |
| 82 | availableRedirection.push_back(Name(item.blockFromValue())); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 87 | } // namespace ndncert |
| 88 | } // namespace ndn |