blob: cdace7e923d074c2a5ffba19ab5fbf70f0f23780 [file] [log] [blame]
Suyong Won19fba4d2020-05-09 13:39:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Tianyuan Yu13aac732022-03-03 20:59:54 -08003 * Copyright (c) 2017-2022, Regents of the University of California.
Suyong Won19fba4d2020-05-09 13:39:46 -07004 *
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 Zhangb041d442020-10-22 21:57:11 -070021#include "detail/probe-encoder.hpp"
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070022
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040023namespace ndncert::probetlv {
Suyong Won19fba4d2020-05-09 13:39:46 -070024
Suyong Won19fba4d2020-05-09 13:39:46 -070025Block
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040026encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters)
Suyong Won19fba4d2020-05-09 13:39:46 -070027{
tylerliu1f480be2020-11-10 13:02:53 -080028 Block content(ndn::tlv::ApplicationParameters);
tylerliu40226332020-11-11 15:37:16 -080029 for (const auto& items : parameters) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050030 content.push_back(ndn::makeStringBlock(tlv::ParameterKey, items.first));
31 content.push_back(ndn::makeStringBlock(tlv::ParameterValue, items.second));
Suyong Won19fba4d2020-05-09 13:39:46 -070032 }
Suyong Won44d0cce2020-05-10 04:07:43 -070033 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070034 return content;
35}
36
tylerliu40226332020-11-11 15:37:16 -080037std::multimap<std::string, std::string>
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040038decodeApplicationParameters(const Block& block)
39{
tylerliu40226332020-11-11 15:37:16 -080040 std::multimap<std::string, std::string> result;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070041 block.parse();
Tianyuan Yu13aac732022-03-03 20:59:54 -080042 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 Zhang3e8ca252020-09-30 17:18:38 -070054 }
55 }
56 return result;
57}
58
Suyong Won19fba4d2020-05-09 13:39:46 -070059Block
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040060encodeDataContent(const std::vector<Name>& identifiers, std::optional<size_t> maxSuffixLength,
61 const std::vector<Name>& redirectionItems)
Suyong Won19fba4d2020-05-09 13:39:46 -070062{
tylerliu1f480be2020-11-10 13:02:53 -080063 Block content(ndn::tlv::Content);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070064 for (const auto& name : identifiers) {
tylerliu50d679e2020-10-14 14:08:39 -070065 Block item(tlv::ProbeResponse);
tylerliub47dad72020-10-08 21:36:55 -070066 item.push_back(name.wireEncode());
67 if (maxSuffixLength) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050068 item.push_back(ndn::makeNonNegativeIntegerBlock(tlv::MaxSuffixLength, *maxSuffixLength));
tylerliub47dad72020-10-08 21:36:55 -070069 }
70 content.push_back(item);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070071 }
Tianyuan Yu13aac732022-03-03 20:59:54 -080072
tylerliuf2e6bb52020-12-13 13:23:05 -080073 for (const auto& item : redirectionItems) {
Tianyuan Yu13aac732022-03-03 20:59:54 -080074 content.push_back(makeNestedBlock(tlv::ProbeRedirect, item));
Zhiyi Zhange537dd52020-10-01 18:02:24 -070075 }
Tianyuan Yu13aac732022-03-03 20:59:54 -080076
Suyong Won44d0cce2020-05-10 04:07:43 -070077 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070078 return content;
79}
80
Zhiyi Zhange537dd52020-10-01 18:02:24 -070081void
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040082decodeDataContent(const Block& block, std::vector<std::pair<Name, int>>& availableNames,
83 std::vector<Name>& availableRedirection)
84{
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070085 block.parse();
86 for (const auto& item : block.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -070087 if (item.type() == tlv::ProbeResponse) {
tylerliub47dad72020-10-08 21:36:55 -070088 item.parse();
89 Name elementName;
90 int maxSuffixLength = 0;
tylerliu1f480be2020-11-10 13:02:53 -080091 for (const auto& subBlock : item.elements()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080092 if (subBlock.type() == ndn::tlv::Name) {
93 if (!elementName.empty()) {
94 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliu6563f932020-10-30 11:13:38 -070095 }
tylerliuf2e6bb52020-12-13 13:23:05 -080096 elementName.wireDecode(subBlock);
97 }
98 else if (subBlock.type() == tlv::MaxSuffixLength) {
99 maxSuffixLength = readNonNegativeInteger(subBlock);
100 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800101 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 }
tylerliub47dad72020-10-08 21:36:55 -0700107 }
108 if (elementName.empty()) {
tylerliuf2e6bb52020-12-13 13:23:05 -0800109 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliub47dad72020-10-08 21:36:55 -0700110 }
111 availableNames.emplace_back(elementName, maxSuffixLength);
Zhiyi Zhange537dd52020-10-01 18:02:24 -0700112 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800113 else if (item.type() == tlv::ProbeRedirect) {
tylerliub47dad72020-10-08 21:36:55 -0700114 availableRedirection.emplace_back(Name(item.blockFromValue()));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700115 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800116 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 Zhang3e8ca252020-09-30 17:18:38 -0700122 }
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700123}
124
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400125} // namespace ndncert::probetlv