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