/src/PcapPlusPlus/Packet++/src/CotpLayer.cpp
Line | Count | Source |
1 | | #include "../header/CotpLayer.h" |
2 | | #include "S7CommLayer.h" |
3 | | #include <PayloadLayer.h> |
4 | | |
5 | | #include <cstring> |
6 | | |
7 | | namespace pcpp |
8 | | { |
9 | | |
10 | | pcpp::CotpLayer::CotpLayer(uint8_t tpduNumber) |
11 | 0 | { |
12 | 0 | const size_t headerLen = sizeof(cotphdr); |
13 | 0 | allocData(headerLen); |
14 | 0 | cotphdr* cotpHdr = getCotpHeader(); |
15 | 0 | cotpHdr->length = 0x02; |
16 | 0 | cotpHdr->pduType = 0x0f; |
17 | 0 | cotpHdr->tpduNumber = tpduNumber; |
18 | 0 | m_Protocol = COTP; |
19 | 0 | } |
20 | | |
21 | | std::string CotpLayer::toString() const |
22 | 0 | { |
23 | 0 | return "Cotp Layer"; |
24 | 0 | } |
25 | | |
26 | | uint8_t CotpLayer::getLength() const |
27 | 0 | { |
28 | 0 | return getCotpHeader()->length; |
29 | 0 | } |
30 | | |
31 | | uint8_t CotpLayer::getPduType() const |
32 | 0 | { |
33 | 0 | return getCotpHeader()->pduType; |
34 | 0 | } |
35 | | |
36 | | uint8_t CotpLayer::getTpduNumber() const |
37 | 0 | { |
38 | 0 | return getCotpHeader()->tpduNumber; |
39 | 0 | } |
40 | | |
41 | | void CotpLayer::setLength(uint8_t length) const |
42 | 0 | { |
43 | 0 | getCotpHeader()->length = length; |
44 | 0 | } |
45 | | |
46 | | void CotpLayer::setPduType(uint8_t pduType) const |
47 | 0 | { |
48 | 0 | getCotpHeader()->pduType = pduType; |
49 | 0 | } |
50 | | |
51 | | void CotpLayer::setTpduNumber(uint8_t tpduNumber) const |
52 | 0 | { |
53 | 0 | getCotpHeader()->tpduNumber = tpduNumber; |
54 | 0 | } |
55 | | |
56 | | bool CotpLayer::isDataValid(const uint8_t* data, size_t dataSize) |
57 | 0 | { |
58 | 0 | if (!data || dataSize < sizeof(cotphdr)) |
59 | 0 | return false; |
60 | | |
61 | 0 | return data[1] == 0xf0 && data[0] == 2; |
62 | 0 | } |
63 | | |
64 | | void CotpLayer::parseNextLayer() |
65 | 0 | { |
66 | 0 | size_t headerLen = getHeaderLen(); |
67 | 0 | if (m_DataLen <= headerLen) |
68 | 0 | return; |
69 | | |
70 | 0 | uint8_t* payload = m_Data + headerLen; |
71 | 0 | size_t payloadLen = m_DataLen - headerLen; |
72 | |
|
73 | 0 | tryConstructNextLayerWithFallback<S7CommLayer, PayloadLayer>(payload, payloadLen); |
74 | 0 | } |
75 | | |
76 | | const FieldDescriptor CotpLayer::SerializedFields::PduType{ Layer::SerializedFields::MaxID + 1, "pduType" }; |
77 | | const FieldDescriptor CotpLayer::SerializedFields::TpduNumber{ Layer::SerializedFields::MaxID + 2, "tpduNumber" }; |
78 | | |
79 | | void CotpLayer::serializeLayer(ObjectScope& serializer) const |
80 | 0 | { |
81 | 0 | serializer.writeField(SerializedFields::PduType, getPduType()); |
82 | 0 | serializer.writeField(SerializedFields::TpduNumber, getTpduNumber()); |
83 | 0 | } |
84 | | } // namespace pcpp |