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_YES
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 * At what time did we reset last_received last?
170 struct GNUNET_TIME_Absolute last_quota_update;
173 * Address of the other peer (either based on our 'connect'
174 * call or on our 'accept' call).
179 * How many bytes have we received since the "last_quota_update"
182 uint64_t last_received;
185 * Number of bytes per ms that this peer is allowed
191 * Length of connect_addr.
196 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
198 int expecting_welcome;
204 * Encapsulation of all of the state of the plugin.
211 struct GNUNET_TRANSPORT_PluginEnvironment *env;
216 struct GNUNET_CONNECTION_Handle *lsock;
219 * List of open TCP sessions.
221 struct Session *sessions;
224 * Handle for the statistics service.
226 struct GNUNET_STATISTICS_Handle *statistics;
229 * Handle to the network service.
231 struct GNUNET_SERVICE_Context *service;
234 * Handle to the server for this service.
236 struct GNUNET_SERVER_Handle *server;
239 * Copy of the handler array where the closures are
240 * set to this struct's instance.
242 struct GNUNET_SERVER_MessageHandler *handlers;
245 * Handle for request of hostname resolution, non-NULL if pending.
247 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
250 * ID of task used to update our addresses when one expires.
252 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
255 * Port that we are actually listening on.
260 * Port that the user said we would have visible to the
269 * Find a session handle for the given peer.
270 * FIXME: using a hash map we could do this in O(1).
272 * @return NULL if no matching session exists
274 static struct Session *
275 find_session_by_target (struct Plugin *plugin,
276 const struct GNUNET_PeerIdentity *target)
280 ret = plugin->sessions;
281 while ( (ret != NULL) &&
282 ((GNUNET_SYSERR == ret->expecting_welcome) ||
283 (0 != memcmp (target,
284 &ret->target, sizeof (struct GNUNET_PeerIdentity)))))
291 * Find the session handle for the given client.
293 * @return NULL if no matching session exists
295 static struct Session *
296 find_session_by_client (struct Plugin *plugin,
297 const struct GNUNET_SERVER_Client *client)
301 ret = plugin->sessions;
302 while ((ret != NULL) && (client != ret->client))
309 * Create a new session. Also queues a welcome message.
312 * @param target peer to connect to
313 * @param client client to use
314 * @return new session object
316 static struct Session *
317 create_session (struct Plugin *plugin,
318 const struct GNUNET_PeerIdentity *target,
319 struct GNUNET_SERVER_Client *client)
322 struct PendingMessage *pm;
323 struct WelcomeMessage welcome;
325 ret = GNUNET_malloc (sizeof (struct Session));
326 ret->plugin = plugin;
327 ret->next = plugin->sessions;
328 plugin->sessions = ret;
329 ret->client = client;
330 ret->target = *target;
331 ret->last_quota_update = GNUNET_TIME_absolute_get ();
332 ret->quota_in = plugin->env->default_quota_in;
333 ret->expecting_welcome = GNUNET_YES;
334 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
335 pm->msg = (const char*) &pm[1];
336 pm->message_size = sizeof (struct WelcomeMessage);
337 welcome.header.size = htons (sizeof (struct WelcomeMessage));
338 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
339 welcome.clientIdentity = *plugin->env->my_identity;
340 memcpy (&pm[1], &welcome, sizeof (welcome));
341 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
342 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
343 ret->pending_messages_tail,
350 * If we have pending messages, ask the server to
351 * transmit them (schedule the respective tasks, etc.)
353 * @param session for which session should we do this
355 static void process_pending_messages (struct Session *session);
359 * Function called to notify a client about the socket
360 * being ready to queue more data. "buf" will be
361 * NULL and "size" zero if the socket was closed for
362 * writing in the meantime.
365 * @param size number of bytes available in buf
366 * @param buf where the callee should write the message
367 * @return number of bytes written to buf
370 do_transmit (void *cls, size_t size, void *buf)
372 struct Session *session = cls;
373 struct PendingMessage *pm;
378 session->transmit_handle = NULL;
382 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
384 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
385 GNUNET_i2s (&session->target));
388 while (NULL != (pm = session->pending_messages_head))
390 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
391 session->pending_messages_tail,
394 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
396 "Failed to transmit %u byte message to `%4s'.\n",
398 GNUNET_i2s (&session->target));
400 if (pm->transmit_cont != NULL)
401 pm->transmit_cont (pm->transmit_cont_cls,
402 &session->target, GNUNET_SYSERR);
409 while (NULL != (pm = session->pending_messages_head))
411 if (size < pm->message_size)
413 memcpy (cbuf, pm->msg, pm->message_size);
414 cbuf += pm->message_size;
415 ret += pm->message_size;
416 size -= pm->message_size;
417 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
418 session->pending_messages_tail,
420 if (pm->transmit_cont != NULL)
421 pm->transmit_cont (pm->transmit_cont_cls,
422 &session->target, GNUNET_OK);
425 if (session->client != NULL)
426 process_pending_messages (session);
428 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
429 "tcp", "Transmitting %u bytes\n", ret);
436 * If we have pending messages, ask the server to
437 * transmit them (schedule the respective tasks, etc.)
439 * @param session for which session should we do this
442 process_pending_messages (struct Session *session)
444 struct PendingMessage *pm;
445 GNUNET_assert (session->client != NULL);
446 if (session->transmit_handle != NULL)
448 if (NULL == (pm = session->pending_messages_head))
450 session->transmit_handle
451 = GNUNET_SERVER_notify_transmit_ready (session->client,
453 GNUNET_TIME_absolute_get_remaining
455 &do_transmit, session);
460 * Functions with this signature are called whenever we need
461 * to close a session due to a disconnect or failure to
462 * establish a connection.
464 * @param session session to close down
467 disconnect_session (struct Session *session)
469 struct Session *prev;
471 struct PendingMessage *pm;
474 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
476 "Disconnecting from `%4s' at %s (session %p).\n",
477 GNUNET_i2s (&session->target),
478 (session->connect_addr != NULL) ?
479 GNUNET_a2s (session->connect_addr,
480 session->connect_alen) : "*", session);
482 /* remove from session list */
484 pos = session->plugin->sessions;
485 while (pos != session)
491 session->plugin->sessions = session->next;
493 prev->next = session->next;
495 if (session->transmit_handle != NULL)
497 GNUNET_CONNECTION_notify_transmit_ready_cancel
498 (session->transmit_handle);
499 session->transmit_handle = NULL;
501 while (NULL != (pm = session->pending_messages_head))
504 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
506 pm->transmit_cont != NULL
507 ? "Could not deliver message to `%4s'.\n"
509 "Could not deliver message to `%4s', notifying.\n",
510 GNUNET_i2s (&session->target));
512 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
513 session->pending_messages_tail,
515 if (NULL != pm->transmit_cont)
516 pm->transmit_cont (pm->transmit_cont_cls,
517 &session->target, GNUNET_SYSERR);
520 if (GNUNET_NO == session->expecting_welcome)
523 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
525 "Notifying transport service about loss of data connection with `%4s'.\n",
526 GNUNET_i2s (&session->target));
528 /* Data session that actually went past the initial handshake;
529 transport service may know about this one, so we need to
530 notify transport service about disconnect */
531 // FIXME: we should have a very clear connect-disconnect
532 // protocol with gnunet-service-transport!
533 session->plugin->env->receive (session->plugin->env->cls,
534 &session->target, NULL,
536 session->connect_addr,
537 session->connect_alen);
539 if (session->client != NULL)
541 GNUNET_SERVER_client_drop (session->client);
542 session->client = NULL;
544 GNUNET_free_non_null (session->connect_addr);
545 GNUNET_free (session);
550 * Function that can be used by the transport service to transmit
551 * a message using the plugin. Note that in the case of a
552 * peer disconnecting, the continuation MUST be called
553 * prior to the disconnect notification itself. This function
554 * will be called with this peer's HELLO message to initiate
555 * a fresh connection to another peer.
558 * @param target who should receive this message
559 * @param msg the message to transmit
560 * @param priority how important is the message (most plugins will
561 * ignore message priority and just FIFO)
562 * @param timeout how long to wait at most for the transmission (does not
563 * require plugins to discard the message after the timeout,
564 * just advisory for the desired delay; most plugins will ignore
566 * @param addr the address to use (can be NULL if the plugin
567 * is "on its own" (i.e. re-use existing TCP connection))
568 * @param addrlen length of the address in bytes
569 * @param force_address GNUNET_YES if the plugin MUST use the given address,
570 * otherwise the plugin may use other addresses or
571 * existing connections (if available)
572 * @param cont continuation to call once the message has
573 * been transmitted (or if the transport is ready
574 * for the next transmission call; or if the
575 * peer disconnected...); can be NULL
576 * @param cont_cls closure for cont
577 * @return number of bytes used (on the physical network, with overheads);
578 * -1 on hard errors (i.e. address invalid); 0 is a legal value
579 * and does NOT mean that the message was not transmitted (DV)
582 tcp_plugin_send (void *cls,
583 const struct GNUNET_PeerIdentity *target,
587 struct GNUNET_TIME_Relative timeout,
591 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
593 struct Plugin *plugin = cls;
594 struct Session *session;
595 struct PendingMessage *pm;
596 struct GNUNET_CONNECTION_Handle *sa;
599 session = plugin->sessions;
600 while ( (session != NULL) &&
601 ( (0 != memcmp (target,
603 sizeof (struct GNUNET_PeerIdentity))) ||
604 ( (GNUNET_YES == force_address) &&
606 ( (addrlen != session->connect_alen) ||
607 (0 != memcmp (session->connect_addr,
610 session = session->next;
611 if ( (session == NULL) &&
615 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
617 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
618 GNUNET_i2s (target));
624 if (sizeof (struct sockaddr_in) == addrlen)
626 else if (sizeof (struct sockaddr_in6) == addrlen)
633 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
635 GNUNET_SERVER_MAX_MESSAGE_SIZE);
639 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
641 "Failed to create connection to `%4s' at `%s'\n",
643 GNUNET_a2s (addr, addrlen));
649 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
651 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
653 GNUNET_a2s (addr, addrlen));
655 session = create_session (plugin,
657 GNUNET_SERVER_connect_socket (plugin->server,
659 session->connect_addr = GNUNET_malloc (addrlen);
660 memcpy (session->connect_addr,
663 session->connect_alen = addrlen;
665 GNUNET_assert (session != NULL);
666 GNUNET_assert (session->client != NULL);
668 /* create new message entry */
669 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
670 pm->msg = (const char*) &pm[1];
671 memcpy (&pm[1], msg, msgbuf_size);
672 pm->message_size = msgbuf_size;
673 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
674 pm->transmit_cont = cont;
675 pm->transmit_cont_cls = cont_cls;
677 /* append pm to pending_messages list */
678 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
679 session->pending_messages_tail,
680 session->pending_messages_tail,
683 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
685 "Asked to transmit %u bytes to `%s', added message to list.\n",
687 GNUNET_i2s (target));
689 process_pending_messages (session);
695 * Function that can be called to force a disconnect from the
696 * specified neighbour. This should also cancel all previously
697 * scheduled transmissions. Obviously the transmission may have been
698 * partially completed already, which is OK. The plugin is supposed
699 * to close the connection (if applicable) and no longer call the
700 * transmit continuation(s).
702 * Finally, plugin MUST NOT call the services's receive function to
703 * notify the service that the connection to the specified target was
704 * closed after a getting this call.
707 * @param target peer for which the last transmission is
711 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
713 struct Plugin *plugin = cls;
714 struct Session *session;
715 struct PendingMessage *pm;
718 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
720 "Asked to cancel session with `%4s'\n",
721 GNUNET_i2s (target));
723 session = plugin->sessions;
724 while (NULL != session)
726 if (0 == memcmp (target,
728 sizeof (struct GNUNET_PeerIdentity)))
730 pm = session->pending_messages_head;
733 pm->transmit_cont = NULL;
734 pm->transmit_cont_cls = NULL;
737 if (session->client != NULL)
739 GNUNET_SERVER_client_drop (session->client);
740 session->client = NULL;
742 /* rest of the clean-up of the session will be done as part of
743 disconnect_notify which should be triggered any time now
744 (or which may be triggering this call in the first place) */
746 session = session->next;
751 struct PrettyPrinterContext
753 GNUNET_TRANSPORT_AddressStringCallback asc;
760 * Append our port and forward the result.
763 append_port (void *cls, const char *hostname)
765 struct PrettyPrinterContext *ppc = cls;
768 if (hostname == NULL)
770 ppc->asc (ppc->asc_cls, NULL);
774 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
775 ppc->asc (ppc->asc_cls, ret);
781 * Convert the transports address to a nice, human-readable
785 * @param type name of the transport that generated the address
786 * @param addr one of the addresses of the host, NULL for the last address
787 * the specific address format depends on the transport
788 * @param addrlen length of the address
789 * @param numeric should (IP) addresses be displayed in numeric form?
790 * @param timeout after how long should we give up?
791 * @param asc function to call on each string
792 * @param asc_cls closure for asc
795 tcp_plugin_address_pretty_printer (void *cls,
800 struct GNUNET_TIME_Relative timeout,
801 GNUNET_TRANSPORT_AddressStringCallback asc,
804 struct Plugin *plugin = cls;
805 const struct sockaddr_in *v4;
806 const struct sockaddr_in6 *v6;
807 struct PrettyPrinterContext *ppc;
809 if ((addrlen != sizeof (struct sockaddr_in)) &&
810 (addrlen != sizeof (struct sockaddr_in6)))
812 /* invalid address */
817 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
819 ppc->asc_cls = asc_cls;
820 if (addrlen == sizeof (struct sockaddr_in))
822 v4 = (const struct sockaddr_in *) addr;
823 ppc->port = ntohs (v4->sin_port);
827 v6 = (const struct sockaddr_in6 *) addr;
828 ppc->port = ntohs (v6->sin6_port);
831 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
835 !numeric, timeout, &append_port, ppc);
840 * Update the last-received and bandwidth quota values
843 * @param session session to update
844 * @param force set to GNUNET_YES if we should update even
845 * though the minimum refresh time has not yet expired
848 update_quota (struct Session *session, int force)
850 struct GNUNET_TIME_Absolute now;
851 unsigned long long delta;
852 unsigned long long total_allowed;
853 unsigned long long total_remaining;
855 now = GNUNET_TIME_absolute_get ();
856 delta = now.value - session->last_quota_update.value;
857 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
858 return; /* too early, not enough data */
860 total_allowed = session->quota_in * delta;
861 if (total_allowed > session->last_received)
863 /* got less than acceptable */
864 total_remaining = total_allowed - session->last_received;
865 session->last_received = 0;
866 delta = total_remaining / session->quota_in; /* bonus seconds */
867 if (delta > MAX_BANDWIDTH_CARRY)
868 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
872 /* got more than acceptable */
873 session->last_received -= total_allowed;
876 session->last_quota_update.value = now.value - delta;
881 * Set a quota for receiving data from the given peer; this is a
882 * per-transport limit. The transport should limit its read/select
883 * calls to stay below the quota (in terms of incoming data).
886 * @param target the peer for whom the quota is given
887 * @param quota_in quota for receiving/sending data in bytes per ms
890 tcp_plugin_set_receive_quota (void *cls,
891 const struct GNUNET_PeerIdentity *target,
894 struct Plugin *plugin = cls;
895 struct Session *session;
897 session = find_session_by_target (plugin, target);
899 return; /* peer must have disconnected, ignore */
900 if (session->quota_in != quota_in)
902 update_quota (session, GNUNET_YES);
903 if (session->quota_in > quota_in)
904 session->last_quota_update = GNUNET_TIME_absolute_get ();
905 session->quota_in = quota_in;
911 * Check if the given port is plausible (must be either
912 * our listen port or our advertised port). If it is
913 * neither, we return one of these two ports at random.
915 * @return either in_port or a more plausible port
918 check_port (struct Plugin *plugin, uint16_t in_port)
920 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
922 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
924 ? plugin->open_port : plugin->adv_port;
929 * Another peer has suggested an address for this peer and transport
930 * plugin. Check that this could be a valid address.
933 * @param addr pointer to the address
934 * @param addrlen length of addr
935 * @return GNUNET_OK if this is a plausible address for this peer
939 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
941 struct Plugin *plugin = cls;
942 char buf[sizeof (struct sockaddr_in6)];
943 struct sockaddr_in *v4;
944 struct sockaddr_in6 *v6;
946 if ((addrlen != sizeof (struct sockaddr_in)) &&
947 (addrlen != sizeof (struct sockaddr_in6)))
950 return GNUNET_SYSERR;
952 memcpy (buf, addr, sizeof (struct sockaddr_in6));
953 if (addrlen == sizeof (struct sockaddr_in))
955 v4 = (struct sockaddr_in *) buf;
956 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
960 v6 = (struct sockaddr_in6 *) buf;
961 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
964 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
966 "Informing transport service about my address `%s'.\n",
967 GNUNET_a2s (addr, addrlen));
974 * We've received a welcome from this peer via TCP. Possibly create a
975 * fresh client record and send back our welcome.
978 * @param client identification of the client
979 * @param message the actual message
982 handle_tcp_welcome (void *cls,
983 struct GNUNET_SERVER_Client *client,
984 const struct GNUNET_MessageHeader *message)
986 struct Plugin *plugin = cls;
987 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
988 struct Session *session;
993 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
995 "Received %s message from a `%4s/%p'.\n",
997 GNUNET_i2s (&wm->clientIdentity), client);
999 session = find_session_by_client (plugin, client);
1000 if (session == NULL)
1002 GNUNET_SERVER_client_keep (client);
1003 session = create_session (plugin,
1004 &wm->clientIdentity, client);
1006 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1009 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1011 "Found address `%s' for incoming connection %p\n",
1012 GNUNET_a2s (vaddr, alen),
1015 session->connect_addr = vaddr;
1016 session->connect_alen = alen;
1021 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1023 "Did not obtain TCP socket address for incoming connection\n");
1027 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1029 "Creating new session %p for connection %p\n",
1032 process_pending_messages (session);
1034 if (session->expecting_welcome != GNUNET_YES)
1036 GNUNET_break_op (0);
1037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1040 session->expecting_welcome = GNUNET_NO;
1041 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1046 * Calculate how long we should delay reading from the TCP socket to
1047 * ensure that we stay within our bandwidth limits (push back).
1049 * @param session for which client should this be calculated
1051 static struct GNUNET_TIME_Relative
1052 calculate_throttle_delay (struct Session *session)
1054 struct GNUNET_TIME_Relative ret;
1055 struct GNUNET_TIME_Absolute now;
1060 now = GNUNET_TIME_absolute_get ();
1061 del = now.value - session->last_quota_update.value;
1062 if (del > MAX_BANDWIDTH_CARRY)
1064 update_quota (session, GNUNET_YES);
1065 del = now.value - session->last_quota_update.value;
1066 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1068 if (session->quota_in == 0)
1069 session->quota_in = 1; /* avoid divison by zero */
1070 avail = del * session->quota_in;
1071 if (avail > session->last_received)
1072 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1073 excess = session->last_received - avail;
1074 ret.value = excess / session->quota_in;
1080 * Task to signal the server that we can continue
1081 * receiving from the TCP client now.
1084 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1086 struct Session *session = cls;
1087 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1092 * We've received data for this peer via TCP. Unbox,
1093 * compute latency and forward.
1095 * @param cls closure
1096 * @param client identification of the client
1097 * @param message the actual message
1100 handle_tcp_data (void *cls,
1101 struct GNUNET_SERVER_Client *client,
1102 const struct GNUNET_MessageHeader *message)
1104 struct Plugin *plugin = cls;
1105 struct Session *session;
1107 struct GNUNET_TIME_Relative delay;
1109 msize = ntohs (message->size);
1110 session = find_session_by_client (plugin, client);
1112 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
1114 /* We don't want to propagate WELCOME messages up! */
1115 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1118 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1120 GNUNET_break_op (0);
1121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1125 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1127 "Passing %u bytes of type %u from `%4s' to transport service.\n",
1128 (unsigned int) msize,
1129 (unsigned int) ntohs (message->type),
1130 GNUNET_i2s (&session->target));
1132 plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1133 session->connect_addr,
1134 session->connect_alen);
1135 /* update bandwidth used */
1136 session->last_received += msize;
1137 update_quota (session, GNUNET_NO);
1138 delay = calculate_throttle_delay (session);
1139 if (delay.value == 0)
1140 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1142 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1143 delay, &delayed_done, session);
1148 * Handlers for the various TCP messages.
1150 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1151 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1152 sizeof (struct WelcomeMessage)},
1153 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1159 * Functions with this signature are called whenever a peer
1160 * is disconnected on the network level.
1162 * @param cls closure
1163 * @param client identification of the client
1166 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1168 struct Plugin *plugin = cls;
1169 struct Session *session;
1173 session = find_session_by_client (plugin, client);
1174 if (session == NULL)
1175 return; /* unknown, nothing to do */
1177 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1179 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1180 GNUNET_i2s (&session->target),
1181 (session->connect_addr != NULL) ?
1182 GNUNET_a2s (session->connect_addr,
1183 session->connect_alen) : "*", client);
1185 disconnect_session (session);
1190 * Add the IP of our network interface to the list of
1191 * our external IP addresses.
1194 process_interfaces (void *cls,
1197 const struct sockaddr *addr, socklen_t addrlen)
1199 struct Plugin *plugin = cls;
1201 struct sockaddr_in *v4;
1202 struct sockaddr_in6 *v6;
1204 af = addr->sa_family;
1207 v4 = (struct sockaddr_in *) addr;
1208 v4->sin_port = htons (plugin->adv_port);
1212 GNUNET_assert (af == AF_INET6);
1213 v6 = (struct sockaddr_in6 *) addr;
1214 v6->sin6_port = htons (plugin->adv_port);
1216 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1217 GNUNET_ERROR_TYPE_BULK,
1218 "tcp", _("Found address `%s' (%s)\n"),
1219 GNUNET_a2s (addr, addrlen), name);
1220 plugin->env->notify_address (plugin->env->cls,
1222 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1228 * Function called by the resolver for each address obtained from DNS
1229 * for our own hostname. Add the addresses to the list of our
1230 * external IP addresses.
1232 * @param cls closure
1233 * @param addr one of the addresses of the host, NULL for the last address
1234 * @param addrlen length of the address
1237 process_hostname_ips (void *cls,
1238 const struct sockaddr *addr, socklen_t addrlen)
1240 struct Plugin *plugin = cls;
1244 plugin->hostname_dns = NULL;
1247 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1252 * Entry point for the plugin.
1255 libgnunet_plugin_transport_tcp_init (void *cls)
1257 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1258 struct GNUNET_TRANSPORT_PluginFunctions *api;
1259 struct Plugin *plugin;
1260 struct GNUNET_SERVICE_Context *service;
1261 unsigned long long aport;
1262 unsigned long long bport;
1265 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1266 if (service == NULL)
1268 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1271 ("Failed to start service for `%s' transport plugin.\n"),
1277 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1283 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1286 &aport)) && (aport > 65535)))
1288 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1291 ("Require valid port number for service `%s' in configuration!\n"),
1293 GNUNET_SERVICE_stop (service);
1298 plugin = GNUNET_malloc (sizeof (struct Plugin));
1299 plugin->open_port = bport;
1300 plugin->adv_port = aport;
1302 plugin->lsock = NULL;
1303 plugin->statistics = NULL;
1304 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1306 api->send = &tcp_plugin_send;
1307 api->disconnect = &tcp_plugin_disconnect;
1308 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1309 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1310 api->check_address = &tcp_plugin_check_address;
1311 plugin->service = service;
1312 plugin->server = GNUNET_SERVICE_get_server (service);
1313 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1314 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1317 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1319 plugin->handlers[i].callback_cls = plugin;
1320 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1322 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1323 "tcp", _("TCP transport listening on port %llu\n"), bport);
1325 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1328 ("TCP transport advertises itself as being on port %llu\n"),
1330 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1332 /* FIXME: do the two calls below periodically again and
1333 not just once (since the info we get might change...) */
1334 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1335 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1338 HOSTNAME_RESOLVE_TIMEOUT,
1339 &process_hostname_ips,
1346 * Exit point from the plugin.
1349 libgnunet_plugin_transport_tcp_done (void *cls)
1351 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1352 struct Plugin *plugin = api->cls;
1353 struct Session *session;
1355 while (NULL != (session = plugin->sessions))
1356 disconnect_session (session);
1357 if (NULL != plugin->hostname_dns)
1359 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1360 plugin->hostname_dns = NULL;
1362 GNUNET_SERVICE_stop (plugin->service);
1363 GNUNET_free (plugin->handlers);
1364 GNUNET_free (plugin);
1369 /* end of plugin_transport_tcp.c */