genode/repos/os/include/net/udp.h

161 lines
4.3 KiB
C
Raw Normal View History

2011-12-22 15:19:25 +00:00
/*
* \brief User datagram protocol.
* \author Stefan Kalkowski
* \date 2010-08-19
*/
/*
* 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
* 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>
#include <net/port.h>
2011-12-22 15:19:25 +00:00
#include <util/endian.h>
#include <net/ethernet.h>
#include <net/ipv4.h>
namespace Net { class Udp_packet; }
2011-12-22 15:19:25 +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:
/**
* Exception used to indicate protocol violation.
*/
class No_udp_packet : Genode::Exception {};
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 14:42:15 +00:00
static void validate_size(Genode::size_t size) {
/* Udp header needs to fit in */
if (size < sizeof(Udp_packet))
throw No_udp_packet();
}
/***************
** Accessors **
***************/
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); }
template <typename T> T const * data() const { return (T const *)(_data); }
template <typename T> T * data() { return (T *)(_data); }
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); }
/***************
** Operators **
***************/
/**
* Placement new.
*/
void * operator new(__SIZE_TYPE__, void* addr) { return addr; }
/***************************
** Convenience functions **
***************************/
/**
* UDP checksum is calculated over the udp datagram + an IPv4
* pseudo header.
*
* IPv4 pseudo header:
*
* --------------------------------------------------------------
* | src-ipaddr | dst-ipaddr | zero-field | prot.-id | udp-length |
* | 4 bytes | 4 bytes | 1 byte | 1 byte | 2 bytes |
* --------------------------------------------------------------
*/
void update_checksum(Ipv4_address src,
Ipv4_address dst)
{
/* have to reset the checksum field for calculation */
_checksum = 0;
/*
* sum up pseudo header
*/
Genode::uint32_t sum = 0;
for (unsigned i = 0; i < Ipv4_packet::ADDR_LEN; i=i+2) {
Genode::uint16_t s = src.addr[i] << 8 | src.addr[i + 1];
Genode::uint16_t d = dst.addr[i] << 8 | dst.addr[i + 1];
sum += s + d;
2011-12-22 15:19:25 +00:00
}
Genode::uint8_t prot[] = { 0, (Genode::uint8_t)Ipv4_packet::Protocol::UDP };
sum += host_to_big_endian(*(Genode::uint16_t*)&prot) + length();
2011-12-22 15:19:25 +00:00
/*
* sum up udp packet itself
2011-12-22 15:19:25 +00:00
*/
unsigned max = (length() & 1) ? (length() - 1) : length();
Genode::uint16_t *udp = (Genode::uint16_t*) this;
for (unsigned i = 0; i < max; i=i+2)
sum += host_to_big_endian(*udp++);
/* if udp length is odd, append a zero byte */
if (length() & 1) {
Genode::uint8_t last[] =
{ *((Genode::uint8_t*)this + (length()-1)), 0 };
sum += host_to_big_endian(*(Genode::uint16_t*)&last);
2011-12-22 15:19:25 +00:00
}
/*
* keep only the last 16 bits of the 32 bit calculated sum
* and add the carries
*/
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
/* one's complement of sum */
_checksum = host_to_big_endian((Genode::uint16_t) ~sum);
}
/*********
** log **
*********/
void print(Genode::Output &output) const;
} __attribute__((packed));
2011-12-22 15:19:25 +00:00
#endif /* _UDP_H_ */