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