blob: 650a9e7bc079817ec88774c27e28c5e324fe17ee [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:
Alexander Afanasyeva4297a62014-06-19 13:29:34 -070036 explicit
Yingdi Yu58f33712014-04-16 16:57:47 -070037 CertificateCacheTtl(boost::asio::io_service& io,
38 const time::seconds& defaultTtl = time::seconds(3600))
39 : m_defaultTtl(defaultTtl)
40 , m_scheduler(io)
41 {
42 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043
Yingdi Yu7640cb32014-01-29 20:00:50 -080044 virtual
Yingdi Yu58f33712014-04-16 16:57:47 -070045 ~CertificateCacheTtl()
46 {
47 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048
Yingdi Yu58f33712014-04-16 16:57:47 -070049 virtual inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070050 insertCertificate(shared_ptr<const IdentityCertificate> certificate);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051
Yingdi Yu58f33712014-04-16 16:57:47 -070052 virtual inline shared_ptr<const IdentityCertificate>
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053 getCertificate(const Name& certificateNameWithoutVersion);
Yingdi Yu7640cb32014-01-29 20:00:50 -080054
Yingdi Yu58f33712014-04-16 16:57:47 -070055 virtual inline void
56 reset();
57
58 virtual inline size_t
59 getSize();
60
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061private:
Yingdi Yu58f33712014-04-16 16:57:47 -070062 inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070063 insert(shared_ptr<const IdentityCertificate> certificate);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064
Yingdi Yu58f33712014-04-16 16:57:47 -070065 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070066 remove(const Name& certificateName);
Yingdi Yu7640cb32014-01-29 20:00:50 -080067
Yingdi Yu58f33712014-04-16 16:57:47 -070068 inline void
69 removeAll();
70
Yingdi Yu7640cb32014-01-29 20:00:50 -080071protected:
Yingdi Yu58f33712014-04-16 16:57:47 -070072 typedef std::map<Name, std::pair<shared_ptr<const IdentityCertificate>, EventId> > Cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080073
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 time::seconds m_defaultTtl;
Yingdi Yu7640cb32014-01-29 20:00:50 -080075 Cache m_cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080076 Scheduler m_scheduler;
77};
78
Yingdi Yu58f33712014-04-16 16:57:47 -070079inline void
80CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
81{
82 m_scheduler.scheduleEvent(time::seconds(0),
83 bind(&CertificateCacheTtl::insert, this, certificate));
84}
85
86inline shared_ptr<const IdentityCertificate>
87CertificateCacheTtl::getCertificate(const Name& certificateName)
88{
89 Cache::iterator it = m_cache.find(certificateName);
90 if (it != m_cache.end())
91 return it->second.first;
92 else
93 return shared_ptr<IdentityCertificate>();
94}
95
96inline void
97CertificateCacheTtl::reset()
98{
99 m_scheduler.scheduleEvent(time::seconds(0),
100 bind(&CertificateCacheTtl::removeAll, this));
101}
102
103inline size_t
104CertificateCacheTtl::getSize()
105{
106 return m_cache.size();
107}
108
109inline void
110CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
111{
112 time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
113 certificate->getFreshnessPeriod() : m_defaultTtl);
114
115 Name index = certificate->getName().getPrefix(-1);
116
117 Cache::iterator it = m_cache.find(index);
118 if (it != m_cache.end())
119 m_scheduler.cancelEvent(it->second.second);
120
121 EventId eventId = m_scheduler.scheduleEvent(expire,
122 bind(&CertificateCacheTtl::remove,
123 this, certificate->getName()));
124
125 m_cache[index] = std::make_pair(certificate, eventId);
126}
127
128inline void
129CertificateCacheTtl::remove(const Name& certificateName)
130{
131 Name name = certificateName.getPrefix(-1);
132 Cache::iterator it = m_cache.find(name);
133 if (it != m_cache.end())
134 m_cache.erase(it);
135}
136
137inline void
138CertificateCacheTtl::removeAll()
139{
140 for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
141 m_scheduler.cancelEvent(it->second.second);
142
143 m_cache.clear();
144}
145
146
Yingdi Yufc40d872014-02-18 12:56:04 -0800147} // namespace ndn
Yingdi Yu7640cb32014-01-29 20:00:50 -0800148
Yingdi Yufc40d872014-02-18 12:56:04 -0800149#endif //NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP