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