NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
key-container.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
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.
20  */
21 
26 
27 namespace ndn {
28 namespace security {
29 namespace pib {
30 
31 NDN_CXX_ASSERT_FORWARD_ITERATOR(KeyContainer::const_iterator);
32 
34  : m_container(nullptr)
35 {
36 }
37 
38 KeyContainer::const_iterator::const_iterator(std::set<Name>::const_iterator it,
39  const KeyContainer& container)
40  : m_it(it)
41  , m_container(&container)
42 {
43 }
44 
45 Key
47 {
48  BOOST_ASSERT(m_container != nullptr);
49  return m_container->get(*m_it);
50 }
51 
54 {
55  ++m_it;
56  return *this;
57 }
58 
61 {
62  const_iterator it(*this);
63  ++m_it;
64  return it;
65 }
66 
67 bool
69 {
70  bool isThisEnd = m_container == nullptr || m_it == m_container->m_keyNames.end();
71  bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_keyNames.end();
72  return ((isThisEnd || isOtherEnd) ?
73  (isThisEnd == isOtherEnd) :
74  m_container->m_pib == other.m_container->m_pib && m_it == other.m_it);
75 }
76 
77 bool
79 {
80  return !(*this == other);
81 }
82 
83 KeyContainer::KeyContainer(const Name& identity, shared_ptr<PibImpl> pibImpl)
84  : m_identity(identity)
85  , m_pib(std::move(pibImpl))
86 {
87  BOOST_ASSERT(m_pib != nullptr);
88  m_keyNames = m_pib->getKeysOfIdentity(identity);
89 }
90 
93 {
94  return const_iterator(m_keyNames.begin(), *this);
95 }
96 
99 {
100  return const_iterator();
101 }
102 
104 KeyContainer::find(const Name& keyName) const
105 {
106  return const_iterator(m_keyNames.find(keyName), *this);
107 }
108 
109 size_t
111 {
112  return m_keyNames.size();
113 }
114 
115 Key
116 KeyContainer::add(const uint8_t* key, size_t keyLen, const Name& keyName)
117 {
118  if (m_identity != v2::extractIdentityFromKeyName(keyName)) {
119  BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
120  "`" + m_identity.toUri() + "`"));
121  }
122 
123  m_keyNames.insert(keyName);
124  m_keys[keyName] = make_shared<detail::KeyImpl>(keyName, key, keyLen, m_pib);
125 
126  return get(keyName);
127 }
128 
129 void
130 KeyContainer::remove(const Name& keyName)
131 {
132  if (m_identity != v2::extractIdentityFromKeyName(keyName)) {
133  BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
134  "`" + m_identity.toUri() + "`"));
135  }
136 
137  m_keyNames.erase(keyName);
138  m_keys.erase(keyName);
139  m_pib->removeKey(keyName);
140 }
141 
142 Key
143 KeyContainer::get(const Name& keyName) const
144 {
145  if (m_identity != v2::extractIdentityFromKeyName(keyName)) {
146  BOOST_THROW_EXCEPTION(std::invalid_argument("Key name `" + keyName.toUri() + "` does not match identity "
147  "`" + m_identity.toUri() + "`"));
148  }
149 
150  shared_ptr<detail::KeyImpl> key;
151  auto it = m_keys.find(keyName);
152 
153  if (it != m_keys.end()) {
154  key = it->second;
155  }
156  else {
157  key = make_shared<detail::KeyImpl>(keyName, m_pib);
158  m_keys[keyName] = key;
159  }
160 
161  return Key(key);
162 }
163 
164 bool
166 {
167  return m_keyNames == m_pib->getKeysOfIdentity(m_identity);
168 }
169 
170 } // namespace pib
171 } // namespace security
172 } // namespace ndn
const_iterator find(const Name &keyName) const
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Get URI representation of the name.
Definition: name.cpp:116
const_iterator begin() const
bool operator==(const const_iterator &other)
void remove(const Name &keyName)
Remove a key with keyName from the container.
NDN_CXX_ASSERT_FORWARD_ITERATOR(CertificateContainer::const_iterator)
bool operator!=(const const_iterator &other)
Container of keys of an identity.
const_iterator end() const
A frontend handle of a key instance.
Definition: key.hpp:49
Key get(const Name &keyName) const
Get a key with keyName from the container.
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE shared_ptr< PibImpl > pibImpl
Key add(const uint8_t *key, size_t keyLen, const Name &keyName)
Add key of keyLen bytes with keyName into the container.
bool isConsistent() const
Check if the container is consistent with the backend storage.
Represents an absolute name.
Definition: name.hpp:43
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160