blob: c7b72d57d758095b61034921753f9197af80aa8e [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
21#include "probe.hpp"
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070022
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070023#include <boost/throw_exception.hpp>
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070024#include <ndn-cxx/encoding/tlv.hpp>
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070025
Suyong Won19fba4d2020-05-09 13:39:46 -070026namespace ndn {
27namespace ndncert {
28
29// For Client
Suyong Won19fba4d2020-05-09 13:39:46 -070030Block
Zhiyi Zhang9829da92020-09-30 16:19:34 -070031PROBE::encodeApplicationParameters(std::vector<std::tuple<std::string, std::string>>&& parameters)
Suyong Won19fba4d2020-05-09 13:39:46 -070032{
33 auto content = makeEmptyBlock(tlv::ApplicationParameters);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070034 for (size_t i = 0; i < parameters.size(); ++i) {
35 content.push_back(makeStringBlock(tlv_parameter_key, std::get<0>(parameters[i])));
36 content.push_back(makeStringBlock(tlv_parameter_value, std::get<1>(parameters[i])));
Suyong Won19fba4d2020-05-09 13:39:46 -070037 }
Suyong Won44d0cce2020-05-10 04:07:43 -070038 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070039 return content;
40}
41
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070042std::vector<std::tuple<std::string, std::string>>
43PROBE::decodeApplicationParameters(const Block& block)
44{
45 std::vector<std::tuple<std::string, std::string>> result;
46 block.parse();
47 for (size_t i = 0; i < block.elements().size() - 1; ++i) {
48 if (block.elements().at(i).type() == tlv_parameter_key && block.elements().at(i + 1).type() == tlv_parameter_value) {
49 result.push_back(std::make_tuple(readString(block.elements().at(i)), readString(block.elements().at(i + 1))));
50 }
51 }
52 return result;
53}
54
Suyong Won19fba4d2020-05-09 13:39:46 -070055Block
Zhiyi Zhang9829da92020-09-30 16:19:34 -070056PROBE::encodeDataContent(const std::vector<Name>& identifiers, boost::optional<size_t> maxSuffixLength)
Suyong Won19fba4d2020-05-09 13:39:46 -070057{
Suyong Won19fba4d2020-05-09 13:39:46 -070058 Block content = makeEmptyBlock(tlv::Content);
Zhiyi Zhang9829da92020-09-30 16:19:34 -070059 for (const auto& name : identifiers) {
60 content.push_back(makeNestedBlock(tlv_probe_response, name));
61 }
62 if (maxSuffixLength) {
63 content.push_back(makeNonNegativeIntegerBlock(tlv_max_suffix_length, *maxSuffixLength));
64 }
Suyong Won44d0cce2020-05-10 04:07:43 -070065 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070066 return content;
67}
68
Zhiyi Zhang3e8ca252020-09-30 17:18:38 -070069std::vector<Name>
70PROBE::decodeDataContent(const Block& block)
71{
72 std::vector<Name> result;
73 block.parse();
74 for (const auto& item : block.elements()) {
75 if (item.type() == tlv_probe_response) {
76 result.push_back(Name(item.blockFromValue()));
77 }
78 }
79 return result;
80}
81
Suyong Won19fba4d2020-05-09 13:39:46 -070082} // namespace ndncert
83} // namespace ndn