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_OPEN, // 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_CLOSE_WAIT, // 8 - FIN recieved, waiting for user to close (error set, wait for node close)
106 TCP_ST_LAST_ACK, // 9 - FIN sent and recieved, waiting for ACK
107 TCP_ST_FINISHED // 10 - Essentially closed, all packets are invalid
110 struct sTCPConnection
112 struct sTCPConnection *Next;
113 enum eTCPConnectionState State; //!< Connection state (see ::eTCPConnectionState)
114 Uint16 LocalPort; //!< Local port
115 Uint16 RemotePort; //!< Remote port
116 tInterface *Interface; //!< Listening Interface
117 tVFS_Node Node; //!< Node
119 Uint32 NextSequenceSend; //!< Next sequence value for outbound packets
120 Uint32 NextSequenceRcv; //!< Next expected sequence value for inbound
122 // Deferred ACK handling
123 Uint32 LastACKSequence;
124 tTimer *DeferredACKTimer;
128 * \brief Non-ACKed packets
132 tMutex lNonACKedPackets;
133 tTCPStoredPacket *SentPackets; //!< Non-acknowleged packets
140 * \brief Unread Packets
144 tMutex lRecievedPackets;
145 tRingBuffer *RecievedBuffer;
151 * \brief Out of sequence packets
152 * \note Sorted list to improve times
153 * \todo Convert this to a ring buffer and a bitmap of valid bytes
156 #if CACHE_FUTURE_PACKETS_IN_BYTES
157 Uint32 HighestSequenceRcvd; //!< Highest sequence number (within window) recieved
158 Uint8 *FuturePacketData; //!< Future packet data (indexed by sequence number)
159 Uint8 *FuturePacketValidBytes; //!< Valid byte bitmap (WINDOW_SIZE/8 bytes)
161 tShortSpinlock lFuturePackets; //!< Future packets spinlock
162 tTCPStoredPacket *FuturePackets; //!< Out of sequence packets
171 } RemoteIP; //!< Remote IP Address
172 // Type is determined by LocalInterface->Type