99f72e72957e3f7afbefc72bc7031a3e7d1ae8f5
[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 "gnunet-service-transport_manipulation.h"
41 #include "transport.h"
42
43 /* globals */
44
45 /**
46  * Statistics handle.
47  */
48 struct GNUNET_STATISTICS_Handle *GST_stats;
49
50 /**
51  * Configuration handle.
52  */
53 const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
54
55 /**
56  * Configuration handle.
57  */
58 struct GNUNET_PeerIdentity GST_my_identity;
59
60 /**
61  * Handle to peerinfo service.
62  */
63 struct GNUNET_PEERINFO_Handle *GST_peerinfo;
64
65 /**
66  * Handle to our service's server.
67  */
68 static struct GNUNET_SERVER_Handle *GST_server;
69
70 /**
71  * Our public key.
72  */
73 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded GST_my_public_key;
74
75 /**
76  * Our private key.
77  */
78 struct GNUNET_CRYPTO_EccPrivateKey *GST_my_private_key;
79
80 /**
81  * ATS handle.
82  */
83 struct GNUNET_ATS_SchedulingHandle *GST_ats;
84
85 /**
86  * DEBUGGING connection counter
87  */
88 static int connections;
89
90 /**
91  * Hello address expiration
92  */
93 struct GNUNET_TIME_Relative hello_expiration;
94
95
96 /**
97  * Transmit our HELLO message to the given (connected) neighbour.
98  *
99  * @param cls the 'HELLO' message
100  * @param target a connected neighbour
101  * @param address the address
102  * @param bandwidth_in inbound quota in NBO
103  * @param bandwidth_out outbound quota in NBO
104  */
105 static void
106 transmit_our_hello (void *cls, const struct GNUNET_PeerIdentity *target,
107                     const struct GNUNET_HELLO_Address *address,
108                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
109                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
110 {
111   const struct GNUNET_MessageHeader *hello = cls;
112
113   GST_neighbours_send (target, (const char *) hello, ntohs (hello->size),
114                        hello_expiration, NULL, NULL);
115 }
116
117
118 /**
119  * My HELLO has changed. Tell everyone who should know.
120  *
121  * @param cls unused
122  * @param hello new HELLO
123  */
124 static void
125 process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
126 {
127   GST_clients_broadcast (hello, GNUNET_NO);
128   GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
129 }
130
131
132
133 /**
134  * We received some payload.  Prepare to pass it on to our clients.
135  *
136  * @param peer (claimed) identity of the other peer
137  * @param address the address
138  * @param session session used
139  * @param message the message to process
140  * @return how long the plugin should wait until receiving more data
141  */
142 static struct GNUNET_TIME_Relative
143 process_payload (const struct GNUNET_PeerIdentity *peer,
144                  const struct GNUNET_HELLO_Address *address,
145                  struct Session *session,
146                  const struct GNUNET_MessageHeader *message)
147 {
148   struct GNUNET_TIME_Relative ret;
149   int do_forward;
150   struct InboundMessage *im;
151   size_t msg_size = ntohs (message->size);
152   size_t size =
153       sizeof (struct InboundMessage) + msg_size;
154   char buf[size] GNUNET_ALIGN;
155
156   ret = GNUNET_TIME_UNIT_ZERO;
157   do_forward = GNUNET_SYSERR;
158   ret = GST_neighbours_calculate_receive_delay (peer, msg_size, &do_forward);
159
160   if (!GST_neighbours_test_connected (peer))
161   {
162
163     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164                 "Discarded %u bytes type %u payload from peer `%s'\n", msg_size,
165                 ntohs (message->type), GNUNET_i2s (peer));
166
167     GNUNET_STATISTICS_update (GST_stats,
168                               gettext_noop
169                               ("# bytes payload discarded due to not connected peer "),
170                               msg_size, GNUNET_NO);
171     return ret;
172   }
173
174   if (do_forward != GNUNET_YES)
175     return ret;
176   im = (struct InboundMessage *) buf;
177   im->header.size = htons (size);
178   im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
179   im->peer = *peer;
180   memcpy (&im[1], message, ntohs (message->size));
181
182   //GNUNET_ATS_address_add (GST_ats, address, session, NULL, 0);
183   GST_clients_broadcast (&im->header, GNUNET_YES);
184
185   return ret;
186 }
187
188
189 /**
190  * Function called by the transport for each received message.
191  * This function should also be called with "NULL" for the
192  * message to signal that the other peer disconnected.
193  *
194  * @param cls closure, const char* with the name of the plugin we received the message from
195  * @param peer (claimed) identity of the other peer
196  * @param message the message, NULL if we only care about
197  *                learning about the delay until we should receive again -- FIXME!
198  * @param session identifier used for this session (NULL for plugins
199  *                that do not offer bi-directional communication to the sender
200  *                using the same "connection")
201  * @param sender_address binary address of the sender (if we established the
202  *                connection or are otherwise sure of it; should be NULL
203  *                for inbound TCP/UDP connections since it it not clear
204  *                that we could establish ourselves a connection to that
205  *                IP address and get the same system)
206  * @param sender_address_len number of bytes in sender_address
207  * @return how long the plugin should wait until receiving more data
208  *         (plugins that do not support this, can ignore the return value)
209  */
210 struct GNUNET_TIME_Relative
211 GST_receive_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
212                              const struct GNUNET_MessageHeader *message,
213                              struct Session *session,
214                              const char *sender_address,
215                              uint16_t sender_address_len)
216 {
217   const char *plugin_name = cls;
218   struct GNUNET_TIME_Relative ret;
219   struct GNUNET_HELLO_Address address;
220   uint16_t type;
221
222   address.peer = *peer;
223   address.address = sender_address;
224   address.address_length = sender_address_len;
225   address.transport_name = plugin_name;
226   ret = GNUNET_TIME_UNIT_ZERO;
227   if (NULL == message)
228     goto end;
229   type = ntohs (message->type);
230   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received Message with type %u from peer `%s'\n", type, GNUNET_i2s (peer));
231
232   GNUNET_STATISTICS_update (GST_stats,
233                         gettext_noop
234                         ("# bytes total received"),
235                             ntohs (message->size), GNUNET_NO);
236
237   switch (type)
238   {
239   case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
240     /* Legacy HELLO message, discard  */
241     return ret;
242   case GNUNET_MESSAGE_TYPE_HELLO:
243     GST_validation_handle_hello (message);
244     return ret;
245   case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
246     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
247                 "Processing `%s' from `%s'\n", "PING",
248                 (sender_address !=
249                  NULL) ? GST_plugins_a2s (&address) : "<inbound>");
250     GST_validation_handle_ping (peer, message, &address, session);
251     break;
252   case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
253     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
254                 "Processing `%s' from `%s'\n", "PONG",
255                 (sender_address !=
256                  NULL) ? GST_plugins_a2s (&address) : "<inbound>");
257     GST_validation_handle_pong (peer, message);
258     break;
259   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT:
260     GST_neighbours_handle_connect (message, peer, &address, session);
261     break;
262   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK:
263     GST_neighbours_handle_connect_ack (message, peer, &address, session);
264     break;
265   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
266     GST_neighbours_handle_session_ack (message, peer, &address, session);
267     break;
268   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
269     GST_neighbours_handle_disconnect_message (peer, message);
270     break;
271   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
272     GST_neighbours_keepalive (peer);
273     break;
274   case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
275     GST_neighbours_keepalive_response (peer);
276     break;
277   default:
278     /* should be payload */
279     GNUNET_STATISTICS_update (GST_stats,
280                               gettext_noop
281                               ("# bytes payload received"),
282                               ntohs (message->size), GNUNET_NO);
283     ret = process_payload (peer, &address, session, message);
284     break;
285   }
286 end:
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288               "Allowing receive from peer %s to continue in %llu ms\n",
289               GNUNET_i2s (peer), (unsigned long long) ret.rel_value);
290   return ret;
291 }
292
293
294 /**
295  * Function that will be called for each address the transport
296  * is aware that it might be reachable under.  Update our HELLO.
297  *
298  * @param cls name of the plugin (const char*)
299  * @param add_remove should the address added (YES) or removed (NO) from the
300  *                   set of valid addresses?
301  * @param addr one of the addresses of the host
302  *        the specific address format depends on the transport
303  * @param addrlen length of the address
304  * @param dest_plugin destination plugin to use this address with
305  */
306 static void
307 plugin_env_address_change_notification (void *cls, int add_remove,
308                                         const void *addr, size_t addrlen,
309                                         const char *dest_plugin)
310 {
311   struct GNUNET_HELLO_Address address;
312
313   address.peer = GST_my_identity;
314   address.transport_name = dest_plugin;
315   address.address = addr;
316   address.address_length = addrlen;
317   GST_hello_modify_addresses (add_remove, &address);
318 }
319
320
321 /**
322  * Function that will be called whenever the plugin internally
323  * cleans up a session pointer and hence the service needs to
324  * discard all of those sessions as well.  Plugins that do not
325  * use sessions can simply omit calling this function and always
326  * use NULL wherever a session pointer is needed.  This function
327  * should be called BEFORE a potential "TransmitContinuation"
328  * from the "TransmitFunction".
329  *
330  * @param cls closure
331  * @param peer which peer was the session for
332  * @param session which session is being destoyed
333  */
334 static void
335 plugin_env_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
336                         struct Session *session)
337 {
338   const char *transport_name = cls;
339   struct GNUNET_HELLO_Address address;
340
341   GNUNET_assert (strlen (transport_name) > 0);
342   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %p to peer `%s' ended \n",
343               session, GNUNET_i2s (peer));
344   if (NULL != session)
345     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
346                      "transport-ats",
347                      "Telling ATS to destroy session %p from peer %s\n",
348                      session, GNUNET_i2s (peer));
349   address.peer = *peer;
350   address.address = NULL;
351   address.address_length = 0;
352   address.transport_name = transport_name;
353   GST_neighbours_session_terminated (peer, session);
354
355   /* Tell ATS that session has ended */
356   GNUNET_ATS_address_destroyed (GST_ats, &address, session);
357 }
358
359
360 /**
361  * Function that will be called to figure if an address is an loopback,
362  * LAN, WAN etc. address
363  *
364  * @param cls closure
365  * @param addr binary address
366  * @param addrlen length of the address
367  * @return ATS Information containing the network type
368  */
369 static struct GNUNET_ATS_Information
370 plugin_env_address_to_type (void *cls,
371                             const struct sockaddr *addr,
372                             size_t addrlen)
373 {
374   struct GNUNET_ATS_Information ats;
375   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
376   ats.value = htonl (GNUNET_ATS_NET_UNSPECIFIED);
377   if (GST_ats == NULL)
378   {
379     GNUNET_break (0);
380     return ats;
381   }
382   if (((addr->sa_family != AF_INET) && (addrlen != sizeof (struct sockaddr_in))) &&
383       ((addr->sa_family != AF_INET6) && (addrlen != sizeof (struct sockaddr_in6))) &&
384       (addr->sa_family != AF_UNIX))
385   {
386     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed address with length %u `%s'\n",
387                 addrlen,
388                 GNUNET_a2s(addr, addrlen));
389     GNUNET_break (0);
390     return ats;
391   }
392   return GNUNET_ATS_address_get_type(GST_ats, addr, addrlen);
393 }
394
395
396 /**
397  * Notify ATS about the new address including the network this address is
398  * located in.
399  *
400  * @param address the address
401  * @param session the session
402  */
403 void
404 GST_ats_add_address (struct GNUNET_HELLO_Address *address,
405                                                  void *session)
406 {
407   struct GNUNET_TRANSPORT_PluginFunctions *papi;
408         struct GNUNET_ATS_Information ats;
409         uint32_t net;
410
411   /* valid new address, let ATS know! */
412   if (NULL == address->transport_name)
413   {
414         GNUNET_break (0);
415         return;
416   }
417   if (NULL == (papi = GST_plugins_find (address->transport_name)))
418   {
419     /* we don't have the plugin for this address */
420         GNUNET_break (0);
421         return;
422   }
423
424         net = papi->get_network (NULL, session);
425         ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
426         ats.value = htonl(net);
427         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Notifying ATS about new address `%s' in network %s\n",
428                         (0 == address->address_length) ? "<inbound>" : GST_plugins_a2s (address),
429                         GNUNET_ATS_print_network_type(net));
430         GNUNET_ATS_address_add (GST_ats,
431                         address, session, &ats, 1);
432 }
433
434
435 /**
436  * Notify ATS about property changes to an address
437  *
438  * @param peer the peer
439  * @param address the address
440  * @param session the session
441  * @param ats performance information
442  * @param ats_count number of elements in ats
443  */
444 void
445 GST_ats_update_metrics (const struct GNUNET_PeerIdentity *peer,
446                         const struct GNUNET_HELLO_Address *address,
447                         struct Session *session,
448                         const struct GNUNET_ATS_Information *ats,
449                         uint32_t ats_count)
450 {
451         struct GNUNET_ATS_Information *ats_new;
452         /* Call to manipulation to manipulate ATS information */
453         ats_new = GST_manipulation_manipulate_metrics (peer, address, session, ats, ats_count);
454         if (NULL == ats_new)
455         {
456                         GNUNET_break (0);
457                         return;
458         }
459   GNUNET_ATS_address_update (GST_ats, address, session, ats_new, ats_count);
460   GNUNET_free (ats_new);
461 }
462
463
464 /**
465  * Function that will be called to figure if an address is an loopback,
466  * LAN, WAN etc. address
467  *
468  * @param cls closure
469  * @param peer the peer
470  * @param address binary address
471  * @param address_len length of the address
472  * @param session the session
473  * @param ats the ats information to update
474  * @param ats_count the number of ats elements
475  */
476 static void
477 plugin_env_update_metrics (void *cls,
478                            const struct GNUNET_PeerIdentity *peer,
479                            const void *address,
480                            uint16_t address_len,
481                            struct Session *session,
482                            const struct GNUNET_ATS_Information *ats,
483                            uint32_t ats_count)
484 {
485   struct GNUNET_HELLO_Address haddress;
486   const char *plugin_name = cls;
487
488         if ((NULL == ats) || (0 == ats_count))
489                 return;
490         GNUNET_assert (NULL != GST_ats);
491
492         haddress.peer = *peer;
493         haddress.address = address;
494   haddress.address_length = address_len;
495   haddress.transport_name = plugin_name;
496
497   GST_ats_update_metrics (peer, &haddress, session, ats, ats_count);
498 }
499
500 static void
501 plugin_env_session_start (void *cls,
502            const struct GNUNET_PeerIdentity *peer,
503            const char *plugin,
504            const void *address,
505            uint16_t address_len,
506            struct Session *session,
507            const struct GNUNET_ATS_Information *ats,
508            uint32_t ats_count)
509 {
510         if (NULL == peer)
511         {
512                 GNUNET_break (0);
513                 return;
514         }
515         if (NULL == plugin)
516         {
517                 GNUNET_break (0);
518                 return;
519         }
520         if (NULL == address)
521         {
522                 GNUNET_break (0);
523                 return;
524         }
525         if (NULL == session)
526         {
527                 GNUNET_break (0);
528                 return;
529         }
530
531         struct GNUNET_HELLO_Address *addr;
532         addr = GNUNET_HELLO_address_allocate (peer, plugin, address, address_len);
533         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding peer `%s' address %s session %p\n",
534                         GNUNET_i2s (peer), GST_plugins_a2s(addr), session);
535         GST_ats_add_address (addr, session);
536         if (0 < ats_count)
537                 GST_ats_update_metrics (peer, addr, session, ats, ats_count);
538         GNUNET_free (addr);
539 }
540
541 /**
542  * Function called by ATS to notify the callee that the
543  * assigned bandwidth or address for a given peer was changed.  If the
544  * callback is called with address/bandwidth assignments of zero, the
545  * ATS disconnect function will still be called once the disconnect
546  * actually happened.
547  *
548  * @param cls closure
549  * @param address address to use (for peer given in address)
550  * @param session session to use (if available)
551  * @param bandwidth_out assigned outbound bandwidth for the connection, 0 to disconnect from peer
552  * @param bandwidth_in assigned inbound bandwidth for the connection, 0 to disconnect from peer
553  * @param ats ATS information
554  * @param ats_count number of ATS elements
555  */
556 static void
557 ats_request_address_change (void *cls,
558                             const struct GNUNET_HELLO_Address *address,
559                             struct Session *session,
560                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
561                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
562                             const struct GNUNET_ATS_Information *ats,
563                             uint32_t ats_count)
564 {
565   uint32_t bw_in = ntohl (bandwidth_in.value__);
566   uint32_t bw_out = ntohl (bandwidth_out.value__);
567
568   /* ATS tells me to disconnect from peer */
569   if ((bw_in == 0) && (bw_out == 0))
570   {
571     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572                 "ATS tells me to disconnect from peer `%s'\n",
573                 GNUNET_i2s (&address->peer));
574     GST_neighbours_force_disconnect (&address->peer);
575     return;
576   }
577   GST_neighbours_switch_to_address (&address->peer, address, session, ats,
578                                          ats_count, bandwidth_in,
579                                          bandwidth_out);
580 }
581
582
583 /**
584  * Function called to notify transport users that another
585  * peer connected to us.
586  *
587  * @param cls closure
588  * @param peer the peer that connected
589  * @param bandwidth_in inbound bandwidth in NBO
590  * @param bandwidth_out outbound bandwidth in NBO
591  */
592 static void
593 neighbours_connect_notification (void *cls,
594                                  const struct GNUNET_PeerIdentity *peer,
595                                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
596                                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
597 {
598   size_t len = sizeof (struct ConnectInfoMessage);
599   char buf[len] GNUNET_ALIGN;
600   struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
601
602   connections++;
603   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
604               "We are now connected to peer `%s' and %u peers in total\n",
605               GNUNET_i2s (peer), connections);
606
607   connect_msg->header.size = htons (sizeof (buf));
608   connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
609   connect_msg->id = *peer;
610   connect_msg->quota_in = bandwidth_in;
611   connect_msg->quota_out = bandwidth_out;
612   GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
613 }
614
615
616 /**
617  * Function called to notify transport users that another
618  * peer disconnected from us.
619  *
620  * @param cls closure
621  * @param peer the peer that disconnected
622  */
623 static void
624 neighbours_disconnect_notification (void *cls,
625                                     const struct GNUNET_PeerIdentity *peer)
626 {
627   struct DisconnectInfoMessage disconnect_msg;
628
629   connections--;
630   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
631               "Peer `%s' disconnected and we are connected to %u peers\n",
632               GNUNET_i2s (peer), connections);
633
634   GST_manipulation_peer_disconnect (peer);
635   disconnect_msg.header.size = htons (sizeof (struct DisconnectInfoMessage));
636   disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
637   disconnect_msg.reserved = htonl (0);
638   disconnect_msg.peer = *peer;
639   GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
640 }
641
642
643 /**
644  * Function called to notify transport users that a neighbour peer changed its
645  * active address.
646  *
647  * @param cls closure
648  * @param peer peer this update is about (never NULL)
649  * @param address address, NULL on disconnect
650  */
651 static void
652 neighbours_address_notification (void *cls,
653                                  const struct GNUNET_PeerIdentity *peer,
654                                  const struct GNUNET_HELLO_Address *address)
655 {
656   GST_clients_broadcast_address_notification (peer, address);
657 }
658
659
660 /**
661  * Function called when the service shuts down.  Unloads our plugins
662  * and cancels pending validations.
663  *
664  * @param cls closure, unused
665  * @param tc task context (unused)
666  */
667 static void
668 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
669 {
670   GST_neighbours_stop ();
671   GST_validation_stop ();
672   GST_plugins_unload ();
673
674   GNUNET_ATS_scheduling_done (GST_ats);
675   GST_ats = NULL;
676   GST_clients_stop ();
677   GST_blacklist_stop ();
678   GST_hello_stop ();
679   GST_manipulation_stop ();
680
681   if (NULL != GST_peerinfo)
682   {
683     GNUNET_PEERINFO_disconnect (GST_peerinfo);
684     GST_peerinfo = NULL;
685   }
686   if (NULL != GST_stats)
687   {
688     GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
689     GST_stats = NULL;
690   }
691   if (NULL != GST_my_private_key)
692   {
693     GNUNET_CRYPTO_ecc_key_free (GST_my_private_key);
694     GST_my_private_key = NULL;
695   }
696   GST_server = NULL;
697 }
698
699
700 /**
701  * Initiate transport service.
702  *
703  * @param cls closure
704  * @param server the initialized server
705  * @param c configuration to use
706  */
707 static void
708 run (void *cls, struct GNUNET_SERVER_Handle *server,
709      const struct GNUNET_CONFIGURATION_Handle *c)
710 {
711   char *keyfile;
712   struct GNUNET_CRYPTO_EccPrivateKey *pk;
713   long long unsigned int max_fd_cfg;
714   int max_fd_rlimit;
715   int max_fd;
716   int friend_only;
717
718   /* setup globals */
719   GST_cfg = c;
720   if (GNUNET_OK !=
721       GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
722                                                &keyfile))
723   {
724     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
725                 _
726                 ("Transport service is lacking key configuration settings.  Exiting.\n"));
727     GNUNET_SCHEDULER_shutdown ();
728     return;
729   }
730   if (GNUNET_OK !=
731       GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION",
732                                            &hello_expiration))
733   {
734     hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
735   }
736   GST_server = server;
737   pk = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile);
738   GNUNET_free (keyfile);
739   GNUNET_assert (NULL != pk);
740   GST_my_private_key = pk;
741
742   GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
743   GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
744   GNUNET_CRYPTO_ecc_key_get_public (GST_my_private_key, &GST_my_public_key);
745   GNUNET_CRYPTO_hash (&GST_my_public_key, sizeof (GST_my_public_key),
746                       &GST_my_identity.hashPubKey);
747   GNUNET_assert (NULL != GST_my_private_key);
748
749   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
750                                 NULL);
751   if (NULL == GST_peerinfo)
752   {
753     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
754                 _("Could not access PEERINFO service.  Exiting.\n"));
755     GNUNET_SCHEDULER_shutdown ();
756     return;
757   }
758
759   max_fd_rlimit = 0;
760   max_fd_cfg = 0;
761   max_fd = 0;
762 #if HAVE_GETRLIMIT
763   struct rlimit r_file;
764   if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
765   {
766     max_fd_rlimit = r_file.rlim_cur;
767     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768                 "Maximum number of open files was: %u/%u\n", r_file.rlim_cur,
769                 r_file.rlim_max);
770   }
771   max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
772 #endif
773   GNUNET_CONFIGURATION_get_value_number (GST_cfg, "transport", "MAX_FD", &max_fd_cfg);
774
775   if (max_fd_cfg > max_fd_rlimit)
776         max_fd = max_fd_cfg;
777   else
778         max_fd = max_fd_rlimit;
779   if (max_fd < DEFAULT_MAX_FDS)
780         max_fd = DEFAULT_MAX_FDS;
781
782   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783               "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
784               max_fd, (max_fd / 3) , (max_fd / 3) * 2);
785
786   friend_only = GNUNET_CONFIGURATION_get_value_yesno(GST_cfg, "topology","FRIENDS-ONLY");
787   if (GNUNET_SYSERR == friend_only)
788         friend_only = GNUNET_NO; /* According to topology defaults */
789   /* start subsystems */
790   GST_hello_start (friend_only, &process_hello_update, NULL);
791   GNUNET_assert (NULL != GST_hello_get());
792   GST_blacklist_start (GST_server, GST_cfg, &GST_my_identity);
793   GST_ats =
794       GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
795   GST_manipulation_init (GST_cfg);
796   GST_plugins_load (&GST_manipulation_recv,
797                     &plugin_env_address_change_notification,
798                     &plugin_env_session_start,
799                     &plugin_env_session_end,
800                     &plugin_env_address_to_type,
801                     &plugin_env_update_metrics);
802   GST_neighbours_start (NULL,
803                         &neighbours_connect_notification,
804                         &neighbours_disconnect_notification,
805                         &neighbours_address_notification,
806                         (max_fd / 3) * 2);
807   GST_clients_start (GST_server);
808   GST_validation_start ((max_fd / 3));
809 }
810
811
812 /**
813  * The main function for the transport service.
814  *
815  * @param argc number of arguments from the command line
816  * @param argv command line arguments
817  * @return 0 ok, 1 on error
818  */
819 int
820 main (int argc, char *const *argv)
821 {
822   return (GNUNET_OK ==
823           GNUNET_SERVICE_run (argc, argv, "transport",
824                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
825 }
826
827 /* end of file gnunet-service-transport.c */