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

IP now fills automatically the sender address when no link layer PDU is present. Made some protocols work when using PacketSender::send_recv.

This commit is contained in:
Matias Fontanini
2013-03-25 14:08:59 -03:00
parent b0dc376494
commit 82ef41dd92
16 changed files with 241 additions and 114 deletions

View File

@@ -117,4 +117,11 @@ void BootP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *p
std::memcpy(buffer, &_bootp, sizeof(bootphdr));
std::copy(_vend.begin(), _vend.end(), buffer + sizeof(bootphdr));
}
bool BootP::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(bootphdr))
return false;
const bootphdr *bootp_ptr = (const bootphdr *)ptr;
return bootp_ptr->xid == _bootp.xid;
}
}

View File

@@ -48,6 +48,7 @@
#include "utils.h"
#include "packet_sender.h"
#include "constants.h"
#include "network_interface.h"
using std::list;
@@ -351,28 +352,46 @@ uint32_t IP::header_size() const {
return sizeof(iphdr) + _padded_options_size;
}
PacketSender::SocketType pdu_type_to_sender_type(PDU::PDUType type) {
switch(type) {
case PDU::TCP:
return PacketSender::IP_TCP_SOCKET;
case PDU::UDP:
return PacketSender::IP_UDP_SOCKET;
case PDU::ICMP:
return PacketSender::ICMP_SOCKET;
default:
return PacketSender::IP_RAW_SOCKET;
}
}
void IP::send(PacketSender& sender) {
struct sockaddr_in link_addr;
PacketSender::SocketType type = PacketSender::IP_SOCKET;
sockaddr_in link_addr;
PacketSender::SocketType type = PacketSender::IP_RAW_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()->pdu_type() == PDU::ICMP)
type = PacketSender::ICMP_SOCKET;
if(inner_pdu())
type = pdu_type_to_sender_type(inner_pdu()->pdu_type());
sender.send_l3(*this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
}
PDU *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()->pdu_type() == PDU::ICMP)
type = PacketSender::ICMP_SOCKET;
sockaddr_in link_addr;
PacketSender::SocketType type = PacketSender::IP_RAW_SOCKET;
std::memset(&link_addr, 0, sizeof(link_addr));
if(inner_pdu())
type = pdu_type_to_sender_type(inner_pdu()->pdu_type());
return sender.recv_l3(*this, (struct sockaddr*)&link_addr, sizeof(link_addr), type);
return sender.recv_l3(*this, 0, sizeof(link_addr), type);
}
void IP::prepare_for_serialize(const PDU *parent) {
if(!parent && _ip.saddr == 0) {
NetworkInterface iface(dst_addr());
src_addr(iface.addresses().ip_addr);
}
}
void IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU* parent) {
@@ -429,7 +448,7 @@ bool IP::matches_response(uint8_t *ptr, uint32_t total_sz) {
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);
uint32_t sz = std::min(_ip.ihl * sizeof(uint32_t), total_sz);
return inner_pdu() ? inner_pdu()->matches_response(ptr + sz, total_sz - sz) : true;
}
return false;

View File

@@ -91,7 +91,9 @@ PacketSender::PacketSender(uint32_t recv_timeout, uint32_t usec)
_timeout(recv_timeout),
_timeout_usec(usec)
{
_types[IP_SOCKET] = IPPROTO_RAW;
_types[IP_TCP_SOCKET] = IPPROTO_TCP;
_types[IP_UDP_SOCKET] = IPPROTO_UDP;
_types[IP_RAW_SOCKET] = IPPROTO_RAW;
_types[IPV6_SOCKET] = IPPROTO_RAW;
_types[ICMP_SOCKET] = IPPROTO_ICMP;
}
@@ -309,7 +311,6 @@ PDU *PacketSender::recv_match_loop(int sock, PDU &pdu, struct sockaddr* link_add
size = recvfrom(sock, (char*)buffer, 2048, 0, link_addr, &length);
if(pdu.matches_response(buffer, size)) {
return Internals::pdu_from_flag(pdu.pdu_type(), buffer, size);
//return pdu.clone_packet(buffer, size);
}
}
struct timeval this_time, diff;

View File

@@ -96,24 +96,15 @@ PDU::serialization_type PDU::serialize() {
void PDU::serialize(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
uint32_t sz = header_size() + trailer_size();
/* Must not happen... */
#ifdef TINS_DEBUG
assert(total_sz >= sz);
#endif
prepare_for_serialize(parent);
if(_inner_pdu)
_inner_pdu->serialize(buffer + header_size(), total_sz - sz, this);
write_serialization(buffer, total_sz, parent);
}
PDU *PDU::clone_inner_pdu(const uint8_t *ptr, uint32_t total_sz) {
PDU *child = 0;
if(inner_pdu()) {
child = inner_pdu()->clone_packet(ptr, total_sz);
if(!child)
return 0;
}
else
child = new RawPDU(ptr, total_sz);
return child;
}
PDU *PDU::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
return 0;
}

View File

@@ -56,4 +56,8 @@ void RawPDU::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *
void RawPDU::payload(const payload_type &pload) {
_payload = pload;
}
bool RawPDU::matches_response(uint8_t *ptr, uint32_t total_sz) {
return true;
}
}

View File

@@ -374,5 +374,17 @@ void TCP::internal_add_option(const tcp_option &option) {
_total_options_size = (padding) ? _options_size - padding + 4 : _options_size;
}
bool TCP::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(tcphdr))
return false;
const tcphdr *tcp_ptr = (const tcphdr*)ptr;
if(tcp_ptr->sport == _tcp.dport && tcp_ptr->dport == _tcp.sport) {
uint32_t sz = std::min(total_sz, tcp_ptr->doff * sizeof(uint32_t));
return inner_pdu() ? inner_pdu()->matches_response(ptr + sz, total_sz - sz) : true;
}
else
return false;
}
}

View File

@@ -36,7 +36,8 @@
#include "ip.h"
#include "rawpdu.h"
Tins::UDP::UDP(uint16_t dport, uint16_t sport, PDU *child)
namespace Tins {
UDP::UDP(uint16_t dport, uint16_t sport, PDU *child)
: PDU(child)
{
this->dport(dport);
@@ -45,7 +46,7 @@ Tins::UDP::UDP(uint16_t dport, uint16_t sport, PDU *child)
_udp.len = 0;
}
Tins::UDP::UDP(const uint8_t *buffer, uint32_t total_sz)
UDP::UDP(const uint8_t *buffer, uint32_t total_sz)
{
if(total_sz < sizeof(udphdr))
throw std::runtime_error("Not enough size for an UDP header in the buffer.");
@@ -55,27 +56,31 @@ Tins::UDP::UDP(const uint8_t *buffer, uint32_t total_sz)
inner_pdu(new RawPDU(buffer + sizeof(udphdr), total_sz));
}
void Tins::UDP::dport(uint16_t new_dport) {
void UDP::dport(uint16_t new_dport) {
_udp.dport = Endian::host_to_be(new_dport);
}
void Tins::UDP::sport(uint16_t new_sport) {
void UDP::sport(uint16_t new_sport) {
_udp.sport = Endian::host_to_be(new_sport);
}
void Tins::UDP::length(uint16_t new_len) {
void UDP::length(uint16_t new_len) {
_udp.len = Endian::host_to_be(new_len);
}
uint32_t Tins::UDP::header_size() const {
uint32_t UDP::header_size() const {
return sizeof(udphdr);
}
void Tins::UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
void UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
#ifdef TINS_DEBUG
assert(total_sz >= sizeof(udphdr));
#endif
const Tins::IP *ip_packet = dynamic_cast<const Tins::IP*>(parent);
if(inner_pdu())
length(sizeof(udphdr) + inner_pdu()->size());
else
length(sizeof(udphdr));
std::memcpy(buffer, &_udp, sizeof(udphdr));
if(!_udp.check && ip_packet) {
uint32_t checksum = Utils::pseudoheader_checksum(ip_packet->src_addr(), ip_packet->dst_addr(), size(), Constants::IP::PROTO_UDP) +
@@ -87,6 +92,21 @@ void Tins::UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
_udp.check = 0;
}
void Tins::UDP::copy_fields(const UDP *other) {
std::memcpy(&_udp, &other->_udp, sizeof(_udp));
bool UDP::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(udphdr))
return false;
const udphdr *udp_ptr = (const udphdr*)ptr;
if(udp_ptr->sport == _udp.dport && udp_ptr->dport == _udp.sport) {
return inner_pdu()
?
inner_pdu()->matches_response(
ptr + sizeof(udphdr),
total_sz - sizeof(udphdr)
)
:
0;
}
return false;
}
}