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

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