Modules/IPStack - Added proper node reference counting to TCP
[tpg/acess2.git] / KernelLand / Modules / IPStack / tcp.c
1 /*
2  * Acess2 IP Stack
3  * - TCP Handling
4  */
5 #define DEBUG   0
6 #include "ipstack.h"
7 #include "ipv4.h"
8 #include "ipv6.h"
9 #include "tcp.h"
10
11 #define USE_SELECT      1
12 #define HEXDUMP_INCOMING        0
13 #define HEXDUMP_OUTGOING        0
14 #define CACHE_FUTURE_PACKETS_IN_BYTES   1       // Use a ring buffer to cache out of order packets
15
16 #define TCP_MIN_DYNPORT 0xC000
17 #define TCP_MAX_HALFOPEN        1024    // Should be enough
18
19 #define TCP_MAX_PACKET_SIZE     1024
20 #define TCP_WINDOW_SIZE 0x2000
21 #define TCP_RECIEVE_BUFFER_SIZE 0x8000
22 #define TCP_DACK_THRESHOLD      4096
23 #define TCP_DACK_TIMEOUT        500
24
25 #define TCP_DEBUG       0       // Set to non-0 to enable TCP packet logging
26
27 // === PROTOTYPES ===
28 void    TCP_Initialise(void);
29 void    TCP_StartConnection(tTCPConnection *Conn);
30 void    TCP_SendPacket(tTCPConnection *Conn, tTCPHeader *Header, size_t DataLen, const void *Data);
31 void    TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer);
32 void    TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header, int Length);
33 int     TCP_INT_AppendRecieved(tTCPConnection *Connection, const void *Data, size_t Length);
34 void    TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection);
35 void    TCP_int_SendDelayedACK(void *ConnPtr);
36 void    TCP_INT_SendACK(tTCPConnection *Connection, const char *Reason);
37 Uint16  TCP_GetUnusedPort();
38  int    TCP_AllocatePort(Uint16 Port);
39  int    TCP_DeallocatePort(Uint16 Port);
40 // --- Server
41 tVFS_Node       *TCP_Server_Init(tInterface *Interface);
42  int    TCP_Server_ReadDir(tVFS_Node *Node, int Pos, char Name[FILENAME_MAX]);
43 tVFS_Node       *TCP_Server_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
44  int    TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data);
45 void    TCP_Server_Close(tVFS_Node *Node);
46 // --- Client
47 tVFS_Node       *TCP_Client_Init(tInterface *Interface);
48 size_t  TCP_Client_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
49 size_t  TCP_Client_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
50  int    TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data);
51 void    TCP_Client_Close(tVFS_Node *Node);
52 // --- Helpers
53  int    WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue);
54
55 // === TEMPLATES ===
56 tSocketFile     gTCP_ServerFile = {NULL, "tcps", TCP_Server_Init};
57 tSocketFile     gTCP_ClientFile = {NULL, "tcpc", TCP_Client_Init};
58 tVFS_NodeType   gTCP_ServerNodeType = {
59         .TypeName = "TCP Server",
60         .ReadDir = TCP_Server_ReadDir,
61         .FindDir = TCP_Server_FindDir,
62         .IOCtl   = TCP_Server_IOCtl,
63         .Close   = TCP_Server_Close
64         };
65 tVFS_NodeType   gTCP_ClientNodeType = {
66         .TypeName = "TCP Client/Connection",
67         .Read  = TCP_Client_Read,
68         .Write = TCP_Client_Write,
69         .IOCtl = TCP_Client_IOCtl,
70         .Close = TCP_Client_Close
71         };
72
73 // === GLOBALS ===
74  int    giTCP_NumHalfopen = 0;
75 tShortSpinlock  glTCP_Listeners;
76 tTCPListener    *gTCP_Listeners;
77 tShortSpinlock  glTCP_OutbountCons;
78 tTCPConnection  *gTCP_OutbountCons;
79 Uint32  gaTCP_PortBitmap[0x800];
80  int    giTCP_NextOutPort = TCP_MIN_DYNPORT;
81
82 // === CODE ===
83 /**
84  * \brief Initialise the TCP Layer
85  * 
86  * Registers the client and server files and the GetPacket callback
87  */
88 void TCP_Initialise(void)
89 {
90         giTCP_NextOutPort += rand()%32;
91         IPStack_AddFile(&gTCP_ServerFile);
92         IPStack_AddFile(&gTCP_ClientFile);
93         IPv4_RegisterCallback(IP4PROT_TCP, TCP_GetPacket);
94         IPv6_RegisterCallback(IP4PROT_TCP, TCP_GetPacket);
95 }
96
97 /**
98  * \brief Sends a packet from the specified connection, calculating the checksums
99  * \param Conn  Connection
100  * \param Length        Length of data
101  * \param Data  Packet data (cast as a TCP Header)
102  */
103 void TCP_SendPacket( tTCPConnection *Conn, tTCPHeader *Header, size_t Length, const void *Data )
104 {
105         tIPStackBuffer  *buffer;
106         Uint16  checksum[3];
107          int    packlen = sizeof(*Header) + Length;
108         
109         buffer = IPStack_Buffer_CreateBuffer(2 + IPV4_BUFFERS);
110         if( Data && Length )
111                 IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, Data, NULL, NULL);
112         IPStack_Buffer_AppendSubBuffer(buffer, sizeof(*Header), 0, Header, NULL, NULL);
113
114         LOG("Sending %i+%i to %s:%i", sizeof(*Header), Length,
115                 IPStack_PrintAddress(Conn->Interface->Type, &Conn->RemoteIP),
116                 Conn->RemotePort
117                 );
118
119         Header->Checksum = 0;
120         checksum[1] = htons( ~IPv4_Checksum(Header, sizeof(tTCPHeader)) );
121         checksum[2] = htons( ~IPv4_Checksum(Data, Length) );
122         
123         // TODO: Fragment packet
124         
125         switch( Conn->Interface->Type )
126         {
127         case 4:
128                 // Get IPv4 pseudo-header checksum
129                 {
130                         Uint32  buf[3];
131                         buf[0] = ((tIPv4*)Conn->Interface->Address)->L;
132                         buf[1] = Conn->RemoteIP.v4.L;
133                         buf[2] = (htons(packlen)<<16) | (6<<8) | 0;
134                         checksum[0] = htons( ~IPv4_Checksum(buf, sizeof(buf)) );        // Partial checksum
135                 }
136                 // - Combine checksums
137                 Header->Checksum = htons( IPv4_Checksum(checksum, sizeof(checksum)) );
138                 IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, buffer);
139                 break;
140                 
141         case 6:
142                 // Append IPv6 Pseudo Header
143                 {
144                         Uint32  buf[4+4+1+1];
145                         memcpy(buf, Conn->Interface->Address, 16);
146                         memcpy(&buf[4], &Conn->RemoteIP, 16);
147                         buf[8] = htonl(packlen);
148                         buf[9] = htonl(6);
149                         checksum[0] = htons( ~IPv4_Checksum(buf, sizeof(buf)) );        // Partial checksum
150                 }
151                 Header->Checksum = htons( IPv4_Checksum(checksum, sizeof(checksum)) );  // Combine the two
152                 IPv6_SendPacket(Conn->Interface, Conn->RemoteIP.v6, IP4PROT_TCP, Length, Data);
153                 break;
154         }
155 }
156
157 /**
158  * \brief Handles a packet from the IP Layer
159  * \param Interface     Interface the packet arrived from
160  * \param Address       Pointer to the addres structure
161  * \param Length        Size of packet in bytes
162  * \param Buffer        Packet data
163  */
164 void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
165 {
166         tTCPHeader      *hdr = Buffer;
167         tTCPListener    *srv;
168         tTCPConnection  *conn;
169
170         #if TCP_DEBUG
171         Log_Log("TCP", "TCP_GetPacket: <Local>:%i from [%s]:%i, Flags = %s%s%s%s%s%s%s%s",
172                 ntohs(hdr->DestPort),
173                 IPStack_PrintAddress(Interface->Type, Address),
174                 ntohs(hdr->SourcePort),
175                 (hdr->Flags & TCP_FLAG_CWR) ? "CWR " : "",
176                 (hdr->Flags & TCP_FLAG_ECE) ? "ECE " : "",
177                 (hdr->Flags & TCP_FLAG_URG) ? "URG " : "",
178                 (hdr->Flags & TCP_FLAG_ACK) ? "ACK " : "",
179                 (hdr->Flags & TCP_FLAG_PSH) ? "PSH " : "",
180                 (hdr->Flags & TCP_FLAG_RST) ? "RST " : "",
181                 (hdr->Flags & TCP_FLAG_SYN) ? "SYN " : "",
182                 (hdr->Flags & TCP_FLAG_FIN) ? "FIN " : ""
183                 );
184         #endif
185
186         if( Length > (hdr->DataOffset >> 4)*4 )
187         {
188                 LOG("SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber));
189 #if HEXDUMP_INCOMING
190                 Debug_HexDump(
191                         "TCP_GetPacket: Packet Data = ",
192                         (Uint8*)hdr + (hdr->DataOffset >> 4)*4,
193                         Length - (hdr->DataOffset >> 4)*4
194                         );
195 #endif
196         }
197
198         // Check Servers
199         for( srv = gTCP_Listeners; srv; srv = srv->Next )
200         {
201                 // Check if the server is active
202                 if(srv->Port == 0)      continue;
203                 // Check the interface
204                 if(srv->Interface && srv->Interface != Interface)       continue;
205                 // Check the destination port
206                 if(srv->Port != htons(hdr->DestPort))   continue;
207                 
208                 Log_Log("TCP", "TCP_GetPacket: Matches server %p", srv);
209                 // Is this in an established connection?
210                 for( conn = srv->Connections; conn; conn = conn->Next )
211                 {
212                         // Check that it is coming in on the same interface
213                         if(conn->Interface != Interface)        continue;
214
215                         // Check Source Port
216                         Log_Log("TCP", "TCP_GetPacket: conn->RemotePort(%i) == hdr->SourcePort(%i)",
217                                 conn->RemotePort, ntohs(hdr->SourcePort));
218                         if(conn->RemotePort != ntohs(hdr->SourcePort))  continue;
219
220                         // Check Source IP
221                         Log_Debug("TCP", "TCP_GetPacket: conn->RemoteIP(%s)",
222                                 IPStack_PrintAddress(conn->Interface->Type, &conn->RemoteIP));
223                         Log_Debug("TCP", "                == Address(%s)",
224                                 IPStack_PrintAddress(conn->Interface->Type, Address));
225                         if( IPStack_CompareAddress(conn->Interface->Type, &conn->RemoteIP, Address, -1) == 0 )
226                                 continue ;
227
228                         Log_Log("TCP", "TCP_GetPacket: Matches connection %p", conn);
229                         // We have a response!
230                         TCP_INT_HandleConnectionPacket(conn, hdr, Length);
231
232                         return;
233                 }
234
235                 Log_Log("TCP", "TCP_GetPacket: Opening Connection");
236                 // Open a new connection (well, check that it's a SYN)
237                 if(hdr->Flags != TCP_FLAG_SYN) {
238                         Log_Log("TCP", "TCP_GetPacket: Packet is not a SYN");
239                         return ;
240                 }
241                 
242                 // TODO: Check for halfopen max
243                 
244                 conn = calloc(1, sizeof(tTCPConnection));
245                 conn->State = TCP_ST_SYN_RCVD;
246                 conn->LocalPort = srv->Port;
247                 conn->RemotePort = ntohs(hdr->SourcePort);
248                 conn->Interface = Interface;
249                 
250                 switch(Interface->Type)
251                 {
252                 case 4: conn->RemoteIP.v4 = *(tIPv4*)Address;   break;
253                 case 6: conn->RemoteIP.v6 = *(tIPv6*)Address;   break;
254                 }
255                 
256                 conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE );
257                 
258                 conn->NextSequenceRcv = ntohl( hdr->SequenceNumber ) + 1;
259                 conn->NextSequenceSend = rand();
260                 
261                 // Create node
262                 conn->Node.NumACLs = 1;
263                 conn->Node.ACLs = &gVFS_ACL_EveryoneRW;
264                 conn->Node.ImplPtr = conn;
265                 conn->Node.ImplInt = srv->NextID ++;
266                 conn->Node.Type = &gTCP_ClientNodeType; // TODO: Special type for the server end?
267                 
268                 // Hmm... Theoretically, this lock will never have to wait,
269                 // as the interface is locked to the watching thread, and this
270                 // runs in the watching thread. But, it's a good idea to have
271                 // it, just in case
272                 // Oh, wait, there is a case where a wildcard can be used
273                 // (srv->Interface == NULL) so having the lock is a good idea
274                 SHORTLOCK(&srv->lConnections);
275                 if( !srv->Connections )
276                         srv->Connections = conn;
277                 else
278                         srv->ConnectionsTail->Next = conn;
279                 srv->ConnectionsTail = conn;
280                 if(!srv->NewConnections)
281                         srv->NewConnections = conn;
282                 VFS_MarkAvaliable( &srv->Node, 1 );
283                 SHORTREL(&srv->lConnections);
284                 Semaphore_Signal(&srv->WaitingConnections, 1);
285
286                 // Send the SYN ACK
287                 hdr->Flags |= TCP_FLAG_ACK;
288                 hdr->AcknowlegementNumber = htonl(conn->NextSequenceRcv);
289                 hdr->SequenceNumber = htonl(conn->NextSequenceSend);
290                 hdr->DestPort = hdr->SourcePort;
291                 hdr->SourcePort = htons(srv->Port);
292                 hdr->DataOffset = (sizeof(tTCPHeader)/4) << 4;
293                 TCP_SendPacket( conn, hdr, 0, NULL );
294                 conn->NextSequenceSend ++;
295                 return ;
296         }
297
298         // Check Open Connections
299         {
300                 for( conn = gTCP_OutbountCons; conn; conn = conn->Next )
301                 {
302                         // Check that it is coming in on the same interface
303                         if(conn->Interface != Interface)        continue;
304
305                         // Check Source Port
306                         if(conn->RemotePort != ntohs(hdr->SourcePort))  continue;
307
308                         // Check Source IP
309                         if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
310                                 continue;
311                         if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
312                                 continue;
313
314                         TCP_INT_HandleConnectionPacket(conn, hdr, Length);
315                         return ;
316                 }
317         }
318         
319         Log_Log("TCP", "TCP_GetPacket: No Match");
320 }
321
322 /**
323  * \brief Handles a packet sent to a specific connection
324  * \param Connection    TCP Connection pointer
325  * \param Header        TCP Packet pointer
326  * \param Length        Length of the packet
327  */
328 void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header, int Length)
329 {
330          int    dataLen;
331         Uint32  sequence_num;
332         
333         // Silently drop once finished
334         // TODO: Check if this needs to be here
335         if( Connection->State == TCP_ST_FINISHED ) {
336                 Log_Log("TCP", "Packet ignored - connection finnished");
337                 return ;
338         }
339         
340         // Syncronise sequence values
341         if(Header->Flags & TCP_FLAG_SYN) {
342                 // TODO: What if the packet also has data?
343                 if( Connection->LastACKSequence != Connection->NextSequenceRcv )
344                         TCP_INT_SendACK(Connection, "SYN");
345                 Connection->NextSequenceRcv = ntohl(Header->SequenceNumber);
346                 Connection->LastACKSequence = Connection->NextSequenceRcv;
347         }
348         
349         // Ackowledge a sent packet
350         if(Header->Flags & TCP_FLAG_ACK) {
351                 // TODO: Process an ACKed Packet
352                 LOG("Conn %p, Sent packet 0x%x ACKed", Connection, Header->AcknowlegementNumber);
353         }
354         
355         // Get length of data
356         dataLen = Length - (Header->DataOffset>>4)*4;
357         LOG("dataLen = %i", dataLen);
358         #if TCP_DEBUG
359         Log_Debug("TCP", "State %i, dataLen = %x", Connection->State, dataLen);
360         #endif
361         
362         // 
363         // State Machine
364         //
365         switch( Connection->State )
366         {
367         // Pre-init connection?
368         case TCP_ST_CLOSED:
369                 Log_Log("TCP", "Packets to a closed connection?!");
370                 break;
371         
372         // --- Init States ---
373         // SYN sent, expecting SYN-ACK Connection Opening
374         case TCP_ST_SYN_SENT:
375                 if( Header->Flags & TCP_FLAG_SYN )
376                 {
377                         Connection->NextSequenceRcv ++;
378                         
379                         if( Header->Flags & TCP_FLAG_ACK )
380                         {       
381                                 Log_Log("TCP", "ACKing SYN-ACK");
382                                 Connection->State = TCP_ST_OPEN;
383                                 VFS_MarkFull(&Connection->Node, 0);
384                         }
385                         else
386                         {
387                                 Log_Log("TCP", "ACKing SYN");
388                                 Connection->State = TCP_ST_SYN_RCVD;
389                         }
390                         Header->DestPort = Header->SourcePort;
391                         Header->SourcePort = htons(Connection->LocalPort);
392                         Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
393                         Header->SequenceNumber = htonl(Connection->NextSequenceSend);
394                         Header->WindowSize = htons(TCP_WINDOW_SIZE);
395                         Header->Flags = TCP_FLAG_ACK;
396                         Header->DataOffset = (sizeof(tTCPHeader)/4) << 4;
397                         TCP_SendPacket( Connection, Header, 0, NULL );
398                 }
399                 break;
400         
401         // SYN-ACK sent, expecting ACK
402         case TCP_ST_SYN_RCVD:
403                 if( Header->Flags & TCP_FLAG_ACK )
404                 {
405                         // TODO: Handle max half-open limit
406                         Log_Log("TCP", "Connection fully opened");
407                         Connection->State = TCP_ST_OPEN;
408                         VFS_MarkFull(&Connection->Node, 0);
409                 }
410                 break;
411                 
412         // --- Established State ---
413         case TCP_ST_OPEN:
414                 // - Handle State changes
415                 //
416                 if( Header->Flags & TCP_FLAG_FIN ) {
417                         Log_Log("TCP", "Conn %p closed, recieved FIN", Connection);
418                         VFS_MarkError(&Connection->Node, 1);
419                         Connection->State = TCP_ST_CLOSE_WAIT;
420 //                      Header->Flags &= ~TCP_FLAG_FIN;
421                         // CLOSE WAIT requires the client to close (or does it?)
422                         #if 0
423                         
424                         #endif
425                 }
426         
427                 // Check for an empty packet
428                 if(dataLen == 0) {
429                         if( Header->Flags == TCP_FLAG_ACK )
430                         {
431                                 Log_Log("TCP", "ACK only packet");
432                                 return ;
433                         }
434                         Connection->NextSequenceRcv ++; // TODO: Is this right? (empty packet counts as one byte)
435                         Log_Log("TCP", "Empty Packet, inc and ACK the current sequence number");
436                         TCP_INT_SendACK(Connection, "Empty");
437                         #if 0
438                         Header->DestPort = Header->SourcePort;
439                         Header->SourcePort = htons(Connection->LocalPort);
440                         Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
441                         Header->SequenceNumber = htonl(Connection->NextSequenceSend);
442                         Header->Flags |= TCP_FLAG_ACK;
443                         TCP_SendPacket( Connection, Header, 0, NULL );
444                         #endif
445                         return ;
446                 }
447                 
448                 // NOTES:
449                 // Flags
450                 //    PSH - Has Data?
451                 // /NOTES
452                 
453                 sequence_num = ntohl(Header->SequenceNumber);
454                 
455                 LOG("0x%08x <= 0x%08x < 0x%08x",
456                         Connection->NextSequenceRcv,
457                         ntohl(Header->SequenceNumber),
458                         Connection->NextSequenceRcv + TCP_WINDOW_SIZE
459                         );
460                 
461                 // Is this packet the next expected packet?
462                 if( sequence_num == Connection->NextSequenceRcv )
463                 {
464                          int    rv;
465                         // Ooh, Goodie! Add it to the recieved list
466                         rv = TCP_INT_AppendRecieved(Connection,
467                                 (Uint8*)Header + (Header->DataOffset>>4)*4,
468                                 dataLen
469                                 );
470                         if(rv != 0) {
471                                 Log_Notice("TCP", "TCP_INT_AppendRecieved rv %i", rv);
472                                 break;
473                         }
474                         LOG("0x%08x += %i", Connection->NextSequenceRcv, dataLen);
475                         Connection->NextSequenceRcv += dataLen;
476                         
477                         // TODO: This should be moved out of the watcher thread,
478                         // so that a single lost packet on one connection doesn't cause
479                         // all connections on the interface to lag.
480                         // - Meh, no real issue, as the cache shouldn't be that large
481                         TCP_INT_UpdateRecievedFromFuture(Connection);
482
483                         #if 1
484                         // - Only send an ACK if we've had a burst
485                         if( Connection->NextSequenceRcv > (Uint32)(TCP_DACK_THRESHOLD + Connection->LastACKSequence) )
486                         {
487                                 TCP_INT_SendACK(Connection, "DACK Burst");
488                                 // - Extend TCP deferred ACK timer
489                                 Time_RemoveTimer(Connection->DeferredACKTimer);
490                         }
491                         // - Schedule the deferred ACK timer (if already scheduled, this is a NOP)
492                         Time_ScheduleTimer(Connection->DeferredACKTimer, TCP_DACK_TIMEOUT);
493                         #else
494                         TCP_INT_SendACK(Connection, "RX");
495                         #endif
496                 }
497                 // Check if the packet is in window
498                 else if( WrapBetween(Connection->NextSequenceRcv, sequence_num,
499                                 Connection->NextSequenceRcv+TCP_WINDOW_SIZE, 0xFFFFFFFF) )
500                 {
501                         Uint8   *dataptr = (Uint8*)Header + (Header->DataOffset>>4)*4;
502                         #if CACHE_FUTURE_PACKETS_IN_BYTES
503                         Uint32  index;
504                          int    i;
505                         
506                         index = sequence_num % TCP_WINDOW_SIZE;
507                         for( i = 0; i < dataLen; i ++ )
508                         {
509                                 Connection->FuturePacketValidBytes[index/8] |= 1 << (index%8);
510                                 Connection->FuturePacketData[index] = dataptr[i];
511                                 // Do a wrap increment
512                                 index ++;
513                                 if(index == TCP_WINDOW_SIZE)    index = 0;
514                         }
515                         #else
516                         tTCPStoredPacket        *pkt, *tmp, *prev = NULL;
517                         
518                         // Allocate and fill cached packet
519                         pkt = malloc( sizeof(tTCPStoredPacket) + dataLen );
520                         pkt->Next = NULL;
521                         pkt->Sequence = ntohl(Header->SequenceNumber);
522                         pkt->Length = dataLen;
523                         memcpy(pkt->Data, dataptr, dataLen);
524                         
525                         Log_Log("TCP", "We missed a packet, caching",
526                                 pkt->Sequence, Connection->NextSequenceRcv);
527                         
528                         // No? Well, let's cache it and look at it later
529                         SHORTLOCK( &Connection->lFuturePackets );
530                         for(tmp = Connection->FuturePackets;
531                                 tmp;
532                                 prev = tmp, tmp = tmp->Next)
533                         {
534                                 if(tmp->Sequence >= pkt->Sequence)      break;
535                         }
536                         
537                         // Add if before first, or sequences don't match 
538                         if( !tmp || tmp->Sequence != pkt->Sequence )
539                         {
540                                 if(prev)
541                                         prev->Next = pkt;
542                                 else
543                                         Connection->FuturePackets = pkt;
544                                 pkt->Next = tmp;
545                         }
546                         // Replace if larger
547                         else if(pkt->Length > tmp->Length)
548                         {
549                                 if(prev)
550                                         prev->Next = pkt;
551                                 pkt->Next = tmp->Next;
552                                 free(tmp);
553                         }
554                         else
555                         {
556                                 free(pkt);      // TODO: Find some way to remove this
557                         }
558                         SHORTREL( &Connection->lFuturePackets );
559                         #endif
560                 }
561                 // Badly out of sequence packet
562                 else
563                 {
564                         Log_Log("TCP", "Fully out of sequence packet (0x%08x not between 0x%08x and 0x%08x), dropped",
565                                 sequence_num, Connection->NextSequenceRcv, Connection->NextSequenceRcv+TCP_WINDOW_SIZE);
566                         // Spec says we should send an empty ACK with the current state
567                         TCP_INT_SendACK(Connection, "Bad Seq");
568                 }
569                 break;
570         
571         // --- Remote close states
572         case TCP_ST_CLOSE_WAIT:
573                 
574                 // Ignore everything, CLOSE_WAIT is terminated by the client
575                 Log_Debug("TCP", "CLOSE WAIT - Ignoring packets");
576                 
577                 break;
578         
579         // LAST-ACK - Waiting for the ACK of FIN (from CLOSE WAIT)
580         case TCP_ST_LAST_ACK:
581                 if( Header->Flags & TCP_FLAG_ACK )
582                 {
583                         Connection->State = TCP_ST_FINISHED;    // Connection completed
584                         Log_Log("TCP", "LAST-ACK to CLOSED - Connection remote closed");
585                         // TODO: Destrory the TCB
586                 }
587                 break;
588         
589         // --- Local close States
590         case TCP_ST_FIN_WAIT1:
591                 if( Header->Flags & TCP_FLAG_FIN )
592                 {
593                         Connection->State = TCP_ST_CLOSING;
594                         Log_Debug("TCP", "Conn %p closed, sent FIN and recieved FIN", Connection);
595                         VFS_MarkError(&Connection->Node, 1);
596                         
597                         // ACK Packet
598                         Header->DestPort = Header->SourcePort;
599                         Header->SourcePort = htons(Connection->LocalPort);
600                         Header->AcknowlegementNumber = Header->SequenceNumber;
601                         Header->SequenceNumber = htonl(Connection->NextSequenceSend);
602                         Header->WindowSize = htons(TCP_WINDOW_SIZE);
603                         Header->Flags = TCP_FLAG_ACK;
604                         TCP_SendPacket( Connection, Header, 0, NULL );
605                         break ;
606                 }
607                 
608                 // TODO: Make sure that the packet is actually ACKing the FIN
609                 if( Header->Flags & TCP_FLAG_ACK )
610                 {
611                         Connection->State = TCP_ST_FIN_WAIT2;
612                         Log_Debug("TCP", "Conn %p closed, sent FIN ACKed", Connection);
613                         VFS_MarkError(&Connection->Node, 1);
614                         return ;
615                 }
616                 break;
617         
618         case TCP_ST_FIN_WAIT2:
619                 if( Header->Flags & TCP_FLAG_FIN )
620                 {
621                         Connection->State = TCP_ST_TIME_WAIT;
622                         Log_Debug("TCP", "FIN sent and recieved, ACKing and going into TIME WAIT %p FINWAIT-2 -> TIME WAIT", Connection);
623                         // Send ACK
624                         Header->DestPort = Header->SourcePort;
625                         Header->SourcePort = htons(Connection->LocalPort);
626                         Header->AcknowlegementNumber = Header->SequenceNumber;
627                         Header->SequenceNumber = htonl(Connection->NextSequenceSend);
628                         Header->WindowSize = htons(TCP_WINDOW_SIZE);
629                         Header->Flags = TCP_FLAG_ACK;
630                         TCP_SendPacket( Connection, Header, 0, NULL );
631                 }
632                 break;
633         
634         case TCP_ST_CLOSING:
635                 // TODO: Make sure that the packet is actually ACKing the FIN
636                 if( Header->Flags & TCP_FLAG_ACK )
637                 {
638                         Connection->State = TCP_ST_TIME_WAIT;
639                         Log_Debug("TCP", "Conn %p CLOSING -> TIME WAIT", Connection);
640                         VFS_MarkError(&Connection->Node, 1);
641                         return ;
642                 }
643                 break;
644         
645         // --- Closed (or near closed) states) ---
646         case TCP_ST_TIME_WAIT:
647                 Log_Log("TCP", "Packets on Time-Wait, ignored");
648                 break;
649         
650         case TCP_ST_FINISHED:
651                 Log_Log("TCP", "Packets when CLOSED, ignoring");
652                 break;
653         
654         //default:
655         //      Log_Warning("TCP", "Unhandled TCP state %i", Connection->State);
656         //      break;
657         }
658         
659 }
660
661 /**
662  * \brief Appends a packet to the recieved list
663  * \param Connection    Connection structure
664  * \param Data  Packet contents
665  * \param Length        Length of \a Data
666  */
667 int TCP_INT_AppendRecieved(tTCPConnection *Connection, const void *Data, size_t Length)
668 {
669         Mutex_Acquire( &Connection->lRecievedPackets );
670
671         if(Connection->RecievedBuffer->Length + Length > Connection->RecievedBuffer->Space )
672         {
673                 VFS_MarkAvaliable(&Connection->Node, 1);
674                 Log_Error("TCP", "Buffer filled, packet dropped (:%i) - %i + %i > %i",
675                         Connection->LocalPort, Connection->RecievedBuffer->Length, Length,
676                         Connection->RecievedBuffer->Space
677                         );
678                 Mutex_Release( &Connection->lRecievedPackets );
679                 return 1;
680         }
681         
682         RingBuffer_Write( Connection->RecievedBuffer, Data, Length );
683
684         VFS_MarkAvaliable(&Connection->Node, 1);
685         
686         Mutex_Release( &Connection->lRecievedPackets );
687         return 0;
688 }
689
690 /**
691  * \brief Updates the connections recieved list from the future list
692  * \param Connection    Connection structure
693  * 
694  * Updates the recieved packets list with packets from the future (out 
695  * of order) packets list that are now able to be added in direct
696  * sequence.
697  */
698 void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection)
699 {
700         #if CACHE_FUTURE_PACKETS_IN_BYTES
701          int    i, length = 0;
702         Uint32  index;
703         
704         // Calculate length of contiguous bytes
705         length = Connection->HighestSequenceRcvd - Connection->NextSequenceRcv;
706         index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE;
707         for( i = 0; i < length; i ++ )
708         {
709                 if( Connection->FuturePacketValidBytes[i / 8] == 0xFF ) {
710                         i += 7; index += 7;
711                         continue;
712                 }
713                 else if( !(Connection->FuturePacketValidBytes[i / 8] & (1 << (i%8))) )
714                         break;
715                 
716                 index ++;
717                 if(index > TCP_WINDOW_SIZE)
718                         index -= TCP_WINDOW_SIZE;
719         }
720         length = i;
721         
722         index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE;
723         
724         // Write data to to the ring buffer
725         if( TCP_WINDOW_SIZE - index > length )
726         {
727                 // Simple case
728                 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, length );
729         }
730         else
731         {
732                  int    endLen = TCP_WINDOW_SIZE - index;
733                 // 2-part case
734                 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, endLen );
735                 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData, endLen - length );
736         }
737         
738         // Mark (now saved) bytes as invalid
739         // - Align index
740         while(index % 8 && length)
741         {
742                 Connection->FuturePacketData[index] = 0;
743                 Connection->FuturePacketData[index/8] &= ~(1 << (index%8));
744                 index ++;
745                 if(index > TCP_WINDOW_SIZE)
746                         index -= TCP_WINDOW_SIZE;
747                 length --;
748         }
749         while( length > 7 )
750         {
751                 Connection->FuturePacketData[index] = 0;
752                 Connection->FuturePacketValidBytes[index/8] = 0;
753                 length -= 8;
754                 index += 8;
755                 if(index > TCP_WINDOW_SIZE)
756                         index -= TCP_WINDOW_SIZE;
757         }
758         while(length)
759         {
760                 Connection->FuturePacketData[index] = 0;
761                 Connection->FuturePacketData[index/8] &= ~(1 << (index%8));
762                 index ++;
763                 if(index > TCP_WINDOW_SIZE)
764                         index -= TCP_WINDOW_SIZE;
765                 length --;
766         }
767         
768         #else
769         tTCPStoredPacket        *pkt;
770         for(;;)
771         {
772                 SHORTLOCK( &Connection->lFuturePackets );
773                 
774                 // Clear out duplicates from cache
775                 // - If a packet has just been recieved, and it is expected, then
776                 //   (since NextSequenceRcv = rcvd->Sequence + rcvd->Length) all
777                 //   packets in cache that are smaller than the next expected
778                 //   are now defunct.
779                 pkt = Connection->FuturePackets;
780                 while(pkt && pkt->Sequence < Connection->NextSequenceRcv)
781                 {
782                         tTCPStoredPacket        *next = pkt->Next;
783                         free(pkt);
784                         pkt = next;
785                 }
786                 
787                 // If there's no packets left in cache, stop looking
788                 if(!pkt || pkt->Sequence > Connection->NextSequenceRcv) {
789                         SHORTREL( &Connection->lFuturePackets );
790                         return;
791                 }
792                 
793                 // Delete packet from future list
794                 Connection->FuturePackets = pkt->Next;
795                 
796                 // Release list
797                 SHORTREL( &Connection->lFuturePackets );
798                 
799                 // Looks like we found one
800                 TCP_INT_AppendRecieved(Connection, pkt);
801                 Connection->NextSequenceRcv += pkt->Length;
802                 free(pkt);
803         }
804         #endif
805 }
806
807 void TCP_int_SendDelayedACK(void *ConnPtr)
808 {
809         TCP_INT_SendACK(ConnPtr, "DACK Timeout");
810 }
811
812 void TCP_INT_SendACK(tTCPConnection *Connection, const char *Reason)
813 {
814         tTCPHeader      hdr;
815         // ACK Packet
816         hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4;
817         hdr.DestPort = htons(Connection->RemotePort);
818         hdr.SourcePort = htons(Connection->LocalPort);
819         hdr.AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
820         hdr.SequenceNumber = htonl(Connection->NextSequenceSend);
821         hdr.WindowSize = htons(TCP_WINDOW_SIZE);
822         hdr.Flags = TCP_FLAG_ACK;       // TODO: Determine if SYN is wanted too
823         hdr.Checksum = 0;       // TODO: Checksum
824         hdr.UrgentPointer = 0;
825         Log_Debug("TCP", "Sending ACK for 0x%08x (%s)", Connection->NextSequenceRcv, Reason);
826         TCP_SendPacket( Connection, &hdr, 0, NULL );
827         //Connection->NextSequenceSend ++;
828         Connection->LastACKSequence = Connection->NextSequenceRcv;
829 }
830
831 /**
832  * \fn Uint16 TCP_GetUnusedPort()
833  * \brief Gets an unused port and allocates it
834  */
835 Uint16 TCP_GetUnusedPort()
836 {
837         Uint16  ret;
838
839         // Get Next outbound port
840         ret = giTCP_NextOutPort++;
841         while( gaTCP_PortBitmap[ret/32] & (1UL << (ret%32)) )
842         {
843                 ret ++;
844                 giTCP_NextOutPort++;
845                 if(giTCP_NextOutPort == 0x10000) {
846                         ret = giTCP_NextOutPort = TCP_MIN_DYNPORT;
847                 }
848         }
849
850         // Mark the new port as used
851         gaTCP_PortBitmap[ret/32] |= 1 << (ret%32);
852
853         return ret;
854 }
855
856 /**
857  * \fn int TCP_AllocatePort(Uint16 Port)
858  * \brief Marks a port as used
859  */
860 int TCP_AllocatePort(Uint16 Port)
861 {
862         // Check if the port has already been allocated
863         if( gaTCP_PortBitmap[Port/32] & (1 << (Port%32)) )
864                 return 0;
865
866         // Allocate
867         gaTCP_PortBitmap[Port/32] |= 1 << (Port%32);
868
869         return 1;
870 }
871
872 /**
873  * \fn int TCP_DeallocatePort(Uint16 Port)
874  * \brief Marks a port as unused
875  */
876 int TCP_DeallocatePort(Uint16 Port)
877 {
878         // Check if the port has already been allocated
879         if( !(gaTCP_PortBitmap[Port/32] & (1 << (Port%32))) )
880                 return 0;
881
882         // Allocate
883         gaTCP_PortBitmap[Port/32] &= ~(1 << (Port%32));
884
885         return 1;
886 }
887
888 // --- Server
889 tVFS_Node *TCP_Server_Init(tInterface *Interface)
890 {
891         tTCPListener    *srv;
892         
893         srv = calloc( 1, sizeof(tTCPListener) );
894
895         if( srv == NULL ) {
896                 Log_Warning("TCP", "malloc failed for listener (%i) bytes", sizeof(tTCPListener));
897                 return NULL;
898         }
899
900         srv->Interface = Interface;
901         srv->Port = 0;
902         srv->NextID = 0;
903         srv->Connections = NULL;
904         srv->ConnectionsTail = NULL;
905         srv->NewConnections = NULL;
906         srv->Next = NULL;
907         srv->Node.Flags = VFS_FFLAG_DIRECTORY;
908         srv->Node.Size = -1;
909         srv->Node.ImplPtr = srv;
910         srv->Node.NumACLs = 1;
911         srv->Node.ACLs = &gVFS_ACL_EveryoneRW;
912         srv->Node.Type = &gTCP_ServerNodeType;
913
914         SHORTLOCK(&glTCP_Listeners);
915         srv->Next = gTCP_Listeners;
916         gTCP_Listeners = srv;
917         SHORTREL(&glTCP_Listeners);
918
919         return &srv->Node;
920 }
921
922 /**
923  * \brief Wait for a new connection and return the connection ID
924  * \note Blocks until a new connection is made
925  * \param Node  Server node
926  * \param Pos   Position (ignored)
927  */
928 int TCP_Server_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
929 {
930         tTCPListener    *srv = Node->ImplPtr;
931         tTCPConnection  *conn;
932         
933         ENTER("pNode iPos", Node, Pos);
934
935         Log_Log("TCP", "Thread %i waiting for a connection", Threads_GetTID());
936         Semaphore_Wait( &srv->WaitingConnections, 1 );
937         
938         SHORTLOCK(&srv->lConnections);
939         // Increment the new list (the current connection is still on the 
940         // normal list)
941         conn = srv->NewConnections;
942         srv->NewConnections = conn->Next;
943
944         if( srv->NewConnections == NULL )
945                 VFS_MarkAvaliable( Node, 0 );
946         
947         SHORTREL( &srv->lConnections );
948         
949         LOG("conn = %p", conn);
950         LOG("srv->Connections = %p", srv->Connections);
951         LOG("srv->NewConnections = %p", srv->NewConnections);
952         LOG("srv->ConnectionsTail = %p", srv->ConnectionsTail);
953
954         itoa(Dest, conn->Node.ImplInt, 16, 8, '0');
955         Log_Log("TCP", "Thread %i got connection '%s'", Threads_GetTID(), Dest);
956         LEAVE('i', 0);
957         return 0;
958 }
959
960 /**
961  * \brief Gets a client connection node
962  * \param Node  Server node
963  * \param Name  Hexadecimal ID of the node
964  */
965 tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
966 {
967         tTCPConnection  *conn;
968         tTCPListener    *srv = Node->ImplPtr;
969         char    tmp[9];
970          int    id = atoi(Name);
971         
972         ENTER("pNode sName", Node, Name);
973
974         // Check for a non-empty name
975         if( Name[0] ) 
976         {       
977                 // Sanity Check
978                 itoa(tmp, id, 16, 8, '0');
979                 if(strcmp(tmp, Name) != 0) {
980                         LOG("'%s' != '%s' (%08x)", Name, tmp, id);
981                         LEAVE('n');
982                         return NULL;
983                 }
984                 
985                 Log_Debug("TCP", "srv->Connections = %p", srv->Connections);
986                 Log_Debug("TCP", "srv->NewConnections = %p", srv->NewConnections);
987                 Log_Debug("TCP", "srv->ConnectionsTail = %p", srv->ConnectionsTail);
988                 
989                 // Search
990                 SHORTLOCK( &srv->lConnections );
991                 for(conn = srv->Connections;
992                         conn;
993                         conn = conn->Next)
994                 {
995                         LOG("conn->Node.ImplInt = %i", conn->Node.ImplInt);
996                         if(conn->Node.ImplInt == id)    break;
997                 }
998                 SHORTREL( &srv->lConnections );
999
1000                 // If not found, ret NULL
1001                 if(!conn) {
1002                         LOG("Connection %i not found", id);
1003                         LEAVE('n');
1004                         return NULL;
1005                 }
1006         }
1007         // Empty Name - Check for a new connection and if it's there, open it
1008         else
1009         {
1010                 SHORTLOCK( &srv->lConnections );
1011                 conn = srv->NewConnections;
1012                 if( conn != NULL )
1013                         srv->NewConnections = conn->Next;
1014                 VFS_MarkAvaliable( Node, srv->NewConnections != NULL );
1015                 SHORTREL( &srv->lConnections );
1016                 if( !conn ) {
1017                         LOG("No new connections");
1018                         LEAVE('n');
1019                         return NULL;
1020                 }
1021         }
1022                 
1023         // Return node
1024         LEAVE('p', &conn->Node);
1025         return &conn->Node;
1026 }
1027
1028 /**
1029  * \brief Handle IOCtl calls
1030  */
1031 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data)
1032 {
1033         tTCPListener    *srv = Node->ImplPtr;
1034
1035         switch(ID)
1036         {
1037         case 4: // Get/Set Port
1038                 if(!Data)       // Get Port
1039                         return srv->Port;
1040
1041                 if(srv->Port)   // Wait, you can't CHANGE the port
1042                         return -1;
1043
1044                 if(!CheckMem(Data, sizeof(Uint16)))     // Sanity check
1045                         return -1;
1046
1047                 // Permissions check
1048                 if(Threads_GetUID() != 0
1049                 && *(Uint16*)Data != 0
1050                 && *(Uint16*)Data < 1024)
1051                         return -1;
1052
1053                 // TODO: Check if a port is in use
1054
1055                 // Set Port
1056                 srv->Port = *(Uint16*)Data;
1057                 if(srv->Port == 0)      // Allocate a random port
1058                         srv->Port = TCP_GetUnusedPort();
1059                 else    // Else, mark this as used
1060                         TCP_AllocatePort(srv->Port);
1061                 
1062                 Log_Log("TCP", "Server %p listening on port %i", srv, srv->Port);
1063                 
1064                 return srv->Port;
1065         }
1066         return 0;
1067 }
1068
1069 void TCP_Server_Close(tVFS_Node *Node)
1070 {
1071         free(Node->ImplPtr);
1072 }
1073
1074 // --- Client
1075 /**
1076  * \brief Create a client node
1077  */
1078 tVFS_Node *TCP_Client_Init(tInterface *Interface)
1079 {
1080         tTCPConnection  *conn = calloc( sizeof(tTCPConnection) + TCP_WINDOW_SIZE + TCP_WINDOW_SIZE/8, 1 );
1081
1082         conn->State = TCP_ST_CLOSED;
1083         conn->Interface = Interface;
1084         conn->LocalPort = -1;
1085         conn->RemotePort = -1;
1086
1087         conn->Node.ReferenceCount = 1;
1088         conn->Node.ImplPtr = conn;
1089         conn->Node.NumACLs = 1;
1090         conn->Node.ACLs = &gVFS_ACL_EveryoneRW;
1091         conn->Node.Type = &gTCP_ClientNodeType;
1092         conn->Node.BufferFull = 1;      // Cleared when connection opens
1093
1094         conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE );
1095         #if 0
1096         conn->SentBuffer = RingBuffer_Create( TCP_SEND_BUFFER_SIZE );
1097         Semaphore_Init(conn->SentBufferSpace, 0, TCP_SEND_BUFFER_SIZE, "TCP SentBuffer", conn->Name);
1098         #endif
1099         
1100         #if CACHE_FUTURE_PACKETS_IN_BYTES
1101         // Future recieved data (ahead of the expected sequence number)
1102         conn->FuturePacketData = (Uint8*)conn + sizeof(tTCPConnection);
1103         conn->FuturePacketValidBytes = conn->FuturePacketData + TCP_WINDOW_SIZE;
1104         #endif
1105
1106         conn->DeferredACKTimer = Time_AllocateTimer( TCP_int_SendDelayedACK, conn);
1107
1108         SHORTLOCK(&glTCP_OutbountCons);
1109         conn->Next = gTCP_OutbountCons;
1110         gTCP_OutbountCons = conn;
1111         SHORTREL(&glTCP_OutbountCons);
1112
1113         return &conn->Node;
1114 }
1115
1116 /**
1117  * \brief Wait for a packet and return it
1118  * \note If \a Length is smaller than the size of the packet, the rest
1119  *       of the packet's data will be discarded.
1120  */
1121 size_t TCP_Client_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
1122 {
1123         tTCPConnection  *conn = Node->ImplPtr;
1124         size_t  len;
1125         
1126         ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
1127         LOG("conn = %p {State:%i}", conn, conn->State);
1128         
1129         // If the connection has been closed (state > ST_OPEN) then clear
1130         // any stale data in the buffer (until it is empty (until it is empty))
1131         if( conn->State > TCP_ST_OPEN )
1132         {
1133                 Mutex_Acquire( &conn->lRecievedPackets );
1134                 len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length );
1135                 Mutex_Release( &conn->lRecievedPackets );
1136                 
1137                 if( len == 0 ) {
1138                         VFS_MarkAvaliable(Node, 0);
1139                         errno = 0;
1140                         LEAVE('i', -1);
1141                         return -1;
1142                 }
1143                 
1144                 LEAVE('i', len);
1145                 return len;
1146         }
1147         
1148         // Wait
1149         {
1150                 tTime   *timeout = NULL;
1151                 tTime   timeout_zero = 0;
1152                 if( Flags & VFS_IOFLAG_NOBLOCK )
1153                         timeout = &timeout_zero;
1154                 if( !VFS_SelectNode(Node, VFS_SELECT_READ|VFS_SELECT_ERROR, timeout, "TCP_Client_Read") ) {
1155                         errno = EWOULDBLOCK;
1156                         LEAVE('i', -1);
1157                         return -1;
1158                 }
1159         }
1160         
1161         // Lock list and read as much as possible (up to `Length`)
1162         Mutex_Acquire( &conn->lRecievedPackets );
1163         len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length );
1164         
1165         if( len == 0 || conn->RecievedBuffer->Length == 0 ) {
1166                 LOG("Marking as none avaliable (len = %i)", len);
1167                 VFS_MarkAvaliable(Node, 0);
1168         }
1169                 
1170         // Release the lock (we don't need it any more)
1171         Mutex_Release( &conn->lRecievedPackets );
1172
1173         LEAVE('i', len);
1174         return len;
1175 }
1176
1177 /**
1178  * \brief Send a data packet on a connection
1179  */
1180 void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, const void *Data)
1181 {
1182         char    buf[sizeof(tTCPHeader)+Length];
1183         tTCPHeader      *packet = (void*)buf;
1184         
1185         packet->SourcePort = htons(Connection->LocalPort);
1186         packet->DestPort = htons(Connection->RemotePort);
1187         packet->DataOffset = (sizeof(tTCPHeader)/4)*16;
1188         packet->WindowSize = htons(TCP_WINDOW_SIZE);
1189         
1190         packet->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
1191         packet->SequenceNumber = htonl(Connection->NextSequenceSend);
1192         packet->Flags = TCP_FLAG_PSH|TCP_FLAG_ACK;      // Hey, ACK if you can!
1193         
1194         memcpy(packet->Options, Data, Length);
1195         
1196         Log_Debug("TCP", "Send sequence 0x%08x", Connection->NextSequenceSend);
1197 #if HEXDUMP_OUTGOING
1198         Debug_HexDump("TCP_INT_SendDataPacket: Data = ", Data, Length);
1199 #endif
1200         
1201         TCP_SendPacket( Connection, packet, Length, Data );
1202         
1203         Connection->NextSequenceSend += Length;
1204 }
1205
1206 /**
1207  * \brief Send some bytes on a connection
1208  */
1209 size_t TCP_Client_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
1210 {
1211         tTCPConnection  *conn = Node->ImplPtr;
1212         size_t  rem = Length;
1213         
1214         ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
1215         
1216 //      #if DEBUG
1217 //      Debug_HexDump("TCP_Client_Write: Buffer = ",
1218 //              Buffer, Length);
1219 //      #endif
1220         
1221         // Don't allow a write to a closed connection
1222         if( conn->State > TCP_ST_OPEN ) {
1223                 VFS_MarkError(Node, 1);
1224                 errno = 0;
1225                 LEAVE('i', -1);
1226                 return -1;
1227         }
1228         
1229         // Wait
1230         {
1231                 tTime   *timeout = NULL;
1232                 tTime   timeout_zero = 0;
1233                 if( Flags & VFS_IOFLAG_NOBLOCK )
1234                         timeout = &timeout_zero;
1235                 if( !VFS_SelectNode(Node, VFS_SELECT_WRITE|VFS_SELECT_ERROR, timeout, "TCP_Client_Write") ) {
1236                         errno = EWOULDBLOCK;
1237                         LEAVE('i', -1);
1238                         return -1;
1239                 }
1240         }
1241         
1242         do
1243         {
1244                  int    len = (rem < TCP_MAX_PACKET_SIZE) ? rem : TCP_MAX_PACKET_SIZE;
1245                 
1246                 #if 0
1247                 // Wait for space in the buffer
1248                 Semaphore_Signal( &Connection->SentBufferSpace, len );
1249                 
1250                 // Save data to buffer (and update the length read by the ammount written)
1251                 len = RingBuffer_Write( &Connection->SentBuffer, Buffer, len);
1252                 #endif
1253                 
1254                 // Send packet
1255                 TCP_INT_SendDataPacket(conn, len, Buffer);
1256                 
1257                 Buffer += len;
1258                 rem -= len;
1259         } while( rem > 0 );
1260         
1261         LEAVE('i', Length);
1262         return Length;
1263 }
1264
1265 /**
1266  * \brief Open a connection to another host using TCP
1267  * \param Conn  Connection structure
1268  */
1269 void TCP_StartConnection(tTCPConnection *Conn)
1270 {
1271         tTCPHeader      hdr = {0};
1272
1273         Conn->State = TCP_ST_SYN_SENT;
1274
1275         hdr.SourcePort = htons(Conn->LocalPort);
1276         hdr.DestPort = htons(Conn->RemotePort);
1277         Conn->NextSequenceSend = rand();
1278         hdr.SequenceNumber = htonl(Conn->NextSequenceSend);
1279         hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4;
1280         hdr.Flags = TCP_FLAG_SYN;
1281         hdr.WindowSize = htons(TCP_WINDOW_SIZE);        // Max
1282         hdr.Checksum = 0;       // TODO
1283         
1284         TCP_SendPacket( Conn, &hdr, 0, NULL );
1285         
1286         Conn->NextSequenceSend ++;
1287         Conn->State = TCP_ST_SYN_SENT;
1288
1289         return ;
1290 }
1291
1292 /**
1293  * \brief Control a client socket
1294  */
1295 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
1296 {
1297         tTCPConnection  *conn = Node->ImplPtr;
1298         
1299         ENTER("pNode iID pData", Node, ID, Data);
1300
1301         switch(ID)
1302         {
1303         case 4: // Get/Set local port
1304                 if(!Data)
1305                         LEAVE_RET('i', conn->LocalPort);
1306                 if(conn->State != TCP_ST_CLOSED)
1307                         LEAVE_RET('i', -1);
1308                 if(!CheckMem(Data, sizeof(Uint16)))
1309                         LEAVE_RET('i', -1);
1310
1311                 if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
1312                         LEAVE_RET('i', -1);
1313
1314                 conn->LocalPort = *(Uint16*)Data;
1315                 LEAVE_RET('i', conn->LocalPort);
1316
1317         case 5: // Get/Set remote port
1318                 if(!Data)       LEAVE_RET('i', conn->RemotePort);
1319                 if(conn->State != TCP_ST_CLOSED)        LEAVE_RET('i', -1);
1320                 if(!CheckMem(Data, sizeof(Uint16)))     LEAVE_RET('i', -1);
1321                 conn->RemotePort = *(Uint16*)Data;
1322                 LEAVE_RET('i', conn->RemotePort);
1323
1324         case 6: // Set Remote IP
1325                 if( conn->State != TCP_ST_CLOSED )
1326                         LEAVE_RET('i', -1);
1327                 if( conn->Interface->Type == 4 )
1328                 {
1329                         if(!CheckMem(Data, sizeof(tIPv4)))      LEAVE_RET('i', -1);
1330                         conn->RemoteIP.v4 = *(tIPv4*)Data;
1331                 }
1332                 else if( conn->Interface->Type == 6 )
1333                 {
1334                         if(!CheckMem(Data, sizeof(tIPv6)))      LEAVE_RET('i', -1);
1335                         conn->RemoteIP.v6 = *(tIPv6*)Data;
1336                 }
1337                 LEAVE_RET('i', 0);
1338
1339         case 7: // Connect
1340                 if(conn->LocalPort == 0xFFFF)
1341                         conn->LocalPort = TCP_GetUnusedPort();
1342                 if(conn->RemotePort == -1)
1343                         LEAVE_RET('i', 0);
1344
1345                 {
1346                         tTime   timeout = conn->Interface->TimeoutDelay;
1347         
1348                         TCP_StartConnection(conn);
1349                         VFS_SelectNode(&conn->Node, VFS_SELECT_WRITE, &timeout, "TCP Connection");
1350                         if( conn->State == TCP_ST_SYN_SENT )
1351                                 LEAVE_RET('i', 0);
1352                 }
1353
1354                 LEAVE_RET('i', 1);
1355         
1356         // Get recieve buffer length
1357         case 8:
1358                 LEAVE_RET('i', conn->RecievedBuffer->Length);
1359         }
1360
1361         return 0;
1362 }
1363
1364 void TCP_Client_Close(tVFS_Node *Node)
1365 {
1366         tTCPConnection  *conn = Node->ImplPtr;
1367         tTCPHeader      packet;
1368         
1369         ENTER("pNode", Node);
1370         
1371         ASSERT(Node->ReferenceCount != 0);
1372
1373         if( Node->ReferenceCount > 1 ) {
1374                 Node->ReferenceCount --;
1375                 LOG("Dereference only");
1376                 LEAVE('-');
1377                 return ;
1378         }
1379         
1380         if( conn->State == TCP_ST_CLOSE_WAIT || conn->State == TCP_ST_OPEN )
1381         {
1382                 packet.SourcePort = htons(conn->LocalPort);
1383                 packet.DestPort = htons(conn->RemotePort);
1384                 packet.DataOffset = (sizeof(tTCPHeader)/4)*16;
1385                 packet.WindowSize = TCP_WINDOW_SIZE;
1386                 
1387                 packet.AcknowlegementNumber = 0;
1388                 packet.SequenceNumber = htonl(conn->NextSequenceSend);
1389                 packet.Flags = TCP_FLAG_FIN;
1390                 
1391                 TCP_SendPacket( conn, &packet, 0, NULL );
1392         }
1393         
1394         switch( conn->State )
1395         {
1396         case TCP_ST_CLOSED:
1397                 Log_Warning("TCP", "Closing connection that was never opened");
1398                 break;
1399         case TCP_ST_CLOSE_WAIT:
1400                 conn->State = TCP_ST_LAST_ACK;
1401                 break;
1402         case TCP_ST_OPEN:
1403                 conn->State = TCP_ST_FIN_WAIT1;
1404                 while( conn->State == TCP_ST_FIN_WAIT1 )        Threads_Yield();
1405                 break;
1406         default:
1407                 Log_Warning("TCP", "Unhandled connection state %i in TCP_Client_Close",
1408                         conn->State);
1409                 break;
1410         }
1411         
1412         Time_RemoveTimer(conn->DeferredACKTimer);
1413         Time_FreeTimer(conn->DeferredACKTimer);
1414         free(conn);
1415         
1416         LEAVE('-');
1417 }
1418
1419 /**
1420  * \brief Checks if a value is between two others (after taking into account wrapping)
1421  */
1422 int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue)
1423 {
1424         if( MaxValue < 0xFFFFFFFF )
1425         {
1426                 Lower %= MaxValue + 1;
1427                 Value %= MaxValue + 1;
1428                 Higher %= MaxValue + 1;
1429         }
1430         
1431         // Simple Case, no wrap ?
1432         //       Lower Value Higher
1433         // | ... + ... + ... + ... |
1434
1435         if( Lower < Higher ) {
1436                 return Lower < Value && Value < Higher;
1437         }
1438         // Higher has wrapped below lower
1439         
1440         // Value > Lower ?
1441         //       Higher Lower Value
1442         // | ... +  ... + ... + ... |
1443         if( Value > Lower ) {
1444                 return 1;
1445         }
1446         
1447         // Value < Higher ?
1448         //       Value Higher Lower
1449         // | ... + ... +  ... + ... |
1450         if( Value < Higher ) {
1451                 return 1;
1452         }
1453         
1454         return 0;
1455 }

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