fix
[oweals/gnunet.git] / src / core / gnunet-service-core_neighbours.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 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 core/gnunet-service-core_neighbours.c
23  * @brief code for managing low-level 'plaintext' connections with transport (key exchange may or may not be done yet)
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet-service-core.h"
31 #include "gnunet-service-core_neighbours.h"
32 #include "gnunet-service-core_kx.h"
33 #include "gnunet-service-core_sessions.h"
34 #include "gnunet_constants.h"
35
36
37 /**
38  * Message ready for transmission via transport service.  This struct
39  * is followed by the actual content of the message.
40  */
41 struct NeighbourMessageEntry
42 {
43
44   /**
45    * We keep messages in a doubly linked list.
46    */
47   struct NeighbourMessageEntry *next;
48
49   /**
50    * We keep messages in a doubly linked list.
51    */
52   struct NeighbourMessageEntry *prev;
53
54   /**
55    * By when are we supposed to transmit this message?
56    */
57   struct GNUNET_TIME_Absolute deadline;
58
59   /**
60    * How long is the message? (number of bytes following the "struct
61    * MessageEntry", but not including the size of "struct
62    * MessageEntry" itself!)
63    */
64   size_t size;
65
66 };
67
68
69 /**
70  * Data kept per transport-connected peer.
71  */
72 struct Neighbour
73 {
74
75   /**
76    * Head of the batched message queue (already ordered, transmit
77    * starting with the head).
78    */
79   struct NeighbourMessageEntry *message_head;
80
81   /**
82    * Tail of the batched message queue (already ordered, append new
83    * messages to tail).
84    */
85   struct NeighbourMessageEntry *message_tail;
86
87   /**
88    * Handle for pending requests for transmission to this peer
89    * with the transport service.  NULL if no request is pending.
90    */
91   struct GNUNET_TRANSPORT_TransmitHandle *th;
92
93   /**
94    * Information about the key exchange with the other peer.
95    */
96   struct GSC_KeyExchangeInfo *kxinfo;
97
98   /**
99    * Identity of the other peer.
100    */
101   struct GNUNET_PeerIdentity peer;
102
103   /**
104    * ID of task used for re-trying plaintext scheduling.
105    */
106   GNUNET_SCHEDULER_TaskIdentifier retry_plaintext_task;
107
108 };
109
110
111 /**
112  * Map of peer identities to 'struct Neighbour'.
113  */
114 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
115
116 /**
117  * Transport service.
118  */
119 static struct GNUNET_TRANSPORT_Handle *transport;
120
121
122 /**
123  * Find the entry for the given neighbour.
124  *
125  * @param peer identity of the neighbour
126  * @return NULL if we are not connected, otherwise the
127  *         neighbour's entry.
128  */
129 static struct Neighbour *
130 find_neighbour (const struct GNUNET_PeerIdentity *peer)
131 {
132   if (NULL == neighbours)
133     return NULL;
134   return GNUNET_CONTAINER_multihashmap_get (neighbours, &peer->hashPubKey);
135 }
136
137
138 /**
139  * Free the given entry for the neighbour.
140  *
141  * @param n neighbour to free
142  */
143 static void
144 free_neighbour (struct Neighbour *n)
145 {
146   struct NeighbourMessageEntry *m;
147
148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149               "Destroying neighbour entry for peer `%4s'\n",
150               GNUNET_i2s (&n->peer));
151   while (NULL != (m = n->message_head))
152   {
153     GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
154     GNUNET_free (m);
155   }
156   if (NULL != n->th)
157   {
158     GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
159     n->th = NULL;
160   }
161   GNUNET_STATISTICS_update (GSC_stats,
162                             gettext_noop
163                             ("# sessions terminated by transport disconnect"),
164                             1, GNUNET_NO);
165   if (NULL != n->kxinfo)
166   {
167     GSC_KX_stop (n->kxinfo);
168     n->kxinfo = NULL;
169   }
170   if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
171   {
172     GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
173     n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
174   }
175   GNUNET_assert (GNUNET_OK ==
176                  GNUNET_CONTAINER_multihashmap_remove (neighbours,
177                                                        &n->peer.hashPubKey, n));
178   GNUNET_STATISTICS_set (GSC_stats,
179                          gettext_noop ("# neighbour entries allocated"),
180                          GNUNET_CONTAINER_multihashmap_size (neighbours),
181                          GNUNET_NO);
182   GNUNET_free (n);
183 }
184
185
186 /**
187  * Check if we have encrypted messages for the specified neighbour
188  * pending, and if so, check with the transport about sending them
189  * out.
190  *
191  * @param n neighbour to check.
192  */
193 static void
194 process_queue (struct Neighbour *n);
195
196
197 /**
198  * Function called when the transport service is ready to receive a
199  * message for the respective peer
200  *
201  * @param cls neighbour to use message from
202  * @param size number of bytes we can transmit
203  * @param buf where to copy the message
204  * @return number of bytes transmitted
205  */
206 static size_t
207 transmit_ready (void *cls, size_t size, void *buf)
208 {
209   struct Neighbour *n = cls;
210   struct NeighbourMessageEntry *m;
211   size_t ret;
212   char *cbuf;
213
214   n->th = NULL;
215   m = n->message_head;
216   if (m == NULL)
217   {
218     GNUNET_break (0);
219     return 0;
220   }
221   GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
222   if (buf == NULL)
223   {
224     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225                 "Transmission of message of type %u and size %u failed\n",
226                 (unsigned int)
227                 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
228                 (unsigned int) m->size);
229     GNUNET_free (m);
230     process_queue (n);
231     return 0;
232   }
233   cbuf = buf;
234   GNUNET_assert (size >= m->size);
235   memcpy (cbuf, &m[1], m->size);
236   ret = m->size;
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238               "Copied message of type %u and size %u into transport buffer for `%4s'\n",
239               (unsigned int)
240               ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
241               (unsigned int) ret, GNUNET_i2s (&n->peer));
242   GNUNET_free (m);
243   process_queue (n);
244   GNUNET_STATISTICS_update (GSC_stats,
245                             gettext_noop
246                             ("# encrypted bytes given to transport"), ret,
247                             GNUNET_NO);
248   return ret;
249 }
250
251
252 /**
253  * Check if we have messages for the specified neighbour pending, and
254  * if so, check with the transport about sending them out.
255  *
256  * @param n neighbour to check.
257  */
258 static void
259 process_queue (struct Neighbour *n)
260 {
261   struct NeighbourMessageEntry *m;
262
263   if (n->th != NULL)
264     return;                     /* request already pending */
265   m = n->message_head;
266   if (m == NULL)
267   {
268     /* notify sessions that the queue is empty and more messages
269      * could thus be queued now */
270     GSC_SESSIONS_solicit (&n->peer);
271     return;
272   }
273   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274               "Asking transport for transmission of %u bytes to `%4s' in next %s\n",
275               (unsigned int) m->size, GNUNET_i2s (&n->peer),
276               GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (m->deadline), GNUNET_NO));
277   n->th =
278       GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer, m->size, 0,
279                                               GNUNET_TIME_absolute_get_remaining
280                                               (m->deadline), &transmit_ready,
281                                               n);
282   if (n->th != NULL)
283     return;
284   /* message request too large or duplicate request */
285   GNUNET_break (0);
286   /* discard encrypted message */
287   GNUNET_CONTAINER_DLL_remove (n->message_head, n->message_tail, m);
288   GNUNET_free (m);
289   process_queue (n);
290 }
291
292
293
294 /**
295  * Function called by transport to notify us that
296  * a peer connected to us (on the network level).
297  *
298  * @param cls closure
299  * @param peer the peer that connected
300  * @param atsi performance data
301  * @param atsi_count number of entries in ats (excluding 0-termination)
302  */
303 static void
304 handle_transport_notify_connect (void *cls,
305                                  const struct GNUNET_PeerIdentity *peer)
306 {
307   struct Neighbour *n;
308
309   if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
310   {
311     GNUNET_break (0);
312     return;
313   }
314   n = find_neighbour (peer);
315   if (n != NULL)
316   {
317     /* duplicate connect notification!? */
318     GNUNET_break (0);
319     return;
320   }
321   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received connection from `%4s'.\n",
322               GNUNET_i2s (peer));
323   n = GNUNET_malloc (sizeof (struct Neighbour));
324   n->peer = *peer;
325   GNUNET_assert (GNUNET_OK ==
326                  GNUNET_CONTAINER_multihashmap_put (neighbours,
327                                                     &n->peer.hashPubKey, n,
328                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
329   GNUNET_STATISTICS_set (GSC_stats,
330                          gettext_noop ("# neighbour entries allocated"),
331                          GNUNET_CONTAINER_multihashmap_size (neighbours),
332                          GNUNET_NO);
333   n->kxinfo = GSC_KX_start (peer);
334 }
335
336
337 /**
338  * Function called by transport telling us that a peer
339  * disconnected.
340  *
341  * @param cls closure
342  * @param peer the peer that disconnected
343  */
344 static void
345 handle_transport_notify_disconnect (void *cls,
346                                     const struct GNUNET_PeerIdentity *peer)
347 {
348   struct Neighbour *n;
349
350   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351               "Peer `%4s' disconnected from us; received notification from transport.\n",
352               GNUNET_i2s (peer));
353   n = find_neighbour (peer);
354   if (n == NULL)
355   {
356     GNUNET_break (0);
357     return;
358   }
359   free_neighbour (n);
360 }
361
362
363 /**
364  * Function called by the transport for each received message.
365  *
366  * @param cls closure
367  * @param peer (claimed) identity of the other peer
368  * @param message the message
369  * @param atsi performance data
370  * @param atsi_count number of entries in ats (excluding 0-termination)
371  */
372 static void
373 handle_transport_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
374                           const struct GNUNET_MessageHeader *message)
375 {
376   struct Neighbour *n;
377   uint16_t type;
378
379   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380               "Received message of type %u from `%4s', demultiplexing.\n",
381               (unsigned int) ntohs (message->type), GNUNET_i2s (peer));
382   if (0 == memcmp (peer, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
383   {
384     GNUNET_break (0);
385     return;
386   }
387   n = find_neighbour (peer);
388   if (n == NULL)
389   {
390     /* received message from peer that is not connected!? */
391     GNUNET_break (0);
392     return;
393   }
394   type = ntohs (message->type);
395   switch (type)
396   {
397   case GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY:
398     GSC_KX_handle_ephemeral_key (n->kxinfo, message);
399     break;
400   case GNUNET_MESSAGE_TYPE_CORE_PING:
401     GSC_KX_handle_ping (n->kxinfo, message);
402     break;
403   case GNUNET_MESSAGE_TYPE_CORE_PONG:
404     GSC_KX_handle_pong (n->kxinfo, message);
405     break;
406   case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
407     GSC_KX_handle_encrypted_message (n->kxinfo, message);
408     break;
409   case GNUNET_MESSAGE_TYPE_DUMMY:
410     /*  Dummy messages for testing / benchmarking, just discard */
411     break;
412   default:
413     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
414                 _
415                 ("Unsupported message of type %u (%u bytes) received from peer `%s'\n"),
416                 (unsigned int) type, (unsigned int) ntohs (message->size),
417                 GNUNET_i2s (peer));
418     return;
419   }
420 }
421
422
423 /**
424  * Transmit the given message to the given target.
425  *
426  * @param target peer that should receive the message (must be connected)
427  * @param msg message to transmit
428  * @param timeout by when should the transmission be done?
429  */
430 void
431 GSC_NEIGHBOURS_transmit (const struct GNUNET_PeerIdentity *target,
432                          const struct GNUNET_MessageHeader *msg,
433                          struct GNUNET_TIME_Relative timeout)
434 {
435   struct NeighbourMessageEntry *me;
436   struct Neighbour *n;
437   size_t msize;
438
439   n = find_neighbour (target);
440   if (NULL == n)
441   {
442     GNUNET_break (0);
443     return;
444   }
445   msize = ntohs (msg->size);
446   me = GNUNET_malloc (sizeof (struct NeighbourMessageEntry) + msize);
447   me->deadline = GNUNET_TIME_relative_to_absolute (timeout);
448   me->size = msize;
449   memcpy (&me[1], msg, msize);
450   GNUNET_CONTAINER_DLL_insert_tail (n->message_head, n->message_tail, me);
451   process_queue (n);
452 }
453
454
455 /**
456  * Initialize neighbours subsystem.
457  */
458 int
459 GSC_NEIGHBOURS_init ()
460 {
461   neighbours = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
462   transport =
463       GNUNET_TRANSPORT_connect (GSC_cfg, &GSC_my_identity, NULL,
464                                 &handle_transport_receive,
465                                 &handle_transport_notify_connect,
466                                 &handle_transport_notify_disconnect);
467   if (NULL == transport)
468   {
469     GNUNET_CONTAINER_multihashmap_destroy (neighbours);
470     neighbours = NULL;
471     return GNUNET_SYSERR;
472   }
473   return GNUNET_OK;
474 }
475
476
477 /**
478  * Wrapper around 'free_neighbour'.
479  *
480  * @param cls unused
481  * @param key peer identity
482  * @param value the 'struct Neighbour' to free
483  * @return GNUNET_OK (continue to iterate)
484  */
485 static int
486 free_neighbour_helper (void *cls, const struct GNUNET_HashCode * key, void *value)
487 {
488   struct Neighbour *n = value;
489
490   /* transport should have 'disconnected' all neighbours... */
491   GNUNET_break (0);
492   free_neighbour (n);
493   return GNUNET_OK;
494 }
495
496
497 /**
498  * Shutdown neighbours subsystem.
499  */
500 void
501 GSC_NEIGHBOURS_done ()
502 {
503   if (NULL != transport)
504   {
505     GNUNET_TRANSPORT_disconnect (transport);
506     transport = NULL;
507   }
508   if (NULL != neighbours)
509   {
510     GNUNET_CONTAINER_multihashmap_iterate (neighbours, &free_neighbour_helper,
511                                            NULL);
512     GNUNET_CONTAINER_multihashmap_destroy (neighbours);
513     neighbours = NULL;
514   }
515 }
516
517 /* end of gnunet-service-core_neighbours.c */