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_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);
41 tVFS_Node *TCP_Server_Init(tInterface *Interface);
42 int TCP_Server_ReadDir(tVFS_Node *Node, int Pos, char Name[FILENAME_MAX]);
43 tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name, Uint Flags);
44 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data);
45 void TCP_Server_Close(tVFS_Node *Node);
47 tVFS_Node *TCP_Client_Init(tInterface *Interface);
48 size_t TCP_Client_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags);
49 size_t TCP_Client_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags);
50 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data);
51 void TCP_Client_Close(tVFS_Node *Node);
53 int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue);
56 tSocketFile gTCP_ServerFile = {NULL, "tcps", TCP_Server_Init};
57 tSocketFile gTCP_ClientFile = {NULL, "tcpc", TCP_Client_Init};
58 tVFS_NodeType gTCP_ServerNodeType = {
59 .TypeName = "TCP Server",
60 .ReadDir = TCP_Server_ReadDir,
61 .FindDir = TCP_Server_FindDir,
62 .IOCtl = TCP_Server_IOCtl,
63 .Close = TCP_Server_Close
65 tVFS_NodeType gTCP_ClientNodeType = {
66 .TypeName = "TCP Client/Connection",
67 .Read = TCP_Client_Read,
68 .Write = TCP_Client_Write,
69 .IOCtl = TCP_Client_IOCtl,
70 .Close = TCP_Client_Close
74 int giTCP_NumHalfopen = 0;
75 tShortSpinlock glTCP_Listeners;
76 tTCPListener *gTCP_Listeners;
77 tShortSpinlock glTCP_OutbountCons;
78 tTCPConnection *gTCP_OutbountCons;
79 Uint32 gaTCP_PortBitmap[0x800];
80 int giTCP_NextOutPort = TCP_MIN_DYNPORT;
84 * \brief Initialise the TCP Layer
86 * Registers the client and server files and the GetPacket callback
88 void TCP_Initialise(void)
90 giTCP_NextOutPort += rand()%128;
91 IPStack_AddFile(&gTCP_ServerFile);
92 IPStack_AddFile(&gTCP_ClientFile);
93 IPv4_RegisterCallback(IP4PROT_TCP, TCP_GetPacket);
94 IPv6_RegisterCallback(IP4PROT_TCP, TCP_GetPacket);
98 * \brief Sends a packet from the specified connection, calculating the checksums
99 * \param Conn Connection
100 * \param Length Length of data
101 * \param Data Packet data (cast as a TCP Header)
103 void TCP_SendPacket( tTCPConnection *Conn, tTCPHeader *Header, size_t Length, const void *Data )
105 TCP_int_SendPacket(Conn->Interface, &Conn->RemoteIP, Header, Length, Data);
108 void TCP_int_SendPacket(tInterface *Interface, const void *Dest, tTCPHeader *Header, size_t Length, const void *Data )
110 tIPStackBuffer *buffer;
112 int packlen = sizeof(*Header) + Length;
114 buffer = IPStack_Buffer_CreateBuffer(2 + IPV4_BUFFERS);
116 IPStack_Buffer_AppendSubBuffer(buffer, Length, 0, Data, NULL, NULL);
117 IPStack_Buffer_AppendSubBuffer(buffer, sizeof(*Header), 0, Header, NULL, NULL);
119 LOG("Sending %i+%i to %s:%i", sizeof(*Header), Length,
120 IPStack_PrintAddress(Interface->Type, Dest),
121 ntohs(Header->RemotePort)
124 Header->Checksum = 0;
125 checksum[1] = htons( ~IPv4_Checksum(Header, sizeof(tTCPHeader)) );
126 checksum[2] = htons( ~IPv4_Checksum(Data, Length) );
128 // TODO: Fragment packet
130 switch( Interface->Type )
133 // Get IPv4 pseudo-header checksum
136 buf[0] = ((tIPv4*)Interface->Address)->L;
137 buf[1] = ((tIPv4*)Dest)->L;
138 buf[2] = (htons(packlen)<<16) | (6<<8) | 0;
139 checksum[0] = htons( ~IPv4_Checksum(buf, sizeof(buf)) ); // Partial checksum
141 // - Combine checksums
142 Header->Checksum = htons( IPv4_Checksum(checksum, sizeof(checksum)) );
143 IPv4_SendPacket(Interface, *(tIPv4*)Dest, IP4PROT_TCP, 0, buffer);
147 // Append IPv6 Pseudo Header
150 memcpy(buf, Interface->Address, 16);
151 memcpy(&buf[4], Dest, 16);
152 buf[8] = htonl(packlen);
154 checksum[0] = htons( ~IPv4_Checksum(buf, sizeof(buf)) ); // Partial checksum
156 Header->Checksum = htons( IPv4_Checksum(checksum, sizeof(checksum)) ); // Combine the two
157 IPv6_SendPacket(Interface, *(tIPv6*)Dest, IP4PROT_TCP, buffer);
163 * \brief Handles a packet from the IP Layer
164 * \param Interface Interface the packet arrived from
165 * \param Address Pointer to the addres structure
166 * \param Length Size of packet in bytes
167 * \param Buffer Packet data
169 void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
171 tTCPHeader *hdr = Buffer;
174 Log_Log("TCP", "TCP_GetPacket: <Local>:%i from [%s]:%i, Flags = %s%s%s%s%s%s%s%s",
175 ntohs(hdr->DestPort),
176 IPStack_PrintAddress(Interface->Type, Address),
177 ntohs(hdr->SourcePort),
178 (hdr->Flags & TCP_FLAG_CWR) ? "CWR " : "",
179 (hdr->Flags & TCP_FLAG_ECE) ? "ECE " : "",
180 (hdr->Flags & TCP_FLAG_URG) ? "URG " : "",
181 (hdr->Flags & TCP_FLAG_ACK) ? "ACK " : "",
182 (hdr->Flags & TCP_FLAG_PSH) ? "PSH " : "",
183 (hdr->Flags & TCP_FLAG_RST) ? "RST " : "",
184 (hdr->Flags & TCP_FLAG_SYN) ? "SYN " : "",
185 (hdr->Flags & TCP_FLAG_FIN) ? "FIN " : ""
189 if( Length > (hdr->DataOffset >> 4)*4 )
191 LOG("SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber));
194 "TCP_GetPacket: Packet Data = ",
195 (Uint8*)hdr + (hdr->DataOffset >> 4)*4,
196 Length - (hdr->DataOffset >> 4)*4
202 for( tTCPListener *srv = gTCP_Listeners; srv; srv = srv->Next )
204 // Check if the server is active
205 if(srv->Port == 0) continue;
206 // Check the interface
207 if(srv->Interface && srv->Interface != Interface) continue;
208 // Check the destination port
209 if(srv->Port != htons(hdr->DestPort)) continue;
211 Log_Log("TCP", "TCP_GetPacket: Matches server %p", srv);
212 // Is this in an established connection?
213 for( tTCPConnection *conn = srv->Connections; conn; conn = conn->Next )
215 // Check that it is coming in on the same interface
216 if(conn->Interface != Interface) continue;
219 Log_Log("TCP", "TCP_GetPacket: conn->RemotePort(%i) == hdr->SourcePort(%i)",
220 conn->RemotePort, ntohs(hdr->SourcePort));
221 if(conn->RemotePort != ntohs(hdr->SourcePort)) continue;
224 Log_Debug("TCP", "TCP_GetPacket: conn->RemoteIP(%s)",
225 IPStack_PrintAddress(conn->Interface->Type, &conn->RemoteIP));
226 Log_Debug("TCP", " == Address(%s)",
227 IPStack_PrintAddress(conn->Interface->Type, Address));
228 if( IPStack_CompareAddress(conn->Interface->Type, &conn->RemoteIP, Address, -1) == 0 )
231 Log_Log("TCP", "TCP_GetPacket: Matches connection %p", conn);
232 // We have a response!
233 TCP_INT_HandleConnectionPacket(conn, hdr, Length);
238 Log_Log("TCP", "TCP_GetPacket: Opening Connection");
239 // Open a new connection (well, check that it's a SYN)
240 if(hdr->Flags != TCP_FLAG_SYN) {
241 Log_Log("TCP", "TCP_GetPacket: Packet is not a SYN");
245 // TODO: Check for halfopen max
247 tTCPConnection *conn = TCP_int_CreateConnection(Interface, TCP_ST_SYN_RCVD);
248 conn->LocalPort = srv->Port;
249 conn->RemotePort = ntohs(hdr->SourcePort);
251 switch(Interface->Type)
253 case 4: conn->RemoteIP.v4 = *(tIPv4*)Address; break;
254 case 6: conn->RemoteIP.v6 = *(tIPv6*)Address; break;
257 conn->NextSequenceRcv = ntohl( hdr->SequenceNumber ) + 1;
258 conn->HighestSequenceRcvd = conn->NextSequenceRcv;
259 conn->NextSequenceSend = rand();
261 conn->Node.ImplInt = srv->NextID ++;
263 // Hmm... Theoretically, this lock will never have to wait,
264 // as the interface is locked to the watching thread, and this
265 // runs in the watching thread. But, it's a good idea to have
267 // Oh, wait, there is a case where a wildcard can be used
268 // (srv->Interface == NULL) so having the lock is a good idea
269 SHORTLOCK(&srv->lConnections);
270 if( !srv->Connections )
271 srv->Connections = conn;
273 srv->ConnectionsTail->Next = conn;
274 srv->ConnectionsTail = conn;
275 if(!srv->NewConnections)
276 srv->NewConnections = conn;
277 VFS_MarkAvaliable( &srv->Node, 1 );
278 SHORTREL(&srv->lConnections);
279 Semaphore_Signal(&srv->WaitingConnections, 1);
282 hdr->Flags |= TCP_FLAG_ACK;
283 hdr->AcknowlegementNumber = htonl(conn->NextSequenceRcv);
284 hdr->SequenceNumber = htonl(conn->NextSequenceSend);
285 hdr->DestPort = hdr->SourcePort;
286 hdr->SourcePort = htons(srv->Port);
287 hdr->DataOffset = (sizeof(tTCPHeader)/4) << 4;
288 TCP_SendPacket( conn, hdr, 0, NULL );
289 conn->NextSequenceSend ++;
293 // Check Open Connections
295 for( tTCPConnection *conn = gTCP_OutbountCons; conn; conn = conn->Next )
297 // Check that it is coming in on the same interface
298 if(conn->Interface != Interface) continue;
301 if(conn->RemotePort != ntohs(hdr->SourcePort)) continue;
304 if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
306 if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
309 TCP_INT_HandleConnectionPacket(conn, hdr, Length);
314 Log_Log("TCP", "TCP_GetPacket: No Match");
315 // If not a RST, send a RST
316 if( !(hdr->Flags & TCP_FLAG_RST) )
318 tTCPHeader out_hdr = {0};
320 out_hdr.DataOffset = (sizeof(out_hdr)/4) << 4;
321 out_hdr.DestPort = hdr->SourcePort;
322 out_hdr.SourcePort = hdr->DestPort;
324 size_t data_len = Length - (hdr->DataOffset>>4)*4;
325 out_hdr.AcknowlegementNumber = htonl( ntohl(hdr->SequenceNumber) + data_len );
326 if( hdr->Flags & TCP_FLAG_ACK ) {
327 out_hdr.Flags = TCP_FLAG_RST;
328 out_hdr.SequenceNumber = hdr->AcknowlegementNumber;
331 out_hdr.Flags = TCP_FLAG_RST|TCP_FLAG_ACK;
332 out_hdr.SequenceNumber = 0;
334 TCP_int_SendPacket(Interface, Address, &out_hdr, 0, NULL);
339 * \brief Handles a packet sent to a specific connection
340 * \param Connection TCP Connection pointer
341 * \param Header TCP Packet pointer
342 * \param Length Length of the packet
344 void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Header, int Length)
349 // Silently drop once finished
350 // TODO: Check if this needs to be here
351 if( Connection->State == TCP_ST_FINISHED ) {
352 Log_Log("TCP", "Packet ignored - connection finnished");
356 // Syncronise sequence values
357 if(Header->Flags & TCP_FLAG_SYN) {
358 // TODO: What if the packet also has data?
359 if( Connection->LastACKSequence != Connection->NextSequenceRcv )
360 TCP_INT_SendACK(Connection, "SYN");
361 Connection->NextSequenceRcv = ntohl(Header->SequenceNumber);
362 // TODO: Process HighestSequenceRcvd
364 if( Connection->HighestSequenceRcvd == 0 )
365 Connection->HighestSequenceRcvd = Connection->NextSequenceRcv;
366 Connection->LastACKSequence = Connection->NextSequenceRcv;
369 // Ackowledge a sent packet
370 if(Header->Flags & TCP_FLAG_ACK) {
371 // TODO: Process an ACKed Packet
372 LOG("Conn %p, Sent packet 0x%x ACKed", Connection, Header->AcknowlegementNumber);
375 // Get length of data
376 dataLen = Length - (Header->DataOffset>>4)*4;
377 LOG("dataLen = %i", dataLen);
379 Log_Debug("TCP", "State %i, dataLen = %x", Connection->State, dataLen);
385 switch( Connection->State )
387 // Pre-init connection?
389 Log_Log("TCP", "Packets to a closed connection?!");
392 // --- Init States ---
393 // SYN sent, expecting SYN-ACK Connection Opening
394 case TCP_ST_SYN_SENT:
395 if( Header->Flags & TCP_FLAG_SYN )
397 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
398 Connection->HighestSequenceRcvd ++;
399 Connection->NextSequenceRcv ++;
401 if( Header->Flags & TCP_FLAG_ACK )
403 Log_Log("TCP", "ACKing SYN-ACK");
404 Connection->State = TCP_ST_OPEN;
405 VFS_MarkFull(&Connection->Node, 0);
409 Log_Log("TCP", "ACKing SYN");
410 Connection->State = TCP_ST_SYN_RCVD;
412 Header->DestPort = Header->SourcePort;
413 Header->SourcePort = htons(Connection->LocalPort);
414 Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
415 Header->SequenceNumber = htonl(Connection->NextSequenceSend);
416 Header->WindowSize = htons(TCP_WINDOW_SIZE);
417 Header->Flags = TCP_FLAG_ACK;
418 Header->DataOffset = (sizeof(tTCPHeader)/4) << 4;
419 TCP_SendPacket( Connection, Header, 0, NULL );
423 // SYN-ACK sent, expecting ACK
424 case TCP_ST_SYN_RCVD:
425 if( Header->Flags & TCP_FLAG_ACK )
427 // TODO: Handle max half-open limit
428 Log_Log("TCP", "Connection fully opened");
429 Connection->State = TCP_ST_OPEN;
430 VFS_MarkFull(&Connection->Node, 0);
434 // --- Established State ---
436 // - Handle State changes
438 if( Header->Flags & TCP_FLAG_FIN ) {
439 Log_Log("TCP", "Conn %p closed, recieved FIN", Connection);
440 VFS_MarkError(&Connection->Node, 1);
441 Connection->State = TCP_ST_CLOSE_WAIT;
442 // Header->Flags &= ~TCP_FLAG_FIN;
443 // CLOSE WAIT requires the client to close (or does it?)
449 // Check for an empty packet
451 if( Header->Flags == TCP_FLAG_ACK )
453 Log_Log("TCP", "ACK only packet");
456 // TODO: Is this right? (empty packet counts as one byte)
457 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
458 Connection->HighestSequenceRcvd ++;
459 Connection->NextSequenceRcv ++;
460 Log_Log("TCP", "Empty Packet, inc and ACK the current sequence number");
461 TCP_INT_SendACK(Connection, "Empty");
470 sequence_num = ntohl(Header->SequenceNumber);
472 LOG("0x%08x <= 0x%08x < 0x%08x",
473 Connection->NextSequenceRcv,
474 ntohl(Header->SequenceNumber),
475 Connection->NextSequenceRcv + TCP_WINDOW_SIZE
478 // Is this packet the next expected packet?
479 if( sequence_num == Connection->NextSequenceRcv )
482 // Ooh, Goodie! Add it to the recieved list
483 rv = TCP_INT_AppendRecieved(Connection,
484 (Uint8*)Header + (Header->DataOffset>>4)*4,
488 Log_Notice("TCP", "TCP_INT_AppendRecieved rv %i", rv);
491 LOG("0x%08x += %i", Connection->NextSequenceRcv, dataLen);
492 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
493 Connection->HighestSequenceRcvd += dataLen;
494 Connection->NextSequenceRcv += dataLen;
496 // TODO: This should be moved out of the watcher thread,
497 // so that a single lost packet on one connection doesn't cause
498 // all connections on the interface to lag.
499 // - Meh, no real issue, as the cache shouldn't be that large
500 TCP_INT_UpdateRecievedFromFuture(Connection);
503 // - Only send an ACK if we've had a burst
504 if( Connection->NextSequenceRcv > (Uint32)(TCP_DACK_THRESHOLD + Connection->LastACKSequence) )
506 TCP_INT_SendACK(Connection, "DACK Burst");
507 // - Extend TCP deferred ACK timer
508 Time_RemoveTimer(Connection->DeferredACKTimer);
510 // - Schedule the deferred ACK timer (if already scheduled, this is a NOP)
511 Time_ScheduleTimer(Connection->DeferredACKTimer, TCP_DACK_TIMEOUT);
513 TCP_INT_SendACK(Connection, "RX");
516 // Check if the packet is in window
517 else if( WrapBetween(Connection->NextSequenceRcv, sequence_num,
518 Connection->NextSequenceRcv+TCP_WINDOW_SIZE, 0xFFFFFFFF) )
520 Uint8 *dataptr = (Uint8*)Header + (Header->DataOffset>>4)*4;
521 #if CACHE_FUTURE_PACKETS_IN_BYTES
524 index = sequence_num % TCP_WINDOW_SIZE;
525 for( int i = 0; i < dataLen; i ++ )
527 Connection->FuturePacketValidBytes[index/8] |= 1 << (index%8);
528 Connection->FuturePacketData[index] = dataptr[i];
529 // Do a wrap increment
531 if(index == TCP_WINDOW_SIZE) index = 0;
534 tTCPStoredPacket *pkt, *tmp, *prev = NULL;
536 // Allocate and fill cached packet
537 pkt = malloc( sizeof(tTCPStoredPacket) + dataLen );
539 pkt->Sequence = ntohl(Header->SequenceNumber);
540 pkt->Length = dataLen;
541 memcpy(pkt->Data, dataptr, dataLen);
543 Log_Log("TCP", "We missed a packet, caching",
544 pkt->Sequence, Connection->NextSequenceRcv);
546 // No? Well, let's cache it and look at it later
547 SHORTLOCK( &Connection->lFuturePackets );
548 for(tmp = Connection->FuturePackets;
550 prev = tmp, tmp = tmp->Next)
552 if(tmp->Sequence >= pkt->Sequence) break;
555 // Add if before first, or sequences don't match
556 if( !tmp || tmp->Sequence != pkt->Sequence )
561 Connection->FuturePackets = pkt;
565 else if(pkt->Length > tmp->Length)
569 pkt->Next = tmp->Next;
574 free(pkt); // TODO: Find some way to remove this
576 SHORTREL( &Connection->lFuturePackets );
579 // Badly out of sequence packet
582 Log_Log("TCP", "Fully out of sequence packet (0x%08x not between 0x%08x and 0x%08x), dropped",
583 sequence_num, Connection->NextSequenceRcv, Connection->NextSequenceRcv+TCP_WINDOW_SIZE);
584 // Spec says we should send an empty ACK with the current state
585 TCP_INT_SendACK(Connection, "Bad Seq");
589 // --- Remote close states
590 case TCP_ST_CLOSE_WAIT:
592 // Ignore everything, CLOSE_WAIT is terminated by the client
593 Log_Debug("TCP", "CLOSE WAIT - Ignoring packets");
597 // LAST-ACK - Waiting for the ACK of FIN (from CLOSE WAIT)
598 case TCP_ST_LAST_ACK:
599 if( Header->Flags & TCP_FLAG_ACK )
601 Connection->State = TCP_ST_FINISHED; // Connection completed
602 Log_Log("TCP", "LAST-ACK to CLOSED - Connection remote closed");
603 // TODO: Destrory the TCB
607 // --- Local close States
608 case TCP_ST_FIN_WAIT1:
609 if( Header->Flags & TCP_FLAG_FIN )
611 Connection->State = TCP_ST_CLOSING;
612 Log_Debug("TCP", "Conn %p closed, sent FIN and recieved FIN", Connection);
613 VFS_MarkError(&Connection->Node, 1);
616 Header->DestPort = Header->SourcePort;
617 Header->SourcePort = htons(Connection->LocalPort);
618 Header->AcknowlegementNumber = Header->SequenceNumber;
619 Header->SequenceNumber = htonl(Connection->NextSequenceSend);
620 Header->WindowSize = htons(TCP_WINDOW_SIZE);
621 Header->Flags = TCP_FLAG_ACK;
622 TCP_SendPacket( Connection, Header, 0, NULL );
626 // TODO: Make sure that the packet is actually ACKing the FIN
627 if( Header->Flags & TCP_FLAG_ACK )
629 Connection->State = TCP_ST_FIN_WAIT2;
630 Log_Debug("TCP", "Conn %p closed, sent FIN ACKed", Connection);
631 VFS_MarkError(&Connection->Node, 1);
636 case TCP_ST_FIN_WAIT2:
637 if( Header->Flags & TCP_FLAG_FIN )
639 Connection->State = TCP_ST_TIME_WAIT;
640 Log_Debug("TCP", "FIN sent and recieved, ACKing and going into TIME WAIT %p FINWAIT-2 -> TIME WAIT", Connection);
642 Header->DestPort = Header->SourcePort;
643 Header->SourcePort = htons(Connection->LocalPort);
644 Header->AcknowlegementNumber = Header->SequenceNumber;
645 Header->SequenceNumber = htonl(Connection->NextSequenceSend);
646 Header->WindowSize = htons(TCP_WINDOW_SIZE);
647 Header->Flags = TCP_FLAG_ACK;
648 TCP_SendPacket( Connection, Header, 0, NULL );
653 // TODO: Make sure that the packet is actually ACKing the FIN
654 if( Header->Flags & TCP_FLAG_ACK )
656 Connection->State = TCP_ST_TIME_WAIT;
657 Log_Debug("TCP", "Conn %p CLOSING -> TIME WAIT", Connection);
658 VFS_MarkError(&Connection->Node, 1);
663 // --- Closed (or near closed) states) ---
664 case TCP_ST_TIME_WAIT:
665 Log_Log("TCP", "Packets on Time-Wait, ignored");
668 case TCP_ST_FINISHED:
669 Log_Log("TCP", "Packets when CLOSED, ignoring");
673 // Log_Warning("TCP", "Unhandled TCP state %i", Connection->State);
680 * \brief Appends a packet to the recieved list
681 * \param Connection Connection structure
682 * \param Data Packet contents
683 * \param Length Length of \a Data
685 int TCP_INT_AppendRecieved(tTCPConnection *Connection, const void *Data, size_t Length)
687 Mutex_Acquire( &Connection->lRecievedPackets );
689 if(Connection->RecievedBuffer->Length + Length > Connection->RecievedBuffer->Space )
691 VFS_MarkAvaliable(&Connection->Node, 1);
692 Log_Error("TCP", "Buffer filled, packet dropped (:%i) - %i + %i > %i",
693 Connection->LocalPort, Connection->RecievedBuffer->Length, Length,
694 Connection->RecievedBuffer->Space
696 Mutex_Release( &Connection->lRecievedPackets );
700 RingBuffer_Write( Connection->RecievedBuffer, Data, Length );
702 VFS_MarkAvaliable(&Connection->Node, 1);
704 Mutex_Release( &Connection->lRecievedPackets );
709 * \brief Updates the connections recieved list from the future list
710 * \param Connection Connection structure
712 * Updates the recieved packets list with packets from the future (out
713 * of order) packets list that are now able to be added in direct
716 void TCP_INT_UpdateRecievedFromFuture(tTCPConnection *Connection)
718 #if CACHE_FUTURE_PACKETS_IN_BYTES
719 // Calculate length of contiguous bytes
720 int length = Connection->HighestSequenceRcvd - Connection->NextSequenceRcv;
721 Uint32 index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE;
722 LOG("length=%i, index=%i", length, index);
723 for( int i = 0; i < length; i ++ )
726 Uint8 bitfield_byte = Connection->FuturePacketValidBytes[index / 8];
727 if( (bitfield_byte & (1 << bit)) == 0 ) {
732 if( bitfield_byte == 0xFF ) {
740 if(index > TCP_WINDOW_SIZE)
741 index -= TCP_WINDOW_SIZE;
744 index = Connection->NextSequenceRcv % TCP_WINDOW_SIZE;
746 // Write data to to the ring buffer
747 if( TCP_WINDOW_SIZE - index > length )
750 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, length );
754 int endLen = TCP_WINDOW_SIZE - index;
756 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData + index, endLen );
757 RingBuffer_Write( Connection->RecievedBuffer, Connection->FuturePacketData, endLen - length );
760 // Mark (now saved) bytes as invalid
762 while(index % 8 && length > 0)
764 Connection->FuturePacketData[index] = 0;
765 Connection->FuturePacketValidBytes[index/8] &= ~(1 << (index%8));
767 if(index > TCP_WINDOW_SIZE)
768 index -= TCP_WINDOW_SIZE;
773 Connection->FuturePacketData[index] = 0;
774 Connection->FuturePacketValidBytes[index/8] = 0;
777 if(index > TCP_WINDOW_SIZE)
778 index -= TCP_WINDOW_SIZE;
782 Connection->FuturePacketData[index] = 0;
783 Connection->FuturePacketData[index/8] &= ~(1 << (index%8));
785 if(index > TCP_WINDOW_SIZE)
786 index -= TCP_WINDOW_SIZE;
791 tTCPStoredPacket *pkt;
794 SHORTLOCK( &Connection->lFuturePackets );
796 // Clear out duplicates from cache
797 // - If a packet has just been recieved, and it is expected, then
798 // (since NextSequenceRcv = rcvd->Sequence + rcvd->Length) all
799 // packets in cache that are smaller than the next expected
801 pkt = Connection->FuturePackets;
802 while(pkt && pkt->Sequence < Connection->NextSequenceRcv)
804 tTCPStoredPacket *next = pkt->Next;
809 // If there's no packets left in cache, stop looking
810 if(!pkt || pkt->Sequence > Connection->NextSequenceRcv) {
811 SHORTREL( &Connection->lFuturePackets );
815 // Delete packet from future list
816 Connection->FuturePackets = pkt->Next;
819 SHORTREL( &Connection->lFuturePackets );
821 // Looks like we found one
822 TCP_INT_AppendRecieved(Connection, pkt->Data, pkt->Length);
823 if( Connection->HighestSequenceRcvd == Connection->NextSequenceRcv )
824 Connection->HighestSequenceRcvd += pkt->Length;
825 Connection->NextSequenceRcv += pkt->Length;
831 void TCP_int_SendDelayedACK(void *ConnPtr)
833 TCP_INT_SendACK(ConnPtr, "DACK Timeout");
836 void TCP_INT_SendACK(tTCPConnection *Connection, const char *Reason)
840 hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4;
841 hdr.DestPort = htons(Connection->RemotePort);
842 hdr.SourcePort = htons(Connection->LocalPort);
843 hdr.AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
844 hdr.SequenceNumber = htonl(Connection->NextSequenceSend);
845 hdr.WindowSize = htons(TCP_WINDOW_SIZE);
846 hdr.Flags = TCP_FLAG_ACK; // TODO: Determine if SYN is wanted too
847 hdr.Checksum = 0; // TODO: Checksum
848 hdr.UrgentPointer = 0;
849 Log_Debug("TCP", "Sending ACK for 0x%08x (%s)", Connection->NextSequenceRcv, Reason);
850 TCP_SendPacket( Connection, &hdr, 0, NULL );
851 //Connection->NextSequenceSend ++;
852 Connection->LastACKSequence = Connection->NextSequenceRcv;
856 * \fn Uint16 TCP_GetUnusedPort()
857 * \brief Gets an unused port and allocates it
859 Uint16 TCP_GetUnusedPort()
863 // Get Next outbound port
864 ret = giTCP_NextOutPort++;
865 while( gaTCP_PortBitmap[ret/32] & (1UL << (ret%32)) )
869 if(giTCP_NextOutPort == 0x10000) {
870 ret = giTCP_NextOutPort = TCP_MIN_DYNPORT;
874 // Mark the new port as used
875 gaTCP_PortBitmap[ret/32] |= 1 << (ret%32);
881 * \fn int TCP_AllocatePort(Uint16 Port)
882 * \brief Marks a port as used
884 int TCP_AllocatePort(Uint16 Port)
886 // Check if the port has already been allocated
887 if( gaTCP_PortBitmap[Port/32] & (1 << (Port%32)) )
891 gaTCP_PortBitmap[Port/32] |= 1 << (Port%32);
897 * \fn int TCP_DeallocatePort(Uint16 Port)
898 * \brief Marks a port as unused
900 int TCP_DeallocatePort(Uint16 Port)
902 // Check if the port has already been allocated
903 if( !(gaTCP_PortBitmap[Port/32] & (1 << (Port%32))) )
907 gaTCP_PortBitmap[Port/32] &= ~(1 << (Port%32));
912 tTCPConnection *TCP_int_CreateConnection(tInterface *Interface, enum eTCPConnectionState State)
914 tTCPConnection *conn = calloc( sizeof(tTCPConnection) + TCP_WINDOW_SIZE + TCP_WINDOW_SIZE/8, 1 );
917 conn->Interface = Interface;
918 conn->LocalPort = -1;
919 conn->RemotePort = -1;
921 conn->Node.ReferenceCount = 1;
922 conn->Node.ImplPtr = conn;
923 conn->Node.NumACLs = 1;
924 conn->Node.ACLs = &gVFS_ACL_EveryoneRW;
925 conn->Node.Type = &gTCP_ClientNodeType;
926 conn->Node.BufferFull = 1; // Cleared when connection opens
928 conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE );
930 conn->SentBuffer = RingBuffer_Create( TCP_SEND_BUFFER_SIZE );
931 Semaphore_Init(conn->SentBufferSpace, 0, TCP_SEND_BUFFER_SIZE, "TCP SentBuffer", conn->Name);
934 #if CACHE_FUTURE_PACKETS_IN_BYTES
935 // Future recieved data (ahead of the expected sequence number)
936 conn->FuturePacketData = (Uint8*)conn + sizeof(tTCPConnection);
937 conn->FuturePacketValidBytes = conn->FuturePacketData + TCP_WINDOW_SIZE;
940 conn->DeferredACKTimer = Time_AllocateTimer( TCP_int_SendDelayedACK, conn);
945 tVFS_Node *TCP_Server_Init(tInterface *Interface)
949 srv = calloc( 1, sizeof(tTCPListener) );
952 Log_Warning("TCP", "malloc failed for listener (%i) bytes", sizeof(tTCPListener));
956 srv->Interface = Interface;
959 srv->Connections = NULL;
960 srv->ConnectionsTail = NULL;
961 srv->NewConnections = NULL;
963 srv->Node.Flags = VFS_FFLAG_DIRECTORY;
965 srv->Node.ImplPtr = srv;
966 srv->Node.NumACLs = 1;
967 srv->Node.ACLs = &gVFS_ACL_EveryoneRW;
968 srv->Node.Type = &gTCP_ServerNodeType;
970 SHORTLOCK(&glTCP_Listeners);
971 srv->Next = gTCP_Listeners;
972 gTCP_Listeners = srv;
973 SHORTREL(&glTCP_Listeners);
979 * \brief Wait for a new connection and return the connection ID
980 * \note Blocks until a new connection is made
981 * \param Node Server node
982 * \param Pos Position (ignored)
984 int TCP_Server_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
986 tTCPListener *srv = Node->ImplPtr;
987 tTCPConnection *conn;
989 ENTER("pNode iPos", Node, Pos);
991 Log_Log("TCP", "Thread %i waiting for a connection", Threads_GetTID());
992 Semaphore_Wait( &srv->WaitingConnections, 1 );
994 SHORTLOCK(&srv->lConnections);
995 // Increment the new list (the current connection is still on the
997 conn = srv->NewConnections;
998 srv->NewConnections = conn->Next;
1000 if( srv->NewConnections == NULL )
1001 VFS_MarkAvaliable( Node, 0 );
1003 SHORTREL( &srv->lConnections );
1005 LOG("conn = %p", conn);
1006 LOG("srv->Connections = %p", srv->Connections);
1007 LOG("srv->NewConnections = %p", srv->NewConnections);
1008 LOG("srv->ConnectionsTail = %p", srv->ConnectionsTail);
1010 itoa(Dest, conn->Node.ImplInt, 16, 8, '0');
1011 Log_Log("TCP", "Thread %i got connection '%s'", Threads_GetTID(), Dest);
1017 * \brief Gets a client connection node
1018 * \param Node Server node
1019 * \param Name Hexadecimal ID of the node
1021 tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
1023 tTCPConnection *conn;
1024 tTCPListener *srv = Node->ImplPtr;
1026 int id = atoi(Name);
1028 ENTER("pNode sName", Node, Name);
1030 // Check for a non-empty name
1034 itoa(tmp, id, 16, 8, '0');
1035 if(strcmp(tmp, Name) != 0) {
1036 LOG("'%s' != '%s' (%08x)", Name, tmp, id);
1041 Log_Debug("TCP", "srv->Connections = %p", srv->Connections);
1042 Log_Debug("TCP", "srv->NewConnections = %p", srv->NewConnections);
1043 Log_Debug("TCP", "srv->ConnectionsTail = %p", srv->ConnectionsTail);
1046 SHORTLOCK( &srv->lConnections );
1047 for(conn = srv->Connections;
1051 LOG("conn->Node.ImplInt = %i", conn->Node.ImplInt);
1052 if(conn->Node.ImplInt == id) break;
1054 SHORTREL( &srv->lConnections );
1056 // If not found, ret NULL
1058 LOG("Connection %i not found", id);
1063 // Empty Name - Check for a new connection and if it's there, open it
1066 SHORTLOCK( &srv->lConnections );
1067 conn = srv->NewConnections;
1069 srv->NewConnections = conn->Next;
1070 VFS_MarkAvaliable( Node, srv->NewConnections != NULL );
1071 SHORTREL( &srv->lConnections );
1073 LOG("No new connections");
1080 LEAVE('p', &conn->Node);
1085 * \brief Handle IOCtl calls
1087 int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data)
1089 tTCPListener *srv = Node->ImplPtr;
1093 case 4: // Get/Set Port
1094 if(!Data) // Get Port
1097 if(srv->Port) // Wait, you can't CHANGE the port
1100 if(!CheckMem(Data, sizeof(Uint16))) // Sanity check
1103 // Permissions check
1104 if(Threads_GetUID() != 0
1105 && *(Uint16*)Data != 0
1106 && *(Uint16*)Data < 1024)
1109 // TODO: Check if a port is in use
1112 srv->Port = *(Uint16*)Data;
1113 if(srv->Port == 0) // Allocate a random port
1114 srv->Port = TCP_GetUnusedPort();
1115 else // Else, mark this as used
1116 TCP_AllocatePort(srv->Port);
1118 Log_Log("TCP", "Server %p listening on port %i", srv, srv->Port);
1125 void TCP_Server_Close(tVFS_Node *Node)
1127 free(Node->ImplPtr);
1132 * \brief Create a client node
1134 tVFS_Node *TCP_Client_Init(tInterface *Interface)
1136 tTCPConnection *conn = TCP_int_CreateConnection(Interface, TCP_ST_CLOSED);
1138 SHORTLOCK(&glTCP_OutbountCons);
1139 conn->Next = gTCP_OutbountCons;
1140 gTCP_OutbountCons = conn;
1141 SHORTREL(&glTCP_OutbountCons);
1147 * \brief Wait for a packet and return it
1148 * \note If \a Length is smaller than the size of the packet, the rest
1149 * of the packet's data will be discarded.
1151 size_t TCP_Client_Read(tVFS_Node *Node, off_t Offset, size_t Length, void *Buffer, Uint Flags)
1153 tTCPConnection *conn = Node->ImplPtr;
1156 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
1157 LOG("conn = %p {State:%i}", conn, conn->State);
1159 // If the connection has been closed (state > ST_OPEN) then clear
1160 // any stale data in the buffer (until it is empty (until it is empty))
1161 if( conn->State > TCP_ST_OPEN )
1163 Mutex_Acquire( &conn->lRecievedPackets );
1164 len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length );
1165 Mutex_Release( &conn->lRecievedPackets );
1168 VFS_MarkAvaliable(Node, 0);
1180 tTime *timeout = NULL;
1181 tTime timeout_zero = 0;
1182 if( Flags & VFS_IOFLAG_NOBLOCK )
1183 timeout = &timeout_zero;
1184 if( !VFS_SelectNode(Node, VFS_SELECT_READ|VFS_SELECT_ERROR, timeout, "TCP_Client_Read") ) {
1185 errno = EWOULDBLOCK;
1191 // Lock list and read as much as possible (up to `Length`)
1192 Mutex_Acquire( &conn->lRecievedPackets );
1193 len = RingBuffer_Read( Buffer, conn->RecievedBuffer, Length );
1195 if( len == 0 || conn->RecievedBuffer->Length == 0 ) {
1196 LOG("Marking as none avaliable (len = %i)", len);
1197 VFS_MarkAvaliable(Node, 0);
1200 // Release the lock (we don't need it any more)
1201 Mutex_Release( &conn->lRecievedPackets );
1208 * \brief Send a data packet on a connection
1210 void TCP_INT_SendDataPacket(tTCPConnection *Connection, size_t Length, const void *Data)
1212 char buf[sizeof(tTCPHeader)+Length];
1213 tTCPHeader *packet = (void*)buf;
1215 // - Stop Delayed ACK timer (as this data packet ACKs)
1216 Time_RemoveTimer(Connection->DeferredACKTimer);
1218 packet->SourcePort = htons(Connection->LocalPort);
1219 packet->DestPort = htons(Connection->RemotePort);
1220 packet->DataOffset = (sizeof(tTCPHeader)/4)*16;
1221 packet->WindowSize = htons(TCP_WINDOW_SIZE);
1223 packet->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
1224 packet->SequenceNumber = htonl(Connection->NextSequenceSend);
1225 packet->Flags = TCP_FLAG_PSH|TCP_FLAG_ACK; // Hey, ACK if you can!
1226 packet->UrgentPointer = 0;
1228 memcpy(packet->Options, Data, Length);
1230 Log_Debug("TCP", "Send sequence 0x%08x", Connection->NextSequenceSend);
1231 #if HEXDUMP_OUTGOING
1232 Debug_HexDump("TCP_INT_SendDataPacket: Data = ", Data, Length);
1235 TCP_SendPacket( Connection, packet, Length, Data );
1237 Connection->NextSequenceSend += Length;
1241 * \brief Send some bytes on a connection
1243 size_t TCP_Client_Write(tVFS_Node *Node, off_t Offset, size_t Length, const void *Buffer, Uint Flags)
1245 tTCPConnection *conn = Node->ImplPtr;
1246 size_t rem = Length;
1248 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
1251 // Debug_HexDump("TCP_Client_Write: Buffer = ",
1255 // Don't allow a write to a closed connection
1256 if( conn->State > TCP_ST_OPEN ) {
1257 VFS_MarkError(Node, 1);
1265 tTime *timeout = NULL;
1266 tTime timeout_zero = 0;
1267 if( Flags & VFS_IOFLAG_NOBLOCK )
1268 timeout = &timeout_zero;
1269 if( !VFS_SelectNode(Node, VFS_SELECT_WRITE|VFS_SELECT_ERROR, timeout, "TCP_Client_Write") ) {
1270 errno = EWOULDBLOCK;
1278 int len = (rem < TCP_MAX_PACKET_SIZE) ? rem : TCP_MAX_PACKET_SIZE;
1281 // Wait for space in the buffer
1282 Semaphore_Signal( &Connection->SentBufferSpace, len );
1284 // Save data to buffer (and update the length read by the ammount written)
1285 len = RingBuffer_Write( &Connection->SentBuffer, Buffer, len);
1289 TCP_INT_SendDataPacket(conn, len, Buffer);
1300 * \brief Open a connection to another host using TCP
1301 * \param Conn Connection structure
1303 void TCP_StartConnection(tTCPConnection *Conn)
1305 tTCPHeader hdr = {0};
1307 Conn->State = TCP_ST_SYN_SENT;
1309 hdr.SourcePort = htons(Conn->LocalPort);
1310 hdr.DestPort = htons(Conn->RemotePort);
1311 Conn->NextSequenceSend = rand();
1312 hdr.SequenceNumber = htonl(Conn->NextSequenceSend);
1313 hdr.DataOffset = (sizeof(tTCPHeader)/4) << 4;
1314 hdr.Flags = TCP_FLAG_SYN;
1315 hdr.WindowSize = htons(TCP_WINDOW_SIZE); // Max
1316 hdr.Checksum = 0; // TODO
1318 TCP_SendPacket( Conn, &hdr, 0, NULL );
1320 Conn->NextSequenceSend ++;
1321 Conn->State = TCP_ST_SYN_SENT;
1327 * \brief Control a client socket
1329 int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
1331 tTCPConnection *conn = Node->ImplPtr;
1333 ENTER("pNode iID pData", Node, ID, Data);
1337 case 4: // Get/Set local port
1339 LEAVE_RET('i', conn->LocalPort);
1340 if(conn->State != TCP_ST_CLOSED)
1342 if(!CheckMem(Data, sizeof(Uint16)))
1345 if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
1348 conn->LocalPort = *(Uint16*)Data;
1349 LEAVE_RET('i', conn->LocalPort);
1351 case 5: // Get/Set remote port
1352 if(!Data) LEAVE_RET('i', conn->RemotePort);
1353 if(conn->State != TCP_ST_CLOSED) LEAVE_RET('i', -1);
1354 if(!CheckMem(Data, sizeof(Uint16))) LEAVE_RET('i', -1);
1355 conn->RemotePort = *(Uint16*)Data;
1356 LEAVE_RET('i', conn->RemotePort);
1358 case 6: // Set Remote IP
1359 if( conn->State != TCP_ST_CLOSED )
1361 if( conn->Interface->Type == 4 )
1363 if(!CheckMem(Data, sizeof(tIPv4))) LEAVE_RET('i', -1);
1364 conn->RemoteIP.v4 = *(tIPv4*)Data;
1366 else if( conn->Interface->Type == 6 )
1368 if(!CheckMem(Data, sizeof(tIPv6))) LEAVE_RET('i', -1);
1369 conn->RemoteIP.v6 = *(tIPv6*)Data;
1374 if(conn->LocalPort == 0xFFFF)
1375 conn->LocalPort = TCP_GetUnusedPort();
1376 if(conn->RemotePort == -1)
1380 tTime timeout = conn->Interface->TimeoutDelay;
1382 TCP_StartConnection(conn);
1383 VFS_SelectNode(&conn->Node, VFS_SELECT_WRITE, &timeout, "TCP Connection");
1384 if( conn->State == TCP_ST_SYN_SENT )
1390 // Get recieve buffer length
1392 LEAVE_RET('i', conn->RecievedBuffer->Length);
1398 void TCP_Client_Close(tVFS_Node *Node)
1400 tTCPConnection *conn = Node->ImplPtr;
1403 ENTER("pNode", Node);
1405 ASSERT(Node->ReferenceCount != 0);
1407 if( Node->ReferenceCount > 1 ) {
1408 Node->ReferenceCount --;
1409 LOG("Dereference only");
1414 if( conn->State == TCP_ST_CLOSE_WAIT || conn->State == TCP_ST_OPEN )
1416 packet.SourcePort = htons(conn->LocalPort);
1417 packet.DestPort = htons(conn->RemotePort);
1418 packet.DataOffset = (sizeof(tTCPHeader)/4)*16;
1419 packet.WindowSize = TCP_WINDOW_SIZE;
1421 packet.AcknowlegementNumber = 0;
1422 packet.SequenceNumber = htonl(conn->NextSequenceSend);
1423 packet.Flags = TCP_FLAG_FIN;
1425 TCP_SendPacket( conn, &packet, 0, NULL );
1428 switch( conn->State )
1431 Log_Warning("TCP", "Closing connection that was never opened");
1433 case TCP_ST_CLOSE_WAIT:
1434 conn->State = TCP_ST_LAST_ACK;
1437 conn->State = TCP_ST_FIN_WAIT1;
1438 while( conn->State == TCP_ST_FIN_WAIT1 ) Threads_Yield();
1441 Log_Warning("TCP", "Unhandled connection state %i in TCP_Client_Close",
1446 Time_RemoveTimer(conn->DeferredACKTimer);
1447 Time_FreeTimer(conn->DeferredACKTimer);
1454 * \brief Checks if a value is between two others (after taking into account wrapping)
1456 int WrapBetween(Uint32 Lower, Uint32 Value, Uint32 Higher, Uint32 MaxValue)
1458 if( MaxValue < 0xFFFFFFFF )
1460 Lower %= MaxValue + 1;
1461 Value %= MaxValue + 1;
1462 Higher %= MaxValue + 1;
1465 // Simple Case, no wrap ?
1466 // Lower Value Higher
1467 // | ... + ... + ... + ... |
1469 if( Lower < Higher ) {
1470 return Lower < Value && Value < Higher;
1472 // Higher has wrapped below lower
1475 // Higher Lower Value
1476 // | ... + ... + ... + ... |
1477 if( Value > Lower ) {
1482 // Value Higher Lower
1483 // | ... + ... + ... + ... |
1484 if( Value < Higher ) {