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 "transport.h"
47 struct GNUNET_STATISTICS_Handle *GST_stats;
50 * Configuration handle.
52 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
55 * Configuration handle.
57 struct GNUNET_PeerIdentity GST_my_identity;
60 * Handle to peerinfo service.
62 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
65 * Hostkey generation context
67 struct GNUNET_CRYPTO_RsaKeyGenerationContext *GST_keygen;
70 * Handle to our service's server.
72 static struct GNUNET_SERVER_Handle *GST_server;
77 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded GST_my_public_key;
82 struct GNUNET_CRYPTO_RsaPrivateKey *GST_my_private_key;
87 struct GNUNET_ATS_SchedulingHandle *GST_ats;
90 * DEBUGGING connection counter
92 static int connections;
96 * Transmit our HELLO message to the given (connected) neighbour.
98 * @param cls the 'HELLO' message
99 * @param target a connected neighbour
100 * @param ats performance information (unused)
101 * @param ats_count number of records in ats (unused)
102 * @param address the address
103 * @param bandwidth_in inbound quota in NBO
104 * @param bandwidth_out outbound quota in NBO
107 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
108 const struct GNUNET_ATS_Information *ats,
110 const struct GNUNET_HELLO_Address *address,
111 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
112 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
114 const struct GNUNET_MessageHeader *hello = cls;
116 GST_neighbours_send (target, (const char *) hello, ntohs (hello->size),
117 GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION, NULL, NULL);
122 * My HELLO has changed. Tell everyone who should know.
125 * @param hello new HELLO
128 process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
130 GST_clients_broadcast (hello, GNUNET_NO);
131 GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
137 * We received some payload. Prepare to pass it on to our clients.
139 * @param peer (claimed) identity of the other peer
140 * @param address the address
141 * @param session session used
142 * @param message the message to process
143 * @param ats performance information
144 * @param ats_count number of records in ats
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,
152 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
154 struct GNUNET_TIME_Relative ret;
156 struct InboundMessage *im;
157 size_t msg_size = ntohs (message->size);
159 sizeof (struct InboundMessage) + msg_size +
160 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1);
161 char buf[size] GNUNET_ALIGN;
162 struct GNUNET_ATS_Information *ap;
164 ret = GNUNET_TIME_UNIT_ZERO;
165 do_forward = GNUNET_SYSERR;
166 ret = GST_neighbours_calculate_receive_delay (peer, msg_size, &do_forward);
168 if (!GST_neighbours_test_connected (peer))
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "Discarded %u bytes type %u payload from peer `%s'\n", msg_size,
173 ntohs (message->type), GNUNET_i2s (peer));
175 GNUNET_STATISTICS_update (GST_stats,
177 ("# bytes payload discarded due to not connected peer "),
178 msg_size, GNUNET_NO);
182 if (do_forward != GNUNET_YES)
184 im = (struct InboundMessage *) buf;
185 im->header.size = htons (size);
186 im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
187 im->ats_count = htonl (ats_count + 1);
189 ap = (struct GNUNET_ATS_Information *) &im[1];
190 memcpy (ap, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
191 ap[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
192 ap[ats_count].value =
193 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
194 memcpy (&ap[ats_count + 1], message, ntohs (message->size));
196 GNUNET_ATS_address_add (GST_ats, address, session, ap, ats_count + 1);
197 GNUNET_ATS_address_update (GST_ats, address, session, ap, ats_count + 1);
198 GST_clients_broadcast (&im->header, GNUNET_YES);
205 * Function called by the transport for each received message.
206 * This function should also be called with "NULL" for the
207 * message to signal that the other peer disconnected.
209 * @param cls closure, const char* with the name of the plugin we received the message from
210 * @param peer (claimed) identity of the other peer
211 * @param message the message, NULL if we only care about
212 * learning about the delay until we should receive again -- FIXME!
213 * @param ats performance information
214 * @param ats_count number of records in ats
215 * @param session identifier used for this session (NULL for plugins
216 * that do not offer bi-directional communication to the sender
217 * using the same "connection")
218 * @param sender_address binary address of the sender (if we established the
219 * connection or are otherwise sure of it; should be NULL
220 * for inbound TCP/UDP connections since it it not clear
221 * that we could establish ourselves a connection to that
222 * IP address and get the same system)
223 * @param sender_address_len number of bytes in sender_address
224 * @return how long the plugin should wait until receiving more data
225 * (plugins that do not support this, can ignore the return value)
227 static struct GNUNET_TIME_Relative
228 plugin_env_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
229 const struct GNUNET_MessageHeader *message,
230 const struct GNUNET_ATS_Information *ats,
231 uint32_t ats_count, struct Session *session,
232 const char *sender_address,
233 uint16_t sender_address_len)
235 const char *plugin_name = cls;
236 struct GNUNET_TIME_Relative ret;
237 struct GNUNET_HELLO_Address address;
240 address.peer = *peer;
241 address.address = sender_address;
242 address.address_length = sender_address_len;
243 address.transport_name = plugin_name;
244 ret = GNUNET_TIME_UNIT_ZERO;
247 type = ntohs (message->type);
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Message with type %u from peer `%s'\n", type, GNUNET_i2s (peer));
250 GNUNET_STATISTICS_update (GST_stats,
252 ("# bytes total received"),
253 ntohs (message->size), GNUNET_NO);
257 case GNUNET_MESSAGE_TYPE_HELLO:
258 GST_validation_handle_hello (message);
260 case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
262 "Processing `%s' from `%s'\n", "PING",
264 NULL) ? GST_plugins_a2s (&address) : "<inbound>");
265 GST_validation_handle_ping (peer, message, &address, session);
267 case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
269 "Processing `%s' from `%s'\n", "PONG",
271 NULL) ? GST_plugins_a2s (&address) : "<inbound>");
272 GST_validation_handle_pong (peer, message);
274 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT:
275 GST_neighbours_handle_connect (message, peer, &address, session, ats,
278 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK:
279 GST_neighbours_handle_connect_ack (message, peer, &address, session, ats,
282 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
283 GST_neighbours_handle_session_ack (message, peer, &address, session, ats,
286 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
287 GST_neighbours_handle_disconnect_message (peer, message);
289 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
290 GST_neighbours_keepalive (peer);
292 case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
293 GST_neighbours_keepalive_response (peer, ats, ats_count);
296 /* should be payload */
297 GNUNET_STATISTICS_update (GST_stats,
299 ("# bytes payload received"),
300 ntohs (message->size), GNUNET_NO);
301 ret = process_payload (peer, &address, session, message, ats, ats_count);
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Allowing receive from peer %s to continue in %llu ms\n",
307 GNUNET_i2s (peer), (unsigned long long) ret.rel_value);
313 * Function that will be called for each address the transport
314 * is aware that it might be reachable under. Update our HELLO.
316 * @param cls name of the plugin (const char*)
317 * @param add_remove should the address added (YES) or removed (NO) from the
318 * set of valid addresses?
319 * @param addr one of the addresses of the host
320 * the specific address format depends on the transport
321 * @param addrlen length of the address
322 * @param dest_plugin destination plugin to use this address with
325 plugin_env_address_change_notification (void *cls, int add_remove,
326 const void *addr, size_t addrlen,
327 const char *dest_plugin)
329 struct GNUNET_HELLO_Address address;
331 address.peer = GST_my_identity;
332 address.transport_name = dest_plugin;
333 address.address = addr;
334 address.address_length = addrlen;
335 GST_hello_modify_addresses (add_remove, &address);
340 * Function that will be called whenever the plugin internally
341 * cleans up a session pointer and hence the service needs to
342 * discard all of those sessions as well. Plugins that do not
343 * use sessions can simply omit calling this function and always
344 * use NULL wherever a session pointer is needed. This function
345 * should be called BEFORE a potential "TransmitContinuation"
346 * from the "TransmitFunction".
349 * @param peer which peer was the session for
350 * @param session which session is being destoyed
353 plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
354 struct Session *session)
356 const char *transport_name = cls;
357 struct GNUNET_HELLO_Address address;
359 GNUNET_assert (strlen (transport_name) > 0);
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %p to peer `%s' ended \n",
361 session, GNUNET_i2s (peer));
363 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
365 "Telling ATS to destroy session %p from peer %s\n",
366 session, GNUNET_i2s (peer));
367 address.peer = *peer;
368 address.address = NULL;
369 address.address_length = 0;
370 address.transport_name = transport_name;
371 GST_neighbours_session_terminated (peer, session);
373 /* Tell ATS that session has ended */
374 GNUNET_ATS_address_destroyed (GST_ats, &address, session);
379 * Function that will be called to figure if an address is an loopback,
380 * LAN, WAN etc. address
383 * @param addr binary address
384 * @param addrlen length of the address
385 * @return ATS Information containing the network type
387 static struct GNUNET_ATS_Information
388 plugin_env_address_to_type (void *cls,
389 const struct sockaddr *addr,
392 struct GNUNET_ATS_Information ats;
393 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
394 ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
400 if (((addr->sa_family != AF_INET) && (addrlen != sizeof (struct sockaddr_in))) &&
401 ((addr->sa_family != AF_INET6) && (addrlen != sizeof (struct sockaddr_in6))) &&
402 (addr->sa_family != AF_UNIX))
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed address with length %u `%s'\n",
406 GNUNET_a2s(addr, addrlen));
408 return (const struct GNUNET_ATS_Information) ats;
410 return GNUNET_ATS_address_get_type(GST_ats, addr, addrlen);
415 * Function called by ATS to notify the callee that the
416 * assigned bandwidth or address for a given peer was changed. If the
417 * callback is called with address/bandwidth assignments of zero, the
418 * ATS disconnect function will still be called once the disconnect
422 * @param address address to use (for peer given in address)
423 * @param session session to use (if available)
424 * @param bandwidth_out assigned outbound bandwidth for the connection, 0 to disconnect from peer
425 * @param bandwidth_in assigned inbound bandwidth for the connection, 0 to disconnect from peer
426 * @param ats ATS information
427 * @param ats_count number of ATS elements
430 ats_request_address_change (void *cls,
431 const struct GNUNET_HELLO_Address *address,
432 struct Session *session,
433 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
434 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
435 const struct GNUNET_ATS_Information *ats,
438 uint32_t bw_in = ntohl (bandwidth_in.value__);
439 uint32_t bw_out = ntohl (bandwidth_out.value__);
441 /* ATS tells me to disconnect from peer */
442 if ((bw_in == 0) && (bw_out == 0))
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "ATS tells me to disconnect from peer `%s'\n",
446 GNUNET_i2s (&address->peer));
447 GST_neighbours_force_disconnect (&address->peer);
450 GST_neighbours_switch_to_address (&address->peer, address, session, ats,
451 ats_count, bandwidth_in,
457 * Function called to notify transport users that another
458 * peer connected to us.
461 * @param peer the peer that connected
462 * @param ats performance data
463 * @param ats_count number of entries in ats
464 * @param bandwidth_in inbound bandwidth in NBO
465 * @param bandwidth_out outbound bandwidth in NBO
468 neighbours_connect_notification (void *cls,
469 const struct GNUNET_PeerIdentity *peer,
470 const struct GNUNET_ATS_Information *ats,
472 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
473 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
476 sizeof (struct ConnectInfoMessage) +
477 ats_count * sizeof (struct GNUNET_ATS_Information);
478 char buf[len] GNUNET_ALIGN;
479 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
480 struct GNUNET_ATS_Information *ap;
483 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
484 "We are now connected to peer `%s' and %u peers in total\n",
485 GNUNET_i2s (peer), connections);
487 connect_msg->header.size = htons (sizeof (buf));
488 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
489 connect_msg->ats_count = htonl (ats_count);
490 connect_msg->id = *peer;
491 connect_msg->quota_in = bandwidth_in;
492 connect_msg->quota_out = bandwidth_out;
493 ap = (struct GNUNET_ATS_Information *) &connect_msg[1];
494 memcpy (ap, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
495 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
500 * Function called to notify transport users that another
501 * peer disconnected from us.
504 * @param peer the peer that disconnected
507 neighbours_disconnect_notification (void *cls,
508 const struct GNUNET_PeerIdentity *peer)
510 struct DisconnectInfoMessage disconnect_msg;
513 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
514 "Peer `%s' disconnected and we are connected to %u peers\n",
515 GNUNET_i2s (peer), connections);
517 disconnect_msg.header.size = htons (sizeof (struct DisconnectInfoMessage));
518 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
519 disconnect_msg.reserved = htonl (0);
520 disconnect_msg.peer = *peer;
521 GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
526 * Function called to notify transport users that a neighbour peer changed its
530 * @param peer peer this update is about (never NULL)
531 * @param address address, NULL on disconnect
534 neighbours_address_notification (void *cls,
535 const struct GNUNET_PeerIdentity *peer,
536 const struct GNUNET_HELLO_Address *address)
538 GST_clients_broadcast_address_notification (peer, address);
543 * Function called when the service shuts down. Unloads our plugins
544 * and cancels pending validations.
546 * @param cls closure, unused
547 * @param tc task context (unused)
550 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
552 if (NULL != GST_keygen)
554 GNUNET_CRYPTO_rsa_key_create_stop (GST_keygen);
557 GST_neighbours_stop ();
558 GST_validation_stop ();
559 GST_plugins_unload ();
561 GNUNET_ATS_scheduling_done (GST_ats);
564 GST_blacklist_stop ();
567 if (NULL != GST_peerinfo)
569 GNUNET_PEERINFO_disconnect (GST_peerinfo);
572 if (NULL != GST_stats)
574 GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
577 if (NULL != GST_my_private_key)
579 GNUNET_CRYPTO_rsa_key_free (GST_my_private_key);
580 GST_my_private_key = NULL;
587 * Callback for hostkey read/generation
590 * @param pk the private key
591 * @param emsg error message
594 key_generation_cb (void *cls,
595 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
598 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded tmp;
603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
604 _("Transport service could not access hostkey: %s. Exiting.\n"),
606 GNUNET_SCHEDULER_shutdown ();
609 GST_my_private_key = pk;
611 GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
612 GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
613 memset (&GST_my_public_key, '\0', sizeof (GST_my_public_key));
614 memset (&tmp, '\0', sizeof (tmp));
615 GNUNET_CRYPTO_rsa_key_get_public (GST_my_private_key, &GST_my_public_key);
616 GNUNET_CRYPTO_hash (&GST_my_public_key, sizeof (GST_my_public_key),
617 &GST_my_identity.hashPubKey);
619 GNUNET_assert (NULL != GST_my_private_key);
620 GNUNET_assert (0 != memcmp (&GST_my_public_key, &tmp, sizeof (GST_my_public_key)));
622 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
624 if (NULL == GST_peerinfo)
626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
627 _("Could not access PEERINFO service. Exiting.\n"));
628 GNUNET_SCHEDULER_shutdown ();
632 /* start subsystems */
633 GST_hello_start (&process_hello_update, NULL);
634 GNUNET_assert (NULL != GST_hello_get());
635 GST_blacklist_start (GST_server);
637 GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
638 GST_plugins_load (&plugin_env_receive_callback,
639 &plugin_env_address_change_notification,
640 &plugin_env_session_end,
641 &plugin_env_address_to_type);
642 GST_neighbours_start (NULL,
643 &neighbours_connect_notification,
644 &neighbours_disconnect_notification,
645 &neighbours_address_notification);
646 GST_clients_start (GST_server);
647 GST_validation_start ();
648 if (NULL != GST_server)
649 GNUNET_SERVER_resume (GST_server);
654 * Initiate transport service.
657 * @param server the initialized server
658 * @param c configuration to use
661 run (void *cls, struct GNUNET_SERVER_Handle *server,
662 const struct GNUNET_CONFIGURATION_Handle *c)
669 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
674 ("Transport service is lacking key configuration settings. Exiting.\n"));
675 GNUNET_SCHEDULER_shutdown ();
679 GNUNET_SERVER_suspend (server);
680 GST_keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
681 GNUNET_free (keyfile);
682 if (NULL == GST_keygen)
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685 _("Transport service is unable to access hostkey. Exiting.\n"));
686 GNUNET_SCHEDULER_shutdown ();
692 * The main function for the transport service.
694 * @param argc number of arguments from the command line
695 * @param argv command line arguments
696 * @return 0 ok, 1 on error
699 main (int argc, char *const *argv)
702 GNUNET_SERVICE_run (argc, argv, "transport",
703 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
706 /* end of file gnunet-service-transport.c */