From 27a73b89f087fb13cecc21784551a89e9827b101 Mon Sep 17 00:00:00 2001 From: Johannes Schlatow Date: Fri, 18 Mar 2016 19:41:00 +0100 Subject: [PATCH] net: write accessors for Ipv4_packet Also adds header-checksum calculation function. Fixes #1915 --- repos/os/include/net/ipv4.h | 16 ++++++++++++++++ repos/os/src/lib/net/ipv4.cc | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/repos/os/include/net/ipv4.h b/repos/os/include/net/ipv4.h index 87ff71821f..a735f05d17 100644 --- a/repos/os/include/net/ipv4.h +++ b/repos/os/include/net/ipv4.h @@ -60,6 +60,8 @@ class Net::Ipv4_packet static Ipv4_address ip_from_string(const char *ip); + static Genode::uint16_t calculate_checksum(Ipv4_packet const &packet); + private: /************************ @@ -161,6 +163,20 @@ class Net::Ipv4_packet template T * data() { return (T *)(_data); } template T const * data() const { return (T const *)(_data); } + /******************************** + ** IPv4 field write-accessors ** + ********************************/ + + void version(Genode::size_t version) { _version = version; } + void header_length(Genode::size_t len) { _header_length = len; } + + void total_length(Genode::uint16_t len) { _total_length = host_to_big_endian(len); } + void time_to_live(Genode::uint8_t ttl) { _time_to_live = ttl; } + + void checksum(Genode::uint16_t checksum) { _header_checksum = host_to_big_endian(checksum); } + + void dst(Ipv4_address ip) { ip.copy(&_dst_addr); } + void src(Ipv4_address ip) { ip.copy(&_src_addr); } /*************** ** Operators ** diff --git a/repos/os/src/lib/net/ipv4.cc b/repos/os/src/lib/net/ipv4.cc index 092ace537f..fdb98a897e 100644 --- a/repos/os/src/lib/net/ipv4.cc +++ b/repos/os/src/lib/net/ipv4.cc @@ -62,6 +62,21 @@ Ipv4_packet::Ipv4_address Ipv4_packet::ip_from_string(const char *ip) return ip_addr; } +Genode::uint16_t Ipv4_packet::calculate_checksum(Ipv4_packet const &packet) +{ + Genode::uint16_t const *data = packet.header(); + Genode::uint32_t const sum = host_to_big_endian(data[0]) + + host_to_big_endian(data[1]) + + host_to_big_endian(data[2]) + + host_to_big_endian(data[3]) + + host_to_big_endian(data[4]) + + host_to_big_endian(data[6]) + + host_to_big_endian(data[7]) + + host_to_big_endian(data[8]) + + host_to_big_endian(data[9]); + return ~((0xFFFF & sum) + (sum >> 16)); +} + const Ipv4_packet::Ipv4_address Ipv4_packet::CURRENT((Genode::uint8_t)0x00); const Ipv4_packet::Ipv4_address Ipv4_packet::BROADCAST((Genode::uint8_t)0xFF);