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"
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 GNUNET_SCHEDULER_TaskIdentifier task;
81 struct GNUNET_STATISTICS_Handle *GST_stats;
84 * Configuration handle.
86 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
89 * Configuration handle.
91 struct GNUNET_PeerIdentity GST_my_identity;
94 * Handle to peerinfo service.
96 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
99 * Handle to our service's server.
101 static struct GNUNET_SERVER_Handle *GST_server;
106 struct GNUNET_CRYPTO_EddsaPrivateKey *GST_my_private_key;
111 struct GNUNET_ATS_SchedulingHandle *GST_ats;
114 * Hello address expiration
116 struct GNUNET_TIME_Relative hello_expiration;
119 * DEBUGGING connection counter
121 static int connections;
124 * Head of DLL of asynchronous tasks to kill sessions.
126 static struct SessionKiller *sk_head;
129 * Tail of DLL of asynchronous tasks to kill sessions.
131 static struct SessionKiller *sk_tail;
135 * Transmit our HELLO message to the given (connected) neighbour.
137 * @param cls the 'HELLO' message
138 * @param target a connected neighbour
139 * @param address the address
140 * @param bandwidth_in inbound quota in NBO
141 * @param bandwidth_out outbound quota in NBO
144 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
145 const struct GNUNET_HELLO_Address *address,
146 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
147 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
149 const struct GNUNET_MessageHeader *hello = cls;
151 GST_neighbours_send (target, (const char *) hello, ntohs (hello->size),
152 hello_expiration, NULL, NULL);
157 * My HELLO has changed. Tell everyone who should know.
160 * @param hello new HELLO
163 process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
165 GST_clients_broadcast (hello, GNUNET_NO);
166 GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
171 * We received some payload. Prepare to pass it on to our clients.
173 * @param peer (claimed) identity of the other peer
174 * @param address the address
175 * @param session session used
176 * @param message the message to process
177 * @return how long the plugin should wait until receiving more data
179 static struct GNUNET_TIME_Relative
180 process_payload (const struct GNUNET_PeerIdentity *peer,
181 const struct GNUNET_HELLO_Address *address,
182 struct Session *session,
183 const struct GNUNET_MessageHeader *message)
185 struct GNUNET_TIME_Relative ret;
187 struct InboundMessage *im;
188 size_t msg_size = ntohs (message->size);
190 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",
200 ntohs (message->type), GNUNET_i2s (peer));
201 GNUNET_STATISTICS_update (GST_stats,
203 ("# bytes payload discarded due to not connected peer"),
204 msg_size, GNUNET_NO);
208 GST_ats_add_address (address, session, NULL, 0);
210 if (GNUNET_YES != do_forward)
212 im = (struct InboundMessage *) buf;
213 im->header.size = htons (size);
214 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
216 memcpy (&im[1], message, ntohs (message->size));
217 GST_clients_broadcast (&im->header, GNUNET_YES);
223 * Task to asynchronously terminate a session.
225 * @param cls the `struct SessionKiller` with the information for the kill
226 * @param tc scheduler context
229 kill_session_task (void *cls,
230 const struct GNUNET_SCHEDULER_TaskContext *tc)
232 struct SessionKiller *sk = cls;
234 sk->task = GNUNET_SCHEDULER_NO_TASK;
235 GNUNET_CONTAINER_DLL_remove (sk_head, sk_tail, sk);
236 sk->plugin->disconnect_session (sk->plugin->cls,
243 * Force plugin to terminate session due to communication
246 * @param plugin_name name of the plugin
247 * @param session session to termiante
250 kill_session (const char *plugin_name,
251 struct Session *session)
253 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
254 struct SessionKiller *sk;
256 for (sk = sk_head; NULL != sk; sk = sk->next)
257 if (sk->session == session)
259 plugin = GST_plugins_find (plugin_name);
265 /* need to issue disconnect asynchronously */
266 sk = GNUNET_new (struct SessionKiller);
267 sk->session = session;
269 sk->task = GNUNET_SCHEDULER_add_now (&kill_session_task,
271 GNUNET_CONTAINER_DLL_insert (sk_head, sk_tail, sk);
276 * Function called by the transport for each received message.
278 * @param cls closure, const char* with the name of the plugin we received the message from
279 * @param peer (claimed) identity of the other peer
280 * @param message the message, NULL if we only care about
281 * learning about the delay until we should receive again
282 * @param session identifier used for this session (NULL for plugins
283 * that do not offer bi-directional communication to the sender
284 * using the same "connection")
285 * @param sender_address binary address of the sender (if we established the
286 * connection or are otherwise sure of it; should be NULL
287 * for inbound TCP/UDP connections since it it not clear
288 * that we could establish ourselves a connection to that
289 * IP address and get the same system)
290 * @param sender_address_len number of bytes in @a sender_address
291 * @return how long the plugin should wait until receiving more data
292 * (plugins that do not support this, can ignore the return value)
294 struct GNUNET_TIME_Relative
295 GST_receive_callback (void *cls,
296 const struct GNUNET_PeerIdentity *peer,
297 const struct GNUNET_MessageHeader *message,
298 struct Session *session,
299 const char *sender_address,
300 uint16_t sender_address_len)
302 const char *plugin_name = cls;
303 struct GNUNET_TIME_Relative ret;
304 struct GNUNET_HELLO_Address address;
307 address.peer = *peer;
308 address.address = sender_address;
309 address.address_length = sender_address_len;
310 address.transport_name = plugin_name;
311 ret = GNUNET_TIME_UNIT_ZERO;
314 type = ntohs (message->type);
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Received Message with type %u from peer `%s'\n",
320 GNUNET_STATISTICS_update (GST_stats,
322 ("# bytes total received"),
323 ntohs (message->size), GNUNET_NO);
324 GST_neighbours_notify_data_recv (peer, &address, session, message);
328 case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
329 /* Legacy HELLO message, discard */
331 case GNUNET_MESSAGE_TYPE_HELLO:
333 GST_validation_handle_hello (message))
336 kill_session (plugin_name, session);
339 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
341 "Processing `%s' from `%s'\n", "PING",
343 NULL) ? GST_plugins_a2s (&address) : TRANSPORT_SESSION_INBOUND_STRING);
345 GST_validation_handle_ping (peer, message, &address, session))
346 kill_session (plugin_name, session);
348 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
350 "Processing `%s' from `%s'\n", "PONG",
352 NULL) ? GST_plugins_a2s (&address) : TRANSPORT_SESSION_INBOUND_STRING);
354 GST_validation_handle_pong (peer, message))
357 kill_session (plugin_name, session);
360 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT:
362 GST_neighbours_handle_connect (message, peer, &address, session))
365 kill_session (plugin_name, session);
368 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK:
370 GST_neighbours_handle_connect_ack (message, peer, &address, session))
372 kill_session (plugin_name, session);
375 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
377 GST_neighbours_handle_session_ack (message, peer, &address, session))
380 kill_session (plugin_name, session);
383 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
384 GST_neighbours_handle_disconnect_message (peer, message);
386 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
387 GST_neighbours_keepalive (peer, message);
389 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
390 GST_neighbours_keepalive_response (peer, message);
393 /* should be payload */
394 GNUNET_STATISTICS_update (GST_stats,
396 ("# bytes payload received"),
397 ntohs (message->size), GNUNET_NO);
398 GST_neighbours_notify_payload_recv (peer, &address, session, message);
399 ret = process_payload (peer, &address, session, message);
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Allowing receive from peer %s to continue in %s\n",
406 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
412 * Function that will be called for each address the transport
413 * is aware that it might be reachable under. Update our HELLO.
415 * @param cls name of the plugin (const char*)
416 * @param add_remove should the address added (YES) or removed (NO) from the
417 * set of valid addresses?
418 * @param addr one of the addresses of the host
419 * the specific address format depends on the transport
420 * @param addrlen length of the @a addr
421 * @param dest_plugin destination plugin to use this address with
424 plugin_env_address_change_notification (void *cls, int add_remove,
425 const void *addr, size_t addrlen,
426 const char *dest_plugin)
428 struct GNUNET_HELLO_Address address;
430 address.peer = GST_my_identity;
431 address.transport_name = dest_plugin;
432 address.address = addr;
433 address.address_length = addrlen;
434 GST_hello_modify_addresses (add_remove, &address);
439 * Function that will be called whenever the plugin internally
440 * cleans up a session pointer and hence the service needs to
441 * discard all of those sessions as well. Plugins that do not
442 * use sessions can simply omit calling this function and always
443 * use NULL wherever a session pointer is needed. This function
444 * should be called BEFORE a potential "TransmitContinuation"
445 * from the "TransmitFunction".
448 * @param peer which peer was the session for
449 * @param session which session is being destoyed
452 plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
453 struct Session *session)
455 const char *transport_name = cls;
456 struct GNUNET_HELLO_Address address;
457 struct SessionKiller *sk;
459 GNUNET_assert (strlen (transport_name) > 0);
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Session %p to peer `%s' ended \n",
465 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
467 "Telling ATS to destroy session %p from peer %s\n",
468 session, GNUNET_i2s (peer));
469 address.peer = *peer;
470 address.address = NULL;
471 address.address_length = 0;
472 address.transport_name = transport_name;
473 GST_neighbours_session_terminated (peer, session);
475 /* Tell ATS that session has ended */
476 GNUNET_ATS_address_destroyed (GST_ats, &address, session);
477 for (sk = sk_head; NULL != sk; sk = sk->next)
479 if (sk->session == session)
481 GNUNET_CONTAINER_DLL_remove (sk_head, sk_tail, sk);
482 GNUNET_SCHEDULER_cancel (sk->task);
491 * Function that will be called to figure if an address is an loopback,
492 * LAN, WAN etc. address
495 * @param addr binary address
496 * @param addrlen length of the address
497 * @return ATS Information containing the network type
499 static struct GNUNET_ATS_Information
500 plugin_env_address_to_type (void *cls,
501 const struct sockaddr *addr,
504 struct GNUNET_ATS_Information ats;
506 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
507 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
513 if (((addr->sa_family != AF_INET) && (addrlen != sizeof (struct sockaddr_in))) &&
514 ((addr->sa_family != AF_INET6) && (addrlen != sizeof (struct sockaddr_in6))) &&
515 (addr->sa_family != AF_UNIX))
517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
518 "Malformed address with length %u `%s'\n",
520 GNUNET_a2s (addr, addrlen));
524 return GNUNET_ATS_address_get_type (GST_ats, addr, addrlen);
529 * Notify ATS about the new address including the network this address is
532 * @param address the address
533 * @param session the session
534 * @param ats ats information
535 * @param ats_count number of @a ats information
538 GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
539 struct Session *session,
540 const struct GNUNET_ATS_Information *ats,
543 struct GNUNET_TRANSPORT_PluginFunctions *papi;
544 struct GNUNET_ATS_Information ats2[ats_count + 1];
547 /* valid new address, let ATS know! */
548 if (NULL == address->transport_name)
553 if (NULL == (papi = GST_plugins_find (address->transport_name)))
555 /* we don't have the plugin for this address */
560 if (GNUNET_YES == GNUNET_ATS_session_known (GST_ats, address, session))
563 net = papi->get_network (NULL, session);
564 if (GNUNET_ATS_NET_UNSPECIFIED == net)
566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
567 _("Could not obtain a valid network for `%s' %s (%s)\n"),
568 GNUNET_i2s (&address->peer),
569 GST_plugins_a2s (address),
570 address->transport_name);
573 ats2[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
574 ats2[0].value = htonl(net);
575 memcpy (&ats2[1], ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577 "Notifying ATS about peer `%s''s new address `%s' session %p in network %s\n",
578 GNUNET_i2s (&address->peer),
579 (0 == address->address_length) ? "<inbound>" : GST_plugins_a2s (address),
581 GNUNET_ATS_print_network_type(net));
582 GNUNET_ATS_address_add (GST_ats,
584 ats2, ats_count + 1);
589 * Notify ATS about property changes to an address
591 * @param peer the peer
592 * @param address the address
593 * @param session the session
594 * @param ats performance information
595 * @param ats_count number of elements in @a ats
598 GST_ats_update_metrics (const struct GNUNET_PeerIdentity *peer,
599 const struct GNUNET_HELLO_Address *address,
600 struct Session *session,
601 const struct GNUNET_ATS_Information *ats,
604 struct GNUNET_ATS_Information *ats_new;
606 if (GNUNET_NO == GNUNET_ATS_session_known (GST_ats, address, session))
609 /* Call to manipulation to manipulate ATS information */
610 ats_new = GST_manipulation_manipulate_metrics (peer, address, session, ats,
617 if (GNUNET_NO == GNUNET_ATS_address_update (GST_ats,
618 address, session, ats_new, ats_count))
620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
621 _("Address or session unknown: failed to update properties for peer `%s' plugin `%s' address `%s' session %p\n"),
623 address->transport_name,
624 GST_plugins_a2s (address),
627 GNUNET_free (ats_new);
632 * Function that will be called to figure if an address is an loopback,
633 * LAN, WAN etc. address
636 * @param peer the peer
637 * @param address binary address
638 * @param address_len length of the @a address
639 * @param session the session
640 * @param ats the ats information to update
641 * @param ats_count the number of @a ats elements
644 plugin_env_update_metrics (void *cls,
645 const struct GNUNET_PeerIdentity *peer,
647 uint16_t address_len,
648 struct Session *session,
649 const struct GNUNET_ATS_Information *ats,
652 const char *plugin_name = cls;
653 struct GNUNET_HELLO_Address haddress;
655 if ((NULL == ats) || (0 == ats_count))
657 GNUNET_assert (NULL != GST_ats);
659 haddress.peer = *peer;
660 haddress.address = address;
661 haddress.address_length = address_len;
662 haddress.transport_name = plugin_name;
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Updating metrics for peer `%s' address %s session %p\n",
667 GST_plugins_a2s (&haddress),
669 GST_ats_update_metrics (peer, &haddress, session, ats, ats_count);
674 * Plugin tells transport service about a new (inbound) session
677 * @param peer the peer
678 * @param plugin plugin name
679 * @param address address
680 * @param address_len @a address length
681 * @param session the new session
682 * @param ats ats information
683 * @param ats_count number of @a ats information
686 plugin_env_session_start (void *cls,
687 const struct GNUNET_PeerIdentity *peer,
689 const void *address, uint16_t address_len,
690 struct Session *session,
691 const struct GNUNET_ATS_Information *ats,
694 struct GNUNET_HELLO_Address *addr;
712 addr = GNUNET_HELLO_address_allocate (peer, plugin, address, address_len);
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Notification from plugin `%s' about new session %p from peer `%s' address `%s'\n",
718 GST_plugins_a2s (addr));
719 GST_ats_add_address (addr, session, ats, ats_count);
725 * Function called by ATS to notify the callee that the
726 * assigned bandwidth or address for a given peer was changed. If the
727 * callback is called with address/bandwidth assignments of zero, the
728 * ATS disconnect function will still be called once the disconnect
732 * @param address address to use (for peer given in address)
733 * @param session session to use (if available)
734 * @param bandwidth_out assigned outbound bandwidth for the connection in NBO,
735 * 0 to disconnect from peer
736 * @param bandwidth_in assigned inbound bandwidth for the connection in NBO,
737 * 0 to disconnect from peer
738 * @param ats ATS information
739 * @param ats_count number of @a ats elements
742 ats_request_address_change (void *cls,
743 const struct GNUNET_HELLO_Address *address,
744 struct Session *session,
745 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
746 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
747 const struct GNUNET_ATS_Information *ats,
750 uint32_t bw_in = ntohl (bandwidth_in.value__);
751 uint32_t bw_out = ntohl (bandwidth_out.value__);
753 /* ATS tells me to disconnect from peer */
754 if ((bw_in == 0) && (bw_out == 0))
756 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
757 "ATS tells me to disconnect from peer `%s'\n",
758 GNUNET_i2s (&address->peer));
759 GST_neighbours_force_disconnect (&address->peer);
762 GST_neighbours_switch_to_address (&address->peer, address, session, ats,
763 ats_count, bandwidth_in,
769 * Function called to notify transport users that another
770 * peer connected to us.
773 * @param peer the peer that connected
774 * @param bandwidth_in inbound bandwidth in NBO
775 * @param bandwidth_out outbound bandwidth in NBO
778 neighbours_connect_notification (void *cls,
779 const struct GNUNET_PeerIdentity *peer,
780 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
781 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
783 size_t len = sizeof (struct ConnectInfoMessage);
784 char buf[len] GNUNET_ALIGN;
785 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
788 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
789 "We are now connected to peer `%s' and %u peers in total\n",
790 GNUNET_i2s (peer), connections);
791 connect_msg->header.size = htons (sizeof (buf));
792 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
793 connect_msg->id = *peer;
794 connect_msg->quota_in = bandwidth_in;
795 connect_msg->quota_out = bandwidth_out;
796 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
801 * Function called to notify transport users that another
802 * peer disconnected from us.
805 * @param peer the peer that disconnected
808 neighbours_disconnect_notification (void *cls,
809 const struct GNUNET_PeerIdentity *peer)
811 struct DisconnectInfoMessage disconnect_msg;
814 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
815 "Peer `%s' disconnected and we are connected to %u peers\n",
816 GNUNET_i2s (peer), connections);
818 GST_manipulation_peer_disconnect (peer);
819 disconnect_msg.header.size = htons (sizeof (struct DisconnectInfoMessage));
820 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
821 disconnect_msg.reserved = htonl (0);
822 disconnect_msg.peer = *peer;
823 GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
828 * Function called to notify transport users that a neighbour peer changed its
832 * @param peer peer this update is about (never NULL)
833 * @param address address, NULL on disconnect
836 neighbours_address_notification (void *cls,
837 const struct GNUNET_PeerIdentity *peer,
838 const struct GNUNET_HELLO_Address *address)
840 GST_clients_broadcast_address_notification (peer, address);
845 * Function called when the service shuts down. Unloads our plugins
846 * and cancels pending validations.
848 * @param cls closure, unused
849 * @param tc task context (unused)
852 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
854 GST_neighbours_stop ();
855 GST_validation_stop ();
856 GST_plugins_unload ();
858 GNUNET_ATS_scheduling_done (GST_ats);
861 GST_blacklist_stop ();
863 GST_manipulation_stop ();
865 if (NULL != GST_peerinfo)
867 GNUNET_PEERINFO_disconnect (GST_peerinfo);
870 if (NULL != GST_stats)
872 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
875 if (NULL != GST_my_private_key)
877 GNUNET_free (GST_my_private_key);
878 GST_my_private_key = NULL;
885 * Initiate transport service.
888 * @param server the initialized server
889 * @param c configuration to use
892 run (void *cls, struct GNUNET_SERVER_Handle *server,
893 const struct GNUNET_CONFIGURATION_Handle *c)
896 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
897 long long unsigned int max_fd_cfg;
905 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
908 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
909 _("Transport service is lacking key configuration settings. Exiting.\n"));
910 GNUNET_SCHEDULER_shutdown ();
914 GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION",
917 hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
920 pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
921 GNUNET_free (keyfile);
922 GNUNET_assert (NULL != pk);
923 GST_my_private_key = pk;
925 GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
926 GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
927 GNUNET_CRYPTO_eddsa_key_get_public (GST_my_private_key,
928 &GST_my_identity.public_key);
929 GNUNET_assert (NULL != GST_my_private_key);
931 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
932 "My identity is `%4s'\n", GNUNET_i2s (&GST_my_identity));
934 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
936 if (NULL == GST_peerinfo)
938 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
939 _("Could not access PEERINFO service. Exiting.\n"));
940 GNUNET_SCHEDULER_shutdown ();
947 struct rlimit r_file;
948 if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
950 max_fd_rlimit = r_file.rlim_cur;
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Maximum number of open files was: %u/%u\n",
956 max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
958 GNUNET_CONFIGURATION_get_value_number (GST_cfg, "transport", "MAX_FD", &max_fd_cfg);
960 if (max_fd_cfg > max_fd_rlimit)
963 max_fd = max_fd_rlimit;
964 if (max_fd < DEFAULT_MAX_FDS)
965 max_fd = DEFAULT_MAX_FDS;
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
969 max_fd, (max_fd / 3) , (max_fd / 3) * 2);
971 friend_only = GNUNET_CONFIGURATION_get_value_yesno(GST_cfg, "topology","FRIENDS-ONLY");
972 if (GNUNET_SYSERR == friend_only)
973 friend_only = GNUNET_NO; /* According to topology defaults */
974 /* start subsystems */
975 GST_hello_start (friend_only, &process_hello_update, NULL);
976 GNUNET_assert (NULL != GST_hello_get());
977 GST_blacklist_start (GST_server, GST_cfg, &GST_my_identity);
979 GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
980 GST_manipulation_init (GST_cfg);
981 GST_plugins_load (&GST_manipulation_recv,
982 &plugin_env_address_change_notification,
983 &plugin_env_session_start,
984 &plugin_env_session_end,
985 &plugin_env_address_to_type,
986 &plugin_env_update_metrics);
987 GST_neighbours_start (NULL,
988 &neighbours_connect_notification,
989 &neighbours_disconnect_notification,
990 &neighbours_address_notification,
992 GST_clients_start (GST_server);
993 GST_validation_start ((max_fd / 3));
998 * The main function for the transport service.
1000 * @param argc number of arguments from the command line
1001 * @param argv command line arguments
1002 * @return 0 ok, 1 on error
1005 main (int argc, char *const *argv)
1007 return (GNUNET_OK ==
1008 GNUNET_SERVICE_run (argc, argv, "transport",
1009 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1012 /* end of file gnunet-service-transport.c */