From 43217549eba137eaf8c3e79543d9b08a48f4bdc3 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Tue, 2 Jun 2015 21:52:40 -0700 Subject: [PATCH] Fix example compilation on Windows. Fixes #75. --- examples/CMakeLists.txt | 21 ++++++++++++++------- examples/arpspoofing.cpp | 13 +++++++++++-- examples/dns_stats.cpp | 19 ++++++++++++------- examples/traceroute.cpp | 10 ++++------ 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 59882bd..6cf51ea 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -3,7 +3,10 @@ FIND_PACKAGE(Threads QUIET) IF(libtins_FOUND) SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples) - INCLUDE_DIRECTORIES(${LIBTINS_INCLUDE_DIRS}) + INCLUDE_DIRECTORIES( + ${LIBTINS_INCLUDE_DIRS} + ${PCAP_INCLUDE_DIR} + ) LINK_LIBRARIES(${LIBTINS_LIBRARIES}) IF(HAVE_CXX11) @@ -32,7 +35,6 @@ IF(libtins_FOUND) ADD_EXECUTABLE(arpmonitor EXCLUDE_FROM_ALL arpmonitor.cpp) ADD_EXECUTABLE(dns_queries EXCLUDE_FROM_ALL dns_queries.cpp) ADD_EXECUTABLE(dns_spoof EXCLUDE_FROM_ALL dns_spoof.cpp) - ADD_EXECUTABLE(dns_stats EXCLUDE_FROM_ALL dns_stats.cpp) ADD_EXECUTABLE(wps_detect EXCLUDE_FROM_ALL wps_detect.cpp) ENDIF(HAVE_CXX11) @@ -41,14 +43,19 @@ IF(libtins_FOUND) if(THREADS_FOUND) IF(HAVE_CXX11) ADD_EXECUTABLE(traceroute EXCLUDE_FROM_ALL traceroute.cpp) + ADD_EXECUTABLE(dns_stats EXCLUDE_FROM_ALL dns_stats.cpp) TARGET_LINK_LIBRARIES(traceroute ${CMAKE_THREAD_LIBS_INIT}) + TARGET_LINK_LIBRARIES(dns_stats ${CMAKE_THREAD_LIBS_INIT}) ENDIF(HAVE_CXX11) - - ADD_EXECUTABLE(portscan EXCLUDE_FROM_ALL portscan.cpp) - TARGET_LINK_LIBRARIES(portscan ${CMAKE_THREAD_LIBS_INIT}) - ELSE(THREADS_FOUND) + IF(WIN32) + MESSAGE(WARNING "Disabling portscan example since it doesn't compile on Windows.") + ELSE() + ADD_EXECUTABLE(portscan EXCLUDE_FROM_ALL portscan.cpp) + TARGET_LINK_LIBRARIES(portscan ${CMAKE_THREAD_LIBS_INIT}) + ENDIF() + ELSE() MESSAGE(WARNING "Disabling portscan and traceroute examples since pthreads library was not found.") - ENDIF(THREADS_FOUND) + ENDIF() ELSE(libtins_FOUND) MESSAGE( WARNING diff --git a/examples/arpspoofing.cpp b/examples/arpspoofing.cpp index f6b2b39..879fcf1 100644 --- a/examples/arpspoofing.cpp +++ b/examples/arpspoofing.cpp @@ -32,7 +32,12 @@ #include #include #include -#include +#ifdef _WIN32 + #define WIN32_LEAN_AND_MEAN + #include +#else + #include +#endif // _WIN32 #include #include #include @@ -78,7 +83,11 @@ void do_arp_spoofing(NetworkInterface iface, IPv4Address gw, IPv4Address victim, // Just send them once every 5 seconds. sender.send(to_gw, iface); sender.send(to_victim, iface); - sleep(5); + #ifdef _WIN32 + Sleep(5); + #else + sleep(5); + #endif } } diff --git a/examples/dns_stats.cpp b/examples/dns_stats.cpp index 0a23d33..ee5f29f 100644 --- a/examples/dns_stats.cpp +++ b/examples/dns_stats.cpp @@ -96,8 +96,8 @@ public: } private: using packet_info = std::tuple; - using clock_type = std::chrono::steady_clock; - using time_point_type = std::chrono::time_point; + using clock_type = std::chrono::system_clock; + using time_point_type = clock_type::time_point; bool callback(const PDU& pdu); static packet_info make_packet_info(const PDU& pdu, const DNS& dns); @@ -161,15 +161,20 @@ auto dns_monitor::make_packet_info(const PDU& pdu, const DNS& dns) -> packet_inf } int main(int argc, char *argv[]) { - if(argc != 2) { - std::cout << "Usage: " << *argv << " \n"; - return 1; + std::string iface; + if (argc == 2) { + // Use the provided interface + iface = argv[1]; + } + else { + // Use the default interface + iface = NetworkInterface::default_interface().name(); } try { SnifferConfiguration config; config.set_promisc_mode(true); - config.set_filter("udp and dst port 53"); - Sniffer sniffer(argv[1], config); + config.set_filter("udp and port 53"); + Sniffer sniffer(iface, config); dns_monitor monitor; std::thread thread( [&]() { diff --git a/examples/traceroute.cpp b/examples/traceroute.cpp index 7a39016..12fbc12 100644 --- a/examples/traceroute.cpp +++ b/examples/traceroute.cpp @@ -46,10 +46,9 @@ public: : iface(interface), addr(address) { } result_type trace() { - // ICMPs that aren't sent from us. SnifferConfiguration config; - config.set_snap_len(500); config.set_promisc_mode(false); + // ICMPs that aren't sent from us. config.set_filter( "ip proto \\icmp and not src host " + iface.addresses().ip_addr.to_string()); Sniffer sniffer(iface.name(), config); @@ -65,10 +64,9 @@ public: running = true; // Start the sniff thread std::thread sniff_thread( - &Sniffer::sniff_loop, - &sniffer, - handler, - 0 + [&]() { + sniffer.sniff_loop(handler); + } ); send_packets(sender); sniff_thread.join();