- if (sock->addr != NULL)
- {
- GNUNET_free (sock->addr);
- sock->addr = NULL;
- sock->addrlen = 0;
- }
- while (1)
- {
- if (sock->ai_pos == NULL)
- {
- /* no more addresses to try, fatal! */
- return GNUNET_SYSERR;
- }
- switch (sock->ai_pos->ai_family)
- {
- case AF_INET:
- ((struct sockaddr_in *) sock->ai_pos->ai_addr)->sin_port =
- htons (sock->port);
- break;
- case AF_INET6:
- ((struct sockaddr_in6 *) sock->ai_pos->ai_addr)->sin6_port =
- htons (sock->port);
- break;
- default:
- sock->ai_pos = sock->ai_pos->ai_next;
- continue;
- }
- s = SOCKET (sock->ai_pos->ai_family, SOCK_STREAM, 0);
- if (s == -1)
- {
- /* maybe unsupported address family, try next */
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "socket");
- sock->ai_pos = sock->ai_pos->ai_next;
- continue;
- }
- if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
- "fcntl");
- if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
- {
- /* we'll treat this one as fatal */
- GNUNET_break (0 == CLOSE (s));
- return GNUNET_SYSERR;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- _("Trying to connect to `%s'\n"),
- GNUNET_a2s(sock->ai_pos->ai_addr,
- sock->ai_pos->ai_addrlen));
- if ((0 != CONNECT (s,
- sock->ai_pos->ai_addr,
- sock->ai_pos->ai_addrlen)) && (errno != EINPROGRESS))
- {
- /* maybe refused / unsupported address, try next */
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect");
- GNUNET_break (0 == CLOSE (s));
- continue;
- }
- break;
- }
- /* got one! copy address information! */
- sock->addrlen = sock->ai_pos->ai_addrlen;
- sock->addr = GNUNET_malloc (sock->addrlen);
- memcpy (sock->addr, sock->ai_pos->ai_addr, sock->addrlen);
- sock->ai_pos = sock->ai_pos->ai_next;
- sock->sock = s;
- return GNUNET_OK;