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