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