1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-30 13:34:27 +01:00

Improved compile time using forward declarations and removing useless includes.

This commit is contained in:
Matias Fontanini
2012-09-05 11:59:46 -03:00
parent 2aa4e10b91
commit 3cb6603151
37 changed files with 267 additions and 238 deletions

View File

@@ -23,15 +23,13 @@
#ifndef TINS_ARP_H
#define TINS_ARP_H
#include <string>
#include "pdu.h"
#include "ipaddress.h"
#include "endianness.h"
#include "hwaddress.h"
#include "network_interface.h"
#include "ipaddress.h"
namespace Tins {
class NetworkInterface;
/**
* \brief Class that represents an ARP PDU.
@@ -274,8 +272,8 @@ namespace Tins {
/**
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<ARP>();
ARP *clone_pdu() const {
return new ARP(*this);
}
private:
struct arphdr {

View File

@@ -293,8 +293,8 @@ namespace Tins {
/**
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<BootP>();
BootP *clone_pdu() const {
return new BootP(*this);
}
protected:
/**

View File

@@ -27,10 +27,9 @@
#include <vector>
#include <string>
#include "bootp.h"
#include "ipaddress.h"
namespace Tins {
class IPv4Address;
/**
* \brief Class that represents the DHCP PDU.
@@ -385,8 +384,8 @@ namespace Tins {
/**
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<DHCP>();
DHCP *clone_pdu() const {
return new DHCP(*this);
}
private:
static const uint32_t MAX_DHCP_SIZE;

View File

@@ -30,16 +30,24 @@
#include <map>
#include "pdu.h"
#include "endianness.h"
#include "ipaddress.h"
namespace Tins {
class IPv4Address;
/**
* \class DNS
* \brief Represents a DNS PDU.
*/
class DNS : public PDU {
public:
/**
* \brief This PDU's flag.
*/
static const PDU::PDUType pdu_flag = PDU::DNS;
/**
* The DNS type.
*/
enum QRType {
QUERY = 0,
RESPONSE = 1
@@ -475,8 +483,8 @@ namespace Tins {
/**
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<DNS>();
DNS *clone_pdu() const {
return new DNS(*this);
}
private:
struct dnshdr {

View File

@@ -25,14 +25,13 @@
#include <list>
#include <vector>
#include <stdint.h>
#include <stdexcept>
#include <utility>
#include "pdu.h"
#include "endianness.h"
#include "network_interface.h"
#include "hwaddress.h"
#include "small_uint.h"
#include "network_interface.h"
namespace Tins {
class RSNInformation;

View File

@@ -22,7 +22,7 @@
#ifndef TINS_EAPOL_H
#define TINS_EAPOL_H
#include <stdint.h>
#include "pdu.h"
#include "small_uint.h"
#include "endianness.h"
@@ -46,6 +46,9 @@ namespace Tins {
*/
static const PDU::PDUType pdu_flag = PDU::EAPOL;
/**
* The EAPOL type enum.
*/
enum EAPOLTYPE {
RC4 = 1,
RSN,

View File

@@ -26,7 +26,6 @@
#ifndef WIN32
#include <endian.h>
#endif
#include "small_uint.h"
#define TINS_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
#define TINS_IS_BIG_ENDIAN (__BYTE_ORDER == __BIG_ENDIAN)

View File

@@ -23,7 +23,6 @@
#define TINS_ETHERNET_II_H
#include <stdint.h>
#include <stdexcept>
#include "pdu.h"
#include "endianness.h"
@@ -42,11 +41,6 @@ namespace Tins {
*/
typedef HWAddress<6> address_type;
/**
* \brief The hardware address size.
*/
static const size_t ADDR_SIZE;
/**
* \brief This PDU's flag.
*/
@@ -189,8 +183,8 @@ namespace Tins {
/**
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<EthernetII>();
EthernetII *clone_pdu() const {
return new EthernetII(*this);
}
private:
/**

View File

@@ -305,8 +305,8 @@ namespace Tins {
/**
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<ICMP>();
ICMP *clone_pdu() const {
return new ICMP(*this);
}
private:
static uint16_t global_id, global_seq;

View File

@@ -23,7 +23,6 @@
#define TINS_IEEE802_3_H
#include <stdint.h>
#include <stdexcept>
#include "pdu.h"
#include "endianness.h"
@@ -127,13 +126,6 @@ namespace Tins {
*/
void iface(const NetworkInterface &new_iface_index);
/**
* \brief Setter for the interface.
*
* \param new_iface string reference containing the new interface name.
*/
void iface(const std::string& new_iface) throw (std::runtime_error);
/**
* \brief Setter for the length field.
*
@@ -189,8 +181,8 @@ namespace Tins {
/**
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<IEEE802_3>();
IEEE802_3 *clone_pdu() const {
return new IEEE802_3(*this);
}
private:
/**

View File

@@ -22,16 +22,11 @@
#ifndef TINS_IP_H
#define TINS_IP_H
#ifndef WIN32
#include <endian.h>
#endif
#include <string>
#include <utility>
#include <list>
#include "pdu.h"
#include "small_uint.h"
#include "ipaddress.h"
#include "endianness.h"
#include "ipaddress.h"
namespace Tins {
@@ -53,11 +48,6 @@ namespace Tins {
* The type used to store addresses.
*/
typedef IPv4Address address_type;
/**
* \brief IP address size.
*/
static const uint32_t ADDR_SIZE = 4;
/**
* \brief Enum indicating the option's class.
@@ -103,11 +93,11 @@ namespace Tins {
struct IPOption {
friend class IP;
struct {
#if __BYTE_ORDER == __LITTLE_ENDIAN
#if TINS_IS_LITTLE_ENDIAN
unsigned int number:5;
unsigned int op_class:2;
unsigned int copied:1;
#elif __BYTE_ORDER == __BIG_ENDIAN
#elif TINS_IS_BIG_ENDIAN
unsigned int copied:1;
unsigned int op_class:2;
unsigned int number:5;
@@ -398,17 +388,17 @@ namespace Tins {
/**
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const {
return do_clone_pdu<IP>();
IP *clone_pdu() const {
return new IP(*this);
}
private:
static const uint8_t DEFAULT_TTL;
struct iphdr {
#if __BYTE_ORDER == __LITTLE_ENDIAN
#if TINS_IS_LITTLE_ENDIAN
unsigned int ihl:4;
unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
#elif TINS_IS_BIG_ENDIAN
unsigned int version:4;
unsigned int ihl:4;
#else

View File

@@ -33,6 +33,11 @@ namespace Tins {
*/
class IPv4Address {
public:
/**
* The address size.
*/
static const size_t address_size = sizeof(uint32_t);
/**
* \brief Constructor taking a const char*.
*

View File

@@ -23,7 +23,7 @@
#define TINS_IEEE8022_H
#include <list>
#include <utility>
#include <vector>
#include <stdint.h>
#include "pdu.h"
#include "endianness.h"
@@ -102,16 +102,6 @@ namespace Tins {
*/
LLC(const uint8_t *buffer, uint32_t total_sz);
/**
* \brief Copy constructor.
*/
LLC(const LLC &other);
/**
* \brief Copy assignment operator.
*/
LLC &operator= (const LLC &other);
/* Setters */
/**
@@ -312,7 +302,9 @@ namespace Tins {
*
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const;
LLC *clone_pdu() const {
return new LLC(*this);
}
private:
struct llchdr {
uint8_t dsap;
@@ -366,6 +358,8 @@ namespace Tins {
} __attribute__((__packed__));
#endif
typedef std::vector<uint8_t> field_type;
void copy_fields(const LLC *other);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
@@ -379,7 +373,7 @@ namespace Tins {
} control_field;
Format _type;
uint8_t information_field_length;
std::list<std::pair<uint8_t,uint8_t*> > information_fields;
std::list<field_type> information_fields;
};
};

View File

@@ -27,12 +27,7 @@
#include <stdint.h>
#include <map>
#ifndef WIN32
#include <netinet/in.h>
#include <sys/time.h>
#endif
#include "pdu.h"
struct timeval;
namespace Tins {
class PDU;
@@ -172,7 +167,7 @@ namespace Tins {
int find_type(SocketType type);
int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
PDU *recv_match_loop(int sock, PDU *pdu, struct sockaddr* link_addr, socklen_t addrlen);
PDU *recv_match_loop(int sock, PDU *pdu, struct sockaddr* link_addr, uint32_t addrlen);
std::vector<int> _sockets;
SocketTypeMap _types;

View File

@@ -25,7 +25,6 @@
#include <stdint.h>
#include <vector>
#include "packetsender.h"
/** \brief The Tins namespace.
*/
@@ -228,14 +227,14 @@ namespace Tins {
* those methods.
* \param sender The PacketSender which will send the packet.
*/
virtual bool send(PacketSender *sender) { return false; }
virtual bool send(PacketSender *sender);
/** \brief Receives a matching response for this packet.
*
* This method should act as a proxy for PacketSender::recv_lX methods.
* \param sender The packet sender which will receive the packet.
*/
virtual PDU *recv_response(PacketSender *sender) { return false; }
virtual PDU *recv_response(PacketSender *sender);
/** \brief Check wether ptr points to a valid response for this PDU.
*

View File

@@ -22,7 +22,6 @@
#ifndef TINS_RADIOTAP_H
#define TINS_RADIOTAP_H
#include <stdexcept>
#include "pdu.h"
#include "endianness.h"
#include "network_interface.h"

View File

@@ -1,3 +1,24 @@
/*
* libtins is a net packet wrapper library for crafting and
* interpreting sniffed packets.
*
* Copyright (C) 2011 Nasel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TINS_SMALL_UINT_H
#define TINS_SMALL_UINT_H

View File

@@ -26,6 +26,7 @@
#include <pcap.h>
#include <string>
#include <memory>
#include <stdexcept>
#include "pdu.h"
#include "ethernetII.h"
@@ -138,14 +139,13 @@ namespace Tins {
template<class Functor>
void Tins::Sniffer::callback_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
try {
PDU *pdu = 0;
std::auto_ptr<PDU> pdu;
LoopData<Functor> *data = reinterpret_cast<LoopData<Functor>*>(args);
if(data->wired)
pdu = new Tins::EthernetII((const uint8_t*)packet, header->caplen);
pdu.reset(new Tins::EthernetII((const uint8_t*)packet, header->caplen));
else
pdu = new Tins::RadioTap((const uint8_t*)packet, header->caplen);
bool ret_val = data->c_handler(pdu);
delete pdu;
pdu.reset(new Tins::RadioTap((const uint8_t*)packet, header->caplen));
bool ret_val = data->c_handler(pdu.get());
if(!ret_val)
pcap_breakloop(data->handle);
}

View File

@@ -26,16 +26,11 @@
#include <list>
#include <vector>
#include <stdint.h>
#ifndef WIN32
#include <endian.h>
#endif
#include "pdu.h"
#include "small_uint.h"
#include "endianness.h"
#include "small_uint.h"
namespace Tins {
/**
* \brief Class that represents an TCP PDU.
*
@@ -416,7 +411,7 @@ namespace Tins {
uint16_t dport;
uint32_t seq;
uint32_t ack_seq;
#if __BYTE_ORDER == __LITTLE_ENDIAN
#if TINS_IS_LITTLE_ENDIAN
uint16_t res1:4,
doff:4,
fin:1,
@@ -427,7 +422,7 @@ namespace Tins {
urg:1,
ece:1,
cwr:1;
#elif __BYTE_ORDER == __BIG_ENDIAN
#elif TINS_IS_BIG_ENDIAN
uint16_t doff:4,
res1:4,
cwr:1,

View File

@@ -36,52 +36,126 @@ namespace Tins {
class Sniffer;
class RawPDU;
class TCPSession {
/**
* \class TCPStream
* \brief Represents a TCP stream.
*/
class TCPStream {
public:
struct SessionInfo {
/**
* The stream information.
*/
struct StreamInfo {
IPv4Address client_addr, server_addr;
uint16_t client_port, server_port;
SessionInfo() {}
StreamInfo() {}
SessionInfo(IPv4Address client, IPv4Address server,
StreamInfo(IPv4Address client, IPv4Address server,
uint16_t cport, uint16_t sport);
bool operator<(const SessionInfo &rhs) const;
bool operator<(const StreamInfo &rhs) const;
};
/**
* The type used to store the payload.
*/
typedef std::vector<uint8_t> payload_type;
TCPSession(IP *ip, TCP *tcp, uint64_t identifier);
TCPSession(const TCPSession &rhs);
TCPSession& operator=(const TCPSession &rhs);
~TCPSession();
/**
* \brief TCPStream constructor.
* \param ip The IP PDU from which to take the initial parameters.
* \param tcp The TCP PDU from which to take the initial parameters.
* \param identifier This stream's identifier number
*/
TCPStream(IP *ip, TCP *tcp, uint64_t identifier);
/**
* Copy constructor.
*/
TCPStream(const TCPStream &rhs);
/**
* Copy assignment operator.
*/
TCPStream& operator=(const TCPStream &rhs);
/**
* Destructor.
*/
~TCPStream();
/**
* \brief Retrieves the client payload.
*
* This is the payload that the connection's client has sent so far.
*
* \return const payload_type& containing the payload.
*/
const payload_type &client_payload() const {
return client_payload_;
}
/**
* \brief Retrieves the server payload.
*
* This is the payload that the connection's server has sent so far.
*
* \return const payload_type& containing the payload.
*/
const payload_type &server_payload() const {
return server_payload_;
}
/**
* \brief Retrieves this stream's identification number.
* \return uint64_t containing the identification number.
*/
uint64_t id() const {
return identifier;
}
const SessionInfo &session_info() const {
/**
* \brief Retrieves the stream information.
* \return const StreamInfo& containing the stream information.
*/
const StreamInfo &stream_info() const {
return info;
}
/**
* \brief Checks whether this stream is finished.
*
* A stream is considered to be finished, if at least one of the
* peers sends a TCP segment containing the FIN bit on.
*
* \return bool indicating whether the stream is finished.
*/
bool is_finished() const {
return fin_sent;
}
/**
* \brief Updates the stream data.
*
* This may update both the payload and the expected sequence numbers.
*
* \param ip The IP PDU from which to take information.
* \param tcp The TCP PDU from which to take information.
* \return bool indicating whether any changes have been done to
* any of the stored payloads.
*/
bool update(IP *ip, TCP *tcp);
/**
* Clears the client payload.
*/
void clear_client_payload();
/**
* Clears the server payload.
*/
void clear_server_payload();
bool operator<(const TCPSession &rhs) const;
private:
typedef std::map<uint32_t, RawPDU*> fragments_type;
@@ -91,22 +165,43 @@ private:
bool generic_process(uint32_t &my_seq, uint32_t &other_seq,
payload_type &pload, fragments_type &frags, TCP *tcp, RawPDU *raw);
uint32_t client_seq, server_seq;
SessionInfo info;
StreamInfo info;
uint64_t identifier;
payload_type client_payload_, server_payload_;
fragments_type client_frags, server_frags;
bool fin_sent;
};
/**
* \class TCPStreamFollower
* \brief Follows TCP streams and notifies the user when data is available.
*/
class TCPStreamFollower {
public:
/**
* \brief Default constructor.
*/
TCPStreamFollower();
/**
* \brief Starts following TCP streams.
*
* The template functors 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.
* \param end_fun This function will be called when a stream is
* closed.
*/
template<typename DataFunctor, typename EndFunctor>
void follow_streams(Sniffer &sniffer, DataFunctor data_fun, EndFunctor end_fun);
private:
typedef std::map<TCPSession::SessionInfo, TCPSession> sessions_type;
typedef std::map<TCPStream::StreamInfo, TCPStream> sessions_type;
template<typename DataFunctor, typename EndFunctor>
struct proxy_caller {
@@ -138,7 +233,7 @@ bool TCPStreamFollower::callback(PDU *pdu, const DataFunctor &data_fun, const En
IP *ip = pdu->find_pdu<IP>();
TCP *tcp = pdu->find_pdu<TCP>();
if(ip && tcp) {
TCPSession::SessionInfo info = {
TCPStream::StreamInfo info = {
ip->src_addr(), ip->dst_addr(),
tcp->sport(), tcp->dport()
};
@@ -151,7 +246,7 @@ bool TCPStreamFollower::callback(PDU *pdu, const DataFunctor &data_fun, const En
sessions.insert(
std::make_pair(
info,
TCPSession(ip, tcp, last_identifier++)
TCPStream(ip, tcp, last_identifier++)
)
);
}

View File

@@ -22,11 +22,8 @@
#ifndef TINS_UTILS_H
#define TINS_UTILS_H
#include <stdexcept>
#ifndef WIN32
#include <ifaddrs.h>
#include <endian.h>
#endif
#include <string>
#include <set>
@@ -35,12 +32,10 @@
#include "packetsender.h"
#include "ipaddress.h"
#include "hwaddress.h"
#include "network_interface.h"
#define TINS_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
#define TINS_IS_BIG_ENDIAN (__BYTE_ORDER == __BIG_ENDIAN)
namespace Tins {
class NetworkInterface;
/**
* \brief Network utils namespace.
*