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;
96 * So that the gnunet-service-transport 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) + sizeof (struct WelcomeMessage));
308 pm->msg = (const char*) &pm[1];
309 pm->message_size = sizeof (struct WelcomeMessage);
310 welcome.header.size = htons (sizeof (struct WelcomeMessage));
311 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
312 welcome.clientIdentity = *plugin->env->my_identity;
313 memcpy (&pm[1], &welcome, sizeof (welcome));
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 if (session->client != NULL)
420 process_pending_messages (session);
422 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
423 "tcp", "Transmitting %u bytes\n", ret);
430 * If we have pending messages, ask the server to
431 * transmit them (schedule the respective tasks, etc.)
433 * @param session for which session should we do this
436 process_pending_messages (struct Session *session)
438 struct PendingMessage *pm;
439 GNUNET_assert (session->client != NULL);
440 if (session->transmit_handle != NULL)
442 if (NULL == (pm = session->pending_messages))
444 session->transmit_handle
445 = GNUNET_SERVER_notify_transmit_ready (session->client,
447 GNUNET_TIME_absolute_get_remaining
449 &do_transmit, session);
454 * Functions with this signature are called whenever we need
455 * to close a session due to a disconnect or failure to
456 * establish a connection.
458 * @param session session to close down
461 disconnect_session (struct Session *session)
463 struct Session *prev;
465 struct PendingMessage *pm;
468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
470 "Disconnecting from `%4s' at %s (session %p).\n",
471 GNUNET_i2s (&session->target),
472 (session->connect_addr != NULL) ?
473 GNUNET_a2s (session->connect_addr,
474 session->connect_alen) : "*", session);
476 /* remove from session list */
478 pos = session->plugin->sessions;
479 while (pos != session)
485 session->plugin->sessions = session->next;
487 prev->next = session->next;
489 if (session->transmit_handle != NULL)
491 GNUNET_CONNECTION_notify_transmit_ready_cancel
492 (session->transmit_handle);
493 session->transmit_handle = NULL;
495 while (NULL != (pm = session->pending_messages))
498 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
500 pm->transmit_cont != NULL
501 ? "Could not deliver message to `%4s'.\n"
503 "Could not deliver message to `%4s', notifying.\n",
504 GNUNET_i2s (&session->target));
506 session->pending_messages = pm->next;
507 if (NULL != pm->transmit_cont)
508 pm->transmit_cont (pm->transmit_cont_cls,
509 &session->target, GNUNET_SYSERR);
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) + msgbuf_size);
663 pm->msg = (const char*) &pm[1];
664 memcpy (&pm[1], 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 session = plugin->sessions;
726 while (NULL != session)
728 if (0 == memcmp (target,
730 sizeof (struct GNUNET_PeerIdentity)))
732 pm = session->pending_messages;
735 pm->transmit_cont = NULL;
736 pm->transmit_cont_cls = NULL;
739 if (session->client != NULL)
741 GNUNET_SERVER_client_drop (session->client);
742 session->client = NULL;
744 /* rest of the clean-up of the session will be done as part of
745 disconnect_notify which should be triggered any time now
746 (or which may be triggering this call in the first place) */
748 session = session->next;
753 struct PrettyPrinterContext
755 GNUNET_TRANSPORT_AddressStringCallback asc;
762 * Append our port and forward the result.
765 append_port (void *cls, const char *hostname)
767 struct PrettyPrinterContext *ppc = cls;
770 if (hostname == NULL)
772 ppc->asc (ppc->asc_cls, NULL);
776 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
777 ppc->asc (ppc->asc_cls, ret);
783 * Convert the transports address to a nice, human-readable
787 * @param type name of the transport that generated the address
788 * @param addr one of the addresses of the host, NULL for the last address
789 * the specific address format depends on the transport
790 * @param addrlen length of the address
791 * @param numeric should (IP) addresses be displayed in numeric form?
792 * @param timeout after how long should we give up?
793 * @param asc function to call on each string
794 * @param asc_cls closure for asc
797 tcp_plugin_address_pretty_printer (void *cls,
802 struct GNUNET_TIME_Relative timeout,
803 GNUNET_TRANSPORT_AddressStringCallback asc,
806 struct Plugin *plugin = cls;
807 const struct sockaddr_in *v4;
808 const struct sockaddr_in6 *v6;
809 struct PrettyPrinterContext *ppc;
811 if ((addrlen != sizeof (struct sockaddr_in)) &&
812 (addrlen != sizeof (struct sockaddr_in6)))
814 /* invalid address */
819 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
821 ppc->asc_cls = asc_cls;
822 if (addrlen == sizeof (struct sockaddr_in))
824 v4 = (const struct sockaddr_in *) addr;
825 ppc->port = ntohs (v4->sin_port);
829 v6 = (const struct sockaddr_in6 *) addr;
830 ppc->port = ntohs (v6->sin6_port);
833 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
837 !numeric, timeout, &append_port, ppc);
842 * Update the last-received and bandwidth quota values
845 * @param session session to update
846 * @param force set to GNUNET_YES if we should update even
847 * though the minimum refresh time has not yet expired
850 update_quota (struct Session *session, int force)
852 struct GNUNET_TIME_Absolute now;
853 unsigned long long delta;
854 unsigned long long total_allowed;
855 unsigned long long total_remaining;
857 now = GNUNET_TIME_absolute_get ();
858 delta = now.value - session->last_quota_update.value;
859 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
860 return; /* too early, not enough data */
862 total_allowed = session->quota_in * delta;
863 if (total_allowed > session->last_received)
865 /* got less than acceptable */
866 total_remaining = total_allowed - session->last_received;
867 session->last_received = 0;
868 delta = total_remaining / session->quota_in; /* bonus seconds */
869 if (delta > MAX_BANDWIDTH_CARRY)
870 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
874 /* got more than acceptable */
875 session->last_received -= total_allowed;
878 session->last_quota_update.value = now.value - delta;
883 * Set a quota for receiving data from the given peer; this is a
884 * per-transport limit. The transport should limit its read/select
885 * calls to stay below the quota (in terms of incoming data).
888 * @param target the peer for whom the quota is given
889 * @param quota_in quota for receiving/sending data in bytes per ms
892 tcp_plugin_set_receive_quota (void *cls,
893 const struct GNUNET_PeerIdentity *target,
896 struct Plugin *plugin = cls;
897 struct Session *session;
899 session = find_session_by_target (plugin, target);
901 return; /* peer must have disconnected, ignore */
902 if (session->quota_in != quota_in)
904 update_quota (session, GNUNET_YES);
905 if (session->quota_in > quota_in)
906 session->last_quota_update = GNUNET_TIME_absolute_get ();
907 session->quota_in = quota_in;
913 * Check if the given port is plausible (must be either
914 * our listen port or our advertised port). If it is
915 * neither, we return one of these two ports at random.
917 * @return either in_port or a more plausible port
920 check_port (struct Plugin *plugin, uint16_t in_port)
922 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
924 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
926 ? plugin->open_port : plugin->adv_port;
931 * Another peer has suggested an address for this peer and transport
932 * plugin. Check that this could be a valid address.
935 * @param addr pointer to the address
936 * @param addrlen length of addr
937 * @return GNUNET_OK if this is a plausible address for this peer
941 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
943 struct Plugin *plugin = cls;
944 char buf[sizeof (struct sockaddr_in6)];
945 struct sockaddr_in *v4;
946 struct sockaddr_in6 *v6;
948 if ((addrlen != sizeof (struct sockaddr_in)) &&
949 (addrlen != sizeof (struct sockaddr_in6)))
952 return GNUNET_SYSERR;
954 memcpy (buf, addr, sizeof (struct sockaddr_in6));
955 if (addrlen == sizeof (struct sockaddr_in))
957 v4 = (struct sockaddr_in *) buf;
958 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
962 v6 = (struct sockaddr_in6 *) buf;
963 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
966 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
968 "Informing transport service about my address `%s'.\n",
969 GNUNET_a2s (addr, addrlen));
976 * We've received a welcome from this peer via TCP. Possibly create a
977 * fresh client record and send back our welcome.
980 * @param client identification of the client
981 * @param message the actual message
984 handle_tcp_welcome (void *cls,
985 struct GNUNET_SERVER_Client *client,
986 const struct GNUNET_MessageHeader *message)
988 struct Plugin *plugin = cls;
989 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
990 struct Session *session;
995 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
997 "Received `%s' message from a `%4s/%p'.\n", "WELCOME",
998 GNUNET_i2s (&wm->clientIdentity), client);
1001 session = find_session_by_client (plugin, client);
1002 if (session == NULL)
1004 GNUNET_SERVER_client_keep (client);
1005 session = create_session (plugin,
1006 &wm->clientIdentity, client);
1008 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1011 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1013 "Found address for incoming `%s' message\n",
1016 session->connect_addr = vaddr;
1017 session->connect_alen = alen;
1022 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1024 "Didn't find address for incoming `%s' message\n",
1029 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1031 "Creating new session %p for incoming `%s' message.\n",
1032 session, "WELCOME");
1034 process_pending_messages (session);
1036 if (session->expecting_welcome != GNUNET_YES)
1038 GNUNET_break_op (0);
1039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1042 session->expecting_welcome = GNUNET_NO;
1043 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1048 * Calculate how long we should delay reading from the TCP socket to
1049 * ensure that we stay within our bandwidth limits (push back).
1051 * @param session for which client should this be calculated
1053 static struct GNUNET_TIME_Relative
1054 calculate_throttle_delay (struct Session *session)
1056 struct GNUNET_TIME_Relative ret;
1057 struct GNUNET_TIME_Absolute now;
1062 now = GNUNET_TIME_absolute_get ();
1063 del = now.value - session->last_quota_update.value;
1064 if (del > MAX_BANDWIDTH_CARRY)
1066 update_quota (session, GNUNET_YES);
1067 del = now.value - session->last_quota_update.value;
1068 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1070 if (session->quota_in == 0)
1071 session->quota_in = 1; /* avoid divison by zero */
1072 avail = del * session->quota_in;
1073 if (avail > session->last_received)
1074 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1075 excess = session->last_received - avail;
1076 ret.value = excess / session->quota_in;
1082 * Task to signal the server that we can continue
1083 * receiving from the TCP client now.
1086 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1088 struct Session *session = cls;
1089 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1094 * We've received data for this peer via TCP. Unbox,
1095 * compute latency and forward.
1097 * @param cls closure
1098 * @param client identification of the client
1099 * @param message the actual message
1102 handle_tcp_data (void *cls,
1103 struct GNUNET_SERVER_Client *client,
1104 const struct GNUNET_MessageHeader *message)
1106 struct Plugin *plugin = cls;
1107 struct Session *session;
1109 struct GNUNET_TIME_Relative delay;
1111 msize = ntohs (message->size);
1112 session = find_session_by_client (plugin, client);
1114 if (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == ntohs(message->type))
1117 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1118 "tcp", "Received a welcome, NOT sending to clients!\n");
1120 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1121 return; /* We don't want to propagate WELCOME messages up! */
1124 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1125 "tcp", "Received DATA message, checking session!\n");
1127 if ( (NULL == session) || (GNUNET_NO != session->expecting_welcome))
1129 GNUNET_break_op (0);
1130 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1134 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1135 "tcp", "Receiving %u bytes from `%4s'.\n",
1136 msize, GNUNET_i2s (&session->target));
1139 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1141 "Forwarding %u bytes of data of type %u to transport service.\n",
1142 (unsigned int) msize,
1143 (unsigned int) ntohs (message->type));
1145 plugin->env->receive (plugin->env->cls, &session->target, message, 1,
1146 session->connect_addr,
1147 session->connect_alen);
1148 /* update bandwidth used */
1149 session->last_received += msize;
1150 update_quota (session, GNUNET_NO);
1151 delay = calculate_throttle_delay (session);
1152 if (delay.value == 0)
1153 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1155 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1156 delay, &delayed_done, session);
1161 * Handlers for the various TCP messages.
1163 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1164 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1165 sizeof (struct WelcomeMessage)},
1166 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1172 create_tcp_handlers (struct Plugin *plugin)
1175 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1176 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1179 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1181 plugin->handlers[i].callback_cls = plugin;
1182 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1187 * Functions with this signature are called whenever a peer
1188 * is disconnected on the network level.
1190 * @param cls closure
1191 * @param client identification of the client
1194 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1196 struct Plugin *plugin = cls;
1197 struct Session *session;
1201 session = find_session_by_client (plugin, client);
1202 if (session == NULL)
1203 return; /* unknown, nothing to do */
1205 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1207 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1208 GNUNET_i2s (&session->target),
1209 (session->connect_addr != NULL) ?
1210 GNUNET_a2s (session->connect_addr,
1211 session->connect_alen) : "*", client);
1213 disconnect_session (session);
1218 * Add the IP of our network interface to the list of
1219 * our external IP addresses.
1222 process_interfaces (void *cls,
1225 const struct sockaddr *addr, socklen_t addrlen)
1227 struct Plugin *plugin = cls;
1229 struct sockaddr_in *v4;
1230 struct sockaddr_in6 *v6;
1232 af = addr->sa_family;
1235 v4 = (struct sockaddr_in *) addr;
1236 v4->sin_port = htons (plugin->adv_port);
1240 GNUNET_assert (af == AF_INET6);
1241 v6 = (struct sockaddr_in6 *) addr;
1242 v6->sin6_port = htons (plugin->adv_port);
1244 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1245 GNUNET_ERROR_TYPE_BULK,
1246 "tcp", _("Found address `%s' (%s)\n"),
1247 GNUNET_a2s (addr, addrlen), name);
1248 plugin->env->notify_address (plugin->env->cls,
1250 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1256 * Function called by the resolver for each address obtained from DNS
1257 * for our own hostname. Add the addresses to the list of our
1258 * external IP addresses.
1260 * @param cls closure
1261 * @param addr one of the addresses of the host, NULL for the last address
1262 * @param addrlen length of the address
1265 process_hostname_ips (void *cls,
1266 const struct sockaddr *addr, socklen_t addrlen)
1268 struct Plugin *plugin = cls;
1272 plugin->hostname_dns = NULL;
1275 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1280 * Entry point for the plugin.
1283 libgnunet_plugin_transport_tcp_init (void *cls)
1285 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1286 struct GNUNET_TRANSPORT_PluginFunctions *api;
1287 struct Plugin *plugin;
1288 struct GNUNET_SERVICE_Context *service;
1289 unsigned long long aport;
1290 unsigned long long bport;
1292 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1293 if (service == NULL)
1295 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1298 ("Failed to start service for `%s' transport plugin.\n"),
1304 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1310 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1313 &aport)) && (aport > 65535)))
1315 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1318 ("Require valid port number for service `%s' in configuration!\n"),
1320 GNUNET_SERVICE_stop (service);
1325 plugin = GNUNET_malloc (sizeof (struct Plugin));
1326 plugin->open_port = bport;
1327 plugin->adv_port = aport;
1329 plugin->lsock = NULL;
1330 plugin->statistics = NULL;
1331 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1333 api->send = &tcp_plugin_send;
1334 api->disconnect = &tcp_plugin_disconnect;
1335 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1336 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1337 api->check_address = &tcp_plugin_check_address;
1338 plugin->service = service;
1339 plugin->server = GNUNET_SERVICE_get_server (service);
1340 create_tcp_handlers (plugin);
1341 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1342 "tcp", _("TCP transport listening on port %llu\n"), bport);
1344 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1347 ("TCP transport advertises itself as being on port %llu\n"),
1349 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1351 /* FIXME: do the two calls below periodically again and
1352 not just once (since the info we get might change...) */
1353 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1354 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1357 HOSTNAME_RESOLVE_TIMEOUT,
1358 &process_hostname_ips,
1365 * Exit point from the plugin.
1368 libgnunet_plugin_transport_tcp_done (void *cls)
1370 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1371 struct Plugin *plugin = api->cls;
1372 struct Session *session;
1374 while (NULL != (session = plugin->sessions))
1375 disconnect_session (session);
1376 if (NULL != plugin->hostname_dns)
1378 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1379 plugin->hostname_dns = NULL;
1381 GNUNET_SERVICE_stop (plugin->service);
1382 GNUNET_free (plugin->handlers);
1383 GNUNET_free (plugin);
1388 /* end of plugin_transport_tcp.c */