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

Handles ARP requests and replies. More...

#include <arp.h>

Inheritance diagram for MaxOS::net::AddressResolutionProtocol:
MaxOS::net::EthernetFramePayloadHandler MaxOS::net::IPV4AddressResolver

Public Member Functions

 AddressResolutionProtocol (EthernetFrameHandler *ethernet_frame_handler, InternetProtocolHandler *internet_protocol_handler, common::OutputStream *error_messages)
 Constructs an AddressResolutionProtocol handler.
 
bool handle_ethernetframe_payload (uint8_t *etherframe_payload, uint32_t size) final
 Called when an ARP packet is received.
 
void request_mac_address (InternetProtocolAddress address)
 Request the MAC address of a given IP address.
 
drivers::ethernet::MediaAccessControlAddress resolve (InternetProtocolAddress address) final
 Get the MAC address from an IP via ARP.
 
void store (InternetProtocolAddress internet_protocol_address, drivers::ethernet::MediaAccessControlAddress media_access_control_address) final
 store a mapping of an IP address to a MAC address.
 
- 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
 
- Public Member Functions inherited from MaxOS::net::IPV4AddressResolver
 IPV4AddressResolver (InternetProtocolHandler *internet_protocol_handler)
 Construct a new IPV4 Address Resolver object and register it with the Internet Protocol Handler.
 

Additional Inherited Members

- 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.
 

Detailed Description

Handles ARP requests and replies.

Definition at line 47 of file arp.h.

Constructor & Destructor Documentation

◆ AddressResolutionProtocol()

net::AddressResolutionProtocol::AddressResolutionProtocol ( EthernetFrameHandler ethernet_frame_handler,
InternetProtocolHandler internet_protocol_handler,
common::OutputStream error_messages 
)

Constructs an AddressResolutionProtocol handler.

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

Definition at line 25 of file arp.cpp.

27 IPV4AddressResolver(internet_protocol_handler) {
28 this->internet_protocol_handler = internet_protocol_handler;
29 this->error_messages = error_messages;
30}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Handles a specific type of Ethernet Frame payload.
Resolves IP addresses to MAC addresses.
Definition ipv4.h:53

Member Function Documentation

◆ handle_ethernetframe_payload()

bool AddressResolutionProtocol::handle_ethernetframe_payload ( uint8_t ethernetframePayload,
uint32_t  size 
)
finalvirtual

Called when an ARP packet is received.

Parameters
ethernetframePayloadThe payload of the ARP packet.
sizeThe size of the ARP packet.
Returns
True if the device should send a response, false otherwise.

Reimplemented from MaxOS::net::EthernetFramePayloadHandler.

Definition at line 41 of file arp.cpp.

41 {
42
43 //Check if the size is correct
44 if (size < sizeof(ARPMessage))
45 return false;
46
47 //Convert the payload to an ARP message
49
50 //Check if the message hardware type is Ethernet (BigEndian)
51 if (arp_message->hardware_type == 0x100) {
52
53 if (arp_message->protocol == 0x0008 //Check if the protocol is IPv4 (BigEndian)
54 && arp_message->hardware_address_size == 6
55 && arp_message->protocol_address_size == 4
56 && arp_message->dst_ip == internet_protocol_handler->get_internet_protocol_address()) {
57
58 switch (arp_message->command) {
59 //Request
60 case 0x0100:
61 arp_message->command = 0x0200; //Set the command to reply
62 arp_message->dst_mac = arp_message->src_mac; //Set the destination MAC to the source MAC
63 arp_message->dst_ip = arp_message->src_ip; //Set the destination IP to the source IP
64 arp_message->src_mac = internet_protocol_handler->get_media_access_control_address(); //Set the source MAC to this MAC
65 arp_message->src_ip = internet_protocol_handler->get_internet_protocol_address(); //Set the source IP to this IP
66 return true;
67
68 //Response
69 case 0x0200:
70 address_cache.insert((InternetProtocolAddress) arp_message->src_ip,
71 (MediaAccessControlAddress) arp_message->src_mac); //Insert the MAC address into the cache
72 break;
73
74 default:
75 break;
76
77 }
78
79 }
80
81 }
82
83 //By default, don't send anything back
84 return false;
85
86
87}
void insert(Key, Value)
Updates the value of an element, or adds a new element if it does not exist.
Definition map.h:253
InternetProtocolAddress get_internet_protocol_address() const
Gets the IP address of this device.
Definition ipv4.cpp:345
drivers::ethernet::MediaAccessControlAddress get_media_access_control_address()
Gets the MAC address of this device.
Definition ipv4.cpp:354
uint64_t MediaAccessControlAddress
Used to make MAC addresses more readable.
Definition ethernet.h:21
An ARP message.
Definition arp.h:26

References MaxOS::net::InternetProtocolHandler::get_internet_protocol_address(), MaxOS::net::InternetProtocolHandler::get_media_access_control_address(), and MaxOS::common::Map< Key, Value >::insert().

◆ request_mac_address()

void AddressResolutionProtocol::request_mac_address ( InternetProtocolAddress  address)

Request the MAC address of a given IP address.

Parameters
addressThe IP address in BigEndian.

Definition at line 95 of file arp.cpp.

95 {
96
97 //When a MAC address is requested, instantiate a new ARP message block on the stack
99
100 //Set the message's values
101 arp_message.hardware_type = 0x0100; //Ethernet, encoded in BigEndian
102 arp_message.protocol = 0x0008; //IPv4, encoded in BigEndian
103 arp_message.hardware_address_size = 6; //MAC address size
104 arp_message.protocol_address_size = 4; //IPv4 address size
105 arp_message.command = 0x0100; //Request, encoded in BigEndian
106
107 //Set the message's source and destination
108 arp_message.src_mac = frame_handler->get_mac(); //Set the source MAC address to the backend's MAC address
109 arp_message.src_ip = internet_protocol_handler->get_internet_protocol_address(); //Set the source IP address to the backend's IP address
110 arp_message.dst_mac = 0xFFFFFFFFFFFF; //Set the destination MAC address to broadcast
111 arp_message.dst_ip = address; //Set the destination IP address to the requested IP address
112
113 //Send the message
114 this->send(arp_message.dst_mac, (uint8_t*) &arp_message, sizeof(ARPMessage));
115
116
117}
drivers::ethernet::MediaAccessControlAddress get_mac()
Get the MAC address of this device.
void send(uint64_t destination, uint8_t *data, uint32_t size)
send an packet via the backend driver
EthernetFrameHandler * frame_handler
The Ethernet frame handler this payload handler is connected to.
uint64_t dst_mac
The MAC address of the target.
Definition arp.h:37

References MaxOS::net::ARPMessage::dst_mac, MaxOS::net::EthernetFramePayloadHandler::frame_handler, MaxOS::net::InternetProtocolHandler::get_internet_protocol_address(), MaxOS::net::EthernetFrameHandler::get_mac(), and MaxOS::net::EthernetFramePayloadHandler::send().

Referenced by resolve().

◆ resolve()

MediaAccessControlAddress AddressResolutionProtocol::resolve ( InternetProtocolAddress  address)
finalvirtual

Get the MAC address from an IP via ARP.

Parameters
addressThe IP address to get the MAC address from.
Returns
The MAC address of the IP address.
Todo:
Should have a timeout in case the address cannot be resolved and avoid infinite loops

Reimplemented from MaxOS::net::IPV4AddressResolver.

Definition at line 128 of file arp.cpp.

128 {
129
131 address); //Check if the MAC address is in the cache
132
133 //If not, request it
134 if (address_cache.end() == cache_iterator) {
135 request_mac_address(address);
136 }
137
138 //This isn't safe because the MAC address might not be in the cache yet or the machine may not be connected to the network (possible infinite loop) //TODO: TIMEOUT
139 while (cache_iterator == address_cache.end()) { //Wait until the MAC address is found
140 cache_iterator = address_cache.find(address);
141 }
142
143 //Return the MAC address
144
145 return cache_iterator->second;
146
147}
iterator find(Key)
Finds an element in the map based on the key.
Definition map.h:153
iterator end()
Returns the end of the map.
Definition map.h:141
void request_mac_address(InternetProtocolAddress address)
Request the MAC address of a given IP address.
Definition arp.cpp:95

References MaxOS::common::Map< Key, Value >::end(), MaxOS::common::Map< Key, Value >::find(), and request_mac_address().

◆ store()

void AddressResolutionProtocol::store ( InternetProtocolAddress  internet_protocol_address,
drivers::ethernet::MediaAccessControlAddress  media_access_control_address 
)
finalvirtual

store a mapping of an IP address to a MAC address.

Parameters
internet_protocol_addressThe IP address.
media_access_control_addressThe MAC address.

Reimplemented from MaxOS::net::IPV4AddressResolver.

Definition at line 155 of file arp.cpp.

References MaxOS::common::Map< Key, Value >::insert().


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