1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Added Timestamp conversion to std::chrono::microseconds. BaseSniffer is now movable.

This commit is contained in:
Matias Fontanini
2012-11-23 20:30:16 -03:00
parent b74a353c17
commit 75a4bbfed6
8 changed files with 120 additions and 4 deletions

16
configure vendored
View File

@@ -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'

View File

@@ -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)

41
include/cxxstd.h Normal file
View File

@@ -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

View File

@@ -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.

View File

@@ -31,6 +31,10 @@
#define TINS_TIMESTAMP_H
#include <sys/time.h>
#include "cxxstd.h"
#if TINS_IS_CXX11
#include <chrono>
#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;
};

View File

@@ -58,8 +58,8 @@ void PacketWriter::write(PDU &pdu) {
gettimeofday(&tm, 0);
struct pcap_pkthdr header = {
tm,
buffer.size(),
buffer.size()
static_cast<bpf_u_int32>(buffer.size()),
static_cast<bpf_u_int32>(buffer.size())
};
pcap_dump((u_char*)dumper, &header, &buffer[0]);
}

View File

@@ -27,6 +27,7 @@
*
*/
#include <algorithm>
#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)

View File

@@ -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<PDU> response(sender.send_recv(packet));
#if TINS_IS_CXX11
std::unique_ptr<PDU> response(sender.send_recv(packet));
#else
std::auto_ptr<PDU> response(sender.send_recv(packet));
#endif
if(response.get()) {
ARP *arp_resp = response->find_pdu<ARP>();
if(arp_resp)