blob: 2a3a71ed8ddbd6bfa2e12c236944d78e39f35f29 [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 Zhang61c43a62020-09-28 12:56:01 -070022
23#include <boost/throw_exception.hpp>
Suyong Won19fba4d2020-05-09 13:39:46 -070024#include <ndn-cxx/encoding/tlv.hpp>
25
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070026#include "../logging.hpp"
27
Suyong Won19fba4d2020-05-09 13:39:46 -070028namespace ndn {
29namespace ndncert {
30
31// For Client
32std::vector<std::string>
33PROBE::parseProbeComponents(const std::string& probe)
34{
35 std::vector<std::string> components;
36 std::string delimiter = ":";
37 size_t last = 0;
38 size_t next = 0;
39 while ((next = probe.find(delimiter, last)) != std::string::npos) {
40 components.push_back(probe.substr(last, next - last));
41 last = next + 1;
42 }
43 components.push_back(probe.substr(last));
44 return components;
45}
46
47Block
48PROBE::encodeApplicationParametersFromProbeInfo(const ClientCaItem& ca, const std::string& probeInfo)
49{
50 auto content = makeEmptyBlock(tlv::ApplicationParameters);
51
52 std::vector<std::string> fields = parseProbeComponents(ca.m_probe);
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070053 std::vector<std::string> arguments = parseProbeComponents(probeInfo);
54 ;
Suyong Won19fba4d2020-05-09 13:39:46 -070055
56 if (arguments.size() != fields.size()) {
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070057 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 -070058 }
59
60 for (size_t i = 0; i < fields.size(); ++i) {
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070061 content.push_back(
62 makeStringBlock(tlv_parameter_key, fields.at(i)));
63 content.push_back(
64 makeStringBlock(tlv_parameter_value, arguments.at(i)));
Suyong Won19fba4d2020-05-09 13:39:46 -070065 }
Suyong Won44d0cce2020-05-10 04:07:43 -070066 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070067 return content;
68}
69
70// For CA
71Block
72PROBE::encodeDataContent(const Name& identifier, const std::string& m_probe, const Block& parameterTLV)
73{
74 std::vector<std::string> fields;
75 std::string delimiter = ":";
76 size_t last = 0;
77 size_t next = 0;
78 while ((next = m_probe.find(delimiter, last)) != std::string::npos) {
79 fields.push_back(m_probe.substr(last, next - last));
80 last = next + 1;
81 }
82 fields.push_back(m_probe.substr(last));
83
84 Block content = makeEmptyBlock(tlv::Content);
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070085
Suyong Won19fba4d2020-05-09 13:39:46 -070086 // TODO: Currently have no mechanism to utilize the given params to determine name
87 //for (size_t i = 0; i < fields.size(); ++i) {
88 // root.put(fields.at(i), parameterJson.get(fields.at(i), ""));
89 //}
90
91 content.push_back(makeNestedBlock(tlv_probe_response, identifier));
92
93 // TODO: Must be determined based on CA config
94 content.push_back(makeEmptyBlock(tlv_allow_longer_name));
Suyong Won44d0cce2020-05-10 04:07:43 -070095 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070096 return content;
97}
98
99} // namespace ndncert
100} // namespace ndn