diff --git a/femtotcp.h b/femtotcp.h index 724bf29..97e4907 100644 --- a/femtotcp.h +++ b/femtotcp.h @@ -1,5 +1,5 @@ -#ifndef QUECTONET_H -#define QUECTONET_H +#ifndef FEMTOTCP_H +#define FEMTOTCP_H #include /* Types */ @@ -39,7 +39,7 @@ struct ipconf { }; /* Socket interface */ -#ifndef NATIVE_POSIX_SOCKET +#ifndef FEMTO_POSIX #define IPSTACK_SOCK_STREAM 1 #define IPSTACK_SOCK_DGRAM 2 struct ipstack_sockaddr_in { @@ -52,6 +52,13 @@ typedef uint32_t socklen_t; #ifndef AF_INET #define AF_INET 2 #endif +#else +#include +#include +#include +#include +#define ipstack_sockaddr_in sockaddr_in +#define ipstack_sockaddr sockaddr #endif int ft_socket(struct ipstack *s, int domain, int type, int protocol); diff --git a/src/femtotcp.c b/src/femtotcp.c index 0585566..1fec713 100644 --- a/src/femtotcp.c +++ b/src/femtotcp.c @@ -1195,7 +1195,6 @@ int ft_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *add { struct tsocket *ts; struct ipstack_sockaddr_in *sin = (struct ipstack_sockaddr_in *)addr; - struct ipstack_tcp_seg tcp; if (!addr) return -2; if (sockfd & MARK_UDP_SOCKET) { @@ -1222,16 +1221,7 @@ int ft_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *add if (ts->src_port < 1024) ts->src_port += 1024; ts->dst_port = ee16(sin->sin_port); - tcp.src_port = ee16(ts->src_port); - tcp.dst_port = sin->sin_port; - tcp.seq = ts->sock.tcp.seq; - tcp.ack = 0; - tcp.hlen = (TCP_HEADER_LEN + TCP_OPTIONS_LEN) << 2; - tcp.flags = 0x02; /* SYN */ - tcp.win = ee16((uint16_t)queue_space(&ts->sock.tcp.rxbuf)); - tcp.csum = 0; - tcp.urg = 0; - fifo_push(&ts->sock.tcp.txbuf, &tcp, sizeof(struct ipstack_tcp_seg) + TCP_OPTIONS_LEN); + tcp_send_syn(ts, 0x02); return -11; } return -2; diff --git a/test/test-linux.c b/test/test-linux.c index 22afa99..7b6c7c9 100644 --- a/test/test-linux.c +++ b/test/test-linux.c @@ -27,11 +27,12 @@ static int conn_fd = -1; static int client_connected = 0; static const uint8_t test_pattern[16] = "Test pattern - -"; + + +/* FemtoTCP: server side callback. */ static void server_cb(int fd, uint16_t event, void *arg) { int ret = 0; - printf("Called server_cb, events: %04x fd %d\n", event, fd & (~0x1000)); - if ((fd == listen_fd) && (event & CB_EVENT_READABLE) && (client_fd == -1)) { client_fd = ft_accept((struct ipstack *)arg, listen_fd, NULL, NULL); if (client_fd > 0) { @@ -92,13 +93,13 @@ static void server_cb(int fd, uint16_t event, void *arg) (void)arg; } +/* Client-side callback. */ static void client_cb(int fd, uint16_t event, void *arg) { struct ipstack *s = (struct ipstack *)arg; uint32_t i; int ret; static unsigned int total_r = 0, total_w = 0; - printf("Called client_cb, events: %04x fd %d\n", event, fd & (~0x1000)); if (fd == conn_fd) { if ((event & CB_EVENT_WRITABLE) && (client_connected == 0)) { printf("Client: connected\n"); @@ -154,6 +155,7 @@ static void client_cb(int fd, uint16_t event, void *arg) } +/* FemtoTCP side: main loop of the stack under test. */ static int test_loop(struct ipstack *s, int active_close) { exit_ok = 0; @@ -177,6 +179,9 @@ static int test_loop(struct ipstack *s, int active_close) return 0; } +/* Test code (Linux side). + * Thread with client to test the echoserver. + */ void *pt_echoclient(void *arg) { int fd, ret; @@ -240,6 +245,9 @@ void *pt_echoclient(void *arg) return (void *)0; } +/* Test code (Linux side). + * Thread with echo server to test the client. + */ static void *pt_echoserver(void *arg) { int fd, ret; @@ -294,64 +302,23 @@ static void *pt_echoserver(void *arg) } } + +/* Catch-all function to initialize a new tap device as the network interface. + * This is defined in port/linux.c + * */ extern int tap_init(struct ll *dev, const char *name, uint32_t host_ip); -int main(int argc, char **argv) +/* Test cases */ + +void test_femtotcp_echoserver(struct ipstack *s, uint32_t srv_ip) { - struct ipstack *s; - struct ll *tapdev; + int ret, test_ret = 0; pthread_t pt; - struct timeval tv; - struct in_addr linux_ip; - ip4 ip = 0, nm = 0, gw = 0; - uint32_t srv_ip; struct ipstack_sockaddr_in local_sock = { .sin_family = AF_INET, .sin_port = ee16(8), /* Echo */ .sin_addr.s_addr = 0 }; - struct ipstack_sockaddr_in remote_sock; - - int ret, test_ret = 0; - (void)argc; - (void)argv; - (void)ip; - (void)nm; - (void)gw; - (void)tv; - (void)pt; - (void)srv_ip; - ipstack_init_static(&s); - tapdev = ipstack_getdev(s); - if (!tapdev) - return 1; - inet_aton(LINUX_IP, &linux_ip); - if (tap_init(tapdev, "femt0", linux_ip.s_addr) < 0) { - perror("tap init"); - return 2; - } - system("tcpdump -i femt0 -w test.pcap &"); - -#ifdef DHCP - gettimeofday(&tv, NULL); - ipstack_poll(s, tv.tv_sec * 1000 + tv.tv_usec / 1000); - dhcp_client_init(s); - do { - gettimeofday(&tv, NULL); - ipstack_poll(s, tv.tv_sec * 1000 + tv.tv_usec / 1000); - usleep(1000); - ipstack_ipconfig_get(s, &ip, &nm, &gw); - } while (!dhcp_bound(s)); - printf("DHCP: obtained IP address.\n"); - ipstack_ipconfig_get(s, &ip, &nm, &gw); - srv_ip = htonl(ip); -#else - ipstack_ipconfig_set(s, atoip4(FEMTOTCP_IP), atoip4("255.255.255.0"), - atoip4(LINUX_IP)); - printf("IP: manually configured\n"); - inet_pton(AF_INET, FEMTOTCP_IP, &srv_ip); -#endif - printf("TCP server tests\n"); listen_fd = ft_socket(s, AF_INET, IPSTACK_SOCK_STREAM, 0); @@ -379,9 +346,13 @@ int main(int argc, char **argv) sleep(1); ft_close(s, listen_fd); - /* End TCP Server tests */ - +} +void test_femtotcp_echoclient(struct ipstack *s) +{ + int ret, test_ret = 0; + pthread_t pt; + struct ipstack_sockaddr_in remote_sock; /* Client side test: client is closing the connection */ remote_sock.sin_family = AF_INET; remote_sock.sin_port = ee16(8); @@ -424,6 +395,62 @@ int main(int argc, char **argv) #endif + +} + +/* Main test function. */ +int main(int argc, char **argv) +{ + struct ipstack *s; + struct ll *tapdev; + struct timeval tv; + struct in_addr linux_ip; + uint32_t srv_ip; + ip4 ip = 0, nm = 0, gw = 0; + + (void)argc; + (void)argv; + (void)ip; + (void)nm; + (void)gw; + (void)tv; + ipstack_init_static(&s); + tapdev = ipstack_getdev(s); + if (!tapdev) + return 1; + inet_aton(LINUX_IP, &linux_ip); + if (tap_init(tapdev, "femt0", linux_ip.s_addr) < 0) { + perror("tap init"); + return 2; + } + system("tcpdump -i femt0 -w test.pcap &"); + +#ifdef DHCP + gettimeofday(&tv, NULL); + ipstack_poll(s, tv.tv_sec * 1000 + tv.tv_usec / 1000); + dhcp_client_init(s); + do { + gettimeofday(&tv, NULL); + ipstack_poll(s, tv.tv_sec * 1000 + tv.tv_usec / 1000); + usleep(1000); + ipstack_ipconfig_get(s, &ip, &nm, &gw); + } while (!dhcp_bound(s)); + printf("DHCP: obtained IP address.\n"); + ipstack_ipconfig_get(s, &ip, &nm, &gw); + srv_ip = htonl(ip); +#else + ipstack_ipconfig_set(s, atoip4(FEMTOTCP_IP), atoip4("255.255.255.0"), + atoip4(LINUX_IP)); + printf("IP: manually configured\n"); + inet_pton(AF_INET, FEMTOTCP_IP, &srv_ip); +#endif + + /* Server side test */ + test_femtotcp_echoserver(s, srv_ip); + + /* Client side test */ + test_femtotcp_echoclient(s); + system("killall tcpdump"); return 0; }