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; -*- */
22 #include "key-container.hpp"
23 #include "pib-impl.hpp"
24 #include "detail/key-impl.hpp"
25 #include "util/concepts.hpp"
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] = shared_ptr<detail::KeyImpl>(new 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 = shared_ptr<detail::KeyImpl>(new 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:122
const_iterator begin() const
bool operator==(const const_iterator &other)
void remove(const Name &keyName)
Remove a key with keyName from the container.
STL namespace.
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:42
Backend instance of Key.
Definition: key-impl.hpp:44
Name extractIdentityFromKeyName(const Name &keyName)
Extract identity namespace from the key name keyName.
Definition: key.cpp:160