1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Added recv mechanism on PacketSender.

This commit is contained in:
Matias Fontanini
2011-08-15 12:39:46 -03:00
parent c803e25db4
commit 5c412208f9
7 changed files with 201 additions and 26 deletions

View File

@@ -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.

View File

@@ -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);

View File

@@ -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;
};
};

View File

@@ -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);

View File

@@ -4,6 +4,7 @@
#include <cstring>
#include <cassert>
#include "icmp.h"
#include "rawpdu.h"
#include "utils.h"
@@ -26,6 +27,10 @@ Tins::ICMP::ICMP(Flags flag) : PDU(IPPROTO_ICMP) {
};
}
Tins::ICMP::ICMP(icmphdr *ptr) : PDU(IPPROTO_ICMP) {
std::memcpy(&_icmp, ptr, sizeof(icmphdr));
}
void Tins::ICMP::code(uint8_t new_code) {
_icmp.code = new_code;
}
@@ -121,4 +126,32 @@ void Tins::ICMP::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
_icmp.check = 0;
}
bool Tins::ICMP::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(icmphdr))
return false;
icmphdr *icmp_ptr = (icmphdr*)ptr;
if(_icmp.type == ECHO_REQUEST) {
return icmp_ptr->type == ECHO_REPLY && icmp_ptr->un.echo.id == _icmp.un.echo.id && icmp_ptr->un.echo.sequence == _icmp.un.echo.sequence;
}
return false;
}
Tins::PDU *Tins::ICMP::clone_packet(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(icmphdr))
return 0;
icmphdr *icmp_ptr = (icmphdr*)ptr;
PDU *child = 0, *cloned;
if(total_sz > sizeof(icmphdr)) {
if(inner_pdu()) {
child = inner_pdu()->clone_packet(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr));
if(!child)
return 0;
}
else
child = new RawPDU(ptr + sizeof(icmphdr), total_sz - sizeof(icmphdr));
}
cloned = new ICMP(icmp_ptr);
cloned->inner_pdu(child);
return cloned;
}

View File

@@ -25,6 +25,7 @@
#include <netinet/in.h>
#endif
#include "ip.h"
#include "rawpdu.h"
#include "utils.h"
#include <iostream>
@@ -42,6 +43,11 @@ Tins::IP::IP(const string &ip_dst, const string &ip_src, PDU *child) : PDU(IPPRO
}
Tins::IP::IP(const iphdr *ptr) : PDU(IPPROTO_IP) {
std::memcpy(&_ip, ptr, sizeof(iphdr));
/* Options... */
}
Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src, PDU *child) : PDU(IPPROTO_IP, child) {
init_ip_fields();
_ip.daddr = ip_dst;
@@ -153,16 +159,30 @@ uint8_t* Tins::IP::IpOption::write(uint8_t* buffer) {
uint32_t Tins::IP::header_size() const {
return sizeof(iphdr) + _padded_options_size;
}
bool Tins::IP::send(PacketSender* sender) {
struct sockaddr_in link_addr;
PacketSender::SocketType type = PacketSender::IP_SOCKET;
link_addr.sin_family = AF_INET;
link_addr.sin_port = 0;
link_addr.sin_addr.s_addr = _ip.daddr;
if(inner_pdu() && inner_pdu()->flag() == IPPROTO_ICMP)
type = PacketSender::ICMP_SOCKET;
return sender->send_l3(this, (const struct sockaddr*)&link_addr, sizeof(link_addr));
return sender->send_l3(this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
}
Tins::PDU *Tins::IP::recv_response(PacketSender *sender) {
struct sockaddr_in link_addr;
PacketSender::SocketType type = PacketSender::IP_SOCKET;
link_addr.sin_family = AF_INET;
link_addr.sin_port = 0;
link_addr.sin_addr.s_addr = _ip.daddr;
if(inner_pdu() && inner_pdu()->flag() == IPPROTO_ICMP)
type = PacketSender::ICMP_SOCKET;
return sender->recv_l3(this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
}
void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
@@ -187,3 +207,36 @@ void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU
for (uint32_t i = 0; i < _ip_options.size(); ++i)
buffer = _ip_options[i].write(buffer);
}
bool Tins::IP::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(iphdr))
return false;
iphdr *ip_ptr = (iphdr*)ptr;
if(_ip.daddr == ip_ptr->saddr && _ip.saddr == ip_ptr->daddr) {
uint32_t sz = _ip.ihl * sizeof(uint32_t);
return inner_pdu() ? inner_pdu()->matches_response(ptr + sz, total_sz - sz) : true;
}
return false;
}
Tins::PDU *Tins::IP::clone_packet(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(iphdr))
return 0;
iphdr *ip_ptr = (iphdr*)ptr;
uint32_t sz = ip_ptr->ihl * sizeof(uint32_t);
if(total_sz < sz)
return 0;
PDU *child = 0, *cloned;
if(total_sz > sz) {
if(inner_pdu()) {
child = inner_pdu()->clone_packet(ptr + sz, total_sz - sz);
if(!child)
return 0;
}
else
child = new RawPDU(ptr + sz, total_sz - sz);
}
cloned = new IP(ip_ptr);
cloned->inner_pdu(child);
return cloned;
}

View File

@@ -24,21 +24,23 @@
#include <sys/select.h>
#include <arpa/inet.h>
#include <unistd.h>
#include </usr/include/linux/if_ether.h>
#include </usr/include/linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <netdb.h>
#endif
#include <assert.h>
#include <iostream>
#include <iostream> //borrar
#include <errno.h>
#include <string.h>
#include "packetsender.h"
#include "utils.h" //borrar
const int Tins::PacketSender::INVALID_RAW_SOCKET = -10;
Tins::PacketSender::PacketSender() : _sockets(SOCKETS_END, INVALID_RAW_SOCKET) {
_types[IP_SOCKET] = IPPROTO_RAW;
_types[ICMP_SOCKET] = IPPROTO_ICMP;
}
bool Tins::PacketSender::open_l2_socket() {
@@ -48,18 +50,21 @@ bool Tins::PacketSender::open_l2_socket() {
return false;
}
bool Tins::PacketSender::open_l3_socket() {
if(_sockets[IP_SOCKET] != INVALID_RAW_SOCKET)
bool Tins::PacketSender::open_l3_socket(SocketType type) {
int socktype = find_type(type);
if(socktype == -1)
return false;
if(_sockets[type] != INVALID_RAW_SOCKET)
return true;
int sockfd;
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
sockfd = socket(AF_INET, SOCK_RAW, socktype);
if (sockfd < 0)
return false;
const int on = 1;
setsockopt(sockfd, IPPROTO_IP,IP_HDRINCL,(const void *)&on,sizeof(on));
setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL,(const void *)&on,sizeof(on));
_sockets[IP_SOCKET] = sockfd;
_sockets[type] = sockfd;
return true;
}
@@ -75,6 +80,12 @@ bool Tins::PacketSender::send(PDU *pdu) {
return pdu->send(this);
}
Tins::PDU *Tins::PacketSender::send_recv(PDU *pdu) {
if(!pdu->send(this))
return 0;
return pdu->recv_response(this);
}
bool Tins::PacketSender::send_l2(PDU *pdu) {
/* To be implemented */
@@ -82,18 +93,42 @@ bool Tins::PacketSender::send_l2(PDU *pdu) {
return false;
}
bool Tins::PacketSender::send_l3(PDU *pdu, const struct sockaddr* link_addr, uint32_t len_link_addr) {
Tins::PDU *Tins::PacketSender::recv_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type) {
if(!open_l3_socket(type))
return 0;
uint8_t buffer[2048];
int sock = _sockets[type];
bool done = false;
socklen_t addrlen = len_link_addr;
while(!done) {
ssize_t size = recvfrom(sock, buffer, 2048, 0, link_addr, &addrlen);
if(size == -1)
return 0;
if(pdu->matches_response(buffer, size))
return pdu->clone_packet(buffer, size);
}
return 0;
}
bool Tins::PacketSender::send_l3(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr, SocketType type) {
bool ret_val = true;
if(!open_l3_socket())
if(!open_l3_socket(type))
ret_val = false;
if (ret_val) {
uint32_t sz;
int sock = _sockets[IP_SOCKET];
int sock = _sockets[type];
uint8_t *buffer = pdu->serialize(sz);
ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_link_addr) != -1);
delete[] buffer;
}
return ret_val;
}
int Tins::PacketSender::find_type(SocketType type) {
SocketTypeMap::iterator it = _types.find(type);
if(it == _types.end())
return -1;
else
return it->second;
}