2 This file is part of GNUnet.
3 (C) 2011 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_new.c
23 * @brief mesh api: client implementation of mesh service
24 * @author Bartlomiej Polot
29 * - AUXILIARY FUNCTIONS
32 * - API CALL DEFINITIONS
39 #if 0 /* keep Emacsens' auto-indent happy */
46 #include "gnunet_common.h"
47 #include "gnunet_client_lib.h"
48 #include "gnunet_util_lib.h"
49 #include "gnunet_peer_lib.h"
50 #include "gnunet_mesh_service_new.h"
52 #include "mesh_protocol.h"
54 /******************************************************************************/
55 /************************ DATA STRUCTURES ****************************/
56 /******************************************************************************/
59 * Opaque handle to the service.
61 struct GNUNET_MESH_Handle
64 * Handle to the server connection, to send messages later
66 struct GNUNET_CLIENT_Connection *client;
69 * Set of handlers used for processing incoming messages in the tunnels
71 const struct GNUNET_MESH_MessageHandler *message_handlers;
75 * Set of applications that should be claimed to be offered at this node.
76 * Note that this is just informative, the appropiate handlers must be
77 * registered independently and the mapping is up to the developer of the
80 const GNUNET_MESH_ApplicationType *applications;
84 * Double linked list of the tunnels this client is connected to.
86 struct GNUNET_MESH_Tunnel *tunnels_head;
87 struct GNUNET_MESH_Tunnel *tunnels_tail;
90 * tid of the next tunnel to create (to avoid reusing IDs often)
92 MESH_TunnelNumber next_tid;
95 * Callback for tunnel disconnection
97 GNUNET_MESH_TunnelEndHandler *cleaner;
100 * Handle to cancel pending transmissions in case of disconnection
102 struct GNUNET_CLIENT_TransmitHandle *th;
105 * Closure for all the handlers given by the client
111 * Opaque handle to a tunnel.
113 struct GNUNET_MESH_Tunnel
119 struct GNUNET_MESH_Tunnel *next;
120 struct GNUNET_MESH_Tunnel *prev;
123 * Local ID of the tunnel
125 MESH_TunnelNumber tid;
128 * Owner of the tunnel
130 GNUNET_PEER_Id owner;
133 * Callback to execute when peers connect to the tunnel
135 GNUNET_MESH_TunnelConnectHandler connect_handler;
138 * Callback to execute when peers disconnect to the tunnel
140 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
143 * All peers added to the tunnel
145 GNUNET_PEER_Id *peers;
148 * Closure for the connect/disconnect handlers
153 * Handle to the mesh this tunnel belongs to
155 struct GNUNET_MESH_Handle *mesh;
158 struct GNUNET_MESH_TransmitHandle
163 /******************************************************************************/
164 /*********************** AUXILIARY FUNCTIONS *************************/
165 /******************************************************************************/
168 * Get the tunnel handler for the tunnel specified by id from the given handle
169 * @param h Mesh handle
170 * @param tid ID of the wanted tunnel
171 * @return handle to the required tunnel or NULL if not found
173 static struct GNUNET_MESH_Tunnel *
174 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
176 struct GNUNET_MESH_Tunnel *t;
190 /******************************************************************************/
191 /*********************** RECEIVE HANDLERS ****************************/
192 /******************************************************************************/
195 * Process the new tunnel notification and add it to the tunnels in the handle
197 * @param h The mesh handle
198 * @param msg A message with the details of the new incoming tunnel
201 process_tunnel_create (struct GNUNET_MESH_Handle *h,
202 const struct GNUNET_MESH_TunnelMessage *msg)
204 struct GNUNET_MESH_Tunnel *t;
205 MESH_TunnelNumber tid;
207 tid = ntohl (msg->tunnel_id);
208 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "MESH: received an incoming tunnel with tid in local range (%X)\n",
214 return; //FIXME abort? reconnect?
216 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
226 * Process the new peer event and notify the upper level of it
228 * @param h The mesh handle
229 * @param msg A message with the details of the peer event
232 process_peer_event (struct GNUNET_MESH_Handle *h,
233 const struct GNUNET_MESH_PeerControl *msg)
235 struct GNUNET_MESH_Tunnel *t;
238 size = ntohs (msg->header.size);
239 if (size != sizeof (struct GNUNET_MESH_PeerControl))
244 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
250 if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED == msg->header.type)
252 if (NULL != t->connect_handler)
254 t->connect_handler (t->cls, &msg->peer, NULL); /* FIXME atsi */
259 if (NULL != t->disconnect_handler)
261 t->disconnect_handler (t->cls, &msg->peer);
268 * Process the incoming data packets
270 * @param h The mesh handle
271 * @param msh A message encapsulating the data
274 process_incoming_data (struct GNUNET_MESH_Handle *h,
275 const struct GNUNET_MessageHeader *message)
277 const struct GNUNET_MessageHeader *payload;
278 const struct GNUNET_MESH_MessageHandler *handler;
279 const struct GNUNET_PeerIdentity *peer;
280 struct GNUNET_MESH_Unicast *ucast;
281 struct GNUNET_MESH_Multicast *mcast;
282 struct GNUNET_MESH_ToOrigin *to_orig;
283 struct GNUNET_MESH_Tunnel *t;
287 type = ntohs (message->type);
290 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
291 ucast = (struct GNUNET_MESH_Unicast *) message;
292 t = retrieve_tunnel (h, ntohl (ucast->tid));
293 payload = (struct GNUNET_MessageHeader *) &ucast[1];
296 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
297 mcast = (struct GNUNET_MESH_Multicast *) message;
298 t = retrieve_tunnel (h, ntohl (mcast->tid));
299 payload = (struct GNUNET_MessageHeader *) &mcast[1];
302 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
303 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
304 t = retrieve_tunnel (h, ntohl (to_orig->tid));
305 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
306 peer = &to_orig->sender;
317 for (i = 0; i < h->n_handlers; i++)
319 handler = &h->message_handlers[i];
320 if (handler->type == type)
322 if (GNUNET_OK == handler->callback (h->cls, t, NULL, /* FIXME ctx */
323 peer, payload, NULL)) /* FIXME atsi */
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "MESH: callback completed successfully\n");
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "MESH: callback caused disconnection\n");
332 GNUNET_MESH_disconnect (h);
341 * Function to process all messages received from the service
344 * @param msg message received, NULL on timeout or fatal error
347 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
349 struct GNUNET_MESH_Handle *h = cls;
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: received a message from MESH\n");
358 switch (ntohs (msg->type))
360 /* Notify of a new incoming tunnel */
361 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
362 process_tunnel_create (h, (struct GNUNET_MESH_TunnelMessage *) msg);
364 /* Notify of a new peer or a peer disconnect in the tunnel */
365 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED:
366 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED:
367 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
369 /* Notify of a new data packet in the tunnel */
370 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
371 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
372 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
373 process_incoming_data (h, msg);
375 /* We shouldn't get any other packages, log and ignore */
377 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
378 "MESH: unsolicited message form service (type %d)\n",
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: message processed\n");
383 GNUNET_CLIENT_receive (h->client, &msg_received, h,
384 GNUNET_TIME_UNIT_FOREVER_REL);
389 /******************************************************************************/
390 /************************ SEND CALLBACKS ****************************/
391 /******************************************************************************/
395 * Function called to send a connect message to the service, specifying the
396 * types and applications that the client is interested in.
397 * "buf" will be NULL and "size" zero if the socket was closed for writing in
400 * @param cls closure, the mesh handle
401 * @param size number of bytes available in buf
402 * @param buf where the callee should write the connect message
403 * @return number of bytes written to buf
406 send_connect_packet (void *cls, size_t size, void *buf)
408 struct GNUNET_MESH_Handle *h = cls;
409 struct GNUNET_MESH_ClientConnect *msg;
410 GNUNET_MESH_ApplicationType *apps;
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send connect packet()\n", size);
417 if (0 == size || NULL == buf)
419 // FIXME: disconnect, reconnect, retry?
422 if (sizeof (struct GNUNET_MessageHeader) > size)
425 // FIXME: disconnect, reconnect, retry!
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "mesh: Send connect packet: %lu bytes buffer\n", size);
430 msg = (struct GNUNET_MESH_ClientConnect *) buf;
431 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
433 for (ntypes = 0, types = NULL; ntypes < h->n_handlers; ntypes++)
435 types = GNUNET_realloc (types, sizeof (uint16_t) * (ntypes + 1));
436 types[ntypes] = h->message_handlers[ntypes].type;
439 for (napps = 0, apps = NULL; napps < h->n_applications; napps++)
442 GNUNET_realloc (apps,
443 sizeof (GNUNET_MESH_ApplicationType) * (napps + 1));
444 apps[napps] = h->applications[napps];
448 htons (sizeof (struct GNUNET_MESH_ClientConnect) +
449 sizeof (uint16_t) * ntypes +
450 sizeof (GNUNET_MESH_ApplicationType) * napps);
451 memcpy (&msg[1], types, sizeof (uint16_t) * ntypes);
452 types = (uint16_t *) & msg[1];
453 memcpy (&types[ntypes], apps, sizeof (GNUNET_MESH_ApplicationType) * napps);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "mesh: Sent %lu bytes long message %d types and %d apps\n",
456 ntohs (msg->header.size), ntypes, napps);
457 msg->applications = htons (napps);
458 msg->types = htons (ntypes);
460 /* start listening */
461 GNUNET_CLIENT_receive (h->client, &msg_received, h,
462 GNUNET_TIME_UNIT_FOREVER_REL);
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Send connect packet() END\n",
466 return ntohs (msg->header.size);
471 * Function called to send a create tunnel message, specifying the tunnel
472 * number chosen by the client.
473 * "buf" will be NULL and "size" zero if the socket was closed for
474 * writing in the meantime.
476 * @param cls closure, the tunnel handle
477 * @param size number of bytes available in buf
478 * @param buf where the callee should write the create tunnel message
479 * @return number of bytes written to buf
482 send_tunnel_create_packet (void *cls, size_t size, void *buf)
484 struct GNUNET_MESH_Tunnel *t = cls;
485 struct GNUNET_MESH_Handle *h;
486 struct GNUNET_MESH_TunnelMessage *msg;
490 if (0 == size || buf == NULL)
492 // FIXME: disconnect, reconnect, retry?
495 if (sizeof (struct GNUNET_MESH_TunnelMessage) > size)
498 // FIXME: disconnect, reconnect, retry?
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Send create tunnel packet: %lu bytes buffer\n", size);
503 msg = (struct GNUNET_MESH_TunnelMessage *) buf;
504 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
506 msg->header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
507 msg->tunnel_id = htonl (t->tid);
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %lu bytes long message\n",
510 ntohs (msg->header.size));
512 return sizeof (struct GNUNET_MESH_TunnelMessage);
517 * Function called to send a destroy tunnel message, specifying the tunnel
518 * number chosen by the client.
519 * "buf" will be NULL and "size" zero if the socket was closed for
520 * writing in the meantime.
522 * @param cls closure, the tunnel handle
523 * @param size number of bytes available in buf
524 * @param buf where the callee should write the create tunnel message
525 * @return number of bytes written to buf
528 send_tunnel_destroy_packet (void *cls, size_t size, void *buf)
530 struct GNUNET_MESH_Tunnel *t = cls;
531 struct GNUNET_MESH_Handle *h;
532 struct GNUNET_MESH_TunnelMessage *msg;
536 if (0 == size || buf == NULL)
540 if (sizeof (struct GNUNET_MESH_TunnelMessage) > size)
543 // FIXME: disconnect, reconnect, retry!
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Send tunnel destroy packet: %lu bytes buffer\n", size);
548 msg = (struct GNUNET_MESH_TunnelMessage *) buf;
549 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
551 msg->header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
552 msg->tunnel_id = htonl (t->tid);
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %lu bytes long message\n",
555 ntohs (msg->header.size));
558 return sizeof (struct GNUNET_MESH_TunnelMessage);
562 /******************************************************************************/
563 /********************** API CALL DEFINITIONS *************************/
564 /******************************************************************************/
567 * Connect to the mesh service.
569 * @param cfg configuration to use
570 * @param cls closure for the various callbacks that follow
571 * (including handlers in the handlers array)
572 * @param cleaner function called when an *inbound* tunnel is destroyed
573 * @param handlers callbacks for messages we care about, NULL-terminated
574 * note that the mesh is allowed to drop notifications about
575 * inbound messages if the client does not process them fast
576 * enough (for this notification type, a bounded queue is used)
577 * @param stypes Application Types the client claims to offer
578 * @return handle to the mesh service
579 * NULL on error (in this case, init is never called)
581 struct GNUNET_MESH_Handle *
582 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
583 GNUNET_MESH_TunnelEndHandler cleaner,
584 const struct GNUNET_MESH_MessageHandler *handlers,
585 const GNUNET_MESH_ApplicationType *stypes)
587 struct GNUNET_MESH_Handle *h;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect()\n");
591 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
593 h->cleaner = cleaner;
594 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
595 if (h->client == NULL)
603 h->message_handlers = handlers;
604 h->applications = stypes;
605 h->next_tid = 0x80000000;
607 for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
608 for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
610 size = sizeof (struct GNUNET_MESH_ClientConnect);
611 size += h->n_handlers * sizeof (uint16_t);
612 size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
616 /* FIXME implement queue system instead */
617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "mesh: overwriting th of mesh\n");
618 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
621 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
622 GNUNET_TIME_UNIT_FOREVER_REL,
623 GNUNET_YES, &send_connect_packet,
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect() END\n");
632 * Disconnect from the mesh service.
634 * @param handle connection to mesh to disconnect
637 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
639 if (NULL != handle->th)
641 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
643 if (NULL != handle->client)
645 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
647 GNUNET_free (handle);
652 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
655 * @param h mesh handle
656 * @param connect_handler function to call when peers are actually connected
657 * @param disconnect_handler function to call when peers are disconnected
658 * @param handler_cls closure for connect/disconnect handlers
660 struct GNUNET_MESH_Tunnel *
661 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
662 GNUNET_MESH_TunnelConnectHandler connect_handler,
663 GNUNET_MESH_TunnelDisconnectHandler
664 disconnect_handler, void *handler_cls)
666 struct GNUNET_MESH_Tunnel *tunnel;
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n");
669 tunnel = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
671 tunnel->connect_handler = connect_handler;
672 tunnel->disconnect_handler = disconnect_handler;
673 tunnel->cls = handler_cls;
675 tunnel->tid = h->next_tid++;
676 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK; // keep in range
679 /* FIXME implement queue system instead */
680 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "mesh: overwriting th of mesh\n");
681 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
684 GNUNET_CLIENT_notify_transmit_ready (h->client,
686 GNUNET_MESH_TunnelMessage),
687 GNUNET_TIME_UNIT_FOREVER_REL,
689 &send_tunnel_create_packet,
697 * Destroy an existing tunnel.
699 * @param tun tunnel handle
702 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun)
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n");
705 if (NULL != tun->mesh->th)
707 /* FIXME implement queue system instead */
708 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "mesh: overwriting th of mesh\n");
709 GNUNET_CLIENT_notify_transmit_ready_cancel (tun->mesh->th);
712 GNUNET_CLIENT_notify_transmit_ready (tun->mesh->client,
714 GNUNET_MESH_TunnelMessage),
715 GNUNET_TIME_UNIT_FOREVER_REL,
717 &send_tunnel_destroy_packet,
723 * Request that a peer should be added to the tunnel. The existing
724 * connect handler will be called ONCE with either success or failure.
726 * @param tunnel handle to existing tunnel
727 * @param timeout how long to try to establish a connection
728 * @param peer peer to add
731 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
732 struct GNUNET_TIME_Relative timeout,
733 const struct GNUNET_PeerIdentity *peer)
735 static GNUNET_PEER_Id peer_id;
737 peer_id = GNUNET_PEER_intern (peer);
739 /* FIXME ACTUALLY DO STUFF */
740 tunnel->peers = &peer_id;
741 tunnel->connect_handler (tunnel->cls, peer, NULL);
747 * Request that a peer should be removed from the tunnel. The existing
748 * disconnect handler will be called ONCE if we were connected.
750 * @param tunnel handle to existing tunnel
751 * @param peer peer to remove
754 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
755 const struct GNUNET_PeerIdentity *peer)
757 /* FIXME ACTUALLY DO STUFF */
758 tunnel->peers = NULL;
759 tunnel->disconnect_handler (tunnel->cls, peer);
765 * Request that the mesh should try to connect to a peer supporting the given
768 * @param tunnel handle to existing tunnel
769 * @param timeout how long to try to establish a connection
770 * @param app_type application type that must be supported by the peer (MESH
771 * should discover peer in proximity handling this type)
774 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
775 struct GNUNET_TIME_Relative timeout,
776 GNUNET_MESH_ApplicationType app_type)
783 * Ask the mesh to call "notify" once it is ready to transmit the
784 * given number of bytes to the specified "target". If we are not yet
785 * connected to the specified peer, a call to this function will cause
786 * us to try to establish a connection.
788 * @param tunnel tunnel to use for transmission
789 * @param cork is corking allowed for this transmission?
790 * @param priority how important is the message?
791 * @param maxdelay how long can the message wait?
792 * @param target destination for the message,
793 * NULL for multicast to all tunnel targets
794 * @param notify_size how many bytes of buffer space does notify want?
795 * @param notify function to call when buffer space is available;
796 * will be called with NULL on timeout or if the overall queue
797 * for this peer is larger than queue_size and this is currently
798 * the message with the lowest priority
799 * @param notify_cls closure for notify
800 * @return non-NULL if the notify callback was queued,
801 * NULL if we can not even queue the request (insufficient
802 * memory); if NULL is returned, "notify" will NOT be called.
804 struct GNUNET_MESH_TransmitHandle *
805 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
807 struct GNUNET_TIME_Relative maxdelay,
808 const struct GNUNET_PeerIdentity *target,
810 GNUNET_CONNECTION_TransmitReadyNotify notify,
813 struct GNUNET_MESH_TransmitHandle *handle;
815 handle = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
821 #if 0 /* keep Emacsens' auto-indent happy */