Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, Regents of the University of California. |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 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 | |
Zhiyi Zhang | b041d44 | 2020-10-22 21:57:11 -0700 | [diff] [blame] | 21 | #include "detail/probe-encoder.hpp" |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 22 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 23 | namespace ndncert::probetlv { |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 24 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 25 | Block |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 26 | encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 27 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 28 | Block content(ndn::tlv::ApplicationParameters); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 29 | for (const auto& items : parameters) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 30 | content.push_back(ndn::makeStringBlock(tlv::ParameterKey, items.first)); |
| 31 | content.push_back(ndn::makeStringBlock(tlv::ParameterValue, items.second)); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 32 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 33 | content.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 34 | return content; |
| 35 | } |
| 36 | |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 37 | std::multimap<std::string, std::string> |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 38 | decodeApplicationParameters(const Block& block) |
| 39 | { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 40 | std::multimap<std::string, std::string> result; |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 41 | block.parse(); |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 42 | const auto& elements = block.elements(); |
| 43 | for (size_t i = 0; i < elements.size(); i++) { |
| 44 | if (i + 1 < elements.size() && elements[i].type() == tlv::ParameterKey && |
| 45 | elements[i + 1].type() == tlv::ParameterValue) { |
| 46 | result.emplace(readString(elements.at(i)), readString(elements.at(i + 1))); |
| 47 | i++; |
| 48 | } |
| 49 | else if (ndn::tlv::isCriticalType(elements[i].type())) { |
| 50 | NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(elements[i].type()))); |
| 51 | } |
| 52 | else { |
| 53 | //ignore |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | return result; |
| 57 | } |
| 58 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 59 | Block |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 60 | encodeDataContent(const std::vector<Name>& identifiers, std::optional<size_t> maxSuffixLength, |
| 61 | const std::vector<Name>& redirectionItems) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 62 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 63 | Block content(ndn::tlv::Content); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 64 | for (const auto& name : identifiers) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 65 | Block item(tlv::ProbeResponse); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 66 | item.push_back(name.wireEncode()); |
| 67 | if (maxSuffixLength) { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 68 | item.push_back(ndn::makeNonNegativeIntegerBlock(tlv::MaxSuffixLength, *maxSuffixLength)); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 69 | } |
| 70 | content.push_back(item); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 71 | } |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 72 | |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 73 | for (const auto& item : redirectionItems) { |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 74 | content.push_back(makeNestedBlock(tlv::ProbeRedirect, item)); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 75 | } |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 76 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 77 | content.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 78 | return content; |
| 79 | } |
| 80 | |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 81 | void |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 82 | decodeDataContent(const Block& block, std::vector<std::pair<Name, int>>& availableNames, |
| 83 | std::vector<Name>& availableRedirection) |
| 84 | { |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 85 | block.parse(); |
| 86 | for (const auto& item : block.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 87 | if (item.type() == tlv::ProbeResponse) { |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 88 | item.parse(); |
| 89 | Name elementName; |
| 90 | int maxSuffixLength = 0; |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 91 | for (const auto& subBlock : item.elements()) { |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 92 | if (subBlock.type() == ndn::tlv::Name) { |
| 93 | if (!elementName.empty()) { |
| 94 | NDN_THROW(std::runtime_error("Invalid probe format")); |
tylerliu | 6563f93 | 2020-10-30 11:13:38 -0700 | [diff] [blame] | 95 | } |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 96 | elementName.wireDecode(subBlock); |
| 97 | } |
| 98 | else if (subBlock.type() == tlv::MaxSuffixLength) { |
| 99 | maxSuffixLength = readNonNegativeInteger(subBlock); |
| 100 | } |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 101 | else if (ndn::tlv::isCriticalType(subBlock.type())) { |
| 102 | NDN_THROW(std::runtime_error("Unrecognized TLV Type in probe name item: " + std::to_string(subBlock.type()))); |
| 103 | } |
| 104 | else { |
| 105 | //ignore |
| 106 | } |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 107 | } |
| 108 | if (elementName.empty()) { |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 109 | NDN_THROW(std::runtime_error("Invalid probe format")); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 110 | } |
| 111 | availableNames.emplace_back(elementName, maxSuffixLength); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 112 | } |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 113 | else if (item.type() == tlv::ProbeRedirect) { |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 114 | availableRedirection.emplace_back(Name(item.blockFromValue())); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 115 | } |
Tianyuan Yu | 13aac73 | 2022-03-03 20:59:54 -0800 | [diff] [blame] | 116 | else if (ndn::tlv::isCriticalType(item.type())) { |
| 117 | NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type()))); |
| 118 | } |
| 119 | else { |
| 120 | //ignore |
| 121 | } |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 122 | } |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 125 | } // namespace ndncert::probetlv |