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