int max = -1;
FD_ZERO(&rfd);
+ FD_ZERO(&wfd);
LOG("gpCmdline_TCPEchoServer = %p", gpCmdline_TCPEchoServer);
if(gpCmdline_TCPEchoServer)
max = MAX(max, NetTest_TCPServer_FillSelect(gpCmdline_TCPEchoServer, &rfd));
- memcpy(&wfd, &rfd, sizeof(rfd));
+ //memcpy(&wfd, &rfd, sizeof(rfd));
memcpy(&efd, &rfd, sizeof(rfd));
LOG("max = %i", max);
- int rv = VFS_Select(max+1, &rfd, &wfd, &efd, NULL, THREAD_EVENT_USER1, true);
+ int rv = VFS_Select(max+1, &rfd, &wfd, &efd, NULL, THREAD_EVENT_USER1, false);
LOG("rv = %i", rv);
if(gpCmdline_TCPEchoServer)
* tcpserver.c
* - TCP Client tester
*/
+#define DEBUG 1
#include <vfs.h>
#include <vfs_ext.h>
#include <nettest.h>
int NetTest_TCPServer_FillSelect(tNetTest_TCPServer *Srv, fd_set *fds)
{
- ASSERT(Srv->ServerFD >= 0);
int max = -1;
- if( Srv->nClients == MAX_CLIENTS ) {
+ if( Srv->nClients < MAX_CLIENTS ) {
max = Srv->ServerFD;
FD_SET(Srv->ServerFD, fds);
}
void NetTest_TCPServer_HandleSelect(tNetTest_TCPServer *Srv, const fd_set *rfds, const fd_set *wfds, const fd_set *efds)
{
+ LOG("Srv=%p", Srv);
if( FD_ISSET(Srv->ServerFD, rfds) )
{
// New connection!
ASSERT(Srv->nClients != MAX_CLIENTS);
struct sClient *client = &Srv->Clients[Srv->nClients++];
+ LOG("Child?");
client->FD = VFS_OpenChild(Srv->ServerFD, "", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
+ LOG("client->FD = %i", client->FD);
+ }
+ if( FD_ISSET(Srv->ServerFD, efds) )
+ {
+ LOG("Oops, error on server");
+ VFS_Close(Srv->ServerFD);
+ Srv->ServerFD = -1;
}
for( int i = 0; i < Srv->nClients; i ++ )
if( FD_ISSET(client->FD, rfds) )
{
// RX'd data on client
+ // TODO: Do something other than echo back
+ char buf[1024];
+ size_t len = VFS_Read(client->FD, sizeof(buf), buf);
+ Debug_HexDump("TCP Srv Rx", buf, len);
+ VFS_Write(client->FD, len, buf);
}
if( FD_ISSET(client->FD, efds) )
extern void IP_Send(int IfNum, int AF, const void *Src, const void *Dst, uint8_t proto,
int BufCount, size_t BufLens[], const void *Bufs[]);
-extern bool IP_Pkt_Check(size_t len, const void *data, size_t *ofs, int AF, const void *Src, const void *Dst, uint8_t proto);
+extern bool IP_Pkt_Check(size_t len, const void *data, size_t *out_ofs, size_t *out_len, int AF, const void *Src, const void *Dst, uint8_t proto);
#endif
// The following skip the next check of each field
extern void TCP_SkipCheck_Seq(bool Skip);
-extern bool TCP_Pkt_Check(size_t len, const void *data, size_t *ofs, int AF, const void *IP, short sport, short dport, uint32_t seq, uint32_t ack, uint8_t flags);
+extern bool TCP_Pkt_Check(size_t len, const void *data, size_t *ofs, size_t *out_len, int AF, const void *IP, short sport, short dport, uint32_t seq, uint32_t ack, uint8_t flags);
// - Get a field from a previously validated packet
extern uint32_t TCP_Pkt_GetSeq(size_t len, const void *data, int AF);
}
}
-bool IP_Pkt_Check(size_t len, const void *data, size_t *ofs_out, int AF, const void *Src, const void *Dst, uint8_t proto)
+bool IP_Pkt_Check(size_t len, const void *data, size_t *ofs_out, size_t *len_out, int AF, const void *Src, const void *Dst, uint8_t proto)
{
size_t ofs;
if( AF == 4 ) {
TEST_ASSERT_REL(IP_Checksum(IP_CHECKSUM_START, sizeof(hdr), &hdr), ==, 0);
TEST_ASSERT_REL(ntohs(hdr.TotalLength), <=, len - ofs);
+ TEST_ASSERT_REL(ntohs(hdr.TotalLength), >, (hdr.VerLen & 0xF) * 4);
TEST_ASSERT_REL(ntohs(hdr.FragmentInfo), ==, 0);
TEST_ASSERT_REL(hdr.TTL, >, 1); // >1 because there's no intervening hops
if(Src) TEST_ASSERT( memcmp(hdr.SrcAddr, Src, 4) == 0 );
if(Dst) TEST_ASSERT( memcmp(hdr.DstAddr, Dst, 4) == 0 );
-
+
+ *len_out = ntohs(hdr.TotalLength) - sizeof(hdr);
*ofs_out = ofs + (hdr.VerLen & 0xF) * 4;
return true;
}
gTCP_Skips.Seq = Skip;
}
-bool TCP_Pkt_Check(size_t len, const void *data, size_t *out_ofs,
+bool TCP_Pkt_Check(size_t len, const void *data, size_t *out_ofs, size_t *len_out,
int AF, const void *IP, short sport, short dport,
uint32_t seq, uint32_t ack, uint8_t flags)
{
- size_t ofs;
- if( !IP_Pkt_Check(len, data, &ofs, AF, IP, BLOB(HOST_IP), IPPROTO_TCP) )
+ size_t ofs, rlen;
+ if( !IP_Pkt_Check(len, data, &ofs, &rlen, AF, IP, BLOB(HOST_IP), IPPROTO_TCP) )
return false;
// TODO: IP has its own length field, use that?
tTCPHeader hdr;
- TEST_ASSERT_REL(len - ofs, >=, sizeof(hdr));
+ TEST_ASSERT_REL(rlen, >=, sizeof(hdr));
memcpy(&hdr, (char*)data + ofs, sizeof(hdr));
TEST_ASSERT_REL( hdr.DataOfs >> 4, >=, sizeof(hdr)/4 );
uint16_t real_cksum = htons(hdr.Checksum);
hdr.Checksum = 0;
uint16_t calc_cksum;
- calc_cksum = TCP_int_GetPseudoHeader(AF, IP, BLOB(HOST_IP), IPPROTO_TCP, len-ofs);
+ calc_cksum = TCP_int_GetPseudoHeader(AF, IP, BLOB(HOST_IP), IPPROTO_TCP, rlen);
calc_cksum = IP_Checksum(calc_cksum, sizeof(hdr), &hdr);
- calc_cksum = IP_Checksum(calc_cksum, len - ofs - sizeof(hdr), (char*)data+ofs+sizeof(hdr));
+ calc_cksum = IP_Checksum(calc_cksum, rlen - sizeof(hdr), (char*)data+ofs+sizeof(hdr));
TEST_ASSERT_REL( real_cksum, ==, calc_cksum );
memset(&gTCP_Skips, 0, sizeof(gTCP_Skips));
*out_ofs = ofs + sizeof(hdr);
+ *len_out = rlen - sizeof(hdr);
return true;
}
uint32_t TCP_Pkt_GetSeq(size_t len, const void *data, int AF) {
- size_t ofs;
- IP_Pkt_Check(len, data, &ofs, AF, NULL, NULL, IPPROTO_TCP);
+ size_t ofs, rlen;
+ IP_Pkt_Check(len, data, &ofs, &rlen, AF, NULL, NULL, IPPROTO_TCP);
tTCPHeader hdr;
memcpy(&hdr, (char*)data + ofs, sizeof(hdr));
#include "stack.h"
#include "arp.h"
#include "tcp.h"
+#include <string.h>
#define TEST_ASSERT_rx() TEST_ASSERT( rxlen = Net_Receive(0, sizeof(rxbuf), rxbuf, ERX_TIMEOUT) )
#define TEST_ASSERT_no_rx() TEST_ASSERT( Net_Receive(0, sizeof(rxbuf), rxbuf, NRX_TIMEOUT) == 0 )
bool Test_TCP_Basic(void)
{
TEST_SETNAME(__func__);
- size_t rxlen, ofs;
+ size_t rxlen, ofs, len;
char rxbuf[MTU];
const int ERX_TIMEOUT = 1000; // Expect RX timeout (timeout=failure)
const int NRX_TIMEOUT = 250; // Not expect RX timeout (timeout=success)
TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_SYN, 0x1000, testblob_len, testblob);
// Expect a TCP_RST|TCP_ACK with SEQ=0,ACK=SEQ+LEN
TEST_ASSERT_rx();
- TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), 80, 1234,
+ TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, &len, 4, BLOB(TEST_IP), 80, 1234,
0, seq_tx+testblob_len, TCP_RST|TCP_ACK) );
TEST_ASSERT_REL(ofs, ==, rxlen);
TCP_Send(0, 4, BLOB(TEST_IP), 1234, 80, seq_tx, seq_exp, TCP_SYN|TCP_ACK, 0x1000, 0, NULL);
// Expect a TCP_RST with SEQ=ACK
TEST_ASSERT_rx();
- TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP), 80, 1234, seq_exp, seq_tx+0, TCP_RST) );
+ TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, &len, 4, BLOB(TEST_IP), 80, 1234, seq_exp, seq_tx+0, TCP_RST) );
TEST_ASSERT_REL(ofs, ==, rxlen);
// 1.3. Send a RST packet
// 2. Establishing connection with a server
- const int server_port = 1024;
+ const int server_port = 7;
const int local_port = 11234;
Stack_SendCommand("tcp_echo_server %i", server_port);
TCP_Send(0, 4, BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp, TCP_ACK, our_window, 0, NULL);
// - Expect RST
TEST_ASSERT_rx();
- TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP),
+ TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, &len, 4, BLOB(TEST_IP),
server_port, local_port, seq_exp, seq_tx+0, TCP_RST) );
// 2.3. Begin hanshake (SYN)
// - Expect SYN,ACK with ACK == SEQ+1
TEST_ASSERT_rx();
TCP_SkipCheck_Seq(true);
- TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, 4, BLOB(TEST_IP),
+ TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, &len, 4, BLOB(TEST_IP),
server_port, local_port, 0, seq_tx+1, TCP_SYN|TCP_ACK) );
seq_exp = TCP_Pkt_GetSeq(rxlen, rxbuf, 4);
// >>> STATE: ESTABLISHED
// 2.5. Send data
+ TCP_Send(0,4,BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp,
+ TCP_ACK|TCP_PSH, our_window, testblob_len, testblob);
+ seq_tx += testblob_len;
+ // Expect burst delayed ACK
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, &len, 4, BLOB(TEST_IP),
+ server_port, local_port, seq_exp, seq_tx, TCP_ACK) );
+ TEST_ASSERT_REL( len, ==, 0 );
+
+ // Expect echoed reponse with ACK
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, &len, 4, BLOB(TEST_IP),
+ server_port, local_port, seq_exp, seq_tx, TCP_ACK|TCP_PSH) );
+ TEST_ASSERT_REL( len, ==, testblob_len );
+ TEST_ASSERT( memcmp(rxbuf + ofs, testblob, testblob_len) == 0 );
+ seq_exp += testblob_len;
+
+ // 2.6. Close connection (TCP FIN)
+ TCP_Send(0,4,BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp,
+ TCP_ACK|TCP_FIN, our_window, 0, NULL);
+ seq_tx ++; // Empty = 1 byte
+ // Expect ACK? (Does acess do delayed ACKs here?)
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, &len, 4, BLOB(TEST_IP),
+ server_port, local_port, seq_exp, seq_tx, TCP_ACK) );
+ TEST_ASSERT_REL( len, ==, 0 );
+ // >>> STATE: CLOSE WAIT
+
+ // Expect FIN
+ TEST_ASSERT_rx();
+ TEST_ASSERT( TCP_Pkt_Check(rxlen, rxbuf, &ofs, &len, 4, BLOB(TEST_IP),
+ server_port, local_port, seq_exp, 0, TCP_FIN) );
+ TEST_ASSERT_REL( len, ==, 0 );
+
+ // >>> STATE: LAST-ACK
+
+ // 2.7 Send ACK of FIN
+ TCP_Send(0,4,BLOB(TEST_IP), local_port, server_port, seq_tx, seq_exp,
+ TCP_ACK, our_window, 0, NULL);
+ // Expect no response
+ TEST_ASSERT_no_rx();
+
+ // >>> STATE: CLOSED
return true;
}