2 This file is part of GNUnet.
3 (C) 2010,2011 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 3, 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/gnunet-service-transport.c
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_ats_service.h"
33 #include "gnunet-service-transport.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet-service-transport_clients.h"
36 #include "gnunet-service-transport_hello.h"
37 #include "gnunet-service-transport_neighbours.h"
38 #include "gnunet-service-transport_plugins.h"
39 #include "gnunet-service-transport_validation.h"
40 #include "gnunet-service-transport_manipulation.h"
41 #include "transport.h"
44 * Information we need for an asynchronous session kill.
51 struct SessionKiller *next;
56 struct SessionKiller *prev;
61 struct Session *session;
64 * Plugin for the session.
66 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
71 struct GNUNET_SCHEDULER_Task * task;
74 struct BlacklistCheckContext
76 struct BlacklistCheckContext *prev;
77 struct BlacklistCheckContext *next;
80 struct GST_BlacklistCheck *blc;
82 struct GNUNET_HELLO_Address *address;
83 struct Session *session;
84 struct GNUNET_MessageHeader *msg;
85 struct GNUNET_ATS_Information *ats;
94 struct GNUNET_STATISTICS_Handle *GST_stats;
97 * Configuration handle.
99 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
102 * Configuration handle.
104 struct GNUNET_PeerIdentity GST_my_identity;
107 * Handle to peerinfo service.
109 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
112 * Handle to our service's server.
114 static struct GNUNET_SERVER_Handle *GST_server;
119 struct GNUNET_CRYPTO_EddsaPrivateKey *GST_my_private_key;
124 struct GNUNET_ATS_SchedulingHandle *GST_ats;
127 * Hello address expiration
129 struct GNUNET_TIME_Relative hello_expiration;
132 * DEBUGGING connection counter
134 static int connections;
137 * Head of DLL of asynchronous tasks to kill sessions.
139 static struct SessionKiller *sk_head;
142 * Tail of DLL of asynchronous tasks to kill sessions.
144 static struct SessionKiller *sk_tail;
146 struct BlacklistCheckContext *bc_head;
147 struct BlacklistCheckContext *bc_tail;
151 * Transmit our HELLO message to the given (connected) neighbour.
153 * @param cls the 'HELLO' message
154 * @param target a connected neighbour
155 * @param address the address
156 * @param state current state this peer is in
157 * @param state_timeout timeout for the current state of the peer
158 * @param bandwidth_in inbound quota in NBO
159 * @param bandwidth_out outbound quota in NBO
162 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
163 const struct GNUNET_HELLO_Address *address,
164 enum GNUNET_TRANSPORT_PeerState state,
165 struct GNUNET_TIME_Absolute state_timeout,
166 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
167 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
169 const struct GNUNET_MessageHeader *hello = cls;
171 if (GNUNET_NO == GST_neighbours_test_connected (target))
174 GST_neighbours_send (target, hello, ntohs (hello->size), hello_expiration,
179 * My HELLO has changed. Tell everyone who should know.
182 * @param hello new HELLO
185 process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
187 GST_clients_broadcast (hello, GNUNET_NO);
188 GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
192 * We received some payload. Prepare to pass it on to our clients.
194 * @param peer (claimed) identity of the other peer
195 * @param address the address
196 * @param session session used
197 * @param message the message to process
198 * @return how long the plugin should wait until receiving more data
200 static struct GNUNET_TIME_Relative
201 process_payload (const struct GNUNET_PeerIdentity *peer,
202 const struct GNUNET_HELLO_Address *address, struct Session *session,
203 const struct GNUNET_MessageHeader *message)
205 struct GNUNET_TIME_Relative ret;
207 struct InboundMessage *im;
208 size_t msg_size = ntohs (message->size);
209 size_t size = sizeof(struct InboundMessage) + msg_size;
210 char buf[size] GNUNET_ALIGN;
212 do_forward = GNUNET_SYSERR;
213 ret = GST_neighbours_calculate_receive_delay (peer, msg_size, &do_forward);
214 if (!GST_neighbours_test_connected (peer))
216 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
217 "Discarded %u bytes type %u payload from peer `%s'\n", msg_size,
218 ntohs (message->type), GNUNET_i2s (peer));
219 GNUNET_STATISTICS_update (GST_stats, gettext_noop
220 ("# bytes payload discarded due to not connected peer"), msg_size,
225 GST_ats_add_address (address, session, NULL, 0);
227 if (GNUNET_YES != do_forward)
229 im = (struct InboundMessage *) buf;
230 im->header.size = htons (size);
231 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
233 memcpy (&im[1], message, ntohs (message->size));
234 GST_clients_broadcast (&im->header, GNUNET_YES);
239 * Task to asynchronously terminate a session.
241 * @param cls the `struct SessionKiller` with the information for the kill
242 * @param tc scheduler context
245 kill_session_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
247 struct SessionKiller *sk = cls;
250 GNUNET_CONTAINER_DLL_remove(sk_head, sk_tail, sk);
251 sk->plugin->disconnect_session (sk->plugin->cls, sk->session);
256 cancel_pending_blacklist_checks (const struct GNUNET_HELLO_Address *address, struct Session *session)
258 struct BlacklistCheckContext *blctx;
259 struct BlacklistCheckContext *next;
261 for (blctx = next; NULL != blctx; blctx = next)
264 if ((NULL != blctx->address) && (0 == GNUNET_HELLO_address_cmp(blctx->address, address)) && (blctx->session == session))
266 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, blctx);
267 if (NULL != blctx->blc)
269 GST_blacklist_test_cancel (blctx->blc);
272 GNUNET_HELLO_address_free (blctx->address);
273 GNUNET_free_non_null (blctx->msg);
274 GNUNET_free_non_null (blctx->ats);
281 * Force plugin to terminate session due to communication
284 * @param plugin_name name of the plugin
285 * @param session session to termiante
288 kill_session (const char *plugin_name, struct Session *session)
290 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
291 struct SessionKiller *sk;
293 for (sk = sk_head; NULL != sk; sk = sk->next)
294 if (sk->session == session)
296 plugin = GST_plugins_find (plugin_name);
302 /* need to issue disconnect asynchronously */
303 sk = GNUNET_new (struct SessionKiller);
304 sk->session = session;
306 sk->task = GNUNET_SCHEDULER_add_now (&kill_session_task, sk);
307 GNUNET_CONTAINER_DLL_insert(sk_head, sk_tail, sk);
313 * Black list check result for try_connect call
314 * If connection to the peer is allowed request adddress and
316 * @param cls blc_ctx bl context
317 * @param peer the peer
318 * @param result the result
321 connect_bl_check_cont (void *cls,
322 const struct GNUNET_PeerIdentity *peer, int result)
324 struct BlacklistCheckContext *blctx = cls;
326 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, blctx);
329 if (GNUNET_OK == result)
331 /* Blacklist allows to speak to this peer, forward SYN to neighbours */
332 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
333 "Received SYN message from peer `%s' with `%s' %p\n",
334 GNUNET_i2s (peer), GST_plugins_a2s (blctx->address), blctx->session);
336 if (GNUNET_OK != GST_neighbours_handle_session_syn (blctx->msg,
337 &blctx->address->peer))
339 cancel_pending_blacklist_checks (blctx->address, blctx->session);
340 kill_session (blctx->address->transport_name, blctx->session);
345 /* Blacklist denies to speak to this peer */
347 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
348 "Discarding SYN message from `%s' due to denied blacklist check\n",
350 cancel_pending_blacklist_checks (blctx->address, blctx->session);
351 kill_session (blctx->address->transport_name, blctx->session);
354 if (NULL != blctx->address)
355 GNUNET_HELLO_address_free (blctx->address);
356 GNUNET_free (blctx->msg);
361 * Black list check result for try_connect call
362 * If connection to the peer is allowed request adddress and
364 * @param cls blc_ctx bl context
365 * @param peer the peer
366 * @param result the result
369 connect_transport_bl_check_cont (void *cls,
370 const struct GNUNET_PeerIdentity *peer, int result)
372 struct BlacklistCheckContext *blctx = cls;
374 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, blctx);
377 if (GNUNET_OK == result)
379 /* Blacklist allows to speak to this transport */
380 GST_ats_add_address(blctx->address, blctx->session, blctx->ats, blctx->ats_count);
383 if (NULL != blctx->address)
384 GNUNET_HELLO_address_free (blctx->address);
385 GNUNET_free (blctx->msg);
391 * Function called by the transport for each received message.
393 * @param cls closure, const char* with the name of the plugin we received the message from
394 * @param address address and (claimed) identity of the other peer
395 * @param message the message, NULL if we only care about
396 * learning about the delay until we should receive again
397 * @param session identifier used for this session (NULL for plugins
398 * that do not offer bi-directional communication to the sender
399 * using the same "connection")
400 * @return how long the plugin should wait until receiving more data
401 * (plugins that do not support this, can ignore the return value)
403 struct GNUNET_TIME_Relative
404 GST_receive_callback (void *cls,
405 const struct GNUNET_HELLO_Address *address,
406 struct Session *session,
407 const struct GNUNET_MessageHeader *message)
409 const char *plugin_name = cls;
410 struct GNUNET_TIME_Relative ret;
411 struct BlacklistCheckContext *blctx;
412 struct GST_BlacklistCheck *blc;
415 ret = GNUNET_TIME_UNIT_ZERO;
418 type = ntohs (message->type);
419 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
420 "Received Message with type %u from peer `%s'\n", type,
421 GNUNET_i2s (&address->peer));
423 GNUNET_STATISTICS_update (GST_stats, gettext_noop
424 ("# bytes total received"), ntohs (message->size), GNUNET_NO);
425 GST_neighbours_notify_data_recv (&address->peer, address, session, message);
429 case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
430 /* Legacy HELLO message, discard */
432 case GNUNET_MESSAGE_TYPE_HELLO:
433 if (GNUNET_OK != GST_validation_handle_hello (message))
436 cancel_pending_blacklist_checks (address, session);
439 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
440 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
441 "Processing `%s' from `%s'\n", "PING", GST_plugins_a2s (address));
443 != GST_validation_handle_ping (&address->peer, message, address, session))
445 cancel_pending_blacklist_checks (address, session);
446 kill_session (plugin_name, session);
449 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
450 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
451 "Processing `%s' from `%s'\n", "PONG",
452 GST_plugins_a2s (address));
453 if (GNUNET_OK != GST_validation_handle_pong (&address->peer, message))
456 cancel_pending_blacklist_checks (address, session);
457 kill_session (plugin_name, session);
460 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN:
461 /* Do blacklist check if communication with this peer is allowed */
462 blctx = GNUNET_new (struct BlacklistCheckContext);
463 blctx->address = GNUNET_HELLO_address_copy (address);
464 blctx->session = session;
465 blctx->msg = GNUNET_malloc (ntohs(message->size));
466 memcpy (blctx->msg, message, ntohs(message->size));
467 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, blctx);
468 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer, NULL,
469 &connect_bl_check_cont, blctx)))
474 blctx = GNUNET_new (struct BlacklistCheckContext);
475 blctx->address = GNUNET_HELLO_address_copy (address);
476 blctx->session = session;
477 blctx->msg = GNUNET_malloc (ntohs(message->size));
478 memcpy (blctx->msg, message, ntohs(message->size));
479 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, blctx);
480 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
481 address->transport_name, &connect_transport_bl_check_cont, blctx)))
486 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK:
487 if (GNUNET_OK != GST_neighbours_handle_session_syn_ack (message,
488 &address->peer, address, session))
490 cancel_pending_blacklist_checks (address, session);
491 kill_session (plugin_name, session);
494 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
496 != GST_neighbours_handle_session_ack (message, &address->peer, address, session))
499 cancel_pending_blacklist_checks (address, session);
500 kill_session (plugin_name, session);
503 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
504 GST_neighbours_handle_disconnect_message (&address->peer, message);
506 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
507 GST_neighbours_keepalive (&address->peer, message);
509 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
510 GST_neighbours_keepalive_response (&address->peer, message);
513 /* should be payload */
514 GNUNET_STATISTICS_update (GST_stats, gettext_noop
515 ("# bytes payload received"), ntohs (message->size), GNUNET_NO);
516 GST_neighbours_notify_payload_recv (&address->peer, address, session, message);
517 ret = process_payload (&address->peer, address, session, message);
521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
522 "Allowing receive from peer %s to continue in %s\n", GNUNET_i2s (&address->peer),
523 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
528 * Function that will be called for each address the transport
529 * is aware that it might be reachable under. Update our HELLO.
531 * @param cls name of the plugin (const char*)
532 * @param add_remove should the address added (YES) or removed (NO) from the
533 * set of valid addresses?
534 * @param address the address to add or remove
537 plugin_env_address_change_notification (void *cls, int add_remove,
538 const struct GNUNET_HELLO_Address *address)
540 static int addresses = 0;
541 struct GNUNET_STATISTICS_Handle *cfg = GST_stats;
543 if (GNUNET_YES == add_remove)
546 GNUNET_STATISTICS_update (cfg, "# transport addresses", 1, GNUNET_NO);
548 else if (GNUNET_NO == add_remove)
555 GNUNET_STATISTICS_update (cfg, "# transport addresses", -1, GNUNET_NO);
559 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
560 "Transport now has %u addresses to communicate\n", addresses);
562 GST_hello_modify_addresses (add_remove, address);
566 * Function that will be called whenever the plugin internally
567 * cleans up a session pointer and hence the service needs to
568 * discard all of those sessions as well. Plugins that do not
569 * use sessions can simply omit calling this function and always
570 * use NULL wherever a session pointer is needed. This function
571 * should be called BEFORE a potential "TransmitContinuation"
572 * from the "TransmitFunction".
575 * @param address which address was the session for
576 * @param session which session is being destoyed
579 plugin_env_session_end (void *cls, const struct GNUNET_HELLO_Address *address,
580 struct Session *session)
582 struct SessionKiller *sk;
596 GNUNET_assert(strlen (address->transport_name) > 0);
597 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Session %p to peer `%s' ended \n",
598 session, GNUNET_i2s (&address->peer));
600 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
601 "Notification from plugin `%s' about terminated %s session %p from peer `%s' address `%s'\n",
602 address->transport_name,
603 GNUNET_HELLO_address_check_option (address,
604 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound", session,
605 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
607 GST_neighbours_session_terminated (&address->peer, session);
609 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
610 "transport-ats", "Telling ATS to destroy session %p from peer %s\n",
611 session, GNUNET_i2s (&address->peer));
613 /* Tell ATS that session has ended */
614 GNUNET_ATS_address_destroyed (GST_ats, address, session);
616 cancel_pending_blacklist_checks (address, session);
618 for (sk = sk_head; NULL != sk; sk = sk->next)
620 if (sk->session == session)
622 GNUNET_CONTAINER_DLL_remove(sk_head, sk_tail, sk);
623 GNUNET_SCHEDULER_cancel (sk->task);
631 * Function that will be called to figure if an address is an loopback,
632 * LAN, WAN etc. address
635 * @param addr binary address
636 * @param addrlen length of the @a addr
637 * @return type of the network @a addr belongs to
639 static enum GNUNET_ATS_Network_Type
640 plugin_env_address_to_type (void *cls,
641 const struct sockaddr *addr,
647 return GNUNET_ATS_NET_UNSPECIFIED;
649 if (((addr->sa_family != AF_INET) && (addrlen != sizeof(struct sockaddr_in)))
650 && ((addr->sa_family != AF_INET6)
651 && (addrlen != sizeof(struct sockaddr_in6)))
652 && (addr->sa_family != AF_UNIX))
655 return GNUNET_ATS_NET_UNSPECIFIED;
657 return GNUNET_ATS_address_get_type (GST_ats,
664 * Notify ATS about the new address including the network this address is
667 * @param address the address
668 * @param session the session
669 * @param ats ats information
670 * @param ats_count number of @a ats information
673 GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
674 struct Session *session, const struct GNUNET_ATS_Information *ats,
677 struct GNUNET_TRANSPORT_PluginFunctions *papi;
678 struct GNUNET_ATS_Information ats2[ats_count + 1];
681 /* valid new address, let ATS know! */
682 if (NULL == address->transport_name)
687 if (NULL == (papi = GST_plugins_find (address->transport_name)))
689 /* we don't have the plugin for this address */
694 if (GNUNET_YES == GNUNET_ATS_session_known (GST_ats, address, session))
696 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
697 "ATS already knows the address, not passing it on again\n");
701 net = papi->get_network (papi->cls, session);
702 if (GNUNET_ATS_NET_UNSPECIFIED == net)
704 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
705 _("Could not obtain a valid network for `%s' %s (%s)\n"),
706 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
707 address->transport_name);
710 ats2[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
711 ats2[0].value = htonl (net);
712 memcpy (&ats2[1], ats, sizeof(struct GNUNET_ATS_Information) * ats_count);
713 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
714 "Notifying ATS about peer `%s''s new address `%s' session %p in network %s\n",
715 GNUNET_i2s (&address->peer),
716 (0 == address->address_length) ? "<inbound>" : GST_plugins_a2s (address),
717 session, GNUNET_ATS_print_network_type (net));
718 GNUNET_ATS_address_add (GST_ats, address, session, ats2, ats_count + 1);
722 * Notify ATS about property changes to an address
724 * @param peer the peer
725 * @param address the address
726 * @param session the session
727 * @param ats performance information
728 * @param ats_count number of elements in @a ats
731 GST_ats_update_metrics (const struct GNUNET_PeerIdentity *peer,
732 const struct GNUNET_HELLO_Address *address,
733 struct Session *session,
734 const struct GNUNET_ATS_Information *ats,
737 struct GNUNET_ATS_Information *ats_new;
739 if (GNUNET_NO == GNUNET_ATS_session_known (GST_ats, address, session))
742 /* Call to manipulation to manipulate ATS information */
743 ats_new = GST_manipulation_manipulate_metrics (peer, address, session, ats,
750 if (GNUNET_NO == GNUNET_ATS_address_update (GST_ats, address, session,
753 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
754 _("Address or session unknown: failed to update properties for peer `%s' plugin `%s' address `%s' session %p\n"),
755 GNUNET_i2s (peer), address->transport_name, GST_plugins_a2s (address),
758 GNUNET_free(ats_new);
762 * Function that will be called to update metrics for an address
765 * @param address address to update metrics for
766 * @param session the session
767 * @param ats the ats information to update
768 * @param ats_count the number of @a ats elements
771 plugin_env_update_metrics (void *cls,
772 const struct GNUNET_HELLO_Address *address,
773 struct Session *session,
774 const struct GNUNET_ATS_Information *ats,
777 if ((NULL == ats) || (0 == ats_count))
779 GNUNET_assert(NULL != GST_ats);
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Updating metrics for peer `%s' address %s session %p\n",
783 GNUNET_i2s (&address->peer),
784 GST_plugins_a2s (address),
786 GST_ats_update_metrics (&address->peer,
794 * Black list check result for try_connect call
795 * If connection to the peer is allowed request adddress and
797 * @param cls blc_ctx bl context
798 * @param peer the peer
799 * @param result the result
802 plugin_env_session_start_bl_check_cont (void *cls,
803 const struct GNUNET_PeerIdentity *peer, int result)
805 struct BlacklistCheckContext *blctx = cls;
807 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, blctx);
810 if (GNUNET_OK == result)
812 GST_ats_add_address (blctx->address, blctx->session,
813 blctx->ats, blctx->ats_count);
817 cancel_pending_blacklist_checks (blctx->address, blctx->session);
818 kill_session (blctx->address->transport_name, blctx->session);
821 GNUNET_HELLO_address_free (blctx->address);
822 GNUNET_free_non_null (blctx->ats);
828 * Plugin tells transport service about a new inbound session
831 * @param address the address
832 * @param session the new session
833 * @param ats ats information
834 * @param ats_count number of @a ats information
837 plugin_env_session_start (void *cls,
838 struct GNUNET_HELLO_Address *address,
839 struct Session *session,
840 const struct GNUNET_ATS_Information *ats,
843 struct BlacklistCheckContext *blctx;
844 struct GST_BlacklistCheck *blc;
857 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
858 "Notification from plugin `%s' about new %s session %p from peer `%s' address `%s'\n",
859 address->transport_name,
860 GNUNET_HELLO_address_check_option (address,
861 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
862 session, GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
864 /* Do blacklist check if communication with this peer is allowed */
865 blctx = GNUNET_new (struct BlacklistCheckContext);
866 blctx->address = GNUNET_HELLO_address_copy (address);
867 blctx->session = session;
870 blctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
871 for (c = 0; c < ats_count; c++)
873 blctx->ats[c].type = ats[c].type;
874 blctx->ats[c].value = ats[c].value;
878 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, blctx);
879 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer, address->transport_name,
880 &plugin_env_session_start_bl_check_cont, blctx)))
888 * Function called by ATS to notify the callee that the
889 * assigned bandwidth or address for a given peer was changed. If the
890 * callback is called with address/bandwidth assignments of zero, the
891 * ATS disconnect function will still be called once the disconnect
895 * @param peer the peer this address is intended for
896 * @param address address to use (for peer given in address)
897 * @param session session to use (if available)
898 * @param bandwidth_out assigned outbound bandwidth for the connection in NBO,
899 * 0 to disconnect from peer
900 * @param bandwidth_in assigned inbound bandwidth for the connection in NBO,
901 * 0 to disconnect from peer
902 * @param ats ATS information
903 * @param ats_count number of @a ats elements
906 ats_request_address_change (void *cls,
907 const struct GNUNET_PeerIdentity *peer,
908 const struct GNUNET_HELLO_Address *address,
909 struct Session *session,
910 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
911 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
913 uint32_t bw_in = ntohl (bandwidth_in.value__);
914 uint32_t bw_out = ntohl (bandwidth_out.value__);
916 /* ATS tells me to disconnect from peer */
917 if ((0 == bw_in) && (0 == bw_out))
919 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
920 "ATS tells me to disconnect from peer `%s'\n",
921 GNUNET_i2s (&address->peer));
922 GST_neighbours_force_disconnect (&address->peer);
926 GST_neighbours_switch_to_address (&address->peer,
929 bandwidth_in, bandwidth_out);
934 * Function called to notify transport users that another
935 * peer connected to us.
938 * @param peer the peer that connected
939 * @param bandwidth_in inbound bandwidth in NBO
940 * @param bandwidth_out outbound bandwidth in NBO
943 neighbours_connect_notification (void *cls,
944 const struct GNUNET_PeerIdentity *peer,
945 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
946 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
948 size_t len = sizeof(struct ConnectInfoMessage);
949 char buf[len] GNUNET_ALIGN;
950 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
953 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
954 "We are now connected to peer `%s' and %u peers in total\n",
955 GNUNET_i2s (peer), connections);
956 connect_msg->header.size = htons (sizeof(buf));
957 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
958 connect_msg->id = *peer;
959 connect_msg->quota_in = bandwidth_in;
960 connect_msg->quota_out = bandwidth_out;
961 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
966 * Function called to notify transport users that another
967 * peer disconnected from us.
970 * @param peer the peer that disconnected
973 neighbours_disconnect_notification (void *cls,
974 const struct GNUNET_PeerIdentity *peer)
976 struct DisconnectInfoMessage disconnect_msg;
979 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
980 "Peer `%s' disconnected and we are connected to %u peers\n",
981 GNUNET_i2s (peer), connections);
983 GST_manipulation_peer_disconnect (peer);
984 disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
985 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
986 disconnect_msg.reserved = htonl (0);
987 disconnect_msg.peer = *peer;
988 GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
993 * Function called to notify transport users that a neighbour peer changed its
997 * @param peer peer this update is about (never NULL)
998 * @param address address, NULL on disconnect
999 * @param state current state this peer is in
1000 * @param state_timeout timeout for the current state of the peer
1001 * @param bandwidth_in bandwidth assigned inbound
1002 * @param bandwidth_out bandwidth assigned outbound
1005 neighbours_changed_notification (void *cls,
1006 const struct GNUNET_PeerIdentity *peer,
1007 const struct GNUNET_HELLO_Address *address,
1008 enum GNUNET_TRANSPORT_PeerState state,
1009 struct GNUNET_TIME_Absolute state_timeout,
1010 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1011 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1013 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1014 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
1016 (NULL != address) ? GST_plugins_a2s (address) : "<none>",
1017 GNUNET_TRANSPORT_ps2s (state),
1018 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1020 GST_clients_broadcast_peer_notification (peer,
1028 * Function called when the service shuts down. Unloads our plugins
1029 * and cancels pending validations.
1031 * @param cls closure, unused
1032 * @param tc task context (unused)
1035 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1037 GST_neighbours_stop ();
1038 GST_validation_stop ();
1039 GST_plugins_unload ();
1041 GNUNET_ATS_scheduling_done (GST_ats);
1043 GST_clients_stop ();
1044 GST_blacklist_stop ();
1046 GST_manipulation_stop ();
1048 if (NULL != GST_peerinfo)
1050 GNUNET_PEERINFO_disconnect (GST_peerinfo);
1051 GST_peerinfo = NULL;
1053 if (NULL != GST_stats)
1055 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
1058 if (NULL != GST_my_private_key)
1060 GNUNET_free(GST_my_private_key);
1061 GST_my_private_key = NULL;
1068 * Initiate transport service.
1070 * @param cls closure
1071 * @param server the initialized server
1072 * @param c configuration to use
1075 run (void *cls, struct GNUNET_SERVER_Handle *server,
1076 const struct GNUNET_CONFIGURATION_Handle *c)
1079 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
1080 long long unsigned int max_fd_cfg;
1088 != GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
1091 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1092 _("Transport service is lacking key configuration settings. Exiting.\n"));
1093 GNUNET_SCHEDULER_shutdown ();
1097 GNUNET_CONFIGURATION_get_value_time (c,
1102 hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
1104 GST_server = server;
1105 pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
1106 GNUNET_free (keyfile);
1107 GNUNET_assert (NULL != pk);
1108 GST_my_private_key = pk;
1110 GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
1111 GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
1112 GNUNET_CRYPTO_eddsa_key_get_public (GST_my_private_key,
1113 &GST_my_identity.public_key);
1114 GNUNET_assert(NULL != GST_my_private_key);
1116 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1117 "My identity is `%4s'\n",
1118 GNUNET_i2s_full (&GST_my_identity));
1120 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1122 if (NULL == GST_peerinfo)
1124 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1125 _("Could not access PEERINFO service. Exiting.\n"));
1126 GNUNET_SCHEDULER_shutdown ();
1133 struct rlimit r_file;
1134 if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
1136 max_fd_rlimit = r_file.rlim_cur;
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "Maximum number of open files was: %u/%u\n",
1142 max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
1144 GNUNET_CONFIGURATION_get_value_number (GST_cfg, "transport", "MAX_FD",
1147 if (max_fd_cfg > max_fd_rlimit)
1148 max_fd = max_fd_cfg;
1150 max_fd = max_fd_rlimit;
1151 if (max_fd < DEFAULT_MAX_FDS)
1152 max_fd = DEFAULT_MAX_FDS;
1154 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1155 "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
1156 max_fd, (max_fd / 3), (max_fd / 3) * 2);
1158 friend_only = GNUNET_CONFIGURATION_get_value_yesno (GST_cfg, "topology",
1160 if (GNUNET_SYSERR == friend_only)
1161 friend_only = GNUNET_NO; /* According to topology defaults */
1162 /* start subsystems */
1163 GST_hello_start (friend_only, &process_hello_update, NULL );
1164 GNUNET_assert(NULL != GST_hello_get());
1165 GST_blacklist_start (GST_server, GST_cfg, &GST_my_identity);
1166 GST_ats = GNUNET_ATS_scheduling_init (GST_cfg,
1167 &ats_request_address_change,
1169 GST_manipulation_init (GST_cfg);
1170 GST_plugins_load (&GST_manipulation_recv,
1171 &GST_neighbours_register_quota_notification,
1172 &GST_neighbours_unregister_quota_notification,
1173 &plugin_env_address_change_notification,
1174 &plugin_env_session_start,
1175 &plugin_env_session_end,
1176 &plugin_env_address_to_type,
1177 &plugin_env_update_metrics);
1178 GST_neighbours_start (NULL,
1179 &neighbours_connect_notification,
1180 &neighbours_disconnect_notification,
1181 &neighbours_changed_notification,
1183 GST_clients_start (GST_server);
1184 GST_validation_start ((max_fd / 3));
1189 * The main function for the transport service.
1191 * @param argc number of arguments from the command line
1192 * @param argv command line arguments
1193 * @return 0 ok, 1 on error
1196 main (int argc, char * const *argv)
1200 == GNUNET_SERVICE_run (argc, argv, "transport",
1201 GNUNET_SERVICE_OPTION_NONE, &run, NULL )) ? 0 : 1;
1204 /* end of file gnunet-service-transport.c */