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