From 127ceaccb5fabdf0acfed39d97871e7aa8ad2dd9 Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Mon, 16 Oct 2017 13:07:49 +0200 Subject: [PATCH] nic_router: move Dhcp_server to extra header/unit Ref #2534 --- repos/os/src/server/nic_router/dhcp_server.cc | 83 +++++++++++++++++++ repos/os/src/server/nic_router/dhcp_server.h | 74 +++++++++++++++++ repos/os/src/server/nic_router/domain.cc | 69 --------------- repos/os/src/server/nic_router/domain.h | 52 +----------- repos/os/src/server/nic_router/target.mk | 2 +- 5 files changed, 159 insertions(+), 121 deletions(-) create mode 100644 repos/os/src/server/nic_router/dhcp_server.cc create mode 100644 repos/os/src/server/nic_router/dhcp_server.h diff --git a/repos/os/src/server/nic_router/dhcp_server.cc b/repos/os/src/server/nic_router/dhcp_server.cc new file mode 100644 index 000000000..0f8d3edde --- /dev/null +++ b/repos/os/src/server/nic_router/dhcp_server.cc @@ -0,0 +1,83 @@ +/* + * \brief DHCP server role of a domain + * \author Martin Stein + * \date 2016-08-19 + */ + +/* + * Copyright (C) 2016-2017 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +/* local includes */ +#include + +using namespace Net; +using namespace Genode; + + +Dhcp_server::Dhcp_server(Xml_node const node, + Allocator &alloc, + Ipv4_address_prefix const &interface) +: + _dns_server(node.attribute_value("dns_server", Ipv4_address())), + _ip_lease_time(_init_ip_lease_time(node)), + _ip_first(node.attribute_value("ip_first", Ipv4_address())), + _ip_last(node.attribute_value("ip_last", Ipv4_address())), + _ip_first_raw(_ip_first.to_uint32_little_endian()), + _ip_count(_ip_last.to_uint32_little_endian() - _ip_first_raw), + _ip_alloc(alloc, _ip_count) +{ + if (!interface.prefix_matches(_ip_first) || + !interface.prefix_matches(_ip_last) || + interface.address.is_in_range(_ip_first, _ip_last)) + { + throw Invalid(); + } +} + + +Microseconds Dhcp_server::_init_ip_lease_time(Xml_node const node) +{ + unsigned long ip_lease_time_sec = + node.attribute_value("ip_lease_time_sec", 0UL); + + if (!ip_lease_time_sec) { + warning("fall back to default ip_lease_time_sec=\"", + (unsigned long)DEFAULT_IP_LEASE_TIME_SEC, "\""); + ip_lease_time_sec = DEFAULT_IP_LEASE_TIME_SEC; + } + return Microseconds((unsigned long)ip_lease_time_sec * 1000 * 1000); +} + + +void Dhcp_server::print(Output &output) const +{ + if (_dns_server.valid()) { + Genode::print(output, "DNS server ", _dns_server, " "); + } + Genode::print(output, "IP first ", _ip_first, + ", last ", _ip_last, + ", count ", _ip_count, + ", lease time ", _ip_lease_time.value / 1000 / 1000, " sec"); +} + + +Ipv4_address Dhcp_server::alloc_ip() +{ + try { + return Ipv4_address::from_uint32_little_endian(_ip_alloc.alloc() + + _ip_first_raw); + } + catch (Bit_allocator_dynamic::Out_of_indices) { + throw Alloc_ip_failed(); + } +} + + +void Dhcp_server::free_ip(Ipv4_address const &ip) +{ + _ip_alloc.free(ip.to_uint32_little_endian() - _ip_first_raw); +} diff --git a/repos/os/src/server/nic_router/dhcp_server.h b/repos/os/src/server/nic_router/dhcp_server.h new file mode 100644 index 000000000..601f25c94 --- /dev/null +++ b/repos/os/src/server/nic_router/dhcp_server.h @@ -0,0 +1,74 @@ +/* + * \brief DHCP server role of a domain + * \author Martin Stein + * \date 2016-08-19 + */ + +/* + * Copyright (C) 2016-2017 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#ifndef _DHCP_SERVER_H_ +#define _DHCP_SERVER_H_ + +/* local includes */ +#include +#include + +/* Genode includes */ +#include +#include +#include + +namespace Net { class Dhcp_server; } + + +class Net::Dhcp_server : private Genode::Noncopyable +{ + private: + + Ipv4_address const _dns_server; + Genode::Microseconds const _ip_lease_time; + Ipv4_address const _ip_first; + Ipv4_address const _ip_last; + Genode::uint32_t const _ip_first_raw; + Genode::uint32_t const _ip_count; + Genode::Bit_allocator_dynamic _ip_alloc; + + Genode::Microseconds _init_ip_lease_time(Genode::Xml_node const node); + + public: + + enum { DEFAULT_IP_LEASE_TIME_SEC = 3600 }; + + struct Alloc_ip_failed : Genode::Exception { }; + struct Invalid : Genode::Exception { }; + + Dhcp_server(Genode::Xml_node const node, + Genode::Allocator &alloc, + Ipv4_address_prefix const &interface); + + Ipv4_address alloc_ip(); + + void free_ip(Ipv4_address const &ip); + + + /********* + ** log ** + *********/ + + void print(Genode::Output &output) const; + + + /*************** + ** Accessors ** + ***************/ + + Ipv4_address const &dns_server() const { return _dns_server; } + Genode::Microseconds ip_lease_time() const { return _ip_lease_time; } +}; + +#endif /* _DHCP_SERVER_H_ */ diff --git a/repos/os/src/server/nic_router/domain.cc b/repos/os/src/server/nic_router/domain.cc index 835f167d8..cc4234eb2 100644 --- a/repos/os/src/server/nic_router/domain.cc +++ b/repos/os/src/server/nic_router/domain.cc @@ -24,75 +24,6 @@ using namespace Net; using namespace Genode; -/***************** - ** Dhcp_server ** - *****************/ - -Dhcp_server::Dhcp_server(Xml_node const node, - Allocator &alloc, - Ipv4_address_prefix const &interface) -: - _dns_server(node.attribute_value("dns_server", Ipv4_address())), - _ip_lease_time(_init_ip_lease_time(node)), - _ip_first(node.attribute_value("ip_first", Ipv4_address())), - _ip_last(node.attribute_value("ip_last", Ipv4_address())), - _ip_first_raw(_ip_first.to_uint32_little_endian()), - _ip_count(_ip_last.to_uint32_little_endian() - _ip_first_raw), - _ip_alloc(alloc, _ip_count) -{ - if (!interface.prefix_matches(_ip_first) || - !interface.prefix_matches(_ip_last) || - interface.address.is_in_range(_ip_first, _ip_last)) - { - throw Invalid(); - } -} - - -Microseconds Dhcp_server::_init_ip_lease_time(Xml_node const node) -{ - unsigned long ip_lease_time_sec = - node.attribute_value("ip_lease_time_sec", 0UL); - - if (!ip_lease_time_sec) { - warning("fall back to default ip_lease_time_sec=\"", - (unsigned long)DEFAULT_IP_LEASE_TIME_SEC, "\""); - ip_lease_time_sec = DEFAULT_IP_LEASE_TIME_SEC; - } - return Microseconds((unsigned long)ip_lease_time_sec * 1000 * 1000); -} - - -void Dhcp_server::print(Output &output) const -{ - if (_dns_server.valid()) { - Genode::print(output, "DNS server ", _dns_server, " "); - } - Genode::print(output, "IP first ", _ip_first, - ", last ", _ip_last, - ", count ", _ip_count, - ", lease time ", _ip_lease_time.value / 1000 / 1000, " sec"); -} - - -Ipv4_address Dhcp_server::alloc_ip() -{ - try { - return Ipv4_address::from_uint32_little_endian(_ip_alloc.alloc() + - _ip_first_raw); - } - catch (Bit_allocator_dynamic::Out_of_indices) { - throw Alloc_ip_failed(); - } -} - - -void Dhcp_server::free_ip(Ipv4_address const &ip) -{ - _ip_alloc.free(ip.to_uint32_little_endian() - _ip_first_raw); -} - - /*********************** ** Domain_avl_member ** ***********************/ diff --git a/repos/os/src/server/nic_router/domain.h b/repos/os/src/server/nic_router/domain.h index e6d473f9f..492b5d1e4 100644 --- a/repos/os/src/server/nic_router/domain.h +++ b/repos/os/src/server/nic_router/domain.h @@ -21,14 +21,11 @@ #include #include #include -#include #include +#include /* Genode includes */ #include -#include -#include -#include #include namespace Genode { class Allocator; } @@ -37,7 +34,6 @@ namespace Net { class Interface; class Configuration; - class Dhcp_server; class Domain_avl_member; class Domain_base; class Domain; @@ -46,52 +42,6 @@ namespace Net { } -class Net::Dhcp_server : Genode::Noncopyable -{ - private: - - Ipv4_address const _dns_server; - Genode::Microseconds const _ip_lease_time; - Ipv4_address const _ip_first; - Ipv4_address const _ip_last; - Genode::uint32_t const _ip_first_raw; - Genode::uint32_t const _ip_count; - Genode::Bit_allocator_dynamic _ip_alloc; - - Genode::Microseconds _init_ip_lease_time(Genode::Xml_node const node); - - public: - - enum { DEFAULT_IP_LEASE_TIME_SEC = 3600 }; - - struct Alloc_ip_failed : Genode::Exception { }; - struct Invalid : Genode::Exception { }; - - Dhcp_server(Genode::Xml_node const node, - Genode::Allocator &alloc, - Ipv4_address_prefix const &interface); - - Ipv4_address alloc_ip(); - - void free_ip(Ipv4_address const &ip); - - - /********* - ** log ** - *********/ - - void print(Genode::Output &output) const; - - - /*************** - ** Accessors ** - ***************/ - - Ipv4_address const &dns_server() const { return _dns_server; } - Genode::Microseconds ip_lease_time() const { return _ip_lease_time; } -}; - - class Net::Domain_avl_member : public Genode::Avl_string_base { private: diff --git a/repos/os/src/server/nic_router/target.mk b/repos/os/src/server/nic_router/target.mk index a50f8ec6b..59fc91023 100644 --- a/repos/os/src/server/nic_router/target.mk +++ b/repos/os/src/server/nic_router/target.mk @@ -8,6 +8,6 @@ SRC_CC += nat_rule.cc mac_allocator.cc main.cc ipv4_config.cc SRC_CC += uplink.cc interface.cc arp_cache.cc configuration.cc SRC_CC += domain.cc l3_protocol.cc direct_rule.cc link.cc SRC_CC += transport_rule.cc leaf_rule.cc permit_rule.cc -SRC_CC += dhcp_client.cc +SRC_CC += dhcp_client.cc dhcp_server.cc INC_DIR += $(PRG_DIR)