From 75a4bbfed624aec7c1c516e3c7e07366c97e200c Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Fri, 23 Nov 2012 20:30:16 -0300 Subject: [PATCH] Added Timestamp conversion to std::chrono::microseconds. BaseSniffer is now movable. --- configure | 16 ++++++++++++++++ configure.ac | 8 ++++++++ include/cxxstd.h | 41 +++++++++++++++++++++++++++++++++++++++++ include/sniffer.h | 15 +++++++++++++++ include/timestamp.h | 13 +++++++++++++ src/packet_writer.cpp | 4 ++-- src/sniffer.cpp | 19 +++++++++++++++++++ src/utils.cpp | 8 ++++++-- 8 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 include/cxxstd.h diff --git a/configure b/configure index bc1b944..611e432 100755 --- a/configure +++ b/configure @@ -652,6 +652,7 @@ SHELL' ac_subst_files='' ac_user_opts=' enable_option_checking +enable_c__11 ' ac_precious_vars='build_alias host_alias @@ -1270,6 +1271,12 @@ if test -n "$ac_init_help"; then esac cat <<\_ACEOF +Optional Features: + --disable-option-checking ignore unrecognized --enable/--with options + --disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no) + --enable-FEATURE[=ARG] include FEATURE [ARG=yes] + --enable-c++11 enable C++11 features + Some influential environment variables: CXX C++ compiler command CXXFLAGS C++ compiler flags @@ -2514,6 +2521,15 @@ else CFLAGS="-O3" fi +# Check whether --enable-c++11 was given. +if test "${enable_c__11+set}" = set; then : + enableval=$enable_c__11; CFLAGS="$CFLAGS -std=c++0x" + +fi + + + + ac_ext=cpp ac_cpp='$CXXCPP $CPPFLAGS' diff --git a/configure.ac b/configure.ac index e122e76..64812db 100644 --- a/configure.ac +++ b/configure.ac @@ -10,6 +10,14 @@ else CFLAGS="-O3" fi +AC_ARG_ENABLE( + c++11, + [ --enable-c++11 enable C++11 features], + [CFLAGS="$CFLAGS -std=c++0x"] +) + + + AC_CHECK_HEADERS([pcap.h]) AC_CHECK_LIB(pcap, pcap_loop, [], [AC_MSG_ERROR([pcap library is needed!])]) AC_SUBST(CFLAGS) diff --git a/include/cxxstd.h b/include/cxxstd.h new file mode 100644 index 0000000..e54d7fe --- /dev/null +++ b/include/cxxstd.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2012, Nasel + * 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_CXXSTD_H +#define TINS_CXXSTD_H + +#ifdef __GXX_EXPERIMENTAL_CXX0X__ + #define TINS_CXXSTD_GCC_FIX 1 +#else + #define TINS_CXXSTD_GCC_FIX 0 +#endif // __GXX_EXPERIMENTAL_CXX0X__ + +#define TINS_IS_CXX11 (__cplusplus > 199711L || TINS_CXXSTD_GCC_FIX == 1) + +#endif // TINS_CXXSTD_H diff --git a/include/sniffer.h b/include/sniffer.h index ff6adca..7bcc35f 100644 --- a/include/sniffer.h +++ b/include/sniffer.h @@ -42,6 +42,7 @@ #include "packet.h" #include "loopback.h" #include "dot11.h" +#include "cxxstd.h" namespace Tins { /** @@ -56,6 +57,20 @@ namespace Tins { */ class BaseSniffer { public: + #if TINS_IS_CXX11 + /** + * \brief Move constructor. + * This constructor is available only in C++11. + */ + BaseSniffer(BaseSniffer &&rhs); + + /** + * \brief Move assignment operator. + * This opeartor is available only in C++11. + */ + BaseSniffer& operator=(BaseSniffer &&rhs); + #endif + /** * \brief Sniffer destructor. * This frees all memory used by the pcap handle. diff --git a/include/timestamp.h b/include/timestamp.h index 7ee91ab..8e4443d 100644 --- a/include/timestamp.h +++ b/include/timestamp.h @@ -31,6 +31,10 @@ #define TINS_TIMESTAMP_H #include +#include "cxxstd.h" +#if TINS_IS_CXX11 + #include +#endif namespace Tins { /** @@ -62,6 +66,15 @@ public: suseconds_t microseconds() const { return tv.tv_usec; } + + #if TINS_IS_CXX11 + /** + * Converts this Timestamp to a std::chrono::microseconds + */ + operator std::chrono::microseconds() const { + return std::chrono::microseconds(seconds() * 1000000 + microseconds()); + } + #endif private: timeval tv; }; diff --git a/src/packet_writer.cpp b/src/packet_writer.cpp index b518bd4..9add099 100644 --- a/src/packet_writer.cpp +++ b/src/packet_writer.cpp @@ -58,8 +58,8 @@ void PacketWriter::write(PDU &pdu) { gettimeofday(&tm, 0); struct pcap_pkthdr header = { tm, - buffer.size(), - buffer.size() + static_cast(buffer.size()), + static_cast(buffer.size()) }; pcap_dump((u_char*)dumper, &header, &buffer[0]); } diff --git a/src/sniffer.cpp b/src/sniffer.cpp index 9eaf0aa..72ce62a 100644 --- a/src/sniffer.cpp +++ b/src/sniffer.cpp @@ -27,6 +27,7 @@ * */ +#include #include "sniffer.h" @@ -38,6 +39,24 @@ BaseSniffer::BaseSniffer() : handle(0), mask(0) { actual_filter.bf_insns = 0; } + +#if TINS_IS_CXX11 +BaseSniffer::BaseSniffer(BaseSniffer &&rhs) +{ + *this = std::move(rhs); +} + +BaseSniffer& BaseSniffer::operator=(BaseSniffer &&rhs) +{ + handle = 0; + mask = rhs.mask; + iface_type = rhs.iface_type; + actual_filter.bf_insns = 0; + std::swap(handle, rhs.handle); + std::swap(actual_filter, rhs.actual_filter); + return *this; +} +#endif BaseSniffer::~BaseSniffer() { if(actual_filter.bf_insns) diff --git a/src/utils.cpp b/src/utils.cpp index ec57043..b3b6fe8 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -44,7 +44,7 @@ #include "endianness.h" #include "network_interface.h" #include "packet_sender.h" - +#include "cxxstd.h" using namespace std; @@ -115,7 +115,11 @@ bool Utils::resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip, IPv4Address my_ip; NetworkInterface::Info info(iface.addresses()); EthernetII packet = ARP::make_arp_request(iface, ip, info.ip_addr, info.hw_addr); - std::auto_ptr response(sender.send_recv(packet)); + #if TINS_IS_CXX11 + std::unique_ptr response(sender.send_recv(packet)); + #else + std::auto_ptr response(sender.send_recv(packet)); + #endif if(response.get()) { ARP *arp_resp = response->find_pdu(); if(arp_resp)