Page MenuHomeFreeBSD

D61994.1795949443.diff
No OneTemporary

Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None

D61994.1795949443.diff

diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c
--- a/lib/libfetch/common.c
+++ b/lib/libfetch/common.c
@@ -591,18 +591,65 @@
return ((mstime_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}
+/*
+ * Interval between the start of two connection attempts to successive
+ * addresses while the earlier ones have not answered yet (RFC 8305, 5).
+ */
+#define CONNECT_DELAY_MS 250
+
+/*
+ * Order the addresses to connect to: keep the resolver's choice for the
+ * first address, then alternate between its address family and the others
+ * so that a family which is configured but not routed cannot hold up the
+ * connection (RFC 8305, 4). Returns the number of addresses, or -1.
+ */
+static int
+fetch_sort_addrs(struct addrinfo *ais, struct addrinfo ***sorted)
+{
+ struct addrinfo **addrs, *ai, *pref, *other;
+ int family, i, n;
+
+ for (n = 0, ai = ais; ai != NULL; ai = ai->ai_next)
+ n++;
+ if ((addrs = calloc(n, sizeof(*addrs))) == NULL)
+ return (-1);
+ family = ais->ai_family;
+ pref = other = ais;
+ for (i = 0; i < n; i++) {
+ while (pref != NULL && pref->ai_family != family)
+ pref = pref->ai_next;
+ while (other != NULL && other->ai_family == family)
+ other = other->ai_next;
+ if (pref != NULL && (other == NULL || i % 2 == 0)) {
+ addrs[i] = pref;
+ pref = pref->ai_next;
+ } else {
+ addrs[i] = other;
+ other = other->ai_next;
+ }
+ }
+ *sorted = addrs;
+ return (n);
+}
+
/*
* Establish a TCP connection to the specified port on the specified host.
+ * The attempts to the host's addresses overlap as RFC 8305 describes, and
+ * fetchTimeout bounds them all together.
*/
conn_t *
fetch_connect(const char *host, int port, int af, int verbose)
{
- struct pollfd pfd;
+ struct pollfd *pfds = NULL;
+ mstime_t *expires = NULL;
struct addrinfo *cais = NULL, *sais = NULL, *cai, *sai;
+ struct addrinfo **addrs = NULL;
const char *bindaddr;
conn_t *conn = NULL;
- mstime_t now, deadline, deltams;
- int err = 0, serrno, sd = -1;
+ socklen_t len;
+ mstime_t deadline, limit, nextstart, now;
+ int err, i, naddrs, next = 0, npend, pollto, sd = -1, serrno;
+ int starved = 0;
char *sockshost;
int socksport;
@@ -640,95 +687,173 @@
}
}
- /* try each server address in turn */
- for (err = 0, sai = sais; sai != NULL; sai = sai->ai_next) {
- /* start the clock */
- if (fetchTimeout > 0)
- deadline = mstime_mono() + fetchTimeout * 1000;
+ if ((naddrs = fetch_sort_addrs(sais, &addrs)) < 0 ||
+ (pfds = calloc(naddrs, sizeof(*pfds))) == NULL ||
+ (expires = calloc(naddrs, sizeof(*expires))) == NULL)
+ goto syserr;
- /* open socket */
- if ((sd = socket(sai->ai_family, SOCK_STREAM, 0)) < 0) {
- err = -1;
- if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)
- continue;
- goto syserr;
- }
+ /* start the clock */
+ now = nextstart = mstime_mono();
+ if (fetchTimeout > 0)
+ deadline = now + fetchTimeout * 1000;
- /* attempt to bind to client address */
- for (err = 0, cai = cais; cai != NULL; cai = cai->ai_next) {
- if (cai->ai_family != sai->ai_family)
- continue;
- if ((err = bind(sd, cai->ai_addr, cai->ai_addrlen)) == 0)
- break;
+ /*
+ * Try the server addresses in turn, without waiting for an attempt
+ * that does not answer promptly. pfds[i] is the attempt to addrs[i],
+ * with fd -1 once it is over, and expires[i] when it times out on its
+ * own; serrno is why the last one failed.
+ */
+ npend = 0;
+ serrno = ECONNREFUSED;
+ for (;;) {
+ /* check the clock */
+ if (fetchTimeout > 0 && !starved && now >= deadline) {
+ errno = ETIMEDOUT;
+ break;
}
- if (err != 0) {
- fetch_verbose("failed to bind to %s", bindaddr);
- goto syserr;
+ if (starved && fetchTimeout > 0) {
+ /* each attempt times out on its own */
+ for (i = 0; i < next; i++) {
+ if (pfds[i].fd >= 0 && now >= expires[i]) {
+ close(pfds[i].fd);
+ pfds[i].fd = -1;
+ npend--;
+ serrno = ETIMEDOUT;
+ }
+ }
}
- /* make the socket non-blocking */
- (void)fcntl(sd, F_SETFL, O_NONBLOCK);
+ /* start the next attempt when due, or when none is pending */
+ while (next < naddrs &&
+ (npend == 0 || (!starved && now >= nextstart))) {
+ i = next;
+ sai = addrs[i];
+ pfds[i].fd = -1;
+ pfds[i].events = POLLOUT;
+
+ /* open socket; others in flight may still succeed */
+ if ((sd = socket(sai->ai_family, SOCK_STREAM, 0)) < 0) {
+ serrno = errno;
+ if (npend > 0 &&
+ (errno == EMFILE || errno == ENFILE)) {
+ /*
+ * Out of descriptors: keep this
+ * address until no attempt is left,
+ * and from now on give each attempt
+ * a timeout of its own, as when
+ * trying them in turn.
+ */
+ starved = 1;
+ break;
+ }
+ next++;
+ continue;
+ }
+ next++;
+
+ /* attempt to bind to client address */
+ for (err = 0, cai = cais; cai != NULL;
+ cai = cai->ai_next) {
+ if (cai->ai_family != sai->ai_family)
+ continue;
+ if ((err = bind(sd, cai->ai_addr,
+ cai->ai_addrlen)) == 0)
+ break;
+ }
+ if (err != 0) {
+ fetch_verbose("failed to bind to %s", bindaddr);
+ goto syserr;
+ }
+
+ /* make the socket non-blocking */
+ (void)fcntl(sd, F_SETFL, O_NONBLOCK);
+
+ /* attempt to connect to server address */
+ if (connect(sd, sai->ai_addr, sai->ai_addrlen) != 0 &&
+ errno != EINPROGRESS) {
+ serrno = errno;
+ close(sd);
+ sd = -1;
+ continue;
+ }
+ pfds[i].fd = sd;
+ sd = -1;
+ npend++;
+ nextstart = now + CONNECT_DELAY_MS;
+ expires[i] = now + fetchTimeout * 1000;
+ }
- /* attempt to connect to server address */
- if ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) == 0)
+ /* nothing left to try */
+ if (npend == 0) {
+ errno = serrno;
break;
- if (errno != EINPROGRESS)
- goto next;
-
- /* wait for connection */
- deltams = INFTIM;
- pfd.fd = sd;
- pfd.events = POLLOUT;
- for (;;) {
- /* check the clock */
- if (fetchTimeout > 0) {
- if ((now = mstime_mono()) > deadline) {
- errno = ETIMEDOUT;
- err = -1;
- goto next;
+ }
+
+ /* wait for an answer, the next attempt or the deadline */
+ pollto = INFTIM;
+ if (next < naddrs && !starved)
+ pollto = (int)(nextstart - now);
+ if (fetchTimeout > 0) {
+ limit = deadline;
+ if (starved) {
+ limit = now + fetchTimeout * 1000;
+ for (i = 0; i < next; i++) {
+ if (pfds[i].fd >= 0 &&
+ expires[i] < limit)
+ limit = expires[i];
}
- deltams = deadline - now;
}
- /* wait for something to happen */
- if ((err = poll(&pfd, 1, deltams)) > 0)
- break;
- if (err == 0)
- continue;
- if (errno != EINTR)
- goto syserr;
- if (!fetchRestartCalls)
- break;
+ if (pollto == INFTIM || limit - now < (mstime_t)pollto)
+ pollto = (int)(limit - now);
}
+ err = poll(pfds, next, pollto);
+ if (err < 0 && (errno != EINTR || !fetchRestartCalls))
+ goto syserr;
+ now = mstime_mono();
+ if (err <= 0)
+ continue;
- /* check the outcome */
- if (err > 0) {
- if (pfd.revents == POLLOUT) {
+ /* check the attempts that answered */
+ for (i = 0; i < next; i++) {
+ if (pfds[i].revents == 0)
+ continue;
+ err = 0;
+ len = sizeof(err);
+ (void)getsockopt(pfds[i].fd, SOL_SOCKET, SO_ERROR, &err,
+ &len);
+ if (err == 0 && pfds[i].revents == POLLOUT) {
/* connection established */
- err = 0;
- break;
+ sd = pfds[i].fd;
+ pfds[i].fd = -1;
+ goto connected;
}
- /* we don't know the actual reason */
- errno = ECONNREFUSED;
+ /* without SO_ERROR, we don't know the actual reason */
+ serrno = err != 0 ? err : ECONNREFUSED;
+ close(pfds[i].fd);
+ pfds[i].fd = -1;
+ npend--;
+ /* a failure lets the next attempt start at once */
+ nextstart = now;
}
-next:
- /* clean up before next attempt */
- serrno = errno;
- close(sd);
- sd = -1;
- errno = serrno;
}
/* all attempts failed */
- if (err != 0) {
- if (sockshost != NULL) {
- fetch_verbose("failed to connect to SOCKS5 server %s:%d",
- sockshost, socksport);
- socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
- goto fail;
- }
- fetch_verbose("failed to connect to %s:%d", host, port);
- goto syserr;
+ if (sockshost != NULL) {
+ fetch_verbose("failed to connect to SOCKS5 server %s:%d",
+ sockshost, socksport);
+ socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
+ goto fail;
+ }
+ fetch_verbose("failed to connect to %s:%d", host, port);
+ goto syserr;
+
+connected:
+ /* abandon the other attempts */
+ for (i = 0; i < next; i++) {
+ if (pfds[i].fd >= 0)
+ close(pfds[i].fd);
}
+ next = 0;
if ((conn = fetch_reopen(sd)) == NULL)
goto syserr;
@@ -738,6 +863,9 @@
goto fail;
free(sockshost);
}
+ free(expires);
+ free(pfds);
+ free(addrs);
if (cais != NULL)
freeaddrinfo(cais);
if (sais != NULL)
@@ -748,6 +876,13 @@
fail:
if (sockshost != NULL)
free(sockshost);
+ for (i = 0; i < next; i++) {
+ if (pfds[i].fd >= 0)
+ close(pfds[i].fd);
+ }
+ free(expires);
+ free(pfds);
+ free(addrs);
/* Fully close if it was opened; otherwise just don't leak the fd. */
if (conn != NULL)
fetch_close(conn);
diff --git a/lib/libfetch/fetch.3 b/lib/libfetch/fetch.3
--- a/lib/libfetch/fetch.3
+++ b/lib/libfetch/fetch.3
@@ -24,7 +24,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd June 27, 2026
+.Dd November 7, 2026
.Dt FETCH 3
.Os
.Sh NAME
@@ -295,9 +295,12 @@
The
.Va fetchTimeout
global variable can be set to the amount of time in seconds to wait
-for each network operation, including each connection attempt (keeping
-in mind that one name may resolve to multiple addresses which will
-each be tried sequentially), each read, and each write.
+for each network operation, including establishing a connection to the
+server, each read, and each write.
+When a name resolves to several addresses, the connection attempts
+overlap as described in RFC 8305, and the timeout covers them all.
+A process that runs out of file descriptors tries the remaining
+addresses in turn, each with a timeout of its own.
Thus,
.Va fetchTimeout
can safely be set to a relatively low value without fear of

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 29, 10:50 AM (9 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42058753
Default Alt Text
D61994.1795949443.diff (9 KB)

Event Timeline