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 GNUNET_SCHEDULER_TaskIdentifier task;
79 struct GNUNET_STATISTICS_Handle *GST_stats;
82 * Configuration handle.
84 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
87 * Configuration handle.
89 struct GNUNET_PeerIdentity GST_my_identity;
92 * Handle to peerinfo service.
94 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
97 * Handle to our service's server.
99 static struct GNUNET_SERVER_Handle *GST_server;
104 struct GNUNET_CRYPTO_EddsaPrivateKey *GST_my_private_key;
109 struct GNUNET_ATS_SchedulingHandle *GST_ats;
112 * Hello address expiration
114 struct GNUNET_TIME_Relative hello_expiration;
117 * DEBUGGING connection counter
119 static int connections;
122 * Head of DLL of asynchronous tasks to kill sessions.
124 static struct SessionKiller *sk_head;
127 * Tail of DLL of asynchronous tasks to kill sessions.
129 static struct SessionKiller *sk_tail;
132 * Transmit our HELLO message to the given (connected) neighbour.
134 * @param cls the 'HELLO' message
135 * @param target a connected neighbour
136 * @param address the address
137 * @param state current state this peer is in
138 * @param state_timeout timeout for the current state of the peer
139 * @param bandwidth_in inbound quota in NBO
140 * @param bandwidth_out outbound quota in NBO
143 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
144 const struct GNUNET_HELLO_Address *address,
145 enum GNUNET_TRANSPORT_PeerState state,
146 struct GNUNET_TIME_Absolute state_timeout,
147 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
148 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
150 const struct GNUNET_MessageHeader *hello = cls;
152 if (GNUNET_NO == GST_neighbours_test_connected (target))
155 GST_neighbours_send (target, hello, ntohs (hello->size), hello_expiration,
160 * My HELLO has changed. Tell everyone who should know.
163 * @param hello new HELLO
166 process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
168 GST_clients_broadcast (hello, GNUNET_NO);
169 GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
173 * We received some payload. Prepare to pass it on to our clients.
175 * @param peer (claimed) identity of the other peer
176 * @param address the address
177 * @param session session used
178 * @param message the message to process
179 * @return how long the plugin should wait until receiving more data
181 static struct GNUNET_TIME_Relative
182 process_payload (const struct GNUNET_PeerIdentity *peer,
183 const struct GNUNET_HELLO_Address *address, struct Session *session,
184 const struct GNUNET_MessageHeader *message)
186 struct GNUNET_TIME_Relative ret;
188 struct InboundMessage *im;
189 size_t msg_size = ntohs (message->size);
190 size_t size = sizeof(struct InboundMessage) + msg_size;
191 char buf[size] GNUNET_ALIGN;
193 do_forward = GNUNET_SYSERR;
194 ret = GST_neighbours_calculate_receive_delay (peer, msg_size, &do_forward);
195 if (!GST_neighbours_test_connected (peer))
197 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
198 "Discarded %u bytes type %u payload from peer `%s'\n", msg_size,
199 ntohs (message->type), GNUNET_i2s (peer));
200 GNUNET_STATISTICS_update (GST_stats, gettext_noop
201 ("# bytes payload discarded due to not connected peer"), msg_size,
206 GST_ats_add_address (address, session, NULL, 0);
208 if (GNUNET_YES != do_forward)
210 im = (struct InboundMessage *) buf;
211 im->header.size = htons (size);
212 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
214 memcpy (&im[1], message, ntohs (message->size));
215 GST_clients_broadcast (&im->header, GNUNET_YES);
220 * Task to asynchronously terminate a session.
222 * @param cls the `struct SessionKiller` with the information for the kill
223 * @param tc scheduler context
226 kill_session_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
228 struct SessionKiller *sk = cls;
230 sk->task = GNUNET_SCHEDULER_NO_TASK;
231 GNUNET_CONTAINER_DLL_remove(sk_head, sk_tail, sk);
232 sk->plugin->disconnect_session (sk->plugin->cls, sk->session);
237 * Force plugin to terminate session due to communication
240 * @param plugin_name name of the plugin
241 * @param session session to termiante
244 kill_session (const char *plugin_name, struct Session *session)
246 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
247 struct SessionKiller *sk;
249 for (sk = sk_head; NULL != sk; sk = sk->next)
250 if (sk->session == session)
252 plugin = GST_plugins_find (plugin_name);
258 /* need to issue disconnect asynchronously */
259 sk = GNUNET_new (struct SessionKiller);
260 sk->session = session;
262 sk->task = GNUNET_SCHEDULER_add_now (&kill_session_task, sk);
263 GNUNET_CONTAINER_DLL_insert(sk_head, sk_tail, sk);
267 * Function called by the transport for each received message.
269 * @param cls closure, const char* with the name of the plugin we received the message from
270 * @param address address and (claimed) identity of the other peer
271 * @param message the message, NULL if we only care about
272 * learning about the delay until we should receive again
273 * @param session identifier used for this session (NULL for plugins
274 * that do not offer bi-directional communication to the sender
275 * using the same "connection")
276 * @return how long the plugin should wait until receiving more data
277 * (plugins that do not support this, can ignore the return value)
279 struct GNUNET_TIME_Relative
280 GST_receive_callback (void *cls,
281 const struct GNUNET_HELLO_Address *address,
282 struct Session *session,
283 const struct GNUNET_MessageHeader *message)
285 const char *plugin_name = cls;
286 struct GNUNET_TIME_Relative ret;
289 ret = GNUNET_TIME_UNIT_ZERO;
292 type = ntohs (message->type);
293 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
294 "Received Message with type %u from peer `%s'\n", type,
295 GNUNET_i2s (&address->peer));
297 GNUNET_STATISTICS_update (GST_stats, gettext_noop
298 ("# bytes total received"), ntohs (message->size), GNUNET_NO);
299 GST_neighbours_notify_data_recv (&address->peer, address, session, message);
303 case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
304 /* Legacy HELLO message, discard */
306 case GNUNET_MESSAGE_TYPE_HELLO:
307 if (GNUNET_OK != GST_validation_handle_hello (message))
310 kill_session (plugin_name, session);
313 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
315 "Processing `%s' from `%s'\n", "PING", GST_plugins_a2s (address));
317 != GST_validation_handle_ping (&address->peer, message, address, session))
318 kill_session (plugin_name, session);
320 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
321 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
322 "Processing `%s' from `%s'\n", "PONG",
323 GST_plugins_a2s (address));
324 if (GNUNET_OK != GST_validation_handle_pong (&address->peer, message))
327 kill_session (plugin_name, session);
330 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT:
332 != GST_neighbours_handle_connect (message, &address->peer, address, session))
335 kill_session (plugin_name, session);
338 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK:
340 != GST_neighbours_handle_connect_ack (message, &address->peer, address, session))
342 kill_session (plugin_name, session);
345 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
347 != GST_neighbours_handle_session_ack (message, &address->peer, address, session))
350 kill_session (plugin_name, session);
353 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
354 GST_neighbours_handle_disconnect_message (&address->peer, message);
356 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
357 GST_neighbours_keepalive (&address->peer, message);
359 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
360 GST_neighbours_keepalive_response (&address->peer, message);
363 /* should be payload */
364 GNUNET_STATISTICS_update (GST_stats, gettext_noop
365 ("# bytes payload received"), ntohs (message->size), GNUNET_NO);
366 GST_neighbours_notify_payload_recv (&address->peer, address, session, message);
367 ret = process_payload (&address->peer, address, session, message);
371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
372 "Allowing receive from peer %s to continue in %s\n", GNUNET_i2s (&address->peer),
373 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
378 * Function that will be called for each address the transport
379 * is aware that it might be reachable under. Update our HELLO.
381 * @param cls name of the plugin (const char*)
382 * @param add_remove should the address added (YES) or removed (NO) from the
383 * set of valid addresses?
384 * @param address the address to add or remove
387 plugin_env_address_change_notification (void *cls, int add_remove,
388 const struct GNUNET_HELLO_Address *address)
390 GST_hello_modify_addresses (add_remove, address);
394 * Function that will be called whenever the plugin internally
395 * cleans up a session pointer and hence the service needs to
396 * discard all of those sessions as well. Plugins that do not
397 * use sessions can simply omit calling this function and always
398 * use NULL wherever a session pointer is needed. This function
399 * should be called BEFORE a potential "TransmitContinuation"
400 * from the "TransmitFunction".
403 * @param address which address was the session for
404 * @param session which session is being destoyed
407 plugin_env_session_end (void *cls, const struct GNUNET_HELLO_Address *address,
408 struct Session *session)
410 struct SessionKiller *sk;
424 GNUNET_assert(strlen (address->transport_name) > 0);
425 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Session %p to peer `%s' ended \n",
426 session, GNUNET_i2s (&address->peer));
428 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
429 "Notification from plugin `%s' about terminated %s session %p from peer `%s' address `%s'\n",
430 address->transport_name,
431 GNUNET_HELLO_address_check_option (address,
432 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound", session,
433 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
435 GST_neighbours_session_terminated (&address->peer, session);
437 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
438 "transport-ats", "Telling ATS to destroy session %p from peer %s\n",
439 session, GNUNET_i2s (&address->peer));
441 /* Tell ATS that session has ended */
442 GNUNET_ATS_address_destroyed (GST_ats, address, session);
443 for (sk = sk_head; NULL != sk; sk = sk->next)
445 if (sk->session == session)
447 GNUNET_CONTAINER_DLL_remove(sk_head, sk_tail, sk);
448 GNUNET_SCHEDULER_cancel (sk->task);
456 * Function that will be called to figure if an address is an loopback,
457 * LAN, WAN etc. address
460 * @param addr binary address
461 * @param addrlen length of the address
462 * @return ATS Information containing the network type
464 static struct GNUNET_ATS_Information
465 plugin_env_address_to_type (void *cls, const struct sockaddr *addr,
468 struct GNUNET_ATS_Information ats;
470 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
471 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
477 if (((addr->sa_family != AF_INET) && (addrlen != sizeof(struct sockaddr_in)))
478 && ((addr->sa_family != AF_INET6)
479 && (addrlen != sizeof(struct sockaddr_in6)))
480 && (addr->sa_family != AF_UNIX))
482 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
483 "Malformed address with length %u `%s'\n", addrlen,
484 GNUNET_a2s (addr, addrlen));
488 return GNUNET_ATS_address_get_type (GST_ats, addr, addrlen);
492 * Notify ATS about the new address including the network this address is
495 * @param address the address
496 * @param session the session
497 * @param ats ats information
498 * @param ats_count number of @a ats information
501 GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
502 struct Session *session, const struct GNUNET_ATS_Information *ats,
505 struct GNUNET_TRANSPORT_PluginFunctions *papi;
506 struct GNUNET_ATS_Information ats2[ats_count + 1];
509 /* valid new address, let ATS know! */
510 if (NULL == address->transport_name)
515 if (NULL == (papi = GST_plugins_find (address->transport_name)))
517 /* we don't have the plugin for this address */
522 if (GNUNET_YES == GNUNET_ATS_session_known (GST_ats, address, session))
524 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
525 "ATS already knows the address, not passing it on again\n");
529 net = papi->get_network (NULL, session);
530 if (GNUNET_ATS_NET_UNSPECIFIED == net)
532 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
533 _("Could not obtain a valid network for `%s' %s (%s)\n"),
534 GNUNET_i2s (&address->peer), GST_plugins_a2s (address),
535 address->transport_name);
538 ats2[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
539 ats2[0].value = htonl (net);
540 memcpy (&ats2[1], ats, sizeof(struct GNUNET_ATS_Information) * ats_count);
541 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
542 "Notifying ATS about peer `%s''s new address `%s' session %p in network %s\n",
543 GNUNET_i2s (&address->peer),
544 (0 == address->address_length) ? "<inbound>" : GST_plugins_a2s (address),
545 session, GNUNET_ATS_print_network_type (net));
546 GNUNET_ATS_address_add (GST_ats, address, session, ats2, ats_count + 1);
550 * Notify ATS about property changes to an address
552 * @param peer the peer
553 * @param address the address
554 * @param session the session
555 * @param ats performance information
556 * @param ats_count number of elements in @a ats
559 GST_ats_update_metrics (const struct GNUNET_PeerIdentity *peer,
560 const struct GNUNET_HELLO_Address *address, struct Session *session,
561 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
563 struct GNUNET_ATS_Information *ats_new;
565 if (GNUNET_NO == GNUNET_ATS_session_known (GST_ats, address, session))
568 /* Call to manipulation to manipulate ATS information */
569 ats_new = GST_manipulation_manipulate_metrics (peer, address, session, ats,
576 if (GNUNET_NO == GNUNET_ATS_address_update (GST_ats, address, session,
579 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
580 _("Address or session unknown: failed to update properties for peer `%s' plugin `%s' address `%s' session %p\n"),
581 GNUNET_i2s (peer), address->transport_name, GST_plugins_a2s (address),
584 GNUNET_free(ats_new);
588 * Function that will be called to update metrics for an address
591 * @param address address to update metrics for
592 * @param session the session
593 * @param ats the ats information to update
594 * @param ats_count the number of @a ats elements
597 plugin_env_update_metrics (void *cls,
598 const struct GNUNET_HELLO_Address *address,
599 struct Session *session,
600 const struct GNUNET_ATS_Information *ats,
603 if ((NULL == ats) || (0 == ats_count))
605 GNUNET_assert(NULL != GST_ats);
607 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
608 "Updating metrics for peer `%s' address %s session %p\n",
609 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
610 GST_ats_update_metrics (&address->peer, address, session, ats, ats_count);
614 * Plugin tells transport service about a new inbound session
617 * @param address the address
618 * @param session the new session
619 * @param ats ats information
620 * @param ats_count number of @a ats information
623 plugin_env_session_start (void *cls, struct GNUNET_HELLO_Address *address,
624 struct Session *session, const struct GNUNET_ATS_Information *ats,
637 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
638 "Notification from plugin `%s' about new %s session %p from peer `%s' address `%s'\n",
639 address->transport_name,
640 GNUNET_HELLO_address_check_option (address,
641 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
642 session, GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
643 GST_ats_add_address (address, session, ats, ats_count);
647 * Function called by ATS to notify the callee that the
648 * assigned bandwidth or address for a given peer was changed. If the
649 * callback is called with address/bandwidth assignments of zero, the
650 * ATS disconnect function will still be called once the disconnect
654 * @param peer the peer this address is intended for
655 * @param address address to use (for peer given in address)
656 * @param session session to use (if available)
657 * @param bandwidth_out assigned outbound bandwidth for the connection in NBO,
658 * 0 to disconnect from peer
659 * @param bandwidth_in assigned inbound bandwidth for the connection in NBO,
660 * 0 to disconnect from peer
661 * @param ats ATS information
662 * @param ats_count number of @a ats elements
665 ats_request_address_change (void *cls,
666 const struct GNUNET_PeerIdentity *peer,
667 const struct GNUNET_HELLO_Address *address,
668 struct Session *session,
669 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
670 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
671 const struct GNUNET_ATS_Information *ats,
674 uint32_t bw_in = ntohl (bandwidth_in.value__);
675 uint32_t bw_out = ntohl (bandwidth_out.value__);
677 /* ATS tells me to disconnect from peer */
678 if ((0 == bw_in) && (0 == bw_out))
680 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
681 "ATS tells me to disconnect from peer `%s'\n",
682 GNUNET_i2s (&address->peer));
683 GST_neighbours_force_disconnect (&address->peer);
687 GST_neighbours_switch_to_address (&address->peer, address, session, ats,
688 ats_count, bandwidth_in, bandwidth_out);
692 * Function called to notify transport users that another
693 * peer connected to us.
696 * @param peer the peer that connected
697 * @param bandwidth_in inbound bandwidth in NBO
698 * @param bandwidth_out outbound bandwidth in NBO
701 neighbours_connect_notification (void *cls,
702 const struct GNUNET_PeerIdentity *peer,
703 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
704 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
706 size_t len = sizeof(struct ConnectInfoMessage);
707 char buf[len] GNUNET_ALIGN;
708 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
711 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
712 "We are now connected to peer `%s' and %u peers in total\n",
713 GNUNET_i2s (peer), connections);
714 connect_msg->header.size = htons (sizeof(buf));
715 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
716 connect_msg->id = *peer;
717 connect_msg->quota_in = bandwidth_in;
718 connect_msg->quota_out = bandwidth_out;
719 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
723 * Function called to notify transport users that another
724 * peer disconnected from us.
727 * @param peer the peer that disconnected
730 neighbours_disconnect_notification (void *cls,
731 const struct GNUNET_PeerIdentity *peer)
733 struct DisconnectInfoMessage disconnect_msg;
736 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
737 "Peer `%s' disconnected and we are connected to %u peers\n",
738 GNUNET_i2s (peer), connections);
740 GST_manipulation_peer_disconnect (peer);
741 disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
742 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
743 disconnect_msg.reserved = htonl (0);
744 disconnect_msg.peer = *peer;
745 GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
749 * Function called to notify transport users that a neighbour peer changed its
753 * @param peer peer this update is about (never NULL)
754 * @param address address, NULL on disconnect
755 * @param state current state this peer is in
756 * @param state_timeout timeout for the current state of the peer
757 * @param bandwidth_in bandwidth assigned inbound
758 * @param bandwidth_out bandwidth assigned outbound
761 neighbours_changed_notification (void *cls,
762 const struct GNUNET_PeerIdentity *peer,
763 const struct GNUNET_HELLO_Address *address,
764 enum GNUNET_TRANSPORT_PeerState state,
765 struct GNUNET_TIME_Absolute state_timeout,
766 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
767 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
769 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
770 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
772 (NULL != address) ? GST_plugins_a2s (address) : "<none>",
773 GNUNET_TRANSPORT_ps2s (state),
774 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
776 GST_clients_broadcast_peer_notification (peer, address, state, state_timeout);
780 * Function called to notify transport users that a neighbour peer changed its
784 * @param peer peer this update is about (never NULL)
785 * @param address address (never NULL)
786 * @param last_validation point in time when last validation was performed
787 * @param valid_until point in time how long address is valid
788 * @param next_validation point in time when next validation will be performed
789 * @param state state of validation notification
792 validation_changed_notification (void *cls,
793 const struct GNUNET_PeerIdentity *peer,
794 const struct GNUNET_HELLO_Address *address,
795 struct GNUNET_TIME_Absolute last_validation,
796 struct GNUNET_TIME_Absolute valid_until,
797 struct GNUNET_TIME_Absolute next_validation,
798 enum GNUNET_TRANSPORT_ValidationState state)
800 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
801 "Notifying about change for for validation entry for peer `%s' with address `%s'\n",
803 (NULL != address) ? GST_plugins_a2s (address) : "<none>");
805 GST_clients_broadcast_validation_notification (peer, address,
806 last_validation, valid_until, next_validation, state);
810 * Function called when the service shuts down. Unloads our plugins
811 * and cancels pending validations.
813 * @param cls closure, unused
814 * @param tc task context (unused)
817 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
819 GST_neighbours_stop ();
820 GST_validation_stop ();
821 GST_plugins_unload ();
823 GNUNET_ATS_scheduling_done (GST_ats);
826 GST_blacklist_stop ();
828 GST_manipulation_stop ();
830 if (NULL != GST_peerinfo)
832 GNUNET_PEERINFO_disconnect (GST_peerinfo);
835 if (NULL != GST_stats)
837 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
840 if (NULL != GST_my_private_key)
842 GNUNET_free(GST_my_private_key);
843 GST_my_private_key = NULL;
849 * Initiate transport service.
852 * @param server the initialized server
853 * @param c configuration to use
856 run (void *cls, struct GNUNET_SERVER_Handle *server,
857 const struct GNUNET_CONFIGURATION_Handle *c)
860 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
861 long long unsigned int max_fd_cfg;
869 != GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
872 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
873 _("Transport service is lacking key configuration settings. Exiting.\n"));
874 GNUNET_SCHEDULER_shutdown ();
878 != GNUNET_CONFIGURATION_get_value_time (c, "transport",
879 "HELLO_EXPIRATION", &hello_expiration))
881 hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
884 pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
885 GNUNET_free(keyfile);
886 GNUNET_assert(NULL != pk);
887 GST_my_private_key = pk;
889 GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
890 GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
891 GNUNET_CRYPTO_eddsa_key_get_public (GST_my_private_key,
892 &GST_my_identity.public_key);
893 GNUNET_assert(NULL != GST_my_private_key);
895 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "My identity is `%4s'\n",
896 GNUNET_i2s (&GST_my_identity));
898 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
900 if (NULL == GST_peerinfo)
902 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
903 _("Could not access PEERINFO service. Exiting.\n"));
904 GNUNET_SCHEDULER_shutdown ();
911 struct rlimit r_file;
912 if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
914 max_fd_rlimit = r_file.rlim_cur;
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Maximum number of open files was: %u/%u\n",
920 max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
922 GNUNET_CONFIGURATION_get_value_number (GST_cfg, "transport", "MAX_FD",
925 if (max_fd_cfg > max_fd_rlimit)
928 max_fd = max_fd_rlimit;
929 if (max_fd < DEFAULT_MAX_FDS)
930 max_fd = DEFAULT_MAX_FDS;
932 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
933 "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
934 max_fd, (max_fd / 3), (max_fd / 3) * 2);
936 friend_only = GNUNET_CONFIGURATION_get_value_yesno (GST_cfg, "topology",
938 if (GNUNET_SYSERR == friend_only)
939 friend_only = GNUNET_NO; /* According to topology defaults */
940 /* start subsystems */
941 GST_hello_start (friend_only, &process_hello_update, NULL );
942 GNUNET_assert(NULL != GST_hello_get());
943 GST_blacklist_start (GST_server, GST_cfg, &GST_my_identity);
944 GST_ats = GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change,
946 GST_manipulation_init (GST_cfg);
947 GST_plugins_load (&GST_manipulation_recv,
948 &GST_neighbours_register_quota_notification,
949 &GST_neighbours_unregister_quota_notification,
950 &plugin_env_address_change_notification,
951 &plugin_env_session_start,
952 &plugin_env_session_end,
953 &plugin_env_address_to_type,
954 &plugin_env_update_metrics);
955 GST_neighbours_start (NULL,
956 &neighbours_connect_notification,
957 &neighbours_disconnect_notification,
958 &neighbours_changed_notification,
960 GST_clients_start (GST_server);
961 GST_validation_start (&validation_changed_notification, NULL, (max_fd / 3));
965 * The main function for the transport service.
967 * @param argc number of arguments from the command line
968 * @param argv command line arguments
969 * @return 0 ok, 1 on error
972 main (int argc, char * const *argv)
976 == GNUNET_SERVICE_run (argc, argv, "transport",
977 GNUNET_SERVICE_OPTION_NONE, &run, NULL )) ? 0 : 1;
980 /* end of file gnunet-service-transport.c */