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