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_log_from (GNUNET_ERROR_TYPE_DEBUG,
293 "Creating new session for peer `%4s'\n",
294 GNUNET_i2s (target));
295 ret = GNUNET_malloc (sizeof (struct Session));
296 ret->plugin = plugin;
297 ret->next = plugin->sessions;
298 plugin->sessions = ret;
299 ret->client = client;
300 ret->target = *target;
301 ret->expecting_welcome = GNUNET_YES;
302 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
303 pm->msg = (const char*) &pm[1];
304 pm->message_size = sizeof (struct WelcomeMessage);
305 welcome.header.size = htons (sizeof (struct WelcomeMessage));
306 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
307 welcome.clientIdentity = *plugin->env->my_identity;
308 memcpy (&pm[1], &welcome, sizeof (welcome));
309 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
310 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
311 ret->pending_messages_tail,
318 * If we have pending messages, ask the server to
319 * transmit them (schedule the respective tasks, etc.)
321 * @param session for which session should we do this
323 static void process_pending_messages (struct Session *session);
327 * Function called to notify a client about the socket
328 * being ready to queue more data. "buf" will be
329 * NULL and "size" zero if the socket was closed for
330 * writing in the meantime.
333 * @param size number of bytes available in buf
334 * @param buf where the callee should write the message
335 * @return number of bytes written to buf
338 do_transmit (void *cls, size_t size, void *buf)
340 struct Session *session = cls;
341 struct PendingMessage *pm;
346 session->transmit_handle = NULL;
350 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
352 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
353 GNUNET_i2s (&session->target));
356 while (NULL != (pm = session->pending_messages_head))
358 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
359 session->pending_messages_tail,
362 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
364 "Failed to transmit %u byte message to `%4s'.\n",
366 GNUNET_i2s (&session->target));
368 if (pm->transmit_cont != NULL)
369 pm->transmit_cont (pm->transmit_cont_cls,
370 &session->target, GNUNET_SYSERR);
377 while (NULL != (pm = session->pending_messages_head))
379 if (size < pm->message_size)
381 memcpy (cbuf, pm->msg, pm->message_size);
382 cbuf += pm->message_size;
383 ret += pm->message_size;
384 size -= pm->message_size;
385 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
386 session->pending_messages_tail,
388 if (pm->transmit_cont != NULL)
389 pm->transmit_cont (pm->transmit_cont_cls,
390 &session->target, GNUNET_OK);
393 if (session->client != NULL)
394 process_pending_messages (session);
396 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
397 "tcp", "Transmitting %u bytes\n", ret);
404 * If we have pending messages, ask the server to
405 * transmit them (schedule the respective tasks, etc.)
407 * @param session for which session should we do this
410 process_pending_messages (struct Session *session)
412 struct PendingMessage *pm;
413 GNUNET_assert (session->client != NULL);
414 if (session->transmit_handle != NULL)
416 if (NULL == (pm = session->pending_messages_head))
418 session->transmit_handle
419 = GNUNET_SERVER_notify_transmit_ready (session->client,
421 GNUNET_TIME_absolute_get_remaining
423 &do_transmit, session);
428 * Functions with this signature are called whenever we need
429 * to close a session due to a disconnect or failure to
430 * establish a connection.
432 * @param session session to close down
435 disconnect_session (struct Session *session)
437 struct Session *prev;
439 struct PendingMessage *pm;
442 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
444 "Disconnecting from `%4s' at %s (session %p).\n",
445 GNUNET_i2s (&session->target),
446 (session->connect_addr != NULL) ?
447 GNUNET_a2s (session->connect_addr,
448 session->connect_alen) : "*", session);
450 /* remove from session list */
452 pos = session->plugin->sessions;
453 while (pos != session)
459 session->plugin->sessions = session->next;
461 prev->next = session->next;
463 if (session->transmit_handle != NULL)
465 GNUNET_CONNECTION_notify_transmit_ready_cancel
466 (session->transmit_handle);
467 session->transmit_handle = NULL;
469 while (NULL != (pm = session->pending_messages_head))
472 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
474 pm->transmit_cont != NULL
475 ? "Could not deliver message to `%4s'.\n"
477 "Could not deliver message to `%4s', notifying.\n",
478 GNUNET_i2s (&session->target));
480 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
481 session->pending_messages_tail,
483 if (NULL != pm->transmit_cont)
484 pm->transmit_cont (pm->transmit_cont_cls,
485 &session->target, GNUNET_SYSERR);
488 if (GNUNET_NO == session->expecting_welcome)
491 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
493 "Notifying transport service about loss of connection with `%4s'.\n",
494 GNUNET_i2s (&session->target));
496 /* Data session that actually went past the initial handshake;
497 transport service may know about this one, so we need to
498 notify transport service about disconnect */
499 // FIXME: we should have a very clear connect-disconnect
500 // protocol with gnunet-service-transport!
501 // FIXME: but this is not possible for all plugins, so what gives?
503 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
505 GNUNET_SCHEDULER_cancel (session->plugin->env->sched,
506 session->receive_delay_task);
507 if (session->client != NULL)
509 GNUNET_SERVER_receive_done (session->client,
513 if (session->client != NULL)
515 GNUNET_SERVER_client_drop (session->client);
516 session->client = NULL;
518 GNUNET_free_non_null (session->connect_addr);
519 GNUNET_free (session);
524 * Function that can be used by the transport service to transmit
525 * a message using the plugin. Note that in the case of a
526 * peer disconnecting, the continuation MUST be called
527 * prior to the disconnect notification itself. This function
528 * will be called with this peer's HELLO message to initiate
529 * a fresh connection to another peer.
532 * @param target who should receive this message
533 * @param msg the message to transmit
534 * @param msgbuf_size number of bytes in 'msg'
535 * @param priority how important is the message (most plugins will
536 * ignore message priority and just FIFO)
537 * @param timeout how long to wait at most for the transmission (does not
538 * require plugins to discard the message after the timeout,
539 * just advisory for the desired delay; most plugins will ignore
541 * @param addr the address to use (can be NULL if the plugin
542 * is "on its own" (i.e. re-use existing TCP connection))
543 * @param addrlen length of the address in bytes
544 * @param force_address GNUNET_YES if the plugin MUST use the given address,
545 * otherwise the plugin may use other addresses or
546 * existing connections (if available)
547 * @param cont continuation to call once the message has
548 * been transmitted (or if the transport is ready
549 * for the next transmission call; or if the
550 * peer disconnected...); can be NULL
551 * @param cont_cls closure for cont
552 * @return number of bytes used (on the physical network, with overheads);
553 * -1 on hard errors (i.e. address invalid); 0 is a legal value
554 * and does NOT mean that the message was not transmitted (DV)
557 tcp_plugin_send (void *cls,
558 const struct GNUNET_PeerIdentity *target,
562 struct GNUNET_TIME_Relative timeout,
566 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
568 struct Plugin *plugin = cls;
569 struct Session *session;
570 struct PendingMessage *pm;
571 struct GNUNET_CONNECTION_Handle *sa;
574 session = plugin->sessions;
575 /* FIXME: we could do this a cheaper with a hash table
576 where we could restrict the iteration to entries that match
577 the target peer... */
578 while ( (session != NULL) &&
579 ( (0 != memcmp (target,
581 sizeof (struct GNUNET_PeerIdentity))) ||
582 ( (GNUNET_YES == force_address) &&
584 ( (addrlen != session->connect_alen) ||
585 (0 != memcmp (session->connect_addr,
588 session = session->next;
589 if ( (session == NULL) &&
593 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
595 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
596 GNUNET_i2s (target));
602 if (sizeof (struct sockaddr_in) == addrlen)
604 else if (sizeof (struct sockaddr_in6) == addrlen)
611 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
613 GNUNET_SERVER_MAX_MESSAGE_SIZE);
617 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
619 "Failed to create connection to `%4s' at `%s'\n",
621 GNUNET_a2s (addr, addrlen));
627 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
629 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
631 GNUNET_a2s (addr, addrlen));
633 session = create_session (plugin,
635 GNUNET_SERVER_connect_socket (plugin->server,
637 session->connect_addr = GNUNET_malloc (addrlen);
638 memcpy (session->connect_addr,
641 session->connect_alen = addrlen;
643 GNUNET_assert (session != NULL);
644 GNUNET_assert (session->client != NULL);
646 /* create new message entry */
647 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
648 pm->msg = (const char*) &pm[1];
649 memcpy (&pm[1], msg, msgbuf_size);
650 pm->message_size = msgbuf_size;
651 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
652 pm->transmit_cont = cont;
653 pm->transmit_cont_cls = cont_cls;
655 /* append pm to pending_messages list */
656 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
657 session->pending_messages_tail,
658 session->pending_messages_tail,
661 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
663 "Asked to transmit %u bytes to `%s', added message to list.\n",
665 GNUNET_i2s (target));
667 process_pending_messages (session);
673 * Function that can be called to force a disconnect from the
674 * specified neighbour. This should also cancel all previously
675 * scheduled transmissions. Obviously the transmission may have been
676 * partially completed already, which is OK. The plugin is supposed
677 * to close the connection (if applicable) and no longer call the
678 * transmit continuation(s).
680 * Finally, plugin MUST NOT call the services's receive function to
681 * notify the service that the connection to the specified target was
682 * closed after a getting this call.
685 * @param target peer for which the last transmission is
689 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
691 struct Plugin *plugin = cls;
692 struct Session *session;
693 struct PendingMessage *pm;
696 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
698 "Asked to cancel session with `%4s'\n",
699 GNUNET_i2s (target));
701 session = plugin->sessions;
702 while (NULL != session)
704 if (0 == memcmp (target,
706 sizeof (struct GNUNET_PeerIdentity)))
708 pm = session->pending_messages_head;
711 pm->transmit_cont = NULL;
712 pm->transmit_cont_cls = NULL;
715 if (session->client != NULL)
717 GNUNET_SERVER_client_drop (session->client);
718 session->client = NULL;
720 /* rest of the clean-up of the session will be done as part of
721 disconnect_notify which should be triggered any time now
722 (or which may be triggering this call in the first place) */
724 session = session->next;
729 struct PrettyPrinterContext
731 GNUNET_TRANSPORT_AddressStringCallback asc;
738 * Append our port and forward the result.
741 append_port (void *cls, const char *hostname)
743 struct PrettyPrinterContext *ppc = cls;
746 if (hostname == NULL)
748 ppc->asc (ppc->asc_cls, NULL);
752 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
753 ppc->asc (ppc->asc_cls, ret);
759 * Convert the transports address to a nice, human-readable
763 * @param type name of the transport that generated the address
764 * @param addr one of the addresses of the host, NULL for the last address
765 * the specific address format depends on the transport
766 * @param addrlen length of the address
767 * @param numeric should (IP) addresses be displayed in numeric form?
768 * @param timeout after how long should we give up?
769 * @param asc function to call on each string
770 * @param asc_cls closure for asc
773 tcp_plugin_address_pretty_printer (void *cls,
778 struct GNUNET_TIME_Relative timeout,
779 GNUNET_TRANSPORT_AddressStringCallback asc,
782 struct Plugin *plugin = cls;
783 const struct sockaddr_in *v4;
784 const struct sockaddr_in6 *v6;
785 struct PrettyPrinterContext *ppc;
787 if ((addrlen != sizeof (struct sockaddr_in)) &&
788 (addrlen != sizeof (struct sockaddr_in6)))
790 /* invalid address */
795 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
797 ppc->asc_cls = asc_cls;
798 if (addrlen == sizeof (struct sockaddr_in))
800 v4 = (const struct sockaddr_in *) addr;
801 ppc->port = ntohs (v4->sin_port);
805 v6 = (const struct sockaddr_in6 *) addr;
806 ppc->port = ntohs (v6->sin6_port);
809 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
813 !numeric, timeout, &append_port, ppc);
818 * Check if the given port is plausible (must be either
819 * our listen port or our advertised port). If it is
820 * neither, we return one of these two ports at random.
822 * @return either in_port or a more plausible port
825 check_port (struct Plugin *plugin, uint16_t in_port)
827 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
829 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
831 ? plugin->open_port : plugin->adv_port;
836 * Another peer has suggested an address for this peer and transport
837 * plugin. Check that this could be a valid address.
840 * @param addr pointer to the address
841 * @param addrlen length of addr
842 * @return GNUNET_OK if this is a plausible address for this peer
846 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
848 struct Plugin *plugin = cls;
849 char buf[sizeof (struct sockaddr_in6)];
850 struct sockaddr_in *v4;
851 struct sockaddr_in6 *v6;
853 if ((addrlen != sizeof (struct sockaddr_in)) &&
854 (addrlen != sizeof (struct sockaddr_in6)))
857 return GNUNET_SYSERR;
859 memcpy (buf, addr, sizeof (struct sockaddr_in6));
860 if (addrlen == sizeof (struct sockaddr_in))
862 v4 = (struct sockaddr_in *) buf;
863 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
867 v6 = (struct sockaddr_in6 *) buf;
868 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
871 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
873 "Informing transport service about my address `%s'.\n",
874 GNUNET_a2s (addr, addrlen));
881 * We've received a welcome from this peer via TCP. Possibly create a
882 * fresh client record and send back our welcome.
885 * @param client identification of the client
886 * @param message the actual message
889 handle_tcp_welcome (void *cls,
890 struct GNUNET_SERVER_Client *client,
891 const struct GNUNET_MessageHeader *message)
893 struct Plugin *plugin = cls;
894 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
895 struct Session *session;
900 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
902 "Received %s message from a `%4s/%p'.\n",
904 GNUNET_i2s (&wm->clientIdentity), client);
906 session = find_session_by_client (plugin, client);
909 GNUNET_SERVER_client_keep (client);
910 session = create_session (plugin,
911 &wm->clientIdentity, client);
913 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
916 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
918 "Found address `%s' for incoming connection %p\n",
919 GNUNET_a2s (vaddr, alen),
922 session->connect_addr = vaddr;
923 session->connect_alen = alen;
928 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
930 "Did not obtain TCP socket address for incoming connection\n");
934 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
936 "Creating new session %p for connection %p\n",
939 process_pending_messages (session);
941 if (session->expecting_welcome != GNUNET_YES)
944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
947 session->expecting_welcome = GNUNET_NO;
948 GNUNET_SERVER_receive_done (client, GNUNET_OK);
953 * Task to signal the server that we can continue
954 * receiving from the TCP client now.
957 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
959 struct Session *session = cls;
960 struct GNUNET_TIME_Relative delay;
962 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
963 delay = session->plugin->env->receive (session->plugin->env->cls,
966 if (delay.value == 0)
967 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
969 session->receive_delay_task =
970 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
971 delay, &delayed_done, session);
976 * We've received data for this peer via TCP. Unbox,
977 * compute latency and forward.
980 * @param client identification of the client
981 * @param message the actual message
984 handle_tcp_data (void *cls,
985 struct GNUNET_SERVER_Client *client,
986 const struct GNUNET_MessageHeader *message)
988 struct Plugin *plugin = cls;
989 struct Session *session;
990 struct GNUNET_TIME_Relative delay;
992 session = find_session_by_client (plugin, client);
993 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
995 /* We don't want to propagate WELCOME messages up! */
996 GNUNET_SERVER_receive_done (client, GNUNET_OK);
999 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1001 GNUNET_break_op (0);
1002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1006 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1008 "Passing %u bytes of type %u from `%4s' to transport service.\n",
1009 (unsigned int) ntohs (message->size),
1010 (unsigned int) ntohs (message->type),
1011 GNUNET_i2s (&session->target));
1013 delay = plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1014 session->connect_addr,
1015 session->connect_alen);
1017 if (delay.value == 0)
1018 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1020 session->receive_delay_task =
1021 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1022 delay, &delayed_done, session);
1027 * Handlers for the various TCP messages.
1029 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1030 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1031 sizeof (struct WelcomeMessage)},
1032 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1038 * Functions with this signature are called whenever a peer
1039 * is disconnected on the network level.
1041 * @param cls closure
1042 * @param client identification of the client
1045 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1047 struct Plugin *plugin = cls;
1048 struct Session *session;
1052 session = find_session_by_client (plugin, client);
1053 if (session == NULL)
1054 return; /* unknown, nothing to do */
1056 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1058 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1059 GNUNET_i2s (&session->target),
1060 (session->connect_addr != NULL) ?
1061 GNUNET_a2s (session->connect_addr,
1062 session->connect_alen) : "*", client);
1064 disconnect_session (session);
1069 * Add the IP of our network interface to the list of
1070 * our external IP addresses.
1073 process_interfaces (void *cls,
1076 const struct sockaddr *addr, socklen_t addrlen)
1078 struct Plugin *plugin = cls;
1080 struct sockaddr_in *v4;
1081 struct sockaddr_in6 *v6;
1083 af = addr->sa_family;
1086 v4 = (struct sockaddr_in *) addr;
1087 v4->sin_port = htons (plugin->adv_port);
1091 GNUNET_assert (af == AF_INET6);
1092 v6 = (struct sockaddr_in6 *) addr;
1093 v6->sin6_port = htons (plugin->adv_port);
1095 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1096 GNUNET_ERROR_TYPE_BULK,
1097 "tcp", _("Found address `%s' (%s)\n"),
1098 GNUNET_a2s (addr, addrlen), name);
1099 plugin->env->notify_address (plugin->env->cls,
1101 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1107 * Function called by the resolver for each address obtained from DNS
1108 * for our own hostname. Add the addresses to the list of our
1109 * external IP addresses.
1111 * @param cls closure
1112 * @param addr one of the addresses of the host, NULL for the last address
1113 * @param addrlen length of the address
1116 process_hostname_ips (void *cls,
1117 const struct sockaddr *addr, socklen_t addrlen)
1119 struct Plugin *plugin = cls;
1123 plugin->hostname_dns = NULL;
1126 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1131 * Entry point for the plugin.
1134 libgnunet_plugin_transport_tcp_init (void *cls)
1136 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1137 struct GNUNET_TRANSPORT_PluginFunctions *api;
1138 struct Plugin *plugin;
1139 struct GNUNET_SERVICE_Context *service;
1140 unsigned long long aport;
1141 unsigned long long bport;
1144 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1145 if (service == NULL)
1147 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1150 ("Failed to start service for `%s' transport plugin.\n"),
1156 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1162 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1165 &aport)) && (aport > 65535)))
1167 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1170 ("Require valid port number for service `%s' in configuration!\n"),
1172 GNUNET_SERVICE_stop (service);
1177 plugin = GNUNET_malloc (sizeof (struct Plugin));
1178 plugin->open_port = bport;
1179 plugin->adv_port = aport;
1181 plugin->lsock = NULL;
1182 plugin->statistics = NULL;
1183 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1185 api->send = &tcp_plugin_send;
1186 api->disconnect = &tcp_plugin_disconnect;
1187 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1188 api->check_address = &tcp_plugin_check_address;
1189 plugin->service = service;
1190 plugin->server = GNUNET_SERVICE_get_server (service);
1191 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1192 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1195 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1197 plugin->handlers[i].callback_cls = plugin;
1198 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1200 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1201 "tcp", _("TCP transport listening on port %llu\n"), bport);
1203 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1206 ("TCP transport advertises itself as being on port %llu\n"),
1208 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1210 /* FIXME: do the two calls below periodically again and
1211 not just once (since the info we get might change...) */
1212 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1213 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1216 HOSTNAME_RESOLVE_TIMEOUT,
1217 &process_hostname_ips,
1224 * Exit point from the plugin.
1227 libgnunet_plugin_transport_tcp_done (void *cls)
1229 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1230 struct Plugin *plugin = api->cls;
1231 struct Session *session;
1233 while (NULL != (session = plugin->sessions))
1234 disconnect_session (session);
1235 if (NULL != plugin->hostname_dns)
1237 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1238 plugin->hostname_dns = NULL;
1240 GNUNET_SERVICE_stop (plugin->service);
1241 GNUNET_free (plugin->handlers);
1242 GNUNET_free (plugin);
1247 /* end of plugin_transport_tcp.c */