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_os_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "plugin_transport.h"
39 #include "transport.h"
41 #define DEBUG_TCP GNUNET_NO
44 * How long until we give up on transmitting the welcome message?
46 #define WELCOME_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
49 * How long until we give up on transmitting the welcome message?
51 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
55 * Initial handshake message for a session.
59 struct GNUNET_MessageHeader header;
62 * Identity of the node connecting (TCP client)
64 struct GNUNET_PeerIdentity clientIdentity;
70 * Encapsulation of all of the state of the plugin.
76 * Information kept for each message that is yet to
83 * This is a linked list.
85 struct PendingMessage *next;
88 * The pending message, pointer to the end
89 * of this struct, do not free!
91 const struct GNUNET_MessageHeader *msg;
94 * Continuation function to call once the message
95 * has been sent. Can be NULL if there is no
96 * continuation to call.
98 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
101 * Closure for transmit_cont.
103 void *transmit_cont_cls;
106 * Timeout value for the pending message.
108 struct GNUNET_TIME_Absolute timeout;
114 * Session handle for TCP connections.
120 * Stored in a linked list.
122 struct Session *next;
125 * Pointer to the global plugin struct.
127 struct Plugin *plugin;
130 * The client (used to identify this connection)
132 struct GNUNET_SERVER_Client *client;
135 * Messages currently pending for transmission
136 * to this peer, if any.
138 struct PendingMessage *pending_messages;
141 * Handle for pending transmission request.
143 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
146 * To whom are we talking to (set to our identity
147 * if we are still waiting for the welcome message)
149 struct GNUNET_PeerIdentity target;
152 * At what time did we reset last_received last?
154 struct GNUNET_TIME_Absolute last_quota_update;
157 * Address of the other peer (either based on our 'connect'
158 * call or on our 'accept' call).
163 * How many bytes have we received since the "last_quota_update"
166 uint64_t last_received;
169 * Number of bytes per ms that this peer is allowed
175 * Length of connect_addr.
180 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
181 * GNUNET_SYSERR is used to mark non-welcoming connections (HELLO
184 int expecting_welcome;
190 * Encapsulation of all of the state of the plugin.
197 struct GNUNET_TRANSPORT_PluginEnvironment *env;
202 struct GNUNET_CONNECTION_Handle *lsock;
205 * List of open TCP sessions.
207 struct Session *sessions;
210 * Handle for the statistics service.
212 struct GNUNET_STATISTICS_Handle *statistics;
215 * Handle to the network service.
217 struct GNUNET_SERVICE_Context *service;
220 * Handle to the server for this service.
222 struct GNUNET_SERVER_Handle *server;
225 * Copy of the handler array where the closures are
226 * set to this struct's instance.
228 struct GNUNET_SERVER_MessageHandler *handlers;
231 * Handle for request of hostname resolution, non-NULL if pending.
233 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
236 * ID of task used to update our addresses when one expires.
238 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
241 * Port that we are actually listening on.
246 * Port that the user said we would have visible to the
255 * Find the session handle for the given peer.
257 static struct Session *
258 find_session_by_target (struct Plugin *plugin,
259 const struct GNUNET_PeerIdentity *target)
263 ret = plugin->sessions;
264 while ( (ret != NULL) &&
265 ((GNUNET_SYSERR == ret->expecting_welcome) ||
266 (0 != memcmp (target,
267 &ret->target, sizeof (struct GNUNET_PeerIdentity)))))
274 * Find the session handle for the given peer.
276 static struct Session *
277 find_session_by_client (struct Plugin *plugin,
278 const struct GNUNET_SERVER_Client *client)
282 ret = plugin->sessions;
283 while ((ret != NULL) && (client != ret->client))
290 * Create a welcome message.
292 static struct PendingMessage *
293 create_welcome (struct Plugin *plugin)
295 struct PendingMessage *pm;
296 struct WelcomeMessage *welcome;
298 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
299 sizeof (struct WelcomeMessage));
300 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
301 welcome = (struct WelcomeMessage *) &pm[1];
302 welcome->header.size = htons (sizeof (struct WelcomeMessage));
303 welcome->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
304 welcome->clientIdentity = *plugin->env->my_identity;
305 pm->timeout = GNUNET_TIME_relative_to_absolute (WELCOME_TIMEOUT);
311 * Create a new session.
314 * @param target peer to connect to
315 * @param client client to use
316 * @return new session object
318 static struct Session *
319 create_session (struct Plugin *plugin,
320 const struct GNUNET_PeerIdentity *target,
321 struct GNUNET_SERVER_Client *client)
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 ret->pending_messages = create_welcome (plugin);
340 * If we have pending messages, ask the server to
341 * transmit them (schedule the respective tasks, etc.)
343 * @param session for which session should we do this
345 static void process_pending_messages (struct Session *session);
349 * Function called to notify a client about the socket
350 * being ready to queue more data. "buf" will be
351 * NULL and "size" zero if the socket was closed for
352 * writing in the meantime.
355 * @param size number of bytes available in buf
356 * @param buf where the callee should write the message
357 * @return number of bytes written to buf
360 do_transmit (void *cls, size_t size, void *buf)
362 struct Session *session = cls;
363 struct PendingMessage *pm;
368 session->transmit_handle = NULL;
372 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
374 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
375 GNUNET_i2s (&session->target));
378 while (NULL != (pm = session->pending_messages))
380 session->pending_messages = pm->next;
382 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
384 "Failed to transmit message of type %u to `%4s'.\n",
385 ntohs (pm->msg->type),
386 GNUNET_i2s (&session->target));
388 if (pm->transmit_cont != NULL)
389 pm->transmit_cont (pm->transmit_cont_cls,
390 &session->target, GNUNET_SYSERR);
397 while (NULL != (pm = session->pending_messages))
399 if (size < (msize = ntohs (pm->msg->size)))
401 memcpy (cbuf, pm->msg, msize);
405 session->pending_messages = pm->next;
406 if (pm->transmit_cont != NULL)
407 pm->transmit_cont (pm->transmit_cont_cls,
408 &session->target, GNUNET_OK);
411 process_pending_messages (session);
413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
414 "tcp", "Transmitting %u bytes\n", ret);
421 * If we have pending messages, ask the server to
422 * transmit them (schedule the respective tasks, etc.)
424 * @param session for which session should we do this
427 process_pending_messages (struct Session *session)
429 struct PendingMessage *pm;
430 GNUNET_assert (session->client != NULL);
431 if (session->transmit_handle != NULL)
433 if (NULL == (pm = session->pending_messages))
435 session->transmit_handle
436 = GNUNET_SERVER_notify_transmit_ready (session->client,
437 ntohs (pm->msg->size),
438 GNUNET_TIME_absolute_get_remaining
440 &do_transmit, session);
445 * Functions with this signature are called whenever we need
446 * to close a session due to a disconnect or failure to
447 * establish a connection.
449 * @param session session to close down
452 disconnect_session (struct Session *session)
454 struct Session *prev;
456 struct PendingMessage *pm;
459 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
461 "Disconnecting from `%4s' at %s (session %p).\n",
462 GNUNET_i2s (&session->target),
463 (session->connect_addr != NULL) ?
464 GNUNET_a2s (session->connect_addr,
465 session->connect_alen) : "*", session);
467 /* remove from session list */
469 pos = session->plugin->sessions;
470 while (pos != session)
476 session->plugin->sessions = session->next;
478 prev->next = session->next;
480 if (session->transmit_handle != NULL)
482 GNUNET_CONNECTION_notify_transmit_ready_cancel
483 (session->transmit_handle);
484 session->transmit_handle = NULL;
486 while (NULL != (pm = session->pending_messages))
489 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
491 pm->transmit_cont != NULL
492 ? "Could not deliver message of type %u to `%4s'.\n"
494 "Could not deliver message of type %u to `%4s', notifying.\n",
495 ntohs (pm->msg->type), GNUNET_i2s (&session->target));
497 session->pending_messages = pm->next;
498 if (NULL != pm->transmit_cont)
499 pm->transmit_cont (pm->transmit_cont_cls,
500 &session->target, GNUNET_SYSERR);
503 if (GNUNET_NO == session->expecting_welcome)
506 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
508 "Notifying transport service about loss of data connection with `%4s'.\n",
509 GNUNET_i2s (&session->target));
511 /* Data session that actually went past the
512 initial handshake; transport service may
513 know about this one, so we need to
514 notify transport service about disconnect */
515 session->plugin->env->receive (session->plugin->env->cls,
517 session->connect_addr,
518 session->connect_alen,
519 &session->target, NULL);
521 if (session->client != NULL)
523 GNUNET_SERVER_client_drop (session->client);
524 session->client = NULL;
526 GNUNET_free_non_null (session->connect_addr);
527 GNUNET_free (session);
532 * Function that can be used by the transport service to transmit
533 * a message using the plugin. Note that in the case of a
534 * peer disconnecting, the continuation MUST be called
535 * prior to the disconnect notification itself. This function
536 * will be called with this peer's HELLO message to initiate
537 * a fresh connection to another peer.
540 * @param target who should receive this message
541 * @param msg the message to transmit
542 * @param priority how important is the message (most plugins will
543 * ignore message priority and just FIFO)
544 * @param timeout how long to wait at most for the transmission (does not
545 * require plugins to discard the message after the timeout,
546 * just advisory for the desired delay; most plugins will ignore
548 * @param addr the address to use (can be NULL if the plugin
549 * is "on its own" (i.e. re-use existing TCP connection))
550 * @param addrlen length of the address in bytes
551 * @param force_address GNUNET_YES if the plugin MUST use the given address,
552 * otherwise the plugin may use other addresses or
553 * existing connections (if available)
554 * @param cont continuation to call once the message has
555 * been transmitted (or if the transport is ready
556 * for the next transmission call; or if the
557 * peer disconnected...); can be NULL
558 * @param cont_cls closure for cont
559 * @return number of bytes used (on the physical network, with overheads);
560 * -1 on hard errors (i.e. address invalid); 0 is a legal value
561 * and does NOT mean that the message was not transmitted (DV)
564 tcp_plugin_send (void *cls,
565 const struct GNUNET_PeerIdentity *target,
566 const struct GNUNET_MessageHeader *msg,
567 unsigned int priority,
568 struct GNUNET_TIME_Relative timeout,
572 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
574 struct Plugin *plugin = cls;
575 struct Session *session;
576 struct PendingMessage *pm;
577 struct PendingMessage *pme;
578 struct GNUNET_CONNECTION_Handle *sa;
582 mlen = ntohs (msg->size);
583 session = find_session_by_target (plugin, target);
584 if ( (GNUNET_YES == force_address) &&
585 ( (session->connect_alen != addrlen) ||
586 (0 != memcmp (session->connect_addr,
589 session = NULL; /* ignore existing session */
590 if ( (session == NULL) &&
594 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
596 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
597 GNUNET_i2s (target));
603 if (sizeof (struct sockaddr_in) == addrlen)
605 else if (sizeof (struct sockaddr_in6) == addrlen)
612 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
614 GNUNET_SERVER_MAX_MESSAGE_SIZE);
618 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
620 "Failed to create connection to `%4s' at `%s'\n",
622 GNUNET_a2s (addr, addrlen));
628 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
630 "Asked to transmit to `%4s', creating fresh session.\n",
631 GNUNET_i2s (target));
633 session = create_session (plugin,
635 GNUNET_SERVER_connect_socket (session->plugin->server,
637 session->connect_addr = GNUNET_malloc (addrlen);
638 memcpy (session->connect_addr,
641 session->connect_alen = addrlen;
643 GNUNET_assert (session != NULL);
644 GNUNET_assert (session->client != NULL);
646 /* create new message entry */
647 pm = GNUNET_malloc (mlen + sizeof (struct PendingMessage));
648 memcpy (&pm[1], msg, mlen);
649 pm->msg = (const struct GNUNET_MessageHeader*) &pm[1];
650 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
651 pm->transmit_cont = cont;
652 pm->transmit_cont_cls = cont_cls;
654 /* append pm to pending_messages list */
655 pme = session->pending_messages;
658 session->pending_messages = pm;
662 /* FIXME: this could be done faster by keeping
663 track of the tail of the list... */
664 while (NULL != pme->next)
669 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
671 "Asked to transmit %u bytes to `%s', added message to list.\n",
673 GNUNET_i2s (target));
675 process_pending_messages (session);
681 * Function that can be called to force a disconnect from the
682 * specified neighbour. This should also cancel all previously
683 * scheduled transmissions. Obviously the transmission may have been
684 * partially completed already, which is OK. The plugin is supposed
685 * to close the connection (if applicable) and no longer call the
686 * transmit continuation(s).
688 * Finally, plugin MUST NOT call the services's receive function to
689 * notify the service that the connection to the specified target was
690 * closed after a getting this call.
693 * @param target peer for which the last transmission is
697 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
699 struct Plugin *plugin = cls;
700 struct Session *session;
701 struct PendingMessage *pm;
704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
706 "Asked to cancel session with `%4s'\n",
707 GNUNET_i2s (target));
709 while (NULL != (session = find_session_by_target (plugin, target)))
711 pm = session->pending_messages;
714 pm->transmit_cont = NULL;
715 pm->transmit_cont_cls = NULL;
718 if (session->client != NULL)
720 GNUNET_SERVER_client_drop (session->client);
721 session->client = NULL;
723 /* rest of the clean-up of the session will be done as part of
724 disconnect_notify which should be triggered any time now
725 (or which may be triggering this call in the first place) */
730 struct PrettyPrinterContext
732 GNUNET_TRANSPORT_AddressStringCallback asc;
739 * Append our port and forward the result.
742 append_port (void *cls, const char *hostname)
744 struct PrettyPrinterContext *ppc = cls;
747 if (hostname == NULL)
749 ppc->asc (ppc->asc_cls, NULL);
753 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
754 ppc->asc (ppc->asc_cls, ret);
760 * Convert the transports address to a nice, human-readable
764 * @param type name of the transport that generated the address
765 * @param addr one of the addresses of the host, NULL for the last address
766 * the specific address format depends on the transport
767 * @param addrlen length of the address
768 * @param numeric should (IP) addresses be displayed in numeric form?
769 * @param timeout after how long should we give up?
770 * @param asc function to call on each string
771 * @param asc_cls closure for asc
774 tcp_plugin_address_pretty_printer (void *cls,
779 struct GNUNET_TIME_Relative timeout,
780 GNUNET_TRANSPORT_AddressStringCallback asc,
783 struct Plugin *plugin = cls;
784 const struct sockaddr_in *v4;
785 const struct sockaddr_in6 *v6;
786 struct PrettyPrinterContext *ppc;
788 if ((addrlen != sizeof (struct sockaddr_in)) &&
789 (addrlen != sizeof (struct sockaddr_in6)))
791 /* invalid address */
796 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
798 ppc->asc_cls = asc_cls;
799 if (addrlen == sizeof (struct sockaddr_in))
801 v4 = (const struct sockaddr_in *) addr;
802 ppc->port = ntohs (v4->sin_port);
806 v6 = (const struct sockaddr_in6 *) addr;
807 ppc->port = ntohs (v6->sin6_port);
810 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
814 !numeric, timeout, &append_port, ppc);
819 * Update the last-received and bandwidth quota values
822 * @param session session to update
823 * @param force set to GNUNET_YES if we should update even
824 * though the minimum refresh time has not yet expired
827 update_quota (struct Session *session, int force)
829 struct GNUNET_TIME_Absolute now;
830 unsigned long long delta;
831 unsigned long long total_allowed;
832 unsigned long long total_remaining;
834 now = GNUNET_TIME_absolute_get ();
835 delta = now.value - session->last_quota_update.value;
836 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
837 return; /* too early, not enough data */
839 total_allowed = session->quota_in * delta;
840 if (total_allowed > session->last_received)
842 /* got less than acceptable */
843 total_remaining = total_allowed - session->last_received;
844 session->last_received = 0;
845 delta = total_remaining / session->quota_in; /* bonus seconds */
846 if (delta > MAX_BANDWIDTH_CARRY)
847 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
851 /* got more than acceptable */
852 session->last_received -= total_allowed;
855 session->last_quota_update.value = now.value - delta;
860 * Set a quota for receiving data from the given peer; this is a
861 * per-transport limit. The transport should limit its read/select
862 * calls to stay below the quota (in terms of incoming data).
865 * @param target the peer for whom the quota is given
866 * @param quota_in quota for receiving/sending data in bytes per ms
869 tcp_plugin_set_receive_quota (void *cls,
870 const struct GNUNET_PeerIdentity *target,
873 struct Plugin *plugin = cls;
874 struct Session *session;
876 session = find_session_by_target (plugin, target);
878 return; /* peer must have disconnected, ignore */
879 if (session->quota_in != quota_in)
881 update_quota (session, GNUNET_YES);
882 if (session->quota_in > quota_in)
883 session->last_quota_update = GNUNET_TIME_absolute_get ();
884 session->quota_in = quota_in;
890 * Check if the given port is plausible (must be either
891 * our listen port or our advertised port). If it is
892 * neither, we return one of these two ports at random.
894 * @return either in_port or a more plausible port
897 check_port (struct Plugin *plugin, uint16_t in_port)
899 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
901 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
903 ? plugin->open_port : plugin->adv_port;
908 * Another peer has suggested an address for this peer and transport
909 * plugin. Check that this could be a valid address.
912 * @param addr pointer to the address
913 * @param addrlen length of addr
914 * @return GNUNET_OK if this is a plausible address for this peer
918 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
920 struct Plugin *plugin = cls;
921 char buf[sizeof (struct sockaddr_in6)];
922 struct sockaddr_in *v4;
923 struct sockaddr_in6 *v6;
925 if ((addrlen != sizeof (struct sockaddr_in)) &&
926 (addrlen != sizeof (struct sockaddr_in6)))
929 return GNUNET_SYSERR;
931 memcpy (buf, addr, sizeof (struct sockaddr_in6));
932 if (addrlen == sizeof (struct sockaddr_in))
934 v4 = (struct sockaddr_in *) buf;
935 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
939 v6 = (struct sockaddr_in6 *) buf;
940 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
943 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
945 "Informing transport service about my address `%s'.\n",
946 GNUNET_a2s (addr, addrlen));
953 * We've received a welcome from this peer via TCP. Possibly create a
954 * fresh client record and send back our welcome.
957 * @param client identification of the client
958 * @param message the actual message
961 handle_tcp_welcome (void *cls,
962 struct GNUNET_SERVER_Client *client,
963 const struct GNUNET_MessageHeader *message)
965 struct Plugin *plugin = cls;
966 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
967 struct Session *session;
972 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
974 "Received `%s' message from `%4s/%p'.\n", "WELCOME",
975 GNUNET_i2s (&wm->clientIdentity), client);
977 session = find_session_by_client (plugin, client);
980 GNUNET_SERVER_client_keep (client);
981 session = create_session (plugin,
982 &wm->clientIdentity, client);
984 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
986 session->connect_addr = vaddr;
987 session->connect_alen = alen;
990 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
992 "Creating new session %p for incoming `%s' message.\n",
993 session_c, "WELCOME");
995 process_pending_messages (session);
997 if (session->expecting_welcome != GNUNET_YES)
1000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1003 session->expecting_welcome = GNUNET_NO;
1004 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1009 * Calculate how long we should delay reading from the TCP socket to
1010 * ensure that we stay within our bandwidth limits (push back).
1012 * @param session for which client should this be calculated
1014 static struct GNUNET_TIME_Relative
1015 calculate_throttle_delay (struct Session *session)
1017 struct GNUNET_TIME_Relative ret;
1018 struct GNUNET_TIME_Absolute now;
1023 now = GNUNET_TIME_absolute_get ();
1024 del = now.value - session->last_quota_update.value;
1025 if (del > MAX_BANDWIDTH_CARRY)
1027 update_quota (session, GNUNET_YES);
1028 del = now.value - session->last_quota_update.value;
1029 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1031 if (session->quota_in == 0)
1032 session->quota_in = 1; /* avoid divison by zero */
1033 avail = del * session->quota_in;
1034 if (avail > session->last_received)
1035 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1036 excess = session->last_received - avail;
1037 ret.value = excess / session->quota_in;
1043 * Task to signal the server that we can continue
1044 * receiving from the TCP client now.
1047 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1049 struct Session *session = cls;
1050 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1055 * We've received data for this peer via TCP. Unbox,
1056 * compute latency and forward.
1058 * @param cls closure
1059 * @param client identification of the client
1060 * @param message the actual message
1063 handle_tcp_data (void *cls,
1064 struct GNUNET_SERVER_Client *client,
1065 const struct GNUNET_MessageHeader *message)
1067 struct Plugin *plugin = cls;
1068 struct Session *session;
1070 struct GNUNET_TIME_Relative delay;
1072 msize = ntohs (message->size);
1073 session = find_session_by_client (plugin, client);
1074 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1076 GNUNET_break_op (0);
1077 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1081 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1082 "tcp", "Receiving %u bytes from `%4s'.\n",
1083 msize, GNUNET_i2s (&session->target));
1086 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1088 "Forwarding %u bytes of data of type %u to transport service.\n",
1089 (unsigned int) msize,
1090 (unsigned int) ntohs (msg->type));
1092 plugin->env->receive (plugin->env->cls, 1,
1093 session->connect_addr,
1094 session->connect_alen,
1095 &session->target, message);
1096 /* update bandwidth used */
1097 session->last_received += msize;
1098 update_quota (session, GNUNET_NO);
1099 delay = calculate_throttle_delay (session);
1100 if (delay.value == 0)
1101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1103 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1104 delay, &delayed_done, session);
1109 * Handlers for the various TCP messages.
1111 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1112 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1113 sizeof (struct WelcomeMessage)},
1114 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA, 0},
1120 create_tcp_handlers (struct Plugin *plugin)
1123 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1124 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1127 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1129 plugin->handlers[i].callback_cls = plugin;
1130 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1135 * Functions with this signature are called whenever a peer
1136 * is disconnected on the network level.
1138 * @param cls closure
1139 * @param client identification of the client
1142 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1144 struct Plugin *plugin = cls;
1145 struct Session *session;
1147 session = find_session_by_client (plugin, client);
1148 if (session == NULL)
1149 return; /* unknown, nothing to do */
1151 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1153 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1154 GNUNET_i2s (&session->target),
1155 (session->connect_addr != NULL) ?
1156 GNUNET_a2s (session->connect_addr,
1157 session->connect_alen) : "*", client);
1159 disconnect_session (session);
1164 * Add the IP of our network interface to the list of
1165 * our external IP addresses.
1168 process_interfaces (void *cls,
1171 const struct sockaddr *addr, socklen_t addrlen)
1173 struct Plugin *plugin = cls;
1175 struct sockaddr_in *v4;
1176 struct sockaddr_in6 *v6;
1178 af = addr->sa_family;
1181 v4 = (struct sockaddr_in *) addr;
1182 v4->sin_port = htons (plugin->adv_port);
1186 GNUNET_assert (af == AF_INET6);
1187 v6 = (struct sockaddr_in6 *) addr;
1188 v6->sin6_port = htons (plugin->adv_port);
1190 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1191 GNUNET_ERROR_TYPE_BULK,
1192 "tcp", _("Found address `%s' (%s)\n"),
1193 GNUNET_a2s (addr, addrlen), name);
1194 plugin->env->notify_address (plugin->env->cls,
1196 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1202 * Function called by the resolver for each address obtained from DNS
1203 * for our own hostname. Add the addresses to the list of our
1204 * external IP addresses.
1206 * @param cls closure
1207 * @param addr one of the addresses of the host, NULL for the last address
1208 * @param addrlen length of the address
1211 process_hostname_ips (void *cls,
1212 const struct sockaddr *addr, socklen_t addrlen)
1214 struct Plugin *plugin = cls;
1218 plugin->hostname_dns = NULL;
1221 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1226 * Entry point for the plugin.
1229 libgnunet_plugin_transport_tcp_init (void *cls)
1231 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1232 struct GNUNET_TRANSPORT_PluginFunctions *api;
1233 struct Plugin *plugin;
1234 struct GNUNET_SERVICE_Context *service;
1235 unsigned long long aport;
1236 unsigned long long bport;
1238 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1239 if (service == NULL)
1241 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1244 ("Failed to start service for `%s' transport plugin.\n"),
1250 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1256 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1259 &aport)) && (aport > 65535)))
1261 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1264 ("Require valid port number for service `%s' in configuration!\n"),
1266 GNUNET_SERVICE_stop (service);
1271 plugin = GNUNET_malloc (sizeof (struct Plugin));
1272 plugin->open_port = bport;
1273 plugin->adv_port = aport;
1275 plugin->lsock = NULL;
1276 plugin->statistics = NULL;
1277 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1279 api->send = &tcp_plugin_send;
1280 api->disconnect = &tcp_plugin_disconnect;
1281 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1282 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1283 api->check_address = &tcp_plugin_check_address;
1284 plugin->service = service;
1285 plugin->server = GNUNET_SERVICE_get_server (service);
1286 create_tcp_handlers (plugin);
1287 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1288 "tcp", _("TCP transport listening on port %llu\n"), bport);
1290 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1293 ("TCP transport advertises itself as being on port %llu\n"),
1295 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1297 /* FIXME: do the two calls below periodically again and
1298 not just once (since the info we get might change...) */
1299 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1300 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1303 HOSTNAME_RESOLVE_TIMEOUT,
1304 &process_hostname_ips,
1311 * Exit point from the plugin.
1314 libgnunet_plugin_transport_tcp_done (void *cls)
1316 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1317 struct Plugin *plugin = api->cls;
1318 struct Session *session;
1320 while (NULL != (session = plugin->sessions))
1321 disconnect_session (session);
1322 if (NULL != plugin->hostname_dns)
1324 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1325 plugin->hostname_dns = NULL;
1327 GNUNET_SERVICE_stop (plugin->service);
1328 GNUNET_free (plugin->handlers);
1329 GNUNET_free (plugin);
1334 /* end of plugin_transport_tcp.c */