net/ipv4: convenience methods valid() and print()

Both methods are now available for Ipv4_address as well as for
Ipv4_address_prefix. An IPv4 address is invalid if it contains zeros only.
An IPv4 address prefix is invalid if its address is invalid and its
prefix is 32.

Ref #2139
This commit is contained in:
Martin Stein 2016-09-22 12:33:45 +02:00 committed by Christian Helmuth
parent 9dddc905e2
commit 6276daecab
2 changed files with 25 additions and 2 deletions

View File

@ -22,10 +22,13 @@
#include <util/endian.h>
#include <net/netaddress.h>
namespace Genode { class Output; }
namespace Net
{
enum { IPV4_ADDR_LEN = 4 };
typedef Network_address<IPV4_ADDR_LEN, '.', false> Ipv4_address;
class Ipv4_address;
class Ipv4_address_prefix;
@ -33,6 +36,16 @@ namespace Net
}
struct Net::Ipv4_address : Network_address<IPV4_ADDR_LEN, '.', false>
{
Ipv4_address(Genode::uint8_t value = 0) : Network_address(value) { }
Ipv4_address(void *src) : Network_address(src) { }
bool valid() const { return *this != Ipv4_address(); }
};
/**
* Data layout of this class conforms to an IPv4 packet (RFC 791)
*
@ -200,7 +213,11 @@ class Net::Ipv4_packet
struct Net::Ipv4_address_prefix
{
Ipv4_address address;
Genode::uint8_t prefix = 0;
Genode::uint8_t prefix = 32;
bool valid() const { return address.valid(); }
void print(Genode::Output &output) const;
};

View File

@ -80,3 +80,9 @@ Genode::uint16_t Ipv4_packet::calculate_checksum(Ipv4_packet const &packet)
const Ipv4_address Ipv4_packet::CURRENT((Genode::uint8_t)0x00);
const Ipv4_address Ipv4_packet::BROADCAST((Genode::uint8_t)0xFF);
void Ipv4_address_prefix::print(Genode::Output &output) const
{
Genode::print(output, address, "/", prefix);
}