blob: bf51438c7e38071bd17285199bf5705848987739 [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/*
3 * Copyright (c) 2017-2021, 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>
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080038probetlv::decodeApplicationParameters(const Block& block)
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070039{
tylerliu40226332020-11-11 15:37:16 -080040 std::multimap<std::string, std::string> result;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070041 block.parse();
tylerliu1f480be2020-11-10 13:02:53 -080042 for (size_t i = 0; i < block.elements().size() - 1; i++) {
43 if (block.elements()[i].type() == tlv::ParameterKey && block.elements()[i + 1].type() == tlv::ParameterValue) {
tylerliu40226332020-11-11 15:37:16 -080044 result.emplace(readString(block.elements().at(i)), readString(block.elements().at(i + 1)));
tylerliu1f480be2020-11-10 13:02:53 -080045 i ++;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070046 }
47 }
48 return result;
49}
50
Suyong Won19fba4d2020-05-09 13:39:46 -070051Block
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080052probetlv::encodeDataContent(const std::vector<Name>& identifiers, optional<size_t> maxSuffixLength,
Davide Pesavento0dc02012021-11-23 22:55:03 -050053 std::vector<std::shared_ptr<Certificate>> redirectionItems)
Suyong Won19fba4d2020-05-09 13:39:46 -070054{
tylerliu1f480be2020-11-10 13:02:53 -080055 Block content(ndn::tlv::Content);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070056 for (const auto& name : identifiers) {
tylerliu50d679e2020-10-14 14:08:39 -070057 Block item(tlv::ProbeResponse);
tylerliub47dad72020-10-08 21:36:55 -070058 item.push_back(name.wireEncode());
59 if (maxSuffixLength) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050060 item.push_back(ndn::makeNonNegativeIntegerBlock(tlv::MaxSuffixLength, *maxSuffixLength));
tylerliub47dad72020-10-08 21:36:55 -070061 }
62 content.push_back(item);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070063 }
tylerliuf2e6bb52020-12-13 13:23:05 -080064 for (const auto& item : redirectionItems) {
65 content.push_back(makeNestedBlock(tlv::ProbeRedirect, item->getFullName()));
Zhiyi Zhange537dd52020-10-01 18:02:24 -070066 }
Suyong Won44d0cce2020-05-10 04:07:43 -070067 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070068 return content;
69}
70
Zhiyi Zhange537dd52020-10-01 18:02:24 -070071void
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080072probetlv::decodeDataContent(const Block& block,
Davide Pesavento0dc02012021-11-23 22:55:03 -050073 std::vector<std::pair<Name, int>>& availableNames,
74 std::vector<Name>& availableRedirection)
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070075{
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070076 block.parse();
77 for (const auto& item : block.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -070078 if (item.type() == tlv::ProbeResponse) {
tylerliub47dad72020-10-08 21:36:55 -070079 item.parse();
80 Name elementName;
81 int maxSuffixLength = 0;
tylerliu1f480be2020-11-10 13:02:53 -080082 for (const auto& subBlock : item.elements()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080083 if (subBlock.type() == ndn::tlv::Name) {
84 if (!elementName.empty()) {
85 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliu6563f932020-10-30 11:13:38 -070086 }
tylerliuf2e6bb52020-12-13 13:23:05 -080087 elementName.wireDecode(subBlock);
88 }
89 else if (subBlock.type() == tlv::MaxSuffixLength) {
90 maxSuffixLength = readNonNegativeInteger(subBlock);
91 }
tylerliub47dad72020-10-08 21:36:55 -070092 }
93 if (elementName.empty()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080094 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliub47dad72020-10-08 21:36:55 -070095 }
96 availableNames.emplace_back(elementName, maxSuffixLength);
Zhiyi Zhange537dd52020-10-01 18:02:24 -070097 }
tylerliu50d679e2020-10-14 14:08:39 -070098 if (item.type() == tlv::ProbeRedirect) {
tylerliub47dad72020-10-08 21:36:55 -070099 availableRedirection.emplace_back(Name(item.blockFromValue()));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700100 }
101 }
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700102}
103
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700104} // namespace ndncert