genode/repos/os/include/net/arp.h
Norman Feske eba9c15746 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
2018-01-17 12:14:35 +01:00

220 lines
7.6 KiB
C++

/*
* \brief Address resolution protocol
* \author Stefan Kalkowski
* \date 2010-08-24
*
* ARP is used to determine a network host's link layer or
* hardware address when only its Network Layer address is known.
*/
/*
* Copyright (C) 2010-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__ARP_H_
#define _NET__ARP_H_
/* Genode */
#include <base/exception.h>
#include <base/stdint.h>
#include <util/endian.h>
#include <net/ethernet.h>
#include <net/ipv4.h>
namespace Net { class Arp_packet; }
/**
* Data layout of this class conforms to an ARP-packet (RFC 826)
*
* It's reduced to Internet protocol (IPv4) over Ethernet.
*
* ARP-packet-format:
*
* --------------------------------------------------------------------
* | Bit 0-7 | Bit 8-15 | Bit 16-23 | Bit 24-31 |
* --------------------------------------------------------------------
* | hw.addr.type | prot.addr.type |
* --------------------------------------------------------------------
* | hw.addr.size | prot.addr.size | operation |
* --------------------------------------------------------------------
* | source-mac-address |
* --------------------------------------------------------------------
* | source-mac-address | source-ip-address |
* --------------------------------------------------------------------
* | source-ip-address | dest.-mac-address |
* --------------------------------------------------------------------
* | dest.-mac-address |
* --------------------------------------------------------------------
* | dest.-ip-address |
* --------------------------------------------------------------------
*/
class Net::Arp_packet
{
private:
Genode::uint16_t _hardware_address_type;
Genode::uint16_t _protocol_address_type;
Genode::uint8_t _hardware_address_size;
Genode::uint8_t _protocol_address_size;
Genode::uint16_t _opcode;
Genode::uint8_t _src_mac[Ethernet_frame::ADDR_LEN];
Genode::uint8_t _src_ip[Ipv4_packet::ADDR_LEN];
Genode::uint8_t _dst_mac[Ethernet_frame::ADDR_LEN];
Genode::uint8_t _dst_ip[Ipv4_packet::ADDR_LEN];
public:
/********************
** ARP parameters **
********************/
enum Protocol_address_type { IPV4 = 0x0800 };
enum Hardware_type {
ETHERNET = 0x0001,
EXP_ETHERNET = 0x0002,
AX_25 = 0x0003,
TOKEN_RING = 0x0004,
CHAOS = 0x0005,
IEEE802_NET = 0x0006,
ARCNET = 0x0007,
HYPERCHANNEL = 0x0008,
LANSTAR = 0x0009,
AUTONET = 0x000A,
LOCALTALK = 0x000B,
LOCALNET = 0x000C,
ULTRA_LINK = 0x000D,
SMDS = 0x000E,
FRAME_RELAY = 0x000F,
ATM_1 = 0x0010,
HDLC = 0x0011,
FIBRE_CHANNEL = 0x0012,
ATM_2 = 0x0013,
SERIAL_LINE = 0x0014,
ATM_3 = 0x0015,
MIL_STD_188_220 = 0x0016,
METRICOM = 0x0017,
IEEE1394 = 0x0018,
MAPOS = 0x0019,
TWINAXIAL = 0x001A,
EUI_64 = 0x001B,
HIPARP = 0x001C,
IP_AND_ARP_OVER_ISO7816 = 0x001D,
ARPSEC = 0x001E,
IPSEC_TUNNEL = 0x001F,
INFINIBAND = 0x0020,
TIA_102 = 0x0021,
WIEGAND_INTERFACE = 0x0022,
Pure_IP = 0x0023,
HW_EXP1 = 0x0024,
HFI = 0x0025,
HW_EXP2 = 0x0100,
};
enum Opcode {
REQUEST = 0x0001,
REPLY = 0x0002,
REQUEST_REVERSE = 0x0003,
REPLY_REVERSE = 0x0004,
DRARP_REQUEST = 0x0005,
DRARP_REPLY = 0x0006,
DRARP_ERROR = 0x0007,
INARP_REQUEST = 0x0008,
INARP_REPLY = 0x0009,
ARP_NAK = 0x000A,
MARS_REQUEST = 0x000B,
MARS_MULTI = 0x000C,
MARS_MSERV = 0x000D,
MARS_JOIN = 0x000E,
MARS_LEAVE = 0x000F,
MARS_NAK = 0x0010,
MARS_UNSERV = 0x0011,
MARS_SJOIN = 0x0012,
MARS_SLEAVE = 0x0013,
MARS_GROUPLIST_REQUEST = 0x0014,
MARS_GROUPLIST_REPLY = 0x0015,
MARS_REDIRECT_MAP = 0x0016,
MAPOS_UNARP = 0x0017,
OP_EXP1 = 0x0018,
OP_EXP2 = 0x0019
};
/**
* Exception used to indicate protocol violation.
*/
class No_arp_packet : Genode::Exception {};
static void validate_size(Genode::size_t size) {
/* arp packet needs to fit in */
if (size < sizeof(Arp_packet))
throw No_arp_packet();
}
/***************
** Accessors **
***************/
Genode::uint16_t hardware_address_type() const { return host_to_big_endian(_hardware_address_type); }
Genode::uint16_t protocol_address_type() const { return host_to_big_endian(_protocol_address_type); }
Genode::uint8_t hardware_address_size() const { return _hardware_address_size; }
Genode::uint8_t protocol_address_size() const { return _protocol_address_size; }
Genode::uint16_t opcode() const { return host_to_big_endian(_opcode); }
Mac_address src_mac() const { return Mac_address((void *)&_src_mac); }
Ipv4_address src_ip() const { return Ipv4_address((void *)&_src_ip); }
Mac_address dst_mac() const { return Mac_address((void *)&_dst_mac); }
Ipv4_address dst_ip() const { return Ipv4_address((void *)&_dst_ip); }
void hardware_address_type(Genode::uint16_t v) { _hardware_address_type = host_to_big_endian(v); }
void protocol_address_type(Genode::uint16_t v) { _protocol_address_type = host_to_big_endian(v); }
void hardware_address_size(Genode::uint8_t v) { _hardware_address_size = v; }
void protocol_address_size(Genode::uint8_t v) { _protocol_address_size = v; }
void opcode(Genode::uint16_t v) { _opcode = host_to_big_endian(v); }
void src_mac(Mac_address v) { v.copy(&_src_mac); }
void src_ip(Ipv4_address v) { v.copy(&_src_ip); }
void dst_mac(Mac_address v) { v.copy(&_dst_mac); }
void dst_ip(Ipv4_address v) { v.copy(&_dst_ip); }
/***************************
** Convenience functions **
***************************/
/**
* \return true when ARP packet really targets ethernet
* address resolution with respect to IPv4 addresses.
*/
bool ethernet_ipv4() const {
return ( host_to_big_endian(_hardware_address_type) == ETHERNET
&& host_to_big_endian(_protocol_address_type) == (Genode::uint16_t)Ethernet_frame::Type::IPV4
&& _hardware_address_size == Ethernet_frame::ADDR_LEN
&& _protocol_address_size == Ipv4_packet::ADDR_LEN);
}
/***************
** Operators **
***************/
/**
* Placement new
*/
void * operator new(__SIZE_TYPE__, void* addr) { return addr; }
/*********
** Log **
*********/
void print(Genode::Output &output) const;
} __attribute__((packed));
#endif /* _NET__ARP_H_ */