+ int test = select_fd (local_sock, opt.connect_timeout, WAIT_FOR_READ);
+ if (test == 0)
+ errno = ETIMEDOUT;
+ if (test <= 0)
+ return -1;
+ }
+ sock = accept (local_sock, sa, &addrlen);
+ DEBUGP (("Accepted client at socket %d.\n", sock));
+ return sock;
+}
+
+/* Get the IP address associated with the connection on FD and store
+ it to IP. Return true on success, false otherwise.
+
+ If ENDPOINT is ENDPOINT_LOCAL, it returns the address of the local
+ (client) side of the socket. Else if ENDPOINT is ENDPOINT_PEER, it
+ returns the address of the remote (peer's) side of the socket. */
+
+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 ();