1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Added correct parsing of PPPoE session packets.

This commit is contained in:
Matias Fontanini
2014-09-24 09:15:20 -03:00
parent 1bc9bd1504
commit 3ad96422b9
4 changed files with 79 additions and 21 deletions

View File

@@ -32,6 +32,7 @@
#endif
#include <cstring>
#include "pppoe.h"
#include "rawpdu.h"
#include "exceptions.h"
namespace Tins {
@@ -52,27 +53,37 @@ PPPoE::PPPoE(const uint8_t *buffer, uint32_t total_sz)
buffer += sizeof(_header);
total_sz -= sizeof(_header);
total_sz = std::min(total_sz, (uint32_t)payload_length());
const uint8_t *end = buffer + total_sz;
while(buffer < end) {
if(buffer + sizeof(uint32_t) * 2 > end)
throw malformed_packet();
uint16_t opt_type;
std::memcpy(&opt_type, buffer, sizeof(uint16_t));
uint16_t opt_len;
std::memcpy(&opt_len, buffer + sizeof(uint16_t), sizeof(uint16_t));
buffer += sizeof(uint16_t) * 2;
total_sz -= sizeof(uint16_t) * 2;
if(Endian::be_to_host(opt_len) > total_sz)
throw malformed_packet();
add_tag(
tag(
static_cast<TagTypes>(opt_type),
Endian::be_to_host(opt_len),
buffer
)
);
buffer += Endian::be_to_host(opt_len);
total_sz -= Endian::be_to_host(opt_len);
// If this is a session data packet
if(code() == 0) {
if(total_sz > 0) {
inner_pdu(
new RawPDU(buffer, total_sz)
);
}
}
else {
const uint8_t *end = buffer + total_sz;
while(buffer < end) {
if(buffer + sizeof(uint32_t) * 2 > end)
throw malformed_packet();
uint16_t opt_type;
std::memcpy(&opt_type, buffer, sizeof(uint16_t));
uint16_t opt_len;
std::memcpy(&opt_len, buffer + sizeof(uint16_t), sizeof(uint16_t));
buffer += sizeof(uint16_t) * 2;
total_sz -= sizeof(uint16_t) * 2;
if(Endian::be_to_host(opt_len) > total_sz)
throw malformed_packet();
add_tag(
tag(
static_cast<TagTypes>(opt_type),
Endian::be_to_host(opt_len),
buffer
)
);
buffer += Endian::be_to_host(opt_len);
total_sz -= Endian::be_to_host(opt_len);
}
}
}