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;
96 * So that the gnunet_transport_service can group messages together,
97 * these pending messages need to accept a message buffer and size
98 * instead of just a GNUNET_MessageHeader.
103 * Continuation function to call once the message
104 * has been sent. Can be NULL if there is no
105 * continuation to call.
107 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
110 * Closure for transmit_cont.
112 void *transmit_cont_cls;
115 * Timeout value for the pending message.
117 struct GNUNET_TIME_Absolute timeout;
123 * Session handle for TCP connections.
129 * Stored in a linked list.
131 struct Session *next;
134 * Pointer to the global plugin struct.
136 struct Plugin *plugin;
139 * The client (used to identify this connection)
141 struct GNUNET_SERVER_Client *client;
144 * Messages currently pending for transmission
145 * to this peer, if any.
147 struct PendingMessage *pending_messages;
150 * Handle for pending transmission request.
152 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
155 * To whom are we talking to (set to our identity
156 * if we are still waiting for the welcome message)
158 struct GNUNET_PeerIdentity target;
161 * At what time did we reset last_received last?
163 struct GNUNET_TIME_Absolute last_quota_update;
166 * Address of the other peer (either based on our 'connect'
167 * call or on our 'accept' call).
172 * How many bytes have we received since the "last_quota_update"
175 uint64_t last_received;
178 * Number of bytes per ms that this peer is allowed
184 * Length of connect_addr.
189 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
190 * GNUNET_SYSERR is used to mark non-welcoming connections (HELLO
193 int expecting_welcome;
199 * Encapsulation of all of the state of the plugin.
206 struct GNUNET_TRANSPORT_PluginEnvironment *env;
211 struct GNUNET_CONNECTION_Handle *lsock;
214 * List of open TCP sessions.
216 struct Session *sessions;
219 * Handle for the statistics service.
221 struct GNUNET_STATISTICS_Handle *statistics;
224 * Handle to the network service.
226 struct GNUNET_SERVICE_Context *service;
229 * Handle to the server for this service.
231 struct GNUNET_SERVER_Handle *server;
234 * Copy of the handler array where the closures are
235 * set to this struct's instance.
237 struct GNUNET_SERVER_MessageHandler *handlers;
240 * Handle for request of hostname resolution, non-NULL if pending.
242 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
245 * ID of task used to update our addresses when one expires.
247 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
250 * Port that we are actually listening on.
255 * Port that the user said we would have visible to the
264 * Find the session handle for the given peer.
266 static struct Session *
267 find_session_by_target (struct Plugin *plugin,
268 const struct GNUNET_PeerIdentity *target)
272 ret = plugin->sessions;
273 while ( (ret != NULL) &&
274 ((GNUNET_SYSERR == ret->expecting_welcome) ||
275 (0 != memcmp (target,
276 &ret->target, sizeof (struct GNUNET_PeerIdentity)))))
283 * Find the session handle for the given peer.
285 static struct Session *
286 find_session_by_client (struct Plugin *plugin,
287 const struct GNUNET_SERVER_Client *client)
291 ret = plugin->sessions;
292 while ((ret != NULL) && (client != ret->client))
299 * Create a welcome message.
301 static struct PendingMessage *
302 create_welcome (struct Plugin *plugin)
304 struct PendingMessage *pm;
305 struct WelcomeMessage *welcome;
307 pm = GNUNET_malloc (sizeof (struct PendingMessage));
308 pm->msg = GNUNET_malloc(sizeof(struct WelcomeMessage));
309 welcome = (struct WelcomeMessage *)pm->msg;
310 pm->message_size = sizeof (struct WelcomeMessage);
311 welcome->header.size = htons (sizeof (struct WelcomeMessage));
312 welcome->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
313 welcome->clientIdentity = *plugin->env->my_identity;
314 pm->timeout = GNUNET_TIME_relative_to_absolute (WELCOME_TIMEOUT);
320 * Create a new session.
323 * @param target peer to connect to
324 * @param client client to use
325 * @return new session object
327 static struct Session *
328 create_session (struct Plugin *plugin,
329 const struct GNUNET_PeerIdentity *target,
330 struct GNUNET_SERVER_Client *client)
334 ret = GNUNET_malloc (sizeof (struct Session));
335 ret->plugin = plugin;
336 ret->next = plugin->sessions;
337 plugin->sessions = ret;
338 ret->client = client;
339 ret->target = *target;
340 ret->last_quota_update = GNUNET_TIME_absolute_get ();
341 ret->quota_in = plugin->env->default_quota_in;
342 ret->expecting_welcome = GNUNET_YES;
343 ret->pending_messages = create_welcome (plugin);
349 * If we have pending messages, ask the server to
350 * transmit them (schedule the respective tasks, etc.)
352 * @param session for which session should we do this
354 static void process_pending_messages (struct Session *session);
358 * Function called to notify a client about the socket
359 * being ready to queue more data. "buf" will be
360 * NULL and "size" zero if the socket was closed for
361 * writing in the meantime.
364 * @param size number of bytes available in buf
365 * @param buf where the callee should write the message
366 * @return number of bytes written to buf
369 do_transmit (void *cls, size_t size, void *buf)
371 struct Session *session = cls;
372 struct PendingMessage *pm;
377 session->transmit_handle = NULL;
381 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
383 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
384 GNUNET_i2s (&session->target));
387 while (NULL != (pm = session->pending_messages))
389 session->pending_messages = pm->next;
391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
393 "Failed to transmit message of to `%4s'.\n",
394 GNUNET_i2s (&session->target));
396 if (pm->transmit_cont != NULL)
397 pm->transmit_cont (pm->transmit_cont_cls,
398 &session->target, GNUNET_SYSERR);
405 while (NULL != (pm = session->pending_messages))
407 if (size < pm->message_size)
409 memcpy (cbuf, pm->msg, pm->message_size);
410 cbuf += pm->message_size;
411 ret += pm->message_size;
412 size -= pm->message_size;
413 session->pending_messages = pm->next;
414 if (pm->transmit_cont != NULL)
415 pm->transmit_cont (pm->transmit_cont_cls,
416 &session->target, GNUNET_OK);
419 process_pending_messages (session);
421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
422 "tcp", "Transmitting %u bytes\n", ret);
429 * If we have pending messages, ask the server to
430 * transmit them (schedule the respective tasks, etc.)
432 * @param session for which session should we do this
435 process_pending_messages (struct Session *session)
437 struct PendingMessage *pm;
438 GNUNET_assert (session->client != NULL);
439 if (session->transmit_handle != NULL)
441 if (NULL == (pm = session->pending_messages))
443 session->transmit_handle
444 = GNUNET_SERVER_notify_transmit_ready (session->client,
446 GNUNET_TIME_absolute_get_remaining
448 &do_transmit, session);
453 * Functions with this signature are called whenever we need
454 * to close a session due to a disconnect or failure to
455 * establish a connection.
457 * @param session session to close down
460 disconnect_session (struct Session *session)
462 struct Session *prev;
464 struct PendingMessage *pm;
467 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
469 "Disconnecting from `%4s' at %s (session %p).\n",
470 GNUNET_i2s (&session->target),
471 (session->connect_addr != NULL) ?
472 GNUNET_a2s (session->connect_addr,
473 session->connect_alen) : "*", session);
475 /* remove from session list */
477 pos = session->plugin->sessions;
478 while (pos != session)
484 session->plugin->sessions = session->next;
486 prev->next = session->next;
488 if (session->transmit_handle != NULL)
490 GNUNET_CONNECTION_notify_transmit_ready_cancel
491 (session->transmit_handle);
492 session->transmit_handle = NULL;
494 while (NULL != (pm = session->pending_messages))
497 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
499 pm->transmit_cont != NULL
500 ? "Could not deliver message to `%4s'.\n"
502 "Could not deliver message to `%4s', notifying.\n",
503 GNUNET_i2s (&session->target));
505 session->pending_messages = pm->next;
506 if (NULL != pm->transmit_cont)
507 pm->transmit_cont (pm->transmit_cont_cls,
508 &session->target, GNUNET_SYSERR);
509 GNUNET_free(pm->msg);
512 if (GNUNET_NO == session->expecting_welcome)
515 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
517 "Notifying transport service about loss of data connection with `%4s'.\n",
518 GNUNET_i2s (&session->target));
520 /* Data session that actually went past the
521 initial handshake; transport service may
522 know about this one, so we need to
523 notify transport service about disconnect */
524 session->plugin->env->receive (session->plugin->env->cls,
525 &session->target, NULL,
527 session->connect_addr,
528 session->connect_alen);
530 if (session->client != NULL)
532 GNUNET_SERVER_client_drop (session->client);
533 session->client = NULL;
535 GNUNET_free_non_null (session->connect_addr);
536 GNUNET_free (session);
541 * Function that can be used by the transport service to transmit
542 * a message using the plugin. Note that in the case of a
543 * peer disconnecting, the continuation MUST be called
544 * prior to the disconnect notification itself. This function
545 * will be called with this peer's HELLO message to initiate
546 * a fresh connection to another peer.
549 * @param target who should receive this message
550 * @param msg the message to transmit
551 * @param priority how important is the message (most plugins will
552 * ignore message priority and just FIFO)
553 * @param timeout how long to wait at most for the transmission (does not
554 * require plugins to discard the message after the timeout,
555 * just advisory for the desired delay; most plugins will ignore
557 * @param addr the address to use (can be NULL if the plugin
558 * is "on its own" (i.e. re-use existing TCP connection))
559 * @param addrlen length of the address in bytes
560 * @param force_address GNUNET_YES if the plugin MUST use the given address,
561 * otherwise the plugin may use other addresses or
562 * existing connections (if available)
563 * @param cont continuation to call once the message has
564 * been transmitted (or if the transport is ready
565 * for the next transmission call; or if the
566 * peer disconnected...); can be NULL
567 * @param cont_cls closure for cont
568 * @return number of bytes used (on the physical network, with overheads);
569 * -1 on hard errors (i.e. address invalid); 0 is a legal value
570 * and does NOT mean that the message was not transmitted (DV)
573 tcp_plugin_send (void *cls,
574 const struct GNUNET_PeerIdentity *target,
578 struct GNUNET_TIME_Relative timeout,
582 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
584 struct Plugin *plugin = cls;
585 struct Session *session;
586 struct PendingMessage *pm;
587 struct PendingMessage *pme;
588 struct GNUNET_CONNECTION_Handle *sa;
591 session = find_session_by_target (plugin, target);
592 if ( (session != NULL) && ((GNUNET_YES == force_address) &&
593 ( (session->connect_alen != addrlen) ||
594 (0 != memcmp (session->connect_addr,
597 session = NULL; /* ignore existing session */
599 if ( (session == NULL) &&
603 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
605 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
606 GNUNET_i2s (target));
612 if (sizeof (struct sockaddr_in) == addrlen)
614 else if (sizeof (struct sockaddr_in6) == addrlen)
621 sa = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
623 GNUNET_SERVER_MAX_MESSAGE_SIZE);
627 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
629 "Failed to create connection to `%4s' at `%s'\n",
631 GNUNET_a2s (addr, addrlen));
637 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
639 "Asked to transmit to `%4s', creating fresh session.\n",
640 GNUNET_i2s (target));
642 session = create_session (plugin,
644 GNUNET_SERVER_connect_socket (plugin->server,
646 session->connect_addr = GNUNET_malloc (addrlen);
647 memcpy (session->connect_addr,
650 session->connect_alen = addrlen;
652 GNUNET_assert (session != NULL);
653 GNUNET_assert (session->client != NULL);
656 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
658 "Creating pending message of size %d\n",
661 /* create new message entry */
662 pm = GNUNET_malloc (sizeof (struct PendingMessage));
663 pm->msg = GNUNET_malloc(msgbuf_size);
664 memcpy (pm->msg, msg, msgbuf_size);
665 pm->message_size = msgbuf_size;
666 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
667 pm->transmit_cont = cont;
668 pm->transmit_cont_cls = cont_cls;
670 /* append pm to pending_messages list */
671 pme = session->pending_messages;
674 session->pending_messages = pm;
678 /* FIXME: this could be done faster by keeping
679 track of the tail of the list... */
680 while (NULL != pme->next)
685 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
687 "Asked to transmit %u bytes to `%s', added message to list.\n",
689 GNUNET_i2s (target));
691 process_pending_messages (session);
697 * Function that can be called to force a disconnect from the
698 * specified neighbour. This should also cancel all previously
699 * scheduled transmissions. Obviously the transmission may have been
700 * partially completed already, which is OK. The plugin is supposed
701 * to close the connection (if applicable) and no longer call the
702 * transmit continuation(s).
704 * Finally, plugin MUST NOT call the services's receive function to
705 * notify the service that the connection to the specified target was
706 * closed after a getting this call.
709 * @param target peer for which the last transmission is
713 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
715 struct Plugin *plugin = cls;
716 struct Session *session;
717 struct PendingMessage *pm;
720 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
722 "Asked to cancel session with `%4s'\n",
723 GNUNET_i2s (target));
725 while (NULL != (session = find_session_by_target (plugin, target)))
727 pm = session->pending_messages;
730 pm->transmit_cont = NULL;
731 pm->transmit_cont_cls = NULL;
734 if (session->client != NULL)
736 GNUNET_SERVER_client_drop (session->client);
737 session->client = NULL;
739 /* rest of the clean-up of the session will be done as part of
740 disconnect_notify which should be triggered any time now
741 (or which may be triggering this call in the first place) */
746 struct PrettyPrinterContext
748 GNUNET_TRANSPORT_AddressStringCallback asc;
755 * Append our port and forward the result.
758 append_port (void *cls, const char *hostname)
760 struct PrettyPrinterContext *ppc = cls;
763 if (hostname == NULL)
765 ppc->asc (ppc->asc_cls, NULL);
769 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
770 ppc->asc (ppc->asc_cls, ret);
776 * Convert the transports address to a nice, human-readable
780 * @param type name of the transport that generated the address
781 * @param addr one of the addresses of the host, NULL for the last address
782 * the specific address format depends on the transport
783 * @param addrlen length of the address
784 * @param numeric should (IP) addresses be displayed in numeric form?
785 * @param timeout after how long should we give up?
786 * @param asc function to call on each string
787 * @param asc_cls closure for asc
790 tcp_plugin_address_pretty_printer (void *cls,
795 struct GNUNET_TIME_Relative timeout,
796 GNUNET_TRANSPORT_AddressStringCallback asc,
799 struct Plugin *plugin = cls;
800 const struct sockaddr_in *v4;
801 const struct sockaddr_in6 *v6;
802 struct PrettyPrinterContext *ppc;
804 if ((addrlen != sizeof (struct sockaddr_in)) &&
805 (addrlen != sizeof (struct sockaddr_in6)))
807 /* invalid address */
812 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
814 ppc->asc_cls = asc_cls;
815 if (addrlen == sizeof (struct sockaddr_in))
817 v4 = (const struct sockaddr_in *) addr;
818 ppc->port = ntohs (v4->sin_port);
822 v6 = (const struct sockaddr_in6 *) addr;
823 ppc->port = ntohs (v6->sin6_port);
826 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
830 !numeric, timeout, &append_port, ppc);
835 * Update the last-received and bandwidth quota values
838 * @param session session to update
839 * @param force set to GNUNET_YES if we should update even
840 * though the minimum refresh time has not yet expired
843 update_quota (struct Session *session, int force)
845 struct GNUNET_TIME_Absolute now;
846 unsigned long long delta;
847 unsigned long long total_allowed;
848 unsigned long long total_remaining;
850 now = GNUNET_TIME_absolute_get ();
851 delta = now.value - session->last_quota_update.value;
852 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
853 return; /* too early, not enough data */
855 total_allowed = session->quota_in * delta;
856 if (total_allowed > session->last_received)
858 /* got less than acceptable */
859 total_remaining = total_allowed - session->last_received;
860 session->last_received = 0;
861 delta = total_remaining / session->quota_in; /* bonus seconds */
862 if (delta > MAX_BANDWIDTH_CARRY)
863 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
867 /* got more than acceptable */
868 session->last_received -= total_allowed;
871 session->last_quota_update.value = now.value - delta;
876 * Set a quota for receiving data from the given peer; this is a
877 * per-transport limit. The transport should limit its read/select
878 * calls to stay below the quota (in terms of incoming data).
881 * @param target the peer for whom the quota is given
882 * @param quota_in quota for receiving/sending data in bytes per ms
885 tcp_plugin_set_receive_quota (void *cls,
886 const struct GNUNET_PeerIdentity *target,
889 struct Plugin *plugin = cls;
890 struct Session *session;
892 session = find_session_by_target (plugin, target);
894 return; /* peer must have disconnected, ignore */
895 if (session->quota_in != quota_in)
897 update_quota (session, GNUNET_YES);
898 if (session->quota_in > quota_in)
899 session->last_quota_update = GNUNET_TIME_absolute_get ();
900 session->quota_in = quota_in;
906 * Check if the given port is plausible (must be either
907 * our listen port or our advertised port). If it is
908 * neither, we return one of these two ports at random.
910 * @return either in_port or a more plausible port
913 check_port (struct Plugin *plugin, uint16_t in_port)
915 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
917 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
919 ? plugin->open_port : plugin->adv_port;
924 * Another peer has suggested an address for this peer and transport
925 * plugin. Check that this could be a valid address.
928 * @param addr pointer to the address
929 * @param addrlen length of addr
930 * @return GNUNET_OK if this is a plausible address for this peer
934 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
936 struct Plugin *plugin = cls;
937 char buf[sizeof (struct sockaddr_in6)];
938 struct sockaddr_in *v4;
939 struct sockaddr_in6 *v6;
941 if ((addrlen != sizeof (struct sockaddr_in)) &&
942 (addrlen != sizeof (struct sockaddr_in6)))
945 return GNUNET_SYSERR;
947 memcpy (buf, addr, sizeof (struct sockaddr_in6));
948 if (addrlen == sizeof (struct sockaddr_in))
950 v4 = (struct sockaddr_in *) buf;
951 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
955 v6 = (struct sockaddr_in6 *) buf;
956 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
959 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
961 "Informing transport service about my address `%s'.\n",
962 GNUNET_a2s (addr, addrlen));
969 * We've received a welcome from this peer via TCP. Possibly create a
970 * fresh client record and send back our welcome.
973 * @param client identification of the client
974 * @param message the actual message
977 handle_tcp_welcome (void *cls,
978 struct GNUNET_SERVER_Client *client,
979 const struct GNUNET_MessageHeader *message)
981 struct Plugin *plugin = cls;
982 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
983 struct Session *session;
988 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
990 "Received `%s' message from a `%4s/%p'.\n", "WELCOME",
991 GNUNET_i2s (&wm->clientIdentity), client);
994 session = find_session_by_client (plugin, client);
997 GNUNET_SERVER_client_keep (client);
998 session = create_session (plugin,
999 &wm->clientIdentity, client);
1001 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1004 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1006 "Found address for incoming `%s' message\n",
1009 session->connect_addr = vaddr;
1010 session->connect_alen = alen;
1015 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1017 "Didn't find address for incoming `%s' message\n",
1022 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1024 "Creating new session %p for incoming `%s' message.\n",
1025 session, "WELCOME");
1027 process_pending_messages (session);
1029 if (session->expecting_welcome != GNUNET_YES)
1031 GNUNET_break_op (0);
1032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1035 session->expecting_welcome = GNUNET_NO;
1036 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1041 * Calculate how long we should delay reading from the TCP socket to
1042 * ensure that we stay within our bandwidth limits (push back).
1044 * @param session for which client should this be calculated
1046 static struct GNUNET_TIME_Relative
1047 calculate_throttle_delay (struct Session *session)
1049 struct GNUNET_TIME_Relative ret;
1050 struct GNUNET_TIME_Absolute now;
1055 now = GNUNET_TIME_absolute_get ();
1056 del = now.value - session->last_quota_update.value;
1057 if (del > MAX_BANDWIDTH_CARRY)
1059 update_quota (session, GNUNET_YES);
1060 del = now.value - session->last_quota_update.value;
1061 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1063 if (session->quota_in == 0)
1064 session->quota_in = 1; /* avoid divison by zero */
1065 avail = del * session->quota_in;
1066 if (avail > session->last_received)
1067 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1068 excess = session->last_received - avail;
1069 ret.value = excess / session->quota_in;
1075 * Task to signal the server that we can continue
1076 * receiving from the TCP client now.
1079 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1081 struct Session *session = cls;
1082 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1087 * We've received data for this peer via TCP. Unbox,
1088 * compute latency and forward.
1090 * @param cls closure
1091 * @param client identification of the client
1092 * @param message the actual message
1095 handle_tcp_data (void *cls,
1096 struct GNUNET_SERVER_Client *client,
1097 const struct GNUNET_MessageHeader *message)
1099 struct Plugin *plugin = cls;
1100 struct Session *session;
1102 struct GNUNET_TIME_Relative delay;
1104 msize = ntohs (message->size);
1105 session = find_session_by_client (plugin, client);
1107 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
1110 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1111 "tcp", "Received a welcome, NOT sending to clients!\n");
1113 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1114 return; /* We don't want to propagate WELCOME messages up! */
1119 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1120 "tcp", "Received DATA message, checking session!\n");
1124 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1126 GNUNET_break_op (0);
1127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1131 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1132 "tcp", "Receiving %u bytes from `%4s'.\n",
1133 msize, GNUNET_i2s (&session->target));
1136 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1138 "Forwarding %u bytes of data of type %u to transport service.\n",
1139 (unsigned int) msize,
1140 (unsigned int) ntohs (message->type));
1142 plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1143 session->connect_addr,
1144 session->connect_alen);
1145 /* update bandwidth used */
1146 session->last_received += msize;
1147 update_quota (session, GNUNET_NO);
1148 delay = calculate_throttle_delay (session);
1149 if (delay.value == 0)
1150 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1152 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1153 delay, &delayed_done, session);
1158 * Handlers for the various TCP messages.
1160 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1161 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1162 sizeof (struct WelcomeMessage)},
1163 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1169 create_tcp_handlers (struct Plugin *plugin)
1172 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1173 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1176 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1178 plugin->handlers[i].callback_cls = plugin;
1179 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1184 * Functions with this signature are called whenever a peer
1185 * is disconnected on the network level.
1187 * @param cls closure
1188 * @param client identification of the client
1191 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1193 struct Plugin *plugin = cls;
1194 struct Session *session;
1198 session = find_session_by_client (plugin, client);
1199 if (session == NULL)
1200 return; /* unknown, nothing to do */
1202 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1204 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1205 GNUNET_i2s (&session->target),
1206 (session->connect_addr != NULL) ?
1207 GNUNET_a2s (session->connect_addr,
1208 session->connect_alen) : "*", client);
1210 disconnect_session (session);
1215 * Add the IP of our network interface to the list of
1216 * our external IP addresses.
1219 process_interfaces (void *cls,
1222 const struct sockaddr *addr, socklen_t addrlen)
1224 struct Plugin *plugin = cls;
1226 struct sockaddr_in *v4;
1227 struct sockaddr_in6 *v6;
1229 af = addr->sa_family;
1232 v4 = (struct sockaddr_in *) addr;
1233 v4->sin_port = htons (plugin->adv_port);
1237 GNUNET_assert (af == AF_INET6);
1238 v6 = (struct sockaddr_in6 *) addr;
1239 v6->sin6_port = htons (plugin->adv_port);
1241 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1242 GNUNET_ERROR_TYPE_BULK,
1243 "tcp", _("Found address `%s' (%s)\n"),
1244 GNUNET_a2s (addr, addrlen), name);
1245 plugin->env->notify_address (plugin->env->cls,
1247 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1253 * Function called by the resolver for each address obtained from DNS
1254 * for our own hostname. Add the addresses to the list of our
1255 * external IP addresses.
1257 * @param cls closure
1258 * @param addr one of the addresses of the host, NULL for the last address
1259 * @param addrlen length of the address
1262 process_hostname_ips (void *cls,
1263 const struct sockaddr *addr, socklen_t addrlen)
1265 struct Plugin *plugin = cls;
1269 plugin->hostname_dns = NULL;
1272 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1277 * Entry point for the plugin.
1280 libgnunet_plugin_transport_tcp_init (void *cls)
1282 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1283 struct GNUNET_TRANSPORT_PluginFunctions *api;
1284 struct Plugin *plugin;
1285 struct GNUNET_SERVICE_Context *service;
1286 unsigned long long aport;
1287 unsigned long long bport;
1289 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1290 if (service == NULL)
1292 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1295 ("Failed to start service for `%s' transport plugin.\n"),
1301 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1307 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1310 &aport)) && (aport > 65535)))
1312 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1315 ("Require valid port number for service `%s' in configuration!\n"),
1317 GNUNET_SERVICE_stop (service);
1322 plugin = GNUNET_malloc (sizeof (struct Plugin));
1323 plugin->open_port = bport;
1324 plugin->adv_port = aport;
1326 plugin->lsock = NULL;
1327 plugin->statistics = NULL;
1328 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1330 api->send = &tcp_plugin_send;
1331 api->disconnect = &tcp_plugin_disconnect;
1332 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1333 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1334 api->check_address = &tcp_plugin_check_address;
1335 plugin->service = service;
1336 plugin->server = GNUNET_SERVICE_get_server (service);
1337 create_tcp_handlers (plugin);
1338 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1339 "tcp", _("TCP transport listening on port %llu\n"), bport);
1341 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1344 ("TCP transport advertises itself as being on port %llu\n"),
1346 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1348 /* FIXME: do the two calls below periodically again and
1349 not just once (since the info we get might change...) */
1350 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1351 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1354 HOSTNAME_RESOLVE_TIMEOUT,
1355 &process_hostname_ips,
1362 * Exit point from the plugin.
1365 libgnunet_plugin_transport_tcp_done (void *cls)
1367 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1368 struct Plugin *plugin = api->cls;
1369 struct Session *session;
1371 while (NULL != (session = plugin->sessions))
1372 disconnect_session (session);
1373 if (NULL != plugin->hostname_dns)
1375 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1376 plugin->hostname_dns = NULL;
1378 GNUNET_SERVICE_stop (plugin->service);
1379 GNUNET_free (plugin->handlers);
1380 GNUNET_free (plugin);
1385 /* end of plugin_transport_tcp.c */