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"
48 struct GNUNET_STATISTICS_Handle *GST_stats;
51 * Configuration handle.
53 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
56 * Configuration handle.
58 struct GNUNET_PeerIdentity GST_my_identity;
61 * Handle to peerinfo service.
63 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
66 * Hostkey generation context
68 struct GNUNET_CRYPTO_EccKeyGenerationContext *GST_keygen;
71 * Handle to our service's server.
73 static struct GNUNET_SERVER_Handle *GST_server;
78 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded GST_my_public_key;
83 struct GNUNET_CRYPTO_EccPrivateKey *GST_my_private_key;
88 struct GNUNET_ATS_SchedulingHandle *GST_ats;
91 * DEBUGGING connection counter
93 static int connections;
96 * Hello address expiration
98 struct GNUNET_TIME_Relative hello_expiration;
102 * Transmit our HELLO message to the given (connected) neighbour.
104 * @param cls the 'HELLO' message
105 * @param target a connected neighbour
106 * @param address the address
107 * @param bandwidth_in inbound quota in NBO
108 * @param bandwidth_out outbound quota in NBO
111 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
112 const struct GNUNET_HELLO_Address *address,
113 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
114 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
116 const struct GNUNET_MessageHeader *hello = cls;
118 GST_neighbours_send (target, (const char *) hello, ntohs (hello->size),
119 hello_expiration, NULL, NULL);
124 * My HELLO has changed. Tell everyone who should know.
127 * @param hello new HELLO
130 process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
132 GST_clients_broadcast (hello, GNUNET_NO);
133 GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
139 * We received some payload. Prepare to pass it on to our clients.
141 * @param peer (claimed) identity of the other peer
142 * @param address the address
143 * @param session session used
144 * @param message the message to process
145 * @return how long the plugin should wait until receiving more data
147 static struct GNUNET_TIME_Relative
148 process_payload (const struct GNUNET_PeerIdentity *peer,
149 const struct GNUNET_HELLO_Address *address,
150 struct Session *session,
151 const struct GNUNET_MessageHeader *message)
153 struct GNUNET_TIME_Relative ret;
155 struct InboundMessage *im;
156 size_t msg_size = ntohs (message->size);
158 sizeof (struct InboundMessage) + msg_size;
159 char buf[size] GNUNET_ALIGN;
161 ret = GNUNET_TIME_UNIT_ZERO;
162 do_forward = GNUNET_SYSERR;
163 ret = GST_neighbours_calculate_receive_delay (peer, msg_size, &do_forward);
165 if (!GST_neighbours_test_connected (peer))
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Discarded %u bytes type %u payload from peer `%s'\n", msg_size,
170 ntohs (message->type), GNUNET_i2s (peer));
172 GNUNET_STATISTICS_update (GST_stats,
174 ("# bytes payload discarded due to not connected peer "),
175 msg_size, GNUNET_NO);
179 if (do_forward != GNUNET_YES)
181 im = (struct InboundMessage *) buf;
182 im->header.size = htons (size);
183 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
185 memcpy (&im[1], message, ntohs (message->size));
187 GNUNET_ATS_address_add (GST_ats, address, session, NULL, 0);
188 GST_clients_broadcast (&im->header, GNUNET_YES);
195 * Function called by the transport for each received message.
196 * This function should also be called with "NULL" for the
197 * message to signal that the other peer disconnected.
199 * @param cls closure, const char* with the name of the plugin we received the message from
200 * @param peer (claimed) identity of the other peer
201 * @param message the message, NULL if we only care about
202 * learning about the delay until we should receive again -- FIXME!
203 * @param session identifier used for this session (NULL for plugins
204 * that do not offer bi-directional communication to the sender
205 * using the same "connection")
206 * @param sender_address binary address of the sender (if we established the
207 * connection or are otherwise sure of it; should be NULL
208 * for inbound TCP/UDP connections since it it not clear
209 * that we could establish ourselves a connection to that
210 * IP address and get the same system)
211 * @param sender_address_len number of bytes in sender_address
212 * @return how long the plugin should wait until receiving more data
213 * (plugins that do not support this, can ignore the return value)
215 struct GNUNET_TIME_Relative
216 GST_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
217 const struct GNUNET_MessageHeader *message,
218 struct Session *session,
219 const char *sender_address,
220 uint16_t sender_address_len)
222 const char *plugin_name = cls;
223 struct GNUNET_TIME_Relative ret;
224 struct GNUNET_HELLO_Address address;
227 address.peer = *peer;
228 address.address = sender_address;
229 address.address_length = sender_address_len;
230 address.transport_name = plugin_name;
231 ret = GNUNET_TIME_UNIT_ZERO;
234 type = ntohs (message->type);
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Message with type %u from peer `%s'\n", type, GNUNET_i2s (peer));
237 GNUNET_STATISTICS_update (GST_stats,
239 ("# bytes total received"),
240 ntohs (message->size), GNUNET_NO);
244 case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
245 /* Legacy HELLO message, discard */
247 case GNUNET_MESSAGE_TYPE_HELLO:
248 GST_validation_handle_hello (message);
250 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
252 "Processing `%s' from `%s'\n", "PING",
254 NULL) ? GST_plugins_a2s (&address) : "<inbound>");
255 GST_validation_handle_ping (peer, message, &address, session);
257 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
259 "Processing `%s' from `%s'\n", "PONG",
261 NULL) ? GST_plugins_a2s (&address) : "<inbound>");
262 GST_validation_handle_pong (peer, message);
264 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT:
265 GST_neighbours_handle_connect (message, peer, &address, session);
267 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK:
268 GST_neighbours_handle_connect_ack (message, peer, &address, session);
270 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
271 GST_neighbours_handle_session_ack (message, peer, &address, session);
273 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
274 GST_neighbours_handle_disconnect_message (peer, message);
276 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
277 GST_neighbours_keepalive (peer);
279 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
280 GST_neighbours_keepalive_response (peer);
283 /* should be payload */
284 GNUNET_STATISTICS_update (GST_stats,
286 ("# bytes payload received"),
287 ntohs (message->size), GNUNET_NO);
288 ret = process_payload (peer, &address, session, message);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Allowing receive from peer %s to continue in %llu ms\n",
294 GNUNET_i2s (peer), (unsigned long long) ret.rel_value);
300 * Function that will be called for each address the transport
301 * is aware that it might be reachable under. Update our HELLO.
303 * @param cls name of the plugin (const char*)
304 * @param add_remove should the address added (YES) or removed (NO) from the
305 * set of valid addresses?
306 * @param addr one of the addresses of the host
307 * the specific address format depends on the transport
308 * @param addrlen length of the address
309 * @param dest_plugin destination plugin to use this address with
312 plugin_env_address_change_notification (void *cls, int add_remove,
313 const void *addr, size_t addrlen,
314 const char *dest_plugin)
316 struct GNUNET_HELLO_Address address;
318 address.peer = GST_my_identity;
319 address.transport_name = dest_plugin;
320 address.address = addr;
321 address.address_length = addrlen;
322 GST_hello_modify_addresses (add_remove, &address);
327 * Function that will be called whenever the plugin internally
328 * cleans up a session pointer and hence the service needs to
329 * discard all of those sessions as well. Plugins that do not
330 * use sessions can simply omit calling this function and always
331 * use NULL wherever a session pointer is needed. This function
332 * should be called BEFORE a potential "TransmitContinuation"
333 * from the "TransmitFunction".
336 * @param peer which peer was the session for
337 * @param session which session is being destoyed
340 plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
341 struct Session *session)
343 const char *transport_name = cls;
344 struct GNUNET_HELLO_Address address;
346 GNUNET_assert (strlen (transport_name) > 0);
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %p to peer `%s' ended \n",
348 session, GNUNET_i2s (peer));
350 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
352 "Telling ATS to destroy session %p from peer %s\n",
353 session, GNUNET_i2s (peer));
354 address.peer = *peer;
355 address.address = NULL;
356 address.address_length = 0;
357 address.transport_name = transport_name;
358 GST_neighbours_session_terminated (peer, session);
360 /* Tell ATS that session has ended */
361 GNUNET_ATS_address_destroyed (GST_ats, &address, session);
366 * Function that will be called to figure if an address is an loopback,
367 * LAN, WAN etc. address
370 * @param addr binary address
371 * @param addrlen length of the address
372 * @return ATS Information containing the network type
374 static struct GNUNET_ATS_Information
375 plugin_env_address_to_type (void *cls,
376 const struct sockaddr *addr,
379 struct GNUNET_ATS_Information ats;
380 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
381 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
387 if (((addr->sa_family != AF_INET) && (addrlen != sizeof (struct sockaddr_in))) &&
388 ((addr->sa_family != AF_INET6) && (addrlen != sizeof (struct sockaddr_in6))) &&
389 (addr->sa_family != AF_UNIX))
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed address with length %u `%s'\n",
393 GNUNET_a2s(addr, addrlen));
397 return GNUNET_ATS_address_get_type(GST_ats, addr, addrlen);
402 GST_update_ats_metrics (const struct GNUNET_PeerIdentity *peer,
403 const struct GNUNET_HELLO_Address *address,
404 struct Session *session,
405 const struct GNUNET_ATS_Information *ats,
408 struct GNUNET_ATS_Information *ats_new;
409 /* Call to manipulation to manipulate ATS information */
410 ats_new = GST_manipulation_manipulate_metrics (peer, address, session, ats, ats_count);
416 GNUNET_ATS_address_update (GST_ats, address, session, ats_new, ats_count);
417 GNUNET_free (ats_new);
422 * Function that will be called to figure if an address is an loopback,
423 * LAN, WAN etc. address
426 * @param peer the peer
427 * @param address binary address
428 * @param address_len length of the address
429 * @param session the session
430 * @param ats the ats information to update
431 * @param ats_count the number of ats elements
434 plugin_env_update_metrics (void *cls,
435 const struct GNUNET_PeerIdentity *peer,
437 uint16_t address_len,
438 struct Session *session,
439 const struct GNUNET_ATS_Information *ats,
442 struct GNUNET_HELLO_Address haddress;
443 const char *plugin_name = cls;
445 if ((NULL == ats) || (0 == ats_count))
447 GNUNET_assert (NULL != GST_ats);
449 haddress.peer = *peer;
450 haddress.address = address;
451 haddress.address_length = address_len;
452 haddress.transport_name = plugin_name;
454 GST_update_ats_metrics (peer, &haddress, session, ats, ats_count);
459 * Function called by ATS to notify the callee that the
460 * assigned bandwidth or address for a given peer was changed. If the
461 * callback is called with address/bandwidth assignments of zero, the
462 * ATS disconnect function will still be called once the disconnect
466 * @param address address to use (for peer given in address)
467 * @param session session to use (if available)
468 * @param bandwidth_out assigned outbound bandwidth for the connection, 0 to disconnect from peer
469 * @param bandwidth_in assigned inbound bandwidth for the connection, 0 to disconnect from peer
470 * @param ats ATS information
471 * @param ats_count number of ATS elements
474 ats_request_address_change (void *cls,
475 const struct GNUNET_HELLO_Address *address,
476 struct Session *session,
477 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
478 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
479 const struct GNUNET_ATS_Information *ats,
482 uint32_t bw_in = ntohl (bandwidth_in.value__);
483 uint32_t bw_out = ntohl (bandwidth_out.value__);
485 /* ATS tells me to disconnect from peer */
486 if ((bw_in == 0) && (bw_out == 0))
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "ATS tells me to disconnect from peer `%s'\n",
490 GNUNET_i2s (&address->peer));
491 GST_neighbours_force_disconnect (&address->peer);
494 GST_neighbours_switch_to_address (&address->peer, address, session, ats,
495 ats_count, bandwidth_in,
501 * Function called to notify transport users that another
502 * peer connected to us.
505 * @param peer the peer that connected
506 * @param bandwidth_in inbound bandwidth in NBO
507 * @param bandwidth_out outbound bandwidth in NBO
510 neighbours_connect_notification (void *cls,
511 const struct GNUNET_PeerIdentity *peer,
512 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
513 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
515 size_t len = sizeof (struct ConnectInfoMessage);
516 char buf[len] GNUNET_ALIGN;
517 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
520 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
521 "We are now connected to peer `%s' and %u peers in total\n",
522 GNUNET_i2s (peer), connections);
524 connect_msg->header.size = htons (sizeof (buf));
525 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
526 connect_msg->id = *peer;
527 connect_msg->quota_in = bandwidth_in;
528 connect_msg->quota_out = bandwidth_out;
529 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
534 * Function called to notify transport users that another
535 * peer disconnected from us.
538 * @param peer the peer that disconnected
541 neighbours_disconnect_notification (void *cls,
542 const struct GNUNET_PeerIdentity *peer)
544 struct DisconnectInfoMessage disconnect_msg;
547 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
548 "Peer `%s' disconnected and we are connected to %u peers\n",
549 GNUNET_i2s (peer), connections);
551 GST_manipulation_peer_disconnect (peer);
552 disconnect_msg.header.size = htons (sizeof (struct DisconnectInfoMessage));
553 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
554 disconnect_msg.reserved = htonl (0);
555 disconnect_msg.peer = *peer;
556 GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
561 * Function called to notify transport users that a neighbour peer changed its
565 * @param peer peer this update is about (never NULL)
566 * @param address address, NULL on disconnect
569 neighbours_address_notification (void *cls,
570 const struct GNUNET_PeerIdentity *peer,
571 const struct GNUNET_HELLO_Address *address)
573 GST_clients_broadcast_address_notification (peer, address);
578 * Function called when the service shuts down. Unloads our plugins
579 * and cancels pending validations.
581 * @param cls closure, unused
582 * @param tc task context (unused)
585 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
587 if (NULL != GST_keygen)
589 GNUNET_CRYPTO_ecc_key_create_stop (GST_keygen);
592 GST_neighbours_stop ();
593 GST_validation_stop ();
594 GST_plugins_unload ();
596 GNUNET_ATS_scheduling_done (GST_ats);
599 GST_blacklist_stop ();
601 GST_manipulation_stop ();
603 if (NULL != GST_peerinfo)
605 GNUNET_PEERINFO_disconnect (GST_peerinfo);
608 if (NULL != GST_stats)
610 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
613 if (NULL != GST_my_private_key)
615 GNUNET_CRYPTO_ecc_key_free (GST_my_private_key);
616 GST_my_private_key = NULL;
623 * Callback for hostkey read/generation
626 * @param pk the private key
627 * @param emsg error message
630 key_generation_cb (void *cls,
631 struct GNUNET_CRYPTO_EccPrivateKey *pk,
634 long long unsigned int max_fd_cfg;
642 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
643 _("Could not access hostkey: %s. Exiting.\n"),
645 GNUNET_SCHEDULER_shutdown ();
648 GST_my_private_key = pk;
650 GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
651 GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
652 GNUNET_CRYPTO_ecc_key_get_public (GST_my_private_key, &GST_my_public_key);
653 GNUNET_CRYPTO_hash (&GST_my_public_key, sizeof (GST_my_public_key),
654 &GST_my_identity.hashPubKey);
655 GNUNET_assert (NULL != GST_my_private_key);
657 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
659 if (NULL == GST_peerinfo)
661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662 _("Could not access PEERINFO service. Exiting.\n"));
663 GNUNET_SCHEDULER_shutdown ();
671 struct rlimit r_file;
672 if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
674 max_fd_rlimit = r_file.rlim_cur;
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Maximum number of open files was: %u/%u\n", r_file.rlim_cur,
679 max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
681 GNUNET_CONFIGURATION_get_value_number (GST_cfg, "transport", "MAX_FD", &max_fd_cfg);
683 if (max_fd_cfg > max_fd_rlimit)
686 max_fd = max_fd_rlimit;
687 if (max_fd < DEFAULT_MAX_FDS)
688 max_fd = DEFAULT_MAX_FDS;
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691 "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
692 max_fd, (max_fd / 3) , (max_fd / 3) * 2);
694 friend_only = GNUNET_CONFIGURATION_get_value_yesno(GST_cfg, "topology","FRIENDS-ONLY");
695 if (GNUNET_SYSERR == friend_only)
696 friend_only = GNUNET_NO; /* According to topology defaults */
697 /* start subsystems */
698 GST_hello_start (friend_only, &process_hello_update, NULL);
699 GNUNET_assert (NULL != GST_hello_get());
700 GST_blacklist_start (GST_server, GST_cfg, &GST_my_identity);
702 GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
703 GST_manipulation_init (GST_cfg);
704 GST_plugins_load (&GST_manipulation_recv,
705 &plugin_env_address_change_notification,
706 &plugin_env_session_end,
707 &plugin_env_address_to_type,
708 &plugin_env_update_metrics);
709 GST_neighbours_start (NULL,
710 &neighbours_connect_notification,
711 &neighbours_disconnect_notification,
712 &neighbours_address_notification,
714 GST_clients_start (GST_server);
715 GST_validation_start ((max_fd / 3));
716 if (NULL != GST_server)
717 GNUNET_SERVER_resume (GST_server);
722 * Initiate transport service.
725 * @param server the initialized server
726 * @param c configuration to use
729 run (void *cls, struct GNUNET_SERVER_Handle *server,
730 const struct GNUNET_CONFIGURATION_Handle *c)
737 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
742 ("Transport service is lacking key configuration settings. Exiting.\n"));
743 GNUNET_SCHEDULER_shutdown ();
747 GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION",
750 hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
753 GNUNET_SERVER_suspend (server);
754 GST_keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile, &key_generation_cb, NULL);
755 GNUNET_free (keyfile);
756 if (NULL == GST_keygen)
758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
759 _("Transport service is unable to access hostkey. Exiting.\n"));
760 GNUNET_SCHEDULER_shutdown ();
766 * The main function for the transport service.
768 * @param argc number of arguments from the command line
769 * @param argv command line arguments
770 * @return 0 ok, 1 on error
773 main (int argc, char *const *argv)
776 GNUNET_SERVICE_run (argc, argv, "transport",
777 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
780 /* end of file gnunet-service-transport.c */