More work on TCP, still not usable
[tpg/acess2.git] / Modules / IPStack / tcp.c
1 /*
2  * Acess2 IP Stack
3  * - TCP Handling
4  */
5 #include "ipstack.h"
6 #include "ipv4.h"
7 #include "tcp.h"
8
9 #define TCP_MIN_DYNPORT 0x1000
10 #define TCP_MAX_HALFOPEN        1024    // Should be enough
11
12 // === PROTOTYPES ===
13 void    TCP_Initialise();
14 void    TCP_StartConnection(tTCPConnection *Conn);
15 void    TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer);
16 void    TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header);
17 Uint16  TCP_GetUnusedPort();
18  int    TCP_AllocatePort(Uint16 Port);
19  int    TCP_DeallocatePort(Uint16 Port);
20 // --- Server
21 tVFS_Node       *TCP_Server_Init(tInterface *Interface);
22 char    *TCP_Server_ReadDir(tVFS_Node *Node, int Pos);
23 tVFS_Node       *TCP_Server_FindDir(tVFS_Node *Node, char *Name);
24  int    TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data);
25 void    TCP_Server_Close(tVFS_Node *Node);
26 // --- Client
27 tVFS_Node       *TCP_Client_Init(tInterface *Interface);
28 Uint64  TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
29 Uint64  TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
30  int    TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data);
31 void    TCP_Client_Close(tVFS_Node *Node);
32
33 // === TEMPLATES ===
34 tSocketFile     gTCP_ServerFile = {NULL, "tcps", TCP_Server_Init};
35 tSocketFile     gTCP_ClientFile = {NULL, "tcpc", TCP_Client_Init};
36
37 // === GLOBALS ===
38  int    giTCP_NumHalfopen = 0;
39 tSpinlock       glTCP_Listeners;
40 tTCPListener    *gTCP_Listeners;
41 tSpinlock       glTCP_OutbountCons;
42 tTCPConnection  *gTCP_OutbountCons;
43 Uint32  gaTCP_PortBitmap[0x800];
44  int    giTCP_NextOutPort = TCP_MIN_DYNPORT;
45
46 // === CODE ===
47 /**
48  * \fn void TCP_Initialise()
49  * \brief Initialise the TCP Layer
50  */
51 void TCP_Initialise()
52 {
53         IPStack_AddFile(&gTCP_ServerFile);
54         IPStack_AddFile(&gTCP_ClientFile);
55         IPv4_RegisterCallback(IP4PROT_TCP, TCP_GetPacket);
56 }
57
58 /**
59  * \brief Open a connection to another host using TCP
60  */
61 void TCP_StartConnection(tTCPConnection *Conn)
62 {
63         tTCPHeader      hdr;
64         
65         hdr->SourcePort = Conn->LocalPort;
66         hdr->DestPort = Conn->RemotePort;
67         hdr->SequenceNumber = rand();
68         hdr->DataOffset = (sizeof(tTCPHeader)+3)/4;
69         hdr->Flags = TCP_FLAG_SYN;
70         // SEND PACKET
71         // Send a TCP SYN to the target to open the connection
72         return ;
73 }
74
75 /**
76  * \fn void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
77  * \brief Handles a packet from the IP Layer
78  */
79 void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
80 {
81         tTCPHeader      *hdr = Buffer;
82         tTCPListener    *srv;
83         tTCPConnection  *conn;
84         
85         Log("[TCP  ] sizeof(tTCPHeader) = %i", sizeof(tTCPHeader));
86         Log("[TCP  ] DestPort = %i", ntohs(hdr->DestPort));
87         Log("[TCP  ] DestPort = %i", ntohs(hdr->DestPort));
88         Log("[TCP  ] SequenceNumber = %i", ntohl(hdr->SequenceNumber));
89         Log("[TCP  ] AcknowlegementNumber = %i", ntohl(hdr->AcknowlegementNumber));
90         Log("[TCP  ] DataOffset = %i", hdr->DataOffset >> 4);
91         Log("[TCP  ] Flags = {");
92         Log("[TCP  ]   CWR = %B", !!(hdr->Flags & TCP_FLAG_CWR));
93         Log("[TCP  ]   ECE = %B", !!(hdr->Flags & TCP_FLAG_ECE));
94         Log("[TCP  ]   URG = %B", !!(hdr->Flags & TCP_FLAG_URG));
95         Log("[TCP  ]   ACK = %B", !!(hdr->Flags & TCP_FLAG_ACK));
96         Log("[TCP  ]   PSH = %B", !!(hdr->Flags & TCP_FLAG_PSH));
97         Log("[TCP  ]   RST = %B", !!(hdr->Flags & TCP_FLAG_RST));
98         Log("[TCP  ]   SYN = %B", !!(hdr->Flags & TCP_FLAG_SYN));
99         Log("[TCP  ]   FIN = %B", !!(hdr->Flags & TCP_FLAG_FIN));
100         Log("[TCP  ] }");
101         Log("[TCP  ] WindowSize = %i", htons(hdr->WindowSize));
102         Log("[TCP  ] Checksum = 0x%x", htons(hdr->Checksum));
103         Log("[TCP  ] UrgentPointer = 0x%x", htons(hdr->UrgentPointer));
104         
105         // Check Servers
106         {
107                 for( srv = gTCP_Listeners; srv; srv = srv->Next )
108                 {
109                         // Check if the server is active
110                         if(srv->Port == 0)      continue;
111                         // Check the interface
112                         if(srv->Interface && srv->Interface != Interface)       continue;
113                         // Check the destination port
114                         if(srv->Port != hdr->DestPort)  continue;
115                         
116                         // Is this in an established connection?
117                         for( conn = srv->Connections; conn; conn = conn->Next )
118                         {
119                                 // Check that it is coming in on the same interface
120                                 if(conn->Interface != Interface)        continue;
121                                 
122                                 // Check Source Port
123                                 if(conn->RemotePort != hdr->SourcePort) continue;
124                                 
125                                 // Check Source IP
126                                 if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
127                                         continue;
128                                 if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
129                                         continue;
130                                 
131                                 // We have a response!
132                                 TCP_INT_HandleConnectionPacket(conn, hdr)
133                                 
134                                 return;
135                         }
136                         
137                         // Open a new connection (well, check that it's a SYN)
138                         //TODO
139                         
140                         break;
141                 }
142         }
143         
144         
145         // Check Open Connections
146         {
147                 for( conn = gTCP_OutbountCons; conn; conn = conn->Next )
148                 {
149                         // Check that it is coming in on the same interface
150                         if(conn->Interface != Interface)        continue;
151                         
152                         // Check Source Port
153                         if(conn->RemotePort != hdr->SourcePort) continue;
154                         
155                         // Check Source IP
156                         if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
157                                 continue;
158                         if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
159                                 continue;
160                         
161                         TCP_INT_HandleConnectionPacket(conn, hdr)
162                 }
163         }
164 }
165
166 /**
167  * \brief Handles a packet sent to a specific connection
168  */
169 void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header)
170 {
171         
172 }
173
174 /**
175  * \fn Uint16 TCP_GetUnusedPort()
176  * \brief Gets an unused port and allocates it
177  */
178 Uint16 TCP_GetUnusedPort()
179 {
180         Uint16  ret;
181         
182         // Get Next outbound port
183         ret = giTCP_NextOutPort++;
184         while( gaTCP_PortBitmap[ret/32] & (1 << (ret%32)) )
185         {
186                 ret ++;
187                 giTCP_NextOutPort++;
188                 if(giTCP_NextOutPort == 0x10000) {
189                         ret = giTCP_NextOutPort = TCP_MIN_DYNPORT;
190                 }
191         }
192         
193         // Mark the new port as used
194         gaTCP_PortBitmap[ret/32] |= 1 << (ret%32);
195         
196         return ret;
197 }
198
199 /**
200  * \fn int TCP_AllocatePort(Uint16 Port)
201  * \brief Marks a port as used
202  */
203 int TCP_AllocatePort(Uint16 Port)
204 {
205         // Check if the port has already been allocated
206         if( gaTCP_PortBitmap[Port/32] & (1 << (Port%32)) )
207                 return 0;
208         
209         // Allocate
210         gaTCP_PortBitmap[Port/32] |= 1 << (Port%32);
211         
212         return 1;
213 }
214
215 /**
216  * \fn int TCP_DeallocatePort(Uint16 Port)
217  * \brief Marks a port as unused
218  */
219 int TCP_DeallocatePort(Uint16 Port)
220 {
221         // Check if the port has already been allocated
222         if( !(gaTCP_PortBitmap[Port/32] & (1 << (Port%32))) )
223                 return 0;
224         
225         // Allocate
226         gaTCP_PortBitmap[Port/32] &= ~(1 << (Port%32));
227         
228         return 1;
229 }
230
231 // --- Server
232 tVFS_Node *TCP_Server_Init(tInterface *Interface)
233 {
234         tTCPListener    *srv = malloc( sizeof(tTCPListener) );
235         
236         srv->Interface = Interface;
237         srv->Port = 0;
238         srv->NextID = 0;
239         srv->Connections = NULL;
240         srv->Next = NULL;
241         srv->Node.ImplPtr = srv;
242         srv->Node.NumACLs = 1;
243         srv->Node.ACLs = &gVFS_ACL_EveryoneRW;
244         srv->Node.ReadDir = TCP_Server_ReadDir;
245         srv->Node.FindDir = TCP_Server_FindDir;
246         srv->Node.IOCtl = TCP_Server_IOCtl;
247         srv->Node.Close = TCP_Server_Close;
248         
249         LOCK(&glTCP_Listeners);
250         srv->Next = gTCP_Listeners;
251         gTCP_Listeners = srv;
252         RELEASE(&glTCP_Listeners);
253         
254         return &srv->Node;
255 }
256
257 /**
258  * \brief Wait for a new connection and return the connection ID
259  * \note Blocks until a new connection is made
260  * \param Node  Server node
261  * \param Pos   Position (ignored)
262  */
263 char *TCP_Server_ReadDir(tVFS_Node *Node, int Pos)
264 {
265         tTCPListener    *srv = Node->ImplPtr;
266         tTCPConnection  *conn;
267         char    *ret;
268         
269         while( srv->NewConnections == NULL )    Threads_Yield();
270         
271         conn = srv->NewConnections;
272         srv->NewConnections = conn->Next;
273         
274         ret = malloc(9);
275         itoa(ret, conn->ImplInt, 16, '0', 8);
276         return ret;
277 }
278
279 /**
280  * \brief Gets a client connection node
281  * \param Node  Server node
282  * \param Name  Hexadecimal ID of the node
283  */
284 tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, char *Name)
285 {
286         return NULL;
287 }
288
289 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data)
290 {
291         tTCPListener    *srv = Node->ImplPtr;
292         
293         switch(ID)
294         {
295         case 4: // Get/Set Port
296                 if(!Data)       // Get Port
297                         return srv->Port;
298                 
299                 if(srv->Port)   // Wait, you can't CHANGE the port
300                         return -1;
301                 
302                 if(!CheckMem(Data, sizeof(Uint16)))     // Sanity check
303                         return -1;
304                 
305                 // Permissions check
306                 if(Threads_GetUID() != 0
307                 && *(Uint16*)Data != 0
308                 && *(Uint16*)Data < 1024)
309                         return -1;
310                 
311                 // TODO: Check if a port is in use
312                 
313                 // Set Port
314                 srv->Port = *(Uint16*)Data;
315                 if(srv->Port == 0)      // Allocate a random port
316                         srv->Port = TCP_GetUnusedPort();
317                 else    // Else, mark this as used
318                         TCP_AllocatePort(srv->Port);
319                 return srv->Port;
320         }
321         return 0;
322 }
323
324 void TCP_Server_Close(tVFS_Node *Node)
325 {
326         free(Node->ImplPtr);
327 }
328
329 // --- Client
330 tVFS_Node *TCP_Client_Init(tInterface *Interface)
331 {
332         tTCPConnection  *conn = malloc( sizeof(tTCPConnection) );
333         
334         conn->State = TCP_ST_CLOSED;
335         conn->Interface = Interface;
336         conn->LocalPort = 0;
337         conn->RemotePort = 0;
338         memset( &conn->RemoteIP, 0, sizeof(conn->RemoteIP) );
339         
340         conn->Node.ImplPtr = conn;
341         conn->Node.NumACLs = 1;
342         conn->Node.ACLs = &gVFS_ACL_EveryoneRW;
343         conn->Node.Read = TCP_Client_Read;
344         conn->Node.Write = TCP_Client_Write;
345         conn->Node.IOCtl = TCP_Client_IOCtl;
346         conn->Node.Close = TCP_Client_Close;
347         
348         LOCK(&glTCP_OutbountCons);
349         conn->Next = gTCP_OutbountCons;
350         gTCP_OutbountCons = conn;
351         RELEASE(&glTCP_OutbountCons);
352         
353         return &conn->Node;
354 }
355
356 Uint64 TCP_Client_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
357 {
358         return 0;
359 }
360
361 Uint64 TCP_Client_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
362 {
363         return 0;
364 }
365
366 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
367 {
368         tTCPConnection  *conn = Node->ImplPtr;
369         
370         switch(ID)
371         {
372         case 4: // Get/Set local port
373                 if(!Data)
374                         return conn->LocalPort;
375                 if(conn->State != TCP_ST_CLOSED)
376                         return -1;
377                 if(!CheckMem(Data, sizeof(Uint16)))
378                         return -1;
379                 
380                 if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
381                         return -1;
382                 
383                 conn->LocalPort = *(Uint16*)Data;
384                 return 0;
385         
386         case 5: // Get/Set remote port
387                 if(!Data)       return conn->RemotePort;
388                 if(conn->State != TCP_ST_CLOSED)        return -1;
389                 if(!CheckMem(Data, sizeof(Uint16)))     return -1;
390                 conn->RemotePort = *(Uint16*)Data;
391                 return 0;
392         
393         case 6: // Set Remote IP
394                 if( conn->State != TCP_ST_CLOSED )
395                         return -1;
396                 if( conn->Interface->Type == 4 )
397                 {
398                         if(!CheckMem(Data, sizeof(tIPv4)))      return -1;
399                         conn->RemoteIP.v4 = *(tIPv4*)Data;
400                 }
401                 else if( conn->Interface->Type == 6 )
402                 {
403                         if(!CheckMem(Data, sizeof(tIPv6)))      return -1;
404                         conn->RemoteIP.v6 = *(tIPv6*)Data;
405                 }
406                 return 0;
407         
408         case 7: // Connect
409                 if(conn->LocalPort == -1)
410                         conn->LocalPort = TCP_GetUnusedPort();
411                 if(conn->RemotePort == -1)
412                         return 0;
413                 
414                 TCP_StartConnection(conn);
415                 return 1;
416         }
417         
418         return 0;
419 }
420
421 void TCP_Client_Close(tVFS_Node *Node)
422 {
423         free(Node->ImplPtr);
424 }

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