Skip to content

Commit

Permalink
Correctly serialize PPPoE session packets
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini committed Jan 1, 2016
1 parent 2c16aaa commit f5a82b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/ethernetII.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "ethernetII.h"
#include "packet_sender.h"
#include "rawpdu.h"
#include "pppoe.h"
#include "ip.h"
#include "ipv6.h"
#include "arp.h"
Expand Down Expand Up @@ -152,9 +153,17 @@ bool EthernetII::matches_response(const uint8_t *ptr, uint32_t total_sz) const {
void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
OutputMemoryStream stream(buffer, total_sz);
if (inner_pdu()) {
Constants::Ethernet::e flag = Internals::pdu_flag_to_ether_type(
inner_pdu()->pdu_type()
);
Constants::Ethernet::e flag;
const PDUType type = inner_pdu()->pdu_type();
// Dirty trick to sucessfully tag PPPoE session/discovery packets
if (type == PDU::PPPOE) {
const PPPoE* pppoe = static_cast<const PPPoE*>(inner_pdu());
flag = (pppoe->code() == 0) ? Constants::Ethernet::PPPOES
: Constants::Ethernet::PPPOED;
}
else {
flag = Internals::pdu_flag_to_ether_type(type);
}
if (flag != Constants::Ethernet::UNKNOWN) {
payload_type(static_cast<uint16_t>(flag));
}
Expand Down Expand Up @@ -188,4 +197,5 @@ PDU *EthernetII::recv_response(PacketSender &sender, const NetworkInterface &ifa
#endif
}
#endif // _WIN32
}

} // Tins
30 changes: 30 additions & 0 deletions tests/src/pppoe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PPPoETest : public testing::Test {
static const uint8_t expected_packet[];
static const uint8_t session_packet[];
static const uint8_t full_session_packet[];
static const uint8_t full_session_packet2[];
};

const uint8_t PPPoETest::expected_packet[] = {
Expand All @@ -33,6 +34,13 @@ const uint8_t PPPoETest::full_session_packet[] = {
, 0, 0, 0, 0, 0, 0, 0
};

const uint8_t PPPoETest::full_session_packet2[] = {
255, 255, 255, 255, 255, 255, 0, 12, 41, 87, 232, 60, 136, 100, 17,
0, 0, 0, 0, 50, 0, 87, 96, 0, 0, 0, 0, 8, 58, 1, 254, 128, 0, 0, 0,
0, 0, 0, 2, 12, 41, 255, 254, 87, 232, 60, 255, 2, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 2, 151, 20, 88, 131, 0, 0, 0, 0
};

TEST_F(PPPoETest, DefaultConstructor) {
PPPoE pdu;
EXPECT_EQ(1, pdu.version());
Expand Down Expand Up @@ -69,6 +77,28 @@ TEST_F(PPPoETest, ConstructorFromFullSessionBuffer) {
const RawPDU* raw = pdu.find_pdu<RawPDU>();
ASSERT_TRUE(raw != NULL);
EXPECT_EQ(21U, raw->payload_size());

PDU::serialization_type buffer = eth.serialize();
EXPECT_EQ(
PDU::serialization_type(
full_session_packet,
full_session_packet + sizeof(full_session_packet)
),
buffer
);
}

TEST_F(PPPoETest, ConstructorFromFullSessionBuffer2) {
EthernetII eth(full_session_packet2, sizeof(full_session_packet2));

PDU::serialization_type buffer = eth.serialize();
EXPECT_EQ(
PDU::serialization_type(
full_session_packet2,
full_session_packet2 + sizeof(full_session_packet2)
),
buffer
);
}

TEST_F(PPPoETest, ConstructorFromBuffer) {
Expand Down

0 comments on commit f5a82b1

Please sign in to comment.