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 
69  Producer
70  (const Name& prefix, const Name& dataType, Face* face, KeyChain* keyChain,
71  const ptr_lib::shared_ptr<ProducerDb>& database, int repeatAttempts = 3)
72  : impl_(new Impl(prefix, dataType, face, keyChain, database, repeatAttempts))
73  {
74  }
75 
97  Name
99  (MillisecondsSince1970 timeSlot, const OnEncryptedKeys& onEncryptedKeys,
100  const EncryptError::OnError& onError = defaultOnError)
101  {
102  return impl_->createContentKey(timeSlot, onEncryptedKeys, onError);
103  }
104 
118  void
119  produce
120  (Data& data, MillisecondsSince1970 timeSlot, const Blob& content,
121  const EncryptError::OnError& onError = defaultOnError)
122  {
123  impl_->produce(data, timeSlot, content, onError);
124  }
125 
129  static void
130  defaultOnError(EncryptError::ErrorCode errorCode, const std::string& message);
131 
132 private:
137  class Impl : public ptr_lib::enable_shared_from_this<Impl> {
138  public:
143  Impl
144  (const Name& prefix, const Name& dataType, Face* face, KeyChain* keyChain,
145  const ptr_lib::shared_ptr<ProducerDb>& database, int repeatAttempts);
146 
147  Name
149  (MillisecondsSince1970 timeSlot, const OnEncryptedKeys& onEncryptedKeys,
150  const EncryptError::OnError& onError);
151 
152  void
153  produce
154  (Data& data, MillisecondsSince1970 timeSlot, const Blob& content,
155  const EncryptError::OnError& onError);
156 
157  private:
158  class KeyInfo {
159  public:
160  MillisecondsSince1970 beginTimeSlot;
161  MillisecondsSince1970 endTimeSlot;
162  Blob keyBits;
163  };
164 
165  class KeyRequest {
166  public:
167  KeyRequest(int interests)
168  {
169  interestCount = interests;
170  }
171 
172  int interestCount;
173  std::map<Name, int> repeatAttempts;
174  std::vector<ptr_lib::shared_ptr<Data> > encryptedKeys;
175  };
176 
183  static MillisecondsSince1970
184  getRoundedTimeSlot(MillisecondsSince1970 timeSlot);
185 
195  void
196  sendKeyInterest
197  (const Interest& interest, MillisecondsSince1970 timeSlot,
198  const OnEncryptedKeys& onEncryptedKeys,
199  const EncryptError::OnError& onError);
200 
212  void
213  handleTimeout
214  (const ptr_lib::shared_ptr<const Interest>& interest,
215  MillisecondsSince1970 timeSlot, const OnEncryptedKeys& onEncryptedKeys,
216  const EncryptError::OnError& onError);
217 
231  void
232  handleNetworkNack
233  (const ptr_lib::shared_ptr<const Interest>& interest,
234  const ptr_lib::shared_ptr<NetworkNack>& networkNack,
235  MillisecondsSince1970 timeSlot,
236  const OnEncryptedKeys& onEncryptedKeys);
237 
248  void
249  updateKeyRequest
250  (const ptr_lib::shared_ptr<KeyRequest>& keyRequest,
251  MillisecondsSince1970 timeCount, const OnEncryptedKeys& onEncryptedKeys);
252 
265  void
266  handleCoveringKey
267  (const ptr_lib::shared_ptr<const Interest>& interest,
268  const ptr_lib::shared_ptr<Data>& data, MillisecondsSince1970 timeSlot,
269  const OnEncryptedKeys& onEncryptedKeys,
270  const EncryptError::OnError& onError);
271 
284  bool
285  encryptContentKey
286  (const Blob& encryptionKey, const Name& eKeyName,
287  MillisecondsSince1970 timeSlot, const OnEncryptedKeys& onEncryptedKeys,
288  const EncryptError::OnError& onError);
289 
290  // TODO: Move this to be the main representation inside the Exclude object.
291  class ExcludeEntry {
292  public:
293  ExcludeEntry(const Name::Component& component, bool anyFollowsComponent)
294  : component_(component), anyFollowsComponent_(anyFollowsComponent)
295  {
296  }
297 
298  Name::Component component_;
299  bool anyFollowsComponent_;
300  };
301 
307  static void
308  getExcludeEntries(const Exclude& exclude, std::vector<ExcludeEntry>& entries);
309 
315  static void
316  setExcludeEntries(Exclude& exclude, const std::vector<ExcludeEntry>& entries);
317 
325  static int
326  findEntryBeforeOrAt
327  (const std::vector<ExcludeEntry>& entries,
328  const Name::Component& component);
329 
335  static void
336  excludeAfter(Exclude& exclude, const Name::Component& from);
337 
343  static void
344  excludeBefore(Exclude& exclude, const Name::Component& to)
345  {
346  excludeRange(exclude, Name::Component(), to);
347  }
348 
355  static void
356  excludeRange
357  (Exclude& exclude, const Name::Component& from, const Name::Component& to);
358 
359  Face* face_;
360  Name namespace_;
361  KeyChain* keyChain_;
362  std::map<Name, ptr_lib::shared_ptr<KeyInfo> > eKeyInfo_;
363  std::map<MillisecondsSince1970, ptr_lib::shared_ptr<KeyRequest> > keyRequests_;
364  ptr_lib::shared_ptr<ProducerDb> database_;
365  int maxRepeatAttempts_;
366 
367  static const int START_TIME_STAMP_INDEX = -2;
368  static const int END_TIME_STAMP_INDEX = -1;
369  };
370 
371  ptr_lib::shared_ptr<Impl> impl_;
372 };
373 
374 }
375 
376 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
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:49
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:120
Producer(const Name &prefix, const Name &dataType, Face *face, KeyChain *keyChain, const ptr_lib::shared_ptr< ProducerDb > &database, int repeatAttempts=3)
Create a Producer to use the given ProducerDb, Face and other values.
Definition: producer.hpp:70
KeyChain is the main class of the security library.
Definition: key-chain.hpp:45
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.
Definition: blob.hpp:42
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:117
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:99