More work on TCP, untested and almost complete
[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 {
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         tSpinlock       lConnections;   //!< Spinlock for connections
71         tTCPConnection  *Connections;   //!< Connections (linked list)
72          tTCPConnection *volatile NewConnections;
73 };
74
75 struct sTCPStoredPacket
76 {
77         struct sTCPStoredPacket *Next;
78         size_t  Length;
79         Uint32  Sequence;
80         Uint8   Data[];
81 };
82
83 struct sTCPConnection
84 {
85         struct sTCPConnection   *Next;
86          int    State;  //!< Connection state (see ::eTCPConnectionState)
87         Uint16  LocalPort;      //!< Local port
88         Uint16  RemotePort;     //!< Remote port
89         tInterface      *Interface;     //!< Listening Interface
90         tVFS_Node       Node;   //!< Node
91         
92          int    NextSequenceSend;       //!< Next sequence value for outbound packets
93          int    NextSequenceRcv;        //!< Next expected sequence value for inbound
94         
95         /**
96          * \brief Non-ACKed packets
97          * \note FIFO list
98          * \{
99          */
100         tSpinlock       lQueuedPackets;
101         tTCPStoredPacket        *QueuedPackets; //!< Non-ACKed packets
102         /**
103          * \}
104          */
105         
106         /**
107          * \brief Unread Packets
108          * \note Double ended list (fifo)
109          * \{
110          */
111         tSpinlock       lRecievedPackets;
112         tTCPStoredPacket        *RecievedPackets;       //!< Unread Packets
113         tTCPStoredPacket        *RecievedPacketsTail;   //!< Unread Packets (End of list)
114         /**
115          * \}
116          */
117         
118         /**
119          * \brief Out of sequence packets
120          * \note Sorted list to improve times
121          * \{
122          */
123         tSpinlock       lFuturePackets; //!< Future packets spinlock
124         tTCPStoredPacket        *FuturePackets; //!< Out of sequence packets
125         /**
126          * \}
127          */
128         
129         union {
130                 tIPv4   v4;
131                 tIPv6   v6;
132         } RemoteIP;     //!< Remote IP Address
133         // Type is determined by LocalInterface->Type
134 };
135
136 enum eTCPConnectionState
137 {
138         TCP_ST_CLOSED,
139         TCP_ST_HALFOPEN,
140         TCP_ST_OPEN
141 };
142
143 #endif

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