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

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