1 | #include "Dissector.h" |
---|
2 | #include "Logger/Logger.h" |
---|
3 | |
---|
4 | Dissector::Dissector() : |
---|
5 | m_pPayload(0), |
---|
6 | m_nPayloadSize(0), |
---|
7 | m_pLayerInfo(0), |
---|
8 | m_pUnprocessedData(0), |
---|
9 | m_nUnprocessedSize(0), |
---|
10 | m_pProtocolInfo(0) |
---|
11 | { |
---|
12 | } |
---|
13 | |
---|
14 | Dissector::~Dissector() |
---|
15 | { |
---|
16 | delete[] m_pPayload; |
---|
17 | delete[] m_pUnprocessedData; |
---|
18 | delete m_pLayerInfo; |
---|
19 | delete m_pProtocolInfo; |
---|
20 | } |
---|
21 | |
---|
22 | bool Dissector::IsPacketValid() const |
---|
23 | { |
---|
24 | return true; |
---|
25 | } |
---|
26 | |
---|
27 | bool Dissector::FinishDissection() |
---|
28 | { |
---|
29 | return false; |
---|
30 | } |
---|
31 | |
---|
32 | bool Dissector::Reassemble(Dissector * pDissector, ProtocolInfo * pProtocolInfo) |
---|
33 | { |
---|
34 | Logger::Instance().LogError("Dissector does not support reassembly"); |
---|
35 | return false; |
---|
36 | } |
---|
37 | |
---|
38 | void Dissector::SaveResult(DispatchInfo & info) const |
---|
39 | { |
---|
40 | info.SetData(m_pPayload, m_nPayloadSize); |
---|
41 | if (m_pLayerInfo) |
---|
42 | info.AddProtocolInfo(m_pLayerInfo->Clone()); |
---|
43 | } |
---|
44 | |
---|
45 | void Dissector::SavePayload(const unsigned char * pData, ssize_t nPayloadSize) |
---|
46 | { |
---|
47 | AllocPayload(nPayloadSize); |
---|
48 | if (nPayloadSize > 0) |
---|
49 | memcpy(m_pPayload, pData, m_nPayloadSize); |
---|
50 | } |
---|
51 | |
---|
52 | void Dissector::AllocPayload(ssize_t nPayloadSize) |
---|
53 | { |
---|
54 | delete[] m_pPayload; |
---|
55 | if (nPayloadSize > 0) |
---|
56 | { |
---|
57 | m_nPayloadSize = nPayloadSize; |
---|
58 | m_pPayload = new unsigned char[nPayloadSize]; |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | m_nPayloadSize = 0; |
---|
63 | m_pPayload = 0; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | void Dissector::SetPayloadAt(const unsigned char * pSrcData, ssize_t nSrcSize, int nOffset) |
---|
68 | { |
---|
69 | memcpy(&m_pPayload[nOffset], pSrcData, nSrcSize); |
---|
70 | } |
---|
71 | |
---|
72 | void Dissector::AppendPayload(Dissector * pSrc) |
---|
73 | { |
---|
74 | int nTgtSize = m_nPayloadSize + pSrc->m_nPayloadSize; |
---|
75 | unsigned char * pTmp = 0; |
---|
76 | if (nTgtSize) |
---|
77 | { |
---|
78 | pTmp = new unsigned char [m_nPayloadSize + pSrc->m_nPayloadSize]; |
---|
79 | if (m_nPayloadSize) |
---|
80 | memcpy(pTmp, m_pPayload, m_nPayloadSize); |
---|
81 | if (pSrc->m_nPayloadSize) |
---|
82 | memcpy(&pTmp[m_nPayloadSize], pSrc->m_pPayload, pSrc->m_nPayloadSize); |
---|
83 | } |
---|
84 | delete m_pPayload; |
---|
85 | m_nPayloadSize = nTgtSize; |
---|
86 | m_pPayload = pTmp; |
---|
87 | } |
---|
88 | unsigned char * Dissector::GetPayload() const |
---|
89 | { |
---|
90 | return m_pPayload; |
---|
91 | } |
---|
92 | |
---|
93 | ssize_t Dissector::GetPayloadSize() const |
---|
94 | { |
---|
95 | return m_nPayloadSize; |
---|
96 | } |
---|
97 | |
---|
98 | std::pair<unsigned char *, ssize_t> Dissector::DetachData() |
---|
99 | { |
---|
100 | std::pair<unsigned char *, ssize_t> res(m_pPayload, m_nPayloadSize); |
---|
101 | m_pPayload = 0; |
---|
102 | m_nPayloadSize = 0; |
---|
103 | return res; |
---|
104 | } |
---|
105 | |
---|
106 | void Dissector::SaveLayerInfo() |
---|
107 | { |
---|
108 | delete m_pLayerInfo; |
---|
109 | m_pLayerInfo = CreateLayerInfo(); |
---|
110 | } |
---|
111 | |
---|
112 | ProtocolInfoElement * Dissector::GetLayerInfo() const |
---|
113 | { |
---|
114 | return m_pLayerInfo; |
---|
115 | } |
---|
116 | |
---|
117 | void Dissector::SeparateUnprocessedData(int nNewPayloadSize) |
---|
118 | { |
---|
119 | if (nNewPayloadSize >= m_nPayloadSize) |
---|
120 | return; |
---|
121 | delete [] m_pUnprocessedData; |
---|
122 | m_nUnprocessedSize = m_nPayloadSize - nNewPayloadSize; |
---|
123 | m_pUnprocessedData = new unsigned char[m_nUnprocessedSize]; |
---|
124 | memcpy(m_pUnprocessedData, &m_pPayload[nNewPayloadSize], m_nUnprocessedSize); |
---|
125 | unsigned char * pTmp = 0; |
---|
126 | if (nNewPayloadSize != 0) |
---|
127 | { |
---|
128 | pTmp = new unsigned char[nNewPayloadSize]; |
---|
129 | memcpy(pTmp, m_pPayload, nNewPayloadSize); |
---|
130 | } |
---|
131 | delete [] m_pPayload; |
---|
132 | m_pPayload = pTmp; |
---|
133 | m_nPayloadSize = nNewPayloadSize; |
---|
134 | } |
---|
135 | |
---|
136 | bool Dissector::CreatesUnprocessedData() const |
---|
137 | { |
---|
138 | return false; |
---|
139 | } |
---|
140 | |
---|
141 | bool Dissector::ContainsUnprocessedData() |
---|
142 | { |
---|
143 | return m_nUnprocessedSize > 0; |
---|
144 | } |
---|
145 | |
---|
146 | std::pair<const unsigned char *, ssize_t> Dissector::GetUnprocessedData() const |
---|
147 | { |
---|
148 | std::pair<unsigned char *, ssize_t> res(m_pUnprocessedData, m_nUnprocessedSize); |
---|
149 | return res; |
---|
150 | } |
---|
151 | |
---|
152 | ProtocolInfo * Dissector::GetProtocolInfo() const |
---|
153 | { |
---|
154 | return m_pProtocolInfo; |
---|
155 | } |
---|
156 | void Dissector::SetProtocolInfo(ProtocolInfo * pInfo) |
---|
157 | { |
---|
158 | delete m_pProtocolInfo; |
---|
159 | m_pProtocolInfo = pInfo; |
---|
160 | } |
---|
161 | |
---|
162 | bool Dissector::MatchProtocolInfo(ProtocolInfo * pInfo) const |
---|
163 | { |
---|
164 | if (!m_pProtocolInfo || !pInfo) |
---|
165 | return !m_pProtocolInfo && !pInfo; |
---|
166 | return m_pProtocolInfo->Match(*pInfo); |
---|
167 | } |
---|