From 8db60323032a77c8712d091a923a004b7b188808 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Mon, 8 Feb 2016 20:59:00 -0800 Subject: [PATCH] Add hardware addresses to Stream --- include/tins/tcp_ip.h | 28 ++++++++++++++++++++++++++++ src/tcp_ip.cpp | 14 ++++++++++++++ tests/src/tcp_ip.cpp | 4 ++++ 3 files changed, 46 insertions(+) diff --git a/include/tins/tcp_ip.h b/include/tins/tcp_ip.h index 0a4e26d..c3d2869 100644 --- a/include/tins/tcp_ip.h +++ b/include/tins/tcp_ip.h @@ -41,6 +41,7 @@ #include #include #include "macros.h" +#include "hw_address.h" namespace Tins { @@ -276,6 +277,11 @@ public: */ typedef std::function 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_; }; diff --git a/src/tcp_ip.cpp b/src/tcp_ip.cpp index 9a6e490..0059f74 100644 --- a/src/tcp_ip.cpp +++ b/src/tcp_ip.cpp @@ -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(); + 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(); } diff --git a/tests/src/tcp_ip.cpp b/tests/src/tcp_ip.cpp index 52aedf9..5e4ffac 100644 --- a/tests/src/tcp_ip.cpp +++ b/tests/src/tcp_ip.cpp @@ -296,6 +296,8 @@ TEST_F(FlowTest, StreamFollower_ThreeWayHandshake) { using std::placeholders::_1; vector 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());