net: safer access to packet data

Replace packet method 'T *data' by the new methods 'T &reinterpret_data'
for parsing or modifying existing sub-protocol packets and 'T
&construct_at_data' for composing a new sub-protocol packet. This has
the advantage that, when composing a new packet, the default constructor
that zero-fills the packet is always called first.

Fixes #2751
This commit is contained in:
Martin Stein
2018-04-09 19:58:08 +02:00
committed by Christian Helmuth
parent 58fcf577ea
commit 373134c4e7
10 changed files with 144 additions and 174 deletions

View File

@ -17,7 +17,7 @@
/* Genode includes */
#include <base/exception.h>
#include <util/string.h>
#include <util/construct_at.h>
#include <util/endian.h>
#include <net/mac_address.h>
@ -72,20 +72,27 @@ class Net::Ethernet_frame
struct Bad_data_type : Genode::Exception { };
template <typename T> T const *data(Genode::size_t data_size) const
template <typename T> T const &data(Genode::size_t data_size) const
{
if (data_size < sizeof(T)) {
throw Bad_data_type();
}
return (T const *)(_data);
return *(T const *)(_data);
}
template <typename T> T *data(Genode::size_t data_size)
template <typename T> T &data(Genode::size_t data_size)
{
if (data_size < sizeof(T)) {
throw Bad_data_type();
}
return (T *)(_data);
return *(T *)(_data);
}
template <typename T, typename SIZE_GUARD>
T &construct_at_data(SIZE_GUARD &size_guard)
{
size_guard.add(sizeof(T));
return *Genode::construct_at<T>(_data);
}