Changeset: 3dfbde27efd4 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/3dfbde27efd4
Modified Files:
        clients/mapilib/connect.c
Branch: monetdburl
Log Message:

Always send the leader bytes

It also helps (depending on the server) to force a quick error when
connecting to an https server

We will actually use this in a test.


diffs (69 lines):

diff --git a/clients/mapilib/connect.c b/clients/mapilib/connect.c
--- a/clients/mapilib/connect.c
+++ b/clients/mapilib/connect.c
@@ -181,7 +181,6 @@ wrap_socket(Mapi mid, SOCKET sock)
        msg = mapi_set_streams(mid, rstream, wstream);
        if (msg != MOK)
                goto bailout;
-       mapi_log_record(mid, "CONN", "Network connection established");
        return MOK;
 
 bailout:
@@ -271,28 +270,37 @@ connect_socket_tcp(Mapi mid)
                return mapi_setError(mid, "connected to self", __func__, 
MERROR);
        }
 
-       if (use_tls) {
-               mapi_log_record(mid, "CONN", "Network connection established");
-               return wrap_tls(mid, s);
-       } else {
-               // Some TLS servers hang if we accidentally make a non-TLS
-               // connection to a TLS server. Sending a bunch of NULs causes
-               // some of them to close the connection instead of hanging.
-               // Also, surprisingly, it seems to make connection setup
-               // slightly faster with non-TLS servers.
-               static const char zeroes[8] = { 0 };
-               ssize_t to_write = sizeof(zeroes);
-               while (to_write > 0) {
-                       ssize_t n = send(s, zeroes, (int)to_write, 0);
-                       if (n < 0) {
-                               closesocket(s);
-                               return mapi_printError(mid, __func__, MERROR, 
"could not send leader block: %s", SOCKET_STRERROR());
-                       }
-                       to_write -= (size_t)n;
+       mapi_log_record(mid, "CONN", "Network connection established");
+       MapiMsg msg = use_tls ? wrap_tls(mid, s) : wrap_socket(mid, s);
+       if (msg != MOK)
+               return msg;
+
+       // Send some NUL bytes. If we're accidentally connecting to the wrong
+       // port or protocol this may cause the remote server to close the
+       // connection. If we don't do this, the connection will often hang
+       // because the server expects us to speak first and we expect the server
+       // to speak first.
+       //
+       // Note that a pair of NUL bytes is a no-op message in MAPI.
+       //
+       // Surprisingly, it seems sending these NUL bytes makes non-TLS
+       // connection setup a little faster rather than slower!
+       static const char zeroes[8] = { 0 };
+       ssize_t to_write = sizeof(zeroes);
+       while (to_write > 0) {
+               ssize_t n = mnstr_write(mid->to, zeroes, 1, to_write);
+               if (n < 0) {
+                       close_connection(mid);
+                       return mapi_printError(mid, __func__, MERROR, "could 
not send leader block: %s", mnstr_peek_error(mid->to));
                }
-               return wrap_socket(mid, s);
+               to_write -= (size_t)n;
        }
-       assert(0 && "unreachable");
+       if (mnstr_flush(mid->to, MNSTR_FLUSH_DATA) != 0) {
+               close_connection(mid);
+               return mapi_printError(mid, __func__, MERROR, "could not flush 
leader block: %s", mnstr_peek_error(mid->to));
+       }
+
+       return msg;
 }
 
 static SOCKET
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to