From dbd846880720e05d66d895e871ffc07b8eb7ac23 Mon Sep 17 00:00:00 2001 From: Nickolay Ihalainen Date: Wed, 3 Aug 2011 13:13:27 +0400 Subject: [PATCH 1/3] connect() should respect timeout parameter --- libhsclient/socket.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/libhsclient/socket.cpp b/libhsclient/socket.cpp index d797910..42727ef 100644 --- a/libhsclient/socket.cpp +++ b/libhsclient/socket.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "socket.hpp" #include "string_util.hpp" @@ -127,11 +128,32 @@ socket_connect(auto_file& fd, const socket_args& args, std::string& err_r) if ((r = socket_open(fd, args, err_r)) != 0) { return r; } + + if(!args.nonblocking ) { + if (fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) { + return errno_string("fcntl O_NONBLOCK", errno, err_r); + } + } + + if (connect(fd.get(), reinterpret_cast(&args.addr), args.addrlen) != 0) { - if (!args.nonblocking || errno != EINPROGRESS) { + if (errno != EINPROGRESS) { return errno_string("connect", errno, err_r); } + // disable NONBLOCK and wait for connect or timeout + if(!args.nonblocking) { + struct pollfd fds[1]; + int nfds = 1; + fds[0].fd = fd.get(); + fds[0].events = POLLIN; + if( poll(fds,nfds, 1000*args.timeout) <= 0){ + return errno_string("connect poll", errno, err_r); + } + if (fcntl(fd.get(), F_SETFL, fcntl(fd.get(), F_GETFL) & ~O_NONBLOCK)) { + return errno_string("fcntl ~O_NONBLOCK", errno, err_r); + } + } } return 0; } From 22f541a52920c808eb6f87ff0334e6dd44f1b4d7 Mon Sep 17 00:00:00 2001 From: Nickolay Ihalainen Date: Wed, 3 Aug 2011 13:25:37 +0400 Subject: [PATCH 2/3] fix a typo with fcntl result check --- libhsclient/socket.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libhsclient/socket.cpp b/libhsclient/socket.cpp index 42727ef..0c33638 100644 --- a/libhsclient/socket.cpp +++ b/libhsclient/socket.cpp @@ -150,7 +150,7 @@ socket_connect(auto_file& fd, const socket_args& args, std::string& err_r) if( poll(fds,nfds, 1000*args.timeout) <= 0){ return errno_string("connect poll", errno, err_r); } - if (fcntl(fd.get(), F_SETFL, fcntl(fd.get(), F_GETFL) & ~O_NONBLOCK)) { + if (fcntl(fd.get(), F_SETFL, fcntl(fd.get(), F_GETFL) & ~O_NONBLOCK) != 0) { return errno_string("fcntl ~O_NONBLOCK", errno, err_r); } } From e8895ab8233346dd344490da2d23d6b7a859a60a Mon Sep 17 00:00:00 2001 From: Nickolay Ihalainen Date: Wed, 3 Aug 2011 16:12:25 +0400 Subject: [PATCH 3/3] Add polling for outgoing packets on connect --- libhsclient/socket.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libhsclient/socket.cpp b/libhsclient/socket.cpp index 0c33638..2537650 100644 --- a/libhsclient/socket.cpp +++ b/libhsclient/socket.cpp @@ -146,7 +146,7 @@ socket_connect(auto_file& fd, const socket_args& args, std::string& err_r) struct pollfd fds[1]; int nfds = 1; fds[0].fd = fd.get(); - fds[0].events = POLLIN; + fds[0].events = POLLIN|POLLOUT; if( poll(fds,nfds, 1000*args.timeout) <= 0){ return errno_string("connect poll", errno, err_r); }