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
23 * @brief main for gnunet-service-transport
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_ats.h"
35 #include "gnunet-service-transport_blacklist.h"
36 #include "gnunet-service-transport_clients.h"
37 #include "gnunet-service-transport_hello.h"
38 #include "gnunet-service-transport_neighbours.h"
39 #include "gnunet-service-transport_plugins.h"
40 #include "gnunet-service-transport_validation.h"
41 #include "gnunet-service-transport_manipulation.h"
42 #include "transport.h"
45 * Information we need for an asynchronous session kill.
52 struct SessionKiller *next;
57 struct SessionKiller *prev;
62 struct Session *session;
65 * Plugin for the session.
67 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
72 struct GNUNET_SCHEDULER_Task * task;
76 struct BlacklistCheckContext
78 struct BlacklistCheckContext *prev;
80 struct BlacklistCheckContext *next;
82 struct GST_BlacklistCheck *blc;
84 struct GNUNET_HELLO_Address *address;
86 struct Session *session;
88 struct GNUNET_MessageHeader *msg;
97 struct GNUNET_STATISTICS_Handle *GST_stats;
100 * Configuration handle.
102 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
105 * Configuration handle.
107 struct GNUNET_PeerIdentity GST_my_identity;
110 * Handle to peerinfo service.
112 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
115 * Handle to our service's server.
117 static struct GNUNET_SERVER_Handle *GST_server;
122 struct GNUNET_CRYPTO_EddsaPrivateKey *GST_my_private_key;
127 struct GNUNET_ATS_SchedulingHandle *GST_ats;
130 * Hello address expiration
132 struct GNUNET_TIME_Relative hello_expiration;
135 * DEBUGGING connection counter
137 static int connections;
140 * Head of DLL of asynchronous tasks to kill sessions.
142 static struct SessionKiller *sk_head;
145 * Tail of DLL of asynchronous tasks to kill sessions.
147 static struct SessionKiller *sk_tail;
152 struct BlacklistCheckContext *bc_head;
157 struct BlacklistCheckContext *bc_tail;
161 * Transmit our HELLO message to the given (connected) neighbour.
163 * @param cls the 'HELLO' message
164 * @param peer identity of the peer
165 * @param address the address
166 * @param state current state this peer is in
167 * @param state_timeout timeout for the current state of the peer
168 * @param bandwidth_in inbound quota in NBO
169 * @param bandwidth_out outbound quota in NBO
172 transmit_our_hello (void *cls,
173 const struct GNUNET_PeerIdentity *peer,
174 const struct GNUNET_HELLO_Address *address,
175 enum GNUNET_TRANSPORT_PeerState state,
176 struct GNUNET_TIME_Absolute state_timeout,
177 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
178 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
180 const struct GNUNET_MessageHeader *hello = cls;
182 if (GNUNET_NO == GST_neighbours_test_connected (peer))
185 GST_neighbours_send (peer,
194 * My HELLO has changed. Tell everyone who should know.
197 * @param hello new HELLO
200 process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
202 GST_clients_broadcast (hello, GNUNET_NO);
203 GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
208 * We received some payload. Prepare to pass it on to our clients.
210 * @param address address and (claimed) identity of the other peer
211 * @param session identifier used for this session (NULL for plugins
212 * that do not offer bi-directional communication to the sender
213 * using the same "connection")
214 * @param message the message to process
215 * @return how long the plugin should wait until receiving more data
217 static struct GNUNET_TIME_Relative
218 process_payload (const struct GNUNET_HELLO_Address *address,
219 struct Session *session,
220 const struct GNUNET_MessageHeader *message)
222 struct GNUNET_TIME_Relative ret;
224 struct InboundMessage *im;
225 size_t msg_size = ntohs (message->size);
226 size_t size = sizeof(struct InboundMessage) + msg_size;
227 char buf[size] GNUNET_ALIGN;
229 do_forward = GNUNET_SYSERR;
230 ret = GST_neighbours_calculate_receive_delay (&address->peer,
233 if (! GST_neighbours_test_connected (&address->peer))
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Discarded %u bytes type %u payload from peer `%s'\n",
238 ntohs (message->type),
239 GNUNET_i2s (&address->peer));
240 GNUNET_STATISTICS_update (GST_stats, gettext_noop
241 ("# bytes payload discarded due to not connected peer"),
247 if (GNUNET_YES != do_forward)
249 im = (struct InboundMessage *) buf;
250 im->header.size = htons (size);
251 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
252 im->peer = address->peer;
253 memcpy (&im[1], message, ntohs (message->size));
254 GST_clients_broadcast (&im->header, GNUNET_YES);
260 * Task to asynchronously terminate a session.
262 * @param cls the `struct SessionKiller` with the information for the kill
263 * @param tc scheduler context
266 kill_session_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
268 struct SessionKiller *sk = cls;
271 GNUNET_CONTAINER_DLL_remove(sk_head, sk_tail, sk);
272 sk->plugin->disconnect_session (sk->plugin->cls, sk->session);
278 * FIXME. Also, consider moving the "bc_*" logic into
282 cancel_pending_blacklist_checks (const struct GNUNET_HELLO_Address *address,
283 struct Session *session)
285 struct BlacklistCheckContext *blctx;
286 struct BlacklistCheckContext *next;
289 for (blctx = next; NULL != blctx; blctx = next)
292 if ( (NULL != blctx->address) &&
293 (0 == GNUNET_HELLO_address_cmp(blctx->address, address)) &&
294 (blctx->session == session))
296 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, blctx);
297 if (NULL != blctx->blc)
299 GST_blacklist_test_cancel (blctx->blc);
302 GNUNET_HELLO_address_free (blctx->address);
303 GNUNET_free_non_null (blctx->msg);
311 * Force plugin to terminate session due to communication
314 * @param plugin_name name of the plugin
315 * @param session session to termiante
318 kill_session (const char *plugin_name,
319 struct Session *session)
321 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
322 struct SessionKiller *sk;
324 for (sk = sk_head; NULL != sk; sk = sk->next)
325 if (sk->session == session)
327 plugin = GST_plugins_find (plugin_name);
333 /* need to issue disconnect asynchronously */
334 sk = GNUNET_new (struct SessionKiller);
335 sk->session = session;
337 sk->task = GNUNET_SCHEDULER_add_now (&kill_session_task, sk);
338 GNUNET_CONTAINER_DLL_insert (sk_head,
345 * Black list check result for try_connect call
346 * If connection to the peer is allowed request adddress and ???
348 * @param cls blc_ctx bl context
349 * @param peer the peer
350 * @param result the result
353 connect_bl_check_cont (void *cls,
354 const struct GNUNET_PeerIdentity *peer,
357 struct BlacklistCheckContext *blctx = cls;
359 GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, blctx);
362 if (GNUNET_OK == result)
364 /* Blacklist allows to speak to this peer, forward SYN to neighbours */
365 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
366 "Received SYN message from peer `%s' with `%s' %p\n",
368 GST_plugins_a2s (blctx->address),
372 GST_neighbours_handle_session_syn (blctx->msg,
373 &blctx->address->peer))
375 cancel_pending_blacklist_checks (blctx->address, blctx->session);
376 kill_session (blctx->address->transport_name, blctx->session);
381 /* Blacklist denies to speak to this peer */
383 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
384 "Discarding SYN message from `%s' due to denied blacklist check\n",
386 cancel_pending_blacklist_checks (blctx->address, blctx->session);
387 kill_session (blctx->address->transport_name, blctx->session);
390 if (NULL != blctx->address)
391 GNUNET_HELLO_address_free (blctx->address);
392 GNUNET_free (blctx->msg);
398 * Function called by the transport for each received message.
400 * @param cls closure, const char* with the name of the plugin we received the message from
401 * @param address address and (claimed) identity of the other peer
402 * @param message the message, NULL if we only care about
403 * learning about the delay until we should receive again
404 * @param session identifier used for this session (NULL for plugins
405 * that do not offer bi-directional communication to the sender
406 * using the same "connection")
407 * @return how long the plugin should wait until receiving more data
408 * (plugins that do not support this, can ignore the return value)
410 struct GNUNET_TIME_Relative
411 GST_receive_callback (void *cls,
412 const struct GNUNET_HELLO_Address *address,
413 struct Session *session,
414 const struct GNUNET_MessageHeader *message)
416 const char *plugin_name = cls;
417 struct GNUNET_TIME_Relative ret;
418 struct BlacklistCheckContext *blctx;
419 struct GST_BlacklistCheck *blc;
422 ret = GNUNET_TIME_UNIT_ZERO;
425 type = ntohs (message->type);
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Received Message with type %u from peer `%s'\n",
429 GNUNET_i2s (&address->peer));
431 GNUNET_STATISTICS_update (GST_stats,
432 gettext_noop ("# bytes total received"),
433 ntohs (message->size),
435 GST_neighbours_notify_data_recv (address,
440 case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
441 /* Legacy HELLO message, discard */
443 case GNUNET_MESSAGE_TYPE_HELLO:
444 if (GNUNET_OK != GST_validation_handle_hello (message))
447 cancel_pending_blacklist_checks (address,
451 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Processing `%s' from `%s'\n", "PING",
454 GST_plugins_a2s (address));
456 GST_validation_handle_ping (&address->peer,
461 cancel_pending_blacklist_checks (address,
463 kill_session (plugin_name,
467 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
468 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
469 "Processing `%s' from `%s'\n", "PONG",
470 GST_plugins_a2s (address));
471 if (GNUNET_OK != GST_validation_handle_pong (&address->peer, message))
474 cancel_pending_blacklist_checks (address, session);
475 kill_session (plugin_name, session);
478 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN:
479 /* Do blacklist check if communication with this peer is allowed */
480 blctx = GNUNET_new (struct BlacklistCheckContext);
481 blctx->address = GNUNET_HELLO_address_copy (address);
482 blctx->session = session;
483 blctx->msg = GNUNET_malloc (ntohs(message->size));
484 memcpy (blctx->msg, message, ntohs(message->size));
485 GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, blctx);
486 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer, NULL,
487 &connect_bl_check_cont,
493 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK:
495 GST_neighbours_handle_session_syn_ack (message,
499 cancel_pending_blacklist_checks (address, session);
500 kill_session (plugin_name, session);
503 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
505 GST_neighbours_handle_session_ack (message,
510 cancel_pending_blacklist_checks (address, session);
511 kill_session (plugin_name, session);
514 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
515 GST_neighbours_handle_disconnect_message (&address->peer, message);
517 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
518 GST_neighbours_keepalive (&address->peer, message);
520 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
521 GST_neighbours_keepalive_response (&address->peer, message);
524 /* should be payload */
525 GNUNET_STATISTICS_update (GST_stats,
526 gettext_noop ("# bytes payload received"),
527 ntohs (message->size),
529 GST_neighbours_notify_payload_recv (address,
532 ret = process_payload (address,
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Allowing receive from peer %s to continue in %s\n",
540 GNUNET_i2s (&address->peer),
541 GNUNET_STRINGS_relative_time_to_string (ret,
548 * Function that will be called for each address the transport
549 * is aware that it might be reachable under. Update our HELLO.
551 * @param cls name of the plugin (const char*)
552 * @param add_remove should the address added (YES) or removed (NO) from the
553 * set of valid addresses?
554 * @param address the address to add or remove
557 plugin_env_address_change_notification (void *cls,
559 const struct GNUNET_HELLO_Address *address)
561 static int addresses = 0;
562 struct GNUNET_STATISTICS_Handle *cfg = GST_stats;
564 if (GNUNET_YES == add_remove)
567 GNUNET_STATISTICS_update (cfg, "# transport addresses", 1, GNUNET_NO);
569 else if (GNUNET_NO == add_remove)
576 GNUNET_STATISTICS_update (cfg, "# transport addresses", -1, GNUNET_NO);
580 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
581 "Transport now has %u addresses to communicate\n",
583 GST_hello_modify_addresses (add_remove, address);
588 * Function that will be called whenever the plugin internally
589 * cleans up a session pointer and hence the service needs to
590 * discard all of those sessions as well. Plugins that do not
591 * use sessions can simply omit calling this function and always
592 * use NULL wherever a session pointer is needed. This function
593 * should be called BEFORE a potential "TransmitContinuation"
594 * from the "TransmitFunction".
597 * @param address which address was the session for
598 * @param session which session is being destoyed
601 plugin_env_session_end (void *cls,
602 const struct GNUNET_HELLO_Address *address,
603 struct Session *session)
605 struct SessionKiller *sk;
617 GNUNET_assert (strlen (address->transport_name) > 0);
619 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
620 "Notification from plugin `%s' about terminated %s session %p from peer `%s' address `%s'\n",
621 address->transport_name,
622 GNUNET_HELLO_address_check_option (address,
623 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
625 GNUNET_i2s (&address->peer),
626 GST_plugins_a2s (address));
628 GST_neighbours_session_terminated (&address->peer, session);
629 GST_ats_del_session (address, session);
630 cancel_pending_blacklist_checks (address, session);
632 for (sk = sk_head; NULL != sk; sk = sk->next)
634 if (sk->session == session)
636 GNUNET_CONTAINER_DLL_remove (sk_head, sk_tail, sk);
637 GNUNET_SCHEDULER_cancel (sk->task);
646 * Function that will be called to figure if an address is an loopback,
647 * LAN, WAN etc. address
650 * @param addr binary address
651 * @param addrlen length of the @a addr
652 * @return type of the network @a addr belongs to
654 static enum GNUNET_ATS_Network_Type
655 plugin_env_address_to_type (void *cls,
656 const struct sockaddr *addr,
662 return GNUNET_ATS_NET_UNSPECIFIED;
664 return GNUNET_ATS_address_get_type (GST_ats,
671 * Function that will be called to update metrics for an address
674 * @param address address to update metrics for
675 * @param session the session
676 * @param ats the ats information to update
677 * @param ats_count the number of @a ats elements
680 plugin_env_update_metrics (void *cls,
681 const struct GNUNET_HELLO_Address *address,
682 struct Session *session,
683 const struct GNUNET_ATS_Information *ats,
686 GST_ats_update_metrics (address,
693 * Black list check result from blacklist check triggered when a
694 * plugin gave us a new session in #plugin_env_session_start(). If
695 * connection to the peer is disallowed, kill the session.
697 * @param cls blc_ctx bl context
698 * @param peer the peer
699 * @param result the result
702 plugin_env_session_start_bl_check_cont (void *cls,
703 const struct GNUNET_PeerIdentity *peer,
706 struct BlacklistCheckContext *blctx = cls;
708 GNUNET_CONTAINER_DLL_remove (bc_head,
712 if (GNUNET_OK != result)
714 cancel_pending_blacklist_checks (blctx->address,
716 kill_session (blctx->address->transport_name,
719 GNUNET_HELLO_address_free (blctx->address);
725 * Plugin tells transport service about a new inbound session
728 * @param address the address
729 * @param session the new session
730 * @param ats ats information
731 * @param ats_count number of @a ats information
734 plugin_env_session_start (void *cls,
735 const struct GNUNET_HELLO_Address *address,
736 struct Session *session,
737 const struct GNUNET_ATS_Information *ats,
740 struct BlacklistCheckContext *blctx;
741 struct GST_BlacklistCheck *blc;
753 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
754 "Notification from plugin `%s' about new %s session %p from peer `%s' address `%s'\n",
755 address->transport_name,
756 GNUNET_HELLO_address_check_option (address,
757 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
759 GNUNET_i2s (&address->peer),
760 GST_plugins_a2s (address));
762 GNUNET_HELLO_address_check_option (address,
763 GNUNET_HELLO_ADDRESS_INFO_INBOUND)) ||
765 GST_ats_is_known (address, NULL) ) )
767 /* inbound is always new, but outbound MAY already be known, but
768 for example for UNIX, we have symmetric connections and thus we
769 may not know the address yet; add if necessary! */
770 GST_ats_add_address (address,
777 GST_ats_new_session (address,
779 GST_ats_update_metrics (address,
784 /* Do blacklist check if communication with this peer is allowed */
785 blctx = GNUNET_new (struct BlacklistCheckContext);
786 blctx->address = GNUNET_HELLO_address_copy (address);
787 blctx->session = session;
788 GNUNET_CONTAINER_DLL_insert (bc_head,
792 (blc = GST_blacklist_test_allowed (&address->peer,
793 address->transport_name,
794 &plugin_env_session_start_bl_check_cont,
803 * Function called by ATS to notify the callee that the
804 * assigned bandwidth or address for a given peer was changed. If the
805 * callback is called with address/bandwidth assignments of zero, the
806 * ATS disconnect function will still be called once the disconnect
810 * @param peer the peer this address is intended for
811 * @param address address to use (for peer given in address)
812 * @param session session to use (if available)
813 * @param bandwidth_out assigned outbound bandwidth for the connection in NBO,
814 * 0 to disconnect from peer
815 * @param bandwidth_in assigned inbound bandwidth for the connection in NBO,
816 * 0 to disconnect from peer
817 * @param ats ATS information
818 * @param ats_count number of @a ats elements
821 ats_request_address_change (void *cls,
822 const struct GNUNET_PeerIdentity *peer,
823 const struct GNUNET_HELLO_Address *address,
824 struct Session *session,
825 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
826 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
828 uint32_t bw_in = ntohl (bandwidth_in.value__);
829 uint32_t bw_out = ntohl (bandwidth_out.value__);
831 /* ATS tells me to disconnect from peer */
832 if ((0 == bw_in) && (0 == bw_out))
834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
835 "ATS tells me to disconnect from peer `%s'\n",
836 GNUNET_i2s (&address->peer));
837 GST_neighbours_force_disconnect (&address->peer);
841 GST_neighbours_switch_to_address (address,
843 bandwidth_in, bandwidth_out);
848 * Function called to notify transport users that another
849 * peer connected to us.
852 * @param peer the peer that connected
853 * @param bandwidth_in inbound bandwidth in NBO
854 * @param bandwidth_out outbound bandwidth in NBO
857 neighbours_connect_notification (void *cls,
858 const struct GNUNET_PeerIdentity *peer,
859 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
860 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
862 size_t len = sizeof(struct ConnectInfoMessage);
863 char buf[len] GNUNET_ALIGN;
864 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
867 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
868 "We are now connected to peer `%s' and %u peers in total\n",
871 connect_msg->header.size = htons (sizeof(buf));
872 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
873 connect_msg->id = *peer;
874 connect_msg->quota_in = bandwidth_in;
875 connect_msg->quota_out = bandwidth_out;
876 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
881 * Function called to notify transport users that another
882 * peer disconnected from us.
885 * @param peer the peer that disconnected
888 neighbours_disconnect_notification (void *cls,
889 const struct GNUNET_PeerIdentity *peer)
891 struct DisconnectInfoMessage disconnect_msg;
894 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
895 "Peer `%s' disconnected and we are connected to %u peers\n",
899 GST_manipulation_peer_disconnect (peer);
900 disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
901 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
902 disconnect_msg.reserved = htonl (0);
903 disconnect_msg.peer = *peer;
904 GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
909 * Function called to notify transport users that a neighbour peer changed its
913 * @param peer identity of the peer
914 * @param address address possibly NULL if peer is not connected
915 * @param state current state this peer is in
916 * @param state_timeout timeout for the current state of the peer
917 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
918 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
921 neighbours_changed_notification (void *cls,
922 const struct GNUNET_PeerIdentity *peer,
923 const struct GNUNET_HELLO_Address *address,
924 enum GNUNET_TRANSPORT_PeerState state,
925 struct GNUNET_TIME_Absolute state_timeout,
926 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
927 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
932 GST_plugins_a2s (address),
933 GNUNET_TRANSPORT_ps2s (state),
934 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
935 /* FIXME: include bandwidth in notification! */
936 GST_clients_broadcast_peer_notification (peer,
944 * Function called when the service shuts down. Unloads our plugins
945 * and cancels pending validations.
947 * @param cls closure, unused
948 * @param tc task context (unused)
951 shutdown_task (void *cls,
952 const struct GNUNET_SCHEDULER_TaskContext *tc)
954 GST_neighbours_stop ();
955 GST_plugins_unload ();
956 GST_validation_stop ();
958 GNUNET_ATS_scheduling_done (GST_ats);
961 GST_blacklist_stop ();
963 GST_manipulation_stop ();
965 if (NULL != GST_peerinfo)
967 GNUNET_PEERINFO_disconnect (GST_peerinfo);
970 if (NULL != GST_stats)
972 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
975 if (NULL != GST_my_private_key)
977 GNUNET_free(GST_my_private_key);
978 GST_my_private_key = NULL;
985 * Initiate transport service.
988 * @param server the initialized server
989 * @param c configuration to use
993 struct GNUNET_SERVER_Handle *server,
994 const struct GNUNET_CONFIGURATION_Handle *c)
997 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
998 long long unsigned int max_fd_cfg;
1006 != GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
1009 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1010 _("Transport service is lacking key configuration settings. Exiting.\n"));
1011 GNUNET_SCHEDULER_shutdown ();
1015 GNUNET_CONFIGURATION_get_value_time (c,
1020 hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
1022 GST_server = server;
1023 pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
1024 GNUNET_free (keyfile);
1025 GNUNET_assert (NULL != pk);
1026 GST_my_private_key = pk;
1028 GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
1029 GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
1030 GNUNET_CRYPTO_eddsa_key_get_public (GST_my_private_key,
1031 &GST_my_identity.public_key);
1032 GNUNET_assert(NULL != GST_my_private_key);
1034 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1035 "My identity is `%4s'\n",
1036 GNUNET_i2s_full (&GST_my_identity));
1038 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1041 if (NULL == GST_peerinfo)
1043 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1044 _("Could not access PEERINFO service. Exiting.\n"));
1045 GNUNET_SCHEDULER_shutdown ();
1052 struct rlimit r_file;
1053 if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
1055 max_fd_rlimit = r_file.rlim_cur;
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "Maximum number of open files was: %u/%u\n",
1061 max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
1063 GNUNET_CONFIGURATION_get_value_number (GST_cfg,
1068 if (max_fd_cfg > max_fd_rlimit)
1069 max_fd = max_fd_cfg;
1071 max_fd = max_fd_rlimit;
1072 if (max_fd < DEFAULT_MAX_FDS)
1073 max_fd = DEFAULT_MAX_FDS;
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
1077 max_fd, (max_fd / 3), (max_fd / 3) * 2);
1079 friend_only = GNUNET_CONFIGURATION_get_value_yesno (GST_cfg, "topology",
1081 if (GNUNET_SYSERR == friend_only)
1082 friend_only = GNUNET_NO; /* According to topology defaults */
1083 /* start subsystems */
1084 GST_hello_start (friend_only, &process_hello_update, NULL );
1085 GNUNET_assert(NULL != GST_hello_get());
1086 GST_blacklist_start (GST_server, GST_cfg, &GST_my_identity);
1087 GST_ats = GNUNET_ATS_scheduling_init (GST_cfg,
1088 &ats_request_address_change,
1091 GST_manipulation_init (GST_cfg);
1092 GST_plugins_load (&GST_manipulation_recv,
1093 &GST_neighbours_register_quota_notification,
1094 &GST_neighbours_unregister_quota_notification,
1095 &plugin_env_address_change_notification,
1096 &plugin_env_session_start,
1097 &plugin_env_session_end,
1098 &plugin_env_address_to_type,
1099 &plugin_env_update_metrics);
1100 GST_neighbours_start (NULL,
1101 &neighbours_connect_notification,
1102 &neighbours_disconnect_notification,
1103 &neighbours_changed_notification,
1105 GST_clients_start (GST_server);
1106 GST_validation_start ((max_fd / 3));
1111 * The main function for the transport service.
1113 * @param argc number of arguments from the command line
1114 * @param argv command line arguments
1115 * @return 0 ok, 1 on error
1118 main (int argc, char * const *argv)
1122 == GNUNET_SERVICE_run (argc, argv, "transport",
1123 GNUNET_SERVICE_OPTION_NONE, &run, NULL )) ? 0 : 1;
1126 /* end of file gnunet-service-transport.c */