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

Handles the UDP protocol. More...

#include <udp.h>

Inheritance diagram for MaxOS::net::UserDatagramProtocolHandler:
MaxOS::net::IPV4PayloadHandler

Public Member Functions

 UserDatagramProtocolHandler (InternetProtocolHandler *internet_protocol_handler, common::OutputStream *error_messages)
 Construct a new User Datagram Protocol Handler object.
 
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.
 
UDPSocketconnect (uint32_t ip, uint16_t port)
 
UDPSocketlisten (uint16_t port)
 Listens for incoming packets on the port.
 
void disconnect (UDPSocket *socket)
 Disconnects the socket from the remote IP and port.
 
void send (UDPSocket *socket, const uint8_t *data, uint16_t size)
 Sends a packet to the remote IP and port.
 

Static Public Member Functions

static UDPSocketconnect (const string &address)
 Connects to a remote host through the UDP protocol.
 
static void bind (UDPSocket *socket, UDPPayloadHandler *udp_payload_handler)
 Binds a handler to the socket.
 

Protected Attributes

common::Vector< UDPSocket * > sockets
 The list of UDP sockets.
 
common::OutputStreamerrorMessages
 Where to write error messages.
 

Static Protected Attributes

static UserDatagramProtocolPort free_ports = 0x8000
 The next free port number.
 

Detailed Description

Handles the UDP protocol.

Definition at line 113 of file udp.h.

Constructor & Destructor Documentation

◆ UserDatagramProtocolHandler()

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

Construct a new User Datagram Protocol Handler object.

Parameters
internet_protocol_handlerThe Internet protocol handler
error_messagesWhere to write error messages

Definition at line 112 of file udp.cpp.

113: IPV4PayloadHandler(internet_protocol_handler, 0x11) //0x11 is the UDP protocol number
114{
115 this -> errorMessages = error_messages;
116}
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
common::OutputStream * errorMessages
Where to write error messages.
Definition udp.h:117

References errorMessages.

Member Function Documentation

◆ bind()

void UserDatagramProtocolHandler::bind ( UDPSocket socket,
UDPPayloadHandler udp_payload_handler 
)
static

Binds a handler to the socket.

Parameters
socketThe socket to bind the handler to
udp_payload_handlerThe handler to bind

Definition at line 308 of file udp.cpp.

308 {
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}
Vector< EventHandler< EventType > * > m_handlers
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22

References MaxOS::common::EventManager< EventType >::m_handlers.

◆ connect() [1/2]

UDPSocket * UserDatagramProtocolHandler::connect ( const string address)
static

Connects to a remote host through the UDP protocol.

Parameters
addressThe address to connect to in the form "IP:PORT"
Returns
The socket that is connected to the remote host, nullptr if it failed
Todo:
Implement string parsing to extract IP and port

Definition at line 211 of file udp.cpp.

211 {
212
213 return nullptr;
214}

◆ connect() [2/2]

UDPSocket * UserDatagramProtocolHandler::connect ( uint32_t  ip,
uint16_t  port 
)

Connects the socket to the remote IP and port

Parameters
ipThe remote IP address in big endian
portThe remote port
Returns
The socket that was connected

Definition at line 181 of file udp.cpp.

181 {
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}
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
InternetProtocolAddress get_internet_protocol_address() const
Gets the IP address of this device.
Definition ipv4.cpp:345
A UDP socket.
Definition udp.h:85
static UserDatagramProtocolPort free_ports
The next free port number.
Definition udp.h:116
common::Vector< UDPSocket * > sockets
The list of UDP sockets.
Definition udp.h:115

References free_ports, MaxOS::net::InternetProtocolHandler::get_internet_protocol_address(), MaxOS::net::IPV4PayloadHandler::internet_protocol_handler, MaxOS::memory::MemoryManager::kmalloc(), and sockets.

◆ disconnect()

void UserDatagramProtocolHandler::disconnect ( UDPSocket socket)

Disconnects the socket from the remote IP and port.

Parameters
socketThe socket to disconnect

Definition at line 248 of file udp.cpp.

248 {
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}
static void kfree(void *pointer)
Frees a block of memory using the kernel memory manager.

References MaxOS::memory::MemoryManager::kfree(), and sockets.

Referenced by MaxOS::net::UDPSocket::disconnect().

◆ handle_internet_protocol_payload()

bool UserDatagramProtocolHandler::handle_internet_protocol_payload ( net::InternetProtocolAddress  source_ip,
net::InternetProtocolAddress  destination_ip,
uint8_t payload_data,
uint32_t  size 
)
overridevirtual

Handle the recivement of an UDP packet.

Parameters
source_ipThe source IP address in big endian
destination_ipThe destination IP address in big endian
payload_dataThe UDP payload
sizeThe size of the UDP payload
Returns
True if the packet is to be sent back to the sender

Reimplemented from MaxOS::net::IPV4PayloadHandler.

Definition at line 129 of file udp.cpp.

129 {
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}
uint32_t remote_ip
The IP of the remote device.
Definition udp.h:95
uint16_t remote_port
The port on the remote device.
Definition udp.h:92
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
The header of a UDP packet.
Definition udp.h:28

References MaxOS::net::UDPSocket::handle_user_datagram_protocol_payload(), MaxOS::net::UDPSocket::listening, MaxOS::net::UDPSocket::remote_ip, MaxOS::net::UDPSocket::remote_port, and sockets.

◆ listen()

UDPSocket * UserDatagramProtocolHandler::listen ( uint16_t  port)

Listens for incoming packets on the port.

Parameters
portThe port to listen on
Returns
The socket that is listening

Definition at line 222 of file udp.cpp.

222 {
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}

References MaxOS::net::InternetProtocolHandler::get_internet_protocol_address(), MaxOS::net::IPV4PayloadHandler::internet_protocol_handler, MaxOS::memory::MemoryManager::kmalloc(), and sockets.

◆ send()

void UserDatagramProtocolHandler::send ( UDPSocket socket,
const uint8_t data,
uint16_t  size 
)

Sends a packet to the remote IP and port.

Parameters
socketThe socket to send the packet from
dataThe data to send
sizeThe size of the data

Definition at line 269 of file udp.cpp.

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

References MaxOS::memory::MemoryManager::kfree(), MaxOS::memory::MemoryManager::kmalloc(), MaxOS::net::UDPSocket::remote_ip, and MaxOS::net::IPV4PayloadHandler::send().

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

Member Data Documentation

◆ errorMessages

common::OutputStream* MaxOS::net::UserDatagramProtocolHandler::errorMessages
protected

Where to write error messages.

Definition at line 117 of file udp.h.

Referenced by UserDatagramProtocolHandler().

◆ free_ports

UserDatagramProtocolPort UserDatagramProtocolHandler::free_ports = 0x8000
staticprotected

The next free port number.

Provider

Definition at line 116 of file udp.h.

Referenced by connect().

◆ sockets

common::Vector<UDPSocket*> MaxOS::net::UserDatagramProtocolHandler::sockets
protected

The list of UDP sockets.

Definition at line 115 of file udp.h.

Referenced by connect(), disconnect(), handle_internet_protocol_payload(), and listen().


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