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

Fixed some bugs. Added a better dependency system for Makefiles.

This commit is contained in:
Matias Fontanini
2012-08-13 00:29:38 -03:00
parent 87e9c4051e
commit e2223bf406
20 changed files with 1530 additions and 159 deletions

View File

@@ -52,6 +52,7 @@ BootP::BootP(const BootP &other) : PDU(other) {
BootP &BootP::operator= (const BootP &other) {
copy_bootp_fields(&other);
copy_inner_pdu(other);
return *this;
}

View File

@@ -46,7 +46,7 @@ DNS::DNS(const uint8_t *buffer, uint32_t total_sz) : PDU(255), extra_size(0) {
while(ptr < end && *ptr)
ptr++;
Query query;
if((ptr + (sizeof(uint16_t) << 1)) > end)
if((ptr + (sizeof(uint16_t) << 1)) >= end)
throw std::runtime_error("Not enough size for a given query.");
query.name = string(buffer, ptr);
ptr++;
@@ -72,6 +72,7 @@ DNS& DNS::operator=(const DNS& rhs) {
free_list(arity);
free_list(addit);
copy_fields(&rhs);
copy_inner_pdu(rhs);
return *this;
}

View File

@@ -33,10 +33,11 @@
#include "arp.h"
#include "utils.h"
const uint8_t* Tins::EthernetII::BROADCAST = (const uint8_t*)"\xff\xff\xff\xff\xff\xff";
const uint32_t Tins::EthernetII::ADDR_SIZE;
namespace Tins {
const EthernetII::address_type EthernetII::BROADCAST("ff:ff:ff:ff:ff:ff");
const size_t EthernetII::ADDR_SIZE(address_type::address_size);
Tins::EthernetII::EthernetII(const NetworkInterface& iface,
EthernetII::EthernetII(const NetworkInterface& iface,
const address_type &dst_hw_addr, const address_type &src_hw_addr,
PDU* child)
: PDU(ETHERTYPE_IP, child)
@@ -49,7 +50,7 @@ Tins::EthernetII::EthernetII(const NetworkInterface& iface,
}
Tins::EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
: PDU(ETHERTYPE_IP)
{
if(total_sz < sizeof(ethhdr))
@@ -72,27 +73,27 @@ Tins::EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
}
}
void Tins::EthernetII::dst_addr(const address_type &new_dst_mac) {
std::copy(new_dst_mac.begin(), new_dst_mac.end(), _eth.dst_mac);
void EthernetII::dst_addr(const address_type &new_dst_addr) {
new_dst_addr.copy(_eth.dst_mac);
}
void Tins::EthernetII::src_addr(const address_type &new_src_mac) {
std::copy(new_src_mac.begin(), new_src_mac.end(), _eth.src_mac);
void EthernetII::src_addr(const address_type &new_src_addr) {
new_src_addr.copy(_eth.src_mac);
}
void Tins::EthernetII::iface(const NetworkInterface& new_iface) {
void EthernetII::iface(const NetworkInterface& new_iface) {
_iface = new_iface;
}
void Tins::EthernetII::payload_type(uint16_t new_payload_type) {
void EthernetII::payload_type(uint16_t new_payload_type) {
this->_eth.payload_type = Utils::net_to_host_s(new_payload_type);
}
uint32_t Tins::EthernetII::header_size() const {
uint32_t EthernetII::header_size() const {
return sizeof(ethhdr);
}
bool Tins::EthernetII::send(PacketSender* sender) {
bool EthernetII::send(PacketSender* sender) {
if(!_iface)
throw std::runtime_error("Interface has not been set");
struct sockaddr_ll addr;
@@ -108,7 +109,7 @@ bool Tins::EthernetII::send(PacketSender* sender) {
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
}
bool Tins::EthernetII::matches_response(uint8_t *ptr, uint32_t total_sz) {
bool EthernetII::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(ethhdr))
return false;
ethhdr *eth_ptr = (ethhdr*)ptr;
@@ -119,7 +120,7 @@ bool Tins::EthernetII::matches_response(uint8_t *ptr, uint32_t total_sz) {
return false;
}
void Tins::EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
uint32_t my_sz = header_size();
assert(total_sz >= my_sz);
@@ -141,7 +142,7 @@ void Tins::EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, c
memcpy(buffer, &_eth, sizeof(ethhdr));
}
Tins::PDU *Tins::EthernetII::recv_response(PacketSender *sender) {
PDU *EthernetII::recv_response(PacketSender *sender) {
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
@@ -154,7 +155,7 @@ Tins::PDU *Tins::EthernetII::recv_response(PacketSender *sender) {
return sender->recv_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
}
Tins::PDU *Tins::EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
PDU *EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(_eth))
return 0;
PDU *child = 0, *cloned;
@@ -166,3 +167,4 @@ Tins::PDU *Tins::EthernetII::clone_packet(const uint8_t *ptr, uint32_t total_sz)
cloned->inner_pdu(child);
return cloned;
}
}

View File

@@ -27,13 +27,14 @@
#include <net/ethernet.h>
#include <netpacket/packet.h>
#endif
#include "ieee802-3.h"
#include "ieee802_3.h"
#include "llc.h"
#include "utils.h"
const uint8_t* Tins::IEEE802_3::BROADCAST = (const uint8_t*)"\xff\xff\xff\xff\xff\xff";
namespace Tins {
const IEEE802_3::address_type IEEE802_3::BROADCAST("ff:ff:ff:ff:ff:ff");
Tins::IEEE802_3::IEEE802_3(const NetworkInterface& iface,
IEEE802_3::IEEE802_3(const NetworkInterface& iface,
const address_type &dst_hw_addr, const address_type &src_hw_addr,
PDU* child)
: PDU(ETHERTYPE_IP, child)
@@ -46,7 +47,7 @@ Tins::IEEE802_3::IEEE802_3(const NetworkInterface& iface,
}
Tins::IEEE802_3::IEEE802_3(const uint8_t *buffer, uint32_t total_sz) : PDU(ETHERTYPE_IP) {
IEEE802_3::IEEE802_3(const uint8_t *buffer, uint32_t total_sz) : PDU(ETHERTYPE_IP) {
if(total_sz < sizeof(ethhdr))
throw std::runtime_error("Not enough size for an ethernetII header in the buffer.");
memcpy(&_eth, buffer, sizeof(ethhdr));
@@ -59,27 +60,27 @@ Tins::IEEE802_3::IEEE802_3(const uint8_t *buffer, uint32_t total_sz) : PDU(ETHER
}
}
void Tins::IEEE802_3::dst_addr(const address_type &new_dst_mac) {
void IEEE802_3::dst_addr(const address_type &new_dst_mac) {
std::copy(new_dst_mac.begin(), new_dst_mac.end(), _eth.dst_mac);
}
void Tins::IEEE802_3::src_addr(const address_type &new_src_mac) {
void IEEE802_3::src_addr(const address_type &new_src_mac) {
std::copy(new_src_mac.begin(), new_src_mac.end(), _eth.src_mac);
}
void Tins::IEEE802_3::iface(const NetworkInterface &new_iface) {
void IEEE802_3::iface(const NetworkInterface &new_iface) {
_iface = new_iface;
}
void Tins::IEEE802_3::length(uint16_t new_length) {
void IEEE802_3::length(uint16_t new_length) {
this->_eth.length = Utils::net_to_host_s(new_length);
}
uint32_t Tins::IEEE802_3::header_size() const {
uint32_t IEEE802_3::header_size() const {
return sizeof(ethhdr);
}
bool Tins::IEEE802_3::send(PacketSender* sender) {
bool IEEE802_3::send(PacketSender* sender) {
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
@@ -93,7 +94,7 @@ bool Tins::IEEE802_3::send(PacketSender* sender) {
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
}
bool Tins::IEEE802_3::matches_response(uint8_t *ptr, uint32_t total_sz) {
bool IEEE802_3::matches_response(uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(ethhdr))
return false;
ethhdr *eth_ptr = (ethhdr*)ptr;
@@ -103,7 +104,7 @@ bool Tins::IEEE802_3::matches_response(uint8_t *ptr, uint32_t total_sz) {
return false;
}
void Tins::IEEE802_3::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
void IEEE802_3::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
uint32_t my_sz = header_size();
bool set_length = _eth.length == 0;
assert(total_sz >= my_sz);
@@ -117,7 +118,7 @@ void Tins::IEEE802_3::write_serialization(uint8_t *buffer, uint32_t total_sz, co
_eth.length = 0;
}
Tins::PDU *Tins::IEEE802_3::recv_response(PacketSender *sender) {
PDU *IEEE802_3::recv_response(PacketSender *sender) {
struct sockaddr_ll addr;
memset(&addr, 0, sizeof(struct sockaddr_ll));
@@ -130,7 +131,7 @@ Tins::PDU *Tins::IEEE802_3::recv_response(PacketSender *sender) {
return sender->recv_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
}
Tins::PDU *Tins::IEEE802_3::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
PDU *IEEE802_3::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
if(total_sz < sizeof(_eth))
return 0;
PDU *child = 0, *cloned;
@@ -142,3 +143,4 @@ Tins::PDU *Tins::IEEE802_3::clone_packet(const uint8_t *ptr, uint32_t total_sz)
cloned->inner_pdu(child);
return cloned;
}
}

View File

@@ -33,8 +33,7 @@
using std::list;
using std::pair;
using Tins::LLC;
namespace Tins {
const uint8_t LLC::GLOBAL_DSAP_ADDR = 0xFF;
const uint8_t LLC::NULL_ADDR = 0x00;
@@ -54,6 +53,7 @@ LLC::LLC(uint8_t dsap, uint8_t ssap, PDU *child) : PDU(0xff, child), _type(LLC::
}
LLC::LLC(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
// header + 1 info byte
if(total_sz < sizeof(_header) + 1)
throw std::runtime_error("Not enough size for a LLC header in the buffer.");
std::memcpy(&_header, buffer, sizeof(_header));
@@ -61,20 +61,25 @@ LLC::LLC(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
total_sz -= sizeof(_header);
information_field_length = 0;
if ((buffer[0] & 0x03) == LLC::UNNUMBERED) {
if(total_sz < sizeof(un_control_field))
throw std::runtime_error("Not enough size for a LLC header in the buffer.");
type(LLC::UNNUMBERED);
std::memcpy(&control_field.unnumbered, buffer, sizeof(un_control_field));
buffer++;
total_sz -= 1;
buffer += sizeof(un_control_field);
total_sz -= sizeof(un_control_field);
//TODO: Create information fields if corresponding.
}
else {
if(total_sz < sizeof(info_control_field))
throw std::runtime_error("Not enough size for a LLC header in the buffer.");
type((Format)(buffer[0] & 0x03));
control_field_length = 2;
std::memcpy(&control_field.info, buffer, sizeof(info_control_field));
buffer += 2;
total_sz -= 2;
}
inner_pdu(new Tins::RawPDU(buffer, total_sz));
if(total_sz > 0)
inner_pdu(new Tins::RawPDU(buffer, total_sz));
}
LLC::LLC(const LLC &other): PDU(other) {
@@ -243,3 +248,5 @@ void LLC::write_serialization(uint8_t *buffer, uint32_t total_sz, const Tins::PD
buffer += it->first;
}
}
}

View File

@@ -49,7 +49,6 @@ struct InterfaceCollector {
}
};
/** \cond */
struct IPv4Collector {
uint32_t ip;
bool found;