Skip to content
This repository has been archived by the owner on Apr 6, 2019. It is now read-only.

Commit

Permalink
make sure socket is in blocking mode before connection (#32) and chec…
Browse files Browse the repository at this point in the history
…k for async-connect errors with getsockopt
  • Loading branch information
Cylix committed Nov 13, 2017
1 parent df072eb commit c6e5399
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions sources/network/unix/unix_tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ tcp_socket::connect(const std::string& host, std::uint32_t port, std::uint32_t t
__TACOPIE_THROW(error, "connect() set non-blocking failure");
}
}
else {
//! For no timeout case, still make sure that the socket is in blocking mode
//! As reported in #32, this might not be the case on some OS
if (fcntl(m_fd, F_SETFL, fcntl(m_fd, F_GETFL, 0) & (~O_NONBLOCK)) == -1) {
close();
__TACOPIE_THROW(error, "connect() set blocking failure");
}
}

int ret = ::connect(m_fd, server_addr, addr_len);
if (ret < 0 && errno != EINPROGRESS) {
Expand All @@ -108,6 +116,14 @@ tcp_socket::connect(const std::string& host, std::uint32_t port, std::uint32_t t
//! 1 means we are connected.
//! 0/-1 means a timeout.
if (select(m_fd + 1, NULL, &set, NULL, &tv) == 1) {
//! Make sure there are no async connection errors
int err = 0;
socklen_t len = sizeof(len);
if (getsockopt(m_fd, SOL_SOCKET, SO_ERROR, &err, &len) == -1 || err != 0) {
close();
__TACOPIE_THROW(error, "connect() failure");
}

//! Set back to blocking mode as the user of this class is expecting
if (fcntl(m_fd, F_SETFL, fcntl(m_fd, F_GETFL, 0) & (~O_NONBLOCK)) == -1) {
close();
Expand Down
17 changes: 17 additions & 0 deletions sources/network/windows/windows_tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ tcp_socket::connect(const std::string& host, std::uint32_t port, std::uint32_t t
__TACOPIE_THROW(error, "connect() set non-blocking failure");
}
}
else {
//! For no timeout case, still make sure that the socket is in blocking mode
//! As reported in #32, this might not be the case on some OS
u_long mode = 0;
if (ioctlsocket(m_fd, FIONBIO, &mode) != 0) {
close();
__TACOPIE_THROW(error, "connect() set blocking failure");
}
}

int ret = ::connect(m_fd, (const struct sockaddr*) &server_addr, sizeof(server_addr));
if (ret == -1 && WSAGetLastError() != WSAEWOULDBLOCK) {
Expand All @@ -89,6 +98,14 @@ tcp_socket::connect(const std::string& host, std::uint32_t port, std::uint32_t t
//! 1 means we are connected.
//! 0 means a timeout.
if (select(static_cast<int>(m_fd) + 1, NULL, &set, NULL, &tv) == 1) {
//! Make sure there are no async connection errors
int err = 0;
int len = sizeof(len);
if (getsockopt(m_fd, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&err), &len) == -1 || err != 0) {
close();
__TACOPIE_THROW(error, "connect() failure");
}

//! Set back to blocking mode as the user of this class is expecting
u_long mode = 0;
if (ioctlsocket(m_fd, FIONBIO, &mode) != 0) {
Expand Down

0 comments on commit c6e5399

Please sign in to comment.