blob: b3c5efd836212170edc9d8d17ed023545f07e2e3 [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 Pesavento6f1a2ab2022-03-17 03:57:21 -04003 * Copyright (c) 2017-2022, 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 Pesavento6f1a2ab2022-03-17 03:57:21 -040039 NDN_THROW(std::runtime_error("Request " + ndn::toHex(requestId) + " does not exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080040 }
41 return search->second;
42}
43
44void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070045CaMemory::addRequest(const RequestState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080046{
tylerliu7b9185c2020-11-24 12:15:18 -080047 auto search = m_requests.find(request.requestId);
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080048 if (search == m_requests.end()) {
tylerliu7b9185c2020-11-24 12:15:18 -080049 m_requests.insert(std::make_pair(request.requestId, request));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080050 }
51 else {
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -040052 NDN_THROW(std::runtime_error("Request " + ndn::toHex(request.requestId) + " already exists"));
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080053 }
54}
55
56void
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070057CaMemory::updateRequest(const RequestState& request)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080058{
tylerliu7b9185c2020-11-24 12:15:18 -080059 auto search = m_requests.find(request.requestId);
Zhiyi Zhang84207c52020-10-28 21:45:44 -070060 if (search == m_requests.end()) {
tylerliu7b9185c2020-11-24 12:15:18 -080061 m_requests.insert(std::make_pair(request.requestId, request));
Zhiyi Zhang84207c52020-10-28 21:45:44 -070062 }
63 else {
64 search->second = request;
65 }
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080066}
67
68void
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070069CaMemory::deleteRequest(const RequestId& requestId)
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080070{
71 auto search = m_requests.find(requestId);
tylerliu7b9185c2020-11-24 12:15:18 -080072 auto keyName = search->second.cert.getKeyName();
Zhiyi Zhangf5246c42017-01-26 09:39:20 -080073 if (search != m_requests.end()) {
74 m_requests.erase(search);
75 }
76}
77
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070078std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070079CaMemory::listAllRequests()
80{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070081 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070082 for (const auto& entry : m_requests) {
83 result.push_back(entry.second);
84 }
85 return result;
86}
87
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070088std::list<RequestState>
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070089CaMemory::listAllRequests(const Name& caName)
90{
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -070091 std::list<RequestState> result;
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070092 for (const auto& entry : m_requests) {
tylerliu7b9185c2020-11-24 12:15:18 -080093 if (entry.second.caPrefix == caName) {
Zhiyi Zhangae123bf2017-04-14 12:24:53 -070094 result.push_back(entry.second);
95 }
96 }
97 return result;
98}
99
Zhiyi Zhang32d4b4e2020-10-28 22:10:49 -0700100} // namespace ca
Zhiyi Zhangf5246c42017-01-26 09:39:20 -0800101} // namespace ndncert