1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-26 20:01:35 +01:00

Fixed merge conflics

This commit is contained in:
Santiago Alessandri
2011-08-19 09:43:49 -03:00
25 changed files with 960 additions and 122 deletions

View File

@@ -50,7 +50,15 @@ namespace Tins {
* ARP requests and replies can be constructed easily using
* ARP::make_arp_request/reply static functions.
*/
ARP();
ARP(uint32_t target_ip = 0, uint32_t sender_ip = 0, const uint8_t *target_hw = 0, const uint8_t *sender_hw = 0);
/**
* \brief Constructor which creates an TCP object from a buffer and adds all identifiable
* PDUs found in the buffer as children of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
ARP(const uint8_t *buffer, uint32_t total_sz);
/* Getters */
/**
@@ -305,7 +313,7 @@ namespace Tins {
* \return The cloned PDU.
* \sa PDU::clone_packet
*/
PDU *clone_packet(uint8_t *ptr, uint32_t total_sz);
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
private:
struct arphdr {
@@ -325,7 +333,7 @@ namespace Tins {
*
* \param arp_ptr The pointer to the arphdr.
*/
ARP(arphdr *arp_ptr);
ARP(const arphdr *arp_ptr);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);

View File

@@ -24,6 +24,7 @@
#include <list>
#include <string>
#include "bootp.h"
@@ -130,10 +131,29 @@ namespace Tins {
* \brief DHCP options struct.
*/
struct DHCPOption {
uint8_t option, length;
/**
* \brief The option number.
*/
uint8_t option;
/**
* \brief The value's length in bytes.
*/
uint8_t length;
/**
* \brief The option's value.
*/
uint8_t *value;
DHCPOption(uint8_t opt, uint8_t len, uint8_t *val);
/**
* \brief Creates an instance of DHCPOption.
*
* The option's value is copied, therefore the user should
* manually free any memory pointed by the "val" parameter.
* \param opt The option number.
* \param len The length of the option's value in bytes.
* \param val The option's value.
*/
DHCPOption(uint8_t opt, uint8_t len, const uint8_t *val);
};
/**
@@ -144,6 +164,13 @@ namespace Tins {
*/
DHCP();
/**
* \brief DHCP destructor
*
* Releases the memory allocated for options.
*/
~DHCP();
/**
* \brief Adds a new option to this DHCP PDU.
*
@@ -154,7 +181,7 @@ namespace Tins {
* \param val The value of this option.
* \return True if the option was added successfully.
*/
bool add_option(Options opt, uint8_t len, uint8_t *val);
bool add_option(Options opt, uint8_t len, const uint8_t *val);
/**
* \brief Adds a type option the the option list.
@@ -177,6 +204,41 @@ namespace Tins {
*/
bool add_lease_time(uint32_t time);
/**
* \brief Adds a subnet mask option.
* \param mask The subnet mask.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_subnet_mask(uint32_t mask);
/**
* \brief Adds a routers option.
* \param routers A list of ip addresses in integer notation.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_routers_option(const std::list<uint32_t> &routers);
/**
* \brief Adds a domain name servers option.
* \param dns A list of ip addresses in integer notation.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_dns_options(const std::list<uint32_t> &dns);
/**
* \brief Adds a broadcast address option.
* \param addr The broadcast address.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_broadcast_option(uint32_t addr);
/**
* \brief Adds a domain name option.
* \param name The domain name.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_domain_name(const std::string &name);
/** \brief Getter for the options list.
* \return The option list.
*/
@@ -199,6 +261,8 @@ namespace Tins {
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
uint8_t *serialize_list(const std::list<uint32_t> &int_list, uint32_t &sz);
std::list<DHCPOption> _options;
uint32_t _size;
};

View File

@@ -66,6 +66,14 @@ namespace Tins {
*/
EthernetII(uint32_t iface_index, const uint8_t* dst_hw_addr = 0, const uint8_t* src_hw_addr = 0, PDU* child = 0);
/**
* \brief Constructor which creates an EthernetII object from a buffer and adds all identifiable
* PDUs found in the buffer as children of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
EthernetII(const uint8_t *buffer, uint32_t total_sz);
/* Getters */
/**
* \brief Getter for the destination's mac address.
@@ -88,7 +96,14 @@ namespace Tins {
*/
inline uint32_t iface() const { return this->_iface_index; }
/**
* \brief Getter for the payload_type
* \return The payload type.
*/
uint16_t payload_type() const;
/* Setters */
/**
* \brief Setter for the destination's MAC.
*
@@ -177,11 +192,11 @@ namespace Tins {
*/
EthernetII(ethhdr *eth_ptr);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
ethhdr _eth;
uint32_t _iface_index;
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
};
};

View File

@@ -57,6 +57,14 @@ namespace Tins {
*/
ICMP(Flags flag = ECHO_REQUEST);
/**
* \brief Constructor which creates an ICMP object from a buffer and adds all identifiable
* PDUs found in the buffer as children of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
ICMP(const uint8_t *buffer, uint32_t total_sz);
/**
* \brief Sets the code field.
*
@@ -273,7 +281,7 @@ namespace Tins {
* \return The cloned PDU.
* \sa PDU::clone_packet
*/
PDU *clone_packet(uint8_t *ptr, uint32_t total_sz);
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
private:
static uint16_t global_id, global_seq;
@@ -298,7 +306,7 @@ namespace Tins {
*
* \param ptr The icmphdr to clone.
*/
ICMP(icmphdr *ptr);
ICMP(const icmphdr *ptr);
/** \brief Serialices this ICMP PDU.
* \param buffer The buffer in which the PDU will be serialized.

View File

@@ -100,6 +100,14 @@ namespace Tins {
*/
IP(uint32_t ip_dst = 0, uint32_t ip_src = 0, PDU *child = 0);
/**
* \brief Constructor which creates an IP object from a buffer and adds all identifiable
* PDUs found in the buffer as children of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
IP(const uint8_t *buffer, uint32_t total_sz);
/**
* \brief Destructor for IP objects.
*
@@ -340,7 +348,7 @@ namespace Tins {
* \return The cloned PDU.
* \sa PDU::clone_packet
*/
PDU *clone_packet(uint8_t *ptr, uint32_t total_sz);
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
private:
static const uint8_t DEFAULT_TTL;

View File

@@ -164,7 +164,7 @@ namespace Tins {
* \param total_sz The size of the buffer.
* \return The cloned PDU.
*/
virtual PDU *clone_packet(uint8_t *ptr, uint32_t total_sz) { return 0; }
virtual PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz) { return 0; }
protected:
/** \brief Serializes this PDU and propagates this action to child PDUs.
*
@@ -181,7 +181,7 @@ namespace Tins {
* \param total_sz The total size of the buffer.
* \return Returns the cloned PDU. Will be 0 if cloning failed.
*/
PDU *clone_inner_pdu(uint8_t *ptr, uint32_t total_sz);
PDU *clone_inner_pdu(const uint8_t *ptr, uint32_t total_sz);
/** \brief Serializes this TCP PDU.
*

View File

@@ -37,14 +37,21 @@ namespace Tins {
public:
/** \brief Creates an instance of RawPDU.
*
* The payload is not copied by default, therefore it must be
* manually freed by the user. If the payload was to be copied,
* then the copy flag must be set to true.
* The payload is copied, therefore the original payload's memory
* must be freed by the user.
* \param pload The payload which the RawPDU will contain.
* \param size The size of the payload.
* \param copy Flag indicating wether to copy the payload.
*/
RawPDU(uint8_t *pload, uint32_t size, bool copy = false);
RawPDU(const uint8_t *pload, uint32_t size);
/** \brief Creates an instance of RawPDU.
*
* The payload is not copied in this constructor, therefore
* it must be manually freed by the user.
* \param pload The payload which the RawPDU will contain.
* \param size The size of the payload.
*/
RawPDU(uint8_t *pload, uint32_t size);
/** \brief RawPDU destructor.
*

138
include/sniffer.h Normal file
View File

@@ -0,0 +1,138 @@
/*
* 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 __SNIFFER_H
#define __SNIFFER_H
#include <pcap.h>
#include <string>
#include "pdu.h"
namespace Tins {
/**
* \brief Abstract sniffed packet handler.
*
* Base class to handle sniffed packets when using Sniffer::sniff_loop.
* Users should either inherit this class, or use the template class
* SnifferHandler to provide their own handlers.
*/
class AbstractSnifferHandler {
public:
/**
* \brief AbstractSnifferHandler destructor.
*/
virtual ~AbstractSnifferHandler() { }
/**
* \brief Handle a captured PDU.
* \return Should return false if no more sniffing is required.
*/
virtual bool handle(PDU *pdu) = 0;
};
/**
* \brief Sniffer class can be used to sniff packets using filters.
*/
class Sniffer {
public:
/**
* \brief Creates an instance of sniffer.
* \param device The device which will be sniffed.
* \param max_packet_size The maximum packet size to be read.
*/
Sniffer(const std::string &device, unsigned max_packet_size);
/**
* \brief Sniffer destructor.
* This frees all memory used by the pcap handle.
*/
~Sniffer();
/**
* \brief Compiles a filter and uses it to capture one packet.
*
* This method should be used only when expecting few packets.
* It's innefficient since it recompiles the filter every time it
* is called. To reuse a filter and sniff more efficiently, use
* Sniffer::sniff_loop.
* \param filter The filter which will be used while sniffing.
* \return The captured packet, matching the given filter, 0 if an
* error occured(probably compiling the filter).
*/
PDU *next_pdu(const std::string &filter);
/**
* \brief Starts a sniffing loop, using a callback object for every
* sniffed packet.
*
* Handlers could be user-provided classes which inherit AbstractSnifferHandler,
* or it could be a specific SnifferHandler specialization. This method deletes
* packets after they are handled, therefore the handlers MUST NOT delete them.
* \param filter The filter to use when sniffing.
* \param cback_handler The callback handler object which should process packets.
* \param max_packets The maximum amount of packets to sniff. 0 == infinite.
*/
void sniff_loop(const std::string &filter, AbstractSnifferHandler *cback_handler, uint32_t max_packets = 0);
/**
* \brief Stops sniffing loops.
*/
void stop_sniff();
private:
bool compile_set_filter(const std::string &filter, bpf_program &prog);
static void callback_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
pcap_t *handle;
bpf_u_int32 ip, mask;
};
/**
* \brief Concrete implementation of AbstractSnifferHandler.
*
* This class is instantiated using a pointer to the actual handler.
* Every time a packet is sniffed, operator() (PDU*) will be called on
* the given pointer. \sa AbstractSnifferHandler
*/
template<class T> class SnifferHandler : public AbstractSnifferHandler {
public:
/**
* Creates an instance of SnifferHandler.
* \param ptr The pointer to the actual handler.
*/
SnifferHandler(T *ptr) : handler(ptr) { }
/**
* \brief The overriden AbstractSnifferHandler::handle.
* \param pdu The sniffed PDU.
* \return False if no more sniffing is required, otherwise true.
*/
bool handle(PDU *pdu) {
return (*handler)(pdu);
}
private:
T *handler;
};
};
#endif

View File

@@ -78,15 +78,21 @@ namespace Tins {
* \param dport Destination port.
* \param sport Source port.
* */
TCP(uint16_t dport = 0, uint16_t sport = 0);
/**
* \brief Constructor which creates an TCP object from a buffer and adds all identifiable
* PDUs found in the buffer as children of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
TCP(const uint8_t *buffer, uint32_t total_sz);
/**
* \brief TCP destructor.
*
* Destructs the TCP instance. Does not free the payload.
* */
~TCP();
/**
@@ -145,6 +151,16 @@ namespace Tins {
*/
inline uint8_t data_offset() const { return this->_tcp.doff; }
/**
* \brief Gets the value of a flag.
*
* \param tcp_flag The polled flag.
* \return The value of the flag.
*/
uint8_t get_flag(Flags tcp_flag);
/* Setters */
/**
* \brief Setter for the destination port field.
*

View File

@@ -34,15 +34,24 @@ namespace Tins {
*/
class UDP : public PDU {
public:
/** \brief UDP constructor.
/**
* \brief UDP constructor.
*
* Creates an instance of UDP. Destination and source port can
* be provided, otherwise both will be 0.
* \param dport Destination port.
* \param sport Source port.
* \param child The child PDU(optional).
* */
UDP(uint16_t dport = 0, uint16_t sport = 0, PDU *child = 0);
/**
* \brief Constructor which creates an UDP object from a buffer and adds all identifiable
* PDUs found in the buffer as children of this one.
* \param buffer The buffer from which this PDU will be constructed.
* \param total_sz The total size of the buffer.
*/
UDP(const uint8_t *buffer, uint32_t total_sz);
/** \brief Returns the destination port
*/

View File

@@ -97,7 +97,8 @@ namespace Tins {
*/
std::set<std::string> network_interfaces();
/** \brief Lookup the ip address of the given interface.
/**
* \brief Lookup the ip address of the given interface.
*
* If the lookup fails, false will be returned, true otherwise.
* \param iface The interface from which to extract the ip address.
@@ -105,7 +106,8 @@ namespace Tins {
*/
bool interface_ip(const std::string &iface, uint32_t &ip);
/** \brief Lookup the hardware address of the given interface.
/**
* \brief Lookup the hardware address of the given interface.
*
* If the lookup fails, false will be returned, true otherwise.
* \param iface The interface from which to extract the hardware address.
@@ -114,15 +116,27 @@ namespace Tins {
*/
bool interface_hwaddr(const std::string &iface, uint8_t *buffer);
/** \brief Lookup the interface identifier.
/**
* \brief Lookup the interface identifier.
*
* If the lookup fails, false will be returned, true otherwise.
* \param iface The interface from which to extract the identifier.
* \param flag The interface id will be returned in this parameter.
* \param id The interface id will be returned in this parameter.
*/
bool interface_id(const std::string &iface, uint32_t &id);
/** \brief Convert 32 bit integer into network byte order.
/**
* \brief Finds the gateway interface matching the given ip.
*
* This function find the interface which would be the gateway
* when sending a packet to the given ip.
* \param ip The ip of the interface we are looking for.
* \return The interface's name.
*/
std::string interface_from_ip(uint32_t ip);
/**
* \brief Convert 32 bit integer into network byte order.
*
* \param data The data to convert.
*/