1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 20:44:26 +01:00

RAII'd some code. Everything(hopefully) uses IPv4Address now.

This commit is contained in:
Matias Fontanini
2012-08-27 00:16:56 -03:00
parent 1b08d1af71
commit da8c77c77e
20 changed files with 206 additions and 267 deletions

View File

@@ -36,7 +36,7 @@ using std::runtime_error;
namespace Tins {
ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip,
ARP::ARP(ipaddress_type target_ip, ipaddress_type sender_ip,
const hwaddress_type &target_hw, const hwaddress_type &sender_hw)
: PDU(0x0608)
{
@@ -66,7 +66,7 @@ void ARP::sender_hw_addr(const hwaddress_type &new_snd_hw_addr) {
std::copy(new_snd_hw_addr.begin(), new_snd_hw_addr.end(), _arp.ar_sha);
}
void ARP::sender_ip_addr(IPv4Address new_snd_ip_addr) {
void ARP::sender_ip_addr(ipaddress_type new_snd_ip_addr) {
this->_arp.ar_sip = new_snd_ip_addr;
}
@@ -74,7 +74,7 @@ void ARP::target_hw_addr(const hwaddress_type &new_tgt_hw_addr) {
std::copy(new_tgt_hw_addr.begin(), new_tgt_hw_addr.end(), _arp.ar_tha);
}
void ARP::target_ip_addr(IPv4Address new_tgt_ip_addr) {
void ARP::target_ip_addr(ipaddress_type new_tgt_ip_addr) {
this->_arp.ar_tip = new_tgt_ip_addr;
}
@@ -128,8 +128,8 @@ PDU *ARP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
return cloned;
}
PDU* ARP::make_arp_request(const NetworkInterface& iface, IPv4Address target,
IPv4Address sender, const hwaddress_type &hw_snd)
PDU* ARP::make_arp_request(const NetworkInterface& iface, ipaddress_type target,
ipaddress_type sender, const hwaddress_type &hw_snd)
{
/* Create ARP packet and set its attributes */
ARP* arp = new ARP();
@@ -142,8 +142,8 @@ PDU* ARP::make_arp_request(const NetworkInterface& iface, IPv4Address target,
return new EthernetII(iface, EthernetII::BROADCAST, hw_snd, arp);
}
PDU* ARP::make_arp_reply(const NetworkInterface& iface, IPv4Address target,
IPv4Address sender, const hwaddress_type &hw_tgt,
PDU* ARP::make_arp_reply(const NetworkInterface& iface, ipaddress_type target,
ipaddress_type sender, const hwaddress_type &hw_tgt,
const hwaddress_type &hw_snd)
{
/* Create ARP packet and set its attributes */

View File

@@ -25,43 +25,25 @@
#include "bootp.h"
namespace Tins{
BootP::BootP() : PDU(255), _vend_size(64) {
_vend = new uint8_t[64];
BootP::BootP()
: PDU(255), _vend(64) {
std::memset(&_bootp, 0, sizeof(bootphdr));
std::memset(_vend, 0, 64);
}
BootP::BootP(const uint8_t *buffer, uint32_t total_sz, uint32_t vend_field_size)
: PDU(255), _vend(0), _vend_size(vend_field_size)
: PDU(255), _vend(vend_field_size)
{
if(total_sz < sizeof(bootphdr) + vend_field_size)
throw std::runtime_error("Not enough size for a BootP header in the buffer.");
std::memcpy(&_bootp, buffer, sizeof(bootphdr));
buffer += sizeof(bootphdr);
total_sz -= sizeof(bootphdr);
if(_vend_size) {
_vend = new uint8_t[_vend_size];
std::copy(buffer, buffer + _vend_size, _vend);
}
_vend.assign(buffer, buffer + vend_field_size);
// Maybe RawPDU on what is left on the buffer?...
}
BootP::BootP(const BootP &other) : PDU(other) {
copy_bootp_fields(&other);
}
BootP &BootP::operator= (const BootP &other) {
copy_bootp_fields(&other);
copy_inner_pdu(other);
return *this;
}
BootP::~BootP() {
delete[] _vend;
}
uint32_t BootP::header_size() const {
return sizeof(bootphdr) + _vend_size;
return sizeof(bootphdr) + _vend.size();
}
void BootP::opcode(uint8_t new_opcode) {
@@ -92,19 +74,19 @@ void BootP::padding(uint16_t new_padding) {
_bootp.padding = Utils::host_to_be(new_padding);
}
void BootP::ciaddr(IPv4Address new_ciaddr) {
void BootP::ciaddr(ipaddress_type new_ciaddr) {
_bootp.ciaddr = new_ciaddr;
}
void BootP::yiaddr(IPv4Address new_yiaddr) {
void BootP::yiaddr(ipaddress_type new_yiaddr) {
_bootp.yiaddr = new_yiaddr;
}
void BootP::siaddr(IPv4Address new_siaddr) {
void BootP::siaddr(ipaddress_type new_siaddr) {
_bootp.siaddr = new_siaddr;
}
void BootP::giaddr(IPv4Address new_giaddr) {
void BootP::giaddr(ipaddress_type new_giaddr) {
_bootp.giaddr = new_giaddr;
}
@@ -116,27 +98,13 @@ void BootP::file(const uint8_t *new_file) {
std::memcpy(_bootp.file, new_file, sizeof(_bootp.file));
}
void BootP::vend(uint8_t *new_vend, uint32_t size) {
delete[] _vend;
_vend_size = size;
_vend = new uint8_t[size];
std::copy(new_vend, new_vend + size, _vend);
void BootP::vend(const vend_type &new_vend) {
_vend = new_vend;
}
void BootP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
assert(total_sz >= sizeof(bootphdr) + _vend_size);
assert(total_sz >= sizeof(bootphdr) + _vend.size());
std::memcpy(buffer, &_bootp, sizeof(bootphdr));
std::copy(_vend, _vend + _vend_size, buffer + sizeof(bootphdr));
}
void BootP::copy_bootp_fields(const BootP *other) {
std::memcpy(&_bootp, &other->_bootp, sizeof(_bootp));
_vend_size = other->_vend_size;
if(_vend_size) {
_vend = new uint8_t[_vend_size];
std::memcpy(_vend, other->_vend, _vend_size);
}
else
_vend = 0;
std::copy(_vend.begin(), _vend.end(), buffer + sizeof(bootphdr));
}
}

View File

@@ -44,8 +44,8 @@ DHCP::DHCP() : _size(sizeof(uint32_t)) {
DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz)
: BootP(buffer, total_sz, 0), _size(sizeof(uint32_t))
{
buffer += BootP::header_size() - vend_size();
total_sz -= BootP::header_size() - vend_size();
buffer += BootP::header_size() - vend().size();
total_sz -= BootP::header_size() - vend().size();
uint8_t args[2] = {0};
if(total_sz < sizeof(uint32_t) || *(uint32_t*)buffer != Utils::host_to_be<uint32_t>(0x63825363))
throw std::runtime_error("Not enough size for a DHCP header in the buffer.");
@@ -105,12 +105,12 @@ bool DHCP::search_type_option(uint8_t *value) {
return generic_search(DHCP_MESSAGE_TYPE, value);
}
bool DHCP::add_server_identifier(IPv4Address ip) {
bool DHCP::add_server_identifier(ipaddress_type ip) {
uint32_t ip_int = ip;
return add_option(DHCP_SERVER_IDENTIFIER, sizeof(uint32_t), (const uint8_t*)&ip_int);
}
bool DHCP::search_server_identifier(IPv4Address *value) {
bool DHCP::search_server_identifier(ipaddress_type *value) {
return generic_search(DHCP_SERVER_IDENTIFIER, value);
}
@@ -132,16 +132,16 @@ bool DHCP::search_renewal_time(uint32_t *value) {
return generic_search(DHCP_RENEWAL_TIME, value);
}
bool DHCP::add_subnet_mask(IPv4Address mask) {
bool DHCP::add_subnet_mask(ipaddress_type mask) {
uint32_t mask_int = mask;
return add_option(SUBNET_MASK, sizeof(uint32_t), (const uint8_t*)&mask_int);
}
bool DHCP::search_subnet_mask(IPv4Address *value) {
bool DHCP::search_subnet_mask(ipaddress_type *value) {
return generic_search(SUBNET_MASK, value);
}
bool DHCP::add_routers_option(const list<IPv4Address> &routers) {
bool DHCP::add_routers_option(const list<ipaddress_type> &routers) {
uint32_t size;
uint8_t *buffer = serialize_list(routers, size);
bool ret = add_option(ROUTERS, size, buffer);
@@ -149,11 +149,11 @@ bool DHCP::add_routers_option(const list<IPv4Address> &routers) {
return ret;
}
bool DHCP::search_routers_option(std::list<IPv4Address> *routers) {
bool DHCP::search_routers_option(std::list<ipaddress_type> *routers) {
return generic_search(ROUTERS, routers);
}
bool DHCP::add_dns_option(const list<IPv4Address> &dns) {
bool DHCP::add_dns_option(const list<ipaddress_type> &dns) {
uint32_t size;
uint8_t *buffer = serialize_list(dns, size);
bool ret = add_option(DOMAIN_NAME_SERVERS, size, buffer);
@@ -161,25 +161,25 @@ bool DHCP::add_dns_option(const list<IPv4Address> &dns) {
return ret;
}
bool DHCP::search_dns_option(std::list<IPv4Address> *dns) {
bool DHCP::search_dns_option(std::list<ipaddress_type> *dns) {
return generic_search(DOMAIN_NAME_SERVERS, dns);
}
bool DHCP::add_broadcast_option(IPv4Address addr) {
bool DHCP::add_broadcast_option(ipaddress_type addr) {
uint32_t int_addr = addr;
return add_option(BROADCAST_ADDRESS, sizeof(uint32_t), (uint8_t*)&int_addr);
}
bool DHCP::search_broadcast_option(IPv4Address *value) {
bool DHCP::search_broadcast_option(ipaddress_type *value) {
return generic_search(BROADCAST_ADDRESS, value);
}
bool DHCP::add_requested_ip_option(IPv4Address addr) {
bool DHCP::add_requested_ip_option(ipaddress_type addr) {
uint32_t int_addr = addr;
return add_option(DHCP_REQUESTED_ADDRESS, sizeof(uint32_t), (uint8_t*)&int_addr);
}
bool DHCP::search_requested_ip_option(IPv4Address *value) {
bool DHCP::search_requested_ip_option(ipaddress_type *value) {
return generic_search(DHCP_REQUESTED_ADDRESS, value);
}
@@ -200,59 +200,38 @@ bool DHCP::search_rebind_time(uint32_t *value) {
return generic_search(DHCP_REBINDING_TIME, value);
}
uint8_t *DHCP::serialize_list(const list<uint32_t> &int_list, uint32_t &sz) {
uint8_t *buffer = new uint8_t[int_list.size() * sizeof(uint32_t)];
uint32_t *ptr = (uint32_t*)buffer;
for(list<uint32_t>::const_iterator it = int_list.begin(); it != int_list.end(); ++it)
*(ptr++) = Utils::host_to_be(*it);
sz = sizeof(uint32_t) * int_list.size();
return buffer;
}
uint8_t *DHCP::serialize_list(const list<IPv4Address> &ip_list, uint32_t &sz) {
uint8_t *DHCP::serialize_list(const list<ipaddress_type> &ip_list, uint32_t &sz) {
uint8_t *buffer = new uint8_t[ip_list.size() * sizeof(uint32_t)];
uint32_t *ptr = (uint32_t*)buffer;
for(list<IPv4Address>::const_iterator it = ip_list.begin(); it != ip_list.end(); ++it)
*(ptr++) = Utils::host_to_be(*it);
for(list<ipaddress_type>::const_iterator it = ip_list.begin(); it != ip_list.end(); ++it)
*(ptr++) = *it;
sz = sizeof(uint32_t) * ip_list.size();
return buffer;
}
uint32_t DHCP::header_size() const {
return BootP::header_size() - vend_size() + _size;
return BootP::header_size() - vend().size() + _size;
}
void DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
assert(total_sz >= header_size());
uint8_t *result = 0;
if(_size) {
result = new uint8_t[_size];
uint8_t *ptr = result + sizeof(uint32_t);
vend_type &result(BootP::vend());
result.resize(_size);
uint8_t *ptr = &result[0] + sizeof(uint32_t);
// Magic cookie
*((uint32_t*)result) = Utils::host_to_be<uint32_t>(0x63825363);
*((uint32_t*)&result[0]) = Utils::host_to_be<uint32_t>(0x63825363);
for(options_type::const_iterator it = _options.begin(); it != _options.end(); ++it) {
*(ptr++) = it->option;
*(ptr++) = it->value.size();
std::copy(it->value.begin(), it->value.end(), ptr);
ptr += it->value.size();
}
// End of options
//result[_size-1] = END;
vend(result, _size);
}
BootP::write_serialization(buffer, total_sz, parent);
delete[] result;
}
void DHCP::copy_fields(const DHCP *other) {
BootP::copy_bootp_fields(other);
_size = other->_size;
for(options_type::const_iterator it = other->_options.begin(); it != other->_options.end(); ++it)
_options.push_back(*it);
}
bool DHCP::generic_search(Options opt, std::list<uint32_t> *container) {
bool DHCP::generic_search(Options opt, std::list<ipaddress_type> *container) {
const DHCPOption *option = search_option(opt);
if(!option)
return false;
@@ -261,22 +240,7 @@ bool DHCP::generic_search(Options opt, std::list<uint32_t> *container) {
if((len % sizeof(uint32_t)) != 0)
return false;
while(len) {
container->push_back(Utils::host_to_be(*(ptr++)));
len -= sizeof(uint32_t);
}
return true;
}
bool DHCP::generic_search(Options opt, std::list<IPv4Address> *container) {
const DHCPOption *option = search_option(opt);
if(!option)
return false;
const uint32_t *ptr = (const uint32_t*)&option->value[0];
uint32_t len = option->value.size();
if((len % sizeof(uint32_t)) != 0)
return false;
while(len) {
container->push_back(Utils::be_to_host(*(ptr++)));
container->push_back(ipaddress_type(*(ptr++)));
len -= sizeof(uint32_t);
}
return true;
@@ -298,7 +262,7 @@ bool DHCP::generic_search(Options opt, uint32_t *value) {
return false;
}
bool DHCP::generic_search(Options opt, IPv4Address *value) {
bool DHCP::generic_search(Options opt, ipaddress_type *value) {
uint32_t ip_int;
if(generic_search(opt, &ip_int)) {
*value = ip_int;

View File

@@ -68,12 +68,6 @@ Dot11::Dot11(const uint8_t *buffer, uint32_t total_sz)
std::memcpy(&_header, buffer, sizeof(_header));
}
Dot11 &Dot11::operator= (const Dot11 &other) {
copy_80211_fields(&other);
copy_inner_pdu(other);
return *this;
}
void Dot11::parse_tagged_parameters(const uint8_t *buffer, uint32_t total_sz) {
if(total_sz > 0) {
uint8_t opcode, length;
@@ -175,8 +169,8 @@ bool Dot11::send(PacketSender* sender) {
addr.sll_family = Utils::host_to_be<uint16_t>(PF_PACKET);
addr.sll_protocol = Utils::host_to_be<uint16_t>(ETH_P_ALL);
addr.sll_halen = 6;
addr.sll_ifindex = this->_iface.id();
memcpy(&(addr.sll_addr), this->_header.addr1, 6);
addr.sll_ifindex = _iface.id();
memcpy(&(addr.sll_addr), _header.addr1, 6);
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
}
@@ -188,7 +182,7 @@ void Dot11::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *p
buffer += sizeof(_header);
total_sz -= sizeof(_header);
uint32_t written = this->write_ext_header(buffer, total_sz);
uint32_t written = write_ext_header(buffer, total_sz);
buffer += written;
total_sz -= written;
@@ -203,12 +197,12 @@ void Dot11::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *p
}
}
PDU *Dot11::from_bytes(const uint8_t *buffer, uint32_t total_sz) {
Dot11 *Dot11::from_bytes(const uint8_t *buffer, uint32_t total_sz) {
// We only need the control field, the length of the PDU will depend on the flags set.
if(total_sz < sizeof(ieee80211_header::control))
throw runtime_error("Not enough size for a IEEE 802.11 header in the buffer.");
const ieee80211_header *hdr = (const ieee80211_header*)buffer;
PDU *ret = 0;
Dot11 *ret = 0;
if(hdr->control.type == MANAGEMENT) {
if(hdr->control.subtype == BEACON)
ret = new Dot11Beacon(buffer, total_sz);
@@ -228,11 +222,8 @@ PDU *Dot11::from_bytes(const uint8_t *buffer, uint32_t total_sz) {
ret = new Dot11Deauthentication(buffer, total_sz);
else if(hdr->control.subtype == PROBE_REQ)
ret = new Dot11ProbeRequest(buffer, total_sz);
//else if(hdr->control.subtype == PROBE_RESP)
else
else if(hdr->control.subtype == PROBE_RESP)
ret = new Dot11ProbeResponse(buffer, total_sz);
}
else if(hdr->control.type == DATA){
if(hdr->control.subtype <= 4)
@@ -256,7 +247,7 @@ PDU *Dot11::from_bytes(const uint8_t *buffer, uint32_t total_sz) {
else if(hdr->control.subtype == BLOCK_ACK_REQ)
ret = new Dot11BlockAckRequest(buffer, total_sz);
}
else
if(ret == 0)
ret = new Dot11(buffer, total_sz);
return ret;
}
@@ -1309,7 +1300,7 @@ uint32_t Dot11Data::write_ext_header(uint8_t *buffer, uint32_t total_sz) {
memcpy(buffer, &_ext_header, sizeof(_ext_header));
buffer += sizeof(_ext_header);
if (from_ds() && to_ds()) {
written += 6;
written += _addr4.size();
_addr4.copy(buffer);
}
return written;
@@ -1348,7 +1339,7 @@ void Dot11QoSData::qos_control(uint16_t new_qos_control) {
}
uint32_t Dot11QoSData::header_size() const {
return Dot11::header_size() + sizeof(this->_qos_control);
return Dot11Data::header_size() + sizeof(this->_qos_control);
}
uint32_t Dot11QoSData::write_fixed_parameters(uint8_t *buffer, uint32_t total_sz) {

View File

@@ -67,6 +67,8 @@ EthernetII::EthernetII(const uint8_t *buffer, uint32_t total_sz)
case ETHERTYPE_ARP:
next = new Tins::ARP(buffer, total_sz);
break;
default:
next = new Tins::RawPDU(buffer, total_sz);
// Other protos plz
}
inner_pdu(next);

View File

@@ -40,8 +40,9 @@ using namespace std;
const uint8_t Tins::IP::DEFAULT_TTL = 128;
Tins::IP::IP(IPv4Address ip_dst, IPv4Address ip_src, PDU *child) :
PDU(Constants::IP::PROTO_IP, child) {
Tins::IP::IP(address_type ip_dst, address_type ip_src, PDU *child)
: PDU(Constants::IP::PROTO_IP, child)
{
init_ip_fields();
this->dst_addr(ip_dst);
this->src_addr(ip_src);
@@ -60,10 +61,12 @@ Tins::IP::IP(const uint8_t *buffer, uint32_t total_sz)
const uint8_t* ptr_buffer = buffer + sizeof(iphdr);
if(total_sz < head_len() * sizeof(uint32_t))
throw std::runtime_error(msg);
if(head_len() * sizeof(uint32_t) < sizeof(iphdr))
throw std::runtime_error("Malformed head len field");
buffer += head_len() * sizeof(uint32_t);
this->_options_size = 0;
this->_padded_options_size = head_len() * sizeof(uint32_t) - sizeof(iphdr);
_options_size = 0;
_padded_options_size = head_len() * sizeof(uint32_t) - sizeof(iphdr);
/* While the end of the options is not reached read an option */
while (ptr_buffer < buffer && (*ptr_buffer != 0)) {
IPOption opt_to_add;
@@ -165,12 +168,12 @@ void Tins::IP::check(uint16_t new_check) {
}
void Tins::IP::src_addr(IPv4Address ip) {
void Tins::IP::src_addr(address_type ip) {
_ip.saddr = ip;
}
void Tins::IP::dst_addr(IPv4Address ip) {
void Tins::IP::dst_addr(address_type ip) {
_ip.daddr = ip;
}
@@ -214,7 +217,7 @@ void Tins::IP::set_option(uint8_t copied,
_ip_options.push_back(option);
_options_size += 1 + (!option.optional_data.empty() ? (data_size) : 0);
uint8_t padding = _options_size & 3;
_padded_options_size = padding? (_options_size - padding + 4) : _options_size;
_padded_options_size = padding ? (_options_size - padding + 4) : _options_size;
}
const Tins::IP::IPOption *Tins::IP::search_option(OptionClass opt_class, Option opt_number) const {

View File

@@ -26,12 +26,17 @@
using std::string;
namespace Tins{
IPv4Address::IPv4Address(uint32_t ip) : ip_addr(ip) {
IPv4Address::IPv4Address(uint32_t ip)
: ip_addr(Utils::be_to_host(ip)) {
}
IPv4Address::IPv4Address(const char *ip) {
ip_addr = ip ? ip_to_int(ip) : 0;
}
IPv4Address::IPv4Address(const std::string &ip)
: ip_addr(Utils::ip_to_int(ip)) {
: ip_addr(ip_to_int(ip)) {
}
@@ -41,7 +46,7 @@ IPv4Address &IPv4Address::operator=(uint32_t ip) {
}
IPv4Address &Tins::IPv4Address::operator=(const string &ip) {
ip_addr = Utils::ip_to_int(ip);
ip_addr = ip_to_int(ip);
return *this;
}

View File

@@ -63,13 +63,17 @@ struct InterfaceInfoCollector {
namespace Tins {
// static
NetworkInterface NetworkInterface::default_interface() {
return NetworkInterface(IPv4Address(0));
return NetworkInterface(0);
}
NetworkInterface::NetworkInterface() : iface_id(0) {
}
NetworkInterface::NetworkInterface(const char *name) {
iface_id = name ? resolve_index(name) : 0;
}
NetworkInterface::NetworkInterface(const std::string &name) {
iface_id = resolve_index(name.c_str());
}

View File

@@ -93,7 +93,7 @@ void Utils::Internals::skip_line(istream &input) {
/** \endcond */
uint32_t Utils::ip_to_int(const string &ip) throw (std::runtime_error) {
uint32_t Utils::ip_to_int(const string &ip) {
uint32_t result(0), i(0), end, bytes_found(0);
while(i < ip.size() && bytes_found < 4) {
uint16_t this_byte(0);
@@ -133,7 +133,7 @@ IPv4Address Utils::resolve_ip(const string &to_resolve) {
struct hostent *data = gethostbyname(to_resolve.c_str());
if(!data)
throw std::runtime_error("Could not resolve IP");
return be_to_host(((struct in_addr**)data->h_addr_list)[0]->s_addr);
return IPv4Address(((struct in_addr**)data->h_addr_list)[0]->s_addr);
}
PDU *Utils::ping_address(IPv4Address ip, PacketSender *sender, IPv4Address ip_src) {