blob: eae67cc13ea185b3d3279cb5f4f4a66f6e93090e [file] [log] [blame]
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob48bbda2020-07-27 19:41:37 -04002/*
Davide Pesavento0dc02012021-11-23 22:55:03 -05003 * Copyright (c) 2017-2021, Regents of the University of California.
Zhiyi Zhangf5246c42017-01-26 09:39:20 -08004 *
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
Zhiyi Zhangb041d442020-10-22 21:57:11 -070021#include "detail/ca-memory.hpp"
Davide Pesaventob48bbda2020-07-27 19:41:37 -040022
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080023namespace ndncert {
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070024namespace ca {
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080025
Davide Pesavento0dc02012021-11-23 22:55:03 -050026const std::string CaMemory::STORAGE_TYPE = "ca-storage-memory";
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080027NDNCERT_REGISTER_CA_STORAGE(CaMemory);
28
Zhiyi Zhangd1d9f5a2020-10-05 18:04:23 -070029CaMemory::CaMemory(const Name& caName, const std::string& path)
30 : CaStorage()
31{
32}
33
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070034RequestState
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070035CaMemory::getRequest(const RequestId& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080036{
37 auto search = m_requests.find(requestId);
38 if (search == m_requests.end()) {
Davide Pesavento0dc02012021-11-23 22:55:03 -050039 NDN_THROW(std::runtime_error("Request " + ndn::toHex(requestId.data(), requestId.size()) +
40 " does not exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080041 }
42 return search->second;
43}
44
45void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070046CaMemory::addRequest(const RequestState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080047{
tylerliu7b9185c2020-11-24 12:15:18 -080048 auto search = m_requests.find(request.requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080049 if (search == m_requests.end()) {
tylerliu7b9185c2020-11-24 12:15:18 -080050 m_requests.insert(std::make_pair(request.requestId, request));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080051 }
52 else {
Davide Pesavento0dc02012021-11-23 22:55:03 -050053 NDN_THROW(std::runtime_error("Request " + ndn::toHex(request.requestId.data(), request.requestId.size()) +
54 " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080055 }
56}
57
58void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070059CaMemory::updateRequest(const RequestState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080060{
tylerliu7b9185c2020-11-24 12:15:18 -080061 auto search = m_requests.find(request.requestId);
Zhiyi Zhang84207c52020-10-28 21:45:44 -070062 if (search == m_requests.end()) {
tylerliu7b9185c2020-11-24 12:15:18 -080063 m_requests.insert(std::make_pair(request.requestId, request));
Zhiyi Zhang84207c52020-10-28 21:45:44 -070064 }
65 else {
66 search->second = request;
67 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080068}
69
70void
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070071CaMemory::deleteRequest(const RequestId& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080072{
73 auto search = m_requests.find(requestId);
tylerliu7b9185c2020-11-24 12:15:18 -080074 auto keyName = search->second.cert.getKeyName();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080075 if (search != m_requests.end()) {
76 m_requests.erase(search);
77 }
78}
79
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070080std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070081CaMemory::listAllRequests()
82{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070083 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070084 for (const auto& entry : m_requests) {
85 result.push_back(entry.second);
86 }
87 return result;
88}
89
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070090std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070091CaMemory::listAllRequests(const Name& caName)
92{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070093 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070094 for (const auto& entry : m_requests) {
tylerliu7b9185c2020-11-24 12:15:18 -080095 if (entry.second.caPrefix == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070096 result.push_back(entry.second);
97 }
98 }
99 return result;
100}
101
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700102} // namespace ca
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800103} // namespace ndncert