From 799ba2b4b6deba53affd444bf49df97271e9ebdd Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Tue, 21 Mar 2017 19:04:33 -0700 Subject: [PATCH] Allow disabling pcap packet capture --- CMakeLists.txt | 18 +++++++++++++---- include/tins/config.h.in | 3 +++ include/tins/internals.h | 3 +++ include/tins/offline_packet_filter.h | 8 +++++++- include/tins/packet_writer.h | 11 +++++++--- include/tins/pktap.h | 5 +++++ include/tins/ppi.h | 7 ++++++- include/tins/sniffer.h | 8 ++++++-- include/tins/tcp_stream.h | 7 ++++++- src/CMakeLists.txt | 30 +++++++++++++++++++--------- src/internals.cpp | 6 +++++- tests/CMakeLists.txt | 2 +- tests/src/CMakeLists.txt | 9 ++++++--- 13 files changed, 91 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ef1961..a2964b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,8 +49,14 @@ SET(LIBTINS_VERSION "${LIBTINS_VERSION_MAJOR}.${LIBTINS_VERSION_MINOR}") # Required Packages SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/") +# Allow disabling packet capture mechanism +OPTION(LIBTINS_ENABLE_PCAP "Enable capturing packets via libpcap" ON) + # Look for libpcap -FIND_PACKAGE(PCAP REQUIRED) +IF(LIBTINS_ENABLE_PCAP) + FIND_PACKAGE(PCAP REQUIRED) + SET(TINS_HAVE_PCAP ON) +ENDIF() # Set some Windows specific flags IF(WIN32) @@ -194,10 +200,10 @@ ENDIF(WIN32) OPTION(LIBTINS_USE_PCAP_SENDPACKET "Use pcap_sendpacket to send l2 packets" ${USE_PCAP_SENDPACKET_DEFAULT}) -IF(LIBTINS_USE_PCAP_SENDPACKET) +IF(LIBTINS_ENABLE_PCAP AND LIBTINS_USE_PCAP_SENDPACKET) SET(TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET ON) MESSAGE(STATUS "Using pcap_sendpacket to send l2 packets.") -ENDIF(LIBTINS_USE_PCAP_SENDPACKET) +ENDIF() # Add a target to generate API documentation using Doxygen FIND_PACKAGE(Doxygen QUIET) @@ -255,8 +261,12 @@ ADD_CUSTOM_TARGET(uninstall # Add subdirectories # ****************** ADD_SUBDIRECTORY(include) -ADD_SUBDIRECTORY(examples) ADD_SUBDIRECTORY(src) +IF(LIBTINS_ENABLE_PCAP) + ADD_SUBDIRECTORY(examples) +ELSE() + MESSAGE(STATUS "Not building examples as pcap support is disabled") +ENDIF() # Only include googletest if the git submodule has been fetched IF(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/googletest/CMakeLists.txt") diff --git a/include/tins/config.h.in b/include/tins/config.h.in index 979163b..a177b9a 100644 --- a/include/tins/config.h.in +++ b/include/tins/config.h.in @@ -28,4 +28,7 @@ /* Have WPA2Decrypter callbacks */ #cmakedefine TINS_HAVE_WPA2_CALLBACKS +/* Have libpcap */ +#cmakedefine TINS_HAVE_PCAP + #endif // TINS_CONFIG_H diff --git a/include/tins/internals.h b/include/tins/internals.h index 4f0d10c..b0b031e 100644 --- a/include/tins/internals.h +++ b/include/tins/internals.h @@ -40,6 +40,7 @@ #include "constants.h" #include "pdu.h" #include "hw_address.h" +#include "macros.h" /** * \cond @@ -124,8 +125,10 @@ PDU* pdu_from_flag(Constants::Ethernet::e flag, const uint8_t* buffer, uint32_t size, bool rawpdu_on_no_match = true); PDU* pdu_from_flag(Constants::IP::e flag, const uint8_t* buffer, uint32_t size, bool rawpdu_on_no_match = true); +#ifdef TINS_HAVE_PCAP PDU* pdu_from_dlt_flag(int flag, const uint8_t* buffer, uint32_t size, bool rawpdu_on_no_match = true); +#endif // TINS_HAVE_PCAP PDU* pdu_from_flag(PDU::PDUType type, const uint8_t* buffer, uint32_t size); Constants::Ethernet::e pdu_flag_to_ether_type(PDU::PDUType flag); diff --git a/include/tins/offline_packet_filter.h b/include/tins/offline_packet_filter.h index 95bcd61..d1c3b07 100644 --- a/include/tins/offline_packet_filter.h +++ b/include/tins/offline_packet_filter.h @@ -32,9 +32,12 @@ #include #include -#include "data_link_type.h" #include "macros.h" +#ifdef TINS_HAVE_PCAP + +#include "data_link_type.h" + namespace Tins { class PDU; @@ -154,6 +157,9 @@ private: mutable bpf_program filter_; std::string string_filter_; }; + } // Tins +#endif // TINS_HAVE_PCAP + #endif // TINS_OFFLINE_PACKET_FILTER_H diff --git a/include/tins/packet_writer.h b/include/tins/packet_writer.h index 75d06be..5daea6d 100644 --- a/include/tins/packet_writer.h +++ b/include/tins/packet_writer.h @@ -33,11 +33,13 @@ #include "utils.h" #include #include -#include -#include "data_link_type.h" #include "macros.h" #include "cxxstd.h" +#ifdef TINS_HAVE_PCAP +#include +#include "data_link_type.h" + struct timeval; namespace Tins { @@ -220,6 +222,9 @@ private: pcap_t* handle_; pcap_dumper_t* dumper_; }; -} + +} // Tins + +#endif // TINS_HAVE_PCAP #endif // TINS_PACKET_WRITER_H diff --git a/include/tins/pktap.h b/include/tins/pktap.h index bf5257b..52f9c25 100644 --- a/include/tins/pktap.h +++ b/include/tins/pktap.h @@ -33,6 +33,9 @@ #include "pdu.h" #include "macros.h" +// This class is only available if pcap is enabled +#ifdef TINS_HAVE_PCAP + namespace Tins { /** @@ -111,4 +114,6 @@ private: } // Tins +#endif // TINS_HAVE_PCAP + #endif // TINS_PKTAP_H diff --git a/include/tins/ppi.h b/include/tins/ppi.h index 6611394..96dd734 100644 --- a/include/tins/ppi.h +++ b/include/tins/ppi.h @@ -35,6 +35,8 @@ #include "endianness.h" #include "small_uint.h" +#ifdef TINS_HAVE_PCAP + namespace Tins { /** @@ -135,6 +137,9 @@ private: ppi_header header_; byte_array data_; }; -} + +} // Tins + +#endif // TINS_HAVE_PCAP #endif // TINS_PPI_H diff --git a/include/tins/sniffer.h b/include/tins/sniffer.h index 96f9f55..7b1cc5e 100644 --- a/include/tins/sniffer.h +++ b/include/tins/sniffer.h @@ -31,8 +31,6 @@ #ifndef TINS_SNIFFER_H #define TINS_SNIFFER_H - -#include #include #include #include @@ -44,6 +42,10 @@ #include "exceptions.h" #include "internals.h" +#ifdef TINS_HAVE_PCAP + +#include + namespace Tins { class SnifferIterator; class SnifferConfiguration; @@ -645,4 +647,6 @@ void Tins::BaseSniffer::sniff_loop(Functor function, uint32_t max_packets) { } // Tins +#endif // TINS_HAVE_PCAP + #endif // TINS_SNIFFER_H diff --git a/include/tins/tcp_stream.h b/include/tins/tcp_stream.h index d2bdbbd..42ae322 100644 --- a/include/tins/tcp_stream.h +++ b/include/tins/tcp_stream.h @@ -36,13 +36,16 @@ #include #include #include -#include "sniffer.h" #include "macros.h" #include "tcp.h" #include "utils.h" #include "ip.h" #include "ip_address.h" +#ifdef TINS_HAVE_PCAP + +#include "sniffer.h" + namespace Tins { class Sniffer; class RawPDU; @@ -385,4 +388,6 @@ bool TCPStreamFollower::callback(PDU& pdu, } // Tins +#endif // TINS_HAVE_PCAP + #endif // TINS_TCP_STREAM_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f24bf8c..63e78af 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,8 +14,7 @@ INCLUDE_DIRECTORIES( ${PCAP_INCLUDE_DIR} ) -ADD_LIBRARY( - tins ${LIBTINS_TYPE} +set(SOURCES arp.cpp bootp.cpp handshake_capturer.cpp @@ -43,19 +42,14 @@ ADD_LIBRARY( loopback.cpp mpls.cpp network_interface.cpp - offline_packet_filter.cpp packet_sender.cpp - packet_writer.cpp - ppi.cpp pdu.cpp - pktap.cpp radiotap.cpp address_range.cpp rawpdu.cpp rsn_information.cpp sll.cpp snap.cpp - sniffer.cpp tcp.cpp tcp_ip/ack_tracker.cpp tcp_ip/flow.cpp @@ -63,7 +57,6 @@ ADD_LIBRARY( tcp_ip/stream.cpp tcp_ip/stream_follower.cpp tcp_ip/stream_identifier.cpp - tcp_stream.cpp timestamp.cpp udp.cpp utils.cpp @@ -77,9 +70,28 @@ ADD_LIBRARY( dot11/dot11_control.cpp ) +SET(PCAP_DEPENDENT_SOURCES + sniffer.cpp + packet_writer.cpp + pktap.cpp + tcp_stream.cpp + offline_packet_filter.cpp + ppi.cpp +) + +IF(LIBTINS_ENABLE_PCAP) + message(STATUS "SETTING TO ${PCAP_DEPENDENT_SOURCES}") + SET(SOURCES ${SOURCES} ${PCAP_DEPENDENT_SOURCES}) +ENDIF() + +ADD_LIBRARY( + tins ${LIBTINS_TYPE} + ${SOURCES} +) + TARGET_LINK_LIBRARIES(tins ${PCAP_LIBRARY} ${OPENSSL_LIBRARIES} ${LIBTINS_OS_LIBS}) -SET_TARGET_PROPERTIES(tins PROPERTIES OUTPUT_NAME tins ) +SET_TARGET_PROPERTIES(tins PROPERTIES OUTPUT_NAME tins) SET_TARGET_PROPERTIES(tins PROPERTIES VERSION ${LIBTINS_VERSION} SOVERSION ${LIBTINS_VERSION} ) # Install instructions for this target diff --git a/src/internals.cpp b/src/internals.cpp index da41033..6c39b22 100644 --- a/src/internals.cpp +++ b/src/internals.cpp @@ -28,7 +28,9 @@ */ #include "internals.h" -#include +#ifdef TINS_HAVE_PCAP + #include +#endif // TINS_HAVE_PCAP #include "ip.h" #include "ethernetII.h" #include "ieee802_3.h" @@ -179,6 +181,7 @@ Tins::PDU* pdu_from_flag(Constants::IP::e flag, return 0; } +#ifdef TINS_HAVE_PCAP PDU* pdu_from_dlt_flag(int flag, const uint8_t* buffer, uint32_t size, @@ -208,6 +211,7 @@ PDU* pdu_from_dlt_flag(int flag, return rawpdu_on_no_match ? new RawPDU(buffer, size) : 0; }; } +#endif // TINS_HAVE_PCAP Tins::PDU* pdu_from_flag(PDU::PDUType type, const uint8_t* buffer, uint32_t size) { switch(type) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d7b5c36..a6e8a06 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,6 @@ SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests) INCLUDE_DIRECTORIES(${gtest_INCLUDE_DIRS}) ADD_SUBDIRECTORY(src) -IF (ENABLE_ACTIVE_TESTS) +IF (ENABLE_ACTIVE_TESTS AND LIBTINS_ENABLE_PCAP) ADD_SUBDIRECTORY(active_tests) ENDIF() \ No newline at end of file diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt index 3daaa91..e2b5dca 100644 --- a/tests/src/CMakeLists.txt +++ b/tests/src/CMakeLists.txt @@ -75,9 +75,7 @@ CREATE_TEST(loopback) CREATE_TEST(matches_response) CREATE_TEST(mpls) CREATE_TEST(network_interface) -CREATE_TEST(offline_packet_filter) CREATE_TEST(pdu) -CREATE_TEST(ppi) CREATE_TEST(pppoe) CREATE_TEST(radiotap) CREATE_TEST(rc4_eapol) @@ -87,11 +85,16 @@ CREATE_TEST(snap) CREATE_TEST(stp) CREATE_TEST(tcp) CREATE_TEST(tcp_ip) -CREATE_TEST(tcp_stream) CREATE_TEST(udp) CREATE_TEST(utils) CREATE_TEST(wep_decrypt) +IF(LIBTINS_ENABLE_PCAP) + CREATE_TEST(offline_packet_filter) + CREATE_TEST(ppi) + CREATE_TEST(tcp_stream) +ENDIF() + IF(LIBTINS_ENABLE_WPA2) CREATE_TEST(wpa2_decrypt) ENDIF() \ No newline at end of file