Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/port/posix/bsd_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

static __thread int in_the_stack = 1;
static struct wolfIP *IPSTACK = NULL;
pthread_mutex_t wolfIP_mutex;
pthread_mutex_t wolfIP_mutex = PTHREAD_MUTEX_INITIALIZER;

struct wolfip_fd_entry;
int wolfIP_sock_poll(struct wolfIP *ipstack, struct pollfd *fds, nfds_t nfds, int timeout);
Expand Down Expand Up @@ -899,7 +899,7 @@ int wolfIP_sock_recvmsg(struct wolfIP *ipstack, int sockfd, struct msghdr *msg,
socklen_t addrlen = 0;
size_t total_len = 0;
int ret;
uint8_t stack_buf[WOLFIP_IOV_STACK_BUF];
uint8_t stack_buf[WOLFIP_IOV_STACK_BUF] = {0};
uint8_t *heap_buf = NULL;
uint8_t *buf = NULL;
struct pollfd pfd;
Expand Down Expand Up @@ -1274,6 +1274,7 @@ static int wolfip_accept_common(int sockfd, struct sockaddr *addr, socklen_t *ad
return -1;
}
wolfip_drain_pipe_locked(entry);
want_nonblock = wolfip_fd_is_nonblock(sockfd);
}
} while (internal_ret == -EAGAIN);
if (internal_ret < 0) {
Expand Down Expand Up @@ -1450,12 +1451,11 @@ ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct
}
if (ret == -EAGAIN) {
if (nonblock) {
if (sent == 0) {
errno = EAGAIN;
pthread_mutex_unlock(&wolfIP_mutex);
return -1;
}
break;
if (sent > 0)
break;
errno = EAGAIN;
pthread_mutex_unlock(&wolfIP_mutex);
return -1;
}
if (entry) {
wait_ret = wolfip_wait_for_event_locked(entry, POLLOUT, entry->snd_timeout_ms);
Expand All @@ -1472,7 +1472,7 @@ ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct
if (internal_fd < 0) {
pthread_mutex_unlock(&wolfIP_mutex);
errno = EBADF;
return (sent == 0) ? -1 : (ssize_t)sent;
return (sent > 0) ? (ssize_t)sent : -1;
}
}
continue;
Expand Down Expand Up @@ -1521,12 +1521,11 @@ ssize_t send(int sockfd, const void *buf, size_t len, int flags) {
}
if (ret == -EAGAIN) {
if (nonblock) {
if (sent == 0) {
errno = EAGAIN;
pthread_mutex_unlock(&wolfIP_mutex);
return -1;
}
break;
if (sent > 0)
break;
errno = EAGAIN;
pthread_mutex_unlock(&wolfIP_mutex);
return -1;
}
if (entry) {
wait_ret = wolfip_wait_for_event_locked(entry, POLLOUT, entry->snd_timeout_ms);
Expand Down Expand Up @@ -1580,12 +1579,11 @@ ssize_t write(int sockfd, const void *buf, size_t len) {
}
if (ret == -EAGAIN) {
if (nonblock) {
if (sent == 0) {
errno = EAGAIN;
pthread_mutex_unlock(&wolfIP_mutex);
return -1;
}
break;
if (sent > 0)
break;
errno = EAGAIN;
pthread_mutex_unlock(&wolfIP_mutex);
return -1;
}
if (entry) {
wait_ret = wolfip_wait_for_event_locked(entry, POLLOUT, entry->snd_timeout_ms);
Expand Down Expand Up @@ -1687,8 +1685,11 @@ void __attribute__((constructor)) init_wolfip_posix() {
#if WOLFIP_POSIX_TCPDUMP
static int tcpdump_atexit_registered;
#endif
if (IPSTACK)
pthread_mutex_lock(&wolfIP_mutex);
if (IPSTACK) {
pthread_mutex_unlock(&wolfIP_mutex);
return;
}
host_stack_ip_str = getenv("WOLFIP_HOST_IP");
if (!host_stack_ip_str || host_stack_ip_str[0] == '\0') {
host_stack_ip_str = HOST_STACK_IP;
Expand Down Expand Up @@ -1734,8 +1735,6 @@ void __attribute__((constructor)) init_wolfip_posix() {
swap_socketcall(select, "select");
swap_socketcall(fcntl, "fcntl");

pthread_mutex_init(&wolfIP_mutex, NULL);
pthread_mutex_lock(&wolfIP_mutex);
wolfIP_init_static(&IPSTACK);
tapdev = wolfIP_getdev(IPSTACK);
if (!tapdev) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ipfilter_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ static void *pt_echoclient(void *arg)
}
wolfSSL_set_fd(client_ssl, fd);
sleep(1);
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
printf("Connecting to echo server\n");
ret = connect(fd, (struct sockaddr *)&remote_sock, sizeof(remote_sock));
if (ret < 0) {
Expand Down
14 changes: 11 additions & 3 deletions src/test/test_eventloop_tun.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void *pt_echoclient(void *arg)
return (void *)-1;
}
sleep(1);
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
printf("Connecting to echo server\n");
old_flags = fcntl(fd, F_GETFL, 0);
if (old_flags < 0) {
Expand Down Expand Up @@ -343,7 +343,7 @@ void *pt_echoclient(void *arg)
*/
static void *pt_echoserver(void *arg)
{
int fd, ret;
int fd, listen_fd, ret;
unsigned total_r = 0;
uint8_t local_buf[BUFFER_SIZE];
struct sockaddr_in local_sock = {
Expand All @@ -358,21 +358,25 @@ static void *pt_echoserver(void *arg)
return (void *)-1;
}
local_sock.sin_addr.s_addr = inet_addr(HOST_STACK_IP);
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
(void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
ret = bind(fd, (struct sockaddr *)&local_sock, sizeof(local_sock));
if (ret < 0) {
printf("test server bind: %d (%s)\n", ret, strerror(errno));
close(fd);
return (void *)-1;
}
ret = listen(fd, 1);
if (ret < 0) {
printf("test server listen: %d\n", ret);
close(fd);
return (void *)-1;
}
listen_fd = fd;
printf("Waiting for client\n");
ret = accept(fd, NULL, NULL);
if (ret < 0) {
printf("test server accept: %d\n", ret);
close(listen_fd);
return (void *)-1;
}
printf("test server: client %d connected\n", ret);
Expand All @@ -381,10 +385,14 @@ static void *pt_echoserver(void *arg)
ret = read(fd, local_buf + total_r, sizeof(local_buf) - total_r);
if (ret < 0) {
printf("failed test server read: %d (%s) \n", ret, strerror(errno));
close(fd);
close(listen_fd);
return (void *)-1;
}
if (ret == 0) {
printf("test server read: client has closed the connection.\n");
close(fd);
close(listen_fd);
if (wolfIP_closing)
return (void *)0;
else
Expand Down
11 changes: 10 additions & 1 deletion src/test/test_ttl_expired.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,16 @@ uint32_t wolfIP_getrandom(void)
return ret;
}
#endif
ret = (uint32_t)rand();
{
FILE *f = fopen("/dev/urandom", "rb");
if (f) {
size_t r = fread(&ret, sizeof(ret), 1, f);
fclose(f);
if (r == 1)
return ret;
}
}
ret = 0;
return ret;
}

Expand Down
1 change: 0 additions & 1 deletion src/test/unit/unit_tests_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -3255,4 +3255,3 @@ START_TEST(test_dns_wrapper_apis)
ck_assert_uint_eq(s.dns_query_type, DNS_QUERY_TYPE_PTR);
}
END_TEST

6 changes: 5 additions & 1 deletion src/wolfip.c
Original file line number Diff line number Diff line change
Expand Up @@ -2003,11 +2003,15 @@ static struct tsocket *tcp_new_socket(struct wolfIP *s)
t->sock.tcp.peer_sack_count = 0;
memset(t->sock.tcp.ooo, 0, sizeof(t->sock.tcp.ooo));
{
#if RXBUF_SIZE > 0xFFFF
uint32_t space = RXBUF_SIZE;
uint8_t shift = 0;
while (shift < 14 && (space >> shift) > 0xFFFF)
shift++;
t->sock.tcp.rcv_wscale = shift;
#else
t->sock.tcp.rcv_wscale = 0;
#endif
}
/* We always include WS in the initial SYN (shift may be 0), so
* mark that we offered it to accept the peer's WS in SYN-ACK. */
Expand Down Expand Up @@ -4505,7 +4509,7 @@ int wolfIP_sock_sendto(struct wolfIP *s, int sockfd, const void *buf, size_t len
}
if (sizeof(struct wolfIP_ip_packet) + payload_len > sizeof(frame))
return -WOLFIP_EINVAL;
memcpy(&icmp->type, buf, payload_len);
memcpy(frame + sizeof(struct wolfIP_ip_packet), buf, payload_len);
if (icmp->type == ICMP_ECHO_REQUEST)
icmp_set_echo_id(icmp, ts->src_port);
icmp->csum = 0;
Expand Down
Loading