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.
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 ( (GNUNET_YES == force_address) &&
588 ( (session->connect_alen != addrlen) ||
589 (0 != memcmp (session->connect_addr,
592 session = NULL; /* ignore existing session */
593 if ( (session == NULL) &&
597 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
599 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
600 GNUNET_i2s (target));
606 if (sizeof (struct sockaddr_in) == addrlen)
608 else if (sizeof (struct sockaddr_in6) == addrlen)
615 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
617 GNUNET_SERVER_MAX_MESSAGE_SIZE);
621 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
623 "Failed to create connection to `%4s' at `%s'\n",
625 GNUNET_a2s (addr, addrlen));
631 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
633 "Asked to transmit to `%4s', creating fresh session.\n",
634 GNUNET_i2s (target));
636 session = create_session (plugin,
638 GNUNET_SERVER_connect_socket (session->plugin->server,
640 session->connect_addr = GNUNET_malloc (addrlen);
641 memcpy (session->connect_addr,
644 session->connect_alen = addrlen;
646 GNUNET_assert (session != NULL);
647 GNUNET_assert (session->client != NULL);
649 /* create new message entry */
650 pm = GNUNET_malloc (mlen + sizeof (struct PendingMessage));
651 memcpy (&pm[1], msg, mlen);
652 pm->msg = (const struct GNUNET_MessageHeader*) &pm[1];
653 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
654 pm->transmit_cont = cont;
655 pm->transmit_cont_cls = cont_cls;
657 /* append pm to pending_messages list */
658 pme = session->pending_messages;
661 session->pending_messages = pm;
665 /* FIXME: this could be done faster by keeping
666 track of the tail of the list... */
667 while (NULL != pme->next)
672 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
674 "Asked to transmit %u bytes to `%s', added message to list.\n",
676 GNUNET_i2s (target));
678 process_pending_messages (session);
684 * Function that can be called to force a disconnect from the
685 * specified neighbour. This should also cancel all previously
686 * scheduled transmissions. Obviously the transmission may have been
687 * partially completed already, which is OK. The plugin is supposed
688 * to close the connection (if applicable) and no longer call the
689 * transmit continuation(s).
691 * Finally, plugin MUST NOT call the services's receive function to
692 * notify the service that the connection to the specified target was
693 * closed after a getting this call.
696 * @param target peer for which the last transmission is
700 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
702 struct Plugin *plugin = cls;
703 struct Session *session;
704 struct PendingMessage *pm;
707 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
709 "Asked to cancel session with `%4s'\n",
710 GNUNET_i2s (target));
712 while (NULL != (session = find_session_by_target (plugin, target)))
714 pm = session->pending_messages;
717 pm->transmit_cont = NULL;
718 pm->transmit_cont_cls = NULL;
721 if (session->client != NULL)
723 GNUNET_SERVER_client_drop (session->client);
724 session->client = NULL;
726 /* rest of the clean-up of the session will be done as part of
727 disconnect_notify which should be triggered any time now
728 (or which may be triggering this call in the first place) */
733 struct PrettyPrinterContext
735 GNUNET_TRANSPORT_AddressStringCallback asc;
742 * Append our port and forward the result.
745 append_port (void *cls, const char *hostname)
747 struct PrettyPrinterContext *ppc = cls;
750 if (hostname == NULL)
752 ppc->asc (ppc->asc_cls, NULL);
756 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
757 ppc->asc (ppc->asc_cls, ret);
763 * Convert the transports address to a nice, human-readable
767 * @param type name of the transport that generated the address
768 * @param addr one of the addresses of the host, NULL for the last address
769 * the specific address format depends on the transport
770 * @param addrlen length of the address
771 * @param numeric should (IP) addresses be displayed in numeric form?
772 * @param timeout after how long should we give up?
773 * @param asc function to call on each string
774 * @param asc_cls closure for asc
777 tcp_plugin_address_pretty_printer (void *cls,
782 struct GNUNET_TIME_Relative timeout,
783 GNUNET_TRANSPORT_AddressStringCallback asc,
786 struct Plugin *plugin = cls;
787 const struct sockaddr_in *v4;
788 const struct sockaddr_in6 *v6;
789 struct PrettyPrinterContext *ppc;
791 if ((addrlen != sizeof (struct sockaddr_in)) &&
792 (addrlen != sizeof (struct sockaddr_in6)))
794 /* invalid address */
799 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
801 ppc->asc_cls = asc_cls;
802 if (addrlen == sizeof (struct sockaddr_in))
804 v4 = (const struct sockaddr_in *) addr;
805 ppc->port = ntohs (v4->sin_port);
809 v6 = (const struct sockaddr_in6 *) addr;
810 ppc->port = ntohs (v6->sin6_port);
813 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
817 !numeric, timeout, &append_port, ppc);
822 * Update the last-received and bandwidth quota values
825 * @param session session to update
826 * @param force set to GNUNET_YES if we should update even
827 * though the minimum refresh time has not yet expired
830 update_quota (struct Session *session, int force)
832 struct GNUNET_TIME_Absolute now;
833 unsigned long long delta;
834 unsigned long long total_allowed;
835 unsigned long long total_remaining;
837 now = GNUNET_TIME_absolute_get ();
838 delta = now.value - session->last_quota_update.value;
839 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
840 return; /* too early, not enough data */
842 total_allowed = session->quota_in * delta;
843 if (total_allowed > session->last_received)
845 /* got less than acceptable */
846 total_remaining = total_allowed - session->last_received;
847 session->last_received = 0;
848 delta = total_remaining / session->quota_in; /* bonus seconds */
849 if (delta > MAX_BANDWIDTH_CARRY)
850 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
854 /* got more than acceptable */
855 session->last_received -= total_allowed;
858 session->last_quota_update.value = now.value - delta;
863 * Set a quota for receiving data from the given peer; this is a
864 * per-transport limit. The transport should limit its read/select
865 * calls to stay below the quota (in terms of incoming data).
868 * @param target the peer for whom the quota is given
869 * @param quota_in quota for receiving/sending data in bytes per ms
872 tcp_plugin_set_receive_quota (void *cls,
873 const struct GNUNET_PeerIdentity *target,
876 struct Plugin *plugin = cls;
877 struct Session *session;
879 session = find_session_by_target (plugin, target);
881 return; /* peer must have disconnected, ignore */
882 if (session->quota_in != quota_in)
884 update_quota (session, GNUNET_YES);
885 if (session->quota_in > quota_in)
886 session->last_quota_update = GNUNET_TIME_absolute_get ();
887 session->quota_in = quota_in;
893 * Check if the given port is plausible (must be either
894 * our listen port or our advertised port). If it is
895 * neither, we return one of these two ports at random.
897 * @return either in_port or a more plausible port
900 check_port (struct Plugin *plugin, uint16_t in_port)
902 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
904 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
906 ? plugin->open_port : plugin->adv_port;
911 * Another peer has suggested an address for this peer and transport
912 * plugin. Check that this could be a valid address.
915 * @param addr pointer to the address
916 * @param addrlen length of addr
917 * @return GNUNET_OK if this is a plausible address for this peer
921 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
923 struct Plugin *plugin = cls;
924 char buf[sizeof (struct sockaddr_in6)];
925 struct sockaddr_in *v4;
926 struct sockaddr_in6 *v6;
928 if ((addrlen != sizeof (struct sockaddr_in)) &&
929 (addrlen != sizeof (struct sockaddr_in6)))
932 return GNUNET_SYSERR;
934 memcpy (buf, addr, sizeof (struct sockaddr_in6));
935 if (addrlen == sizeof (struct sockaddr_in))
937 v4 = (struct sockaddr_in *) buf;
938 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
942 v6 = (struct sockaddr_in6 *) buf;
943 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
946 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
948 "Informing transport service about my address `%s'.\n",
949 GNUNET_a2s (addr, addrlen));
956 * We've received a welcome from this peer via TCP. Possibly create a
957 * fresh client record and send back our welcome.
960 * @param client identification of the client
961 * @param message the actual message
964 handle_tcp_welcome (void *cls,
965 struct GNUNET_SERVER_Client *client,
966 const struct GNUNET_MessageHeader *message)
968 struct Plugin *plugin = cls;
969 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
970 struct Session *session;
975 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
977 "Received `%s' message from `%4s/%p'.\n", "WELCOME",
978 GNUNET_i2s (&wm->clientIdentity), client);
980 session = find_session_by_client (plugin, client);
983 GNUNET_SERVER_client_keep (client);
984 session = create_session (plugin,
985 &wm->clientIdentity, client);
987 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
989 session->connect_addr = vaddr;
990 session->connect_alen = alen;
993 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
995 "Creating new session %p for incoming `%s' message.\n",
996 session_c, "WELCOME");
998 process_pending_messages (session);
1000 if (session->expecting_welcome != GNUNET_YES)
1002 GNUNET_break_op (0);
1003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1006 session->expecting_welcome = GNUNET_NO;
1007 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1012 * Calculate how long we should delay reading from the TCP socket to
1013 * ensure that we stay within our bandwidth limits (push back).
1015 * @param session for which client should this be calculated
1017 static struct GNUNET_TIME_Relative
1018 calculate_throttle_delay (struct Session *session)
1020 struct GNUNET_TIME_Relative ret;
1021 struct GNUNET_TIME_Absolute now;
1026 now = GNUNET_TIME_absolute_get ();
1027 del = now.value - session->last_quota_update.value;
1028 if (del > MAX_BANDWIDTH_CARRY)
1030 update_quota (session, GNUNET_YES);
1031 del = now.value - session->last_quota_update.value;
1032 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1034 if (session->quota_in == 0)
1035 session->quota_in = 1; /* avoid divison by zero */
1036 avail = del * session->quota_in;
1037 if (avail > session->last_received)
1038 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1039 excess = session->last_received - avail;
1040 ret.value = excess / session->quota_in;
1046 * Task to signal the server that we can continue
1047 * receiving from the TCP client now.
1050 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1052 struct Session *session = cls;
1053 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1058 * We've received data for this peer via TCP. Unbox,
1059 * compute latency and forward.
1061 * @param cls closure
1062 * @param client identification of the client
1063 * @param message the actual message
1066 handle_tcp_data (void *cls,
1067 struct GNUNET_SERVER_Client *client,
1068 const struct GNUNET_MessageHeader *message)
1070 struct Plugin *plugin = cls;
1071 struct Session *session;
1073 struct GNUNET_TIME_Relative delay;
1075 msize = ntohs (message->size);
1076 session = find_session_by_client (plugin, client);
1077 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1079 GNUNET_break_op (0);
1080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1084 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1085 "tcp", "Receiving %u bytes from `%4s'.\n",
1086 msize, GNUNET_i2s (&session->target));
1089 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1091 "Forwarding %u bytes of data of type %u to transport service.\n",
1092 (unsigned int) msize,
1093 (unsigned int) ntohs (msg->type));
1095 plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1096 session->connect_addr,
1097 session->connect_alen);
1098 /* update bandwidth used */
1099 session->last_received += msize;
1100 update_quota (session, GNUNET_NO);
1101 delay = calculate_throttle_delay (session);
1102 if (delay.value == 0)
1103 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1105 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1106 delay, &delayed_done, session);
1111 * Handlers for the various TCP messages.
1113 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1114 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1115 sizeof (struct WelcomeMessage)},
1116 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA, 0},
1122 create_tcp_handlers (struct Plugin *plugin)
1125 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1126 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1129 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1131 plugin->handlers[i].callback_cls = plugin;
1132 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1137 * Functions with this signature are called whenever a peer
1138 * is disconnected on the network level.
1140 * @param cls closure
1141 * @param client identification of the client
1144 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1146 struct Plugin *plugin = cls;
1147 struct Session *session;
1149 session = find_session_by_client (plugin, client);
1150 if (session == NULL)
1151 return; /* unknown, nothing to do */
1153 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1155 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1156 GNUNET_i2s (&session->target),
1157 (session->connect_addr != NULL) ?
1158 GNUNET_a2s (session->connect_addr,
1159 session->connect_alen) : "*", client);
1161 disconnect_session (session);
1166 * Add the IP of our network interface to the list of
1167 * our external IP addresses.
1170 process_interfaces (void *cls,
1173 const struct sockaddr *addr, socklen_t addrlen)
1175 struct Plugin *plugin = cls;
1177 struct sockaddr_in *v4;
1178 struct sockaddr_in6 *v6;
1180 af = addr->sa_family;
1183 v4 = (struct sockaddr_in *) addr;
1184 v4->sin_port = htons (plugin->adv_port);
1188 GNUNET_assert (af == AF_INET6);
1189 v6 = (struct sockaddr_in6 *) addr;
1190 v6->sin6_port = htons (plugin->adv_port);
1192 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1193 GNUNET_ERROR_TYPE_BULK,
1194 "tcp", _("Found address `%s' (%s)\n"),
1195 GNUNET_a2s (addr, addrlen), name);
1196 plugin->env->notify_address (plugin->env->cls,
1198 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1204 * Function called by the resolver for each address obtained from DNS
1205 * for our own hostname. Add the addresses to the list of our
1206 * external IP addresses.
1208 * @param cls closure
1209 * @param addr one of the addresses of the host, NULL for the last address
1210 * @param addrlen length of the address
1213 process_hostname_ips (void *cls,
1214 const struct sockaddr *addr, socklen_t addrlen)
1216 struct Plugin *plugin = cls;
1220 plugin->hostname_dns = NULL;
1223 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1228 * Entry point for the plugin.
1231 libgnunet_plugin_transport_tcp_init (void *cls)
1233 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1234 struct GNUNET_TRANSPORT_PluginFunctions *api;
1235 struct Plugin *plugin;
1236 struct GNUNET_SERVICE_Context *service;
1237 unsigned long long aport;
1238 unsigned long long bport;
1240 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1241 if (service == NULL)
1243 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1246 ("Failed to start service for `%s' transport plugin.\n"),
1252 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1258 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1261 &aport)) && (aport > 65535)))
1263 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1266 ("Require valid port number for service `%s' in configuration!\n"),
1268 GNUNET_SERVICE_stop (service);
1273 plugin = GNUNET_malloc (sizeof (struct Plugin));
1274 plugin->open_port = bport;
1275 plugin->adv_port = aport;
1277 plugin->lsock = NULL;
1278 plugin->statistics = NULL;
1279 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1281 api->send = &tcp_plugin_send;
1282 api->disconnect = &tcp_plugin_disconnect;
1283 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1284 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1285 api->check_address = &tcp_plugin_check_address;
1286 plugin->service = service;
1287 plugin->server = GNUNET_SERVICE_get_server (service);
1288 create_tcp_handlers (plugin);
1289 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1290 "tcp", _("TCP transport listening on port %llu\n"), bport);
1292 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1295 ("TCP transport advertises itself as being on port %llu\n"),
1297 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1299 /* FIXME: do the two calls below periodically again and
1300 not just once (since the info we get might change...) */
1301 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1302 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1305 HOSTNAME_RESOLVE_TIMEOUT,
1306 &process_hostname_ips,
1313 * Exit point from the plugin.
1316 libgnunet_plugin_transport_tcp_done (void *cls)
1318 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1319 struct Plugin *plugin = api->cls;
1320 struct Session *session;
1322 while (NULL != (session = plugin->sessions))
1323 disconnect_session (session);
1324 if (NULL != plugin->hostname_dns)
1326 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1327 plugin->hostname_dns = NULL;
1329 GNUNET_SERVICE_stop (plugin->service);
1330 GNUNET_free (plugin->handlers);
1331 GNUNET_free (plugin);
1336 /* end of plugin_transport_tcp.c */