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

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