A Discrete-Event Network Simulator
API
qkd-crypto.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
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 #define NS_LOG_APPEND_CONTEXT \
22  if (GetObject<Node> ()) { std::clog << "[node " << GetObject<Node> ()->GetId () << "] "; }
23 
24 #include "qkd-crypto.h"
25 #include "ns3/packet.h"
26 #include "ns3/assert.h"
27 #include "ns3/log.h"
28 #include <string>
29 #include <cstdarg>
30 #include <iostream>
31 #include <sstream>
32 
33 #include "ns3/header.h"
34 #include "ns3/tcp-header.h"
35 #include "ns3/udp-header.h"
36 #include "ns3/icmpv4.h"
37 
38 #include "ns3/dsdv-packet.h"
39 #include "ns3/dsdvq-packet.h"
40 
41 #include "ns3/aodv-packet.h"
42 #include "ns3/aodvq-packet.h"
43 
44 #include "ns3/olsr-header.h"
45 
46 #include "ns3/node.h"
47 #include "ns3/qkd-internal-tag.h"
48 #include "ns3/virtual-ipv4-l3-protocol.h"
49 
50 #include <boost/iostreams/filtering_streambuf.hpp>
51 #include <boost/iostreams/filter/gzip.hpp>
52 #include <boost/iostreams/copy.hpp>
53 
64 namespace ns3 {
65 
66 NS_LOG_COMPONENT_DEFINE ("QKDCrypto");
67 
68 NS_OBJECT_ENSURE_REGISTERED (QKDCrypto);
69 
70 static const std::string base64_chars =
71  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
72  "abcdefghijklmnopqrstuvwxyz"
73  "0123456789+/";
74 
75 static inline bool is_base64(unsigned char c) {
76  return (isalnum(c) || (c == '+') || (c == '/'));
77 }
78 
79 TypeId
81 {
82  static TypeId tid = TypeId ("ns3::QKDCrypto")
83  .SetParent<Object> ()
84  .AddAttribute ("CompressionEnabled", "Indicates whether a compression of packets is enabled.",
85  BooleanValue (false),
88  .AddAttribute ("EncryptionEnabled", "Indicates whether a real encryption of packets is enabled.",
89  BooleanValue (false),
92 
93  .AddTraceSource ("PacketEncrypted",
94  "The change trance for currenly ecrypted packet",
96  "ns3::QKDCrypto::PacketEncrypted")
97  .AddTraceSource ("PacketDecrypted",
98  "The change trance for currenly decrypted packet",
100  "ns3::QKDCrypto::PacketDecrypted")
101 
102  .AddTraceSource ("PacketAuthenticated",
103  "The change trance for currenly authenticated packet",
105  "ns3::QKDCrypto::PacketAuthenticated")
106  .AddTraceSource ("PacketDeAuthenticated",
107  "The change trance for currenly deauthenticated packet",
109  "ns3::QKDCrypto::PacketDeAuthenticated")
110  ;
111  return tid;
112 }
113 
115 {
116  NS_LOG_FUNCTION (this);
117  memset( m_iv, 0x00, CryptoPP::AES::BLOCKSIZE );
118  m_authenticationTagLengthInBits = 32;//Wegman-Carter default value
119  m_qkdHeaderSize = 72;
120 }
121 
123 {
124  //NS_LOG_FUNCTION (this);
125 }
126 
127 std::vector<uint8_t>
129  std::string& input
130 ){
131  NS_LOG_FUNCTION( this << input.size() );
132  std::vector<uint8_t> outputVector;
133  const uint8_t* p = reinterpret_cast<const uint8_t*>(input.c_str());
134 
135  for(uint32_t i=0; i<input.size(); i++)
136  outputVector.push_back(p[i]);
137 
138  //delete p;
139  return outputVector;
140 }
141 
142 std::string
143 QKDCrypto::VectorToString(std::vector<uint8_t> inputVector)
144 {
145  NS_LOG_FUNCTION( this << inputVector.size() );
146 
147  //Copy from vector to uint8_t array
148  uint16_t b = 0;
149  uint8_t *messageContent = new uint8_t[inputVector.size()];
150  typename std::vector<uint8_t>::iterator itb = inputVector.begin();
151  for( ; itb != inputVector.end(); ++itb)
152  messageContent[b++] = *itb;
153 
154  //finally create the string
155  std::string output = std::string((char*)messageContent, inputVector.size());
156  delete[] messageContent;
157  return output;
158 }
159 
160 std::vector<uint8_t>
162 {
163  NS_LOG_FUNCTION ( this << qkdHeader );
164 
165  Buffer qkdHeaderBuffer;
166  qkdHeaderBuffer.AddAtStart (qkdHeader.GetSerializedSize());//20
167  qkdHeader.Serialize(qkdHeaderBuffer.Begin ());
168 
169  uint8_t *qkdBuffer = new uint8_t[qkdHeaderBuffer.GetSerializedSize() + 4];
170  uint32_t serializeOutput = qkdHeaderBuffer.Serialize(qkdBuffer, qkdHeaderBuffer.GetSerializedSize() + 4);
171  NS_LOG_FUNCTION(this << "QKD Header Serialize result: " << serializeOutput << qkdHeaderBuffer.GetSerializedSize() + 4);
172  m_qkdHeaderSize = qkdHeaderBuffer.GetSerializedSize() + 4;
173 
174  //check if serialized process was sucessful
175  NS_ASSERT(serializeOutput != 0);
176 
177  std::vector<uint8_t> qkdHeaderVector;
178  //Add to headers vector which is going to be encrypted
179  for(uint16_t a=0; a<(qkdHeaderBuffer.GetSerializedSize() + 4); a++)
180  qkdHeaderVector.push_back(qkdBuffer[a]);
181 
182  delete[] qkdBuffer;
183  return qkdHeaderVector;
184 }
185 
186 std::vector<uint8_t>
188 {
189  NS_LOG_FUNCTION ( this << qkdDHeader );
190 
191  Buffer qkdDHeaderBuffer;
192  qkdDHeaderBuffer.AddAtStart (qkdDHeader.GetSerializedSize());//20
193  qkdDHeader.Serialize(qkdDHeaderBuffer.Begin ());
194 
195  uint8_t *qkdBuffer = new uint8_t[qkdDHeaderBuffer.GetSerializedSize() + 4];
196  uint32_t serializeOutput = qkdDHeaderBuffer.Serialize(qkdBuffer, qkdDHeaderBuffer.GetSerializedSize() + 4);
197  NS_LOG_FUNCTION(this << "QKD Delimiter Header Serialize result: " << serializeOutput << qkdDHeaderBuffer.GetSerializedSize() + 4);
198  m_qkdDHeaderSize = qkdDHeaderBuffer.GetSerializedSize() + 4;
199 
200  NS_LOG_FUNCTION (this << "setting m_qkdDHeaderSize to" << m_qkdDHeaderSize);
201 
202  //check if serialized process was sucessful
203  NS_ASSERT(serializeOutput != 0);
204 
205  std::vector<uint8_t> qkdDHeaderVector;
206  //Add to headers vector which is going to be encrypted
207  for(uint16_t a=0; a<(qkdDHeaderBuffer.GetSerializedSize() + 4); a++){
208  qkdDHeaderVector.push_back(qkdBuffer[a]);
209  }
210 
211  delete[] qkdBuffer;
212  return qkdDHeaderVector;
213 }
214 
215 QKDHeader
216 QKDCrypto::StringToQKDHeader(std::string& input)
217 {
218  NS_LOG_FUNCTION (this
219  << input.size()
220  );
221 
222  const uint8_t* qkdBuffer = reinterpret_cast<const uint8_t*>(input.c_str());
223 
224  //Create QKDBuffer
225  Buffer qkdHeaderBuffer;
226  qkdHeaderBuffer.Deserialize( qkdBuffer, m_qkdHeaderSize );
227 
228  //Fetch QKDHeader
229  QKDHeader qkdHeader;
230  qkdHeader.Deserialize(qkdHeaderBuffer.Begin ());
231 
232  NS_LOG_FUNCTION (this << "QKD Header sucessfully Deserialized!" << qkdHeader );
233  //delete[] qkdBuffer;
234 
235  return qkdHeader;
236 }
237 
240 {
241  NS_LOG_FUNCTION (this
242  << input.size()
243  );
244 
245  const uint8_t* qkdBuffer = reinterpret_cast<const uint8_t*>(input.c_str());
246 
247  //Create QKDBuffer
248  Buffer qkdHeaderBuffer;
249  qkdHeaderBuffer.Deserialize( qkdBuffer, m_qkdDHeaderSize );
250 
251  //Fetch QKDDelimiterHeader
252  QKDDelimiterHeader qkdHeader;
253  qkdHeader.Deserialize(qkdHeaderBuffer.Begin ());
254 
255  NS_LOG_FUNCTION (this << "QKD Delimiter Header sucessfully Deserialized!" << qkdHeader );
256  //delete qkdBuffer;
257 
258  return qkdHeader;
259 }
260 
263 {
264  NS_LOG_FUNCTION(this << p);
265 
266  QKDCommandTag qkdCommandTag;
267  QKDCommandHeader qkdCommandHeader;
268 
269  //Check whether this is routing message from routing protocol
270  if(p->PeekPacketTag(qkdCommandTag)){
271  /*
272  std::cout << "\n......SENDING plain CreateQKDCommandHeader qkdCommandTag....." << p->GetUid() << "..." << p->GetSize() << ".....\n";
273  qkdCommandTag.Print(std::cout);
274  std::cout << "\n.............................\n";
275  */
276  if(qkdCommandTag.GetCommand () == 'R')
277  NS_LOG_FUNCTION (this << "IT IS ROUTING HELLO PACKET!" << qkdCommandTag.GetCommand() );
278  else {
279  NS_LOG_FUNCTION (this << "IT IS NORMAL PACKET!" << qkdCommandTag.GetCommand () );
280  }
281 
282  NS_LOG_FUNCTION (this << "RoutingProtocolNumber:" << qkdCommandTag.GetRoutingProtocolNumber ());
283  qkdCommandHeader.SetCommand ( qkdCommandTag.GetCommand() );//Routing hello message
284 
285  }else
286  NS_LOG_FUNCTION (this << "PACKET WITHOUT QKDCOMMAND TAG RECEIVED!");
287 
288  return qkdCommandHeader;
289 }
290 
291 
292 bool
294  Ptr<Packet> p,
295  uint32_t TOSBand,
296  Ptr<QKDBuffer> QKDbuffer
297 ){
298 
299  NS_LOG_FUNCTION(this << p << TOSBand);
300 
301  if(QKDbuffer == 0)
302  return false;
303 
304  QKDInternalTag tag;
305  p->PeekPacketTag(tag);
306 
307  uint8_t shouldEncrypt = tag.GetEncryptValue();
308  uint8_t shouldAuthenticate = tag.GetAuthenticateValue();
309  uint32_t keySize = 0;
310 
311  switch (shouldEncrypt)
312  {
313  case QKDCRYPTO_OTP:
314  keySize += p->GetSize() * 8;//in bits
315  break;
316 
317  case QKDCRYPTO_AES:
318  keySize += CryptoPP::AES::MAX_KEYLENGTH;
319  break;
320  }
321 
322  //KEY IS NEEDED ONLY FOR VMAC
323  if(shouldAuthenticate == QKDCRYPTO_AUTH_VMAC){
325  }
326 
327  uint32_t QKDbufferStatus = QKDbuffer->FetchState();
328  uint32_t Mcur = QKDbuffer->GetMcurrent();
329 
330  NS_LOG_DEBUG ( this
331  << "\tQKDBuffer: \t" << QKDbufferStatus
332  << "\tTOSband: \t" << TOSBand
333  << "\tKeySize: \t" << keySize
334  << "\n");
335 
336  NS_LOG_DEBUG ( this
337  << "\tQKDBuffer Material: \t" << Mcur
338  << "\tKeySize: \t" << keySize
339  << "\t Enough resources: \t" << (float) (Mcur > keySize)
340  << "\n");
341 
342  //if buffer is empty then only top priority packet can be transmitted
343  if(QKDbufferStatus == QKDBuffer::QKDSTATUS_EMPTY && TOSBand != 0){
344  NS_LOG_DEBUG(this << "Buffer is empty so only top priority packet can be transmitted! Returning false!");
345  return false;
346  }
347 
348  return Mcur > keySize;
349 }
350 
351 std::vector<Ptr<Packet> >
353  Ptr<Packet> p,
354  Ptr<QKDBuffer> QKDbuffer,
355  uint32_t channelID
356 )
357 {
358  QKDInternalTag tag;
359  if(p->PeekPacketTag(tag)) {
360  p->RemovePacketTag(tag);
361  }
362  uint8_t shouldEncrypt = tag.GetEncryptValue();
363  uint8_t shouldAuthenticate = tag.GetAuthenticateValue();
364 
365  NS_LOG_FUNCTION(this
366  << "Processing outgoing packet \n PacketID:" << p->GetUid()
367  << " of size: " << p->GetSize()
368  << "ChannelID:" << channelID
369  );
370  std::vector<Ptr<Packet> > packetOutput;
371 
372  //QKD INTERNAL NEXT HOP TAG
373  bool QKDInternalTOSTagPresent = false;
374  QKDInternalTOSTag qkdNextHopTag;
375  if(p->RemovePacketTag(qkdNextHopTag))
376  QKDInternalTOSTagPresent = true;
377 
378  std::string plainText;
379  std::string cipherText;
380  std::string authTag;
381 
382  QKDHeader qkdHeader;
383 
384  /*
385  Convert whole packet to string because of reassembly and fragmentation in the underlying network when TCP is used
386  Consider following example:
387 
388  - We crate three packets with three QKD header
389  |QKDHeader|***Payload***||QKDHeader|*************Payload|*************|
390 
391  - TCP in the underlying network transmit only small part of the packet, that is it creates fragmenatation like:
392  |QKDHeader|***Payload*|
393 
394  Then we need to store this small fragment and wait until we receive whole packet.
395 
396  - In next iteration we receive following small fragment
397 
398  |**||QKDHea
399 
400  It is easy to decrypt the first packet, but it is very dificult to decrypt the second one, because TCP might deliver only
401  small piece of QKD Header, and then we need to deal with half-QKDheader. Therefore, we decide to convert everything into string
402  (payload and QKDHeader), which is easeier for manipulation and storing.
403  It is just the implementation detail in the current version of QKD mosdule
404 
405 
406  Also, coversion of packet to string is necessary for packet authentication and encryption
407  */
408 
409  //Convert whole packet to string, just do it!
410  plainText = PacketToString (p);
411 
412  // COMPRES PACKET
414  plainText = StringCompressEncode (plainText);
415  qkdHeader.SetZipped (1);
416  }
417 
418  //Now perform encryption of the packet
419  if(shouldEncrypt > 0){
420 
421  NS_LOG_FUNCTION ( "***** ENCRYPTION MODE *****" << shouldEncrypt );
422 
424  // ENCRYPTION
426 
427  Ptr<QKDKey> key;
428  switch (shouldEncrypt)
429  {
430  case QKDCRYPTO_OTP:
431 
432  if(QKDbuffer != 0)
433  key = QKDbuffer->ProcessOutgoingRequest ( plainText.size() * 8 ); //In bits
434 
435  if(key == 0){
436  NS_LOG_FUNCTION ("NO KEY PROVIDED!");
437  NS_LOG_WARN ("NO ENOUGH KEY IN THE BUFFER! BUFFER IS EMPTY! ABORT ENCRYPTION and AUTHENTICATION PROCESS");
438  return packetOutput;
439  }else{
440  cipherText = OTP ( plainText, key );
441  m_encryptionTrace (p);
442  }
443  break;
444 
445  case QKDCRYPTO_AES:
446 
447  if(QKDbuffer != 0)
448  key = QKDbuffer->ProcessOutgoingRequest ( CryptoPP::AES::MAX_KEYLENGTH ); //AES in bits
449 
450  if(key == 0){
451  NS_LOG_FUNCTION ("NO KEY PROVIDED!");
452  NS_LOG_WARN ("NO ENOUGH KEY IN THE BUFFER! BUFFER IS EMPTY! ABORT ENCRYPTION and AUTHENTICATION PROCESS");
453  return packetOutput;
454  }else{
455  cipherText = AESEncrypt ( plainText, key );
456  m_encryptionTrace (p);
457  }
458  break;
459  }
460 
461  qkdHeader.SetEncryptionKeyId(key->GetUid());
462  qkdHeader.SetEncrypted (shouldEncrypt);
463 
464  NS_LOG_FUNCTION(this << "Encryption completed!");
465 
466  //or continue without encryption
467  }else
468  cipherText = plainText;
469 
470  qkdHeader.SetMessageId( p->GetUid() );
471  qkdHeader.SetChannelId(channelID);
472  qkdHeader.SetLength(m_qkdHeaderSize + cipherText.size());
473 
474 
475  Ptr<Packet> outputPacket;
476  //Now perform authentication if it is necessary
477  //Note: All encrypted packets MUST be authenticated as well!
478  if (shouldEncrypt>0 || shouldAuthenticate>0){
479 
480  NS_LOG_FUNCTION ( "***** AUTHENTICATION MODE *****" << shouldAuthenticate);
481 
483  // AUTHENTICATION
485 
486  Ptr<QKDKey> key;
487 
488  //KEY IS NEEDED ONLY FOR VMAC
489  if(shouldAuthenticate == QKDCRYPTO_AUTH_VMAC){
490 
491  if(QKDbuffer != 0)
492  key = QKDbuffer->ProcessOutgoingRequest ( m_authenticationTagLengthInBits ); //In bits
493 
494  if(key == 0){
495  NS_LOG_FUNCTION ("NO KEY PROVIDED!");
496  NS_LOG_WARN ("NO ENOUGH KEY IN THE BUFFER! BUFFER IS EMPTY! ABORT ENCRYPTION and AUTHENTICATION PROCESS");
497  return packetOutput;
498  }
499  }else
500  key = 0;
501 
502  authTag = Authenticate (cipherText, key, shouldAuthenticate);
503  m_authenticationTrace(p, authTag);
504  NS_LOG_FUNCTION(this << "Adding AUTHTAG to the packet!" << authTag << authTag.size() );
505 
506  qkdHeader.SetAuthenticationKeyId(key->GetUid());
507  qkdHeader.SetAuthTag(authTag);
508  qkdHeader.SetAuthenticated (shouldAuthenticate);
509 
510  }else{
511 
512  NS_LOG_FUNCTION ( "***** PLAIN TEXT MODE *****" );
513 
515  // PLAIN TEXT MODE
517 
518  qkdHeader.SetAuthenticated (0);
519  qkdHeader.SetEncrypted (0);
520  }
521 
522  /*
523  * Now convert QKDHeader to std::string and merge it with the rest of the packet
524  */
525 
526  //QKD Header is now ready and it can be encoded into packet
527  std::vector<uint8_t> qkdHeaderInVector = QKDHeaderToVector(qkdHeader);
528 
529  //Convert whole packet into vector
530  std::vector<uint8_t> packetContentInVector = StringToVector( cipherText );
531 
532  //Now add qkdheader in the begining of packet vector
533  qkdHeaderInVector.insert(qkdHeaderInVector.end(), packetContentInVector.begin(), packetContentInVector.end());
534 
535  //Finally convert everything to one string
536  std::string finalPayload = VectorToString(qkdHeaderInVector);
537 
538  //NS_LOG_FUNCTION (this << HexEncode(finalPayload) );
539  NS_LOG_FUNCTION( this
540  << "ChannelID:" << channelID
541  << "Final packetContent size:" << finalPayload.size()
542  );
543 
544  Ptr<Packet> packet = Create<Packet> ((uint8_t*) finalPayload.c_str(), finalPayload.size());
545  outputPacket = packet;
546 
547 
549  // ADDING INTERNAL NEXT HOP TAG
550  // This tag is needed since encrypted packet does not have IP,
551  // so it is a problem for QKDManager to reveal information about
552  // the underlying network (socket on which packet needs to be passed)
554 
555  if(QKDInternalTOSTagPresent){
556  NS_LOG_FUNCTION (this << "Adding QKD Internal NextHop Tag!");
557  outputPacket->AddPacketTag(qkdNextHopTag);
558  }
559 
561  // FINALLY, PACKET IS READY
563 
564  NS_LOG_FUNCTION(this
565  << "Final outgoing packet from QCrypto:"
566  << "PacketID:" << outputPacket->GetUid()
567  << "of size" << outputPacket->GetSize()
568  << "MessageID:" << qkdHeader.GetMessageId()
569  << "QKDHeaderLength:" << qkdHeader.GetLength()
570  << "ChannelID:" << channelID
571  );
572 
573  packetOutput.push_back(outputPacket);
574  return packetOutput;
575 }
576 
577 std::string
579 {
580  NS_LOG_FUNCTION (this << p->GetUid() << p->GetSize());
581  /*
582  std::cout << "\n ..................SENDING plain....." << p->GetUid() << "..." << p->GetSize() << "....... \n";
583  p->Print(std::cout);
584  std::cout << "\n ............................................ \n";
585  */
586  QKDCommandHeader qkdCommandHeader = CreateQKDCommandHeader(p);
587 
588  PacketMetadata::Item item;
589  PacketMetadata::ItemIterator metadataIterator = p->BeginItem();
590 
591  uint16_t counter = 0;
592  uint32_t firstNextHeader = 4; //IPv4 by default
593  uint16_t headerContentSize = 0;
594  std::vector<uint8_t> headerContentVector;
595 
596  NS_LOG_FUNCTION(this << firstNextHeader);
597 
598 
600  // SERALIZE ALL HEADERS OF THE PACKET
602 
603  while (metadataIterator.HasNext()) {
604 
605  item = metadataIterator.Next();
606  if(!item.tid.GetUid()) break;
607 
608  NS_LOG_FUNCTION(this << "---------");
609  NS_LOG_FUNCTION(this << item.tid.GetName() );
610 
612  // IPv4 Header Serialize
614 
615  if(item.tid.GetName() == "ns3::Ipv4Header")
616  {
617  Callback<ObjectBase *> constr = item.tid.GetConstructor();
618  NS_ASSERT(!constr.IsNull());
619 
620  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
621  ObjectBase *instance = constr();
622  NS_ASSERT(instance != 0);
623 
624  Ipv4Header* ipv4Header = dynamic_cast<Ipv4Header*> (instance);
625  NS_ASSERT(ipv4Header != 0);
626  ipv4Header->Deserialize(item.current);
627 
628  NS_LOG_FUNCTION(this << uint32_t (ipv4Header->GetProtocol()) << qkdCommandHeader);
629 
630  Buffer ipHeaderBuffer;
631  ipHeaderBuffer.AddAtStart (ipv4Header->GetSerializedSize());//20
632 
633  NS_LOG_FUNCTION (this << "Ipv4Header Protocol value: " << (uint32_t) ipv4Header->GetProtocol() );
634  ipv4Header->Serialize(ipHeaderBuffer.Begin ());
635  /*
636  std::cout << "\n....IPv4 HEADER....\n";
637  ipv4Header->Print(std::cout);
638  std::cout << "\n...................\n";
639  */
640 
641  // finished, clear the header
642  headerContentSize += ipv4Header->GetSerializedSize();
643  NS_LOG_FUNCTION(this << "IPv4 Header size: " << ipv4Header->GetSerializedSize());
644  delete ipv4Header;
645 
646  uint8_t *ipBuffer = new uint8_t[ipHeaderBuffer.GetSerializedSize() + 4];
647  uint32_t serializeOutput = ipHeaderBuffer.Serialize(ipBuffer, ipHeaderBuffer.GetSerializedSize() + 4);
648  NS_LOG_FUNCTION(this << "IPv4 Header Serialize result: " << serializeOutput << ipHeaderBuffer.GetSerializedSize() + 4);
649 
650  //SET HEADER SIZE
651  m_ipv4HeaderSize = ipHeaderBuffer.GetSerializedSize() + 4;
652 
653  //check if serialized process was sucessful
654  NS_ASSERT(serializeOutput != 0);
655 
656  //Add to headers vector which is going to be encrypted
657  for(uint16_t a=0; a<(ipHeaderBuffer.GetSerializedSize() + 4); a++)
658  headerContentVector.push_back(ipBuffer[a]);
659 
660  NS_LOG_FUNCTION ("PLAINTEXT.size after IPv4: " << headerContentVector.size() );
661 
662  if(counter == 0) firstNextHeader = 4;
663  NS_LOG_FUNCTION (this << "IPv4 Header serialized" << counter);
664 
665  delete[] ipBuffer;
666 
668  // ICMPv4 Header Serialize
670 
671  }else if(item.tid.GetName() == "ns3::Icmpv4Header")
672  {
673  Callback<ObjectBase *> constr = item.tid.GetConstructor();
674  NS_ASSERT(!constr.IsNull());
675 
676  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
677  ObjectBase *instance = constr();
678  NS_ASSERT(instance != 0);
679 
680  Icmpv4Header* icmpv4Header = dynamic_cast<Icmpv4Header*> (instance);
681  NS_ASSERT(icmpv4Header != 0);
682  icmpv4Header->Deserialize(item.current);
683  /*
684  std::cout << "\n....icmpv4Header HEADER....\n";
685  icmpv4Header->Print(std::cout);
686  std::cout << "\n...................\n";
687  */
688  Buffer icmpv4HeaderBuffer;
689  icmpv4HeaderBuffer.AddAtStart (icmpv4Header->GetSerializedSize());//8
690  icmpv4Header->Serialize(icmpv4HeaderBuffer.Begin ());
691 
692  // finished, clear the header
693  headerContentSize += icmpv4Header->GetSerializedSize();
694  NS_LOG_FUNCTION(this << "icmpv4Header size: " << icmpv4Header->GetSerializedSize());
695  delete icmpv4Header;
696 
697  uint8_t *icmpv4Buffer = new uint8_t[icmpv4HeaderBuffer.GetSerializedSize() + 4];
698  uint32_t serializeOutput = icmpv4HeaderBuffer.Serialize(icmpv4Buffer, icmpv4HeaderBuffer.GetSerializedSize() + 4);
699  NS_LOG_FUNCTION(this << "Icmpv4Header Header Serialize result: " << serializeOutput << icmpv4HeaderBuffer.GetSerializedSize() + 4);
700 
701  //SET HEADER SIZE
702  m_icmpv4HeaderSize = icmpv4HeaderBuffer.GetSerializedSize() + 4;
703 
704  //check if serialized process was sucessful
705  NS_ASSERT(serializeOutput != 0);
706 
707  //Add to headers vector which is going to be encrypted
708  for(uint16_t a=0; a<(icmpv4HeaderBuffer.GetSerializedSize() + 4); a++)
709  headerContentVector.push_back(icmpv4Buffer[a]);
710 
711  NS_LOG_FUNCTION ("PLAINTEXT.size after Icmpv4Header: " << headerContentVector.size() );
712 
713  if(counter == 0) firstNextHeader = 1;
714  NS_LOG_FUNCTION (this << "ICMPv4 Header serialized" << counter);
715  delete[] icmpv4Buffer;
716 
718  // Icmpv4 Destination Unreachable Header Serialize
720 
721  }else if(item.tid.GetName() == "ns3::Icmpv4DestinationUnreachable")
722  {
723  Callback<ObjectBase *> constr = item.tid.GetConstructor();
724  NS_ASSERT(!constr.IsNull());
725 
726  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
727  ObjectBase *instance = constr();
728  NS_ASSERT(instance != 0);
729 
730  Icmpv4DestinationUnreachable* header = dynamic_cast<Icmpv4DestinationUnreachable*> (instance);
731  NS_ASSERT(header != 0);
732  header->Deserialize(item.current);
733  /*
734  std::cout << "\n....header HEADER....\n";
735  header->Print(std::cout);
736  std::cout << "\n...................\n";
737  */
738  Buffer headerBuffer;
739  headerBuffer.AddAtStart (header->GetSerializedSize());//8
740  header->Serialize(headerBuffer.Begin ());
741 
742  // finished, clear the header
743  headerContentSize += header->GetSerializedSize();
744  NS_LOG_FUNCTION(this << "header size: " << header->GetSerializedSize());
745  delete header;
746 
747  uint8_t *headerSerializedBuffer = new uint8_t[headerBuffer.GetSerializedSize() + 4];
748  uint32_t serializeOutput = headerBuffer.Serialize(headerSerializedBuffer, headerBuffer.GetSerializedSize() + 4);
749  NS_LOG_FUNCTION(this << "Icmpv4DestinationUnreachable Header Serialize result: " << serializeOutput << headerBuffer.GetSerializedSize() + 4);
750 
751  //SET HEADER SIZE
753 
754  //check if serialized process was sucessful
755  NS_ASSERT(serializeOutput != 0);
756 
757  //Add to headers vector which is going to be encrypted
758  for(uint16_t a=0; a<(headerBuffer.GetSerializedSize() + 4); a++)
759  headerContentVector.push_back(headerSerializedBuffer[a]);
760 
761  NS_LOG_FUNCTION ("PLAINTEXT.size after Icmpv4DestinationUnreachable: " << headerContentVector.size() );
762 
763  if(counter == 0) firstNextHeader = 1;
764  NS_LOG_FUNCTION (this << "Icmpv4 Destination Unreachable Header serialized" << counter);
765 
766  delete[] headerSerializedBuffer;
767 
769  // ICMPv4 Echo Header Serialize
771 
772  }else if(item.tid.GetName() == "ns3::Icmpv4Echo")
773  {
774  Callback<ObjectBase *> constr = item.tid.GetConstructor();
775  NS_ASSERT(!constr.IsNull());
776 
777  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
778  ObjectBase *instance = constr();
779  NS_ASSERT(instance != 0);
780 
781  Icmpv4Echo* header = dynamic_cast<Icmpv4Echo*> (instance);
782  NS_ASSERT(header != 0);
783  header->Deserialize(item.current);
784  /*
785  std::cout << "\n....header HEADER....\n";
786  header->Print(std::cout);
787  std::cout << "\n...................\n";
788  */
789  Buffer headerBuffer;
790  headerBuffer.AddAtStart (header->GetSerializedSize());//8
791  header->Serialize(headerBuffer.Begin ());
792 
793  // finished, clear the header
794  headerContentSize += header->GetSerializedSize();
795  NS_LOG_FUNCTION(this << "header size: " << header->GetSerializedSize());
796  delete header;
797 
798  uint8_t *headerSerializedBuffer = new uint8_t[headerBuffer.GetSerializedSize() + 4];
799  uint32_t serializeOutput = headerBuffer.Serialize(headerSerializedBuffer, headerBuffer.GetSerializedSize() + 4);
800  NS_LOG_FUNCTION(this << "Icmpv4Echo Header Serialize result: " << serializeOutput << headerBuffer.GetSerializedSize() + 4);
801 
802  //SET HEADER SIZE
803  m_icmpv4EchoHeaderSize = headerBuffer.GetSerializedSize() + 4;
804 
805  //check if serialized process was sucessful
806  NS_ASSERT(serializeOutput != 0);
807 
808  //Add to headers vector which is going to be encrypted
809  for(uint16_t a=0; a<(headerBuffer.GetSerializedSize() + 4); a++)
810  headerContentVector.push_back(headerSerializedBuffer[a]);
811 
812  NS_LOG_FUNCTION ("PLAINTEXT.size after Icmpv4Echo: " << headerContentVector.size() );
813 
814  if(counter == 0) firstNextHeader = 1;
815  NS_LOG_FUNCTION (this << "ICMPv4 Echo Header serialized" << counter);
816 
817  delete[] headerSerializedBuffer;
818 
820  // Icmpv4 Time Exceeded Header Serialize
822 
823  }else if(item.tid.GetName() == "ns3::Icmpv4TimeExceeded")
824  {
825  Callback<ObjectBase *> constr = item.tid.GetConstructor();
826  NS_ASSERT(!constr.IsNull());
827 
828  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
829  ObjectBase *instance = constr();
830  NS_ASSERT(instance != 0);
831 
832  Icmpv4TimeExceeded* header = dynamic_cast<Icmpv4TimeExceeded*> (instance);
833  NS_ASSERT(header != 0);
834  header->Deserialize(item.current);
835  /*
836  std::cout << "\n....header HEADER....\n";
837  header->Print(std::cout);
838  std::cout << "\n...................\n";
839  */
840  Buffer headerBuffer;
841  headerBuffer.AddAtStart (header->GetSerializedSize());//8
842  header->Serialize(headerBuffer.Begin ());
843 
844  // finished, clear the header
845  headerContentSize += header->GetSerializedSize();
846  NS_LOG_FUNCTION(this << "header size: " << header->GetSerializedSize());
847  delete header;
848 
849  uint8_t *headerSerializedBuffer = new uint8_t[headerBuffer.GetSerializedSize() + 4];
850  uint32_t serializeOutput = headerBuffer.Serialize(headerSerializedBuffer, headerBuffer.GetSerializedSize() + 4);
851  NS_LOG_FUNCTION(this << "Icmpv4TimeExceeded Header Serialize result: " << serializeOutput << headerBuffer.GetSerializedSize() + 4);
852 
853  //SET HEADER SIZE
855 
856  //check if serialized process was sucessful
857  NS_ASSERT(serializeOutput != 0);
858 
859  //Add to headers vector which is going to be encrypted
860  for(uint16_t a=0; a<(headerBuffer.GetSerializedSize() + 4); a++)
861  headerContentVector.push_back(headerSerializedBuffer[a]);
862 
863  NS_LOG_FUNCTION ("PLAINTEXT.size after Icmpv4TimeExceeded: " << headerContentVector.size() );
864 
865  if(counter == 0) firstNextHeader = 1;
866  NS_LOG_FUNCTION (this << "Icmpv4 Time Exceeded Header serialized" << counter);
867 
868  delete[] headerSerializedBuffer;
869 
871  // UDP Header Serialize
873 
874  }else if(item.tid.GetName() == "ns3::UdpHeader")
875  {
876  Callback<ObjectBase *> constr = item.tid.GetConstructor();
877  NS_ASSERT(!constr.IsNull());
878 
879  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
880  ObjectBase *instance = constr();
881  NS_ASSERT(instance != 0);
882 
883  UdpHeader* udpHeader = dynamic_cast<UdpHeader*> (instance);
884  NS_ASSERT(udpHeader != 0);
885  udpHeader->Deserialize(item.current);
886  /*
887  std::cout << "\n....UDP HEADER....\n";
888  udpHeader->Print(std::cout);
889  std::cout << "\n...................\n";
890  */
891  Buffer udpHeaderBuffer;
892  udpHeaderBuffer.AddAtStart (udpHeader->GetSerializedSize());//8
893  udpHeader->Serialize(udpHeaderBuffer.Begin ());
894 
895  // finished, clear the header
896  headerContentSize += udpHeader->GetSerializedSize();
897  NS_LOG_FUNCTION(this << "udpHeader size: " << udpHeader->GetSerializedSize());
898  delete udpHeader;
899 
900  uint8_t *udpBuffer = new uint8_t[udpHeaderBuffer.GetSerializedSize() + 4];
901  uint32_t serializeOutput = udpHeaderBuffer.Serialize(udpBuffer, udpHeaderBuffer.GetSerializedSize() + 4);
902  NS_LOG_FUNCTION(this << "UDP Header Serialize result: " << serializeOutput << udpHeaderBuffer.GetSerializedSize() + 4);
903 
904  //SET HEADER SIZE
905  m_udpHeaderSize = udpHeaderBuffer.GetSerializedSize() + 4;
906 
907  //check if serialized process was sucessful
908  NS_ASSERT(serializeOutput != 0);
909 
910  //Add to headers vector which is going to be encrypted
911  for(uint16_t a=0; a<(udpHeaderBuffer.GetSerializedSize() + 4); a++)
912  headerContentVector.push_back(udpBuffer[a]);
913 
914  NS_LOG_FUNCTION ("PLAINTEXT.size after UdpHeader: " << headerContentVector.size() );
915 
916  if(counter == 0) firstNextHeader = 17;
917  NS_LOG_FUNCTION (this << "UDP Header serialized" << counter);
918 
919  delete[] udpBuffer;
920 
922  // TCP Header Serialize
924 
925  }else if(item.tid.GetName() == "ns3::TcpHeader")
926  {
927  Callback<ObjectBase *> constr = item.tid.GetConstructor();
928  NS_ASSERT(!constr.IsNull());
929 
930  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
931  ObjectBase *instance = constr();
932  NS_ASSERT(instance != 0);
933 
934  TcpHeader* tcpHeader = dynamic_cast<TcpHeader*> (instance);
935  NS_ASSERT(tcpHeader != 0);
936  tcpHeader->Deserialize(item.current);
937  /*
938  std::cout << "\n\n";
939  tcpHeader->Print(std::cout);
940  std::cout << "\n\n";
941  */
942  Buffer tcpHeaderBuffer;
943  tcpHeaderBuffer.AddAtStart (tcpHeader->GetSerializedSize());
944  tcpHeader->Serialize(tcpHeaderBuffer.Begin ());
945 
946  // finished, clear the header
947  headerContentSize += tcpHeader->GetSerializedSize();//tcpHeader->GetSerializedSize();
948  NS_LOG_FUNCTION(this << "tcpHeader size: " << tcpHeader->GetSerializedSize());
949  delete tcpHeader;
950 
951  uint8_t *tcpBuffer = new uint8_t[tcpHeaderBuffer.GetSerializedSize() + 4];
952  uint32_t serializeOutput = tcpHeaderBuffer.Serialize(tcpBuffer, tcpHeaderBuffer.GetSerializedSize() + 4);
953  NS_LOG_FUNCTION(this << "TCP Header Serialize result: " << serializeOutput << tcpHeaderBuffer.GetSerializedSize() + 4);
954 
955  //check if serialized process was sucessful
956  NS_ASSERT(serializeOutput != 0);
957 
958 
959  //Create QKDDelimiterHeader which is used for dynamic header so we know where header starts and where header stops
960  //Otherwise we do not know how to detect header for enrypted text
961  QKDDelimiterHeader qkdDHeader;
962  qkdDHeader.SetDelimiterSize ( tcpHeaderBuffer.GetSerializedSize() + 4 );
963 
964  std::vector<uint8_t> QKDDelimiterHeaderInVector = QKDDelimiterHeaderToVector(qkdDHeader);
965 
966  NS_LOG_FUNCTION(this << "Adding QKDDelimiterHeader of size" << QKDDelimiterHeaderInVector.size());
967  //Add to headers vector which is going to be encrypted
968  for(uint16_t a=0; a<QKDDelimiterHeaderInVector.size(); a++)
969  headerContentVector.push_back(QKDDelimiterHeaderInVector[a]);
970 
971  //FINALLY ADD TCP HEADER
972  //Add to headers vector which is going to be encrypted
973  for(uint16_t a=0; a<(tcpHeaderBuffer.GetSerializedSize() + 4); a++)
974  headerContentVector.push_back(tcpBuffer[a]);
975 
976  NS_LOG_FUNCTION ("PLAINTEXT.size after TcpHeader: " << headerContentVector.size() );
977 
978  if(counter == 0) firstNextHeader = 6;
979  NS_LOG_FUNCTION (this << "TCP Header serialized" << counter);
980 
981  delete[] tcpBuffer;
982 
984  // AODV Type Header Serialize
986  }else if(item.tid.GetName() == "ns3::aodv::TypeHeader")
987  {
988 
989  Callback<ObjectBase *> constr = item.tid.GetConstructor();
990  NS_ASSERT(!constr.IsNull());
991 
992  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
993  ObjectBase *instance = constr();
994  NS_ASSERT(instance != 0);
995 
996  ns3::aodv::TypeHeader* aodvPositionHeader = dynamic_cast<ns3::aodv::TypeHeader*> (instance);
997  NS_ASSERT(aodvPositionHeader != 0);
998  aodvPositionHeader->Deserialize(item.current);
999  /*
1000  std::cout << "\n\n";
1001  aodvPositionHeader->Print(std::cout);
1002  std::cout << "\n\n";
1003  */
1004  Buffer aodvHeaderBuffer;
1005  aodvHeaderBuffer.AddAtStart (aodvPositionHeader->GetSerializedSize());
1006  aodvPositionHeader->Serialize(aodvHeaderBuffer.Begin ());
1007 
1008  // finished, clear the header
1009  headerContentSize += aodvPositionHeader->GetSerializedSize();
1010  NS_LOG_FUNCTION(this << "aodv type Header size: " << aodvPositionHeader->GetSerializedSize());
1011  delete aodvPositionHeader;
1012 
1013  uint8_t *aodvBuffer = new uint8_t[aodvHeaderBuffer.GetSerializedSize() + 4];
1014  uint32_t serializeOutput = aodvHeaderBuffer.Serialize(aodvBuffer, aodvHeaderBuffer.GetSerializedSize() + 4);
1015  NS_LOG_FUNCTION(this << "AODV TypeHeader Serialize result: " << serializeOutput << aodvHeaderBuffer.GetSerializedSize() + 4);
1016 
1017  //SET HEADER SIZE
1018  m_aodvTypeHeaderSize = aodvHeaderBuffer.GetSerializedSize() + 4;
1019 
1020  //check if serialized process was sucessful
1021  NS_ASSERT(serializeOutput != 0);
1022 
1023  //Add to headers vector which is going to be encrypted
1024  for(uint16_t a=0; a<(aodvHeaderBuffer.GetSerializedSize() + 4); a++)
1025  headerContentVector.push_back(aodvBuffer[a]);
1026 
1027  NS_LOG_FUNCTION ("PLAINTEXT.size after aodv::TypeHeader: " << headerContentVector.size() );
1028 
1029  if(counter == 0) firstNextHeader = AODV_TYPE_HEADER_PROTOCOL_NUMBER;
1030  NS_LOG_FUNCTION (this << "AODV TypeHeader serialized" << counter);
1031 
1032  delete[] aodvBuffer;
1033 
1035  // AODV Rrep Header Serialize
1037  }else if(item.tid.GetName() == "ns3::aodv::RrepHeader")
1038  {
1039 
1040  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1041  NS_ASSERT(!constr.IsNull());
1042 
1043  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1044  ObjectBase *instance = constr();
1045  NS_ASSERT(instance != 0);
1046 
1047  ns3::aodv::RrepHeader* aodvPositionHeader = dynamic_cast<ns3::aodv::RrepHeader*> (instance);
1048  NS_ASSERT(aodvPositionHeader != 0);
1049  aodvPositionHeader->Deserialize(item.current);
1050  /*
1051  std::cout << "\n\n";
1052  aodvPositionHeader->Print(std::cout);
1053  std::cout << "\n\n";
1054  */
1055  Buffer aodvHeaderBuffer;
1056  aodvHeaderBuffer.AddAtStart (aodvPositionHeader->GetSerializedSize());
1057  aodvPositionHeader->Serialize(aodvHeaderBuffer.Begin ());
1058 
1059  // finished, clear the header
1060  headerContentSize += aodvPositionHeader->GetSerializedSize();
1061  NS_LOG_FUNCTION(this << "aodv RrepHeader size: " << aodvPositionHeader->GetSerializedSize());
1062  delete aodvPositionHeader;
1063 
1064  uint8_t *aodvBuffer = new uint8_t[aodvHeaderBuffer.GetSerializedSize() + 4];
1065  uint32_t serializeOutput = aodvHeaderBuffer.Serialize(aodvBuffer, aodvHeaderBuffer.GetSerializedSize() + 4);
1066  NS_LOG_FUNCTION(this << "AODV RrepHeader Serialize result: " << serializeOutput << aodvHeaderBuffer.GetSerializedSize() + 4);
1067 
1068  //SET HEADER SIZE
1069  m_aodvRrepHeaderSize = aodvHeaderBuffer.GetSerializedSize() + 4;
1070 
1071  //check if serialized process was sucessful
1072  NS_ASSERT(serializeOutput != 0);
1073 
1074  //Add to headers vector which is going to be encrypted
1075  for(uint16_t a=0; a<(aodvHeaderBuffer.GetSerializedSize() + 4); a++)
1076  headerContentVector.push_back(aodvBuffer[a]);
1077 
1078  NS_LOG_FUNCTION ("PLAINTEXT.size after aodv::RrepHeader: " << headerContentVector.size() );
1079 
1080  if(counter == 0) firstNextHeader = AODV_RREP_HEADER_PROTOCOL_NUMBER;
1081  NS_LOG_FUNCTION (this << "AODV RrepHeader serialized" << counter);
1082 
1083  delete[] aodvBuffer;
1084 
1086  // AODV Rreq Header Serialize
1088  }else if(item.tid.GetName() == "ns3::aodv::RreqHeader")
1089  {
1090 
1091  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1092  NS_ASSERT(!constr.IsNull());
1093 
1094  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1095  ObjectBase *instance = constr();
1096  NS_ASSERT(instance != 0);
1097 
1098  ns3::aodv::RreqHeader* aodvPositionHeader = dynamic_cast<ns3::aodv::RreqHeader*> (instance);
1099  NS_ASSERT(aodvPositionHeader != 0);
1100  aodvPositionHeader->Deserialize(item.current);
1101  /*
1102  std::cout << "\n\n";
1103  aodvPositionHeader->Print(std::cout);
1104  std::cout << "\n\n";
1105  */
1106  Buffer aodvHeaderBuffer;
1107  aodvHeaderBuffer.AddAtStart (aodvPositionHeader->GetSerializedSize());
1108  aodvPositionHeader->Serialize(aodvHeaderBuffer.Begin ());
1109 
1110  // finished, clear the header
1111  headerContentSize += aodvPositionHeader->GetSerializedSize();
1112  NS_LOG_FUNCTION(this << "aodv RreqHeader size: " << aodvPositionHeader->GetSerializedSize());
1113  delete aodvPositionHeader;
1114 
1115  uint8_t *aodvBuffer = new uint8_t[aodvHeaderBuffer.GetSerializedSize() + 4];
1116  uint32_t serializeOutput = aodvHeaderBuffer.Serialize(aodvBuffer, aodvHeaderBuffer.GetSerializedSize() + 4);
1117  NS_LOG_FUNCTION(this << "AODV RreqHeader Serialize result: " << serializeOutput << aodvHeaderBuffer.GetSerializedSize() + 4);
1118 
1119  //SET HEADER SIZE
1120  m_aodvRreqHeaderSize = aodvHeaderBuffer.GetSerializedSize() + 4;
1121 
1122  //check if serialized process was sucessful
1123  NS_ASSERT(serializeOutput != 0);
1124 
1125  //Add to headers vector which is going to be encrypted
1126  for(uint16_t a=0; a<(aodvHeaderBuffer.GetSerializedSize() + 4); a++)
1127  headerContentVector.push_back(aodvBuffer[a]);
1128 
1129  NS_LOG_FUNCTION ("PLAINTEXT.size after aodv::RreqHeader: " << headerContentVector.size() );
1130 
1131  if(counter == 0) firstNextHeader = AODV_RREQ_HEADER_PROTOCOL_NUMBER;
1132  NS_LOG_FUNCTION (this << "AODV RreqHeader serialized" << counter);
1133 
1134  delete[] aodvBuffer;
1135 
1137  // AODV Rrep Ack Header Serialize
1139  }else if(item.tid.GetName() == "ns3::aodv::RrepAckHeader")
1140  {
1141 
1142  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1143  NS_ASSERT(!constr.IsNull());
1144 
1145  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1146  ObjectBase *instance = constr();
1147  NS_ASSERT(instance != 0);
1148 
1149  ns3::aodv::RrepAckHeader* aodvPositionHeader = dynamic_cast<ns3::aodv::RrepAckHeader*> (instance);
1150  NS_ASSERT(aodvPositionHeader != 0);
1151  aodvPositionHeader->Deserialize(item.current);
1152  /*
1153  std::cout << "\n\n";
1154  aodvPositionHeader->Print(std::cout);
1155  std::cout << "\n\n";
1156  */
1157  Buffer aodvHeaderBuffer;
1158  aodvHeaderBuffer.AddAtStart (aodvPositionHeader->GetSerializedSize());
1159  aodvPositionHeader->Serialize(aodvHeaderBuffer.Begin ());
1160 
1161  // finished, clear the header
1162  headerContentSize += aodvPositionHeader->GetSerializedSize();
1163  NS_LOG_FUNCTION(this << "aodv RrepAckHeader size: " << aodvPositionHeader->GetSerializedSize());
1164  delete aodvPositionHeader;
1165 
1166  uint8_t *aodvBuffer = new uint8_t[aodvHeaderBuffer.GetSerializedSize() + 4];
1167  uint32_t serializeOutput = aodvHeaderBuffer.Serialize(aodvBuffer, aodvHeaderBuffer.GetSerializedSize() + 4);
1168  NS_LOG_FUNCTION(this << "AODV RrepAckHeader Serialize result: " << serializeOutput << aodvHeaderBuffer.GetSerializedSize() + 4);
1169 
1170  //SET HEADER SIZE
1171  m_aodvRrepAckHeaderSize = aodvHeaderBuffer.GetSerializedSize() + 4;
1172 
1173  //check if serialized process was sucessful
1174  NS_ASSERT(serializeOutput != 0);
1175 
1176  //Add to headers vector which is going to be encrypted
1177  for(uint16_t a=0; a<(aodvHeaderBuffer.GetSerializedSize() + 4); a++)
1178  headerContentVector.push_back(aodvBuffer[a]);
1179 
1180  NS_LOG_FUNCTION ("PLAINTEXT.size after aodv::RrepAckHeader: " << headerContentVector.size() );
1181 
1182  if(counter == 0) firstNextHeader = AODV_RREP_ACK_HEADER_PROTOCOL_NUMBER;
1183  NS_LOG_FUNCTION (this << "AODV RrepAckHeader serialized" << counter);
1184 
1185  delete[] aodvBuffer;
1186 
1188  // AODV Rrerr Header Serialize
1190  }else if(item.tid.GetName() == "ns3::aodv::RerrHeader")
1191  {
1192 
1193  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1194  NS_ASSERT(!constr.IsNull());
1195 
1196  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1197  ObjectBase *instance = constr();
1198  NS_ASSERT(instance != 0);
1199 
1200  ns3::aodv::RerrHeader* aodvPositionHeader = dynamic_cast<ns3::aodv::RerrHeader*> (instance);
1201  NS_ASSERT(aodvPositionHeader != 0);
1202  aodvPositionHeader->Deserialize(item.current);
1203  /*
1204  std::cout << "\n\n";
1205  aodvPositionHeader->Print(std::cout);
1206  std::cout << "\n\n";
1207  */
1208  Buffer aodvHeaderBuffer;
1209  aodvHeaderBuffer.AddAtStart (aodvPositionHeader->GetSerializedSize());
1210  aodvPositionHeader->Serialize(aodvHeaderBuffer.Begin ());
1211 
1212  // finished, clear the header
1213  headerContentSize += aodvPositionHeader->GetSerializedSize();
1214  NS_LOG_FUNCTION(this << "aodv RerrHeader size: " << aodvPositionHeader->GetSerializedSize());
1215  delete aodvPositionHeader;
1216 
1217  uint8_t *aodvBuffer = new uint8_t[aodvHeaderBuffer.GetSerializedSize() + 4];
1218  uint32_t serializeOutput = aodvHeaderBuffer.Serialize(aodvBuffer, aodvHeaderBuffer.GetSerializedSize() + 4);
1219  NS_LOG_FUNCTION(this << "AODV RerrHeader Serialize result: " << serializeOutput << aodvHeaderBuffer.GetSerializedSize() + 4);
1220 
1221  //SET HEADER SIZE
1222  m_aodvRerrHeaderSize = aodvHeaderBuffer.GetSerializedSize() + 4;
1223 
1224  //check if serialized process was sucessful
1225  NS_ASSERT(serializeOutput != 0);
1226 
1227  //Add to headers vector which is going to be encrypted
1228  for(uint16_t a=0; a<(aodvHeaderBuffer.GetSerializedSize() + 4); a++)
1229  headerContentVector.push_back(aodvBuffer[a]);
1230 
1231  NS_LOG_FUNCTION ("PLAINTEXT.size after aodv::RerrHeader: " << headerContentVector.size() );
1232 
1233  if(counter == 0) firstNextHeader = AODV_RERR_HEADER_PROTOCOL_NUMBER;
1234  NS_LOG_FUNCTION (this << "AODV RerrHeader serialized" << counter);
1235 
1236  delete[] aodvBuffer;
1237 
1239  // AODVQ Type Header Serialize
1241  }else if(item.tid.GetName() == "ns3::aodvq::TypeHeader")
1242  {
1243 
1244  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1245  NS_ASSERT(!constr.IsNull());
1246 
1247  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1248  ObjectBase *instance = constr();
1249  NS_ASSERT(instance != 0);
1250 
1251  ns3::aodvq::TypeHeader* aodvqPositionHeader = dynamic_cast<ns3::aodvq::TypeHeader*> (instance);
1252  NS_ASSERT(aodvqPositionHeader != 0);
1253  aodvqPositionHeader->Deserialize(item.current);
1254  /*
1255  std::cout << "\n\n";
1256  aodvqPositionHeader->Print(std::cout);
1257  std::cout << "\n\n";
1258  */
1259  Buffer aodvqHeaderBuffer;
1260  aodvqHeaderBuffer.AddAtStart (aodvqPositionHeader->GetSerializedSize());
1261  aodvqPositionHeader->Serialize(aodvqHeaderBuffer.Begin ());
1262 
1263  // finished, clear the header
1264  headerContentSize += aodvqPositionHeader->GetSerializedSize();
1265  NS_LOG_FUNCTION(this << "aodvq type Header size: " << aodvqPositionHeader->GetSerializedSize());
1266  delete aodvqPositionHeader;
1267 
1268  uint8_t *aodvqBuffer = new uint8_t[aodvqHeaderBuffer.GetSerializedSize() + 4];
1269  uint32_t serializeOutput = aodvqHeaderBuffer.Serialize(aodvqBuffer, aodvqHeaderBuffer.GetSerializedSize() + 4);
1270  NS_LOG_FUNCTION(this << "AODVQ TypeHeader Serialize result: " << serializeOutput << aodvqHeaderBuffer.GetSerializedSize() + 4);
1271 
1272  //SET HEADER SIZE
1273  m_aodvqTypeHeaderSize = aodvqHeaderBuffer.GetSerializedSize() + 4;
1274 
1275  //check if serialized process was sucessful
1276  NS_ASSERT(serializeOutput != 0);
1277 
1278  //Add to headers vector which is going to be encrypted
1279  for(uint16_t a=0; a<(aodvqHeaderBuffer.GetSerializedSize() + 4); a++)
1280  headerContentVector.push_back(aodvqBuffer[a]);
1281 
1282  NS_LOG_FUNCTION ("PLAINTEXT.size after aodvq::TypeHeader: " << headerContentVector.size() );
1283 
1284  if(counter == 0) firstNextHeader = AODVQ_TYPE_HEADER_PROTOCOL_NUMBER;
1285  NS_LOG_FUNCTION (this << "AODVQ TypeHeader serialized" << counter);
1286 
1287  delete[] aodvqBuffer;
1288 
1290  // AODVQ Rrep Header Serialize
1292  }else if(item.tid.GetName() == "ns3::aodvq::RrepHeader")
1293  {
1294 
1295  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1296  NS_ASSERT(!constr.IsNull());
1297 
1298  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1299  ObjectBase *instance = constr();
1300  NS_ASSERT(instance != 0);
1301 
1302  ns3::aodvq::RrepHeader* aodvqPositionHeader = dynamic_cast<ns3::aodvq::RrepHeader*> (instance);
1303  NS_ASSERT(aodvqPositionHeader != 0);
1304  aodvqPositionHeader->Deserialize(item.current);
1305  /*
1306  std::cout << "\n\n";
1307  aodvqPositionHeader->Print(std::cout);
1308  std::cout << "\n\n";
1309  */
1310  Buffer aodvqHeaderBuffer;
1311  aodvqHeaderBuffer.AddAtStart (aodvqPositionHeader->GetSerializedSize());
1312  aodvqPositionHeader->Serialize(aodvqHeaderBuffer.Begin ());
1313 
1314  // finished, clear the header
1315  headerContentSize += aodvqPositionHeader->GetSerializedSize();
1316  NS_LOG_FUNCTION(this << "aodvq RrepHeader size: " << aodvqPositionHeader->GetSerializedSize());
1317  delete aodvqPositionHeader;
1318 
1319  uint8_t *aodvqBuffer = new uint8_t[aodvqHeaderBuffer.GetSerializedSize() + 4];
1320  uint32_t serializeOutput = aodvqHeaderBuffer.Serialize(aodvqBuffer, aodvqHeaderBuffer.GetSerializedSize() + 4);
1321  NS_LOG_FUNCTION(this << "AODVQ RrepHeader Serialize result: " << serializeOutput << aodvqHeaderBuffer.GetSerializedSize() + 4);
1322 
1323  //SET HEADER SIZE
1324  m_aodvqRrepHeaderSize = aodvqHeaderBuffer.GetSerializedSize() + 4;
1325 
1326  //check if serialized process was sucessful
1327  NS_ASSERT(serializeOutput != 0);
1328 
1329  //Add to headers vector which is going to be encrypted
1330  for(uint16_t a=0; a<(aodvqHeaderBuffer.GetSerializedSize() + 4); a++)
1331  headerContentVector.push_back(aodvqBuffer[a]);
1332 
1333  NS_LOG_FUNCTION ("PLAINTEXT.size after aodvq::RrepHeader: " << headerContentVector.size() );
1334 
1335  if(counter == 0) firstNextHeader = AODVQ_RREP_HEADER_PROTOCOL_NUMBER;
1336  NS_LOG_FUNCTION (this << "AODVQ RrepHeader serialized" << counter);
1337 
1338  delete[] aodvqBuffer;
1339 
1341  // AODVQ Rreq Header Serialize
1343  }else if(item.tid.GetName() == "ns3::aodvq::RreqHeader")
1344  {
1345 
1346  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1347  NS_ASSERT(!constr.IsNull());
1348 
1349  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1350  ObjectBase *instance = constr();
1351  NS_ASSERT(instance != 0);
1352 
1353  ns3::aodvq::RreqHeader* aodvqPositionHeader = dynamic_cast<ns3::aodvq::RreqHeader*> (instance);
1354  NS_ASSERT(aodvqPositionHeader != 0);
1355  aodvqPositionHeader->Deserialize(item.current);
1356  /*
1357  std::cout << "\n\n";
1358  aodvqPositionHeader->Print(std::cout);
1359  std::cout << "\n\n";
1360  */
1361  Buffer aodvqHeaderBuffer;
1362  aodvqHeaderBuffer.AddAtStart (aodvqPositionHeader->GetSerializedSize());
1363  aodvqPositionHeader->Serialize(aodvqHeaderBuffer.Begin ());
1364 
1365  // finished, clear the header
1366  headerContentSize += aodvqPositionHeader->GetSerializedSize();
1367  NS_LOG_FUNCTION(this << "aodvq RreqHeader size: " << aodvqPositionHeader->GetSerializedSize());
1368  delete aodvqPositionHeader;
1369 
1370  uint8_t *aodvqBuffer = new uint8_t[aodvqHeaderBuffer.GetSerializedSize() + 4];
1371  uint32_t serializeOutput = aodvqHeaderBuffer.Serialize(aodvqBuffer, aodvqHeaderBuffer.GetSerializedSize() + 4);
1372  NS_LOG_FUNCTION(this << "AODVQ RreqHeader Serialize result: " << serializeOutput << aodvqHeaderBuffer.GetSerializedSize() + 4);
1373 
1374  //SET HEADER SIZE
1375  m_aodvqRreqHeaderSize = aodvqHeaderBuffer.GetSerializedSize() + 4;
1376 
1377  //check if serialized process was sucessful
1378  NS_ASSERT(serializeOutput != 0);
1379 
1380  //Add to headers vector which is going to be encrypted
1381  for(uint16_t a=0; a<(aodvqHeaderBuffer.GetSerializedSize() + 4); a++)
1382  headerContentVector.push_back(aodvqBuffer[a]);
1383 
1384  NS_LOG_FUNCTION ("PLAINTEXT.size after aodvq::RreqHeader: " << headerContentVector.size() );
1385 
1386  if(counter == 0) firstNextHeader = AODVQ_RREQ_HEADER_PROTOCOL_NUMBER;
1387  NS_LOG_FUNCTION (this << "AODVQ RreqHeader serialized" << counter);
1388 
1389  delete[] aodvqBuffer;
1390 
1392  // AODVQ Rrep Ack Header Serialize
1394  }else if(item.tid.GetName() == "ns3::aodvq::RrepAckHeader")
1395  {
1396 
1397  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1398  NS_ASSERT(!constr.IsNull());
1399 
1400  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1401  ObjectBase *instance = constr();
1402  NS_ASSERT(instance != 0);
1403 
1404  ns3::aodvq::RrepAckHeader* aodvqPositionHeader = dynamic_cast<ns3::aodvq::RrepAckHeader*> (instance);
1405  NS_ASSERT(aodvqPositionHeader != 0);
1406  aodvqPositionHeader->Deserialize(item.current);
1407  /*
1408  std::cout << "\n\n";
1409  aodvqPositionHeader->Print(std::cout);
1410  std::cout << "\n\n";
1411  */
1412  Buffer aodvqHeaderBuffer;
1413  aodvqHeaderBuffer.AddAtStart (aodvqPositionHeader->GetSerializedSize());
1414  aodvqPositionHeader->Serialize(aodvqHeaderBuffer.Begin ());
1415 
1416  // finished, clear the header
1417  headerContentSize += aodvqPositionHeader->GetSerializedSize();
1418  NS_LOG_FUNCTION(this << "aodvq RrepAckHeader size: " << aodvqPositionHeader->GetSerializedSize());
1419  delete aodvqPositionHeader;
1420 
1421  uint8_t *aodvqBuffer = new uint8_t[aodvqHeaderBuffer.GetSerializedSize() + 4];
1422  uint32_t serializeOutput = aodvqHeaderBuffer.Serialize(aodvqBuffer, aodvqHeaderBuffer.GetSerializedSize() + 4);
1423  NS_LOG_FUNCTION(this << "AODVQ RrepAckHeader Serialize result: " << serializeOutput << aodvqHeaderBuffer.GetSerializedSize() + 4);
1424 
1425  //SET HEADER SIZE
1426  m_aodvqRrepAckHeaderSize = aodvqHeaderBuffer.GetSerializedSize() + 4;
1427 
1428  //check if serialized process was sucessful
1429  NS_ASSERT(serializeOutput != 0);
1430 
1431  //Add to headers vector which is going to be encrypted
1432  for(uint16_t a=0; a<(aodvqHeaderBuffer.GetSerializedSize() + 4); a++)
1433  headerContentVector.push_back(aodvqBuffer[a]);
1434 
1435  NS_LOG_FUNCTION ("PLAINTEXT.size after aodvq::RrepAckHeader: " << headerContentVector.size() );
1436 
1437  if(counter == 0) firstNextHeader = AODVQ_RREP_ACK_HEADER_PROTOCOL_NUMBER;
1438  NS_LOG_FUNCTION (this << "AODVQ RrepAckHeader serialized" << counter);
1439 
1440  delete[] aodvqBuffer;
1441 
1443  // AODVQ Rrerr Header Serialize
1445  }else if(item.tid.GetName() == "ns3::aodvq::RerrHeader")
1446  {
1447 
1448  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1449  NS_ASSERT(!constr.IsNull());
1450 
1451  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1452  ObjectBase *instance = constr();
1453  NS_ASSERT(instance != 0);
1454 
1455  ns3::aodvq::RerrHeader* aodvqPositionHeader = dynamic_cast<ns3::aodvq::RerrHeader*> (instance);
1456  NS_ASSERT(aodvqPositionHeader != 0);
1457  aodvqPositionHeader->Deserialize(item.current);
1458  /*
1459  std::cout << "\n\n";
1460  aodvqPositionHeader->Print(std::cout);
1461  std::cout << "\n\n";
1462  */
1463  Buffer aodvqHeaderBuffer;
1464  aodvqHeaderBuffer.AddAtStart (aodvqPositionHeader->GetSerializedSize());
1465  aodvqPositionHeader->Serialize(aodvqHeaderBuffer.Begin ());
1466 
1467  // finished, clear the header
1468  headerContentSize += aodvqPositionHeader->GetSerializedSize();
1469  NS_LOG_FUNCTION(this << "aodvq RerrHeader size: " << aodvqPositionHeader->GetSerializedSize());
1470  delete aodvqPositionHeader;
1471 
1472  uint8_t *aodvqBuffer = new uint8_t[aodvqHeaderBuffer.GetSerializedSize() + 4];
1473  uint32_t serializeOutput = aodvqHeaderBuffer.Serialize(aodvqBuffer, aodvqHeaderBuffer.GetSerializedSize() + 4);
1474  NS_LOG_FUNCTION(this << "AODVQ RerrHeader Serialize result: " << serializeOutput << aodvqHeaderBuffer.GetSerializedSize() + 4);
1475 
1476  //SET HEADER SIZE
1477  m_aodvqRerrHeaderSize = aodvqHeaderBuffer.GetSerializedSize() + 4;
1478 
1479  //check if serialized process was sucessful
1480  NS_ASSERT(serializeOutput != 0);
1481 
1482  //Add to headers vector which is going to be encrypted
1483  for(uint16_t a=0; a<(aodvqHeaderBuffer.GetSerializedSize() + 4); a++)
1484  headerContentVector.push_back(aodvqBuffer[a]);
1485 
1486  NS_LOG_FUNCTION ("PLAINTEXT.size after aodvq::RerrHeader: " << headerContentVector.size() );
1487 
1488  if(counter == 0) firstNextHeader = AODVQ_RERR_HEADER_PROTOCOL_NUMBER;
1489  NS_LOG_FUNCTION (this << "AODVQ RerrHeader serialized" << counter);
1490 
1491  delete[] aodvqBuffer;
1492 
1494  // OLSR PacketHeader Serialize
1496  }else if(item.tid.GetName() == "ns3::olsr::PacketHeader")
1497  {
1498 
1499  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1500  NS_ASSERT(!constr.IsNull());
1501 
1502  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1503  ObjectBase *instance = constr();
1504  NS_ASSERT(instance != 0);
1505 
1506  ns3::olsr::PacketHeader* olsrPositionHeader = dynamic_cast<ns3::olsr::PacketHeader*> (instance);
1507  NS_ASSERT(olsrPositionHeader != 0);
1508  olsrPositionHeader->Deserialize(item.current);
1509  /*
1510  std::cout << "\n\n";
1511  olsrPositionHeader->Print(std::cout);
1512  std::cout << "\n\n";
1513  */
1514  Buffer olsrHeaderBuffer;
1515  olsrHeaderBuffer.AddAtStart ( olsrPositionHeader->GetSerializedSize() );
1516  olsrPositionHeader->Serialize(olsrHeaderBuffer.Begin ());
1517 
1518  // finished, clear the header
1519  headerContentSize += olsrPositionHeader->GetSerializedSize();
1520  NS_LOG_FUNCTION(this << "olsr PacketHeader size: " << olsrPositionHeader->GetSerializedSize());
1521  delete olsrPositionHeader;
1522 
1523  uint8_t *olsrBuffer = new uint8_t[olsrHeaderBuffer.GetSerializedSize() + 4];
1524  uint32_t serializeOutput = olsrHeaderBuffer.Serialize(olsrBuffer, olsrHeaderBuffer.GetSerializedSize() + 4);
1525  NS_LOG_FUNCTION(this << "olsr PacketHeader Serialize result: " << serializeOutput << olsrHeaderBuffer.GetSerializedSize() + 4);
1526 
1527  //SET HEADER SIZE
1528  m_olsrPacketHeaderSize = olsrHeaderBuffer.GetSerializedSize() + 4;
1529 
1530  //check if serialized process was sucessful
1531  NS_ASSERT(serializeOutput != 0);
1532 
1533  //Add to headers vector which is going to be encrypted
1534  for(uint16_t a=0; a<(olsrHeaderBuffer.GetSerializedSize() + 4); a++)
1535  headerContentVector.push_back(olsrBuffer[a]);
1536 
1537  NS_LOG_FUNCTION ("PLAINTEXT.size after olsr::PacketHeader: " << headerContentVector.size() );
1538 
1539  if(counter == 0) firstNextHeader = OLSR_PACKET_HEADER_PROTOCOL_NUMBER;
1540  NS_LOG_FUNCTION (this << "olsr PacketHeader serialized" << counter);
1541 
1542  delete[] olsrBuffer;
1543 
1545  // OLSR MessageHeader Serialize
1547  }else if(item.tid.GetName() == "ns3::olsr::MessageHeader")
1548  {
1549 
1550  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1551  NS_ASSERT(!constr.IsNull());
1552 
1553  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1554  ObjectBase *instance = constr();
1555  NS_ASSERT(instance != 0);
1556 
1557  ns3::olsr::MessageHeader* olsrPositionHeader = dynamic_cast<ns3::olsr::MessageHeader*> (instance);
1558  NS_ASSERT(olsrPositionHeader != 0);
1559  olsrPositionHeader->Deserialize(item.current);
1560  /*
1561  std::cout << "\n\n";
1562  olsrPositionHeader->Print(std::cout);
1563  std::cout << "\n\n";
1564  */
1565  Buffer olsrHeaderBuffer;
1566  olsrHeaderBuffer.AddAtStart ( olsrPositionHeader->GetSerializedSize() );
1567  olsrPositionHeader->Serialize(olsrHeaderBuffer.Begin ());
1568 
1569  // finished, clear the header
1570  headerContentSize += olsrPositionHeader->GetSerializedSize();
1571  NS_LOG_FUNCTION(this << "olsr MessageHeader size: " << olsrPositionHeader->GetSerializedSize());
1572  NS_LOG_FUNCTION(this << "olsr MessageHeader type: " << olsrPositionHeader->GetMessageType());
1573  delete olsrPositionHeader;
1574 
1575  uint8_t *olsrBuffer = new uint8_t[olsrHeaderBuffer.GetSerializedSize() + 4];
1576  uint32_t serializeOutput = olsrHeaderBuffer.Serialize(olsrBuffer, olsrHeaderBuffer.GetSerializedSize() + 4);
1577  NS_LOG_FUNCTION(this << "olsr MessageHeader Serialize result: " << serializeOutput << olsrHeaderBuffer.GetSerializedSize() + 4);
1578 
1579  //check if serialized process was sucessful
1580  NS_ASSERT(serializeOutput != 0);
1581 
1582  //Create QKDDelimiterHeader which is used for dynamic header so we know where header starts and where header stops
1583  //Otherwise we do not know how to detect header for enrypted text
1584  QKDDelimiterHeader qkdDHeader;
1585  qkdDHeader.SetDelimiterSize ( olsrHeaderBuffer.GetSerializedSize() + 4 );
1586 
1587  std::vector<uint8_t> QKDDelimiterHeaderInVector = QKDDelimiterHeaderToVector(qkdDHeader);
1588 
1589  NS_LOG_FUNCTION(this << "Adding QKDDelimiterHeader of size" << QKDDelimiterHeaderInVector.size());
1590  //Add to headers vector which is going to be encrypted
1591  for(uint16_t a=0; a<QKDDelimiterHeaderInVector.size(); a++)
1592  headerContentVector.push_back(QKDDelimiterHeaderInVector[a]);
1593 
1594  //Finaly add OLSR vector
1595  //Add to headers vector which is going to be encrypted
1596  NS_LOG_FUNCTION(this << "Adding OLSR MessageHeader of size" << olsrHeaderBuffer.GetSerializedSize() + 4 );
1597  for(uint16_t a=0; a<(olsrHeaderBuffer.GetSerializedSize() + 4); a++)
1598  headerContentVector.push_back(olsrBuffer[a]);
1599 
1600  NS_LOG_FUNCTION ("PLAINTEXT.size after olsr::MessageHeader: " << headerContentVector.size() );
1601 
1602  if(counter == 0) firstNextHeader = OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER;
1603  NS_LOG_FUNCTION (this << "olsr MessageHeader serialized" << counter);
1604 
1605  delete[] olsrBuffer;
1606 
1608  // DSDVQ Header Serialize
1610  }else if(item.tid.GetName() == "ns3::dsdvq::DsdvqHeader")
1611  {
1612  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1613  NS_ASSERT(!constr.IsNull());
1614 
1615  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1616  ObjectBase *instance = constr();
1617  NS_ASSERT(instance != 0);
1618 
1619  ns3::dsdvq::DsdvqHeader* DsdvqHeader = dynamic_cast<ns3::dsdvq::DsdvqHeader*> (instance);
1620  NS_ASSERT(DsdvqHeader != 0);
1621  DsdvqHeader->Deserialize(item.current);
1622  /*
1623  std::cout << "\n\n";
1624  DsdvqHeader->Print(std::cout);
1625  std::cout << "\n\n";
1626  */
1627  Buffer dsdvqHeaderBuffer;
1628  dsdvqHeaderBuffer.AddAtStart (DsdvqHeader->GetSerializedSize());//8
1629  DsdvqHeader->Serialize(dsdvqHeaderBuffer.Begin ());
1630 
1631  // finished, clear the header
1632  headerContentSize += DsdvqHeader->GetSerializedSize();
1633  NS_LOG_FUNCTION(this << "dsdvq Header size: " << DsdvqHeader->GetSerializedSize());
1634  delete DsdvqHeader;
1635 
1636  uint8_t *dsdvqBuffer = new uint8_t[dsdvqHeaderBuffer.GetSerializedSize() + 4];
1637  uint32_t serializeOutput = dsdvqHeaderBuffer.Serialize(dsdvqBuffer, dsdvqHeaderBuffer.GetSerializedSize() + 4);
1638  NS_LOG_FUNCTION(this << "DSDVQ HelloHeader Serialize result: " << serializeOutput << dsdvqHeaderBuffer.GetSerializedSize() + 4);
1639 
1640  //SET HEADER SIZE
1641  m_dsdvqHeaderSize = dsdvqHeaderBuffer.GetSerializedSize() + 4;
1642 
1643  //check if serialized process was sucessful
1644  NS_ASSERT(serializeOutput != 0);
1645 
1646  //Add to headers vector which is going to be encrypted
1647  for(uint16_t a=0; a<(dsdvqHeaderBuffer.GetSerializedSize() + 4); a++)
1648  headerContentVector.push_back(dsdvqBuffer[a]);
1649 
1650  NS_LOG_FUNCTION ("PLAINTEXT.size after dsdvq::DsdvqHeader: " << headerContentVector.size() );
1651 
1652  if(counter == 0) firstNextHeader = DSDVQ_PACKET_HEADER_PROTOCOL_NUMBER;
1653  NS_LOG_FUNCTION (this << "DSDVQ HelloHeader serialized" << counter);
1654 
1655  delete[] dsdvqBuffer;
1657  // DSDV Header Serialize
1659  }else if(item.tid.GetName() == "ns3::dsdv::DsdvHeader")
1660  {
1661  Callback<ObjectBase *> constr = item.tid.GetConstructor();
1662  NS_ASSERT(!constr.IsNull());
1663 
1664  // Ptr<> and DynamicCast<> won't work here as all headers are from ObjectBase, not Object
1665  ObjectBase *instance = constr();
1666  NS_ASSERT(instance != 0);
1667 
1668  ns3::dsdv::DsdvHeader* DsdvHeader = dynamic_cast<ns3::dsdv::DsdvHeader*> (instance);
1669  NS_ASSERT(DsdvHeader != 0);
1670  DsdvHeader->Deserialize(item.current);
1671  /*
1672  std::cout << "\n\n";
1673  DsdvHeader->Print(std::cout);
1674  std::cout << "\n\n";
1675  */
1676  Buffer dsdvHeaderBuffer;
1677  dsdvHeaderBuffer.AddAtStart (DsdvHeader->GetSerializedSize());//8
1678  DsdvHeader->Serialize(dsdvHeaderBuffer.Begin ());
1679 
1680  // finished, clear the header
1681  headerContentSize += DsdvHeader->GetSerializedSize();
1682  NS_LOG_FUNCTION(this << "dsdv Header size: " << DsdvHeader->GetSerializedSize());
1683  delete DsdvHeader;
1684 
1685  uint8_t *dsdvBuffer = new uint8_t[dsdvHeaderBuffer.GetSerializedSize() + 4];
1686  uint32_t serializeOutput = dsdvHeaderBuffer.Serialize(dsdvBuffer, dsdvHeaderBuffer.GetSerializedSize() + 4);
1687  NS_LOG_FUNCTION(this << "DSDV HelloHeader Serialize result: " << serializeOutput << dsdvHeaderBuffer.GetSerializedSize() + 4);
1688 
1689  //SET HEADER SIZE
1690  m_dsdvHeaderSize = dsdvHeaderBuffer.GetSerializedSize() + 4;
1691 
1692  //check if serialized process was sucessful
1693  NS_ASSERT(serializeOutput != 0);
1694 
1695  //Add to headers vector which is going to be encrypted
1696  for(uint16_t a=0; a<(dsdvHeaderBuffer.GetSerializedSize() + 4); a++)
1697  headerContentVector.push_back(dsdvBuffer[a]);
1698 
1699  NS_LOG_FUNCTION ("PLAINTEXT.size after dsdv::DsdvHeader: " << headerContentVector.size() );
1700 
1701  if(counter == 0) firstNextHeader = DSDV_PACKET_HEADER_PROTOCOL_NUMBER;
1702  NS_LOG_FUNCTION (this << "DSDV HelloHeader serialized" << counter);
1703 
1704  delete[] dsdvBuffer;
1705 
1706  }else {
1707  NS_LOG_FUNCTION ( this << item.tid.GetName() << item.tid.GetSize() << counter );
1708  //EXIT HERE BECAUSE HEADER WAS NOT PROPERLY DETECTED!
1709  NS_ASSERT_MSG (1 == 0, "UNKNOWN HEADER RECEIVED!");
1710  }
1711  counter++;
1712  }
1713 
1714  uint32_t packetSize = p->GetSize();
1715  NS_LOG_FUNCTION(this << "HEADERS FETCHED!" << headerContentSize << packetSize);
1716 
1717 
1719  // SERIALIZE REST OF THE PACKET (payload)
1721 
1722  if(headerContentSize - packetSize > 0){
1723 
1724  NS_LOG_FUNCTION(this << "ok, WE ARE READY now to add some payload" << headerContentSize << packetSize);
1725  NS_LOG_FUNCTION(this << "Size of payload is: " << packetSize - headerContentSize);
1726 
1727  //Fetch data from the packet
1728  uint8_t *buffer = new uint8_t[packetSize];
1729  p->CopyData(buffer, packetSize);
1730 
1731  //and add it to vector to be encrypted
1732  for(uint16_t a=headerContentSize; a<packetSize; a++)
1733  headerContentVector.push_back(buffer[a]);
1734 
1735  delete[] buffer;
1736 
1737  } else
1738  NS_LOG_FUNCTION(this << "There is NO data payload!");
1739 
1741  // SERIALIZE QKDCOMMAND HEADER - goes on the end of the packet
1743 
1744  NS_LOG_FUNCTION (this << "qkdCommandHeader is to be added" << qkdCommandHeader);
1745 
1746  /*
1747  std::cout << "\n......SENDING plain qkdCommandHeader....." << p->GetUid() << "..." << p->GetSize() << ".....\n";
1748  qkdCommandHeader.Print(std::cout);
1749  std::cout << "\n.............................\n";
1750  */
1751 
1752  // SERIALIZE QKDCOMMAND HEADER
1753  NS_LOG_FUNCTION (this << "qkdCommandHeader.GetCommand" << qkdCommandHeader.GetCommand () );
1754  NS_LOG_FUNCTION (this << "qkdCommandHeader.GetRoutingProtocolNumber" << (uint32_t) qkdCommandHeader.GetProtocol () );
1755 
1756  //if it is routing routing protocol message
1757 
1758  /*
1759  QKDCommandHeader points to IPv4 which points to TCP/UDP by default.
1760  However, when there is GSPRQ header siting between IPv4 and TCP/UDP,
1761  QKDCommandHeader has different values:
1762 
1763  value 4 : Header order is following and it is used by default:
1764  QKDHeader, QKDCommandHeader, IPv4, TCP/UDP
1765 
1766  DSDVQ_HEADER_PROTOCOL_NUMBER : Header order is following:
1767  QKDHeader, QKDCommandHeader, IPv4, DSDVQ
1768 
1769  DSDV_HEADER_PROTOCOL_NUMBER : Header order is following:
1770  QKDHeader, QKDCommandHeader, IPv4, DSDV
1771  */
1772 
1773  QKDCommandTag qkdCommandTag;
1774 
1775  if( p->PeekPacketTag(qkdCommandTag) &&
1776  (qkdCommandTag.GetCommand () == 'R' ||
1777  ( qkdCommandTag.GetRoutingProtocolNumber () == DSDV_PACKET_HEADER_PROTOCOL_NUMBER // DSDV
1778  || qkdCommandTag.GetRoutingProtocolNumber () == DSDVQ_PACKET_HEADER_PROTOCOL_NUMBER // DSDVQ
1779  )
1780  )
1781  ){
1782  NS_LOG_FUNCTION (this << "Need to fix QKDCommandHeader Protocol field to : " << qkdCommandTag.GetRoutingProtocolNumber ());
1783 
1784  /*
1785  std::cout << "\n......SENDING plain qkdCommandTag....." << p->GetUid() << "..." << p->GetSize() << ".....\n";
1786  qkdCommandTag.Print(std::cout);
1787  std::cout << "\n.............................\n";
1788  */
1789 
1790  qkdCommandHeader.SetProtocol( static_cast<uint32_t> (qkdCommandTag.GetRoutingProtocolNumber ()) );
1791  p->RemovePacketTag(qkdCommandTag);
1792 
1793  if(qkdCommandHeader.GetProtocol () == DSDV_PACKET_HEADER_PROTOCOL_NUMBER || qkdCommandHeader.GetProtocol () == DSDVQ_PACKET_HEADER_PROTOCOL_NUMBER)
1794  NS_ASSERT (qkdCommandHeader.GetCommand () == 'R' );
1795 
1796  }else{
1797  NS_LOG_FUNCTION (this << "PACKET WITHOUT QKDCOMMAND TAG RECEIVED!");
1798  qkdCommandHeader.SetProtocol( 4 ); //IPv4 by default
1799  }
1800  NS_LOG_FUNCTION (this << "qkdCommandHeader protocol:" << qkdCommandHeader.GetProtocol());
1801 
1802  Buffer qkdCommandHeaderBuffer;
1803  qkdCommandHeaderBuffer.AddAtStart (qkdCommandHeader.GetSerializedSize());
1804  qkdCommandHeader.Serialize(qkdCommandHeaderBuffer.Begin ());
1805 
1806  uint8_t *qkdcBuffer = new uint8_t[qkdCommandHeaderBuffer.GetSerializedSize() + 4];
1807  uint32_t serializeOutput = qkdCommandHeaderBuffer.Serialize(qkdcBuffer, qkdCommandHeaderBuffer.GetSerializedSize() + 4);
1808 
1809  //check if serialized process was sucessful
1810  NS_ASSERT(serializeOutput != 0);
1811  NS_LOG_FUNCTION(this << "QKDCommand Header Serialize result: " << serializeOutput << qkdCommandHeaderBuffer.GetSerializedSize() + 4);
1812  NS_LOG_FUNCTION(this << "QKDCommand Header size: " << qkdCommandHeaderBuffer.GetSerializedSize() );
1813  headerContentSize += qkdCommandHeaderBuffer.GetSerializedSize();
1814 
1815  //Add to headers vector which is going to be encrypted
1816  for(uint16_t a=0; a<(qkdCommandHeaderBuffer.GetSerializedSize() + 4); a++)
1817  headerContentVector.push_back(qkdcBuffer[a]);
1818 
1819  delete[] qkdcBuffer;
1820 
1821  NS_LOG_FUNCTION ("PLAINTEXT.size after QKDCommand: " << headerContentVector.size() );
1822 
1823  // QKDCOMMAND HEADER SERIALIZED
1824 
1825  NS_LOG_FUNCTION( this << "headerContentVector:" << headerContentVector.size() );
1826  return VectorToString(headerContentVector);
1827 }
1828 
1829 std::vector<Ptr<Packet> >
1831 {
1832  uint32_t channelID = 0;
1833 
1834  if(QKDBuffer != 0)
1835  channelID = QKDBuffer->m_bufferID;
1836 
1837  NS_LOG_FUNCTION (this
1838  << "PacketID:" << p->GetUid()
1839  << "PacketSize:" << p->GetSize()
1840  << "channelID:" << channelID
1841  );
1842 
1843  uint32_t packetSize = p->GetSize();
1844  uint8_t *buffer = new uint8_t[packetSize];
1845  p->CopyData(buffer, packetSize);
1846  std::string cipherTextTotal = std::string((char*)buffer, packetSize);
1847  NS_LOG_FUNCTION (this << "Whole PDU size of received packet:" << cipherTextTotal.size() );
1848 
1849  delete[] buffer;
1850 
1851  //First check for cannelID and search for previously stored fragments in the cache memory
1852  std::map<uint32_t, std::string>::iterator a = m_cacheFlowValues.find (channelID);
1853  if (a == m_cacheFlowValues.end ()){
1854  std::string tempString;
1855  m_cacheFlowValues.insert(
1856  std::make_pair( channelID , tempString)
1857  );
1858  a = m_cacheFlowValues.find (channelID);
1859  }
1860 
1861  if(a->second.size() > 0){
1862  NS_LOG_FUNCTION( this << "m_reassemblyCache is not empty, we need to add it to the packet!");
1863  NS_LOG_FUNCTION( this << "m_reassemblyCache size: " << a->second.size() );
1864  cipherTextTotal = a->second + cipherTextTotal;
1865  a->second.clear();
1866  }
1867 
1868  std::string wholePacketPayloadInString = cipherTextTotal;
1869  NS_LOG_FUNCTION (this << "Whole PDU size of received packet + cache:" << wholePacketPayloadInString.size() );
1870 
1871  std::string qkdHeaderText = cipherTextTotal.substr (0, m_qkdHeaderSize );
1872  NS_LOG_FUNCTION (this << "Croping QKD HEADER! of size " << qkdHeaderText.size() );
1873 
1874  QKDHeader qkdHeader = StringToQKDHeader(qkdHeaderText);
1875 
1876  //Ok ommit first 48 bytes and create a new QKD packet with that content
1877 
1878  cipherTextTotal = cipherTextTotal.substr (m_qkdHeaderSize, cipherTextTotal.size() - m_qkdHeaderSize );
1879  Ptr<Packet> packetWithQKDHeader = Create<Packet> ((uint8_t*) cipherTextTotal.c_str(), cipherTextTotal.size());
1880  packetWithQKDHeader->AddHeader(qkdHeader);
1881 
1882  NS_LOG_INFO (this << "packetWithQKDHeader: " << packetWithQKDHeader);
1883 
1885  // Check for fragmentation
1887 
1888  std::vector<Ptr<Packet> > packetOutput;
1889 
1890  uint32_t pointer = 0;
1891  bool newQkdHeaderNeeded = false;
1892  uint32_t cipherTextLength = cipherTextTotal.size();
1893  Ptr<Packet> tempPacket;
1894 
1895  while(pointer < cipherTextTotal.size()){
1896 
1897  NS_LOG_FUNCTION(this << "$$$$$$$$ POINTER ON LOCATION:" << pointer << cipherTextTotal.size());
1898 
1899  //if we have less then 48 bytes (highly unlikely)
1900  if(newQkdHeaderNeeded && cipherTextTotal.size() - pointer <= m_qkdHeaderSize){
1901  NS_LOG_FUNCTION(this << "we have less then 48 bytes of the packet" << cipherTextTotal.size() );
1902  a->second += cipherTextTotal.substr (pointer, cipherTextTotal.size() - pointer);
1903  break;
1904  }
1905 
1906  NS_LOG_FUNCTION (this << "Do we need new QKD HEADER:" << newQkdHeaderNeeded);
1907  QKDHeader qkdHeaderTemp;
1908  if(newQkdHeaderNeeded){
1909 
1910  //detech qkd header first
1911  NS_LOG_FUNCTION( this << "Detach QKD header first from pointer:" << pointer );
1912  std::string tempQKDHeaderText = cipherTextTotal.substr (pointer, m_qkdHeaderSize);
1913 
1914  qkdHeaderTemp = StringToQKDHeader(tempQKDHeaderText);
1915  pointer += m_qkdHeaderSize;
1916  NS_LOG_FUNCTION(this << "Pointer:" << pointer);
1917 
1918  }else{
1919  qkdHeaderTemp = qkdHeader;
1920  NS_LOG_FUNCTION(this << "qkdHeaderTemp:" << qkdHeaderTemp);
1921  }
1922 
1923  /*
1924  * First check whether we have whole packet or we just received a small fragment.
1925  * If we have a fragment, than we need to wait to obtain full packet and perform decryption.
1926  */
1927  if( cipherTextLength - pointer >= qkdHeaderTemp.GetLength() - m_qkdHeaderSize ){
1928 
1929  NS_LOG_FUNCTION (this << "We have enough material to decrypt the packet!" );
1930 
1931  //we have enough information to decrypt the packet
1932  std::string tempPacketPayload = cipherTextTotal.substr (pointer, qkdHeaderTemp.GetLength() - m_qkdHeaderSize);
1933  tempPacket = Create<Packet> ((uint8_t*) tempPacketPayload.c_str(), tempPacketPayload.size());
1934 
1935  NS_LOG_FUNCTION (this << "Whole PDU of the decrypted packet is:" << tempPacketPayload.size() );
1936 
1937  pointer += qkdHeaderTemp.GetLength() - m_qkdHeaderSize;
1938  NS_LOG_FUNCTION(this << "Pointer:" << pointer);
1939 
1940  tempPacket->AddHeader(qkdHeaderTemp);
1941  packetOutput.push_back(tempPacket);
1942  newQkdHeaderNeeded = true;
1943 
1944  NS_LOG_FUNCTION (this
1945  << "New packet created!"
1946  << tempPacket->GetUid()
1947  << " of size "
1948  << tempPacket->GetSize()
1949  );
1950  NS_LOG_FUNCTION (this
1951  << "UNUSED MATERIAL:"
1952  << cipherTextLength - pointer
1953  << "channelID:"
1954  << channelID
1955  );
1956  tempPacket = 0;
1957 
1958  }else{
1959 
1960  NS_LOG_FUNCTION (this << "We do *NOT* have enough material to decrypt the packet! Please wait for it!" );
1961  NS_LOG_FUNCTION(this << "m_reassemblyCache size: " << a->second.size() );
1962 
1963  if(pointer == 0){
1964  NS_LOG_FUNCTION (this << "ADDING TO CACHE - POINTER is " << pointer);
1965  a->second += wholePacketPayloadInString;
1966  NS_LOG_FUNCTION(this << "Adding to m_reassemblyCache the rest of the content of size: " << wholePacketPayloadInString.size() );
1967  }else{
1968  NS_LOG_FUNCTION (this << "ADDING TO CACHE - POINTER is " << pointer);
1969 
1970  uint32_t cacheFrom = pointer - m_qkdHeaderSize;
1971  uint32_t cacheLength = wholePacketPayloadInString.size() - cacheFrom;
1972 
1973  NS_LOG_FUNCTION (this
1974  << "Cropping from " << cacheFrom
1975  << " by size " << cacheLength
1976  << " and adding to cache!"
1977  );
1978 
1979  std::string restOfContent = cipherTextTotal.substr (
1980  cacheFrom,
1981  cacheLength
1982  );
1983 
1984  NS_LOG_FUNCTION(this << "Adding to m_reassemblyCache the rest of the content of size: " << restOfContent.size() );
1985  a->second += restOfContent;
1986  NS_LOG_FUNCTION (this << HexEncode(restOfContent) );
1987  }
1988 
1989  NS_LOG_FUNCTION(this << "m_reassemblyCache size: " << a->second.size() );
1990  break;
1991  }
1992  }
1993 
1994  NS_LOG_FUNCTION(this
1995  << "REASSEMBLY COMPLETED with " << packetOutput.size() << " packets "
1996  << "m_reassemblyCache size: " << a->second.size()
1997  << "channelID:"
1998  << channelID
1999  );
2000 
2001 
2002  NS_LOG_FUNCTION(this
2003  << "CacheID:"
2004  << a->first
2005  << "CacheValue:"
2006  << a->second.size()
2007  << "ChannelID:"
2008  << channelID
2009  );
2010 
2011  return packetOutput;
2012  }
2013 
2014 std::vector<Ptr<Packet> >
2016  Ptr<Packet> p,
2018  uint32_t channelID
2019 )
2020 {
2021  NS_LOG_FUNCTION (this << "PacketID:"
2022  << p->GetUid()
2023  << "PacketSize:"
2024  << p->GetSize()
2025  << QKDBuffer
2026  << "ChannelID:"
2027  << channelID
2028  );
2029 
2030  std::vector<Ptr<Packet> > packetOutput = CheckForFragmentation(
2031  p,
2032  QKDBuffer
2033  );
2034 
2035  // Iterate over appended elements
2036  for(uint32_t i = 0; i < packetOutput.size(); i++)
2037  {
2038  packetOutput[i] = Decrypt(packetOutput[i], QKDBuffer);
2039  NS_LOG_FUNCTION(this << "Decrytion completed!" << packetOutput[i]);
2040  }
2041  return packetOutput;
2042 }
2043 
2046 {
2047  NS_LOG_FUNCTION ( this << p->GetUid() << p->GetSize() );
2048 
2049  /*
2050  std::cout << "\n ..................RECEIVED encrpyted............" << p->GetUid() << " ...." << p->GetSize() << " \n";
2051  p->Print(std::cout);
2052  std::cout << "\n ................................................ \n";
2053  */
2054 
2055  //Before decryption we need to remove the QKDheader
2056  QKDHeader qkdHeader;
2057 
2058  //If no QKDheader is found, then just return the packet since the packet probabily is not encrpyted!
2059  p->PeekHeader (qkdHeader);
2060  if(!qkdHeader.IsValid () ){
2061  NS_LOG_FUNCTION(this << "UNKNOWN packet received!");
2062  return p;
2063  }
2064 
2065  NS_LOG_FUNCTION(this << qkdHeader);
2066  if (qkdHeader.GetAuthenticated() > 1) {
2067 
2068  Ptr<QKDKey> key;
2069  //KEY IS NEEDED ONLY FOR VMAC
2070  if(qkdHeader.GetAuthenticated() == QKDCRYPTO_AUTH_VMAC){
2071 
2072  if(QKDbuffer != 0)
2073  key = QKDbuffer->ProcessIncomingRequest ( qkdHeader.GetAuthenticationKeyId(), m_authenticationTagLengthInBits ); //in bits
2074 
2075  if(key == 0){
2076  NS_LOG_FUNCTION(this << "UNKNOWN AuthenticationKey ID!");
2077  NS_LOG_WARN ("NO ENOUGH KEY IN THE BUFFER! BUFFER IS EMPTY! ABORT ENCRYPTION and AUTHENTICATION PROCESS");
2078  return 0;
2079  }
2080  } else
2081  key = 0;
2082 
2083  Ptr<Packet> pTemp = CheckAuthentication(p, key, qkdHeader.GetAuthenticated() );
2084  if(pTemp == 0){
2085  NS_LOG_FUNCTION(this << "Authentication tag is not valid!");
2086  return 0;return p;
2087  }
2088  }
2089  p->RemoveHeader (qkdHeader);
2090 
2091  uint32_t packetSize = p->GetSize();
2092  //Now fetch the content of the packet
2093  uint8_t *buffer = new uint8_t[packetSize];
2094  p->CopyData(buffer, packetSize);
2095 
2096  std::string cipherText = std::string((char*)buffer, packetSize);
2097  std::string plainText;
2098 
2099  delete[] buffer;
2100 
2102  // START DECRYPTION
2104 
2105  if (qkdHeader.GetEncrypted() > 0){
2106  NS_ASSERT (qkdHeader.GetEncrypted() > 0);
2107 
2108  Ptr<QKDKey> key;
2109  switch (qkdHeader.GetEncrypted())
2110  {
2111  case QKDCRYPTO_OTP:
2112 
2113  if(QKDbuffer != 0)
2114  key = QKDbuffer->ProcessIncomingRequest ( qkdHeader.GetEncryptionKeyId(), cipherText.size() * 8 ); //in bits
2115 
2116  if(key == 0){
2117  NS_LOG_FUNCTION ("NO KEY PROVIDED!");
2118  NS_LOG_WARN ("NO ENOUGH KEY IN THE BUFFER! BUFFER IS EMPTY! ABORT ENCRYPTION and AUTHENTICATION PROCESS");
2119  return 0;
2120  }else{
2121  plainText = OTP ( cipherText, key );
2122  }
2123  break;
2124 
2125  case QKDCRYPTO_AES:
2126 
2127  if(QKDbuffer != 0)
2128  key = QKDbuffer->ProcessIncomingRequest ( qkdHeader.GetEncryptionKeyId(), CryptoPP::AES::MAX_KEYLENGTH ); // in bits
2129 
2130  if(key == 0){
2131  NS_LOG_FUNCTION ("NO KEY PROVIDED!");
2132  NS_LOG_WARN ("NO ENOUGH KEY IN THE BUFFER! BUFFER IS EMPTY! ABORT ENCRYPTION and AUTHENTICATION PROCESS");
2133  return 0;
2134  }else{
2135  plainText = AESDecrypt ( cipherText, key );
2136  }
2137  break;
2138  }
2139  }else{
2140  NS_LOG_FUNCTION(this << "Unencrypted packet received!");
2141  plainText = cipherText;
2142  }
2143  NS_LOG_FUNCTION ("DECRYPTED PLAINTEXT.size: " << plainText.size() );
2144 
2145  // DECOMPRESS PACKET
2146  if(qkdHeader.GetZipped () == 1){
2147  plainText = StringDecompressDecode (plainText);
2148  }
2149 
2150  if ( plainText.size() <= m_ipv4HeaderSize){
2151  NS_FATAL_ERROR ("The plain text contains less then m_ipv4HeaderSize(36) chars! This should not happen, problem with fragmentation!");
2152  NS_LOG_FUNCTION (this << plainText);
2153  }
2154 
2155  std::string ipHeaderPlainText = plainText.substr (0, m_ipv4HeaderSize);
2156  plainText = plainText.substr(m_ipv4HeaderSize, plainText.size() - m_ipv4HeaderSize);
2157  NS_LOG_FUNCTION(this << "Decryption of IPv4 Header completed!" << ipHeaderPlainText.size());
2158  NS_LOG_FUNCTION ("DECRYPTED PLAINTEXT.size after IPv4: " << plainText.size() );
2159 
2161  // DECRYPT IP HEADER
2163 
2164  const uint8_t* ipBuffer = reinterpret_cast<const uint8_t*>(ipHeaderPlainText.c_str());
2165 
2166  Buffer ipHeaderBuffer;
2167  ipHeaderBuffer.Deserialize(ipBuffer, m_ipv4HeaderSize);
2168  //delete[] ipBuffer;
2169 
2170  Ipv4Header ipv4Header;
2171  ipv4Header.Deserialize(ipHeaderBuffer.Begin ());
2172 
2173  NS_LOG_FUNCTION(this << "IPv4 Header decrypted");
2174  NS_LOG_FUNCTION(this << "Ipv4 original protocol:" << static_cast<uint32_t> (ipv4Header.GetProtocol()) );
2175 
2177  // FETCH HEADERS FROM DECRYPTED TEXT
2179  NS_LOG_FUNCTION(this << "Fetching headers start!" << p->GetUid() );
2180 
2181  uint32_t counter = 0;
2182  std::vector<uint32_t> protocols;
2183 
2184  //First remove QKDCommandHeader
2185  NS_LOG_FUNCTION(this << "QKDCommand Header detected!");
2186 
2187  std::string qkdCHeaderText = plainText.substr (plainText.size()-20, 20);
2188  plainText = plainText.substr (0, plainText.size()-20);
2189  packetSize = plainText.size();
2190  const uint8_t* qkdcBuffer = reinterpret_cast<const uint8_t*>(qkdCHeaderText.c_str());
2191  NS_LOG_FUNCTION ("DECRYPTED PLAINTEXT.size after IPv4 and qkdCommandHeader: " << plainText.size() );
2192 
2193  //Create QKDCBuffer
2194  Buffer qkdcHeaderBuffer;
2195  qkdcHeaderBuffer.Deserialize(qkdcBuffer, 20);
2196  //delete[] qkdcBuffer;
2197 
2198  //Fetch QKDCHeader
2199  QKDCommandHeader qkdCommandHeader;
2200  qkdCommandHeader.Deserialize(qkdcHeaderBuffer.Begin ());
2201 
2202  NS_LOG_FUNCTION (this << "qkdCommandHeader Deserialized" << qkdCommandHeader);
2203 
2204  /*
2205  std::cout << "\n ..................QKDCommand decrypted............" << p->GetUid() << " ...." << p->GetSize() << " \n";
2206  qkdCommandHeader.Print(std::cout);
2207  std::cout << "\n ................................................ \n";
2208  */
2209 
2210  /*
2211  QKDCommandHeader points to IPv4 which points to TCP/UDP by default.
2212  However, when there is GSPRQ header siting between IPv4 and TCP/UDP,
2213  QKDCommandHeader has different values:
2214  4 - Header order is following:
2215  QKDHeader, QKDCommandHeader, IPv4, TCP/UDP
2216  DSDVQ_HEADER_PROTOCOL_NUMBER - Header order is following:
2217  QKDHeader, QKDCommandHeader, IPv4, DSDVQ
2218  DSDV_HEADER_PROTOCOL_NUMBER - Header order is following:
2219  QKDHeader, QKDCommandHeader, IPv4, DSDV
2220  */
2221  NS_LOG_FUNCTION (this << "qkdCommandHeader Protocol Value" << qkdCommandHeader.GetProtocol() );
2222 
2223  bool allowIPv4ToPushHeaderInProtocolChain = true;
2224  if(qkdCommandHeader.GetProtocol () == 4){
2225  protocols.push_back(qkdCommandHeader.GetProtocol());
2226  }else{
2227  protocols.push_back(4); //ADD Ipv4 by default
2228 
2230  // OLSR
2232 
2233  //OLSR_PACKET_HEADER_PROTOCOL_NUMBER
2234  if(qkdCommandHeader.GetProtocol() == OLSR_PACKET_HEADER_PROTOCOL_NUMBER){//147
2235 
2236  NS_LOG_FUNCTION (this << "IT IS OLSR_PACKET_HEADER_PROTOCOL_NUMBER");
2237  allowIPv4ToPushHeaderInProtocolChain = false;
2238 
2239  protocols.push_back(17); //UDP
2240  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2241 
2242  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2243  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2244 
2245  protocols.push_back( OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER );
2246  NS_LOG_FUNCTION(this << "NextHeader" << OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER );
2247 
2248  //OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER
2249  }else if(qkdCommandHeader.GetProtocol() == OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER){//147
2250 
2251  NS_LOG_FUNCTION (this << "IT IS OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER");
2252  allowIPv4ToPushHeaderInProtocolChain = false;
2253 
2254  protocols.push_back(17); //UDP
2255  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2256 
2257  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2258  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2259 
2261  // DSDVQ
2263 
2264  }else if(qkdCommandHeader.GetProtocol() == DSDVQ_PACKET_HEADER_PROTOCOL_NUMBER){//147
2265 
2266  NS_LOG_FUNCTION (this << "IT IS DSDVQ_HEADER_PROTOCOL_NUMBER");
2267  allowIPv4ToPushHeaderInProtocolChain = false;
2268 
2269  protocols.push_back(17); //UDP
2270  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2271 
2272  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2273  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2274 
2276  // DSDV
2278 
2279  }else if(qkdCommandHeader.GetProtocol() == DSDV_PACKET_HEADER_PROTOCOL_NUMBER){//147
2280 
2281  NS_LOG_FUNCTION (this << "IT IS DSDV_HEADER_PROTOCOL_NUMBER");
2282  allowIPv4ToPushHeaderInProtocolChain = false;
2283 
2284  protocols.push_back(17); //UDP
2285  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2286 
2287  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2288  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2289 
2291  // AODV
2293 
2294  }else if(qkdCommandHeader.GetProtocol() == AODV_TYPE_HEADER_PROTOCOL_NUMBER){//41
2295 
2296  NS_LOG_FUNCTION (this << "IT IS AODV_TYPE_HEADER_PROTOCOL_NUMBER");
2297  allowIPv4ToPushHeaderInProtocolChain = false;
2298 
2299  protocols.push_back(17); //UDP
2300  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2301 
2302  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2303  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2304 
2305  }else if(qkdCommandHeader.GetProtocol() == AODV_RREQ_HEADER_PROTOCOL_NUMBER){//42
2306 
2307  NS_LOG_FUNCTION (this << "IT IS AODV_RREQ_HEADER_PROTOCOL_NUMBER");
2308  allowIPv4ToPushHeaderInProtocolChain = false;
2309 
2310  protocols.push_back(17); //UDP
2311  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2312 
2313  protocols.push_back(AODV_TYPE_HEADER_PROTOCOL_NUMBER); //41
2314  NS_LOG_FUNCTION(this << "NextHeader" << AODV_TYPE_HEADER_PROTOCOL_NUMBER);
2315 
2316  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2317  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2318 
2319  }else if(qkdCommandHeader.GetProtocol() == AODV_RREP_HEADER_PROTOCOL_NUMBER){//43
2320 
2321  NS_LOG_FUNCTION (this << "IT IS AODV_RREP_HEADER_PROTOCOL_NUMBER");
2322  allowIPv4ToPushHeaderInProtocolChain = false;
2323 
2324  protocols.push_back(17); //UDP
2325  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2326 
2327  protocols.push_back(AODV_TYPE_HEADER_PROTOCOL_NUMBER); //41
2328  NS_LOG_FUNCTION(this << "NextHeader" << AODV_TYPE_HEADER_PROTOCOL_NUMBER);
2329 
2330  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2331  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2332 
2333  }else if(qkdCommandHeader.GetProtocol() == AODV_RREP_ACK_HEADER_PROTOCOL_NUMBER){//44
2334 
2335  NS_LOG_FUNCTION (this << "IT IS AODV_RREP_ACK_HEADER_PROTOCOL_NUMBER");
2336  allowIPv4ToPushHeaderInProtocolChain = false;
2337 
2338  protocols.push_back(17); //UDP
2339  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2340 
2341  protocols.push_back(AODV_TYPE_HEADER_PROTOCOL_NUMBER); //41
2342  NS_LOG_FUNCTION(this << "NextHeader" << AODV_TYPE_HEADER_PROTOCOL_NUMBER);
2343 
2344  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2345  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2346 
2347  }else if(qkdCommandHeader.GetProtocol() == AODV_RERR_HEADER_PROTOCOL_NUMBER){//45
2348 
2349  NS_LOG_FUNCTION (this << "IT IS AODV_RERR_HEADER_PROTOCOL_NUMBER");
2350  allowIPv4ToPushHeaderInProtocolChain = false;
2351 
2352  protocols.push_back(17); //UDP
2353  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2354 
2355  protocols.push_back(AODV_TYPE_HEADER_PROTOCOL_NUMBER); //41
2356  NS_LOG_FUNCTION(this << "NextHeader" << AODV_TYPE_HEADER_PROTOCOL_NUMBER);
2357 
2358  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2359  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2360 
2362  // AODVQ
2364 
2365  }else if(qkdCommandHeader.GetProtocol() == AODVQ_TYPE_HEADER_PROTOCOL_NUMBER){//41
2366 
2367  NS_LOG_FUNCTION (this << "IT IS AODVQ_TYPE_HEADER_PROTOCOL_NUMBER");
2368  allowIPv4ToPushHeaderInProtocolChain = false;
2369 
2370  protocols.push_back(17); //UDP
2371  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2372 
2373  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2374  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2375 
2376  }else if(qkdCommandHeader.GetProtocol() == AODVQ_RREQ_HEADER_PROTOCOL_NUMBER){//42
2377 
2378  NS_LOG_FUNCTION (this << "IT IS AODVQ_RREQ_HEADER_PROTOCOL_NUMBER");
2379  allowIPv4ToPushHeaderInProtocolChain = false;
2380 
2381  protocols.push_back(17); //UDP
2382  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2383 
2384  protocols.push_back(AODVQ_TYPE_HEADER_PROTOCOL_NUMBER); //41
2385  NS_LOG_FUNCTION(this << "NextHeader" << AODVQ_TYPE_HEADER_PROTOCOL_NUMBER);
2386 
2387  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2388  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2389 
2390  }else if(qkdCommandHeader.GetProtocol() == AODVQ_RREP_HEADER_PROTOCOL_NUMBER){//43
2391 
2392  NS_LOG_FUNCTION (this << "IT IS AODVQ_RREP_HEADER_PROTOCOL_NUMBER");
2393  allowIPv4ToPushHeaderInProtocolChain = false;
2394 
2395  protocols.push_back(17); //UDP
2396  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2397 
2398  protocols.push_back(AODVQ_TYPE_HEADER_PROTOCOL_NUMBER); //41
2399  NS_LOG_FUNCTION(this << "NextHeader" << AODVQ_TYPE_HEADER_PROTOCOL_NUMBER);
2400 
2401  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2402  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2403 
2404  }else if(qkdCommandHeader.GetProtocol() == AODVQ_RREP_ACK_HEADER_PROTOCOL_NUMBER){//44
2405 
2406  NS_LOG_FUNCTION (this << "IT IS AODVQ_RREP_ACK_HEADER_PROTOCOL_NUMBER");
2407  allowIPv4ToPushHeaderInProtocolChain = false;
2408 
2409  protocols.push_back(17); //UDP
2410  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2411 
2412  protocols.push_back(AODVQ_TYPE_HEADER_PROTOCOL_NUMBER); //41
2413  NS_LOG_FUNCTION(this << "NextHeader" << AODVQ_TYPE_HEADER_PROTOCOL_NUMBER);
2414 
2415  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2416  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2417 
2418  }else if(qkdCommandHeader.GetProtocol() == AODVQ_RERR_HEADER_PROTOCOL_NUMBER){//45
2419 
2420  NS_LOG_FUNCTION (this << "IT IS AODVQ_RERR_HEADER_PROTOCOL_NUMBER");
2421  allowIPv4ToPushHeaderInProtocolChain = false;
2422 
2423  protocols.push_back(17); //UDP
2424  NS_LOG_FUNCTION(this << "NextHeader" << 17);
2425 
2426  protocols.push_back(AODVQ_TYPE_HEADER_PROTOCOL_NUMBER); //41
2427  NS_LOG_FUNCTION(this << "NextHeader" << AODVQ_TYPE_HEADER_PROTOCOL_NUMBER);
2428 
2429  protocols.push_back(static_cast<uint32_t> (qkdCommandHeader.GetProtocol()));
2430  NS_LOG_FUNCTION(this << "NextHeader" << static_cast<uint32_t> (qkdCommandHeader.GetProtocol()) );
2431 
2433  // ICMPv4
2435 
2436  }else if(qkdCommandHeader.GetProtocol() == 4 && ipv4Header.GetProtocol() == 1){
2437 
2438  NS_LOG_FUNCTION (this << "IT IS Icmpv4");
2439  allowIPv4ToPushHeaderInProtocolChain = false;
2440 
2441  protocols.push_back(1); //1
2442  NS_LOG_FUNCTION(this << "NextHeader" << 1);
2443 
2444  }
2445  //protocols.push_back(qkdCommandHeader.GetProtocol());
2446  }
2447 
2448  //Decrease packetSize calculation
2449  NS_LOG_FUNCTION(this << "QKDCommand Header decrypted" << qkdcHeaderBuffer.GetSerializedSize() + 4 << packetSize);
2450 
2451  //PLAIN HEADERS - to be used in the loop
2452 
2453  //UDP
2454  UdpHeader udpHeader;
2455 
2456  //TCP
2457  TcpHeader tcpHeader;
2458 
2459  //DSDV
2460  std::vector<ns3::dsdv::DsdvHeader> dsdvHeaders;
2461 
2462  //DSDVQ
2463  std::vector<ns3::dsdvq::DsdvqHeader> dsdvqHeaders;
2464 
2465  //OLSR
2466  ns3::olsr::PacketHeader olsrPacketHeader;
2467  std::vector<ns3::olsr::MessageHeader> olsrMessageHeaders;
2468 
2469  //AODV
2470  ns3::aodv::TypeHeader aodvTypeHeader;
2471  ns3::aodv::RreqHeader aodvRreqHeader;
2472  ns3::aodv::RrepHeader aodvRrepHeader;
2473  ns3::aodv::RrepAckHeader aodvRrepAckHeader;
2474  ns3::aodv::RerrHeader aodvRerrHeader;
2475 
2476  //AODVQ
2477  ns3::aodvq::TypeHeader aodvqTypeHeader;
2478  ns3::aodvq::RreqHeader aodvqRreqHeader;
2479  ns3::aodvq::RrepHeader aodvqRrepHeader;
2480  ns3::aodvq::RrepAckHeader aodvqRrepAckHeader;
2481  ns3::aodvq::RerrHeader aodvqRerrHeader;
2482 
2483  //ICMPv4
2485  ns3::Icmpv4Echo Icmpv4EchoHeader;
2486  ns3::Icmpv4DestinationUnreachable Icmpv4DestinationUnreachableHeader;
2487  ns3::Icmpv4TimeExceeded Icmpv4TimeExceededHeader;
2488 
2489 
2490  //-------------- END OF PLAIN HEADERS
2491 
2492  // Iterate over appended elements
2493  uint32_t i = 0;
2494  while(i < protocols.size())
2495  {
2496  NS_LOG_FUNCTION (this << "--------");
2497  NS_LOG_FUNCTION(this << "Detection started!"
2498  << "Protocol: " << protocols[i]
2499  << "Counter: " << counter
2500  );
2501 
2503  // IPv4
2505 
2506  if(protocols[i] == 4){
2507 
2508  NS_LOG_FUNCTION(this << "IPv4 Header detected!" << counter);
2509  NS_LOG_FUNCTION (this << "NextHeader value: " << (uint32_t) ipv4Header.GetProtocol() );
2510 
2511  if(allowIPv4ToPushHeaderInProtocolChain)
2512  protocols.push_back(static_cast<uint32_t> (ipv4Header.GetProtocol()));
2513 
2514  NS_LOG_FUNCTION(this << "Ipv4 original protocol:" << static_cast<uint32_t> (ipv4Header.GetProtocol()) );
2515  NS_LOG_FUNCTION(this << "IPv4 Header decrypted");
2516  /*
2517  std::cout << "\n ...........ipv4 header decrypted...........\n";
2518  ipv4Header.Print(std::cout);
2519  std::cout << "\n............................................\n";
2520  */
2521 
2522 
2524  // ICMPv4 Header
2526 
2527  }else if(protocols[i] == 1){
2528 
2529  NS_LOG_FUNCTION(this << "ICMPv4 Header detected!" << counter);
2530 
2531  std::string icmpv4HeaderText = plainText.substr (counter,counter + m_icmpv4HeaderSize);
2532  const uint8_t* icmpv4Buffer = reinterpret_cast<const uint8_t*>(icmpv4HeaderText.c_str());
2533 
2534  Buffer icmpv4HeaderBuffer;
2535  icmpv4HeaderBuffer.Deserialize(icmpv4Buffer, m_icmpv4HeaderSize);
2536 
2537  Icmpv4Header.Deserialize(icmpv4HeaderBuffer.Begin ());
2538 
2539  counter += m_icmpv4HeaderSize;
2540 
2541  NS_LOG_FUNCTION(this << "ICMPv4 Header decrypted");
2542  NS_LOG_FUNCTION(this << "ICMPv4 Header;Type value:" << (uint32_t) Icmpv4Header.GetType() );
2543  /*
2544  std::cout << "\n";
2545  icmpv4TypePosHeader.Print(std::cout);
2546  std::cout << "\n";
2547  */
2548 
2549  protocols.push_back(static_cast<uint32_t> (Icmpv4Header.GetType()));
2550  //delete[] icmpv4Buffer;
2551 
2552  }else if(protocols[i] == Icmpv4Header::ICMPV4_ECHO || protocols[i] == Icmpv4Header::ICMPV4_ECHO_REPLY){// || ECHO_REPLY
2553 
2554  NS_LOG_FUNCTION(this << "ICMPv4 ECHO detected!" << counter);
2555 
2556  std::string icmpv4HeaderText = plainText.substr (counter,counter + m_icmpv4EchoHeaderSize);
2557  const uint8_t* icmpv4Buffer = reinterpret_cast<const uint8_t*>(icmpv4HeaderText.c_str());
2558 
2559  Buffer icmpv4HeaderBuffer;
2560  icmpv4HeaderBuffer.Deserialize(icmpv4Buffer, m_icmpv4EchoHeaderSize);
2561 
2562  Icmpv4EchoHeader.Deserialize(icmpv4HeaderBuffer.Begin ());
2563 
2564  counter += m_icmpv4EchoHeaderSize;
2565 
2566  /*
2567  std::cout << "\n";
2568  Icmpv4EchoHeader.Print(std::cout);
2569  std::cout << "\n";
2570  */
2571 
2572  NS_LOG_FUNCTION(this << "ICMPv4 ECHO decrypted");
2573  //delete[] icmpv4Buffer;
2574 
2575  }else if(protocols[i] == Icmpv4Header::ICMPV4_DEST_UNREACH){
2576 
2577  NS_LOG_FUNCTION(this << "ICMPv4 DEST_UNREACH detected!" << counter);
2578 
2579  std::string icmpv4HeaderText = plainText.substr (counter,counter + m_icmpv4DestinationUnreachableHeaderSize);
2580  const uint8_t* icmpv4Buffer = reinterpret_cast<const uint8_t*>(icmpv4HeaderText.c_str());
2581 
2582  Buffer icmpv4HeaderBuffer;
2583  icmpv4HeaderBuffer.Deserialize(icmpv4Buffer, m_icmpv4DestinationUnreachableHeaderSize);
2584 
2585  Icmpv4DestinationUnreachableHeader.Deserialize(icmpv4HeaderBuffer.Begin ());
2586 
2588 
2589  NS_LOG_FUNCTION(this << "ICMPv4 DEST_UNREACH decrypted");
2590  /*
2591  std::cout << "\n";
2592  Icmpv4DestinationUnreachableHeader.Print(std::cout);
2593  std::cout << "\n";
2594  */
2595  //delete[] icmpv4Buffer;
2596 
2597  }else if(protocols[i] == Icmpv4Header::ICMPV4_TIME_EXCEEDED){
2598 
2599  NS_LOG_FUNCTION(this << "ICMPv4 TIME_EXCEEDED detected!" << counter);
2600 
2601  std::string icmpv4HeaderText = plainText.substr (counter,counter + m_icmpv4TimeExceededHeaderSize);
2602  const uint8_t* icmpv4Buffer = reinterpret_cast<const uint8_t*>(icmpv4HeaderText.c_str());
2603 
2604  Buffer icmpv4HeaderBuffer;
2605  icmpv4HeaderBuffer.Deserialize(icmpv4Buffer, m_icmpv4TimeExceededHeaderSize);
2606 
2607  Icmpv4TimeExceededHeader.Deserialize(icmpv4HeaderBuffer.Begin ());
2608 
2609  counter += m_icmpv4TimeExceededHeaderSize;
2610 
2611  NS_LOG_FUNCTION(this << "ICMPv4 TIME_EXCEEDED decrypted");
2612  /*
2613  std::cout << "\n";
2614  Icmpv4TimeExceededHeader.Print(std::cout);
2615  std::cout << "\n";
2616  */
2617  //delete[] icmpv4Buffer;
2618 
2620  // UDP
2622 
2623  } else if(protocols[i] == 17){
2624 
2625  NS_LOG_FUNCTION(this << "UDP Header detected!" << counter);
2626 
2627  std::string udpHeaderText = plainText.substr (counter,counter + m_udpHeaderSize);
2628  const uint8_t* udpBuffer = reinterpret_cast<const uint8_t*>(udpHeaderText.c_str());
2629 
2630  Buffer udpHeaderBuffer;
2631  udpHeaderBuffer.Deserialize(udpBuffer, m_udpHeaderSize);
2632 
2633  udpHeader.Deserialize(udpHeaderBuffer.Begin ());
2634 
2635  counter += m_udpHeaderSize;
2636 
2637  NS_LOG_FUNCTION(this << "UDP Header decrypted");
2638  /*
2639  std::cout << "\n";
2640  udpHeader.Print(std::cout);
2641  std::cout << "\n";
2642  */
2643  //delete[] udpBuffer;
2644 
2646  // TCP
2648 
2649  } else if(protocols[i] == 6){
2650 
2651  //FIRST WE NEED TO FIGURE OUT THE LENGTH OF THIS HEADER
2652  //Therefore, we crop the QKDDelimiterHeader and read the length of TCP Header
2653 
2654  std::string temp = plainText.substr(counter, m_qkdDHeaderSize);
2655  QKDDelimiterHeader qkdDHeader = StringToQKDDelimiterHeader (temp);
2656  counter += m_qkdDHeaderSize;
2657 
2658  NS_LOG_FUNCTION (this << "TCP size should be " << qkdDHeader.GetDelimiterSize());
2659 
2660  std::string tcpHeaderText = plainText.substr(counter, qkdDHeader.GetDelimiterSize());
2661  uint32_t tcpheaderSize = tcpHeaderText.size();
2662 
2663  NS_LOG_FUNCTION(this << "TCP Header detected!" << tcpheaderSize );
2664 
2665  const uint8_t* tcpBuffer = reinterpret_cast<const uint8_t*>(tcpHeaderText.c_str());
2666 
2667  Buffer tcpHeaderBuffer;
2668  tcpHeaderBuffer.Deserialize(tcpBuffer, tcpheaderSize);
2669 
2670  tcpHeader.Deserialize(tcpHeaderBuffer.Begin ());
2671  counter+=tcpheaderSize;
2672 
2673  NS_LOG_FUNCTION(this << "TCP Header decrypted" << tcpheaderSize);
2674  /*
2675  std::cout << "\n";
2676  tcpHeader.Print(std::cout);
2677  std::cout << "\n";
2678  */
2679  //delete[] tcpBuffer;
2680 
2682  // DSDVQ
2684 
2685  } else if(protocols[i] == DSDVQ_PACKET_HEADER_PROTOCOL_NUMBER){
2686 
2687  NS_LOG_FUNCTION(this << "DSDVQ Header detected!" << counter);
2688 
2689  std::string dsdvqHeaderText = plainText.substr (counter,counter + m_dsdvqHeaderSize);
2690  const uint8_t* dsdvqBuffer = reinterpret_cast<const uint8_t*>(dsdvqHeaderText.c_str());
2691 
2692  NS_ASSERT(dsdvqHeaderText.size() >= m_dsdvqHeaderSize);
2693 
2694  Buffer dsdvqHeaderBuffer;
2695  dsdvqHeaderBuffer.Deserialize(dsdvqBuffer, m_dsdvqHeaderSize);
2696 
2697  ns3::dsdvq::DsdvqHeader dsdvqHeader;
2698  dsdvqHeader.Deserialize(dsdvqHeaderBuffer.Begin ());
2699  dsdvqHeaders.push_back(dsdvqHeader);
2700 
2701  counter += m_dsdvqHeaderSize;
2702  uint32_t restOfPacket = plainText.size() - counter;
2703 
2704  NS_LOG_FUNCTION(this << "DSDVQ HelloHeader decrypted" << counter << restOfPacket);
2705 
2706  if(restOfPacket > 0 && restOfPacket % m_dsdvqHeaderSize == 0)
2707  protocols.push_back(DSDVQ_PACKET_HEADER_PROTOCOL_NUMBER);
2708  /*
2709  std::cout << "\n";
2710  dsdvqHeader.Print(std::cout);
2711  std::cout << "\n";
2712  */
2713  //delete[] dsdvqBuffer;
2714 
2716  // DSDV
2718 
2719  } else if(protocols[i] == DSDV_PACKET_HEADER_PROTOCOL_NUMBER){
2720 
2721  NS_LOG_FUNCTION(this << "DSDV Header detected!" << counter);
2722 
2723  std::string dsdvHeaderText = plainText.substr (counter,counter + m_dsdvHeaderSize);
2724  const uint8_t* dsdvBuffer = reinterpret_cast<const uint8_t*>(dsdvHeaderText.c_str());
2725 
2726  NS_ASSERT(dsdvHeaderText.size() >= m_dsdvHeaderSize);
2727 
2728  Buffer dsdvHeaderBuffer;
2729  dsdvHeaderBuffer.Deserialize(dsdvBuffer, m_dsdvHeaderSize);
2730 
2731  ns3::dsdv::DsdvHeader dsdvHeader;
2732  dsdvHeader.Deserialize(dsdvHeaderBuffer.Begin ());
2733  dsdvHeaders.push_back(dsdvHeader);
2734 
2735  counter += m_dsdvHeaderSize;
2736  uint32_t restOfPacket = plainText.size() - counter;
2737 
2738  NS_LOG_FUNCTION(this << "DSDV HelloHeader decrypted" << counter << restOfPacket);
2739 
2740  if(restOfPacket > 0 && restOfPacket % m_dsdvHeaderSize == 0)
2741  protocols.push_back(DSDV_PACKET_HEADER_PROTOCOL_NUMBER);
2742 
2743  /*
2744  std::cout << "\n";
2745  dsdvHeader.Print(std::cout);
2746  std::cout << "\n";
2747  */
2748 
2750  // OLSR
2752 
2753  } else if(protocols[i] == OLSR_PACKET_HEADER_PROTOCOL_NUMBER){
2754 
2755  NS_LOG_FUNCTION(this << "OLSR_PACKET_HEADER detected!" << counter);
2756 
2757  std::string olsrHeaderText = plainText.substr (counter,counter + m_olsrPacketHeaderSize);
2758  const uint8_t* olsrBuffer = reinterpret_cast<const uint8_t*>(olsrHeaderText.c_str());
2759 
2760  Buffer olsrHeaderBuffer;
2761  olsrHeaderBuffer.Deserialize(olsrBuffer, m_olsrPacketHeaderSize);
2762 
2763  olsrPacketHeader.Deserialize(olsrHeaderBuffer.Begin ());
2764 
2765  counter += m_olsrPacketHeaderSize;
2766 
2767  NS_LOG_FUNCTION(this << "OLSR_PACKET_HEADER decrypted");
2768 
2769  /*
2770  std::cout << "\n";
2771  olsrPacketHeader.Print(std::cout);
2772  std::cout << "\n";
2773  */
2774  //delete[] olsrBuffer;
2775 
2776  } else if(protocols[i] == OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER){
2777 
2778  NS_LOG_FUNCTION(this << "OLSR_MESSAGE_HEADER detected!" << counter << plainText.size() << plainText.size() - counter);
2779 
2780  //FIRST WE NEED TO FIGURE OUT THE LENGTH OF THIS HEADER
2781  //Therefore, we crop the QKDDelimiterHeader and read the length of OLSR Message Header
2782 
2783  std::string temp = plainText.substr(counter, m_qkdDHeaderSize);
2784  QKDDelimiterHeader qkdDHeader = StringToQKDDelimiterHeader (temp);
2785  counter += m_qkdDHeaderSize;
2786 
2787  NS_LOG_FUNCTION (this << "OLSR_MESSAGE_HEADER size should be " << qkdDHeader.GetDelimiterSize());
2788 
2789  std::string olsrHeaderText = plainText.substr(counter, qkdDHeader.GetDelimiterSize());
2790  uint32_t olsrheaderSize = olsrHeaderText.size();
2791 
2792  NS_LOG_FUNCTION (this << "Cropped temp is : " << olsrHeaderText);
2793  NS_LOG_FUNCTION(this << "OLSR_MESSAGE_HEADER size:" << olsrheaderSize << temp.size() );
2794 
2795  const uint8_t* olsrBuffer = reinterpret_cast<const uint8_t*>(olsrHeaderText.c_str());
2796 
2797  Buffer olsrHeaderBuffer;
2798  olsrHeaderBuffer.Deserialize(olsrBuffer, olsrheaderSize);
2799 
2800  ns3::olsr::MessageHeader olsrMessageHeader;
2801  olsrMessageHeader.Deserialize(olsrHeaderBuffer.Begin ());
2802 
2803  olsrMessageHeaders.push_back(olsrMessageHeader);
2804  counter += olsrheaderSize;
2805 
2806  NS_LOG_FUNCTION(this << "OLSR_MESSAGE_HEADER decrypted" << counter << olsrheaderSize);
2807  NS_LOG_FUNCTION(this << "OLSR_MESSAGE_HEADER type" << olsrMessageHeader.GetMessageType() );
2808 
2809  if(plainText.size() - counter > 0)
2810  protocols.push_back(OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER);
2811 
2812  /*
2813  std::cout << "\n";
2814  olsrMessageHeader.Print(std::cout);
2815  std::cout << "\n";
2816  */
2817  //delete[] olsrBuffer;
2818 
2820  // AODV
2822 
2823  } else if(protocols[i] == AODV_TYPE_HEADER_PROTOCOL_NUMBER){
2824 
2825  NS_LOG_FUNCTION(this << "aodv TypeHeader detected!" << counter);
2826 
2827  std::string aodvHeaderText = plainText.substr (counter,counter + m_aodvTypeHeaderSize);
2828  const uint8_t* aodvBuffer = reinterpret_cast<const uint8_t*>(aodvHeaderText.c_str());
2829 
2830  Buffer aodvHeaderBuffer;
2831  aodvHeaderBuffer.Deserialize(aodvBuffer, m_aodvTypeHeaderSize);
2832 
2833  aodvTypeHeader.Deserialize(aodvHeaderBuffer.Begin ());
2834 
2835  counter += m_aodvTypeHeaderSize;
2836 
2837  NS_LOG_FUNCTION(this << "aodv TypeHeader decrypted");
2838  /*
2839  std::cout << "\n";
2840  aodvTypeHeader.Print(std::cout);
2841  std::cout << "\n";
2842  */
2843  //delete[] aodvBuffer;
2844 
2845  } else if(protocols[i] == AODV_RREQ_HEADER_PROTOCOL_NUMBER){
2846 
2847  NS_LOG_FUNCTION(this << "aodv RreqHeader detected!" << counter);
2848 
2849  std::string aodvHeaderText = plainText.substr (counter,counter + m_aodvRreqHeaderSize);
2850  const uint8_t* aodvBuffer = reinterpret_cast<const uint8_t*>(aodvHeaderText.c_str());
2851 
2852  Buffer aodvHeaderBuffer;
2853  aodvHeaderBuffer.Deserialize(aodvBuffer, m_aodvRreqHeaderSize);
2854 
2855  aodvRreqHeader.Deserialize(aodvHeaderBuffer.Begin ());
2856 
2857  counter += m_aodvRreqHeaderSize;
2858 
2859  NS_LOG_FUNCTION(this << "aodv RreqHeader decrypted");
2860  /*
2861  std::cout << "\n";
2862  aodvRreqHeader.Print(std::cout);
2863  std::cout << "\n";
2864  */
2865  //delete[] aodvBuffer;
2866 
2867  } else if(protocols[i] == AODV_RREP_HEADER_PROTOCOL_NUMBER){
2868 
2869  NS_LOG_FUNCTION(this << "aodv RrepHeader detected!" << counter);
2870 
2871  std::string aodvHeaderText = plainText.substr (counter,counter + m_aodvRrepHeaderSize);
2872  const uint8_t* aodvBuffer = reinterpret_cast<const uint8_t*>(aodvHeaderText.c_str());
2873 
2874  Buffer aodvHeaderBuffer;
2875  aodvHeaderBuffer.Deserialize(aodvBuffer, m_aodvRrepHeaderSize);
2876 
2877  aodvRrepHeader.Deserialize(aodvHeaderBuffer.Begin ());
2878 
2879  counter += m_aodvRrepHeaderSize;
2880 
2881  NS_LOG_FUNCTION(this << "aodv RrepHeader decrypted");
2882  /*
2883  std::cout << "\n";
2884  aodvRrepHeader.Print(std::cout);
2885  std::cout << "\n";
2886  */
2887  //delete[] aodvBuffer;
2888 
2889  } else if(protocols[i] == AODV_RERR_HEADER_PROTOCOL_NUMBER){
2890 
2891  NS_LOG_FUNCTION(this << "aodv RrepHeader detected!" << counter);
2892 
2893  std::string aodvHeaderText = plainText.substr (counter,counter + m_aodvRerrHeaderSize);
2894  const uint8_t* aodvBuffer = reinterpret_cast<const uint8_t*>(aodvHeaderText.c_str());
2895 
2896  Buffer aodvHeaderBuffer;
2897  aodvHeaderBuffer.Deserialize(aodvBuffer, m_aodvRerrHeaderSize);
2898 
2899  aodvRerrHeader.Deserialize(aodvHeaderBuffer.Begin ());
2900 
2901  counter += m_aodvRerrHeaderSize;
2902 
2903  NS_LOG_FUNCTION(this << "aodv RrepHeader decrypted");
2904  /*
2905  std::cout << "\n";
2906  m_aodvRerrHeaderSize.Print(std::cout);
2907  std::cout << "\n";
2908  */
2909  //delete[] aodvBuffer;
2910 
2911  } else if(protocols[i] == AODV_RREP_ACK_HEADER_PROTOCOL_NUMBER){
2912 
2913  NS_LOG_FUNCTION(this << "aodv RrepHeader detected!" << counter);
2914 
2915  std::string aodvHeaderText = plainText.substr (counter,counter + m_aodvRrepAckHeaderSize);
2916  const uint8_t* aodvBuffer = reinterpret_cast<const uint8_t*>(aodvHeaderText.c_str());
2917 
2918  Buffer aodvHeaderBuffer;
2919  aodvHeaderBuffer.Deserialize(aodvBuffer, m_aodvRrepAckHeaderSize);
2920 
2921  aodvRrepAckHeader.Deserialize(aodvHeaderBuffer.Begin ());
2922 
2923  counter += m_aodvRrepAckHeaderSize;
2924 
2925  NS_LOG_FUNCTION(this << "aodv RrepHeader decrypted");
2926  /*
2927  std::cout << "\n";
2928  m_aodvRrepAckHeaderSize.Print(std::cout);
2929  std::cout << "\n";
2930  */
2931  //delete[] aodvBuffer;
2932 
2934  // AODVQ
2936 
2937  } else if(protocols[i] == AODVQ_TYPE_HEADER_PROTOCOL_NUMBER){
2938 
2939  NS_LOG_FUNCTION(this << "aodvq TypeHeader detected!" << counter);
2940 
2941  std::string aodvqHeaderText = plainText.substr (counter,counter + m_aodvqTypeHeaderSize);
2942  const uint8_t* aodvqBuffer = reinterpret_cast<const uint8_t*>(aodvqHeaderText.c_str());
2943 
2944  Buffer aodvqHeaderBuffer;
2945  aodvqHeaderBuffer.Deserialize(aodvqBuffer, m_aodvqTypeHeaderSize);
2946 
2947  aodvqTypeHeader.Deserialize(aodvqHeaderBuffer.Begin ());
2948 
2949  counter += m_aodvqTypeHeaderSize;
2950 
2951  NS_LOG_FUNCTION(this << "aodvq TypeHeader decrypted");
2952  /*
2953  std::cout << "\n";
2954  aodvqTypeHeader.Print(std::cout);
2955  std::cout << "\n";
2956  */
2957  //delete[] aodvqBuffer;
2958 
2959  } else if(protocols[i] == AODVQ_RREQ_HEADER_PROTOCOL_NUMBER){
2960 
2961  NS_LOG_FUNCTION(this << "aodvq RreqHeader detected!" << counter);
2962 
2963  std::string aodvqHeaderText = plainText.substr (counter,counter + m_aodvqRreqHeaderSize);
2964  const uint8_t* aodvqBuffer = reinterpret_cast<const uint8_t*>(aodvqHeaderText.c_str());
2965 
2966  Buffer aodvqHeaderBuffer;
2967  aodvqHeaderBuffer.Deserialize(aodvqBuffer, m_aodvqRreqHeaderSize);
2968 
2969  aodvqRreqHeader.Deserialize(aodvqHeaderBuffer.Begin ());
2970 
2971  counter += m_aodvqRreqHeaderSize;
2972 
2973  NS_LOG_FUNCTION(this << "aodvq RreqHeader decrypted");
2974  /*
2975  std::cout << "\n";
2976  aodvqRreqHeader.Print(std::cout);
2977  std::cout << "\n";
2978  */
2979  //delete[] aodvqBuffer;
2980 
2981  } else if(protocols[i] == AODVQ_RREP_HEADER_PROTOCOL_NUMBER){
2982 
2983  NS_LOG_FUNCTION(this << "aodvq RrepHeader detected!" << counter);
2984 
2985  std::string aodvqHeaderText = plainText.substr (counter,counter + m_aodvqRrepHeaderSize);
2986  const uint8_t* aodvqBuffer = reinterpret_cast<const uint8_t*>(aodvqHeaderText.c_str());
2987 
2988  Buffer aodvqHeaderBuffer;
2989  aodvqHeaderBuffer.Deserialize(aodvqBuffer, m_aodvqRrepHeaderSize);
2990 
2991  aodvqRrepHeader.Deserialize(aodvqHeaderBuffer.Begin ());
2992 
2993  counter += m_aodvqRrepHeaderSize;
2994 
2995  NS_LOG_FUNCTION(this << "aodvq RrepHeader decrypted");
2996  /*
2997  std::cout << "\n";
2998  aodvqRrepHeader.Print(std::cout);
2999  std::cout << "\n";
3000  */
3001  //delete[] aodvqBuffer;
3002 
3003  } else if(protocols[i] == AODVQ_RERR_HEADER_PROTOCOL_NUMBER){
3004 
3005  NS_LOG_FUNCTION(this << "aodvq RrepHeader detected!" << counter);
3006 
3007  std::string aodvqHeaderText = plainText.substr (counter,counter + m_aodvqRerrHeaderSize);
3008  const uint8_t* aodvqBuffer = reinterpret_cast<const uint8_t*>(aodvqHeaderText.c_str());
3009 
3010  Buffer aodvqHeaderBuffer;
3011  aodvqHeaderBuffer.Deserialize(aodvqBuffer, m_aodvqRerrHeaderSize);
3012 
3013  aodvqRerrHeader.Deserialize(aodvqHeaderBuffer.Begin ());
3014 
3015  counter += m_aodvqRerrHeaderSize;
3016 
3017  NS_LOG_FUNCTION(this << "aodvq RrepHeader decrypted");
3018  /*
3019  std::cout << "\n";
3020  m_aodvqRerrHeaderSize.Print(std::cout);
3021  std::cout << "\n";
3022  */
3023  //delete[] aodvqBuffer;
3024 
3025  } else if(protocols[i] == AODVQ_RREP_ACK_HEADER_PROTOCOL_NUMBER){
3026 
3027  NS_LOG_FUNCTION(this << "aodvq RrepHeader detected!" << counter);
3028 
3029  std::string aodvqHeaderText = plainText.substr (counter,counter + m_aodvqRrepAckHeaderSize);
3030  const uint8_t* aodvqBuffer = reinterpret_cast<const uint8_t*>(aodvqHeaderText.c_str());
3031 
3032  Buffer aodvqHeaderBuffer;
3033  aodvqHeaderBuffer.Deserialize(aodvqBuffer, m_aodvqRrepAckHeaderSize);
3034 
3035  aodvqRrepAckHeader.Deserialize(aodvqHeaderBuffer.Begin ());
3036 
3037  counter += m_aodvqRrepAckHeaderSize;
3038 
3039  NS_LOG_FUNCTION(this << "aodvq RrepHeader decrypted");
3040  /*
3041  std::cout << "\n";
3042  m_aodvqRrepAckHeaderSize.Print(std::cout);
3043  std::cout << "\n";
3044  */
3045  //delete[] aodvqBuffer;
3046  }
3047 
3048  ++i;
3049  }
3050 
3051  NS_LOG_FUNCTION (this << "HeaderSize" << counter);
3052 
3053  NS_LOG_FUNCTION(this << "Size of payload is: " << plainText.size() - counter);
3054 
3055  std::string packetPayloadText = plainText.substr (counter, plainText.size() - counter);
3056 
3057  Ptr<Packet> packet = Create<Packet> ((uint8_t*) packetPayloadText.c_str(), packetPayloadText.size());
3058  NS_LOG_FUNCTION(this << "Packet " << p->GetUid() << " >>>> GOES TO >>>> " << packet->GetUid() );
3059 
3060  NS_LOG_FUNCTION(this << "Decryption completed!" << packet->GetUid() << packet->GetSize() );
3061 
3063  // ADD ALL HEADERS TO THE PACKET
3065 
3066  i = protocols.size();
3067  while(i > 0){
3068 
3069  NS_LOG_FUNCTION(this << "ADDING PROTOCOL:" << protocols[i-1] << "TO THE PACKET!");
3070 
3071  switch (protocols[i-1]){
3072 
3074  // Ipv4
3076  case 4:
3077  packet->AddHeader (ipv4Header);
3078  NS_LOG_FUNCTION(this << "IPv4 header added to packet!");
3079  break;
3080 
3081 
3083  // ICMPv4
3085  case 1:
3086  packet->AddHeader (Icmpv4Header);
3087  NS_LOG_FUNCTION(this << "Icmpv4Header added to packet!");
3088  break;
3090  packet->AddHeader (Icmpv4EchoHeader);
3091  NS_LOG_FUNCTION(this << "Icmpv4Header::ICMPV4_ECHO header added to packet!");
3092  break;
3094  packet->AddHeader (Icmpv4EchoHeader);
3095  NS_LOG_FUNCTION(this << "Icmpv4Header::ICMPV4_ECHO_REPLY header added to packet!");
3096  break;
3098  packet->AddHeader (Icmpv4DestinationUnreachableHeader);
3099  NS_LOG_FUNCTION(this << "Icmpv4DestinationUnreachable Header added to packet!");
3100  break;
3102  packet->AddHeader (Icmpv4TimeExceededHeader);
3103  NS_LOG_FUNCTION(this << "Icmpv4TimeExceeded Header added to packet!");
3104  break;
3105 
3106 
3107 
3109  // UDP
3111  case 17:
3112  //udpHeader.InitializeChecksum(ipv4Header.GetSource(), ipv4Header.GetDestination(), ipv4Header.GetProtocol());
3113  packet->AddHeader (udpHeader);
3114  NS_LOG_FUNCTION(this << "UDP header added to packet!");
3115  break;
3116 
3118  // TCP
3120  case 6:
3121  packet->AddHeader (tcpHeader);
3122  NS_LOG_FUNCTION(this << "TCP header added to packet!");
3123  break;
3124 
3125 
3127  // OLSR
3130  packet->AddHeader (olsrPacketHeader);
3131  NS_LOG_FUNCTION(this << "olsr Packet Header added to packet!");
3132  break;
3134  packet->AddHeader ( olsrMessageHeaders.back() );
3135  olsrMessageHeaders.pop_back();
3136  NS_LOG_FUNCTION(this << "olsr Message Header added to packet!");
3137  break;
3138 
3140  // DSDVQ
3143  packet->AddHeader ( dsdvqHeaders.back() );
3144  dsdvqHeaders.pop_back();
3145  NS_LOG_FUNCTION(this << "DSDVQ header added to packet!");
3146  break;
3147 
3149  // DSDV
3152  packet->AddHeader ( dsdvHeaders.back() );
3153  dsdvHeaders.pop_back();
3154  NS_LOG_FUNCTION(this << "DSDV header added to packet!");
3155  break;
3156 
3157 
3159  // AODV
3162  packet->AddHeader (aodvTypeHeader);
3163  NS_LOG_FUNCTION(this << "aodvTypeHeaderadded to packet!");
3164  break;
3166  packet->AddHeader (aodvRreqHeader);
3167  NS_LOG_FUNCTION(this << "aodvRreqHeader added to packet!");
3168  break;
3170  packet->AddHeader (aodvRrepHeader);
3171  NS_LOG_FUNCTION(this << "aodvRrepHeader added to packet!");
3172  break;
3174  packet->AddHeader (aodvRrepAckHeader);
3175  NS_LOG_FUNCTION(this << "aodvRrepAckHeader added to packet!");
3176  break;
3178  packet->AddHeader (aodvRerrHeader);
3179  NS_LOG_FUNCTION(this << "aodvRerrHeader added to packet!");
3180  break;
3181 
3182 
3184  // AODVQ
3187  packet->AddHeader (aodvqTypeHeader);
3188  NS_LOG_FUNCTION(this << "aodvqTypeHeaderadded to packet!");
3189  break;
3191  packet->AddHeader (aodvqRreqHeader);
3192  NS_LOG_FUNCTION(this << "aodvqRreqHeader added to packet!");
3193  break;
3195  packet->AddHeader (aodvqRrepHeader);
3196  NS_LOG_FUNCTION(this << "aodvqRrepHeader added to packet!");
3197  break;
3199  packet->AddHeader (aodvqRrepAckHeader);
3200  NS_LOG_FUNCTION(this << "aodvqRrepAckHeader added to packet!");
3201  break;
3203  packet->AddHeader (aodvqRerrHeader);
3204  NS_LOG_FUNCTION(this << "aodvqRerrHeader added to packet!");
3205  break;
3206 
3207  }
3208  i--;
3209  }
3210 
3211  NS_LOG_FUNCTION (this << "Output of DECRYPT function! PacketID: " << packet->GetUid() << " PacketSize: " << packet->GetSize() );
3212 
3213  QKDInternalTag internalTag;
3214  internalTag.SetAuthenticateValue ( qkdHeader.GetAuthenticated() );
3215  internalTag.SetEncryptValue ( qkdHeader.GetEncrypted() );
3216  packet->AddPacketTag(internalTag);
3217 
3218  /*
3219  packet->AddHeader(qkdCommandHeader);
3220  std::cout << "\n ..................RECEIVED plain....." << packet->GetUid() << "..." << packet->GetSize() << "....... \n";
3221  packet->Print(std::cout);
3222  std::cout << "\n ............................................ \n";
3223  packet->RemoveHeader(qkdCommandHeader);
3224  */
3225 
3226  m_decryptionTrace(packet);
3227  return packet;
3228 }
3229 
3230 
3231 /***************************************************************
3232 * CRYPTO++ CRYPTOGRAPHIC FUNCTIONS
3233 ***************************************************************/
3234 
3235 std::string
3236 QKDCrypto::OTP (const std::string& data, Ptr<QKDKey> key)
3237 {
3238  NS_LOG_FUNCTION (this << data.size());
3239  if(!m_encryptionEnabled) return data;
3240 
3241  std::string encryptData = data;
3242  std::string keyString = key->KeyToString();
3243 
3244  if(keyString.size() != encryptData.size()){
3245  NS_LOG_FUNCTION(this << "KEY IS NOT GOOD FOR OTP!");
3246  return data;
3247  }
3248 
3249  for (uint32_t i = 0; i < encryptData.length(); i++) {
3250  encryptData[i] ^= keyString[i];
3251  }
3252 
3253  return encryptData;
3254 }
3255 
3256 std::string
3257 QKDCrypto::AESEncrypt (const std::string& data, Ptr<QKDKey> key)
3258 {
3259  NS_LOG_FUNCTION (this << data.size());
3260  if(!m_encryptionEnabled) return data;
3261 
3262  std::string encryptData;
3263  byte* Kkey = key->GetKey();
3264 
3265  // Encryption
3266  CryptoPP::CTR_Mode< CryptoPP::AES >::Encryption encryptor;
3267  encryptor.SetKeyWithIV(Kkey, key->GetSize(), m_iv);
3268  //encryptor.SetKeyWithIV( key, CryptoPP::AES::DEFAULT_KEYLENGTH, m_iv );
3269 
3270  CryptoPP::StreamTransformationFilter stf( encryptor, new CryptoPP::StringSink( encryptData ) );
3271  stf.Put( (byte*)data.c_str(), data.size() );
3272  stf.MessageEnd();
3273  delete[] Kkey;
3274 
3275  return encryptData;
3276 }
3277 
3278 std::string
3279 QKDCrypto::AESDecrypt (const std::string& data, Ptr<QKDKey> key)
3280 {
3281  NS_LOG_FUNCTION (this << data.size());
3282  if(!m_encryptionEnabled) return data;
3283 
3284  std::string decryptData;
3285  byte* Kkey = key->GetKey();
3286 
3287  // Decryption
3288  CryptoPP::CTR_Mode< CryptoPP::AES >::Decryption decryptor;
3289  decryptor.SetKeyWithIV(Kkey, key->GetSize(), m_iv);
3290  //decryptor.SetKeyWithIV( key, CryptoPP::AES::DEFAULT_KEYLENGTH, m_iv );
3291 
3292  CryptoPP::StreamTransformationFilter stf( decryptor, new CryptoPP::StringSink( decryptData ) );
3293  stf.Put( (byte*)data.c_str(), data.size() );
3294  stf.MessageEnd();
3295  delete[] Kkey;
3296 
3297  return decryptData;
3298 }
3299 
3300 std::string
3301 QKDCrypto::HexEncode(const std::string& data)
3302 {
3303  NS_LOG_FUNCTION (this << data.size());
3304  if(!m_encryptionEnabled) return data;
3305 
3306  std::string encoded;
3307  CryptoPP::StringSource ss(
3308  (
3309  byte*)data.data(),
3310  data.size(),
3311  true,
3312  new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)
3313  )
3314  );
3315  return encoded;
3316 }
3317 
3318 std::string
3319 QKDCrypto::HexDecode(const std::string& data)
3320 {
3321  NS_LOG_FUNCTION (this << data.size());
3322  if(!m_encryptionEnabled) return data;
3323 
3324  std::string decoded;
3325  CryptoPP::StringSource ss(
3326  (
3327  byte*)data.data(),
3328  data.size(),
3329  true,
3330  new CryptoPP::HexDecoder(new CryptoPP::StringSink(decoded)
3331  )
3332  );
3333  return decoded;
3334 }
3335 
3336 std::string
3337 QKDCrypto::Authenticate (std::string& inputString, Ptr<QKDKey> key, uint8_t authenticationType)
3338 {
3339  NS_LOG_FUNCTION (this << inputString.length());
3340 
3341  switch (authenticationType)
3342  {
3343  case QKDCRYPTO_AUTH_VMAC:
3344  return VMAC(inputString, key);
3345  break;
3346  case QKDCRYPTO_AUTH_MD5:
3347  return MD5(inputString);
3348  break;
3349  case QKDCRYPTO_AUTH_SHA1:
3350  return SHA1(inputString);
3351  break;
3352  }
3353  std::string temp;
3354  return temp;
3355 }
3356 
3358 QKDCrypto::CheckAuthentication(Ptr<Packet> p, Ptr<QKDKey> key, uint8_t authenticationType)
3359 {
3360  NS_LOG_FUNCTION (this << p->GetUid() );
3361 
3362  QKDHeader qkdHeader;
3363  p->PeekHeader(qkdHeader);
3364  //If no QKDheader is found, then just return the packet since the packet probabily is not authenticated!
3365  if(!qkdHeader.IsValid () || qkdHeader.GetAuthenticated() <= 0){
3366  NS_LOG_FUNCTION(this << "Unauthenticated packet received!");
3367  return p;
3368  }
3369 
3370  Ptr<Packet> packet = p->Copy ();
3371  packet->RemoveHeader(qkdHeader);
3372 
3373  uint32_t packetSize = packet->GetSize();
3374  uint8_t *buffer = new uint8_t[packetSize];
3375  packet->CopyData(buffer, packetSize);
3376  std::string plaintext = std::string((char*)buffer, packetSize);
3377  std::string hashTag;
3378  delete[] buffer;
3379 
3380  switch (authenticationType)
3381  {
3382  case QKDCRYPTO_AUTH_VMAC:
3383  hashTag = VMAC(plaintext, key);
3384  break;
3385  case QKDCRYPTO_AUTH_MD5:
3386  hashTag = MD5(plaintext);
3387  break;
3388  case QKDCRYPTO_AUTH_SHA1:
3389  hashTag = SHA1(plaintext);
3390  break;
3391  }
3392 
3393  m_deauthenticationTrace(p, hashTag);
3394 
3395  if(hashTag != qkdHeader.GetAuthTag()){
3396 
3397  NS_LOG_FUNCTION (this << "1:" << hashTag << hashTag.length() );
3398  NS_LOG_FUNCTION (this << "2:" << qkdHeader.GetAuthTag() << qkdHeader.GetAuthTag().length() );
3399 
3400  NS_LOG_FUNCTION(this << "\n\nMAC VERIFICATION FAILED!\n\n");
3401  return 0;
3402  }else{
3403  NS_LOG_FUNCTION(this << "\n\nMAC VERIFICATION SUCCESSFUL!\n\n");
3404  }
3405 
3406  return p;
3407 }
3408 
3409 std::string
3410 QKDCrypto::VMAC (std::string& inputString, Ptr<QKDKey> key)
3411 {
3412  NS_LOG_FUNCTION (this << inputString.length() << key->KeyToString() );
3413  if(!m_encryptionEnabled)
3414  return std::string( m_authenticationTagLengthInBits, '0');
3415 
3416  byte* Kkey = key->GetKey();
3417  byte digestBytes[key->GetSize()];
3418 
3419  CryptoPP::VMAC<CryptoPP::AES> vmac;
3420 
3421  vmac.SetKeyWithIV(Kkey, key->GetSize(), m_iv, CryptoPP::AES::BLOCKSIZE);
3422  vmac.CalculateDigest(digestBytes, (byte *) inputString.c_str(), inputString.length());
3423 
3424  std::string outputString;
3425  CryptoPP::HexEncoder encoder;
3426  encoder.Attach(new CryptoPP::StringSink(outputString));
3427  encoder.Put(digestBytes, sizeof(digestBytes));
3428  encoder.MessageEnd();
3429 
3430  outputString = outputString.substr(0, m_authenticationTagLengthInBits);
3431 
3432  delete[] Kkey;
3433  return outputString;
3434 }
3435 
3436 std::string
3437 QKDCrypto::MD5(std::string& inputString)
3438 {
3439  NS_LOG_FUNCTION (this << inputString.length() );
3440  if(!m_encryptionEnabled)
3441  return std::string( m_authenticationTagLengthInBits, '0');
3442 
3443  byte digestBytes[CryptoPP::Weak::MD5::DIGESTSIZE];
3444 
3445  CryptoPP::Weak1::MD5 md5;
3446  md5.CalculateDigest(digestBytes, (byte *) inputString.c_str(), inputString.length());
3447 
3448  std::string outputString;
3449  CryptoPP::HexEncoder encoder;
3450 
3451  encoder.Attach(new CryptoPP::StringSink(outputString));
3452  encoder.Put(digestBytes, sizeof(digestBytes));
3453  encoder.MessageEnd();
3454 
3455  outputString = outputString.substr(0, m_authenticationTagLengthInBits);
3456  return outputString;
3457 }
3458 
3459 std::string
3460 QKDCrypto::SHA1(std::string& inputString)
3461 {
3462  NS_LOG_FUNCTION (this << inputString.length() );
3463  if(!m_encryptionEnabled)
3464  return std::string( m_authenticationTagLengthInBits, '0');
3465 
3466  byte digestBytes[CryptoPP::SHA::DIGESTSIZE];
3467 
3468  CryptoPP::SHA1 sha1;
3469  sha1.CalculateDigest(digestBytes, (byte *) inputString.c_str(), inputString.length());
3470 
3471  std::string outputString;
3472  CryptoPP::HexEncoder encoder;
3473 
3474  encoder.Attach(new CryptoPP::StringSink(outputString));
3475  encoder.Put(digestBytes, sizeof(digestBytes));
3476  encoder.MessageEnd();
3477 
3478  outputString = outputString.substr(0, m_authenticationTagLengthInBits);
3479  return outputString;
3480 }
3481 
3482 std::string
3484 {
3485  NS_LOG_FUNCTION (this << s.length() );
3486 
3487  unsigned char const* bytes_to_encode = reinterpret_cast<const unsigned char*>(s.c_str());
3488  unsigned int in_len = s.length();
3489 
3490  std::string ret;
3491  int i = 0;
3492  int j = 0;
3493  unsigned char char_array_3[3];
3494  unsigned char char_array_4[4];
3495 
3496  while (in_len--) {
3497  char_array_3[i++] = *(bytes_to_encode++);
3498  if (i == 3) {
3499  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
3500  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
3501  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
3502  char_array_4[3] = char_array_3[2] & 0x3f;
3503 
3504  for(i = 0; (i <4) ; i++)
3505  ret += base64_chars[char_array_4[i]];
3506  i = 0;
3507  }
3508  }
3509 
3510  if (i)
3511  {
3512  for(j = i; j < 3; j++)
3513  char_array_3[j] = '\0';
3514 
3515  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
3516  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
3517  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
3518  char_array_4[3] = char_array_3[2] & 0x3f;
3519 
3520  for (j = 0; (j < i + 1); j++)
3521  ret += base64_chars[char_array_4[j]];
3522 
3523  while((i++ < 3))
3524  ret += '=';
3525 
3526  }
3527  delete[] bytes_to_encode;
3528  return ret;
3529 
3530 }
3531 
3532 std::string
3533 QKDCrypto::base64_decode(std::string const& encoded_string)
3534 {
3535  NS_LOG_FUNCTION (this << encoded_string.length() );
3536 
3537  int in_len = encoded_string.size();
3538  int i = 0;
3539  int j = 0;
3540  int in_ = 0;
3541  unsigned char char_array_4[4], char_array_3[3];
3542  std::string ret;
3543 
3544  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
3545  char_array_4[i++] = encoded_string[in_]; in_++;
3546  if (i ==4) {
3547  for (i = 0; i <4; i++)
3548  char_array_4[i] = base64_chars.find(char_array_4[i]);
3549 
3550  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
3551  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
3552  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
3553 
3554  for (i = 0; (i < 3); i++)
3555  ret += char_array_3[i];
3556  i = 0;
3557  }
3558  }
3559 
3560  if (i) {
3561  for (j = i; j <4; j++)
3562  char_array_4[j] = 0;
3563 
3564  for (j = 0; j <4; j++)
3565  char_array_4[j] = base64_chars.find(char_array_4[j]);
3566 
3567  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
3568  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
3569  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
3570 
3571  for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
3572  }
3573 
3574  return ret;
3575 }
3576 
3577 std::string
3579 {
3580  NS_LOG_FUNCTION (this << "INPUT:" << data.length());
3581  if(!m_compressionEnabled) return data;
3582 
3583  namespace bio = boost::iostreams;
3584 
3585  std::stringstream compressed;
3586  std::stringstream origin(data);
3587 
3588  bio::filtering_streambuf<bio::input> out;
3589  out.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_compression)));
3590  out.push(origin);
3591  bio::copy(out, compressed);
3592 
3593  //std::string output = base64_encode(compressed.str());
3594  std::string output = compressed.str();
3595  NS_LOG_FUNCTION (this << "OUTPUT:" << output.length());
3596 
3597  return output;
3598 }
3599 
3600 std::string
3602 {
3603  NS_LOG_FUNCTION (this << "INPUT:" << data.length());
3604  if(!m_compressionEnabled) return data;
3605 
3606  namespace bio = boost::iostreams;
3607  //std::string temp = base64_decode(data);
3608  std::string temp = data;
3609 
3610  std::stringstream compressed(temp);
3611  std::stringstream decompressed;
3612 
3613  bio::filtering_streambuf<bio::input> out;
3614  out.push(bio::gzip_decompressor());
3615  out.push(compressed);
3616  bio::copy(out, decompressed);
3617 
3618  std::string output = decompressed.str();
3619  NS_LOG_FUNCTION (this << "OUTPUT:" << output.length());
3620 
3621  return output;
3622 }
3623 
3624 
3625 } // namespace ns3
void SetAuthenticated(uint32_t value)
Definition: qkd-header.cc:413
#define QKDCRYPTO_AUTH_VMAC
Definition: qkd-crypto.h:70
Route Reply Acknowledgment (RREP-ACK) Message Format.
Definition: aodvq-packet.h:508
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
QKDDelimiterHeader StringToQKDDelimiterHeader(std::string &input)
Help function used to covert std::string to QKDDelimiterHeader Function is used in decryption (deseri...
Definition: qkd-crypto.cc:239
void AddAtStart(uint32_t start)
Definition: buffer.cc:309
#define DSDVQ_PACKET_HEADER_PROTOCOL_NUMBER
Definition: dsdvq-packet.h:41
void SetLength(uint32_t value)
Definition: qkd-header.cc:372
std::string StringCompressEncode(const std::string &data)
Help function used to compress string.
Definition: qkd-crypto.cc:3578
void SetDelimiterSize(uint32_t value)
Definition: qkd-header.cc:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
std::string VectorToString(std::vector< uint8_t > inputVector)
Help function used to vector<uint8_t> to convert string.
Definition: qkd-crypto.cc:143
AttributeValue implementation for Boolean.
Definition: boolean.h:36
Callback template class.
Definition: callback.h:1176
uint32_t m_udpHeaderSize
Definition: qkd-crypto.h:409
uint32_t GetSerializedSize() const
Definition: qkd-header.cc:258
#define QKDCRYPTO_AUTH_MD5
Definition: qkd-crypto.h:71
Buffer::Iterator current
an iterator which can be fed to Deserialize.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
std::string HexDecode(const std::string &data)
Help function used to decode string to HEX string.
Definition: qkd-crypto.cc:3319
This header can store HELP, TC, MID and HNA messages.
Definition: olsr-header.h:160
#define QKDCRYPTO_AES
Definition: qkd-crypto.h:68
virtual uint32_t GetSerializedSize(void) const
Definition: olsr-header.cc:130
Route Error (RERR) Message Format.
Definition: aodv-packet.h:562
virtual uint32_t GetSerializedSize() const
Definition: dsdv-packet.cc:68
uint32_t m_aodvqRreqHeaderSize
Definition: qkd-crypto.h:430
void Serialize(Buffer::Iterator i) const
Definition: aodv-packet.cc:552
#define QKDCRYPTO_AUTH_SHA1
Definition: qkd-crypto.h:72
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: dsdvq-packet.cc:82
ICMP Time Exceeded header.
Definition: icmpv4.h:245
Introspection did not find any typical Config paths.
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:84
Item Next(void)
Retrieve the next metadata item.
static TypeId GetTypeId(void)
Get the TypeId.
Definition: qkd-crypto.cc:80
automatically resized byte buffer
Definition: buffer.h:92
structure describing a packet metadata item
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:852
virtual uint32_t GetSerializedSize(void) const
Definition: icmpv4.cc:462
uint32_t Deserialize(Buffer::Iterator start)
Definition: qkd-header.cc:289
#define AODVQ_RREP_HEADER_PROTOCOL_NUMBER
Definition: aodvq-packet.h:43
bool m_compressionEnabled
encryption (ZIP or similar) enabled?
Definition: qkd-crypto.h:391
uint32_t m_aodvRrepHeaderSize
Definition: qkd-crypto.h:422
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
Introspection did not find any typical Config paths.
Definition: qkd-buffer.h:67
Introspection did not find any typical Config paths.
Definition: qkd-header.h:49
TypeId tid
TypeId of Header or Trailer.
uint32_t Deserialize(Buffer::Iterator start)
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
uint32_t GetSerializedSize() const
#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
uint8_t GetProtocol(void) const
Definition: ipv4-header.cc:272
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: icmpv4.cc:479
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void Serialize(Buffer::Iterator start) const
Definition: qkd-header.cc:267
uint32_t m_authenticationTagLengthInBits
length of the authentication tag in bits (32 by default)
Definition: qkd-crypto.h:378
bool CheckForResourcesToProcessThePacket(Ptr< Packet > p, uint32_t TOSBand, Ptr< QKDBuffer > QKDbuffer)
Check whether there is enough resources (key material) to process (encrypt or decrypt) the packet...
Definition: qkd-crypto.cc:293
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
uint32_t GetSerializedSize(void) const
Return the number of bytes required for serialization.
Definition: buffer.cc:559
#define AODVQ_RREQ_HEADER_PROTOCOL_NUMBER
Definition: aodvq-packet.h:42
#define AODVQ_RREP_ACK_HEADER_PROTOCOL_NUMBER
Definition: aodvq-packet.h:44
uint32_t GetSerializedSize() const
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: icmpv4.cc:93
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual void Serialize(Buffer::Iterator start) const
Definition: icmpv4.cc:468
#define AODV_TYPE_HEADER_PROTOCOL_NUMBER
Definition: aodv-packet.h:41
std::string StringDecompressDecode(const std::string &data)
Help function used to decompress string.
Definition: qkd-crypto.cc:3601
Route Error (RERR) Message Format.
Definition: aodvq-packet.h:562
#define AODV_RERR_HEADER_PROTOCOL_NUMBER
Definition: aodv-packet.h:45
Anchor the ns-3 type and attribute system.
Definition: object-base.h:119
Ptr< Packet > Decrypt(Ptr< Packet > p, Ptr< QKDBuffer > QKDBuffer)
This functions is used for real decryption process.
Definition: qkd-crypto.cc:2045
static const std::string base64_chars
Definition: qkd-crypto.cc:70
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: olsr-header.cc:150
virtual uint32_t GetSerializedSize() const
Definition: dsdvq-packet.cc:67
virtual void Serialize(Buffer::Iterator start) const
Definition: olsr-header.cc:142
Route Request (RREQ) Message Format.
Definition: aodv-packet.h:136
uint32_t m_icmpv4EchoHeaderSize
Definition: qkd-crypto.h:404
Base class for all the ICMP packet headers.
Definition: icmpv4.h:40
uint32_t GetSerializedSize() const
Definition: aodvq-packet.cc:61
MessageType GetMessageType() const
Get the message type.
Definition: olsr-header.h:189
uint32_t Deserialize(Buffer::Iterator start)
Definition: aodv-packet.cc:490
std::vector< uint8_t > StringToVector(std::string &input)
Help function used to convert string to vector<uint8_t>
Definition: qkd-crypto.cc:128
void SetZipped(uint8_t value)
Definition: qkd-header.cc:427
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1053
void Serialize(Buffer::Iterator start) const
Definition: qkd-header.cc:166
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
uint32_t GetSerializedSize() const
Definition: aodv-packet.cc:546
virtual void Serialize(Buffer::Iterator start) const
Definition: icmpv4.cc:239
uint32_t Deserialize(Buffer::Iterator start)
Definition: aodv-packet.cc:566
virtual uint32_t GetSerializedSize(void) const
Definition: icmpv4.cc:69
void Serialize(Buffer::Iterator start) const
Definition: aodv-packet.cc:67
Packet header for IPv4.
Definition: ipv4-header.h:33
QKDCrypto()
Constructor.
Definition: qkd-crypto.cc:114
uint32_t Deserialize(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:651
uint32_t Deserialize(Buffer::Iterator start)
Definition: aodv-packet.cc:73
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:874
void SetAuthenticateValue(uint8_t value)
NS_ASSERT_MSG(false,"Ipv4AddressGenerator::MaskToIndex(): Impossible")
uint16_t GetProtocol(void) const
Definition: qkd-header.cc:123
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: olsr-header.cc:252
static bool is_base64(unsigned char c)
Definition: qkd-crypto.cc:75
byte m_iv[CryptoPP::AES::BLOCKSIZE]
Definition: qkd-crypto.h:197
uint32_t m_olsrPacketHeaderSize
Definition: qkd-crypto.h:412
uint32_t GetRoutingProtocolNumber(void) const
uint32_t Deserialize(Buffer::Iterator start)
Definition: aodv-packet.cc:349
#define QKDCRYPTO_OTP
Definition: qkd-crypto.h:67
void Serialize(Buffer::Iterator start) const
Introspection did not find any typical Config paths.
Introspection did not find any typical Config paths.
std::string VMAC(std::string &inputString, Ptr< QKDKey > key)
Authentication function in Wegman-Carter fashion.
Definition: qkd-crypto.cc:3410
void SetEncrypted(uint32_t value)
Definition: qkd-header.cc:399
TracedCallback< Ptr< Packet >, std::string > m_authenticationTrace
trace callback for authentication
Definition: qkd-crypto.h:383
std::string base64_decode(std::string const &s)
Help function - base64_decode.
Definition: qkd-crypto.cc:3533
std::vector< Ptr< Packet > > ProcessIncomingPacket(Ptr< Packet > p, Ptr< QKDBuffer > QKDBuffer, uint32_t channelID)
This functions is an entry point toward deencryption/authentication-check of the packet Packet is des...
Definition: qkd-crypto.cc:2015
void SetChannelId(uint16_t value)
Definition: qkd-header.cc:455
#define OLSR_PACKET_HEADER_PROTOCOL_NUMBER
Definition: olsr-header.h:34
uint8_t data[writeSize]
uint8_t GetAuthenticateValue(void) const
void Serialize(Buffer::Iterator start) const
Iterator class for metadata items.
uint32_t Deserialize(Buffer::Iterator start)
Definition: aodvq-packet.cc:73
uint32_t m_icmpv4TimeExceededHeaderSize
Definition: qkd-crypto.h:405
std::string AESEncrypt(const std::string &data, Ptr< QKDKey > key)
AES encryption.
Definition: qkd-crypto.cc:3257
void SetAuthenticationKeyId(uint32_t value)
Definition: qkd-header.cc:483
#define OLSR_MESSAGE_HEADER_PROTOCOL_NUMBER
Definition: olsr-header.h:35
virtual uint32_t GetSerializedSize(void) const
Definition: icmpv4.cc:233
TracedCallback< Ptr< Packet > > m_decryptionTrace
trace callback for decryption
Definition: qkd-crypto.h:381
void Serialize(Buffer::Iterator start) const
bool HasNext(void) const
Checks if there is another metadata item.
uint32_t Deserialize(Buffer::Iterator start)
void Serialize(Buffer::Iterator start) const
Definition: aodv-packet.cc:337
uint32_t GetDelimiterSize(void) const
Definition: qkd-header.cc:212
std::map< uint32_t, std::string > m_cacheFlowValues
map used to hold info about fragmented packets
Definition: qkd-crypto.h:386
std::vector< Ptr< Packet > > CheckForFragmentation(Ptr< Packet > p, Ptr< QKDBuffer > QKDBuffer)
Check for reassembly of packet fragments.
Definition: qkd-crypto.cc:1830
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
std::vector< uint8_t > QKDHeaderToVector(QKDHeader &qkdHeader)
Help function used to covert QKDHeader to vector<uint8_t> which is suitable for encryption.
Definition: qkd-crypto.cc:161
std::string SHA1(std::string &inputString)
SHA1 Authentication function.
Definition: qkd-crypto.cc:3460
uint32_t m_ipv4HeaderSize
we store details about the ipv4 header size which is later used in decryption
Definition: qkd-crypto.h:400
Route Reply (RREP) Message Format.
Definition: aodv-packet.h:340
uint32_t m_qkdDHeaderSize
qkd delimiter header size
Definition: qkd-crypto.h:389
uint32_t m_aodvqRerrHeaderSize
Definition: qkd-crypto.h:432
uint32_t m_aodvqRrepHeaderSize
Definition: qkd-crypto.h:429
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
void SetMessageId(uint32_t value)
Definition: qkd-header.cc:385
Route Request (RREQ) Message Format.
Definition: aodvq-packet.h:136
void Serialize(Buffer::Iterator start) const
Definition: aodv-packet.cc:181
bool m_encryptionEnabled
real encryption used?
Definition: qkd-crypto.h:392
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t GetEncryptValue(void) const
void Serialize(Buffer::Iterator start) const
Definition: aodv-packet.cc:484
uint32_t GetSerializedSize() const
PacketMetadata::ItemIterator BeginItem(void) const
Returns an iterator which points to the first &#39;item&#39; stored in this buffer.
Definition: packet.cc:566
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Introspection did not find any typical Config paths.
Definition: qkd-header.h:202
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
uint32_t Deserialize(Buffer::Iterator start)
ICMP Echo header.
Definition: icmpv4.h:107
uint32_t GetSerializedSize() const
virtual void Serialize(Buffer::Iterator start) const
Definition: dsdv-packet.cc:74
Packet header for UDP packets.
Definition: udp-header.h:39
TracedCallback< Ptr< Packet >, std::string > m_deauthenticationTrace
trace callback for authentication check
Definition: qkd-crypto.h:384
Introspection did not find any typical Config paths.
Definition: qkd-header.h:106
std::string GetName(void) const
Get the name.
Definition: type-id.cc:969
TracedCallback< Ptr< Packet > > m_encryptionTrace
trace callback for encryption
Definition: qkd-crypto.h:380
uint32_t GetSerializedSize() const
Definition: aodv-packet.cc:478
uint16_t GetUid(void) const
Get the internal id of this TypeId.
Definition: type-id.cc:1183
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: icmpv4.cc:367
uint32_t m_dsdvqHeaderSize
Definition: qkd-crypto.h:415
void Serialize(Buffer::Iterator i) const
uint32_t GetSerializedSize() const
Definition: aodv-packet.cc:331
The basic layout of any packet in OLSR is as follows (omitting IP and UDP headers): ...
Definition: olsr-header.h:78
std::string Authenticate(std::string &, Ptr< QKDKey > key, uint8_t authenticationType)
Help parent function used for calling child authentication functions.
Definition: qkd-crypto.cc:3337
virtual uint32_t GetSerializedSize(void) const
Definition: tcp-header.cc:309
Route Reply Acknowledgment (RREP-ACK) Message Format.
Definition: aodv-packet.h:508
uint32_t m_aodvRrepAckHeaderSize
Definition: qkd-crypto.h:424
virtual void Serialize(Buffer::Iterator start) const
Definition: udp-header.cc:184
Ptr< Packet > CheckAuthentication(Ptr< Packet > p, Ptr< QKDKey > key, uint8_t authenticationType)
Help parent function used for calling child authentication functions for authentication check...
Definition: qkd-crypto.cc:3358
std::string PacketToString(Ptr< Packet > p)
Help function used to serialize packet to std::string which is later used for encryption.
Definition: qkd-crypto.cc:578
void Serialize(Buffer::Iterator start) const
Definition: aodvq-packet.cc:67
uint32_t Serialize(uint8_t *buffer, uint32_t maxSize) const
Definition: buffer.cc:579
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: tcp-header.cc:360
QKDHeader StringToQKDHeader(std::string &input)
Help function used to covert std::string to QKDHeader Function is used in decryption (deserialize pro...
Definition: qkd-crypto.cc:216
uint32_t Deserialize(Buffer::Iterator start)
Definition: aodv-packet.cc:194
uint32_t Deserialize(Buffer::Iterator start)
virtual ~QKDCrypto()
Destructor.
Definition: qkd-crypto.cc:122
uint32_t GetSerializedSize() const
Definition: aodv-packet.cc:175
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: udp-header.cc:220
virtual uint32_t GetSerializedSize(void) const
Definition: icmpv4.cc:349
char GetCommand(void) const
std::string OTP(const std::string &data, Ptr< QKDKey > key)
One-time cipher.
Definition: qkd-crypto.cc:3236
std::size_t GetSize(void) const
Get the size of this object.
Definition: type-id.cc:983
uint32_t m_aodvqRrepAckHeaderSize
Definition: qkd-crypto.h:431
std::string AESDecrypt(const std::string &data, Ptr< QKDKey > key)
AES decryption.
Definition: qkd-crypto.cc:3279
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:262
uint32_t m_aodvRerrHeaderSize
Definition: qkd-crypto.h:425
uint32_t GetSerializedSize() const
Definition: aodv-packet.cc:61
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:859
ICMP Destination Unreachable header.
Definition: icmpv4.h:172
#define AODVQ_TYPE_HEADER_PROTOCOL_NUMBER
Definition: aodvq-packet.h:41
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
std::vector< uint8_t > QKDDelimiterHeaderToVector(QKDDelimiterHeader &qkdHeader)
Help function used to covert QKDDelimiterHeader to vector<uint8_t> which is suitable for encryption...
Definition: qkd-crypto.cc:187
#define AODV_RREQ_HEADER_PROTOCOL_NUMBER
Definition: aodv-packet.h:42
uint32_t GetMessageId(void) const
Definition: qkd-header.cc:391
DSDV Update Packet Format.
Definition: dsdvq-packet.h:63
uint32_t m_qkdHeaderSize
qkd header size
Definition: qkd-crypto.h:388
uint32_t m_aodvRreqHeaderSize
Definition: qkd-crypto.h:423
uint8_t GetType(void) const
Get ICMP type.
Definition: icmpv4.cc:121
uint32_t m_icmpv4DestinationUnreachableHeaderSize
Definition: qkd-crypto.h:406
DSDV Update Packet Format.
Definition: dsdv-packet.h:60
QKDCommandHeader CreateQKDCommandHeader(Ptr< Packet > p)
Help function used to create QKDCommandHeader by analyzing tags of the packet.
Definition: qkd-crypto.cc:262
uint32_t Deserialize(Buffer::Iterator start)
Definition: qkd-header.cc:67
virtual void Serialize(Buffer::Iterator start) const
Definition: ipv4-header.cc:383
uint32_t GetLength(void) const
Definition: qkd-header.cc:378
uint32_t m_aodvqTypeHeaderSize
Definition: qkd-crypto.h:428
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: icmpv4.cc:247
virtual void Serialize(Buffer::Iterator start) const
Definition: dsdvq-packet.cc:73
uint32_t m_dsdvHeaderSize
Definition: qkd-crypto.h:418
std::string base64_encode(std::string &s)
Help function - base64_encode.
Definition: qkd-crypto.cc:3483
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
#define AODV_RREP_HEADER_PROTOCOL_NUMBER
Definition: aodv-packet.h:43
uint32_t m_aodvTypeHeaderSize
Definition: qkd-crypto.h:421
#define AODV_RREP_ACK_HEADER_PROTOCOL_NUMBER
Definition: aodv-packet.h:44
void SetAuthTag(std::string value)
Definition: qkd-header.cc:497
static const uint32_t packetSize
std::string HexEncode(const std::string &data)
Help function used to encode string to HEX string.
Definition: qkd-crypto.cc:3301
std::string MD5(std::string &inputString)
MD5 Authentication function.
Definition: qkd-crypto.cc:3437
A base class which provides memory management and object aggregation.
Definition: object.h:87
uint32_t m_icmpv4HeaderSize
Definition: qkd-crypto.h:403
void SetEncryptionKeyId(uint32_t value)
Definition: qkd-header.cc:469
virtual uint32_t GetSerializedSize(void) const
Definition: olsr-header.cc:189
virtual void Serialize(Buffer::Iterator start) const
Definition: icmpv4.cc:75
virtual uint32_t GetSerializedSize(void) const
Definition: ipv4-header.cc:375
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
static const uint32_t QKDSTATUS_EMPTY
QKDStatus EMPTY.
Definition: qkd-buffer.h:74
#define AODVQ_RERR_HEADER_PROTOCOL_NUMBER
Definition: aodvq-packet.h:45
virtual void Serialize(Buffer::Iterator start) const
Definition: icmpv4.cc:355
uint32_t Deserialize(Buffer::Iterator start)
Definition: qkd-header.cc:172
uint32_t GetSerializedSize() const
Definition: qkd-header.cc:160
virtual uint32_t GetSerializedSize(void) const
Definition: udp-header.cc:178
std::vector< Ptr< Packet > > ProcessOutgoingPacket(Ptr< Packet > p, Ptr< QKDBuffer > QKDBuffer, uint32_t channelID)
This functions is an entry point toward encryption/authentication of the packet Packet is serialized ...
Definition: qkd-crypto.cc:352
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
void SetCommand(char value)
Definition: qkd-header.cc:102
virtual void Serialize(Buffer::Iterator start) const
Definition: tcp-header.cc:315
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: ipv4-header.cc:423
#define DSDV_PACKET_HEADER_PROTOCOL_NUMBER
Definition: dsdv-packet.h:40
virtual void Serialize(Buffer::Iterator start) const
Definition: olsr-header.cc:220
Route Reply (RREP) Message Format.
Definition: aodvq-packet.h:340
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: dsdv-packet.cc:83