blob: 24471a7bffc0594d8a1d79dfccecffc76f102bda [file] [log] [blame]
Suyong Won19fba4d2020-05-09 13:39:46 -07001/* -*- 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 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 ndn {
24namespace ndncert {
25
Suyong Won19fba4d2020-05-09 13:39:46 -070026Block
tylerliu7b9185c2020-11-24 12:15:18 -080027probetlv::encodeApplicationParameters(const std::multimap<std::string, std::string>& parameters)
Suyong Won19fba4d2020-05-09 13:39:46 -070028{
tylerliu1f480be2020-11-10 13:02:53 -080029 Block content(ndn::tlv::ApplicationParameters);
tylerliu40226332020-11-11 15:37:16 -080030 for (const auto& items : parameters) {
31 content.push_back(makeStringBlock(tlv::ParameterKey, items.first));
32 content.push_back(makeStringBlock(tlv::ParameterValue, items.second));
Suyong Won19fba4d2020-05-09 13:39:46 -070033 }
Suyong Won44d0cce2020-05-10 04:07:43 -070034 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070035 return content;
36}
37
tylerliu40226332020-11-11 15:37:16 -080038std::multimap<std::string, std::string>
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080039probetlv::decodeApplicationParameters(const Block& block)
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070040{
tylerliu40226332020-11-11 15:37:16 -080041 std::multimap<std::string, std::string> result;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070042 block.parse();
tylerliu1f480be2020-11-10 13:02:53 -080043 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) {
tylerliu40226332020-11-11 15:37:16 -080045 result.emplace(readString(block.elements().at(i)), readString(block.elements().at(i + 1)));
tylerliu1f480be2020-11-10 13:02:53 -080046 i ++;
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070047 }
48 }
49 return result;
50}
51
Suyong Won19fba4d2020-05-09 13:39:46 -070052Block
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080053probetlv::encodeDataContent(const std::vector<Name>& identifiers, optional<size_t> maxSuffixLength,
tylerliuf2e6bb52020-12-13 13:23:05 -080054 std::vector<std::shared_ptr<security::Certificate>> redirectionItems)
Suyong Won19fba4d2020-05-09 13:39:46 -070055{
tylerliu1f480be2020-11-10 13:02:53 -080056 Block content(ndn::tlv::Content);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070057 for (const auto& name : identifiers) {
tylerliu50d679e2020-10-14 14:08:39 -070058 Block item(tlv::ProbeResponse);
tylerliub47dad72020-10-08 21:36:55 -070059 item.push_back(name.wireEncode());
60 if (maxSuffixLength) {
tylerliu50d679e2020-10-14 14:08:39 -070061 item.push_back(makeNonNegativeIntegerBlock(tlv::MaxSuffixLength, *maxSuffixLength));
tylerliub47dad72020-10-08 21:36:55 -070062 }
63 content.push_back(item);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070064 }
tylerliuf2e6bb52020-12-13 13:23:05 -080065 for (const auto& item : redirectionItems) {
66 content.push_back(makeNestedBlock(tlv::ProbeRedirect, item->getFullName()));
Zhiyi Zhange537dd52020-10-01 18:02:24 -070067 }
Suyong Won44d0cce2020-05-10 04:07:43 -070068 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070069 return content;
70}
71
Zhiyi Zhange537dd52020-10-01 18:02:24 -070072void
Zhiyi Zhangf22ae242020-11-17 10:51:15 -080073probetlv::decodeDataContent(const Block& block,
tylerliuc4cf1862020-11-10 11:49:59 -080074 std::vector<std::pair<Name, int>>& availableNames,
75 std::vector<Name>& availableRedirection)
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070076{
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070077 block.parse();
78 for (const auto& item : block.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -070079 if (item.type() == tlv::ProbeResponse) {
tylerliub47dad72020-10-08 21:36:55 -070080 item.parse();
81 Name elementName;
82 int maxSuffixLength = 0;
tylerliu1f480be2020-11-10 13:02:53 -080083 for (const auto& subBlock : item.elements()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080084 if (subBlock.type() == ndn::tlv::Name) {
85 if (!elementName.empty()) {
86 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliu6563f932020-10-30 11:13:38 -070087 }
tylerliuf2e6bb52020-12-13 13:23:05 -080088 elementName.wireDecode(subBlock);
89 }
90 else if (subBlock.type() == tlv::MaxSuffixLength) {
91 maxSuffixLength = readNonNegativeInteger(subBlock);
92 }
tylerliub47dad72020-10-08 21:36:55 -070093 }
94 if (elementName.empty()) {
tylerliuf2e6bb52020-12-13 13:23:05 -080095 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliub47dad72020-10-08 21:36:55 -070096 }
97 availableNames.emplace_back(elementName, maxSuffixLength);
Zhiyi Zhange537dd52020-10-01 18:02:24 -070098 }
tylerliu50d679e2020-10-14 14:08:39 -070099 if (item.type() == tlv::ProbeRedirect) {
tylerliub47dad72020-10-08 21:36:55 -0700100 availableRedirection.emplace_back(Name(item.blockFromValue()));
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700101 }
102 }
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -0700103}
104
Zhiyi Zhange4891b72020-10-10 15:11:57 -0700105} // namespace ndncert
106} // namespace ndn