/src/PcapPlusPlus/Packet++/src/PPPoELayer.cpp
Line | Count | Source |
1 | 0 | #define LOG_MODULE PacketLogModulePPPoELayer |
2 | | |
3 | | #include "PPPoELayer.h" |
4 | | #include "IPv4Layer.h" |
5 | | #include "IPv6Layer.h" |
6 | | #include "PayloadLayer.h" |
7 | | #include "Logger.h" |
8 | | #include <unordered_map> |
9 | | #include <sstream> |
10 | | #include "EndianPortable.h" |
11 | | |
12 | | namespace pcpp |
13 | | { |
14 | | |
15 | | /// PPPoELayer |
16 | | /// ~~~~~~~~~~ |
17 | | |
18 | | PPPoELayer::PPPoELayer(uint8_t version, uint8_t type, PPPoELayer::PPPoECode code, uint16_t sessionId, |
19 | | size_t additionalBytesToAllocate) |
20 | 0 | { |
21 | 0 | const size_t dataLen = sizeof(pppoe_header) + additionalBytesToAllocate; |
22 | 0 | m_DataLen = dataLen; |
23 | 0 | m_Data = new uint8_t[dataLen]; |
24 | 0 | memset(m_Data, 0, dataLen); |
25 | |
|
26 | 0 | pppoe_header* pppoeHdr = getPPPoEHeader(); |
27 | 0 | pppoeHdr->version = (version & 0xf); |
28 | 0 | pppoeHdr->type = (type & 0x0f); |
29 | 0 | pppoeHdr->code = code; |
30 | 0 | pppoeHdr->sessionId = htobe16(sessionId); |
31 | 0 | pppoeHdr->payloadLength = 0; |
32 | 0 | } |
33 | | |
34 | | void PPPoELayer::computeCalculateFields() |
35 | 2.55k | { |
36 | 2.55k | pppoe_header* pppoeHdr = getPPPoEHeader(); |
37 | 2.55k | pppoeHdr->payloadLength = htobe16(m_DataLen - sizeof(pppoe_header)); |
38 | 2.55k | } |
39 | | |
40 | | /// PPPoESessionLayer |
41 | | /// ~~~~~~~~~~~~~~~~~ |
42 | | |
43 | | void PPPoESessionLayer::parseNextLayer() |
44 | 17.9k | { |
45 | 17.9k | size_t headerLen = getHeaderLen(); |
46 | 17.9k | if (m_DataLen <= headerLen) |
47 | 7 | return; |
48 | | |
49 | 17.9k | uint8_t* payload = m_Data + headerLen; |
50 | 17.9k | size_t payloadLen = m_DataLen - headerLen; |
51 | | |
52 | 17.9k | switch (getPPPNextProtocol()) |
53 | 17.9k | { |
54 | 15.7k | case PCPP_PPP_IP: |
55 | 15.7k | tryConstructNextLayerWithFallback<IPv4Layer, PayloadLayer>(payload, payloadLen); |
56 | 15.7k | break; |
57 | 833 | case PCPP_PPP_IPV6: |
58 | 833 | tryConstructNextLayerWithFallback<IPv6Layer, PayloadLayer>(payload, payloadLen); |
59 | 833 | break; |
60 | 1.38k | default: |
61 | 1.38k | constructNextLayer<PayloadLayer>(payload, payloadLen); |
62 | 1.38k | break; |
63 | 17.9k | } |
64 | 17.9k | } |
65 | | |
66 | | uint16_t PPPoESessionLayer::getPPPNextProtocol() const |
67 | 23.0k | { |
68 | 23.0k | if (m_DataLen < getHeaderLen()) |
69 | 0 | { |
70 | 0 | PCPP_LOG_ERROR("ERROR: size of layer is smaller then PPPoE session header"); |
71 | 0 | return 0; |
72 | 0 | } |
73 | | |
74 | 23.0k | uint16_t pppNextProto = *reinterpret_cast<uint16_t*>(m_Data + sizeof(pppoe_header)); |
75 | 23.0k | return be16toh(pppNextProto); |
76 | 23.0k | } |
77 | | |
78 | | void PPPoESessionLayer::setPPPNextProtocol(uint16_t nextProtocol) |
79 | 0 | { |
80 | 0 | if (m_DataLen < getHeaderLen()) |
81 | 0 | { |
82 | 0 | PCPP_LOG_ERROR("ERROR: size of layer is smaller then PPPoE session header"); |
83 | 0 | return; |
84 | 0 | } |
85 | | |
86 | 0 | uint16_t* pppProto = reinterpret_cast<uint16_t*>(m_Data + sizeof(pppoe_header)); |
87 | 0 | *pppProto = htobe16(nextProtocol); |
88 | 0 | } |
89 | | |
90 | | std::unordered_map<uint16_t, std::string> createPPPNextProtoToStringMap() |
91 | 2 | { |
92 | 2 | std::unordered_map<uint16_t, std::string> tempMap; |
93 | 2 | tempMap[PCPP_PPP_PADDING] = "Padding Protocol"; |
94 | 2 | tempMap[PCPP_PPP_ROHC_SCID] = "ROHC small-CID"; |
95 | 2 | tempMap[PCPP_PPP_ROHC_LCID] = "ROHC large-CID"; |
96 | 2 | tempMap[PCPP_PPP_IP] = "Internet Protocol version 4"; |
97 | 2 | tempMap[PCPP_PPP_OSI] = "OSI Network Layer"; |
98 | 2 | tempMap[PCPP_PPP_XNSIDP] = "Xerox NS IDP"; |
99 | 2 | tempMap[PCPP_PPP_DEC4] = "DECnet Phase IV"; |
100 | 2 | tempMap[PCPP_PPP_AT] = "Appletalk"; |
101 | 2 | tempMap[PCPP_PPP_IPX] = "Novell IPX"; |
102 | 2 | tempMap[PCPP_PPP_VJC_COMP] = "Van Jacobson Compressed TCP/IP"; |
103 | 2 | tempMap[PCPP_PPP_VJC_UNCOMP] = "Van Jacobson Uncompressed TCP/IP"; |
104 | 2 | tempMap[PCPP_PPP_BCP] = "Bridging PDU"; |
105 | 2 | tempMap[PCPP_PPP_ST] = "Stream Protocol (ST-II)"; |
106 | 2 | tempMap[PCPP_PPP_VINES] = "Banyan Vines"; |
107 | 2 | tempMap[PCPP_PPP_AT_EDDP] = "AppleTalk EDDP"; |
108 | 2 | tempMap[PCPP_PPP_AT_SB] = "AppleTalk SmartBuffered"; |
109 | 2 | tempMap[PCPP_PPP_MP] = "Multi-Link"; |
110 | 2 | tempMap[PCPP_PPP_NB] = "NETBIOS Framing"; |
111 | 2 | tempMap[PCPP_PPP_CISCO] = "Cisco Systems"; |
112 | 2 | tempMap[PCPP_PPP_ASCOM] = "Ascom Timeplex"; |
113 | 2 | tempMap[PCPP_PPP_LBLB] = "Fujitsu Link Backup and Load Balancing (LBLB)"; |
114 | 2 | tempMap[PCPP_PPP_RL] = "DCA Remote Lan"; |
115 | 2 | tempMap[PCPP_PPP_SDTP] = "Serial Data Transport Protocol (PPP-SDTP)"; |
116 | 2 | tempMap[PCPP_PPP_LLC] = "SNA over 802.2"; |
117 | 2 | tempMap[PCPP_PPP_SNA] = "SNA"; |
118 | 2 | tempMap[PCPP_PPP_IPV6HC] = "IPv6 Header Compression "; |
119 | 2 | tempMap[PCPP_PPP_KNX] = "KNX Bridging Data"; |
120 | 2 | tempMap[PCPP_PPP_ENCRYPT] = "Encryption"; |
121 | 2 | tempMap[PCPP_PPP_ILE] = "Individual Link Encryption"; |
122 | 2 | tempMap[PCPP_PPP_IPV6] = "Internet Protocol version 6"; |
123 | 2 | tempMap[PCPP_PPP_MUX] = "PPP Muxing"; |
124 | 2 | tempMap[PCPP_PPP_VSNP] = "Vendor-Specific Network Protocol (VSNP)"; |
125 | 2 | tempMap[PCPP_PPP_TNP] = "TRILL Network Protocol (TNP)"; |
126 | 2 | tempMap[PCPP_PPP_RTP_FH] = "RTP IPHC Full Header"; |
127 | 2 | tempMap[PCPP_PPP_RTP_CTCP] = "RTP IPHC Compressed TCP"; |
128 | 2 | tempMap[PCPP_PPP_RTP_CNTCP] = "RTP IPHC Compressed Non TCP"; |
129 | 2 | tempMap[PCPP_PPP_RTP_CUDP8] = "RTP IPHC Compressed UDP 8"; |
130 | 2 | tempMap[PCPP_PPP_RTP_CRTP8] = "RTP IPHC Compressed RTP 8"; |
131 | 2 | tempMap[PCPP_PPP_STAMPEDE] = "Stampede Bridging"; |
132 | 2 | tempMap[PCPP_PPP_MPPLUS] = "MP+ Protocol"; |
133 | 2 | tempMap[PCPP_PPP_NTCITS_IPI] = "NTCITS IPI"; |
134 | 2 | tempMap[PCPP_PPP_ML_SLCOMP] = "Single link compression in multilink"; |
135 | 2 | tempMap[PCPP_PPP_COMP] = "Compressed datagram"; |
136 | 2 | tempMap[PCPP_PPP_STP_HELLO] = "802.1d Hello Packets"; |
137 | 2 | tempMap[PCPP_PPP_IBM_SR] = "IBM Source Routing BPDU"; |
138 | 2 | tempMap[PCPP_PPP_DEC_LB] = "DEC LANBridge100 Spanning Tree"; |
139 | 2 | tempMap[PCPP_PPP_CDP] = "Cisco Discovery Protocol"; |
140 | 2 | tempMap[PCPP_PPP_NETCS] = "Netcs Twin Routing"; |
141 | 2 | tempMap[PCPP_PPP_STP] = "STP - Scheduled Transfer Protocol"; |
142 | 2 | tempMap[PCPP_PPP_EDP] = "EDP - Extreme Discovery Protocol"; |
143 | 2 | tempMap[PCPP_PPP_OSCP] = "Optical Supervisory Channel Protocol (OSCP)"; |
144 | 2 | tempMap[PCPP_PPP_OSCP2] = "Optical Supervisory Channel Protocol (OSCP)"; |
145 | 2 | tempMap[PCPP_PPP_LUXCOM] = "Luxcom"; |
146 | 2 | tempMap[PCPP_PPP_SIGMA] = "Sigma Network Systems"; |
147 | 2 | tempMap[PCPP_PPP_ACSP] = "Apple Client Server Protocol"; |
148 | 2 | tempMap[PCPP_PPP_MPLS_UNI] = "MPLS Unicast"; |
149 | 2 | tempMap[PCPP_PPP_MPLS_MULTI] = "MPLS Multicast"; |
150 | 2 | tempMap[PCPP_PPP_P12844] = "IEEE p1284.4 standard - data packets"; |
151 | 2 | tempMap[PCPP_PPP_TETRA] = "ETSI TETRA Network Protocol Type 1"; |
152 | 2 | tempMap[PCPP_PPP_MFTP] = "Multichannel Flow Treatment Protocol"; |
153 | 2 | tempMap[PCPP_PPP_RTP_CTCPND] = "RTP IPHC Compressed TCP No Delta"; |
154 | 2 | tempMap[PCPP_PPP_RTP_CS] = "RTP IPHC Context State"; |
155 | 2 | tempMap[PCPP_PPP_RTP_CUDP16] = "RTP IPHC Compressed UDP 16"; |
156 | 2 | tempMap[PCPP_PPP_RTP_CRDP16] = "RTP IPHC Compressed RTP 16"; |
157 | 2 | tempMap[PCPP_PPP_CCCP] = "Cray Communications Control Protocol"; |
158 | 2 | tempMap[PCPP_PPP_CDPD_MNRP] = "CDPD Mobile Network Registration Protocol"; |
159 | 2 | tempMap[PCPP_PPP_EXPANDAP] = "Expand accelerator protocol"; |
160 | 2 | tempMap[PCPP_PPP_ODSICP] = "ODSICP NCP"; |
161 | 2 | tempMap[PCPP_PPP_DOCSIS] = "DOCSIS DLL"; |
162 | 2 | tempMap[PCPP_PPP_CETACEANNDP] = "Cetacean Network Detection Protocol"; |
163 | 2 | tempMap[PCPP_PPP_LZS] = "Stacker LZS"; |
164 | 2 | tempMap[PCPP_PPP_REFTEK] = "RefTek Protocol"; |
165 | 2 | tempMap[PCPP_PPP_FC] = "Fibre Channel"; |
166 | 2 | tempMap[PCPP_PPP_EMIT] = "EMIT Protocols"; |
167 | 2 | tempMap[PCPP_PPP_VSP] = "Vendor-Specific Protocol (VSP)"; |
168 | 2 | tempMap[PCPP_PPP_TLSP] = "TRILL Link State Protocol (TLSP)"; |
169 | 2 | tempMap[PCPP_PPP_IPCP] = "Internet Protocol Control Protocol"; |
170 | 2 | tempMap[PCPP_PPP_OSINLCP] = "OSI Network Layer Control Protocol"; |
171 | 2 | tempMap[PCPP_PPP_XNSIDPCP] = "Xerox NS IDP Control Protocol"; |
172 | 2 | tempMap[PCPP_PPP_DECNETCP] = "DECnet Phase IV Control Protocol"; |
173 | 2 | tempMap[PCPP_PPP_ATCP] = "AppleTalk Control Protocol"; |
174 | 2 | tempMap[PCPP_PPP_IPXCP] = "Novell IPX Control Protocol"; |
175 | 2 | tempMap[PCPP_PPP_BRIDGENCP] = "Bridging NCP"; |
176 | 2 | tempMap[PCPP_PPP_SPCP] = "Stream Protocol Control Protocol"; |
177 | 2 | tempMap[PCPP_PPP_BVCP] = "Banyan Vines Control Protocol"; |
178 | 2 | tempMap[PCPP_PPP_MLCP] = "Multi-Link Control Protocol"; |
179 | 2 | tempMap[PCPP_PPP_NBCP] = "NETBIOS Framing Control Protocol"; |
180 | 2 | tempMap[PCPP_PPP_CISCOCP] = "Cisco Systems Control Protocol"; |
181 | 2 | tempMap[PCPP_PPP_ASCOMCP] = "Ascom Timeplex"; |
182 | 2 | tempMap[PCPP_PPP_LBLBCP] = "Fujitsu LBLB Control Protocol"; |
183 | 2 | tempMap[PCPP_PPP_RLNCP] = "DCA Remote Lan Network Control Protocol (RLNCP)"; |
184 | 2 | tempMap[PCPP_PPP_SDCP] = "Serial Data Control Protocol (PPP-SDCP)"; |
185 | 2 | tempMap[PCPP_PPP_LLCCP] = "SNA over 802.2 Control Protocol"; |
186 | 2 | tempMap[PCPP_PPP_SNACP] = "SNA Control Protocol"; |
187 | 2 | tempMap[PCPP_PPP_IP6HCCP] = "IP6 Header Compression Control Protocol"; |
188 | 2 | tempMap[PCPP_PPP_KNXCP] = "KNX Bridging Control Protocol"; |
189 | 2 | tempMap[PCPP_PPP_ECP] = "Encryption Control Protocol"; |
190 | 2 | tempMap[PCPP_PPP_ILECP] = "Individual Link Encryption Control Protocol"; |
191 | 2 | tempMap[PCPP_PPP_IPV6CP] = "IPv6 Control Protocol"; |
192 | 2 | tempMap[PCPP_PPP_MUXCP] = "PPP Muxing Control Protocol"; |
193 | 2 | tempMap[PCPP_PPP_VSNCP] = "Vendor-Specific Network Control Protocol (VSNCP)"; |
194 | 2 | tempMap[PCPP_PPP_TNCP] = "TRILL Network Control Protocol"; |
195 | 2 | tempMap[PCPP_PPP_STAMPEDECP] = "Stampede Bridging Control Protocol"; |
196 | 2 | tempMap[PCPP_PPP_MPPCP] = "MP+ Control Protocol"; |
197 | 2 | tempMap[PCPP_PPP_IPICP] = "NTCITS IPI Control Protocol"; |
198 | 2 | tempMap[PCPP_PPP_SLCC] = "Single link compression in multilink control"; |
199 | 2 | tempMap[PCPP_PPP_CCP] = "Compression Control Protocol"; |
200 | 2 | tempMap[PCPP_PPP_CDPCP] = "Cisco Discovery Protocol Control Protocol"; |
201 | 2 | tempMap[PCPP_PPP_NETCSCP] = "Netcs Twin Routing"; |
202 | 2 | tempMap[PCPP_PPP_STPCP] = "STP - Control Protocol"; |
203 | 2 | tempMap[PCPP_PPP_EDPCP] = "EDPCP - Extreme Discovery Protocol Control Protocol"; |
204 | 2 | tempMap[PCPP_PPP_ACSPC] = "Apple Client Server Protocol Control"; |
205 | 2 | tempMap[PCPP_PPP_MPLSCP] = "MPLS Control Protocol"; |
206 | 2 | tempMap[PCPP_PPP_P12844CP] = "IEEE p1284.4 standard - Protocol Control"; |
207 | 2 | tempMap[PCPP_PPP_TETRACP] = "ETSI TETRA TNP1 Control Protocol"; |
208 | 2 | tempMap[PCPP_PPP_MFTPCP] = "Multichannel Flow Treatment Protocol"; |
209 | 2 | tempMap[PCPP_PPP_LCP] = "Link Control Protocol"; |
210 | 2 | tempMap[PCPP_PPP_PAP] = "Password Authentication Protocol"; |
211 | 2 | tempMap[PCPP_PPP_LQR] = "Link Quality Report"; |
212 | 2 | tempMap[PCPP_PPP_SPAP] = "Shiva Password Authentication Protocol"; |
213 | 2 | tempMap[PCPP_PPP_CBCP] = "Callback Control Protocol (CBCP)"; |
214 | 2 | tempMap[PCPP_PPP_BACP] = "BACP Bandwidth Allocation Control Protocol"; |
215 | 2 | tempMap[PCPP_PPP_BAP] = "BAP Bandwidth Allocation Protocol"; |
216 | 2 | tempMap[PCPP_PPP_VSAP] = "Vendor-Specific Authentication Protocol (VSAP)"; |
217 | 2 | tempMap[PCPP_PPP_CONTCP] = "Container Control Protocol"; |
218 | 2 | tempMap[PCPP_PPP_CHAP] = "Challenge Handshake Authentication Protocol"; |
219 | 2 | tempMap[PCPP_PPP_RSAAP] = "RSA Authentication Protocol"; |
220 | 2 | tempMap[PCPP_PPP_EAP] = "Extensible Authentication Protocol"; |
221 | 2 | tempMap[PCPP_PPP_SIEP] = "Mitsubishi Security Information Exchange Protocol (SIEP)"; |
222 | 2 | tempMap[PCPP_PPP_SBAP] = "Stampede Bridging Authorization Protocol"; |
223 | 2 | tempMap[PCPP_PPP_PRPAP] = "Proprietary Authentication Protocol"; |
224 | 2 | tempMap[PCPP_PPP_PRPAP2] = "Proprietary Authentication Protocol"; |
225 | 2 | tempMap[PCPP_PPP_PRPNIAP] = "Proprietary Node ID Authentication Protocol"; |
226 | 2 | return tempMap; |
227 | 2 | } |
228 | | |
229 | | const std::unordered_map<uint16_t, std::string> PPPNextProtoToString = createPPPNextProtoToStringMap(); |
230 | | |
231 | | std::string PPPoESessionLayer::toString() const |
232 | 4.60k | { |
233 | 4.60k | auto findResult = PPPNextProtoToString.find(getPPPNextProtocol()); |
234 | 4.60k | std::string nextProtocol; |
235 | 4.60k | if (findResult != PPPNextProtoToString.end()) |
236 | 4.12k | nextProtocol = findResult->second; |
237 | 482 | else |
238 | 482 | { |
239 | 482 | std::ostringstream stream; |
240 | 482 | stream << "Unknown (0x" << std::hex << getPPPNextProtocol() << ")"; |
241 | 482 | nextProtocol = stream.str(); |
242 | 482 | } |
243 | | |
244 | 4.60k | return "PPP-over-Ethernet Session (followed by '" + nextProtocol + "')"; |
245 | 4.60k | } |
246 | | |
247 | | /// PPPoEDiscoveryLayer |
248 | | /// ~~~~~~~~~~~~~~~~~~~ |
249 | | |
250 | | PPPoEDiscoveryLayer::PPPoETagTypes PPPoEDiscoveryLayer::PPPoETag::getType() const |
251 | 0 | { |
252 | 0 | if (m_Data == nullptr) |
253 | 0 | return PPPoEDiscoveryLayer::PPPoETagTypes::PPPOE_TAG_EOL; |
254 | | |
255 | 0 | return static_cast<PPPoEDiscoveryLayer::PPPoETagTypes>(be16toh(m_Data->recordType)); |
256 | 0 | } |
257 | | |
258 | | size_t PPPoEDiscoveryLayer::PPPoETag::getTotalSize() const |
259 | 0 | { |
260 | 0 | if (m_Data == nullptr) |
261 | 0 | return 0; |
262 | | |
263 | 0 | return 2 * sizeof(uint16_t) + be16toh(m_Data->recordLen); |
264 | 0 | } |
265 | | |
266 | | size_t PPPoEDiscoveryLayer::PPPoETag::getDataSize() const |
267 | 0 | { |
268 | 0 | if (m_Data == nullptr) |
269 | 0 | return 0; |
270 | | |
271 | 0 | return be16toh(m_Data->recordLen); |
272 | 0 | } |
273 | | |
274 | | PPPoEDiscoveryLayer::PPPoETag PPPoEDiscoveryLayer::PPPoETagBuilder::build() const |
275 | 0 | { |
276 | 0 | size_t tagSize = 2 * sizeof(uint16_t) + m_RecValueLen; |
277 | 0 | uint8_t* recordBuffer = new uint8_t[tagSize]; |
278 | 0 | uint16_t tagTypeVal = htobe16(static_cast<uint16_t>(m_RecType)); |
279 | 0 | uint16_t tagLength = htobe16(static_cast<uint16_t>(m_RecValueLen)); |
280 | 0 | memcpy(recordBuffer, &tagTypeVal, sizeof(uint16_t)); |
281 | 0 | memcpy(recordBuffer + sizeof(uint16_t), &tagLength, sizeof(uint16_t)); |
282 | 0 | if (tagLength > 0 && m_RecValue != nullptr) |
283 | 0 | memcpy(recordBuffer + 2 * sizeof(uint16_t), m_RecValue, m_RecValueLen); |
284 | |
|
285 | 0 | return PPPoEDiscoveryLayer::PPPoETag(recordBuffer); |
286 | 0 | } |
287 | | |
288 | | PPPoEDiscoveryLayer::PPPoETag PPPoEDiscoveryLayer::getTag(PPPoEDiscoveryLayer::PPPoETagTypes tagType) const |
289 | 0 | { |
290 | 0 | return m_TagReader.getTLVRecord(static_cast<uint32_t>(tagType), getTagBasePtr(), |
291 | 0 | m_DataLen - sizeof(pppoe_header)); |
292 | 0 | } |
293 | | |
294 | | PPPoEDiscoveryLayer::PPPoETag PPPoEDiscoveryLayer::getFirstTag() const |
295 | 0 | { |
296 | 0 | return m_TagReader.getFirstTLVRecord(getTagBasePtr(), m_DataLen - sizeof(pppoe_header)); |
297 | 0 | } |
298 | | |
299 | | PPPoEDiscoveryLayer::PPPoETag PPPoEDiscoveryLayer::getNextTag(const PPPoEDiscoveryLayer::PPPoETag& tag) const |
300 | 0 | { |
301 | 0 | return m_TagReader.getNextTLVRecord(const_cast<PPPoEDiscoveryLayer::PPPoETag&>(tag), getTagBasePtr(), |
302 | 0 | m_DataLen - sizeof(pppoe_header)); |
303 | 0 | } |
304 | | |
305 | | int PPPoEDiscoveryLayer::getTagCount() const |
306 | 0 | { |
307 | 0 | return m_TagReader.getTLVRecordCount(getTagBasePtr(), m_DataLen - sizeof(pppoe_header)); |
308 | 0 | } |
309 | | |
310 | | PPPoEDiscoveryLayer::PPPoETag PPPoEDiscoveryLayer::addTagAt(const PPPoETagBuilder& tagBuilder, int offset) |
311 | 0 | { |
312 | 0 | PPPoETag newTag = tagBuilder.build(); |
313 | 0 | if (newTag.isNull()) |
314 | 0 | { |
315 | 0 | PCPP_LOG_ERROR("Cannot build new tag of type " << (int)newTag.getType()); |
316 | 0 | return newTag; |
317 | 0 | } |
318 | | |
319 | 0 | size_t sizeToExtend = newTag.getTotalSize(); |
320 | |
|
321 | 0 | if (!extendLayer(offset, sizeToExtend)) |
322 | 0 | { |
323 | 0 | PCPP_LOG_ERROR("Could not extend PPPoEDiscoveryLayer in [" << sizeToExtend << "] bytes"); |
324 | 0 | newTag.purgeRecordData(); |
325 | 0 | return PPPoETag(nullptr); |
326 | 0 | } |
327 | | |
328 | 0 | memcpy(m_Data + offset, newTag.getRecordBasePtr(), newTag.getTotalSize()); |
329 | |
|
330 | 0 | uint8_t* newTagPtr = m_Data + offset; |
331 | |
|
332 | 0 | m_TagReader.changeTLVRecordCount(1); |
333 | |
|
334 | 0 | newTag.purgeRecordData(); |
335 | |
|
336 | 0 | getPPPoEHeader()->payloadLength += htobe16(sizeToExtend); |
337 | |
|
338 | 0 | return PPPoETag(newTagPtr); |
339 | 0 | } |
340 | | |
341 | | PPPoEDiscoveryLayer::PPPoETag PPPoEDiscoveryLayer::addTagAfter(const PPPoETagBuilder& tagBuilder, |
342 | | PPPoETagTypes prevTagType) |
343 | 0 | { |
344 | 0 | int offset = 0; |
345 | |
|
346 | 0 | PPPoETag prevTag = getTag(prevTagType); |
347 | |
|
348 | 0 | if (prevTag.isNull()) |
349 | 0 | { |
350 | 0 | offset = getHeaderLen(); |
351 | 0 | } |
352 | 0 | else |
353 | 0 | { |
354 | 0 | offset = prevTag.getRecordBasePtr() + prevTag.getTotalSize() - m_Data; |
355 | 0 | } |
356 | |
|
357 | 0 | return addTagAt(tagBuilder, offset); |
358 | 0 | } |
359 | | |
360 | | PPPoEDiscoveryLayer::PPPoETag PPPoEDiscoveryLayer::addTag(const PPPoETagBuilder& tagBuilder) |
361 | 0 | { |
362 | 0 | return addTagAt(tagBuilder, getHeaderLen()); |
363 | 0 | } |
364 | | |
365 | | size_t PPPoEDiscoveryLayer::getHeaderLen() const |
366 | 1.15k | { |
367 | 1.15k | size_t payloadLen = sizeof(pppoe_header) + be16toh(getPPPoEHeader()->payloadLength); |
368 | 1.15k | if (payloadLen > m_DataLen) |
369 | 325 | return m_DataLen; |
370 | | |
371 | 828 | return payloadLen; |
372 | 1.15k | } |
373 | | |
374 | | bool PPPoEDiscoveryLayer::removeTag(PPPoEDiscoveryLayer::PPPoETagTypes tagType) |
375 | 0 | { |
376 | 0 | PPPoEDiscoveryLayer::PPPoETag tagToRemove = getTag(tagType); |
377 | 0 | if (tagToRemove.isNull()) |
378 | 0 | { |
379 | 0 | PCPP_LOG_ERROR("Couldn't find tag"); |
380 | 0 | return false; |
381 | 0 | } |
382 | | |
383 | 0 | int offset = tagToRemove.getRecordBasePtr() - m_Data; |
384 | |
|
385 | 0 | uint16_t tagTotalSize = tagToRemove.getTotalSize(); |
386 | |
|
387 | 0 | if (!shortenLayer(offset, tagTotalSize)) |
388 | 0 | { |
389 | 0 | return false; |
390 | 0 | } |
391 | | |
392 | 0 | m_TagReader.changeTLVRecordCount(-1); |
393 | |
|
394 | 0 | getPPPoEHeader()->payloadLength -= htobe16(tagTotalSize); |
395 | 0 | return true; |
396 | 0 | } |
397 | | |
398 | | bool PPPoEDiscoveryLayer::removeAllTags() |
399 | 0 | { |
400 | 0 | size_t tagCount = getTagCount(); |
401 | 0 | int offset = sizeof(pppoe_header); |
402 | 0 | if (!shortenLayer(offset, m_DataLen - offset)) |
403 | 0 | { |
404 | 0 | return false; |
405 | 0 | } |
406 | 0 | m_TagReader.changeTLVRecordCount(0 - tagCount); |
407 | 0 | getPPPoEHeader()->payloadLength = 0; |
408 | 0 | return true; |
409 | 0 | } |
410 | | |
411 | | std::string PPPoEDiscoveryLayer::codeToString(PPPoECode code) const |
412 | 496 | { |
413 | 496 | switch (code) |
414 | 496 | { |
415 | 6 | case PPPoELayer::PPPOE_CODE_SESSION: |
416 | 6 | return std::string("PPPoE Session"); |
417 | 2 | case PPPoELayer::PPPOE_CODE_PADO: |
418 | 2 | return std::string("PADO"); |
419 | 272 | case PPPoELayer::PPPOE_CODE_PADI: |
420 | 272 | return std::string("PADI"); |
421 | 10 | case PPPoELayer::PPPOE_CODE_PADG: |
422 | 10 | return std::string("PADG"); |
423 | 6 | case PPPoELayer::PPPOE_CODE_PADC: |
424 | 6 | return std::string("PADC"); |
425 | 2 | case PPPoELayer::PPPOE_CODE_PADQ: |
426 | 2 | return std::string("PADQ"); |
427 | 6 | case PPPoELayer::PPPOE_CODE_PADR: |
428 | 6 | return std::string("PADR"); |
429 | 134 | case PPPoELayer::PPPOE_CODE_PADS: |
430 | 134 | return std::string("PADS"); |
431 | 14 | case PPPoELayer::PPPOE_CODE_PADT: |
432 | 14 | return std::string("PADT"); |
433 | 2 | case PPPoELayer::PPPOE_CODE_PADM: |
434 | 2 | return std::string("PADM"); |
435 | 6 | case PPPoELayer::PPPOE_CODE_PADN: |
436 | 6 | return std::string("PADN"); |
437 | 36 | default: |
438 | 36 | return std::string("Unknown PPPoE code"); |
439 | 496 | } |
440 | 496 | } |
441 | | |
442 | | } // namespace pcpp |