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 | |
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 | |
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 |
tylerliu | 7b9185c | 2020-11-24 12:15:18 -0800 | [diff] [blame] | 27 | probetlv::encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 28 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 29 | Block content(ndn::tlv::ApplicationParameters); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 30 | for (const auto& items : parameters) { |
| 31 | content.push_back(makeStringBlock(tlv::ParameterKey, items.first)); |
| 32 | content.push_back(makeStringBlock(tlv::ParameterValue, items.second)); |
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 | |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 38 | std::multimap<std::string, std::string> |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 39 | probetlv::decodeApplicationParameters(const Block& block) |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 40 | { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 41 | std::multimap<std::string, std::string> result; |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 42 | block.parse(); |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 43 | for (size_t i = 0; i < block.elements().size() - 1; i++) { |
| 44 | if (block.elements()[i].type() == tlv::ParameterKey && block.elements()[i + 1].type() == tlv::ParameterValue) { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 45 | result.emplace(readString(block.elements().at(i)), readString(block.elements().at(i + 1))); |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 46 | i ++; |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 52 | Block |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 53 | probetlv::encodeDataContent(const std::vector<Name>& identifiers, optional<size_t> maxSuffixLength, |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 54 | std::vector<std::shared_ptr<security::Certificate>> redirectionItems) |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 55 | { |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 56 | Block content(ndn::tlv::Content); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 57 | for (const auto& name : identifiers) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 58 | Block item(tlv::ProbeResponse); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 59 | item.push_back(name.wireEncode()); |
| 60 | if (maxSuffixLength) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 61 | item.push_back(makeNonNegativeIntegerBlock(tlv::MaxSuffixLength, *maxSuffixLength)); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 62 | } |
| 63 | content.push_back(item); |
Zhiyi Zhang | 9829da9 | 2020-09-30 16:19:34 -0700 | [diff] [blame] | 64 | } |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 65 | for (const auto& item : redirectionItems) { |
| 66 | content.push_back(makeNestedBlock(tlv::ProbeRedirect, item->getFullName())); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 67 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 68 | content.encode(); |
Suyong Won | 19fba4d | 2020-05-09 13:39:46 -0700 | [diff] [blame] | 69 | return content; |
| 70 | } |
| 71 | |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 72 | void |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 73 | probetlv::decodeDataContent(const Block& block, |
tylerliu | c4cf186 | 2020-11-10 11:49:59 -0800 | [diff] [blame] | 74 | std::vector<std::pair<Name, int>>& availableNames, |
| 75 | std::vector<Name>& availableRedirection) |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 76 | { |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 77 | block.parse(); |
| 78 | for (const auto& item : block.elements()) { |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 79 | if (item.type() == tlv::ProbeResponse) { |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 80 | item.parse(); |
| 81 | Name elementName; |
| 82 | int maxSuffixLength = 0; |
tylerliu | 1f480be | 2020-11-10 13:02:53 -0800 | [diff] [blame] | 83 | for (const auto& subBlock : item.elements()) { |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 84 | if (subBlock.type() == ndn::tlv::Name) { |
| 85 | if (!elementName.empty()) { |
| 86 | NDN_THROW(std::runtime_error("Invalid probe format")); |
tylerliu | 6563f93 | 2020-10-30 11:13:38 -0700 | [diff] [blame] | 87 | } |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 88 | elementName.wireDecode(subBlock); |
| 89 | } |
| 90 | else if (subBlock.type() == tlv::MaxSuffixLength) { |
| 91 | maxSuffixLength = readNonNegativeInteger(subBlock); |
| 92 | } |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 93 | } |
| 94 | if (elementName.empty()) { |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 95 | NDN_THROW(std::runtime_error("Invalid probe format")); |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 96 | } |
| 97 | availableNames.emplace_back(elementName, maxSuffixLength); |
Zhiyi Zhang | e537dd5 | 2020-10-01 18:02:24 -0700 | [diff] [blame] | 98 | } |
tylerliu | 50d679e | 2020-10-14 14:08:39 -0700 | [diff] [blame] | 99 | if (item.type() == tlv::ProbeRedirect) { |
tylerliu | b47dad7 | 2020-10-08 21:36:55 -0700 | [diff] [blame] | 100 | availableRedirection.emplace_back(Name(item.blockFromValue())); |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Zhiyi Zhang | 3e8ca25 | 2020-09-30 17:18:38 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Zhiyi Zhang | e4891b7 | 2020-10-10 15:11:57 -0700 | [diff] [blame] | 105 | } // namespace ndncert |
| 106 | } // namespace ndn |