Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::net::InternetControlMessageProtocol Class Reference

Handles ICMP packets. More...

#include <icmp.h>

Inheritance diagram for MaxOS::net::InternetControlMessageProtocol:
MaxOS::net::IPV4PayloadHandler

Public Member Functions

 InternetControlMessageProtocol (InternetProtocolHandler *internet_protocol_handler, common::OutputStream *error_messages)
 Constructs an InternetControlMessageProtocol handler.
 
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.
 
void request_echo_reply (uint32_t ip_be)
 Sends an ICMP echo request to the specified IP address.
 

Detailed Description

Handles ICMP packets.

Definition at line 41 of file icmp.h.

Constructor & Destructor Documentation

◆ InternetControlMessageProtocol()

InternetControlMessageProtocol::InternetControlMessageProtocol ( InternetProtocolHandler internet_protocol_handler,
common::OutputStream error_messages 
)

Constructs an InternetControlMessageProtocol handler.

Parameters
internet_protocol_handlerThe Internet protocol handler to use.
error_messagesThe output stream to use for error messages.

Definition at line 21 of file icmp.cpp.

23{
24 this -> error_messages = error_messages;
25}
Handles the payload of a specific IP protocol.
Definition ipv4.h:66
InternetProtocolHandler * internet_protocol_handler
The Internet protocol handler this payload handler is connected to.
Definition ipv4.h:70

Member Function Documentation

◆ handle_internet_protocol_payload()

bool InternetControlMessageProtocol::handle_internet_protocol_payload ( net::InternetProtocolAddress  src_ip_be,
net::InternetProtocolAddress  dst_ip_be,
uint8_t payload_data,
uint32_t  size 
)
finalvirtual

Called by the InternetProtocolProvider when a new packet has arrived.

Parameters
src_ip_beThe source IP address of the packet
dst_ip_beThe destination IP address of the packet
payload_dataThe payload of the packet
sizeThe size of the payload
Returns
True if the packet is to be sent back to the sender, false otherwise
Todo:

Reply to ping requests

Implement the rest of the ICMP messages

Reimplemented from MaxOS::net::IPV4PayloadHandler.

Definition at line 43 of file icmp.cpp.

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}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
static uint16_t checksum(const uint16_t *data, uint32_t length_in_bytes)
Creates a checksum for the given data.
Definition ipv4.cpp:249
The header of an ICMP packet.
Definition icmp.h:27

References MaxOS::net::InternetProtocolHandler::checksum().

◆ request_echo_reply()

void InternetControlMessageProtocol::request_echo_reply ( uint32_t  ip_be)

Sends an ICMP echo request to the specified IP address.

Parameters
ip_beThe IP address to send the request to

Definition at line 84 of file icmp.cpp.

84 {
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}
void send(InternetProtocolAddress destination_ip, uint8_t *payload_data, uint32_t size)
Sends an IP packet.
Definition ipv4.cpp:93

References MaxOS::net::InternetProtocolHandler::checksum(), and MaxOS::net::IPV4PayloadHandler::send().


The documentation for this class was generated from the following files: