From a31d410d3dbc0f0c866a11a5a46b58054625f4ba Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 16 Mar 2010 22:14:31 +0100 Subject: [PATCH] udp_recv: Replace recv_bin_buffer() by Use para_readv(). As pointed out by Gerrit Renker, the UDP receiver suffers from the same problem that was fixed in commit 9e44ee8 for the DCCP receiver: recv() might lose data if less than a full datagram is available in the receiving buffer. This patch solves the problem for udp_recv in the same way as 9e44ee8 does for dccp_recv. --- udp_recv.c | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/udp_recv.c b/udp_recv.c index ae2d49f1..c768d324 100644 --- a/udp_recv.c +++ b/udp_recv.c @@ -44,14 +44,32 @@ static void udp_recv_pre_select(struct sched *s, struct task *t) para_fd_set(purd->fd, &s->rfds, &s->max_fileno); } +static int udp_check_eof(size_t sz, struct iovec iov[2]) +{ + if (sz < FEC_EOF_PACKET_LEN) + return 0; + if (iov[0].iov_len >= FEC_EOF_PACKET_LEN) { + if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, + FEC_EOF_PACKET_LEN) != 0) + return 0; + return -E_RECV_EOF; + } + if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, iov[0].iov_len) != 0) + return 0; + if (memcmp(iov[1].iov_base, FEC_EOF_PACKET + iov[0].iov_len, + FEC_EOF_PACKET_LEN - iov[0].iov_len) != 0) + return 0; + return -E_RECV_EOF; +} + static void udp_recv_post_select(__a_unused struct sched *s, struct task *t) { struct receiver_node *rn = container_of(t, struct receiver_node, task); struct private_udp_recv_data *purd = rn->private_data; struct btr_node *btrn = rn->btrn; - int ret; - char *buf = NULL; - size_t packet_size, bufsize; + size_t packet_size; + struct iovec iov[2]; + int ret, iovcnt; t->error = 0; ret = btr_node_status(btrn, 0, BTR_NT_ROOT); @@ -61,24 +79,26 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t) return; if (!FD_ISSET(purd->fd, &s->rfds)) return; - bufsize = btr_pool_get_buffer(purd->btrp, &buf); + iovcnt = btr_pool_get_buffers(purd->btrp, iov); ret = -E_UDP_OVERRUN; - if (bufsize == 0) + if (iovcnt == 0) goto err; - ret = recv_bin_buffer(purd->fd, buf, bufsize); + ret = para_readv(purd->fd, iov, iovcnt); if (ret == 0) ret = -E_RECV_EOF; if (ret < 0) goto err; packet_size = ret; - if (packet_size >= FEC_EOF_PACKET_LEN) { - if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) { - PARA_INFO_LOG("received eof packet\n"); - ret = -E_RECV_EOF; - goto err; - } + ret = udp_check_eof(packet_size, iov); + if (ret < 0) + goto err; + if (iov[0].iov_len >= packet_size) + btr_add_output_pool(purd->btrp, packet_size, btrn); + else { /* both buffers contain data */ + btr_add_output_pool(purd->btrp, iov[0].iov_len, btrn); + btr_add_output_pool(purd->btrp, packet_size - iov[0].iov_len, + btrn); } - btr_add_output_pool(purd->btrp, packet_size, btrn); return; err: btr_remove_node(btrn); -- 2.39.5