More fixing work on TCP, now correctly accepts packets,
[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         tSpinlock       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 struct sTCPConnection
85 {
86         struct sTCPConnection   *Next;
87          int    State;  //!< Connection state (see ::eTCPConnectionState)
88         Uint16  LocalPort;      //!< Local port
89         Uint16  RemotePort;     //!< Remote port
90         tInterface      *Interface;     //!< Listening Interface
91         tVFS_Node       Node;   //!< Node
92         
93         Uint32  NextSequenceSend;       //!< Next sequence value for outbound packets
94         Uint32  NextSequenceRcv;        //!< Next expected sequence value for inbound
95         
96         /**
97          * \brief Non-ACKed packets
98          * \note FIFO list
99          * \{
100          */
101         tSpinlock       lQueuedPackets;
102         tTCPStoredPacket        *QueuedPackets; //!< Non-ACKed packets
103         /**
104          * \}
105          */
106         
107         /**
108          * \brief Unread Packets
109          * \note Ring buffer
110          * \{
111          */
112         tSpinlock       lRecievedPackets;
113         tRingBuffer     *RecievedBuffer;
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