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

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