/*
- This file is part of GNUnet
- (C) 2002--2013 Christian Grothoff (and other contributing authors)
-
- GNUnet is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 3, or (at your
- option) any later version.
-
- GNUnet is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ This file is part of GNUnet
+ (C) 2002--2014 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
*/
/**
* @file transport/plugin_transport_tcp.c
* @return string representing the same address
*/
static const char *
-tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
+tcp_address_to_string (void *cls,
+ const void *addr,
+ size_t addrlen);
+
/**
* Function to check if an inbound connection is acceptable.
*/
static int
plugin_tcp_access_check (void *cls,
- const struct GNUNET_CONNECTION_Credentials *ucred,
- const struct sockaddr *addr, socklen_t addrlen)
+ const struct GNUNET_CONNECTION_Credentials *ucred,
+ const struct sockaddr *addr,
+ socklen_t addrlen)
{
struct Plugin *plugin = cls;
LOG(GNUNET_ERROR_TYPE_DEBUG,
return GNUNET_YES;
}
+
/**
* Our external IP address/port mapping has changed.
*
- * @param cls closure, the 'struct Plugin'
+ * @param cls closure, the `struct Plugin`
* @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
* the previous (now invalid) one
* @param addr either the previous or the new public IP address
- * @param addrlen actual lenght of the address
+ * @param addrlen actual length of @a addr
*/
static void
-tcp_nat_port_map_callback (void *cls, int add_remove,
- const struct sockaddr *addr, socklen_t addrlen)
+tcp_nat_port_map_callback (void *cls,
+ int add_remove,
+ const struct sockaddr *addr,
+ socklen_t addrlen)
{
struct Plugin *plugin = cls;
struct GNUNET_HELLO_Address *address;
t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
arg = &t4;
- args = sizeof(t4);
+ args = sizeof (t4);
break;
case AF_INET6:
GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
t6.options = htonl (myoptions);
t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
arg = &t6;
- args = sizeof(t6);
+ args = sizeof (t6);
break;
default:
GNUNET_break(0);
return;
}
/* modify our published address list */
+ GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
+ (args == sizeof (struct IPv6TcpAddress)));
address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
plugin->env->notify_address (plugin->env->cls, add_remove, address);
GNUNET_HELLO_address_free(address);
}
+
/**
* Function called for a quick conversion of the binary address to
* a numeric address. Note that the caller must not free the
*
* @param cls closure (`struct Plugin*`)
* @param addr binary address
- * @param addrlen length of the address
+ * @param addrlen length of @a addr
* @return string representing the same address
*/
static const char *
-tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
+tcp_address_to_string (void *cls,
+ const void *addr,
+ size_t addrlen)
{
static char rbuf[INET6_ADDRSTRLEN + 12];
char buf[INET6_ADDRSTRLEN];
memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
sb = &a4;
break;
- case 0:
- {
- GNUNET_snprintf (rbuf, sizeof(rbuf), "%s",
- TRANSPORT_SESSION_INBOUND_STRING);
- return rbuf;
- }
default:
- LOG(GNUNET_ERROR_TYPE_WARNING, _("Unexpected address length: %u bytes\n"),
- (unsigned int ) addrlen);
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ _("Unexpected address length: %u bytes\n"),
+ (unsigned int ) addrlen);
return NULL ;
}
if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
{
- GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+ "inet_ntop");
return NULL ;
}
GNUNET_snprintf (rbuf, sizeof(rbuf),
- (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u", PLUGIN_NAME, options,
- buf, port);
+ (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
+ PLUGIN_NAME,
+ options,
+ buf,
+ port);
return rbuf;
}
+
/**
* Function called to convert a string address to
* a binary address.
* @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
*/
static int
-tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
- void **buf, size_t *added)
+tcp_string_to_address (void *cls,
+ const char *addr,
+ uint16_t addrlen,
+ void **buf,
+ size_t *added)
{
struct sockaddr_storage socket_address;
char *address;
}
}
+
/**
* Closure for #session_lookup_by_client_it().
*/
struct Session *ret;
};
+
static int
-session_lookup_by_client_it (void *cls, const struct GNUNET_PeerIdentity *key,
- void *value)
+session_lookup_by_client_it (void *cls,
+ const struct GNUNET_PeerIdentity *key,
+ void *value)
{
struct SessionClientCtx *sc_ctx = cls;
struct Session *s = value;
return GNUNET_YES;
}
+
/**
* Find the session handle for the given client.
* Currently uses both the hashmap and the client
*/
static struct Session *
lookup_session_by_client (struct Plugin *plugin,
- struct GNUNET_SERVER_Client *client)
+ struct GNUNET_SERVER_Client *client)
{
struct Session *ret;
struct SessionClientCtx sc_ctx;
return sc_ctx.ret;
}
+
/**
* Functions with this signature are called whenever we need
* to close a session due to a disconnect or failure to
* @return #GNUNET_OK on success
*/
static int
-tcp_disconnect_session (void *cls, struct Session *session)
+tcp_disconnect_session (void *cls,
+ struct Session *session)
{
struct Plugin *plugin = cls;
struct PendingMessage *pm;
LOG(GNUNET_ERROR_TYPE_DEBUG,
"Disconnecting session of peer `%s' address `%s'\n",
GNUNET_i2s (&session->target),
- tcp_address_to_string (NULL, session->address->address, session->address->address_length));
+ tcp_address_to_string (NULL,
+ session->address->address,
+ session->address->address_length));
if (GNUNET_SCHEDULER_NO_TASK != session->timeout_task)
{
}
else
{
- GNUNET_assert(
- GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &session->target, session));
+ GNUNET_assert(GNUNET_YES ==
+ GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
+ &session->target,
+ session));
}
if (NULL != session->client)
- GNUNET_SERVER_client_set_user_context(session->client, (void *) NULL);
+ GNUNET_SERVER_client_set_user_context (session->client,
+ (void *) NULL);
/* clean up state */
if (NULL != session->transmit_handle)
GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
session->transmit_handle = NULL;
}
+ plugin->env->unregister_quota_notification (plugin->env->cls,
+ &session->target, PLUGIN_NAME, session);
session->plugin->env->session_end (session->plugin->env->cls,
- &session->target, session);
+ session->address, session);
if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
{
while (NULL != (pm = session->pending_messages_head))
{
- LOG(GNUNET_ERROR_TYPE_DEBUG,
- pm->transmit_cont != NULL ? "Could not deliver message to `%4s'.\n" : "Could not deliver message to `%4s', notifying.\n",
- GNUNET_i2s (&session->target));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ (NULL != pm->transmit_cont)
+ ? "Could not deliver message to `%4s'.\n"
+ : "Could not deliver message to `%4s', notifying.\n",
+ GNUNET_i2s (&session->target));
GNUNET_STATISTICS_update (session->plugin->env->stats,
gettext_noop ("# bytes currently in TCP buffers"),
-(int64_t) pm->message_size, GNUNET_NO);
GNUNET_SERVER_client_drop (session->client);
session->client = NULL;
}
- GNUNET_HELLO_address_free(session->address);
+ GNUNET_HELLO_address_free (session->address);
GNUNET_assert(NULL == session->transmit_handle);
GNUNET_free(session);
return GNUNET_OK;
}
+
/**
* Function that is called to get the keepalive factor.
- * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
+ * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
* calculate the interval between keepalive packets.
*
* @param cls closure with the `struct Plugin`
return 3;
}
+
/**
* Session was idle, so disconnect it
*
* @param tc scheduler context
*/
static void
-session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+session_timeout (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct Session *s = cls;
s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
- "Session %p was idle for %s, disconnecting\n", s,
- GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_YES));
+ "Session %p was idle for %s, disconnecting\n", s,
+ GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
+ GNUNET_YES));
/* call session destroy function */
tcp_disconnect_session (s->plugin, s);
}
+
/**
* Increment session timeout due to activity
*
* Create a new session. Also queues a welcome message.
*
* @param plugin the plugin
- * @param target peer to connect to
+ * @param address the address to create the session for
* @param client client to use, reference counter must have already been increased
* @param is_nat this a NAT session, we should wait for a client to
* connect to us from an address, then assign that to
else
GNUNET_assert(NULL == client);
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new session for peer `%4s'\n",
+ LOG(GNUNET_ERROR_TYPE_DEBUG,
+ "Creating new session for peer `%4s'\n",
GNUNET_i2s (&address->peer));
session = GNUNET_new (struct Session);
session->last_activity = GNUNET_TIME_absolute_get ();
GNUNET_STATISTICS_update (plugin->env->stats,
gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
}
+ plugin->env->register_quota_notification (plugin->env->cls,
+ &address->peer, PLUGIN_NAME, session);
session->timeout_task = GNUNET_SCHEDULER_add_delayed (
GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
return session;
}
+
/**
* If we have pending messages, ask the server to
* transmit them (schedule the respective tasks, etc.)
static void
process_pending_messages (struct Session *session);
+
/**
* Function called to notify a client about the socket
* being ready to queue more data. "buf" will be
* writing in the meantime.
*
* @param cls closure
- * @param size number of bytes available in buf
+ * @param size number of bytes available in @a buf
* @param buf where the callee should write the message
- * @return number of bytes written to buf
+ * @return number of bytes written to @a buf
*/
static size_t
do_transmit (void *cls, size_t size, void *buf)
GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
session->pending_messages_tail, pos);
GNUNET_assert(size >= pos->message_size);
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Transmitting message of type %u\n",
- ntohs (((struct GNUNET_MessageHeader * ) pos->msg)->type));
+ LOG(GNUNET_ERROR_TYPE_DEBUG, "Transmitting message of type %u size %u\n",
+ ntohs (((struct GNUNET_MessageHeader * ) pos->msg)->type),
+ pos->message_size);
/* FIXME: this memcpy can be up to 7% of our total runtime */
memcpy (cbuf, pos->msg, pos->message_size);
cbuf += pos->message_size;
return ret;
}
+
/**
* If we have pending messages, ask the server to
* transmit them (schedule the respective tasks, etc.)
int res;
};
+
/**
* Function called to check if a session is in our maps.
*
}
#endif
+
/**
* Function that can be used by the transport service to transmit
* a message using the plugin. Note that in the case of a
};
+
/**
* Look for a session by address.
*
* @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
*/
static int
-session_lookup_it (void *cls, const struct GNUNET_PeerIdentity *key,
- void *value)
+session_lookup_it (void *cls,
+ const struct GNUNET_PeerIdentity *key,
+ void *value)
{
struct SessionItCtx * si_ctx = cls;
struct Session * session = value;
reschedule_session_timeout (session);
}
+/**
+ * Task to signal the server that we can continue
+ * receiving from the TCP client now.
+ *
+ * @param cls the `struct Session*`
+ * @param tc task context (unused)
+ */
+static void
+delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct Session *session = cls;
+
+ session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
+ reschedule_session_timeout (session);
+
+ GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
+}
+
+static void tcp_plugin_update_inbound_delay (void *cls,
+ const struct GNUNET_PeerIdentity *peer,
+ struct Session *session,
+ struct GNUNET_TIME_Relative delay)
+{
+ if (GNUNET_SCHEDULER_NO_TASK == session->receive_delay_task)
+ return;
+
+ LOG(GNUNET_ERROR_TYPE_DEBUG,
+ "New inbound delay %llu us\n",delay.rel_value_us);
+
+ GNUNET_SCHEDULER_cancel (session->receive_delay_task);
+ session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
+ &delayed_done, session);
+}
+
+
/**
* Create a new session to transmit data to the target
* This session will used to send data to this peer and the plugin will
session = create_session (plugin, address,
GNUNET_SERVER_connect_socket (plugin->server, sa), GNUNET_NO);
- session->address = GNUNET_HELLO_address_copy (address);
session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
ats.value);
GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
{
struct Plugin *plugin = cls;
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s'\n",
+ LOG(GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnecting peer `%4s'\n",
GNUNET_i2s (target));
GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, target,
&session_disconnect_it, plugin);
static struct PrettyPrinterContext *ppc_dll_tail;
/**
- * Context for address to string conversion.
+ * Context for address to string conversion, closure
+ * for #append_port().
*/
struct PrettyPrinterContext
{
GNUNET_TRANSPORT_AddressStringCallback asc;
/**
- * Clsoure for 'asc'.
+ * Clsoure for @e asc.
*/
void *asc_cls;
uint32_t options;
};
-static void
-ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct PrettyPrinterContext *ppc = cls;
-
- ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
- if (NULL != ppc->resolver_handle)
- {
- GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
- ppc->resolver_handle = NULL;
- }
- GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
- GNUNET_free(ppc);
-}
/**
* Append our port and forward the result.
*
- * @param cls the 'struct PrettyPrinterContext*'
+ * @param cls the `struct PrettyPrinterContext *`
* @param hostname hostname part of the address
*/
static void
-append_port (void *cls, const char *hostname)
+append_port (void *cls,
+ const char *hostname)
{
struct PrettyPrinterContext *ppc = cls;
- struct PrettyPrinterContext *cur;
char *ret;
if (NULL == hostname)
{
- ppc->asc (ppc->asc_cls, NULL );
- GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
- GNUNET_SCHEDULER_cancel (ppc->timeout_task);
- ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ /* Final call, done */
ppc->resolver_handle = NULL;
- GNUNET_free(ppc);
- return;
- }
- for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
- if (cur == ppc)
- break;
- if (NULL == cur)
- {
- GNUNET_break(0);
+ GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
+ ppc_dll_tail,
+ ppc);
+ ppc->asc (ppc->asc_cls,
+ NULL,
+ GNUNET_OK);
+ GNUNET_free (ppc);
return;
}
-
if (GNUNET_YES == ppc->ipv6)
- GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname,
- ppc->port);
+ GNUNET_asprintf (&ret,
+ "%s.%u.[%s]:%d",
+ PLUGIN_NAME,
+ ppc->options,
+ hostname,
+ ppc->port);
else
- GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname,
- ppc->port);
- ppc->asc (ppc->asc_cls, ret);
- GNUNET_free(ret);
+ GNUNET_asprintf (&ret,
+ "%s.%u.%s:%d",
+ PLUGIN_NAME,
+ ppc->options,
+ hostname,
+ ppc->port);
+ ppc->asc (ppc->asc_cls,
+ ret,
+ GNUNET_OK);
+ GNUNET_free (ret);
}
+
/**
* Convert the transports address to a nice, human-readable
* format.
* @param type name of the transport that generated the address
* @param addr one of the addresses of the host, NULL for the last address
* the specific address format depends on the transport
- * @param addrlen length of the address
+ * @param addrlen length of the @a addr
* @param numeric should (IP) addresses be displayed in numeric form?
* @param timeout after how long should we give up?
* @param asc function to call on each string
- * @param asc_cls closure for asc
+ * @param asc_cls closure for @a asc
*/
static void
-tcp_plugin_address_pretty_printer (void *cls, const char *type,
- const void *addr, size_t addrlen, int numeric,
- struct GNUNET_TIME_Relative timeout,
- GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
+tcp_plugin_address_pretty_printer (void *cls,
+ const char *type,
+ const void *addr,
+ size_t addrlen,
+ int numeric,
+ struct GNUNET_TIME_Relative timeout,
+ GNUNET_TRANSPORT_AddressStringCallback asc,
+ void *asc_cls)
{
struct PrettyPrinterContext *ppc;
const void *sb;
uint16_t port;
uint32_t options;
- if (addrlen == sizeof(struct IPv6TcpAddress))
+ if (sizeof(struct IPv6TcpAddress) == addrlen)
{
t6 = addr;
memset (&a6, 0, sizeof(a6));
sb = &a6;
sbs = sizeof(a6);
}
- else if (addrlen == sizeof(struct IPv4TcpAddress))
+ else if (sizeof(struct IPv4TcpAddress) == addrlen)
{
t4 = addr;
memset (&a4, 0, sizeof(a4));
sb = &a4;
sbs = sizeof(a4);
}
- else if (0 == addrlen)
- {
- asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
- asc (asc_cls, NULL );
- return;
- }
else
{
/* invalid address */
- GNUNET_break_op(0);
- asc (asc_cls, NULL );
+ asc (asc_cls, NULL, GNUNET_SYSERR);
+ asc (asc_cls, NULL, GNUNET_OK);
return;
}
ppc = GNUNET_new (struct PrettyPrinterContext);
ppc->asc_cls = asc_cls;
ppc->port = port;
ppc->options = options;
- ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (
- GNUNET_TIME_relative_multiply (timeout, 2), &ppc_cancel_task, ppc);
- ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
- timeout, &append_port, ppc);
- if (NULL != ppc->resolver_handle)
+ ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
+ sbs,
+ ! numeric,
+ timeout,
+ &append_port, ppc);
+ if (NULL == ppc->resolver_handle)
{
- //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding request %p\n", ppc);
- GNUNET_CONTAINER_DLL_insert(ppc_dll_head, ppc_dll_tail, ppc);
- }
- else
- {
- GNUNET_break(0);
- GNUNET_free(ppc);
+ GNUNET_break (0);
+ GNUNET_free (ppc);
+ return;
}
+ GNUNET_CONTAINER_DLL_insert (ppc_dll_head,
+ ppc_dll_tail,
+ ppc);
}
+
/**
* Check if the given port is plausible (must be either our listen
* port or our advertised port), or any port if we are behind NAT
case AF_INET:
s4 = vaddr;
t4 = GNUNET_new (struct IPv4TcpAddress);
- t4->options = 0;
+ t4->options = htonl(0);
t4->t4_port = s4->sin_port;
t4->ipv4_addr = s4->sin_addr.s_addr;
session->address = GNUNET_HELLO_address_allocate (
&tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t4,
- sizeof(struct IPv4TcpAddress), GNUNET_HELLO_ADDRESS_INFO_NONE);
+ sizeof(struct IPv4TcpAddress),
+ GNUNET_HELLO_ADDRESS_INFO_NONE);
break;
case AF_INET6:
s6 = vaddr;
t6 = GNUNET_new (struct IPv6TcpAddress);
- t6->options = 0;
+ t6->options = htonl(0);
t6->t6_port = s6->sin6_port;
memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
session->address = GNUNET_HELLO_address_allocate (
- &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t6,
- sizeof(struct IPv6TcpAddress), GNUNET_HELLO_ADDRESS_INFO_NONE);
+ &tcp_nat_probe->clientIdentity,
+ PLUGIN_NAME, &t6, sizeof(struct IPv6TcpAddress),
+ GNUNET_HELLO_ADDRESS_INFO_NONE);
break;
default:
GNUNET_break_op(0);
const struct sockaddr_in6 *s6;
struct GNUNET_ATS_Information ats;
+
if (0 == memcmp (&wm->clientIdentity, plugin->env->my_identity,
sizeof(struct GNUNET_PeerIdentity)))
{
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
{
- LOG(GNUNET_ERROR_TYPE_WARNING,
+ LOG(GNUNET_ERROR_TYPE_INFO,
"Received %s message from my own identity `%4s' on address `%s'\n",
"WELCOME", GNUNET_i2s (&wm->clientIdentity),
GNUNET_a2s (vaddr, alen));
GNUNET_free(vaddr);
}
- GNUNET_break_op(0);
return;
}
+
LOG(GNUNET_ERROR_TYPE_DEBUG, "Received %s message from `%4s' %p\n", "WELCOME",
GNUNET_i2s (&wm->clientIdentity), client);
GNUNET_STATISTICS_update (plugin->env->stats,
t4.t4_port = s4->sin_port;
t4.ipv4_addr = s4->sin_addr.s_addr;
address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
- PLUGIN_NAME, &t4, sizeof(struct IPv4TcpAddress),
+ PLUGIN_NAME, &t4, sizeof(t4),
GNUNET_HELLO_ADDRESS_INFO_INBOUND);
}
else if (alen == sizeof(struct sockaddr_in6))
t6.t6_port = s6->sin6_port;
memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
- PLUGIN_NAME, &t6, sizeof(struct IPv6TcpAddress),
+ PLUGIN_NAME, &t6, sizeof (t6),
GNUNET_HELLO_ADDRESS_INFO_INBOUND);
}
+ else
+ {
+ GNUNET_break (0);
+ GNUNET_free_non_null (vaddr);
+ return;
+ }
session = create_session (plugin, address, client, GNUNET_NO);
GNUNET_HELLO_address_free (address);
ats = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
ats.value);
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new%s session %p for peer `%s'\n",
- GNUNET_HELLO_address_check_option (session->address, GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? " inbound" : "",
- session,
- tcp_address_to_string(NULL, (void *) session->address->address,session->address->address_length));
+ LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new%s session %p for peer `%s' client %p \n",
+ GNUNET_HELLO_address_check_option (session->address,
+ GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? " inbound" : "", session,
+ tcp_address_to_string(NULL, (void *) session->address->address,
+ session->address->address_length),
+ client);
GNUNET_free(vaddr);
GNUNET_SERVER_client_set_user_context(session->client, session);
GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+
+ /* Notify transport and ATS about new session */
+ plugin->env->session_start (NULL, session->address, session, &ats, 1);
}
else
{
LOG(GNUNET_ERROR_TYPE_DEBUG,
"Did not obtain TCP socket address for incoming connection\n");
GNUNET_break(0);
+ return;
}
}
session->last_activity = GNUNET_TIME_absolute_get ();
session->expecting_welcome = GNUNET_NO;
- /* Notify transport and ATS about new session */
- plugin->env->session_start (NULL, session->address, session, &ats, 1);
-
process_pending_messages (session);
GNUNET_SERVER_client_set_timeout (client,
GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
GNUNET_SERVER_receive_done (client, GNUNET_OK);
}
-/**
- * Task to signal the server that we can continue
- * receiving from the TCP client now.
- *
- * @param cls the `struct Session*`
- * @param tc task context (unused)
- */
-static void
-delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- struct Session *session = cls;
-
- session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
- reschedule_session_timeout (session);
-
- GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
-}
/**
* We've received data for this peer via TCP. Unbox,
}
else
{
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Throttling receiving from `%s' for %s\n",
+ LOG(GNUNET_ERROR_TYPE_DEBUG,
+ "Throttling receiving from `%s' for %s\n",
GNUNET_i2s (&session->target),
GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
GNUNET_SERVER_disable_receive_done_warning (client);
struct Plugin *plugin = cls;
struct Session *session;
- if (client == NULL )
+ if (NULL == client)
return;
session = lookup_session_by_client (plugin, client);
- if (session == NULL )
+ if (NULL == session)
return; /* unknown, nothing to do */
LOG(GNUNET_ERROR_TYPE_DEBUG,
"Destroying session of `%4s' with %s due to network-level disconnect.\n",
* @param cls closure, a struct TCPProbeContext
* @param size max size to copy
* @param buf buffer to copy message to
- * @return number of bytes copied into buf
+ * @return number of bytes copied into @a buf
*/
static size_t
-notify_send_probe (void *cls, size_t size, void *buf)
+notify_send_probe (void *cls,
+ size_t size,
+ void *buf)
{
struct TCPProbeContext *tcp_probe_ctx = cls;
struct Plugin *plugin = tcp_probe_ctx->plugin;
return ret;
}
+
/**
* Function called by the NAT subsystem suggesting another peer wants
* to connect to us via connection reversal. Try to connect back to the
* @param addrlen number of bytes in @a addr
*/
static void
-try_connection_reversal (void *cls, const struct sockaddr *addr,
- socklen_t addrlen)
+try_connection_reversal (void *cls,
+ const struct sockaddr *addr,
+ socklen_t addrlen)
{
struct Plugin *plugin = cls;
struct GNUNET_CONNECTION_Handle *sock;
return;
}
- /* FIXME: do we need to track these probe context objects so that
- * we can clean them up on plugin unload? */
tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
tcp_probe_ctx->message.header.size = htons (
sizeof(struct TCP_NAT_ProbeMessage));
}
+
/**
* Function obtain the network type for a session
*
* @return the network type in HBO or #GNUNET_SYSERR
*/
static enum GNUNET_ATS_Network_Type
-tcp_get_network (void *cls, struct Session *session)
+tcp_get_network (void *cls,
+ struct Session *session)
{
- GNUNET_assert(NULL != session);
+ struct Plugin * plugin = cls;
+
+ GNUNET_assert (NULL != plugin);
+ GNUNET_assert (NULL != session);
+ if (GNUNET_SYSERR == find_session (plugin,session))
+ return GNUNET_ATS_NET_UNSPECIFIED;
return session->ats_address_network_type;
}
+
/**
* Entry point for the plugin.
*
void *
libgnunet_plugin_transport_tcp_init (void *cls)
{
- static const struct GNUNET_SERVER_MessageHandler my_handlers[] = { {
- &handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
- sizeof(struct WelcomeMessage) }, { &handle_tcp_nat_probe, NULL,
- GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
- sizeof(struct TCP_NAT_ProbeMessage) }, { &handle_tcp_data, NULL,
- GNUNET_MESSAGE_TYPE_ALL, 0 }, { NULL, NULL, 0, 0 } };
+ static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
+ { &handle_tcp_welcome, NULL,
+ GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, sizeof(struct WelcomeMessage) },
+ { &handle_tcp_nat_probe, NULL,
+ GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof(struct TCP_NAT_ProbeMessage) },
+ { &handle_tcp_data, NULL,
+ GNUNET_MESSAGE_TYPE_ALL, 0 },
+ { NULL, NULL, 0, 0 }
+ };
struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
struct GNUNET_TRANSPORT_PluginFunctions *api;
struct Plugin *plugin;
return api;
}
- GNUNET_assert(NULL != env->cfg);
- if (GNUNET_OK
- != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
- "MAX_CONNECTIONS", &max_connections))
+ GNUNET_assert (NULL != env->cfg);
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_number (env->cfg,
+ "transport-tcp",
+ "MAX_CONNECTIONS",
+ &max_connections))
max_connections = 128;
aport = 0;
aport = 0;
if (bport != 0)
{
- service = GNUNET_SERVICE_start ("transport-tcp", env->cfg,
- GNUNET_SERVICE_OPTION_NONE);
- if (service == NULL )
+ service = GNUNET_SERVICE_start ("transport-tcp",
+ env->cfg,
+ GNUNET_SERVICE_OPTION_NONE);
+ if (service == NULL)
{
- LOG(GNUNET_ERROR_TYPE_WARNING, _("Failed to start service.\n"));
- return NULL ;
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ _("Failed to start service.\n"));
+ return NULL;
}
}
else
plugin->adv_port = aport;
plugin->env = env;
plugin->lsock = NULL;
- if ((service != NULL )&&
- (GNUNET_SYSERR !=
- (ret_s =
- GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
- &addrlens)))){
- for (ret = ret_s-1; ret >= 0; ret--)
- LOG (GNUNET_ERROR_TYPE_INFO,
- "Binding to address `%s'\n",
- GNUNET_a2s (addrs[ret], addrlens[ret]));
- plugin->nat =
- GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
- (const struct sockaddr **) addrs, addrlens,
- &tcp_nat_port_map_callback,
- &try_connection_reversal, plugin);
- for (ret = ret_s -1; ret >= 0; ret--)
- {
- GNUNET_assert (addrs[ret] != NULL);
- GNUNET_free (addrs[ret]);
- }
- GNUNET_free_non_null (addrs);
- GNUNET_free_non_null (addrlens);
-}
-else
-{
- plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
- GNUNET_YES, 0, 0, NULL, NULL, NULL,
- &try_connection_reversal, plugin);
-}
+ if ( (service != NULL) &&
+ (GNUNET_SYSERR !=
+ (ret_s =
+ GNUNET_SERVICE_get_server_addresses ("transport-tcp",
+ env->cfg,
+ &addrs,
+ &addrlens))))
+ {
+ for (ret = ret_s-1; ret >= 0; ret--)
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ "Binding to address `%s'\n",
+ GNUNET_a2s (addrs[ret], addrlens[ret]));
+ plugin->nat
+ = GNUNET_NAT_register (env->cfg,
+ GNUNET_YES,
+ aport,
+ (unsigned int) ret_s,
+ (const struct sockaddr **) addrs, addrlens,
+ &tcp_nat_port_map_callback,
+ &try_connection_reversal,
+ plugin);
+ for (ret = ret_s -1; ret >= 0; ret--)
+ GNUNET_free (addrs[ret]);
+ GNUNET_free_non_null (addrs);
+ GNUNET_free_non_null (addrlens);
+ }
+ else
+ {
+ plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
+ GNUNET_YES, 0, 0, NULL, NULL, NULL,
+ &try_connection_reversal, plugin);
+ }
api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
api->cls = plugin;
api->send = &tcp_plugin_send;
api->get_session = &tcp_plugin_get_session;
-
api->disconnect_session = &tcp_disconnect_session;
api->query_keepalive_factor = &tcp_query_keepalive_factor;
api->disconnect_peer = &tcp_plugin_disconnect;
api->string_to_address = &tcp_string_to_address;
api->get_network = &tcp_get_network;
api->update_session_timeout = &tcp_plugin_update_session_timeout;
+ api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
plugin->service = service;
if (NULL != service)
{
}
else
{
- if (GNUNET_OK
- != GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
- "TIMEOUT", &idle_timeout))
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_time (env->cfg,
+ "transport-tcp",
+ "TIMEOUT",
+ &idle_timeout))
{
- GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "transport-tcp",
- "TIMEOUT");
- if (plugin->nat != NULL )
+ GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+ "transport-tcp",
+ "TIMEOUT");
+ if (NULL != plugin->nat)
GNUNET_NAT_unregister (plugin->nat);
GNUNET_free(plugin);
GNUNET_free(api);
- return NULL ;
+ return NULL;
}
- plugin->server = GNUNET_SERVER_create_with_sockets (
- &plugin_tcp_access_check, plugin, NULL, idle_timeout, GNUNET_YES);
+ plugin->server
+ = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check,
+ plugin, NULL,
+ idle_timeout, GNUNET_YES);
}
plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
- for (i = 0;
- i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);
- i++)
+ for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
plugin->handlers[i].callback_cls = plugin;
GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
- GNUNET_YES);
- if (bport != 0)
- LOG(GNUNET_ERROR_TYPE_INFO, _("TCP transport listening on port %llu\n"),
- bport);
+ GNUNET_YES);
+ if (0 != bport)
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ _("TCP transport listening on port %llu\n"),
+ bport);
else
- LOG(GNUNET_ERROR_TYPE_INFO,
- _("TCP transport not listening on any port (client only)\n"));
- if (aport != bport)
- LOG(GNUNET_ERROR_TYPE_INFO,
- _("TCP transport advertises itself as being on port %llu\n"), aport);
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ _("TCP transport not listening on any port (client only)\n"));
+ if ( (aport != bport) &&
+ (0 != bport) )
+ LOG (GNUNET_ERROR_TYPE_INFO,
+ _("TCP transport advertises itself as being on port %llu\n"),
+ aport);
/* Initially set connections to 0 */
GNUNET_assert(NULL != plugin->env->stats);
GNUNET_STATISTICS_set (plugin->env->stats,
return api;
}
+
/**
* Exit point from the plugin.
*
GNUNET_free(api);
return NULL ;
}
- LOG(GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Shutting down TCP plugin\n");
/* Removing leftover sessions */
GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
- &session_disconnect_it, plugin);
+ &session_disconnect_it,
+ plugin);
/* Removing leftover NAT sessions */
GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
- &session_disconnect_it, plugin);
+ &session_disconnect_it,
+ plugin);
next = ppc_dll_head;
for (cur = next; NULL != cur; cur = next)
{
+ GNUNET_break (0);
next = cur->next;
- GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, cur);
- if (NULL != cur->resolver_handle)
- GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
- GNUNET_SCHEDULER_cancel (cur->timeout_task);
- GNUNET_free(cur);
- GNUNET_break(0);
+ GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
+ ppc_dll_tail,
+ cur);
+ GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
+ GNUNET_free (cur);
}
- if (plugin->service != NULL )
+ if (NULL != plugin->service)
GNUNET_SERVICE_stop (plugin->service);
else
GNUNET_SERVER_destroy (plugin->server);
GNUNET_free(plugin->handlers);
- if (plugin->nat != NULL )
+ if (NULL != plugin->nat)
GNUNET_NAT_unregister (plugin->nat);
while (NULL != (tcp_probe = plugin->probe_head))
{
GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
GNUNET_free(plugin);
GNUNET_free(api);
- return NULL ;
+ return NULL;
}
/* end of plugin_transport_tcp.c */