diff --git a/depends.d b/depends.d index aa8c415..21350f2 100644 --- a/depends.d +++ b/depends.d @@ -135,11 +135,13 @@ include/cxxstd.h: include/macros.h: include/endianness.h: -src/dot11.o: src/dot11.cpp include/dot11.h include/macros.h include/pdu.h \ - include/endianness.h include/hw_address.h include/small_uint.h \ - include/pdu_option.h include/network_interface.h include/ip_address.h \ - include/rawpdu.h include/rsn_information.h include/packet_sender.h \ - include/snap.h +src/dot11.o: src/dot11.cpp include/macros.h include/dot11.h \ + include/macros.h include/pdu.h include/endianness.h include/hw_address.h \ + include/small_uint.h include/pdu_option.h include/network_interface.h \ + include/ip_address.h include/rawpdu.h include/rsn_information.h \ + include/packet_sender.h include/snap.h + +include/macros.h: include/dot11.h: diff --git a/include/packet.h b/include/packet.h index dee6f17..870d8e4 100644 --- a/include/packet.h +++ b/include/packet.h @@ -130,8 +130,19 @@ public: * * The PDU* is cloned using PDU::clone. */ - Packet(PDU *apdu, const Timestamp &tstamp) - : pdu_(apdu->clone()), ts(tstamp) {} + Packet(const PDU *apdu, const Timestamp &tstamp) + : pdu_(apdu->clone()), ts(tstamp) { } + + /** + * \brief Constructs a Packet from a const PDU&. + * + * The timestamp will be set to the current time. + * + * This calls PDU::clone on the PDU parameter. + * + */ + Packet(const PDU &rhs) + : pdu_(rhs.clone()), ts(Timestamp::current_time()) { } /** * \brief Constructs a Packet from a RefPacket. @@ -248,6 +259,19 @@ public: operator bool() const { return bool(pdu_); } + + /** + * + * \brief Concatenation operator. + * + * Adds the PDU at the end of the PDU stack. + * + * \param rhs The PDU to be appended. + */ + Packet &operator/=(const PDU &rhs) { + pdu_ /= rhs; + return *this; + } private: PDU *pdu_; Timestamp ts; diff --git a/include/pdu.h b/include/pdu.h index 6226240..394f4a0 100644 --- a/include/pdu.h +++ b/include/pdu.h @@ -362,6 +362,17 @@ namespace Tins { lop /= rop; return lop; } + + /** + * \brief Concatenation operator on PDU pointers. + * + * \sa operator/= + */ + template + T *operator/= (T* lop, const PDU &rop) { + *lop /= rop; + return lop; + } }; #endif // TINS_PDU_H diff --git a/include/timestamp.h b/include/timestamp.h index a2636ee..6ec61b9 100644 --- a/include/timestamp.h +++ b/include/timestamp.h @@ -55,6 +55,20 @@ public: typedef suseconds_t microseconds_type; #endif + /** + * \brief Constructs a Timestamp which will hold the current time. + */ + static Timestamp current_time() { + #ifdef WIN32 + //fixme + return Timestamp(); + #else + timeval tv; + gettimeofday(&tv, 0); + return tv; + #endif + } + /** * Default constructs the timestamp. */ diff --git a/tests/depends.d b/tests/depends.d index 14aeb0e..c0eb0f3 100644 --- a/tests/depends.d +++ b/tests/depends.d @@ -139,13 +139,15 @@ ../include/macros.h: ../include/endianness.h: -../src/dot11.o: ../src/dot11.cpp ../include/dot11.h ../include/macros.h \ - ../include/pdu.h ../include/endianness.h ../include/hw_address.h \ - ../include/small_uint.h ../include/pdu_option.h \ +../src/dot11.o: ../src/dot11.cpp ../include/macros.h ../include/dot11.h \ + ../include/macros.h ../include/pdu.h ../include/endianness.h \ + ../include/hw_address.h ../include/small_uint.h ../include/pdu_option.h \ ../include/network_interface.h ../include/ip_address.h \ ../include/rawpdu.h ../include/rsn_information.h \ ../include/packet_sender.h ../include/snap.h +../include/macros.h: + ../include/dot11.h: ../include/macros.h: @@ -1655,7 +1657,8 @@ src/network_interface.o: src/network_interface.cpp \ src/pdu.o: src/pdu.cpp ../include/ip.h ../include/pdu.h \ ../include/small_uint.h ../include/endianness.h ../include/macros.h \ ../include/ip_address.h ../include/pdu_option.h ../include/tcp.h \ - ../include/rawpdu.h ../include/pdu.h + ../include/rawpdu.h ../include/pdu.h ../include/packet.h \ + ../include/cxxstd.h ../include/timestamp.h ../include/ip.h: @@ -1676,6 +1679,12 @@ src/pdu.o: src/pdu.cpp ../include/ip.h ../include/pdu.h \ ../include/rawpdu.h: ../include/pdu.h: + +../include/packet.h: + +../include/cxxstd.h: + +../include/timestamp.h: src/radiotap.o: src/radiotap.cpp ../include/radiotap.h \ ../include/macros.h ../include/pdu.h ../include/endianness.h \ ../include/network_interface.h ../include/hw_address.h \ diff --git a/tests/src/pdu.cpp b/tests/src/pdu.cpp index 5d1e6c4..1d29fca 100644 --- a/tests/src/pdu.cpp +++ b/tests/src/pdu.cpp @@ -6,6 +6,7 @@ #include "tcp.h" #include "rawpdu.h" #include "pdu.h" +#include "packet.h" using namespace std; using namespace Tins; @@ -30,3 +31,26 @@ TEST_F(PDUTest, OperatorConcat) { EXPECT_TRUE(std::equal(raw_payload.begin(), raw_payload.end(), raw->payload().begin())); } +TEST_F(PDUTest, OperatorConcatOnPointers) { + std::string raw_payload = "Test"; + IP ip = IP("192.168.0.1") / TCP(22, 52); + TCP *tcp = ip.find_pdu(); + ASSERT_TRUE(tcp); + tcp /= RawPDU(raw_payload); + RawPDU *raw = ip.find_pdu(); + ASSERT_TRUE(raw); + ASSERT_EQ(raw->payload_size(), raw_payload.size()); + EXPECT_TRUE(std::equal(raw->payload().begin(), raw->payload().end(), raw_payload.begin())); +} + +TEST_F(PDUTest, OperatorConcatOnPacket) { + std::string raw_payload = "Test"; + Packet packet = IP("192.168.0.1") / TCP(22, 52); + TCP *tcp = packet.pdu()->find_pdu(); + ASSERT_TRUE(tcp); + tcp /= RawPDU(raw_payload); + RawPDU *raw = packet.pdu()->find_pdu(); + ASSERT_TRUE(raw); + ASSERT_EQ(raw->payload_size(), raw_payload.size()); + EXPECT_TRUE(std::equal(raw->payload().begin(), raw->payload().end(), raw_payload.begin())); +}