Max OS 0.3
Loading...
Searching...
No Matches
icmp.cpp
Go to the documentation of this file.
1
9#include <net/icmp.h>
10
11using namespace MaxOS;
12using namespace MaxOS::common;
13using namespace MaxOS::net;
14
22: IPV4PayloadHandler(internet_protocol_handler, 0x01)
23{
24 this -> error_messages = error_messages;
25}
26
27
28
29InternetControlMessageProtocol::~InternetControlMessageProtocol() = default;
46 uint32_t size)
47{
48
49 error_messages -> write("ICMP received a packet\n");
50
51 // Check if the size is at least the size of the header
52 if(size < sizeof(ICMPHeader)){
53 return false;
54 }
55
56 // Cast the payload to the ICMP header
58
59 switch (icmp -> type) {
60
61 case 0: // Echo reply
62 break;
63
64 case 8: // Echo request
65
66 // Create a response
67 icmp -> type = 0; // Echo reply
68 icmp -> checksum = 0; // Reset the checksum
69 icmp -> checksum = InternetProtocolHandler::checksum((uint16_t*) &icmp, sizeof(ICMPHeader)); // Calculate the checksum
70
71 return true; //Send data back
72
73 }
74
75 return false;
76
77}
78
85
86 error_messages -> write("ICMP: Sending echo request\n");
87
88 ICMPHeader icmp = {};
89 icmp.type = 8; // Echo request
90 icmp.code = 0; // Code must be 0
91 icmp.checksum = 0; // Checksum must be 0 to calculate it
92 icmp.data = 0x69420; // Data
93
95
96 send(ip_be, (uint8_t*) &icmp, sizeof(ICMPHeader));
97
98 error_messages -> write("ICMP: Echo request sent\n");
99}
A stream that strings can be written to.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles the payload of a specific IP protocol.
Definition ipv4.h:66
void send(InternetProtocolAddress destination_ip, uint8_t *payload_data, uint32_t size)
Sends an IP packet.
Definition ipv4.cpp:93
InternetControlMessageProtocol(InternetProtocolHandler *internet_protocol_handler, common::OutputStream *error_messages)
Constructs an InternetControlMessageProtocol handler.
Definition icmp.cpp:21
bool handle_internet_protocol_payload(net::InternetProtocolAddress src_ip_be, net::InternetProtocolAddress dst_ip_be, uint8_t *payload_data, uint32_t size) final
Called by the InternetProtocolProvider when a new packet has arrived.
Definition icmp.cpp:43
void request_echo_reply(uint32_t ip_be)
Sends an ICMP echo request to the specified IP address.
Definition icmp.cpp:84
Handles IPv4 packets over Ethernet frames.
Definition ipv4.h:85
static uint16_t checksum(const uint16_t *data, uint32_t length_in_bytes)
Creates a checksum for the given data.
Definition ipv4.cpp:249
Defines the Internet Control Message Protocol (ICMP) handler for network communication.
uint32_t InternetProtocolAddress
An IPv4 address.
Definition ipv4.h:18
The header of an ICMP packet.
Definition icmp.h:27