mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 21:44:26 +01:00
Modified PacketWriter interface to take references instead of pointers.
This commit is contained in:
@@ -22,8 +22,9 @@
|
||||
#ifndef TINS_PACKET_WRITER_H
|
||||
#define TINS_PACKET_WRITER_H
|
||||
|
||||
#include <pcap.h>
|
||||
#include <string>
|
||||
#include <iterator>
|
||||
#include <pcap.h>
|
||||
|
||||
namespace Tins {
|
||||
class PDU;
|
||||
@@ -61,7 +62,7 @@ public:
|
||||
/**
|
||||
* \brief Writes a PDU to this file.
|
||||
*/
|
||||
void write(PDU *pdu);
|
||||
void write(PDU &pdu);
|
||||
|
||||
/**
|
||||
* \brief Writes all the PDUs in the range [start, end)
|
||||
@@ -69,15 +70,30 @@ public:
|
||||
* to be written.
|
||||
* \param end A forward iterator pointing to one past the last
|
||||
* PDU in the range.
|
||||
* \return ForwardIterator which will be a copy of end.
|
||||
*/
|
||||
template<typename ForwardIterator>
|
||||
ForwardIterator write(ForwardIterator start, ForwardIterator end) {
|
||||
void write(ForwardIterator start, ForwardIterator end) {
|
||||
typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
|
||||
typedef derefer<value_type> deref_type;
|
||||
|
||||
while(start != end)
|
||||
write(*start++);
|
||||
return start;
|
||||
write(deref_type::deref(*start++));
|
||||
}
|
||||
private:
|
||||
template<typename T>
|
||||
struct derefer {
|
||||
static T &deref(T &value) {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct derefer<T*> {
|
||||
static T &deref(T *value) {
|
||||
return *value;
|
||||
}
|
||||
};
|
||||
|
||||
// You shall not copy
|
||||
PacketWriter(const PacketWriter&);
|
||||
PacketWriter& operator=(const PacketWriter&);
|
||||
|
||||
@@ -212,6 +212,20 @@ public:
|
||||
*/
|
||||
template<typename DataFunctor, typename EndFunctor>
|
||||
void follow_streams(BaseSniffer &sniffer, DataFunctor data_fun, EndFunctor end_fun);
|
||||
|
||||
/**
|
||||
* \brief Starts following TCP streams.
|
||||
*
|
||||
* The template functor must accept a TCPStream& as argument, which
|
||||
* will point to the stream which has been modified.
|
||||
*
|
||||
* \param sniffer The sniffer which will be used to sniff PDUs.
|
||||
* \param data_fun The function which will be called whenever one of
|
||||
* the peers in a connection sends data.
|
||||
* closed.
|
||||
*/
|
||||
template<typename DataFunctor>
|
||||
void follow_streams(BaseSniffer &sniffer, DataFunctor data_fun);
|
||||
private:
|
||||
typedef std::map<TCPStream::StreamInfo, TCPStream> sessions_type;
|
||||
|
||||
@@ -228,6 +242,7 @@ private:
|
||||
|
||||
template<typename DataFunctor, typename EndFunctor>
|
||||
bool callback(PDU &pdu, const DataFunctor &fun, const EndFunctor &end_fun);
|
||||
static void dummy_function(TCPStream&) { }
|
||||
|
||||
sessions_type sessions;
|
||||
uint64_t last_identifier;
|
||||
@@ -240,6 +255,11 @@ void TCPStreamFollower::follow_streams(BaseSniffer &sniffer, DataFunctor data_fu
|
||||
sniffer.sniff_loop(make_sniffer_handler(&proxy, &proxy_type::callback));
|
||||
}
|
||||
|
||||
template<typename DataFunctor>
|
||||
void TCPStreamFollower::follow_streams(BaseSniffer &sniffer, DataFunctor data_fun) {
|
||||
return follow_streams(sniffer, data_fun, dummy_function);
|
||||
}
|
||||
|
||||
template<typename DataFunctor, typename EndFunctor>
|
||||
bool TCPStreamFollower::callback(PDU &pdu, const DataFunctor &data_fun, const EndFunctor &end_fun) {
|
||||
IP *ip = pdu.find_pdu<IP>();
|
||||
|
||||
@@ -80,7 +80,8 @@ namespace Tins {
|
||||
*/
|
||||
IPv4Address resolve_ip(const std::string &to_resolve);
|
||||
|
||||
/** \brief Resolves the hardware address for a given ip.
|
||||
/**
|
||||
* \brief Resolves the hardware address for a given ip.
|
||||
*
|
||||
* \param iface The interface in which the packet will be sent.
|
||||
* \param ip The ip to resolve, in integer format.
|
||||
@@ -91,6 +92,20 @@ namespace Tins {
|
||||
*/
|
||||
bool resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip,
|
||||
HWAddress<6> *address, PacketSender &sender);
|
||||
|
||||
/**
|
||||
* \brief Resolves the hardware address for a given ip.
|
||||
*
|
||||
* If the address can't be resolved, a std::runtime_error
|
||||
* exception is thrown.
|
||||
*
|
||||
* \param iface The interface in which the packet will be sent.
|
||||
* \param ip The ip to resolve, in integer format.
|
||||
* \param sender The sender to use to send and receive the ARP requests.
|
||||
* \return HWAddress<6> containing the resolved hardware address.
|
||||
*/
|
||||
HWAddress<6> resolve_hwaddr(const NetworkInterface &iface,
|
||||
IPv4Address ip, PacketSender &sender);
|
||||
|
||||
/** \brief List all network interfaces.
|
||||
*
|
||||
|
||||
@@ -44,8 +44,8 @@ PacketWriter::~PacketWriter() {
|
||||
pcap_close(handle);
|
||||
}
|
||||
|
||||
void PacketWriter::write(PDU *pdu) {
|
||||
PDU::serialization_type buffer = pdu->serialize();
|
||||
void PacketWriter::write(PDU &pdu) {
|
||||
PDU::serialization_type buffer = pdu.serialize();
|
||||
struct timeval tm;
|
||||
gettimeofday(&tm, 0);
|
||||
struct pcap_pkthdr header = {
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
#include "utils.h"
|
||||
#include "pdu.h"
|
||||
#include "ip.h"
|
||||
#include "icmp.h"
|
||||
#include "arp.h"
|
||||
#include "endianness.h"
|
||||
#include "network_interface.h"
|
||||
@@ -119,6 +118,20 @@ bool Utils::resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip,
|
||||
return false;
|
||||
}
|
||||
|
||||
HWAddress<6> Utils::resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip, PacketSender &sender)
|
||||
{
|
||||
IPv4Address my_ip;
|
||||
NetworkInterface::Info info(iface.addresses());
|
||||
std::auto_ptr<PDU> packet(ARP::make_arp_request(iface, ip, info.ip_addr, info.hw_addr));
|
||||
std::auto_ptr<PDU> response(sender.send_recv(*packet));
|
||||
if(response.get()) {
|
||||
const ARP *arp_resp = response->find_pdu<ARP>();
|
||||
if(arp_resp)
|
||||
return arp_resp->sender_hw_addr();
|
||||
}
|
||||
throw std::runtime_error("Could not resolve hardware address");
|
||||
}
|
||||
|
||||
bool Utils::gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr) {
|
||||
typedef std::vector<RouteEntry> entries_type;
|
||||
entries_type entries;
|
||||
|
||||
Reference in New Issue
Block a user