/src/PcapPlusPlus/Packet++/src/GvcpLayer.cpp
Line | Count | Source |
1 | | #include "GvcpLayer.h" |
2 | | #include "PayloadLayer.h" |
3 | | #include "SystemUtils.h" |
4 | | #include <algorithm> |
5 | | #include <cstddef> |
6 | | #include <cstring> |
7 | | #include <stdexcept> |
8 | | #include <sstream> |
9 | | |
10 | | namespace pcpp |
11 | | { |
12 | | namespace |
13 | | { |
14 | | using GvcpCommand = GvcpLayer::GvcpCommand; |
15 | | using GvcpResponseStatus = GvcpAcknowledgeLayer::GvcpResponseStatus; |
16 | | using GvcpVersion = GvcpDiscoveryAcknowledgeLayer::GvcpVersion; |
17 | | |
18 | | constexpr uint8_t gvcpMagicNumber = 0x42; |
19 | | constexpr GvcpFlag gvcpAcknowledgeFlag = 0x01; |
20 | | constexpr GvcpFlag gvcpAllowBroadcastAckFlag = 0x10; |
21 | | |
22 | | /// @return True if the value is an acknowledge value. Command values are even and each acknowledge value is |
23 | | /// the matching command value + 1 |
24 | | bool isAcknowledgeValue(GvcpCommand command) |
25 | 0 | { |
26 | 0 | return (static_cast<uint16_t>(command) & 0x01) != 0; |
27 | 0 | } |
28 | | |
29 | | /// @return The name of a command defined by the spec, or nullptr if the value isn't defined |
30 | | constexpr const char* getGvcpCommandName(GvcpCommand command) |
31 | 0 | { |
32 | 0 | switch (command) |
33 | 0 | { |
34 | 0 | case GvcpCommand::DiscoveredCmd: |
35 | 0 | return "DiscoveredCmd"; |
36 | 0 | case GvcpCommand::DiscoveredAck: |
37 | 0 | return "DiscoveredAck"; |
38 | 0 | case GvcpCommand::ForceIpCmd: |
39 | 0 | return "ForceIpCmd"; |
40 | 0 | case GvcpCommand::ForceIpAck: |
41 | 0 | return "ForceIpAck"; |
42 | 0 | case GvcpCommand::PacketResendCmd: |
43 | 0 | return "PacketResendCmd"; |
44 | 0 | case GvcpCommand::PacketResendAck: |
45 | 0 | return "PacketResendAck"; |
46 | 0 | case GvcpCommand::ReadRegCmd: |
47 | 0 | return "ReadRegCmd"; |
48 | 0 | case GvcpCommand::ReadRegAck: |
49 | 0 | return "ReadRegAck"; |
50 | 0 | case GvcpCommand::WriteRegCmd: |
51 | 0 | return "WriteRegCmd"; |
52 | 0 | case GvcpCommand::WriteRegAck: |
53 | 0 | return "WriteRegAck"; |
54 | 0 | case GvcpCommand::ReadMemCmd: |
55 | 0 | return "ReadMemCmd"; |
56 | 0 | case GvcpCommand::ReadMemAck: |
57 | 0 | return "ReadMemAck"; |
58 | 0 | case GvcpCommand::WriteMemCmd: |
59 | 0 | return "WriteMemCmd"; |
60 | 0 | case GvcpCommand::WriteMemAck: |
61 | 0 | return "WriteMemAck"; |
62 | 0 | case GvcpCommand::PendingAck: |
63 | 0 | return "PendingAck"; |
64 | 0 | case GvcpCommand::EventCmd: |
65 | 0 | return "EventCmd"; |
66 | 0 | case GvcpCommand::EventAck: |
67 | 0 | return "EventAck"; |
68 | 0 | case GvcpCommand::EventDataCmd: |
69 | 0 | return "EventDataCmd"; |
70 | 0 | case GvcpCommand::EventDataAck: |
71 | 0 | return "EventDataAck"; |
72 | 0 | case GvcpCommand::ActionCmd: |
73 | 0 | return "ActionCmd"; |
74 | 0 | case GvcpCommand::ActionAck: |
75 | 0 | return "ActionAck"; |
76 | 0 | default: |
77 | 0 | return nullptr; |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | /// @return The name of a status defined by the spec, or nullptr if the value isn't defined |
82 | | constexpr const char* getGvcpResponseStatusName(GvcpResponseStatus status) |
83 | 0 | { |
84 | 0 | switch (status) |
85 | 0 | { |
86 | 0 | case GvcpResponseStatus::Success: |
87 | 0 | return "Success"; |
88 | 0 | case GvcpResponseStatus::PacketResend: |
89 | 0 | return "PacketResend"; |
90 | 0 | case GvcpResponseStatus::NotImplemented: |
91 | 0 | return "NotImplemented"; |
92 | 0 | case GvcpResponseStatus::InvalidParameter: |
93 | 0 | return "InvalidParameter"; |
94 | 0 | case GvcpResponseStatus::InvalidAddress: |
95 | 0 | return "InvalidAddress"; |
96 | 0 | case GvcpResponseStatus::WriteProtect: |
97 | 0 | return "WriteProtect"; |
98 | 0 | case GvcpResponseStatus::BadAlignment: |
99 | 0 | return "BadAlignment"; |
100 | 0 | case GvcpResponseStatus::AccessDenied: |
101 | 0 | return "AccessDenied"; |
102 | 0 | case GvcpResponseStatus::Busy: |
103 | 0 | return "Busy"; |
104 | 0 | case GvcpResponseStatus::LocalProblem: |
105 | 0 | return "LocalProblem"; |
106 | 0 | case GvcpResponseStatus::MsgMismatch: |
107 | 0 | return "MsgMismatch"; |
108 | 0 | case GvcpResponseStatus::InvalidProtocol: |
109 | 0 | return "InvalidProtocol"; |
110 | 0 | case GvcpResponseStatus::NoMsg: |
111 | 0 | return "NoMsg"; |
112 | 0 | case GvcpResponseStatus::PacketUnavailable: |
113 | 0 | return "PacketUnavailable"; |
114 | 0 | case GvcpResponseStatus::DataOverrun: |
115 | 0 | return "DataOverrun"; |
116 | 0 | case GvcpResponseStatus::InvalidHeader: |
117 | 0 | return "InvalidHeader"; |
118 | 0 | case GvcpResponseStatus::WrongConfig: |
119 | 0 | return "WrongConfig"; |
120 | 0 | case GvcpResponseStatus::PacketNotYetAvailable: |
121 | 0 | return "PacketNotYetAvailable"; |
122 | 0 | case GvcpResponseStatus::PacketAndPrevRemovedFromMemory: |
123 | 0 | return "PacketAndPrevRemovedFromMemory"; |
124 | 0 | case GvcpResponseStatus::PacketRemovedFromMemory: |
125 | 0 | return "PacketRemovedFromMemory"; |
126 | 0 | case GvcpResponseStatus::NoRefTime: |
127 | 0 | return "NoRefTime"; |
128 | 0 | case GvcpResponseStatus::PacketTemporarilyUnavailable: |
129 | 0 | return "PacketTemporarilyUnavailable"; |
130 | 0 | case GvcpResponseStatus::Overflow: |
131 | 0 | return "Overflow"; |
132 | 0 | case GvcpResponseStatus::ActionLate: |
133 | 0 | return "ActionLate"; |
134 | 0 | case GvcpResponseStatus::LeaderTrailerOverflow: |
135 | 0 | return "LeaderTrailerOverflow"; |
136 | 0 | case GvcpResponseStatus::Error: |
137 | 0 | return "Error"; |
138 | 0 | default: |
139 | 0 | return nullptr; |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | | GvcpCommand toGvcpCommand(uint16_t value) |
144 | 0 | { |
145 | 0 | const auto command = static_cast<GvcpCommand>(value); |
146 | 0 | return getGvcpCommandName(command) != nullptr ? command : GvcpCommand::Unknown; |
147 | 0 | } |
148 | | |
149 | | GvcpResponseStatus toGvcpResponseStatus(uint16_t value) |
150 | 0 | { |
151 | 0 | const auto status = static_cast<GvcpResponseStatus>(value); |
152 | 0 | return getGvcpResponseStatusName(status) != nullptr ? status : GvcpResponseStatus::Unknown; |
153 | 0 | } |
154 | | |
155 | | const char* commandToString(GvcpCommand command) |
156 | 0 | { |
157 | 0 | const char* name = getGvcpCommandName(command); |
158 | 0 | return name != nullptr ? name : "Unknown"; |
159 | 0 | } |
160 | | |
161 | | const char* statusToString(GvcpResponseStatus status) |
162 | 0 | { |
163 | 0 | const char* name = getGvcpResponseStatusName(status); |
164 | 0 | return name != nullptr ? name : "Unknown"; |
165 | 0 | } |
166 | | |
167 | | /// Read a fixed size char field which isn't necessarily null-terminated |
168 | | template <size_t N> std::string fieldToString(const char (&field)[N]) |
169 | 0 | { |
170 | 0 | return std::string(field, std::find(field, field + N, '\0')); |
171 | 0 | } Unexecuted instantiation: GvcpLayer.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > pcpp::(anonymous namespace)::fieldToString<32ul>(char const (&) [32ul]) Unexecuted instantiation: GvcpLayer.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > pcpp::(anonymous namespace)::fieldToString<48ul>(char const (&) [48ul]) Unexecuted instantiation: GvcpLayer.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > pcpp::(anonymous namespace)::fieldToString<16ul>(char const (&) [16ul]) |
172 | | |
173 | | /// Write a string to a fixed size char field, always leaving room for the null terminator |
174 | | template <size_t N> void stringToField(const std::string& value, char (&field)[N]) |
175 | 0 | { |
176 | 0 | memset(field, 0, N); |
177 | 0 | memcpy(field, value.data(), std::min(value.size(), N - 1)); |
178 | 0 | } Unexecuted instantiation: GvcpLayer.cpp:void pcpp::(anonymous namespace)::stringToField<32ul>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char (&) [32ul]) Unexecuted instantiation: GvcpLayer.cpp:void pcpp::(anonymous namespace)::stringToField<48ul>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char (&) [48ul]) Unexecuted instantiation: GvcpLayer.cpp:void pcpp::(anonymous namespace)::stringToField<16ul>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char (&) [16ul]) |
179 | | } // namespace |
180 | | |
181 | | std::ostream& operator<<(std::ostream& os, GvcpCommand command) |
182 | 0 | { |
183 | 0 | return os << commandToString(command); |
184 | 0 | } |
185 | | |
186 | | std::ostream& operator<<(std::ostream& os, GvcpResponseStatus status) |
187 | 0 | { |
188 | 0 | return os << statusToString(status); |
189 | 0 | } |
190 | | |
191 | | // -------- Class GvcpLayer ----------------- |
192 | | |
193 | | uint16_t GvcpLayer::getRawCommand(const uint8_t* data) |
194 | 0 | { |
195 | 0 | static_assert(offsetof(GvcpRequestLayer::gvcp_request_header, command) == |
196 | 0 | offsetof(GvcpAcknowledgeLayer::gvcp_ack_header, command), |
197 | 0 | "The command must have the same offset in both GVCP headers"); |
198 | 0 | return netToHost16(reinterpret_cast<const GvcpRequestLayer::gvcp_request_header*>(data)->command); |
199 | 0 | } |
200 | | |
201 | | GvcpLayer* GvcpLayer::parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
202 | 0 | { |
203 | 0 | const bool isRequest = GvcpRequestLayer::isDataValid(data, dataLen); |
204 | 0 | if (!isRequest && !GvcpAcknowledgeLayer::isDataValid(data, dataLen)) |
205 | 0 | return nullptr; |
206 | | |
207 | 0 | const GvcpCommand command = toGvcpCommand(getRawCommand(data)); |
208 | |
|
209 | 0 | if (isRequest) |
210 | 0 | { |
211 | 0 | if (command == GvcpCommand::DiscoveredCmd) |
212 | 0 | return new GvcpDiscoveryRequestLayer(data, dataLen, prevLayer, packet); |
213 | 0 | if (command == GvcpCommand::ForceIpCmd && GvcpForceIpRequestLayer::isDataValid(data, dataLen)) |
214 | 0 | return new GvcpForceIpRequestLayer(data, dataLen, prevLayer, packet); |
215 | 0 | return new GvcpRequestLayer(data, dataLen, prevLayer, packet); |
216 | 0 | } |
217 | | |
218 | 0 | if (command == GvcpCommand::DiscoveredAck && GvcpDiscoveryAcknowledgeLayer::isDataValid(data, dataLen)) |
219 | 0 | return new GvcpDiscoveryAcknowledgeLayer(data, dataLen, prevLayer, packet); |
220 | 0 | if (command == GvcpCommand::ForceIpAck) |
221 | 0 | return new GvcpForceIpAcknowledgeLayer(data, dataLen, prevLayer, packet); |
222 | 0 | return new GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet); |
223 | 0 | } |
224 | | |
225 | | void GvcpLayer::initLayer(size_t dataLen) |
226 | 0 | { |
227 | 0 | allocData(dataLen); |
228 | 0 | m_Protocol = GVCP; |
229 | 0 | } |
230 | | |
231 | | void GvcpLayer::parseNextLayer() |
232 | 0 | { |
233 | 0 | const size_t headerLen = getHeaderLen(); |
234 | 0 | if (m_DataLen <= headerLen) |
235 | 0 | return; |
236 | | |
237 | 0 | constructNextLayer<PayloadLayer>(m_Data + headerLen, m_DataLen - headerLen); |
238 | 0 | } |
239 | | |
240 | | const FieldDescriptor GvcpLayer::SerializedFields::Command{ Layer::SerializedFields::MaxID + 1, "command" }; |
241 | | const FieldDescriptor GvcpLayer::SerializedFields::CommandName{ Layer::SerializedFields::MaxID + 2, "commandName" }; |
242 | | |
243 | | void GvcpLayer::serializeLayer(ObjectScope& serializer) const |
244 | 0 | { |
245 | 0 | serializer.writeField(SerializedFields::Command, getRawCommand(m_Data)); |
246 | 0 | serializer.writeField(SerializedFields::CommandName, commandToString(getCommand())); |
247 | 0 | } |
248 | | |
249 | | // -------- Class GvcpRequestLayer ----------------- |
250 | | |
251 | | GvcpRequestLayer::GvcpRequestLayer(GvcpCommand command, GvcpFlag flag, uint16_t requestId, size_t bodyLen) |
252 | 0 | { |
253 | 0 | if (isAcknowledgeValue(command)) |
254 | 0 | throw std::invalid_argument("A GVCP request can't be created with an acknowledge value"); |
255 | | |
256 | 0 | initLayer(sizeof(gvcp_request_header) + bodyLen); |
257 | |
|
258 | 0 | auto* header = getGvcpHeader(); |
259 | 0 | header->magicNumber = gvcpMagicNumber; |
260 | 0 | header->flag = flag; |
261 | 0 | header->command = hostToNet16(static_cast<uint16_t>(command)); |
262 | 0 | header->dataSize = hostToNet16(static_cast<uint16_t>(bodyLen)); |
263 | 0 | header->requestId = hostToNet16(requestId); |
264 | 0 | } |
265 | | |
266 | | bool GvcpRequestLayer::isDataValid(const uint8_t* data, size_t dataLen) |
267 | 0 | { |
268 | 0 | return canReinterpretAs<gvcp_request_header>(data, dataLen) && data[0] == gvcpMagicNumber; |
269 | 0 | } |
270 | | |
271 | | GvcpFlag GvcpRequestLayer::getFlag() const |
272 | 0 | { |
273 | 0 | return getGvcpHeader()->flag; |
274 | 0 | } |
275 | | |
276 | | void GvcpRequestLayer::setFlag(GvcpFlag flag) |
277 | 0 | { |
278 | 0 | getGvcpHeader()->flag = flag; |
279 | 0 | } |
280 | | |
281 | | bool GvcpRequestLayer::hasAcknowledgeFlag() const |
282 | 0 | { |
283 | 0 | return (getGvcpHeader()->flag & gvcpAcknowledgeFlag) != 0; |
284 | 0 | } |
285 | | |
286 | | uint16_t GvcpRequestLayer::getRequestId() const |
287 | 0 | { |
288 | 0 | return netToHost16(getGvcpHeader()->requestId); |
289 | 0 | } |
290 | | |
291 | | void GvcpRequestLayer::setRequestId(uint16_t requestId) |
292 | 0 | { |
293 | 0 | getGvcpHeader()->requestId = hostToNet16(requestId); |
294 | 0 | } |
295 | | |
296 | | GvcpCommand GvcpRequestLayer::getCommand() const |
297 | 0 | { |
298 | 0 | return toGvcpCommand(netToHost16(getGvcpHeader()->command)); |
299 | 0 | } |
300 | | |
301 | | uint16_t GvcpRequestLayer::getDataSize() const |
302 | 0 | { |
303 | 0 | return netToHost16(getGvcpHeader()->dataSize); |
304 | 0 | } |
305 | | |
306 | | void GvcpRequestLayer::computeCalculateFields() |
307 | 0 | { |
308 | 0 | getGvcpHeader()->dataSize = hostToNet16(static_cast<uint16_t>(m_DataLen - sizeof(gvcp_request_header))); |
309 | 0 | } |
310 | | |
311 | | std::string GvcpRequestLayer::toString() const |
312 | 0 | { |
313 | 0 | std::ostringstream ss; |
314 | 0 | ss << "GVCP Request Layer, Command: " << getCommand() << ", Request ID: " << getRequestId(); |
315 | 0 | return ss.str(); |
316 | 0 | } |
317 | | |
318 | | const FieldDescriptor GvcpRequestLayer::SerializedFields::Flag{ GvcpLayer::SerializedFields::MaxID + 1, "flag" }; |
319 | | const FieldDescriptor GvcpRequestLayer::SerializedFields::AcknowledgeRequired{ |
320 | | GvcpLayer::SerializedFields::MaxID + 2, "acknowledgeRequired" |
321 | | }; |
322 | | const FieldDescriptor GvcpRequestLayer::SerializedFields::RequestId{ GvcpLayer::SerializedFields::MaxID + 3, |
323 | | "requestId" }; |
324 | | |
325 | | void GvcpRequestLayer::serializeLayer(ObjectScope& serializer) const |
326 | 0 | { |
327 | 0 | GvcpLayer::serializeLayer(serializer); |
328 | 0 | serializer.writeField(SerializedFields::Flag, getFlag()); |
329 | 0 | serializer.writeField(SerializedFields::AcknowledgeRequired, hasAcknowledgeFlag()); |
330 | 0 | serializer.writeField(SerializedFields::RequestId, getRequestId()); |
331 | 0 | } |
332 | | |
333 | | // -------- Class GvcpAcknowledgeLayer ----------------- |
334 | | |
335 | | GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, uint16_t ackId, |
336 | | size_t bodyLen) |
337 | 0 | { |
338 | 0 | if (!isAcknowledgeValue(command)) |
339 | 0 | throw std::invalid_argument("A GVCP acknowledge can't be created with a command value"); |
340 | | |
341 | 0 | initLayer(sizeof(gvcp_ack_header) + bodyLen); |
342 | |
|
343 | 0 | auto* header = getGvcpHeader(); |
344 | 0 | header->status = hostToNet16(static_cast<uint16_t>(status)); |
345 | 0 | header->command = hostToNet16(static_cast<uint16_t>(command)); |
346 | 0 | header->dataSize = hostToNet16(static_cast<uint16_t>(bodyLen)); |
347 | 0 | header->ackId = hostToNet16(ackId); |
348 | 0 | } |
349 | | |
350 | | bool GvcpAcknowledgeLayer::isDataValid(const uint8_t* data, size_t dataLen) |
351 | 0 | { |
352 | 0 | return canReinterpretAs<gvcp_ack_header>(data, dataLen); |
353 | 0 | } |
354 | | |
355 | | GvcpResponseStatus GvcpAcknowledgeLayer::getStatus() const |
356 | 0 | { |
357 | 0 | return toGvcpResponseStatus(netToHost16(getGvcpHeader()->status)); |
358 | 0 | } |
359 | | |
360 | | void GvcpAcknowledgeLayer::setStatus(GvcpResponseStatus status) |
361 | 0 | { |
362 | 0 | getGvcpHeader()->status = hostToNet16(static_cast<uint16_t>(status)); |
363 | 0 | } |
364 | | |
365 | | uint16_t GvcpAcknowledgeLayer::getAckId() const |
366 | 0 | { |
367 | 0 | return netToHost16(getGvcpHeader()->ackId); |
368 | 0 | } |
369 | | |
370 | | void GvcpAcknowledgeLayer::setAckId(uint16_t ackId) |
371 | 0 | { |
372 | 0 | getGvcpHeader()->ackId = hostToNet16(ackId); |
373 | 0 | } |
374 | | |
375 | | GvcpCommand GvcpAcknowledgeLayer::getCommand() const |
376 | 0 | { |
377 | 0 | return toGvcpCommand(netToHost16(getGvcpHeader()->command)); |
378 | 0 | } |
379 | | |
380 | | uint16_t GvcpAcknowledgeLayer::getDataSize() const |
381 | 0 | { |
382 | 0 | return netToHost16(getGvcpHeader()->dataSize); |
383 | 0 | } |
384 | | |
385 | | void GvcpAcknowledgeLayer::computeCalculateFields() |
386 | 0 | { |
387 | 0 | getGvcpHeader()->dataSize = hostToNet16(static_cast<uint16_t>(m_DataLen - sizeof(gvcp_ack_header))); |
388 | 0 | } |
389 | | |
390 | | std::string GvcpAcknowledgeLayer::toString() const |
391 | 0 | { |
392 | 0 | std::ostringstream ss; |
393 | 0 | ss << "GVCP Acknowledge Layer, Command: " << getCommand() << ", Acknowledge ID: " << getAckId() |
394 | 0 | << ", Status: " << getStatus(); |
395 | 0 | return ss.str(); |
396 | 0 | } |
397 | | |
398 | | const FieldDescriptor GvcpAcknowledgeLayer::SerializedFields::Status{ GvcpLayer::SerializedFields::MaxID + 1, |
399 | | "status" }; |
400 | | const FieldDescriptor GvcpAcknowledgeLayer::SerializedFields::StatusName{ GvcpLayer::SerializedFields::MaxID + 2, |
401 | | "statusName" }; |
402 | | const FieldDescriptor GvcpAcknowledgeLayer::SerializedFields::AckId{ GvcpLayer::SerializedFields::MaxID + 3, |
403 | | "ackId" }; |
404 | | |
405 | | void GvcpAcknowledgeLayer::serializeLayer(ObjectScope& serializer) const |
406 | 0 | { |
407 | 0 | GvcpLayer::serializeLayer(serializer); |
408 | 0 | serializer.writeField(SerializedFields::Status, netToHost16(getGvcpHeader()->status)); |
409 | 0 | serializer.writeField(SerializedFields::StatusName, statusToString(getStatus())); |
410 | 0 | serializer.writeField(SerializedFields::AckId, getAckId()); |
411 | 0 | } |
412 | | |
413 | | // -------- Class GvcpDiscoveryRequestLayer ----------------- |
414 | | |
415 | | GvcpDiscoveryRequestLayer::GvcpDiscoveryRequestLayer(bool allowBroadcastAck, bool acknowledgeRequired, |
416 | | uint16_t requestId) |
417 | 0 | : GvcpRequestLayer(GvcpCommand::DiscoveredCmd, |
418 | 0 | static_cast<GvcpFlag>((allowBroadcastAck ? gvcpAllowBroadcastAckFlag : 0) | |
419 | 0 | (acknowledgeRequired ? gvcpAcknowledgeFlag : 0)), |
420 | 0 | requestId) |
421 | 0 | {} |
422 | | |
423 | | bool GvcpDiscoveryRequestLayer::hasAllowBroadcastAckFlag() const |
424 | 0 | { |
425 | 0 | return (getFlag() & gvcpAllowBroadcastAckFlag) != 0; |
426 | 0 | } |
427 | | |
428 | | void GvcpDiscoveryRequestLayer::setAllowBroadcastAckFlag(bool allowBroadcastAck) |
429 | 0 | { |
430 | 0 | setFlag(static_cast<GvcpFlag>(allowBroadcastAck ? getFlag() | gvcpAllowBroadcastAckFlag |
431 | 0 | : getFlag() & ~gvcpAllowBroadcastAckFlag)); |
432 | 0 | } |
433 | | |
434 | | const FieldDescriptor GvcpDiscoveryRequestLayer::SerializedFields::AllowBroadcastAck{ |
435 | | GvcpRequestLayer::SerializedFields::MaxID + 1, "allowBroadcastAck" |
436 | | }; |
437 | | |
438 | | void GvcpDiscoveryRequestLayer::serializeLayer(ObjectScope& serializer) const |
439 | 0 | { |
440 | 0 | GvcpRequestLayer::serializeLayer(serializer); |
441 | 0 | serializer.writeField(SerializedFields::AllowBroadcastAck, hasAllowBroadcastAckFlag()); |
442 | 0 | } |
443 | | |
444 | | // -------- Class GvcpDiscoveryAcknowledgeLayer ----------------- |
445 | | |
446 | | GvcpDiscoveryAcknowledgeLayer::GvcpDiscoveryAcknowledgeLayer(GvcpResponseStatus status, uint16_t ackId) |
447 | 0 | : GvcpAcknowledgeLayer(status, GvcpCommand::DiscoveredAck, ackId, sizeof(gvcp_discovery_body)) |
448 | 0 | {} |
449 | | |
450 | | bool GvcpDiscoveryAcknowledgeLayer::isDataValid(const uint8_t* data, size_t dataLen) |
451 | 0 | { |
452 | 0 | return GvcpAcknowledgeLayer::isDataValid(data, dataLen) && |
453 | 0 | dataLen >= sizeof(gvcp_ack_header) + sizeof(gvcp_discovery_body); |
454 | 0 | } |
455 | | |
456 | | GvcpVersion GvcpDiscoveryAcknowledgeLayer::getVersion() const |
457 | 0 | { |
458 | 0 | auto* body = getGvcpDiscoveryBody(); |
459 | 0 | return { netToHost16(body->versionMajor), netToHost16(body->versionMinor) }; |
460 | 0 | } |
461 | | |
462 | | void GvcpDiscoveryAcknowledgeLayer::setVersion(const GvcpVersion& version) |
463 | 0 | { |
464 | 0 | auto* body = getGvcpDiscoveryBody(); |
465 | 0 | body->versionMajor = hostToNet16(version.major); |
466 | 0 | body->versionMinor = hostToNet16(version.minor); |
467 | 0 | } |
468 | | |
469 | | MacAddress GvcpDiscoveryAcknowledgeLayer::getMacAddress() const |
470 | 0 | { |
471 | 0 | return MacAddress(getGvcpDiscoveryBody()->macAddress); |
472 | 0 | } |
473 | | |
474 | | void GvcpDiscoveryAcknowledgeLayer::setMacAddress(const MacAddress& macAddress) |
475 | 0 | { |
476 | 0 | macAddress.copyTo(getGvcpDiscoveryBody()->macAddress, sizeof(gvcp_discovery_body::macAddress)); |
477 | 0 | } |
478 | | |
479 | | IPv4Address GvcpDiscoveryAcknowledgeLayer::getIpAddress() const |
480 | 0 | { |
481 | 0 | return { getGvcpDiscoveryBody()->ipAddress }; |
482 | 0 | } |
483 | | |
484 | | void GvcpDiscoveryAcknowledgeLayer::setIpAddress(const IPv4Address& ipAddress) |
485 | 0 | { |
486 | 0 | getGvcpDiscoveryBody()->ipAddress = ipAddress.toInt(); |
487 | 0 | } |
488 | | |
489 | | IPv4Address GvcpDiscoveryAcknowledgeLayer::getSubnetMask() const |
490 | 0 | { |
491 | 0 | return { getGvcpDiscoveryBody()->subnetMask }; |
492 | 0 | } |
493 | | |
494 | | void GvcpDiscoveryAcknowledgeLayer::setSubnetMask(const IPv4Address& subnetMask) |
495 | 0 | { |
496 | 0 | getGvcpDiscoveryBody()->subnetMask = subnetMask.toInt(); |
497 | 0 | } |
498 | | |
499 | | IPv4Address GvcpDiscoveryAcknowledgeLayer::getGatewayIpAddress() const |
500 | 0 | { |
501 | 0 | return { getGvcpDiscoveryBody()->defaultGateway }; |
502 | 0 | } |
503 | | |
504 | | void GvcpDiscoveryAcknowledgeLayer::setGatewayIpAddress(const IPv4Address& gatewayIpAddress) |
505 | 0 | { |
506 | 0 | getGvcpDiscoveryBody()->defaultGateway = gatewayIpAddress.toInt(); |
507 | 0 | } |
508 | | |
509 | | std::string GvcpDiscoveryAcknowledgeLayer::getManufacturerName() const |
510 | 0 | { |
511 | 0 | return fieldToString(getGvcpDiscoveryBody()->manufacturerName); |
512 | 0 | } |
513 | | |
514 | | void GvcpDiscoveryAcknowledgeLayer::setManufacturerName(const std::string& manufacturerName) |
515 | 0 | { |
516 | 0 | stringToField(manufacturerName, getGvcpDiscoveryBody()->manufacturerName); |
517 | 0 | } |
518 | | |
519 | | std::string GvcpDiscoveryAcknowledgeLayer::getModelName() const |
520 | 0 | { |
521 | 0 | return fieldToString(getGvcpDiscoveryBody()->modelName); |
522 | 0 | } |
523 | | |
524 | | void GvcpDiscoveryAcknowledgeLayer::setModelName(const std::string& modelName) |
525 | 0 | { |
526 | 0 | stringToField(modelName, getGvcpDiscoveryBody()->modelName); |
527 | 0 | } |
528 | | |
529 | | std::string GvcpDiscoveryAcknowledgeLayer::getDeviceVersion() const |
530 | 0 | { |
531 | 0 | return fieldToString(getGvcpDiscoveryBody()->deviceVersion); |
532 | 0 | } |
533 | | |
534 | | void GvcpDiscoveryAcknowledgeLayer::setDeviceVersion(const std::string& deviceVersion) |
535 | 0 | { |
536 | 0 | stringToField(deviceVersion, getGvcpDiscoveryBody()->deviceVersion); |
537 | 0 | } |
538 | | |
539 | | std::string GvcpDiscoveryAcknowledgeLayer::getManufacturerSpecificInformation() const |
540 | 0 | { |
541 | 0 | return fieldToString(getGvcpDiscoveryBody()->manufacturerSpecificInformation); |
542 | 0 | } |
543 | | |
544 | | void GvcpDiscoveryAcknowledgeLayer::setManufacturerSpecificInformation(const std::string& info) |
545 | 0 | { |
546 | 0 | stringToField(info, getGvcpDiscoveryBody()->manufacturerSpecificInformation); |
547 | 0 | } |
548 | | |
549 | | std::string GvcpDiscoveryAcknowledgeLayer::getSerialNumber() const |
550 | 0 | { |
551 | 0 | return fieldToString(getGvcpDiscoveryBody()->serialNumber); |
552 | 0 | } |
553 | | |
554 | | void GvcpDiscoveryAcknowledgeLayer::setSerialNumber(const std::string& serialNumber) |
555 | 0 | { |
556 | 0 | stringToField(serialNumber, getGvcpDiscoveryBody()->serialNumber); |
557 | 0 | } |
558 | | |
559 | | std::string GvcpDiscoveryAcknowledgeLayer::getUserDefinedName() const |
560 | 0 | { |
561 | 0 | return fieldToString(getGvcpDiscoveryBody()->userDefinedName); |
562 | 0 | } |
563 | | |
564 | | void GvcpDiscoveryAcknowledgeLayer::setUserDefinedName(const std::string& userDefinedName) |
565 | 0 | { |
566 | 0 | stringToField(userDefinedName, getGvcpDiscoveryBody()->userDefinedName); |
567 | 0 | } |
568 | | |
569 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::VersionMajor{ |
570 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 1, "versionMajor" |
571 | | }; |
572 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::VersionMinor{ |
573 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 2, "versionMinor" |
574 | | }; |
575 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::MacAddress{ |
576 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 3, "macAddress" |
577 | | }; |
578 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::IpAddress{ |
579 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 4, "ipAddress" |
580 | | }; |
581 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::SubnetMask{ |
582 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 5, "subnetMask" |
583 | | }; |
584 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::GatewayIpAddress{ |
585 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 6, "gatewayIpAddress" |
586 | | }; |
587 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::ManufacturerName{ |
588 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 7, "manufacturerName" |
589 | | }; |
590 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::ModelName{ |
591 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 8, "modelName" |
592 | | }; |
593 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::DeviceVersion{ |
594 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 9, "deviceVersion" |
595 | | }; |
596 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::ManufacturerSpecificInformation{ |
597 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 10, "manufacturerSpecificInformation" |
598 | | }; |
599 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::SerialNumber{ |
600 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 11, "serialNumber" |
601 | | }; |
602 | | const FieldDescriptor GvcpDiscoveryAcknowledgeLayer::SerializedFields::UserDefinedName{ |
603 | | GvcpAcknowledgeLayer::SerializedFields::MaxID + 12, "userDefinedName" |
604 | | }; |
605 | | |
606 | | void GvcpDiscoveryAcknowledgeLayer::serializeLayer(ObjectScope& serializer) const |
607 | 0 | { |
608 | 0 | GvcpAcknowledgeLayer::serializeLayer(serializer); |
609 | 0 | const GvcpVersion version = getVersion(); |
610 | 0 | serializer.writeField(SerializedFields::VersionMajor, version.major); |
611 | 0 | serializer.writeField(SerializedFields::VersionMinor, version.minor); |
612 | 0 | serializer.writeField(SerializedFields::MacAddress, getMacAddress().toString()); |
613 | 0 | serializer.writeField(SerializedFields::IpAddress, getIpAddress().toString()); |
614 | 0 | serializer.writeField(SerializedFields::SubnetMask, getSubnetMask().toString()); |
615 | 0 | serializer.writeField(SerializedFields::GatewayIpAddress, getGatewayIpAddress().toString()); |
616 | 0 | serializer.writeField(SerializedFields::ManufacturerName, getManufacturerName()); |
617 | 0 | serializer.writeField(SerializedFields::ModelName, getModelName()); |
618 | 0 | serializer.writeField(SerializedFields::DeviceVersion, getDeviceVersion()); |
619 | 0 | serializer.writeField(SerializedFields::ManufacturerSpecificInformation, getManufacturerSpecificInformation()); |
620 | 0 | serializer.writeField(SerializedFields::SerialNumber, getSerialNumber()); |
621 | 0 | serializer.writeField(SerializedFields::UserDefinedName, getUserDefinedName()); |
622 | 0 | } |
623 | | |
624 | | // -------- Class GvcpForceIpRequestLayer ----------------- |
625 | | |
626 | | GvcpForceIpRequestLayer::GvcpForceIpRequestLayer(const MacAddress& macAddress, const IPv4Address& ipAddress, |
627 | | const IPv4Address& subnetMask, const IPv4Address& gatewayIpAddress, |
628 | | GvcpFlag flag, uint16_t requestId) |
629 | 0 | : GvcpRequestLayer(GvcpCommand::ForceIpCmd, flag, requestId, sizeof(gvcp_forceip_body)) |
630 | 0 | { |
631 | 0 | setMacAddress(macAddress); |
632 | 0 | setIpAddress(ipAddress); |
633 | 0 | setSubnetMask(subnetMask); |
634 | 0 | setGatewayIpAddress(gatewayIpAddress); |
635 | 0 | } |
636 | | |
637 | | bool GvcpForceIpRequestLayer::isDataValid(const uint8_t* data, size_t dataLen) |
638 | 0 | { |
639 | 0 | return GvcpRequestLayer::isDataValid(data, dataLen) && |
640 | 0 | dataLen >= sizeof(gvcp_request_header) + sizeof(gvcp_forceip_body); |
641 | 0 | } |
642 | | |
643 | | MacAddress GvcpForceIpRequestLayer::getMacAddress() const |
644 | 0 | { |
645 | 0 | return MacAddress(getGvcpForceIpBody()->macAddress); |
646 | 0 | } |
647 | | |
648 | | void GvcpForceIpRequestLayer::setMacAddress(const MacAddress& macAddress) |
649 | 0 | { |
650 | 0 | macAddress.copyTo(getGvcpForceIpBody()->macAddress, sizeof(gvcp_forceip_body::macAddress)); |
651 | 0 | } |
652 | | |
653 | | IPv4Address GvcpForceIpRequestLayer::getIpAddress() const |
654 | 0 | { |
655 | 0 | return { getGvcpForceIpBody()->ipAddress }; |
656 | 0 | } |
657 | | |
658 | | void GvcpForceIpRequestLayer::setIpAddress(const IPv4Address& ipAddress) |
659 | 0 | { |
660 | 0 | getGvcpForceIpBody()->ipAddress = ipAddress.toInt(); |
661 | 0 | } |
662 | | |
663 | | IPv4Address GvcpForceIpRequestLayer::getSubnetMask() const |
664 | 0 | { |
665 | 0 | return { getGvcpForceIpBody()->subnetMask }; |
666 | 0 | } |
667 | | |
668 | | void GvcpForceIpRequestLayer::setSubnetMask(const IPv4Address& subnetMask) |
669 | 0 | { |
670 | 0 | getGvcpForceIpBody()->subnetMask = subnetMask.toInt(); |
671 | 0 | } |
672 | | |
673 | | IPv4Address GvcpForceIpRequestLayer::getGatewayIpAddress() const |
674 | 0 | { |
675 | 0 | return { getGvcpForceIpBody()->gateway }; |
676 | 0 | } |
677 | | |
678 | | void GvcpForceIpRequestLayer::setGatewayIpAddress(const IPv4Address& gatewayIpAddress) |
679 | 0 | { |
680 | 0 | getGvcpForceIpBody()->gateway = gatewayIpAddress.toInt(); |
681 | 0 | } |
682 | | const FieldDescriptor GvcpForceIpRequestLayer::SerializedFields::MacAddress{ |
683 | | GvcpRequestLayer::SerializedFields::MaxID + 1, "macAddress" |
684 | | }; |
685 | | const FieldDescriptor GvcpForceIpRequestLayer::SerializedFields::IpAddress{ |
686 | | GvcpRequestLayer::SerializedFields::MaxID + 2, "ipAddress" |
687 | | }; |
688 | | const FieldDescriptor GvcpForceIpRequestLayer::SerializedFields::SubnetMask{ |
689 | | GvcpRequestLayer::SerializedFields::MaxID + 3, "subnetMask" |
690 | | }; |
691 | | const FieldDescriptor GvcpForceIpRequestLayer::SerializedFields::GatewayIpAddress{ |
692 | | GvcpRequestLayer::SerializedFields::MaxID + 4, "gatewayIpAddress" |
693 | | }; |
694 | | |
695 | | void GvcpForceIpRequestLayer::serializeLayer(ObjectScope& serializer) const |
696 | 0 | { |
697 | 0 | GvcpRequestLayer::serializeLayer(serializer); |
698 | 0 | serializer.writeField(SerializedFields::MacAddress, getMacAddress().toString()); |
699 | 0 | serializer.writeField(SerializedFields::IpAddress, getIpAddress().toString()); |
700 | 0 | serializer.writeField(SerializedFields::SubnetMask, getSubnetMask().toString()); |
701 | 0 | serializer.writeField(SerializedFields::GatewayIpAddress, getGatewayIpAddress().toString()); |
702 | 0 | } |
703 | | |
704 | | } // namespace pcpp |