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>
35 uint32_t id GNUNET_PACKED;
36 struct GNUNET_PeerIdentity initiator;
37 struct GNUNET_PeerIdentity target;
40 static uint32_t current_id = 0;
44 struct GNUNET_MessageHeader hdr;
46 /* followed by another GNUNET_MessageHeader */
52 GNUNET_CONNECTION_TransmitReadyNotify notify;
53 struct GNUNET_MESH_Tunnel *tunnel;
56 struct GNUNET_MESH_Tunnel
58 /* The other peer this tunnel leads to; just unicast for the moment! */
59 struct GNUNET_PeerIdentity peer;
63 /* The handlers and cls for outbound tunnels. Are NULL for inbound tunnels. */
64 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
65 GNUNET_MESH_TunnelConnectHandler connect_handler;
68 struct GNUNET_MESH_Handle* handle;
70 /* The message-type requested for this tunnel. Is only needed for pending
73 uint16_t message_type;
75 /* The context of the receive-function. */
79 struct tunnel_list_element
81 struct GNUNET_MESH_Tunnel tunnel;
82 struct tunnel_list_element *next, *prev;
87 struct tunnel_list_element *head, *tail;
90 struct peer_list_element
92 struct GNUNET_PeerIdentity peer;
94 /* how many Message-Types can this peer receive */
95 unsigned int num_types;
97 /* array of message-types */
98 GNUNET_MESH_ApplicationType *types;
100 struct GNUNET_TRANSPORT_ATS_Information atsi;
101 struct peer_list_element *next, *prev;
106 struct peer_list_element *head, *tail;
109 struct GNUNET_MESH_Handle
111 struct GNUNET_CORE_Handle *core;
112 struct GNUNET_MESH_MessageHandler *handlers;
113 struct GNUNET_PeerIdentity myself;
114 unsigned int connected_to_core;
115 struct peer_list connected_peers;
116 struct tunnel_list established_tunnels;
117 struct tunnel_list pending_tunnels;
118 struct tunnel_list pending_by_type_tunnels;
120 GNUNET_MESH_TunnelEndHandler *cleaner;
121 size_t hello_message_size;
122 uint16_t *hello_message;
126 send_end_connect(void* cls,
127 const struct GNUNET_SCHEDULER_TaskContext* tc)
129 struct GNUNET_MESH_Tunnel* tunnel = cls;
131 tunnel->connect_handler(tunnel->handler_cls, NULL, NULL);
135 send_self_connect(void* cls,
136 const struct GNUNET_SCHEDULER_TaskContext* tc)
138 struct GNUNET_MESH_Tunnel* tunnel = cls;
140 tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL);
141 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
145 call_connect_handler (void *cls,
146 const struct GNUNET_SCHEDULER_TaskContext *tc)
148 struct GNUNET_MESH_Tunnel *tunnel = cls;
150 tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer,
152 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
156 core_startup (void *cls,
157 struct GNUNET_CORE_Handle *core,
158 const struct GNUNET_PeerIdentity *my_identity,
159 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
161 struct GNUNET_MESH_Handle *handle = cls;
162 memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity));
163 handle->connected_to_core = GNUNET_YES;
167 send_hello_message (void *cls, size_t size, void *buf)
169 struct GNUNET_MESH_Handle *handle = cls;
170 struct GNUNET_MessageHeader *hdr = buf;
172 size_t sent = sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size;
174 hdr->type = htons(GNUNET_MESSAGE_TYPE_MESH_HELLO);
175 hdr->size = htons(size);
177 memcpy(hdr+1, handle->hello_message, handle->hello_message_size);
183 * Core calls this if we are connected to a new peer.
185 * The peer is added to the connected_peers-list.
189 core_connect (void *cls,
190 const struct GNUNET_PeerIdentity *peer,
191 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
193 struct GNUNET_MESH_Handle *handle = cls;
195 /* Send a hello to this peer */
196 GNUNET_CORE_notify_transmit_ready(handle->core,
199 GNUNET_TIME_UNIT_SECONDS,
201 sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size,
205 /* put the new peer into the list of connected peers */
206 struct peer_list_element *element =
207 GNUNET_malloc (sizeof (struct peer_list_element));
208 memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity));
211 memcpy (&element->atsi, atsi,
212 sizeof (struct GNUNET_TRANSPORT_ATS_Information));
214 GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head,
215 handle->connected_peers.tail,
216 handle->connected_peers.tail, element);
218 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
219 while (tunnel != NULL)
222 memcmp (&tunnel->tunnel.peer, peer,
223 sizeof (struct GNUNET_PeerIdentity)))
225 struct tunnel_list_element *next = tunnel->next;
226 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
227 handle->pending_tunnels.tail, tunnel);
228 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
229 handle->established_tunnels.tail,
230 handle->established_tunnels.tail,
232 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
234 GNUNET_SCHEDULER_add_now(send_end_connect, tunnel);
238 tunnel = tunnel->next;
243 * Core calls this if we disconnect a peer
245 * Remove this peer from the list of connected peers
246 * Close all tunnels this peer belongs to
249 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
251 struct GNUNET_MESH_Handle *handle = cls;
253 struct peer_list_element *element = handle->connected_peers.head;
254 while (element != NULL)
257 memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)))
259 element = element->next;
263 GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head,
264 handle->connected_peers.tail, element);
265 GNUNET_free_non_null(element->types);
266 GNUNET_free (element);
269 struct tunnel_list_element *telement = handle->established_tunnels.head;
270 while (telement != NULL)
273 memcmp (&telement->tunnel.peer, peer,
274 sizeof (struct GNUNET_PeerIdentity)))
276 /* disconnect tunnels */
277 /* outbound tunnels */
278 if (telement->tunnel.connect_handler != NULL && NULL != telement->tunnel.disconnect_handler)
279 telement->tunnel.disconnect_handler (telement->tunnel.handler_cls,
281 /* inbound tunnels */
282 else if (NULL != handle->cleaner)
283 handle->cleaner (handle->cls, &telement->tunnel,
284 &telement->tunnel.ctx);
286 struct tunnel_list_element *next = telement->next;
287 GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head,
288 handle->established_tunnels.tail,
290 GNUNET_free (telement);
295 telement = telement->next;
301 * Receive a message from core.
302 * This is a hello-message, containing the message-types the other peer can receive
305 receive_hello (void *cls,
306 const struct GNUNET_PeerIdentity *other,
307 const struct GNUNET_MessageHeader *message,
308 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
310 struct GNUNET_MESH_Handle *handle = cls;
311 uint16_t *num = (uint16_t *) (message + 1);
312 uint16_t *ports = num + 1;
315 struct peer_list_element *element = handle->connected_peers.head;
316 while (element != NULL)
319 memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity)))
321 element = element->next;
324 /* TODO: add, not replace! */
325 /* TODO: if this changes anything: send new hello */
326 element->num_types = *num;
327 element->types = GNUNET_malloc (*num * sizeof (GNUNET_MESH_ApplicationType));
329 for (i = 0; i < *num; i++)
330 element->types[i] = (GNUNET_MESH_ApplicationType)ntohs (ports[i]);
332 struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head;
333 while (tunnel != NULL)
335 struct tunnel_list_element *next = tunnel->next;
336 for (i = 0; i < *num; i++)
338 if (ntohs (ports[i]) == tunnel->tunnel.message_type)
340 GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head,
341 handle->pending_tunnels.tail,
343 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.
345 handle->established_tunnels.
347 handle->established_tunnels.
349 tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls,
350 &tunnel->tunnel.peer, atsi);
351 GNUNET_SCHEDULER_add_now (send_end_connect, tunnel);
355 if (ntohs (ports[i]) == tunnel->tunnel.message_type)
358 tunnel = tunnel->next;
364 * Receive a message from core.
367 core_receive (void *cls,
368 const struct GNUNET_PeerIdentity *other,
369 const struct GNUNET_MessageHeader *message,
370 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
372 struct GNUNET_MESH_Handle *handle = cls;
373 struct tunnel_message *tmessage = (struct tunnel_message *) message;
374 struct GNUNET_MessageHeader *rmessage =
375 (struct GNUNET_MessageHeader *) (tmessage + 1);
377 struct GNUNET_MESH_MessageHandler *handler;
379 for (handler = handle->handlers; handler->callback != NULL; handler++)
381 if ( (ntohs (rmessage->type) == handler->type)
382 && ( (handler->expected_size == 0)
383 || (handler->expected_size == ntohs (rmessage->size))) )
389 /* handler->callback handles this message */
391 /* If no handler was found, drop the message but keep the channel open */
392 if (handler->callback == NULL)
395 struct tunnel_list_element *tunnel = handle->established_tunnels.head;
397 while (tunnel != NULL)
399 if (tunnel->tunnel.id.id == tmessage->id.id &&
401 memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator,
402 sizeof (struct GNUNET_PeerIdentity)))
404 memcmp (&tmessage->id.target, &tunnel->tunnel.id.target,
405 sizeof (struct GNUNET_PeerIdentity))))
407 tunnel = tunnel->next;
410 /* if no tunnel was found: create a new inbound tunnel */
413 tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element));
414 tunnel->tunnel.connect_handler = NULL;
415 tunnel->tunnel.disconnect_handler = NULL;
416 tunnel->tunnel.handler_cls = NULL;
417 tunnel->tunnel.ctx = NULL;
418 tunnel->tunnel.handle = handle;
419 memcpy (&tunnel->tunnel.peer, other,
420 sizeof (struct GNUNET_PeerIdentity));
421 memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id));
423 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
424 handle->established_tunnels.tail,
425 handle->established_tunnels.tail,
429 return handler->callback (handle->cls, &tunnel->tunnel,
430 &tunnel->tunnel.ctx, other, rmessage, atsi);
433 struct GNUNET_MESH_Tunnel *
434 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle,
435 struct GNUNET_TIME_Relative timeout,
436 GNUNET_MESH_ApplicationType message_type,
437 GNUNET_MESH_TunnelConnectHandler
439 GNUNET_MESH_TunnelDisconnectHandler
443 /* Look in the list of connected peers */
444 struct peer_list_element *element = handle->connected_peers.head;
445 while (element != NULL)
448 for (i = 0; i < element->num_types; i++)
449 if (message_type == element->types[i])
450 return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1,
455 element = element->next;
458 /* Put into pending list */
459 struct tunnel_list_element *tunnel =
460 GNUNET_malloc (sizeof (struct tunnel_list_element));
462 tunnel->tunnel.connect_handler = connect_handler;
463 tunnel->tunnel.disconnect_handler = disconnect_handler;
464 tunnel->tunnel.handler_cls = handler_cls;
465 tunnel->tunnel.ctx = NULL;
466 tunnel->tunnel.handle = handle;
467 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
468 sizeof (struct GNUNET_PeerIdentity));
469 tunnel->tunnel.id.id = current_id++;
470 tunnel->tunnel.message_type = message_type;
472 GNUNET_CONTAINER_DLL_insert_after (handle->pending_by_type_tunnels.head,
473 handle->pending_by_type_tunnels.tail,
474 handle->pending_by_type_tunnels.tail,
476 return &tunnel->tunnel;
481 struct GNUNET_MESH_Tunnel *
482 GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle,
483 struct GNUNET_TIME_Relative timeout,
484 unsigned int num_peers,
485 const struct GNUNET_PeerIdentity *peers,
486 GNUNET_MESH_TunnelConnectHandler
488 GNUNET_MESH_TunnelDisconnectHandler
489 disconnect_handler, void *handler_cls)
494 struct tunnel_list_element *tunnel =
495 GNUNET_malloc (sizeof (struct tunnel_list_element));
497 tunnel->tunnel.connect_handler = connect_handler;
498 tunnel->tunnel.disconnect_handler = disconnect_handler;
499 tunnel->tunnel.handler_cls = handler_cls;
500 tunnel->tunnel.ctx = NULL;
501 tunnel->tunnel.handle = handle;
502 memcpy (&tunnel->tunnel.id.initiator, &handle->myself,
503 sizeof (struct GNUNET_PeerIdentity));
504 memcpy (&tunnel->tunnel.id.target, peers,
505 sizeof (struct GNUNET_PeerIdentity));
506 tunnel->tunnel.id.id = current_id++;
507 memcpy (&tunnel->tunnel.peer, peers, sizeof(struct GNUNET_PeerIdentity));
509 struct peer_list_element *element = handle->connected_peers.head;
510 while (element != NULL)
513 memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity)))
515 element = element->next;
520 /* we are connected to this peer */
521 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
522 handle->established_tunnels.tail,
523 handle->established_tunnels.tail,
525 GNUNET_SCHEDULER_add_now(call_connect_handler, tunnel);
528 memcmp (peers, &handle->myself,
529 sizeof (struct GNUNET_PeerIdentity)))
531 /* we are the peer */
532 GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head,
533 handle->established_tunnels.tail,
534 handle->established_tunnels.tail,
536 GNUNET_SCHEDULER_add_now(send_self_connect, tunnel);
540 /* we are not connected to this peer */
541 GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head,
542 handle->pending_tunnels.tail,
543 handle->pending_tunnels.tail,
545 (void) GNUNET_CORE_peer_request_connect (handle->core,
551 return &tunnel->tunnel;
554 const struct GNUNET_PeerIdentity*
555 GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel)
557 return &tunnel->peer;
561 core_notify(void* cls, size_t size, void* buf)
563 struct notify_cls *ncls = cls;
564 struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel;
565 struct tunnel_message* message = buf;
566 void* cbuf = (void*) &message[1];
567 GNUNET_assert(NULL != ncls->notify);
569 size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf);
573 if (0 == sent) return 0;
575 sent += sizeof(struct tunnel_message);
577 message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH);
578 message->hdr.size = htons(sent);
579 memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id));
585 * Ask the mesh to call "notify" once it is ready to transmit the
586 * given number of bytes to the specified "target". If we are not yet
587 * connected to the specified peer, a call to this function will cause
588 * us to try to establish a connection.
590 * @param tunnel tunnel to use for transmission
591 * @param cork is corking allowed for this transmission?
592 * @param priority how important is the message?
593 * @param maxdelay how long can the message wait?
594 * @param target destination for the message, NULL for multicast to all tunnel targets
595 * @param notify_size how many bytes of buffer space does notify want?
596 * @param notify function to call when buffer space is available;
597 * will be called with NULL on timeout or if the overall queue
598 * for this peer is larger than queue_size and this is currently
599 * the message with the lowest priority
600 * @param notify_cls closure for notify
601 * @return non-NULL if the notify callback was queued,
602 * NULL if we can not even queue the request (insufficient
603 * memory); if NULL is returned, "notify" will NOT be called.
605 struct GNUNET_MESH_TransmitHandle *
606 GNUNET_MESH_notify_transmit_ready (struct
614 const struct GNUNET_PeerIdentity *target,
617 GNUNET_CONNECTION_TransmitReadyNotify
618 notify, void *notify_cls)
620 struct notify_cls *cls = GNUNET_malloc(sizeof(struct notify_cls));
621 cls->notify_cls = notify_cls;
622 GNUNET_assert(NULL != notify);
623 cls->notify = notify;
624 cls->tunnel = tunnel;
625 GNUNET_CORE_notify_transmit_ready(tunnel->handle->core,
630 notify_size + sizeof(struct tunnel_message),
634 /* aborting is not implemented yet */
635 return (struct GNUNET_MESH_TransmitHandle*) 1;
638 void build_hello_message(struct GNUNET_MESH_Handle* handle, int num)
640 handle->hello_message_size = sizeof(uint16_t) + /* For the number of types */
641 num * sizeof(uint16_t); /* For the types */
643 uint16_t *nums = GNUNET_malloc(handle->hello_message_size);
644 uint16_t *types = nums + 1;
649 for(i = 0; i < num; i++)
650 types[i] = handle->handlers[i].type;
652 handle->hello_message = nums;
656 struct GNUNET_MESH_Handle *
657 GNUNET_MESH_connect (const struct
658 GNUNET_CONFIGURATION_Handle
660 GNUNET_MESH_TunnelEndHandler
662 const struct GNUNET_MESH_MessageHandler *handlers,
663 const GNUNET_MESH_ApplicationType *stypes)
665 struct GNUNET_MESH_Handle *ret =
666 GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
668 ret->connected_to_core = GNUNET_NO;
669 ret->connected_peers.head = NULL;
670 ret->connected_peers.tail = NULL;
671 ret->cleaner = cleaner;
674 const struct GNUNET_MESH_MessageHandler *it;
675 unsigned int len = 1;
676 for (it = handlers; it->callback != NULL; it++)
682 GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler));
683 memset(ret->handlers, 0, len * sizeof(struct GNUNET_MESH_MessageHandler));
684 memcpy (ret->handlers, handlers,
685 len * sizeof (struct GNUNET_MESH_MessageHandler));
687 build_hello_message(ret, len);
689 const static struct GNUNET_CORE_MessageHandler core_handlers[] = {
690 {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0},
691 {&receive_hello, GNUNET_MESSAGE_TYPE_MESH_HELLO, 0},
695 ret->core = GNUNET_CORE_connect (cfg,
703 GNUNET_NO, NULL, GNUNET_NO, core_handlers);
708 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
710 GNUNET_free (handle->handlers);
711 GNUNET_free (handle->hello_message);
712 GNUNET_CORE_disconnect (handle->core);
714 struct peer_list_element *element = handle->connected_peers.head;
715 while (element != NULL)
717 struct peer_list_element *next = element->next;
718 GNUNET_free_non_null(element->types);
719 GNUNET_free (element);
723 struct tunnel_list_element *tunnel = handle->pending_tunnels.head;
724 while (tunnel != NULL)
726 struct tunnel_list_element *next = tunnel->next;
727 GNUNET_free (tunnel);
730 tunnel = handle->established_tunnels.head;;
731 while (tunnel != NULL)
733 struct tunnel_list_element *next = tunnel->next;
734 GNUNET_free (tunnel);
738 GNUNET_free (handle);
741 /* end of mesh_api.c */