A Discrete-Event Network Simulator
API
qkd-sdn-controller.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 DOTFEESA www.tk.etf.unsa.ba
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Emir Dervisevic <emir.dervisevic@etf.unsa.ba>
19  * Miralem Mehic <miralem.mehic@ieee.org>
20  */
21 #ifndef QKD_SDN_CONTROLLER_H
22 #define QKD_SDN_CONTROLLER_H
23 
24 #include "ns3/address.h"
25 #include "ns3/application.h"
26 #include "ns3/event-id.h"
27 #include "ns3/ptr.h"
28 #include "ns3/data-rate.h"
29 #include "ns3/traced-callback.h"
30 #include "ns3/random-variable-stream.h"
31 #include "ns3/qkd-buffer.h"
32 #include "ns3/qkd-connection-register.h"
33 #include "ns3/qkd-key-association-link-entry.h"
34 #include "ns3/qkd-application-entry.h"
35 #include "ns3/qkd-kms-queue-logic.h"
36 #include "ns3/json.h"
37 #include <unordered_map>
38 #include "ns3/http.h"
39 #include "ns3/uuid.h"
40 
41 #include <iostream>
42 #include <sstream>
43 #include <unistd.h>
44 #include <sstream>
45 #include <string>
46 #include <regex>
47 #include <cryptopp/base64.h>
48 
49 
50 namespace ns3 {
51 
52 class Address;
53 class Socket;
54 
67 {
68 public:
69 
74  {
75  NONE = 50,
76  GET_ROUTE = 12,
80 
87  NEW_APP = 6,
88  REGISTER = 7,
89  FILL = 8,
90  STORE_PP_KEYS = 9, //Store postprocessing keys
91  TRANSFORM_KEYS = 10, //Transform (merge, split) QKD keys
93  };
94 
99  static TypeId GetTypeId (void);
100 
104  QKDSDNController ();
105 
109  virtual ~QKDSDNController ();
110 
115  Ptr<Socket> GetSocket (void) const;
116 
117  //void PrepareOutput (std::string key, uint32_t value); @toDo ? not used
118 
124  void SetSocket (std::string type, Ptr<Socket> socket);
125 
130  uint32_t GetTotalRx () const;
131 
136  std::map<Ptr<Socket>, Ptr<Socket> > GetAcceptedSockets (void) const;
137 
138 
143  uint32_t GetMaxKeyPerRequest();
144 
152  void AddNewLink(
153  uint32_t srcSaeId,
154  uint32_t dstSaeId,
155  Ipv4Address kmsDstAddress,
156  Ptr<QKDBuffer> srcBuffer
157  );
158 
167  void RegisterSaePair(
168  Ptr<Node> srcNode,
169  Ptr<Node> dstNode,
170  uint32_t srcSaeId,
171  uint32_t dstSaeId,
172  std::string type
173  );
174 
179  void SetNode(Ptr<Node> n);
180 
185  Ptr<Node> GetNode();
186 
191  uint32_t GetId();
192 
198  m_local = address;
199  }
200 
206  return m_local;
207  }
208 
213  void SetPort(uint32_t port) {
214  m_port = port;
215  }
216 
221  uint32_t GetPort() {
222  return m_port;
223  }
224 
225 protected:
226 
230  virtual void DoDispose (void);
231 
232 private:
233 
240 
244  struct AddressHash
245  {
256  size_t operator() (const Address &x) const
257  {
260  return std::hash<uint32_t>()(a.GetIpv4 ().Get ());
261  }
262  };
263 
264  // inherited from Application base class.
268  void StartApplication (void); // Called at time specified by Start
269 
273  void StopApplication (void); // Called at time specified by Stop
274 
280  void SendToSocketPair (Ptr<Socket> socket, Ptr<Packet> packet);
281 
288 
293  void HandleRead (Ptr<Socket> socket);
294 
300  void HandleAccept (Ptr<Socket> s, const Address& from);
301 
306  void HandlePeerClose (Ptr<Socket> socket);
307 
312  void HandlePeerError (Ptr<Socket> socket);
313 
322  void PacketReceived (const Ptr<Packet> &p, const Address &from, Ptr<Socket> socket);
323 
335  void ProcessRequest(HTTPMessage header, Ptr<Packet> packet, Ptr<Socket> socket);
336 
337  /*
338  * \brief Process OPEN_CONNECT request - ETSI QKD GS 004
339  * \param header received request
340  * \param socket receiving socket
341  */
343 
344  /*
345  * \brief Process GET_KEY request - ETSI QKD GS 004
346  * \param ksid Unique identifier of the association
347  * \param header received request
348  * \param socket receiving socket
349  */
350  void ProcessGetKey004Request(std::string ksid, HTTPMessage header, Ptr<Socket> socket);
351 
352  /*
353  * \brief Process CLOSE request - ETSI QKD GS 004
354  * \param ksid Unique identifier of the association
355  * \param header received request
356  * \param socket receiving socket
357  */
358  void ProcessCloseRequest(std::string ksid, HTTPMessage header, Ptr<Socket> socket);
359 
360  /*
361  * \brief Process NEW_APP request
362  * \param header received request
363  * \param socket receiving socket
364  */
366 
368 
369  void RegisterRequest (std::string ksid);
370 
371  void ProcessRegisterRequest (HTTPMessage header, std::string ksid, Ptr<Socket> socket);
372 
374 
375  void PrepareSinkSocket ();
376 
386  void ProcessAddKeysRequest (HTTPMessage h, Ptr<Socket> socket, std::string ksid);
387 
399 
406  void ReleaseAssociation (std::string ksid, std::string surplusKeyId, uint32_t syncIndex);
407 
415  //nlohmann::json Check014GetKeyRequest(nlohmann::json request, QKDLocationRegisterEntry conn);
416 
423 
429  std::string Base64Encode(std::string input);
430 
436  std::string Base64Decode(std::string input);
437 
438 
439 private:
440 
441  struct QoS
442  {
443  uint32_t chunkSize; //Key_chunk_size
444  uint32_t maxRate; //Max_bps
445  uint32_t minRate; //Min_bps
446  uint32_t jitter; //Jitter
447  uint32_t priority; //Priority
448  uint32_t timeout; //Timeout
449  uint32_t TTL; //Time to Live
450  //metadata mimetype is left out
451  };
452 
453  struct ChunkKey
454  {
455  uint32_t index;
456  uint32_t chunkSize;
457  bool ready;
458  std::string key; //key of key_chunk_size
459  //std::vector<std::pair<std::string, std::pair<uint32_t, uint32_t> > > keyIds; //"QKDKey"s that form ChunkKey
460  //keyId start end
461  };
462 
463  struct HttpQuery
464  {
465  RequestType method_type; //For every query!
466 
467  //Specific to TRANSFORM
470  std::vector<std::string> transform_key_IDs;
471  std::vector<std::string> to_transform_key_IDs;
472  std::string surplus_key_ID;
473  uint32_t sae_id; //Needed to specify buffer to fetch the key from
474 
475  //Specific to ETSI 004 (NEW_APP)
476  uint32_t source_sae;
477  uint32_t destination_sae;
478  std::string ksid;
479 
480  //Specific to ETSI 004 (KMS CLOSE)
481  uint32_t sync_index;
482 
483  };
484 
485  struct Association004 //Holds information of the association and dedicated key store
486  {
487  uint32_t srcSaeId; //Source application that requested the KSID
488  uint32_t dstSaeId; //Destination application
489  uint32_t associationDirection; //0-Outbound, 1-Inbound - Important while monitoring the associations!
490  QoS qos; //Quality of service
491  Ipv4Address dstKmsNode; //Address of the destination KMS. Important!
492  bool peerRegistered; //KMS must know the state of connection for association on peer KMS!
493  std::map<uint32_t, ChunkKey> buffer; //index & key ; index is KeyId for key of the association
494  uint32_t lastIndex;
495  std::vector<std::string> tempBuffer; //Buffer for keys in negotiation. It holds only KeyIds, while keys are in QKDBuffer!
496  };
497 
499 
500  std::map<std::string, Association004> m_associations004; //Associations map
501 
502  Ptr<Socket> m_sinkSocket; // Associated socket
503 
505 
506  uint32_t m_port;
507 
508  uint32_t m_totalRx;
509 
510  uint32_t m_totalRxKMSs;
511 
513 
514  uint32_t m_kms_id;
515 
516  uint32_t m_kms_key_id; //key counter to generate unique keyIDs on KMS
517 
519 
520  std::map<uint32_t, EventId > m_scheduledChecks;
521 
522  std::map<Ipv4Address, uint32_t> m_flagedIPAdr;
524 
532 
535 
537 
540 
541  uint32_t m_maxKeyPerRequest; //Maximal number of keys per request QKDApp can ask for
542  uint32_t m_minKeySize; //Minimal size of key QKDApp can request from KMS
543  uint32_t m_maxKeySize; //Maximal size of key QKDApp can request from KMS
544  uint32_t m_defaultKeySize; //Default size of key KMS delivers to QKDApp
545 
546  std::unordered_map<Address, Ptr<Packet>, AddressHash> m_buffer;
547  std::unordered_map<Address, Ptr<Packet>, AddressHash> m_bufferKMS;
548 
549  // In the case of TCP, each socket accept returns a new socket, so the
550  // listening socket is stored separately from the accepted sockets
551  std::map<Ptr<Socket>, Ptr<Socket> > m_socketPairs;
552 
553  std::map<Ipv4Address, std::pair<Ptr<Socket>, Ptr<Socket> > > m_socketPairsKMS;
554 
555  Ptr<Node> m_node; //<! node on which KMS is installed
556  std::map<Ptr<Socket>, Ptr<Packet> > m_packetQueues;
557 
559 
563  void ConnectionSucceeded (Ptr<Socket> socket);
564  void ConnectionFailed (Ptr<Socket> socket);
565  void DataSend (Ptr<Socket>, uint32_t); // for socket's SetSendCallback
566 
569  void DataSendKMSs (Ptr<Socket>, uint32_t); // for socket's SetSendCallback
570 
571 
579  std::map<Ipv4Address, std::vector<HttpQuery> > m_httpRequestsQueryKMS;
580  std::multimap<uint32_t, Ptr<Socket> > m_http004App; //SAE_ID, receiving socket
581 
587  void HttpKMSAddQuery(Ipv4Address dstKms, HttpQuery request);
588 
594 
601 
602  void Http004AppQuery (uint32_t saeId, Ptr<Socket> socket);
603 
604  void Http004AppQueryComplete (uint32_t saeId);
605 
607 
609 
614  void CheckSocketsKMS (Ipv4Address dstSaeId);
615 
622 
628  std::string PacketToString (Ptr<Packet> packet);
629 
638  void ReadJsonQos (
639  QKDSDNController::QoS &inQos,
640  nlohmann::json jOpenConnectRequest );
641 
656  uint32_t srcSaeId, uint32_t dstSaeId,
657  QKDSDNController::QoS inQos,
658  Ipv4Address dstKms, std::string ksid );
659 
660 
661 };
662 
663 } // namespace ns3
664 
665 #endif /* QKD_APPLICATION_H */
666 
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
An identifier for simulation events.
Definition: event-id.h:55
The basic class to represent both HTTP requests and responses.
Definition: http.h:78
an Inet address class
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
uint32_t Get() const
Get the host-order 32-bit IP address.
TracedCallback< Ptr< const Packet > > m_txTraceSDNs
static TypeId GetTypeId(void)
Get the type ID.
std::map< Ptr< Socket >, Ptr< Packet > > m_packetQueues
Buffering unsend messages due to connection problems.
void ProcessNewAppResponse(HTTPMessage header, Ptr< Socket > socket)
std::map< std::string, Association004 > m_associations004
uint32_t GetMaxKeyPerRequest()
Get maximum number of keys per request (ETSI QKD 014)
uint32_t GetId()
Get key menager system ID.
TracedCallback< Ptr< const Packet >, const Address & > m_rxTrace
Traced Callback: received packets, source address.
Ipv4Address GetAddress()
Get local address.
void RegisterRequest(std::string ksid)
void ProcessCloseRequest(std::string ksid, HTTPMessage header, Ptr< Socket > socket)
void ProcessGetKey004Request(std::string ksid, HTTPMessage header, Ptr< Socket > socket)
Ptr< Socket > GetSendSocketKMS(Ipv4Address kmsDstAddress)
Obtain send socket.
uint32_t GetTotalRx() const
Get the total amount of bytes received.
void HttpKMSAddQuery(Ipv4Address dstKms, HttpQuery request)
remember HTTP request made to peer KMS
std::map< Ptr< Socket >, Ptr< Socket > > m_socketPairs
the accepted sockets
void HandleRead(Ptr< Socket > socket)
Handle a packet received by the KMS application.
void StopApplication(void)
Stop KMS Application.
uint32_t GetPort()
Get local port.
TracedCallback< const std::string &, const uint32_t &, const uint32_t &, const uint32_t & > m_keyServedETSI004Trace
void ProcessRegisterRequest(HTTPMessage header, std::string ksid, Ptr< Socket > socket)
TracedCallback< Ptr< const Packet >, const Address & > m_rxTraceSDNs
void StartApplication(void)
Start KMS Application.
std::string Base64Decode(std::string input)
Base64 decoder.
void HandlePeerClose(Ptr< Socket > socket)
Handle an connection close.
Ptr< QKDKMSQueueLogic > m_queueLogic
KMS Queue Logic for ETSI 004 QoS handling.
uint32_t m_totalRxKMSs
Total bytes received between KMSs.
void HandlePeerError(Ptr< Socket > socket)
Handle an connection error.
std::string CreateNew004Association(uint32_t srcSaeId, uint32_t dstSaeId, QKDSDNController::QoS inQos, Ipv4Address dstKms, std::string ksid)
Create a new assocation.
uint32_t m_port
Local port to bind to.
Ipv4Address m_local
Local address to bind to.
void DataSendKMSs(Ptr< Socket >, uint32_t)
TracedCallback< const uint32_t & > m_newKeyGeneratedTraceEmir
std::map< uint32_t, EventId > m_scheduledChecks
uint32_t m_totalRx
Total bytes received
void ConnectionFailed(Ptr< Socket > socket)
RequestType HttpQueryMethod(Ipv4Address dstKms)
obtain method_type to map the HTTP response
TracedCallback< const std::string &, const uint32_t &, Ptr< QKDKey > > m_keyServedETSI014Trace
void ConnectionSucceededKMSs(Ptr< Socket > socket)
double m_qkdLinkDefaultUpdateInterval
Default update interval of QKD link status (seconds)
std::multimap< uint32_t, Ptr< Socket > > m_http004App
void SetSocket(std::string type, Ptr< Socket > socket)
Set sink socket.
@ ETSI_QKD_014_GET_KEY
Integer equivalent = 1.
@ ETSI_QKD_014_GET_KEY_WITH_KEY_IDS
Integer equivalent = 2.
@ ETSI_QKD_014_GET_STATUS
Integer equivalent = 0.
void SetAddress(Ipv4Address address)
Set local address.
QKDSDNController::RequestType FetchRequestType(std::string s)
Get request type.
TracedCallback< const uint32_t & > m_keyServedTraceEmir
void Http004AppQuery(uint32_t saeId, Ptr< Socket > socket)
void PacketReceived(const Ptr< Packet > &p, const Address &from, Ptr< Socket > socket)
Assemble byte stream to extract HTTPMessage.
std::string PacketToString(Ptr< Packet > packet)
Convert packet to string.
Ptr< Node > GetNode()
Get node.
void SendToSocketPair(Ptr< Socket > socket, Ptr< Packet > packet)
Send packet to the pair socket.
void CheckSocketsKMS(Ipv4Address dstSaeId)
Prepare send socket to communicate with peer KMS Application.
std::map< Ipv4Address, std::pair< Ptr< Socket >, Ptr< Socket > > > m_socketPairsKMS
the accepted sockets for communication between KMSs
virtual void DoDispose(void)
@toDo
std::string Base64Encode(std::string input)
Base64 encoder.
nlohmann::json CreateKeyContainer(std::vector< Ptr< QKDKey >> keys)
Check the correctness of QKD application request and ability of KMS to fullfil correct request.
virtual ~QKDSDNController()
QKDSDNController destructor.
void Http004AppQueryComplete(uint32_t saeId)
void ConnectionSucceeded(Ptr< Socket > socket)
@toDo:following functions
std::map< Ptr< Socket >, Ptr< Socket > > GetAcceptedSockets(void) const
Get list of all accepted sockets.
void AddNewLink(uint32_t srcSaeId, uint32_t dstSaeId, Ipv4Address kmsDstAddress, Ptr< QKDBuffer > srcBuffer)
Inform KMS about the new QKD connection/link.
std::map< Ipv4Address, std::vector< HttpQuery > > m_httpRequestsQueryKMS
TracedCallback< const Ipv4Address &, Ptr< const Packet > > m_dropTrace
Traced callback: fired when a packet is dropped.
void ConnectionFailedKMSs(Ptr< Socket > socket)
void RegisterSaePair(Ptr< Node > srcNode, Ptr< Node > dstNode, uint32_t srcSaeId, uint32_t dstSaeId, std::string type)
Inform KMS about the SAE connection.
void ProcessAddKeysResponse(HTTPMessage header, Ptr< Socket > socket)
Process FILL response.
void HandleAccept(Ptr< Socket > s, const Address &from)
Handle an incoming connection.
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_bufferKMS
Buffer for received packets (TCP segmentation)
void ProcessRegisterResponse(HTTPMessage header, Ptr< Socket > socket)
TracedCallback< const uint32_t &, const uint32_t & > m_newKeyGeneratedTrace
void SetPort(uint32_t port)
Set local port.
void ReadJsonQos(QKDSDNController::QoS &inQos, nlohmann::json jOpenConnectRequest)
Read the parameters from the JSON OPEN_CONNECT structure!
void DataSend(Ptr< Socket >, uint32_t)
void SendToSocketPairKMS(Ptr< Socket > socket, Ptr< Packet > packet)
Send packet to the pair socket.
std::unordered_map< Address, Ptr< Packet >, AddressHash > m_buffer
Buffer for received packets (TCP segmentation)
void ProcessOpenConnectRequest(HTTPMessage header, Ptr< Socket > socket)
void HttpKMSCompleteQuery(Ipv4Address dstKms)
remove mapped HTTP response from query
std::map< Ipv4Address, uint32_t > m_flagedIPAdr
void ReleaseAssociation(std::string ksid, std::string surplusKeyId, uint32_t syncIndex)
release key stream association
Ptr< Socket > GetSocket(void) const
Get sink socket.
QKDSDNController()
QKDSDNController constructor.
void ProcessRequest(HTTPMessage header, Ptr< Packet > packet, Ptr< Socket > socket)
QKD key manager system application process the request from QKDApp, and complete certain actions to r...
TracedCallback< Ptr< const Packet > > m_txTrace
void ProcessNewAppRequest(HTTPMessage header, Ptr< Socket > socket)
void ProcessAddKeysRequest(HTTPMessage h, Ptr< Socket > socket, std::string ksid)
Process FILL request.
void SetNode(Ptr< Node > n)
Set node.
Ipv4Address GetDestinationKmsAddress(Ptr< Socket > socket)
Ptr< Socket > GetSocketFromHttp004AppQuery(uint32_t saeId)
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:76
address
Definition: first.py:47
basic_json<> json
default JSON class
Definition: json.h:3000
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hashing for the Address class.
size_t operator()(const Address &x) const
operator ()
std::map< uint32_t, ChunkKey > buffer
std::vector< std::string > tempBuffer
std::vector< std::string > transform_key_IDs
std::vector< std::string > to_transform_key_IDs