use enum GNUNET_ATS_Network_Type instead of uint32_t where appropriate
[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 (NULL == GST_ats)
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 (%s)\n"),
480                 GNUNET_i2s (&address->peer),
481                 GST_plugins_a2s (address),
482                 address->transport_name);
483     GNUNET_break (0);
484   }
485   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
486   ats.value = htonl(net);
487   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488               "Notifying ATS about peer `%s''s new address `%s' session %p in network %s\n",
489               GNUNET_i2s (&address->peer),
490               (0 == address->address_length) ? "<inbound>" : GST_plugins_a2s (address),
491               session,
492               GNUNET_ATS_print_network_type(net));
493   GNUNET_ATS_address_add (GST_ats,
494                           address, session, &ats, 1);
495 }
496
497
498 /**
499  * Notify ATS about property changes to an address
500  *
501  * @param peer the peer
502  * @param address the address
503  * @param session the session
504  * @param ats performance information
505  * @param ats_count number of elements in @a ats
506  */
507 void
508 GST_ats_update_metrics (const struct GNUNET_PeerIdentity *peer,
509                         const struct GNUNET_HELLO_Address *address,
510                         struct Session *session,
511                         const struct GNUNET_ATS_Information *ats,
512                         uint32_t ats_count)
513 {
514   struct GNUNET_ATS_Information *ats_new;
515
516   if (GNUNET_NO == GNUNET_ATS_session_known (GST_ats, address, session))
517     return;
518
519   /* Call to manipulation to manipulate ATS information */
520   ats_new = GST_manipulation_manipulate_metrics (peer, address, session, ats,
521                                                  ats_count);
522   if (NULL == ats_new)
523   {
524     GNUNET_break(0);
525     return;
526   }
527   if (GNUNET_NO == GNUNET_ATS_address_update (GST_ats,
528       address, session, ats_new, ats_count))
529   {
530     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
531                 _("Address or session unknown: failed to update properties for peer `%s' plugin `%s' address `%s' session %p\n"),
532                 GNUNET_i2s (peer),
533                 address->transport_name,
534                 GST_plugins_a2s (address),
535                 session);
536   }
537   GNUNET_free (ats_new);
538 }
539
540
541 /**
542  * Function that will be called to figure if an address is an loopback,
543  * LAN, WAN etc. address
544  *
545  * @param cls closure
546  * @param peer the peer
547  * @param address binary address
548  * @param address_len length of the @a address
549  * @param session the session
550  * @param ats the ats information to update
551  * @param ats_count the number of @a ats elements
552  */
553 static void
554 plugin_env_update_metrics (void *cls,
555                            const struct GNUNET_PeerIdentity *peer,
556                            const void *address,
557                            uint16_t address_len,
558                            struct Session *session,
559                            const struct GNUNET_ATS_Information *ats,
560                            uint32_t ats_count)
561 {
562   const char *plugin_name = cls;
563   struct GNUNET_HELLO_Address haddress;
564
565   if ((NULL == ats) || (0 == ats_count))
566     return;
567   GNUNET_assert (NULL != GST_ats);
568
569   haddress.peer = *peer;
570   haddress.address = address;
571   haddress.address_length = address_len;
572   haddress.transport_name = plugin_name;
573
574   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575               "Updating metrics for peer `%s' address %s session %p\n",
576               GNUNET_i2s (peer),
577               GST_plugins_a2s (&haddress),
578               session);
579   GST_ats_update_metrics (peer, &haddress, session, ats, ats_count);
580 }
581
582
583 /**
584  * Plugin tells transport service about a new (inbound) session
585  *
586  * @param cls unused
587  * @param peer the peer
588  * @param plugin plugin name
589  * @param address address
590  * @param address_len @a address length
591  * @param session the new session
592  * @param ats ats information
593  * @param ats_count number of @a ats information
594  */
595 static void
596 plugin_env_session_start (void *cls,
597                           const struct GNUNET_PeerIdentity *peer,
598                           const char *plugin,
599                           const void *address, uint16_t address_len,
600                           struct Session *session,
601                           const struct GNUNET_ATS_Information *ats,
602                           uint32_t ats_count)
603 {
604   struct GNUNET_HELLO_Address *addr;
605
606   if (NULL == peer)
607   {
608     GNUNET_break(0);
609     return;
610   }
611   if (NULL == plugin)
612   {
613     GNUNET_break(0);
614     return;
615   }
616   if (NULL == session)
617   {
618     GNUNET_break(0);
619     return;
620   }
621
622   addr = GNUNET_HELLO_address_allocate (peer, plugin, address, address_len);
623   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
624              "Notification from plugin `%s' about new session %p from peer `%s' address `%s'\n",
625              plugin,
626              session,
627              GNUNET_i2s (peer),
628              GST_plugins_a2s (addr));
629   GST_ats_add_address (addr, session);
630
631   if (0 < ats_count)
632     GST_ats_update_metrics (peer, addr, session, ats, ats_count);
633   GNUNET_free(addr);
634 }
635
636
637 /**
638  * Function called by ATS to notify the callee that the
639  * assigned bandwidth or address for a given peer was changed.  If the
640  * callback is called with address/bandwidth assignments of zero, the
641  * ATS disconnect function will still be called once the disconnect
642  * actually happened.
643  *
644  * @param cls closure
645  * @param address address to use (for peer given in address)
646  * @param session session to use (if available)
647  * @param bandwidth_out assigned outbound bandwidth for the connection in NBO,
648  *      0 to disconnect from peer
649  * @param bandwidth_in assigned inbound bandwidth for the connection in NBO,
650  *      0 to disconnect from peer
651  * @param ats ATS information
652  * @param ats_count number of @a ats elements
653  */
654 static void
655 ats_request_address_change (void *cls,
656                             const struct GNUNET_HELLO_Address *address,
657                             struct Session *session,
658                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
659                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
660                             const struct GNUNET_ATS_Information *ats,
661                             uint32_t ats_count)
662 {
663   uint32_t bw_in = ntohl (bandwidth_in.value__);
664   uint32_t bw_out = ntohl (bandwidth_out.value__);
665
666   /* ATS tells me to disconnect from peer */
667   if ((bw_in == 0) && (bw_out == 0))
668   {
669     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
670                 "ATS tells me to disconnect from peer `%s'\n",
671                 GNUNET_i2s (&address->peer));
672     GST_neighbours_force_disconnect (&address->peer);
673     return;
674   }
675   GST_neighbours_switch_to_address (&address->peer, address, session, ats,
676                                     ats_count, bandwidth_in,
677                                     bandwidth_out);
678 }
679
680
681 /**
682  * Function called to notify transport users that another
683  * peer connected to us.
684  *
685  * @param cls closure
686  * @param peer the peer that connected
687  * @param bandwidth_in inbound bandwidth in NBO
688  * @param bandwidth_out outbound bandwidth in NBO
689  */
690 static void
691 neighbours_connect_notification (void *cls,
692                                  const struct GNUNET_PeerIdentity *peer,
693                                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
694                                  struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
695 {
696   size_t len = sizeof (struct ConnectInfoMessage);
697   char buf[len] GNUNET_ALIGN;
698   struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
699
700   connections++;
701   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
702               "We are now connected to peer `%s' and %u peers in total\n",
703               GNUNET_i2s (peer), connections);
704   connect_msg->header.size = htons (sizeof (buf));
705   connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
706   connect_msg->id = *peer;
707   connect_msg->quota_in = bandwidth_in;
708   connect_msg->quota_out = bandwidth_out;
709   GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
710 }
711
712
713 /**
714  * Function called to notify transport users that another
715  * peer disconnected from us.
716  *
717  * @param cls closure
718  * @param peer the peer that disconnected
719  */
720 static void
721 neighbours_disconnect_notification (void *cls,
722                                     const struct GNUNET_PeerIdentity *peer)
723 {
724   struct DisconnectInfoMessage disconnect_msg;
725
726   connections--;
727   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
728               "Peer `%s' disconnected and we are connected to %u peers\n",
729               GNUNET_i2s (peer), connections);
730
731   GST_manipulation_peer_disconnect (peer);
732   disconnect_msg.header.size = htons (sizeof (struct DisconnectInfoMessage));
733   disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
734   disconnect_msg.reserved = htonl (0);
735   disconnect_msg.peer = *peer;
736   GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
737 }
738
739
740 /**
741  * Function called to notify transport users that a neighbour peer changed its
742  * active address.
743  *
744  * @param cls closure
745  * @param peer peer this update is about (never NULL)
746  * @param address address, NULL on disconnect
747  */
748 static void
749 neighbours_address_notification (void *cls,
750                                  const struct GNUNET_PeerIdentity *peer,
751                                  const struct GNUNET_HELLO_Address *address)
752 {
753   GST_clients_broadcast_address_notification (peer, address);
754 }
755
756
757 /**
758  * Function called when the service shuts down.  Unloads our plugins
759  * and cancels pending validations.
760  *
761  * @param cls closure, unused
762  * @param tc task context (unused)
763  */
764 static void
765 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
766 {
767   GST_neighbours_stop ();
768   GST_validation_stop ();
769   GST_plugins_unload ();
770
771   GNUNET_ATS_scheduling_done (GST_ats);
772   GST_ats = NULL;
773   GST_clients_stop ();
774   GST_blacklist_stop ();
775   GST_hello_stop ();
776   GST_manipulation_stop ();
777
778   if (NULL != GST_peerinfo)
779   {
780     GNUNET_PEERINFO_disconnect (GST_peerinfo);
781     GST_peerinfo = NULL;
782   }
783   if (NULL != GST_stats)
784   {
785     GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
786     GST_stats = NULL;
787   }
788   if (NULL != GST_my_private_key)
789   {
790     GNUNET_free (GST_my_private_key);
791     GST_my_private_key = NULL;
792   }
793   GST_server = NULL;
794 }
795
796
797 /**
798  * Initiate transport service.
799  *
800  * @param cls closure
801  * @param server the initialized server
802  * @param c configuration to use
803  */
804 static void
805 run (void *cls, struct GNUNET_SERVER_Handle *server,
806      const struct GNUNET_CONFIGURATION_Handle *c)
807 {
808   char *keyfile;
809   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
810   long long unsigned int max_fd_cfg;
811   int max_fd_rlimit;
812   int max_fd;
813   int friend_only;
814
815   /* setup globals */
816   GST_cfg = c;
817   if (GNUNET_OK !=
818       GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
819                                                &keyfile))
820   {
821     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
822                 _("Transport service is lacking key configuration settings. Exiting.\n"));
823     GNUNET_SCHEDULER_shutdown ();
824     return;
825   }
826   if (GNUNET_OK !=
827       GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION",
828                                            &hello_expiration))
829   {
830     hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
831   }
832   GST_server = server;
833   pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile);
834   GNUNET_free (keyfile);
835   GNUNET_assert (NULL != pk);
836   GST_my_private_key = pk;
837
838   GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
839   GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
840   GNUNET_CRYPTO_eddsa_key_get_public (GST_my_private_key,
841                                                   &GST_my_identity.public_key);
842   GNUNET_assert (NULL != GST_my_private_key);
843
844   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
845               "My identity is `%4s'\n", GNUNET_i2s (&GST_my_identity));
846
847   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
848                                 NULL);
849   if (NULL == GST_peerinfo)
850   {
851     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
852                 _("Could not access PEERINFO service.  Exiting.\n"));
853     GNUNET_SCHEDULER_shutdown ();
854     return;
855   }
856
857   max_fd_rlimit = 0;
858   max_fd_cfg = 0;
859 #if HAVE_GETRLIMIT
860   struct rlimit r_file;
861   if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
862   {
863     max_fd_rlimit = r_file.rlim_cur;
864     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865                 "Maximum number of open files was: %u/%u\n",
866                 r_file.rlim_cur,
867                 r_file.rlim_max);
868   }
869   max_fd_rlimit = (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
870 #endif
871   GNUNET_CONFIGURATION_get_value_number (GST_cfg, "transport", "MAX_FD", &max_fd_cfg);
872
873   if (max_fd_cfg > max_fd_rlimit)
874         max_fd = max_fd_cfg;
875   else
876         max_fd = max_fd_rlimit;
877   if (max_fd < DEFAULT_MAX_FDS)
878         max_fd = DEFAULT_MAX_FDS;
879
880   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881               "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
882               max_fd, (max_fd / 3) , (max_fd / 3) * 2);
883
884   friend_only = GNUNET_CONFIGURATION_get_value_yesno(GST_cfg, "topology","FRIENDS-ONLY");
885   if (GNUNET_SYSERR == friend_only)
886         friend_only = GNUNET_NO; /* According to topology defaults */
887   /* start subsystems */
888   GST_hello_start (friend_only, &process_hello_update, NULL);
889   GNUNET_assert (NULL != GST_hello_get());
890   GST_blacklist_start (GST_server, GST_cfg, &GST_my_identity);
891   GST_ats =
892       GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
893   GST_manipulation_init (GST_cfg);
894   GST_plugins_load (&GST_manipulation_recv,
895                     &plugin_env_address_change_notification,
896                     &plugin_env_session_start,
897                     &plugin_env_session_end,
898                     &plugin_env_address_to_type,
899                     &plugin_env_update_metrics);
900   GST_neighbours_start (NULL,
901                         &neighbours_connect_notification,
902                         &neighbours_disconnect_notification,
903                         &neighbours_address_notification,
904                         (max_fd / 3) * 2);
905   GST_clients_start (GST_server);
906   GST_validation_start ((max_fd / 3));
907 }
908
909
910 /**
911  * The main function for the transport service.
912  *
913  * @param argc number of arguments from the command line
914  * @param argv command line arguments
915  * @return 0 ok, 1 on error
916  */
917 int
918 main (int argc, char *const *argv)
919 {
920   return (GNUNET_OK ==
921           GNUNET_SERVICE_run (argc, argv, "transport",
922                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
923 }
924
925 /* end of file gnunet-service-transport.c */