Max OS 0.3
Loading...
Searching...
No Matches
arp.cpp
Go to the documentation of this file.
1
9#include <net/arp.h>
10
11using namespace MaxOS;
12using namespace MaxOS::common;
13using namespace MaxOS::net;
14using namespace MaxOS::drivers;
15using namespace MaxOS::drivers::ethernet;
16
17
27 IPV4AddressResolver(internet_protocol_handler) {
28 this->internet_protocol_handler = internet_protocol_handler;
29 this->error_messages = error_messages;
30}
31
32net::AddressResolutionProtocol::~AddressResolutionProtocol() = default;
33
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}
88
89
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}
118
119
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}
148
Defines the Address Resolution Protocol (ARP) for resolving IP addresses to MAC addresses.
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 insert(Key, Value)
Updates the value of an element, or adds a new element if it does not exist.
Definition map.h:253
A stream that strings can be written to.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
void request_mac_address(InternetProtocolAddress address)
Request the MAC address of a given IP address.
Definition arp.cpp:95
drivers::ethernet::MediaAccessControlAddress resolve(InternetProtocolAddress address) final
Get the MAC address from an IP via ARP.
Definition arp.cpp:128
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.
Definition arp.cpp:155
AddressResolutionProtocol(EthernetFrameHandler *ethernet_frame_handler, InternetProtocolHandler *internet_protocol_handler, common::OutputStream *error_messages)
Constructs an AddressResolutionProtocol handler.
Definition arp.cpp:25
bool handle_ethernetframe_payload(uint8_t *etherframe_payload, uint32_t size) final
Called when an ARP packet is received.
Definition arp.cpp:41
Handles incoming Ethernet Frames and routes them to the appropriate payload handlers.
drivers::ethernet::MediaAccessControlAddress get_mac()
Get the MAC address of this device.
Handles a specific type of Ethernet Frame payload.
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.
Resolves IP addresses to MAC addresses.
Definition ipv4.h:53
Handles IPv4 packets over Ethernet frames.
Definition ipv4.h:85
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
uint32_t InternetProtocolAddress
An IPv4 address.
Definition ipv4.h:18
An ARP message.
Definition arp.h:26
uint64_t dst_mac
The MAC address of the target.
Definition arp.h:37