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,
352 "mesh: received a message from MESH\n");
359 switch (ntohs (msg->type))
361 /* Notify of a new incoming tunnel */
362 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
363 process_tunnel_create (h, (struct GNUNET_MESH_TunnelMessage *) msg);
365 /* Notify of a new peer or a peer disconnect in the tunnel */
366 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED:
367 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED:
368 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
370 /* Notify of a new data packet in the tunnel */
371 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
372 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
373 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
374 process_incoming_data (h, msg);
376 /* We shouldn't get any other packages, log and ignore */
378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
379 "MESH: unsolicited message form service (type %d)\n",
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: message processed\n");
384 GNUNET_CLIENT_receive (h->client, &msg_received, h,
385 GNUNET_TIME_UNIT_FOREVER_REL);
390 /******************************************************************************/
391 /************************ SEND CALLBACKS ****************************/
392 /******************************************************************************/
396 * Function called to send a connect message to the service, specifying the
397 * types and applications that the client is interested in.
398 * "buf" will be NULL and "size" zero if the socket was closed for writing in
401 * @param cls closure, the mesh handle
402 * @param size number of bytes available in buf
403 * @param buf where the callee should write the connect message
404 * @return number of bytes written to buf
407 send_connect_packet (void *cls, size_t size, void *buf)
409 struct GNUNET_MESH_Handle *h = cls;
410 struct GNUNET_MESH_ClientConnect *msg;
411 GNUNET_MESH_ApplicationType *apps;
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "mesh: Send connect packet()\n", size);
419 if (0 == size || NULL == buf)
421 // FIXME: disconnect, reconnect, retry?
424 if (sizeof (struct GNUNET_MessageHeader) > size)
427 // FIXME: disconnect, reconnect, retry!
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "mesh: Send connect packet: %lu bytes buffer\n", size);
432 msg = (struct GNUNET_MESH_ClientConnect *) buf;
433 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
435 for (ntypes = 0, types = NULL; ntypes < h->n_handlers; ntypes++)
437 types = GNUNET_realloc (types, sizeof (uint16_t) * (ntypes + 1));
438 types[ntypes] = h->message_handlers[ntypes].type;
441 for (napps = 0, apps = NULL; napps < h->n_applications; napps++)
444 GNUNET_realloc (apps,
445 sizeof (GNUNET_MESH_ApplicationType) * (napps + 1));
446 apps[napps] = h->applications[napps];
450 htons (sizeof (struct GNUNET_MESH_ClientConnect) +
451 sizeof (uint16_t) * ntypes +
452 sizeof (GNUNET_MESH_ApplicationType) * napps);
454 memcpy (&msg[1], types, sizeof (uint16_t) * ntypes);
455 memcpy (&msg[1] + sizeof (uint16_t) * ntypes, apps,
456 sizeof (GNUNET_MESH_ApplicationType) * napps);
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "mesh: Sent %lu bytes long message %d types and %d apps\n",
459 ntohs (msg->header.size), ntypes, napps);
460 msg->applications = htons (napps);
461 msg->types = htons (ntypes);
463 /* start listening */
464 GNUNET_CLIENT_receive (h->client, &msg_received, h,
465 GNUNET_TIME_UNIT_FOREVER_REL);
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 "mesh: Send connect packet() END\n", size);
469 return ntohs (msg->header.size);
474 * Function called to send a create tunnel message, specifying the tunnel
475 * number chosen by the client.
476 * "buf" will be NULL and "size" zero if the socket was closed for
477 * writing in the meantime.
479 * @param cls closure, the tunnel handle
480 * @param size number of bytes available in buf
481 * @param buf where the callee should write the create tunnel message
482 * @return number of bytes written to buf
485 send_tunnel_create_packet (void *cls, size_t size, void *buf)
487 struct GNUNET_MESH_Tunnel *t = cls;
488 struct GNUNET_MESH_Handle *h;
489 struct GNUNET_MESH_TunnelMessage *msg;
493 if (0 == size || buf == NULL)
495 // FIXME: disconnect, reconnect, retry?
498 if (sizeof (struct GNUNET_MESH_TunnelMessage) > size)
501 // FIXME: disconnect, reconnect, retry?
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Send create tunnel packet: %lu bytes buffer\n", size);
506 msg = (struct GNUNET_MESH_TunnelMessage *) buf;
507 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
509 msg->header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
510 msg->tunnel_id = htonl (t->tid);
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %lu bytes long message\n",
513 ntohs (msg->header.size));
515 return sizeof (struct GNUNET_MESH_TunnelMessage);
520 * Function called to send a destroy tunnel message, specifying the tunnel
521 * number chosen by the client.
522 * "buf" will be NULL and "size" zero if the socket was closed for
523 * writing in the meantime.
525 * @param cls closure, the tunnel handle
526 * @param size number of bytes available in buf
527 * @param buf where the callee should write the create tunnel message
528 * @return number of bytes written to buf
531 send_tunnel_destroy_packet (void *cls, size_t size, void *buf)
533 struct GNUNET_MESH_Tunnel *t = cls;
534 struct GNUNET_MESH_Handle *h;
535 struct GNUNET_MESH_TunnelMessage *msg;
539 if (0 == size || buf == NULL)
543 if (sizeof (struct GNUNET_MESH_TunnelMessage) > size)
546 // FIXME: disconnect, reconnect, retry!
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "Send tunnel destroy packet: %lu bytes buffer\n", size);
551 msg = (struct GNUNET_MESH_TunnelMessage *) buf;
552 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
554 msg->header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
555 msg->tunnel_id = htonl (t->tid);
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent %lu bytes long message\n",
558 ntohs (msg->header.size));
561 return sizeof (struct GNUNET_MESH_TunnelMessage);
565 /******************************************************************************/
566 /********************** API CALL DEFINITIONS *************************/
567 /******************************************************************************/
570 * Connect to the mesh service.
572 * @param cfg configuration to use
573 * @param cls closure for the various callbacks that follow
574 * (including handlers in the handlers array)
575 * @param cleaner function called when an *inbound* tunnel is destroyed
576 * @param handlers callbacks for messages we care about, NULL-terminated
577 * note that the mesh is allowed to drop notifications about
578 * inbound messages if the client does not process them fast
579 * enough (for this notification type, a bounded queue is used)
580 * @param stypes Application Types the client claims to offer
581 * @return handle to the mesh service
582 * NULL on error (in this case, init is never called)
584 struct GNUNET_MESH_Handle *
585 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
586 GNUNET_MESH_TunnelEndHandler cleaner,
587 const struct GNUNET_MESH_MessageHandler *handlers,
588 const GNUNET_MESH_ApplicationType *stypes)
590 struct GNUNET_MESH_Handle *h;
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect()\n");
594 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
596 h->cleaner = cleaner;
597 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
598 if (h->client == NULL)
606 h->message_handlers = handlers;
607 h->applications = stypes;
608 h->next_tid = 0x80000000;
610 for (h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++) ;
611 for (h->n_applications = 0; stypes[h->n_applications]; h->n_applications++) ;
613 size = sizeof (struct GNUNET_MESH_ClientConnect);
614 size += h->n_handlers * sizeof (uint16_t);
615 size += h->n_applications * sizeof (GNUNET_MESH_ApplicationType);
618 /* FIXME implement queue system instead */
619 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
620 "mesh: overwriting th of mesh\n");
621 GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
624 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
625 GNUNET_TIME_UNIT_FOREVER_REL,
626 GNUNET_YES, &send_connect_packet,
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: GNUNET_MESH_connect() END\n");
635 * Disconnect from the mesh service.
637 * @param handle connection to mesh to disconnect
640 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
642 if (NULL != handle->th)
644 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
646 if (NULL != handle->client)
648 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
650 GNUNET_free (handle);
655 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
658 * @param h mesh handle
659 * @param connect_handler function to call when peers are actually connected
660 * @param disconnect_handler function to call when peers are disconnected
661 * @param handler_cls closure for connect/disconnect handlers
663 struct GNUNET_MESH_Tunnel *
664 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
665 GNUNET_MESH_TunnelConnectHandler connect_handler,
666 GNUNET_MESH_TunnelDisconnectHandler
667 disconnect_handler, void *handler_cls)
669 struct GNUNET_MESH_Tunnel *tunnel;
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n");
672 tunnel = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
674 tunnel->connect_handler = connect_handler;
675 tunnel->disconnect_handler = disconnect_handler;
676 tunnel->cls = handler_cls;
678 tunnel->tid = h->next_tid++;
679 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK; // keep in range
681 /* FIXME implement queue system instead */
682 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
683 "mesh: overwriting th of mesh\n");
684 GNUNET_CLIENT_notify_transmit_ready_cancel(h->th);
687 GNUNET_CLIENT_notify_transmit_ready (h->client,
689 GNUNET_MESH_TunnelMessage),
690 GNUNET_TIME_UNIT_FOREVER_REL,
692 &send_tunnel_create_packet,
700 * Destroy an existing tunnel.
702 * @param tun tunnel handle
705 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun)
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n");
708 if (NULL != tun->mesh->th) {
709 /* FIXME implement queue system instead */
710 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
711 "mesh: overwriting th of mesh\n");
712 GNUNET_CLIENT_notify_transmit_ready_cancel(tun->mesh->th);
714 tun->mesh->th = GNUNET_CLIENT_notify_transmit_ready (
717 GNUNET_MESH_TunnelMessage),
718 GNUNET_TIME_UNIT_FOREVER_REL,
720 &send_tunnel_destroy_packet,
726 * Request that a peer should be added to the tunnel. The existing
727 * connect handler will be called ONCE with either success or failure.
729 * @param tunnel handle to existing tunnel
730 * @param timeout how long to try to establish a connection
731 * @param peer peer to add
734 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
735 struct GNUNET_TIME_Relative timeout,
736 const struct GNUNET_PeerIdentity *peer)
738 static GNUNET_PEER_Id peer_id;
740 peer_id = GNUNET_PEER_intern (peer);
742 /* FIXME ACTUALLY DO STUFF */
743 tunnel->peers = &peer_id;
744 tunnel->connect_handler (tunnel->cls, peer, NULL);
750 * Request that a peer should be removed from the tunnel. The existing
751 * disconnect handler will be called ONCE if we were connected.
753 * @param tunnel handle to existing tunnel
754 * @param peer peer to remove
757 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
758 const struct GNUNET_PeerIdentity *peer)
760 /* FIXME ACTUALLY DO STUFF */
761 tunnel->peers = NULL;
762 tunnel->disconnect_handler (tunnel->cls, peer);
768 * Request that the mesh should try to connect to a peer supporting the given
771 * @param tunnel handle to existing tunnel
772 * @param timeout how long to try to establish a connection
773 * @param app_type application type that must be supported by the peer (MESH
774 * should discover peer in proximity handling this type)
777 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
778 struct GNUNET_TIME_Relative timeout,
779 GNUNET_MESH_ApplicationType app_type)
786 * Ask the mesh to call "notify" once it is ready to transmit the
787 * given number of bytes to the specified "target". If we are not yet
788 * connected to the specified peer, a call to this function will cause
789 * us to try to establish a connection.
791 * @param tunnel tunnel to use for transmission
792 * @param cork is corking allowed for this transmission?
793 * @param priority how important is the message?
794 * @param maxdelay how long can the message wait?
795 * @param target destination for the message,
796 * NULL for multicast to all tunnel targets
797 * @param notify_size how many bytes of buffer space does notify want?
798 * @param notify function to call when buffer space is available;
799 * will be called with NULL on timeout or if the overall queue
800 * for this peer is larger than queue_size and this is currently
801 * the message with the lowest priority
802 * @param notify_cls closure for notify
803 * @return non-NULL if the notify callback was queued,
804 * NULL if we can not even queue the request (insufficient
805 * memory); if NULL is returned, "notify" will NOT be called.
807 struct GNUNET_MESH_TransmitHandle *
808 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
810 struct GNUNET_TIME_Relative maxdelay,
811 const struct GNUNET_PeerIdentity *target,
813 GNUNET_CONNECTION_TransmitReadyNotify notify,
816 struct GNUNET_MESH_TransmitHandle *handle;
818 handle = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
824 #if 0 /* keep Emacsens' auto-indent happy */