1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00
Files
libtins/include/ip.h
2012-05-03 12:03:26 -03:00

448 lines
13 KiB
C++

/*
* 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_IP_H
#define TINS_IP_H
#ifndef WIN32
#include <endian.h>
#endif
#include <string>
#include <utility>
#include <list>
#include "pdu.h"
#include "ipaddress.h"
#include "utils.h"
namespace Tins {
/**
* \brief Class that represents an IP PDU.
*
* By default, IP PDUs are initialized, setting TTL to IP::DEFAULT_TTL,
* id field to 1 and version to 4. Taking this into account, users
* should set destination and source port and would be enough to send one.
*/
class IP : public PDU {
public:
/**
* \brief IP address size.
*/
static const uint32_t ADDR_SIZE = 4;
/**
* \brief Enum indicating the option's class.
*
* Enum OptionClass represents the different classes of
* IP Options.
*/
enum OptionClass {
CONTROL = 0,
MEASUREMENT = 2
};
/**
* \brief Enum indicating the option's id number.
*
* Enum Option indicates the possible IP Options.
*/
enum Option {
END = 0,
NOOP = 1,
SEC = 2,
LSSR = 3,
TIMESTAMP = 4,
EXTSEC = 5,
RR = 7,
SID = 8,
SSRR = 9,
MTUPROBE = 11,
MTUREPLY = 12,
EIP = 17,
TR = 18,
ADDEXT = 19,
RTRALT = 20,
SDB = 21,
DPS = 23,
UMP = 24,
QS = 25
};
/**
* \brief This class represents an IP option.
*/
struct IPOption {
friend class IP;
struct {
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int number:5;
unsigned int op_class:2;
unsigned int copied:1;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int copied:1;
unsigned int op_class:2;
unsigned int number:5;
#endif
} type;
uint8_t* write(uint8_t* buffer);
/**
* \brief Getter for IP options' data pointer.
*/
const uint8_t* data_ptr() const;
/**
* \brief Getter for the data size field
*/
uint8_t data_size() const;
private:
uint8_t* optional_data, optional_data_size;
} __attribute__((__packed__));
/**
* \brief Default constructor.
* Sets the source and destination port to 0.
*/
IP();
/**
* \brief Constructor for building the IP PDU taking integer as ip addresses.
*
* Constructor that builds an IP using strings as addresses. They
* can be hostnames or IPs.
*
* \param ip_dst The destination ip address(optional).
* \param ip_src The source ip address(optional).
* \param child pointer to a PDU which will be set as the inner_pdu for the packet being constructed(optional).
*/
IP(IPv4Address ip_dst, IPv4Address ip_src = 0, PDU *child = 0);
/**
* \brief Copy constructor.
*/
IP(const IP &other);
/**
* \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.
*
* Destructs IP objects releasing the allocated memory for the options
* if options exist.
*/
~IP();
/**
* \brief Copy assignment operator.
*/
IP &operator= (const IP &other);
/* Getters */
/**
* \brief Getter for the header length field.
*
* \return The number of dwords the header occupies in an uin8_t.
*/
inline uint8_t head_len() const { return this->_ip.ihl; }
/**
* \brief Getter for the type of service field.
*
* \return The this IP PDU's type of service.
*/
inline uint8_t tos() const { return _ip.tos; }
/**
* \brief Getter for the total length field.
*
* \return The total length of this IP PDU.
*/
inline uint16_t tot_len() const { return Utils::net_to_host_s(_ip.tot_len); }
/**
* \brief Getter for the id field.
*
* \return The id for this IP PDU.
*/
inline uint16_t id() const { return Utils::net_to_host_s(_ip.id); }
/**
* \brief Getter for the fragment offset field.
*
* \return The fragment offset for this IP PDU.
*/
inline uint16_t frag_off() const { return Utils::net_to_host_s(_ip.frag_off); }
/**
* \brief Getter for the time to live field.
*
* \return The time to live for this IP PDU.
*/
inline uint8_t ttl() const { return _ip.ttl; }
/**
* \brief Getter for the protocol field.
*
* \return The protocol for this IP PDU.
*/
inline uint8_t protocol() const { return _ip.protocol; }
/**
* \brief Getter for the checksum field.
*
* \return The checksum for this IP PDU.
*/
inline uint16_t check() const { return Utils::net_to_host_s(_ip.check); }
/**
* \brief Getter for the source address field.
*
* \return The source address for this IP PDU.
*/
inline IPv4Address src_addr() const { return Utils::net_to_host_l(_ip.saddr); }
/** \brief Getter for the destination address field.
* \return The destination address for this IP PDU.
*/
inline IPv4Address dst_addr() const { return Utils::net_to_host_l(_ip.daddr); }
/** \brief Getter for the version field.
* \return The version for this IP PDU.
*/
inline uint8_t version() const { return _ip.version; }
/* Setters */
/**
* \brief Setter for the header length field.
*
* \param new_head_len uint8_t with the new header length.
*/
void head_len(uint8_t new_head_len);
/**
* \brief Setter for the type of service field.
*
* \param new_tos The new type of service.
*/
void tos(uint8_t new_tos);
/**
* \brief Setter for the total length field.
*
* \param new_tot_len The new total length.
*/
void tot_len(uint16_t new_tot_len);
/**
* \brief Setter for the id field.
*
* \param new_id The new id.
*/
void id(uint16_t new_id);
/**
* \brief Setter for the fragment offset field.
*
* \param new_frag_off The new fragment offset.
*/
void frag_off(uint16_t new_frag_off);
/**
* \brief Setter for the time to live field.
*
* \param new_ttl The new time to live.
*/
void ttl(uint8_t new_ttl);
/**
* \brief Setter for the protocol field.
*
* \param new_protocol The new protocol.
*/
void protocol(uint8_t new_protocol);
/**
* \brief Setter for the checksum field.
*
* \param new_check The new checksum.
*/
void check(uint16_t new_check);
/**
* \brief Setter for the source address field.
*
* \param ip The ip address in integer notation.
*/
void src_addr(IPv4Address ip);
/**
* \brief Setter for the destination address field.
*
* \param ip The ip address in integer notation.
*/
void dst_addr(IPv4Address ip);
/**
* \brief Setter for the version field.
*
* \param ver The version field to be set.
*/
void version(uint8_t ver);
/**
* \brief Sets an IP option.
*
* \param copied The copied flag for this option.
* \param op_class The option class to be set.
* \param number The options number to be set.
* \param data The data of this options.
* \param data_size The data size.
*/
void set_option(uint8_t copied, OptionClass op_class, Option number, const uint8_t* data = 0, uint32_t data_size = 0);
/**
* \brief Searchs for an option that matchs the given flag.
* \param opt_class The option class to be searched.
* \param opt_number The option number to be searched.
* \return A pointer to the option, or 0 if it was not found.
*/
const IPOption *search_option(OptionClass opt_class, Option opt_number) const;
/**
* \brief Sets the End of List option.
*/
void set_eol_option();
/**
* \brief Sets the NOP option.
*/
void set_noop_option();
/**
* \brief Sets the security option.
*
* \param data The data for this option
* \param data_len The length of the data.
*/
void set_sec_option(const uint8_t* data, uint32_t data_len);
/* Add more option setters */
/* Virtual methods */
/**
* \brief Returns the header size.
*
* This metod overrides PDU::header_size. \sa PDU::header_size
*/
uint32_t header_size() const;
/**
* \sa PDU::send()
*/
bool send(PacketSender* sender);
/**
* \brief Check wether ptr points to a valid response for this PDU.
*
* \sa PDU::matches_response
* \param ptr The pointer to the buffer.
* \param total_sz The size of the buffer.
*/
bool matches_response(uint8_t *ptr, uint32_t total_sz);
/**
* \brief Receives a matching response for this packet.
*
* \sa PDU::recv_response
* \param sender The packet sender which will receive the packet.
*/
PDU *recv_response(PacketSender *sender);
/**
* \brief Getter for the PDU's type.
* \sa PDU::pdu_type
*/
PDUType pdu_type() const { return PDU::IP; }
/**
* \brief Clones this pdu, filling the corresponding header with data
* extracted from a buffer.
*
* \param ptr The pointer to the from from which the data will be extracted.
* \param total_sz The size of the buffer.
* \return The cloned PDU.
* \sa PDU::clone_packet
*/
PDU *clone_packet(const uint8_t *ptr, uint32_t total_sz);
/**
* \brief Clones this PDU.
*
* \sa PDU::clone_pdu
*/
PDU *clone_pdu() const;
private:
static const uint8_t DEFAULT_TTL;
struct iphdr {
#if __BYTE_ORDER == __LITTLE_ENDIAN
unsigned int ihl:4;
unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int version:4;
unsigned int ihl:4;
#else
# error "Endian is not LE nor BE..."
#endif
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
/*The options start here. */
} __attribute__((__packed__));
void copy_fields(const IP *other);
void init_ip_fields();
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
void cleanup();
iphdr _ip;
std::list<IPOption> _ip_options;
uint32_t _options_size, _padded_options_size;
};
};
#endif // TINS_IP_H