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:
245 GST_validation_handle_hello (message);
247 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
249 "Processing `%s' from `%s'\n", "PING",
251 NULL) ? GST_plugins_a2s (&address) : "<inbound>");
252 GST_validation_handle_ping (peer, message, &address, session);
254 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
256 "Processing `%s' from `%s'\n", "PONG",
258 NULL) ? GST_plugins_a2s (&address) : "<inbound>");
259 GST_validation_handle_pong (peer, message);
261 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT:
262 GST_neighbours_handle_connect (message, peer, &address, session);
264 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK:
265 GST_neighbours_handle_connect_ack (message, peer, &address, session);
267 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
268 GST_neighbours_handle_session_ack (message, peer, &address, session);
270 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
271 GST_neighbours_handle_disconnect_message (peer, message);
273 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
274 GST_neighbours_keepalive (peer);
276 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
277 GST_neighbours_keepalive_response (peer);
280 /* should be payload */
281 GNUNET_STATISTICS_update (GST_stats,
283 ("# bytes payload received"),
284 ntohs (message->size), GNUNET_NO);
285 ret = process_payload (peer, &address, session, message);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Allowing receive from peer %s to continue in %llu ms\n",
291 GNUNET_i2s (peer), (unsigned long long) ret.rel_value);
297 * Function that will be called for each address the transport
298 * is aware that it might be reachable under. Update our HELLO.
300 * @param cls name of the plugin (const char*)
301 * @param add_remove should the address added (YES) or removed (NO) from the
302 * set of valid addresses?
303 * @param addr one of the addresses of the host
304 * the specific address format depends on the transport
305 * @param addrlen length of the address
306 * @param dest_plugin destination plugin to use this address with
309 plugin_env_address_change_notification (void *cls, int add_remove,
310 const void *addr, size_t addrlen,
311 const char *dest_plugin)
313 struct GNUNET_HELLO_Address address;
315 address.peer = GST_my_identity;
316 address.transport_name = dest_plugin;
317 address.address = addr;
318 address.address_length = addrlen;
319 GST_hello_modify_addresses (add_remove, &address);
324 * Function that will be called whenever the plugin internally
325 * cleans up a session pointer and hence the service needs to
326 * discard all of those sessions as well. Plugins that do not
327 * use sessions can simply omit calling this function and always
328 * use NULL wherever a session pointer is needed. This function
329 * should be called BEFORE a potential "TransmitContinuation"
330 * from the "TransmitFunction".
333 * @param peer which peer was the session for
334 * @param session which session is being destoyed
337 plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
338 struct Session *session)
340 const char *transport_name = cls;
341 struct GNUNET_HELLO_Address address;
343 GNUNET_assert (strlen (transport_name) > 0);
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %p to peer `%s' ended \n",
345 session, GNUNET_i2s (peer));
347 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
349 "Telling ATS to destroy session %p from peer %s\n",
350 session, GNUNET_i2s (peer));
351 address.peer = *peer;
352 address.address = NULL;
353 address.address_length = 0;
354 address.transport_name = transport_name;
355 GST_neighbours_session_terminated (peer, session);
357 /* Tell ATS that session has ended */
358 GNUNET_ATS_address_destroyed (GST_ats, &address, session);
363 * Function that will be called to figure if an address is an loopback,
364 * LAN, WAN etc. address
367 * @param addr binary address
368 * @param addrlen length of the address
369 * @return ATS Information containing the network type
371 static struct GNUNET_ATS_Information
372 plugin_env_address_to_type (void *cls,
373 const struct sockaddr *addr,
376 struct GNUNET_ATS_Information ats;
377 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
378 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
384 if (((addr->sa_family != AF_INET) && (addrlen != sizeof (struct sockaddr_in))) &&
385 ((addr->sa_family != AF_INET6) && (addrlen != sizeof (struct sockaddr_in6))) &&
386 (addr->sa_family != AF_UNIX))
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed address with length %u `%s'\n",
390 GNUNET_a2s(addr, addrlen));
394 return GNUNET_ATS_address_get_type(GST_ats, addr, addrlen);
399 GST_update_ats_metrics (const struct GNUNET_PeerIdentity *peer,
400 const struct GNUNET_HELLO_Address *address,
401 struct Session *session,
402 const struct GNUNET_ATS_Information *ats,
405 struct GNUNET_ATS_Information *ats_new;
406 /* Call to manipulation to manipulate ATS information */
407 ats_new = GST_manipulation_manipulate_metrics (peer, address, session, ats, ats_count);
413 GNUNET_ATS_address_update (GST_ats, address, session, ats_new, ats_count);
414 GNUNET_free (ats_new);
419 * Function that will be called to figure if an address is an loopback,
420 * LAN, WAN etc. address
423 * @param peer the peer
424 * @param address binary address
425 * @param address_len length of the address
426 * @param session the session
427 * @param ats the ats information to update
428 * @param ats_count the number of ats elements
431 plugin_env_update_metrics (void *cls,
432 const struct GNUNET_PeerIdentity *peer,
434 uint16_t address_len,
435 struct Session *session,
436 const struct GNUNET_ATS_Information *ats,
439 struct GNUNET_HELLO_Address haddress;
440 const char *plugin_name = cls;
442 if ((NULL == ats) || (0 == ats_count))
444 GNUNET_assert (NULL != GST_ats);
446 haddress.peer = *peer;
447 haddress.address = address;
448 haddress.address_length = address_len;
449 haddress.transport_name = plugin_name;
451 GST_update_ats_metrics (peer, &haddress, session, ats, ats_count);
456 * Function called by ATS to notify the callee that the
457 * assigned bandwidth or address for a given peer was changed. If the
458 * callback is called with address/bandwidth assignments of zero, the
459 * ATS disconnect function will still be called once the disconnect
463 * @param address address to use (for peer given in address)
464 * @param session session to use (if available)
465 * @param bandwidth_out assigned outbound bandwidth for the connection, 0 to disconnect from peer
466 * @param bandwidth_in assigned inbound bandwidth for the connection, 0 to disconnect from peer
467 * @param ats ATS information
468 * @param ats_count number of ATS elements
471 ats_request_address_change (void *cls,
472 const struct GNUNET_HELLO_Address *address,
473 struct Session *session,
474 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
475 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
476 const struct GNUNET_ATS_Information *ats,
479 uint32_t bw_in = ntohl (bandwidth_in.value__);
480 uint32_t bw_out = ntohl (bandwidth_out.value__);
482 /* ATS tells me to disconnect from peer */
483 if ((bw_in == 0) && (bw_out == 0))
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "ATS tells me to disconnect from peer `%s'\n",
487 GNUNET_i2s (&address->peer));
488 GST_neighbours_force_disconnect (&address->peer);
491 GST_neighbours_switch_to_address (&address->peer, address, session, ats,
492 ats_count, bandwidth_in,
498 * Function called to notify transport users that another
499 * peer connected to us.
502 * @param peer the peer that connected
503 * @param bandwidth_in inbound bandwidth in NBO
504 * @param bandwidth_out outbound bandwidth in NBO
507 neighbours_connect_notification (void *cls,
508 const struct GNUNET_PeerIdentity *peer,
509 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
510 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
512 size_t len = sizeof (struct ConnectInfoMessage);
513 char buf[len] GNUNET_ALIGN;
514 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
517 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
518 "We are now connected to peer `%s' and %u peers in total\n",
519 GNUNET_i2s (peer), connections);
521 connect_msg->header.size = htons (sizeof (buf));
522 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
523 connect_msg->id = *peer;
524 connect_msg->quota_in = bandwidth_in;
525 connect_msg->quota_out = bandwidth_out;
526 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
531 * Function called to notify transport users that another
532 * peer disconnected from us.
535 * @param peer the peer that disconnected
538 neighbours_disconnect_notification (void *cls,
539 const struct GNUNET_PeerIdentity *peer)
541 struct DisconnectInfoMessage disconnect_msg;
544 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
545 "Peer `%s' disconnected and we are connected to %u peers\n",
546 GNUNET_i2s (peer), connections);
548 disconnect_msg.header.size = htons (sizeof (struct DisconnectInfoMessage));
549 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
550 disconnect_msg.reserved = htonl (0);
551 disconnect_msg.peer = *peer;
552 GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
557 * Function called to notify transport users that a neighbour peer changed its
561 * @param peer peer this update is about (never NULL)
562 * @param address address, NULL on disconnect
565 neighbours_address_notification (void *cls,
566 const struct GNUNET_PeerIdentity *peer,
567 const struct GNUNET_HELLO_Address *address)
569 GST_clients_broadcast_address_notification (peer, address);
574 * Function called when the service shuts down. Unloads our plugins
575 * and cancels pending validations.
577 * @param cls closure, unused
578 * @param tc task context (unused)
581 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
583 if (NULL != GST_keygen)
585 GNUNET_CRYPTO_ecc_key_create_stop (GST_keygen);
588 GST_neighbours_stop ();
589 GST_validation_stop ();
590 GST_plugins_unload ();
592 GNUNET_ATS_scheduling_done (GST_ats);
595 GST_blacklist_stop ();
597 GST_manipulation_stop ();
599 if (NULL != GST_peerinfo)
601 GNUNET_PEERINFO_disconnect (GST_peerinfo);
604 if (NULL != GST_stats)
606 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
609 if (NULL != GST_my_private_key)
611 GNUNET_CRYPTO_ecc_key_free (GST_my_private_key);
612 GST_my_private_key = NULL;
619 * Callback for hostkey read/generation
622 * @param pk the private key
623 * @param emsg error message
626 key_generation_cb (void *cls,
627 struct GNUNET_CRYPTO_EccPrivateKey *pk,
630 long long unsigned int max_fd_cfg;
637 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
638 _("Transport service could not access hostkey: %s. Exiting.\n"),
640 GNUNET_SCHEDULER_shutdown ();
643 GST_my_private_key = pk;
645 GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
646 GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
647 GNUNET_CRYPTO_ecc_key_get_public (GST_my_private_key, &GST_my_public_key);
648 GNUNET_CRYPTO_hash (&GST_my_public_key, sizeof (GST_my_public_key),
649 &GST_my_identity.hashPubKey);
650 GNUNET_assert (NULL != GST_my_private_key);
652 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
654 if (NULL == GST_peerinfo)
656 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
657 _("Could not access PEERINFO service. Exiting.\n"));
658 GNUNET_SCHEDULER_shutdown ();
666 struct rlimit r_file;
667 if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
669 max_fd_rlimit = r_file.rlim_cur;
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "Maximum number of open files was: %u/%u\n", r_file.rlim_cur,
674 max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
676 GNUNET_CONFIGURATION_get_value_number (GST_cfg, "transport", "MAX_FD", &max_fd_cfg);
678 if (max_fd_cfg > max_fd_rlimit)
681 max_fd = max_fd_rlimit;
682 if (max_fd < DEFAULT_MAX_FDS)
683 max_fd = DEFAULT_MAX_FDS;
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
687 max_fd, (max_fd / 3) , (max_fd / 3) * 2);
689 /* start subsystems */
690 GST_hello_start (&process_hello_update, NULL);
691 GNUNET_assert (NULL != GST_hello_get());
692 GST_blacklist_start (GST_server, GST_cfg, &GST_my_identity);
694 GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
695 GST_manipulation_init (GST_cfg);
696 GST_plugins_load (&GST_manipulation_recv,
697 &plugin_env_address_change_notification,
698 &plugin_env_session_end,
699 &plugin_env_address_to_type,
700 &plugin_env_update_metrics);
701 GST_neighbours_start (NULL,
702 &neighbours_connect_notification,
703 &neighbours_disconnect_notification,
704 &neighbours_address_notification,
706 GST_clients_start (GST_server);
707 GST_validation_start ((max_fd / 3));
708 if (NULL != GST_server)
709 GNUNET_SERVER_resume (GST_server);
714 * Initiate transport service.
717 * @param server the initialized server
718 * @param c configuration to use
721 run (void *cls, struct GNUNET_SERVER_Handle *server,
722 const struct GNUNET_CONFIGURATION_Handle *c)
729 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
734 ("Transport service is lacking key configuration settings. Exiting.\n"));
735 GNUNET_SCHEDULER_shutdown ();
739 GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION",
742 hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
745 GNUNET_SERVER_suspend (server);
746 GST_keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile, &key_generation_cb, NULL);
747 GNUNET_free (keyfile);
748 if (NULL == GST_keygen)
750 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
751 _("Transport service is unable to access hostkey. Exiting.\n"));
752 GNUNET_SCHEDULER_shutdown ();
758 * The main function for the transport service.
760 * @param argc number of arguments from the command line
761 * @param argv command line arguments
762 * @return 0 ok, 1 on error
765 main (int argc, char *const *argv)
768 GNUNET_SERVICE_run (argc, argv, "transport",
769 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
772 /* end of file gnunet-service-transport.c */