blob: ce3c3fe22e00c611b13b884b7c58e2fe655e33cf [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 Zhang8f1ade32020-10-14 16:42:57 -070021#include "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
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070027ProbeEncoder::encodeApplicationParameters(std::vector<std::tuple<std::string, std::string>>&& parameters)
Suyong Won19fba4d2020-05-09 13:39:46 -070028{
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070029 auto content = makeEmptyBlock(ndn::tlv::ApplicationParameters);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070030 for (size_t i = 0; i < parameters.size(); ++i) {
tylerliu50d679e2020-10-14 14:08:39 -070031 content.push_back(makeStringBlock(tlv::ParameterKey, std::get<0>(parameters[i])));
32 content.push_back(makeStringBlock(tlv::ParameterValue, std::get<1>(parameters[i])));
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
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070038std::vector<std::tuple<std::string, std::string>>
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070039ProbeEncoder::decodeApplicationParameters(const Block& block)
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070040{
41 std::vector<std::tuple<std::string, std::string>> result;
42 block.parse();
43 for (size_t i = 0; i < block.elements().size() - 1; ++i) {
tylerliu50d679e2020-10-14 14:08:39 -070044 if (block.elements().at(i).type() == tlv::ParameterKey && block.elements().at(i + 1).type() == tlv::ParameterValue) {
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070045 result.push_back(std::make_tuple(readString(block.elements().at(i)), readString(block.elements().at(i + 1))));
46 }
47 }
48 return result;
49}
50
Suyong Won19fba4d2020-05-09 13:39:46 -070051Block
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070052ProbeEncoder::encodeDataContent(const std::vector<Name>& identifiers, boost::optional<size_t> maxSuffixLength,
tylerliua7bea662020-10-08 18:51:02 -070053 boost::optional<std::vector<std::shared_ptr<security::Certificate>>> redirectionItems)
Suyong Won19fba4d2020-05-09 13:39:46 -070054{
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070055 Block content = makeEmptyBlock(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) {
tylerliu50d679e2020-10-14 14:08:39 -070060 item.push_back(makeNonNegativeIntegerBlock(tlv::MaxSuffixLength, *maxSuffixLength));
tylerliub47dad72020-10-08 21:36:55 -070061 }
62 content.push_back(item);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070063 }
Zhiyi Zhange537dd52020-10-01 18:02:24 -070064 if (redirectionItems) {
65 for (const auto& item : *redirectionItems) {
tylerliu50d679e2020-10-14 14:08:39 -070066 content.push_back(makeNestedBlock(tlv::ProbeRedirect, item->getFullName()));
Zhiyi Zhange537dd52020-10-01 18:02:24 -070067 }
68 }
Suyong Won44d0cce2020-05-10 04:07:43 -070069 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070070 return content;
71}
72
Zhiyi Zhange537dd52020-10-01 18:02:24 -070073void
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070074ProbeEncoder::decodeDataContent(const Block& block,
tylerliub47dad72020-10-08 21:36:55 -070075 std::vector<std::pair<Name, int>>& availableNames,
Zhiyi Zhange537dd52020-10-01 18:02:24 -070076 std::vector<Name>& availableRedirection)
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070077{
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070078 block.parse();
79 for (const auto& item : block.elements()) {
tylerliu50d679e2020-10-14 14:08:39 -070080 if (item.type() == tlv::ProbeResponse) {
tylerliub47dad72020-10-08 21:36:55 -070081 item.parse();
82 Name elementName;
83 int maxSuffixLength = 0;
84 for (const auto& subBlock: item.elements()) {
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -070085 if (subBlock.type() == ndn::tlv::Name) {
tylerliub47dad72020-10-08 21:36:55 -070086 if (!elementName.empty()) {
tylerliu41c11532020-10-10 16:14:45 -070087 NDN_THROW(std::runtime_error("Invalid probe format"));
tylerliub47dad72020-10-08 21:36:55 -070088 }
89 elementName.wireDecode(subBlock);
tylerliu50d679e2020-10-14 14:08:39 -070090 } else if (subBlock.type() == tlv::MaxSuffixLength) {
tylerliub47dad72020-10-08 21:36:55 -070091 maxSuffixLength = readNonNegativeInteger(subBlock);
92 }
93 }
94 if (elementName.empty()) {
tylerliu41c11532020-10-10 16:14:45 -070095 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