A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
qkd-app-header.cc
Go to the documentation of this file.
1/*
2 * Copyright(c) 2020 DOTFEESA www.tk.etf.unsa.ba
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 *
7 *
8 * Author: Miralem Mehic <miralem.mehic@ieee.org>
9 */
10
11#include "ns3/log.h"
12#include "ns3/object-vector.h"
13#include "ns3/pointer.h"
14#include "ns3/uinteger.h"
15#include "qkd-app-header.h"
16
17namespace ns3 {
18
19NS_LOG_COMPONENT_DEFINE("QKDAppHeader");
20
21NS_OBJECT_ENSURE_REGISTERED(QKDAppHeader);
22
24{
25 m_length = 0;
26 m_messageId = 0;
27 m_encryped = 0;
29}
30
33{
34 static TypeId tid = TypeId("ns3::QKDAppHeader")
37 ;
38 return tid;
39}
40
43{
44 return GetTypeId();
45}
46
49{
50 return 2 * sizeof(uint32_t)
51 + 2 * sizeof(uint8_t)
52 + 3 * 32 * sizeof(uint8_t); //@toDo: AuthTag is variable length!
53}
54
55void
57{
58 i.WriteHtonU32((uint32_t) m_length);
59 i.WriteHtonU32((uint32_t) m_messageId);
60 i.WriteU8((uint8_t) m_encryped);
61 i.WriteU8((uint8_t) m_authenticated);
62
63 i.Write(reinterpret_cast<const uint8_t*>(m_encryptionKeyId.c_str()), m_encryptionKeyId.length());
64 i.Write(reinterpret_cast<const uint8_t*>(m_authenticationKeyId.c_str()), m_authenticationKeyId.length());
65 i.Write(reinterpret_cast<const uint8_t*>(m_authTag.c_str()), m_authTag.length());
66}
67
70{
71
72 Buffer::Iterator i = start;
73 m_valid = false;
74
75 m_length = i.ReadNtohU32();
76 m_messageId = i.ReadNtohU32();
77 m_encryped = i.ReadU8();
78 m_authenticated = i.ReadU8();
79
80 uint32_t len1 = 32;
81 std::vector<uint8_t> tmpBuffer1(len1);
82 i.Read(tmpBuffer1.data(), len1);
83 m_encryptionKeyId = std::string(tmpBuffer1.begin(), tmpBuffer1.end());
84
85 uint32_t len2 = 32;
86 std::vector<uint8_t> tmpBuffer2(len2);
87 i.Read(tmpBuffer2.data(), len2);
88 m_authenticationKeyId = std::string(tmpBuffer2.begin(), tmpBuffer2.end());
89
90 uint32_t len3 = 32;
91 std::vector<uint8_t> tmpBuffer3(len3);
92 i.Read(tmpBuffer3.data(), len3);
93 m_authTag = std::string(tmpBuffer3.begin(), tmpBuffer3.end());
94
95 NS_LOG_DEBUG("Deserialize m_length: " <<(uint32_t) m_length
96 << " m_messageId: " <<(uint32_t) m_messageId
97 << " m_encryptionKeyId: " << m_encryptionKeyId
98 << " m_authenticationKeyId: " << m_authenticationKeyId
99 << " m_valid: " <<(uint32_t) m_valid
100 << " m_authTag: " << m_authTag
101 );
102
103 uint32_t dist = i.GetDistanceFrom(start);
104 NS_LOG_FUNCTION( this << dist << GetSerializedSize() );
106 return dist;
107}
108
109void
110QKDAppHeader::Print(std::ostream &os) const
111{
112 os << "\n"
113 << "MESSAGE ID: " <<(uint32_t) m_messageId << "\t"
114 << "Length: " <<(uint32_t) m_length << "\t"
115
116 << "Authenticated: " <<(uint32_t) m_authenticated << "\t"
117 << "Encrypted: " <<(uint32_t) m_encryped << "\t"
118
119 << "EncryptKeyID: " << m_encryptionKeyId << "\t"
120 << "AuthKeyID: " << m_authenticationKeyId << "\t"
121
122 << "AuthTag: " << m_authTag << "\t\n";
123
124}
125
126bool
128{
129 return(m_messageId == o.m_messageId && m_authenticationKeyId == o.m_authenticationKeyId && m_authTag == o.m_authTag);
130}
131
132std::ostream &
133operator<<(std::ostream & os, QKDAppHeader const & h)
134{
135 h.Print(os);
136 return os;
137}
138
139
140void
148
149 NS_LOG_FUNCTION (this << m_length);
150 return m_length;
151}
152
153void
161
163 return m_messageId;
164}
165
166void
168
169 NS_LOG_FUNCTION (this << value);
170
171 NS_ASSERT(value.size() <= 32);
172 if(value.size() < 32) {
173 uint32_t diff = 32-value.size();
174 std::string newValue = std::string(diff, '0') + value;
176 } else
178}
179
180std::string
186
187
188void
190
191 NS_LOG_FUNCTION (this << value);
192
193 NS_ASSERT(value.size() <= 32);
194 if(value.size() < 32) {
195 uint32_t diff = 32-value.size();
196 std::string newValue = std::string(diff, '0') + value;
198 } else
200}
201
202std::string
208
209void
211
212 NS_LOG_FUNCTION (this << value << value.size());
214}
215
216std::string
218
219 NS_LOG_FUNCTION (this << m_authTag << m_authTag.size());
220 return m_authTag;
221}
222
223void
231
232 NS_LOG_FUNCTION (this << m_encryped);
233 return(uint32_t) m_encryped;
234}
235
236void
248
249
250} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
Protocol header serialization and deserialization.
Definition header.h:33
QKD app packet header that carries info about used encryption, auth tag and other.
uint32_t GetLength() const
bool m_valid
Is header valid or corrupted.
std::string GetAuthTag() const
bool operator==(QKDAppHeader const &o) const
QKDAppHeader()
Constructor.
uint32_t GetEncrypted() const
TypeId GetInstanceTypeId() const override
Get the type ID.
uint32_t m_messageId
message id field
std::string m_authTag
authentication tag of the packet
uint32_t Deserialize(Buffer::Iterator start) override
uint32_t m_length
message length field
void Serialize(Buffer::Iterator start) const override
void Print(std::ostream &os) const override
uint8_t m_authenticated
is packet authenticated or not
void SetMessageId(uint32_t value)
void SetEncryptionKeyId(std::string value)
uint8_t m_encryped
is packet encrypted or not
void SetEncrypted(uint32_t value)
uint32_t GetSerializedSize() const override
void SetLength(uint32_t value)
void SetAuthenticated(uint32_t value)
void SetAuthTag(std::string value)
std::string GetEncryptionKeyId() const
std::string GetAuthenticationKeyId() const
uint32_t GetMessageId() const
std::string m_authenticationKeyId
authentication key id
uint32_t GetAuthenticated() const
void SetAuthenticationKeyId(std::string keyID)
static TypeId GetTypeId()
Get the type ID.
std::string m_encryptionKeyId
encryption key id
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
static unsigned int value(char c)