nic_router: move Dhcp_server to extra header/unit

Ref #2534
This commit is contained in:
Martin Stein 2017-10-16 13:07:49 +02:00 committed by Christian Helmuth
parent 5e227f9ff1
commit 127ceaccb5
5 changed files with 159 additions and 121 deletions

View File

@ -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 <dhcp_server.h>
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);
}

View File

@ -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 <ipv4_address_prefix.h>
#include <bit_allocator_dynamic.h>
/* Genode includes */
#include <util/noncopyable.h>
#include <util/xml_node.h>
#include <os/duration.h>
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_ */

View File

@ -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 **
***********************/

View File

@ -21,14 +21,11 @@
#include <ip_rule.h>
#include <port_allocator.h>
#include <pointer.h>
#include <bit_allocator_dynamic.h>
#include <ipv4_config.h>
#include <dhcp_server.h>
/* Genode includes */
#include <util/avl_string.h>
#include <util/xml_node.h>
#include <util/noncopyable.h>
#include <os/duration.h>
#include <util/reconstructible.h>
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:

View File

@ -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)