Renamed socket API
This commit is contained in:
parent
1829548cd5
commit
fe8a25b446
3 changed files with 49 additions and 41 deletions
20
femtotcp.h
20
femtotcp.h
|
@ -54,16 +54,16 @@ typedef uint32_t socklen_t;
|
|||
#endif
|
||||
#endif
|
||||
|
||||
int posix_socket(struct ipstack *s, int domain, int type, int protocol);
|
||||
int posix_bind(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen);
|
||||
int posix_listen(struct ipstack *s, int sockfd, int backlog);
|
||||
int posix_accept(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
|
||||
int posix_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen);
|
||||
int posix_sendto(struct ipstack *s, int sockfd, const void *buf, size_t len, int flags, const struct ipstack_sockaddr *dest_addr, socklen_t addrlen);
|
||||
int posix_recvfrom(struct ipstack *s, int sockfd, void *buf, size_t len, int flags, struct ipstack_sockaddr *src_addr, socklen_t *addrlen);
|
||||
int posix_close(struct ipstack *s, int sockfd);
|
||||
int posix_getpeername(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
|
||||
int posix_getsockname(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
|
||||
int ft_socket(struct ipstack *s, int domain, int type, int protocol);
|
||||
int ft_bind(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen);
|
||||
int ft_listen(struct ipstack *s, int sockfd, int backlog);
|
||||
int ft_accept(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
|
||||
int ft_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen);
|
||||
int ft_sendto(struct ipstack *s, int sockfd, const void *buf, size_t len, int flags, const struct ipstack_sockaddr *dest_addr, socklen_t addrlen);
|
||||
int ft_recvfrom(struct ipstack *s, int sockfd, void *buf, size_t len, int flags, struct ipstack_sockaddr *src_addr, socklen_t *addrlen);
|
||||
int ft_close(struct ipstack *s, int sockfd);
|
||||
int ft_getpeername(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
|
||||
int ft_getsockname(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
|
||||
|
||||
int dhcp_client_init(struct ipstack *s);
|
||||
int dhcp_bound(struct ipstack *s);
|
||||
|
|
|
@ -603,7 +603,7 @@ static struct ipstack_timer timers_binheap_pop(struct timers_binheap *heap)
|
|||
heap->timers[j] = tmp;
|
||||
i = j;
|
||||
}
|
||||
} while (tmr.expires == 0);
|
||||
} while ((tmr.expires == 0) && (heap->size > 0));
|
||||
return tmr;
|
||||
}
|
||||
|
||||
|
@ -1173,7 +1173,7 @@ static void close_socket(struct tsocket *ts)
|
|||
}
|
||||
|
||||
|
||||
int posix_socket(struct ipstack *s, int domain, int type, int protocol)
|
||||
int ft_socket(struct ipstack *s, int domain, int type, int protocol)
|
||||
{
|
||||
struct tsocket *ts;
|
||||
if (domain != AF_INET)
|
||||
|
@ -1193,7 +1193,7 @@ int posix_socket(struct ipstack *s, int domain, int type, int protocol)
|
|||
return -1;
|
||||
}
|
||||
|
||||
int posix_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen)
|
||||
int ft_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
struct tsocket *ts;
|
||||
struct ipstack_sockaddr_in *sin = (struct ipstack_sockaddr_in *)addr;
|
||||
|
@ -1231,7 +1231,7 @@ int posix_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *
|
|||
return -2;
|
||||
}
|
||||
|
||||
int posix_accept(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen)
|
||||
int ft_accept(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
struct tsocket *ts;
|
||||
struct ipstack_sockaddr_in *sin = (struct ipstack_sockaddr_in *)addr;
|
||||
|
@ -1276,7 +1276,7 @@ int posix_accept(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, s
|
|||
return -1;;
|
||||
}
|
||||
|
||||
int posix_sendto(struct ipstack *s, int sockfd, const void *buf, size_t len, int flags,
|
||||
int ft_sendto(struct ipstack *s, int sockfd, const void *buf, size_t len, int flags,
|
||||
const struct ipstack_sockaddr *dest_addr, socklen_t addrlen)
|
||||
{
|
||||
uint8_t frame[LINK_MTU];
|
||||
|
@ -1359,7 +1359,7 @@ int posix_sendto(struct ipstack *s, int sockfd, const void *buf, size_t len, int
|
|||
} else return -1;
|
||||
}
|
||||
|
||||
int posix_recvfrom(struct ipstack *s, int sockfd, void *buf, size_t len, int flags,
|
||||
int ft_recvfrom(struct ipstack *s, int sockfd, void *buf, size_t len, int flags,
|
||||
struct ipstack_sockaddr *src_addr, socklen_t *addrlen)
|
||||
{
|
||||
uint32_t seg_len;
|
||||
|
@ -1405,7 +1405,7 @@ int posix_recvfrom(struct ipstack *s, int sockfd, void *buf, size_t len, int fla
|
|||
} else return -1;
|
||||
}
|
||||
|
||||
int posix_close(struct ipstack *s, int sockfd)
|
||||
int ft_close(struct ipstack *s, int sockfd)
|
||||
{
|
||||
if (sockfd & MARK_TCP_SOCKET) {
|
||||
struct tsocket *ts = &s->tcpsockets[sockfd & ~MARK_TCP_SOCKET];
|
||||
|
@ -1439,7 +1439,7 @@ int posix_close(struct ipstack *s, int sockfd)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int posix_getsockname(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen)
|
||||
int ft_getsockname(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
struct tsocket *ts = &s->tcpsockets[sockfd];
|
||||
struct ipstack_sockaddr_in *sin = (struct ipstack_sockaddr_in *)addr;
|
||||
|
@ -1451,7 +1451,7 @@ int posix_getsockname(struct ipstack *s, int sockfd, struct ipstack_sockaddr *ad
|
|||
return 0;
|
||||
}
|
||||
|
||||
int posix_bind(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen)
|
||||
int ft_bind(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
struct tsocket *ts;
|
||||
struct ipstack_sockaddr_in *sin = (struct ipstack_sockaddr_in *)addr;
|
||||
|
@ -1479,7 +1479,7 @@ int posix_bind(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *add
|
|||
|
||||
}
|
||||
|
||||
int posix_listen(struct ipstack *s, int sockfd, int backlog)
|
||||
int ft_listen(struct ipstack *s, int sockfd, int backlog)
|
||||
{
|
||||
struct tsocket *ts;
|
||||
(void)backlog;
|
||||
|
@ -1492,7 +1492,7 @@ int posix_listen(struct ipstack *s, int sockfd, int backlog)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int posix_getpeername(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen)
|
||||
int ft_getpeername(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
struct tsocket *ts = &s->tcpsockets[sockfd];
|
||||
struct ipstack_sockaddr_in *sin = (struct ipstack_sockaddr_in *)addr;
|
||||
|
@ -1646,7 +1646,7 @@ static int dhcp_poll(struct ipstack *s)
|
|||
struct dhcp_msg msg;
|
||||
int len;
|
||||
memset(&msg, 0xBB, sizeof(msg));
|
||||
len = posix_recvfrom(s, s->dhcp_udp_sd, &msg, sizeof(struct dhcp_msg), 0, (struct ipstack_sockaddr *)&sin, &sl);
|
||||
len = ft_recvfrom(s, s->dhcp_udp_sd, &msg, sizeof(struct dhcp_msg), 0, (struct ipstack_sockaddr *)&sin, &sl);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
if ((s->dhcp_state == DHCP_DISCOVER_SENT) && (dhcp_parse_offer(s, &msg) == 0))
|
||||
|
@ -1708,7 +1708,7 @@ static int dhcp_send_request(struct ipstack *s)
|
|||
sin.sin_port = ee16(DHCP_SERVER_PORT);
|
||||
sin.sin_addr.s_addr = ee32(0xFFFFFFFF); /* Broadcast */
|
||||
sin.sin_family = AF_INET;
|
||||
posix_sendto(s, s->dhcp_udp_sd, &req, DHCP_HEADER_LEN + opt_sz, 0,
|
||||
ft_sendto(s, s->dhcp_udp_sd, &req, DHCP_HEADER_LEN + opt_sz, 0,
|
||||
(struct ipstack_sockaddr *)&sin, sizeof(struct ipstack_sockaddr_in));
|
||||
tmr.expires = s->last_tick + DHCP_REQUEST_TIMEOUT + (ipstack_getrandom() % 200);
|
||||
tmr.arg = s;
|
||||
|
@ -1752,7 +1752,7 @@ static int dhcp_send_discover(struct ipstack *s)
|
|||
sin.sin_port = ee16(DHCP_SERVER_PORT);
|
||||
sin.sin_addr.s_addr = ee32(0xFFFFFFFF); /* Broadcast */
|
||||
sin.sin_family = AF_INET;
|
||||
posix_sendto(s, s->dhcp_udp_sd, &disc, DHCP_HEADER_LEN + opt_sz, 0,
|
||||
ft_sendto(s, s->dhcp_udp_sd, &disc, DHCP_HEADER_LEN + opt_sz, 0,
|
||||
(struct ipstack_sockaddr *)&sin, sizeof(struct ipstack_sockaddr_in));
|
||||
tmr.expires = s->last_tick + DHCP_DISCOVER_TIMEOUT + (ipstack_getrandom() % 200);
|
||||
tmr.arg = s;
|
||||
|
@ -1776,10 +1776,10 @@ int dhcp_client_init(struct ipstack *s)
|
|||
s->dhcp_xid = ipstack_getrandom();
|
||||
|
||||
if (s->dhcp_udp_sd > 0) {
|
||||
posix_close(s, s->dhcp_udp_sd);
|
||||
ft_close(s, s->dhcp_udp_sd);
|
||||
}
|
||||
|
||||
s->dhcp_udp_sd = posix_socket(s, AF_INET, IPSTACK_SOCK_DGRAM, IPPROTO_UDP);
|
||||
s->dhcp_udp_sd = ft_socket(s, AF_INET, IPSTACK_SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (s->dhcp_udp_sd < 0) {
|
||||
s->dhcp_state = DHCP_OFF;
|
||||
return -1;
|
||||
|
@ -1787,7 +1787,7 @@ int dhcp_client_init(struct ipstack *s)
|
|||
memset(&sin, 0, sizeof(struct ipstack_sockaddr_in));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_port = ee16(DHCP_CLIENT_PORT);
|
||||
if (posix_bind(s, s->dhcp_udp_sd, (struct ipstack_sockaddr *)&sin, sizeof(struct ipstack_sockaddr_in)) < 0) {
|
||||
if (ft_bind(s, s->dhcp_udp_sd, (struct ipstack_sockaddr *)&sin, sizeof(struct ipstack_sockaddr_in)) < 0) {
|
||||
s->dhcp_state = DHCP_OFF;
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -145,7 +145,7 @@ uint32_t ipstack_getrandom(void)
|
|||
return ret;
|
||||
}
|
||||
#define BUFFER_SIZE TEST_SIZE
|
||||
static int test_echoserver_closewait(struct ipstack *s)
|
||||
static int test_echoserver(struct ipstack *s, int active_close)
|
||||
{
|
||||
int fd, ret;
|
||||
int client_fd = -1;
|
||||
|
@ -156,11 +156,11 @@ static int test_echoserver_closewait(struct ipstack *s)
|
|||
.sin_port = ee16(8), /* Echo */
|
||||
.sin_addr.s_addr = 0
|
||||
};
|
||||
fd = posix_socket(s, AF_INET, IPSTACK_SOCK_STREAM, 0);
|
||||
fd = ft_socket(s, AF_INET, IPSTACK_SOCK_STREAM, 0);
|
||||
printf("socket: %04x\n", fd);
|
||||
ret = posix_bind(s, fd, (struct ipstack_sockaddr *)&local_sock, sizeof(local_sock));
|
||||
ret = ft_bind(s, fd, (struct ipstack_sockaddr *)&local_sock, sizeof(local_sock));
|
||||
printf("bind: %d\n", ret);
|
||||
ret = posix_listen(s, fd, 1);
|
||||
ret = ft_listen(s, fd, 1);
|
||||
printf("listen: %d\n", ret);
|
||||
|
||||
while(1) {
|
||||
|
@ -176,22 +176,22 @@ static int test_echoserver_closewait(struct ipstack *s)
|
|||
else break;
|
||||
}
|
||||
if (client_fd < 0) {
|
||||
client_fd = posix_accept(s, fd, NULL, NULL);
|
||||
client_fd = ft_accept(s, fd, NULL, NULL);
|
||||
if (client_fd > 0) {
|
||||
printf("accept: %04x\n", client_fd);
|
||||
}
|
||||
} else {
|
||||
if (ret <= 0) {
|
||||
ret = posix_recvfrom(s, client_fd, buf, sizeof(buf), 0, NULL, NULL);
|
||||
ret = ft_recvfrom(s, client_fd, buf, sizeof(buf), 0, NULL, NULL);
|
||||
if (ret == -11)
|
||||
continue; /* Call again */
|
||||
if (ret < 0) {
|
||||
printf("Recv error: %d\n", ret);
|
||||
posix_close(s, client_fd);
|
||||
ft_close(s, client_fd);
|
||||
return -1;
|
||||
} else if (ret == 0) {
|
||||
printf("Client side closed the connection.\n");
|
||||
posix_close(s, client_fd);
|
||||
ft_close(s, client_fd);
|
||||
client_fd = -1;
|
||||
printf("Server: Exiting.\n");
|
||||
exit_ok = 1;
|
||||
|
@ -202,12 +202,12 @@ static int test_echoserver_closewait(struct ipstack *s)
|
|||
}
|
||||
if (ret > 0) {
|
||||
int snd_ret;
|
||||
snd_ret = posix_sendto(s, client_fd, buf + tot_sent, ret - tot_sent, 0, NULL, 0);
|
||||
snd_ret = ft_sendto(s, client_fd, buf + tot_sent, ret - tot_sent, 0, NULL, 0);
|
||||
if (snd_ret == -11)
|
||||
continue; /* Call again */
|
||||
if (snd_ret < 0) {
|
||||
printf("Send error: %d\n", snd_ret);
|
||||
posix_close(s, client_fd);
|
||||
ft_close(s, client_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -219,11 +219,18 @@ static int test_echoserver_closewait(struct ipstack *s)
|
|||
}
|
||||
}
|
||||
}
|
||||
if ((tot_sent >= 8192) && active_close) {
|
||||
ft_close(s, client_fd);
|
||||
client_fd = -1;
|
||||
printf("Server: Exiting.\n");
|
||||
exit_ok = 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *pt_echoclient_closing(void *arg)
|
||||
void *pt_echoclient_closing(void *arg)
|
||||
{
|
||||
int fd, ret;
|
||||
unsigned total_r = 0;
|
||||
|
@ -292,13 +299,15 @@ int main(int argc, char **argv)
|
|||
ip4 ip = 0, nm = 0, gw = 0;
|
||||
uint32_t srv_ip;
|
||||
|
||||
int ret, test_ret;
|
||||
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)
|
||||
|
@ -311,7 +320,6 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
system("tcpdump -i femt0 -w test.pcap &");
|
||||
sleep(1);
|
||||
|
||||
#ifdef DHCP
|
||||
gettimeofday(&tv, NULL);
|
||||
|
@ -336,7 +344,7 @@ int main(int argc, char **argv)
|
|||
|
||||
pthread_create(&pt, NULL, pt_echoclient_closing, &srv_ip);
|
||||
printf("Starting test: echo server close-wait\n");
|
||||
ret = test_echoserver_closewait(s);
|
||||
ret = test_echoserver(s, 0);
|
||||
pthread_join(pt, (void **)&test_ret);
|
||||
printf("Test echo server close-wait: %d\n", ret);
|
||||
printf("Test linux client: %d\n", test_ret);
|
||||
|
|
Loading…
Reference in a new issue