+bool
+socket_ip_address (int sock, ip_address *ip, int endpoint)
+{
+ struct sockaddr_storage storage;
+ struct sockaddr *sockaddr = (struct sockaddr *)&storage;
+ socklen_t addrlen = sizeof (storage);
+ int ret;
+
+ if (endpoint == ENDPOINT_LOCAL)
+ ret = getsockname (sock, sockaddr, &addrlen);
+ else if (endpoint == ENDPOINT_PEER)
+ ret = getpeername (sock, sockaddr, &addrlen);
+ else
+ abort ();
+ if (ret < 0)
+ return false;
+
+ ip->family = sockaddr->sa_family;
+ switch (sockaddr->sa_family)
+ {
+#ifdef ENABLE_IPV6
+ case AF_INET6:
+ {
+ struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&storage;
+ ip->data.d6 = sa6->sin6_addr;
+#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
+ ip->ipv6_scope = sa6->sin6_scope_id;
+#endif
+ DEBUGP (("conaddr is: %s\n", print_address (ip)));
+ return true;
+ }
+#endif
+ case AF_INET:
+ {
+ struct sockaddr_in *sa = (struct sockaddr_in *)&storage;
+ ip->data.d4 = sa->sin_addr;
+ DEBUGP (("conaddr is: %s\n", print_address (ip)));
+ return true;
+ }
+ default:
+ abort ();
+ }
+}
+
+/* Return true if the error from the connect code can be considered
+ retryable. Wget normally retries after errors, but the exception
+ are the "unsupported protocol" type errors (possible on IPv4/IPv6
+ dual family systems) and "connection refused". */
+
+bool
+retryable_socket_connect_error (int err)
+{
+ /* Have to guard against some of these values not being defined.
+ Cannot use a switch statement because some of the values might be
+ equal. */
+ if (false
+#ifdef EAFNOSUPPORT
+ || err == EAFNOSUPPORT
+#endif
+#ifdef EPFNOSUPPORT
+ || err == EPFNOSUPPORT
+#endif
+#ifdef ESOCKTNOSUPPORT /* no, "sockt" is not a typo! */
+ || err == ESOCKTNOSUPPORT
+#endif
+#ifdef EPROTONOSUPPORT
+ || err == EPROTONOSUPPORT
+#endif
+#ifdef ENOPROTOOPT
+ || err == ENOPROTOOPT
+#endif
+ /* Apparently, older versions of Linux and BSD used EINVAL
+ instead of EAFNOSUPPORT and such. */
+ || err == EINVAL
+ )
+ return false;
+
+ if (!opt.retry_connrefused)
+ if (err == ECONNREFUSED
+#ifdef ENETUNREACH
+ || err == ENETUNREACH /* network is unreachable */
+#endif
+#ifdef EHOSTUNREACH
+ || err == EHOSTUNREACH /* host is unreachable */
+#endif
+ )
+ return false;
+
+ return true;
+}
+
+/* Wait for a single descriptor to become available, timing out after
+ MAXTIME seconds. Returns 1 if FD is available, 0 for timeout and
+ -1 for error. The argument WAIT_FOR can be a combination of
+ WAIT_FOR_READ and WAIT_FOR_WRITE.
+
+ This is a mere convenience wrapper around the select call, and
+ should be taken as such (for example, it doesn't implement Wget's
+ 0-timeout-means-no-timeout semantics.) */