diff --git a/CMakeLists.txt b/CMakeLists.txt index 88f641a..76abe8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,12 +37,24 @@ SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") # Look for libpcap FIND_PACKAGE(PCAP REQUIRED) +# Set some Windows specific flags +IF(WIN32) + # We need to link against these libs + SET(LIBTINS_OS_LIBS Ws2_32.lib Iphlp/api.lib) + + # Add the NOMINMAX macro to avoid Windows' min and max macros. + # While compiling on windows, for some reason, WIN32 is not defined, + # maybe we could fix this later, but it's OK for now. + ADD_DEFINITIONS(-DNOMINMAX -DWIN32) +ENDIF(WIN32) + # Compilation options OPTION(LIBTINS_ENABLE_CXX11 "Compile libtins with c++11 features" OFF) IF(LIBTINS_ENABLE_CXX11) SET(HAVE_CXX11 ON) INCLUDE(CheckCXX11Features) IF(CXX11_COMPILER_FLAGS AND HAS_CXX11_NULLPTR AND HAS_CXX11_RVALUE_REFERENCES) + MESSAGE(STATUS "Enabling C++11 features") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX11_COMPILER_FLAGS}") ELSE(CXX11_COMPILER_FLAGS AND HAS_CXX11_NULLPTR AND HAS_CXX11_RVALUE_REFERENCES) MESSAGE(FATAL_ERROR "C++11 features requested but the compiler does not support them.") @@ -51,8 +63,8 @@ ELSE(LIBTINS_ENABLE_CXX11) MESSAGE( WARNING "Disabling C++11 features. Use LIBTINS_ENABLE_CXX11=1 to enable them. " - "If your compiler is fairly new, you should enable this option, as it " - "increases the library performance") + "Unless you are using an old compiler, you should enable this option, " + "as it increases the library's performance") ENDIF(LIBTINS_ENABLE_CXX11) OPTION(LIBTINS_ENABLE_DOT11 "Compile libtins with IEEE 802.11 support" ON) diff --git a/cmake/Modules/FindPCAP.cmake b/cmake/Modules/FindPCAP.cmake index e5049bf..b3a3075 100644 --- a/cmake/Modules/FindPCAP.cmake +++ b/cmake/Modules/FindPCAP.cmake @@ -29,7 +29,7 @@ find_path(PCAP_INCLUDE_DIR ) find_library(PCAP_LIBRARY - NAMES pcap + NAMES pcap wpcap HINTS ${PCAP_ROOT_DIR}/lib ) @@ -39,9 +39,9 @@ find_package_handle_standard_args(PCAP DEFAULT_MSG PCAP_INCLUDE_DIR ) -include(CheckCSourceCompiles) +include(CheckCXXSourceCompiles) set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY}) -check_c_source_compiles("int main() { return 0; }" PCAP_LINKS_SOLO) +check_cxx_source_compiles("int main() { return 0; }" PCAP_LINKS_SOLO) set(CMAKE_REQUIRED_LIBRARIES) # check if linking against libpcap also needs to link against a thread library @@ -49,18 +49,18 @@ if (NOT PCAP_LINKS_SOLO) find_package(Threads) if (THREADS_FOUND) set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) - check_c_source_compiles("int main() { return 0; }" PCAP_NEEDS_THREADS) + check_cxx_source_compiles("int main() { return 0; }" PCAP_NEEDS_THREADS) set(CMAKE_REQUIRED_LIBRARIES) - endif () + endif (THREADS_FOUND) if (THREADS_FOUND AND PCAP_NEEDS_THREADS) set(_tmp ${PCAP_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) list(REMOVE_DUPLICATES _tmp) set(PCAP_LIBRARY ${_tmp} CACHE STRING "Libraries needed to link against libpcap" FORCE) - else () + else (THREADS_FOUND AND PCAP_NEEDS_THREADS) message(FATAL_ERROR "Couldn't determine how to link against libpcap") - endif () -endif () + endif (THREADS_FOUND AND PCAP_NEEDS_THREADS) +endif (NOT PCAP_LINKS_SOLO) include(CheckFunctionExists) set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIBRARY}) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8d2cca1..28db244 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,4 +1,5 @@ FIND_PACKAGE(libtins QUIET) +FIND_PACKAGE(Threads QUIET) IF(libtins_FOUND) SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples) @@ -39,7 +40,11 @@ IF(libtins_FOUND) ADD_EXECUTABLE(portscan EXCLUDE_FROM_ALL portscan.cpp) ADD_EXECUTABLE(traceroute EXCLUDE_FROM_ALL traceroute.cpp) - TARGET_LINK_LIBRARIES(portscan pthread) + if(THREADS_FOUND) + TARGET_LINK_LIBRARIES(portscan ${CMAKE_THREAD_LIBS_INIT}) + ELSE(THREADS_FOUND) + MESSAGE(WARNING "Disabling portscan example since pthreads library was not found.") + ENDIF(THREADS_FOUND) ELSE(libtins_FOUND) MESSAGE( WARNING diff --git a/include/packet.h b/include/packet.h index 7a1fec9..1bc580d 100644 --- a/include/packet.h +++ b/include/packet.h @@ -286,7 +286,7 @@ public: * \return true if pdu() == nullptr, false otherwise. */ operator bool() const { - return bool(pdu_); + return pdu_ ? true : false; } /** diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ad41217..b74c59a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -58,7 +58,7 @@ ADD_LIBRARY( dot11/dot11_control.cpp ) -TARGET_LINK_LIBRARIES(tins ${PCAP_LIBRARY} ${OPENSSL_LIBRARIES}) +TARGET_LINK_LIBRARIES(tins ${PCAP_LIBRARY} ${OPENSSL_LIBRARIES} ${LIBTINS_OS_LIBS}) SET_TARGET_PROPERTIES(tins PROPERTIES OUTPUT_NAME tins ) SET_TARGET_PROPERTIES(tins PROPERTIES VERSION ${LIBTINS_CPP_VERSION} SOVERSION ${LIBTINS_CPP_VERSION} ) diff --git a/src/sniffer.cpp b/src/sniffer.cpp index d446f61..aee0987 100644 --- a/src/sniffer.cpp +++ b/src/sniffer.cpp @@ -28,6 +28,7 @@ */ #include +#include #include "sniffer.h" #include "dot11/dot11_base.h" #include "ethernetII.h" @@ -196,14 +197,17 @@ void BaseSniffer::set_timeout(int ms) { // ****************************** Sniffer ****************************** pcap_t * -pcap_open_live_extended(const char *source, int snaplen, int promisc, int to_ms, int rfmon, char *errbuf) +pcap_open_live_extended(const char *source, int snaplen, int promisc, int to_ms, int rfmon, std::string& error) { pcap_t *p; + char errbuf[PCAP_ERRBUF_SIZE]; int status; p = pcap_create(source, errbuf); - if (p == NULL) + if (p == NULL) { + error = errbuf; return (NULL); + } status = pcap_set_snaplen(p, snaplen); if (status < 0) goto fail; @@ -213,18 +217,24 @@ pcap_open_live_extended(const char *source, int snaplen, int promisc, int to_ms, status = pcap_set_timeout(p, to_ms); if (status < 0) goto fail; + + #ifndef WIN32 if(pcap_can_set_rfmon(p) == 1) { status = pcap_set_rfmon(p, rfmon); if (status < 0) goto fail; } + #endif // WIN32 + status = pcap_activate(p); if (status < 0) goto fail; return (p); fail: - snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", source, pcap_geterr(p)); + std::ostringstream oss; + oss << source << ": " << pcap_geterr(p); + error = oss.str(); pcap_close(p); return (NULL); } @@ -250,9 +260,17 @@ void Sniffer::init_sniffer(const std::string &device, unsigned max_packet_size, ip = 0; if_mask = 0; } - pcap_t *phandle = pcap_open_live_extended(device.c_str(), max_packet_size, promisc, 1000, rfmon, error); + std::string string_error; + pcap_t *phandle = pcap_open_live_extended( + device.c_str(), + max_packet_size, + promisc, + 1000, + rfmon, + string_error + ); if(!phandle) - throw runtime_error(error); + throw runtime_error(string_error); init(phandle, filter, if_mask); } diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt index a58b621..fee1f69 100644 --- a/tests/src/CMakeLists.txt +++ b/tests/src/CMakeLists.txt @@ -1,9 +1,17 @@ # Use libtins' include directories + test include directories INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include/ ../include/) +# Find pthread library +FIND_PACKAGE(Threads REQUIRED) + # Link against GoogleTest, libtins and pthread. # Pthread is required by GoogleTest -LINK_LIBRARIES(${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARY} tins pthread) +LINK_LIBRARIES( + ${GTEST_LIBRARIES} + ${GTEST_MAIN_LIBRARY} + tins + ${CMAKE_THREAD_LIBS_INIT} +) # Add tests target ADD_CUSTOM_TARGET(