blob: 4cf20e2fe9840a0b62e527faec9548b46675fd42 [file] [log] [blame]
Yingdi Yu7640cb32014-01-29 20:00:50 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu7640cb32014-01-29 20:00:50 -080013 */
14
Yingdi Yufc40d872014-02-18 12:56:04 -080015#ifndef NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP
16#define NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP
Yingdi Yu7640cb32014-01-29 20:00:50 -080017
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080018#include "../common.hpp"
Yingdi Yu7640cb32014-01-29 20:00:50 -080019#include "certificate-cache.hpp"
20#include "../util/scheduler.hpp"
Yingdi Yu7640cb32014-01-29 20:00:50 -080021
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080022namespace ndn {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070023
Yingdi Yu7640cb32014-01-29 20:00:50 -080024class CertificateCacheTtl : public CertificateCache
25{
26public:
Yingdi Yu58f33712014-04-16 16:57:47 -070027 CertificateCacheTtl(boost::asio::io_service& io,
28 const time::seconds& defaultTtl = time::seconds(3600))
29 : m_defaultTtl(defaultTtl)
30 , m_scheduler(io)
31 {
32 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070033
Yingdi Yu7640cb32014-01-29 20:00:50 -080034 virtual
Yingdi Yu58f33712014-04-16 16:57:47 -070035 ~CertificateCacheTtl()
36 {
37 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038
Yingdi Yu58f33712014-04-16 16:57:47 -070039 virtual inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070040 insertCertificate(shared_ptr<const IdentityCertificate> certificate);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070041
Yingdi Yu58f33712014-04-16 16:57:47 -070042 virtual inline shared_ptr<const IdentityCertificate>
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043 getCertificate(const Name& certificateNameWithoutVersion);
Yingdi Yu7640cb32014-01-29 20:00:50 -080044
Yingdi Yu58f33712014-04-16 16:57:47 -070045 virtual inline void
46 reset();
47
48 virtual inline size_t
49 getSize();
50
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051private:
Yingdi Yu58f33712014-04-16 16:57:47 -070052 inline void
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070053 insert(shared_ptr<const IdentityCertificate> certificate);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070054
Yingdi Yu58f33712014-04-16 16:57:47 -070055 inline void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056 remove(const Name& certificateName);
Yingdi Yu7640cb32014-01-29 20:00:50 -080057
Yingdi Yu58f33712014-04-16 16:57:47 -070058 inline void
59 removeAll();
60
Yingdi Yu7640cb32014-01-29 20:00:50 -080061protected:
Yingdi Yu58f33712014-04-16 16:57:47 -070062 typedef std::map<Name, std::pair<shared_ptr<const IdentityCertificate>, EventId> > Cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080063
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064 time::seconds m_defaultTtl;
Yingdi Yu7640cb32014-01-29 20:00:50 -080065 Cache m_cache;
Yingdi Yu7640cb32014-01-29 20:00:50 -080066 Scheduler m_scheduler;
67};
68
Yingdi Yu58f33712014-04-16 16:57:47 -070069inline void
70CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate)
71{
72 m_scheduler.scheduleEvent(time::seconds(0),
73 bind(&CertificateCacheTtl::insert, this, certificate));
74}
75
76inline shared_ptr<const IdentityCertificate>
77CertificateCacheTtl::getCertificate(const Name& certificateName)
78{
79 Cache::iterator it = m_cache.find(certificateName);
80 if (it != m_cache.end())
81 return it->second.first;
82 else
83 return shared_ptr<IdentityCertificate>();
84}
85
86inline void
87CertificateCacheTtl::reset()
88{
89 m_scheduler.scheduleEvent(time::seconds(0),
90 bind(&CertificateCacheTtl::removeAll, this));
91}
92
93inline size_t
94CertificateCacheTtl::getSize()
95{
96 return m_cache.size();
97}
98
99inline void
100CertificateCacheTtl::insert(shared_ptr<const IdentityCertificate> certificate)
101{
102 time::milliseconds expire = (certificate->getFreshnessPeriod() >= time::seconds::zero() ?
103 certificate->getFreshnessPeriod() : m_defaultTtl);
104
105 Name index = certificate->getName().getPrefix(-1);
106
107 Cache::iterator it = m_cache.find(index);
108 if (it != m_cache.end())
109 m_scheduler.cancelEvent(it->second.second);
110
111 EventId eventId = m_scheduler.scheduleEvent(expire,
112 bind(&CertificateCacheTtl::remove,
113 this, certificate->getName()));
114
115 m_cache[index] = std::make_pair(certificate, eventId);
116}
117
118inline void
119CertificateCacheTtl::remove(const Name& certificateName)
120{
121 Name name = certificateName.getPrefix(-1);
122 Cache::iterator it = m_cache.find(name);
123 if (it != m_cache.end())
124 m_cache.erase(it);
125}
126
127inline void
128CertificateCacheTtl::removeAll()
129{
130 for(Cache::iterator it = m_cache.begin(); it != m_cache.end(); it++)
131 m_scheduler.cancelEvent(it->second.second);
132
133 m_cache.clear();
134}
135
136
Yingdi Yufc40d872014-02-18 12:56:04 -0800137} // namespace ndn
Yingdi Yu7640cb32014-01-29 20:00:50 -0800138
Yingdi Yufc40d872014-02-18 12:56:04 -0800139#endif //NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP