blob: 34b769ed0a46dfc5fc7b1bfd022f6eff654d1916 [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 Zhangc87d52b2020-09-28 22:07:18 -070022#include <ndn-cxx/encoding/tlv.hpp>
23#include <boost/throw_exception.hpp>
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070024
Suyong Won19fba4d2020-05-09 13:39:46 -070025namespace ndn {
26namespace ndncert {
27
28// For Client
29std::vector<std::string>
30PROBE::parseProbeComponents(const std::string& probe)
31{
32 std::vector<std::string> components;
33 std::string delimiter = ":";
34 size_t last = 0;
35 size_t next = 0;
36 while ((next = probe.find(delimiter, last)) != std::string::npos) {
37 components.push_back(probe.substr(last, next - last));
38 last = next + 1;
39 }
40 components.push_back(probe.substr(last));
41 return components;
42}
43
44Block
45PROBE::encodeApplicationParametersFromProbeInfo(const ClientCaItem& ca, const std::string& probeInfo)
46{
47 auto content = makeEmptyBlock(tlv::ApplicationParameters);
48
49 std::vector<std::string> fields = parseProbeComponents(ca.m_probe);
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070050 std::vector<std::string> arguments = parseProbeComponents(probeInfo);
51 ;
Suyong Won19fba4d2020-05-09 13:39:46 -070052
53 if (arguments.size() != fields.size()) {
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070054 BOOST_THROW_EXCEPTION(std::runtime_error("Error in genProbeRequestJson: argument list does not match field list in the config file."));
Suyong Won19fba4d2020-05-09 13:39:46 -070055 }
56
57 for (size_t i = 0; i < fields.size(); ++i) {
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070058 content.push_back(
59 makeStringBlock(tlv_parameter_key, fields.at(i)));
60 content.push_back(
61 makeStringBlock(tlv_parameter_value, arguments.at(i)));
Suyong Won19fba4d2020-05-09 13:39:46 -070062 }
Suyong Won44d0cce2020-05-10 04:07:43 -070063 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070064 return content;
65}
66
67// For CA
68Block
69PROBE::encodeDataContent(const Name& identifier, const std::string& m_probe, const Block& parameterTLV)
70{
71 std::vector<std::string> fields;
72 std::string delimiter = ":";
73 size_t last = 0;
74 size_t next = 0;
75 while ((next = m_probe.find(delimiter, last)) != std::string::npos) {
76 fields.push_back(m_probe.substr(last, next - last));
77 last = next + 1;
78 }
79 fields.push_back(m_probe.substr(last));
80
81 Block content = makeEmptyBlock(tlv::Content);
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070082
Suyong Won19fba4d2020-05-09 13:39:46 -070083 // TODO: Currently have no mechanism to utilize the given params to determine name
84 //for (size_t i = 0; i < fields.size(); ++i) {
85 // root.put(fields.at(i), parameterJson.get(fields.at(i), ""));
86 //}
87
88 content.push_back(makeNestedBlock(tlv_probe_response, identifier));
89
90 // TODO: Must be determined based on CA config
91 content.push_back(makeEmptyBlock(tlv_allow_longer_name));
Suyong Won44d0cce2020-05-10 04:07:43 -070092 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070093 return content;
94}
95
96} // namespace ndncert
97} // namespace ndn