8 #define TCP_MIN_DYNPORT 0x1000
11 void TCP_Initialise();
12 void *TCP_Open(tInterface *Interface, Uint16 LocalPort, void *Address, Uint16 Port);
13 void TCP_GetPacket(tInterface *Interface, int Length, void *Buffer);
14 Uint16 TCP_GetUnusedPort();
15 int TCP_AllocatePort(Uint16 Port);
16 int TCP_DeallocatePort(Uint16 Port);
19 int giTCP_NumHalfopen = 0;
20 tTCPListener *gTCP_Listeners;
21 tTCPConnection *gTCP_OutbountCons;
22 Uint32 gaTCP_PortBitmap[0x800];
23 int giTCP_NextOutPort = TCP_MIN_DYNPORT;
27 * \fn void TCP_Initialise()
28 * \brief Initialise the TCP Layer
36 * \fn void *TCP_Open(tInterface *Interface, Uint16 LocalPort, void *Address, Uint16 Port)
37 * \brief Open a connection to another host using TCP
39 void *TCP_Open(tInterface *Interface, Uint16 LocalPort, void *Address, Uint16 Port)
41 tTCPConnection *ret = malloc( sizeof(tTCPConnection) );
43 ret->State = TCP_ST_CLOSED;
45 ret->LocalPort = TCP_GetUnusedPort();
47 ret->LocalPort = LocalPort;
48 ret->RemotePort = Port;
50 ret->LocalInterface = Interface;
52 if(Interface->Type == 6)
53 ret->RemoteIP.v6 = *(tIPv6*)Address;
55 ret->RemoteIP.v4 = *(tIPv4*)Address;
63 * \fn void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
64 * \brief Handles a packet from the IP Layer
66 void TCP_GetPacket(tInterface *Interface, void *Address, int Length, void *Buffer)
68 tTCPHeader *hdr = Buffer;
74 for( srv = gTCP_Listeners; srv; srv = srv->Next )
76 // Check the interface
77 if(srv->Interface && srv->Interface != Interface) continue;
78 // Check the destination port
79 if(srv->Port != hdr->DestPort) continue;
81 // Is this in an established connection?
82 for( conn = srv->Connections; conn; conn = conn->Next )
84 // Check that it is coming in on the same interface
85 if(conn->Interface != Interface) continue;
88 if(conn->RemotePort != hdr->SourcePort) continue;
91 if(conn->Interface->Type == 6 && !IP6_EQU(conn->RemoteIP.v6, *(tIPv6*)Address))
93 if(conn->Interface->Type == 4 && !IP4_EQU(conn->RemoteIP.v4, *(tIPv4*)Address))
96 // We have a response!
102 // Open a new connection
110 // Check Open Connections
112 for( conn = gTCP_OutbountCons; conn; conn = conn->Next )
120 * \fn Uint16 TCP_GetUnusedPort()
121 * \brief Gets an unused port and allocates it
123 Uint16 TCP_GetUnusedPort()
127 // Get Next outbound port
128 ret = giTCP_NextOutPort++;
129 while( gaTCP_PortBitmap[ret/32] & (1 << (ret%32)) )
133 if(giTCP_NextOutPort == 0x10000) {
134 ret = giTCP_NextOutPort = TCP_MIN_DYNPORT;
138 // Mark the new port as used
139 gaTCP_PortBitmap[ret/32] |= 1 << (ret%32);
145 * \fn int TCP_AllocatePort(Uint16 Port)
146 * \brief Marks a port as used
148 int TCP_AllocatePort(Uint16 Port)
150 // Check if the port has already been allocated
151 if( gaTCP_PortBitmap[Port/32] & (1 << (Port%32)) )
155 gaTCP_PortBitmap[Port/32] |= 1 << (Port%32);
161 * \fn int TCP_DeallocatePort(Uint16 Port)
162 * \brief Marks a port as unused
164 int TCP_DeallocatePort(Uint16 Port)
166 // Check if the port has already been allocated
167 if( !(gaTCP_PortBitmap[Port/32] & (1 << (Port%32))) )
171 gaTCP_PortBitmap[Port/32] &= ~(1 << (Port%32));