- fix for mantis 0002618
[oweals/gnunet.git] / src / transport / gnunet-service-transport.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/gnunet-service-transport.c
23  * @brief
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
41
42 /* globals */
43
44 /**
45  * Statistics handle.
46  */
47 struct GNUNET_STATISTICS_Handle *GST_stats;
48
49 /**
50  * Configuration handle.
51  */
52 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
53
54 /**
55  * Configuration handle.
56  */
57 struct GNUNET_PeerIdentity GST_my_identity;
58
59 /**
60  * Handle to peerinfo service.
61  */
62 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
63
64 /**
65  * Hostkey generation context
66  */
67 struct GNUNET_CRYPTO_RsaKeyGenerationContext *GST_keygen;
68
69 /**
70  * Handle to our service's server.
71  */
72 static struct GNUNET_SERVER_Handle *GST_server;
73
74 /**
75  * Our public key.
76  */
77 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded GST_my_public_key;
78
79 /**
80  * Our private key.
81  */
82 struct GNUNET_CRYPTO_RsaPrivateKey *GST_my_private_key;
83
84 /**
85  * ATS handle.
86  */
87 struct GNUNET_ATS_SchedulingHandle *GST_ats;
88
89 /**
90  * DEBUGGING connection counter
91  */
92 static int connections;
93
94
95 /**
96  * Transmit our HELLO message to the given (connected) neighbour.
97  *
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
105  */
106 static void
107 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
108                     const struct GNUNET_ATS_Information *ats,
109                     uint32_t ats_count,
110                     const struct GNUNET_HELLO_Address *address,
111                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
112                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
113 {
114   const struct GNUNET_MessageHeader *hello = cls;
115
116   GST_neighbours_send (target, (const char *) hello, ntohs (hello->size),
117                        GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION, NULL, NULL);
118 }
119
120
121 /**
122  * My HELLO has changed. Tell everyone who should know.
123  *
124  * @param cls unused
125  * @param hello new HELLO
126  */
127 static void
128 process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
129 {
130   GST_clients_broadcast (hello, GNUNET_NO);
131   GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
132 }
133
134
135
136 /**
137  * We received some payload.  Prepare to pass it on to our clients.
138  *
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
146  */
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)
153 {
154   struct GNUNET_TIME_Relative ret;
155   int do_forward;
156   struct InboundMessage *im;
157   size_t msg_size = ntohs (message->size);
158   size_t 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;
163
164   ret = GNUNET_TIME_UNIT_ZERO;
165   do_forward = GNUNET_SYSERR;
166   ret = GST_neighbours_calculate_receive_delay (peer, msg_size, &do_forward);
167
168   if (!GST_neighbours_test_connected (peer))
169   {
170
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));
174
175     GNUNET_STATISTICS_update (GST_stats,
176                               gettext_noop
177                               ("# bytes payload discarded due to not connected peer "),
178                               msg_size, GNUNET_NO);
179     return ret;
180   }
181
182   if (do_forward != GNUNET_YES)
183     return ret;
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);
188   im->peer = *peer;
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));
195
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);
199
200   return ret;
201 }
202
203
204 /**
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.
208  *
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)
226  */
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)
234 {
235   const char *plugin_name = cls;
236   struct GNUNET_TIME_Relative ret;
237   struct GNUNET_HELLO_Address address;
238   uint16_t type;
239
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;
245   if (NULL == message)
246     goto end;
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));
249
250   GNUNET_STATISTICS_update (GST_stats,
251                         gettext_noop
252                         ("# bytes total received"),
253                             ntohs (message->size), GNUNET_NO);
254
255   switch (type)
256   {
257   case GNUNET_MESSAGE_TYPE_HELLO:
258     GST_validation_handle_hello (message);
259     return ret;
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",
263                 (sender_address !=
264                  NULL) ? GST_plugins_a2s (&address) : "<inbound>");
265     GST_validation_handle_ping (peer, message, &address, session);
266     break;
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",
270                 (sender_address !=
271                  NULL) ? GST_plugins_a2s (&address) : "<inbound>");
272     GST_validation_handle_pong (peer, message);
273     break;
274   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT:
275     GST_neighbours_handle_connect (message, peer, &address, session, ats,
276                                    ats_count);
277     break;
278   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK:
279     GST_neighbours_handle_connect_ack (message, peer, &address, session, ats,
280                                        ats_count);
281     break;
282   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
283     GST_neighbours_handle_session_ack (message, peer, &address, session, ats,
284                                        ats_count);
285     break;
286   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
287     GST_neighbours_handle_disconnect_message (peer, message);
288     break;
289   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
290     GST_neighbours_keepalive (peer);
291     break;
292   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
293     GST_neighbours_keepalive_response (peer, ats, ats_count);
294     break;
295   default:
296     /* should be payload */
297     GNUNET_STATISTICS_update (GST_stats,
298                               gettext_noop
299                               ("# bytes payload received"),
300                               ntohs (message->size), GNUNET_NO);
301     ret = process_payload (peer, &address, session, message, ats, ats_count);
302     break;
303   }
304 end:
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);
308   return ret;
309 }
310
311
312 /**
313  * Function that will be called for each address the transport
314  * is aware that it might be reachable under.  Update our HELLO.
315  *
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
323  */
324 static void
325 plugin_env_address_change_notification (void *cls, int add_remove,
326                                         const void *addr, size_t addrlen,
327                                         const char *dest_plugin)
328 {
329   struct GNUNET_HELLO_Address address;
330
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);
336 }
337
338
339 /**
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".
347  *
348  * @param cls closure
349  * @param peer which peer was the session for
350  * @param session which session is being destoyed
351  */
352 static void
353 plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
354                         struct Session *session)
355 {
356   const char *transport_name = cls;
357   struct GNUNET_HELLO_Address address;
358
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));
362   if (NULL != session)
363     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
364                      "transport-ats",
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);
372
373   /* Tell ATS that session has ended */
374   GNUNET_ATS_address_destroyed (GST_ats, &address, session);
375 }
376
377
378 /**
379  * Function that will be called to figure if an address is an loopback,
380  * LAN, WAN etc. address
381  *
382  * @param cls closure
383  * @param addr binary address
384  * @param addrlen length of the address
385  * @return ATS Information containing the network type
386  */
387 static struct GNUNET_ATS_Information
388 plugin_env_address_to_type (void *cls,
389                             const struct sockaddr *addr,
390                             size_t addrlen)
391 {
392   struct GNUNET_ATS_Information ats;
393   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
394   ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
395   if (GST_ats == NULL)
396   {
397     GNUNET_break (0);
398     return ats;
399   }
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))
403   {
404     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed address with length %u `%s'\n",
405                 addrlen,
406                 GNUNET_a2s(addr, addrlen));
407     GNUNET_break (0);
408     return ats;
409   }
410   return GNUNET_ATS_address_get_type(GST_ats, addr, addrlen);
411 }
412
413
414 /**
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
419  * actually happened.
420  *
421  * @param cls closure
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
428  */
429 static void
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,
436                             uint32_t ats_count)
437 {
438   uint32_t bw_in = ntohl (bandwidth_in.value__);
439   uint32_t bw_out = ntohl (bandwidth_out.value__);
440
441   /* ATS tells me to disconnect from peer */
442   if ((bw_in == 0) && (bw_out == 0))
443   {
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);
448     return;
449   }
450   GST_neighbours_switch_to_address (&address->peer, address, session, ats,
451                                          ats_count, bandwidth_in,
452                                          bandwidth_out);
453 }
454
455
456 /**
457  * Function called to notify transport users that another
458  * peer connected to us.
459  *
460  * @param cls closure
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
466  */
467 static void
468 neighbours_connect_notification (void *cls,
469                                  const struct GNUNET_PeerIdentity *peer,
470                                  const struct GNUNET_ATS_Information *ats,
471                                  uint32_t ats_count,
472                                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
473                                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
474 {
475   size_t len =
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;
481
482   connections++;
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);
486
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);
496 }
497
498
499 /**
500  * Function called to notify transport users that another
501  * peer disconnected from us.
502  *
503  * @param cls closure
504  * @param peer the peer that disconnected
505  */
506 static void
507 neighbours_disconnect_notification (void *cls,
508                                     const struct GNUNET_PeerIdentity *peer)
509 {
510   struct DisconnectInfoMessage disconnect_msg;
511
512   connections--;
513   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
514               "Peer `%s' disconnected and we are connected to %u peers\n",
515               GNUNET_i2s (peer), connections);
516
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);
522 }
523
524
525 /**
526  * Function called to notify transport users that a neighbour peer changed its
527  * active address.
528  *
529  * @param cls closure
530  * @param peer peer this update is about (never NULL)
531  * @param address address, NULL on disconnect
532  */
533 static void
534 neighbours_address_notification (void *cls,
535                                  const struct GNUNET_PeerIdentity *peer,
536                                  const struct GNUNET_HELLO_Address *address)
537 {
538   GST_clients_broadcast_address_notification (peer, address);
539 }
540
541
542 /**
543  * Function called when the service shuts down.  Unloads our plugins
544  * and cancels pending validations.
545  *
546  * @param cls closure, unused
547  * @param tc task context (unused)
548  */
549 static void
550 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
551 {
552   if (NULL != GST_keygen)
553   {
554     GNUNET_CRYPTO_rsa_key_create_stop (GST_keygen);
555     GST_keygen = NULL;
556   }
557   GST_neighbours_stop ();
558   GST_validation_stop ();
559   GST_plugins_unload ();
560
561   GNUNET_ATS_scheduling_done (GST_ats);
562   GST_ats = NULL;
563   GST_clients_stop ();
564   GST_blacklist_stop ();
565   GST_hello_stop ();
566
567   if (NULL != GST_peerinfo)
568   {
569     GNUNET_PEERINFO_disconnect (GST_peerinfo);
570     GST_peerinfo = NULL;
571   }
572   if (NULL != GST_stats)
573   {
574     GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
575     GST_stats = NULL;
576   }
577   if (NULL != GST_my_private_key)
578   {
579     GNUNET_CRYPTO_rsa_key_free (GST_my_private_key);
580     GST_my_private_key = NULL;
581   }
582   GST_server = NULL;
583 }
584
585
586 /**
587  * Callback for hostkey read/generation
588  *
589  * @param cls NULL
590  * @param pk the private key
591  * @param emsg error message
592  */
593 static void
594 key_generation_cb (void *cls,
595                    struct GNUNET_CRYPTO_RsaPrivateKey *pk,
596                    const char *emsg)
597 {
598   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded tmp;
599
600   GST_keygen = NULL;
601   if (NULL == pk)
602   {
603     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
604                 _("Transport service could not access hostkey: %s. Exiting.\n"),
605                 emsg);
606     GNUNET_SCHEDULER_shutdown ();
607     return;
608   }
609   GST_my_private_key = pk;
610
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);
618
619   GNUNET_assert (NULL != GST_my_private_key);
620   GNUNET_assert (0 != memcmp (&GST_my_public_key, &tmp, sizeof (GST_my_public_key)));
621
622   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
623                                 NULL);
624   if (NULL == GST_peerinfo)
625   {
626     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
627                 _("Could not access PEERINFO service.  Exiting.\n"));
628     GNUNET_SCHEDULER_shutdown ();
629     return;
630   }
631
632   /* start subsystems */
633   GST_hello_start (&process_hello_update, NULL);
634   GNUNET_assert (NULL != GST_hello_get());
635   GST_blacklist_start (GST_server);
636   GST_ats =
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);
650 }
651
652
653 /**
654  * Initiate transport service.
655  *
656  * @param cls closure
657  * @param server the initialized server
658  * @param c configuration to use
659  */
660 static void
661 run (void *cls, struct GNUNET_SERVER_Handle *server,
662      const struct GNUNET_CONFIGURATION_Handle *c)
663 {
664   char *keyfile;
665
666   /* setup globals */
667   GST_cfg = c;
668   if (GNUNET_OK !=
669       GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
670                                                &keyfile))
671   {
672     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673                 _
674                 ("Transport service is lacking key configuration settings.  Exiting.\n"));
675     GNUNET_SCHEDULER_shutdown ();
676     return;
677   }
678   GST_server = server;
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)
683   {
684     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685                 _("Transport service is unable to access hostkey. Exiting.\n"));
686     GNUNET_SCHEDULER_shutdown ();
687   }
688 }
689
690
691 /**
692  * The main function for the transport service.
693  *
694  * @param argc number of arguments from the command line
695  * @param argv command line arguments
696  * @return 0 ok, 1 on error
697  */
698 int
699 main (int argc, char *const *argv)
700 {
701   return (GNUNET_OK ==
702           GNUNET_SERVICE_run (argc, argv, "transport",
703                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
704 }
705
706 /* end of file gnunet-service-transport.c */