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 GNUNET_TRANSPORT_ATS_Information atsi;
117 struct peer_list_element *next, *prev;
119 /* The handle that sends the hellos to this peer */
120 struct GNUNET_CORE_TransmitHandle *hello;
122 GNUNET_SCHEDULER_TaskIdentifier sched;
124 struct GNUNET_MESH_Handle *handle;
129 struct peer_list_element *head, *tail;
132 struct GNUNET_MESH_Handle
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;
144 GNUNET_MESH_TunnelEndHandler *cleaner;
145 size_t hello_message_size;
146 uint16_t *hello_message;
150 send_end_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
152 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
155 struct GNUNET_MESH_Tunnel *tunnel = cls;
157 tunnel->connect_handler (tunnel->handler_cls, NULL, NULL);
161 send_self_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
166 struct GNUNET_MESH_Tunnel *tunnel = cls;
168 tunnel->connect_handler (tunnel->handler_cls, &tunnel->handle->myself, NULL);
169 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
173 call_connect_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
178 struct GNUNET_MESH_Tunnel *tunnel = cls;
180 tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, NULL);
181 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
185 core_startup (void *cls, struct GNUNET_CORE_Handle *core
186 __attribute__ ((unused)),
187 const struct GNUNET_PeerIdentity *my_identity)
189 struct GNUNET_MESH_Handle *handle = cls;
191 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
192 handle->connected_to_core = GNUNET_YES;
196 send_hello_message (void *cls, size_t size, void *buf)
201 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
203 struct peer_list_element *element = cls;
204 struct GNUNET_MESH_Handle *handle = element->handle;
206 element->hello = NULL;
207 struct GNUNET_MessageHeader *hdr = buf;
210 sizeof (struct GNUNET_MessageHeader) + handle->hello_message_size;
215 hdr->type = htons (GNUNET_MESSAGE_TYPE_MESH_HELLO);
216 hdr->size = htons (size);
218 memcpy (hdr + 1, handle->hello_message, handle->hello_message_size);
223 schedule_hello_message (void *cls,
224 const struct GNUNET_SCHEDULER_TaskContext *tctx)
226 struct peer_list_element *element = cls;
228 element->sched = GNUNET_SCHEDULER_NO_TASK;
230 if ((tctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
233 if (element->hello == NULL)
235 GNUNET_CORE_notify_transmit_ready (element->handle->core, GNUNET_NO, 42,
236 GNUNET_TIME_UNIT_SECONDS,
238 sizeof (struct GNUNET_MessageHeader)
240 element->handle->hello_message_size,
241 &send_hello_message, element);
244 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
245 schedule_hello_message, cls);
250 * Core calls this if we are connected to a new peer.
252 * The peer is added to the connected_peers-list.
256 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
257 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
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);
274 memcpy (&element->atsi, atsi,
275 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
277 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
278 handle->connected_peers.tail,
279 handle->connected_peers.tail, element);
281 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
283 while (tunnel != NULL)
286 memcmp (&tunnel->tunnel.peer, peer,
287 sizeof (struct GNUNET_PeerIdentity)))
289 struct tunnel_list_element *next = tunnel->next;
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,
297 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, peer, atsi);
298 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
302 tunnel = tunnel->next;
307 * Core calls this if we disconnect a peer
309 * Remove this peer from the list of connected peers
310 * Close all tunnels this peer belongs to
313 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
315 struct GNUNET_MESH_Handle *handle = cls;
317 LOG (GNUNET_ERROR_TYPE_DEBUG,
318 "Core tells us we are no longer connected to peer %s\n",
321 struct peer_list_element *element = handle->connected_peers.head;
323 while (element != NULL)
325 if (0 == memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
327 element = element->next;
331 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
332 handle->connected_peers.tail, element);
333 while (element->type_head != NULL)
335 struct type_list_element *tail = element->type_tail;
337 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
341 if (element->hello != NULL)
342 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
343 GNUNET_SCHEDULER_cancel (element->sched);
344 GNUNET_free (element);
347 struct tunnel_list_element *telement = handle->established_tunnels.head;
349 while (telement != NULL)
352 memcmp (&telement->tunnel.peer, peer,
353 sizeof (struct GNUNET_PeerIdentity)))
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,
361 /* inbound tunnels */
362 else if (NULL != handle->cleaner)
363 handle->cleaner (handle->cls, &telement->tunnel, &telement->tunnel.ctx);
365 struct tunnel_list_element *next = telement->next;
367 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
368 handle->established_tunnels.tail, telement);
369 GNUNET_free (telement);
374 telement = telement->next;
380 * Receive a message from core.
381 * This is a hello-message, containing the application-types the other peer can receive
384 receive_hello (void *cls, const struct GNUNET_PeerIdentity *other,
385 const struct GNUNET_MessageHeader *message,
386 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
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);
394 LOG (GNUNET_ERROR_TYPE_DEBUG,
395 "The peer %s tells us he supports %d application-types.\n",
396 GNUNET_i2s (other), ntohs (*num));
398 struct peer_list_element *element = handle->connected_peers.head;
400 while (element != NULL)
403 memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
405 element = element->next;
408 GNUNET_assert (NULL != element);
410 for (i = 0; i < ntohs (*num); i++)
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]))
417 struct type_list_element *new_type = GNUNET_malloc (sizeof *new_type);
419 new_type->type = (GNUNET_MESH_ApplicationType) ntohs (ports[i]);
420 GNUNET_CONTAINER_DLL_insert (element->type_head, element->type_tail,
424 struct type_list_element *type;
426 for (type = element->type_head; type != NULL; type = type->next)
428 LOG (GNUNET_ERROR_TYPE_DEBUG,
429 "The peer %s supports the application-type %d\n", GNUNET_i2s (other),
433 struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
435 while (tunnel != NULL)
437 struct tunnel_list_element *next = tunnel->next;
439 for (i = 0; i < ntohs (*num); i++)
441 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
443 GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
444 handle->pending_by_type_tunnels.tail,
446 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
447 handle->established_tunnels.tail,
448 handle->established_tunnels.tail,
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);
458 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
461 tunnel = tunnel->next;
467 * Receive a message from core.
470 core_receive (void *cls, const struct GNUNET_PeerIdentity *other,
471 const struct GNUNET_MessageHeader *message,
472 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
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);
479 struct GNUNET_MESH_MessageHandler *handler;
481 for (handler = handle->handlers; handler->callback != NULL; handler++)
483 if ((ntohs (rmessage->type) == handler->type) &&
484 ((handler->expected_size == 0) ||
485 (handler->expected_size == ntohs (rmessage->size))))
491 /* handler->callback handles this message */
493 /* If no handler was found, drop the message but keep the channel open */
494 if (handler->callback == NULL)
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));
502 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
504 while (tunnel != NULL)
506 if (tunnel->tunnel.id.id == tmessage->id.id &&
508 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
509 sizeof (struct GNUNET_PeerIdentity))) &&
511 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
512 sizeof (struct GNUNET_PeerIdentity))))
514 tunnel = tunnel->next;
517 /* if no tunnel was found: create a new inbound tunnel */
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));
532 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
533 handle->established_tunnels.tail,
534 handle->established_tunnels.tail,
538 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inbound message from peer %s; type %d.\n",
539 GNUNET_i2s (other), ntohs (rmessage->type));
541 return handler->callback (handle->cls, &tunnel->tunnel, &tunnel->tunnel.ctx,
542 other, rmessage, atsi);
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
550 GNUNET_MESH_TunnelConnectHandler
552 GNUNET_MESH_TunnelDisconnectHandler
553 disconnect_handler, void *handler_cls)
555 /* Look in the list of connected peers */
556 struct peer_list_element *element = handle->connected_peers.head;
558 while (element != NULL)
560 struct type_list_element *i;
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,
569 element = element->next;
572 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n",
575 /* Put into pending list */
576 struct tunnel_list_element *tunnel =
577 GNUNET_malloc (sizeof (struct tunnel_list_element));
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;
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,
593 return &tunnel->tunnel;
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
605 GNUNET_MESH_TunnelDisconnectHandler
606 disconnect_handler, void *handler_cls)
611 struct tunnel_list_element *tunnel =
612 GNUNET_malloc (sizeof (struct tunnel_list_element));
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));
626 struct peer_list_element *element = handle->connected_peers.head;
628 while (element != NULL)
631 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
633 element = element->next;
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,
643 GNUNET_SCHEDULER_add_now (call_connect_handler, tunnel);
646 memcmp (peers, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
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,
653 GNUNET_SCHEDULER_add_now (send_self_connect, tunnel);
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);
664 return &tunnel->tunnel;
667 const struct GNUNET_PeerIdentity *
668 GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel)
670 return &tunnel->peer;
674 core_notify (void *cls, size_t size, void *buf)
676 struct notify_cls *ncls = cls;
677 struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
680 return ncls->notify (ncls->notify_cls, 0, NULL);
682 tunnel->notify_handle = NULL;
683 struct tunnel_message *message = buf;
684 void *cbuf = (void *) &message[1];
686 GNUNET_assert (NULL != ncls->notify);
689 ncls->notify (ncls->notify_cls, size - sizeof (struct tunnel_message),
697 sent += sizeof (struct tunnel_message);
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));
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.
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.
727 struct GNUNET_MESH_TransmitHandle *
728 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
730 struct GNUNET_TIME_Relative maxdelay,
731 const struct GNUNET_PeerIdentity *target
732 __attribute__ ((unused)), size_t notify_size,
733 GNUNET_CONNECTION_TransmitReadyNotify notify,
736 if (NULL != tunnel->notify_handle)
742 struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls));
744 cls->notify_cls = notify_cls;
745 GNUNET_assert (NULL != notify);
746 cls->notify = notify;
747 cls->tunnel = tunnel;
749 tunnel->notify_handle =
750 (struct GNUNET_MESH_TransmitHandle *)
751 GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
752 maxdelay, &tunnel->peer,
754 sizeof (struct tunnel_message),
755 &core_notify, (void *) cls);
757 return tunnel->notify_handle;
761 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
763 GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
768 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
770 tunnel->app_head = head;
774 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
776 tunnel->app_tail = tail;
780 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
782 return tunnel->app_head;
786 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
788 return tunnel->app_head;
792 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
794 tunnel->app_data = data;
798 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
800 return tunnel->app_data;
805 build_hello_message (struct GNUNET_MESH_Handle *handle,
806 const GNUNET_MESH_ApplicationType *stypes)
809 const GNUNET_MESH_ApplicationType *t;
811 for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++) ;
813 LOG (GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
815 handle->hello_message_size = sizeof (uint16_t) + /* For the number of types */
816 num * sizeof (GNUNET_MESH_ApplicationType); /* For the types */
818 uint16_t *nums = GNUNET_malloc (handle->hello_message_size);
819 GNUNET_MESH_ApplicationType *types =
820 (GNUNET_MESH_ApplicationType *) (nums + 1);
826 for (i = 0; i < num; i++)
828 LOG (GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n", stypes[i]);
829 types[i] = htons (stypes[i]);
832 handle->hello_message = nums;
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)
842 struct GNUNET_MESH_Handle *ret =
843 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
845 ret->connected_to_core = GNUNET_NO;
846 ret->connected_peers.head = NULL;
847 ret->connected_peers.tail = NULL;
848 ret->cleaner = cleaner;
851 const struct GNUNET_MESH_MessageHandler *it;
852 unsigned int len = 1;
854 for (it = handlers; it->callback != NULL; it++)
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));
865 build_hello_message (ret, stypes);
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},
874 GNUNET_CORE_connect (cfg, 42, ret, &core_startup, &core_connect,
875 &core_disconnect, NULL, GNUNET_NO, NULL, GNUNET_NO,
877 ret->transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
882 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
884 GNUNET_free (handle->handlers);
885 GNUNET_free (handle->hello_message);
886 GNUNET_CORE_disconnect (handle->core);
887 GNUNET_TRANSPORT_disconnect (handle->transport);
889 struct peer_list_element *element = handle->connected_peers.head;
891 while (element != NULL)
893 struct peer_list_element *next = element->next;
895 while (element->type_head != NULL)
897 struct type_list_element *tail = element->type_tail;
899 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
903 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
904 GNUNET_SCHEDULER_cancel (element->sched);
905 GNUNET_free (element);
909 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
911 while (tunnel != NULL)
913 struct tunnel_list_element *next = tunnel->next;
915 GNUNET_free (tunnel);
918 tunnel = handle->established_tunnels.head;;
919 while (tunnel != NULL)
921 struct tunnel_list_element *next = tunnel->next;
923 GNUNET_free (tunnel);
927 GNUNET_free (handle);
930 /* end of mesh_api.c */