1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 04:34:27 +01:00

Added STP class.

This commit is contained in:
Matias F
2013-04-15 14:01:44 -03:00
parent 7faf514496
commit 9631734805
7 changed files with 936 additions and 359 deletions

View File

@@ -13,6 +13,7 @@ AM_CXXFLAGS = -Wall -pedantic -I@LIBTINS_INCLUDE_DIR@
libtins_la_SOURCES=src/arp.cpp \
src/bootp.cpp \
src/stp.cpp \
src/pppoe.cpp \
src/crypto.cpp \
src/dhcp.cpp \
@@ -95,4 +96,5 @@ libtins_include_HEADERS = include/internals.h \
include/ipv6_address.h \
include/constants.h \
include/utils.h \
include/cxxstd.h
include/cxxstd.h \
include/stp.h

View File

@@ -102,12 +102,12 @@ am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(pkgconfigdir)" \
LTLIBRARIES = $(lib_LTLIBRARIES)
libtins_la_LIBADD =
am__dirstamp = $(am__leading_dot)dirstamp
am_libtins_la_OBJECTS = src/arp.lo src/bootp.lo src/pppoe.lo \
src/crypto.lo src/dhcp.lo src/dhcpv6.lo src/dns.lo \
src/dns_record.lo src/dot11.lo src/dot3.lo src/dot1q.lo \
src/eapol.lo src/ethernetII.lo src/icmp.lo src/icmpv6.lo \
src/internals.lo src/ip.lo src/ip_address.lo src/ipv6.lo \
src/ipv6_address.lo src/llc.lo src/loopback.lo \
am_libtins_la_OBJECTS = src/arp.lo src/bootp.lo src/stp.lo \
src/pppoe.lo src/crypto.lo src/dhcp.lo src/dhcpv6.lo \
src/dns.lo src/dns_record.lo src/dot11.lo src/dot3.lo \
src/dot1q.lo src/eapol.lo src/ethernetII.lo src/icmp.lo \
src/icmpv6.lo src/internals.lo src/ip.lo src/ip_address.lo \
src/ipv6.lo src/ipv6_address.lo src/llc.lo src/loopback.lo \
src/network_interface.lo src/packet_sender.lo \
src/packet_writer.lo src/pdu.lo src/radiotap.lo src/rawpdu.lo \
src/rsn_information.lo src/sll.lo src/snap.lo src/sniffer.lo \
@@ -287,6 +287,7 @@ libtins_la_LDFLAGS = -version-info @LIBTINS_VERSION@
AM_CXXFLAGS = -Wall -pedantic -I@LIBTINS_INCLUDE_DIR@
libtins_la_SOURCES = src/arp.cpp \
src/bootp.cpp \
src/stp.cpp \
src/pppoe.cpp \
src/crypto.cpp \
src/dhcp.cpp \
@@ -369,7 +370,8 @@ libtins_include_HEADERS = include/internals.h \
include/ipv6_address.h \
include/constants.h \
include/utils.h \
include/cxxstd.h
include/cxxstd.h \
include/stp.h
all: all-am
@@ -451,6 +453,7 @@ src/$(DEPDIR)/$(am__dirstamp):
@: > src/$(DEPDIR)/$(am__dirstamp)
src/arp.lo: src/$(am__dirstamp) src/$(DEPDIR)/$(am__dirstamp)
src/bootp.lo: src/$(am__dirstamp) src/$(DEPDIR)/$(am__dirstamp)
src/stp.lo: src/$(am__dirstamp) src/$(DEPDIR)/$(am__dirstamp)
src/pppoe.lo: src/$(am__dirstamp) src/$(DEPDIR)/$(am__dirstamp)
src/crypto.lo: src/$(am__dirstamp) src/$(DEPDIR)/$(am__dirstamp)
src/dhcp.lo: src/$(am__dirstamp) src/$(DEPDIR)/$(am__dirstamp)
@@ -558,6 +561,8 @@ mostlyclean-compile:
-rm -f src/snap.lo
-rm -f src/sniffer.$(OBJEXT)
-rm -f src/sniffer.lo
-rm -f src/stp.$(OBJEXT)
-rm -f src/stp.lo
-rm -f src/tcp.$(OBJEXT)
-rm -f src/tcp.lo
-rm -f src/tcp_stream.$(OBJEXT)
@@ -602,6 +607,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/sll.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/snap.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/sniffer.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/stp.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/tcp.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/tcp_stream.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@src/$(DEPDIR)/udp.Plo@am__quote@

View File

@@ -114,7 +114,8 @@ namespace Tins {
SLL,
DHCPv6,
DOT1Q,
PPPOE
PPPOE,
STP
};
/**

258
include/stp.h Normal file
View File

@@ -0,0 +1,258 @@
/*
* Copyright (c) 2012, Nasel
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef TINS_STP_H
#define TINS_STP_H
#include "pdu.h"
#include "endianness.h"
#include "small_uint.h"
namespace Tins {
class STP : public PDU {
public:
/**
* This PDU's flag.
*/
static const PDU::PDUType pdu_flag = PDU::STP;
/**
* \brief Default constructor.
*/
STP();
/**
* \brief Constructor which constructs an STP object from a buffer
* and adds all identifiable PDUs found in the buffer as children
* of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
STP(const uint8_t *buffer, uint32_t total_sz);
// Getters
/**
* \brief Getter for the proto_id field.
* \return The stored proto_id field value.
*/
uint16_t proto_id() const {
return Endian::be_to_host(_header.proto_id);
}
/**
* \brief Getter for the proto_version field.
* \return The stored proto_version field value.
*/
uint8_t proto_version() const {
return _header.proto_version;
}
/**
* \brief Getter for the bpdu_type field.
* \return The stored bpdu_type field value.
*/
uint8_t bpdu_type() const {
return _header.bpdu_type;
}
/**
* \brief Getter for the bpdu_flags field.
* \return The stored bpdu_flags field value.
*/
uint8_t bpdu_flags() const {
return _header.bpdu_flags;
}
/**
* \brief Getter for the root_path_cost field.
* \return The stored root_path_cost field value.
*/
uint32_t root_path_cost() const {
return Endian::be_to_host(_header.root_path_cost);
}
/**
* \brief Getter for the port_id field.
* \return The stored port_id field value.
*/
uint16_t port_id() const {
return Endian::be_to_host(_header.port_id);
}
/**
* \brief Getter for the msg_age field.
* \return The stored msg_age field value.
*/
uint16_t msg_age() const {
return Endian::be_to_host(_header.msg_age) / 256;
}
/**
* \brief Getter for the max_age field.
* \return The stored max_age field value.
*/
uint16_t max_age() const {
return Endian::be_to_host(_header.max_age) / 256;
}
/**
* \brief Getter for the hello_time field.
* \return The stored hello_time field value.
*/
uint16_t hello_time() const {
return Endian::be_to_host(_header.hello_time) / 256;
}
/**
* \brief Getter for the fwd_delay field.
* \return The stored fwd_delay field value.
*/
uint16_t fwd_delay() const {
return Endian::be_to_host(_header.fwd_delay) / 256;
}
/**
* \brief Getter for the PDU's type.
* \sa PDU::pdu_type
*/
PDUType pdu_type() const { return pdu_flag; }
/**
* \sa PDU::clone
*/
STP *clone() const {
return new STP(*this);
}
/**
* \brief Returns the header size.
*
* This metod overrides PDU::header_size. \sa PDU::header_size
*/
uint32_t header_size() const;
// Setters
/**
* \brief Setter for the proto_id field.
* \param new_proto_id The new proto_id field value.
*/
void proto_id(uint16_t new_proto_id);
/**
* \brief Setter for the proto_version field.
* \param new_proto_version The new proto_version field value.
*/
void proto_version(uint8_t new_proto_version);
/**
* \brief Setter for the bpdu_type field.
* \param new_bpdu_type The new bpdu_type field value.
*/
void bpdu_type(uint8_t new_bpdu_type);
/**
* \brief Setter for the bpdu_flags field.
* \param new_bpdu_flags The new bpdu_flags field value.
*/
void bpdu_flags(uint8_t new_bpdu_flags);
/**
* \brief Setter for the root_path_cost field.
* \param new_root_path_cost The new root_path_cost field value.
*/
void root_path_cost(uint32_t new_root_path_cost);
/**
* \brief Setter for the port_id field.
* \param new_port_id The new port_id field value.
*/
void port_id(uint16_t new_port_id);
/**
* \brief Setter for the msg_age field.
* \param new_msg_age The new msg_age field value.
*/
void msg_age(uint16_t new_msg_age);
/**
* \brief Setter for the max_age field.
* \param new_max_age The new max_age field value.
*/
void max_age(uint16_t new_max_age);
/**
* \brief Setter for the hello_time field.
* \param new_hello_time The new hello_time field value.
*/
void hello_time(uint16_t new_hello_time);
/**
* \brief Setter for the fwd_delay field.
* \param new_fwd_delay The new fwd_delay field value.
*/
void fwd_delay(uint16_t new_fwd_delay);
private:
TINS_BEGIN_PACK
struct pvt_bpdu_id {
#if TINS_IS_LITTLE_ENDIAN
// fixme
uint16_t priority:4,
ext_id:12;
#else
uint16_t priority:4,
ext_id:12;
#endif
uint8_t id[6];
} TINS_END_PACK;
TINS_BEGIN_PACK
struct stphdr {
uint16_t proto_id;
uint8_t proto_version;
uint8_t bpdu_type;
uint8_t bpdu_flags;
pvt_bpdu_id root_id;
uint32_t root_path_cost;
pvt_bpdu_id bridge_id;
uint16_t port_id;
uint16_t msg_age;
uint16_t max_age;
uint16_t hello_time;
uint16_t fwd_delay;
} TINS_END_PACK;
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
stphdr _header;
};
}
#endif // TINS_STP_H

96
src/stp.cpp Normal file
View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2012, Nasel
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <cstring>
#include "stp.h"
namespace Tins {
STP::STP()
: _header()
{
}
STP::STP(const uint8_t *buffer, uint32_t total_sz)
{
if(total_sz < sizeof(_header))
throw std::runtime_error("Not enough size.");
std::memcpy(&_header, buffer ,sizeof(_header));
}
void STP::proto_id(uint16_t new_proto_id) {
_header.proto_id = Endian::host_to_be(new_proto_id);
}
void STP::proto_version(uint8_t new_proto_version) {
_header.proto_version = new_proto_version;
}
void STP::bpdu_type(uint8_t new_bpdu_type) {
_header.bpdu_type = new_bpdu_type;
}
void STP::bpdu_flags(uint8_t new_bpdu_flags) {
_header.bpdu_flags = new_bpdu_flags;
}
void STP::root_path_cost(uint32_t new_root_path_cost) {
_header.root_path_cost = Endian::host_to_be(new_root_path_cost);
}
void STP::port_id(uint16_t new_port_id) {
_header.port_id = Endian::host_to_be(new_port_id);
}
void STP::msg_age(uint16_t new_msg_age) {
_header.msg_age = Endian::host_to_be<uint16_t>(new_msg_age * 256);
}
void STP::max_age(uint16_t new_max_age) {
_header.max_age = Endian::host_to_be<uint16_t>(new_max_age * 256);
}
void STP::hello_time(uint16_t new_hello_time) {
_header.hello_time = Endian::host_to_be<uint16_t>(new_hello_time * 256);
}
void STP::fwd_delay(uint16_t new_fwd_delay) {
_header.fwd_delay = Endian::host_to_be<uint16_t>(new_fwd_delay * 256);
}
void STP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
}
uint32_t STP::header_size() const {
return sizeof(_header);
}
}

File diff suppressed because it is too large Load Diff

109
tests/src/stp.cpp Normal file
View File

@@ -0,0 +1,109 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include <stdint.h>
#include "stp.h"
using namespace std;
using namespace Tins;
class STPTest : public testing::Test {
public:
static const uint8_t expected_packet[];
};
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
};
TEST_F(STPTest, DefaultConstructor) {
STP pdu;
EXPECT_EQ(0, pdu.proto_id());
EXPECT_EQ(0, pdu.proto_version());
EXPECT_EQ(0, pdu.bpdu_type());
EXPECT_EQ(0, pdu.bpdu_flags());
EXPECT_EQ(0, pdu.root_path_cost());
EXPECT_EQ(0, pdu.port_id());
EXPECT_EQ(0, pdu.msg_age());
EXPECT_EQ(0, pdu.max_age());
EXPECT_EQ(0, pdu.hello_time());
EXPECT_EQ(0, pdu.fwd_delay());
}
TEST_F(STPTest, ConstructorFromBuffer) {
STP pdu(expected_packet, sizeof(expected_packet));
EXPECT_EQ(0x9283, pdu.proto_id());
EXPECT_EQ(0x8a, pdu.proto_version());
EXPECT_EQ(0x92, pdu.bpdu_type());
EXPECT_EQ(0x92, pdu.bpdu_flags());
// root identifier(32768. 0, 00:90:4c:08:17:b5
EXPECT_EQ(0x928378, pdu.root_path_cost());
// bridge identifier(32768. 0, 00:90:4c:08:17:b5
EXPECT_EQ(0x8001, pdu.port_id());
EXPECT_EQ(15, pdu.msg_age());
EXPECT_EQ(20, pdu.max_age());
EXPECT_EQ(2, pdu.hello_time());
EXPECT_EQ(0, pdu.fwd_delay());
}
TEST_F(STPTest, ProtoID) {
STP pdu;
pdu.proto_id(0x9283);
EXPECT_EQ(0x9283, pdu.proto_id());
}
TEST_F(STPTest, ProtoVersion) {
STP pdu;
pdu.proto_version(0x15);
EXPECT_EQ(0x15, pdu.proto_version());
}
TEST_F(STPTest, BPDUType) {
STP pdu;
pdu.bpdu_type(0x15);
EXPECT_EQ(0x15, pdu.bpdu_type());
}
TEST_F(STPTest, BPDUFlags) {
STP pdu;
pdu.bpdu_flags(0x15);
EXPECT_EQ(0x15, pdu.bpdu_flags());
}
TEST_F(STPTest, RootPathCost) {
STP pdu;
pdu.root_path_cost(0x28378462);
EXPECT_EQ(0x28378462, pdu.root_path_cost());
}
TEST_F(STPTest, PortID) {
STP pdu;
pdu.port_id(0x9283);
EXPECT_EQ(0x9283, pdu.port_id());
}
TEST_F(STPTest, MsgAge) {
STP pdu;
pdu.msg_age(15);
EXPECT_EQ(15, pdu.msg_age());
}
TEST_F(STPTest, MaxAge) {
STP pdu;
pdu.max_age(15);
EXPECT_EQ(15, pdu.max_age());
}
TEST_F(STPTest, FwdDelay) {
STP pdu;
pdu.fwd_delay(15);
EXPECT_EQ(15, pdu.fwd_delay());
}
TEST_F(STPTest, HelloTime) {
STP pdu;
pdu.hello_time(15);
EXPECT_EQ(15, pdu.hello_time());
}