All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
producer.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
24 #ifndef NDN_PRODUCER_HPP
25 #define NDN_PRODUCER_HPP
26 
27 #include <map>
28 #include "../face.hpp"
29 #include "../security/key-chain.hpp"
30 #include "encrypt-error.hpp"
31 #include "producer-db.hpp"
32 
33 namespace ndn {
34 
40 class Producer {
41 public:
42  typedef func_lib::function<
43  void(const std::vector<ptr_lib::shared_ptr<Data> >& keys)> OnEncryptedKeys;
44 
73  Producer
74  (const Name& prefix, const Name& dataType, Face* face, KeyChain* keyChain,
75  const ptr_lib::shared_ptr<ProducerDb>& database, int repeatAttempts = 3,
76  const Link& keyRetrievalLink = getNO_LINK())
77  : impl_(new Impl
78  (prefix, dataType, face, keyChain, database, repeatAttempts,
79  keyRetrievalLink))
80  {
81  }
82 
104  Name
106  (MillisecondsSince1970 timeSlot, const OnEncryptedKeys& onEncryptedKeys,
107  const EncryptError::OnError& onError = defaultOnError)
108  {
109  return impl_->createContentKey(timeSlot, onEncryptedKeys, onError);
110  }
111 
125  void
126  produce
127  (Data& data, MillisecondsSince1970 timeSlot, const Blob& content,
128  const EncryptError::OnError& onError = defaultOnError)
129  {
130  impl_->produce(data, timeSlot, content, onError);
131  }
132 
136  static void
137  defaultOnError(EncryptError::ErrorCode errorCode, const std::string& message);
138 
139 private:
144  class Impl : public ptr_lib::enable_shared_from_this<Impl> {
145  public:
150  Impl
151  (const Name& prefix, const Name& dataType, Face* face, KeyChain* keyChain,
152  const ptr_lib::shared_ptr<ProducerDb>& database, int repeatAttempts,
153  const Link& keyRetrievalLink);
154 
155  Name
157  (MillisecondsSince1970 timeSlot, const OnEncryptedKeys& onEncryptedKeys,
158  const EncryptError::OnError& onError);
159 
160  void
161  produce
162  (Data& data, MillisecondsSince1970 timeSlot, const Blob& content,
163  const EncryptError::OnError& onError);
164 
165  private:
166  class KeyInfo {
167  public:
168  KeyInfo() : beginTimeSlot(0), endTimeSlot(0) {}
169 
170  MillisecondsSince1970 beginTimeSlot;
171  MillisecondsSince1970 endTimeSlot;
172  Blob keyBits;
173  };
174 
175  class KeyRequest {
176  public:
177  KeyRequest(int interests)
178  {
179  interestCount = interests;
180  }
181 
182  int interestCount;
183  std::map<Name, int> repeatAttempts;
184  std::vector<ptr_lib::shared_ptr<Data> > encryptedKeys;
185  };
186 
193  static MillisecondsSince1970
194  getRoundedTimeSlot(MillisecondsSince1970 timeSlot);
195 
205  void
206  sendKeyInterest
207  (const Interest& interest, MillisecondsSince1970 timeSlot,
208  const OnEncryptedKeys& onEncryptedKeys,
209  const EncryptError::OnError& onError);
210 
222  void
223  handleTimeout
224  (const ptr_lib::shared_ptr<const Interest>& interest,
225  MillisecondsSince1970 timeSlot, const OnEncryptedKeys& onEncryptedKeys,
226  const EncryptError::OnError& onError);
227 
241  void
242  handleNetworkNack
243  (const ptr_lib::shared_ptr<const Interest>& interest,
244  const ptr_lib::shared_ptr<NetworkNack>& networkNack,
245  MillisecondsSince1970 timeSlot,
246  const OnEncryptedKeys& onEncryptedKeys,
247  const EncryptError::OnError& onError);
248 
259  void
260  updateKeyRequest
261  (const ptr_lib::shared_ptr<KeyRequest>& keyRequest,
262  MillisecondsSince1970 timeCount, const OnEncryptedKeys& onEncryptedKeys);
263 
276  void
277  handleCoveringKey
278  (const ptr_lib::shared_ptr<const Interest>& interest,
279  const ptr_lib::shared_ptr<Data>& data, MillisecondsSince1970 timeSlot,
280  const OnEncryptedKeys& onEncryptedKeys,
281  const EncryptError::OnError& onError);
282 
295  bool
296  encryptContentKey
297  (const Blob& encryptionKey, const Name& eKeyName,
298  MillisecondsSince1970 timeSlot, const OnEncryptedKeys& onEncryptedKeys,
299  const EncryptError::OnError& onError);
300 
301  // TODO: Move this to be the main representation inside the Exclude object.
302  class ExcludeEntry {
303  public:
304  ExcludeEntry(const Name::Component& component, bool anyFollowsComponent)
305  : component_(component), anyFollowsComponent_(anyFollowsComponent)
306  {
307  }
308 
309  Name::Component component_;
310  bool anyFollowsComponent_;
311  };
312 
318  static void
319  getExcludeEntries(const Exclude& exclude, std::vector<ExcludeEntry>& entries);
320 
326  static void
327  setExcludeEntries(Exclude& exclude, const std::vector<ExcludeEntry>& entries);
328 
336  static int
337  findEntryBeforeOrAt
338  (const std::vector<ExcludeEntry>& entries,
339  const Name::Component& component);
340 
346  static void
347  excludeAfter(Exclude& exclude, const Name::Component& from);
348 
354  static void
355  excludeBefore(Exclude& exclude, const Name::Component& to)
356  {
357  excludeRange(exclude, Name::Component(), to);
358  }
359 
366  static void
367  excludeRange
368  (Exclude& exclude, const Name::Component& from, const Name::Component& to);
369 
370  Face* face_;
371  Name namespace_;
372  KeyChain* keyChain_;
373  std::map<Name, ptr_lib::shared_ptr<KeyInfo> > eKeyInfo_;
374  std::map<MillisecondsSince1970, ptr_lib::shared_ptr<KeyRequest> > keyRequests_;
375  ptr_lib::shared_ptr<ProducerDb> database_;
376  int maxRepeatAttempts_;
377 
378  Link keyRetrievalLink_;
379 
380  static const int START_TIME_STAMP_INDEX = -2;
381  static const int END_TIME_STAMP_INDEX = -1;
382  };
383 
389  static Link&
390  getNO_LINK()
391  {
392  if (!noLink_)
393  noLink_ = new Link();
394 
395  return *noLink_;
396  }
397 
398  ptr_lib::shared_ptr<Impl> impl_;
399  static Link* noLink_;
400 };
401 
402 }
403 
404 #endif
A Producer manages content keys used to encrypt a data packet in the group-based encryption protocol...
Definition: producer.hpp:40
Definition: data.hpp:37
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
func_lib::function< void(ErrorCode errorCode, const std::string &message)> OnError
A method calls onError(errorCode, message) for an error.
Definition: encrypt-error.hpp:50
Producer(const Name &prefix, const Name &dataType, Face *face, KeyChain *keyChain, const ptr_lib::shared_ptr< ProducerDb > &database, int repeatAttempts=3, const Link &keyRetrievalLink=getNO_LINK())
Create a Producer to use the given ProducerDb, Face and other values.
Definition: producer.hpp:74
void produce(Data &data, MillisecondsSince1970 timeSlot, const Blob &content, const EncryptError::OnError &onError=defaultOnError)
Encrypt the given content with the content key that covers timeSlot, and update the data packet with ...
Definition: producer.hpp:127
KeyChain is the main class of the security library.
Definition: key-chain.hpp:53
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:119
static void defaultOnError(EncryptError::ErrorCode errorCode, const std::string &message)
The default OnError callback which does nothing.
Definition: producer.cpp:37
Name createContentKey(MillisecondsSince1970 timeSlot, const OnEncryptedKeys &onEncryptedKeys, const EncryptError::OnError &onError=defaultOnError)
Create the content key corresponding to the timeSlot.
Definition: producer.hpp:106