Max OS 0.3
Loading...
Searching...
No Matches
tcp.h
Go to the documentation of this file.
1
9#ifndef MAXOS_NET_TCP_H
10#define MAXOS_NET_TCP_H
11
12#include <cstdint>
13#include <net/ipv4.h>
15
16
17namespace MaxOS::net {
18
20
25 enum class TCPSocketState {
26 CLOSED,
27 LISTEN,
28 SYN_SENT,
29 SYN_RECEIVED,
30
31 ESTABLISHED,
32
33 FIN_WAIT1,
34 FIN_WAIT2,
35 CLOSING,
36 TIME_WAIT,
37
38 CLOSE_WAIT,
39 LAST_ACK
40 };
41
46 enum class TCPFlag {
47 FIN = 1,
48 SYN = 2,
49 RST = 4,
50 PSH = 8,
51 ACK = 16,
52 URG = 32,
53 ECE = 64,
54 CWR = 128,
55 NS = 256
56 };
57
65 typedef struct PACKED TCPHeader {
66
67 uint16_t src_port;
68 uint16_t dst_port;
69 uint32_t sequence_number;
71
72 uint8_t reserved : 4;
73 uint8_t header_size_32 : 4;
74 uint8_t flags;
75
76 uint16_t window_size;
77 uint16_t checksum;
78 uint16_t urgent_ptr;
79
80 uint32_t options;
81
82 } tcp_header_t;
83
94 typedef struct PACKED TCPPseudoHeader {
95
96 uint32_t src_ip;
97 uint32_t dst_ip;
98 uint16_t protocol;
99 uint16_t total_length;
100
101 } tcp_pseudo_header_t;
102
103
104 // Forward declarations
105 class TCPSocket;
106
107 class TransmissionControlProtocolHandler;
108
114 CONNECTED,
115 DISCONNECTED,
116 DATA_RECEIVED
117 };
118
123 class DataReceivedEvent : public common::Event<TCPPayloadHandlerEvents> {
124 public:
126 uint8_t* data;
127 uint16_t size;
128 DataReceivedEvent(TCPSocket* socket, uint8_t* data, uint16_t size);
130 };
131
136 class ConnectedEvent : public common::Event<TCPPayloadHandlerEvents> {
137 public:
141 };
142
147 class DisconnectedEvent : public common::Event<TCPPayloadHandlerEvents> {
148 public:
152 };
153
158 class TCPPayloadHandler : public common::EventHandler<TCPPayloadHandlerEvents> {
159 public:
162
164
165 virtual void handle_transmission_control_protocol_payload(TCPSocket* socket, uint8_t* data, uint16_t size);
166 virtual void connected(TCPSocket* socket);
167 virtual void disconnected(TCPSocket* socket);
168 };
169
174 class TCPSocket : public common::EventManager<TCPPayloadHandlerEvents> {
176
177 friend class TransmissionControlProtocolPortListener;
178
179 protected:
180 uint16_t remotePort = 0;
181 uint32_t remote_ip = 0;
182 uint16_t local_port = 0;
183 uint32_t local_ip = 0;
184 uint32_t sequence_number = 0;
186
189 public:
191 ~TCPSocket();
192
193 virtual void send(uint8_t* data, uint16_t size);
194 virtual void disconnect();
195
196 void disconnected();
197 void connected();
198
199 bool handle_transmission_control_protocol_payload(uint8_t* data, uint16_t size);
200 };
201
207 friend class TCPSocket;
208
209 protected:
212
214 void send_transmission_control_protocol_packet(TCPSocket* socket, const uint8_t* data, uint16_t size, uint16_t flags = 0);
215
216 public:
219
220 bool handle_internet_protocol_payload(net::InternetProtocolAddress source_ip, net::InternetProtocolAddress destination_ip, uint8_t* payload_data, uint32_t size) override;
221
223 static TCPSocket* connect(const string& address);
224
225 void disconnect(TCPSocket* socket);
226
227 virtual TCPSocket* listen(uint16_t port);
228 virtual void bind(TCPSocket* socket, TCPPayloadHandler* handler);
229 };
230
231
232}
233
234
235#endif //MAXOS_NET_TCP_H
Used to handle an event.
Manages the m_handlers for a type of event, raises events and calls the 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
Event for when a TCP socket is connected.
Definition tcp.h:136
TCPSocket * socket
The socket that is connected.
Definition tcp.h:138
Event for when data is received on a TCP socket.
Definition tcp.h:123
uint16_t size
The size of the data received.
Definition tcp.h:127
TCPSocket * socket
The socket that received the data.
Definition tcp.h:125
uint8_t * data
The data received.
Definition tcp.h:126
Event for when a TCP socket is disconnected.
Definition tcp.h:147
TCPSocket * socket
The socket that is disconnected.
Definition tcp.h:149
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
Handles IPv4 packets over Ethernet frames.
Definition ipv4.h:85
Handler for TCP payloads.
Definition tcp.h:158
virtual void disconnected(TCPSocket *socket)
Handle a TCP disconnection on the socket.
Definition tcp.cpp:49
virtual void connected(TCPSocket *socket)
Handle a new TCP connection on the socket.
Definition tcp.cpp:40
common::Event< TCPPayloadHandlerEvents > * on_event(common::Event< TCPPayloadHandlerEvents > *event) override
Handle an event occurring on the TCP payload handler.
Definition tcp.cpp:59
virtual void handle_transmission_control_protocol_payload(TCPSocket *socket, uint8_t *data, uint16_t size)
Handle TCP data received on the socket.
Definition tcp.cpp:31
A TCP socket. Allows for sending and receiving data over TCP at a port.
Definition tcp.h:174
uint32_t acknowledgement_number
The number used to keep track of what has been received, incremented by 1 each time.
Definition tcp.h:185
virtual void send(uint8_t *data, uint16_t size)
send data over the socket
Definition tcp.cpp:113
TransmissionControlProtocolHandler * transmission_control_protocol_handler
The TCP handler this socket is using.
Definition tcp.h:187
uint16_t local_port
The port on this device.
Definition tcp.h:182
void disconnected()
Raise the disconnected event.
Definition tcp.cpp:133
uint32_t local_ip
The IP address of this device.
Definition tcp.h:183
virtual void disconnect()
disconnect the socket
Definition tcp.cpp:126
bool handle_transmission_control_protocol_payload(uint8_t *data, uint16_t size)
Handle the TCP message (socket end)
Definition tcp.cpp:100
void connected()
Raise the connected event.
Definition tcp.cpp:143
uint16_t remotePort
The port on the external device.
Definition tcp.h:180
uint32_t sequence_number
The current order number of the data being sent.
Definition tcp.h:184
TCPSocketState state
The state of the socket.
Definition tcp.h:188
uint32_t remote_ip
The IP address of the external device.
Definition tcp.h:181
Handles TCP packets and manages TCP sockets.
Definition tcp.h:206
void disconnect(TCPSocket *socket)
Begin the disconnect process.
Definition tcp.cpp:537
common::OutputStream * error_messages
Where to write error messages.
Definition tcp.h:210
static TransmissionControlProtocolPort free_ports
The next free port to use for new sockets.
Definition tcp.h:213
TCPSocket * connect(InternetProtocolAddress ip, TransmissionControlProtocolPort port)
connect to a remote host through the TCP protocol
Definition tcp.cpp:484
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)
Definition tcp.cpp:420
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)
Definition tcp.cpp:201
common::Vector< TCPSocket * > sockets
The list of connected sockets.
Definition tcp.h:211
virtual void bind(TCPSocket *socket, TCPPayloadHandler *handler)
bind a data handler to this socket
Definition tcp.cpp:581
virtual TCPSocket * listen(uint16_t port)
Begin listening on a port.
Definition tcp.cpp:551
Defines classes and structures for handling Internet Protocol version 4 (IPv4) packets.
uint32_t InternetProtocolAddress
An IPv4 address.
Definition ipv4.h:18
Defines a MemoryManager class for handling memory allocation and deallocation.
The header of a TCP packet.
Definition tcp.h:65
uint8_t flags
The flags of the TCP packet (see TCPFlag enum)
Definition tcp.h:74
uint32_t acknowledgement_number
The next expected sequence number from the sender (used by the receiver to request the next data)
Definition tcp.h:70
uint16_t dst_port
The port of the receiver.
Definition tcp.h:68
uint16_t urgent_ptr
Where in the data the urgent data ends (if the URG flag is set)
Definition tcp.h:78
uint16_t checksum
The checksum of the header.
Definition tcp.h:77
uint32_t sequence_number
Where this packet's data fits in the overall order of the data stream.
Definition tcp.h:69
uint32_t options
The options for the TCP packet (MSS, Window Scale, SACK Permitted)
Definition tcp.h:80
uint8_t header_size_32
The size of the header in 32-bit words.
Definition tcp.h:73
uint16_t window_size
How many bytes the sender is willing to receive.
Definition tcp.h:76
uint16_t src_port
The port of the sender.
Definition tcp.h:67
uint8_t reserved
Unused, must be 0.
Definition tcp.h:72
The pseudo header used for TCP checksum calculation.
Definition tcp.h:94
uint16_t protocol
The protocol (set to 6 for TCP)
Definition tcp.h:98
uint32_t dst_ip
The IP address of the receiver.
Definition tcp.h:97
uint16_t total_length
The total length of the TCP header and data.
Definition tcp.h:99
uint32_t src_ip
The IP address of the sender.
Definition tcp.h:96
TCPPayloadHandlerEvents
Events for the TCPPayloadHandler.
Definition tcp.h:113
uint16_t TransmissionControlProtocolPort
TCP port.
Definition tcp.h:19
TCPFlag
The flags in a TCP header.
Definition tcp.h:46
TCPSocketState
The state of a TCP socket.
Definition tcp.h:25