Repository navigation
Expand file tree
/
Copy pathMessage.cpp
More file actions
164 lines (149 loc) · 4.6 KB
/
Copy pathMessage.cpp
File metadata and controls
164 lines (149 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*!
* @file Message.cpp
* @brief Implementations of the functions defined in \a Message.h
* @author lhm
* @date 17/07/2019
*/
#include <stdlib.h>
#include <time.h>
#include "Logger.h"
#include "Message.h"
#include "Exception.h"
#include "Conf_format.h"
#include "Header.h"
namespace ModGen {
using namespace std;
map<Message::FILL_METHOD, string> Message::fillString = {
{FILL_METHOD::MESG_FILL_ZERO, "MESG_FILL_ZERO" },
{FILL_METHOD::MESG_FILL_RANDOM, "MESG_FILL_RANDOM" },
{FILL_METHOD::MESG_FILL_UNSET, "MESG_FILL_UNSET" }
};
////////////////////////////////////////////////////////////////////////
Message::Message() :
name(),
header(),
header_ptr(nullptr),
data_size(0),
src_port(0),
dst_port(0),
src_ip(),
dst_ip(),
interface(),
fill(MESG_FILL_UNSET),
mesgToSend(vector<uint8_t>())
{
srand (static_cast<unsigned int>(time(nullptr)));
}
////////////////////////////////////////////////////////////////////////
Message::~Message()
{}
////////////////////////////////////////////////////////////////////////
bool Message::isValid() const
{
return ( !name.empty() &&
!header.empty() &&
data_size &&
src_port &&
dst_port &&
!src_ip.empty() &&
!dst_ip.empty() &&
fill != MESG_FILL_UNSET
);
}
////////////////////////////////////////////////////////////////////////
void Message::setParam(const string& p_name, const string& p_value)
{
if(p_name.compare(Messages_format::name) == 0)
{
name = p_value;
}
else if(p_name.compare(Messages_format::header) == 0)
{
header = p_value;
}
else if(p_name.compare(Messages_format::size) == 0)
{
data_size = static_cast<uint32_t>(stoi(p_value));
}
else if(p_name.compare(Messages_format::src_ip) == 0)
{
src_ip = p_value;
}
else if(p_name.compare(Messages_format::dst_ip) == 0)
{
dst_ip = p_value;
}
else if(p_name.compare(Messages_format::src_port) == 0)
{
src_port = static_cast<uint32_t>(stoi(p_value));
}
else if(p_name.compare(Messages_format::dst_port) == 0)
{
dst_port = static_cast<uint32_t>(stoi(p_value));
}
else if(p_name.compare(Messages_format::fill) == 0)
{
if(p_value.compare("MESG_FILL_ZERO") == 0)
{
fill = MESG_FILL_ZERO;
}
else if(p_value.compare("MESG_FILL_RANDOM") == 0)
{
fill = MESG_FILL_RANDOM;
}
else
{
throw Exception::UnimplementedElement<FILL_METHOD>(p_value);
}
}
else if(p_name.compare(Messages_format::interface) == 0)
{
interface = p_value;
}
else
{
throw Exception::ParsingFileParamError<Message>(p_name);
}
}
////////////////////////////////////////////////////////////////////////
string Message::getDesc() const
{
return (
"Message " + name +
"\n\t-header = " + header +
"\n\t-interface = " + interface +
"\n\t-data_size = " + to_string(data_size) +
"\n\t-src port = " + to_string(src_port) +
"\n\t-dst port = " + to_string(dst_port) +
"\n\t-src ip = " + src_ip +
"\n\t-dst ip = " + dst_ip +
"\n\t-fill meth = " + fillString[fill] + "\n"
);
}
////////////////////////////////////////////////////////////////////////
vector<uint8_t>& Message::createMessage()
{
mesgToSend.clear();
if(!header_ptr)
{
ERROR("Error - The message which ID is " + name + " references a null Header.");
throw Exception::IntegrityCheckException<Header>("The message which ID is " + name + " references a null Header." );
}
header_ptr->mesgSizeParams(data_size);
header_ptr->addToMessage(mesgToSend);
switch(fill)
{
case MESG_FILL_ZERO:
for(uint32_t i = 0; i < data_size; i++) { mesgToSend.push_back(0); }
break;
case MESG_FILL_RANDOM:
for(uint32_t i = 0; i < data_size; i++) { mesgToSend.push_back(static_cast<uint8_t>(rand() % 255)); }
break;
default:
ERROR("Error - createMessage Could not parse the filling method.");
throw Exception::UnimplementedElement<FILL_METHOD>(fill);
}
//FieldHelper::printBits(mesgToSend, "Message " + name + ": ");
return mesgToSend;
}
} // namespace ModGen