blob: 31f8255d4f99d70405817b2db67246cfdb0c3961 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu7640cb32014-01-29 20:00:50 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library 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 Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu7640cb32014-01-29 20:00:50 -080022 */
23
Yingdi Yufc40d872014-02-18 12:56:04 -080024#ifndef NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP
25#define NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP
Yingdi Yu7640cb32014-01-29 20:00:50 -080026
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080027#include "../common.hpp"
Yingdi Yu7640cb32014-01-29 20:00:50 -080028#include "certificate-cache.hpp"
29#include "../util/scheduler.hpp"
Yingdi Yu7640cb32014-01-29 20:00:50 -080030
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080031namespace ndn {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070032
Yingdi Yu7640cb32014-01-29 20:00:50 -080033class CertificateCacheTtl : public CertificateCache
34{
35public:
Yingdi Yu58f33712014-04-16 16:57:47 -070036 CertificateCacheTtl(boost::asio::io_service& io,
37 const time::seconds& defaultTtl = time::seconds(3600))
38 : m_defaultTtl(defaultTtl)
39 , m_scheduler(io)
40 {
41 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042
Yingdi Yu7640cb32014-01-29 20:00:50 -080043 virtual
Yingdi Yu58f33712014-04-16 16:57:47 -070044 ~CertificateCacheTtl()
45 {
46 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070047
Yingdi Yu58f33712014-04-16 16:57:47 -070048 virtual inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070049 insertCertificate(shared_ptr<const IdentityCertificate> certificate);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070050
Yingdi Yu58f33712014-04-16 16:57:47 -070051 virtual inline shared_ptr<const IdentityCertificate>
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 getCertificate(const Name& certificateNameWithoutVersion);
Yingdi Yu7640cb32014-01-29 20:00:50 -080053
Yingdi Yu58f33712014-04-16 16:57:47 -070054 virtual inline void
55 reset();
56
57 virtual inline size_t
58 getSize();
59
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060private:
Yingdi Yu58f33712014-04-16 16:57:47 -070061 inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070062 insert(shared_ptr<const IdentityCertificate> certificate);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070063
Yingdi Yu58f33712014-04-16 16:57:47 -070064 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065 remove(const Name& certificateName);
Yingdi Yu7640cb32014-01-29 20:00:50 -080066
Yingdi Yu58f33712014-04-16 16:57:47 -070067 inline void
68 removeAll();
69
Yingdi Yu7640cb32014-01-29 20:00:50 -080070protected:
Yingdi Yu58f33712014-04-16 16:57:47 -070071 typedef std::map<Name, std::pair<shared_ptr<const IdentityCertificate>, EventId> > Cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080072
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 time::seconds m_defaultTtl;
Yingdi Yu7640cb32014-01-29 20:00:50 -080074 Cache m_cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080075 Scheduler m_scheduler;
76};
77
Yingdi Yu58f33712014-04-16 16:57:47 -070078inline void
79CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
80{
81 m_scheduler.scheduleEvent(time::seconds(0),
82 bind(&CertificateCacheTtl::insert, this, certificate));
83}
84
85inline shared_ptr<const IdentityCertificate>
86CertificateCacheTtl::getCertificate(const Name& certificateName)
87{
88 Cache::iterator it = m_cache.find(certificateName);
89 if (it != m_cache.end())
90 return it->second.first;
91 else
92 return shared_ptr<IdentityCertificate>();
93}
94
95inline void
96CertificateCacheTtl::reset()
97{
98 m_scheduler.scheduleEvent(time::seconds(0),
99 bind(&CertificateCacheTtl::removeAll, this));
100}
101
102inline size_t
103CertificateCacheTtl::getSize()
104{
105 return m_cache.size();
106}
107
108inline void
109CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
110{
111 time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
112 certificate->getFreshnessPeriod() : m_defaultTtl);
113
114 Name index = certificate->getName().getPrefix(-1);
115
116 Cache::iterator it = m_cache.find(index);
117 if (it != m_cache.end())
118 m_scheduler.cancelEvent(it->second.second);
119
120 EventId eventId = m_scheduler.scheduleEvent(expire,
121 bind(&CertificateCacheTtl::remove,
122 this, certificate->getName()));
123
124 m_cache[index] = std::make_pair(certificate, eventId);
125}
126
127inline void
128CertificateCacheTtl::remove(const Name& certificateName)
129{
130 Name name = certificateName.getPrefix(-1);
131 Cache::iterator it = m_cache.find(name);
132 if (it != m_cache.end())
133 m_cache.erase(it);
134}
135
136inline void
137CertificateCacheTtl::removeAll()
138{
139 for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
140 m_scheduler.cancelEvent(it->second.second);
141
142 m_cache.clear();
143}
144
145
Yingdi Yufc40d872014-02-18 12:56:04 -0800146} // namespace ndn
Yingdi Yu7640cb32014-01-29 20:00:50 -0800147
Yingdi Yufc40d872014-02-18 12:56:04 -0800148#endif //NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP