2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_tcp.c
23 * @brief Implementation of the TCP transport service
24 * @author Christian Grothoff
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_container_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_service_lib.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "plugin_transport.h"
40 #include "transport.h"
42 #define DEBUG_TCP GNUNET_NO
45 * How long until we give up on transmitting the welcome message?
47 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
51 * Initial handshake message for a session.
56 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
58 struct GNUNET_MessageHeader header;
61 * Identity of the node connecting (TCP client)
63 struct GNUNET_PeerIdentity clientIdentity;
69 * Encapsulation of all of the state of the plugin.
75 * Information kept for each message that is yet to
82 * This is a doubly-linked list.
84 struct PendingMessage *next;
87 * This is a doubly-linked list.
89 struct PendingMessage *prev;
97 * Continuation function to call once the message
98 * has been sent. Can be NULL if there is no
99 * continuation to call.
101 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
104 * Closure for transmit_cont.
106 void *transmit_cont_cls;
109 * Timeout value for the pending message.
111 struct GNUNET_TIME_Absolute timeout;
114 * So that the gnunet-service-transport can group messages together,
115 * these pending messages need to accept a message buffer and size
116 * instead of just a GNUNET_MessageHeader.
124 * Session handle for TCP connections.
130 * Stored in a linked list.
132 struct Session *next;
135 * Pointer to the global plugin struct.
137 struct Plugin *plugin;
140 * The client (used to identify this connection)
142 struct GNUNET_SERVER_Client *client;
145 * Messages currently pending for transmission
146 * to this peer, if any.
148 struct PendingMessage *pending_messages_head;
151 * Messages currently pending for transmission
152 * to this peer, if any.
154 struct PendingMessage *pending_messages_tail;
157 * Handle for pending transmission request.
159 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
162 * To whom are we talking to (set to our identity
163 * if we are still waiting for the welcome message)
165 struct GNUNET_PeerIdentity target;
168 * ID of task used to delay receiving more to throttle sender.
170 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
173 * Address of the other peer (either based on our 'connect'
174 * call or on our 'accept' call).
179 * Length of connect_addr.
184 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
186 int expecting_welcome;
192 * Encapsulation of all of the state of the plugin.
199 struct GNUNET_TRANSPORT_PluginEnvironment *env;
204 struct GNUNET_CONNECTION_Handle *lsock;
207 * List of open TCP sessions.
209 struct Session *sessions;
212 * Handle for the statistics service.
214 struct GNUNET_STATISTICS_Handle *statistics;
217 * Handle to the network service.
219 struct GNUNET_SERVICE_Context *service;
222 * Handle to the server for this service.
224 struct GNUNET_SERVER_Handle *server;
227 * Copy of the handler array where the closures are
228 * set to this struct's instance.
230 struct GNUNET_SERVER_MessageHandler *handlers;
233 * Handle for request of hostname resolution, non-NULL if pending.
235 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
238 * ID of task used to update our addresses when one expires.
240 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
243 * Port that we are actually listening on.
248 * Port that the user said we would have visible to the
257 * Find the session handle for the given client.
259 * @return NULL if no matching session exists
261 static struct Session *
262 find_session_by_client (struct Plugin *plugin,
263 const struct GNUNET_SERVER_Client *client)
267 ret = plugin->sessions;
268 while ((ret != NULL) && (client != ret->client))
275 * Create a new session. Also queues a welcome message.
278 * @param target peer to connect to
279 * @param client client to use
280 * @return new session object
282 static struct Session *
283 create_session (struct Plugin *plugin,
284 const struct GNUNET_PeerIdentity *target,
285 struct GNUNET_SERVER_Client *client)
288 struct PendingMessage *pm;
289 struct WelcomeMessage welcome;
291 GNUNET_assert (client != NULL);
292 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
294 "Creating new session for peer `%4s'\n",
295 GNUNET_i2s (target));
296 ret = GNUNET_malloc (sizeof (struct Session));
297 ret->plugin = plugin;
298 ret->next = plugin->sessions;
299 plugin->sessions = ret;
300 ret->client = client;
301 ret->target = *target;
302 ret->expecting_welcome = GNUNET_YES;
303 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
304 pm->msg = (const char*) &pm[1];
305 pm->message_size = sizeof (struct WelcomeMessage);
306 welcome.header.size = htons (sizeof (struct WelcomeMessage));
307 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
308 welcome.clientIdentity = *plugin->env->my_identity;
309 memcpy (&pm[1], &welcome, sizeof (welcome));
310 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
311 GNUNET_STATISTICS_update (plugin->env->stats,
312 gettext_noop ("# bytes currently in TCP buffers"),
315 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
316 ret->pending_messages_tail,
318 GNUNET_STATISTICS_update (plugin->env->stats,
319 gettext_noop ("# TCP sessions active"),
327 * If we have pending messages, ask the server to
328 * transmit them (schedule the respective tasks, etc.)
330 * @param session for which session should we do this
332 static void process_pending_messages (struct Session *session);
336 * Function called to notify a client about the socket
337 * being ready to queue more data. "buf" will be
338 * NULL and "size" zero if the socket was closed for
339 * writing in the meantime.
342 * @param size number of bytes available in buf
343 * @param buf where the callee should write the message
344 * @return number of bytes written to buf
347 do_transmit (void *cls, size_t size, void *buf)
349 struct Session *session = cls;
350 struct PendingMessage *pm;
355 session->transmit_handle = NULL;
359 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
361 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
362 GNUNET_i2s (&session->target));
365 while (NULL != (pm = session->pending_messages_head))
367 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
368 session->pending_messages_tail,
371 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
373 "Failed to transmit %u byte message to `%4s'.\n",
375 GNUNET_i2s (&session->target));
377 GNUNET_STATISTICS_update (session->plugin->env->stats,
378 gettext_noop ("# bytes currently in TCP buffers"),
379 - (int64_t) pm->message_size,
381 GNUNET_STATISTICS_update (session->plugin->env->stats,
382 gettext_noop ("# bytes discarded by TCP (timeout)"),
385 if (pm->transmit_cont != NULL)
386 pm->transmit_cont (pm->transmit_cont_cls,
387 &session->target, GNUNET_SYSERR);
394 while (NULL != (pm = session->pending_messages_head))
396 if (size < pm->message_size)
398 memcpy (cbuf, pm->msg, pm->message_size);
399 cbuf += pm->message_size;
400 ret += pm->message_size;
401 size -= pm->message_size;
402 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
403 session->pending_messages_tail,
405 GNUNET_STATISTICS_update (session->plugin->env->stats,
406 gettext_noop ("# bytes currently in TCP buffers"),
407 - (int64_t) pm->message_size,
409 if (pm->transmit_cont != NULL)
410 pm->transmit_cont (pm->transmit_cont_cls,
411 &session->target, GNUNET_OK);
414 if (session->client != NULL)
415 process_pending_messages (session);
417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
418 "tcp", "Transmitting %u bytes\n", ret);
420 GNUNET_STATISTICS_update (session->plugin->env->stats,
421 gettext_noop ("# bytes transmitted via TCP"),
429 * If we have pending messages, ask the server to
430 * transmit them (schedule the respective tasks, etc.)
432 * @param session for which session should we do this
435 process_pending_messages (struct Session *session)
437 struct PendingMessage *pm;
438 GNUNET_assert (session->client != NULL);
439 if (session->transmit_handle != NULL)
441 if (NULL == (pm = session->pending_messages_head))
443 session->transmit_handle
444 = GNUNET_SERVER_notify_transmit_ready (session->client,
446 GNUNET_TIME_absolute_get_remaining
448 &do_transmit, session);
453 * Functions with this signature are called whenever we need
454 * to close a session due to a disconnect or failure to
455 * establish a connection.
457 * @param session session to close down
460 disconnect_session (struct Session *session)
462 struct Session *prev;
464 struct PendingMessage *pm;
467 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
469 "Disconnecting from `%4s' at %s (session %p).\n",
470 GNUNET_i2s (&session->target),
471 (session->connect_addr != NULL) ?
472 GNUNET_a2s (session->connect_addr,
473 session->connect_alen) : "*", session);
475 /* remove from session list */
477 pos = session->plugin->sessions;
478 while (pos != session)
484 session->plugin->sessions = session->next;
486 prev->next = session->next;
488 if (session->transmit_handle != NULL)
490 GNUNET_CONNECTION_notify_transmit_ready_cancel
491 (session->transmit_handle);
492 session->transmit_handle = NULL;
494 while (NULL != (pm = session->pending_messages_head))
497 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
499 pm->transmit_cont != NULL
500 ? "Could not deliver message to `%4s'.\n"
502 "Could not deliver message to `%4s', notifying.\n",
503 GNUNET_i2s (&session->target));
505 GNUNET_STATISTICS_update (session->plugin->env->stats,
506 gettext_noop ("# bytes currently in TCP buffers"),
507 - (int64_t) pm->message_size,
509 GNUNET_STATISTICS_update (session->plugin->env->stats,
510 gettext_noop ("# bytes discarded by TCP (disconnect)"),
513 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
514 session->pending_messages_tail,
516 if (NULL != pm->transmit_cont)
517 pm->transmit_cont (pm->transmit_cont_cls,
518 &session->target, GNUNET_SYSERR);
521 if (GNUNET_NO == session->expecting_welcome)
524 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
526 "Notifying transport service about loss of connection with `%4s'.\n",
527 GNUNET_i2s (&session->target));
529 /* Data session that actually went past the initial handshake;
530 transport service may know about this one, so we need to
531 notify transport service about disconnect */
532 // FIXME: we should have a very clear connect-disconnect
533 // protocol with gnunet-service-transport!
534 // FIXME: but this is not possible for all plugins, so what gives?
536 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
538 GNUNET_SCHEDULER_cancel (session->plugin->env->sched,
539 session->receive_delay_task);
540 if (session->client != NULL)
542 GNUNET_SERVER_receive_done (session->client,
546 if (session->client != NULL)
548 GNUNET_SERVER_client_drop (session->client);
549 session->client = NULL;
551 GNUNET_STATISTICS_update (session->plugin->env->stats,
552 gettext_noop ("# TCP sessions active"),
555 GNUNET_free_non_null (session->connect_addr);
556 GNUNET_free (session);
561 * Function that can be used by the transport service to transmit
562 * a message using the plugin. Note that in the case of a
563 * peer disconnecting, the continuation MUST be called
564 * prior to the disconnect notification itself. This function
565 * will be called with this peer's HELLO message to initiate
566 * a fresh connection to another peer.
569 * @param target who should receive this message
570 * @param msg the message to transmit
571 * @param msgbuf_size number of bytes in 'msg'
572 * @param priority how important is the message (most plugins will
573 * ignore message priority and just FIFO)
574 * @param timeout how long to wait at most for the transmission (does not
575 * require plugins to discard the message after the timeout,
576 * just advisory for the desired delay; most plugins will ignore
578 * @param addr the address to use (can be NULL if the plugin
579 * is "on its own" (i.e. re-use existing TCP connection))
580 * @param addrlen length of the address in bytes
581 * @param force_address GNUNET_YES if the plugin MUST use the given address,
582 * otherwise the plugin may use other addresses or
583 * existing connections (if available)
584 * @param cont continuation to call once the message has
585 * been transmitted (or if the transport is ready
586 * for the next transmission call; or if the
587 * peer disconnected...); can be NULL
588 * @param cont_cls closure for cont
589 * @return number of bytes used (on the physical network, with overheads);
590 * -1 on hard errors (i.e. address invalid); 0 is a legal value
591 * and does NOT mean that the message was not transmitted (DV)
594 tcp_plugin_send (void *cls,
595 const struct GNUNET_PeerIdentity *target,
599 struct GNUNET_TIME_Relative timeout,
603 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
605 struct Plugin *plugin = cls;
606 struct Session *session;
607 struct PendingMessage *pm;
608 struct GNUNET_CONNECTION_Handle *sa;
611 GNUNET_STATISTICS_update (plugin->env->stats,
612 gettext_noop ("# bytes TCP was asked to transmit"),
615 session = plugin->sessions;
616 /* FIXME: we could do this a cheaper with a hash table
617 where we could restrict the iteration to entries that match
618 the target peer... */
619 while ( (session != NULL) &&
620 (session->client != NULL) &&
621 ( (0 != memcmp (target,
623 sizeof (struct GNUNET_PeerIdentity))) ||
624 ( (GNUNET_YES == force_address) &&
626 ( (addrlen != session->connect_alen) ||
627 (0 != memcmp (session->connect_addr,
630 session = session->next;
631 if ( (session == NULL) &&
635 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
637 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
638 GNUNET_i2s (target));
640 GNUNET_STATISTICS_update (plugin->env->stats,
641 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
648 if (sizeof (struct sockaddr_in) == addrlen)
650 else if (sizeof (struct sockaddr_in6) == addrlen)
657 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
659 GNUNET_SERVER_MAX_MESSAGE_SIZE);
663 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
665 "Failed to create connection to `%4s' at `%s'\n",
667 GNUNET_a2s (addr, addrlen));
669 GNUNET_STATISTICS_update (plugin->env->stats,
670 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
677 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
679 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
681 GNUNET_a2s (addr, addrlen));
683 session = create_session (plugin,
685 GNUNET_SERVER_connect_socket (plugin->server,
687 session->connect_addr = GNUNET_malloc (addrlen);
688 memcpy (session->connect_addr,
691 session->connect_alen = addrlen;
693 GNUNET_assert (session != NULL);
694 GNUNET_assert (session->client != NULL);
696 GNUNET_STATISTICS_update (plugin->env->stats,
697 gettext_noop ("# bytes currently in TCP buffers"),
700 /* create new message entry */
701 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
702 pm->msg = (const char*) &pm[1];
703 memcpy (&pm[1], msg, msgbuf_size);
704 pm->message_size = msgbuf_size;
705 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
706 pm->transmit_cont = cont;
707 pm->transmit_cont_cls = cont_cls;
709 /* append pm to pending_messages list */
710 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
711 session->pending_messages_tail,
712 session->pending_messages_tail,
715 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
717 "Asked to transmit %u bytes to `%s', added message to list.\n",
719 GNUNET_i2s (target));
721 process_pending_messages (session);
727 * Function that can be called to force a disconnect from the
728 * specified neighbour. This should also cancel all previously
729 * scheduled transmissions. Obviously the transmission may have been
730 * partially completed already, which is OK. The plugin is supposed
731 * to close the connection (if applicable) and no longer call the
732 * transmit continuation(s).
734 * Finally, plugin MUST NOT call the services's receive function to
735 * notify the service that the connection to the specified target was
736 * closed after a getting this call.
739 * @param target peer for which the last transmission is
743 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
745 struct Plugin *plugin = cls;
746 struct Session *session;
747 struct PendingMessage *pm;
750 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
752 "Asked to cancel session with `%4s'\n",
753 GNUNET_i2s (target));
755 session = plugin->sessions;
756 while (NULL != session)
758 if (0 == memcmp (target,
760 sizeof (struct GNUNET_PeerIdentity)))
762 pm = session->pending_messages_head;
765 pm->transmit_cont = NULL;
766 pm->transmit_cont_cls = NULL;
769 if (session->client != NULL)
771 GNUNET_SERVER_client_drop (session->client);
772 session->client = NULL;
774 /* rest of the clean-up of the session will be done as part of
775 disconnect_notify which should be triggered any time now
776 (or which may be triggering this call in the first place) */
778 session = session->next;
783 struct PrettyPrinterContext
785 GNUNET_TRANSPORT_AddressStringCallback asc;
792 * Append our port and forward the result.
795 append_port (void *cls, const char *hostname)
797 struct PrettyPrinterContext *ppc = cls;
800 if (hostname == NULL)
802 ppc->asc (ppc->asc_cls, NULL);
806 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
807 ppc->asc (ppc->asc_cls, ret);
813 * Convert the transports address to a nice, human-readable
817 * @param type name of the transport that generated the address
818 * @param addr one of the addresses of the host, NULL for the last address
819 * the specific address format depends on the transport
820 * @param addrlen length of the address
821 * @param numeric should (IP) addresses be displayed in numeric form?
822 * @param timeout after how long should we give up?
823 * @param asc function to call on each string
824 * @param asc_cls closure for asc
827 tcp_plugin_address_pretty_printer (void *cls,
832 struct GNUNET_TIME_Relative timeout,
833 GNUNET_TRANSPORT_AddressStringCallback asc,
836 struct Plugin *plugin = cls;
837 const struct sockaddr_in *v4;
838 const struct sockaddr_in6 *v6;
839 struct PrettyPrinterContext *ppc;
841 if ((addrlen != sizeof (struct sockaddr_in)) &&
842 (addrlen != sizeof (struct sockaddr_in6)))
844 /* invalid address */
849 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
851 ppc->asc_cls = asc_cls;
852 if (addrlen == sizeof (struct sockaddr_in))
854 v4 = (const struct sockaddr_in *) addr;
855 ppc->port = ntohs (v4->sin_port);
859 v6 = (const struct sockaddr_in6 *) addr;
860 ppc->port = ntohs (v6->sin6_port);
863 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
867 !numeric, timeout, &append_port, ppc);
872 * Check if the given port is plausible (must be either
873 * our listen port or our advertised port). If it is
874 * neither, we return one of these two ports at random.
876 * @return either in_port or a more plausible port
879 check_port (struct Plugin *plugin, uint16_t in_port)
881 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
883 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
885 ? plugin->open_port : plugin->adv_port;
890 * Another peer has suggested an address for this peer and transport
891 * plugin. Check that this could be a valid address.
894 * @param addr pointer to the address
895 * @param addrlen length of addr
896 * @return GNUNET_OK if this is a plausible address for this peer
900 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
902 struct Plugin *plugin = cls;
903 char buf[sizeof (struct sockaddr_in6)];
904 struct sockaddr_in *v4;
905 struct sockaddr_in6 *v6;
907 if ((addrlen != sizeof (struct sockaddr_in)) &&
908 (addrlen != sizeof (struct sockaddr_in6)))
911 return GNUNET_SYSERR;
913 memcpy (buf, addr, sizeof (struct sockaddr_in6));
914 if (addrlen == sizeof (struct sockaddr_in))
916 v4 = (struct sockaddr_in *) buf;
917 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
921 v6 = (struct sockaddr_in6 *) buf;
922 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
925 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
927 "Informing transport service about my address `%s'.\n",
928 GNUNET_a2s (addr, addrlen));
935 * We've received a welcome from this peer via TCP. Possibly create a
936 * fresh client record and send back our welcome.
939 * @param client identification of the client
940 * @param message the actual message
943 handle_tcp_welcome (void *cls,
944 struct GNUNET_SERVER_Client *client,
945 const struct GNUNET_MessageHeader *message)
947 struct Plugin *plugin = cls;
948 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
949 struct Session *session;
954 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
956 "Received %s message from a `%4s/%p'.\n",
958 GNUNET_i2s (&wm->clientIdentity), client);
960 GNUNET_STATISTICS_update (plugin->env->stats,
961 gettext_noop ("# TCP WELCOME messages received"),
964 session = find_session_by_client (plugin, client);
967 GNUNET_SERVER_client_keep (client);
968 session = create_session (plugin,
969 &wm->clientIdentity, client);
971 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
974 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
976 "Found address `%s' for incoming connection %p\n",
977 GNUNET_a2s (vaddr, alen),
980 session->connect_addr = vaddr;
981 session->connect_alen = alen;
986 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
988 "Did not obtain TCP socket address for incoming connection\n");
992 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
994 "Creating new session %p for connection %p\n",
997 process_pending_messages (session);
999 if (session->expecting_welcome != GNUNET_YES)
1001 GNUNET_break_op (0);
1002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1005 session->expecting_welcome = GNUNET_NO;
1006 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1011 * Task to signal the server that we can continue
1012 * receiving from the TCP client now.
1015 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1017 struct Session *session = cls;
1018 struct GNUNET_TIME_Relative delay;
1020 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1021 delay = session->plugin->env->receive (session->plugin->env->cls,
1024 if (delay.value == 0)
1025 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1027 session->receive_delay_task =
1028 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1029 delay, &delayed_done, session);
1034 * We've received data for this peer via TCP. Unbox,
1035 * compute latency and forward.
1037 * @param cls closure
1038 * @param client identification of the client
1039 * @param message the actual message
1042 handle_tcp_data (void *cls,
1043 struct GNUNET_SERVER_Client *client,
1044 const struct GNUNET_MessageHeader *message)
1046 struct Plugin *plugin = cls;
1047 struct Session *session;
1048 struct GNUNET_TIME_Relative delay;
1050 session = find_session_by_client (plugin, client);
1051 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
1053 /* We don't want to propagate WELCOME messages up! */
1054 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1057 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1059 GNUNET_break_op (0);
1060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1064 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1066 "Passing %u bytes of type %u from `%4s' to transport service.\n",
1067 (unsigned int) ntohs (message->size),
1068 (unsigned int) ntohs (message->type),
1069 GNUNET_i2s (&session->target));
1071 GNUNET_STATISTICS_update (plugin->env->stats,
1072 gettext_noop ("# bytes received via TCP"),
1073 ntohs (message->size),
1075 delay = plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1076 session->connect_addr,
1077 session->connect_alen);
1079 if (delay.value == 0)
1080 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1082 session->receive_delay_task =
1083 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1084 delay, &delayed_done, session);
1089 * Handlers for the various TCP messages.
1091 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1092 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1093 sizeof (struct WelcomeMessage)},
1094 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1100 * Functions with this signature are called whenever a peer
1101 * is disconnected on the network level.
1103 * @param cls closure
1104 * @param client identification of the client
1107 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1109 struct Plugin *plugin = cls;
1110 struct Session *session;
1114 session = find_session_by_client (plugin, client);
1115 if (session == NULL)
1116 return; /* unknown, nothing to do */
1118 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1120 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1121 GNUNET_i2s (&session->target),
1122 (session->connect_addr != NULL) ?
1123 GNUNET_a2s (session->connect_addr,
1124 session->connect_alen) : "*", client);
1126 disconnect_session (session);
1131 * Add the IP of our network interface to the list of
1132 * our external IP addresses.
1135 process_interfaces (void *cls,
1138 const struct sockaddr *addr, socklen_t addrlen)
1140 struct Plugin *plugin = cls;
1142 struct sockaddr_in *v4;
1143 struct sockaddr_in6 *v6;
1145 af = addr->sa_family;
1148 v4 = (struct sockaddr_in *) addr;
1149 v4->sin_port = htons (plugin->adv_port);
1153 GNUNET_assert (af == AF_INET6);
1154 v6 = (struct sockaddr_in6 *) addr;
1155 v6->sin6_port = htons (plugin->adv_port);
1157 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1158 GNUNET_ERROR_TYPE_BULK,
1159 "tcp", _("Found address `%s' (%s)\n"),
1160 GNUNET_a2s (addr, addrlen), name);
1161 plugin->env->notify_address (plugin->env->cls,
1163 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1169 * Function called by the resolver for each address obtained from DNS
1170 * for our own hostname. Add the addresses to the list of our
1171 * external IP addresses.
1173 * @param cls closure
1174 * @param addr one of the addresses of the host, NULL for the last address
1175 * @param addrlen length of the address
1178 process_hostname_ips (void *cls,
1179 const struct sockaddr *addr, socklen_t addrlen)
1181 struct Plugin *plugin = cls;
1185 plugin->hostname_dns = NULL;
1188 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1193 * Entry point for the plugin.
1196 libgnunet_plugin_transport_tcp_init (void *cls)
1198 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1199 struct GNUNET_TRANSPORT_PluginFunctions *api;
1200 struct Plugin *plugin;
1201 struct GNUNET_SERVICE_Context *service;
1202 unsigned long long aport;
1203 unsigned long long bport;
1206 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1207 if (service == NULL)
1209 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1212 ("Failed to start service for `%s' transport plugin.\n"),
1218 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1224 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1227 &aport)) && (aport > 65535)))
1229 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1232 ("Require valid port number for service `%s' in configuration!\n"),
1234 GNUNET_SERVICE_stop (service);
1239 plugin = GNUNET_malloc (sizeof (struct Plugin));
1240 plugin->open_port = bport;
1241 plugin->adv_port = aport;
1243 plugin->lsock = NULL;
1244 plugin->statistics = NULL;
1245 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1247 api->send = &tcp_plugin_send;
1248 api->disconnect = &tcp_plugin_disconnect;
1249 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1250 api->check_address = &tcp_plugin_check_address;
1251 plugin->service = service;
1252 plugin->server = GNUNET_SERVICE_get_server (service);
1253 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1254 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1257 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1259 plugin->handlers[i].callback_cls = plugin;
1260 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1262 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1263 "tcp", _("TCP transport listening on port %llu\n"), bport);
1265 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1268 ("TCP transport advertises itself as being on port %llu\n"),
1270 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1272 /* FIXME: do the two calls below periodically again and
1273 not just once (since the info we get might change...) */
1274 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1275 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1278 HOSTNAME_RESOLVE_TIMEOUT,
1279 &process_hostname_ips,
1286 * Exit point from the plugin.
1289 libgnunet_plugin_transport_tcp_done (void *cls)
1291 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1292 struct Plugin *plugin = api->cls;
1293 struct Session *session;
1295 while (NULL != (session = plugin->sessions))
1296 disconnect_session (session);
1297 if (NULL != plugin->hostname_dns)
1299 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1300 plugin->hostname_dns = NULL;
1302 GNUNET_SERVICE_stop (plugin->service);
1303 GNUNET_free (plugin->handlers);
1304 GNUNET_free (plugin);
1309 /* end of plugin_transport_tcp.c */