+struct SessionItCtx
+{
+ void * addr;
+ size_t addrlen;
+ struct Session * result;
+};
+
+int session_it (void *cls,
+ const GNUNET_HashCode * key,
+ void *value)
+{
+ struct SessionItCtx * si_ctx = cls;
+ struct Session * session = value;
+
+ if (session->connect_alen != si_ctx->addrlen)
+ return GNUNET_YES;
+ if (0 != memcmp (&session->connect_addr, si_ctx->addr, si_ctx->addrlen))
+ return GNUNET_YES;
+
+ /* Found existing session */
+ si_ctx->result = session;
+ return GNUNET_NO;
+}
+
+
+/**
+ * Create a new session to transmit data to the target
+ * This session will used to send data to this peer and the plugin will
+ * notify us by calling the env->session_end function
+ *
+ * @param cls closure
+ * @param target the neighbour id
+ * @param addr pointer to the address
+ * @param addrlen length of addr
+ * @return the session if the address is valid, NULL otherwise
+ */
+const void * tcp_plugin_create_session (void *cls,
+ const struct GNUNET_PeerIdentity *target,
+ const void *addr,
+ size_t addrlen)
+{
+ struct Plugin * plugin = cls;
+ struct Session * session = NULL;
+
+ int af;
+ const void *sb;
+ size_t sbs;
+ struct GNUNET_CONNECTION_Handle *sa;
+ struct sockaddr_in a4;
+ struct sockaddr_in6 a6;
+ const struct IPv4TcpAddress *t4;
+ const struct IPv6TcpAddress *t6;
+ unsigned int is_natd;
+
+ if (addrlen == sizeof (struct IPv6TcpAddress))
+ {
+ GNUNET_assert (NULL != addr); /* make static analysis happy */
+ t6 = addr;
+ af = AF_INET6;
+ memset (&a6, 0, sizeof (a6));
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ a6.sin6_len = sizeof (a6);
+#endif
+ a6.sin6_family = AF_INET6;
+ a6.sin6_port = t6->t6_port;
+ if (t6->t6_port == 0)
+ is_natd = GNUNET_YES;
+ memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
+ sb = &a6;
+ sbs = sizeof (a6);
+ }
+ else if (addrlen == sizeof (struct IPv4TcpAddress))
+ {
+ GNUNET_assert (NULL != addr); /* make static analysis happy */
+ t4 = addr;
+ af = AF_INET;
+ memset (&a4, 0, sizeof (a4));
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ a4.sin_len = sizeof (a4);
+#endif
+ a4.sin_family = AF_INET;
+ a4.sin_port = t4->t4_port;
+ if (t4->t4_port == 0)
+ is_natd = GNUNET_YES;
+ a4.sin_addr.s_addr = t4->ipv4_addr;
+ sb = &a4;
+ sbs = sizeof (a4);
+ }
+ else
+ {
+ GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "tcp",
+ _("Address of unexpected length: %u\n"), addrlen);
+ GNUNET_break (0);
+ return NULL;
+ }
+
+ /* look for existing session */
+ if (GNUNET_CONTAINER_multihashmap_contains(plugin->sessionmap, &target->hashPubKey))
+ {
+ struct SessionItCtx si_ctx;
+ si_ctx.addr = &sbs;
+ si_ctx.addrlen = sbs;
+ GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessionmap, &target->hashPubKey, &session_it, &si_ctx);
+ if (si_ctx.result != NULL)
+ session = si_ctx.result;
+ return session;
+ }
+
+ if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
+ return NULL; /* NAT client only works with IPv4 addresses */
+
+ if (0 == plugin->max_connections)
+ return NULL; /* saturated */
+
+ if ((is_natd == GNUNET_YES) &&
+ (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
+ &target->hashPubKey)))
+ return NULL; /* Only do one NAT punch attempt per peer identity */
+
+ if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
+ (GNUNET_NO ==
+ GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
+ &target->hashPubKey)))
+ {
+#if DEBUG_TCP_NAT
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "tcp",
+ _("Found valid IPv4 NAT address (creating session)!\n"));
+#endif
+ session = create_session (plugin, target, NULL, GNUNET_YES);
+ GNUNET_assert (session != NULL);
+
+ GNUNET_assert (GNUNET_CONTAINER_multihashmap_put
+ (plugin->nat_wait_conns, &target->hashPubKey, session,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
+#if DEBUG_TCP_NAT
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "tcp",
+ "Created NAT WAIT connection to `%4s' at `%s'\n",
+ GNUNET_i2s (target), GNUNET_a2s (sb, sbs));
+#endif
+ GNUNET_NAT_run_client (plugin->nat, &a4);
+ return session;
+ }
+
+ /* create new outbound session */
+ GNUNET_assert (0 != plugin->max_connections);
+ sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
+ if (sa == NULL)
+ {
+#if DEBUG_TCP
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "tcp",
+ "Failed to create connection to `%4s' at `%s'\n",
+ GNUNET_i2s (target), GNUNET_a2s (sb, sbs));
+#endif
+ return NULL;
+ }
+ plugin->max_connections--;
+#if DEBUG_TCP_NAT
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "tcp",
+ "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
+ GNUNET_i2s (target), GNUNET_a2s (sb, sbs));
+#endif
+ session = create_session (plugin,
+ target,
+ GNUNET_SERVER_connect_socket (plugin->server, sa),
+ GNUNET_NO);
+ session->connect_addr = GNUNET_malloc (addrlen);
+ memcpy (session->connect_addr, addr, addrlen);
+ session->connect_alen = addrlen;
+ if (addrlen != 0)
+ {
+ struct GNUNET_ATS_Information ats;
+ ats = plugin->env->get_address_type (plugin->env->cls, sb ,sbs);
+ session->ats_address_network_type = ats.value;
+ }
+ else
+ GNUNET_break (0);
+
+ GNUNET_CONTAINER_multihashmap_put(plugin->sessionmap, &target->hashPubKey, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+
+ /* Send TCP Welcome */
+ process_pending_messages (session);
+
+ return session;
+}
+