]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
m_ssl_gnutls, m_ssl_openssl Deduplicate Handshake() calling code
authorAttila Molnar <attilamolnar@hush.com>
Thu, 5 Mar 2015 16:12:34 +0000 (17:12 +0100)
committerAttila Molnar <attilamolnar@hush.com>
Thu, 5 Mar 2015 16:12:34 +0000 (17:12 +0100)
src/modules/extra/m_ssl_gnutls.cpp
src/modules/extra/m_ssl_openssl.cpp

index 30b54ff8be86609eda7933917206f9c724e10a12..f8dc85659b1a82c927faef5d3f691c74ab968047 100644 (file)
@@ -778,6 +778,22 @@ info_done_dealloc:
                gnutls_x509_crt_deinit(cert);
        }
 
+       // Returns 1 if application I/O should proceed, 0 if it must wait for the underlying protocol to progress, -1 on fatal error
+       int PrepareIO(StreamSocket* sock)
+       {
+               if (status == ISSL_HANDSHAKEN)
+                       return 1;
+               else if (status == ISSL_HANDSHAKING)
+               {
+                       // The handshake isn't finished, try to finish it
+                       return Handshake(sock);
+               }
+
+               CloseSession();
+               sock->SetError("No SSL session");
+               return -1;
+       }
+
        static const char* UnknownIfNULL(const char* str)
        {
                return str ? str : "UNKNOWN";
@@ -874,20 +890,10 @@ info_done_dealloc:
 
        int OnStreamSocketRead(StreamSocket* user, std::string& recvq) CXX11_OVERRIDE
        {
-               if (!this->sess)
-               {
-                       CloseSession();
-                       user->SetError("No SSL session");
-                       return -1;
-               }
-
-               if (this->status == ISSL_HANDSHAKING)
-               {
-                       // The handshake isn't finished, try to finish it.
-                       int ret = Handshake(user);
-                       if (ret <= 0)
-                               return ret;
-               }
+               // Finish handshake if needed
+               int prepret = PrepareIO(user);
+               if (prepret <= 0)
+                       return prepret;
 
                // If we resumed the handshake then this->status will be ISSL_HANDSHAKEN.
                {
@@ -919,20 +925,10 @@ info_done_dealloc:
 
        int OnStreamSocketWrite(StreamSocket* user, std::string& sendq) CXX11_OVERRIDE
        {
-               if (!this->sess)
-               {
-                       CloseSession();
-                       user->SetError("No SSL session");
-                       return -1;
-               }
-
-               if (this->status == ISSL_HANDSHAKING)
-               {
-                       // The handshake isn't finished, try to finish it.
-                       int ret = Handshake(user);
-                       if (ret <= 0)
-                               return ret;
-               }
+               // Finish handshake if needed
+               int prepret = PrepareIO(user);
+               if (prepret <= 0)
+                       return prepret;
 
                // Session is ready for transferring application data
                int ret = 0;
index c0ab862d24c20d4a8609ba268653e2def167e577..8540ab41f3d87ab8028de00199c9f3840484fc31 100644 (file)
@@ -502,6 +502,21 @@ class OpenSSLIOHook : public SSLIOHook
        }
 #endif
 
+       // Returns 1 if application I/O should proceed, 0 if it must wait for the underlying protocol to progress, -1 on fatal error
+       int PrepareIO(StreamSocket* sock)
+       {
+               if (status == ISSL_OPEN)
+                       return 1;
+               else if (status == ISSL_HANDSHAKING)
+               {
+                       // The handshake isn't finished, try to finish it
+                       return Handshake(sock);
+               }
+
+               CloseSession();
+               return -1;
+       }
+
        // Calls our private SSLInfoCallback()
        friend void StaticSSLInfoCallback(const SSL* ssl, int where, int rc);
 
@@ -531,19 +546,10 @@ class OpenSSLIOHook : public SSLIOHook
 
        int OnStreamSocketRead(StreamSocket* user, std::string& recvq) CXX11_OVERRIDE
        {
-               if (!sess)
-               {
-                       CloseSession();
-                       return -1;
-               }
-
-               if (status == ISSL_HANDSHAKING)
-               {
-                       // The handshake isn't finished and it wants to read, try to finish it.
-                       int ret = Handshake(user);
-                       if (ret <= 0)
-                               return ret;
-               }
+               // Finish handshake if needed
+               int prepret = PrepareIO(user);
+               if (prepret <= 0)
+                       return prepret;
 
                // If we resumed the handshake then this->status will be ISSL_OPEN
                {
@@ -596,21 +602,13 @@ class OpenSSLIOHook : public SSLIOHook
 
        int OnStreamSocketWrite(StreamSocket* user, std::string& buffer) CXX11_OVERRIDE
        {
-               if (!sess)
-               {
-                       CloseSession();
-                       return -1;
-               }
+               // Finish handshake if needed
+               int prepret = PrepareIO(user);
+               if (prepret <= 0)
+                       return prepret;
 
                data_to_write = true;
 
-               if (status == ISSL_HANDSHAKING)
-               {
-                       int ret = Handshake(user);
-                       if (ret <= 0)
-                               return ret;
-               }
-
                // Session is ready for transferring application data
                {
                        ERR_clear_error();