[Libreoffice-commits] online.git: Branch 'private/Ashod/nonblocking' - 3 commits - net/clientnb.cpp net/loolnb.cpp net/socket.hpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Sat Feb 18 01:44:56 UTC 2017


 net/clientnb.cpp |   45 ++++++++++++++++++++++++++---------------
 net/loolnb.cpp   |    2 -
 net/socket.hpp   |   59 +++++++++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 82 insertions(+), 24 deletions(-)

New commits:
commit 4289058cbe741919cd492eb5fcbf28b87bb32dcb
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Feb 17 20:41:27 2017 -0500

    nb: enable HTTPS in server
    
    Change-Id: Ib0a4cb29f239bafe477ffab4194d3c91a588c384

diff --git a/net/loolnb.cpp b/net/loolnb.cpp
index b91e4d2..2a0b6d1 100644
--- a/net/loolnb.cpp
+++ b/net/loolnb.cpp
@@ -415,7 +415,7 @@ int main(int, const char**)
     });
 
     // Start the server.
-    server<SimpleResponseClient<StreamSocket>>(addrHttp, poller);
+    server<SimpleResponseClient<SslStreamSocket>>(addrSsl, poller);
 
     std::cout << "Shutting down server." << std::endl;
 
commit 049d43c3fe3ff02385d9aaace76577d738153af3
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Feb 17 20:41:09 2017 -0500

    nb: support ssl handshake
    
    Change-Id: I9974b1228cdc35a22ee784b734811720ddd2a311

diff --git a/net/socket.hpp b/net/socket.hpp
index 95d4991..fbc9003 100644
--- a/net/socket.hpp
+++ b/net/socket.hpp
@@ -388,6 +388,27 @@ class SslStreamSocket : public BufferingSocket
 public:
     bool readIncomingData() override
     {
+        if (_doHandshake)
+        {
+            int rc;
+            do
+            {
+                rc = SSL_do_handshake(_ssl);
+            }
+            while (rc < 0 && errno == EINTR);
+
+            if (rc <= 0)
+            {
+                rc = handleSslState(rc);
+                if (rc <= 0)
+                {
+                    return (rc != 0);
+                }
+            }
+
+            _doHandshake = false;
+        }
+
         ssize_t len;
         char buf[4096];
         do
@@ -397,7 +418,6 @@ public:
         while (len < 0 && errno == EINTR);
 
         len = handleSslState(len);
-
         if (len > 0)
         {
             // We have more data, let the application consume it, if possible.
@@ -414,6 +434,28 @@ public:
     {
         // Should never call SSL_write with 0 length data.
         assert (_outBuffer.size() > 0);
+
+        if (_doHandshake)
+        {
+            int rc;
+            do
+            {
+                rc = SSL_do_handshake(_ssl);
+            }
+            while (rc < 0 && errno == EINTR);
+
+            if (rc <= 0)
+            {
+                rc = handleSslState(rc);
+                if (rc <= 0)
+                {
+                    return;
+                }
+            }
+
+            _doHandshake = false;
+        }
+
         ssize_t len;
         do
         {
@@ -422,7 +464,6 @@ public:
         while (len < 0 && errno == EINTR);
 
         len = handleSslState(len);
-
         if (len > 0)
         {
             // We've sent some data, remove from the buffer.
@@ -453,7 +494,8 @@ protected:
     SslStreamSocket(const int fd) :
         BufferingSocket(fd),
         _ssl(nullptr),
-        _sslWantsTo(SslWantsTo::ReadOrWrite)
+        _sslWantsTo(SslWantsTo::ReadOrWrite),
+        _doHandshake(true)
     {
         BIO* bio = BIO_new(BIO_s_socket());
         if (bio == nullptr)
@@ -533,8 +575,8 @@ private:
         default:
             {
                 // The error is comming from BIO. Find out what happened.
-                const long lastError = ERR_get_error();
-                if (lastError == 0)
+                const long bioError = ERR_get_error();
+                if (bioError == 0)
                 {
                     if (rc == 0)
                     {
@@ -553,7 +595,7 @@ private:
                 else
                 {
                     char buf[512];
-                    ERR_error_string_n(lastError, buf, sizeof(buf));
+                    ERR_error_string_n(bioError, buf, sizeof(buf));
                     throw std::runtime_error(buf);
                 }
             }
@@ -565,7 +607,12 @@ private:
 
 private:
     SSL* _ssl;
+    /// During handshake SSL might want to read
+    /// on write, or write on read.
     SslWantsTo _sslWantsTo;
+    /// We must do the handshake during the first
+    /// read or write in non-blocking.
+    bool _doHandshake;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
commit 095623915a8b62101d8127e125c93cc8e0eaecc2
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Feb 17 20:40:17 2017 -0500

    nb: support ssl in all client tests
    
    Change-Id: Ie077212426a07817914abe796c1280ef2afe89be

diff --git a/net/clientnb.cpp b/net/clientnb.cpp
index 2dc7950..275938d 100644
--- a/net/clientnb.cpp
+++ b/net/clientnb.cpp
@@ -53,6 +53,8 @@ const char *HostName = "127.0.0.1";
 constexpr int HttpPortNumber = 9191;
 constexpr int SslPortNumber = 9193;
 
+static bool EnableHttps = false;
+
 struct Session
 {
     std::string _session_name;
@@ -132,7 +134,7 @@ struct ThreadWorker : public Runnable
     {
         for (int i = 0; i < 100; ++i)
         {
-            Session ping(_domain ? _domain : "init");
+            Session ping(_domain ? _domain : "init", EnableHttps);
             ping.sendPing(i);
             int back = ping.getResponse();
             assert(back == i + 1);
@@ -142,8 +144,28 @@ struct ThreadWorker : public Runnable
 
 struct Client : public Poco::Util::Application
 {
+    void testPing()
+    {
+        std::cerr << "testPing\n";
+        Session first("init", EnableHttps);
+        Session second("init", EnableHttps);
+
+        int count = 42, back;
+        first.sendPing(count);
+        second.sendPing(count + 1);
+
+        back = first.getResponse();
+        std::cerr << "testPing: " << back << "\n";
+        assert (back == count + 1);
+
+        back = second.getResponse();
+        std::cerr << "testPing: " << back << "\n";
+        assert (back == count + 2);
+    }
+
     void testLadder()
     {
+        std::cerr << "testLadder\n";
         ThreadWorker ladder;
         Thread thread;
         thread.start(ladder);
@@ -152,6 +174,7 @@ struct Client : public Poco::Util::Application
 
     void testParallel()
     {
+        std::cerr << "testParallel\n";
         const int num = 10;
         Thread snakes[num];
         ThreadWorker ladders[num];
@@ -165,7 +188,7 @@ struct Client : public Poco::Util::Application
 
     void testWebsocket()
     {
-        Session session("ws");
+        Session session("ws", EnableHttps);
         std::shared_ptr<WebSocket> ws = session.getWebSocket();
 
         std::string send = "hello there";
@@ -186,24 +209,12 @@ struct Client : public Poco::Util::Application
 public:
     int main(const std::vector<std::string>& args) override
     {
-        const bool https = (args.size() > 0 && args[0] == "ssl");
-        std::cerr << "Starting " << (https ? "HTTPS" : "HTTP") << " client." << std::endl;
+        EnableHttps = (args.size() > 0 && args[0] == "ssl");
+        std::cerr << "Starting " << (EnableHttps ? "HTTPS" : "HTTP") << " client." << std::endl;
 
         testWebsocket();
 
-        Session first("init");
-        Session second("init");
-
-        int count = 42, back;
-        first.sendPing(count);
-        second.sendPing(count + 1);
-
-        back = first.getResponse();
-        assert (back == count + 1);
-
-        back = second.getResponse();
-        assert (back == count + 2);
-
+        testPing();
         testLadder();
         testParallel();
 


More information about the Libreoffice-commits mailing list