From 6276daecabbf0268750c395a6e215ce96e1dd377 Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Thu, 22 Sep 2016 12:33:45 +0200 Subject: [PATCH] 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 --- repos/os/include/net/ipv4.h | 21 +++++++++++++++++++-- repos/os/src/lib/net/ipv4.cc | 6 ++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/repos/os/include/net/ipv4.h b/repos/os/include/net/ipv4.h index 8fc4713d7f..f4c333b48b 100644 --- a/repos/os/include/net/ipv4.h +++ b/repos/os/include/net/ipv4.h @@ -22,10 +22,13 @@ #include #include +namespace Genode { class Output; } + namespace Net { enum { IPV4_ADDR_LEN = 4 }; - typedef Network_address Ipv4_address; + + class Ipv4_address; class Ipv4_address_prefix; @@ -33,6 +36,16 @@ namespace Net } +struct Net::Ipv4_address : Network_address +{ + 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; }; diff --git a/repos/os/src/lib/net/ipv4.cc b/repos/os/src/lib/net/ipv4.cc index 2e8e32fd53..46307108c4 100644 --- a/repos/os/src/lib/net/ipv4.cc +++ b/repos/os/src/lib/net/ipv4.cc @@ -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); +}