/src/PcapPlusPlus/Packet++/src/CotpLayer.cpp
Line | Count | Source (jump to first uncovered line) |
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 | m_DataLen = headerLen; |
14 | 0 | m_Data = new uint8_t[headerLen]; |
15 | 0 | memset(m_Data, 0, headerLen); |
16 | 0 | cotphdr* cotpHdr = getCotpHeader(); |
17 | 0 | cotpHdr->length = 0x02; |
18 | 0 | cotpHdr->pduType = 0x0f; |
19 | 0 | cotpHdr->tpduNumber = tpduNumber; |
20 | 0 | m_Protocol = COTP; |
21 | 0 | } |
22 | | |
23 | | std::string CotpLayer::toString() const |
24 | 0 | { |
25 | 0 | return "Cotp Layer"; |
26 | 0 | } |
27 | | |
28 | | uint8_t CotpLayer::getLength() const |
29 | 0 | { |
30 | 0 | return getCotpHeader()->length; |
31 | 0 | } |
32 | | |
33 | | uint8_t CotpLayer::getPduType() const |
34 | 0 | { |
35 | 0 | return getCotpHeader()->pduType; |
36 | 0 | } |
37 | | |
38 | | uint8_t CotpLayer::getTpduNumber() const |
39 | 0 | { |
40 | 0 | return getCotpHeader()->tpduNumber; |
41 | 0 | } |
42 | | |
43 | | void CotpLayer::setLength(uint8_t length) const |
44 | 0 | { |
45 | 0 | getCotpHeader()->length = length; |
46 | 0 | } |
47 | | |
48 | | void CotpLayer::setPduType(uint8_t pduType) const |
49 | 0 | { |
50 | 0 | getCotpHeader()->pduType = pduType; |
51 | 0 | } |
52 | | |
53 | | void CotpLayer::setTpduNumber(uint8_t tpduNumber) const |
54 | 0 | { |
55 | 0 | getCotpHeader()->tpduNumber = tpduNumber; |
56 | 0 | } |
57 | | |
58 | | bool CotpLayer::isDataValid(const uint8_t* data, size_t dataSize) |
59 | 1.44k | { |
60 | 1.44k | if (!data || dataSize < sizeof(cotphdr)) |
61 | 0 | return false; |
62 | | |
63 | 1.44k | return data[1] == 0xf0 && data[0] == 2; |
64 | 1.44k | } |
65 | | |
66 | | void CotpLayer::parseNextLayer() |
67 | 0 | { |
68 | 0 | size_t headerLen = getHeaderLen(); |
69 | 0 | if (m_DataLen <= headerLen) |
70 | 0 | return; |
71 | | |
72 | 0 | uint8_t* payload = m_Data + headerLen; |
73 | 0 | size_t payloadLen = m_DataLen - headerLen; |
74 | |
|
75 | 0 | if (S7CommLayer::isDataValid(payload, payloadLen)) |
76 | 0 | m_NextLayer = new S7CommLayer(payload, payloadLen, this, m_Packet); |
77 | 0 | else |
78 | 0 | m_NextLayer = new PayloadLayer(payload, payloadLen, this, m_Packet); |
79 | 0 | } |
80 | | } // namespace pcpp |