#define DEBUG_NETWORK GNUNET_YES
+/**
+ * List of address families to give as hints to
+ * getaddrinfo, in reverse order of preference.
+ */
+static int address_families[] =
+ { AF_INET, AF_INET6, AF_UNSPEC };
+
struct GNUNET_NETWORK_TransmitHandle
{
*/
struct sockaddr *addr;
+ /**
+ * Pointer to the hostname if socket was
+ * created using DNS lookup, otherwise NULL.
+ */
+ char *hostname;
+
/**
* Pointer to our write buffer.
*/
*/
socklen_t addrlen;
+ /**
+ * Offset in our address family list
+ * that we used last.
+ */
+ int af_fam_offset;
+
/**
* Connect task that we may need to wait for.
*/
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
return NULL;
}
+#ifndef MINGW
+ // FIXME NILS
if (0 != fcntl (fd, F_SETFD, fcntl (fd, F_GETFD) | FD_CLOEXEC))
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
"fcntl");
+#endif
if (addrlen > sizeof (addr))
{
GNUNET_break (0);
if (aret == GNUNET_NO)
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Access denied to `%s'\n"),
- GNUNET_a2s(uaddr, addrlen));
+ GNUNET_a2s(uaddr, addrlen));
GNUNET_break (0 == SHUTDOWN (fd, SHUT_RDWR));
GNUNET_break (0 == CLOSE (fd));
GNUNET_free (uaddr);
return NULL;
}
+#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Accepting connection from `%s'\n"),
GNUNET_a2s(uaddr, addrlen));
+#endif
ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
ret->write_buffer = (char *) &ret[1];
ret->write_buffer_size = maxbuf;
}
+/**
+ * Perform a DNS lookup for the hostname associated
+ * with the current socket, iterating over the address
+ * families as specified in the "address_families" array.
+ */
+static void
+try_lookup (struct GNUNET_NETWORK_SocketHandle *sock)
+{
+ struct addrinfo hints;
+ int ec;
+
+ while ( (sock->ai_pos == NULL) &&
+ (sock->af_fam_offset > 0) )
+ {
+ if (sock->ai != NULL)
+ freeaddrinfo (sock->ai);
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = address_families[--sock->af_fam_offset];
+ hints.ai_socktype = SOCK_STREAM;
+ if (0 != (ec = getaddrinfo (sock->hostname, NULL, &hints, &sock->ai)))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
+ "`%s' failed for hostname `%s': %s\n",
+ "getaddrinfo", sock->hostname, gai_strerror (ec));
+ sock->ai = NULL;
+ }
+ sock->ai_pos = sock->ai;
+ }
+}
+
+
/**
* Initiate asynchronous TCP connect request.
*
}
while (1)
{
+ if (sock->ai_pos == NULL)
+ try_lookup (sock);
if (sock->ai_pos == NULL)
{
/* no more addresses to try, fatal! */
sock->ai_pos = sock->ai_pos->ai_next;
continue;
}
+#ifndef MINGW
+ // FIXME NILS
if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
"fcntl");
+#endif
if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
{
/* we'll treat this one as fatal */
GNUNET_break (0 == CLOSE (s));
return GNUNET_SYSERR;
}
+#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Trying to connect to `%s'\n"),
GNUNET_a2s(sock->ai_pos->ai_addr,
sock->ai_pos->ai_addrlen));
+#endif
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));
+ sock->ai_pos = sock->ai_pos->ai_next;
continue;
}
break;
}
/* connect succeeded! clean up "ai" */
#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Connection to `%s' succeeded!\n",
GNUNET_a2s(sock->addr, sock->addrlen));
#endif
uint16_t port, size_t maxbuf)
{
struct GNUNET_NETWORK_SocketHandle *ret;
- struct addrinfo hints;
- int ec;
ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_SocketHandle) + maxbuf);
ret->sock = -1;
ret->write_buffer = (char *) &ret[1];
ret->write_buffer_size = maxbuf;
ret->port = port;
- memset (&hints, 0, sizeof (hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- if (0 != (ec = getaddrinfo (hostname, NULL, &hints, &ret->ai)))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
- "`%s' failed for hostname `%s': %s\n",
- "getaddrinfo", hostname, gai_strerror (ec));
- GNUNET_free (ret);
- return NULL;
- }
- ret->ai_pos = ret->ai;
+ ret->af_fam_offset = sizeof (address_families) / sizeof(address_families[0]);
+ ret->hostname = GNUNET_strdup (hostname);
if (GNUNET_SYSERR == try_connect (ret))
{
- freeaddrinfo (ret->ai);
+ if (NULL != ret->ai)
+ freeaddrinfo (ret->ai);
+ GNUNET_free (ret->hostname);
GNUNET_free (ret);
return NULL;
}
socklen_t addrlen, size_t maxbuf)
{
int s;
+ struct GNUNET_NETWORK_SocketHandle *ret;
s = SOCKET (af_family, SOCK_STREAM, 0);
if (s == -1)
GNUNET_ERROR_TYPE_BULK, "socket");
return NULL;
}
+#ifndef MINGW
if (0 != fcntl (s, F_SETFD, fcntl (s, F_GETFD) | FD_CLOEXEC))
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
"fcntl");
+#endif
if (GNUNET_SYSERR == socket_set_blocking (s, GNUNET_NO))
{
/* we'll treat this one as fatal */
GNUNET_break (0 == CLOSE (s));
return NULL;
}
+#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Trying to connect to `%s'\n"),
GNUNET_a2s(serv_addr, addrlen));
+#endif
if ((0 != CONNECT (s, serv_addr, addrlen)) && (errno != EINPROGRESS))
{
/* maybe refused / unsupported address, try next */
GNUNET_break (0 == CLOSE (s));
return NULL;
}
- return GNUNET_NETWORK_socket_create_from_existing (sched, s, maxbuf);
+ ret = GNUNET_NETWORK_socket_create_from_existing (sched, s, maxbuf);
+ ret->addr = GNUNET_malloc (addrlen);
+ memcpy (ret->addr, serv_addr, addrlen);
+ ret->addrlen = addrlen;
+ return ret;
}
}
}
if (sock->sock != -1)
- GNUNET_break (0 == CLOSE (sock->sock));
+ GNUNET_break (0 == CLOSE (sock->sock));
GNUNET_free_non_null (sock->addr);
if (sock->ai != NULL)
- freeaddrinfo (sock->ai);
+ freeaddrinfo (sock->ai);
+ GNUNET_free_non_null (sock->hostname);
GNUNET_free (sock);
}
}
GNUNET_assert (FD_ISSET (sh->sock, tc->read_ready));
RETRY:
- ret = RECV (sh->sock, buffer, sh->max, MSG_DONTWAIT);
+ ret = RECV (sh->sock, buffer, sh->max,
+#ifndef MINGW
+ // FIXME MINGW
+ MSG_DONTWAIT
+#else
+ 0
+#endif
+ );
if (ret == -1)
{
if (errno == EINTR)
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"receive_ready read %u/%u bytes from `%s'!\n",
- (unsigned int) ret,
+ (unsigned int) ret,
sh->max,
GNUNET_a2s(sh->addr, sh->addrlen));
#endif
GNUNET_TIME_absolute_get_remaining
(sh->receive_timeout),
sh->sock, &receive_ready,
- sh);
+ sh);
}
}
if (sock->sock == -1)
{
+#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_("Could not satisfy pending transmission request, socket closed or connect failed.\n"));
+#endif
transmit_error (sock);
return; /* connect failed for good, we're finished */
}
RETRY:
ret = SEND (sock->sock,
&sock->write_buffer[sock->write_buffer_pos],
- have, MSG_DONTWAIT | MSG_NOSIGNAL);
+ have,
+#ifndef MINGW
+ // FIXME NILS
+ MSG_DONTWAIT | MSG_NOSIGNAL
+#else
+ 0
+#endif
+ );
if (ret == -1)
{
if (errno == EINTR)