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_container_lib.h>
32 #include <gnunet_applications.h>
36 uint32_t id GNUNET_PACKED;
37 struct GNUNET_PeerIdentity initiator;
38 struct GNUNET_PeerIdentity target;
41 static uint32_t current_id = 0;
45 struct GNUNET_MessageHeader hdr;
47 /* followed by another GNUNET_MessageHeader */
53 GNUNET_CONNECTION_TransmitReadyNotify notify;
54 struct GNUNET_MESH_Tunnel *tunnel;
57 struct GNUNET_MESH_Tunnel
59 /* The other peer this tunnel leads to; just unicast for the moment! */
60 struct GNUNET_PeerIdentity peer;
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;
69 struct GNUNET_MESH_Handle *handle;
71 /* The application-type requested for this tunnel. Is only needed for pending
74 uint16_t application_type;
76 struct GNUNET_MESH_TransmitHandle *notify_handle;
78 /* The context of the receive-function. */
81 /* A list, usable by application-code (for queues) */
85 /* A pointer, usable by application-code */
89 struct tunnel_list_element
91 struct GNUNET_MESH_Tunnel tunnel;
92 struct tunnel_list_element *next, *prev;
97 struct tunnel_list_element *head, *tail;
100 struct type_list_element
102 GNUNET_MESH_ApplicationType type;
103 struct type_list_element *next, *prev;
106 struct peer_list_element
108 struct GNUNET_PeerIdentity peer;
110 /* list of application-types */
111 struct type_list_element *type_head, *type_tail;
113 struct GNUNET_TRANSPORT_ATS_Information atsi;
114 struct peer_list_element *next, *prev;
116 /* The handle that sends the hellos to this peer */
117 struct GNUNET_CORE_TransmitHandle *hello;
119 GNUNET_SCHEDULER_TaskIdentifier sched;
121 struct GNUNET_MESH_Handle *handle;
126 struct peer_list_element *head, *tail;
129 struct GNUNET_MESH_Handle
131 struct GNUNET_CORE_Handle *core;
132 struct GNUNET_MESH_MessageHandler *handlers;
133 struct GNUNET_PeerIdentity myself;
134 unsigned int connected_to_core;
135 struct peer_list connected_peers;
136 struct tunnel_list established_tunnels;
137 struct tunnel_list pending_tunnels;
138 struct tunnel_list pending_by_type_tunnels;
140 GNUNET_MESH_TunnelEndHandler *cleaner;
141 size_t hello_message_size;
142 uint16_t *hello_message;
146 send_end_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
151 struct GNUNET_MESH_Tunnel *tunnel = cls;
153 tunnel->connect_handler (tunnel->handler_cls, NULL, NULL);
157 send_self_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
162 struct GNUNET_MESH_Tunnel *tunnel = cls;
164 tunnel->connect_handler (tunnel->handler_cls, &tunnel->handle->myself, NULL);
165 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
169 call_connect_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
174 struct GNUNET_MESH_Tunnel *tunnel = cls;
176 tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, NULL);
177 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
181 core_startup (void *cls, struct GNUNET_CORE_Handle *core
182 __attribute__ ((unused)),
183 const struct GNUNET_PeerIdentity *my_identity,
184 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey
185 __attribute__ ((unused)))
187 struct GNUNET_MESH_Handle *handle = cls;
189 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
190 handle->connected_to_core = GNUNET_YES;
194 send_hello_message (void *cls, size_t size, void *buf)
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n");
201 struct peer_list_element *element = cls;
202 struct GNUNET_MESH_Handle *handle = element->handle;
204 element->hello = NULL;
205 struct GNUNET_MessageHeader *hdr = buf;
208 sizeof (struct GNUNET_MessageHeader) + handle->hello_message_size;
213 hdr->type = htons (GNUNET_MESSAGE_TYPE_MESH_HELLO);
214 hdr->size = htons (size);
216 memcpy (hdr + 1, handle->hello_message, handle->hello_message_size);
221 schedule_hello_message (void *cls,
222 const struct GNUNET_SCHEDULER_TaskContext *tctx)
224 struct peer_list_element *element = cls;
226 element->sched = GNUNET_SCHEDULER_NO_TASK;
228 if ((tctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
231 if (element->hello == NULL)
233 GNUNET_CORE_notify_transmit_ready (element->handle->core, GNUNET_NO, 42,
234 GNUNET_TIME_UNIT_SECONDS,
236 sizeof (struct GNUNET_MessageHeader)
238 element->handle->hello_message_size,
239 &send_hello_message, element);
242 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
243 schedule_hello_message, cls);
248 * Core calls this if we are connected to a new peer.
250 * The peer is added to the connected_peers-list.
254 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
255 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
257 struct GNUNET_MESH_Handle *handle = cls;
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Core tells us we are connected to peer %s\n", GNUNET_i2s (peer));
262 /* put the new peer into the list of connected peers */
263 struct peer_list_element *element =
264 GNUNET_malloc (sizeof (struct peer_list_element));
265 memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
266 element->handle = handle;
268 /* Send a hello to this peer */
269 element->sched = GNUNET_SCHEDULER_add_now (schedule_hello_message, element);
272 memcpy (&element->atsi, atsi,
273 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
275 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
276 handle->connected_peers.tail,
277 handle->connected_peers.tail, element);
279 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
281 while (tunnel != NULL)
284 memcmp (&tunnel->tunnel.peer, peer,
285 sizeof (struct GNUNET_PeerIdentity)))
287 struct tunnel_list_element *next = tunnel->next;
289 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
290 handle->pending_tunnels.tail, tunnel);
291 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
292 handle->established_tunnels.tail,
293 handle->established_tunnels.tail,
295 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, peer, atsi);
296 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
300 tunnel = tunnel->next;
305 * Core calls this if we disconnect a peer
307 * Remove this peer from the list of connected peers
308 * Close all tunnels this peer belongs to
311 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
313 struct GNUNET_MESH_Handle *handle = cls;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Core tells us we are no longer connected to peer %s\n",
319 struct peer_list_element *element = handle->connected_peers.head;
321 while (element != NULL)
323 if (0 == memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
325 element = element->next;
329 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
330 handle->connected_peers.tail, element);
331 while (element->type_head != NULL)
333 struct type_list_element *tail = element->type_tail;
335 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
339 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
340 GNUNET_SCHEDULER_cancel (element->sched);
341 GNUNET_free (element);
344 struct tunnel_list_element *telement = handle->established_tunnels.head;
346 while (telement != NULL)
349 memcmp (&telement->tunnel.peer, peer,
350 sizeof (struct GNUNET_PeerIdentity)))
352 /* disconnect tunnels */
353 /* outbound tunnels */
354 if (telement->tunnel.connect_handler != NULL &&
355 NULL != telement->tunnel.disconnect_handler)
356 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
358 /* inbound tunnels */
359 else if (NULL != handle->cleaner)
360 handle->cleaner (handle->cls, &telement->tunnel, &telement->tunnel.ctx);
362 struct tunnel_list_element *next = telement->next;
364 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
365 handle->established_tunnels.tail, telement);
366 GNUNET_free (telement);
371 telement = telement->next;
377 * Receive a message from core.
378 * This is a hello-message, containing the application-types the other peer can receive
381 receive_hello (void *cls, const struct GNUNET_PeerIdentity *other,
382 const struct GNUNET_MessageHeader *message,
383 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
385 struct GNUNET_MESH_Handle *handle = cls;
386 uint16_t *num = (uint16_t *) (message + 1);
387 GNUNET_MESH_ApplicationType *ports =
388 (GNUNET_MESH_ApplicationType *) (num + 1);
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "The peer %s tells us he supports %d application-types.\n",
393 GNUNET_i2s (other), ntohs (*num));
395 struct peer_list_element *element = handle->connected_peers.head;
397 while (element != NULL)
400 memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
402 element = element->next;
405 GNUNET_assert (NULL != element);
407 for (i = 0; i < ntohs (*num); i++)
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "The peer %s newly supports the application-type %d\n",
411 GNUNET_i2s (other), ntohs (ports[i]));
412 if (GNUNET_APPLICATION_TYPE_END == ntohs (ports[i]))
414 struct type_list_element *new_type = GNUNET_malloc (sizeof *new_type);
416 new_type->type = (GNUNET_MESH_ApplicationType) ntohs (ports[i]);
417 GNUNET_CONTAINER_DLL_insert (element->type_head, element->type_tail,
421 struct type_list_element *type;
423 for (type = element->type_head; type != NULL; type = type->next)
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "The peer %s supports the application-type %d\n",
427 GNUNET_i2s (other), type->type);
430 struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
432 while (tunnel != NULL)
434 struct tunnel_list_element *next = tunnel->next;
436 for (i = 0; i < ntohs (*num); i++)
438 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
440 GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head,
441 handle->pending_by_type_tunnels.tail,
443 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
444 handle->established_tunnels.tail,
445 handle->established_tunnels.tail,
447 memcpy (&tunnel->tunnel.peer, other,
448 sizeof (struct GNUNET_PeerIdentity));
449 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
450 &tunnel->tunnel.peer, atsi);
451 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
455 if (ntohs (ports[i]) == tunnel->tunnel.application_type)
458 tunnel = tunnel->next;
464 * Receive a message from core.
467 core_receive (void *cls, const struct GNUNET_PeerIdentity *other,
468 const struct GNUNET_MessageHeader *message,
469 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Received message of type %d from peer %s; dropping it.\n",
495 ntohs (rmessage->type), GNUNET_i2s (other));
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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "New inbound tunnel from peer %s; first message has type %d.\n",
519 GNUNET_i2s (other), ntohs (rmessage->type));
520 tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
521 tunnel->tunnel.connect_handler = NULL;
522 tunnel->tunnel.disconnect_handler = NULL;
523 tunnel->tunnel.handler_cls = NULL;
524 tunnel->tunnel.ctx = NULL;
525 tunnel->tunnel.handle = handle;
526 memcpy (&tunnel->tunnel.peer, other, sizeof (struct GNUNET_PeerIdentity));
527 memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
529 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
530 handle->established_tunnels.tail,
531 handle->established_tunnels.tail,
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
536 "Inbound message from peer %s; type %d.\n", GNUNET_i2s (other),
537 ntohs (rmessage->type));
539 return handler->callback (handle->cls, &tunnel->tunnel, &tunnel->tunnel.ctx,
540 other, rmessage, atsi);
543 struct GNUNET_MESH_Tunnel *
544 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
545 struct GNUNET_TIME_Relative timeout,
546 GNUNET_MESH_ApplicationType
548 GNUNET_MESH_TunnelConnectHandler
550 GNUNET_MESH_TunnelDisconnectHandler
551 disconnect_handler, void *handler_cls)
553 /* Look in the list of connected peers */
554 struct peer_list_element *element = handle->connected_peers.head;
556 while (element != NULL)
558 struct type_list_element *i;
560 for (i = element->type_head; i != NULL; i = i->next)
561 if (application_type == i->type)
562 return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
567 element = element->next;
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n",
573 /* Put into pending list */
574 struct tunnel_list_element *tunnel =
575 GNUNET_malloc (sizeof (struct tunnel_list_element));
577 tunnel->tunnel.connect_handler = connect_handler;
578 tunnel->tunnel.disconnect_handler = disconnect_handler;
579 tunnel->tunnel.handler_cls = handler_cls;
580 tunnel->tunnel.ctx = NULL;
581 tunnel->tunnel.handle = handle;
582 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
583 sizeof (struct GNUNET_PeerIdentity));
584 tunnel->tunnel.id.id = current_id++;
585 tunnel->tunnel.application_type = application_type;
587 GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
588 handle->pending_by_type_tunnels.tail,
589 handle->pending_by_type_tunnels.tail,
591 return &tunnel->tunnel;
596 struct GNUNET_MESH_Tunnel *
597 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
598 struct GNUNET_TIME_Relative timeout,
599 unsigned int num_peers,
600 const struct GNUNET_PeerIdentity *peers,
601 GNUNET_MESH_TunnelConnectHandler
603 GNUNET_MESH_TunnelDisconnectHandler
604 disconnect_handler, void *handler_cls)
609 struct tunnel_list_element *tunnel =
610 GNUNET_malloc (sizeof (struct tunnel_list_element));
612 tunnel->tunnel.connect_handler = connect_handler;
613 tunnel->tunnel.disconnect_handler = disconnect_handler;
614 tunnel->tunnel.handler_cls = handler_cls;
615 tunnel->tunnel.ctx = NULL;
616 tunnel->tunnel.handle = handle;
617 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
618 sizeof (struct GNUNET_PeerIdentity));
619 memcpy (&tunnel->tunnel.id.target, peers,
620 sizeof (struct GNUNET_PeerIdentity));
621 tunnel->tunnel.id.id = current_id++;
622 memcpy (&tunnel->tunnel.peer, peers, sizeof (struct GNUNET_PeerIdentity));
624 struct peer_list_element *element = handle->connected_peers.head;
626 while (element != NULL)
629 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
631 element = element->next;
636 /* we are connected to this peer */
637 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
638 handle->established_tunnels.tail,
639 handle->established_tunnels.tail,
641 GNUNET_SCHEDULER_add_now (call_connect_handler, tunnel);
644 memcmp (peers, &handle->myself, sizeof (struct GNUNET_PeerIdentity)))
646 /* we are the peer */
647 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
648 handle->established_tunnels.tail,
649 handle->established_tunnels.tail,
651 GNUNET_SCHEDULER_add_now (send_self_connect, tunnel);
655 /* we are not connected to this peer */
656 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
657 handle->pending_tunnels.tail,
658 handle->pending_tunnels.tail, tunnel);
659 (void) GNUNET_CORE_peer_request_connect (handle->core, peers, NULL, NULL);
662 return &tunnel->tunnel;
665 const struct GNUNET_PeerIdentity *
666 GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel)
668 return &tunnel->peer;
672 core_notify (void *cls, size_t size, void *buf)
674 struct notify_cls *ncls = cls;
675 struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
677 tunnel->notify_handle = NULL;
678 struct tunnel_message *message = buf;
679 void *cbuf = (void *) &message[1];
681 GNUNET_assert (NULL != ncls->notify);
684 ncls->notify (ncls->notify_cls, size - sizeof (struct tunnel_message),
692 sent += sizeof (struct tunnel_message);
694 message->hdr.type = htons (GNUNET_MESSAGE_TYPE_MESH);
695 message->hdr.size = htons (sent);
696 memcpy (&message->id, &tunnel->id, sizeof (struct tunnel_id));
702 * Ask the mesh to call "notify" once it is ready to transmit the
703 * given number of bytes to the specified "target". If we are not yet
704 * connected to the specified peer, a call to this function will cause
705 * us to try to establish a connection.
707 * @param tunnel tunnel to use for transmission
708 * @param cork is corking allowed for this transmission?
709 * @param priority how important is the message?
710 * @param maxdelay how long can the message wait?
711 * @param target destination for the message, NULL for multicast to all tunnel targets
712 * @param notify_size how many bytes of buffer space does notify want?
713 * @param notify function to call when buffer space is available;
714 * will be called with NULL on timeout or if the overall queue
715 * for this peer is larger than queue_size and this is currently
716 * the message with the lowest priority
717 * @param notify_cls closure for notify
718 * @return non-NULL if the notify callback was queued,
719 * NULL if we can not even queue the request (insufficient
720 * memory); if NULL is returned, "notify" will NOT be called.
722 struct GNUNET_MESH_TransmitHandle *
723 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
725 struct GNUNET_TIME_Relative maxdelay,
726 const struct GNUNET_PeerIdentity *target
727 __attribute__ ((unused)), size_t notify_size,
728 GNUNET_CONNECTION_TransmitReadyNotify notify,
731 if (NULL != tunnel->notify_handle)
737 struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls));
739 cls->notify_cls = notify_cls;
740 GNUNET_assert (NULL != notify);
741 cls->notify = notify;
742 cls->tunnel = tunnel;
744 tunnel->notify_handle =
745 (struct GNUNET_MESH_TransmitHandle *)
746 GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority,
747 maxdelay, &tunnel->peer,
749 sizeof (struct tunnel_message),
750 &core_notify, (void *) cls);
752 return tunnel->notify_handle;
756 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
758 GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle
763 GNUNET_MESH_tunnel_set_head (struct GNUNET_MESH_Tunnel *tunnel, void *head)
765 tunnel->app_head = head;
769 GNUNET_MESH_tunnel_set_tail (struct GNUNET_MESH_Tunnel *tunnel, void *tail)
771 tunnel->app_tail = tail;
775 GNUNET_MESH_tunnel_get_head (struct GNUNET_MESH_Tunnel *tunnel)
777 return tunnel->app_head;
781 GNUNET_MESH_tunnel_get_tail (struct GNUNET_MESH_Tunnel *tunnel)
783 return tunnel->app_head;
787 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
789 tunnel->app_data = data;
793 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)
795 return tunnel->app_data;
800 build_hello_message (struct GNUNET_MESH_Handle *handle,
801 const GNUNET_MESH_ApplicationType *stypes)
804 const GNUNET_MESH_ApplicationType *t;
806 for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++) ;
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num);
810 handle->hello_message_size = sizeof (uint16_t) + /* For the number of types */
811 num * sizeof (GNUNET_MESH_ApplicationType); /* For the types */
813 uint16_t *nums = GNUNET_malloc (handle->hello_message_size);
814 GNUNET_MESH_ApplicationType *types =
815 (GNUNET_MESH_ApplicationType *) (nums + 1);
821 for (i = 0; i < num; i++)
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n",
825 types[i] = htons (stypes[i]);
828 handle->hello_message = nums;
832 struct GNUNET_MESH_Handle *
833 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
834 GNUNET_MESH_TunnelEndHandler cleaner,
835 const struct GNUNET_MESH_MessageHandler *handlers,
836 const GNUNET_MESH_ApplicationType *stypes)
838 struct GNUNET_MESH_Handle *ret =
839 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
841 ret->connected_to_core = GNUNET_NO;
842 ret->connected_peers.head = NULL;
843 ret->connected_peers.tail = NULL;
844 ret->cleaner = cleaner;
847 const struct GNUNET_MESH_MessageHandler *it;
848 unsigned int len = 1;
850 for (it = handlers; it->callback != NULL; it++)
856 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
857 memset (ret->handlers, 0, len * sizeof (struct GNUNET_MESH_MessageHandler));
858 memcpy (ret->handlers, handlers,
859 len * sizeof (struct GNUNET_MESH_MessageHandler));
861 build_hello_message (ret, stypes);
863 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
864 {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
865 {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
870 GNUNET_CORE_connect (cfg, 42, ret, &core_startup, &core_connect,
871 &core_disconnect, NULL, NULL, GNUNET_NO, NULL,
872 GNUNET_NO, core_handlers);
877 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
879 GNUNET_free (handle->handlers);
880 GNUNET_free (handle->hello_message);
881 GNUNET_CORE_disconnect (handle->core);
883 struct peer_list_element *element = handle->connected_peers.head;
885 while (element != NULL)
887 struct peer_list_element *next = element->next;
889 while (element->type_head != NULL)
891 struct type_list_element *tail = element->type_tail;
893 GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail,
897 GNUNET_CORE_notify_transmit_ready_cancel (element->hello);
898 GNUNET_SCHEDULER_cancel (element->sched);
899 GNUNET_free (element);
903 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
905 while (tunnel != NULL)
907 struct tunnel_list_element *next = tunnel->next;
909 GNUNET_free (tunnel);
912 tunnel = handle->established_tunnels.head;;
913 while (tunnel != NULL)
915 struct tunnel_list_element *next = tunnel->next;
917 GNUNET_free (tunnel);
921 GNUNET_free (handle);
924 /* end of mesh_api.c */