fixing it one more time!
[oweals/gnunet.git] / src / mesh / mesh_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 mesh/mesh_api.c
23  * @brief mesh service; API for the Mesh. This is used to talk to arbitrary peers
24  *        as of 2011-01Jan-06 this is a mockup.
25  * @author Philipp Tölke
26  */
27 #include <platform.h>
28 #include <gnunet_constants.h>
29 #include <gnunet_mesh_service.h>
30 #include <gnunet_core_service.h>
31 #include <gnunet_container_lib.h>
32 #include <gnunet_applications.h>
33
34 struct tunnel_id
35 {
36   uint32_t id GNUNET_PACKED;
37   struct GNUNET_PeerIdentity initiator;
38   struct GNUNET_PeerIdentity target;
39 };
40
41 static uint32_t current_id = 0;
42
43 struct tunnel_message
44 {
45   struct GNUNET_MessageHeader hdr;
46   struct tunnel_id id;
47   /* followed by another GNUNET_MessageHeader */
48 };
49
50 struct notify_cls
51 {
52   void *notify_cls;
53   GNUNET_CONNECTION_TransmitReadyNotify notify;
54   struct GNUNET_MESH_Tunnel *tunnel;
55 };
56
57 struct GNUNET_MESH_Tunnel
58 {
59   /* The other peer this tunnel leads to; just unicast for the moment! */
60   struct GNUNET_PeerIdentity peer;
61
62   struct tunnel_id id;
63
64   /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
65   GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
66   GNUNET_MESH_TunnelConnectHandler connect_handler;
67   void *handler_cls;
68
69   struct GNUNET_MESH_Handle *handle;
70
71   /* The application-type requested for this tunnel. Is only needed for pending
72    * by_tupe-tunnels
73    */
74   uint16_t application_type;
75
76   struct GNUNET_MESH_TransmitHandle *notify_handle;
77
78   /* The context of the receive-function. */
79   void *ctx;
80
81   /* A list, usable by application-code (for queues) */
82   void *app_head;
83   void *app_tail;
84
85   /* A pointer, usable by application-code */
86   void *app_data;
87 };
88
89 struct tunnel_list_element
90 {
91   struct GNUNET_MESH_Tunnel tunnel;
92   struct tunnel_list_element *next, *prev;
93 };
94
95 struct tunnel_list
96 {
97   struct tunnel_list_element *head, *tail;
98 };
99
100 struct type_list_element
101 {
102   GNUNET_MESH_ApplicationType type;
103   struct type_list_element *next, *prev;
104 };
105
106 struct peer_list_element
107 {
108   struct GNUNET_PeerIdentity peer;
109
110   /* list of application-types */
111   struct type_list_element *type_head, *type_tail;
112
113   struct GNUNET_TRANSPORT_ATS_Information atsi;
114   struct peer_list_element *next, *prev;
115
116   /* The handle that sends the hellos to this peer */
117   struct GNUNET_CORE_TransmitHandle *hello;
118
119   GNUNET_SCHEDULER_TaskIdentifier sched;
120
121   struct GNUNET_MESH_Handle *handle;
122 };
123
124 struct peer_list
125 {
126   struct peer_list_element *head, *tail;
127 };
128
129 struct GNUNET_MESH_Handle
130 {
131   struct GNUNET_CORE_Handle *core;
132   struct GNUNET_MESH_MessageHandler *handlers;
133   struct GNUNET_PeerIdentity myself;
134   unsigned int connected_to_core;
135   struct peer_list connected_peers;
136   struct tunnel_list established_tunnels;
137   struct tunnel_list pending_tunnels;
138   struct tunnel_list pending_by_type_tunnels;
139   void *cls;
140   GNUNET_MESH_TunnelEndHandler *cleaner;
141   size_t hello_message_size;
142   uint16_t *hello_message;
143 };
144
145 static void
146 send_end_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 {
148   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
149     return;
150
151   struct GNUNET_MESH_Tunnel *tunnel = cls;
152
153   tunnel->connect_handler (tunnel->handler_cls, NULL, NULL);
154 }
155
156 static void
157 send_self_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 {
159   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
160     return;
161
162   struct GNUNET_MESH_Tunnel *tunnel = cls;
163
164   tunnel->connect_handler (tunnel->handler_cls, &tunnel->handle->myself, NULL);
165   GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
166 }
167
168 static void
169 call_connect_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 {
171   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
172     return;
173
174   struct GNUNET_MESH_Tunnel *tunnel = cls;
175
176   tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, NULL);
177   GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
178 }
179
180 static void
181 core_startup (void *cls, struct GNUNET_CORE_Handle *core
182               __attribute__ ((unused)),
183               const struct GNUNET_PeerIdentity *my_identity,
184               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey
185               __attribute__ ((unused)))
186 {
187   struct GNUNET_MESH_Handle *handle = cls;
188
189   memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
190   handle->connected_to_core = GNUNET_YES;
191 }
192
193 static size_t
194 send_hello_message (void *cls, size_t size, void *buf)
195 {
196   if (cls == NULL)
197     return 0;
198
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
200
201   struct peer_list_element *element = cls;
202   struct GNUNET_MESH_Handle *handle = element->handle;
203
204   element->hello = NULL;
205   struct GNUNET_MessageHeader *hdr = buf;
206
207   size_t sent =
208       sizeof (struct GNUNET_MessageHeader) + handle->hello_message_size;
209
210   if (sent > size)
211     return 0;
212
213   hdr->type = htons (GNUNET_MESSAGE_TYPE_MESH_HELLO);
214   hdr->size = htons (size);
215
216   memcpy (hdr + 1, handle->hello_message, handle->hello_message_size);
217   return sent;
218 }
219
220 void
221 schedule_hello_message (void *cls,
222                         const struct GNUNET_SCHEDULER_TaskContext *tctx)
223 {
224   struct peer_list_element *element = cls;
225
226   element->sched = GNUNET_SCHEDULER_NO_TASK;
227
228   if ((tctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
229     return;
230
231   if (element->hello == NULL)
232     element->hello =
233         GNUNET_CORE_notify_transmit_ready (element->handle->core, GNUNET_NO, 42,
234                                            GNUNET_TIME_UNIT_SECONDS,
235                                            &element->peer,
236                                            sizeof (struct GNUNET_MessageHeader)
237                                            +
238                                            element->handle->hello_message_size,
239                                            &send_hello_message, element);
240
241   element->sched =
242       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
243                                     schedule_hello_message, cls);
244 }
245
246
247 /**
248  * Core calls this if we are connected to a new peer.
249  *
250  * The peer is added to the connected_peers-list.
251  *
252  */
253 static void
254 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
255               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
256 {
257   struct GNUNET_MESH_Handle *handle = cls;
258
259   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260               "Core tells us we are connected to peer %s\n", GNUNET_i2s (peer));
261
262   /* put the new peer into the list of connected peers */
263   struct peer_list_element *element =
264       GNUNET_malloc (sizeof (struct peer_list_element));
265   memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
266   element->handle = handle;
267
268   /* Send a hello to this peer */
269   element->sched = GNUNET_SCHEDULER_add_now (schedule_hello_message, element);
270
271   if (NULL != atsi)
272     memcpy (&element->atsi, atsi,
273             sizeof (struct GNUNET_TRANSPORT_ATS_Information));
274
275   GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
276                                      handle->connected_peers.tail,
277                                      handle->connected_peers.tail, element);
278
279   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
280
281   while (tunnel != NULL)
282   {
283     if (0 ==
284         memcmp (&tunnel->tunnel.peer, peer,
285                 sizeof (struct GNUNET_PeerIdentity)))
286     {
287       struct tunnel_list_element *next = tunnel->next;
288
289       GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
290                                    handle->pending_tunnels.tail, tunnel);
291       GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
292                                          handle->established_tunnels.tail,
293                                          handle->established_tunnels.tail,
294                                          tunnel);
295       tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, peer, atsi);
296       GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
297       tunnel = next;
298     }
299     else
300       tunnel = tunnel->next;
301   }
302 }
303
304 /**
305  * Core calls this if we disconnect a peer
306  *
307  * Remove this peer from the list of connected peers
308  * Close all tunnels this peer belongs to
309  */
310 static void
311 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
312 {
313   struct GNUNET_MESH_Handle *handle = cls;
314
315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316               "Core tells us we are no longer connected to peer %s\n",
317               GNUNET_i2s (peer));
318
319   struct peer_list_element *element = handle->connected_peers.head;
320
321   while (element != NULL)
322   {
323     if (0 == memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
324       break;
325     element = element->next;
326   }
327   if (element != NULL)
328   {
329     GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
330                                  handle->connected_peers.tail, element);
331     while (element->type_head != NULL)
332     {
333       struct type_list_element *tail = element->type_tail;
334
335       GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
336                                    tail);
337       GNUNET_free (tail);
338     }
339     GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
340     GNUNET_SCHEDULER_cancel (element->sched);
341     GNUNET_free (element);
342   }
343
344   struct tunnel_list_element *telement = handle->established_tunnels.head;
345
346   while (telement != NULL)
347   {
348     if (0 ==
349         memcmp (&telement->tunnel.peer, peer,
350                 sizeof (struct GNUNET_PeerIdentity)))
351     {
352       /* disconnect tunnels */
353       /* outbound tunnels */
354       if (telement->tunnel.connect_handler != NULL &&
355           NULL != telement->tunnel.disconnect_handler)
356         telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
357                                              peer);
358       /* inbound tunnels */
359       else if (NULL != handle->cleaner)
360         handle->cleaner (handle->cls, &telement->tunnel, &telement->tunnel.ctx);
361
362       struct tunnel_list_element *next = telement->next;
363
364       GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
365                                    handle->established_tunnels.tail, telement);
366       GNUNET_free (telement);
367       telement = next;
368     }
369     else
370     {
371       telement = telement->next;
372     }
373   }
374 }
375
376 /**
377  * Receive a message from core.
378  * This is a hello-message, containing the application-types the other peer can receive
379  */
380 static int
381 receive_hello (void *cls, const struct GNUNET_PeerIdentity *other,
382                const struct GNUNET_MessageHeader *message,
383                const struct GNUNET_TRANSPORT_ATS_Information *atsi)
384 {
385   struct GNUNET_MESH_Handle *handle = cls;
386   uint16_t *num = (uint16_t *) (message + 1);
387   GNUNET_MESH_ApplicationType *ports =
388       (GNUNET_MESH_ApplicationType *) (num + 1);
389   unsigned int i;
390
391   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392               "The peer %s tells us he supports %d application-types.\n",
393               GNUNET_i2s (other), ntohs (*num));
394
395   struct peer_list_element *element = handle->connected_peers.head;
396
397   while (element != NULL)
398   {
399     if (0 ==
400         memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
401       break;
402     element = element->next;
403   }
404
405   GNUNET_assert (NULL != element);
406
407   for (i = 0; i < ntohs (*num); i++)
408   {
409     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410                 "The peer %s newly supports the application-type %d\n",
411                 GNUNET_i2s (other), ntohs (ports[i]));
412     if (GNUNET_APPLICATION_TYPE_END == ntohs (ports[i]))
413       continue;
414     struct type_list_element *new_type = GNUNET_malloc (sizeof *new_type);
415
416     new_type->type = (GNUNET_MESH_ApplicationType) ntohs (ports[i]);
417     GNUNET_CONTAINER_DLL_insert (element->type_head, element->type_tail,
418                                  new_type);
419   }
420
421   struct type_list_element *type;
422
423   for (type = element->type_head; type != NULL; type = type->next)
424   {
425     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426                 "The peer %s supports the application-type %d\n",
427                 GNUNET_i2s (other), type->type);
428   }
429
430   struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
431
432   while (tunnel != NULL)
433   {
434     struct tunnel_list_element *next = tunnel->next;
435
436     for (i = 0; i < ntohs (*num); i++)
437     {
438       if (ntohs (ports[i]) == tunnel->tunnel.application_type)
439       {
440         GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
441                                      handle->pending_by_type_tunnels.tail,
442                                      tunnel);
443         GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
444                                            handle->established_tunnels.tail,
445                                            handle->established_tunnels.tail,
446                                            tunnel);
447         memcpy (&tunnel->tunnel.peer, other,
448                 sizeof (struct GNUNET_PeerIdentity));
449         tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
450                                         &tunnel->tunnel.peer, atsi);
451         GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
452         break;
453       }
454     }
455     if (ntohs (ports[i]) == tunnel->tunnel.application_type)
456       tunnel = next;
457     else
458       tunnel = tunnel->next;
459   }
460   return GNUNET_OK;
461 }
462
463 /**
464  * Receive a message from core.
465  */
466 static int
467 core_receive (void *cls, const struct GNUNET_PeerIdentity *other,
468               const struct GNUNET_MessageHeader *message,
469               const struct GNUNET_TRANSPORT_ATS_Information *atsi)
470 {
471   struct GNUNET_MESH_Handle *handle = cls;
472   struct tunnel_message *tmessage = (struct tunnel_message *) message;
473   struct GNUNET_MessageHeader *rmessage =
474       (struct GNUNET_MessageHeader *) (tmessage + 1);
475
476   struct GNUNET_MESH_MessageHandler *handler;
477
478   for (handler = handle->handlers; handler->callback != NULL; handler++)
479   {
480     if ((ntohs (rmessage->type) == handler->type) &&
481         ((handler->expected_size == 0) ||
482          (handler->expected_size == ntohs (rmessage->size))))
483     {
484       break;
485     }
486   }
487
488   /* handler->callback handles this message */
489
490   /* If no handler was found, drop the message but keep the channel open */
491   if (handler->callback == NULL)
492   {
493     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494                 "Received message of type %d from peer %s; dropping it.\n",
495                 ntohs (rmessage->type), GNUNET_i2s (other));
496     return GNUNET_OK;
497   }
498
499   struct tunnel_list_element *tunnel = handle->established_tunnels.head;
500
501   while (tunnel != NULL)
502   {
503     if (tunnel->tunnel.id.id == tmessage->id.id &&
504         (0 ==
505          memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
506                  sizeof (struct GNUNET_PeerIdentity))) &&
507         (0 ==
508          memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
509                  sizeof (struct GNUNET_PeerIdentity))))
510       break;
511     tunnel = tunnel->next;
512   }
513
514   /* if no tunnel was found: create a new inbound tunnel */
515   if (tunnel == NULL)
516   {
517     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518                 "New inbound tunnel from peer %s; first message has type %d.\n",
519                 GNUNET_i2s (other), ntohs (rmessage->type));
520     tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
521     tunnel->tunnel.connect_handler = NULL;
522     tunnel->tunnel.disconnect_handler = NULL;
523     tunnel->tunnel.handler_cls = NULL;
524     tunnel->tunnel.ctx = NULL;
525     tunnel->tunnel.handle = handle;
526     memcpy (&tunnel->tunnel.peer, other, sizeof (struct GNUNET_PeerIdentity));
527     memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
528
529     GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
530                                        handle->established_tunnels.tail,
531                                        handle->established_tunnels.tail,
532                                        tunnel);
533   }
534   else
535     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536                 "Inbound message from peer %s; type %d.\n", GNUNET_i2s (other),
537                 ntohs (rmessage->type));
538
539   return handler->callback (handle->cls, &tunnel->tunnel, &tunnel->tunnel.ctx,
540                             other, rmessage, atsi);
541 }
542
543 struct GNUNET_MESH_Tunnel *
544 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
545                                           struct GNUNET_TIME_Relative timeout,
546                                           GNUNET_MESH_ApplicationType
547                                           application_type,
548                                           GNUNET_MESH_TunnelConnectHandler
549                                           connect_handler,
550                                           GNUNET_MESH_TunnelDisconnectHandler
551                                           disconnect_handler, void *handler_cls)
552 {
553   /* Look in the list of connected peers */
554   struct peer_list_element *element = handle->connected_peers.head;
555
556   while (element != NULL)
557   {
558     struct type_list_element *i;
559
560     for (i = element->type_head; i != NULL; i = i->next)
561       if (application_type == i->type)
562         return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
563                                                      &element->peer,
564                                                      connect_handler,
565                                                      disconnect_handler,
566                                                      handler_cls);
567     element = element->next;
568   }
569
570   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n",
571               application_type);
572
573   /* Put into pending list */
574   struct tunnel_list_element *tunnel =
575       GNUNET_malloc (sizeof (struct tunnel_list_element));
576
577   tunnel->tunnel.connect_handler = connect_handler;
578   tunnel->tunnel.disconnect_handler = disconnect_handler;
579   tunnel->tunnel.handler_cls = handler_cls;
580   tunnel->tunnel.ctx = NULL;
581   tunnel->tunnel.handle = handle;
582   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
583           sizeof (struct GNUNET_PeerIdentity));
584   tunnel->tunnel.id.id = current_id++;
585   tunnel->tunnel.application_type = application_type;
586
587   GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
588                                      handle->pending_by_type_tunnels.tail,
589                                      handle->pending_by_type_tunnels.tail,
590                                      tunnel);
591   return &tunnel->tunnel;
592 }
593
594
595
596 struct GNUNET_MESH_Tunnel *
597 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
598                                       struct GNUNET_TIME_Relative timeout,
599                                       unsigned int num_peers,
600                                       const struct GNUNET_PeerIdentity *peers,
601                                       GNUNET_MESH_TunnelConnectHandler
602                                       connect_handler,
603                                       GNUNET_MESH_TunnelDisconnectHandler
604                                       disconnect_handler, void *handler_cls)
605 {
606   if (num_peers != 1)
607     return NULL;
608
609   struct tunnel_list_element *tunnel =
610       GNUNET_malloc (sizeof (struct tunnel_list_element));
611
612   tunnel->tunnel.connect_handler = connect_handler;
613   tunnel->tunnel.disconnect_handler = disconnect_handler;
614   tunnel->tunnel.handler_cls = handler_cls;
615   tunnel->tunnel.ctx = NULL;
616   tunnel->tunnel.handle = handle;
617   memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
618           sizeof (struct GNUNET_PeerIdentity));
619   memcpy (&tunnel->tunnel.id.target, peers,
620           sizeof (struct GNUNET_PeerIdentity));
621   tunnel->tunnel.id.id = current_id++;
622   memcpy (&tunnel->tunnel.peer, peers, sizeof (struct GNUNET_PeerIdentity));
623
624   struct peer_list_element *element = handle->connected_peers.head;
625
626   while (element != NULL)
627   {
628     if (0 ==
629         memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
630       break;
631     element = element->next;
632   }
633
634   if (element != NULL)
635   {
636     /* we are connected to this peer */
637     GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
638                                        handle->established_tunnels.tail,
639                                        handle->established_tunnels.tail,
640                                        tunnel);
641     GNUNET_SCHEDULER_add_now (call_connect_handler, tunnel);
642   }
643   else if (0 ==
644            memcmp (peers, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
645   {
646     /* we are the peer */
647     GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
648                                        handle->established_tunnels.tail,
649                                        handle->established_tunnels.tail,
650                                        tunnel);
651     GNUNET_SCHEDULER_add_now (send_self_connect, tunnel);
652   }
653   else
654   {
655     /* we are not connected to this peer */
656     GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
657                                        handle->pending_tunnels.tail,
658                                        handle->pending_tunnels.tail, tunnel);
659     (void) GNUNET_CORE_peer_request_connect (handle->core, peers, NULL, NULL);
660   }
661
662   return &tunnel->tunnel;
663 }
664
665 const struct GNUNET_PeerIdentity *
666 GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel)
667 {
668   return &tunnel->peer;
669 }
670
671 static size_t
672 core_notify (void *cls, size_t size, void *buf)
673 {
674   struct notify_cls *ncls = cls;
675   struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
676
677   tunnel->notify_handle = NULL;
678   struct tunnel_message *message = buf;
679   void *cbuf = (void *) &message[1];
680
681   GNUNET_assert (NULL != ncls->notify);
682
683   size_t sent =
684       ncls->notify (ncls->notify_cls, size - sizeof (struct tunnel_message),
685                     cbuf);
686
687   GNUNET_free (ncls);
688
689   if (0 == sent)
690     return 0;
691
692   sent += sizeof (struct tunnel_message);
693
694   message->hdr.type = htons (GNUNET_MESSAGE_TYPE_MESH);
695   message->hdr.size = htons (sent);
696   memcpy (&message->id, &tunnel->id, sizeof (struct tunnel_id));
697   return sent;
698 }
699
700
701 /**
702  * Ask the mesh to call "notify" once it is ready to transmit the
703  * given number of bytes to the specified "target".  If we are not yet
704  * connected to the specified peer, a call to this function will cause
705  * us to try to establish a connection.
706  *
707  * @param tunnel tunnel to use for transmission
708  * @param cork is corking allowed for this transmission?
709  * @param priority how important is the message?
710  * @param maxdelay how long can the message wait?
711  * @param target destination for the message, NULL for multicast to all tunnel targets
712  * @param notify_size how many bytes of buffer space does notify want?
713  * @param notify function to call when buffer space is available;
714  *        will be called with NULL on timeout or if the overall queue
715  *        for this peer is larger than queue_size and this is currently
716  *        the message with the lowest priority
717  * @param notify_cls closure for notify
718  * @return non-NULL if the notify callback was queued,
719  *         NULL if we can not even queue the request (insufficient
720  *         memory); if NULL is returned, "notify" will NOT be called.
721  */
722 struct GNUNET_MESH_TransmitHandle *
723 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
724                                    uint32_t priority,
725                                    struct GNUNET_TIME_Relative maxdelay,
726                                    const struct GNUNET_PeerIdentity *target
727                                    __attribute__ ((unused)), size_t notify_size,
728                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
729                                    void *notify_cls)
730 {
731   if (NULL != tunnel->notify_handle)
732   {
733     GNUNET_break (0);
734     return NULL;
735   }
736
737   struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls));
738
739   cls->notify_cls = notify_cls;
740   GNUNET_assert (NULL != notify);
741   cls->notify = notify;
742   cls->tunnel = tunnel;
743
744   tunnel->notify_handle =
745       (struct GNUNET_MESH_TransmitHandle *)
746       GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
747                                          maxdelay, &tunnel->peer,
748                                          notify_size +
749                                          sizeof (struct tunnel_message),
750                                          &core_notify, (void *) cls);
751
752   return tunnel->notify_handle;
753 }
754
755 void
756 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
757 {
758   GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
759                                              *) th);
760 }
761
762 void
763 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
764 {
765   tunnel->app_head = head;
766 }
767
768 void
769 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
770 {
771   tunnel->app_tail = tail;
772 }
773
774 void *
775 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
776 {
777   return tunnel->app_head;
778 }
779
780 void *
781 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
782 {
783   return tunnel->app_head;
784 }
785
786 void
787 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
788 {
789   tunnel->app_data = data;
790 }
791
792 void *
793 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
794 {
795   return tunnel->app_data;
796 }
797
798
799 void
800 build_hello_message (struct GNUNET_MESH_Handle *handle,
801                      const GNUNET_MESH_ApplicationType *stypes)
802 {
803   int num = 0;
804   const GNUNET_MESH_ApplicationType *t;
805
806   for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++) ;
807
808   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
809
810   handle->hello_message_size = sizeof (uint16_t) +      /* For the number of types */
811       num * sizeof (GNUNET_MESH_ApplicationType);       /* For the types */
812
813   uint16_t *nums = GNUNET_malloc (handle->hello_message_size);
814   GNUNET_MESH_ApplicationType *types =
815       (GNUNET_MESH_ApplicationType *) (nums + 1);
816
817   *nums = htons (num);
818
819   int i;
820
821   for (i = 0; i < num; i++)
822   {
823     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n",
824                 stypes[i]);
825     types[i] = htons (stypes[i]);
826   }
827
828   handle->hello_message = nums;
829 }
830
831
832 struct GNUNET_MESH_Handle *
833 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
834                      GNUNET_MESH_TunnelEndHandler cleaner,
835                      const struct GNUNET_MESH_MessageHandler *handlers,
836                      const GNUNET_MESH_ApplicationType *stypes)
837 {
838   struct GNUNET_MESH_Handle *ret =
839       GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
840
841   ret->connected_to_core = GNUNET_NO;
842   ret->connected_peers.head = NULL;
843   ret->connected_peers.tail = NULL;
844   ret->cleaner = cleaner;
845   ret->cls = cls;
846
847   const struct GNUNET_MESH_MessageHandler *it;
848   unsigned int len = 1;
849
850   for (it = handlers; it->callback != NULL; it++)
851   {
852     len++;
853   }
854
855   ret->handlers =
856       GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
857   memset (ret->handlers, 0, len * sizeof (struct GNUNET_MESH_MessageHandler));
858   memcpy (ret->handlers, handlers,
859           len * sizeof (struct GNUNET_MESH_MessageHandler));
860
861   build_hello_message (ret, stypes);
862
863   static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
864     {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
865     {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
866     {NULL, 0, 0}
867   };
868
869   ret->core =
870       GNUNET_CORE_connect (cfg, 42, ret, &core_startup, &core_connect,
871                            &core_disconnect, NULL, NULL, GNUNET_NO, NULL,
872                            GNUNET_NO, core_handlers);
873   return ret;
874 }
875
876 void
877 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
878 {
879   GNUNET_free (handle->handlers);
880   GNUNET_free (handle->hello_message);
881   GNUNET_CORE_disconnect (handle->core);
882
883   struct peer_list_element *element = handle->connected_peers.head;
884
885   while (element != NULL)
886   {
887     struct peer_list_element *next = element->next;
888
889     while (element->type_head != NULL)
890     {
891       struct type_list_element *tail = element->type_tail;
892
893       GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
894                                    tail);
895       GNUNET_free (tail);
896     }
897     GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
898     GNUNET_SCHEDULER_cancel (element->sched);
899     GNUNET_free (element);
900     element = next;
901   }
902
903   struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
904
905   while (tunnel != NULL)
906   {
907     struct tunnel_list_element *next = tunnel->next;
908
909     GNUNET_free (tunnel);
910     tunnel = next;
911   }
912   tunnel = handle->established_tunnels.head;;
913   while (tunnel != NULL)
914   {
915     struct tunnel_list_element *next = tunnel->next;
916
917     GNUNET_free (tunnel);
918     tunnel = next;
919   }
920
921   GNUNET_free (handle);
922 }
923
924 /* end of mesh_api.c */