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.
21 * @file transport/plugin_transport_tcp.c
22 * @brief Implementation of the TCP transport service
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_connection_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_resolver_service.h"
32 #include "gnunet_server_lib.h"
33 #include "gnunet_service_lib.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_transport_service.h"
37 #include "plugin_transport.h"
38 #include "transport.h"
40 #define DEBUG_TCP GNUNET_YES
43 * How long until we give up on transmitting the welcome message?
45 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
49 * Initial handshake message for a session.
54 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
56 struct GNUNET_MessageHeader header;
59 * Identity of the node connecting (TCP client)
61 struct GNUNET_PeerIdentity clientIdentity;
67 * Network format for IPv4 addresses.
72 * IPv4 address, in network byte order.
77 * Port number, in network byte order.
85 * Network format for IPv6 addresses.
92 unsigned char ipv6_addr[16];
95 * Port number, in network byte order.
103 * Encapsulation of all of the state of the plugin.
109 * Information kept for each message that is yet to
112 struct PendingMessage
116 * This is a doubly-linked list.
118 struct PendingMessage *next;
121 * This is a doubly-linked list.
123 struct PendingMessage *prev;
126 * The pending message
131 * Continuation function to call once the message
132 * has been sent. Can be NULL if there is no
133 * continuation to call.
135 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
138 * Closure for transmit_cont.
140 void *transmit_cont_cls;
143 * Timeout value for the pending message.
145 struct GNUNET_TIME_Absolute timeout;
148 * So that the gnunet-service-transport can group messages together,
149 * these pending messages need to accept a message buffer and size
150 * instead of just a GNUNET_MessageHeader.
158 * Session handle for TCP connections.
164 * Stored in a linked list.
166 struct Session *next;
169 * Pointer to the global plugin struct.
171 struct Plugin *plugin;
174 * The client (used to identify this connection)
176 struct GNUNET_SERVER_Client *client;
179 * Messages currently pending for transmission
180 * to this peer, if any.
182 struct PendingMessage *pending_messages_head;
185 * Messages currently pending for transmission
186 * to this peer, if any.
188 struct PendingMessage *pending_messages_tail;
191 * Handle for pending transmission request.
193 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
196 * To whom are we talking to (set to our identity
197 * if we are still waiting for the welcome message)
199 struct GNUNET_PeerIdentity target;
202 * ID of task used to delay receiving more to throttle sender.
204 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
207 * Address of the other peer (either based on our 'connect'
208 * call or on our 'accept' call).
213 * Last activity on this connection. Used to select preferred
216 struct GNUNET_TIME_Absolute last_activity;
219 * Length of connect_addr.
224 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
226 int expecting_welcome;
229 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
237 * Encapsulation of all of the state of the plugin.
244 struct GNUNET_TRANSPORT_PluginEnvironment *env;
249 struct GNUNET_CONNECTION_Handle *lsock;
252 * List of open TCP sessions.
254 struct Session *sessions;
257 * Handle to the network service.
259 struct GNUNET_SERVICE_Context *service;
262 * Handle to the server for this service.
264 struct GNUNET_SERVER_Handle *server;
267 * Copy of the handler array where the closures are
268 * set to this struct's instance.
270 struct GNUNET_SERVER_MessageHandler *handlers;
273 * Handle for request of hostname resolution, non-NULL if pending.
275 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
278 * ID of task used to update our addresses when one expires.
280 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
283 * Port that we are actually listening on.
288 * Port that the user said we would have visible to the
299 * Function called for a quick conversion of the binary address to
300 * a numeric address. Note that the caller must not free the
301 * address and that the next call to this function is allowed
302 * to override the address again.
304 * @param cls closure ('struct Plugin*')
305 * @param addr binary address
306 * @param addrlen length of the address
307 * @return string representing the same address
310 tcp_address_to_string (void *cls,
314 static char rbuf[INET6_ADDRSTRLEN + 10];
315 char buf[INET6_ADDRSTRLEN];
319 const struct IPv4TcpAddress *t4;
320 const struct IPv6TcpAddress *t6;
324 if (addrlen == sizeof (struct IPv6TcpAddress))
328 port = ntohs (t6->t6_port);
329 memcpy (&a6, t6->ipv6_addr, sizeof (a6));
332 else if (addrlen == sizeof (struct IPv4TcpAddress))
336 port = ntohs (t4->t_port);
337 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
345 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
346 GNUNET_snprintf (rbuf,
356 * Find the session handle for the given client.
358 * @return NULL if no matching session exists
360 static struct Session *
361 find_session_by_client (struct Plugin *plugin,
362 const struct GNUNET_SERVER_Client *client)
366 ret = plugin->sessions;
367 while ((ret != NULL) && (client != ret->client))
374 * Create a new session. Also queues a welcome message.
377 * @param target peer to connect to
378 * @param client client to use
379 * @return new session object
381 static struct Session *
382 create_session (struct Plugin *plugin,
383 const struct GNUNET_PeerIdentity *target,
384 struct GNUNET_SERVER_Client *client)
387 struct PendingMessage *pm;
388 struct WelcomeMessage welcome;
390 GNUNET_assert (client != NULL);
391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
393 "Creating new session for peer `%4s'\n",
394 GNUNET_i2s (target));
395 ret = GNUNET_malloc (sizeof (struct Session));
396 ret->last_activity = GNUNET_TIME_absolute_get ();
397 ret->plugin = plugin;
398 ret->next = plugin->sessions;
399 plugin->sessions = ret;
400 ret->client = client;
401 ret->target = *target;
402 ret->expecting_welcome = GNUNET_YES;
403 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
404 pm->msg = (const char*) &pm[1];
405 pm->message_size = sizeof (struct WelcomeMessage);
406 welcome.header.size = htons (sizeof (struct WelcomeMessage));
407 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
408 welcome.clientIdentity = *plugin->env->my_identity;
409 memcpy (&pm[1], &welcome, sizeof (welcome));
410 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
411 GNUNET_STATISTICS_update (plugin->env->stats,
412 gettext_noop ("# bytes currently in TCP buffers"),
415 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
416 ret->pending_messages_tail,
418 GNUNET_STATISTICS_update (plugin->env->stats,
419 gettext_noop ("# TCP sessions active"),
427 * If we have pending messages, ask the server to
428 * transmit them (schedule the respective tasks, etc.)
430 * @param session for which session should we do this
432 static void process_pending_messages (struct Session *session);
436 * Function called to notify a client about the socket
437 * being ready to queue more data. "buf" will be
438 * NULL and "size" zero if the socket was closed for
439 * writing in the meantime.
442 * @param size number of bytes available in buf
443 * @param buf where the callee should write the message
444 * @return number of bytes written to buf
447 do_transmit (void *cls, size_t size, void *buf)
449 struct Session *session = cls;
450 struct GNUNET_PeerIdentity pid;
451 struct Plugin *plugin;
452 struct PendingMessage *pos;
453 struct PendingMessage *hd;
454 struct PendingMessage *tl;
455 struct GNUNET_TIME_Absolute now;
459 session->transmit_handle = NULL;
460 plugin = session->plugin;
464 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
466 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
467 GNUNET_i2s (&session->target));
469 /* timeout; cancel all messages that have already expired */
473 now = GNUNET_TIME_absolute_get ();
474 while ( (NULL != (pos = session->pending_messages_head)) &&
475 (pos->timeout.value <= now.value) )
477 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
478 session->pending_messages_tail,
481 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
483 "Failed to transmit %u byte message to `%4s'.\n",
485 GNUNET_i2s (&session->target));
487 ret += pos->message_size;
488 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
490 /* do this call before callbacks (so that if callbacks destroy
491 session, they have a chance to cancel actions done by this
493 process_pending_messages (session);
494 pid = session->target;
495 /* no do callbacks and do not use session again since
496 the callbacks may abort the session */
497 while (NULL != (pos = hd))
499 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
500 if (pos->transmit_cont != NULL)
501 pos->transmit_cont (pos->transmit_cont_cls,
502 &pid, GNUNET_SYSERR);
505 GNUNET_STATISTICS_update (plugin->env->stats,
506 gettext_noop ("# bytes currently in TCP buffers"),
509 GNUNET_STATISTICS_update (plugin->env->stats,
510 gettext_noop ("# bytes discarded by TCP (timeout)"),
515 /* copy all pending messages that would fit */
520 while (NULL != (pos = session->pending_messages_head))
522 if (ret + pos->message_size > size)
524 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
525 session->pending_messages_tail,
527 GNUNET_assert (size >= pos->message_size);
528 memcpy (cbuf, pos->msg, pos->message_size);
529 cbuf += pos->message_size;
530 ret += pos->message_size;
531 size -= pos->message_size;
532 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
534 /* schedule 'continuation' before callbacks so that callbacks that
535 cancel everything don't cause us to use a session that no longer
537 process_pending_messages (session);
538 session->last_activity = GNUNET_TIME_absolute_get ();
539 pid = session->target;
540 /* we'll now call callbacks that may cancel the session; hence
541 we should not use 'session' after this point */
542 while (NULL != (pos = hd))
544 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
545 if (pos->transmit_cont != NULL)
546 pos->transmit_cont (pos->transmit_cont_cls,
550 GNUNET_assert (hd == NULL);
551 GNUNET_assert (tl == NULL);
553 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
554 "tcp", "Transmitting %u bytes\n", ret);
556 GNUNET_STATISTICS_update (plugin->env->stats,
557 gettext_noop ("# bytes currently in TCP buffers"),
560 GNUNET_STATISTICS_update (plugin->env->stats,
561 gettext_noop ("# bytes transmitted via TCP"),
569 * If we have pending messages, ask the server to
570 * transmit them (schedule the respective tasks, etc.)
572 * @param session for which session should we do this
575 process_pending_messages (struct Session *session)
577 struct PendingMessage *pm;
579 GNUNET_assert (session->client != NULL);
580 if (session->transmit_handle != NULL)
582 if (NULL == (pm = session->pending_messages_head))
584 session->transmit_handle
585 = GNUNET_SERVER_notify_transmit_ready (session->client,
587 GNUNET_TIME_absolute_get_remaining
589 &do_transmit, session);
594 * Functions with this signature are called whenever we need
595 * to close a session due to a disconnect or failure to
596 * establish a connection.
598 * @param session session to close down
601 disconnect_session (struct Session *session)
603 struct Session *prev;
605 struct PendingMessage *pm;
608 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
610 "Disconnecting from `%4s' at %s (session %p).\n",
611 GNUNET_i2s (&session->target),
612 (session->connect_addr != NULL) ?
613 tcp_address_to_string (session->plugin,
614 session->connect_addr,
615 session->connect_alen) : "*",
618 /* remove from session list */
620 pos = session->plugin->sessions;
621 while (pos != session)
627 session->plugin->sessions = session->next;
629 prev->next = session->next;
630 session->plugin->env->session_end (session->plugin->env->cls,
634 if (session->transmit_handle != NULL)
636 GNUNET_CONNECTION_notify_transmit_ready_cancel
637 (session->transmit_handle);
638 session->transmit_handle = NULL;
640 while (NULL != (pm = session->pending_messages_head))
643 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
645 pm->transmit_cont != NULL
646 ? "Could not deliver message to `%4s'.\n"
648 "Could not deliver message to `%4s', notifying.\n",
649 GNUNET_i2s (&session->target));
651 GNUNET_STATISTICS_update (session->plugin->env->stats,
652 gettext_noop ("# bytes currently in TCP buffers"),
653 - (int64_t) pm->message_size,
655 GNUNET_STATISTICS_update (session->plugin->env->stats,
656 gettext_noop ("# bytes discarded by TCP (disconnect)"),
659 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
660 session->pending_messages_tail,
662 if (NULL != pm->transmit_cont)
663 pm->transmit_cont (pm->transmit_cont_cls,
664 &session->target, GNUNET_SYSERR);
667 GNUNET_break (session->client != NULL);
668 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
670 GNUNET_SCHEDULER_cancel (session->plugin->env->sched,
671 session->receive_delay_task);
672 GNUNET_SERVER_receive_done (session->client,
675 GNUNET_SERVER_client_drop (session->client);
676 GNUNET_STATISTICS_update (session->plugin->env->stats,
677 gettext_noop ("# TCP sessions active"),
680 GNUNET_free_non_null (session->connect_addr);
681 GNUNET_free (session);
686 * Given two otherwise equivalent sessions, pick the better one.
688 * @param s1 one session (also default)
689 * @param s2 other session
690 * @return "better" session (more active)
692 static struct Session *
693 select_better_session (struct Session *s1,
700 if ( (s1->expecting_welcome == GNUNET_NO) &&
701 (s2->expecting_welcome == GNUNET_YES) )
703 if ( (s1->expecting_welcome == GNUNET_YES) &&
704 (s2->expecting_welcome == GNUNET_NO) )
706 if (s1->last_activity.value < s2->last_activity.value)
708 if (s1->last_activity.value > s2->last_activity.value)
710 if ( (GNUNET_YES == s1->inbound) &&
711 (GNUNET_NO == s2->inbound) )
713 if ( (GNUNET_NO == s1->inbound) &&
714 (GNUNET_YES == s2->inbound) )
721 * Function that can be used by the transport service to transmit
722 * a message using the plugin. Note that in the case of a
723 * peer disconnecting, the continuation MUST be called
724 * prior to the disconnect notification itself. This function
725 * will be called with this peer's HELLO message to initiate
726 * a fresh connection to another peer.
729 * @param target who should receive this message
730 * @param msg the message to transmit
731 * @param msgbuf_size number of bytes in 'msg'
732 * @param priority how important is the message (most plugins will
733 * ignore message priority and just FIFO)
734 * @param timeout how long to wait at most for the transmission (does not
735 * require plugins to discard the message after the timeout,
736 * just advisory for the desired delay; most plugins will ignore
738 * @param session which session must be used (or NULL for "any")
739 * @param addr the address to use (can be NULL if the plugin
740 * is "on its own" (i.e. re-use existing TCP connection))
741 * @param addrlen length of the address in bytes
742 * @param force_address GNUNET_YES if the plugin MUST use the given address,
743 * GNUNET_NO means the plugin may use any other address and
744 * GNUNET_SYSERR means that only reliable existing
745 * bi-directional connections should be used (regardless
747 * @param cont continuation to call once the message has
748 * been transmitted (or if the transport is ready
749 * for the next transmission call; or if the
750 * peer disconnected...); can be NULL
751 * @param cont_cls closure for cont
752 * @return number of bytes used (on the physical network, with overheads);
753 * -1 on hard errors (i.e. address invalid); 0 is a legal value
754 * and does NOT mean that the message was not transmitted (DV)
757 tcp_plugin_send (void *cls,
758 const struct GNUNET_PeerIdentity *target,
762 struct GNUNET_TIME_Relative timeout,
763 struct Session *session,
767 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
769 struct Plugin *plugin = cls;
770 struct Session *cand_session;
771 struct Session *next;
772 struct PendingMessage *pm;
773 struct GNUNET_CONNECTION_Handle *sa;
777 struct sockaddr_in a4;
778 struct sockaddr_in6 a6;
779 const struct IPv4TcpAddress *t4;
780 const struct IPv6TcpAddress *t6;
782 GNUNET_STATISTICS_update (plugin->env->stats,
783 gettext_noop ("# bytes TCP was asked to transmit"),
786 /* FIXME: we could do this cheaper with a hash table
787 where we could restrict the iteration to entries that match
788 the target peer... */
792 next = plugin->sessions;
793 while (NULL != (session = next))
795 next = session->next;
796 GNUNET_assert (session->client != NULL);
797 if (0 != memcmp (target,
799 sizeof (struct GNUNET_PeerIdentity)))
801 if ( ( (GNUNET_SYSERR == force_address) &&
802 (session->expecting_welcome == GNUNET_NO) ) ||
803 (GNUNET_NO == force_address) )
805 cand_session = select_better_session (cand_session,
809 if (GNUNET_SYSERR == force_address)
811 GNUNET_break (GNUNET_YES == force_address);
817 if (session->inbound == GNUNET_YES)
819 if (addrlen != session->connect_alen)
821 if (0 != memcmp (session->connect_addr,
825 cand_session = select_better_session (cand_session,
828 session = cand_session;
830 if ( (session == NULL) &&
834 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
836 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
837 GNUNET_i2s (target));
839 GNUNET_STATISTICS_update (plugin->env->stats,
840 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
847 if (addrlen == sizeof (struct IPv6TcpAddress))
851 memset (&a6, 0, sizeof (a6));
852 #if HAVE_SOCKADDR_IN_SIN_LEN
853 a6.sin6_len = sizeof (a6);
855 a6.sin6_family = AF_INET6;
856 a6.sin6_port = t6->t6_port;
857 memcpy (a6.sin6_addr.s6_addr,
863 else if (addrlen == sizeof (struct IPv4TcpAddress))
867 memset (&a4, 0, sizeof (a4));
868 #if HAVE_SOCKADDR_IN_SIN_LEN
869 a4.sin_len = sizeof (a4);
871 a4.sin_family = AF_INET;
872 a4.sin_port = t4->t_port;
873 a4.sin_addr.s_addr = t4->ipv4_addr;
882 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
884 GNUNET_SERVER_MAX_MESSAGE_SIZE);
888 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
890 "Failed to create connection to `%4s' at `%s'\n",
892 GNUNET_a2s (sb, sbs));
894 GNUNET_STATISTICS_update (plugin->env->stats,
895 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
901 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
903 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
905 GNUNET_a2s (sb, sbs));
907 session = create_session (plugin,
909 GNUNET_SERVER_connect_socket (plugin->server,
911 session->connect_addr = GNUNET_malloc (addrlen);
912 memcpy (session->connect_addr,
915 session->connect_alen = addrlen;
917 GNUNET_assert (session != NULL);
918 GNUNET_assert (session->client != NULL);
919 GNUNET_STATISTICS_update (plugin->env->stats,
920 gettext_noop ("# bytes currently in TCP buffers"),
923 /* create new message entry */
924 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
925 pm->msg = (const char*) &pm[1];
926 memcpy (&pm[1], msg, msgbuf_size);
927 pm->message_size = msgbuf_size;
928 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
929 pm->transmit_cont = cont;
930 pm->transmit_cont_cls = cont_cls;
932 /* append pm to pending_messages list */
933 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
934 session->pending_messages_tail,
935 session->pending_messages_tail,
938 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
940 "Asked to transmit %u bytes to `%s', added message to list.\n",
942 GNUNET_i2s (target));
944 process_pending_messages (session);
950 * Function that can be called to force a disconnect from the
951 * specified neighbour. This should also cancel all previously
952 * scheduled transmissions. Obviously the transmission may have been
953 * partially completed already, which is OK. The plugin is supposed
954 * to close the connection (if applicable) and no longer call the
955 * transmit continuation(s).
957 * Finally, plugin MUST NOT call the services's receive function to
958 * notify the service that the connection to the specified target was
959 * closed after a getting this call.
962 * @param target peer for which the last transmission is
966 tcp_plugin_disconnect (void *cls,
967 const struct GNUNET_PeerIdentity *target)
969 struct Plugin *plugin = cls;
970 struct Session *session;
971 struct Session *next;
972 struct PendingMessage *pm;
975 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
977 "Asked to cancel session with `%4s'\n",
978 GNUNET_i2s (target));
980 next = plugin->sessions;
981 while (NULL != (session = next))
983 next = session->next;
984 if (0 != memcmp (target,
986 sizeof (struct GNUNET_PeerIdentity)))
988 pm = session->pending_messages_head;
991 pm->transmit_cont = NULL;
992 pm->transmit_cont_cls = NULL;
995 disconnect_session (session);
1001 * Context for address to string conversion.
1003 struct PrettyPrinterContext
1006 * Function to call with the result.
1008 GNUNET_TRANSPORT_AddressStringCallback asc;
1011 * Clsoure for 'asc'.
1016 * Port to add after the IP address.
1023 * Append our port and forward the result.
1025 * @param cls the 'struct PrettyPrinterContext*'
1026 * @param hostname hostname part of the address
1029 append_port (void *cls, const char *hostname)
1031 struct PrettyPrinterContext *ppc = cls;
1034 if (hostname == NULL)
1036 ppc->asc (ppc->asc_cls, NULL);
1040 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1041 ppc->asc (ppc->asc_cls, ret);
1047 * Convert the transports address to a nice, human-readable
1050 * @param cls closure
1051 * @param type name of the transport that generated the address
1052 * @param addr one of the addresses of the host, NULL for the last address
1053 * the specific address format depends on the transport
1054 * @param addrlen length of the address
1055 * @param numeric should (IP) addresses be displayed in numeric form?
1056 * @param timeout after how long should we give up?
1057 * @param asc function to call on each string
1058 * @param asc_cls closure for asc
1061 tcp_plugin_address_pretty_printer (void *cls,
1066 struct GNUNET_TIME_Relative timeout,
1067 GNUNET_TRANSPORT_AddressStringCallback asc,
1070 struct Plugin *plugin = cls;
1071 struct PrettyPrinterContext *ppc;
1074 struct sockaddr_in a4;
1075 struct sockaddr_in6 a6;
1076 const struct IPv4TcpAddress *t4;
1077 const struct IPv6TcpAddress *t6;
1080 if (addrlen == sizeof (struct IPv6TcpAddress))
1083 memset (&a6, 0, sizeof (a6));
1084 a6.sin6_family = AF_INET6;
1085 a6.sin6_port = t6->t6_port;
1086 memcpy (a6.sin6_addr.s6_addr,
1089 port = ntohs (t6->t6_port);
1093 else if (addrlen == sizeof (struct IPv4TcpAddress))
1096 memset (&a4, 0, sizeof (a4));
1097 a4.sin_family = AF_INET;
1098 a4.sin_port = t4->t_port;
1099 a4.sin_addr.s_addr = t4->ipv4_addr;
1100 port = ntohs (t4->t_port);
1106 /* invalid address */
1107 GNUNET_break_op (0);
1108 asc (asc_cls, NULL);
1111 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1113 ppc->asc_cls = asc_cls;
1115 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1119 !numeric, timeout, &append_port, ppc);
1124 * Check if the given port is plausible (must be either
1125 * our listen port or our advertised port). If it is
1126 * neither, we return one of these two ports at random.
1128 * @param plugin global variables
1129 * @param in_port port number to check
1130 * @return either in_port or a more plausible port
1133 check_port (struct Plugin *plugin, uint16_t in_port)
1135 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1137 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1139 ? plugin->open_port : plugin->adv_port;
1144 * Another peer has suggested an address for this peer and transport
1145 * plugin. Check that this could be a valid address. This function
1146 * is not expected to 'validate' the address in the sense of trying to
1147 * connect to it but simply to see if the binary format is technically
1148 * legal for establishing a connection.
1150 * @param cls closure, our 'struct Plugin*'
1151 * @param addr pointer to the address
1152 * @param addrlen length of addr
1153 * @return GNUNET_OK if this is a plausible address for this peer
1154 * and transport, GNUNET_SYSERR if not
1157 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
1159 struct Plugin *plugin = cls;
1160 struct IPv4TcpAddress *v4;
1161 struct IPv6TcpAddress *v6;
1163 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1164 (addrlen != sizeof (struct IPv6TcpAddress)))
1166 GNUNET_break_op (0);
1167 return GNUNET_SYSERR;
1169 if (addrlen == sizeof (struct IPv4TcpAddress))
1171 v4 = (struct IPv4TcpAddress *) addr;
1172 v4->t_port = htons (check_port (plugin, ntohs (v4->t_port)));
1176 v6 = (struct IPv6TcpAddress *) addr;
1177 v6->t6_port = htons (check_port (plugin, ntohs (v6->t6_port)));
1184 * We've received a welcome from this peer via TCP. Possibly create a
1185 * fresh client record and send back our welcome.
1187 * @param cls closure
1188 * @param client identification of the client
1189 * @param message the actual message
1192 handle_tcp_welcome (void *cls,
1193 struct GNUNET_SERVER_Client *client,
1194 const struct GNUNET_MessageHeader *message)
1196 struct Plugin *plugin = cls;
1197 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1198 struct Session *session;
1201 struct IPv4TcpAddress *t4;
1202 struct IPv6TcpAddress *t6;
1203 const struct sockaddr_in *s4;
1204 const struct sockaddr_in6 *s6;
1208 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1210 "Received %s message from a `%4s/%p'.\n",
1212 GNUNET_i2s (&wm->clientIdentity), client);
1214 GNUNET_STATISTICS_update (plugin->env->stats,
1215 gettext_noop ("# TCP WELCOME messages received"),
1218 session = find_session_by_client (plugin, client);
1219 if (session == NULL)
1221 GNUNET_SERVER_client_keep (client);
1222 session = create_session (plugin,
1223 &wm->clientIdentity, client);
1224 session->inbound = GNUNET_YES;
1226 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1229 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1231 "Found address `%s' for incoming connection %p\n",
1232 GNUNET_a2s (vaddr, alen),
1235 if (alen == sizeof (struct sockaddr_in))
1238 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1239 t4->t_port = s4->sin_port;
1240 t4->ipv4_addr = s4->sin_addr.s_addr;
1241 session->connect_addr = t4;
1242 session->connect_alen = sizeof (struct IPv4TcpAddress);
1244 else if (alen == sizeof (struct sockaddr_in6))
1247 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1248 t6->t6_port = s6->sin6_port;
1249 memcpy (t6->ipv6_addr,
1250 s6->sin6_addr.s6_addr,
1252 session->connect_addr = t6;
1253 session->connect_alen = sizeof (struct IPv6TcpAddress);
1255 GNUNET_free (vaddr);
1260 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1262 "Did not obtain TCP socket address for incoming connection\n");
1266 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1268 "Creating new session %p for connection %p\n",
1271 process_pending_messages (session);
1273 if (session->expecting_welcome != GNUNET_YES)
1275 GNUNET_break_op (0);
1276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1279 session->last_activity = GNUNET_TIME_absolute_get ();
1280 session->expecting_welcome = GNUNET_NO;
1281 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1286 * Task to signal the server that we can continue
1287 * receiving from the TCP client now.
1289 * @param cls the 'struct Session*'
1290 * @param tc task context (unused)
1293 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1295 struct Session *session = cls;
1296 struct GNUNET_TIME_Relative delay;
1298 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1299 delay = session->plugin->env->receive (session->plugin->env->cls,
1304 if (delay.value == 0)
1305 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1307 session->receive_delay_task =
1308 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1309 delay, &delayed_done, session);
1314 * We've received data for this peer via TCP. Unbox,
1315 * compute latency and forward.
1317 * @param cls closure
1318 * @param client identification of the client
1319 * @param message the actual message
1322 handle_tcp_data (void *cls,
1323 struct GNUNET_SERVER_Client *client,
1324 const struct GNUNET_MessageHeader *message)
1326 struct Plugin *plugin = cls;
1327 struct Session *session;
1328 struct GNUNET_TIME_Relative delay;
1330 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
1332 /* We don't want to propagate WELCOME messages up! */
1333 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1336 session = find_session_by_client (plugin, client);
1337 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1342 session->last_activity = GNUNET_TIME_absolute_get ();
1344 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1346 "Passing %u bytes of type %u from `%4s' to transport service.\n",
1347 (unsigned int) ntohs (message->size),
1348 (unsigned int) ntohs (message->type),
1349 GNUNET_i2s (&session->target));
1351 GNUNET_STATISTICS_update (plugin->env->stats,
1352 gettext_noop ("# bytes received via TCP"),
1353 ntohs (message->size),
1355 delay = plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1357 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
1358 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
1359 if (delay.value == 0)
1360 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1362 session->receive_delay_task =
1363 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1364 delay, &delayed_done, session);
1369 * Handlers for the various TCP messages.
1371 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1372 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1373 sizeof (struct WelcomeMessage)},
1374 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1380 * Functions with this signature are called whenever a peer
1381 * is disconnected on the network level.
1383 * @param cls closure
1384 * @param client identification of the client
1387 disconnect_notify (void *cls,
1388 struct GNUNET_SERVER_Client *client)
1390 struct Plugin *plugin = cls;
1391 struct Session *session;
1395 session = find_session_by_client (plugin, client);
1396 if (session == NULL)
1397 return; /* unknown, nothing to do */
1399 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1401 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1402 GNUNET_i2s (&session->target),
1403 (session->connect_addr != NULL) ?
1404 tcp_address_to_string (session->plugin,
1405 session->connect_addr,
1406 session->connect_alen) : "*",
1409 disconnect_session (session);
1414 * Add the IP of our network interface to the list of
1415 * our external IP addresses.
1417 * @param cls the 'struct Plugin*'
1418 * @param name name of the interface
1419 * @param isDefault do we think this may be our default interface
1420 * @param addr address of the interface
1421 * @param addrlen number of bytes in addr
1422 * @return GNUNET_OK to continue iterating
1425 process_interfaces (void *cls,
1428 const struct sockaddr *addr, socklen_t addrlen)
1430 struct Plugin *plugin = cls;
1432 struct IPv4TcpAddress t4;
1433 struct IPv6TcpAddress t6;
1437 af = addr->sa_family;
1440 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1441 t4.t_port = htons (plugin->adv_port);
1445 else if (af == AF_INET6)
1447 memcpy (t6.ipv6_addr,
1448 ((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr,
1450 t6.t6_port = htons (plugin->adv_port);
1459 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1460 GNUNET_ERROR_TYPE_BULK,
1462 _("Found address `%s' (%s)\n"),
1463 GNUNET_a2s (addr, addrlen), name);
1464 plugin->env->notify_address (plugin->env->cls,
1466 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
1472 * Function called by the resolver for each address obtained from DNS
1473 * for our own hostname. Add the addresses to the list of our
1474 * external IP addresses.
1476 * @param cls closure
1477 * @param addr one of the addresses of the host, NULL for the last address
1478 * @param addrlen length of the address
1481 process_hostname_ips (void *cls,
1482 const struct sockaddr *addr, socklen_t addrlen)
1484 struct Plugin *plugin = cls;
1488 plugin->hostname_dns = NULL;
1491 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1496 * Entry point for the plugin.
1498 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1499 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1502 libgnunet_plugin_transport_tcp_init (void *cls)
1504 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1505 struct GNUNET_TRANSPORT_PluginFunctions *api;
1506 struct Plugin *plugin;
1507 struct GNUNET_SERVICE_Context *service;
1508 unsigned long long aport;
1509 unsigned long long bport;
1512 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1513 if (service == NULL)
1515 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1518 ("Failed to start service for `%s' transport plugin.\n"),
1524 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1530 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1533 &aport)) && (aport > 65535)))
1535 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1538 ("Require valid port number for service `%s' in configuration!\n"),
1540 GNUNET_SERVICE_stop (service);
1545 plugin = GNUNET_malloc (sizeof (struct Plugin));
1546 plugin->open_port = bport;
1547 plugin->adv_port = aport;
1549 plugin->lsock = NULL;
1550 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1552 api->send = &tcp_plugin_send;
1553 api->disconnect = &tcp_plugin_disconnect;
1554 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1555 api->check_address = &tcp_plugin_check_address;
1556 api->address_to_string = &tcp_address_to_string;
1557 plugin->service = service;
1558 plugin->server = GNUNET_SERVICE_get_server (service);
1559 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1560 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1563 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1565 plugin->handlers[i].callback_cls = plugin;
1566 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1568 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1569 "tcp", _("TCP transport listening on port %llu\n"), bport);
1571 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1573 _("TCP transport advertises itself as being on port %llu\n"),
1575 GNUNET_SERVER_disconnect_notify (plugin->server,
1578 /* FIXME: do the two calls below periodically again and
1579 not just once (since the info we get might change...) */
1580 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1581 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1584 HOSTNAME_RESOLVE_TIMEOUT,
1585 &process_hostname_ips,
1592 * Exit point from the plugin.
1595 libgnunet_plugin_transport_tcp_done (void *cls)
1597 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1598 struct Plugin *plugin = api->cls;
1599 struct Session *session;
1601 while (NULL != (session = plugin->sessions))
1602 disconnect_session (session);
1603 if (NULL != plugin->hostname_dns)
1605 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1606 plugin->hostname_dns = NULL;
1608 GNUNET_SERVICE_stop (plugin->service);
1609 GNUNET_free (plugin->handlers);
1610 GNUNET_free (plugin);
1615 /* end of plugin_transport_tcp.c */