#include "gnunet_network_lib.h"
#include "gnunet_scheduler_lib.h"
-#define DEBUG_NETWORK GNUNET_NO
+#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.
*/
{
struct GNUNET_NETWORK_SocketHandle *ret;
char addr[32];
- char msg[INET6_ADDRSTRLEN];
socklen_t addrlen;
- int fam;
int fd;
int aret;
struct sockaddr_in *v4;
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);
(GNUNET_YES != (aret = access (access_cls, uaddr, addrlen))))
{
if (aret == GNUNET_NO)
- {
- fam = ((struct sockaddr *) addr)->sa_family;
- GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- _("Access denied to `%s'\n"),
- inet_ntop (fam,
- (fam == AF_INET6)
- ? (const void *) &((struct sockaddr_in6 *)
- &addr)->
- sin6_addr : (const void *)
- &((struct sockaddr_in *) &addr)->sin_addr,
- msg, sizeof (msg)));
- }
+ GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+ _("Access denied to `%s'\n"),
+ 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;
(0 != getsockopt (sock->sock, SOL_SOCKET, SO_ERROR, &error, &len)) ||
(error != 0) || (errno != 0))
{
+#if DEBUG_NETWORK
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Failed to establish TCP connection to `%s'\n",
+ GNUNET_a2s(sock->addr, sock->addrlen));
+#endif
/* connect failed / timed out */
GNUNET_break (0 == CLOSE (sock->sock));
sock->sock = -1;
if (GNUNET_SYSERR == try_connect (sock))
{
/* failed for good */
+#if DEBUG_NETWORK
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Failed to establish TCP connection, no further addresses to try.\n");
+#endif
+ /* connect failed / timed out */
GNUNET_break (sock->ai_pos == NULL);
freeaddrinfo (sock->ai);
sock->ai = NULL;
}
/* connect succeeded! clean up "ai" */
#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection succeeded!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Connection to `%s' succeeded!\n",
+ GNUNET_a2s(sock->addr, sock->addrlen));
#endif
freeaddrinfo (sock->ai);
sock->ai_pos = NULL;
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->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
{
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Destroy code waiting for writes to complete.\n");
-#endif
GNUNET_SCHEDULER_add_after (sock->sched,
GNUNET_YES,
GNUNET_SCHEDULER_PRIORITY_KEEP,
}
if (sock->read_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
{
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Destroy code waiting for receive to complete.\n");
-#endif
GNUNET_SCHEDULER_add_after (sock->sched,
GNUNET_YES,
GNUNET_SCHEDULER_PRIORITY_KEEP,
}
}
if (sock->sock != -1)
- {
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Closing socket.\n");
-#endif
- 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);
}
void
GNUNET_NETWORK_socket_destroy (struct GNUNET_NETWORK_SocketHandle *sock)
{
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Network asked to destroy socket %p\n", sock);
-#endif
if (sock->write_buffer_off == 0)
sock->ai_pos = NULL; /* if we're still trying to connect and have
no message pending, stop trying! */
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Network signals timeout to receiver!\n");
+ "Network signals time out to receiver!\n");
#endif
GNUNET_assert (NULL != (receiver = sh->receiver));
sh->receiver = NULL;
{
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Receive encounters error: timeout...\n");
+ "Receive encounters error: time out...\n");
#endif
signal_timeout (sh);
return;
}
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 got %d bytes from OS!\n", ret);
+ "receive_ready read %u/%u bytes from `%s'!\n",
+ (unsigned int) ret,
+ sh->max,
+ GNUNET_a2s(sh->addr, sh->addrlen));
#endif
GNUNET_assert (NULL != (receiver = sh->receiver));
sh->receiver = NULL;
{
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Receive encounters error: timeout...\n");
+ "Receive encounters error: time out...\n");
#endif
signal_timeout (sh);
return;
if (sh->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
{
/* connect was retried */
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Receive still waits on connect...\n");
-#endif
sh->read_task = GNUNET_SCHEDULER_add_after (tc->sched,
GNUNET_YES,
GNUNET_SCHEDULER_PRIORITY_KEEP,
sh->connect_task,
&receive_again, sh);
+ return;
}
- else
- {
- /* connect succeeded, wait for data! */
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Receive now waits for socket...\n");
-#endif
- sh->read_task = GNUNET_SCHEDULER_add_read (tc->sched,
- GNUNET_YES,
- GNUNET_SCHEDULER_PRIORITY_KEEP,
- sh->connect_task,
- GNUNET_TIME_absolute_get_remaining
- (sh->receive_timeout),
- sh->sock, &receive_ready,
- sh);
- }
+ /* connect succeeded, wait for data! */
+ sh->read_task = GNUNET_SCHEDULER_add_read (tc->sched,
+ GNUNET_YES,
+ GNUNET_SCHEDULER_PRIORITY_KEEP,
+ sh->connect_task,
+ GNUNET_TIME_absolute_get_remaining
+ (sh->receive_timeout),
+ sh->sock, &receive_ready,
+ sh);
}
GNUNET_NETWORK_Receiver receiver, void *receiver_cls)
{
struct GNUNET_SCHEDULER_TaskContext tc;
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Network asked to receive from socket...\n");
-#endif
+
GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK) &&
(sock->receiver == NULL));
sock->receiver = receiver;
ssize_t ret;
size_t have;
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Transmit ready called --- will try to send\n");
-#endif
GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
sock->write_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
if (sock->connect_task != GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
{
/* still waiting for connect */
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Transmission still waiting for connect...\n");
-#endif
GNUNET_assert (sock->write_task ==
GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
sock->write_task =
}
if (sock->sock == -1)
{
+#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- _
- ("Did not transmit request, socket closed or connect failed.\n"));
+ _("Could not satisfy pending transmission request, socket closed or connect failed.\n"));
+#endif
transmit_error (sock);
return; /* connect failed for good, we're finished */
}
if ((tc->write_ready == NULL) || (!FD_ISSET (sock->sock, tc->write_ready)))
{
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Socket not yet ready for writing, will wait for that.\n");
-#endif
+ /* special circumstances: not yet ready to write */
goto SCHEDULE_WRITE;
}
GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
have = sock->write_buffer_off - sock->write_buffer_pos;
if (have == 0)
{
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No data ready for writing.\n");
-#endif
+ /* no data ready for writing, terminate write loop */
return;
}
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)
return;
}
#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %d bytes to OS\n", ret);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "transmit_ready transmitted %u/%u bytes to `%s'\n",
+ (unsigned int) ret,
+ have,
+ GNUNET_a2s(sock->addr, sock->addrlen));
#endif
sock->write_buffer_pos += ret;
if (sock->write_buffer_pos == sock->write_buffer_off)
/* transmitted all pending data */
sock->write_buffer_pos = 0;
sock->write_buffer_off = 0;
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Transmission buffer now empty.\n", ret);
-#endif
}
if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
return; /* all data sent! */
/* not done writing, schedule more */
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "More data ready for transmission, scheduling task again!\n");
-#endif
SCHEDULE_WRITE:
if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
sock->write_task =
{
#if DEBUG_NETWORK
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Transmit fails, connection failed.\n");
+ "Transmission request fails, connection failed.\n");
#endif
notify (notify_cls, 0, NULL);
return &sock->nth;
timeout,
&transmit_timeout,
sock);
-#if DEBUG_NETWORK
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Scheduling asynchronous transmission once connect is done...\n");
-#endif
if (sock->write_task == GNUNET_SCHEDULER_NO_PREREQUISITE_TASK)
sock->write_task = GNUNET_SCHEDULER_add_delayed (sock->sched,
GNUNET_NO,