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

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