From b2f7916b9f911f782ef6a6513a52cfa24420f71c Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Tue, 23 Aug 2016 21:16:12 +0200 Subject: [PATCH] Refactorings --- kernel/include/net/icmp_layer.hpp | 3 ++ kernel/src/net/icmp_layer.cpp | 51 ++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/kernel/include/net/icmp_layer.hpp b/kernel/include/net/icmp_layer.hpp index efcda3eb..27700725 100644 --- a/kernel/include/net/icmp_layer.hpp +++ b/kernel/include/net/icmp_layer.hpp @@ -52,6 +52,9 @@ enum class type : uint8_t { 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); +void finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p); + void ping(network::interface_descriptor& interface, network::ip::address addr); } // end of icmp namespace diff --git a/kernel/src/net/icmp_layer.cpp b/kernel/src/net/icmp_layer.cpp index 7df2b129..7d076fa7 100644 --- a/kernel/src/net/icmp_layer.cpp +++ b/kernel/src/net/icmp_layer.cpp @@ -66,6 +66,34 @@ void network::icmp::decode(network::interface_descriptor& /*interface*/, network } } +network::ethernet::packet network::icmp::prepare_packet(network::interface_descriptor& interface, network::ip::address target_ip, size_t payload_size, type t, size_t code){ + // Ask the IP layer to craft a packet + auto packet = network::ip::prepare_packet(interface, sizeof(header) + payload_size, target_ip, 0x01); + + // Set the ICMP header + + auto* icmp_header = reinterpret_cast(packet.payload + packet.index); + + icmp_header->type = static_cast(t); + icmp_header->code = code; + + packet.index += sizeof(header) - sizeof(uint32_t); + + return packet; +} + +void network::icmp::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& packet){ + packet.index -= sizeof(header) - sizeof(uint32_t); + + auto* icmp_header = reinterpret_cast(packet.payload + packet.index); + + // Compute the checksum + compute_checksum(icmp_header, 0); + + // Give the packet to the IP layer for finalization + network::ip::finalize_packet(interface, packet); +} + void network::icmp::ping(network::interface_descriptor& interface, network::ip::address target_ip){ logging::logf(logging::log_level::TRACE, "icmp: Ping %u.%u.%u.%u \n", uint64_t(target_ip(0)), uint64_t(target_ip(1)), uint64_t(target_ip(2)), uint64_t(target_ip(3))); @@ -74,27 +102,16 @@ void network::icmp::ping(network::interface_descriptor& interface, network::ip:: logging::logf(logging::log_level::TRACE, "icmp: Target MAC Address: %h\n", target_mac); - // Ask the IP layer to craft a packet - auto packet = network::ip::prepare_packet(interface, sizeof(header), target_ip, 0x01); + // Ask the ICMP layer to craft a packet + auto packet = network::icmp::prepare_packet(interface, target_ip, 0, type::ECHO_REQUEST, 0); - // Set the ICMP header + // Set the Command header - auto* icmp_header = reinterpret_cast(packet.payload + packet.index); - - icmp_header->type = static_cast(type::ECHO_REQUEST); - icmp_header->code = 0; - - // Set the command header - - auto* command_header = reinterpret_cast(&icmp_header->rest); + auto* command_header = reinterpret_cast(packet.payload + packet.index); command_header->identifier = 0x666; command_header->sequence = echo_sequence++; - // Compute the checksum - - compute_checksum(icmp_header, 0); - - // Give the packet to the IP layer for finalization - network::ip::finalize_packet(interface, packet); + // Send the packet back to ICMP + network::icmp::finalize_packet(interface, packet); }