2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief User datagram protocol.
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2010-08-19
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2010-2017 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _UDP_H_
|
|
|
|
#define _UDP_H_
|
|
|
|
|
|
|
|
/* Genode */
|
|
|
|
#include <base/exception.h>
|
|
|
|
#include <base/stdint.h>
|
2016-12-06 13:12:18 +00:00
|
|
|
#include <net/port.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
#include <util/endian.h>
|
|
|
|
#include <net/ethernet.h>
|
|
|
|
#include <net/ipv4.h>
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
namespace Net { class Udp_packet; }
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
|
|
|
* Data layout of this class conforms to an UDP packet (RFC 768)
|
|
|
|
*
|
|
|
|
* UDP-header-format:
|
|
|
|
*
|
|
|
|
* -----------------------------------------------------------------------
|
|
|
|
* | source-port | destination-port | length | checksum |
|
|
|
|
* | 2 bytes | 2 bytes | 2 bytes | 2 bytes |
|
|
|
|
* -----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class Net::Udp_packet
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
/***********************
|
|
|
|
** UDP header fields **
|
|
|
|
***********************/
|
|
|
|
|
|
|
|
Genode::uint16_t _src_port;
|
|
|
|
Genode::uint16_t _dst_port;
|
|
|
|
Genode::uint16_t _length;
|
|
|
|
Genode::uint16_t _checksum;
|
|
|
|
unsigned _data[0];
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2018-01-08 13:11:29 +00:00
|
|
|
struct Bad_data_type : Genode::Exception { };
|
|
|
|
|
2018-04-09 17:58:08 +00:00
|
|
|
template <typename T> T const &data(Genode::size_t data_size) const
|
2018-01-08 13:11:29 +00:00
|
|
|
{
|
|
|
|
if (data_size < sizeof(T)) {
|
|
|
|
throw Bad_data_type();
|
|
|
|
}
|
2018-04-09 17:58:08 +00:00
|
|
|
return *(T const *)(_data);
|
2018-01-08 13:11:29 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 17:58:08 +00:00
|
|
|
template <typename T> T &data(Genode::size_t data_size)
|
2018-01-08 13:11:29 +00:00
|
|
|
{
|
|
|
|
if (data_size < sizeof(T)) {
|
|
|
|
throw Bad_data_type();
|
|
|
|
}
|
2018-04-09 17:58:08 +00:00
|
|
|
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);
|
2015-03-04 20:12:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 22:35:16 +00:00
|
|
|
void update_checksum(Ipv4_address ip_src,
|
|
|
|
Ipv4_address ip_dst);
|
|
|
|
|
2018-04-18 13:29:58 +00:00
|
|
|
bool checksum_error(Ipv4_address ip_src,
|
|
|
|
Ipv4_address ip_dst) const;
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2017-09-14 13:10:32 +00:00
|
|
|
/***************
|
|
|
|
** Accessors **
|
|
|
|
***************/
|
2016-05-09 13:47:28 +00:00
|
|
|
|
2018-01-08 13:11:29 +00:00
|
|
|
Port src_port() const { return Port(host_to_big_endian(_src_port)); }
|
|
|
|
Port dst_port() const { return Port(host_to_big_endian(_dst_port)); }
|
|
|
|
Genode::uint16_t length() const { return host_to_big_endian(_length); }
|
|
|
|
Genode::uint16_t checksum() const { return host_to_big_endian(_checksum); }
|
2016-08-25 15:48:06 +00:00
|
|
|
|
2017-09-14 13:10:32 +00:00
|
|
|
void length(Genode::uint16_t v) { _length = host_to_big_endian(v); }
|
|
|
|
void src_port(Port p) { _src_port = host_to_big_endian(p.value); }
|
|
|
|
void dst_port(Port p) { _dst_port = host_to_big_endian(p.value); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
|
2016-11-02 00:00:59 +00:00
|
|
|
/*********
|
|
|
|
** log **
|
|
|
|
*********/
|
|
|
|
|
|
|
|
void print(Genode::Output &output) const;
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
} __attribute__((packed));
|
2011-12-22 15:19:25 +00:00
|
|
|
|
|
|
|
#endif /* _UDP_H_ */
|