9 #include <adt.h> // tRingBuffer
10 #include <timers.h> // tTimer
11 #include <semaphore.h> // tSemaphore
13 #define CACHE_FUTURE_PACKETS_IN_BYTES 1 // Use a ring buffer to cache out of order packets?
15 typedef struct sTCPHeader tTCPHeader;
16 typedef struct sTCPListener tTCPListener;
17 typedef struct sTCPStoredPacket tTCPStoredPacket;
18 typedef struct sTCPConnection tTCPConnection;
24 Uint32 SequenceNumber;
25 Uint32 AcknowlegementNumber;
27 struct { // Lowest to highest
29 unsigned DataOffset: 4; // Size of the header in 32-bit words
30 } __attribute__ ((packed));
35 struct { // Lowest to Highest
36 unsigned FIN: 1; // Last packet
37 unsigned SYN: 1; // Synchronise Sequence Numbers
38 unsigned RST: 1; // Reset Connection
39 unsigned PSH: 1; // Push Function
40 unsigned ACK: 1; // Acknowlegement field is significant
41 unsigned URG: 1; // Urgent pointer is significant
42 unsigned ECE: 1; // ECN-Echo
43 unsigned CWR: 1; // Congestion Window Reduced
44 } __attribute__ ((packed)) Flags;
54 } __attribute__ ((packed));
70 struct sTCPListener *Next; //!< Next server in the list
71 Uint16 Port; //!< Listening port (0 disables the server)
72 tInterface *Interface; //!< Listening Interface
73 tVFS_Node Node; //!< Server Directory node
74 int NextID; //!< Name of the next connection
75 tSemaphore WaitingConnections;
76 tShortSpinlock lConnections; //!< Spinlock for connections
77 tTCPConnection *Connections; //!< Connections (linked list)
78 tTCPConnection *volatile NewConnections;
79 tTCPConnection *ConnectionsTail;
82 struct sTCPStoredPacket
84 struct sTCPStoredPacket *Next;
90 enum eTCPConnectionState
92 TCP_ST_CLOSED, // 0 - Connection invalid
94 TCP_ST_SYN_SENT, // 1 - SYN sent by local, waiting for SYN-ACK
95 TCP_ST_SYN_RCVD, // 2 - SYN recieved, SYN-ACK sent
97 TCP_ST_ESTABLISHED, // 3 - Connection open
100 TCP_ST_FIN_WAIT1, // 4 - FIN sent, waiting for reply (ACK or FIN)
101 TCP_ST_FIN_WAIT2, // 5 - sent FIN acked, waiting for FIN from peer
102 TCP_ST_CLOSING, // 6 - Waiting for ACK of FIN (FIN sent and recieved)
103 TCP_ST_TIME_WAIT, // 7 - Waiting for timeout after local close
105 TCP_ST_FORCE_CLOSE, // 8 - RST recieved, waiting for user close
106 TCP_ST_CLOSE_WAIT, // 9 - FIN recieved, waiting for user to close (error set, wait for node close)
107 TCP_ST_LAST_ACK, // 10 - FIN sent and recieved, waiting for ACK
108 TCP_ST_FINISHED // 11 - Essentially closed, all packets are invalid
111 struct sTCPConnection
113 struct sTCPConnection *Next;
114 struct sTCPConnection *Prev;
115 struct sTCPListener *Server;
117 enum eTCPConnectionState State; //!< Connection state (see ::eTCPConnectionState)
118 Uint16 LocalPort; //!< Local port
119 Uint16 RemotePort; //!< Remote port
120 tInterface *Interface; //!< Listening Interface
121 tVFS_Node Node; //!< Node
123 Uint32 NextSequenceSend; //!< Next sequence value for outbound packets
124 Uint32 NextSequenceRcv; //!< Next expected sequence value for inbound
126 // Deferred ACK handling
127 Uint32 LastACKSequence;
128 tTimer *DeferredACKTimer;
132 * \brief Non-ACKed packets
136 tMutex lNonACKedPackets;
137 tTCPStoredPacket *SentPackets; //!< Non-acknowleged packets
144 * \brief Unread Packets
148 tMutex lRecievedPackets;
149 tRingBuffer *RecievedBuffer;
155 * \brief Out of sequence packets
156 * \note Sorted list to improve times
157 * \todo Convert this to a ring buffer and a bitmap of valid bytes
160 Uint32 HighestSequenceRcvd; //!< Highest sequence number (within window) recieved
161 Uint8 *FuturePacketData; //!< Future packet data (indexed by sequence number)
162 Uint8 *FuturePacketValidBytes; //!< Valid byte bitmap (WINDOW_SIZE/8 bytes)
170 } RemoteIP; //!< Remote IP Address
171 // Type is determined by LocalInterface->Type