]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Split out cancellable recv/send/gets functions from proxy code
authorDavid Woodhouse <dwmw2@infradead.org>
Fri, 4 Jan 2019 12:24:04 +0000 (12:24 +0000)
committerDavid Woodhouse <dwmw2@infradead.org>
Fri, 4 Jan 2019 12:44:18 +0000 (12:44 +0000)
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
http.c
openconnect-internal.h
ssl.c

diff --git a/http.c b/http.c
index d1941247bd30d46c49a3eaadb74975b45feb6028..fa3279b186cd0acd249270d0add84f4dbc157d79 100644 (file)
--- a/http.c
+++ b/http.c
@@ -1005,101 +1005,17 @@ char *openconnect_create_useragent(const char *base)
 
 static int proxy_gets(struct openconnect_info *vpninfo, char *buf, size_t len)
 {
-       int i = 0;
-       int ret;
-
-       if (len < 2)
-               return -EINVAL;
-
-       while ((ret = proxy_read(vpninfo, (void *)(buf + i), 1)) == 1) {
-               if (buf[i] == '\n') {
-                       buf[i] = 0;
-                       if (i && buf[i-1] == '\r') {
-                               buf[i-1] = 0;
-                               i--;
-                       }
-                       return i;
-               }
-               i++;
-
-               if (i >= len - 1) {
-                       buf[i] = 0;
-                       return i;
-               }
-       }
-       buf[i] = 0;
-       return i ?: ret;
+       return cancellable_gets(vpninfo, vpninfo->proxy_fd, buf, len);
 }
 
 static int proxy_write(struct openconnect_info *vpninfo, char *buf, size_t len)
 {
-       size_t count;
-       int fd = vpninfo->proxy_fd;
-
-       if (fd == -1)
-               return -EINVAL;
-
-       for (count = 0; count < len; ) {
-               fd_set rd_set, wr_set;
-               int maxfd = fd;
-               int i;
-
-               FD_ZERO(&wr_set);
-               FD_ZERO(&rd_set);
-               FD_SET(fd, &wr_set);
-               cmd_fd_set(vpninfo, &rd_set, &maxfd);
-
-               select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
-               if (is_cancel_pending(vpninfo, &rd_set))
-                       return -EINTR;
-
-               /* Not that this should ever be able to happen... */
-               if (!FD_ISSET(fd, &wr_set))
-                       continue;
-
-               i = send(fd, (void *)&buf[count], len - count, 0);
-               if (i < 0)
-                       return -errno;
-
-               count += i;
-       }
-       return count;
+       return cancellable_send(vpninfo, vpninfo->proxy_fd, buf, len);
 }
 
 static int proxy_read(struct openconnect_info *vpninfo, char *buf, size_t len)
 {
-       size_t count;
-       int fd = vpninfo->proxy_fd;
-
-       if (fd == -1)
-               return -EINVAL;
-
-       for (count = 0; count < len; ) {
-               fd_set rd_set;
-               int maxfd = fd;
-               int i;
-
-               FD_ZERO(&rd_set);
-               FD_SET(fd, &rd_set);
-               cmd_fd_set(vpninfo, &rd_set, &maxfd);
-
-               select(maxfd + 1, &rd_set, NULL, NULL, NULL);
-               if (is_cancel_pending(vpninfo, &rd_set))
-                       return -EINTR;
-
-               /* Not that this should ever be able to happen... */
-               if (!FD_ISSET(fd, &rd_set))
-                       continue;
-
-               i = recv(fd, (void *)&buf[count], len - count, 0);
-               if (i < 0)
-                       return -errno;
-               else if (i == 0)
-                       return -ECONNRESET;
-
-               count += i;
-       }
-       return count;
+       return cancellable_recv(vpninfo, vpninfo->proxy_fd, buf, len);
 }
 
 static const char *socks_errors[] = {
index e823cd5a3dbbeb6da63f4cc716b2b55584db70b2..43bbeea0e14b0041f551c427366ec221d54aaaf7 100644 (file)
@@ -908,7 +908,13 @@ int udp_sockaddr(struct openconnect_info *vpninfo, int port);
 int udp_connect(struct openconnect_info *vpninfo);
 int ssl_reconnect(struct openconnect_info *vpninfo);
 void openconnect_clear_cookies(struct openconnect_info *vpninfo);
+int cancellable_gets(struct openconnect_info *vpninfo, int fd,
+                    char *buf, size_t len);
 
+int cancellable_send(struct openconnect_info *vpninfo, int fd,
+                    char *buf, size_t len);
+int cancellable_recv(struct openconnect_info *vpninfo, int fd,
+                    char *buf, size_t len);
 /* openssl-pkcs11.c */
 int load_pkcs11_key(struct openconnect_info *vpninfo);
 int load_pkcs11_certificate(struct openconnect_info *vpninfo);
diff --git a/ssl.c b/ssl.c
index c3a9a77570235927bf2bb741fd1a5336367db2e8..6bcb5e0a1efcfc65f6818f7e45e89c19a1d6e5de 100644 (file)
--- a/ssl.c
+++ b/ssl.c
@@ -1038,3 +1038,104 @@ int ssl_reconnect(struct openconnect_info *vpninfo)
 
        return 0;
 }
+
+int cancellable_gets(struct openconnect_info *vpninfo, int fd,
+                    char *buf, size_t len)
+{
+       int i = 0;
+       int ret;
+
+       if (len < 2)
+               return -EINVAL;
+
+       while ((ret = cancellable_recv(vpninfo, fd, (void *)(buf + i), 1)) == 1) {
+               if (buf[i] == '\n') {
+                       buf[i] = 0;
+                       if (i && buf[i-1] == '\r') {
+                               buf[i-1] = 0;
+                               i--;
+                       }
+                       return i;
+               }
+               i++;
+
+               if (i >= len - 1) {
+                       buf[i] = 0;
+                       return i;
+               }
+       }
+       buf[i] = 0;
+       return i ?: ret;
+}
+
+int cancellable_send(struct openconnect_info *vpninfo, int fd,
+                    char *buf, size_t len)
+{
+       size_t count;
+
+       if (fd == -1)
+               return -EINVAL;
+
+       for (count = 0; count < len; ) {
+               fd_set rd_set, wr_set;
+               int maxfd = fd;
+               int i;
+
+               FD_ZERO(&wr_set);
+               FD_ZERO(&rd_set);
+               FD_SET(fd, &wr_set);
+               cmd_fd_set(vpninfo, &rd_set, &maxfd);
+
+               select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
+               if (is_cancel_pending(vpninfo, &rd_set))
+                       return -EINTR;
+
+               /* Not that this should ever be able to happen... */
+               if (!FD_ISSET(fd, &wr_set))
+                       continue;
+
+               i = send(fd, (void *)&buf[count], len - count, 0);
+               if (i < 0)
+                       return -errno;
+
+               count += i;
+       }
+       return count;
+}
+
+
+int cancellable_recv(struct openconnect_info *vpninfo, int fd,
+                    char *buf, size_t len)
+{
+       size_t count;
+
+       if (fd == -1)
+               return -EINVAL;
+
+       for (count = 0; count < len; ) {
+               fd_set rd_set;
+               int maxfd = fd;
+               int i;
+
+               FD_ZERO(&rd_set);
+               FD_SET(fd, &rd_set);
+               cmd_fd_set(vpninfo, &rd_set, &maxfd);
+
+               select(maxfd + 1, &rd_set, NULL, NULL, NULL);
+               if (is_cancel_pending(vpninfo, &rd_set))
+                       return -EINTR;
+
+               /* Not that this should ever be able to happen... */
+               if (!FD_ISSET(fd, &rd_set))
+                       continue;
+
+               i = recv(fd, (void *)&buf[count], len - count, 0);
+               if (i < 0)
+                       return -errno;
+               else if (i == 0)
+                       return -ECONNRESET;
+
+               count += i;
+       }
+       return count;
+}