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

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