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,
569 const struct GNUNET_MessageHeader *msg,
571 struct GNUNET_TIME_Relative timeout,
575 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
577 struct Plugin *plugin = cls;
578 struct Session *session;
579 struct PendingMessage *pm;
580 struct PendingMessage *pme;
581 struct GNUNET_CONNECTION_Handle *sa;
585 mlen = ntohs (msg->size);
586 session = find_session_by_target (plugin, target);
587 if ( (session != NULL) && ((GNUNET_YES == force_address) &&
588 ( (session->connect_alen != addrlen) ||
589 (0 != memcmp (session->connect_addr,
592 session = NULL; /* ignore existing session */
594 if ( (session == NULL) &&
598 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
600 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
601 GNUNET_i2s (target));
607 if (sizeof (struct sockaddr_in) == addrlen)
609 else if (sizeof (struct sockaddr_in6) == addrlen)
616 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
618 GNUNET_SERVER_MAX_MESSAGE_SIZE);
622 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
624 "Failed to create connection to `%4s' at `%s'\n",
626 GNUNET_a2s (addr, addrlen));
632 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
634 "Asked to transmit to `%4s', creating fresh session.\n",
635 GNUNET_i2s (target));
637 session = create_session (plugin,
639 GNUNET_SERVER_connect_socket (plugin->server,
641 session->connect_addr = GNUNET_malloc (addrlen);
642 memcpy (session->connect_addr,
645 session->connect_alen = addrlen;
647 GNUNET_assert (session != NULL);
648 GNUNET_assert (session->client != NULL);
650 /* create new message entry */
651 pm = GNUNET_malloc (mlen + sizeof (struct PendingMessage));
652 memcpy (&pm[1], msg, mlen);
653 pm->msg = (const struct GNUNET_MessageHeader*) &pm[1];
654 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
655 pm->transmit_cont = cont;
656 pm->transmit_cont_cls = cont_cls;
658 /* append pm to pending_messages list */
659 pme = session->pending_messages;
662 session->pending_messages = pm;
666 /* FIXME: this could be done faster by keeping
667 track of the tail of the list... */
668 while (NULL != pme->next)
673 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
675 "Asked to transmit %u bytes to `%s', added message to list.\n",
677 GNUNET_i2s (target));
679 process_pending_messages (session);
685 * Function that can be called to force a disconnect from the
686 * specified neighbour. This should also cancel all previously
687 * scheduled transmissions. Obviously the transmission may have been
688 * partially completed already, which is OK. The plugin is supposed
689 * to close the connection (if applicable) and no longer call the
690 * transmit continuation(s).
692 * Finally, plugin MUST NOT call the services's receive function to
693 * notify the service that the connection to the specified target was
694 * closed after a getting this call.
697 * @param target peer for which the last transmission is
701 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
703 struct Plugin *plugin = cls;
704 struct Session *session;
705 struct PendingMessage *pm;
708 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
710 "Asked to cancel session with `%4s'\n",
711 GNUNET_i2s (target));
713 while (NULL != (session = find_session_by_target (plugin, target)))
715 pm = session->pending_messages;
718 pm->transmit_cont = NULL;
719 pm->transmit_cont_cls = NULL;
722 if (session->client != NULL)
724 GNUNET_SERVER_client_drop (session->client);
725 session->client = NULL;
727 /* rest of the clean-up of the session will be done as part of
728 disconnect_notify which should be triggered any time now
729 (or which may be triggering this call in the first place) */
734 struct PrettyPrinterContext
736 GNUNET_TRANSPORT_AddressStringCallback asc;
743 * Append our port and forward the result.
746 append_port (void *cls, const char *hostname)
748 struct PrettyPrinterContext *ppc = cls;
751 if (hostname == NULL)
753 ppc->asc (ppc->asc_cls, NULL);
757 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
758 ppc->asc (ppc->asc_cls, ret);
764 * Convert the transports address to a nice, human-readable
768 * @param type name of the transport that generated the address
769 * @param addr one of the addresses of the host, NULL for the last address
770 * the specific address format depends on the transport
771 * @param addrlen length of the address
772 * @param numeric should (IP) addresses be displayed in numeric form?
773 * @param timeout after how long should we give up?
774 * @param asc function to call on each string
775 * @param asc_cls closure for asc
778 tcp_plugin_address_pretty_printer (void *cls,
783 struct GNUNET_TIME_Relative timeout,
784 GNUNET_TRANSPORT_AddressStringCallback asc,
787 struct Plugin *plugin = cls;
788 const struct sockaddr_in *v4;
789 const struct sockaddr_in6 *v6;
790 struct PrettyPrinterContext *ppc;
792 if ((addrlen != sizeof (struct sockaddr_in)) &&
793 (addrlen != sizeof (struct sockaddr_in6)))
795 /* invalid address */
800 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
802 ppc->asc_cls = asc_cls;
803 if (addrlen == sizeof (struct sockaddr_in))
805 v4 = (const struct sockaddr_in *) addr;
806 ppc->port = ntohs (v4->sin_port);
810 v6 = (const struct sockaddr_in6 *) addr;
811 ppc->port = ntohs (v6->sin6_port);
814 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
818 !numeric, timeout, &append_port, ppc);
823 * Update the last-received and bandwidth quota values
826 * @param session session to update
827 * @param force set to GNUNET_YES if we should update even
828 * though the minimum refresh time has not yet expired
831 update_quota (struct Session *session, int force)
833 struct GNUNET_TIME_Absolute now;
834 unsigned long long delta;
835 unsigned long long total_allowed;
836 unsigned long long total_remaining;
838 now = GNUNET_TIME_absolute_get ();
839 delta = now.value - session->last_quota_update.value;
840 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
841 return; /* too early, not enough data */
843 total_allowed = session->quota_in * delta;
844 if (total_allowed > session->last_received)
846 /* got less than acceptable */
847 total_remaining = total_allowed - session->last_received;
848 session->last_received = 0;
849 delta = total_remaining / session->quota_in; /* bonus seconds */
850 if (delta > MAX_BANDWIDTH_CARRY)
851 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
855 /* got more than acceptable */
856 session->last_received -= total_allowed;
859 session->last_quota_update.value = now.value - delta;
864 * Set a quota for receiving data from the given peer; this is a
865 * per-transport limit. The transport should limit its read/select
866 * calls to stay below the quota (in terms of incoming data).
869 * @param target the peer for whom the quota is given
870 * @param quota_in quota for receiving/sending data in bytes per ms
873 tcp_plugin_set_receive_quota (void *cls,
874 const struct GNUNET_PeerIdentity *target,
877 struct Plugin *plugin = cls;
878 struct Session *session;
880 session = find_session_by_target (plugin, target);
882 return; /* peer must have disconnected, ignore */
883 if (session->quota_in != quota_in)
885 update_quota (session, GNUNET_YES);
886 if (session->quota_in > quota_in)
887 session->last_quota_update = GNUNET_TIME_absolute_get ();
888 session->quota_in = quota_in;
894 * Check if the given port is plausible (must be either
895 * our listen port or our advertised port). If it is
896 * neither, we return one of these two ports at random.
898 * @return either in_port or a more plausible port
901 check_port (struct Plugin *plugin, uint16_t in_port)
903 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
905 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
907 ? plugin->open_port : plugin->adv_port;
912 * Another peer has suggested an address for this peer and transport
913 * plugin. Check that this could be a valid address.
916 * @param addr pointer to the address
917 * @param addrlen length of addr
918 * @return GNUNET_OK if this is a plausible address for this peer
922 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
924 struct Plugin *plugin = cls;
925 char buf[sizeof (struct sockaddr_in6)];
926 struct sockaddr_in *v4;
927 struct sockaddr_in6 *v6;
929 if ((addrlen != sizeof (struct sockaddr_in)) &&
930 (addrlen != sizeof (struct sockaddr_in6)))
933 return GNUNET_SYSERR;
935 memcpy (buf, addr, sizeof (struct sockaddr_in6));
936 if (addrlen == sizeof (struct sockaddr_in))
938 v4 = (struct sockaddr_in *) buf;
939 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
943 v6 = (struct sockaddr_in6 *) buf;
944 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
947 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
949 "Informing transport service about my address `%s'.\n",
950 GNUNET_a2s (addr, addrlen));
957 * We've received a welcome from this peer via TCP. Possibly create a
958 * fresh client record and send back our welcome.
961 * @param client identification of the client
962 * @param message the actual message
965 handle_tcp_welcome (void *cls,
966 struct GNUNET_SERVER_Client *client,
967 const struct GNUNET_MessageHeader *message)
969 struct Plugin *plugin = cls;
970 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
971 struct Session *session;
976 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
978 "Received `%s' message from a `%4s/%p'.\n", "WELCOME",
979 GNUNET_i2s (&wm->clientIdentity), client);
982 session = find_session_by_client (plugin, client);
985 GNUNET_SERVER_client_keep (client);
986 session = create_session (plugin,
987 &wm->clientIdentity, client);
989 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
992 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
994 "Found address for incoming `%s' message\n",
997 session->connect_addr = vaddr;
998 session->connect_alen = alen;
1003 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1005 "Didn't find address for incoming `%s' message\n",
1010 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1012 "Creating new session %p for incoming `%s' message.\n",
1013 session, "WELCOME");
1015 process_pending_messages (session);
1017 if (session->expecting_welcome != GNUNET_YES)
1019 GNUNET_break_op (0);
1020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1023 session->expecting_welcome = GNUNET_NO;
1024 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1029 * Calculate how long we should delay reading from the TCP socket to
1030 * ensure that we stay within our bandwidth limits (push back).
1032 * @param session for which client should this be calculated
1034 static struct GNUNET_TIME_Relative
1035 calculate_throttle_delay (struct Session *session)
1037 struct GNUNET_TIME_Relative ret;
1038 struct GNUNET_TIME_Absolute now;
1043 now = GNUNET_TIME_absolute_get ();
1044 del = now.value - session->last_quota_update.value;
1045 if (del > MAX_BANDWIDTH_CARRY)
1047 update_quota (session, GNUNET_YES);
1048 del = now.value - session->last_quota_update.value;
1049 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1051 if (session->quota_in == 0)
1052 session->quota_in = 1; /* avoid divison by zero */
1053 avail = del * session->quota_in;
1054 if (avail > session->last_received)
1055 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1056 excess = session->last_received - avail;
1057 ret.value = excess / session->quota_in;
1063 * Task to signal the server that we can continue
1064 * receiving from the TCP client now.
1067 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1069 struct Session *session = cls;
1070 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1075 * We've received data for this peer via TCP. Unbox,
1076 * compute latency and forward.
1078 * @param cls closure
1079 * @param client identification of the client
1080 * @param message the actual message
1083 handle_tcp_data (void *cls,
1084 struct GNUNET_SERVER_Client *client,
1085 const struct GNUNET_MessageHeader *message)
1087 struct Plugin *plugin = cls;
1088 struct Session *session;
1090 struct GNUNET_TIME_Relative delay;
1092 msize = ntohs (message->size);
1093 session = find_session_by_client (plugin, client);
1095 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
1098 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1099 "tcp", "Received a welcome, NOT sending to clients!\n");
1101 return; /* We don't want to propogate WELCOME messages up! */
1104 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1106 GNUNET_break_op (0);
1107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1111 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1112 "tcp", "Receiving %u bytes from `%4s'.\n",
1113 msize, GNUNET_i2s (&session->target));
1116 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1118 "Forwarding %u bytes of data of type %u to transport service.\n",
1119 (unsigned int) msize,
1120 (unsigned int) ntohs (message->type));
1122 plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1123 session->connect_addr,
1124 session->connect_alen);
1125 /* update bandwidth used */
1126 session->last_received += msize;
1127 update_quota (session, GNUNET_NO);
1128 delay = calculate_throttle_delay (session);
1129 if (delay.value == 0)
1130 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1132 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1133 delay, &delayed_done, session);
1138 * Handlers for the various TCP messages.
1140 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1141 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1142 sizeof (struct WelcomeMessage)},
1143 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1149 create_tcp_handlers (struct Plugin *plugin)
1152 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1153 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1156 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1158 plugin->handlers[i].callback_cls = plugin;
1159 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1164 * Functions with this signature are called whenever a peer
1165 * is disconnected on the network level.
1167 * @param cls closure
1168 * @param client identification of the client
1171 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1173 struct Plugin *plugin = cls;
1174 struct Session *session;
1178 session = find_session_by_client (plugin, client);
1179 if (session == NULL)
1180 return; /* unknown, nothing to do */
1182 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1184 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1185 GNUNET_i2s (&session->target),
1186 (session->connect_addr != NULL) ?
1187 GNUNET_a2s (session->connect_addr,
1188 session->connect_alen) : "*", client);
1190 disconnect_session (session);
1195 * Add the IP of our network interface to the list of
1196 * our external IP addresses.
1199 process_interfaces (void *cls,
1202 const struct sockaddr *addr, socklen_t addrlen)
1204 struct Plugin *plugin = cls;
1206 struct sockaddr_in *v4;
1207 struct sockaddr_in6 *v6;
1209 af = addr->sa_family;
1212 v4 = (struct sockaddr_in *) addr;
1213 v4->sin_port = htons (plugin->adv_port);
1217 GNUNET_assert (af == AF_INET6);
1218 v6 = (struct sockaddr_in6 *) addr;
1219 v6->sin6_port = htons (plugin->adv_port);
1221 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1222 GNUNET_ERROR_TYPE_BULK,
1223 "tcp", _("Found address `%s' (%s)\n"),
1224 GNUNET_a2s (addr, addrlen), name);
1225 plugin->env->notify_address (plugin->env->cls,
1227 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1233 * Function called by the resolver for each address obtained from DNS
1234 * for our own hostname. Add the addresses to the list of our
1235 * external IP addresses.
1237 * @param cls closure
1238 * @param addr one of the addresses of the host, NULL for the last address
1239 * @param addrlen length of the address
1242 process_hostname_ips (void *cls,
1243 const struct sockaddr *addr, socklen_t addrlen)
1245 struct Plugin *plugin = cls;
1249 plugin->hostname_dns = NULL;
1252 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1257 * Entry point for the plugin.
1260 libgnunet_plugin_transport_tcp_init (void *cls)
1262 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1263 struct GNUNET_TRANSPORT_PluginFunctions *api;
1264 struct Plugin *plugin;
1265 struct GNUNET_SERVICE_Context *service;
1266 unsigned long long aport;
1267 unsigned long long bport;
1269 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1270 if (service == NULL)
1272 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1275 ("Failed to start service for `%s' transport plugin.\n"),
1281 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1287 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1290 &aport)) && (aport > 65535)))
1292 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1295 ("Require valid port number for service `%s' in configuration!\n"),
1297 GNUNET_SERVICE_stop (service);
1302 plugin = GNUNET_malloc (sizeof (struct Plugin));
1303 plugin->open_port = bport;
1304 plugin->adv_port = aport;
1306 plugin->lsock = NULL;
1307 plugin->statistics = NULL;
1308 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1310 api->send = &tcp_plugin_send;
1311 api->disconnect = &tcp_plugin_disconnect;
1312 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1313 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1314 api->check_address = &tcp_plugin_check_address;
1315 plugin->service = service;
1316 plugin->server = GNUNET_SERVICE_get_server (service);
1317 create_tcp_handlers (plugin);
1318 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1319 "tcp", _("TCP transport listening on port %llu\n"), bport);
1321 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1324 ("TCP transport advertises itself as being on port %llu\n"),
1326 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1328 /* FIXME: do the two calls below periodically again and
1329 not just once (since the info we get might change...) */
1330 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1331 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1334 HOSTNAME_RESOLVE_TIMEOUT,
1335 &process_hostname_ips,
1342 * Exit point from the plugin.
1345 libgnunet_plugin_transport_tcp_done (void *cls)
1347 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1348 struct Plugin *plugin = api->cls;
1349 struct Session *session;
1351 while (NULL != (session = plugin->sessions))
1352 disconnect_session (session);
1353 if (NULL != plugin->hostname_dns)
1355 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1356 plugin->hostname_dns = NULL;
1358 GNUNET_SERVICE_stop (plugin->service);
1359 GNUNET_free (plugin->handlers);
1360 GNUNET_free (plugin);
1365 /* end of plugin_transport_tcp.c */