Max OS 0.3
Loading...
Searching...
No Matches
udp.cpp
Go to the documentation of this file.
1
9#include <net/udp.h>
10
11using namespace MaxOS;
12using namespace MaxOS::net;
13using namespace MaxOS::common;
14using namespace MaxOS::memory;
15
17
19
20UDPPayloadHandler::~UDPPayloadHandler() = default;
21
32
40
41 switch (event -> type) {
42 case UDPEvents::DATA_RECEIVED:
44 ((UDPDataReceivedEvent*) event)->data,
45 ((UDPDataReceivedEvent*) event)->size);
46 break;
47 default:
48 break;
49 }
50
51 return event;
52}
53
55
56
58
59 //Set the instance variables
60 listening = false;
61
62}
63
64UDPSocket::~UDPSocket() = default;
65
73
74 // Create the event
75 auto* event = new UDPDataReceivedEvent(this, data, size);
78
79}
80
87void UDPSocket::send(uint8_t *data, uint16_t size) {
88
89 user_datagram_protocol_handler->send(this, data, size);
90
91}
92
101
103
105
113: IPV4PayloadHandler(internet_protocol_handler, 0x11) //0x11 is the UDP protocol number
114{
115 this -> errorMessages = error_messages;
116}
117
118UserDatagramProtocolHandler::~UserDatagramProtocolHandler() = default;
119
130
131 //Check the size
132 if(size < sizeof(UDPHeader)) {
133 return false;
134 }
135
136 //Get the header
137 auto* header = (UDPHeader*)payload_data;
138
139 //Set the local and remote ports
140 uint16_t local_port = header -> destination_port;
141 uint16_t remote_port = header -> source_port;
142
143 UDPSocket* socket = nullptr; //The socket that will be used
144 for(auto & current_socket : sockets) {
145 if(current_socket->local_port == local_port //If the local port (header dst, our port) is the same as the local port of the socket
146 && current_socket->local_ip == destination_ip //If the local IP (packet dst, our IP) is the same as the local IP of the socket
147 && current_socket->listening) //If the socket is listening
148 {
149
150 socket = current_socket; //Set the socket to the socket that is being checked
151 socket->listening = false; //Set the socket to not listening, as it is now in use
152 socket->remote_port = remote_port; //Set the remote port of the socket to the remote port of the packet
153 socket->remote_ip = source_ip; //Set the remote IP of the socket to the remote IP of the packet
154
155 }else if(current_socket->local_port == local_port //If the local port (header dst, our port) is the same as the local port of the socket
156 && current_socket->local_ip == destination_ip //If the local IP (packet dst, our IP) is the same as the local IP of the socket
157 && current_socket->remote_port == remote_port //If the remote port (header src, their port) is the same as the remote port of the socket
158 && current_socket->remote_ip == source_ip) //If the remote IP (packet src, their IP) is the same as the remote IP of the socket
159 {
160 socket = current_socket; //Set the socket to the current socket
161 }
162
163 }
164
165 if(socket != nullptr) { //If the socket is not null then pass the data to the socket
167 size - sizeof(UDPHeader));
168 }
169
170 //UDP doesn't send back packets, so always return false
171 return false;
172
173}
174
182
183
184 auto* socket = (UDPSocket*)MemoryManager::kmalloc(sizeof(UDPSocket)); //Allocate memory for the socket
185
186 if(socket != nullptr) //If the socket was created
187 {
188 new (socket) UDPSocket(); //Create the socket
189
190 //Configure the socket
191 socket -> remote_port = port; //Port to that application wants to connect to
192 socket -> remote_ip = ip; //IP to that application wants to connect to
193 socket -> local_port = free_ports++; //Port that we will use to connect to the remote application (note, local port doesnt have to be the same as remote)
194 socket -> local_ip = internet_protocol_handler->get_internet_protocol_address(); //IP that we will use to connect to the remote application
195 socket -> user_datagram_protocol_handler = this; //Set the UDP handler
196
197 sockets.push_back(socket); //Add the socket to the list of sockets
198 }
199
200 return socket; //Return the socket
201}
202
212
213 return nullptr;
214}
215
223
224 auto* socket = (UDPSocket*)MemoryManager::kmalloc(sizeof(UDPSocket)); //Allocate memory for the socket
225
226 if(socket != nullptr) //If the socket was created
227 {
228 new (socket) UDPSocket(); //Create the socket
229
230 //Configure the socket
231 socket -> listening = true; //Set the socket to listening
232 socket -> local_port = port; //Port that we will use to connect to the remote application (note, local port doesnt have to be the same as remote)
233 socket -> local_ip = internet_protocol_handler->get_internet_protocol_address(); //IP that we will use to connect to the remote application
234 socket -> user_datagram_protocol_handler = this; //Set the UDP handler
235
236 sockets.push_back(socket); //Add the socket to the list of sockets
237 }
238
239 return socket; //Return the socket
240
241}
242
249
250
252 if((*current_socket) == socket) //If the socket is the same as the socket that is being checked
253 {
254 sockets.erase(current_socket); //Remove the socket from the list of sockets
255 MemoryManager::kfree(socket); //Free the socket
256 break; //Break out of the loop
257 }
258 }
259
260}
261
270
271 uint16_t total_size = sizeof(UDPHeader) + size; //Get the total size of the packet
272 auto* buffer = (uint8_t*)MemoryManager::kmalloc(total_size); //Allocate memory for the packet
273 uint8_t* buffer2 = buffer + sizeof(UDPHeader); //Get the buffer that will be used to store the data
274
275 auto* header = (UDPHeader*)buffer; //Create the header of the packet
276
277 //Set the header
278 header -> source_port = socket -> local_port; //Set the source port to the local port of the socket (this is the port that the packet will be sent from)
279 header -> destination_port = socket -> remote_port; //Set the destination port to the remote port of the socket (this is the port that the packet will be sent to)
280 header -> length = ((total_size & 0x00FF) << 8) | ((total_size & 0xFF00) >> 8); //Set the length of the packet
281
282 // Convert the ports into big endian
283 header -> source_port = ((header -> source_port & 0x00FF) << 8) | ((header -> source_port & 0xFF00) >> 8);
284 header -> destination_port = ((header -> destination_port & 0x00FF) << 8) | ((header -> destination_port & 0xFF00) >> 8);
285
286 //Copy the data to the buffer
287 for (int i = 0; i < size; ++i) { //Loop through the data
288 buffer2[i] = data[i]; //Copy the data to the buffer
289 }
290
291 //Set the checksum
292 header -> checksum = 0; //Set the checksum to 0, this is becuase UDP doesnt have to have a checksum
293
294 //Send the packet
296
297 //Free the buffer
298 MemoryManager::kfree(buffer);
299
300}
301
309
310 socket->m_handlers.push_back(udp_payload_handler); //Set the handler of the socket to the handler that was passed in
311
312
313}
314
323: Event(UDPEvents::DATA_RECEIVED)
324{
325 this -> socket = socket; //Set the socket
326 this -> data = data; //Set the data
327 this -> size = size; //Set the size
328
329}
330
331UDPDataReceivedEvent::~UDPDataReceivedEvent() = default;
Vector< Event< UDPEvents > * > raise_event(Event< UDPEvents > *event)
Calls the on_event function of all the event m_handlers connected to the event manager and returns a ...
Vector< EventHandler< EventType > * > m_handlers
Used to store information about an event, has a type and a return value.
A stream that strings can be written to.
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
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.
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
InternetProtocolHandler * internet_protocol_handler
The Internet protocol handler this payload handler is connected to.
Definition ipv4.h:70
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
Event fired when data is received on a UDP socket.
Definition udp.h:56
UDPDataReceivedEvent(UDPSocket *socket, uint8_t *data, uint16_t size)
Construct a new UDP Data Received Event object.
Definition udp.cpp:322
UDPSocket * socket
The socket that received the data.
Definition udp.h:58
uint16_t size
The size of the data received.
Definition udp.h:60
uint8_t * data
The data received.
Definition udp.h:59
Handles the payload of a UDP packet.
Definition udp.h:70
common::Event< UDPEvents > * on_event(common::Event< UDPEvents > *event) override
Event handler for UDP payload events.
Definition udp.cpp:39
virtual void handle_user_datagram_protocol_message(UDPSocket *socket, uint8_t *data, uint16_t size)
Handle the recivement of a UDP message.
Definition udp.cpp:29
A UDP socket.
Definition udp.h:85
uint32_t remote_ip
The IP of the remote device.
Definition udp.h:95
UDPSocket()
Socket
Definition udp.cpp:57
uint16_t remote_port
The port on the remote device.
Definition udp.h:92
virtual void disconnect()
disconnect the UDP socket
Definition udp.cpp:96
UserDatagramProtocolHandler * user_datagram_protocol_handler
The UDP handler this socket is connected to.
Definition udp.h:97
virtual void send(uint8_t *data, uint16_t size)
send data through the UDP socket
Definition udp.cpp:87
bool listening
Wether the port is waiting for incoming connections.
Definition udp.h:89
virtual void handle_user_datagram_protocol_payload(uint8_t *data, uint16_t size)
Handle the recivement of a UDP payload.
Definition udp.cpp:72
UserDatagramProtocolHandler(InternetProtocolHandler *internet_protocol_handler, common::OutputStream *error_messages)
Construct a new User Datagram Protocol Handler object.
Definition udp.cpp:112
static UserDatagramProtocolPort free_ports
The next free port number.
Definition udp.h:116
void disconnect(UDPSocket *socket)
Disconnects the socket from the remote IP and port.
Definition udp.cpp:248
UDPSocket * connect(uint32_t ip, uint16_t port)
Definition udp.cpp:181
common::Vector< UDPSocket * > sockets
The list of UDP sockets.
Definition udp.h:115
UDPSocket * listen(uint16_t port)
Listens for incoming packets on the port.
Definition udp.cpp:222
static void bind(UDPSocket *socket, UDPPayloadHandler *udp_payload_handler)
Binds a handler to the socket.
Definition udp.cpp:308
bool handle_internet_protocol_payload(net::InternetProtocolAddress source_ip, net::InternetProtocolAddress destination_ip, uint8_t *payload_data, uint32_t size) override
Handle the recivement of an UDP packet.
Definition udp.cpp:129
common::OutputStream * errorMessages
Where to write error messages.
Definition udp.h:117
void send(UDPSocket *socket, const uint8_t *data, uint16_t size)
Sends a packet to the remote IP and port.
Definition udp.cpp:269
uint32_t InternetProtocolAddress
An IPv4 address.
Definition ipv4.h:18
The header of a UDP packet.
Definition udp.h:28
Defines the User Datagram Protocol (UDP) for network communication.
UDPEvents
The events that can be fired by the UDP protocol.
Definition udp.h:41