blob: b8034b11d52ec559372f134ed7bddd3f5facfacc [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"
22#include "boost/throw_exception.hpp"
23#include "../logging.hpp"
24#include <ndn-cxx/encoding/tlv.hpp>
25
26namespace ndn {
27namespace ndncert {
28
29// For Client
30std::vector<std::string>
31PROBE::parseProbeComponents(const std::string& probe)
32{
33 std::vector<std::string> components;
34 std::string delimiter = ":";
35 size_t last = 0;
36 size_t next = 0;
37 while ((next = probe.find(delimiter, last)) != std::string::npos) {
38 components.push_back(probe.substr(last, next - last));
39 last = next + 1;
40 }
41 components.push_back(probe.substr(last));
42 return components;
43}
44
45Block
46PROBE::encodeApplicationParametersFromProbeInfo(const ClientCaItem& ca, const std::string& probeInfo)
47{
48 auto content = makeEmptyBlock(tlv::ApplicationParameters);
49
50 std::vector<std::string> fields = parseProbeComponents(ca.m_probe);
51 std::vector<std::string> arguments = parseProbeComponents(probeInfo);;
52
53 if (arguments.size() != fields.size()) {
54 BOOST_THROW_EXCEPTION(Error("Error in genProbeRequestJson: argument list does not match field list in the config file."));
55 }
56
57 for (size_t i = 0; i < fields.size(); ++i) {
58 content.push_back(
59 makeStringBlock(tlv_parameter_key, fields.at(i))
60 );
61 content.push_back(
62 makeStringBlock(tlv_parameter_value, arguments.at(i))
63 );
64 }
65 content.parse();
66 return content;
67}
68
69// For CA
70Block
71PROBE::encodeDataContent(const Name& identifier, const std::string& m_probe, const Block& parameterTLV)
72{
73 std::vector<std::string> fields;
74 std::string delimiter = ":";
75 size_t last = 0;
76 size_t next = 0;
77 while ((next = m_probe.find(delimiter, last)) != std::string::npos) {
78 fields.push_back(m_probe.substr(last, next - last));
79 last = next + 1;
80 }
81 fields.push_back(m_probe.substr(last));
82
83 Block content = makeEmptyBlock(tlv::Content);
84
85 // TODO: Currently have no mechanism to utilize the given params to determine name
86 //for (size_t i = 0; i < fields.size(); ++i) {
87 // root.put(fields.at(i), parameterJson.get(fields.at(i), ""));
88 //}
89
90 content.push_back(makeNestedBlock(tlv_probe_response, identifier));
91
92 // TODO: Must be determined based on CA config
93 content.push_back(makeEmptyBlock(tlv_allow_longer_name));
94 content.parse();
95 return content;
96}
97
98} // namespace ndncert
99} // namespace ndn