Modules/IPStack - Killing Threads_Yield
[tpg/acess2.git] / KernelLand / Modules / IPStack / tcp.h
1 /*
2  * Acess2 IP Stack
3  * - TCP Definitions
4  */
5 #ifndef _TCP_H_
6 #define _TCP_H_
7
8 #include "ipstack.h"
9 #include <adt.h>        // tRingBuffer
10 #include <timers.h>     // tTimer
11 #include <semaphore.h>  // tSemaphore
12
13 typedef struct sTCPHeader       tTCPHeader;
14 typedef struct sTCPListener     tTCPListener;
15 typedef struct sTCPStoredPacket tTCPStoredPacket;
16 typedef struct sTCPConnection   tTCPConnection;
17
18 struct sTCPHeader
19 {
20         Uint16  SourcePort;
21         Uint16  DestPort;
22         Uint32  SequenceNumber;
23         Uint32  AcknowlegementNumber;
24         #if 0
25         struct {        // Lowest to highest
26                 unsigned Reserved:      4;
27                 unsigned DataOffset: 4; // Size of the header in 32-bit words
28         } __attribute__ ((packed));
29         #else
30         Uint8   DataOffset;
31         #endif
32         #if 0
33         struct {        // Lowest to Highest
34                 unsigned FIN:   1;      // Last packet
35                 unsigned SYN:   1;      // Synchronise Sequence Numbers
36                 unsigned RST:   1;      // Reset Connection
37                 unsigned PSH:   1;      // Push Function
38                 unsigned ACK:   1;      // Acknowlegement field is significant
39                 unsigned URG:   1;      // Urgent pointer is significant
40                 unsigned ECE:   1;      // ECN-Echo
41                 unsigned CWR:   1;      // Congestion Window Reduced
42         } __attribute__ ((packed)) Flags;
43         #else
44         Uint8   Flags;
45         #endif
46         Uint16  WindowSize;
47         
48         Uint16  Checksum;
49         Uint16  UrgentPointer;
50         
51         Uint8   Options[];
52 } __attribute__ ((packed));
53
54 enum eTCPFlags
55 {
56         TCP_FLAG_FIN    = 0x01,
57         TCP_FLAG_SYN    = 0x02,
58         TCP_FLAG_RST    = 0x04,
59         TCP_FLAG_PSH    = 0x08,
60         TCP_FLAG_ACK    = 0x10,
61         TCP_FLAG_URG    = 0x20,
62         TCP_FLAG_ECE    = 0x40,
63         TCP_FLAG_CWR    = 0x80
64 };
65
66 struct sTCPListener
67 {
68         struct sTCPListener     *Next;  //!< Next server in the list
69         Uint16  Port;           //!< Listening port (0 disables the server)
70         tInterface      *Interface;     //!< Listening Interface
71         tVFS_Node       Node;   //!< Server Directory node
72          int    NextID;         //!< Name of the next connection
73         tSemaphore      WaitingConnections;
74         tShortSpinlock  lConnections;   //!< Spinlock for connections
75         tTCPConnection  *Connections;   //!< Connections (linked list)
76         tTCPConnection  *volatile NewConnections;
77         tTCPConnection  *ConnectionsTail;
78 };
79
80 struct sTCPStoredPacket
81 {
82         struct sTCPStoredPacket *Next;
83         size_t  Length;
84         Uint32  Sequence;
85         Uint8   Data[];
86 };
87
88 enum eTCPConnectionState
89 {
90         TCP_ST_CLOSED,          // 0 - Connection invalid
91         
92         TCP_ST_SYN_SENT,        // 1 - SYN sent by local, waiting for SYN-ACK
93         TCP_ST_SYN_RCVD,        // 2 - SYN recieved, SYN-ACK sent
94         
95         TCP_ST_OPEN,            // 3 - Connection open
96         
97         // Local Close
98         TCP_ST_FIN_WAIT1,       // 4 - FIN sent, waiting for reply (ACK or FIN)
99         TCP_ST_FIN_WAIT2,       // 5 - sent FIN acked, waiting for FIN from peer
100         TCP_ST_CLOSING,         // 6 - Waiting for ACK of FIN (FIN sent and recieved)
101         TCP_ST_TIME_WAIT,       // 7 - Waiting for timeout after local close
102         // Remote close
103         TCP_ST_CLOSE_WAIT,      // 8 - FIN recieved, waiting for user to close (error set, wait for node close)
104         TCP_ST_LAST_ACK,        // 9 - FIN sent and recieved, waiting for ACK
105         TCP_ST_FINISHED         // 10 - Essentially closed, all packets are invalid
106 };
107
108 struct sTCPConnection
109 {
110         struct sTCPConnection   *Next;
111         enum eTCPConnectionState        State;  //!< Connection state (see ::eTCPConnectionState)
112         Uint16  LocalPort;      //!< Local port
113         Uint16  RemotePort;     //!< Remote port
114         tInterface      *Interface;     //!< Listening Interface
115         tVFS_Node       Node;   //!< Node
116         
117         Uint32  NextSequenceSend;       //!< Next sequence value for outbound packets
118         Uint32  NextSequenceRcv;        //!< Next expected sequence value for inbound
119
120         // Deferred ACK handling
121         Uint32  LastACKSequence;
122         tTimer  *DeferredACKTimer;      
123
124         #if 0
125         /**
126          * \brief Non-ACKed packets
127          * \note Ring buffer
128          * \{
129          */
130         tMutex  lNonACKedPackets;
131         tTCPStoredPacket        *SentPackets;   //!< Non-acknowleged packets
132         /**
133          * \}
134          */
135         #endif
136         
137         /**
138          * \brief Unread Packets
139          * \note Ring buffer
140          * \{
141          */
142         tMutex  lRecievedPackets;
143         tRingBuffer     *RecievedBuffer;
144         /**
145          * \}
146          */
147         
148         /**
149          * \brief Out of sequence packets
150          * \note Sorted list to improve times
151          * \todo Convert this to a ring buffer and a bitmap of valid bytes
152          * \{
153          */
154         #if CACHE_FUTURE_PACKETS_OR_BYTES == bytes
155         Uint32  HighestSequenceRcvd;    //!< Highest sequence number (within window) recieved
156         Uint8   *FuturePacketData;      //!< Future packet data (indexed by sequence number)
157         Uint8   *FuturePacketValidBytes;        //!< Valid byte bitmap (WINDOW_SIZE/8 bytes)
158         #else
159         tShortSpinlock  lFuturePackets; //!< Future packets spinlock
160         tTCPStoredPacket        *FuturePackets; //!< Out of sequence packets
161         #endif
162         /**
163          * \}
164          */
165         
166         union {
167                 tIPv4   v4;
168                 tIPv6   v6;
169         } RemoteIP;     //!< Remote IP Address
170         // Type is determined by LocalInterface->Type
171 };
172
173 #endif

UCC git Repository :: git.ucc.asn.au