Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * 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 Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 15 | #ifndef NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP |
| 16 | #define NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 18 | #include "../common.hpp" |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 19 | #include "certificate-cache.hpp" |
| 20 | #include "../util/scheduler.hpp" |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 22 | namespace ndn { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 23 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 24 | class CertificateCacheTtl : public CertificateCache |
| 25 | { |
| 26 | public: |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 27 | 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 Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 33 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 34 | virtual |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 35 | ~CertificateCacheTtl() |
| 36 | { |
| 37 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 39 | virtual inline void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 40 | insertCertificate(shared_ptr<const IdentityCertificate> certificate); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 41 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 42 | virtual inline shared_ptr<const IdentityCertificate> |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | getCertificate(const Name& certificateNameWithoutVersion); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 44 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 45 | virtual inline void |
| 46 | reset(); |
| 47 | |
| 48 | virtual inline size_t |
| 49 | getSize(); |
| 50 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 51 | private: |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 52 | inline void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 53 | insert(shared_ptr<const IdentityCertificate> certificate); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 54 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 55 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 56 | remove(const Name& certificateName); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 57 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 58 | inline void |
| 59 | removeAll(); |
| 60 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 61 | protected: |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 62 | typedef std::map<Name, std::pair<shared_ptr<const IdentityCertificate>, EventId> > Cache; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 64 | time::seconds m_defaultTtl; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 65 | Cache m_cache; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 66 | Scheduler m_scheduler; |
| 67 | }; |
| 68 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 69 | inline void |
| 70 | CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate) |
| 71 | { |
| 72 | m_scheduler.scheduleEvent(time::seconds(0), |
| 73 | bind(&CertificateCacheTtl::insert, this, certificate)); |
| 74 | } |
| 75 | |
| 76 | inline shared_ptr<const IdentityCertificate> |
| 77 | CertificateCacheTtl::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 | |
| 86 | inline void |
| 87 | CertificateCacheTtl::reset() |
| 88 | { |
| 89 | m_scheduler.scheduleEvent(time::seconds(0), |
| 90 | bind(&CertificateCacheTtl::removeAll, this)); |
| 91 | } |
| 92 | |
| 93 | inline size_t |
| 94 | CertificateCacheTtl::getSize() |
| 95 | { |
| 96 | return m_cache.size(); |
| 97 | } |
| 98 | |
| 99 | inline void |
| 100 | CertificateCacheTtl::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 | |
| 118 | inline void |
| 119 | CertificateCacheTtl::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 | |
| 127 | inline void |
| 128 | CertificateCacheTtl::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 Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 137 | } // namespace ndn |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 138 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 139 | #endif //NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP |