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