A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
17
namespace
ns3
{
18
19
NS_LOG_COMPONENT_DEFINE
(
"QKDAppHeader"
);
20
21
NS_OBJECT_ENSURE_REGISTERED
(QKDAppHeader);
22
23
QKDAppHeader::QKDAppHeader
():m_valid(
true
)
24
{
25
m_length
= 0;
26
m_messageId
= 0;
27
m_encryped
= 0;
28
m_authenticated
= 0;
29
}
30
31
TypeId
32
QKDAppHeader::GetTypeId
()
33
{
34
static
TypeId
tid =
TypeId
(
"ns3::QKDAppHeader"
)
35
.
SetParent
<
Header
>()
36
.
AddConstructor<QKDAppHeader>
()
37
;
38
return
tid;
39
}
40
41
TypeId
42
QKDAppHeader::GetInstanceTypeId
()
const
43
{
44
return
GetTypeId
();
45
}
46
47
uint32_t
48
QKDAppHeader::GetSerializedSize
()
const
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
55
void
56
QKDAppHeader::Serialize
(
Buffer::Iterator
i
)
const
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
68
uint32_t
69
QKDAppHeader::Deserialize
(
Buffer::Iterator
start)
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
() );
105
NS_ASSERT
(
dist
==
GetSerializedSize
());
106
return
dist
;
107
}
108
109
void
110
QKDAppHeader::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
126
bool
127
QKDAppHeader::operator==
(
QKDAppHeader
const
&
o
)
const
128
{
129
return
(
m_messageId
==
o
.m_messageId &&
m_authenticationKeyId
==
o
.m_authenticationKeyId &&
m_authTag
==
o
.m_authTag);
130
}
131
132
std::ostream &
133
operator<<
(std::ostream & os,
QKDAppHeader
const
&
h
)
134
{
135
h
.Print(os);
136
return
os;
137
}
138
139
140
void
141
QKDAppHeader::SetLength
(
uint32_t
value
){
142
143
NS_LOG_FUNCTION
(
this
<<
value
);
144
m_length
=
value
;
145
}
146
uint32_t
147
QKDAppHeader::GetLength
()
const
{
148
149
NS_LOG_FUNCTION
(
this
<<
m_length
);
150
return
m_length
;
151
}
152
153
void
154
QKDAppHeader::SetMessageId
(
uint32_t
value
){
155
156
NS_LOG_FUNCTION
(
this
<<
value
);
157
m_messageId
=
value
;
158
}
159
uint32_t
160
QKDAppHeader::GetMessageId
()
const
{
161
162
NS_LOG_FUNCTION
(
this
<<
m_messageId
);
163
return
m_messageId
;
164
}
165
166
void
167
QKDAppHeader::SetEncryptionKeyId
(std::string
value
){
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
;
175
m_encryptionKeyId
=
newValue
;
176
}
else
177
m_encryptionKeyId
=
value
;
178
}
179
180
std::string
181
QKDAppHeader::GetEncryptionKeyId
()
const
{
182
183
NS_LOG_FUNCTION
(
this
<<
m_encryptionKeyId
);
184
return
m_encryptionKeyId
;
185
}
186
187
188
void
189
QKDAppHeader::SetAuthenticationKeyId
(std::string
value
){
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
;
197
m_authenticationKeyId
=
newValue
;
198
}
else
199
m_authenticationKeyId
=
value
;
200
}
201
202
std::string
203
QKDAppHeader::GetAuthenticationKeyId
()
const
{
204
205
NS_LOG_FUNCTION
(
this
<<
m_authenticationKeyId
);
206
return
m_authenticationKeyId
;
207
}
208
209
void
210
QKDAppHeader::SetAuthTag
(std::string
value
){
211
212
NS_LOG_FUNCTION
(
this
<<
value
<<
value
.size());
213
m_authTag
=
value
;
214
}
215
216
std::string
217
QKDAppHeader::GetAuthTag
()
const
{
218
219
NS_LOG_FUNCTION
(
this
<<
m_authTag
<<
m_authTag
.size());
220
return
m_authTag
;
221
}
222
223
void
224
QKDAppHeader::SetEncrypted
(
uint32_t
value
){
225
226
NS_LOG_FUNCTION
(
this
<<
value
);
227
m_encryped
=
value
;
228
}
229
uint32_t
230
QKDAppHeader::GetEncrypted
()
const
{
231
232
NS_LOG_FUNCTION
(
this
<<
m_encryped
);
233
return
(
uint32_t
)
m_encryped
;
234
}
235
236
void
237
QKDAppHeader::SetAuthenticated
(
uint32_t
value
){
238
239
NS_LOG_FUNCTION
(
this
<<
value
);
240
m_authenticated
=
value
;
241
}
242
uint32_t
243
QKDAppHeader::GetAuthenticated
()
const
{
244
245
NS_LOG_FUNCTION
(
this
<<
m_authenticated
);
246
return
(
uint32_t
)
m_authenticated
;
247
}
248
249
250
}
// namespace ns3
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Header
Protocol header serialization and deserialization.
Definition
header.h:33
ns3::QKDAppHeader
QKD app packet header that carries info about used encryption, auth tag and other.
Definition
qkd-app-header.h:61
ns3::QKDAppHeader::GetLength
uint32_t GetLength() const
Definition
qkd-app-header.cc:147
ns3::QKDAppHeader::m_valid
bool m_valid
Is header valid or corrupted.
Definition
qkd-app-header.h:176
ns3::QKDAppHeader::GetAuthTag
std::string GetAuthTag() const
Definition
qkd-app-header.cc:217
ns3::QKDAppHeader::operator==
bool operator==(QKDAppHeader const &o) const
Definition
qkd-app-header.cc:127
ns3::QKDAppHeader::QKDAppHeader
QKDAppHeader()
Constructor.
Definition
qkd-app-header.cc:23
ns3::QKDAppHeader::GetEncrypted
uint32_t GetEncrypted() const
Definition
qkd-app-header.cc:230
ns3::QKDAppHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the type ID.
Definition
qkd-app-header.cc:42
ns3::QKDAppHeader::m_messageId
uint32_t m_messageId
message id field
Definition
qkd-app-header.h:167
ns3::QKDAppHeader::m_authTag
std::string m_authTag
authentication tag of the packet
Definition
qkd-app-header.h:174
ns3::QKDAppHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Definition
qkd-app-header.cc:69
ns3::QKDAppHeader::m_length
uint32_t m_length
message length field
Definition
qkd-app-header.h:166
ns3::QKDAppHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition
qkd-app-header.cc:56
ns3::QKDAppHeader::Print
void Print(std::ostream &os) const override
Definition
qkd-app-header.cc:110
ns3::QKDAppHeader::m_authenticated
uint8_t m_authenticated
is packet authenticated or not
Definition
qkd-app-header.h:170
ns3::QKDAppHeader::SetMessageId
void SetMessageId(uint32_t value)
Definition
qkd-app-header.cc:154
ns3::QKDAppHeader::SetEncryptionKeyId
void SetEncryptionKeyId(std::string value)
Definition
qkd-app-header.cc:167
ns3::QKDAppHeader::m_encryped
uint8_t m_encryped
is packet encrypted or not
Definition
qkd-app-header.h:169
ns3::QKDAppHeader::SetEncrypted
void SetEncrypted(uint32_t value)
Definition
qkd-app-header.cc:224
ns3::QKDAppHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition
qkd-app-header.cc:48
ns3::QKDAppHeader::SetLength
void SetLength(uint32_t value)
Definition
qkd-app-header.cc:141
ns3::QKDAppHeader::SetAuthenticated
void SetAuthenticated(uint32_t value)
Definition
qkd-app-header.cc:237
ns3::QKDAppHeader::SetAuthTag
void SetAuthTag(std::string value)
Definition
qkd-app-header.cc:210
ns3::QKDAppHeader::GetEncryptionKeyId
std::string GetEncryptionKeyId() const
Definition
qkd-app-header.cc:181
ns3::QKDAppHeader::GetAuthenticationKeyId
std::string GetAuthenticationKeyId() const
Definition
qkd-app-header.cc:203
ns3::QKDAppHeader::GetMessageId
uint32_t GetMessageId() const
Definition
qkd-app-header.cc:160
ns3::QKDAppHeader::m_authenticationKeyId
std::string m_authenticationKeyId
authentication key id
Definition
qkd-app-header.h:173
ns3::QKDAppHeader::GetAuthenticated
uint32_t GetAuthenticated() const
Definition
qkd-app-header.cc:243
ns3::QKDAppHeader::SetAuthenticationKeyId
void SetAuthenticationKeyId(std::string keyID)
Definition
qkd-app-header.cc:189
ns3::QKDAppHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
qkd-app-header.cc:32
ns3::QKDAppHeader::m_encryptionKeyId
std::string m_encryptionKeyId
encryption key id
Definition
qkd-app-header.h:172
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
uint32_t
NS_ASSERT
#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
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition
log.h:257
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition
log-macros-enabled.h:229
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::operator<<
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition
angles.cc:148
ns3::value
static unsigned int value(char c)
Definition
qkd-encryptor.cc:267
qkd-app-header.h
src
applications
model
qkd-app-header.cc
Generated on Mon Dec 15 2025 15:21:49 for ns-3 by
1.9.8