11 #define HEXDUMP_INCOMING 0
12 #define HEXDUMP_OUTGOING 0
14 #define TCP_MIN_DYNPORT 0xC000
15 #define TCP_MAX_HALFOPEN 1024 // Should be enough
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
23 #define TCP_DEBUG 0 // Set to non-0 to enable TCP packet logging
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);
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);
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);
52 int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue);
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
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
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;
83 * \brief Initialise the TCP Layer
85 * Registers the client and server files and the GetPacket callback
87 void TCP_Initialise(void)
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);
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)
102 void TCP_SendPacket( tTCPConnection *Conn, tTCPHeader *Header, size_t Length, const void *Data )
104 tIPStackBuffer *buffer;
106 int packlen = sizeof(*Header) + Length;
108 buffer = IPStack_Buffer_CreateBuffer(2 + IPV4_BUFFERS);
110 IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, Data, NULL, NULL);
111 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(*Header), 0, Header, NULL, NULL);
113 LOG("Sending %i+%i to %s:%i", sizeof(*Header), Length,
114 IPStack_PrintAddress(Conn->Interface->Type, &Conn->RemoteIP),
118 Header->Checksum = 0;
119 checksum[1] = htons( ~IPv4_Checksum(Header, sizeof(tTCPHeader)) );
120 checksum[2] = htons( ~IPv4_Checksum(Data, Length) );
122 // TODO: Fragment packet
124 switch( Conn->Interface->Type )
127 // Get IPv4 pseudo-header checksum
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
135 // - Combine checksums
136 Header->Checksum = htons( IPv4_Checksum(checksum, sizeof(checksum)) );
137 IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, buffer);
141 // Append IPv6 Pseudo Header
144 memcpy(buf, Conn->Interface->Address, 16);
145 memcpy(&buf[4], &Conn->RemoteIP, 16);
146 buf[8] = htonl(packlen);
148 checksum[0] = htons( ~IPv4_Checksum(buf, sizeof(buf)) ); // Partial checksum
150 Header->Checksum = htons( IPv4_Checksum(checksum, sizeof(checksum)) ); // Combine the two
151 IPv6_SendPacket(Conn->Interface, Conn->RemoteIP.v6, IP4PROT_TCP, Length, Data);
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
163 void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
165 tTCPHeader *hdr = Buffer;
167 tTCPConnection *conn;
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 " : ""
185 if( Length > (hdr->DataOffset >> 4)*4 )
187 LOG("SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber));
190 "TCP_GetPacket: Packet Data = ",
191 (Uint8*)hdr + (hdr->DataOffset >> 4)*4,
192 Length - (hdr->DataOffset >> 4)*4
198 for( srv = gTCP_Listeners; srv; srv = srv->Next )
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;
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 )
211 // Check that it is coming in on the same interface
212 if(conn->Interface != Interface) continue;
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;
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 )
227 Log_Log("TCP", "TCP_GetPacket: Matches connection %p", conn);
228 // We have a response!
229 TCP_INT_HandleConnectionPacket(conn, hdr, Length);
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");
241 // TODO: Check for halfopen max
243 conn = TCP_int_CreateConnection(Interface, TCP_ST_SYN_RCVD);
244 conn->LocalPort = srv->Port;
245 conn->RemotePort = ntohs(hdr->SourcePort);
247 switch(Interface->Type)
249 case 4: conn->RemoteIP.v4 = *(tIPv4*)Address; break;
250 case 6: conn->RemoteIP.v6 = *(tIPv6*)Address; break;
253 conn->NextSequenceRcv = ntohl( hdr->SequenceNumber ) + 1;
254 conn->HighestSequenceRcvd = conn->NextSequenceRcv;
255 conn->NextSequenceSend = rand();
257 conn->Node.ImplInt = srv->NextID ++;
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
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;
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);
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 ++;
289 // Check Open Connections
291 for( conn = gTCP_OutbountCons; conn; conn = conn->Next )
293 // Check that it is coming in on the same interface
294 if(conn->Interface != Interface) continue;
297 if(conn->RemotePort != ntohs(hdr->SourcePort)) continue;
300 if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
302 if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
305 TCP_INT_HandleConnectionPacket(conn, hdr, Length);
310 Log_Log("TCP", "TCP_GetPacket: No Match");
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
319 void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header, int Length)
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");
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
339 if( Connection->HighestSequenceRcvd == 0 )
340 Connection->HighestSequenceRcvd = Connection->NextSequenceRcv;
341 Connection->LastACKSequence = Connection->NextSequenceRcv;
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);
350 // Get length of data
351 dataLen = Length - (Header->DataOffset>>4)*4;
352 LOG("dataLen = %i", dataLen);
354 Log_Debug("TCP", "State %i, dataLen = %x", Connection->State, dataLen);
360 switch( Connection->State )
362 // Pre-init connection?
364 Log_Log("TCP", "Packets to a closed connection?!");
367 // --- Init States ---
368 // SYN sent, expecting SYN-ACK Connection Opening
369 case TCP_ST_SYN_SENT:
370 if( Header->Flags & TCP_FLAG_SYN )
372 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
373 Connection->HighestSequenceRcvd ++;
374 Connection->NextSequenceRcv ++;
376 if( Header->Flags & TCP_FLAG_ACK )
378 Log_Log("TCP", "ACKing SYN-ACK");
379 Connection->State = TCP_ST_OPEN;
380 VFS_MarkFull(&Connection->Node, 0);
384 Log_Log("TCP", "ACKing SYN");
385 Connection->State = TCP_ST_SYN_RCVD;
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 );
398 // SYN-ACK sent, expecting ACK
399 case TCP_ST_SYN_RCVD:
400 if( Header->Flags & TCP_FLAG_ACK )
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);
409 // --- Established State ---
411 // - Handle State changes
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?)
424 // Check for an empty packet
426 if( Header->Flags == TCP_FLAG_ACK )
428 Log_Log("TCP", "ACK only packet");
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");
445 sequence_num = ntohl(Header->SequenceNumber);
447 LOG("0x%08x <= 0x%08x < 0x%08x",
448 Connection->NextSequenceRcv,
449 ntohl(Header->SequenceNumber),
450 Connection->NextSequenceRcv + TCP_WINDOW_SIZE
453 // Is this packet the next expected packet?
454 if( sequence_num == Connection->NextSequenceRcv )
457 // Ooh, Goodie! Add it to the recieved list
458 rv = TCP_INT_AppendRecieved(Connection,
459 (Uint8*)Header + (Header->DataOffset>>4)*4,
463 Log_Notice("TCP", "TCP_INT_AppendRecieved rv %i", rv);
466 LOG("0x%08x += %i", Connection->NextSequenceRcv, dataLen);
467 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
468 Connection->HighestSequenceRcvd += dataLen;
469 Connection->NextSequenceRcv += dataLen;
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);
478 // - Only send an ACK if we've had a burst
479 if( Connection->NextSequenceRcv > (Uint32)(TCP_DACK_THRESHOLD + Connection->LastACKSequence) )
481 TCP_INT_SendACK(Connection, "DACK Burst");
482 // - Extend TCP deferred ACK timer
483 Time_RemoveTimer(Connection->DeferredACKTimer);
485 // - Schedule the deferred ACK timer (if already scheduled, this is a NOP)
486 Time_ScheduleTimer(Connection->DeferredACKTimer, TCP_DACK_TIMEOUT);
488 TCP_INT_SendACK(Connection, "RX");
491 // Check if the packet is in window
492 else if( WrapBetween(Connection->NextSequenceRcv, sequence_num,
493 Connection->NextSequenceRcv+TCP_WINDOW_SIZE, 0xFFFFFFFF) )
495 Uint8 *dataptr = (Uint8*)Header + (Header->DataOffset>>4)*4;
496 #if CACHE_FUTURE_PACKETS_IN_BYTES
499 index = sequence_num % TCP_WINDOW_SIZE;
500 for( int i = 0; i < dataLen; i ++ )
502 Connection->FuturePacketValidBytes[index/8] |= 1 << (index%8);
503 Connection->FuturePacketData[index] = dataptr[i];
504 // Do a wrap increment
506 if(index == TCP_WINDOW_SIZE) index = 0;
509 tTCPStoredPacket *pkt, *tmp, *prev = NULL;
511 // Allocate and fill cached packet
512 pkt = malloc( sizeof(tTCPStoredPacket) + dataLen );
514 pkt->Sequence = ntohl(Header->SequenceNumber);
515 pkt->Length = dataLen;
516 memcpy(pkt->Data, dataptr, dataLen);
518 Log_Log("TCP", "We missed a packet, caching",
519 pkt->Sequence, Connection->NextSequenceRcv);
521 // No? Well, let's cache it and look at it later
522 SHORTLOCK( &Connection->lFuturePackets );
523 for(tmp = Connection->FuturePackets;
525 prev = tmp, tmp = tmp->Next)
527 if(tmp->Sequence >= pkt->Sequence) break;
530 // Add if before first, or sequences don't match
531 if( !tmp || tmp->Sequence != pkt->Sequence )
536 Connection->FuturePackets = pkt;
540 else if(pkt->Length > tmp->Length)
544 pkt->Next = tmp->Next;
549 free(pkt); // TODO: Find some way to remove this
551 SHORTREL( &Connection->lFuturePackets );
554 // Badly out of sequence packet
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");
564 // --- Remote close states
565 case TCP_ST_CLOSE_WAIT:
567 // Ignore everything, CLOSE_WAIT is terminated by the client
568 Log_Debug("TCP", "CLOSE WAIT - Ignoring packets");
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 )
576 Connection->State = TCP_ST_FINISHED; // Connection completed
577 Log_Log("TCP", "LAST-ACK to CLOSED - Connection remote closed");
578 // TODO: Destrory the TCB
582 // --- Local close States
583 case TCP_ST_FIN_WAIT1:
584 if( Header->Flags & TCP_FLAG_FIN )
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);
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 );
601 // TODO: Make sure that the packet is actually ACKing the FIN
602 if( Header->Flags & TCP_FLAG_ACK )
604 Connection->State = TCP_ST_FIN_WAIT2;
605 Log_Debug("TCP", "Conn %p closed, sent FIN ACKed", Connection);
606 VFS_MarkError(&Connection->Node, 1);
611 case TCP_ST_FIN_WAIT2:
612 if( Header->Flags & TCP_FLAG_FIN )
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);
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 );
628 // TODO: Make sure that the packet is actually ACKing the FIN
629 if( Header->Flags & TCP_FLAG_ACK )
631 Connection->State = TCP_ST_TIME_WAIT;
632 Log_Debug("TCP", "Conn %p CLOSING -> TIME WAIT", Connection);
633 VFS_MarkError(&Connection->Node, 1);
638 // --- Closed (or near closed) states) ---
639 case TCP_ST_TIME_WAIT:
640 Log_Log("TCP", "Packets on Time-Wait, ignored");
643 case TCP_ST_FINISHED:
644 Log_Log("TCP", "Packets when CLOSED, ignoring");
648 // Log_Warning("TCP", "Unhandled TCP state %i", Connection->State);
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
660 int TCP_INT_AppendRecieved(tTCPConnection *Connection, const void *Data, size_t Length)
662 Mutex_Acquire( &Connection->lRecievedPackets );
664 if(Connection->RecievedBuffer->Length + Length > Connection->RecievedBuffer->Space )
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
671 Mutex_Release( &Connection->lRecievedPackets );
675 RingBuffer_Write( Connection->RecievedBuffer, Data, Length );
677 VFS_MarkAvaliable(&Connection->Node, 1);
679 Mutex_Release( &Connection->lRecievedPackets );
684 * \brief Updates the connections recieved list from the future list
685 * \param Connection Connection structure
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
691 void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection)
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 ++ )
701 Uint8 bitfield_byte = Connection->FuturePacketValidBytes[index / 8];
702 if( (bitfield_byte & (1 << bit)) == 0 ) {
707 if( bitfield_byte == 0xFF ) {
715 if(index > TCP_WINDOW_SIZE)
716 index -= TCP_WINDOW_SIZE;
719 index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE;
721 // Write data to to the ring buffer
722 if( TCP_WINDOW_SIZE - index > length )
725 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, length );
729 int endLen = TCP_WINDOW_SIZE - index;
731 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, endLen );
732 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData, endLen - length );
735 // Mark (now saved) bytes as invalid
737 while(index % 8 && length > 0)
739 Connection->FuturePacketData[index] = 0;
740 Connection->FuturePacketValidBytes[index/8] &= ~(1 << (index%8));
742 if(index > TCP_WINDOW_SIZE)
743 index -= TCP_WINDOW_SIZE;
748 Connection->FuturePacketData[index] = 0;
749 Connection->FuturePacketValidBytes[index/8] = 0;
752 if(index > TCP_WINDOW_SIZE)
753 index -= TCP_WINDOW_SIZE;
757 Connection->FuturePacketData[index] = 0;
758 Connection->FuturePacketData[index/8] &= ~(1 << (index%8));
760 if(index > TCP_WINDOW_SIZE)
761 index -= TCP_WINDOW_SIZE;
766 tTCPStoredPacket *pkt;
769 SHORTLOCK( &Connection->lFuturePackets );
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
776 pkt = Connection->FuturePackets;
777 while(pkt && pkt->Sequence < Connection->NextSequenceRcv)
779 tTCPStoredPacket *next = pkt->Next;
784 // If there's no packets left in cache, stop looking
785 if(!pkt || pkt->Sequence > Connection->NextSequenceRcv) {
786 SHORTREL( &Connection->lFuturePackets );
790 // Delete packet from future list
791 Connection->FuturePackets = pkt->Next;
794 SHORTREL( &Connection->lFuturePackets );
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;
806 void TCP_int_SendDelayedACK(void *ConnPtr)
808 TCP_INT_SendACK(ConnPtr, "DACK Timeout");
811 void TCP_INT_SendACK(tTCPConnection *Connection, const char *Reason)
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;
831 * \fn Uint16 TCP_GetUnusedPort()
832 * \brief Gets an unused port and allocates it
834 Uint16 TCP_GetUnusedPort()
838 // Get Next outbound port
839 ret = giTCP_NextOutPort++;
840 while( gaTCP_PortBitmap[ret/32] & (1UL << (ret%32)) )
844 if(giTCP_NextOutPort == 0x10000) {
845 ret = giTCP_NextOutPort = TCP_MIN_DYNPORT;
849 // Mark the new port as used
850 gaTCP_PortBitmap[ret/32] |= 1 << (ret%32);
856 * \fn int TCP_AllocatePort(Uint16 Port)
857 * \brief Marks a port as used
859 int TCP_AllocatePort(Uint16 Port)
861 // Check if the port has already been allocated
862 if( gaTCP_PortBitmap[Port/32] & (1 << (Port%32)) )
866 gaTCP_PortBitmap[Port/32] |= 1 << (Port%32);
872 * \fn int TCP_DeallocatePort(Uint16 Port)
873 * \brief Marks a port as unused
875 int TCP_DeallocatePort(Uint16 Port)
877 // Check if the port has already been allocated
878 if( !(gaTCP_PortBitmap[Port/32] & (1 << (Port%32))) )
882 gaTCP_PortBitmap[Port/32] &= ~(1 << (Port%32));
887 tTCPConnection *TCP_int_CreateConnection(tInterface *Interface, enum eTCPConnectionState State)
889 tTCPConnection *conn = calloc( sizeof(tTCPConnection) + TCP_WINDOW_SIZE + TCP_WINDOW_SIZE/8, 1 );
892 conn->Interface = Interface;
893 conn->LocalPort = -1;
894 conn->RemotePort = -1;
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
903 conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE );
905 conn->SentBuffer = RingBuffer_Create( TCP_SEND_BUFFER_SIZE );
906 Semaphore_Init(conn->SentBufferSpace, 0, TCP_SEND_BUFFER_SIZE, "TCP SentBuffer", conn->Name);
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;
915 conn->DeferredACKTimer = Time_AllocateTimer( TCP_int_SendDelayedACK, conn);
920 tVFS_Node *TCP_Server_Init(tInterface *Interface)
924 srv = calloc( 1, sizeof(tTCPListener) );
927 Log_Warning("TCP", "malloc failed for listener (%i) bytes", sizeof(tTCPListener));
931 srv->Interface = Interface;
934 srv->Connections = NULL;
935 srv->ConnectionsTail = NULL;
936 srv->NewConnections = NULL;
938 srv->Node.Flags = VFS_FFLAG_DIRECTORY;
940 srv->Node.ImplPtr = srv;
941 srv->Node.NumACLs = 1;
942 srv->Node.ACLs = &gVFS_ACL_EveryoneRW;
943 srv->Node.Type = &gTCP_ServerNodeType;
945 SHORTLOCK(&glTCP_Listeners);
946 srv->Next = gTCP_Listeners;
947 gTCP_Listeners = srv;
948 SHORTREL(&glTCP_Listeners);
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)
959 int TCP_Server_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
961 tTCPListener *srv = Node->ImplPtr;
962 tTCPConnection *conn;
964 ENTER("pNode iPos", Node, Pos);
966 Log_Log("TCP", "Thread %i waiting for a connection", Threads_GetTID());
967 Semaphore_Wait( &srv->WaitingConnections, 1 );
969 SHORTLOCK(&srv->lConnections);
970 // Increment the new list (the current connection is still on the
972 conn = srv->NewConnections;
973 srv->NewConnections = conn->Next;
975 if( srv->NewConnections == NULL )
976 VFS_MarkAvaliable( Node, 0 );
978 SHORTREL( &srv->lConnections );
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);
985 itoa(Dest, conn->Node.ImplInt, 16, 8, '0');
986 Log_Log("TCP", "Thread %i got connection '%s'", Threads_GetTID(), Dest);
992 * \brief Gets a client connection node
993 * \param Node Server node
994 * \param Name Hexadecimal ID of the node
996 tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
998 tTCPConnection *conn;
999 tTCPListener *srv = Node->ImplPtr;
1001 int id = atoi(Name);
1003 ENTER("pNode sName", Node, Name);
1005 // Check for a non-empty name
1009 itoa(tmp, id, 16, 8, '0');
1010 if(strcmp(tmp, Name) != 0) {
1011 LOG("'%s' != '%s' (%08x)", Name, tmp, id);
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);
1021 SHORTLOCK( &srv->lConnections );
1022 for(conn = srv->Connections;
1026 LOG("conn->Node.ImplInt = %i", conn->Node.ImplInt);
1027 if(conn->Node.ImplInt == id) break;
1029 SHORTREL( &srv->lConnections );
1031 // If not found, ret NULL
1033 LOG("Connection %i not found", id);
1038 // Empty Name - Check for a new connection and if it's there, open it
1041 SHORTLOCK( &srv->lConnections );
1042 conn = srv->NewConnections;
1044 srv->NewConnections = conn->Next;
1045 VFS_MarkAvaliable( Node, srv->NewConnections != NULL );
1046 SHORTREL( &srv->lConnections );
1048 LOG("No new connections");
1055 LEAVE('p', &conn->Node);
1060 * \brief Handle IOCtl calls
1062 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data)
1064 tTCPListener *srv = Node->ImplPtr;
1068 case 4: // Get/Set Port
1069 if(!Data) // Get Port
1072 if(srv->Port) // Wait, you can't CHANGE the port
1075 if(!CheckMem(Data, sizeof(Uint16))) // Sanity check
1078 // Permissions check
1079 if(Threads_GetUID() != 0
1080 && *(Uint16*)Data != 0
1081 && *(Uint16*)Data < 1024)
1084 // TODO: Check if a port is in use
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);
1093 Log_Log("TCP", "Server %p listening on port %i", srv, srv->Port);
1100 void TCP_Server_Close(tVFS_Node *Node)
1102 free(Node->ImplPtr);
1107 * \brief Create a client node
1109 tVFS_Node *TCP_Client_Init(tInterface *Interface)
1111 tTCPConnection *conn = TCP_int_CreateConnection(Interface, TCP_ST_CLOSED);
1113 SHORTLOCK(&glTCP_OutbountCons);
1114 conn->Next = gTCP_OutbountCons;
1115 gTCP_OutbountCons = conn;
1116 SHORTREL(&glTCP_OutbountCons);
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.
1126 size_t TCP_Client_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
1128 tTCPConnection *conn = Node->ImplPtr;
1131 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
1132 LOG("conn = %p {State:%i}", conn, conn->State);
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 )
1138 Mutex_Acquire( &conn->lRecievedPackets );
1139 len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length );
1140 Mutex_Release( &conn->lRecievedPackets );
1143 VFS_MarkAvaliable(Node, 0);
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;
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 );
1170 if( len == 0 || conn->RecievedBuffer->Length == 0 ) {
1171 LOG("Marking as none avaliable (len = %i)", len);
1172 VFS_MarkAvaliable(Node, 0);
1175 // Release the lock (we don't need it any more)
1176 Mutex_Release( &conn->lRecievedPackets );
1183 * \brief Send a data packet on a connection
1185 void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, const void *Data)
1187 char buf[sizeof(tTCPHeader)+Length];
1188 tTCPHeader *packet = (void*)buf;
1190 // - Stop Delayed ACK timer (as this data packet ACKs)
1191 Time_RemoveTimer(Connection->DeferredACKTimer);
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);
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;
1203 memcpy(packet->Options, Data, Length);
1205 Log_Debug("TCP", "Send sequence 0x%08x", Connection->NextSequenceSend);
1206 #if HEXDUMP_OUTGOING
1207 Debug_HexDump("TCP_INT_SendDataPacket: Data = ", Data, Length);
1210 TCP_SendPacket( Connection, packet, Length, Data );
1212 Connection->NextSequenceSend += Length;
1216 * \brief Send some bytes on a connection
1218 size_t TCP_Client_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
1220 tTCPConnection *conn = Node->ImplPtr;
1221 size_t rem = Length;
1223 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
1226 // Debug_HexDump("TCP_Client_Write: Buffer = ",
1230 // Don't allow a write to a closed connection
1231 if( conn->State > TCP_ST_OPEN ) {
1232 VFS_MarkError(Node, 1);
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;
1253 int len = (rem < TCP_MAX_PACKET_SIZE) ? rem : TCP_MAX_PACKET_SIZE;
1256 // Wait for space in the buffer
1257 Semaphore_Signal( &Connection->SentBufferSpace, len );
1259 // Save data to buffer (and update the length read by the ammount written)
1260 len = RingBuffer_Write( &Connection->SentBuffer, Buffer, len);
1264 TCP_INT_SendDataPacket(conn, len, Buffer);
1275 * \brief Open a connection to another host using TCP
1276 * \param Conn Connection structure
1278 void TCP_StartConnection(tTCPConnection *Conn)
1280 tTCPHeader hdr = {0};
1282 Conn->State = TCP_ST_SYN_SENT;
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
1293 TCP_SendPacket( Conn, &hdr, 0, NULL );
1295 Conn->NextSequenceSend ++;
1296 Conn->State = TCP_ST_SYN_SENT;
1302 * \brief Control a client socket
1304 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
1306 tTCPConnection *conn = Node->ImplPtr;
1308 ENTER("pNode iID pData", Node, ID, Data);
1312 case 4: // Get/Set local port
1314 LEAVE_RET('i', conn->LocalPort);
1315 if(conn->State != TCP_ST_CLOSED)
1317 if(!CheckMem(Data, sizeof(Uint16)))
1320 if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
1323 conn->LocalPort = *(Uint16*)Data;
1324 LEAVE_RET('i', conn->LocalPort);
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);
1333 case 6: // Set Remote IP
1334 if( conn->State != TCP_ST_CLOSED )
1336 if( conn->Interface->Type == 4 )
1338 if(!CheckMem(Data, sizeof(tIPv4))) LEAVE_RET('i', -1);
1339 conn->RemoteIP.v4 = *(tIPv4*)Data;
1341 else if( conn->Interface->Type == 6 )
1343 if(!CheckMem(Data, sizeof(tIPv6))) LEAVE_RET('i', -1);
1344 conn->RemoteIP.v6 = *(tIPv6*)Data;
1349 if(conn->LocalPort == 0xFFFF)
1350 conn->LocalPort = TCP_GetUnusedPort();
1351 if(conn->RemotePort == -1)
1355 tTime timeout = conn->Interface->TimeoutDelay;
1357 TCP_StartConnection(conn);
1358 VFS_SelectNode(&conn->Node, VFS_SELECT_WRITE, &timeout, "TCP Connection");
1359 if( conn->State == TCP_ST_SYN_SENT )
1365 // Get recieve buffer length
1367 LEAVE_RET('i', conn->RecievedBuffer->Length);
1373 void TCP_Client_Close(tVFS_Node *Node)
1375 tTCPConnection *conn = Node->ImplPtr;
1378 ENTER("pNode", Node);
1380 ASSERT(Node->ReferenceCount != 0);
1382 if( Node->ReferenceCount > 1 ) {
1383 Node->ReferenceCount --;
1384 LOG("Dereference only");
1389 if( conn->State == TCP_ST_CLOSE_WAIT || conn->State == TCP_ST_OPEN )
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;
1396 packet.AcknowlegementNumber = 0;
1397 packet.SequenceNumber = htonl(conn->NextSequenceSend);
1398 packet.Flags = TCP_FLAG_FIN;
1400 TCP_SendPacket( conn, &packet, 0, NULL );
1403 switch( conn->State )
1406 Log_Warning("TCP", "Closing connection that was never opened");
1408 case TCP_ST_CLOSE_WAIT:
1409 conn->State = TCP_ST_LAST_ACK;
1412 conn->State = TCP_ST_FIN_WAIT1;
1413 while( conn->State == TCP_ST_FIN_WAIT1 ) Threads_Yield();
1416 Log_Warning("TCP", "Unhandled connection state %i in TCP_Client_Close",
1421 Time_RemoveTimer(conn->DeferredACKTimer);
1422 Time_FreeTimer(conn->DeferredACKTimer);
1429 * \brief Checks if a value is between two others (after taking into account wrapping)
1431 int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue)
1433 if( MaxValue < 0xFFFFFFFF )
1435 Lower %= MaxValue + 1;
1436 Value %= MaxValue + 1;
1437 Higher %= MaxValue + 1;
1440 // Simple Case, no wrap ?
1441 // Lower Value Higher
1442 // | ... + ... + ... + ... |
1444 if( Lower < Higher ) {
1445 return Lower < Value && Value < Higher;
1447 // Higher has wrapped below lower
1450 // Higher Lower Value
1451 // | ... + ... + ... + ... |
1452 if( Value > Lower ) {
1457 // Value Higher Lower
1458 // | ... + ... + ... + ... |
1459 if( Value < Higher ) {