]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
set TCP_NODELAY unconditionally on TCP/TLS sockets
authorDaniel Lenski <dlenski@gmail.com>
Tue, 21 Apr 2020 20:03:42 +0000 (13:03 -0700)
committerDaniel Lenski <dlenski@gmail.com>
Tue, 21 Apr 2020 20:46:17 +0000 (13:46 -0700)
This replaces 67162301, where I tried to only set `TCP_NODELAY` when using
TLS for the tunnel transport.

See https://gitlab.com/openconnect/openconnect/-/merge_requests/89#note_328398311
for why setting it unconditionally is probably the best choice for openconnect.

Signed-off-by: Daniel Lenski <dlenski@gmail.com>
ssl.c

diff --git a/ssl.c b/ssl.c
index 181399c46c8baf2c8f10a107ce768be5c68463ab..7d277c3e01d9f640ed9c302bd8aaabb0476ef504 100644 (file)
--- a/ssl.c
+++ b/ssl.c
 #include <sys/statfs.h>
 #endif
 
+/* setsockopt and TCP_NODELAY */
+#ifndef _WIN32
+#include <netinet/tcp.h>
+#include <sys/socket.h>
+#endif
+
 #include "openconnect-internal.h"
 
 #ifdef ANDROID_KEYSTORE
@@ -180,6 +186,22 @@ static int match_sockaddr(struct sockaddr *a, struct sockaddr *b)
                return 0;
 }
 
+static int set_tcp_nodelay(struct openconnect_info *vpninfo, int ssl_sock)
+{
+       int flag = 1;
+       if (setsockopt(ssl_sock, IPPROTO_TCP, TCP_NODELAY, (void *)(&flag), sizeof(flag)) < 0) {;
+               vpn_perror(vpninfo,
+                          _("Failed setsockopt(TCP_NODELAY) on TLS socket:"));
+#ifdef _WIN32
+               return WSAGetLastError();
+#else
+               return -errno;
+#endif
+       }
+       return 0;
+}
+
+
 int connect_https_socket(struct openconnect_info *vpninfo)
 {
        int ssl_sock = -1;
@@ -210,6 +232,7 @@ int connect_https_socket(struct openconnect_info *vpninfo)
                        }
                        set_fd_cloexec(ssl_sock);
                }
+               set_tcp_nodelay(vpninfo, ssl_sock);
                err = cancellable_connect(vpninfo, ssl_sock, vpninfo->peer_addr, vpninfo->peer_addrlen);
                if (err) {
                        char *errstr;
@@ -358,6 +381,7 @@ int connect_https_socket(struct openconnect_info *vpninfo)
                        if (ssl_sock < 0)
                                continue;
                        set_fd_cloexec(ssl_sock);
+                       set_tcp_nodelay(vpninfo, ssl_sock);
                        err = cancellable_connect(vpninfo, ssl_sock, rp->ai_addr, rp->ai_addrlen);
                        if (!err) {
                                /* Store the peer address we actually used, so that DTLS can