summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/sockets.h10
-rw-r--r--modules/extra/m_ssl.cpp4
-rw-r--r--src/sockets.cpp32
3 files changed, 46 insertions, 0 deletions
diff --git a/include/sockets.h b/include/sockets.h
index 92279f81f..1bcafd79f 100644
--- a/include/sockets.h
+++ b/include/sockets.h
@@ -92,6 +92,16 @@ class CoreExport Socket : public Flags<SocketFlag, 1>
*/
int GetSock() const;
+ /** Mark a socket as blockig
+ * @return true if the socket is now blocking
+ */
+ bool SetBlocking();
+
+ /** Mark a socket as non-blocking
+ * @return true if the socket is now non-blocking
+ */
+ bool SetNonBlocking();
+
/** Check if this socket is IPv6
* @return true or false
*/
diff --git a/modules/extra/m_ssl.cpp b/modules/extra/m_ssl.cpp
index 0771e6d4c..599e76e8f 100644
--- a/modules/extra/m_ssl.cpp
+++ b/modules/extra/m_ssl.cpp
@@ -31,6 +31,8 @@ class SSLSocket : public ClientSocket
public:
SSLSocket(const std::string &nTargetHost, int nPort, const std::string &nBindHost = "", bool nIPv6 = false) : ClientSocket(nTargetHost, nPort, nBindHost, nIPv6)
{
+ this->SetBlocking();
+
sslsock = SSL_new(ctx);
if (!sslsock)
@@ -41,6 +43,8 @@ class SSLSocket : public ClientSocket
SSL_connect(sslsock);
UplinkSock = this;
+
+ this->SetNonBlocking();
}
~SSLSocket()
diff --git a/src/sockets.cpp b/src/sockets.cpp
index f2e0d65f6..418b1b334 100644
--- a/src/sockets.cpp
+++ b/src/sockets.cpp
@@ -65,6 +65,34 @@ int Socket::GetSock() const
return sock;
}
+/** Mark a socket as blockig
+ * @return true if the socket is now blocking
+ */
+bool Socket::SetBlocking()
+{
+#ifdef _WIN32
+ unsigned long opt = 0;
+ return !ioctlsocket(this->GetSock(), FIONBIO, &opt);
+#else
+ int flags = fcntl(this->GetSock(), F_GETFL, 0);
+ return !fcntl(this->GetSock(), F_SETFL, flags & ~O_NONBLOCK);
+#endif
+}
+
+/** Mark a socket as non-blocking
+ * @return true if the socket is now non-blocking
+ */
+bool Socket::SetNonBlocking()
+{
+#ifdef _WIN32
+ unsigned long opt = 0;
+ return !ioctlsocket(this->GetSock(), FIONBIO, &opt);
+#else
+ int flags = fcntl(this->GetSock(), F_GETFL, 0);
+ return !fcntl(this->GetSock(), F_SETFL, flags | O_NONBLOCK);
+#endif
+}
+
/** Check if this socket is IPv6
* @return true or false
*/
@@ -305,6 +333,8 @@ ClientSocket::ClientSocket(const std::string &nTargetHost, int nPort, const std:
throw SocketException("Error connecting to server: " + std::string(strerror(errno)));
}
}
+
+ this->SetNonBlocking();
}
/** Default destructor
@@ -375,6 +405,8 @@ ListenSocket::ListenSocket(const std::string &bindip, int port) : Socket(0, (bin
{
throw SocketException("Unable to listen: " + std::string(strerror(errno)));
}
+
+ this->SetNonBlocking();
}
/** Destructor