1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-29 13:04:28 +01:00

Working ethernet II PDU. Fixed bugs in IP and added checksum calculation. Added layer2 sending to PacketSender

This commit is contained in:
Santiago Alessandri
2011-08-15 18:16:48 -03:00
parent 5c412208f9
commit b76c9d0525
9 changed files with 192 additions and 136 deletions

View File

@@ -23,6 +23,7 @@
#define __ETHERNET_H
#include <stdint.h>
#include <stdexcept>
#include "pdu.h"
@@ -42,8 +43,23 @@ namespace Tins {
*
* \param mac_dst uint8_t array of 6 bytes containing the destination's MAC.
* \param mac_src uint8_t array of 6 bytes containing the source's MAC.
* \param iface string containing the interface's name from where to send the packet.
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
*/
Ethernet(const uint8_t mac_dst[6], const uint8_t mac_src[6], PDU* child = 0);
Ethernet(const uint8_t* mac_dst, const uint8_t* mac_src, const std::string& iface, PDU* child = 0) throw (std::runtime_error);
/**
* \brief Constructor for creating an ethernet PDU
*
* Constructor that builds an ethernet PDU taking the destination's
* and source's MAC.
*
* \param mac_dst uint8_t array of 6 bytes containing the destination's MAC.
* \param mac_src uint8_t array of 6 bytes containing the source's MAC.
* \param iface_index uint32_t containing the interface's index from where to send the packet.
* \param child PDU* with the PDU contained by the ethernet PDU (optional).
*/
Ethernet(const uint8_t* mac_dst, const uint8_t* mac_src, const uint32_t iface_index, PDU* child = 0);
/* Getters */
/**
@@ -61,11 +77,11 @@ namespace Tins {
inline const uint8_t* src_mac() const { return this->header.src_mac; }
/**
* \brief Getter for the CRC value.
* \brief Getter for the interface.
*
* \return Returns the CRC.
* \return Returns the interface's index as an uint32_t.
*/
inline uint32_t crc() const { return this->_crc; }
inline uint32_t iface() const { return this->_iface_index; }
/* Setters */
/**
@@ -73,21 +89,28 @@ namespace Tins {
*
* \param new_dst_mac uint8_t array of 6 bytes containing the new destination's MAC.
*/
void dst_mac(uint8_t new_dst_mac[6]);
void dst_mac(const uint8_t* new_dst_mac);
/**
* \brief Setter for the source's MAC.
*
* \param new_src_mac uint8_t array of 6 bytes containing the new source's MAC.
*/
void src_mac(uint8_t new_src_mac[6]);
void src_mac(const uint8_t* new_src_mac);
/**
* \brief Setter for the CRC value.
* \brief Setter for the interface.
*
* \param new_crc uint32_t containing the new CRC value.
* \param new_iface_index uint32_t containing the new interface index.
*/
void crc(uint32_t new_crc);
void iface(uint32_t 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);
/* Virtual methods */
/**
@@ -98,14 +121,6 @@ namespace Tins {
*/
uint32_t header_size() const;
/**
* \brief Returns the ethernet frame's trailer length.
*
* \return An uint32_t with the trailer's size.
* \sa PDU::trailer_size()
*/
uint32_t trailer_size() const;
/**
* \sa PDU::send()
*/
@@ -113,18 +128,18 @@ namespace Tins {
private:
/**
* Struct that represents the IEEE 802.3 header
* Struct that represents the Ethernet II header
*/
struct ethernet_header {
uint8_t dst_mac[6];
uint8_t src_mac[6];
uint16_t payload_type;
};
} __attribute__((__packed__));
ethernet_header header;
uint32_t _crc;
uint32_t _iface_index;
void write_serialization(uint8_t *buffer, uint32_t total_sz, PDU *parent);
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
};

View File

@@ -5,16 +5,16 @@
#include "pdu.h"
namespace Tins {
/** \brief ICMP represents the ICMP PDU.
*
*
* ICMP is the representation of the ICMP PDU. Instances of this class
* must be sent over a level 3 PDU, this will otherwise fail.
*/
class ICMP : public PDU {
public:
/** \brief ICMP flags
*/
*/
enum Flags {
ECHO_REPLY = 0,
DEST_UNREACHABLE = 3,
@@ -25,123 +25,123 @@ namespace Tins {
PARAM_PROBLEM = 12,
INFO_REQUEST = 15,
INFO_REPLY = 16
};
};
/** \brief Creates an instance of ICMP.
*
*
* If no flag is specified, then ECHO_REPLY will be used.
* \param flag The type flag which will be set.
*/
ICMP(Flags flag = ECHO_REPLY);
ICMP(Flags flag = ECHO_REQUEST);
/** \brief Sets the code field.
*
*
* \param new_code The code which will be stored in the ICMP struct.
*/
void code(uint8_t new_code);
/** \brief Sets the type field.
*
*
* \param new_code The type which will be stored in the ICMP struct.
*/
void type(uint8_t type);
/** \brief Sets echo request flag for this PDU.
*
*
* \param id The identifier for this request.
* \param seq The sequence number for this request.
*/
void set_echo_request(uint16_t id, uint16_t seq);
/** \brief Sets echo request flag for this PDU.
*
*
* This uses a global id and sequence number to fill the request's
* fields.
* fields.
*/
void set_echo_request();
/** \brief Sets echo reply flag for this PDU.
*
*
* \param id The identifier for this request.
* \param seq The sequence number for this request.
*/
void set_echo_reply(uint16_t id, uint16_t seq);
/** \brief Sets echo reply flag for this PDU.
*
*
* This uses a global id and sequence number to fill the request's
* fields.
* fields.
*/
void set_echo_reply();
/** \brief Sets information request flag for this PDU.
*
*
* \param id The identifier for this request.
* \param seq The sequence number for this request.
*/
void set_info_request(uint16_t id, uint16_t seq);
/** \brief Sets information reply flag for this PDU.
*
*
* \param id The identifier for this request.
* \param seq The sequence number for this request.
*/
void set_info_reply(uint16_t id, uint16_t seq);
/** \brief Sets destination unreachable for this PDU.
*/
void set_dest_unreachable();
/** \brief Sets time exceeded flag for this PDU.
*
* \param ttl_exceeded If true this PDU will represent a ICMP ttl
* exceeded, otherwise it will represent a fragment reassembly
*
* \param ttl_exceeded If true this PDU will represent a ICMP ttl
* exceeded, otherwise it will represent a fragment reassembly
* time exceeded.
*/
void set_time_exceeded(bool ttl_exceeded = true);
/** \brief Sets parameter problem flag for this PDU.
*
*
* \param set_pointer Indicates wether a pointer to the bad octet
* is provided.
* \param bad_octet Identifies the octet in which the error was
* detected. If set_pointer == false, it is ignored.
*/
void set_param_problem(bool set_pointer = false, uint8_t bad_octet = 0);
/** \brief Sets source quench flag for this PDU.
*/
void set_source_quench();
/** \brief Sets redirect flag for this PDU.
*
*
* \param icode The code to be set.
* \param address Address of the gateway to which traffic should
* be sent.
*/
void set_redirect(uint8_t icode, uint32_t address);
/** \brief Returns the ICMP type flag.
*/
Flags type() const { return (Flags)_icmp.type; }
/** \brief Returns the ICMP code flag.
*/
uint8_t code() const { return _icmp.code; }
/** \brief Returns the header size.
*
*
* This metod overrides PDU::header_size. This size includes the
* payload and options size. \sa PDU::header_size
*/
uint32_t header_size() const;
bool matches_response(uint8_t *ptr, uint32_t total_sz);
PDU *clone_packet(uint8_t *ptr, uint32_t total_sz);
private:
static uint16_t global_id, global_seq;
struct icmphdr {
uint8_t type;
uint8_t code;
@@ -157,21 +157,21 @@ namespace Tins {
uint16_t mtu;
} frag;
} un;
} __attribute__((packed));
} __attribute__((__packed__));
/** \brief Creates an instance of ICMP from a icmphdr pointer.
*
*
* \param ptr The icmphdr to clone.
*/
ICMP(icmphdr *ptr);
/** \brief Serialices this ICMP PDU.
* \param buffer The buffer in which the PDU will be serialized.
* \param total_sz The size available in the buffer.
* \param parent The PDU that's one level below this one on the stack.
*/
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
icmphdr _icmp;
};
};

View File

@@ -26,6 +26,11 @@
#include <vector>
#include <stdint.h>
#include <map>
#ifndef WIN32
#include <netinet/in.h>
#endif
#include "pdu.h"
namespace Tins {
@@ -40,17 +45,21 @@ namespace Tins {
class PacketSender {
public:
enum SocketType {
ETHER_SOCKET,
IP_SOCKET,
ICMP_SOCKET,
SOCKETS_END
};
/**
* \brief Constructor for PacketSender objects.
*/
PacketSender();
/**
* \brief
*
*/
bool open_l2_socket();
bool open_l3_socket(SocketType type);
@@ -58,19 +67,19 @@ namespace Tins {
bool close_socket(uint32_t flag);
bool send(PDU* pdu);
PDU *send_recv(PDU *pdu);
bool send_l2(PDU *pdu);
bool send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr);
PDU *recv_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type);
bool send_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type);
private:
static const int INVALID_RAW_SOCKET;
typedef std::map<SocketType, int> SocketTypeMap;
int find_type(SocketType type);
std::vector<int> _sockets;