1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 12:44:25 +01:00

STP is now serialized correctly. LLC behaves correctly when it contains an STP is its inner pdu.

This commit is contained in:
Matias Fontanini
2013-04-16 01:05:06 -03:00
parent 9631734805
commit 38ccb4413b
5 changed files with 51 additions and 8 deletions

View File

@@ -376,7 +376,6 @@ namespace Tins {
typedef std::vector<uint8_t> field_type;
void copy_fields(const LLC *other);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
llchdr _header;

View File

@@ -66,5 +66,6 @@
#include "sll.h"
#include "dhcpv6.h"
#include "pppoe.h"
#include "stp.h"
#endif // TINS_TINS_H

View File

@@ -30,9 +30,8 @@
#include <stdexcept>
#include <cstring>
#include <cassert>
#include "pdu.h"
#include "llc.h"
#include "stp.h"
#include "rawpdu.h"
using std::list;
@@ -86,8 +85,12 @@ LLC::LLC(const uint8_t *buffer, uint32_t total_sz) {
buffer += 2;
total_sz -= 2;
}
if(total_sz > 0)
inner_pdu(new Tins::RawPDU(buffer, total_sz));
if(total_sz > 0) {
if(dsap() == 0x42 && ssap() == 0x42)
inner_pdu(new Tins::STP(buffer, total_sz));
else
inner_pdu(new Tins::RawPDU(buffer, total_sz));
}
}
void LLC::group(bool value) {
@@ -201,6 +204,10 @@ void LLC::clear_information_fields() {
void LLC::write_serialization(uint8_t *buffer, uint32_t total_sz, const Tins::PDU *parent) {
assert(total_sz >= header_size());
if(inner_pdu() && inner_pdu()->pdu_type() == PDU::STP) {
dsap(0x42);
ssap(0x42);
}
std::memcpy(buffer, &_header, sizeof(_header));
buffer += sizeof(_header);
switch (type()) {
@@ -219,7 +226,6 @@ void LLC::write_serialization(uint8_t *buffer, uint32_t total_sz, const Tins::PD
}
for (list<field_type>::const_iterator it = information_fields.begin(); it != information_fields.end(); it++) {
//std::memcpy(buffer, it->second, it->first);
std::copy(it->begin(), it->end(), buffer);
buffer += it->size();
}

View File

@@ -28,6 +28,7 @@
*/
#include <cstring>
#include <cassert>
#include "stp.h"
namespace Tins {
@@ -86,7 +87,10 @@ void STP::fwd_delay(uint16_t new_fwd_delay) {
}
void STP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
#ifdef TINS_DEBUG
assert(total_sz >= sizeof(_header));
#endif
std::memcpy(buffer, &_header, sizeof(_header));
}
uint32_t STP::header_size() const {

View File

@@ -3,6 +3,8 @@
#include <string>
#include <stdint.h>
#include "stp.h"
#include "dot3.h"
#include "llc.h"
using namespace std;
using namespace Tins;
@@ -15,7 +17,7 @@ public:
const uint8_t STPTest::expected_packet[] = {
146, 131, 138, 146, 146, 128, 0, 0, 144, 76, 8, 23, 181, 0, 146, 131,
120, 128, 0, 0, 144, 76, 8, 23, 181, 128, 1, 15, 0, 20, 0, 2, 0, 0,
0, 165, 165, 165, 165, 165, 165, 165, 165
0
};
TEST_F(STPTest, DefaultConstructor) {
@@ -48,6 +50,37 @@ TEST_F(STPTest, ConstructorFromBuffer) {
EXPECT_EQ(0, pdu.fwd_delay());
}
TEST_F(STPTest, ChainedPDUs) {
const uint8_t input[] = {
1, 128, 194, 0, 0, 0, 0, 144, 76, 8, 23, 181, 0, 38, 66, 66, 3,
0, 0, 0, 0, 0, 128, 0, 0, 144, 76, 8, 23, 181, 0, 0, 0, 0, 128,
0, 0, 144, 76, 8, 23, 181, 128, 1, 0, 0, 20, 0, 2, 0, 0, 0
};
Dot3 pkt(input, sizeof(input));
STP *stp = pkt.find_pdu<STP>();
LLC *llc = pkt.find_pdu<LLC>();
ASSERT_TRUE(stp);
ASSERT_TRUE(llc);
EXPECT_EQ(0x8001, stp->port_id());
EXPECT_EQ(0, stp->msg_age());
EXPECT_EQ(20, stp->max_age());
EXPECT_EQ(2, stp->hello_time());
llc->dsap(0);
llc->ssap(0);
EXPECT_EQ(
PDU::serialization_type(input, input + sizeof(input)),
pkt.serialize()
);
}
TEST_F(STPTest, Serialize) {
STP pdu(expected_packet, sizeof(expected_packet));
EXPECT_EQ(
PDU::serialization_type(expected_packet, expected_packet + sizeof(expected_packet)),
pdu.serialize()
);
}
TEST_F(STPTest, ProtoID) {
STP pdu;
pdu.proto_id(0x9283);