14using namespace MaxOS::net;
15using namespace MaxOS::common;
16using namespace MaxOS::memory;
22TCPPayloadHandler::~TCPPayloadHandler() =
default;
62 case TCPPayloadHandlerEvents::CONNECTED:
65 case TCPPayloadHandlerEvents::DISCONNECTED:
68 case TCPPayloadHandlerEvents::DATA_RECEIVED:
88 state = TCPSocketState::CLOSED;
91TCPSocket::~TCPSocket() =
default;
115 while(
state != TCPSocketState::ESTABLISHED);
166TransmissionControlProtocolHandler::~TransmissionControlProtocolHandler() =
default;
175 return ((x & 0xFF000000) >> 24)
176 | ((x & 0x00FF0000) >> 8)
177 | ((x & 0x0000FF00) << 8)
178 | ((x & 0x000000FF) << 24);
188 return ((x & 0xFF00) >> 8)
189 | ((x & 0x00FF) << 8);
232 TCPSocketState::LISTEN
254 if(socket !=
nullptr &&
msg->flags & (
uint16_t) TCPFlag::RST) {
255 socket->
state = TCPSocketState::CLOSED;
260 if(socket !=
nullptr && socket->
state != TCPSocketState::CLOSED) {
275 if(socket->
state == TCPSocketState::LISTEN) {
276 socket->
state = TCPSocketState::SYN_RECEIVED;
290 if(socket->
state == TCPSocketState::SYN_SENT) {
291 socket->
state = TCPSocketState::ESTABLISHED;
308 if(socket->
state == TCPSocketState::ESTABLISHED) {
309 socket->
state = TCPSocketState::CLOSE_WAIT;
315 }
else if(socket->
state == TCPSocketState::CLOSE_WAIT) {
316 socket->
state = TCPSocketState::CLOSED;
317 }
else if(socket->
state == TCPSocketState::FIN_WAIT1 || socket->
state == TCPSocketState::FIN_WAIT2) {
318 socket->
state = TCPSocketState::CLOSED;
328 if(socket->
state == TCPSocketState::SYN_RECEIVED) {
329 socket->
state = TCPSocketState::ESTABLISHED;
332 }
else if(socket->
state == TCPSocketState::FIN_WAIT1) {
333 socket->
state = TCPSocketState::FIN_WAIT2;
335 }
else if(socket->
state == TCPSocketState::CLOSE_WAIT) {
336 socket->
state = TCPSocketState::CLOSED;
354 size -
msg->header_size_32 * 4));
390 msg->acknowledgement_number);
401 if(socket !=
nullptr && socket->
state ==
402 TCPSocketState::CLOSED)
446 msg->window_size = 0xFFFF;
450 msg->options = ((flags & (
uint16_t) TCPFlag::SYN) != 0) ? 0xB4050402 : 0;
456 if(data !=
nullptr) {
458 for(
int i = 0; i < size; i++)
465 phdr->protocol = 0x0600;
466 phdr->total_length = ((total_length & 0x00FF) << 8) | ((total_length & 0xFF00) >> 8);
490 if(socket !=
nullptr) {
496 socket->remote_ip =
ip;
501 socket->remotePort = ((socket->remotePort & 0xFF00) >> 8) | ((socket->remotePort & 0x00FF) << 8);
502 socket->local_port = ((socket->local_port & 0xFF00) >> 8) | ((socket->local_port & 0x00FF) << 8);
506 socket->state = TCPSocketState::SYN_SENT;
509 socket->sequence_number = 0xbeefcafe;
539 socket->
state = TCPSocketState::FIN_WAIT1;
557 if(socket !=
nullptr) {
562 socket->
state = TCPSocketState::LISTEN;
564 socket->local_port = ((
port & 0xFF00) >> 8) | ((
port & 0x00FF) << 8);
601DataReceivedEvent::~DataReceivedEvent() =
default;
613ConnectedEvent::~ConnectedEvent() =
default;
625DisconnectedEvent::~DisconnectedEvent() =
default;
Vector< Event< TCPPayloadHandlerEvents > * > raise_event(Event< TCPPayloadHandlerEvents > *event)
Calls the on_event function of all the event m_handlers connected to the event manager and returns a ...
void connect_event_handler(EventHandler< EventType > *handler)
connect an event handler to the event manager if it is not already connected
Used to store information about an event, has a type and a return value.
A stream that strings can be written to.
void write(string string_to_write) override
Writes a string to the output stream.
Stores the left, top, width and height of a rectangle.
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.
Event for when a TCP socket is connected.
TCPSocket * socket
The socket that is connected.
ConnectedEvent(TCPSocket *socket)
Construct a new connected Event object.
Event for when data is received on a TCP socket.
uint16_t size
The size of the data received.
TCPSocket * socket
The socket that received the data.
DataReceivedEvent(TCPSocket *socket, uint8_t *data, uint16_t size)
Construct a new Data Received Event object.
uint8_t * data
The data received.
Event for when a TCP socket is disconnected.
TCPSocket * socket
The socket that is disconnected.
DisconnectedEvent(TCPSocket *socket)
Construct a new disconnected Event object.
Handles the payload of a specific IP protocol.
void send(InternetProtocolAddress destination_ip, uint8_t *payload_data, uint32_t size)
Sends an IP packet.
InternetProtocolHandler * internet_protocol_handler
The Internet protocol handler this payload handler is connected to.
Handles IPv4 packets over Ethernet frames.
InternetProtocolAddress get_internet_protocol_address() const
Gets the IP address of this device.
static uint16_t checksum(const uint16_t *data, uint32_t length_in_bytes)
Creates a checksum for the given data.
Handler for TCP payloads.
virtual void disconnected(TCPSocket *socket)
Handle a TCP disconnection on the socket.
virtual void connected(TCPSocket *socket)
Handle a new TCP connection on the socket.
TCPPayloadHandler()
Handler///
common::Event< TCPPayloadHandlerEvents > * on_event(common::Event< TCPPayloadHandlerEvents > *event) override
Handle an event occurring on the TCP payload handler.
virtual void handle_transmission_control_protocol_payload(TCPSocket *socket, uint8_t *data, uint16_t size)
Handle TCP data received on the socket.
A TCP socket. Allows for sending and receiving data over TCP at a port.
TCPSocket(TransmissionControlProtocolHandler *transmission_control_protocol_handler)
Construct a new TCP Socket object.
uint32_t acknowledgement_number
The number used to keep track of what has been received, incremented by 1 each time.
virtual void send(uint8_t *data, uint16_t size)
send data over the socket
TransmissionControlProtocolHandler * transmission_control_protocol_handler
The TCP handler this socket is using.
uint16_t local_port
The port on this device.
void disconnected()
Raise the disconnected event.
uint32_t local_ip
The IP address of this device.
virtual void disconnect()
disconnect the socket
bool handle_transmission_control_protocol_payload(uint8_t *data, uint16_t size)
Handle the TCP message (socket end)
void connected()
Raise the connected event.
uint16_t remotePort
The port on the external device.
uint32_t sequence_number
The current order number of the data being sent.
TCPSocketState state
The state of the socket.
uint32_t remote_ip
The IP address of the external device.
Handles TCP packets and manages TCP sockets.
void disconnect(TCPSocket *socket)
Begin the disconnect process.
common::OutputStream * error_messages
Where to write error messages.
static TransmissionControlProtocolPort free_ports
The next free port to use for new sockets.
TCPSocket * connect(InternetProtocolAddress ip, TransmissionControlProtocolPort port)
connect to a remote host through the TCP protocol
TransmissionControlProtocolHandler(InternetProtocolHandler *internet_protocol_handler, common::OutputStream *error_messages)
Construct a new Transmission Control Protocol Handler object.
void send_transmission_control_protocol_packet(TCPSocket *socket, const uint8_t *data, uint16_t size, uint16_t flags=0)
send a packet (Throught the provider)
bool handle_internet_protocol_payload(net::InternetProtocolAddress source_ip, net::InternetProtocolAddress destination_ip, uint8_t *payload_data, uint32_t size) override
Handle the TCP message (provider end)
common::Vector< TCPSocket * > sockets
The list of connected sockets.
virtual void bind(TCPSocket *socket, TCPPayloadHandler *handler)
bind a data handler to this socket
virtual TCPSocket * listen(uint16_t port)
Begin listening on a port.
uint32_t InternetProtocolAddress
An IPv4 address.
uint32_t big_endian_32(uint32_t x)
Convert a 32-bit integer to big-endian format.
uint32_t big_endian_16(uint16_t x)
Convert a 16-bit integer to big-endian format.
Defines the Transmission Control Protocol (TCP) structures and classes for handling TCP sockets and p...
TCPPayloadHandlerEvents
Events for the TCPPayloadHandler.
uint16_t TransmissionControlProtocolPort
TCP port.
Defines the User Datagram Protocol (UDP) for network communication.