genode/repos/os/include/net/size_guard.h
Martin Stein 980f3e9c5c net: use Size_guard for packet-data accessors
Instead of handing over the maximum available size to the packet data
accessors, hand over a size guard that keeps track of the packets
boundaries.

This commit also moves the size-guard utilitiy header of Ping and NIC
Router to the include/net directory making it a part of the net library.
It applies the new approach to all net-lib users in the basic repositories.

Ping looses its configurability regarding the ICMP data size as this would
require an additional method in the size guard which would be used only by
Ping.

The size guard was also re-worked to fit the fact that a packet can
bring a tail as well as a header (Ethernet).

Issue #2788
2018-05-30 13:36:11 +02:00

67 lines
1.5 KiB
C++

/*
* \brief Utility to ensure that a size value doesn't exceed a limit
* \author Martin Stein
* \date 2016-08-24
*/
/*
* 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 _NET__SIZE_GUARD_H_
#define _NET__SIZE_GUARD_H_
/* Genode includes */
#include <base/stdint.h>
#include <base/exception.h>
namespace Net { class Size_guard; }
class Net::Size_guard
{
public:
struct Exceeded : Genode::Exception { };
private:
Genode::size_t const _total_size;
Genode::size_t _head_size { 0 };
Genode::size_t _tail_size { 0 };
void _consume(Genode::size_t &s1,
Genode::size_t &s2,
Genode::size_t s1_consume)
{
Genode::size_t const new_s1 = s1 + s1_consume;
if (new_s1 < s1 || new_s1 > _total_size - s2)
throw Exceeded();
s1 = new_s1;
}
public:
Size_guard(Genode::size_t total_size) : _total_size { total_size } { }
void consume_head(Genode::size_t size) { _consume(_head_size, _tail_size, size); }
void consume_tail(Genode::size_t size) { _consume(_tail_size, _head_size, size); }
/***************
** Accessors **
***************/
Genode::size_t unconsumed() const { return _total_size - _head_size - _tail_size; }
Genode::size_t tail_size() const { return _tail_size; }
Genode::size_t head_size() const { return _head_size; }
Genode::size_t total_size() const { return _total_size; }
};
#endif /* _NET__SIZE_GUARD_H_ */