mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 04:54:28 +01:00
Added recv mechanism on PacketSender.
This commit is contained in:
@@ -135,6 +135,10 @@ namespace Tins {
|
||||
* 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;
|
||||
|
||||
@@ -155,6 +159,12 @@ namespace Tins {
|
||||
} un;
|
||||
} __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.
|
||||
|
||||
16
include/ip.h
16
include/ip.h
@@ -119,7 +119,15 @@ namespace Tins {
|
||||
/* Virtual methods */
|
||||
uint32_t header_size() const;
|
||||
bool send(PacketSender* sender);
|
||||
|
||||
bool matches_response(uint8_t *ptr, uint32_t total_sz);
|
||||
|
||||
PDU *recv_response(PacketSender *sender);
|
||||
|
||||
PDU *clone_packet(uint8_t *ptr, uint32_t total_sz);
|
||||
private:
|
||||
static const uint8_t DEFAULT_TTL;
|
||||
|
||||
struct iphdr {
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
unsigned int ihl:4;
|
||||
@@ -161,8 +169,12 @@ namespace Tins {
|
||||
|
||||
} __attribute__((__packed__));
|
||||
|
||||
static const uint8_t DEFAULT_TTL;
|
||||
|
||||
/** \brief Creates an instance of IP from an iphdr pointer.
|
||||
*
|
||||
* \param ptr The ip header pointer.
|
||||
*/
|
||||
IP(const iphdr *ptr);
|
||||
|
||||
void init_ip_fields();
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
#include <map>
|
||||
#include "pdu.h"
|
||||
|
||||
namespace Tins {
|
||||
@@ -38,6 +39,12 @@ namespace Tins {
|
||||
*/
|
||||
class PacketSender {
|
||||
public:
|
||||
enum SocketType {
|
||||
IP_SOCKET,
|
||||
ICMP_SOCKET,
|
||||
SOCKETS_END
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Constructor for PacketSender objects.
|
||||
*/
|
||||
@@ -46,23 +53,28 @@ namespace Tins {
|
||||
|
||||
bool open_l2_socket();
|
||||
|
||||
bool open_l3_socket();
|
||||
bool open_l3_socket(SocketType type);
|
||||
|
||||
bool close_socket(uint32_t flag);
|
||||
|
||||
bool send(PDU* pdu);
|
||||
|
||||
PDU *send_recv(PDU *pdu);
|
||||
|
||||
bool send_l2(PDU *pdu);
|
||||
|
||||
PDU *recv_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type);
|
||||
|
||||
bool send_l3(PDU *pdu, const struct sockaddr* link_addr, uint32_t len_link_addr);
|
||||
bool send_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type);
|
||||
private:
|
||||
enum SocketType {
|
||||
IP_SOCKET,
|
||||
SOCKETS_END
|
||||
};
|
||||
static const int INVALID_RAW_SOCKET;
|
||||
|
||||
typedef std::map<SocketType, int> SocketTypeMap;
|
||||
|
||||
int find_type(SocketType type);
|
||||
|
||||
std::vector<int> _sockets;
|
||||
SocketTypeMap _types;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace Tins {
|
||||
|
||||
/** \brief The child PDU.
|
||||
*/
|
||||
inline const PDU *inner_pdu() const { return _inner_pdu; }
|
||||
inline PDU *inner_pdu() const { return _inner_pdu; }
|
||||
|
||||
/** \brief Sets the flag identifier.
|
||||
*/
|
||||
@@ -105,7 +105,27 @@ 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) { return false; }
|
||||
|
||||
/** \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; }
|
||||
|
||||
/** \brief Check wether ptr points to a valid response for this PDU.
|
||||
*
|
||||
* This method must check wether the buffer pointed by ptr is a valid
|
||||
* response for this PDU. If it is valid, then it might want to propagate
|
||||
* the call to the next PDU. Note that in some cases, such as ICMP
|
||||
* Host Unreachable, there is no need to ask the next layer for matching.
|
||||
* \param ptr The pointer to the buffer.
|
||||
* \param total_sz The size of the buffer.
|
||||
*/
|
||||
virtual bool matches_response(uint8_t *ptr, uint32_t total_sz) { return false; }
|
||||
|
||||
virtual PDU *clone_packet(uint8_t *ptr, uint32_t total_sz) { return 0; }
|
||||
protected:
|
||||
/* Serialize this PDU storing the result in buffer. */
|
||||
void serialize(uint8_t *buffer, uint32_t total_sz, const PDU *parent);
|
||||
|
||||
Reference in New Issue
Block a user