blob: 13b040edb6483bb4c9b79acbe46bbc9019a03613 [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
Suyong Won19fba4d2020-05-09 13:39:46 -070023namespace ndncert {
24
Suyong Won19fba4d2020-05-09 13:39:46 -070025Block
tylerliu7b9185c2020-11-24 12:15:18 -080026probetlv::encodeApplicationParameters(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>
Tianyuan Yu13aac732022-03-03 20:59:54 -080038probetlv::decodeApplicationParameters(const Block& block) {
tylerliu40226332020-11-11 15:37:16 -080039 std::multimap<std::string, std::string> result;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070040 block.parse();
Tianyuan Yu13aac732022-03-03 20:59:54 -080041 const auto& elements = block.elements();
42 for (size_t i = 0; i < elements.size(); i++) {
43 if (i + 1 < elements.size() && elements[i].type() == tlv::ParameterKey &&
44 elements[i + 1].type() == tlv::ParameterValue) {
45 result.emplace(readString(elements.at(i)), readString(elements.at(i + 1)));
46 i++;
47 }
48 else if (ndn::tlv::isCriticalType(elements[i].type())) {
49 NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(elements[i].type())));
50 }
51 else {
52 //ignore
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070053 }
54 }
55 return result;
56}
57
Suyong Won19fba4d2020-05-09 13:39:46 -070058Block
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080059probetlv::encodeDataContent(const std::vector<Name>& identifiers, optional<size_t> maxSuffixLength,
Tianyuan Yu13aac732022-03-03 20:59:54 -080060 std::vector<ndn::Name> redirectionItems)
Suyong Won19fba4d2020-05-09 13:39:46 -070061{
tylerliu1f480be2020-11-10 13:02:53 -080062 Block content(ndn::tlv::Content);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070063 for (const auto& name : identifiers) {
tylerliu50d679e2020-10-14 14:08:39 -070064 Block item(tlv::ProbeResponse);
tylerliub47dad72020-10-08 21:36:55 -070065 item.push_back(name.wireEncode());
66 if (maxSuffixLength) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050067 item.push_back(ndn::makeNonNegativeIntegerBlock(tlv::MaxSuffixLength, *maxSuffixLength));
tylerliub47dad72020-10-08 21:36:55 -070068 }
69 content.push_back(item);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070070 }
Tianyuan Yu13aac732022-03-03 20:59:54 -080071
tylerliuf2e6bb52020-12-13 13:23:05 -080072 for (const auto& item : redirectionItems) {
Tianyuan Yu13aac732022-03-03 20:59:54 -080073 content.push_back(makeNestedBlock(tlv::ProbeRedirect, item));
Zhiyi Zhange537dd52020-10-01 18:02:24 -070074 }
Tianyuan Yu13aac732022-03-03 20:59:54 -080075
Suyong Won44d0cce2020-05-10 04:07:43 -070076 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070077 return content;
78}
79
Zhiyi Zhange537dd52020-10-01 18:02:24 -070080void
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080081probetlv::decodeDataContent(const Block& block,
Davide Pesavento0dc02012021-11-23 22:55:03 -050082 std::vector<std::pair<Name, int>>& availableNames,
Tianyuan Yu13aac732022-03-03 20:59:54 -080083 std::vector<Name>& availableRedirection) {
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070084 block.parse();
85 for (const auto& item : block.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -070086 if (item.type() == tlv::ProbeResponse) {
tylerliub47dad72020-10-08 21:36:55 -070087 item.parse();
88 Name elementName;
89 int maxSuffixLength = 0;
tylerliu1f480be2020-11-10 13:02:53 -080090 for (const auto& subBlock : item.elements()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080091 if (subBlock.type() == ndn::tlv::Name) {
92 if (!elementName.empty()) {
93 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliu6563f932020-10-30 11:13:38 -070094 }
tylerliuf2e6bb52020-12-13 13:23:05 -080095 elementName.wireDecode(subBlock);
96 }
97 else if (subBlock.type() == tlv::MaxSuffixLength) {
98 maxSuffixLength = readNonNegativeInteger(subBlock);
99 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800100 else if (ndn::tlv::isCriticalType(subBlock.type())) {
101 NDN_THROW(std::runtime_error("Unrecognized TLV Type in probe name item: " + std::to_string(subBlock.type())));
102 }
103 else {
104 //ignore
105 }
tylerliub47dad72020-10-08 21:36:55 -0700106 }
107 if (elementName.empty()) {
tylerliuf2e6bb52020-12-13 13:23:05 -0800108 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliub47dad72020-10-08 21:36:55 -0700109 }
110 availableNames.emplace_back(elementName, maxSuffixLength);
Zhiyi Zhange537dd52020-10-01 18:02:24 -0700111 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800112 else if (item.type() == tlv::ProbeRedirect) {
tylerliub47dad72020-10-08 21:36:55 -0700113 availableRedirection.emplace_back(Name(item.blockFromValue()));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700114 }
Tianyuan Yu13aac732022-03-03 20:59:54 -0800115 else if (ndn::tlv::isCriticalType(item.type())) {
116 NDN_THROW(std::runtime_error("Unrecognized TLV Type: " + std::to_string(item.type())));
117 }
118 else {
119 //ignore
120 }
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700121 }
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700122}
123
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700124} // namespace ndncert