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

Handles IPv4 packets over Ethernet frames. More...

#include <ipv4.h>

Inheritance diagram for MaxOS::net::InternetProtocolHandler:
MaxOS::net::EthernetFramePayloadHandler

Public Member Functions

 InternetProtocolHandler (EthernetFrameHandler *backend, InternetProtocolAddress own_internet_protocol_address, InternetProtocolAddress default_gateway_internet_protocol_address, SubnetMask subnet_mask, common::OutputStream *error_messages)
 Construct a new Internet Protocol Handler object.
 
bool handle_ethernetframe_payload (uint8_t *ethernetframe_payload, uint32_t size) override
 Called when an IP packet is received.
 
void send_internet_protocol_packet (uint32_t dst_ip_be, uint8_t protocol, const uint8_t *data, uint32_t size)
 Sends an IP packet.
 
InternetProtocolAddress get_internet_protocol_address () const
 Gets the IP address of this device.
 
drivers::ethernet::MediaAccessControlAddress get_media_access_control_address ()
 Gets the MAC address of this device.
 
void connect_ipv_4_payload_handler (IPV4PayloadHandler *ipv_4_payload_handler)
 Connects an IP protocol payload handler.
 
- Public Member Functions inherited from MaxOS::net::EthernetFramePayloadHandler
 EthernetFramePayloadHandler (EthernetFrameHandler *frame_handler, uint16_t handled_type)
 Construct a new Ether Frame Payload Handler object.
 
 ~EthernetFramePayloadHandler ()
 Destroy the EtherFrameHandler:: EtherFrameHandler object, Removes it from the handler list.
 
void send (uint64_t destination, uint8_t *data, uint32_t size)
 send an packet via the backend driver
 

Static Public Member Functions

static uint16_t checksum (const uint16_t *data, uint32_t length_in_bytes)
 Creates a checksum for the given data.
 
static InternetProtocolAddress create_internet_protocol_address (uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4)
 Creates an IP address from four digits.
 
static InternetProtocolAddress parse (string address)
 Parses a string representation of an IP address.
 
static SubnetMask create_subnet_mask (uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4)
 Creates a subnet mask from four digits.
 

Protected Member Functions

void register_ipv_4_address_resolver (IPV4AddressResolver *ipv4_resolver)
 Registers an IP address resolver.
 

Protected Attributes

common::Map< uint8_t, IPV4PayloadHandler * > ipv_4_payload_handlers
 Map of IP protocol numbers to their payload handlers.
 
IPV4AddressResolverresolver = nullptr
 The IP address resolver.
 
common::OutputStreamerror_messages
 Stream to output error messages to.
 
InternetProtocolAddress own_internet_protocol_address
 The IP address of this device.
 
InternetProtocolAddress default_gateway_internet_protocol_address
 The IP address of the default gateway.
 
SubnetMask subnet_mask
 The subnet mask.
 
- Protected Attributes inherited from MaxOS::net::EthernetFramePayloadHandler
EthernetFrameHandlerframe_handler
 The Ethernet frame handler this payload handler is connected to.
 
uint16_t handled_type
 The Ethernet frame type this handler handles.
 

Friends

class IPV4AddressResolver
 

Detailed Description

Handles IPv4 packets over Ethernet frames.

Definition at line 85 of file ipv4.h.

Constructor & Destructor Documentation

◆ InternetProtocolHandler()

InternetProtocolHandler::InternetProtocolHandler ( EthernetFrameHandler backend,
InternetProtocolAddress  own_internet_protocol_address,
InternetProtocolAddress  default_gateway_internet_protocol_address,
SubnetMask  subnet_mask,
common::OutputStream error_messages 
)

Construct a new Internet Protocol Handler object.

Parameters
backendThe backend Ethernet frame handler.
own_internet_protocol_addressThe IP address of this device.
default_gateway_internet_protocol_addressThe IP address of the default gateway.
subnet_maskThe subnet mask.
error_messagesWhere to write error messages.

Definition at line 110 of file ipv4.cpp.

112 //Store vars
115 this->subnet_mask = subnet_mask;
117}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles a specific type of Ethernet Frame payload.
SubnetMask subnet_mask
The subnet mask.
Definition ipv4.h:98
common::OutputStream * error_messages
Stream to output error messages to.
Definition ipv4.h:94
InternetProtocolAddress default_gateway_internet_protocol_address
The IP address of the default gateway.
Definition ipv4.h:97
InternetProtocolAddress own_internet_protocol_address
The IP address of this device.
Definition ipv4.h:96

References default_gateway_internet_protocol_address, error_messages, own_internet_protocol_address, and subnet_mask.

Member Function Documentation

◆ checksum()

uint16_t InternetProtocolHandler::checksum ( const uint16_t data,
uint32_t  length_in_bytes 
)
static

Creates a checksum for the given data.

Parameters
dataThe data to create a checksum for.
length_in_bytesThe length of the data in bytes.
Returns
The checksum.

Definition at line 249 of file ipv4.cpp.

249 {
250
251 uint32_t temp = 0; //Init sum
252
253 for(uint32_t i = 0; i < length_in_bytes /
254 2; i++) //Loop through data (/2 bc bytes)
255 temp += ((data[i] & 0xFF00) >> 8) |
256 ((data[i] & 0x00FF) << 8); //Add data to sum in big endian
257
258 if(length_in_bytes %
259 2) //If there is an odd number of bytes
260 temp += ((uint16_t) ((char*) data)[length_in_bytes - 1])
261 << 8; //Add the last byte to the sum
262
263 while(temp &
264 0xFFFF0000) //While there is a carry
265 temp = (temp & 0xFFFF) +
266 (temp >> 16); //Add the carry to the sum
267
268 return ((~temp & 0xFF00) >> 8) | ((~temp & 0x00FF) << 8);
269}

Referenced by handle_ethernetframe_payload(), MaxOS::net::InternetControlMessageProtocol::handle_internet_protocol_payload(), MaxOS::net::InternetControlMessageProtocol::request_echo_reply(), send_internet_protocol_packet(), and MaxOS::net::TransmissionControlProtocolHandler::send_transmission_control_protocol_packet().

◆ connect_ipv_4_payload_handler()

void InternetProtocolHandler::connect_ipv_4_payload_handler ( IPV4PayloadHandler ipv_4_payload_handler)

Connects an IP protocol payload handler.

Parameters
ipv_4_payload_handlerThe payload handler to connect.

Definition at line 363 of file ipv4.cpp.

363 {
365}
common::Map< uint8_t, IPV4PayloadHandler * > ipv_4_payload_handlers
Map of IP protocol numbers to their payload handlers.
Definition ipv4.h:91

References ipv_4_payload_handlers.

Referenced by MaxOS::net::IPV4PayloadHandler::IPV4PayloadHandler().

◆ create_internet_protocol_address()

InternetProtocolAddress InternetProtocolHandler::create_internet_protocol_address ( uint8_t  digit1,
uint8_t  digit2,
uint8_t  digit3,
uint8_t  digit4 
)
static

Creates an IP address from four digits.

Parameters
digit1The first digit.
digit2The second digit.
digit3The third digit.
digit4The fourth digit.
Returns
The created IP address.

Definition at line 292 of file ipv4.cpp.

292 {
294 result = (result << 8) | digit3;
295 result = (result << 8) | digit2;
296 result = (result << 8) | digit1;
297 return result;
298}
uint32_t InternetProtocolAddress
An IPv4 address.
Definition ipv4.h:18

Referenced by create_subnet_mask(), and parse().

◆ create_subnet_mask()

SubnetMask InternetProtocolHandler::create_subnet_mask ( uint8_t  digit1,
uint8_t  digit2,
uint8_t  digit3,
uint8_t  digit4 
)
static

Creates a subnet mask from four digits.

Parameters
digit1The first digit.
digit2The second digit.
digit3The third digit.
digit4The fourth digit.
Returns
The created subnet mask.

Definition at line 336 of file ipv4.cpp.

336 {
338}
static InternetProtocolAddress create_internet_protocol_address(uint8_t digit1, uint8_t digit2, uint8_t digit3, uint8_t digit4)
Creates an IP address from four digits.
Definition ipv4.cpp:292
uint32_t SubnetMask
A subnet mask.
Definition ipv4.h:19

References create_internet_protocol_address().

◆ get_internet_protocol_address()

◆ get_media_access_control_address()

MediaAccessControlAddress InternetProtocolHandler::get_media_access_control_address ( )

Gets the MAC address of this device.

Returns
The MAC address.

Definition at line 354 of file ipv4.cpp.

354 {
355 return frame_handler->get_mac();
356}
drivers::ethernet::MediaAccessControlAddress get_mac()
Get the MAC address of this device.
EthernetFrameHandler * frame_handler
The Ethernet frame handler this payload handler is connected to.

References MaxOS::net::EthernetFramePayloadHandler::frame_handler, and MaxOS::net::EthernetFrameHandler::get_mac().

Referenced by MaxOS::net::AddressResolutionProtocol::handle_ethernetframe_payload().

◆ handle_ethernetframe_payload()

bool InternetProtocolHandler::handle_ethernetframe_payload ( uint8_t ethernetframe_payload,
uint32_t  size 
)
overridevirtual

Called when an IP packet is received.

Parameters
ethernetframe_payloadThe payload of the IP packet.
sizeThe size of the IP packet.
Returns
True if the packet is to be sent back, false otherwise.
Todo:
Set the identifier when sending packets back

Reimplemented from MaxOS::net::EthernetFramePayloadHandler.

Definition at line 130 of file ipv4.cpp.

130 {
131
132 error_messages->write("IP: Handling packet\n");
133
134 //Check if the size is big enough to contain an ethernet frame
135 if(size < sizeof(IPV4Header))
136 return false;
137
138 //Convert to struct for easier use
140 bool send_back = false;
141
142 //Only handle if it is for this device
143 if(ip_message->destination_ip == get_internet_protocol_address()) {
144 uint32_t length = ip_message->total_length; //Get length of the message
145 if(length >
146 size) //Check if the length is bigger than the size of the message
147 length = size; //If so, set length to size (this stops heartbleed attacks as it will not read past the end of the message, which the attacker could have filled with data)
148
149 // Get the handler for the protocol
153 if(handler != nullptr) {
154 send_back = handler->handle_internet_protocol_payload(ip_message->source_ip, ip_message->destination_ip,
156 length - sizeof(IPV4Header));
157 }
158 }
159
160
161 }
162
163 //If the data is to be sent back again
164 if(send_back) {
165
166 //Swap source and destination
167 uint32_t temp = ip_message->destination_ip; //Store destination IP
168 ip_message->destination_ip = ip_message->source_ip; //Set destination IP to source IP
169 ip_message->source_ip = temp; //Set source IP to destination IP
170
171 ip_message->time_to_live = 0x40; //Reset TTL
172 ip_message->checksum = checksum((uint16_t*) ip_message, 4 *
173 ip_message->header_length); //Reset checksum as the source and destination IP have changed so has the time to live and therefore there is a different header
174
175 }
176
177 error_messages->write("IP: Handled packet\n");
178 return send_back;
179}
void write(string string_to_write) override
Writes a string to the output stream.
Handles the payload of a specific IP protocol.
Definition ipv4.h:66
InternetProtocolAddress get_internet_protocol_address() const
Gets the IP address of this device.
Definition ipv4.cpp:345
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 IPv4 packet.
Definition ipv4.h:28

References checksum(), error_messages, get_internet_protocol_address(), ipv_4_payload_handlers, and MaxOS::common::OutputStream::write().

◆ parse()

InternetProtocolAddress InternetProtocolHandler::parse ( string  address)
static

Parses a string representation of an IP address.

Parameters
addressThe string representation of the IP address.
Returns
The parsed IP address.

Definition at line 306 of file ipv4.cpp.

306 {
307 uint8_t digits[4];
308
310 for(unsigned char& digit : digits)
311 digit = 0;
312
313 for(size_t i = 0; i < address.length(); i++) {
314 if(address[i] == '.') {
316 continue;
317 }
318
319 digits[current_digit] *= 10;
320 digits[current_digit] += address[i] - '0';
321 }
322
324}

References create_internet_protocol_address().

◆ register_ipv_4_address_resolver()

void InternetProtocolHandler::register_ipv_4_address_resolver ( IPV4AddressResolver ipv4_resolver)
protected

Registers an IP address resolver.

Parameters
ipv4_resolverThe resolver to register.

Definition at line 276 of file ipv4.cpp.

276 {
277
278 this->resolver = ipv4_resolver;
279
280}
IPV4AddressResolver * resolver
The IP address resolver.
Definition ipv4.h:93

References resolver.

Referenced by MaxOS::net::IPV4AddressResolver::IPV4AddressResolver().

◆ send_internet_protocol_packet()

void InternetProtocolHandler::send_internet_protocol_packet ( uint32_t  dst_ip_be,
uint8_t  protocol,
const uint8_t data,
uint32_t  size 
)

Sends an IP packet.

Parameters
dst_ip_beThe destination IP address.
protocolThe protocol of the IP packet.
dataThe payload of the IP packet.
sizeThe size of the IP packet.

Definition at line 190 of file ipv4.cpp.

190 {
191
192 auto* buffer = (uint8_t*) MemoryManager::kmalloc(
193 sizeof(IPV4Header) + size); //Allocate memory for the message
194 auto* message = (IPV4Header*) buffer; //Convert to struct for easier use
195
196 message->version = 4; //Set version
197 message->header_length = sizeof(IPV4Header) /
198 4; //Set header length
199 message->type_of_service = 0; //Set type of service (not private)
200
201 message->total_length = size +
202 sizeof(IPV4Header); //Set total length
203 message->total_length = ((message->total_length & 0xFF00)
204 >> 8) // Convert to big endian (Swap bytes)
205 | ((message->total_length & 0x00FF)
206 << 8); // Convert to big endian (Swap bytes)
207
208 message->identifier = 0x100; //Set identification TODO: do properly
209 message->flags_and_offset = 0x0040; //Set flags/offset, 0x40 because we are not fragmenting (TODO: doesnt work for packets bigger than 1500 bytes)
210
211 message->time_to_live = 0x40; //Set time to live
212 message->protocol = protocol; //Set protocol
213
214 message->destination_ip = dst_ip_be; //Set destination IP
215 message->source_ip = get_internet_protocol_address(); //Set source IP
216
217 message->checksum = 0; //Set checksum to 0, init with 0 as checksum funct will also add this value
218 message->checksum = checksum((uint16_t*) message,
219 sizeof(IPV4Header)); //Calculate checksum
220
221 //Copy data
222 uint8_t* data_buffer = buffer +
223 sizeof(IPV4Header); //Get pointer to the data
224 for(uint32_t i = 0; i <
225 size; i++) //Loop through data
226 data_buffer[i] = data[i]; //Copy data
227
228 //Check if the destination is on the same subnet, The if condition determines if the destination device is on the same Local network as the source device . and if they are not on the same local network then we resolve the ip address of the gateway .
229 InternetProtocolAddress route = dst_ip_be; //Set route to destination IP by default
231 subnet_mask)) //Check if the destination is on the same subnet
232 route = default_gateway_internet_protocol_address; //If not, set route to gateway IP
233 //Print debug info
235
236 //Send message
237 frame_handler->send_ethernet_frame(mac, this->handled_type, buffer, size + sizeof(IPV4Header)); //Send message
239 buffer); //Free memory
240}
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
static void kfree(void *pointer)
Frees a block of memory using the kernel memory manager.
void send_ethernet_frame(uint64_t destination_mac, uint16_t frame_type, uint8_t *data, uint32_t size)
send an packet via the backend driver
uint16_t handled_type
The Ethernet frame type this handler handles.
virtual drivers::ethernet::MediaAccessControlAddress resolve(InternetProtocolAddress address)
Resolves an IP address to a MAC address. (Default, returns broadcast address, override for use)
Definition ipv4.cpp:37

References checksum(), default_gateway_internet_protocol_address, MaxOS::net::EthernetFramePayloadHandler::frame_handler, get_internet_protocol_address(), MaxOS::net::EthernetFramePayloadHandler::handled_type, MaxOS::memory::MemoryManager::kfree(), MaxOS::memory::MemoryManager::kmalloc(), own_internet_protocol_address, MaxOS::net::IPV4AddressResolver::resolve(), resolver, MaxOS::net::EthernetFrameHandler::send_ethernet_frame(), and subnet_mask.

Referenced by MaxOS::net::IPV4PayloadHandler::send().

Friends And Related Symbol Documentation

◆ IPV4AddressResolver

friend class IPV4AddressResolver
friend

Definition at line 87 of file ipv4.h.

Member Data Documentation

◆ default_gateway_internet_protocol_address

InternetProtocolAddress MaxOS::net::InternetProtocolHandler::default_gateway_internet_protocol_address
protected

The IP address of the default gateway.

Definition at line 97 of file ipv4.h.

Referenced by InternetProtocolHandler(), and send_internet_protocol_packet().

◆ error_messages

common::OutputStream* MaxOS::net::InternetProtocolHandler::error_messages
protected

Stream to output error messages to.

Definition at line 94 of file ipv4.h.

Referenced by handle_ethernetframe_payload(), and InternetProtocolHandler().

◆ ipv_4_payload_handlers

common::Map<uint8_t, IPV4PayloadHandler*> MaxOS::net::InternetProtocolHandler::ipv_4_payload_handlers
protected

Map of IP protocol numbers to their payload handlers.

Definition at line 91 of file ipv4.h.

Referenced by connect_ipv_4_payload_handler(), and handle_ethernetframe_payload().

◆ own_internet_protocol_address

InternetProtocolAddress MaxOS::net::InternetProtocolHandler::own_internet_protocol_address
protected

The IP address of this device.

Definition at line 96 of file ipv4.h.

Referenced by get_internet_protocol_address(), InternetProtocolHandler(), and send_internet_protocol_packet().

◆ resolver

IPV4AddressResolver* MaxOS::net::InternetProtocolHandler::resolver = nullptr
protected

The IP address resolver.

Definition at line 93 of file ipv4.h.

Referenced by register_ipv_4_address_resolver(), and send_internet_protocol_packet().

◆ subnet_mask

SubnetMask MaxOS::net::InternetProtocolHandler::subnet_mask
protected

The subnet mask.

Definition at line 98 of file ipv4.h.

Referenced by InternetProtocolHandler(), and send_internet_protocol_packet().


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