mirror of
https://github.com/mfontanini/libtins
synced 2026-01-27 04:11:35 +01:00
RAII'd some code. Everything(hopefully) uses IPv4Address now.
This commit is contained in:
86
src/dhcp.cpp
86
src/dhcp.cpp
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user