#include "ipv4.h"
#include "tcp.h"
+#define USE_SELECT 1
+
#define TCP_MIN_DYNPORT 0xC000
#define TCP_MAX_HALFOPEN 1024 // Should be enough
// --- Server
tVFS_Node *TCP_Server_Init(tInterface *Interface);
char *TCP_Server_ReadDir(tVFS_Node *Node, int Pos);
-tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, char *Name);
+tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name);
int TCP_Server_IOCtl(tVFS_Node *Node, int ID, void *Data);
void TCP_Server_Close(tVFS_Node *Node);
// --- Client
// === GLOBALS ===
int giTCP_NumHalfopen = 0;
-tSpinlock glTCP_Listeners;
+tShortSpinlock glTCP_Listeners;
tTCPListener *gTCP_Listeners;
-tSpinlock glTCP_OutbountCons;
+tShortSpinlock glTCP_OutbountCons;
tTCPConnection *gTCP_OutbountCons;
Uint32 gaTCP_PortBitmap[0x800];
int giTCP_NextOutPort = TCP_MIN_DYNPORT;
case 4: // Append IPv4 Pseudo Header
buflen = 4 + 4 + 4 + ((Length+1)&~1);
buf = malloc( buflen );
- buf[0] = Conn->Interface->IP4.Address.L;
+ buf[0] = ((tIPv4*)Conn->Interface->Address)->L;
buf[1] = Conn->RemoteIP.v4.L;
buf[2] = (htons(Length)<<16) | (6<<8) | 0;
Data->Checksum = 0;
memcpy( &buf[3], Data, Length );
- Data->Checksum = IPv4_Checksum( buf, buflen );
+ Data->Checksum = htons( IPv4_Checksum( buf, buflen ) );
free(buf);
IPv4_SendPacket(Conn->Interface, Conn->RemoteIP.v4, IP4PROT_TCP, 0, Length, Data);
break;
tTCPListener *srv;
tTCPConnection *conn;
- Log_Log("TCP", "SourcePort = %i, DestPort = %i",
+ Log_Log("TCP", "TCP_GetPacket: SourcePort = %i, DestPort = %i",
ntohs(hdr->SourcePort), ntohs(hdr->DestPort));
/*
- Log_Log("TCP", "SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber));
- Log_Log("TCP", "AcknowlegementNumber = 0x%x", ntohl(hdr->AcknowlegementNumber));
- Log_Log("TCP", "DataOffset = %i", hdr->DataOffset >> 4);
- Log_Log("TCP", "Flags = {");
- Log_Log("TCP", " CWR = %B, ECE = %B",
+ Log_Log("TCP", "TCP_GetPacket: SequenceNumber = 0x%x", ntohl(hdr->SequenceNumber));
+ Log_Log("TCP", "TCP_GetPacket: AcknowlegementNumber = 0x%x", ntohl(hdr->AcknowlegementNumber));
+ Log_Log("TCP", "TCP_GetPacket: DataOffset = %i", hdr->DataOffset >> 4);
+ Log_Log("TCP", "TCP_GetPacket: Flags = {");
+ Log_Log("TCP", "TCP_GetPacket: CWR = %B, ECE = %B",
!!(hdr->Flags & TCP_FLAG_CWR), !!(hdr->Flags & TCP_FLAG_ECE));
- Log_Log("TCP", " URG = %B, ACK = %B",
+ Log_Log("TCP", "TCP_GetPacket: URG = %B, ACK = %B",
!!(hdr->Flags & TCP_FLAG_URG), !!(hdr->Flags & TCP_FLAG_ACK));
- Log_Log("TCP", " PSH = %B, RST = %B",
+ Log_Log("TCP", "TCP_GetPacket: PSH = %B, RST = %B",
!!(hdr->Flags & TCP_FLAG_PSH), !!(hdr->Flags & TCP_FLAG_RST));
- Log_Log("TCP", " SYN = %B, FIN = %B",
+ Log_Log("TCP", "TCP_GetPacket: SYN = %B, FIN = %B",
!!(hdr->Flags & TCP_FLAG_SYN), !!(hdr->Flags & TCP_FLAG_FIN));
- Log_Log("TCP", "}");
- Log_Log("TCP", "WindowSize = %i", htons(hdr->WindowSize));
- Log_Log("TCP", "Checksum = 0x%x", htons(hdr->Checksum));
- Log_Log("TCP", "UrgentPointer = 0x%x", htons(hdr->UrgentPointer));
+ Log_Log("TCP", "TCP_GetPacket: }");
+ Log_Log("TCP", "TCP_GetPacket: WindowSize = %i", htons(hdr->WindowSize));
+ Log_Log("TCP", "TCP_GetPacket: Checksum = 0x%x", htons(hdr->Checksum));
+ Log_Log("TCP", "TCP_GetPacket: UrgentPointer = 0x%x", htons(hdr->UrgentPointer));
*/
- Log_Log("TCP", "Flags = %s%s%s%s%s%s",
+ Log_Log("TCP", "TCP_GetPacket: Flags = %s%s%s%s%s%s",
(hdr->Flags & TCP_FLAG_CWR) ? "CWR " : "",
(hdr->Flags & TCP_FLAG_ECE) ? "ECE " : "",
(hdr->Flags & TCP_FLAG_URG) ? "URG " : "",
// Check the destination port
if(srv->Port != htons(hdr->DestPort)) continue;
- Log_Log("TCP", "Matches server %p", srv);
+ Log_Log("TCP", "TCP_GetPacket: Matches server %p", srv);
// Is this in an established connection?
for( conn = srv->Connections; conn; conn = conn->Next )
{
- Log_Log("TCP", "conn->Interface(%p) == Interface(%p)",
+ Log_Log("TCP", "TCP_GetPacket: conn->Interface(%p) == Interface(%p)",
conn->Interface, Interface);
// Check that it is coming in on the same interface
if(conn->Interface != Interface) continue;
// Check Source Port
- Log_Log("TCP", "conn->RemotePort(%i) == hdr->SourcePort(%i)",
+ Log_Log("TCP", "TCP_GetPacket: conn->RemotePort(%i) == hdr->SourcePort(%i)",
conn->RemotePort, ntohs(hdr->SourcePort));
if(conn->RemotePort != ntohs(hdr->SourcePort)) continue;
if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
continue;
- Log_Log("TCP", "Matches connection %p", conn);
+ Log_Log("TCP", "TCP_GetPacket: Matches connection %p", conn);
// We have a response!
TCP_INT_HandleConnectionPacket(conn, hdr, Length);
return;
}
- Log_Log("TCP", "Opening Connection");
+ Log_Log("TCP", "TCP_GetPacket: Opening Connection");
// Open a new connection (well, check that it's a SYN)
if(hdr->Flags != TCP_FLAG_SYN) {
- Log_Log("TCP", "Packet is not a SYN");
+ Log_Log("TCP", "TCP_GetPacket: Packet is not a SYN");
return ;
}
// it, just in case
// Oh, wait, there is a case where a wildcard can be used
// (srv->Interface == NULL) so having the lock is a good idea
- LOCK(&srv->lConnections);
+ SHORTLOCK(&srv->lConnections);
if( !srv->Connections )
srv->Connections = conn;
else
srv->ConnectionsTail = conn;
if(!srv->NewConnections)
srv->NewConnections = conn;
- RELEASE(&srv->lConnections);
+ SHORTREL(&srv->lConnections);
// Send the SYN ACK
hdr->Flags |= TCP_FLAG_ACK;
}
}
- Log_Log("TCP", "No Match");
+ Log_Log("TCP", "TCP_GetPacket: No Match");
}
/**
tTCPStoredPacket *pkt;
int dataLen;
+ // Syncronise sequence values
if(Header->Flags & TCP_FLAG_SYN) {
Connection->NextSequenceRcv = ntohl(Header->SequenceNumber) + 1;
}
+ // Handle a server replying to our initial SYN
if( Connection->State == TCP_ST_SYN_SENT )
{
- if( (Header->Flags & (TCP_FLAG_SYN|TCP_FLAG_ACK)) == (TCP_FLAG_SYN|TCP_FLAG_ACK) ) {
-
+ if( (Header->Flags & (TCP_FLAG_SYN|TCP_FLAG_ACK)) == (TCP_FLAG_SYN|TCP_FLAG_ACK) )
+ {
Header->DestPort = Header->SourcePort;
Header->SourcePort = htons(Connection->LocalPort);
Header->AcknowlegementNumber = htonl(Connection->NextSequenceRcv);
}
}
+ // Handle a client confirming the connection
+ if( Connection->State == TCP_ST_HALFOPEN && (Header->Flags & TCP_FLAG_ACK) )
+ {
+ Connection->State = TCP_ST_OPEN;
+ Log_Log("TCP", "Connection fully opened");
+ }
+
// Get length of data
dataLen = Length - (Header->DataOffset>>4)*4;
Log_Log("TCP", "HandleConnectionPacket - dataLen = %i", dataLen);
if(Header->Flags & TCP_FLAG_ACK) {
// TODO: Process an ACKed Packet
Log_Log("TCP", "Conn %p, Packet 0x%x ACKed", Connection, Header->AcknowlegementNumber);
+
+ // HACK
+ // TODO: Make sure that the packet is actually ACKing the FIN
+ if( Connection->State == TCP_ST_FIN_SENT ) {
+ Connection->State = TCP_ST_FINISHED;
+ return ;
+ }
}
// TODO: Check what to do here
}
}
+ // Check for an empty packet
if(dataLen == 0) {
Log_Log("TCP", "Empty Packet");
return ;
pkt->Sequence, Connection->NextSequenceRcv);
// No? Well, let's cache it and look at it later
- LOCK( &Connection->lFuturePackets );
+ SHORTLOCK( &Connection->lFuturePackets );
for(tmp = Connection->FuturePackets;
tmp;
prev = tmp, tmp = tmp->Next)
else
Connection->FuturePackets = pkt;
pkt->Next = tmp;
- RELEASE( &Connection->lFuturePackets );
+ SHORTREL( &Connection->lFuturePackets );
}
else
{
*/
void TCP_INT_AppendRecieved(tTCPConnection *Connection, tTCPStoredPacket *Pkt)
{
- LOCK( &Connection->lRecievedPackets );
+ Mutex_Acquire( &Connection->lRecievedPackets );
if(Connection->RecievedBuffer->Length + Pkt->Length > Connection->RecievedBuffer->Space )
{
Log_Error("TCP", "Buffer filled, packet dropped (%s)",
}
RingBuffer_Write( Connection->RecievedBuffer, Pkt->Data, Pkt->Length );
+
+ #if USE_SELECT
+ VFS_MarkAvaliable(&Connection->Node, 1);
+ #endif
- RELEASE( &Connection->lRecievedPackets );
+ Mutex_Release( &Connection->lRecievedPackets );
}
/**
{
prev = NULL;
// Look for the next expected packet in the cache.
- LOCK( &Connection->lFuturePackets );
+ SHORTLOCK( &Connection->lFuturePackets );
for(pkt = Connection->FuturePackets;
pkt && pkt->Sequence < Connection->NextSequenceRcv;
prev = pkt, pkt = pkt->Next);
// If we can't find the expected next packet, stop looking
if(!pkt || pkt->Sequence > Connection->NextSequenceRcv) {
- RELEASE( &Connection->lFuturePackets );
+ SHORTREL( &Connection->lFuturePackets );
return;
}
Connection->FuturePackets = pkt->Next;
// Release list
- RELEASE( &Connection->lFuturePackets );
+ SHORTREL( &Connection->lFuturePackets );
// Looks like we found one
TCP_INT_AppendRecieved(Connection, pkt);
srv->Node.IOCtl = TCP_Server_IOCtl;
srv->Node.Close = TCP_Server_Close;
- LOCK(&glTCP_Listeners);
+ SHORTLOCK(&glTCP_Listeners);
srv->Next = gTCP_Listeners;
gTCP_Listeners = srv;
- RELEASE(&glTCP_Listeners);
+ SHORTREL(&glTCP_Listeners);
return &srv->Node;
}
Log_Log("TCP", "Thread %i waiting for a connection", Threads_GetTID());
for(;;)
{
- LOCK( &srv->lConnections );
+ SHORTLOCK( &srv->lConnections );
if( srv->NewConnections != NULL ) break;
- RELEASE( &srv->lConnections );
- Threads_Yield();
+ SHORTREL( &srv->lConnections );
+ Threads_Yield(); // TODO: Sleep until poked
continue;
}
conn = srv->NewConnections;
srv->NewConnections = conn->Next;
+ SHORTREL( &srv->lConnections );
+
LOG("conn = %p", conn);
LOG("srv->Connections = %p", srv->Connections);
LOG("srv->NewConnections = %p", srv->NewConnections);
LOG("srv->ConnectionsTail = %p", srv->ConnectionsTail);
-
- RELEASE( &srv->lConnections );
ret = malloc(9);
itoa(ret, conn->Node.ImplInt, 16, 8, '0');
* \param Node Server node
* \param Name Hexadecimal ID of the node
*/
-tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, char *Name)
+tVFS_Node *TCP_Server_FindDir(tVFS_Node *Node, const char *Name)
{
tTCPConnection *conn;
tTCPListener *srv = Node->ImplPtr;
Log_Debug("TCP", "srv->ConnectionsTail = %p", srv->ConnectionsTail);
// Search
- LOCK( &srv->lConnections );
+ SHORTLOCK( &srv->lConnections );
for(conn = srv->Connections;
conn;
conn = conn->Next)
LOG("conn->Node.ImplInt = %i", conn->Node.ImplInt);
if(conn->Node.ImplInt == id) break;
}
- RELEASE( &srv->lConnections );
+ SHORTREL( &srv->lConnections );
// If not found, ret NULL
if(!conn) {
conn->RecievedBuffer = RingBuffer_Create( TCP_RECIEVE_BUFFER_SIZE );
- LOCK(&glTCP_OutbountCons);
+ SHORTLOCK(&glTCP_OutbountCons);
conn->Next = gTCP_OutbountCons;
gTCP_OutbountCons = conn;
- RELEASE(&glTCP_OutbountCons);
+ SHORTREL(&glTCP_OutbountCons);
return &conn->Node;
}
// Poll packets
for(;;)
{
- // Lock list and check if there is a packet
- LOCK( &conn->lRecievedPackets );
- if( conn->RecievedBuffer->Length == 0 ) {
- // If not, release the lock, yield and try again
- RELEASE( &conn->lRecievedPackets );
- Threads_Yield();
- continue;
- }
+ // Wait
+ VFS_SelectNode(Node, VFS_SELECT_READ, NULL);
+ // Lock list and read
+ Mutex_Acquire( &conn->lRecievedPackets );
// Attempt to read all `Length` bytes
len = RingBuffer_Read( destbuf, conn->RecievedBuffer, Length );
+ if( len == 0 || conn->RecievedBuffer->Length == 0 ) {
+ LOG("Marking as none avaliable (len = %i)\n", len);
+ VFS_MarkAvaliable(Node, 0);
+ }
+
// Release the lock (we don't need it any more)
- RELEASE( &conn->lRecievedPackets );
+ Mutex_Release( &conn->lRecievedPackets );
LEAVE('i', len);
return len;
int TCP_Client_IOCtl(tVFS_Node *Node, int ID, void *Data)
{
tTCPConnection *conn = Node->ImplPtr;
+
+ ENTER("pNode iID pData", Node, ID, Data);
switch(ID)
{
case 4: // Get/Set local port
if(!Data)
- return conn->LocalPort;
+ LEAVE_RET('i', conn->LocalPort);
if(conn->State != TCP_ST_CLOSED)
- return -1;
+ LEAVE_RET('i', -1);
if(!CheckMem(Data, sizeof(Uint16)))
- return -1;
+ LEAVE_RET('i', -1);
if(Threads_GetUID() != 0 && *(Uint16*)Data < 1024)
- return -1;
+ LEAVE_RET('i', -1);
conn->LocalPort = *(Uint16*)Data;
- return 0;
+ LEAVE_RET('i', conn->LocalPort);
case 5: // Get/Set remote port
- if(!Data) return conn->RemotePort;
- if(conn->State != TCP_ST_CLOSED) return -1;
- if(!CheckMem(Data, sizeof(Uint16))) return -1;
+ if(!Data) LEAVE_RET('i', conn->RemotePort);
+ if(conn->State != TCP_ST_CLOSED) LEAVE_RET('i', -1);
+ if(!CheckMem(Data, sizeof(Uint16))) LEAVE_RET('i', -1);
conn->RemotePort = *(Uint16*)Data;
- return 0;
+ LEAVE_RET('i', conn->RemotePort);
case 6: // Set Remote IP
if( conn->State != TCP_ST_CLOSED )
- return -1;
+ LEAVE_RET('i', -1);
if( conn->Interface->Type == 4 )
{
- if(!CheckMem(Data, sizeof(tIPv4))) return -1;
+ if(!CheckMem(Data, sizeof(tIPv4))) LEAVE_RET('i', -1);
conn->RemoteIP.v4 = *(tIPv4*)Data;
}
else if( conn->Interface->Type == 6 )
{
- if(!CheckMem(Data, sizeof(tIPv6))) return -1;
+ if(!CheckMem(Data, sizeof(tIPv6))) LEAVE_RET('i', -1);
conn->RemoteIP.v6 = *(tIPv6*)Data;
}
- return 0;
+ LEAVE_RET('i', 0);
case 7: // Connect
if(conn->LocalPort == 0xFFFF)
conn->LocalPort = TCP_GetUnusedPort();
if(conn->RemotePort == -1)
- return 0;
+ LEAVE_RET('i', 0);
TCP_StartConnection(conn);
- return 1;
+ LEAVE_RET('i', 1);
+
+ // Get recieve buffer length
+ case 8:
+ LEAVE_RET('i', conn->RecievedBuffer->Length);
}
return 0;
tTCPConnection *conn = Node->ImplPtr;
tTCPHeader packet;
+ ENTER("pNode", Node);
+
packet.SourcePort = htons(conn->LocalPort);
packet.DestPort = htons(conn->RemotePort);
packet.DataOffset = (sizeof(tTCPHeader)/4)*16;
while( conn->State == TCP_ST_FIN_SENT ) Threads_Yield();
free(conn);
+
+ LEAVE('-');
}