From 7ea54ce2df6b30663aebb88925b02b25c40c4b4c Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sat, 3 Sep 2016 17:16:29 +0200 Subject: [PATCH] Expose net structures to the tlib --- kernel/include/net/icmp_layer.hpp | 21 +--------- kernel/include/net/ip_address.hpp | 51 ------------------------ kernel/include/net/ip_layer.hpp | 2 +- kernel/include/net/network.hpp | 3 +- tlib/include/tlib/net_constants.hpp | 62 +++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 72 deletions(-) delete mode 100644 kernel/include/net/ip_address.hpp diff --git a/kernel/include/net/icmp_layer.hpp b/kernel/include/net/icmp_layer.hpp index 27700725..9c5ef74d 100644 --- a/kernel/include/net/icmp_layer.hpp +++ b/kernel/include/net/icmp_layer.hpp @@ -10,6 +10,8 @@ #include +#include "tlib/net_constants.hpp" + #include "net/network.hpp" #include "net/ip_layer.hpp" @@ -31,25 +33,6 @@ struct echo_request_header { static_assert(sizeof(echo_request_header) == sizeof(header::rest), "Invalid size for echo request header"); -enum class type : uint8_t { - ECHO_REPLY = 0, - UNREACHABLE = 3, - SOURCE_QUENCH = 4, - REDICT = 5, - ECHO_REQUEST = 8, - ROUTER_ADVERTISEMENT = 9, - ROUTER_SOLICITATION = 10, - TIME_EXCEEDED = 11, - PARAMETER_PROBLEM = 12, - TIMESTAMP = 13, - TIMESTAMP_REPLY = 14, - INFORMATION_REQUEST = 15, - INFORMATION_REPLY = 16, - ADDRESS_MASK_REQUEST = 17, - ADDRESS_MASK_REPLY = 18, - TRACEROUTE = 30 -}; - void decode(network::interface_descriptor& interface, network::ethernet::packet& packet); network::ethernet::packet prepare_packet(network::interface_descriptor& interface, network::ip::address target_ip, size_t payload_size, type t, size_t code); diff --git a/kernel/include/net/ip_address.hpp b/kernel/include/net/ip_address.hpp deleted file mode 100644 index 41b313f1..00000000 --- a/kernel/include/net/ip_address.hpp +++ /dev/null @@ -1,51 +0,0 @@ -//======================================================================= -// Copyright Baptiste Wicht 2013-2016. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -//======================================================================= - -#ifndef NET_IP_ADDRESS_H -#define NET_IP_ADDRESS_H - -#include - -namespace network { - -namespace ip { - -//TODO Maybe packed 4x8 is better - -struct address { - uint32_t raw_address = 0; - - address(){} - address(uint32_t raw) : raw_address(raw) {} - - uint8_t operator()(size_t index) const { - return (raw_address >> ((3 - index) * 8)) & 0xFF; - } - - void set_sub(size_t index, uint8_t value){ - raw_address |= uint32_t(value) << ((3 - index) * 8); - } - - bool operator==(const address& rhs) const { - return this->raw_address == rhs.raw_address; - } -}; - -inline address make_address(uint8_t a, uint8_t b, uint8_t c, uint8_t d){ - address addr; - addr.set_sub(0, a); - addr.set_sub(1, b); - addr.set_sub(2, c); - addr.set_sub(3, d); - return addr; -} - -} // end of ip namespace - -} // end of network namespace - -#endif diff --git a/kernel/include/net/ip_layer.hpp b/kernel/include/net/ip_layer.hpp index b4e5f858..c1ccbd7c 100644 --- a/kernel/include/net/ip_layer.hpp +++ b/kernel/include/net/ip_layer.hpp @@ -11,7 +11,7 @@ #include #include "net/network.hpp" -#include "net/ip_address.hpp" +#include "tlib/net_constants.hpp" namespace network { diff --git a/kernel/include/net/network.hpp b/kernel/include/net/network.hpp index 36363d8a..497e9fcc 100644 --- a/kernel/include/net/network.hpp +++ b/kernel/include/net/network.hpp @@ -15,8 +15,9 @@ #include #include +#include "tlib/net_constants.hpp" + #include "net/ethernet_packet.hpp" -#include "net/ip_address.hpp" namespace network { diff --git a/tlib/include/tlib/net_constants.hpp b/tlib/include/tlib/net_constants.hpp index 75e303c6..359f5dfa 100644 --- a/tlib/include/tlib/net_constants.hpp +++ b/tlib/include/tlib/net_constants.hpp @@ -14,6 +14,61 @@ THOR_NAMESPACE(tlib, network) { +namespace ip { + +struct address { + uint32_t raw_address = 0; + + address(){} + address(uint32_t raw) : raw_address(raw) {} + + uint8_t operator()(size_t index) const { + return (raw_address >> ((3 - index) * 8)) & 0xFF; + } + + void set_sub(size_t index, uint8_t value){ + raw_address |= uint32_t(value) << ((3 - index) * 8); + } + + bool operator==(const address& rhs) const { + return this->raw_address == rhs.raw_address; + } +}; + +inline address make_address(uint8_t a, uint8_t b, uint8_t c, uint8_t d){ + address addr; + addr.set_sub(0, a); + addr.set_sub(1, b); + addr.set_sub(2, c); + addr.set_sub(3, d); + return addr; +} + +} // end of namespace ip + +namespace icmp { + +enum class type : uint8_t { + ECHO_REPLY = 0, + UNREACHABLE = 3, + SOURCE_QUENCH = 4, + REDICT = 5, + ECHO_REQUEST = 8, + ROUTER_ADVERTISEMENT = 9, + ROUTER_SOLICITATION = 10, + TIME_EXCEEDED = 11, + PARAMETER_PROBLEM = 12, + TIMESTAMP = 13, + TIMESTAMP_REPLY = 14, + INFORMATION_REQUEST = 15, + INFORMATION_REPLY = 16, + ADDRESS_MASK_REQUEST = 17, + ADDRESS_MASK_REPLY = 18, + TRACEROUTE = 30 +}; + +} // end of namespace icmp + enum class socket_domain : size_t { AF_INET }; @@ -26,6 +81,13 @@ enum class socket_protocol : size_t { ICMP }; +struct icmp_packet_descriptor { + size_t payload_size; + ip::address target_ip; + icmp::type type; + size_t code; +}; + } // end of network namespace #endif