[tor-commits] [torsocks/master] Block, rather than busy-wait, in send/recv_data_impl.

dgoulet at torproject.org dgoulet at torproject.org
Mon Jun 13 21:14:03 UTC 2016


commit 6e36f49c5f08cc96b4c8469e245c28c3b58c23bb
Author: Taylor R Campbell <campbell+torsocks at mumble.net>
Date:   Wed Jun 17 17:39:02 2015 +0000

    Block, rather than busy-wait, in send/recv_data_impl.
---
 src/common/socks5.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/common/socks5.c b/src/common/socks5.c
index 3f284ca..f56d69c 100644
--- a/src/common/socks5.c
+++ b/src/common/socks5.c
@@ -49,9 +49,13 @@ static ssize_t recv_data_impl(int fd, void *buf, size_t len)
 				/* Try again after interruption. */
 				continue;
 			} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
-				if (index) {
-					/* Return the number of bytes received up to this point. */
-					ret = index;
+				/* Wait for data to become available */
+				fd_set readfds;
+				FD_ZERO(&readfds);
+				FD_SET(fd, &readfds);
+				if (select(fd + 1, &readfds, NULL, NULL, NULL) < 0) {
+					ret = -errno;
+					goto error;
 				}
 				continue;
 			} else if (read_len == 0) {
@@ -102,9 +106,13 @@ static ssize_t send_data_impl(int fd, const void *buf, size_t len)
 				/* Send again after interruption. */
 				continue;
 			} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
-				if (index) {
-					/* Return the number of bytes sent up to this point. */
-					ret = index;
+				/* Wait for buffer space to become available */
+				fd_set writefds;
+				FD_ZERO(&writefds);
+				FD_SET(fd, &writefds);
+				if (select(fd + 1, NULL, &writefds, NULL, NULL) < 0) {
+					ret = -errno;
+					goto error;
 				}
 				continue;
 			} else {





More information about the tor-commits mailing list