blob: aa7755f0ccae6af93f01b5da0d60432c2f30753e [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#include "../logging.hpp"
Zhiyi Zhangc87d52b2020-09-28 22:07:18 -070023#include <ndn-cxx/encoding/tlv.hpp>
24#include <boost/throw_exception.hpp>
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070025
Suyong Won19fba4d2020-05-09 13:39:46 -070026namespace 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);
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070051 std::vector<std::string> arguments = parseProbeComponents(probeInfo);
52 ;
Suyong Won19fba4d2020-05-09 13:39:46 -070053
54 if (arguments.size() != fields.size()) {
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070055 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 -070056 }
57
58 for (size_t i = 0; i < fields.size(); ++i) {
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070059 content.push_back(
60 makeStringBlock(tlv_parameter_key, fields.at(i)));
61 content.push_back(
62 makeStringBlock(tlv_parameter_value, arguments.at(i)));
Suyong Won19fba4d2020-05-09 13:39:46 -070063 }
Suyong Won44d0cce2020-05-10 04:07:43 -070064 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070065 return content;
66}
67
68// For CA
69Block
70PROBE::encodeDataContent(const Name& identifier, const std::string& m_probe, const Block& parameterTLV)
71{
72 std::vector<std::string> fields;
73 std::string delimiter = ":";
74 size_t last = 0;
75 size_t next = 0;
76 while ((next = m_probe.find(delimiter, last)) != std::string::npos) {
77 fields.push_back(m_probe.substr(last, next - last));
78 last = next + 1;
79 }
80 fields.push_back(m_probe.substr(last));
81
82 Block content = makeEmptyBlock(tlv::Content);
Zhiyi Zhang61c43a62020-09-28 12:56:01 -070083
Suyong Won19fba4d2020-05-09 13:39:46 -070084 // TODO: Currently have no mechanism to utilize the given params to determine name
85 //for (size_t i = 0; i < fields.size(); ++i) {
86 // root.put(fields.at(i), parameterJson.get(fields.at(i), ""));
87 //}
88
89 content.push_back(makeNestedBlock(tlv_probe_response, identifier));
90
91 // TODO: Must be determined based on CA config
92 content.push_back(makeEmptyBlock(tlv_allow_longer_name));
Suyong Won44d0cce2020-05-10 04:07:43 -070093 content.encode();
Suyong Won19fba4d2020-05-09 13:39:46 -070094 return content;
95}
96
97} // namespace ndncert
98} // namespace ndn