mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Add hardware addresses to Stream
This commit is contained in:
@@ -41,6 +41,7 @@
|
||||
#include <functional>
|
||||
#include <stdint.h>
|
||||
#include "macros.h"
|
||||
#include "hw_address.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
@@ -276,6 +277,11 @@ public:
|
||||
*/
|
||||
typedef std::function<void(Stream&)> stream_callback;
|
||||
|
||||
/**
|
||||
* The type used to store hardware addresses
|
||||
*/
|
||||
typedef HWAddress<6> hwaddress_type;
|
||||
|
||||
/**
|
||||
* The type used to store payloads
|
||||
*/
|
||||
@@ -341,6 +347,26 @@ public:
|
||||
*/
|
||||
IPv6Address client_addr_v6() const;
|
||||
|
||||
/**
|
||||
* \brief Retrieves the client's hardware address.
|
||||
*
|
||||
* Note that this is not the actual hardware address of the client, but
|
||||
* just the address seen from packets coming from it. If the client
|
||||
* is on another network, then this will be the address of the last
|
||||
* device (switch, route, etc) the packet went through.
|
||||
*/
|
||||
const hwaddress_type& client_hw_addr() const;
|
||||
|
||||
/**
|
||||
* \brief Retrieves the server's hardware address.
|
||||
*
|
||||
* Note that this is not the actual hardware address of the server, but
|
||||
* just the address seen from packets coming from it. If the server
|
||||
* is on another network, then this will be the address of the last
|
||||
* device (switch, route, etc) the packet went through.
|
||||
*/
|
||||
const hwaddress_type& server_hw_addr() const;
|
||||
|
||||
/**
|
||||
* \brief Retrieves the server's IPv4 address
|
||||
*
|
||||
@@ -486,6 +512,8 @@ private:
|
||||
stream_callback on_server_data_callback_;
|
||||
stream_callback on_client_buffering_callback_;
|
||||
stream_callback on_server_buffering_callback_;
|
||||
hwaddress_type client_hw_addr_;
|
||||
hwaddress_type server_hw_addr_;
|
||||
bool auto_cleanup_;
|
||||
};
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include "tcp.h"
|
||||
#include "ip.h"
|
||||
#include "ipv6.h"
|
||||
#include "ethernetII.h"
|
||||
#include "rawpdu.h"
|
||||
#include "exceptions.h"
|
||||
#include "memory_helpers.h"
|
||||
@@ -301,6 +302,11 @@ Stream::Stream(const PDU& packet)
|
||||
if (tcp.flags() == TCP::SYN) {
|
||||
client_flow().state(Flow::SYN_SENT);
|
||||
}
|
||||
const EthernetII* eth = packet.find_pdu<EthernetII>();
|
||||
if (eth) {
|
||||
client_hw_addr_ = eth->src_addr();
|
||||
server_hw_addr_ = eth->dst_addr();
|
||||
}
|
||||
}
|
||||
|
||||
void Stream::process_packet(PDU& packet) {
|
||||
@@ -384,6 +390,14 @@ IPv6Address Stream::client_addr_v6() const {
|
||||
return server_flow().dst_addr_v6();
|
||||
}
|
||||
|
||||
const Stream::hwaddress_type& Stream::client_hw_addr() const {
|
||||
return client_hw_addr_;
|
||||
}
|
||||
|
||||
const Stream::hwaddress_type& Stream::server_hw_addr() const {
|
||||
return server_hw_addr_;
|
||||
}
|
||||
|
||||
IPv4Address Stream::server_addr_v4() const {
|
||||
return client_flow().dst_addr_v4();
|
||||
}
|
||||
|
||||
@@ -296,6 +296,8 @@ TEST_F(FlowTest, StreamFollower_ThreeWayHandshake) {
|
||||
using std::placeholders::_1;
|
||||
|
||||
vector<EthernetII> packets = three_way_handshake(29, 60, "1.2.3.4", 22, "4.3.2.1", 25);
|
||||
packets[0].src_addr("00:01:02:03:04:05");
|
||||
packets[0].dst_addr("05:04:03:02:01:00");
|
||||
StreamFollower follower;
|
||||
follower.new_stream_callback(bind(&FlowTest::on_new_stream, this, _1));
|
||||
for (size_t i = 0; i < packets.size(); ++i) {
|
||||
@@ -313,6 +315,8 @@ TEST_F(FlowTest, StreamFollower_ThreeWayHandshake) {
|
||||
EXPECT_EQ(22, stream.server_flow().dport());
|
||||
EXPECT_EQ(IPv4Address("1.2.3.4"), stream.client_addr_v4());
|
||||
EXPECT_EQ(IPv4Address("4.3.2.1"), stream.server_addr_v4());
|
||||
EXPECT_EQ(HWAddress<6>("00:01:02:03:04:05"), stream.client_hw_addr());
|
||||
EXPECT_EQ(HWAddress<6>("05:04:03:02:01:00"), stream.server_hw_addr());
|
||||
EXPECT_EQ(22, stream.client_port());
|
||||
EXPECT_EQ(25, stream.server_port());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user