+
+/**
+ * Read and process a message from the given socket.
+ *
+ * @param plugin the overall plugin
+ * @param rsock socket to read from
+ */
+static void
+udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
+{
+ socklen_t fromlen;
+ char addr[32];
+ char buf[65536] GNUNET_ALIGN;
+ ssize_t size;
+ const struct GNUNET_MessageHeader *msg;
+
+ fromlen = sizeof (addr);
+ memset (&addr, 0, sizeof (addr));
+ size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
+ (struct sockaddr *) &addr, &fromlen);
+#if MINGW
+ /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
+ * WSAECONNRESET error to indicate that previous sendto() (???)
+ * on this socket has failed.
+ */
+ if ( (-1 == size) && (ECONNRESET == errno) )
+ return;
+#endif
+ if ( (-1 == size) || (size < sizeof (struct GNUNET_MessageHeader)))
+ {
+ GNUNET_break_op (0);
+ return;
+ }
+ msg = (const struct GNUNET_MessageHeader *) buf;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
+ GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
+
+ if (size != ntohs (msg->size))
+ {
+ GNUNET_break_op (0);
+ return;
+ }
+
+ switch (ntohs (msg->type))
+ {
+ case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
+ udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
+ return;
+
+ case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
+ read_process_msg (plugin, msg, addr, fromlen);
+ return;
+
+ case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
+ read_process_ack (plugin, msg, addr, fromlen);
+ return;
+
+ case GNUNET_MESSAGE_TYPE_FRAGMENT:
+ read_process_fragment (plugin, msg, addr, fromlen);
+ return;
+
+ default:
+ GNUNET_break_op (0);
+ return;
+ }
+}
+
+
+static size_t
+udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
+{
+ ssize_t sent;
+ size_t slen;
+ struct GNUNET_TIME_Absolute max;
+ struct UDPMessageWrapper *udpw = NULL;
+ static int network_down_error;
+
+ if (sock == plugin->sockv4)
+ {
+ udpw = plugin->ipv4_queue_head;
+ }
+ else if (sock == plugin->sockv6)
+ {
+ udpw = plugin->ipv6_queue_head;
+ }
+ else
+ {
+ GNUNET_break (0);
+ return 0;
+ }
+
+ const struct sockaddr * sa = udpw->session->sock_addr;
+ slen = udpw->session->addrlen;
+
+ max = GNUNET_TIME_absolute_max(udpw->timeout, GNUNET_TIME_absolute_get());
+
+ while (udpw != NULL)
+ {
+ if (max.abs_value != udpw->timeout.abs_value)
+ {
+ /* Message timed out */
+ call_continuation(udpw, GNUNET_SYSERR);
+ if (udpw->frag_ctx != NULL)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Fragmented message for peer `%s' with size %u timed out\n",
+ GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->bytes_to_send);
+ udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy(udpw->frag_ctx->frag);
+ GNUNET_free (udpw->frag_ctx);
+ udpw->session->frag_ctx = NULL;
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Message for peer `%s' with size %u timed out\n",
+ GNUNET_i2s(&udpw->session->target), udpw->msg_size);
+ }
+
+ if (sock == plugin->sockv4)
+ {
+ GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
+ GNUNET_free (udpw);
+ udpw = plugin->ipv4_queue_head;
+ }
+ else if (sock == plugin->sockv6)
+ {
+ GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
+ GNUNET_free (udpw);
+ udpw = plugin->ipv6_queue_head;
+ }
+ }
+ else
+ {
+ struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
+ if (delta.rel_value == 0)
+ {
+ /* this message is not delayed */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Message for peer `%s' (%u bytes) is not delayed \n",
+ GNUNET_i2s(&udpw->session->target), udpw->msg_size);
+ break;
+ }
+ else
+ {
+ /* this message is delayed, try next */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Message for peer `%s' (%u bytes) is delayed for %llu \n",
+ GNUNET_i2s(&udpw->session->target), udpw->msg_size,
+ delta);
+ udpw = udpw->next;
+ }
+ }
+ }
+
+ if (udpw == NULL)
+ {
+ /* No message left */
+ return 0;
+ }
+
+ sent = GNUNET_NETWORK_socket_sendto (sock, udpw->udp, udpw->msg_size, sa, slen);
+
+ if (GNUNET_SYSERR == sent)
+ {
+ const struct GNUNET_ATS_Information type = plugin->env->get_address_type
+ (plugin->env->cls,sa, slen);
+
+ if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
+ ((ENETUNREACH == errno) || (ENETDOWN == errno)))
+ {
+ if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
+ {
+ /* IPv4: "Network unreachable" or "Network down"
+ *
+ * This indicates we do not have connectivity
+ */
+ LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
+ _("UDP could not transmit message to `%s': "
+ "Network seems down, please check your network configuration\n"),
+ GNUNET_a2s (sa, slen));
+ }
+ if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
+ {
+ /* IPv6: "Network unreachable" or "Network down"
+ *
+ * This indicates that this system is IPv6 enabled, but does not
+ * have a valid global IPv6 address assigned or we do not have
+ * connectivity
+ */
+
+ LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
+ _("UDP could not transmit message to `%s': "
+ "Please check your network configuration and disable IPv6 if your "
+ "connection does not have a global IPv6 address\n"),
+ GNUNET_a2s (sa, slen));
+ }
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "UDP could not transmit %u-byte message to `%s': `%s'\n",
+ (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen),
+ STRERROR (errno));
+ }
+ call_continuation(udpw, GNUNET_SYSERR);
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "UDP transmitted %u-byte message to `%s' (%d: %s)\n",
+ (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
+ (sent < 0) ? STRERROR (errno) : "ok");
+ call_continuation(udpw, GNUNET_OK);
+ network_down_error = GNUNET_NO;
+ }
+
+ if (sock == plugin->sockv4)
+ GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
+ else if (sock == plugin->sockv6)
+ GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
+ GNUNET_free (udpw);
+ udpw = NULL;
+
+ return sent;
+}
+
+
+/**
+ * We have been notified that our readset has something to read. We don't
+ * know which socket needs to be read, so we have to check each one
+ * Then reschedule this function to be called again once more is available.
+ *
+ * @param cls the plugin handle
+ * @param tc the scheduling context (for rescheduling this function again)
+ */
+static void
+udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct Plugin *plugin = cls;
+
+ plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+ return;
+ plugin->with_v4_ws = GNUNET_NO;
+
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
+ {
+ if ((NULL != plugin->sockv4) &&
+ (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
+ udp_select_read (plugin, plugin->sockv4);
+
+ }
+
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
+ {
+ if ((NULL != plugin->sockv4) && (plugin->ipv4_queue_head != NULL) &&
+ (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
+ {
+ udp_select_send (plugin, plugin->sockv4);
+ }
+ }
+
+ if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel (plugin->select_task);
+ plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ plugin->rs_v4,
+ (plugin->ipv4_queue_head != NULL) ? plugin->ws_v4 : NULL,
+ &udp_plugin_select, plugin);
+ if (plugin->ipv4_queue_head != NULL)
+ plugin->with_v4_ws = GNUNET_YES;
+ else
+ plugin->with_v4_ws = GNUNET_NO;
+}
+
+
+/**
+ * We have been notified that our readset has something to read. We don't
+ * know which socket needs to be read, so we have to check each one
+ * Then reschedule this function to be called again once more is available.
+ *
+ * @param cls the plugin handle
+ * @param tc the scheduling context (for rescheduling this function again)
+ */
+static void
+udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct Plugin *plugin = cls;
+
+ plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
+ return;
+
+ plugin->with_v6_ws = GNUNET_NO;
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
+ {
+ if ((NULL != plugin->sockv6) &&
+ (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
+ udp_select_read (plugin, plugin->sockv6);
+ }
+
+ if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
+ {
+ if ((NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
+ (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)))
+ {
+ udp_select_send (plugin, plugin->sockv6);
+ }
+ }
+ if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
+ plugin->select_task_v6 = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ plugin->rs_v6,
+ (plugin->ipv6_queue_head != NULL) ? plugin->ws_v6 : NULL,
+ &udp_plugin_select_v6, plugin);
+ if (plugin->ipv6_queue_head != NULL)
+ plugin->with_v6_ws = GNUNET_YES;
+ else
+ plugin->with_v6_ws = GNUNET_NO;
+}
+
+
+static int
+setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
+{
+ int tries;
+ int sockets_created = 0;
+ struct sockaddr *serverAddr;
+ struct sockaddr *addrs[2];
+ socklen_t addrlens[2];
+ socklen_t addrlen;
+
+ /* Create IPv6 socket */
+ if (plugin->enable_ipv6 == GNUNET_YES)
+ {
+ plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
+ if (NULL == plugin->sockv6)
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
+ plugin->enable_ipv6 = GNUNET_NO;
+ }
+ else
+ {
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ serverAddrv6->sin6_len = sizeof (serverAddrv6);
+#endif
+ serverAddrv6->sin6_family = AF_INET6;
+ serverAddrv6->sin6_addr = in6addr_any;
+ serverAddrv6->sin6_port = htons (plugin->port);
+ addrlen = sizeof (struct sockaddr_in6);
+ serverAddr = (struct sockaddr *) serverAddrv6;
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
+ ntohs (serverAddrv6->sin6_port));
+ tries = 0;
+ while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
+ GNUNET_OK)
+ {
+ serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "IPv6 Binding failed, trying new port %d\n",
+ ntohs (serverAddrv6->sin6_port));
+ tries++;
+ if (tries > 10)
+ {
+ GNUNET_NETWORK_socket_close (plugin->sockv6);
+ plugin->sockv6 = NULL;
+ break;
+ }
+ }
+ if (plugin->sockv6 != NULL)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "IPv6 socket created on port %d\n",
+ ntohs (serverAddrv6->sin6_port));
+ addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
+ addrlens[sockets_created] = sizeof (struct sockaddr_in6);
+ sockets_created++;
+ }
+ }
+ }
+
+ /* Create IPv4 socket */
+ plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
+ if (NULL == plugin->sockv4)
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
+ }
+ else
+ {
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ serverAddrv4->sin_len = sizeof (serverAddrv4);
+#endif
+ serverAddrv4->sin_family = AF_INET;
+ serverAddrv4->sin_addr.s_addr = INADDR_ANY;
+ serverAddrv4->sin_port = htons (plugin->port);
+ addrlen = sizeof (struct sockaddr_in);
+ serverAddr = (struct sockaddr *) serverAddrv4;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
+ ntohs (serverAddrv4->sin_port));
+ tries = 0;
+ while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
+ GNUNET_OK)
+ {
+ serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
+ ntohs (serverAddrv4->sin_port));
+ tries++;
+ if (tries > 10)
+ {
+ GNUNET_NETWORK_socket_close (plugin->sockv4);
+ plugin->sockv4 = NULL;
+ break;
+ }
+ }
+ if (plugin->sockv4 != NULL)
+ {
+ addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
+ addrlens[sockets_created] = sizeof (struct sockaddr_in);
+ sockets_created++;
+ }
+ }
+
+ /* Create file descriptors */
+ plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
+ plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
+ GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
+ GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
+ if (NULL != plugin->sockv4)
+ {
+ GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
+ GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
+ }
+
+ if (sockets_created == 0)
+ LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
+
+ plugin->select_task =
+ GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ plugin->rs_v4,
+ NULL,
+ &udp_plugin_select, plugin);
+ plugin->with_v4_ws = GNUNET_NO;
+
+ if (plugin->enable_ipv6 == GNUNET_YES)
+ {
+ plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
+ plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
+ GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
+ GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
+ if (NULL != plugin->sockv6)
+ {
+ GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
+ GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
+ }
+
+ plugin->select_task_v6 =
+ GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ plugin->rs_v6,
+ NULL,
+ &udp_plugin_select_v6, plugin);
+ plugin->with_v6_ws = GNUNET_NO;
+ }
+
+ plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
+ GNUNET_NO, plugin->port,
+ sockets_created,
+ (const struct sockaddr **) addrs, addrlens,
+ &udp_nat_port_map_callback, NULL, plugin);
+
+ return sockets_created;
+}
+
+/**
+ * Session was idle, so disconnect it
+ */
+static void
+session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ GNUNET_assert (NULL != cls);
+ struct Session *s = cls;
+
+ s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Session %p was idle for %llu ms, disconnecting\n",
+ s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
+ /* call session destroy function */
+ disconnect_session(s);
+}
+
+
+/**
+ * Start session timeout
+ */
+static void
+start_session_timeout (struct Session *s)
+{
+ GNUNET_assert (NULL != s);
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
+ s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
+ &session_timeout,
+ s);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Timeout for session %p set to %llu ms\n",
+ s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
+}
+
+
+/**
+ * Increment session timeout due to activity
+ */
+static void
+reschedule_session_timeout (struct Session *s)
+{
+ GNUNET_assert (NULL != s);
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
+
+ GNUNET_SCHEDULER_cancel (s->timeout_task);
+ s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
+ &session_timeout,
+ s);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Timeout rescheduled for session %p set to %llu ms\n",
+ s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
+}
+
+
+/**
+ * Cancel timeout
+ */
+static void
+stop_session_timeout (struct Session *s)
+{
+ GNUNET_assert (NULL != s);
+
+ if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
+ {
+ GNUNET_SCHEDULER_cancel (s->timeout_task);
+ s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Timeout stopped for session %p canceled\n",
+ s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
+ }
+}
+
+
+