From 594eabd2829f34ab9ebec3496e9f1dbdb37c5112 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Fri, 23 Mar 2012 11:25:28 -0300 Subject: [PATCH] Added option helper functions on DHCP. --- include/dhcp.h | 30 +++++++++++++++++++++++++++++- src/dhcp.cpp | 48 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/include/dhcp.h b/include/dhcp.h index a82679e..723625a 100644 --- a/include/dhcp.h +++ b/include/dhcp.h @@ -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 *container); - bool generic_search(Options opt, std::string *str); + bool generic_search(Options opt, uint32_t *value); uint8_t *serialize_list(const std::list &int_list, uint32_t &sz); diff --git a/src/dhcp.cpp b/src/dhcp.cpp index 9631b0a..4d61c7a 100644 --- a/src/dhcp.cpp +++ b/src/dhcp.cpp @@ -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 *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 &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(opt, value)) { + *value = Utils::net_to_host_l(*value); + return true; + } + return false; +}