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

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