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_YES
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.
60 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
62 struct GNUNET_MessageHeader header;
65 * Identity of the node connecting (TCP client)
67 struct GNUNET_PeerIdentity clientIdentity;
73 * Encapsulation of all of the state of the plugin.
79 * Information kept for each message that is yet to
86 * This is a linked list.
88 struct PendingMessage *next;
91 * The pending message, pointer to the end
92 * of this struct, do not free!
94 const struct GNUNET_MessageHeader *msg;
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;
117 * Session handle for TCP connections.
123 * Stored in a linked list.
125 struct Session *next;
128 * Pointer to the global plugin struct.
130 struct Plugin *plugin;
133 * The client (used to identify this connection)
135 struct GNUNET_SERVER_Client *client;
138 * Messages currently pending for transmission
139 * to this peer, if any.
141 struct PendingMessage *pending_messages;
144 * Handle for pending transmission request.
146 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
149 * To whom are we talking to (set to our identity
150 * if we are still waiting for the welcome message)
152 struct GNUNET_PeerIdentity target;
155 * At what time did we reset last_received last?
157 struct GNUNET_TIME_Absolute last_quota_update;
160 * Address of the other peer (either based on our 'connect'
161 * call or on our 'accept' call).
166 * How many bytes have we received since the "last_quota_update"
169 uint64_t last_received;
172 * Number of bytes per ms that this peer is allowed
178 * Length of connect_addr.
183 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
184 * GNUNET_SYSERR is used to mark non-welcoming connections (HELLO
187 int expecting_welcome;
193 * Encapsulation of all of the state of the plugin.
200 struct GNUNET_TRANSPORT_PluginEnvironment *env;
205 struct GNUNET_CONNECTION_Handle *lsock;
208 * List of open TCP sessions.
210 struct Session *sessions;
213 * Handle for the statistics service.
215 struct GNUNET_STATISTICS_Handle *statistics;
218 * Handle to the network service.
220 struct GNUNET_SERVICE_Context *service;
223 * Handle to the server for this service.
225 struct GNUNET_SERVER_Handle *server;
228 * Copy of the handler array where the closures are
229 * set to this struct's instance.
231 struct GNUNET_SERVER_MessageHandler *handlers;
234 * Handle for request of hostname resolution, non-NULL if pending.
236 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
239 * ID of task used to update our addresses when one expires.
241 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
244 * Port that we are actually listening on.
249 * Port that the user said we would have visible to the
258 * Find the session handle for the given peer.
260 static struct Session *
261 find_session_by_target (struct Plugin *plugin,
262 const struct GNUNET_PeerIdentity *target)
266 ret = plugin->sessions;
267 while ( (ret != NULL) &&
268 ((GNUNET_SYSERR == ret->expecting_welcome) ||
269 (0 != memcmp (target,
270 &ret->target, sizeof (struct GNUNET_PeerIdentity)))))
277 * Find the session handle for the given peer.
279 static struct Session *
280 find_session_by_client (struct Plugin *plugin,
281 const struct GNUNET_SERVER_Client *client)
285 ret = plugin->sessions;
286 while ((ret != NULL) && (client != ret->client))
293 * Create a welcome message.
295 static struct PendingMessage *
296 create_welcome (struct Plugin *plugin)
298 struct PendingMessage *pm;
299 struct WelcomeMessage *welcome;
301 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
302 sizeof (struct WelcomeMessage));
303 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
304 welcome = (struct WelcomeMessage *) &pm[1];
305 welcome->header.size = htons (sizeof (struct WelcomeMessage));
306 welcome->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
307 welcome->clientIdentity = *plugin->env->my_identity;
308 pm->timeout = GNUNET_TIME_relative_to_absolute (WELCOME_TIMEOUT);
314 * Create a new session.
317 * @param target peer to connect to
318 * @param client client to use
319 * @return new session object
321 static struct Session *
322 create_session (struct Plugin *plugin,
323 const struct GNUNET_PeerIdentity *target,
324 struct GNUNET_SERVER_Client *client)
328 ret = GNUNET_malloc (sizeof (struct Session));
329 ret->plugin = plugin;
330 ret->next = plugin->sessions;
331 plugin->sessions = ret;
332 ret->client = client;
333 ret->target = *target;
334 ret->last_quota_update = GNUNET_TIME_absolute_get ();
335 ret->quota_in = plugin->env->default_quota_in;
336 ret->expecting_welcome = GNUNET_YES;
337 ret->pending_messages = create_welcome (plugin);
343 * If we have pending messages, ask the server to
344 * transmit them (schedule the respective tasks, etc.)
346 * @param session for which session should we do this
348 static void process_pending_messages (struct Session *session);
352 * Function called to notify a client about the socket
353 * being ready to queue more data. "buf" will be
354 * NULL and "size" zero if the socket was closed for
355 * writing in the meantime.
358 * @param size number of bytes available in buf
359 * @param buf where the callee should write the message
360 * @return number of bytes written to buf
363 do_transmit (void *cls, size_t size, void *buf)
365 struct Session *session = cls;
366 struct PendingMessage *pm;
371 session->transmit_handle = NULL;
375 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
377 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
378 GNUNET_i2s (&session->target));
381 while (NULL != (pm = session->pending_messages))
383 session->pending_messages = pm->next;
385 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
387 "Failed to transmit message of type %u to `%4s'.\n",
388 ntohs (pm->msg->type),
389 GNUNET_i2s (&session->target));
391 if (pm->transmit_cont != NULL)
392 pm->transmit_cont (pm->transmit_cont_cls,
393 &session->target, GNUNET_SYSERR);
400 while (NULL != (pm = session->pending_messages))
402 if (size < (msize = ntohs (pm->msg->size)))
404 memcpy (cbuf, pm->msg, msize);
408 session->pending_messages = pm->next;
409 if (pm->transmit_cont != NULL)
410 pm->transmit_cont (pm->transmit_cont_cls,
411 &session->target, GNUNET_OK);
414 process_pending_messages (session);
416 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
417 "tcp", "Transmitting %u bytes\n", ret);
424 * If we have pending messages, ask the server to
425 * transmit them (schedule the respective tasks, etc.)
427 * @param session for which session should we do this
430 process_pending_messages (struct Session *session)
432 struct PendingMessage *pm;
433 GNUNET_assert (session->client != NULL);
434 if (session->transmit_handle != NULL)
436 if (NULL == (pm = session->pending_messages))
438 session->transmit_handle
439 = GNUNET_SERVER_notify_transmit_ready (session->client,
440 ntohs (pm->msg->size),
441 GNUNET_TIME_absolute_get_remaining
443 &do_transmit, session);
448 * Functions with this signature are called whenever we need
449 * to close a session due to a disconnect or failure to
450 * establish a connection.
452 * @param session session to close down
455 disconnect_session (struct Session *session)
457 struct Session *prev;
459 struct PendingMessage *pm;
462 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
464 "Disconnecting from `%4s' at %s (session %p).\n",
465 GNUNET_i2s (&session->target),
466 (session->connect_addr != NULL) ?
467 GNUNET_a2s (session->connect_addr,
468 session->connect_alen) : "*", session);
470 /* remove from session list */
472 pos = session->plugin->sessions;
473 while (pos != session)
479 session->plugin->sessions = session->next;
481 prev->next = session->next;
483 if (session->transmit_handle != NULL)
485 GNUNET_CONNECTION_notify_transmit_ready_cancel
486 (session->transmit_handle);
487 session->transmit_handle = NULL;
489 while (NULL != (pm = session->pending_messages))
492 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
494 pm->transmit_cont != NULL
495 ? "Could not deliver message of type %u to `%4s'.\n"
497 "Could not deliver message of type %u to `%4s', notifying.\n",
498 ntohs (pm->msg->type), GNUNET_i2s (&session->target));
500 session->pending_messages = pm->next;
501 if (NULL != pm->transmit_cont)
502 pm->transmit_cont (pm->transmit_cont_cls,
503 &session->target, GNUNET_SYSERR);
506 if (GNUNET_NO == session->expecting_welcome)
509 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
511 "Notifying transport service about loss of data connection with `%4s'.\n",
512 GNUNET_i2s (&session->target));
514 /* Data session that actually went past the
515 initial handshake; transport service may
516 know about this one, so we need to
517 notify transport service about disconnect */
518 session->plugin->env->receive (session->plugin->env->cls,
519 &session->target, NULL,
521 session->connect_addr,
522 session->connect_alen);
524 if (session->client != NULL)
526 GNUNET_SERVER_client_drop (session->client);
527 session->client = NULL;
529 GNUNET_free_non_null (session->connect_addr);
530 GNUNET_free (session);
535 * Function that can be used by the transport service to transmit
536 * a message using the plugin. Note that in the case of a
537 * peer disconnecting, the continuation MUST be called
538 * prior to the disconnect notification itself. This function
539 * will be called with this peer's HELLO message to initiate
540 * a fresh connection to another peer.
543 * @param target who should receive this message
544 * @param msg the message to transmit
545 * @param priority how important is the message (most plugins will
546 * ignore message priority and just FIFO)
547 * @param timeout how long to wait at most for the transmission (does not
548 * require plugins to discard the message after the timeout,
549 * just advisory for the desired delay; most plugins will ignore
551 * @param addr the address to use (can be NULL if the plugin
552 * is "on its own" (i.e. re-use existing TCP connection))
553 * @param addrlen length of the address in bytes
554 * @param force_address GNUNET_YES if the plugin MUST use the given address,
555 * otherwise the plugin may use other addresses or
556 * existing connections (if available)
557 * @param cont continuation to call once the message has
558 * been transmitted (or if the transport is ready
559 * for the next transmission call; or if the
560 * peer disconnected...); can be NULL
561 * @param cont_cls closure for cont
562 * @return number of bytes used (on the physical network, with overheads);
563 * -1 on hard errors (i.e. address invalid); 0 is a legal value
564 * and does NOT mean that the message was not transmitted (DV)
567 tcp_plugin_send (void *cls,
568 const struct GNUNET_PeerIdentity *target,
572 struct GNUNET_TIME_Relative timeout,
576 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
578 struct Plugin *plugin = cls;
579 struct Session *session;
580 struct PendingMessage *pm;
581 struct PendingMessage *pme;
582 struct GNUNET_CONNECTION_Handle *sa;
587 session = find_session_by_target (plugin, target);
588 if ( (session != NULL) && ((GNUNET_YES == force_address) &&
589 ( (session->connect_alen != addrlen) ||
590 (0 != memcmp (session->connect_addr,
593 session = NULL; /* ignore existing session */
595 if ( (session == NULL) &&
599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
601 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
602 GNUNET_i2s (target));
608 if (sizeof (struct sockaddr_in) == addrlen)
610 else if (sizeof (struct sockaddr_in6) == addrlen)
617 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
619 GNUNET_SERVER_MAX_MESSAGE_SIZE);
623 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
625 "Failed to create connection to `%4s' at `%s'\n",
627 GNUNET_a2s (addr, addrlen));
633 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
635 "Asked to transmit to `%4s', creating fresh session.\n",
636 GNUNET_i2s (target));
638 session = create_session (plugin,
640 GNUNET_SERVER_connect_socket (plugin->server,
642 session->connect_addr = GNUNET_malloc (addrlen);
643 memcpy (session->connect_addr,
646 session->connect_alen = addrlen;
648 GNUNET_assert (session != NULL);
649 GNUNET_assert (session->client != NULL);
651 /* create new message entry */
652 pm = GNUNET_malloc (mlen + sizeof (struct PendingMessage));
653 memcpy (&pm[1], msg, mlen);
654 pm->msg = (const struct GNUNET_MessageHeader*) &pm[1];
655 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
656 pm->transmit_cont = cont;
657 pm->transmit_cont_cls = cont_cls;
659 /* append pm to pending_messages list */
660 pme = session->pending_messages;
663 session->pending_messages = pm;
667 /* FIXME: this could be done faster by keeping
668 track of the tail of the list... */
669 while (NULL != pme->next)
674 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
676 "Asked to transmit %u bytes to `%s', added message to list.\n",
678 GNUNET_i2s (target));
680 process_pending_messages (session);
686 * Function that can be called to force a disconnect from the
687 * specified neighbour. This should also cancel all previously
688 * scheduled transmissions. Obviously the transmission may have been
689 * partially completed already, which is OK. The plugin is supposed
690 * to close the connection (if applicable) and no longer call the
691 * transmit continuation(s).
693 * Finally, plugin MUST NOT call the services's receive function to
694 * notify the service that the connection to the specified target was
695 * closed after a getting this call.
698 * @param target peer for which the last transmission is
702 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
704 struct Plugin *plugin = cls;
705 struct Session *session;
706 struct PendingMessage *pm;
709 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
711 "Asked to cancel session with `%4s'\n",
712 GNUNET_i2s (target));
714 while (NULL != (session = find_session_by_target (plugin, target)))
716 pm = session->pending_messages;
719 pm->transmit_cont = NULL;
720 pm->transmit_cont_cls = NULL;
723 if (session->client != NULL)
725 GNUNET_SERVER_client_drop (session->client);
726 session->client = NULL;
728 /* rest of the clean-up of the session will be done as part of
729 disconnect_notify which should be triggered any time now
730 (or which may be triggering this call in the first place) */
735 struct PrettyPrinterContext
737 GNUNET_TRANSPORT_AddressStringCallback asc;
744 * Append our port and forward the result.
747 append_port (void *cls, const char *hostname)
749 struct PrettyPrinterContext *ppc = cls;
752 if (hostname == NULL)
754 ppc->asc (ppc->asc_cls, NULL);
758 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
759 ppc->asc (ppc->asc_cls, ret);
765 * Convert the transports address to a nice, human-readable
769 * @param type name of the transport that generated the address
770 * @param addr one of the addresses of the host, NULL for the last address
771 * the specific address format depends on the transport
772 * @param addrlen length of the address
773 * @param numeric should (IP) addresses be displayed in numeric form?
774 * @param timeout after how long should we give up?
775 * @param asc function to call on each string
776 * @param asc_cls closure for asc
779 tcp_plugin_address_pretty_printer (void *cls,
784 struct GNUNET_TIME_Relative timeout,
785 GNUNET_TRANSPORT_AddressStringCallback asc,
788 struct Plugin *plugin = cls;
789 const struct sockaddr_in *v4;
790 const struct sockaddr_in6 *v6;
791 struct PrettyPrinterContext *ppc;
793 if ((addrlen != sizeof (struct sockaddr_in)) &&
794 (addrlen != sizeof (struct sockaddr_in6)))
796 /* invalid address */
801 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
803 ppc->asc_cls = asc_cls;
804 if (addrlen == sizeof (struct sockaddr_in))
806 v4 = (const struct sockaddr_in *) addr;
807 ppc->port = ntohs (v4->sin_port);
811 v6 = (const struct sockaddr_in6 *) addr;
812 ppc->port = ntohs (v6->sin6_port);
815 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
819 !numeric, timeout, &append_port, ppc);
824 * Update the last-received and bandwidth quota values
827 * @param session session to update
828 * @param force set to GNUNET_YES if we should update even
829 * though the minimum refresh time has not yet expired
832 update_quota (struct Session *session, int force)
834 struct GNUNET_TIME_Absolute now;
835 unsigned long long delta;
836 unsigned long long total_allowed;
837 unsigned long long total_remaining;
839 now = GNUNET_TIME_absolute_get ();
840 delta = now.value - session->last_quota_update.value;
841 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
842 return; /* too early, not enough data */
844 total_allowed = session->quota_in * delta;
845 if (total_allowed > session->last_received)
847 /* got less than acceptable */
848 total_remaining = total_allowed - session->last_received;
849 session->last_received = 0;
850 delta = total_remaining / session->quota_in; /* bonus seconds */
851 if (delta > MAX_BANDWIDTH_CARRY)
852 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
856 /* got more than acceptable */
857 session->last_received -= total_allowed;
860 session->last_quota_update.value = now.value - delta;
865 * Set a quota for receiving data from the given peer; this is a
866 * per-transport limit. The transport should limit its read/select
867 * calls to stay below the quota (in terms of incoming data).
870 * @param target the peer for whom the quota is given
871 * @param quota_in quota for receiving/sending data in bytes per ms
874 tcp_plugin_set_receive_quota (void *cls,
875 const struct GNUNET_PeerIdentity *target,
878 struct Plugin *plugin = cls;
879 struct Session *session;
881 session = find_session_by_target (plugin, target);
883 return; /* peer must have disconnected, ignore */
884 if (session->quota_in != quota_in)
886 update_quota (session, GNUNET_YES);
887 if (session->quota_in > quota_in)
888 session->last_quota_update = GNUNET_TIME_absolute_get ();
889 session->quota_in = quota_in;
895 * Check if the given port is plausible (must be either
896 * our listen port or our advertised port). If it is
897 * neither, we return one of these two ports at random.
899 * @return either in_port or a more plausible port
902 check_port (struct Plugin *plugin, uint16_t in_port)
904 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
906 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
908 ? plugin->open_port : plugin->adv_port;
913 * Another peer has suggested an address for this peer and transport
914 * plugin. Check that this could be a valid address.
917 * @param addr pointer to the address
918 * @param addrlen length of addr
919 * @return GNUNET_OK if this is a plausible address for this peer
923 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
925 struct Plugin *plugin = cls;
926 char buf[sizeof (struct sockaddr_in6)];
927 struct sockaddr_in *v4;
928 struct sockaddr_in6 *v6;
930 if ((addrlen != sizeof (struct sockaddr_in)) &&
931 (addrlen != sizeof (struct sockaddr_in6)))
934 return GNUNET_SYSERR;
936 memcpy (buf, addr, sizeof (struct sockaddr_in6));
937 if (addrlen == sizeof (struct sockaddr_in))
939 v4 = (struct sockaddr_in *) buf;
940 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
944 v6 = (struct sockaddr_in6 *) buf;
945 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
948 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
950 "Informing transport service about my address `%s'.\n",
951 GNUNET_a2s (addr, addrlen));
958 * We've received a welcome from this peer via TCP. Possibly create a
959 * fresh client record and send back our welcome.
962 * @param client identification of the client
963 * @param message the actual message
966 handle_tcp_welcome (void *cls,
967 struct GNUNET_SERVER_Client *client,
968 const struct GNUNET_MessageHeader *message)
970 struct Plugin *plugin = cls;
971 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
972 struct Session *session;
977 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
979 "Received `%s' message from a `%4s/%p'.\n", "WELCOME",
980 GNUNET_i2s (&wm->clientIdentity), client);
983 session = find_session_by_client (plugin, client);
986 GNUNET_SERVER_client_keep (client);
987 session = create_session (plugin,
988 &wm->clientIdentity, client);
990 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
993 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
995 "Found address for incoming `%s' message\n",
998 session->connect_addr = vaddr;
999 session->connect_alen = alen;
1004 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1006 "Didn't find address for incoming `%s' message\n",
1011 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1013 "Creating new session %p for incoming `%s' message.\n",
1014 session, "WELCOME");
1016 process_pending_messages (session);
1018 if (session->expecting_welcome != GNUNET_YES)
1020 GNUNET_break_op (0);
1021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1024 session->expecting_welcome = GNUNET_NO;
1025 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1030 * Calculate how long we should delay reading from the TCP socket to
1031 * ensure that we stay within our bandwidth limits (push back).
1033 * @param session for which client should this be calculated
1035 static struct GNUNET_TIME_Relative
1036 calculate_throttle_delay (struct Session *session)
1038 struct GNUNET_TIME_Relative ret;
1039 struct GNUNET_TIME_Absolute now;
1044 now = GNUNET_TIME_absolute_get ();
1045 del = now.value - session->last_quota_update.value;
1046 if (del > MAX_BANDWIDTH_CARRY)
1048 update_quota (session, GNUNET_YES);
1049 del = now.value - session->last_quota_update.value;
1050 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1052 if (session->quota_in == 0)
1053 session->quota_in = 1; /* avoid divison by zero */
1054 avail = del * session->quota_in;
1055 if (avail > session->last_received)
1056 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1057 excess = session->last_received - avail;
1058 ret.value = excess / session->quota_in;
1064 * Task to signal the server that we can continue
1065 * receiving from the TCP client now.
1068 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1070 struct Session *session = cls;
1071 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1076 * We've received data for this peer via TCP. Unbox,
1077 * compute latency and forward.
1079 * @param cls closure
1080 * @param client identification of the client
1081 * @param message the actual message
1084 handle_tcp_data (void *cls,
1085 struct GNUNET_SERVER_Client *client,
1086 const struct GNUNET_MessageHeader *message)
1088 struct Plugin *plugin = cls;
1089 struct Session *session;
1091 struct GNUNET_TIME_Relative delay;
1093 msize = ntohs (message->size);
1094 session = find_session_by_client (plugin, client);
1096 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
1099 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1100 "tcp", "Received a welcome, NOT sending to clients!\n");
1102 return; /* We don't want to propogate WELCOME messages up! */
1105 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1107 GNUNET_break_op (0);
1108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1112 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1113 "tcp", "Receiving %u bytes from `%4s'.\n",
1114 msize, GNUNET_i2s (&session->target));
1117 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1119 "Forwarding %u bytes of data of type %u to transport service.\n",
1120 (unsigned int) msize,
1121 (unsigned int) ntohs (message->type));
1123 plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1124 session->connect_addr,
1125 session->connect_alen);
1126 /* update bandwidth used */
1127 session->last_received += msize;
1128 update_quota (session, GNUNET_NO);
1129 delay = calculate_throttle_delay (session);
1130 if (delay.value == 0)
1131 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1133 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1134 delay, &delayed_done, session);
1139 * Handlers for the various TCP messages.
1141 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1142 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1143 sizeof (struct WelcomeMessage)},
1144 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1150 create_tcp_handlers (struct Plugin *plugin)
1153 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1154 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1157 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1159 plugin->handlers[i].callback_cls = plugin;
1160 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1165 * Functions with this signature are called whenever a peer
1166 * is disconnected on the network level.
1168 * @param cls closure
1169 * @param client identification of the client
1172 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1174 struct Plugin *plugin = cls;
1175 struct Session *session;
1179 session = find_session_by_client (plugin, client);
1180 if (session == NULL)
1181 return; /* unknown, nothing to do */
1183 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1185 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1186 GNUNET_i2s (&session->target),
1187 (session->connect_addr != NULL) ?
1188 GNUNET_a2s (session->connect_addr,
1189 session->connect_alen) : "*", client);
1191 disconnect_session (session);
1196 * Add the IP of our network interface to the list of
1197 * our external IP addresses.
1200 process_interfaces (void *cls,
1203 const struct sockaddr *addr, socklen_t addrlen)
1205 struct Plugin *plugin = cls;
1207 struct sockaddr_in *v4;
1208 struct sockaddr_in6 *v6;
1210 af = addr->sa_family;
1213 v4 = (struct sockaddr_in *) addr;
1214 v4->sin_port = htons (plugin->adv_port);
1218 GNUNET_assert (af == AF_INET6);
1219 v6 = (struct sockaddr_in6 *) addr;
1220 v6->sin6_port = htons (plugin->adv_port);
1222 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1223 GNUNET_ERROR_TYPE_BULK,
1224 "tcp", _("Found address `%s' (%s)\n"),
1225 GNUNET_a2s (addr, addrlen), name);
1226 plugin->env->notify_address (plugin->env->cls,
1228 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1234 * Function called by the resolver for each address obtained from DNS
1235 * for our own hostname. Add the addresses to the list of our
1236 * external IP addresses.
1238 * @param cls closure
1239 * @param addr one of the addresses of the host, NULL for the last address
1240 * @param addrlen length of the address
1243 process_hostname_ips (void *cls,
1244 const struct sockaddr *addr, socklen_t addrlen)
1246 struct Plugin *plugin = cls;
1250 plugin->hostname_dns = NULL;
1253 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1258 * Entry point for the plugin.
1261 libgnunet_plugin_transport_tcp_init (void *cls)
1263 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1264 struct GNUNET_TRANSPORT_PluginFunctions *api;
1265 struct Plugin *plugin;
1266 struct GNUNET_SERVICE_Context *service;
1267 unsigned long long aport;
1268 unsigned long long bport;
1270 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1271 if (service == NULL)
1273 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1276 ("Failed to start service for `%s' transport plugin.\n"),
1282 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1288 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1291 &aport)) && (aport > 65535)))
1293 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1296 ("Require valid port number for service `%s' in configuration!\n"),
1298 GNUNET_SERVICE_stop (service);
1303 plugin = GNUNET_malloc (sizeof (struct Plugin));
1304 plugin->open_port = bport;
1305 plugin->adv_port = aport;
1307 plugin->lsock = NULL;
1308 plugin->statistics = NULL;
1309 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1311 api->send = &tcp_plugin_send;
1312 api->disconnect = &tcp_plugin_disconnect;
1313 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1314 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1315 api->check_address = &tcp_plugin_check_address;
1316 plugin->service = service;
1317 plugin->server = GNUNET_SERVICE_get_server (service);
1318 create_tcp_handlers (plugin);
1319 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1320 "tcp", _("TCP transport listening on port %llu\n"), bport);
1322 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1325 ("TCP transport advertises itself as being on port %llu\n"),
1327 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1329 /* FIXME: do the two calls below periodically again and
1330 not just once (since the info we get might change...) */
1331 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1332 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1335 HOSTNAME_RESOLVE_TIMEOUT,
1336 &process_hostname_ips,
1343 * Exit point from the plugin.
1346 libgnunet_plugin_transport_tcp_done (void *cls)
1348 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1349 struct Plugin *plugin = api->cls;
1350 struct Session *session;
1352 while (NULL != (session = plugin->sessions))
1353 disconnect_session (session);
1354 if (NULL != plugin->hostname_dns)
1356 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1357 plugin->hostname_dns = NULL;
1359 GNUNET_SERVICE_stop (plugin->service);
1360 GNUNET_free (plugin->handlers);
1361 GNUNET_free (plugin);
1366 /* end of plugin_transport_tcp.c */