2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
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>
35 #define LOG(kind,...) GNUNET_log_from (kind, "mesh-api",__VA_ARGS__)
39 uint32_t id GNUNET_PACKED;
40 struct GNUNET_PeerIdentity initiator;
41 struct GNUNET_PeerIdentity target;
44 static uint32_t current_id = 0;
48 struct GNUNET_MessageHeader hdr;
50 /* followed by another GNUNET_MessageHeader */
56 GNUNET_CONNECTION_TransmitReadyNotify notify;
57 struct GNUNET_MESH_Tunnel *tunnel;
60 struct GNUNET_MESH_Tunnel
62 /* The other peer this tunnel leads to; just unicast for the moment! */
63 struct GNUNET_PeerIdentity peer;
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;
72 struct GNUNET_MESH_Handle *handle;
74 /* The application-type requested for this tunnel. Is only needed for pending
77 uint16_t application_type;
79 struct GNUNET_MESH_TransmitHandle *notify_handle;
81 /* The context of the receive-function. */
84 /* A list, usable by application-code (for queues) */
88 /* A pointer, usable by application-code */
92 struct tunnel_list_element
94 struct GNUNET_MESH_Tunnel tunnel;
95 struct tunnel_list_element *next, *prev;
100 struct tunnel_list_element *head, *tail;
103 struct type_list_element
105 GNUNET_MESH_ApplicationType type;
106 struct type_list_element *next, *prev;
109 struct peer_list_element
111 struct GNUNET_PeerIdentity peer;
113 /* list of application-types */
114 struct type_list_element *type_head, *type_tail;
116 struct peer_list_element *next, *prev;
118 /* The handle that sends the hellos to this peer */
119 struct GNUNET_CORE_TransmitHandle *hello;
121 GNUNET_SCHEDULER_TaskIdentifier sched;
123 struct GNUNET_MESH_Handle *handle;
128 struct peer_list_element *head, *tail;
131 struct GNUNET_MESH_Handle
133 struct GNUNET_CORE_Handle *core;
134 struct GNUNET_TRANSPORT_Handle *transport;
135 struct GNUNET_MESH_MessageHandler *handlers;
136 struct GNUNET_PeerIdentity myself;
137 unsigned int connected_to_core;
138 struct peer_list connected_peers;
139 struct tunnel_list established_tunnels;
140 struct tunnel_list pending_tunnels;
141 struct tunnel_list pending_by_type_tunnels;
143 GNUNET_MESH_TunnelEndHandler *cleaner;
144 size_t hello_message_size;
145 uint16_t *hello_message;
149 send_end_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
154 struct GNUNET_MESH_Tunnel *tunnel = cls;
156 tunnel->connect_handler (tunnel->handler_cls, NULL, NULL, 0);
160 send_self_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
165 struct GNUNET_MESH_Tunnel *tunnel = cls;
167 tunnel->connect_handler (tunnel->handler_cls, &tunnel->handle->myself, NULL, 0);
168 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
172 call_connect_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
174 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
177 struct GNUNET_MESH_Tunnel *tunnel = cls;
179 tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, NULL, 0);
180 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
184 core_startup (void *cls, struct GNUNET_CORE_Handle *core
185 __attribute__ ((unused)),
186 const struct GNUNET_PeerIdentity *my_identity)
188 struct GNUNET_MESH_Handle *handle = cls;
190 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
191 handle->connected_to_core = GNUNET_YES;
195 send_hello_message (void *cls, size_t size, void *buf)
200 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
202 struct peer_list_element *element = cls;
203 struct GNUNET_MESH_Handle *handle = element->handle;
205 element->hello = NULL;
206 struct GNUNET_MessageHeader *hdr = buf;
209 sizeof (struct GNUNET_MessageHeader) + handle->hello_message_size;
214 hdr->type = htons (GNUNET_MESSAGE_TYPE_MESH_HELLO);
215 hdr->size = htons (size);
217 memcpy (hdr + 1, handle->hello_message, handle->hello_message_size);
222 schedule_hello_message (void *cls,
223 const struct GNUNET_SCHEDULER_TaskContext *tctx)
225 struct peer_list_element *element = cls;
227 element->sched = GNUNET_SCHEDULER_NO_TASK;
229 if ((tctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
232 if (element->hello == NULL)
234 GNUNET_CORE_notify_transmit_ready (element->handle->core, GNUNET_NO, 42,
235 GNUNET_TIME_UNIT_SECONDS,
237 sizeof (struct GNUNET_MessageHeader)
239 element->handle->hello_message_size,
240 &send_hello_message, element);
243 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
244 schedule_hello_message, cls);
249 * Core calls this if we are connected to a new peer.
251 * The peer is added to the connected_peers-list.
255 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
256 const struct GNUNET_ATS_Information *atsi,
257 unsigned int atsi_count)
259 struct GNUNET_MESH_Handle *handle = cls;
261 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core tells us we are connected to peer %s\n",
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;
270 /* Send a hello to this peer */
271 element->sched = GNUNET_SCHEDULER_add_now (schedule_hello_message, element);
272 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
273 handle->connected_peers.tail,
274 handle->connected_peers.tail, element);
276 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
278 while (tunnel != NULL)
281 memcmp (&tunnel->tunnel.peer, peer,
282 sizeof (struct GNUNET_PeerIdentity)))
284 struct tunnel_list_element *next = tunnel->next;
286 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
287 handle->pending_tunnels.tail, tunnel);
288 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
289 handle->established_tunnels.tail,
290 handle->established_tunnels.tail,
292 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, peer, atsi, atsi_count);
293 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
297 tunnel = tunnel->next;
302 * Core calls this if we disconnect a peer
304 * Remove this peer from the list of connected peers
305 * Close all tunnels this peer belongs to
308 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
310 struct GNUNET_MESH_Handle *handle = cls;
312 LOG (GNUNET_ERROR_TYPE_DEBUG,
313 "Core tells us we are no longer connected to peer %s\n",
316 struct peer_list_element *element = handle->connected_peers.head;
318 while (element != NULL)
320 if (0 == memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
322 element = element->next;
326 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
327 handle->connected_peers.tail, element);
328 while (element->type_head != NULL)
330 struct type_list_element *tail = element->type_tail;
332 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
336 if (element->hello != NULL)
337 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
338 GNUNET_SCHEDULER_cancel (element->sched);
339 GNUNET_free (element);
342 struct tunnel_list_element *telement = handle->established_tunnels.head;
344 while (telement != NULL)
347 memcmp (&telement->tunnel.peer, peer,
348 sizeof (struct GNUNET_PeerIdentity)))
350 /* disconnect tunnels */
351 /* outbound tunnels */
352 if (telement->tunnel.connect_handler != NULL &&
353 NULL != telement->tunnel.disconnect_handler)
354 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
356 /* inbound tunnels */
357 else if (NULL != handle->cleaner)
358 handle->cleaner (handle->cls, &telement->tunnel, &telement->tunnel.ctx);
360 struct tunnel_list_element *next = telement->next;
362 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
363 handle->established_tunnels.tail, telement);
364 GNUNET_free (telement);
369 telement = telement->next;
375 * Receive a message from core.
376 * This is a hello-message, containing the application-types the other peer can receive
379 receive_hello (void *cls, const struct GNUNET_PeerIdentity *other,
380 const struct GNUNET_MessageHeader *message,
381 const struct GNUNET_ATS_Information *atsi,
382 unsigned int atsi_count)
384 struct GNUNET_MESH_Handle *handle = cls;
385 uint16_t *num = (uint16_t *) (message + 1);
386 GNUNET_MESH_ApplicationType *ports =
387 (GNUNET_MESH_ApplicationType *) (num + 1);
390 LOG (GNUNET_ERROR_TYPE_DEBUG,
391 "The peer %s tells us he supports %d application-types.\n",
392 GNUNET_i2s (other), ntohs (*num));
394 struct peer_list_element *element = handle->connected_peers.head;
396 while (element != NULL)
399 memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
401 element = element->next;
404 GNUNET_assert (NULL != element);
406 for (i = 0; i < ntohs (*num); i++)
408 LOG (GNUNET_ERROR_TYPE_DEBUG,
409 "The peer %s newly supports the application-type %d\n",
410 GNUNET_i2s (other), ntohs (ports[i]));
411 if (GNUNET_APPLICATION_TYPE_END == ntohs (ports[i]))
413 struct type_list_element *new_type = GNUNET_malloc (sizeof *new_type);
415 new_type->type = (GNUNET_MESH_ApplicationType) ntohs (ports[i]);
416 GNUNET_CONTAINER_DLL_insert (element->type_head, element->type_tail,
420 struct type_list_element *type;
422 for (type = element->type_head; type != NULL; type = type->next)
424 LOG (GNUNET_ERROR_TYPE_DEBUG,
425 "The peer %s supports the application-type %d\n", GNUNET_i2s (other),
429 struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
431 while (tunnel != NULL)
433 struct tunnel_list_element *next = tunnel->next;
435 for (i = 0; i < ntohs (*num); i++)
437 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
439 GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
440 handle->pending_by_type_tunnels.tail,
442 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
443 handle->established_tunnels.tail,
444 handle->established_tunnels.tail,
446 memcpy (&tunnel->tunnel.peer, other,
447 sizeof (struct GNUNET_PeerIdentity));
448 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
449 &tunnel->tunnel.peer, atsi, atsi_count);
450 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
454 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
457 tunnel = tunnel->next;
463 * Receive a message from core.
466 core_receive (void *cls, const struct GNUNET_PeerIdentity *other,
467 const struct GNUNET_MessageHeader *message,
468 const struct GNUNET_ATS_Information *atsi,
469 unsigned int atsi_count)
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);
476 struct GNUNET_MESH_MessageHandler *handler;
478 for (handler = handle->handlers; handler->callback != NULL; handler++)
480 if ((ntohs (rmessage->type) == handler->type) &&
481 ((handler->expected_size == 0) ||
482 (handler->expected_size == ntohs (rmessage->size))))
488 /* handler->callback handles this message */
490 /* If no handler was found, drop the message but keep the channel open */
491 if (handler->callback == NULL)
493 LOG (GNUNET_ERROR_TYPE_DEBUG,
494 "Received message of type %d from peer %s; dropping it.\n",
495 ntohs (rmessage->type), GNUNET_i2s (other));
499 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
501 while (tunnel != NULL)
503 if (tunnel->tunnel.id.id == tmessage->id.id &&
505 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
506 sizeof (struct GNUNET_PeerIdentity))) &&
508 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
509 sizeof (struct GNUNET_PeerIdentity))))
511 tunnel = tunnel->next;
514 /* if no tunnel was found: create a new inbound tunnel */
517 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));
529 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
530 handle->established_tunnels.tail,
531 handle->established_tunnels.tail,
535 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inbound message from peer %s; type %d.\n",
536 GNUNET_i2s (other), ntohs (rmessage->type));
538 return handler->callback (handle->cls, &tunnel->tunnel, &tunnel->tunnel.ctx,
539 other, rmessage, atsi, atsi_count);
542 struct GNUNET_MESH_Tunnel *
543 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
544 struct GNUNET_TIME_Relative timeout,
545 GNUNET_MESH_ApplicationType
547 GNUNET_MESH_TunnelConnectHandler
549 GNUNET_MESH_TunnelDisconnectHandler
550 disconnect_handler, void *handler_cls)
552 /* Look in the list of connected peers */
553 struct peer_list_element *element = handle->connected_peers.head;
555 while (element != NULL)
557 struct type_list_element *i;
559 for (i = element->type_head; i != NULL; i = i->next)
560 if (application_type == i->type)
561 return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
566 element = element->next;
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n",
572 /* Put into pending list */
573 struct tunnel_list_element *tunnel =
574 GNUNET_malloc (sizeof (struct tunnel_list_element));
576 tunnel->tunnel.connect_handler = connect_handler;
577 tunnel->tunnel.disconnect_handler = disconnect_handler;
578 tunnel->tunnel.handler_cls = handler_cls;
579 tunnel->tunnel.ctx = NULL;
580 tunnel->tunnel.handle = handle;
581 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
582 sizeof (struct GNUNET_PeerIdentity));
583 tunnel->tunnel.id.id = current_id++;
584 tunnel->tunnel.application_type = application_type;
586 GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
587 handle->pending_by_type_tunnels.tail,
588 handle->pending_by_type_tunnels.tail,
590 return &tunnel->tunnel;
595 struct GNUNET_MESH_Tunnel *
596 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
597 struct GNUNET_TIME_Relative timeout,
598 unsigned int num_peers,
599 const struct GNUNET_PeerIdentity *peers,
600 GNUNET_MESH_TunnelConnectHandler
602 GNUNET_MESH_TunnelDisconnectHandler
603 disconnect_handler, void *handler_cls)
608 struct tunnel_list_element *tunnel =
609 GNUNET_malloc (sizeof (struct tunnel_list_element));
611 tunnel->tunnel.connect_handler = connect_handler;
612 tunnel->tunnel.disconnect_handler = disconnect_handler;
613 tunnel->tunnel.handler_cls = handler_cls;
614 tunnel->tunnel.ctx = NULL;
615 tunnel->tunnel.handle = handle;
616 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
617 sizeof (struct GNUNET_PeerIdentity));
618 memcpy (&tunnel->tunnel.id.target, peers,
619 sizeof (struct GNUNET_PeerIdentity));
620 tunnel->tunnel.id.id = current_id++;
621 memcpy (&tunnel->tunnel.peer, peers, sizeof (struct GNUNET_PeerIdentity));
623 struct peer_list_element *element = handle->connected_peers.head;
625 while (element != NULL)
628 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
630 element = element->next;
635 /* we are connected to this peer */
636 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
637 handle->established_tunnels.tail,
638 handle->established_tunnels.tail,
640 GNUNET_SCHEDULER_add_now (call_connect_handler, tunnel);
643 memcmp (peers, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
645 /* we are the peer */
646 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
647 handle->established_tunnels.tail,
648 handle->established_tunnels.tail,
650 GNUNET_SCHEDULER_add_now (send_self_connect, tunnel);
654 /* we are not connected to this peer */
655 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
656 handle->pending_tunnels.tail,
657 handle->pending_tunnels.tail, tunnel);
658 GNUNET_TRANSPORT_try_connect (handle->transport, peers);
661 return &tunnel->tunnel;
664 const struct GNUNET_PeerIdentity *
665 GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel)
667 return &tunnel->peer;
671 core_notify (void *cls, size_t size, void *buf)
673 struct notify_cls *ncls = cls;
674 struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
675 tunnel->notify_handle = NULL;
678 return ncls->notify (ncls->notify_cls, 0, NULL);
680 struct tunnel_message *message = buf;
681 void *cbuf = (void *) &message[1];
683 GNUNET_assert (NULL != ncls->notify);
686 ncls->notify (ncls->notify_cls, size - sizeof (struct tunnel_message),
694 sent += sizeof (struct tunnel_message);
696 message->hdr.type = htons (GNUNET_MESSAGE_TYPE_MESH);
697 message->hdr.size = htons (sent);
698 memcpy (&message->id, &tunnel->id, sizeof (struct tunnel_id));
704 * Ask the mesh to call "notify" once it is ready to transmit the
705 * given number of bytes to the specified "target". If we are not yet
706 * connected to the specified peer, a call to this function will cause
707 * us to try to establish a connection.
709 * @param tunnel tunnel to use for transmission
710 * @param cork is corking allowed for this transmission?
711 * @param priority how important is the message?
712 * @param maxdelay how long can the message wait?
713 * @param target destination for the message, NULL for multicast to all tunnel targets
714 * @param notify_size how many bytes of buffer space does notify want?
715 * @param notify function to call when buffer space is available;
716 * will be called with NULL on timeout or if the overall queue
717 * for this peer is larger than queue_size and this is currently
718 * the message with the lowest priority
719 * @param notify_cls closure for notify
720 * @return non-NULL if the notify callback was queued,
721 * NULL if we can not even queue the request (insufficient
722 * memory); if NULL is returned, "notify" will NOT be called.
724 struct GNUNET_MESH_TransmitHandle *
725 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
727 struct GNUNET_TIME_Relative maxdelay,
728 const struct GNUNET_PeerIdentity *target
729 __attribute__ ((unused)), size_t notify_size,
730 GNUNET_CONNECTION_TransmitReadyNotify notify,
733 if (NULL != tunnel->notify_handle)
739 struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls));
741 cls->notify_cls = notify_cls;
742 GNUNET_assert (NULL != notify);
743 cls->notify = notify;
744 cls->tunnel = tunnel;
746 tunnel->notify_handle =
747 (struct GNUNET_MESH_TransmitHandle *)
748 GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
749 maxdelay, &tunnel->peer,
751 sizeof (struct tunnel_message),
752 &core_notify, (void *) cls);
754 return tunnel->notify_handle;
758 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
760 GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
765 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
767 tunnel->app_head = head;
771 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
773 tunnel->app_tail = tail;
777 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
779 return tunnel->app_head;
783 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
785 return tunnel->app_head;
789 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
791 tunnel->app_data = data;
795 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
797 return tunnel->app_data;
802 build_hello_message (struct GNUNET_MESH_Handle *handle,
803 const GNUNET_MESH_ApplicationType *stypes)
806 const GNUNET_MESH_ApplicationType *t;
808 for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++) ;
810 LOG (GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
812 handle->hello_message_size = sizeof (uint16_t) + /* For the number of types */
813 num * sizeof (GNUNET_MESH_ApplicationType); /* For the types */
815 uint16_t *nums = GNUNET_malloc (handle->hello_message_size);
816 GNUNET_MESH_ApplicationType *types =
817 (GNUNET_MESH_ApplicationType *) (nums + 1);
823 for (i = 0; i < num; i++)
825 LOG (GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n", stypes[i]);
826 types[i] = htons (stypes[i]);
829 handle->hello_message = nums;
833 struct GNUNET_MESH_Handle *
834 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
835 GNUNET_MESH_TunnelEndHandler cleaner,
836 const struct GNUNET_MESH_MessageHandler *handlers,
837 const GNUNET_MESH_ApplicationType *stypes)
839 struct GNUNET_MESH_Handle *ret =
840 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
842 ret->connected_to_core = GNUNET_NO;
843 ret->connected_peers.head = NULL;
844 ret->connected_peers.tail = NULL;
845 ret->cleaner = cleaner;
848 const struct GNUNET_MESH_MessageHandler *it;
849 unsigned int len = 1;
851 for (it = handlers; it->callback != NULL; it++)
857 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
858 memset (ret->handlers, 0, len * sizeof (struct GNUNET_MESH_MessageHandler));
859 memcpy (ret->handlers, handlers,
860 len * sizeof (struct GNUNET_MESH_MessageHandler));
862 build_hello_message (ret, stypes);
864 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
865 {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
866 {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
871 GNUNET_CORE_connect (cfg, 42, ret, &core_startup, &core_connect,
872 &core_disconnect, NULL, GNUNET_NO, NULL, GNUNET_NO,
874 ret->transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
879 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
881 GNUNET_free (handle->handlers);
882 GNUNET_free (handle->hello_message);
883 GNUNET_CORE_disconnect (handle->core);
884 GNUNET_TRANSPORT_disconnect (handle->transport);
886 struct peer_list_element *element = handle->connected_peers.head;
888 while (element != NULL)
890 struct peer_list_element *next = element->next;
892 while (element->type_head != NULL)
894 struct type_list_element *tail = element->type_tail;
896 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
900 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
901 GNUNET_SCHEDULER_cancel (element->sched);
902 GNUNET_free (element);
906 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
908 while (tunnel != NULL)
910 struct tunnel_list_element *next = tunnel->next;
912 GNUNET_free (tunnel);
915 tunnel = handle->established_tunnels.head;;
916 while (tunnel != NULL)
918 struct tunnel_list_element *next = tunnel->next;
920 GNUNET_free (tunnel);
924 GNUNET_free (handle);
927 /* end of mesh_api.c */