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)
508 GNUNET_SERVER_receive_done (session->client,
511 if (session->client != NULL)
513 GNUNET_SERVER_client_drop (session->client);
514 session->client = NULL;
516 GNUNET_free_non_null (session->connect_addr);
517 GNUNET_free (session);
522 * Function that can be used by the transport service to transmit
523 * a message using the plugin. Note that in the case of a
524 * peer disconnecting, the continuation MUST be called
525 * prior to the disconnect notification itself. This function
526 * will be called with this peer's HELLO message to initiate
527 * a fresh connection to another peer.
530 * @param target who should receive this message
531 * @param msg the message to transmit
532 * @param msgbuf_size number of bytes in 'msg'
533 * @param priority how important is the message (most plugins will
534 * ignore message priority and just FIFO)
535 * @param timeout how long to wait at most for the transmission (does not
536 * require plugins to discard the message after the timeout,
537 * just advisory for the desired delay; most plugins will ignore
539 * @param addr the address to use (can be NULL if the plugin
540 * is "on its own" (i.e. re-use existing TCP connection))
541 * @param addrlen length of the address in bytes
542 * @param force_address GNUNET_YES if the plugin MUST use the given address,
543 * otherwise the plugin may use other addresses or
544 * existing connections (if available)
545 * @param cont continuation to call once the message has
546 * been transmitted (or if the transport is ready
547 * for the next transmission call; or if the
548 * peer disconnected...); can be NULL
549 * @param cont_cls closure for cont
550 * @return number of bytes used (on the physical network, with overheads);
551 * -1 on hard errors (i.e. address invalid); 0 is a legal value
552 * and does NOT mean that the message was not transmitted (DV)
555 tcp_plugin_send (void *cls,
556 const struct GNUNET_PeerIdentity *target,
560 struct GNUNET_TIME_Relative timeout,
564 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
566 struct Plugin *plugin = cls;
567 struct Session *session;
568 struct PendingMessage *pm;
569 struct GNUNET_CONNECTION_Handle *sa;
572 session = plugin->sessions;
573 /* FIXME: we could do this a cheaper with a hash table
574 where we could restrict the iteration to entries that match
575 the target peer... */
576 while ( (session != NULL) &&
577 ( (0 != memcmp (target,
579 sizeof (struct GNUNET_PeerIdentity))) ||
580 ( (GNUNET_YES == force_address) &&
582 ( (addrlen != session->connect_alen) ||
583 (0 != memcmp (session->connect_addr,
586 session = session->next;
587 if ( (session == NULL) &&
591 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
593 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
594 GNUNET_i2s (target));
600 if (sizeof (struct sockaddr_in) == addrlen)
602 else if (sizeof (struct sockaddr_in6) == addrlen)
609 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
611 GNUNET_SERVER_MAX_MESSAGE_SIZE);
615 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
617 "Failed to create connection to `%4s' at `%s'\n",
619 GNUNET_a2s (addr, addrlen));
625 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
627 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
629 GNUNET_a2s (addr, addrlen));
631 session = create_session (plugin,
633 GNUNET_SERVER_connect_socket (plugin->server,
635 session->connect_addr = GNUNET_malloc (addrlen);
636 memcpy (session->connect_addr,
639 session->connect_alen = addrlen;
641 GNUNET_assert (session != NULL);
642 GNUNET_assert (session->client != NULL);
644 /* create new message entry */
645 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
646 pm->msg = (const char*) &pm[1];
647 memcpy (&pm[1], msg, msgbuf_size);
648 pm->message_size = msgbuf_size;
649 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
650 pm->transmit_cont = cont;
651 pm->transmit_cont_cls = cont_cls;
653 /* append pm to pending_messages list */
654 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
655 session->pending_messages_tail,
656 session->pending_messages_tail,
659 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
661 "Asked to transmit %u bytes to `%s', added message to list.\n",
663 GNUNET_i2s (target));
665 process_pending_messages (session);
671 * Function that can be called to force a disconnect from the
672 * specified neighbour. This should also cancel all previously
673 * scheduled transmissions. Obviously the transmission may have been
674 * partially completed already, which is OK. The plugin is supposed
675 * to close the connection (if applicable) and no longer call the
676 * transmit continuation(s).
678 * Finally, plugin MUST NOT call the services's receive function to
679 * notify the service that the connection to the specified target was
680 * closed after a getting this call.
683 * @param target peer for which the last transmission is
687 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
689 struct Plugin *plugin = cls;
690 struct Session *session;
691 struct PendingMessage *pm;
694 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
696 "Asked to cancel session with `%4s'\n",
697 GNUNET_i2s (target));
699 session = plugin->sessions;
700 while (NULL != session)
702 if (0 == memcmp (target,
704 sizeof (struct GNUNET_PeerIdentity)))
706 pm = session->pending_messages_head;
709 pm->transmit_cont = NULL;
710 pm->transmit_cont_cls = NULL;
713 if (session->client != NULL)
715 GNUNET_SERVER_client_drop (session->client);
716 session->client = NULL;
718 /* rest of the clean-up of the session will be done as part of
719 disconnect_notify which should be triggered any time now
720 (or which may be triggering this call in the first place) */
722 session = session->next;
727 struct PrettyPrinterContext
729 GNUNET_TRANSPORT_AddressStringCallback asc;
736 * Append our port and forward the result.
739 append_port (void *cls, const char *hostname)
741 struct PrettyPrinterContext *ppc = cls;
744 if (hostname == NULL)
746 ppc->asc (ppc->asc_cls, NULL);
750 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
751 ppc->asc (ppc->asc_cls, ret);
757 * Convert the transports address to a nice, human-readable
761 * @param type name of the transport that generated the address
762 * @param addr one of the addresses of the host, NULL for the last address
763 * the specific address format depends on the transport
764 * @param addrlen length of the address
765 * @param numeric should (IP) addresses be displayed in numeric form?
766 * @param timeout after how long should we give up?
767 * @param asc function to call on each string
768 * @param asc_cls closure for asc
771 tcp_plugin_address_pretty_printer (void *cls,
776 struct GNUNET_TIME_Relative timeout,
777 GNUNET_TRANSPORT_AddressStringCallback asc,
780 struct Plugin *plugin = cls;
781 const struct sockaddr_in *v4;
782 const struct sockaddr_in6 *v6;
783 struct PrettyPrinterContext *ppc;
785 if ((addrlen != sizeof (struct sockaddr_in)) &&
786 (addrlen != sizeof (struct sockaddr_in6)))
788 /* invalid address */
793 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
795 ppc->asc_cls = asc_cls;
796 if (addrlen == sizeof (struct sockaddr_in))
798 v4 = (const struct sockaddr_in *) addr;
799 ppc->port = ntohs (v4->sin_port);
803 v6 = (const struct sockaddr_in6 *) addr;
804 ppc->port = ntohs (v6->sin6_port);
807 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
811 !numeric, timeout, &append_port, ppc);
816 * Check if the given port is plausible (must be either
817 * our listen port or our advertised port). If it is
818 * neither, we return one of these two ports at random.
820 * @return either in_port or a more plausible port
823 check_port (struct Plugin *plugin, uint16_t in_port)
825 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
827 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
829 ? plugin->open_port : plugin->adv_port;
834 * Another peer has suggested an address for this peer and transport
835 * plugin. Check that this could be a valid address.
838 * @param addr pointer to the address
839 * @param addrlen length of addr
840 * @return GNUNET_OK if this is a plausible address for this peer
844 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
846 struct Plugin *plugin = cls;
847 char buf[sizeof (struct sockaddr_in6)];
848 struct sockaddr_in *v4;
849 struct sockaddr_in6 *v6;
851 if ((addrlen != sizeof (struct sockaddr_in)) &&
852 (addrlen != sizeof (struct sockaddr_in6)))
855 return GNUNET_SYSERR;
857 memcpy (buf, addr, sizeof (struct sockaddr_in6));
858 if (addrlen == sizeof (struct sockaddr_in))
860 v4 = (struct sockaddr_in *) buf;
861 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
865 v6 = (struct sockaddr_in6 *) buf;
866 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
869 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
871 "Informing transport service about my address `%s'.\n",
872 GNUNET_a2s (addr, addrlen));
879 * We've received a welcome from this peer via TCP. Possibly create a
880 * fresh client record and send back our welcome.
883 * @param client identification of the client
884 * @param message the actual message
887 handle_tcp_welcome (void *cls,
888 struct GNUNET_SERVER_Client *client,
889 const struct GNUNET_MessageHeader *message)
891 struct Plugin *plugin = cls;
892 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
893 struct Session *session;
898 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
900 "Received %s message from a `%4s/%p'.\n",
902 GNUNET_i2s (&wm->clientIdentity), client);
904 session = find_session_by_client (plugin, client);
907 GNUNET_SERVER_client_keep (client);
908 session = create_session (plugin,
909 &wm->clientIdentity, client);
911 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
914 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
916 "Found address `%s' for incoming connection %p\n",
917 GNUNET_a2s (vaddr, alen),
920 session->connect_addr = vaddr;
921 session->connect_alen = alen;
926 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
928 "Did not obtain TCP socket address for incoming connection\n");
932 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
934 "Creating new session %p for connection %p\n",
937 process_pending_messages (session);
939 if (session->expecting_welcome != GNUNET_YES)
942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
945 session->expecting_welcome = GNUNET_NO;
946 GNUNET_SERVER_receive_done (client, GNUNET_OK);
951 * Task to signal the server that we can continue
952 * receiving from the TCP client now.
955 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
957 struct Session *session = cls;
958 struct GNUNET_TIME_Relative delay;
960 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
961 delay = session->plugin->env->receive (session->plugin->env->cls,
964 if (delay.value == 0)
965 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
967 session->receive_delay_task =
968 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
969 delay, &delayed_done, session);
974 * We've received data for this peer via TCP. Unbox,
975 * compute latency and forward.
978 * @param client identification of the client
979 * @param message the actual message
982 handle_tcp_data (void *cls,
983 struct GNUNET_SERVER_Client *client,
984 const struct GNUNET_MessageHeader *message)
986 struct Plugin *plugin = cls;
987 struct Session *session;
988 struct GNUNET_TIME_Relative delay;
990 session = find_session_by_client (plugin, client);
991 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
993 /* We don't want to propagate WELCOME messages up! */
994 GNUNET_SERVER_receive_done (client, GNUNET_OK);
997 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1004 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1006 "Passing %u bytes of type %u from `%4s' to transport service.\n",
1007 (unsigned int) ntohs (message->size),
1008 (unsigned int) ntohs (message->type),
1009 GNUNET_i2s (&session->target));
1011 delay = plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1012 session->connect_addr,
1013 session->connect_alen);
1015 if (delay.value == 0)
1016 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1018 session->receive_delay_task =
1019 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1020 delay, &delayed_done, session);
1025 * Handlers for the various TCP messages.
1027 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1028 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1029 sizeof (struct WelcomeMessage)},
1030 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1036 * Functions with this signature are called whenever a peer
1037 * is disconnected on the network level.
1039 * @param cls closure
1040 * @param client identification of the client
1043 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1045 struct Plugin *plugin = cls;
1046 struct Session *session;
1050 session = find_session_by_client (plugin, client);
1051 if (session == NULL)
1052 return; /* unknown, nothing to do */
1054 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1056 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1057 GNUNET_i2s (&session->target),
1058 (session->connect_addr != NULL) ?
1059 GNUNET_a2s (session->connect_addr,
1060 session->connect_alen) : "*", client);
1062 disconnect_session (session);
1067 * Add the IP of our network interface to the list of
1068 * our external IP addresses.
1071 process_interfaces (void *cls,
1074 const struct sockaddr *addr, socklen_t addrlen)
1076 struct Plugin *plugin = cls;
1078 struct sockaddr_in *v4;
1079 struct sockaddr_in6 *v6;
1081 af = addr->sa_family;
1084 v4 = (struct sockaddr_in *) addr;
1085 v4->sin_port = htons (plugin->adv_port);
1089 GNUNET_assert (af == AF_INET6);
1090 v6 = (struct sockaddr_in6 *) addr;
1091 v6->sin6_port = htons (plugin->adv_port);
1093 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1094 GNUNET_ERROR_TYPE_BULK,
1095 "tcp", _("Found address `%s' (%s)\n"),
1096 GNUNET_a2s (addr, addrlen), name);
1097 plugin->env->notify_address (plugin->env->cls,
1099 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1105 * Function called by the resolver for each address obtained from DNS
1106 * for our own hostname. Add the addresses to the list of our
1107 * external IP addresses.
1109 * @param cls closure
1110 * @param addr one of the addresses of the host, NULL for the last address
1111 * @param addrlen length of the address
1114 process_hostname_ips (void *cls,
1115 const struct sockaddr *addr, socklen_t addrlen)
1117 struct Plugin *plugin = cls;
1121 plugin->hostname_dns = NULL;
1124 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1129 * Entry point for the plugin.
1132 libgnunet_plugin_transport_tcp_init (void *cls)
1134 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1135 struct GNUNET_TRANSPORT_PluginFunctions *api;
1136 struct Plugin *plugin;
1137 struct GNUNET_SERVICE_Context *service;
1138 unsigned long long aport;
1139 unsigned long long bport;
1142 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1143 if (service == NULL)
1145 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1148 ("Failed to start service for `%s' transport plugin.\n"),
1154 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1160 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1163 &aport)) && (aport > 65535)))
1165 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1168 ("Require valid port number for service `%s' in configuration!\n"),
1170 GNUNET_SERVICE_stop (service);
1175 plugin = GNUNET_malloc (sizeof (struct Plugin));
1176 plugin->open_port = bport;
1177 plugin->adv_port = aport;
1179 plugin->lsock = NULL;
1180 plugin->statistics = NULL;
1181 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1183 api->send = &tcp_plugin_send;
1184 api->disconnect = &tcp_plugin_disconnect;
1185 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1186 api->check_address = &tcp_plugin_check_address;
1187 plugin->service = service;
1188 plugin->server = GNUNET_SERVICE_get_server (service);
1189 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1190 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1193 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1195 plugin->handlers[i].callback_cls = plugin;
1196 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1198 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1199 "tcp", _("TCP transport listening on port %llu\n"), bport);
1201 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1204 ("TCP transport advertises itself as being on port %llu\n"),
1206 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1208 /* FIXME: do the two calls below periodically again and
1209 not just once (since the info we get might change...) */
1210 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1211 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1214 HOSTNAME_RESOLVE_TIMEOUT,
1215 &process_hostname_ips,
1222 * Exit point from the plugin.
1225 libgnunet_plugin_transport_tcp_done (void *cls)
1227 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1228 struct Plugin *plugin = api->cls;
1229 struct Session *session;
1231 while (NULL != (session = plugin->sessions))
1232 disconnect_session (session);
1233 if (NULL != plugin->hostname_dns)
1235 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1236 plugin->hostname_dns = NULL;
1238 GNUNET_SERVICE_stop (plugin->service);
1239 GNUNET_free (plugin->handlers);
1240 GNUNET_free (plugin);
1245 /* end of plugin_transport_tcp.c */