1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Added option helper functions on DHCP.

This commit is contained in:
Matias Fontanini
2012-03-23 11:25:28 -03:00
parent e642dfa418
commit 594eabd282
2 changed files with 61 additions and 17 deletions

View File

@@ -265,6 +265,20 @@ namespace Tins {
*/
bool search_renewal_time(uint32_t *value);
/**
* \brief Adds a rebind time option.
* \param time The lease rebind time.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_rebind_time(uint32_t time);
/**
* \brief Searchs for a rebind time option.
* \param value A pointer in which the option's value will be stored.
* \return True if the option was found, false otherwise.
*/
bool search_rebind_time(uint32_t *value);
/**
* \brief Adds a subnet mask option.
* \param mask The subnet mask.
@@ -321,6 +335,20 @@ namespace Tins {
*/
bool search_broadcast_option(uint32_t *value);
/**
* \brief Adds a requested address option.
* \param addr The requested address.
* \return True if the option was added successfully. \sa DHCP::add_option
*/
bool add_requested_ip_option(uint32_t addr);
/**
* \brief Searchs for a requested option.
* \param value A pointer in which the option's value will be stored.
* \return True if the option was found, false otherwise.
*/
bool search_requested_ip_option(uint32_t *value);
/**
* \brief Adds a domain name option.
* \param name The domain name.
@@ -375,8 +403,8 @@ namespace Tins {
}
bool generic_search(Options opt, std::list<uint32_t> *container);
bool generic_search(Options opt, std::string *str);
bool generic_search(Options opt, uint32_t *value);
uint8_t *serialize_list(const std::list<uint32_t> &int_list, uint32_t &sz);

View File

@@ -127,11 +127,7 @@ bool Tins::DHCP::add_server_identifier(uint32_t ip) {
}
bool Tins::DHCP::search_server_identifier(uint32_t *value) {
if(generic_search(DHCP_SERVER_IDENTIFIER, value)) {
*value = Utils::net_to_host_l(*value);
return true;
}
return false;
return generic_search(DHCP_SERVER_IDENTIFIER, value);
}
bool Tins::DHCP::add_lease_time(uint32_t time) {
@@ -140,11 +136,7 @@ bool Tins::DHCP::add_lease_time(uint32_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;
return generic_search(DHCP_LEASE_TIME, value);
}
bool Tins::DHCP::add_renewal_time(uint32_t time) {
@@ -153,14 +145,11 @@ bool Tins::DHCP::add_renewal_time(uint32_t time) {
}
bool Tins::DHCP::search_renewal_time(uint32_t *value) {
if(generic_search(DHCP_RENEWAL_TIME, value)) {
*value = Utils::net_to_host_l(*value);
return true;
}
return false;
return generic_search(DHCP_RENEWAL_TIME, value);
}
bool Tins::DHCP::add_subnet_mask(uint32_t mask) {
mask = Utils::net_to_host_l(mask);
return add_option(SUBNET_MASK, sizeof(uint32_t), (const uint8_t*)&mask);
}
@@ -193,13 +182,23 @@ bool Tins::DHCP::search_dns_option(std::list<uint32_t> *dns) {
}
bool Tins::DHCP::add_broadcast_option(uint32_t addr) {
return add_option(BROADCAST_ADDRESS, 4, (uint8_t*)&addr);
addr = Utils::net_to_host_l(addr);
return add_option(BROADCAST_ADDRESS, sizeof(uint32_t), (uint8_t*)&addr);
}
bool Tins::DHCP::search_broadcast_option(uint32_t *value) {
return generic_search(BROADCAST_ADDRESS, value);
}
bool Tins::DHCP::add_requested_ip_option(uint32_t addr) {
addr = Utils::net_to_host_l(addr);
return add_option(DHCP_REQUESTED_ADDRESS, sizeof(uint32_t), (uint8_t*)&addr);
}
bool Tins::DHCP::search_requested_ip_option(uint32_t *value) {
return generic_search(DHCP_REQUESTED_ADDRESS, value);
}
bool Tins::DHCP::add_domain_name(const string &name) {
return add_option(DOMAIN_NAME, name.size(), (const uint8_t*)name.c_str());
}
@@ -208,6 +207,15 @@ bool Tins::DHCP::search_domain_name(std::string *value) {
return generic_search(DOMAIN_NAME, value);
}
bool Tins::DHCP::add_rebind_time(uint32_t time) {
time = Utils::net_to_host_l(time);
return add_option(DHCP_REBINDING_TIME, sizeof(uint32_t), (uint8_t*)&time);
}
bool Tins::DHCP::search_rebind_time(uint32_t *value) {
return generic_search(DHCP_REBINDING_TIME, 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;
@@ -279,3 +287,11 @@ bool Tins::DHCP::generic_search(Options opt, std::string *str) {
*str = string((const char*)option->value, option->length);
return true;
}
bool Tins::DHCP::generic_search(Options opt, uint32_t *value) {
if(generic_search<uint32_t>(opt, value)) {
*value = Utils::net_to_host_l(*value);
return true;
}
return false;
}