Skip to content

Commit

Permalink
send: Fix send_sync()
Browse files Browse the repository at this point in the history
send_sync needs a filter to work - enforce this
Clear out some arrays that could have random values
  • Loading branch information
chrissie-c authored and fabbione committed Aug 25, 2021
1 parent 2ef5a12 commit 196a53a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions libknet/libknet.h
Expand Up @@ -476,6 +476,7 @@ ssize_t knet_send(knet_handle_t knet_h,
* @retval EHOSTDOWN - unicast pckt cannot be delivered because dest host is not connected yet
* @retval ECHILD - crypto failed
* @retval EAGAIN - sendmmsg was unable to send all messages and there was no progress during retry
* @retval ENETDOWN - a packet filter was not installed (necessary for knet_send_sync, but not knet_send)
*/

int knet_send_sync(knet_handle_t knet_h,
Expand Down
28 changes: 20 additions & 8 deletions libknet/tests/api_knet_send_sync.c
Expand Up @@ -178,6 +178,26 @@ static void test(void)

flush_logs(logfds[0], stdout);

printf("Test knet_send_sync with no filter configured\n");

channel = 1;
if ((!knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != ENETDOWN)) {
printf("knet_send_sync Did not return ENETDOWN for no filter installed: %s\n", strerror(errno));
knet_handle_free(knet_h);
flush_logs(logfds[0], stdout);
close_logpipes(logfds);
exit(FAIL);
}

if (knet_handle_enable_filter(knet_h, NULL, dhost_filter) < 0) {
printf("knet_handle_enable_filter failed: %s\n", strerror(errno));
knet_handle_free(knet_h);
flush_logs(logfds[0], stdout);
close_logpipes(logfds);
exit(FAIL);
}


printf("Test knet_send_sync with unconfigured channel\n");

channel = 0;
Expand Down Expand Up @@ -233,14 +253,6 @@ static void test(void)
exit(FAIL);
}

if (knet_handle_enable_filter(knet_h, NULL, dhost_filter) < 0) {
printf("knet_handle_enable_filter failed: %s\n", strerror(errno));
knet_handle_free(knet_h);
flush_logs(logfds[0], stdout);
close_logpipes(logfds);
exit(FAIL);
}

dhost_filter_ret = -1;

if ((knet_send_sync(knet_h, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != EFAULT)) {
Expand Down
9 changes: 9 additions & 0 deletions libknet/threads_tx.c
Expand Up @@ -422,6 +422,8 @@ static int _get_data_dests(knet_handle_t knet_h, unsigned char* data, size_t inl
struct knet_host *dst_host;
size_t host_idx;

memset(dst_host_ids_temp, 0, sizeof(dst_host_ids_temp));

if (knet_h->dst_host_filter_fn) {
*bcast = knet_h->dst_host_filter_fn(
knet_h->dst_host_filter_fn_private_data,
Expand Down Expand Up @@ -610,6 +612,7 @@ static int _parse_recv_from_sock(knet_handle_t knet_h, size_t inlen, int8_t chan
}
}

memset(dst_host_ids, 0, sizeof(dst_host_ids));
err = _get_data_dests(knet_h, data, inlen,
&channel, &bcast, &send_local,
dst_host_ids, &dst_host_ids_entries,
Expand Down Expand Up @@ -874,6 +877,12 @@ int knet_send_sync(knet_handle_t knet_h, const char *buff, const size_t buff_len
return -1;
}

if (!knet_h->dst_host_filter_fn) {
savederrno = ENETDOWN;
err = -1;
goto out;
}

if (!knet_h->sockfd[channel].in_use) {
savederrno = EINVAL;
err = -1;
Expand Down

0 comments on commit 196a53a

Please sign in to comment.