Usermode/libc - Fix strchr and strrchr behavior
[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 #define CACHE_FUTURE_PACKETS_IN_BYTES   1       // Use a ring buffer to cache out of order packets?
14
15 typedef struct sTCPHeader       tTCPHeader;
16 typedef struct sTCPListener     tTCPListener;
17 typedef struct sTCPStoredPacket tTCPStoredPacket;
18 typedef struct sTCPConnection   tTCPConnection;
19
20 struct sTCPHeader
21 {
22         Uint16  SourcePort;
23         Uint16  DestPort;
24         Uint32  SequenceNumber;
25         Uint32  AcknowlegementNumber;
26         #if 0
27         struct {        // Lowest to highest
28                 unsigned Reserved:      4;
29                 unsigned DataOffset: 4; // Size of the header in 32-bit words
30         } __attribute__ ((packed));
31         #else
32         Uint8   DataOffset;
33         #endif
34         #if 0
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;
45         #else
46         Uint8   Flags;
47         #endif
48         Uint16  WindowSize;
49         
50         Uint16  Checksum;
51         Uint16  UrgentPointer;
52         
53         Uint8   Options[];
54 } __attribute__ ((packed));
55
56 enum eTCPFlags
57 {
58         TCP_FLAG_FIN    = 0x01,
59         TCP_FLAG_SYN    = 0x02,
60         TCP_FLAG_RST    = 0x04,
61         TCP_FLAG_PSH    = 0x08,
62         TCP_FLAG_ACK    = 0x10,
63         TCP_FLAG_URG    = 0x20,
64         TCP_FLAG_ECE    = 0x40,
65         TCP_FLAG_CWR    = 0x80
66 };
67
68 struct sTCPListener
69 {
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;
80 };
81
82 struct sTCPStoredPacket
83 {
84         struct sTCPStoredPacket *Next;
85         size_t  Length;
86         Uint32  Sequence;
87         Uint8   Data[];
88 };
89
90 enum eTCPConnectionState
91 {
92         TCP_ST_CLOSED,          // 0 - Connection invalid
93         
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
96         
97         TCP_ST_ESTABLISHED,     // 3 - Connection open
98         
99         // Local Close
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
104         // Remote 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
109 };
110
111 struct sTCPConnection
112 {
113         struct sTCPConnection   *Next;
114         struct sTCPConnection   *Prev;
115         struct sTCPListener     *Server;
116         
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
122         
123         Uint32  NextSequenceSend;       //!< Next sequence value for outbound packets
124         Uint32  NextSequenceRcv;        //!< Next expected sequence value for inbound
125
126         // Deferred ACK handling
127         Uint32  LastACKSequence;
128         tTimer  *DeferredACKTimer;      
129
130         #if 0
131         /**
132          * \brief Non-ACKed packets
133          * \note Ring buffer
134          * \{
135          */
136         tMutex  lNonACKedPackets;
137         tTCPStoredPacket        *SentPackets;   //!< Non-acknowleged packets
138         /**
139          * \}
140          */
141         #endif
142         
143         /**
144          * \brief Unread Packets
145          * \note Ring buffer
146          * \{
147          */
148         tMutex  lRecievedPackets;
149         tRingBuffer     *RecievedBuffer;
150         /**
151          * \}
152          */
153         
154         /**
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
158          * \{
159          */
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)
163         /**
164          * \}
165          */
166         
167         union {
168                 tIPv4   v4;
169                 tIPv6   v6;
170         } RemoteIP;     //!< Remote IP Address
171         // Type is determined by LocalInterface->Type
172 };
173
174 #endif

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