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