A Discrete-Event Network Simulator
API
qkd-control.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 DOTFEESA www.tk.etf.unsa.ba
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Miralem Mehic <miralem.mehic@ieee.org>
19  */
20 
21 #include <cmath>
22 #include <algorithm>
23 #include <numeric>
24 #include "ns3/packet.h"
25 #include "ns3/simulator.h"
26 #include "ns3/log.h"
27 #include "ns3/boolean.h"
28 #include "ns3/double.h"
29 #include "ns3/uinteger.h"
30 
31 #include "qkd-control.h"
32 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("QKDControl");
36 
37 NS_OBJECT_ENSURE_REGISTERED (QKDControl);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::QKDControl")
43  .SetParent<Object> ()
44  .SetGroupName ("QKDControl")
45  .AddConstructor<QKDControl> ()
46  .AddAttribute ("BufferList", "The list of buffers associated to this QKD Control.",
49  MakeObjectVectorChecker<QKDControl> ())
50  ;
51  return tid;
52 }
53 
54 TypeId
56 {
57  return GetTypeId ();
58 }
59 
61  : Object ()
62 {
64 }
65 
67 {
68  m_destinations.clear();
69 }
70 
71 void
73 {
74  for (std::vector<Ptr<QKDBuffer> >::iterator i = m_qkdbuffers.begin ();
75  i != m_qkdbuffers.end (); i++)
76  {
77  Ptr<QKDBuffer> device = *i;
78  device->Dispose ();
79  *i = 0;
80  }
81  m_qkdbuffers.clear ();
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION (this);
90 }
91 
93 QKDControl::AddNewLink (
94  Ptr<Node> a, //alice
95  Ptr<Node> b, //bob
96  Ptr<Node> aKMS, //alice KMS
97  Ptr<Node> bKMS, //bob KMS
98  uint32_t Mmin,
99  uint32_t Mthr,
100  uint32_t Mmax,
101  uint32_t Mcurrent,
102  bool isMaster,
103  bool useRealStorages
104 )
105 {
106  NS_LOG_FUNCTION (this << GetNode()->GetId() << a->GetId() << b->GetId() );
107 
108  // Only one buffer can exist per connection between two nodes!
109  // It is possible to implement multiple number of links,
110  // but if the source and the destination of the link is the same,
111  // these links belong to the same connection and they sohuld use the same buffer.
112  std::map<uint32_t, QKDControl::QKDLink >::iterator i = m_destinations.find ( b->GetId() );
113  if (i != m_destinations.end ()){
114 
115  NS_LOG_FUNCTION (this << "BUFFER ALREADY EXISTS!");
116  return i->second;
117 
118  }else{
119 
120  struct QKDControl::QKDLink newLink;
121  newLink.alice = a;
122  newLink.bob = b;
123  newLink.publicChannelMetric = 0;
124  newLink.quantumChannelMetric = 0;
125 
126  NS_LOG_FUNCTION (this << GetNode()->GetId() << "CREATE NEW BUFFER FOR ALICE!");
127  newLink.qkdBufferAlice = CreateObject<QKDBuffer> ();
128  newLink.qkdBufferAlice->Init(
129  a,
130  b,
131  Mmin,
132  Mthr,
133  Mmax,
134  Mcurrent,
135  useRealStorages
136  );
137  m_qkdbuffers.push_back(newLink.qkdBufferAlice);
138  newLink.qkdBufferAlice->SetIndex( m_qkdbuffers.size() - 1 );
139 
140  NS_LOG_FUNCTION (this << GetNode()->GetId() << "CREATE NEW BUFFER FOR BOB!");
141  newLink.qkdBufferBob = CreateObject<QKDBuffer> ();
142  newLink.qkdBufferBob->Init(
143  b,
144  a,
145  Mmin,
146  Mthr,
147  Mmax,
148  Mcurrent,
149  useRealStorages
150  );
151 
152  m_destinations.insert (std::make_pair ( b->GetId(), newLink) );
153  NS_LOG_FUNCTION (this << GetNode()->GetId() << "save destination to " << b->GetId() );
154 
155  Ptr<QKDKeyManagerSystemApplication> lkmsA;
156  Ptr<QKDKeyManagerSystemApplication> lkmsB;
157  for (uint32_t i = 0; i < aKMS->GetNApplications (); ++i){
158  lkmsA = aKMS->GetApplication (i)->GetObject <QKDKeyManagerSystemApplication> ();
159  if(lkmsA !=0) break;
160  }
161  for (uint32_t i = 0; i < bKMS->GetNApplications (); ++i){
162  lkmsB = bKMS->GetApplication (i)->GetObject <QKDKeyManagerSystemApplication> ();
163  if(lkmsB !=0) break;
164  }
165 
166  if(lkmsA && lkmsB){
167  std::string keyAssociationId = lkmsA->AddNewLink(
168  a->GetId(),
169  b->GetId(),
170  lkmsB->GetAddress(),
171  newLink.qkdBufferAlice
172  );
173  newLink.keyAssociationId = keyAssociationId;
174 
175  lkmsB->AddNewLink(
176  b->GetId(),
177  a->GetId(),
178  lkmsA->GetAddress(),
179  newLink.qkdBufferBob,
181  );
182  }else{
183  NS_FATAL_ERROR ("NO QKD KEY MANAGER INSTALLED ON THE NODE!");
184  }
185 
186  return newLink;
187  }
188 }
189 
190 void
192 {
193  NS_LOG_FUNCTION (this << GetNode()->GetId() << newLink.alice->GetId() );
194 
195  // Only one buffer can exist per connection between two nodes!
196  // It is possible to implement multiple number of links,
197  // but if the source and the destination of the link is the same,
198  // these links belong to the same connection and they sohuld use the same buffer.
199  std::map<uint32_t, QKDControl::QKDLink >::iterator i = m_destinations.find ( newLink.alice->GetId() );
200  if (i != m_destinations.end ()){
201  NS_LOG_FUNCTION (this << "BUFFER ALREADY EXISTS!");
202  }else{
203  Ptr<Node> temp = newLink.alice;
204  newLink.alice = newLink.bob;
205  newLink.bob = temp;
206  m_qkdbuffers.push_back( newLink.qkdBufferBob );
207  newLink.qkdBufferBob->SetIndex( m_qkdbuffers.size() - 1 );
208  m_destinations.insert (std::make_pair ( newLink.bob->GetId(), newLink) );
209 
210  NS_LOG_FUNCTION (this << GetNode()->GetId() << "save destination to " << newLink.bob->GetId() );
211  }
212 }
213 
214 
215 
218 
219  NS_LOG_FUNCTION(this << GetNode()->GetId() << dst->GetId() << m_destinations.size() );
220 
221  std::map<uint32_t, QKDControl::QKDLink >::iterator i = m_destinations.find ( dst->GetId() );
222  if (i != m_destinations.end ()){
223  return i->second.qkdBufferAlice;
224  }else{
225  NS_LOG_FUNCTION(this << GetNode()->GetId() << "Unable to find QKDbuffer for destination " << dst->GetId());
226  return 0;
227  }
228 }
229 
230 uint32_t
232 {
233  NS_LOG_FUNCTION (this);
234  return m_qkdbuffers.size ();
235 }
236 
237 Ptr<Node>
239  return m_node;
240 }
241 
242 void
244  m_node = node;
245 }
246 
247 
248 std::vector<std::string>
250  Ptr<QKDApp004> alice,
251  Ptr<QKDApp004> bob,
254 ){
255  NS_LOG_FUNCTION(this);
256 
257  std::vector<std::string> output;
258 
259  UintegerValue aliceEncryptionType;
260  UintegerValue bobEncryptionType;
261 
262  UintegerValue aliceAuthenticationType;
263  UintegerValue bobAuthenticationType;
264 
265  alice->GetAttribute ("EncryptionType", aliceEncryptionType);
266  alice->GetAttribute ("AuthenticationType", aliceAuthenticationType);
267 
268  bob->GetAttribute ("EncryptionType", bobEncryptionType);
269  bob->GetAttribute ("AuthenticationType", bobAuthenticationType);
270 
271  NS_ASSERT (aliceEncryptionType.Get() == bobEncryptionType.Get());
272  NS_ASSERT (aliceAuthenticationType.Get() == bobAuthenticationType.Get());
273 
274  UintegerValue priorityAttrValue;
275  alice->GetAttribute ("Priority", priorityAttrValue);
276  uint32_t priority = priorityAttrValue.Get();
277 
278  UUID aliceId = alice->GetId();
279  UUID bobId = bob->GetId();
280 
281  if(
282  aliceEncryptionType.Get() == QKDEncryptor::QKDCRYPTO_OTP ||
283  aliceEncryptionType.Get() == QKDEncryptor::QKDCRYPTO_AES
284  ){
285  QKDApplicationEntry appEntry = kmsA->RegisterApplicationEntry(
286  aliceId,
287  bobId,
288  "etsi004_enc",
289  kmsB->GetAddress(),
290  priority,
291  200 //expiration time value of the application entry. @toDo: for later usage purge expired apps
292  );
293  std::string appId = appEntry.GetId().string();
294  NS_LOG_FUNCTION(this << appId);
295  output.push_back(appId);
296 
297  QKDApplicationEntry appEntry2 = kmsB->RegisterApplicationEntry(
298  appEntry.GetKeyAssociationId(),
299  appEntry.GetId(),
300  bobId,
301  aliceId,
302  "etsi004_enc",
303  kmsA->GetAddress(),
304  priority,
305  200 //expiration time value of the application entry. @toDo: for later usage purge expired apps
306  );
307  NS_LOG_FUNCTION(this << "Encryption SAE connections established!");
308 
309  alice->SetKsidEncryption(appEntry.GetId());
310  bob->SetKsidEncryption(appEntry.GetId());
311  }
312 
313  if(
314  aliceAuthenticationType.Get() == QKDEncryptor::QKDCRYPTO_AUTH_VMAC
315  ){
316  QKDApplicationEntry appEntry = kmsA->RegisterApplicationEntry(
317  aliceId,
318  bobId,
319  "etsi004_auth",
320  kmsB->GetAddress(),
321  priority,
322  200 //expiration time value of the application entry. @toDo: for later usage purge expired apps
323  );
324  std::string appId = appEntry.GetId().string();
325  NS_LOG_FUNCTION(this << appId);
326  output.push_back(appId);
327 
328  QKDApplicationEntry appEntry2 = kmsB->RegisterApplicationEntry(
329  appEntry.GetKeyAssociationId(),
330  appEntry.GetId(),
331  bobId,
332  aliceId,
333  "etsi004_auth",
334  kmsA->GetAddress(),
335  priority,
336  200 //expiration time value of the application entry. @toDo: for later usage purge expired apps
337  );
338  NS_LOG_FUNCTION(this << "Authentication SAE connections established!");
339 
340  alice->SetKsidAuthentication(appEntry.GetId());
341  bob->SetKsidAuthentication(appEntry.GetId());
342  }
343 
344  NS_LOG_FUNCTION(this << output.size());
345 
346  return output;
347 }
348 
349 
350 std::vector<std::string>
352  Ptr<QKDApp014> alice,
353  Ptr<QKDApp014> bob,
356 ){
357  NS_LOG_FUNCTION(this);
358 
359  std::vector<std::string> output;
360 
361  UintegerValue aliceEncryptionType;
362  UintegerValue bobEncryptionType;
363 
364  UintegerValue aliceAuthenticationType;
365  UintegerValue bobAuthenticationType;
366 
367  alice->GetAttribute ("EncryptionType", aliceEncryptionType);
368  alice->GetAttribute ("AuthenticationType", aliceAuthenticationType);
369 
370  bob->GetAttribute ("EncryptionType", bobEncryptionType);
371  bob->GetAttribute ("AuthenticationType", bobAuthenticationType);
372 
373  NS_ASSERT (aliceEncryptionType.Get() == bobEncryptionType.Get());
374  NS_ASSERT (aliceAuthenticationType.Get() == bobAuthenticationType.Get());
375 
376  //UintegerValue priorityAttrValue;
377  //alice->GetAttribute ("Priority", priorityAttrValue);
378  uint32_t priority = 0;//priorityAttrValue.Get();
379 
380  UUID aliceId = alice->GetId();
381  UUID bobId = bob->GetId();
382 
383  if(
384  aliceEncryptionType.Get() == QKDEncryptor::QKDCRYPTO_OTP ||
385  aliceEncryptionType.Get() == QKDEncryptor::QKDCRYPTO_AES
386  ){
387  QKDApplicationEntry appEntry = kmsA->RegisterApplicationEntry(
388  aliceId,
389  bobId,
390  "etsi014_enc",
391  kmsB->GetAddress(),
392  priority,
393  200 //expiration time value of the application entry. @toDo: for later usage purge expired apps
394  );
395  std::string appId = appEntry.GetId().string();
396  NS_LOG_FUNCTION(this << appId);
397  output.push_back(appId);
398 
399  QKDApplicationEntry appEntry2 = kmsB->RegisterApplicationEntry(
400  appEntry.GetKeyAssociationId(),
401  appEntry.GetId(),
402  bobId,
403  aliceId,
404  "etsi014_enc",
405  kmsA->GetAddress(),
406  priority,
407  200 //expiration time value of the application entry. @toDo: for later usage purge expired apps
408  );
409  NS_LOG_FUNCTION(this << "Encryption SAE connections established!");
410 
411  alice->SetKsidEncryption(appEntry.GetId());
412  bob->SetKsidEncryption(appEntry.GetId());
413  }
414 
415  if(
416  aliceAuthenticationType.Get() == QKDEncryptor::QKDCRYPTO_AUTH_VMAC
417  ){
418  QKDApplicationEntry appEntry = kmsA->RegisterApplicationEntry(
419  aliceId,
420  bobId,
421  "etsi014_auth",
422  kmsB->GetAddress(),
423  priority,
424  200 //expiration time value of the application entry. @toDo: for later usage purge expired apps
425  );
426  std::string appId = appEntry.GetId().string();
427  NS_LOG_FUNCTION(this << appId);
428  output.push_back(appId);
429 
430  QKDApplicationEntry appEntry2 = kmsB->RegisterApplicationEntry(
431  appEntry.GetKeyAssociationId(),
432  appEntry.GetId(),
433  bobId,
434  aliceId,
435  "etsi014_auth",
436  kmsA->GetAddress(),
437  priority,
438  200 //expiration time value of the application entry. @toDo: for later usage purge expired apps
439  );
440  NS_LOG_FUNCTION(this << "Authentication SAE connections established!");
441 
442  alice->SetKsidAuthentication(appEntry.GetId());
443  bob->SetKsidAuthentication(appEntry.GetId());
444  }
445 
446  NS_LOG_FUNCTION(this << output.size());
447 
448  return output;
449 }
450 
451 } // namespace ns3
uint32_t GetId(void) const
Definition: node.cc:109
A base class which provides memory management and object aggregation.
Definition: object.h:88
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
Container for a set of ns3::Object pointers.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
Introspection did not find any typical Config paths.
UUID GetKeyAssociationId()
Get the association identifier.
UUID GetId()
Get the application identifier.
QKD control is a network component with the knowledge of the network status.
Definition: qkd-control.h:73
Ptr< Node > m_node
The controller node.
Definition: qkd-control.h:231
std::vector< std::string > RegisterQKDApplications(Ptr< QKDApp004 > alice, Ptr< QKDApp004 > bob, Ptr< QKDKeyManagerSystemApplication > kmsA, Ptr< QKDKeyManagerSystemApplication > kmsB)
Register the QKD application pair (that implements ETSI QKD 004 API) on the site.
Definition: qkd-control.cc:249
uint32_t GetNQKDBuffers(void) const
Get the number of QKDBuffers associated to this QKDControl.
Definition: qkd-control.cc:231
virtual void DoDispose(void)
The dispose method.
Definition: qkd-control.cc:72
std::vector< Ptr< QKDBuffer > > m_qkdbuffers
The list of associated QKDBuffers.
Definition: qkd-control.h:233
virtual ~QKDControl()
Destructor.
Definition: qkd-control.cc:66
std::map< uint32_t, QKDLink > m_destinations
Definition: qkd-control.h:235
Ptr< QKDBuffer > GetBufferByDestinationNode(Ptr< Node >)
Get the QKDBuffer for the given destination node.
Definition: qkd-control.cc:217
virtual void DoInitialize(void)
Initialization function.
Definition: qkd-control.cc:86
Ptr< Node > GetNode()
Get the controller node.
Definition: qkd-control.cc:238
void SetNode(Ptr< Node >)
Set the controller node.
Definition: qkd-control.cc:243
virtual TypeId GetInstanceTypeId(void) const
Get the type ID for the instance.
Definition: qkd-control.cc:55
QKDControl()
Constructor.
Definition: qkd-control.cc:60
static TypeId GetTypeId(void)
Get the type ID.
Definition: qkd-control.cc:40
void AddLinkRecord(struct QKDControl::QKDLink)
Record details about the new QKD link.
Definition: qkd-control.cc:191
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Universally unique identifier (UUID)
Definition: uuid.h:35
std::string string() const
Get string from the current UUID in format "00000000-0000-0000-0000-000000000000".
Definition: uuid.cc:74
Hold an unsigned integer type.
Definition: uinteger.h:44
uint64_t Get(void) const
Definition: uinteger.cc:35
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:81
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.