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_NO
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 * Encapsulation of all of the state of the plugin.
73 * Information kept for each message that is yet to
80 * This is a doubly-linked list.
82 struct PendingMessage *next;
85 * This is a doubly-linked list.
87 struct PendingMessage *prev;
95 * Continuation function to call once the message
96 * has been sent. Can be NULL if there is no
97 * continuation to call.
99 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
102 * Closure for transmit_cont.
104 void *transmit_cont_cls;
107 * Timeout value for the pending message.
109 struct GNUNET_TIME_Absolute timeout;
112 * So that the gnunet-service-transport can group messages together,
113 * these pending messages need to accept a message buffer and size
114 * instead of just a GNUNET_MessageHeader.
122 * Session handle for TCP connections.
128 * Stored in a linked list.
130 struct Session *next;
133 * Pointer to the global plugin struct.
135 struct Plugin *plugin;
138 * The client (used to identify this connection)
140 struct GNUNET_SERVER_Client *client;
143 * Messages currently pending for transmission
144 * to this peer, if any.
146 struct PendingMessage *pending_messages_head;
149 * Messages currently pending for transmission
150 * to this peer, if any.
152 struct PendingMessage *pending_messages_tail;
155 * Handle for pending transmission request.
157 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
160 * To whom are we talking to (set to our identity
161 * if we are still waiting for the welcome message)
163 struct GNUNET_PeerIdentity target;
166 * ID of task used to delay receiving more to throttle sender.
168 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
171 * Address of the other peer (either based on our 'connect'
172 * call or on our 'accept' call).
177 * Last activity on this connection. Used to select preferred
180 struct GNUNET_TIME_Absolute last_activity;
183 * Length of connect_addr.
188 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
190 int expecting_welcome;
193 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
201 * Encapsulation of all of the state of the plugin.
208 struct GNUNET_TRANSPORT_PluginEnvironment *env;
213 struct GNUNET_CONNECTION_Handle *lsock;
216 * List of open TCP sessions.
218 struct Session *sessions;
221 * Handle for the statistics service.
223 struct GNUNET_STATISTICS_Handle *statistics;
226 * Handle to the network service.
228 struct GNUNET_SERVICE_Context *service;
231 * Handle to the server for this service.
233 struct GNUNET_SERVER_Handle *server;
236 * Copy of the handler array where the closures are
237 * set to this struct's instance.
239 struct GNUNET_SERVER_MessageHandler *handlers;
242 * Handle for request of hostname resolution, non-NULL if pending.
244 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
247 * ID of task used to update our addresses when one expires.
249 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
252 * Port that we are actually listening on.
257 * Port that the user said we would have visible to the
266 * Find the session handle for the given client.
268 * @return NULL if no matching session exists
270 static struct Session *
271 find_session_by_client (struct Plugin *plugin,
272 const struct GNUNET_SERVER_Client *client)
276 ret = plugin->sessions;
277 while ((ret != NULL) && (client != ret->client))
284 * Create a new session. Also queues a welcome message.
287 * @param target peer to connect to
288 * @param client client to use
289 * @return new session object
291 static struct Session *
292 create_session (struct Plugin *plugin,
293 const struct GNUNET_PeerIdentity *target,
294 struct GNUNET_SERVER_Client *client)
297 struct PendingMessage *pm;
298 struct WelcomeMessage welcome;
300 GNUNET_assert (client != NULL);
301 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
303 "Creating new session for peer `%4s'\n",
304 GNUNET_i2s (target));
305 ret = GNUNET_malloc (sizeof (struct Session));
306 ret->last_activity = GNUNET_TIME_absolute_get ();
307 ret->plugin = plugin;
308 ret->next = plugin->sessions;
309 plugin->sessions = ret;
310 ret->client = client;
311 ret->target = *target;
312 ret->expecting_welcome = GNUNET_YES;
313 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
314 pm->msg = (const char*) &pm[1];
315 pm->message_size = sizeof (struct WelcomeMessage);
316 welcome.header.size = htons (sizeof (struct WelcomeMessage));
317 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
318 welcome.clientIdentity = *plugin->env->my_identity;
319 memcpy (&pm[1], &welcome, sizeof (welcome));
320 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
321 GNUNET_STATISTICS_update (plugin->env->stats,
322 gettext_noop ("# bytes currently in TCP buffers"),
325 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
326 ret->pending_messages_tail,
328 GNUNET_STATISTICS_update (plugin->env->stats,
329 gettext_noop ("# TCP sessions active"),
337 * If we have pending messages, ask the server to
338 * transmit them (schedule the respective tasks, etc.)
340 * @param session for which session should we do this
342 static void process_pending_messages (struct Session *session);
346 * Function called to notify a client about the socket
347 * being ready to queue more data. "buf" will be
348 * NULL and "size" zero if the socket was closed for
349 * writing in the meantime.
352 * @param size number of bytes available in buf
353 * @param buf where the callee should write the message
354 * @return number of bytes written to buf
357 do_transmit (void *cls, size_t size, void *buf)
359 struct Session *session = cls;
360 struct GNUNET_PeerIdentity pid;
361 struct Plugin *plugin;
362 struct PendingMessage *pos;
363 struct PendingMessage *hd;
364 struct PendingMessage *tl;
365 struct GNUNET_TIME_Absolute now;
369 session->transmit_handle = NULL;
370 plugin = session->plugin;
374 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
376 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
377 GNUNET_i2s (&session->target));
379 /* timeout; cancel all messages that have already expired */
383 now = GNUNET_TIME_absolute_get ();
384 while ( (NULL != (pos = session->pending_messages_head)) &&
385 (pos->timeout.value <= now.value) )
387 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
388 session->pending_messages_tail,
391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
393 "Failed to transmit %u byte message to `%4s'.\n",
395 GNUNET_i2s (&session->target));
397 ret += pos->message_size;
398 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
400 /* do this call before callbacks (so that if callbacks destroy
401 session, they have a chance to cancel actions done by this
403 process_pending_messages (session);
404 pid = session->target;
405 /* no do callbacks and do not use session again since
406 the callbacks may abort the session */
407 while (NULL != (pos = hd))
409 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
410 if (pos->transmit_cont != NULL)
411 pos->transmit_cont (pos->transmit_cont_cls,
412 &pid, GNUNET_SYSERR);
415 GNUNET_STATISTICS_update (plugin->env->stats,
416 gettext_noop ("# bytes currently in TCP buffers"),
419 GNUNET_STATISTICS_update (plugin->env->stats,
420 gettext_noop ("# bytes discarded by TCP (timeout)"),
425 /* copy all pending messages that would fit */
430 while (NULL != (pos = session->pending_messages_head))
432 if (ret + pos->message_size > size)
434 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
435 session->pending_messages_tail,
437 GNUNET_assert (size >= pos->message_size);
438 memcpy (cbuf, pos->msg, pos->message_size);
439 cbuf += pos->message_size;
440 ret += pos->message_size;
441 size -= pos->message_size;
442 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
444 /* schedule 'continuation' before callbacks so that callbacks that
445 cancel everything don't cause us to use a session that no longer
447 process_pending_messages (session);
448 session->last_activity = GNUNET_TIME_absolute_get ();
449 pid = session->target;
450 /* we'll now call callbacks that may cancel the session; hence
451 we should not use 'session' after this point */
452 while (NULL != (pos = hd))
454 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
455 if (pos->transmit_cont != NULL)
456 pos->transmit_cont (pos->transmit_cont_cls,
460 GNUNET_assert (hd == NULL);
461 GNUNET_assert (tl == NULL);
463 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
464 "tcp", "Transmitting %u bytes\n", ret);
466 GNUNET_STATISTICS_update (plugin->env->stats,
467 gettext_noop ("# bytes currently in TCP buffers"),
470 GNUNET_STATISTICS_update (plugin->env->stats,
471 gettext_noop ("# bytes transmitted via TCP"),
479 * If we have pending messages, ask the server to
480 * transmit them (schedule the respective tasks, etc.)
482 * @param session for which session should we do this
485 process_pending_messages (struct Session *session)
487 struct PendingMessage *pm;
489 GNUNET_assert (session->client != NULL);
490 if (session->transmit_handle != NULL)
492 if (NULL == (pm = session->pending_messages_head))
494 session->transmit_handle
495 = GNUNET_SERVER_notify_transmit_ready (session->client,
497 GNUNET_TIME_absolute_get_remaining
499 &do_transmit, session);
504 * Functions with this signature are called whenever we need
505 * to close a session due to a disconnect or failure to
506 * establish a connection.
508 * @param session session to close down
511 disconnect_session (struct Session *session)
513 struct Session *prev;
515 struct PendingMessage *pm;
518 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
520 "Disconnecting from `%4s' at %s (session %p).\n",
521 GNUNET_i2s (&session->target),
522 (session->connect_addr != NULL) ?
523 GNUNET_a2s (session->connect_addr,
524 session->connect_alen) : "*", session);
526 /* remove from session list */
528 pos = session->plugin->sessions;
529 while (pos != session)
535 session->plugin->sessions = session->next;
537 prev->next = session->next;
539 if (session->transmit_handle != NULL)
541 GNUNET_CONNECTION_notify_transmit_ready_cancel
542 (session->transmit_handle);
543 session->transmit_handle = NULL;
545 while (NULL != (pm = session->pending_messages_head))
548 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
550 pm->transmit_cont != NULL
551 ? "Could not deliver message to `%4s'.\n"
553 "Could not deliver message to `%4s', notifying.\n",
554 GNUNET_i2s (&session->target));
556 GNUNET_STATISTICS_update (session->plugin->env->stats,
557 gettext_noop ("# bytes currently in TCP buffers"),
558 - (int64_t) pm->message_size,
560 GNUNET_STATISTICS_update (session->plugin->env->stats,
561 gettext_noop ("# bytes discarded by TCP (disconnect)"),
564 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
565 session->pending_messages_tail,
567 if (NULL != pm->transmit_cont)
568 pm->transmit_cont (pm->transmit_cont_cls,
569 &session->target, GNUNET_SYSERR);
572 GNUNET_break (session->client != NULL);
573 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
575 GNUNET_SCHEDULER_cancel (session->plugin->env->sched,
576 session->receive_delay_task);
577 GNUNET_SERVER_receive_done (session->client,
580 GNUNET_SERVER_client_drop (session->client);
581 GNUNET_STATISTICS_update (session->plugin->env->stats,
582 gettext_noop ("# TCP sessions active"),
585 GNUNET_free_non_null (session->connect_addr);
586 GNUNET_free (session);
591 * Given two otherwise equivalent sessions, pick the better one.
593 * @param s1 one session (also default)
594 * @param s2 other session
595 * @return "better" session (more active)
597 static struct Session *
598 select_better_session (struct Session *s1,
605 if ( (s1->expecting_welcome == GNUNET_NO) &&
606 (s2->expecting_welcome == GNUNET_YES) )
608 if ( (s1->expecting_welcome == GNUNET_YES) &&
609 (s2->expecting_welcome == GNUNET_NO) )
611 if (s1->last_activity.value < s2->last_activity.value)
613 if (s1->last_activity.value > s2->last_activity.value)
615 if ( (GNUNET_YES == s1->inbound) &&
616 (GNUNET_NO == s2->inbound) )
618 if ( (GNUNET_NO == s1->inbound) &&
619 (GNUNET_YES == s2->inbound) )
626 * Function that can be used by the transport service to transmit
627 * a message using the plugin. Note that in the case of a
628 * peer disconnecting, the continuation MUST be called
629 * prior to the disconnect notification itself. This function
630 * will be called with this peer's HELLO message to initiate
631 * a fresh connection to another peer.
634 * @param target who should receive this message
635 * @param msg the message to transmit
636 * @param msgbuf_size number of bytes in 'msg'
637 * @param priority how important is the message (most plugins will
638 * ignore message priority and just FIFO)
639 * @param timeout how long to wait at most for the transmission (does not
640 * require plugins to discard the message after the timeout,
641 * just advisory for the desired delay; most plugins will ignore
643 * @param addr the address to use (can be NULL if the plugin
644 * is "on its own" (i.e. re-use existing TCP connection))
645 * @param addrlen length of the address in bytes
646 * @param force_address GNUNET_YES if the plugin MUST use the given address,
647 * GNUNET_NO means the plugin may use any other address and
648 * GNUNET_SYSERR means that only reliable existing
649 * bi-directional connections should be used (regardless
651 * @param cont continuation to call once the message has
652 * been transmitted (or if the transport is ready
653 * for the next transmission call; or if the
654 * peer disconnected...); can be NULL
655 * @param cont_cls closure for cont
656 * @return number of bytes used (on the physical network, with overheads);
657 * -1 on hard errors (i.e. address invalid); 0 is a legal value
658 * and does NOT mean that the message was not transmitted (DV)
661 tcp_plugin_send (void *cls,
662 const struct GNUNET_PeerIdentity *target,
666 struct GNUNET_TIME_Relative timeout,
670 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
672 struct Plugin *plugin = cls;
673 struct Session *session;
674 struct Session *cand_session;
675 struct Session *next;
676 struct PendingMessage *pm;
677 struct GNUNET_CONNECTION_Handle *sa;
680 GNUNET_STATISTICS_update (plugin->env->stats,
681 gettext_noop ("# bytes TCP was asked to transmit"),
684 /* FIXME: we could do this a cheaper with a hash table
685 where we could restrict the iteration to entries that match
686 the target peer... */
688 next = plugin->sessions;
689 while (NULL != (session = next))
691 next = session->next;
692 GNUNET_assert (session->client != NULL);
693 if (0 != memcmp (target,
695 sizeof (struct GNUNET_PeerIdentity)))
697 if ( ( (GNUNET_SYSERR == force_address) &&
698 (session->expecting_welcome == GNUNET_NO) ) ||
699 (GNUNET_NO == force_address) )
701 cand_session = select_better_session (cand_session,
705 GNUNET_break (GNUNET_YES == force_address);
711 if (session->inbound == GNUNET_YES)
713 if (addrlen != session->connect_alen)
715 if (0 != memcmp (session->connect_addr,
719 cand_session = select_better_session (cand_session,
722 session = cand_session;
723 if ( (session == NULL) &&
727 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
729 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
730 GNUNET_i2s (target));
732 GNUNET_STATISTICS_update (plugin->env->stats,
733 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
740 if (sizeof (struct sockaddr_in) == addrlen)
744 else if (sizeof (struct sockaddr_in6) == addrlen)
753 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
755 GNUNET_SERVER_MAX_MESSAGE_SIZE);
759 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
761 "Failed to create connection to `%4s' at `%s'\n",
763 GNUNET_a2s (addr, addrlen));
765 GNUNET_STATISTICS_update (plugin->env->stats,
766 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
772 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
774 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
776 GNUNET_a2s (addr, addrlen));
778 session = create_session (plugin,
780 GNUNET_SERVER_connect_socket (plugin->server,
782 session->connect_addr = GNUNET_malloc (addrlen);
783 memcpy (session->connect_addr,
786 session->connect_alen = addrlen;
788 GNUNET_assert (session != NULL);
789 GNUNET_assert (session->client != NULL);
790 GNUNET_STATISTICS_update (plugin->env->stats,
791 gettext_noop ("# bytes currently in TCP buffers"),
794 /* create new message entry */
795 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
796 pm->msg = (const char*) &pm[1];
797 memcpy (&pm[1], msg, msgbuf_size);
798 pm->message_size = msgbuf_size;
799 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
800 pm->transmit_cont = cont;
801 pm->transmit_cont_cls = cont_cls;
803 /* append pm to pending_messages list */
804 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
805 session->pending_messages_tail,
806 session->pending_messages_tail,
809 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
811 "Asked to transmit %u bytes to `%s', added message to list.\n",
813 GNUNET_i2s (target));
815 process_pending_messages (session);
821 * Function that can be called to force a disconnect from the
822 * specified neighbour. This should also cancel all previously
823 * scheduled transmissions. Obviously the transmission may have been
824 * partially completed already, which is OK. The plugin is supposed
825 * to close the connection (if applicable) and no longer call the
826 * transmit continuation(s).
828 * Finally, plugin MUST NOT call the services's receive function to
829 * notify the service that the connection to the specified target was
830 * closed after a getting this call.
833 * @param target peer for which the last transmission is
837 tcp_plugin_disconnect (void *cls,
838 const struct GNUNET_PeerIdentity *target)
840 struct Plugin *plugin = cls;
841 struct Session *session;
842 struct Session *next;
843 struct PendingMessage *pm;
846 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
848 "Asked to cancel session with `%4s'\n",
849 GNUNET_i2s (target));
851 next = plugin->sessions;
852 while (NULL != (session = next))
854 next = session->next;
855 if (0 != memcmp (target,
857 sizeof (struct GNUNET_PeerIdentity)))
859 pm = session->pending_messages_head;
862 pm->transmit_cont = NULL;
863 pm->transmit_cont_cls = NULL;
866 disconnect_session (session);
872 * Context for address to string conversion.
874 struct PrettyPrinterContext
877 * Function to call with the result.
879 GNUNET_TRANSPORT_AddressStringCallback asc;
887 * Port to add after the IP address.
894 * Append our port and forward the result.
896 * @param cls the 'struct PrettyPrinterContext*'
897 * @param hostname hostname part of the address
900 append_port (void *cls, const char *hostname)
902 struct PrettyPrinterContext *ppc = cls;
905 if (hostname == NULL)
907 ppc->asc (ppc->asc_cls, NULL);
911 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
912 ppc->asc (ppc->asc_cls, ret);
918 * Convert the transports address to a nice, human-readable
922 * @param type name of the transport that generated the address
923 * @param addr one of the addresses of the host, NULL for the last address
924 * the specific address format depends on the transport
925 * @param addrlen length of the address
926 * @param numeric should (IP) addresses be displayed in numeric form?
927 * @param timeout after how long should we give up?
928 * @param asc function to call on each string
929 * @param asc_cls closure for asc
932 tcp_plugin_address_pretty_printer (void *cls,
937 struct GNUNET_TIME_Relative timeout,
938 GNUNET_TRANSPORT_AddressStringCallback asc,
941 struct Plugin *plugin = cls;
942 const struct sockaddr_in *v4;
943 const struct sockaddr_in6 *v6;
944 struct PrettyPrinterContext *ppc;
946 if ((addrlen != sizeof (struct sockaddr_in)) &&
947 (addrlen != sizeof (struct sockaddr_in6)))
949 /* invalid address */
954 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
956 ppc->asc_cls = asc_cls;
957 if (addrlen == sizeof (struct sockaddr_in))
959 v4 = (const struct sockaddr_in *) addr;
960 ppc->port = ntohs (v4->sin_port);
964 v6 = (const struct sockaddr_in6 *) addr;
965 ppc->port = ntohs (v6->sin6_port);
968 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
972 !numeric, timeout, &append_port, ppc);
977 * Check if the given port is plausible (must be either
978 * our listen port or our advertised port). If it is
979 * neither, we return one of these two ports at random.
981 * @param plugin global variables
982 * @param in_port port number to check
983 * @return either in_port or a more plausible port
986 check_port (struct Plugin *plugin, uint16_t in_port)
988 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
990 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
992 ? plugin->open_port : plugin->adv_port;
997 * Another peer has suggested an address for this peer and transport
998 * plugin. Check that this could be a valid address.
1000 * @param cls closure, our 'struct Plugin*'
1001 * @param addr pointer to the address
1002 * @param addrlen length of addr
1003 * @return GNUNET_OK if this is a plausible address for this peer
1007 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
1009 struct Plugin *plugin = cls;
1010 char buf[sizeof (struct sockaddr_in6)];
1011 struct sockaddr_in *v4;
1012 struct sockaddr_in6 *v6;
1014 if ((addrlen != sizeof (struct sockaddr_in)) &&
1015 (addrlen != sizeof (struct sockaddr_in6)))
1017 GNUNET_break_op (0);
1018 return GNUNET_SYSERR;
1020 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1021 if (addrlen == sizeof (struct sockaddr_in))
1023 v4 = (struct sockaddr_in *) buf;
1024 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1028 v6 = (struct sockaddr_in6 *) buf;
1029 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1032 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1034 "Informing transport service about my address `%s'.\n",
1035 GNUNET_a2s (addr, addrlen));
1042 * We've received a welcome from this peer via TCP. Possibly create a
1043 * fresh client record and send back our welcome.
1045 * @param cls closure
1046 * @param client identification of the client
1047 * @param message the actual message
1050 handle_tcp_welcome (void *cls,
1051 struct GNUNET_SERVER_Client *client,
1052 const struct GNUNET_MessageHeader *message)
1054 struct Plugin *plugin = cls;
1055 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1056 struct Session *session;
1061 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1063 "Received %s message from a `%4s/%p'.\n",
1065 GNUNET_i2s (&wm->clientIdentity), client);
1067 GNUNET_STATISTICS_update (plugin->env->stats,
1068 gettext_noop ("# TCP WELCOME messages received"),
1071 session = find_session_by_client (plugin, client);
1072 if (session == NULL)
1074 GNUNET_SERVER_client_keep (client);
1075 session = create_session (plugin,
1076 &wm->clientIdentity, client);
1077 session->inbound = GNUNET_YES;
1079 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1082 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1084 "Found address `%s' for incoming connection %p\n",
1085 GNUNET_a2s (vaddr, alen),
1088 session->connect_addr = vaddr;
1089 session->connect_alen = alen;
1094 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1096 "Did not obtain TCP socket address for incoming connection\n");
1100 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1102 "Creating new session %p for connection %p\n",
1105 process_pending_messages (session);
1107 if (session->expecting_welcome != GNUNET_YES)
1109 GNUNET_break_op (0);
1110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1113 session->expecting_welcome = GNUNET_NO;
1114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1119 * Task to signal the server that we can continue
1120 * receiving from the TCP client now.
1122 * @param cls the 'struct Session*'
1123 * @param tc task context (unused)
1126 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1128 struct Session *session = cls;
1129 struct GNUNET_TIME_Relative delay;
1131 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1132 delay = session->plugin->env->receive (session->plugin->env->cls,
1135 if (delay.value == 0)
1136 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1138 session->receive_delay_task =
1139 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1140 delay, &delayed_done, session);
1145 * We've received data for this peer via TCP. Unbox,
1146 * compute latency and forward.
1148 * @param cls closure
1149 * @param client identification of the client
1150 * @param message the actual message
1153 handle_tcp_data (void *cls,
1154 struct GNUNET_SERVER_Client *client,
1155 const struct GNUNET_MessageHeader *message)
1157 struct Plugin *plugin = cls;
1158 struct Session *session;
1159 struct GNUNET_TIME_Relative delay;
1161 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
1163 /* We don't want to propagate WELCOME messages up! */
1164 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1167 session = find_session_by_client (plugin, client);
1168 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1173 session->last_activity = GNUNET_TIME_absolute_get ();
1175 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1177 "Passing %u bytes of type %u from `%4s' to transport service.\n",
1178 (unsigned int) ntohs (message->size),
1179 (unsigned int) ntohs (message->type),
1180 GNUNET_i2s (&session->target));
1182 GNUNET_STATISTICS_update (plugin->env->stats,
1183 gettext_noop ("# bytes received via TCP"),
1184 ntohs (message->size),
1186 delay = plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1187 session->connect_addr,
1188 session->connect_alen);
1190 if (delay.value == 0)
1191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1193 session->receive_delay_task =
1194 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1195 delay, &delayed_done, session);
1200 * Handlers for the various TCP messages.
1202 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1203 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1204 sizeof (struct WelcomeMessage)},
1205 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1211 * Functions with this signature are called whenever a peer
1212 * is disconnected on the network level.
1214 * @param cls closure
1215 * @param client identification of the client
1218 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1220 struct Plugin *plugin = cls;
1221 struct Session *session;
1225 session = find_session_by_client (plugin, client);
1226 if (session == NULL)
1227 return; /* unknown, nothing to do */
1229 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1231 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1232 GNUNET_i2s (&session->target),
1233 (session->connect_addr != NULL) ?
1234 GNUNET_a2s (session->connect_addr,
1235 session->connect_alen) : "*", client);
1237 disconnect_session (session);
1242 * Add the IP of our network interface to the list of
1243 * our external IP addresses.
1246 process_interfaces (void *cls,
1249 const struct sockaddr *addr, socklen_t addrlen)
1251 struct Plugin *plugin = cls;
1253 struct sockaddr_in *v4;
1254 struct sockaddr_in6 *v6;
1256 af = addr->sa_family;
1259 v4 = (struct sockaddr_in *) addr;
1260 v4->sin_port = htons (plugin->adv_port);
1264 GNUNET_assert (af == AF_INET6);
1265 v6 = (struct sockaddr_in6 *) addr;
1266 v6->sin6_port = htons (plugin->adv_port);
1268 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1269 GNUNET_ERROR_TYPE_BULK,
1270 "tcp", _("Found address `%s' (%s)\n"),
1271 GNUNET_a2s (addr, addrlen), name);
1272 plugin->env->notify_address (plugin->env->cls,
1274 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1280 * Function called by the resolver for each address obtained from DNS
1281 * for our own hostname. Add the addresses to the list of our
1282 * external IP addresses.
1284 * @param cls closure
1285 * @param addr one of the addresses of the host, NULL for the last address
1286 * @param addrlen length of the address
1289 process_hostname_ips (void *cls,
1290 const struct sockaddr *addr, socklen_t addrlen)
1292 struct Plugin *plugin = cls;
1296 plugin->hostname_dns = NULL;
1299 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1304 * Entry point for the plugin.
1307 libgnunet_plugin_transport_tcp_init (void *cls)
1309 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1310 struct GNUNET_TRANSPORT_PluginFunctions *api;
1311 struct Plugin *plugin;
1312 struct GNUNET_SERVICE_Context *service;
1313 unsigned long long aport;
1314 unsigned long long bport;
1317 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1318 if (service == NULL)
1320 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1323 ("Failed to start service for `%s' transport plugin.\n"),
1329 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1335 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1338 &aport)) && (aport > 65535)))
1340 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1343 ("Require valid port number for service `%s' in configuration!\n"),
1345 GNUNET_SERVICE_stop (service);
1350 plugin = GNUNET_malloc (sizeof (struct Plugin));
1351 plugin->open_port = bport;
1352 plugin->adv_port = aport;
1354 plugin->lsock = NULL;
1355 plugin->statistics = NULL;
1356 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1358 api->send = &tcp_plugin_send;
1359 api->disconnect = &tcp_plugin_disconnect;
1360 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1361 api->check_address = &tcp_plugin_check_address;
1362 plugin->service = service;
1363 plugin->server = GNUNET_SERVICE_get_server (service);
1364 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1365 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1368 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1370 plugin->handlers[i].callback_cls = plugin;
1371 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1373 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1374 "tcp", _("TCP transport listening on port %llu\n"), bport);
1376 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1378 _("TCP transport advertises itself as being on port %llu\n"),
1380 GNUNET_SERVER_disconnect_notify (plugin->server,
1383 /* FIXME: do the two calls below periodically again and
1384 not just once (since the info we get might change...) */
1385 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1386 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1389 HOSTNAME_RESOLVE_TIMEOUT,
1390 &process_hostname_ips,
1397 * Exit point from the plugin.
1400 libgnunet_plugin_transport_tcp_done (void *cls)
1402 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1403 struct Plugin *plugin = api->cls;
1404 struct Session *session;
1406 while (NULL != (session = plugin->sessions))
1407 disconnect_session (session);
1408 if (NULL != plugin->hostname_dns)
1410 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1411 plugin->hostname_dns = NULL;
1413 GNUNET_SERVICE_stop (plugin->service);
1414 GNUNET_free (plugin->handlers);
1415 GNUNET_free (plugin);
1420 /* end of plugin_transport_tcp.c */