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

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