Make the http code respect MAX_CONN_TIMEOUT and fail connects once they
authorclaudio <claudio@openbsd.org>
Tue, 9 Aug 2022 09:02:26 +0000 (09:02 +0000)
committerclaudio <claudio@openbsd.org>
Tue, 9 Aug 2022 09:02:26 +0000 (09:02 +0000)
hit this timeout. This is in line with the rsync code.
OK tb@ job@

usr.sbin/rpki-client/extern.h
usr.sbin/rpki-client/http.c
usr.sbin/rpki-client/rsync.c

index 58a5842..e5868ff 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: extern.h,v 1.145 2022/08/08 15:22:31 job Exp $ */
+/*     $OpenBSD: extern.h,v 1.146 2022/08/09 09:02:26 claudio Exp $ */
 /*
  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
  *
@@ -728,7 +728,7 @@ int mkpathat(int, const char *);
 #define MAX_RSYNC_REQUESTS     16
 
 /* How many seconds to wait for a connection to succeed. */
-#define MAX_CONTIMEOUT         15
+#define MAX_CONN_TIMEOUT       15
 
 /* How long to wait for IO from a remote server. */
 #define MAX_IO_TIMEOUT         180
index d55482e..d3263fe 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: http.c,v 1.63 2022/08/08 15:22:31 job Exp $ */
+/*     $OpenBSD: http.c,v 1.64 2022/08/09 09:02:26 claudio Exp $ */
 /*
  * Copyright (c) 2020 Nils Fisher <nils_fisher@hotmail.com>
  * Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
@@ -755,6 +755,19 @@ http_failed(struct http_connection *conn)
        return DONE;
 }
 
+/*
+ * Called in case of connect timeout, try an alternate connection.
+ */
+static enum res
+http_connect_failed(struct http_connection *conn)
+{
+       assert(conn->state == STATE_CONNECT);
+       close(conn->fd);
+       conn->fd = -1;
+
+       return http_connect(conn);
+}
+
 /*
  * Call the function f and update the connection events based
  * on the return value.
@@ -1812,8 +1825,12 @@ proc_http(char *bind_addr, int fd)
                        if (i >= NPFDS)
                                errx(1, "too many connections");
 
-                       if (conn->io_time == 0)
-                               conn->io_time = now + MAX_IO_TIMEOUT;
+                       if (conn->io_time == 0) {
+                               if (conn->state == STATE_CONNECT)
+                                       conn->io_time = now + MAX_CONN_TIMEOUT;
+                               else
+                                       conn->io_time = now + MAX_IO_TIMEOUT;
+                       }
 
                        if (conn->io_time <= now)
                                timeout = 0;
@@ -1901,9 +1918,15 @@ proc_http(char *bind_addr, int fd)
                        if (conn->pfd != NULL && conn->pfd->revents != 0)
                                http_do(conn, http_handle);
                        else if (conn->io_time <= now) {
-                               warnx("%s: timeout, connection closed",
-                                   http_info(conn->host));
-                               http_do(conn, http_failed);
+                               if (conn->state == STATE_CONNECT) {
+                                       warnx("%s: connect timeout",
+                                           http_info(conn->host));
+                                       http_do(conn, http_connect_failed);
+                               } else {
+                                       warnx("%s: timeout, connection closed",
+                                           http_info(conn->host));
+                                       http_do(conn, http_failed);
+                               }
                        }
 
                        if (conn->state == STATE_FREE)
index 5139e51..6fc689d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: rsync.c,v 1.40 2022/08/08 15:22:31 job Exp $ */
+/*     $OpenBSD: rsync.c,v 1.41 2022/08/09 09:02:26 claudio Exp $ */
 /*
  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
  *
@@ -312,7 +312,7 @@ proc_rsync(char *prog, char *bind_addr, int fd)
                        args[i++] = "-rt";
                        args[i++] = "--no-motd";
                        args[i++] = "--max-size=" STRINGIFY(MAX_FILE_SIZE);
-                       args[i++] = "--contimeout=" STRINGIFY(MAX_CONTIMEOUT);
+                       args[i++] = "--contimeout=" STRINGIFY(MAX_CONN_TIMEOUT);
                        args[i++] = "--timeout=" STRINGIFY(MAX_IO_TIMEOUT);
                        args[i++] = "--include=*/";
                        args[i++] = "--include=*.cer";