mirror of
https://github.com/mfontanini/libtins
synced 2026-01-30 05:24:26 +01:00
Fixed bugs in EthernetII, BootP and DHCP.
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include "bootp.h"
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
Tins::BootP::BootP() : PDU(255), _vend_size(64) {
|
||||
@@ -71,30 +70,30 @@ void Tins::BootP::padding(uint16_t new_padding) {
|
||||
}
|
||||
|
||||
void Tins::BootP::ciaddr(uint32_t new_ciaddr) {
|
||||
_bootp.ciaddr = Utils::net_to_host_l(new_ciaddr);
|
||||
_bootp.ciaddr = new_ciaddr;
|
||||
}
|
||||
|
||||
void Tins::BootP::yiaddr(uint32_t new_yiaddr) {
|
||||
_bootp.yiaddr = Utils::net_to_host_l(new_yiaddr);
|
||||
_bootp.yiaddr = new_yiaddr;
|
||||
}
|
||||
|
||||
void Tins::BootP::siaddr(uint32_t new_siaddr) {
|
||||
_bootp.siaddr = Utils::net_to_host_l(new_siaddr);
|
||||
_bootp.siaddr = new_siaddr;
|
||||
}
|
||||
|
||||
void Tins::BootP::giaddr(uint32_t new_giaddr) {
|
||||
_bootp.giaddr = Utils::net_to_host_l(new_giaddr);
|
||||
_bootp.giaddr = new_giaddr;
|
||||
}
|
||||
|
||||
void Tins::BootP::chaddr(uint8_t *new_chaddr) {
|
||||
void Tins::BootP::chaddr(const uint8_t *new_chaddr) {
|
||||
std::memcpy(_bootp.chaddr, new_chaddr, _bootp.hlen);
|
||||
}
|
||||
|
||||
void Tins::BootP::sname(uint8_t *new_sname) {
|
||||
void Tins::BootP::sname(const uint8_t *new_sname) {
|
||||
std::memcpy(_bootp.sname, new_sname, sizeof(_bootp.sname));
|
||||
}
|
||||
|
||||
void Tins::BootP::file(uint8_t *new_file) {
|
||||
void Tins::BootP::file(const uint8_t *new_file) {
|
||||
std::memcpy(_bootp.file, new_file, sizeof(_bootp.file));
|
||||
}
|
||||
|
||||
|
||||
96
src/dhcp.cpp
96
src/dhcp.cpp
@@ -24,6 +24,7 @@
|
||||
#include <cassert>
|
||||
#include "utils.h"
|
||||
#include "dhcp.h"
|
||||
#include "ethernetII.h"
|
||||
|
||||
const uint32_t Tins::DHCP::MAX_DHCP_SIZE = 312;
|
||||
|
||||
@@ -34,18 +35,24 @@ using namespace std;
|
||||
Tins::DHCP::DHCP() : _size(sizeof(uint32_t) + 1) {
|
||||
opcode(BOOTREQUEST);
|
||||
htype(1); //ethernet
|
||||
hlen(6);
|
||||
hlen(EthernetII::ADDR_SIZE);
|
||||
}
|
||||
|
||||
Tins::DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz) : BootP(buffer, total_sz, 0) {
|
||||
buffer += BootP::header_size();
|
||||
total_sz -= BootP::header_size();
|
||||
Tins::DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz) : BootP(buffer, total_sz, 0), _size(sizeof(uint32_t) + 1){
|
||||
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::net_to_host_l(0x63825363))
|
||||
throw std::runtime_error("Not enough size for a DHCP header in the buffer.");
|
||||
buffer += sizeof(uint32_t);
|
||||
total_sz -= sizeof(uint32_t);
|
||||
while(total_sz) {
|
||||
for(unsigned i(0); i < 2 && args[0] != END && args[0] != PAD; ++i) {
|
||||
for(unsigned i(0); i < 2; ++i) {
|
||||
args[i] = *(buffer++);
|
||||
total_sz--;
|
||||
if(!total_sz)
|
||||
if(args[0] == END || args[0] == PAD)
|
||||
i = 2;
|
||||
else if(!total_sz)
|
||||
throw std::runtime_error("Not enough size for a DHCP header in the buffer.");
|
||||
}
|
||||
// If the END-OF-OPTIONS was not found...
|
||||
@@ -98,21 +105,49 @@ bool Tins::DHCP::add_option(Options opt, uint8_t len, const uint8_t *val) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const Tins::DHCP::DHCPOption *Tins::DHCP::search_option(Options opt) const{
|
||||
for(std::list<DHCPOption>::const_iterator it = _options.begin(); it != _options.end(); ++it) {
|
||||
if(it->option == opt)
|
||||
return &(*it);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_type_option(Flags type) {
|
||||
return add_option(DHCP_MESSAGE_TYPE, 1, (const uint8_t*)&type);
|
||||
return add_option(DHCP_MESSAGE_TYPE, sizeof(uint8_t), (const uint8_t*)&type);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::search_type_option(uint8_t *value) {
|
||||
return generic_search(DHCP_MESSAGE_TYPE, value);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_server_identifier(uint32_t ip) {
|
||||
return add_option(DHCP_SERVER_IDENTIFIER, 4, (const uint8_t*)&ip);
|
||||
return add_option(DHCP_SERVER_IDENTIFIER, sizeof(uint32_t), (const uint8_t*)&ip);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::search_server_identifier(uint32_t *value) {
|
||||
return generic_search(DHCP_SERVER_IDENTIFIER, value);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_lease_time(uint32_t time) {
|
||||
time = Utils::net_to_host_l(time);
|
||||
return add_option(DHCP_LEASE_TIME, 4, (const uint8_t*)&time);
|
||||
return add_option(DHCP_LEASE_TIME, sizeof(uint32_t), (const uint8_t*)&time);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::search_lease_time(uint32_t *value) {
|
||||
if(generic_search(DHCP_LEASE_TIME, value)) {
|
||||
*value = Utils::net_to_host_l(*value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_subnet_mask(uint32_t mask) {
|
||||
return add_option(SUBNET_MASK, 4, (const uint8_t*)&mask);
|
||||
return add_option(SUBNET_MASK, sizeof(uint32_t), (const uint8_t*)&mask);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::search_subnet_mask(uint32_t *value) {
|
||||
return generic_search(SUBNET_MASK, value);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_routers_option(const list<uint32_t> &routers) {
|
||||
@@ -123,7 +158,11 @@ bool Tins::DHCP::add_routers_option(const list<uint32_t> &routers) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_dns_options(const list<uint32_t> &dns) {
|
||||
bool Tins::DHCP::search_routers_option(std::list<uint32_t> *routers) {
|
||||
return generic_search(ROUTERS, routers);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_dns_option(const list<uint32_t> &dns) {
|
||||
uint32_t size;
|
||||
uint8_t *buffer = serialize_list(dns, size);
|
||||
bool ret = add_option(DOMAIN_NAME_SERVERS, size, buffer);
|
||||
@@ -131,14 +170,26 @@ bool Tins::DHCP::add_dns_options(const list<uint32_t> &dns) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool Tins::DHCP::search_dns_option(std::list<uint32_t> *dns) {
|
||||
return generic_search(DOMAIN_NAME_SERVERS, dns);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_broadcast_option(uint32_t addr) {
|
||||
return add_option(BROADCAST_ADDRESS, 4, (uint8_t*)&addr);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::search_broadcast_option(uint32_t *value) {
|
||||
return generic_search(BROADCAST_ADDRESS, value);
|
||||
}
|
||||
|
||||
bool Tins::DHCP::add_domain_name(const string &name) {
|
||||
return add_option(DOMAIN_NAME, name.size(), (const uint8_t*)name.c_str());
|
||||
}
|
||||
|
||||
bool Tins::DHCP::search_domain_name(std::string *value) {
|
||||
return generic_search(DOMAIN_NAME, value);
|
||||
}
|
||||
|
||||
uint8_t *Tins::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;
|
||||
@@ -187,3 +238,26 @@ Tins::PDU *Tins::DHCP::clone_pdu() const {
|
||||
new_pdu->copy_fields(this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
bool Tins::DHCP::generic_search(Options opt, std::list<uint32_t> *container) {
|
||||
const DHCPOption *option = search_option(opt);
|
||||
if(!option)
|
||||
return false;
|
||||
const uint32_t *ptr = (const uint32_t*)option->value;
|
||||
uint32_t len = option->length;
|
||||
if((len % sizeof(uint32_t)) != 0)
|
||||
return false;
|
||||
while(len) {
|
||||
container->push_back(*(ptr++));
|
||||
len -= sizeof(uint32_t);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Tins::DHCP::generic_search(Options opt, std::string *str) {
|
||||
const DHCPOption *option = search_option(opt);
|
||||
if(!option)
|
||||
return false;
|
||||
*str = string((const char*)option->value, option->length);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user