diff --git a/include/dhcp.h b/include/dhcp.h index 7414c22..1aba1f7 100644 --- a/include/dhcp.h +++ b/include/dhcp.h @@ -309,6 +309,15 @@ namespace Tins { * \param name The domain name. */ void domain_name(const std::string &name); + + /** + * \brief Adds a hostname option. + * + * The new option is appended at the end of the list. + * + * \param name The hostname. + */ + void hostname(const std::string &name); // Option getters @@ -423,6 +432,16 @@ namespace Tins { * \return std::string Containing the domain name. */ std::string domain_name() const; + + /** + * \brief Searchs for a hostname option. + * + * If the option is not found, an option_not_found exception + * is thrown. + * + * \return std::string Containing the hostname. + */ + std::string hostname() const; /** * \brief Getter for the options list. diff --git a/src/dhcp.cpp b/src/dhcp.cpp index a57a18f..db194c1 100644 --- a/src/dhcp.cpp +++ b/src/dhcp.cpp @@ -187,6 +187,14 @@ std::string DHCP::domain_name() const { return search_and_convert(DOMAIN_NAME); } +void DHCP::hostname(const std::string &name) { + add_option(option(HOST_NAME, name.size(), (const uint8_t*)name.c_str())); +} + +std::string DHCP::hostname() const { + return search_and_convert(HOST_NAME); +} + void DHCP::rebind_time(uint32_t time) { time = Endian::host_to_be(time); add_option(option(DHCP_REBINDING_TIME, sizeof(uint32_t), (uint8_t*)&time)); diff --git a/tests/src/dhcp.cpp b/tests/src/dhcp.cpp index 07fc894..9043ac0 100644 --- a/tests/src/dhcp.cpp +++ b/tests/src/dhcp.cpp @@ -225,11 +225,18 @@ TEST_F(DHCPTest, DNSOption) { TEST_F(DHCPTest, DomainNameOption) { DHCP dhcp; - string domain = "libtins.test.domain", domain_found; + string domain = "libtins.test.domain"; dhcp.domain_name(domain); EXPECT_EQ(domain, dhcp.domain_name()); } +TEST_F(DHCPTest, HostnameOption) { + DHCP dhcp; + string hostname = "libtins-hostname"; + dhcp.hostname(hostname); + EXPECT_EQ(hostname, dhcp.hostname()); +} + TEST_F(DHCPTest, BroadcastOption) { DHCP dhcp; IPv4Address ip = "192.168.0.1", ip_found;