Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 24 | #ifndef NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP |
| 25 | #define NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 27 | #include "../common.hpp" |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 28 | #include "certificate-cache.hpp" |
| 29 | #include "../util/scheduler.hpp" |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 31 | namespace ndn { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 32 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 33 | class CertificateCacheTtl : public CertificateCache |
| 34 | { |
| 35 | public: |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 36 | 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 Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 42 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 43 | virtual |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 44 | ~CertificateCacheTtl() |
| 45 | { |
| 46 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 48 | virtual inline void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 49 | insertCertificate(shared_ptr<const IdentityCertificate> certificate); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 50 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 51 | virtual inline shared_ptr<const IdentityCertificate> |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 52 | getCertificate(const Name& certificateNameWithoutVersion); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 53 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 54 | virtual inline void |
| 55 | reset(); |
| 56 | |
| 57 | virtual inline size_t |
| 58 | getSize(); |
| 59 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 60 | private: |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 61 | inline void |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 62 | insert(shared_ptr<const IdentityCertificate> certificate); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 63 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 64 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 65 | remove(const Name& certificateName); |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 66 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 67 | inline void |
| 68 | removeAll(); |
| 69 | |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 70 | protected: |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 71 | typedef std::map<Name, std::pair<shared_ptr<const IdentityCertificate>, EventId> > Cache; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 73 | time::seconds m_defaultTtl; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 74 | Cache m_cache; |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 75 | Scheduler m_scheduler; |
| 76 | }; |
| 77 | |
Yingdi Yu | 58f3371 | 2014-04-16 16:57:47 -0700 | [diff] [blame] | 78 | inline void |
| 79 | CertificateCacheTtl::insertCertificate(shared_ptr<const IdentityCertificate> certificate) |
| 80 | { |
| 81 | m_scheduler.scheduleEvent(time::seconds(0), |
| 82 | bind(&CertificateCacheTtl::insert, this, certificate)); |
| 83 | } |
| 84 | |
| 85 | inline shared_ptr<const IdentityCertificate> |
| 86 | CertificateCacheTtl::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 | |
| 95 | inline void |
| 96 | CertificateCacheTtl::reset() |
| 97 | { |
| 98 | m_scheduler.scheduleEvent(time::seconds(0), |
| 99 | bind(&CertificateCacheTtl::removeAll, this)); |
| 100 | } |
| 101 | |
| 102 | inline size_t |
| 103 | CertificateCacheTtl::getSize() |
| 104 | { |
| 105 | return m_cache.size(); |
| 106 | } |
| 107 | |
| 108 | inline void |
| 109 | CertificateCacheTtl::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 | |
| 127 | inline void |
| 128 | CertificateCacheTtl::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 | |
| 136 | inline void |
| 137 | CertificateCacheTtl::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 Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 146 | } // namespace ndn |
Yingdi Yu | 7640cb3 | 2014-01-29 20:00:50 -0800 | [diff] [blame] | 147 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 148 | #endif //NDN_SECURITY_CERTIFICATE_CACHE_TTL_HPP |