diff --git a/src/ip.cpp b/src/ip.cpp index afdadb8..1df5127 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -435,7 +435,7 @@ void IP::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* pare Internals::pdu_type_to_id(inner_pdu()->pdu_type()) ); } - if (!is_fragmented() && new_flag != 0xff) { + if (new_flag != 0xff) { protocol(new_flag); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1cf6832..6ecbe9a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,2 +1,3 @@ INCLUDE_DIRECTORIES(${gtest_INCLUDE_DIRS}) -ADD_SUBDIRECTORY(src) \ No newline at end of file +ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(active_tests) \ No newline at end of file diff --git a/tests/active_tests/CMakeLists.txt b/tests/active_tests/CMakeLists.txt new file mode 100644 index 0000000..130b119 --- /dev/null +++ b/tests/active_tests/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(src) \ No newline at end of file diff --git a/tests/active_tests/include/active_test.h b/tests/active_tests/include/active_test.h new file mode 100644 index 0000000..f1d2e37 --- /dev/null +++ b/tests/active_tests/include/active_test.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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_ACTIVE_TEST_H +#define TINS_ACTIVE_TEST_H + +#include +#include +#include "tins/packet_sender.h" +#include "configuration.h" + +namespace Tins { + +class PDU; + +} // Tins + +class TestFailed : public std::runtime_error { +public: + using std::runtime_error::runtime_error; +}; + +class ActiveTest { +public: + using PacketSenderPtr = std::shared_ptr; + using ConfigurationPtr = std::shared_ptr; + + ActiveTest(const PacketSenderPtr& packet_sender, + const ConfigurationPtr& configuration); + + virtual ~ActiveTest() = default; + + void execute(); + virtual std::string name() const = 0; + bool matches_packet(const Tins::PDU& pdu) const; + virtual void validate_packet(const Tins::PDU& pdu) = 0; +protected: + const PacketSenderPtr& packet_sender() const; + const ConfigurationPtr& configuration() const; + virtual bool test_matches_packet(const Tins::PDU& pdu) const = 0; + virtual void execute_test() = 0; +private: + PacketSenderPtr packet_sender_; + ConfigurationPtr configuration_; +}; + +#endif // TINS_ACTIVE_TEST_H diff --git a/tests/active_tests/include/active_test_runner.h b/tests/active_tests/include/active_test_runner.h new file mode 100644 index 0000000..dfa8705 --- /dev/null +++ b/tests/active_tests/include/active_test_runner.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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_ACTIVE_TEST_RUNNER_H +#define TINS_ACTIVE_TEST_RUNNER_H + +#include +#include +#include "active_test.h" +#include "configuration.h" +#include "packet_capturer.h" + +class ActiveTestRunner { +public: + ActiveTestRunner(const Configuration& configuration); + + template + void add_test(); + + bool validate_tests(); + void run(); +private: + using ConfigurationPtr = ActiveTest::ConfigurationPtr; + using ActiveTestPtr = std::unique_ptr; + + template + void add_tests() { } + + void do_run(); + + ConfigurationPtr configuration_; + ActiveTest::PacketSenderPtr packet_sender_; + PacketCapturer capturer_; + std::vector tests_; +}; + +template +void ActiveTestRunner::add_test() { + tests_.emplace_back(new T(packet_sender_, configuration_)); +} + +#endif // TINS_ACTIVE_TEST_RUNNER_H diff --git a/tests/active_tests/include/configuration.h b/tests/active_tests/include/configuration.h new file mode 100644 index 0000000..0452236 --- /dev/null +++ b/tests/active_tests/include/configuration.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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_ACTIVE_TEST_CONFIGURATION_H +#define TINS_ACTIVE_TEST_CONFIGURATION_H + +#include +#include +#include "tins/network_interface.h" + +class Configuration { +public: + void interface(const Tins::NetworkInterface& interface); + void source_port(uint16_t value); + void destination_port(uint16_t value); + + const Tins::NetworkInterface& interface() const; + uint16_t source_port() const; + uint16_t destination_port() const; +private: + Tins::NetworkInterface interface_; + uint16_t source_port_ = 0; + uint16_t destination_port_ = 0; +}; + +#endif // TINS_ACTIVE_TEST_CONFIGURATION_H diff --git a/tests/active_tests/include/ipv4_tests.h b/tests/active_tests/include/ipv4_tests.h new file mode 100644 index 0000000..78c2362 --- /dev/null +++ b/tests/active_tests/include/ipv4_tests.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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_IPV4_TESTS_H +#define TINS_IPV4_TESTS_H + +#include +#include +#include "active_test.h" + +class IPv4SourceAddressTest : public ActiveTest { +public: + using ActiveTest::ActiveTest; + + std::string name() const; +private: + void execute_test(); + void validate_packet(const Tins::PDU& pdu); + bool test_matches_packet(const Tins::PDU& pdu) const; +}; + +class IPv4FragmentationTest : public ActiveTest { +public: + using ActiveTest::ActiveTest; + + std::string name() const; +private: + void execute_test(); + void validate_packet(const Tins::PDU& pdu); + bool test_matches_packet(const Tins::PDU& pdu) const; +}; + +#endif // TINS_IPV4_TESTS_H diff --git a/tests/active_tests/include/packet_capturer.h b/tests/active_tests/include/packet_capturer.h new file mode 100644 index 0000000..83134d7 --- /dev/null +++ b/tests/active_tests/include/packet_capturer.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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_PACKET_CAPTURER_H +#define TINS_PACKET_CAPTURER_H + +#include +#include +#include +#include +#include +#include "tins/sniffer.h" +#include "configuration.h" + +class PacketCapturer { +public: + using PacketPtr = std::unique_ptr; + using PacketStorage = std::vector; + + PacketCapturer(const Configuration& configuration); + + void start_capture(); + void stop_capture(); + + PacketStorage captured_packets(); +private: + bool callback(const Tins::PDU& pdu); + std::string make_filter(const Configuration& configuration) const; + + std::unique_ptr sniffer_; + std::thread sniffer_thread_; + PacketStorage storage_; + std::atomic running_; +}; + +#endif // TINS_PACKET_CAPTURER_H diff --git a/tests/active_tests/src/CMakeLists.txt b/tests/active_tests/src/CMakeLists.txt new file mode 100644 index 0000000..23f235b --- /dev/null +++ b/tests/active_tests/src/CMakeLists.txt @@ -0,0 +1,17 @@ +FILE(GLOB SOURCES "*.cpp") + +INCLUDE_DIRECTORIES( + ${CMAKE_CURRENT_SOURCE_DIR}/../include +) + +# Find pthread library +FIND_PACKAGE(Threads REQUIRED) + +# Link against GoogleTest, libtins and pthread. +LINK_LIBRARIES( + tins + ${CMAKE_THREAD_LIBS_INIT} + ${PCAP_LIBRARY} +) + +ADD_EXECUTABLE(active_tester EXCLUDE_FROM_ALL ${SOURCES}) \ No newline at end of file diff --git a/tests/active_tests/src/active_test.cpp b/tests/active_tests/src/active_test.cpp new file mode 100644 index 0000000..628a89d --- /dev/null +++ b/tests/active_tests/src/active_test.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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 +#include "tins/exceptions.h" +#include "tins/pdu.h" +#include "active_test.h" + +using std::cout; +using std::endl; + +using Tins::PDU; +using Tins::pdu_not_found; +using Tins::option_not_found; + +ActiveTest::ActiveTest(const PacketSenderPtr& packet_sender, + const ConfigurationPtr& configuration) +: packet_sender_(packet_sender), configuration_(configuration) { + +} + +void ActiveTest::execute() { + execute_test(); +} + +bool ActiveTest::matches_packet(const PDU& pdu) const { + try { + return test_matches_packet(pdu); + } + catch (pdu_not_found&) { + return false; + } + catch (option_not_found&) { + return false; + } +} + +const ActiveTest::PacketSenderPtr& ActiveTest::packet_sender() const { + return packet_sender_; +} + +const ActiveTest::ConfigurationPtr& ActiveTest::configuration() const { + return configuration_; +} + diff --git a/tests/active_tests/src/active_test_runner.cpp b/tests/active_tests/src/active_test_runner.cpp new file mode 100644 index 0000000..5fde4ec --- /dev/null +++ b/tests/active_tests/src/active_test_runner.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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 +#include +#include +#include +#include +#include "active_test_runner.h" + +using std::make_shared; +using std::set; +using std::string; +using std::exception; +using std::cout; +using std::cerr; +using std::endl; +using std::this_thread::sleep_for; +using std::chrono::seconds; + +using Tins::PacketSender; + +ActiveTestRunner::ActiveTestRunner(const Configuration& configuration) +: configuration_(make_shared(configuration)), + packet_sender_(make_shared()), + capturer_(*configuration_) { + packet_sender_->default_interface(configuration.interface()); +} + +bool ActiveTestRunner::validate_tests() { + set names; + for (const auto& test : tests_) { + if (names.insert(test->name()).second == false) { + return false; + } + } + return true; +} + +void ActiveTestRunner::run() { + try { + do_run(); + } + catch (exception& ex) { + cerr << "[-] Caught exception while running: " << ex.what() << endl; + } +} + +void ActiveTestRunner::do_run() { + string prefix = "[runner] "; + cout << prefix << "Starting capture on interface " << configuration_->interface() << endl; + capturer_.start_capture(); + cout << prefix << "Executing " << tests_.size() << " tests" << endl; + for (auto& test : tests_) { + cout << prefix << "Sending packet for " << test->name() << " test" << endl; + test->execute(); + } + cout << prefix << "Done executing tests. Sleeping for a second" << endl; + sleep_for(seconds(1)); + cout << prefix << "Stopping capture" << endl; + capturer_.stop_capture(); + cout << prefix << "Capture stopped" << endl; + + auto packets = capturer_.captured_packets(); + cout << prefix << "Captured " << packets.size() << " packets" << endl; + for (const auto& test : tests_) { + prefix = "[" + test->name() + "] "; + size_t i = 0; + while (i < packets.size() && !test->matches_packet(*packets[i])) { + ++i; + } + if (i == packets.size()) { + cout << prefix << "ERROR: Packet was not captured" << endl; + } + else { + try { + test->validate_packet(*packets[i]); + cout << prefix << "OK" << endl; + } + catch (TestFailed& ex) { + cout << prefix << "ERROR: " << ex.what() << endl; + } + packets.erase(packets.begin() + i); + } + } +} diff --git a/tests/active_tests/src/configuration.cpp b/tests/active_tests/src/configuration.cpp new file mode 100644 index 0000000..c57f87d --- /dev/null +++ b/tests/active_tests/src/configuration.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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 "configuration.h" + +using std::string; + +using Tins::NetworkInterface; + +void Configuration::interface(const NetworkInterface& interface) { + interface_ = interface; +} + +void Configuration::source_port(uint16_t value) { + source_port_ = value; +} + +void Configuration::destination_port(uint16_t value) { + destination_port_ = value; +} + +const NetworkInterface& Configuration::interface() const { + return interface_; +} + +uint16_t Configuration::source_port() const { + return source_port_; +} + +uint16_t Configuration::destination_port() const { + return destination_port_; +} + diff --git a/tests/active_tests/src/ipv4_tests.cpp b/tests/active_tests/src/ipv4_tests.cpp new file mode 100644 index 0000000..3cf6514 --- /dev/null +++ b/tests/active_tests/src/ipv4_tests.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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 +#include "ipv4_tests.h" +#include "tins/rawpdu.h" +#include "tins/ethernetII.h" +#include "tins/ip.h" +#include "tins/udp.h" +#include "tins/network_interface.h" + +using std::string; +using std::ostringstream; + +using Tins::IP; +using Tins::UDP; +using Tins::PDU; +using Tins::RawPDU; +using Tins::EthernetII; +using Tins::PacketSender; +using Tins::NetworkInterface; + +// Source address test + +string IPv4SourceAddressTest::name() const { + return "ipv4_source_address"; +} + +void IPv4SourceAddressTest::execute_test() { + PacketSender& sender = *packet_sender(); + Configuration& config = *configuration(); + auto packet = IP("8.8.8.8"); + packet /= UDP(config.destination_port(), config.source_port()); + packet /= RawPDU(name()); + sender.send(packet); +} + +void IPv4SourceAddressTest::validate_packet(const PDU& pdu) { + const IP& ip = pdu.rfind_pdu(); + const NetworkInterface& iface = configuration()->interface(); + // The source address should be the same as the default interface's + if (iface.ipv4_address() != ip.src_addr()) { + ostringstream oss; + oss << "Source address should be " << iface.ipv4_address() + << " but is " << ip.src_addr(); + throw TestFailed(oss.str()); + } +} + +bool IPv4SourceAddressTest::test_matches_packet(const PDU& pdu) const { + const string& test_name = name(); + RawPDU::payload_type expected_payload(test_name.begin(), test_name.end()); + return pdu.rfind_pdu().payload() == expected_payload; +} + +// Fragmentation test + +string IPv4FragmentationTest::name() const { + return "ipv4_fragmentation"; +} + +void IPv4FragmentationTest::execute_test() { + PacketSender& sender = *packet_sender(); + Configuration& config = *configuration(); + auto packet = IP("8.8.8.8"); + packet /= UDP(config.destination_port(), config.source_port()); + packet /= RawPDU(name()); + + IP& ip = packet.rfind_pdu(); + ip.fragment_offset(100); + ip.flags(IP::MORE_FRAGMENTS); + sender.send(packet); +} + +void IPv4FragmentationTest::validate_packet(const PDU& pdu) { + ostringstream oss; + const IP& ip = pdu.rfind_pdu(); + if (ip.fragment_offset() != 100) { + oss << "Expected fragment offset 100 but got " << ip.fragment_offset(); + throw TestFailed(oss.str()); + } + if (ip.flags() != IP::MORE_FRAGMENTS) { + oss << "Expected MORE_FRAGMENT flags but got " << (unsigned)ip.flags(); + throw TestFailed(oss.str()); + } +} + +bool IPv4FragmentationTest::test_matches_packet(const PDU& pdu) const { + string test_name = name(); + const RawPDU& raw = pdu.rfind_pdu(); + string payload(raw.payload().begin(), raw.payload().end()); + return payload.find(test_name) != string::npos; +} + diff --git a/tests/active_tests/src/main.cpp b/tests/active_tests/src/main.cpp new file mode 100644 index 0000000..f18ba39 --- /dev/null +++ b/tests/active_tests/src/main.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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 +#include +#include "configuration.h" +#include "active_test_runner.h" +#include "ipv4_tests.h" +#include "tins/network_interface.h" + +using std::cerr; +using std::endl; +using std::exception; +using std::runtime_error; + +using Tins::NetworkInterface; + +int main() { + try { + Configuration config; + config.source_port(1234); + config.destination_port(4321); + config.interface(NetworkInterface::default_interface()); + + ActiveTestRunner runner(config); + runner.add_test(); + runner.add_test(); + + if (!runner.validate_tests()) { + throw runtime_error("Test validation failed"); + } + runner.run(); + } + catch (exception& ex) { + cerr << "Error: " << ex.what() << endl; + return 1; + } +} \ No newline at end of file diff --git a/tests/active_tests/src/packet_capturer.cpp b/tests/active_tests/src/packet_capturer.cpp new file mode 100644 index 0000000..0045e8a --- /dev/null +++ b/tests/active_tests/src/packet_capturer.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2016, Matias Fontanini + * 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 +#include +#include +#include +#include "packet_capturer.h" + +using std::string; +using std::thread; +using std::move; +using std::bind; +using std::condition_variable; +using std::unique_lock; +using std::lock_guard; +using std::mutex; +using std::ostringstream; + +using Tins::PDU; +using Tins::Sniffer; +using Tins::SnifferConfiguration; + +PacketCapturer::PacketCapturer(const Configuration& configuration) { + + SnifferConfiguration sniffer_config; + sniffer_config.set_filter(make_filter(configuration)); + sniffer_config.set_immediate_mode(true); + sniffer_.reset(new Sniffer(configuration.interface().name(), sniffer_config)); +} + +void PacketCapturer::start_capture() { + using std::placeholders::_1; + + mutex mtx; + condition_variable cond; + bool started = false; + + running_ = true; + sniffer_thread_ = thread([&]() { + { + lock_guard _(mtx); + started = true; + cond.notify_one(); + } + sniffer_->sniff_loop(bind(&PacketCapturer::callback, this, _1)); + }); + + unique_lock locker(mtx); + while (!started) { + cond.wait(locker); + } +} + +void PacketCapturer::stop_capture() { + running_ = false; + sniffer_->stop_sniff(); + sniffer_thread_.join(); +} + +PacketCapturer::PacketStorage PacketCapturer::captured_packets() { + return move(storage_); +} + +bool PacketCapturer::callback(const PDU& pdu) { + storage_.emplace_back(pdu.clone()); + return running_; +} + +string PacketCapturer::make_filter(const Configuration& configuration) const { + ostringstream oss; + oss << "((tcp or udp) and src port " << configuration.source_port() + << " and dst port " << configuration.destination_port() << ") or icmp" + // Fragmentted IP packets + << " or (ip[6:2] & 0x1fff) > 0"; + return oss.str(); +} +