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

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