2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
4 GNUnet is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; either version 3, or (at your
7 option) any later version.
8 GNUnet is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with GNUnet; see the file COPYING. If not, write to the
14 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15 Boston, MA 02111-1307, USA.
19 * @file mesh/mesh2_api.c
20 * @brief mesh2 api: client implementation of new mesh service
21 * @author Bartlomiej Polot
26 * - AUXILIARY FUNCTIONS
29 * - API CALL DEFINITIONS
31 * TODO: add regex to reconnect
34 #include "gnunet_common.h"
35 #include "gnunet_client_lib.h"
36 #include "gnunet_util_lib.h"
37 #include "gnunet_peer_lib.h"
38 #include "gnunet_mesh2_service.h"
40 #include "mesh_protocol.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "mesh2-api",__VA_ARGS__)
44 #define DEBUG_ACK GNUNET_YES
46 /******************************************************************************/
47 /************************ DATA STRUCTURES ****************************/
48 /******************************************************************************/
51 * Transmission queue to the service
53 struct GNUNET_MESH_TransmitHandle
59 struct GNUNET_MESH_TransmitHandle *next;
64 struct GNUNET_MESH_TransmitHandle *prev;
67 * Tunnel this message is sent on / for (may be NULL for control messages).
69 struct GNUNET_MESH_Tunnel *tunnel;
72 * Callback to obtain the message to transmit, or NULL if we
73 * got the message in 'data'. Notice that messages built
74 * by 'notify' need to be encapsulated with information about
77 GNUNET_CONNECTION_TransmitReadyNotify notify;
80 * Closure for 'notify'
85 * How long is this message valid. Once the timeout has been
86 * reached, the message must no longer be sent. If this
87 * is a message with a 'notify' callback set, the 'notify'
88 * function should be called with 'buf' NULL and size 0.
90 struct GNUNET_TIME_Absolute timeout;
93 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
95 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
98 * Target of the message, 0 for multicast. This field
99 * is only valid if 'notify' is non-NULL.
101 GNUNET_PEER_Id target;
104 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
111 * Opaque handle to the service.
113 struct GNUNET_MESH_Handle
117 * Handle to the server connection, to send messages later
119 struct GNUNET_CLIENT_Connection *client;
122 * Set of handlers used for processing incoming messages in the tunnels
124 const struct GNUNET_MESH_MessageHandler *message_handlers;
127 * Double linked list of the tunnels this client is connected to, head.
129 struct GNUNET_MESH_Tunnel *tunnels_head;
132 * Double linked list of the tunnels this client is connected to, tail.
134 struct GNUNET_MESH_Tunnel *tunnels_tail;
137 * Callback for inbound tunnel creation
139 GNUNET_MESH_InboundTunnelNotificationHandler *new_tunnel;
142 * Callback for inbound tunnel disconnection
144 GNUNET_MESH_TunnelEndHandler *cleaner;
147 * Handle to cancel pending transmissions in case of disconnection
149 struct GNUNET_CLIENT_TransmitHandle *th;
152 * Closure for all the handlers given by the client
157 * Messages to send to the service, head.
159 struct GNUNET_MESH_TransmitHandle *th_head;
162 * Messages to send to the service, tail.
164 struct GNUNET_MESH_TransmitHandle *th_tail;
167 * tid of the next tunnel to create (to avoid reusing IDs often)
169 MESH_TunnelNumber next_tid;
172 * Number of handlers in the handlers array.
174 unsigned int n_handlers;
177 * Number of applications in the applications array.
179 unsigned int n_applications;
182 * Have we started the task to receive messages from the service
183 * yet? We do this after we send the 'MESH_LOCAL_CONNECT' message.
188 * Configuration given by the client, in case of reconnection
190 const struct GNUNET_CONFIGURATION_Handle *cfg;
193 * Time to the next reconnect in case one reconnect fails
195 struct GNUNET_TIME_Relative reconnect_time;
198 * Task for trying to reconnect.
200 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
205 GNUNET_MESH_TunnelsCB tunnels_cb;
208 * Monitor callback closure.
215 GNUNET_MESH_TunnelCB tunnel_cb;
218 * Tunnel callback closure.
223 * All the peer in the tunnel so far.
225 struct GNUNET_PeerIdentity *peers;
228 * How many peers we have in this tunnel so far.
230 unsigned int tunnel_npeers;
233 unsigned int acks_sent;
234 unsigned int acks_recv;
240 * Description of a peer
242 struct GNUNET_MESH_Peer
245 * ID of the peer in short form
250 * Tunnel this peer belongs to
252 struct GNUNET_MESH_Tunnel *t;
255 * Flag indicating whether service has informed about its connection
263 * Opaque handle to a tunnel.
265 struct GNUNET_MESH_Tunnel
271 struct GNUNET_MESH_Tunnel *next;
276 struct GNUNET_MESH_Tunnel *prev;
279 * Handle to the mesh this tunnel belongs to
281 struct GNUNET_MESH_Handle *mesh;
284 * Local ID of the tunnel
286 MESH_TunnelNumber tid;
289 * Owner of the tunnel. (1 if the tunnel is a local client).
291 GNUNET_PEER_Id owner;
294 * Destination of the tunnel.
296 GNUNET_PEER_Id destination;
299 * Next hop for the tunnel.
301 GNUNET_PEER_Id next_hop;
304 * Previous hop for the tunnel.
306 GNUNET_PEER_Id prev_hop;
309 * Any data the caller wants to put in here
314 * Size of packet queued in this tunnel
316 unsigned int packet_size;
319 * Number of applications requested this tunnel
324 * Is the tunnel throttled to the slowest peer?
329 * Is the tunnel allowed to buffer?
334 * Next packet ID to send.
336 uint32_t next_send_pid;
339 * Maximum allowed PID to send (ACK recevied).
341 uint32_t max_send_pid;
344 * Last pid received from the service.
346 uint32_t last_recv_pid;
349 * Which ACK value have we last sent to the service?
351 uint32_t max_recv_pid;
355 /******************************************************************************/
356 /*********************** DECLARATIONS *************************/
357 /******************************************************************************/
360 * Function called to send a message to the service.
361 * "buf" will be NULL and "size" zero if the socket was closed for writing in
364 * @param cls closure, the mesh handle
365 * @param size number of bytes available in buf
366 * @param buf where the callee should write the connect message
367 * @return number of bytes written to buf
370 send_callback (void *cls, size_t size, void *buf);
373 /******************************************************************************/
374 /*********************** AUXILIARY FUNCTIONS *************************/
375 /******************************************************************************/
378 * Check if transmission is a payload packet.
380 * @param th Transmission handle.
382 * @return GNUNET_YES if it is a payload packet,
383 * GNUNET_NO if it is a mesh management packet.
386 th_is_payload (struct GNUNET_MESH_TransmitHandle *th)
388 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
393 * Check whether there is any message ready in the queue and find the size.
395 * @param h Mesh handle.
397 * @return The size of the first ready message in the queue,
398 * 0 if there is none.
401 message_ready_size (struct GNUNET_MESH_Handle *h)
403 struct GNUNET_MESH_TransmitHandle *th;
404 struct GNUNET_MESH_Tunnel *t;
406 for (th = h->th_head; NULL != th; th = th->next)
409 if (GNUNET_NO == th_is_payload (th))
411 LOG (GNUNET_ERROR_TYPE_DEBUG, " message internal\n");
414 if (GNUNET_NO == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
416 LOG (GNUNET_ERROR_TYPE_DEBUG, " message payload ok (%u <= %u)\n",
417 t->next_send_pid, t->max_send_pid);
426 * Get the tunnel handler for the tunnel specified by id from the given handle
427 * @param h Mesh handle
428 * @param tid ID of the wanted tunnel
429 * @return handle to the required tunnel or NULL if not found
431 static struct GNUNET_MESH_Tunnel *
432 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
434 struct GNUNET_MESH_Tunnel *t;
448 * Create a new tunnel and insert it in the tunnel list of the mesh handle
449 * @param h Mesh handle
450 * @param tid desired tid of the tunnel, 0 to assign one automatically
451 * @return handle to the created tunnel
453 static struct GNUNET_MESH_Tunnel *
454 create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
456 struct GNUNET_MESH_Tunnel *t;
458 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
459 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
463 t->tid = h->next_tid;
464 while (NULL != retrieve_tunnel (h, h->next_tid))
467 h->next_tid &= ~GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
468 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
475 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
476 t->last_recv_pid = (uint32_t) -1;
477 t->buffering = GNUNET_YES;
483 * Destroy the specified tunnel.
484 * - Destroys all peers, calling the disconnect callback on each if needed
485 * - Cancels all outgoing traffic for that tunnel, calling respective notifys
486 * - Calls cleaner if tunnel was inbound
487 * - Frees all memory used
489 * @param t Pointer to the tunnel.
490 * @param call_cleaner Whether to call the cleaner handler.
492 * @return Handle to the required tunnel or NULL if not found.
495 destroy_tunnel (struct GNUNET_MESH_Tunnel *t, int call_cleaner)
497 struct GNUNET_MESH_Handle *h;
498 struct GNUNET_PeerIdentity pi;
499 struct GNUNET_MESH_TransmitHandle *th;
500 struct GNUNET_MESH_TransmitHandle *next;
503 LOG (GNUNET_ERROR_TYPE_DEBUG, "destroy_tunnel %X\n", t->tid);
512 /* free all peer's ID */
513 GNUNET_CONTAINER_DLL_remove (h->tunnels_head, h->tunnels_tail, t);
514 GNUNET_PEER_change_rc (t->owner, -1);
515 GNUNET_PEER_change_rc (t->destination, -1);
516 GNUNET_PEER_change_rc (t->next_hop, -1);
517 GNUNET_PEER_change_rc (t->prev_hop, -1);
519 /* signal tunnel destruction */
520 if ( (NULL != h->cleaner) && (0 != t->owner) && (GNUNET_YES == call_cleaner) )
521 h->cleaner (h->cls, t, t->ctx);
523 /* check that clients did not leave messages behind in the queue */
524 for (th = h->th_head; NULL != th; th = next)
529 /* Clients should have aborted their requests already.
530 * Management traffic should be ok, as clients can't cancel that */
531 GNUNET_break (GNUNET_NO == th_is_payload(th));
532 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
534 /* clean up request */
535 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
536 GNUNET_SCHEDULER_cancel (th->timeout_task);
540 /* if there are no more pending requests with mesh service, cancel active request */
541 /* Note: this should be unnecessary... */
542 if ((0 == message_ready_size (h)) && (NULL != h->th))
544 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
550 GNUNET_free (t->peers);
552 GNUNET_PEER_change_rc (t->owner, -1);
553 if (0 != t->napps && t->apps)
554 GNUNET_free (t->apps);
561 * Get the peer descriptor for the peer with id from the given tunnel
562 * @param t Tunnel handle
563 * @param id Short form ID of the wanted peer
564 * @return handle to the requested peer or NULL if not found
566 static struct GNUNET_MESH_Peer *
567 retrieve_peer (struct GNUNET_MESH_Tunnel *t, GNUNET_PEER_Id id)
571 for (i = 0; i < t->npeers; i++)
572 if (t->peers[i]->id == id)
579 * Add a peer into a tunnel
580 * @param t Tunnel handle
581 * @param pi Full ID of the new peer
582 * @return handle to the newly created peer
584 static struct GNUNET_MESH_Peer *
585 add_peer_to_tunnel (struct GNUNET_MESH_Tunnel *t,
586 const struct GNUNET_PeerIdentity *pi)
588 struct GNUNET_MESH_Peer *p;
596 id = GNUNET_PEER_intern (pi);
598 p = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
601 GNUNET_array_append (t->peers, t->npeers, p);
607 * Remove a peer from a tunnel
608 * @param p Peer handle
611 remove_peer_from_tunnel (struct GNUNET_MESH_Peer *p)
615 for (i = 0; i < p->t->npeers; i++)
617 if (p->t->peers[i] == p)
620 if (i == p->t->npeers)
625 p->t->peers[i] = p->t->peers[p->t->npeers - 1];
626 GNUNET_array_grow (p->t->peers, p->t->npeers, p->t->npeers - 1);
631 * Notify client that the transmission has timed out
634 * @param tc task context
637 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
639 struct GNUNET_MESH_TransmitHandle *th = cls;
640 struct GNUNET_MESH_Handle *mesh;
642 mesh = th->tunnel->mesh;
643 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
644 th->tunnel->packet_size = 0;
645 if (GNUNET_YES == th_is_payload (th))
646 th->notify (th->notify_cls, 0, NULL);
648 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
650 /* nothing ready to transmit, no point in asking for transmission */
651 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
658 * Add a transmit handle to the transmission queue and set the
661 * @param h mesh handle with the queue head and tail
662 * @param th handle to the packet to be transmitted
665 add_to_queue (struct GNUNET_MESH_Handle *h,
666 struct GNUNET_MESH_TransmitHandle *th)
668 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
669 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value == th->timeout.abs_value)
672 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
673 (th->timeout), &timeout_transmission, th);
678 * Auxiliary function to send an already constructed packet to the service.
679 * Takes care of creating a new queue element, copying the message and
680 * calling the tmt_rdy function if necessary.
682 * @param h mesh handle
683 * @param msg message to transmit
684 * @param tunnel tunnel this send is related to (NULL if N/A)
687 send_packet (struct GNUNET_MESH_Handle *h,
688 const struct GNUNET_MessageHeader *msg,
689 struct GNUNET_MESH_Tunnel *tunnel);
693 * Send an ack on the tunnel to confirm the processing of a message.
695 * @param h Mesh handle.
696 * @param t Tunnel on which to send the ACK.
699 send_ack (struct GNUNET_MESH_Handle *h, struct GNUNET_MESH_Tunnel *t)
701 struct GNUNET_MESH_LocalAck msg;
704 delta = t->max_recv_pid - t->last_recv_pid;
705 if (delta > ACK_THRESHOLD)
707 LOG (GNUNET_ERROR_TYPE_DEBUG,
708 "Not sending ACK on tunnel %X: ACK: %u, PID: %u, buffer %u\n",
709 t->tid, t->max_recv_pid, t->last_recv_pid, delta);
712 if (GNUNET_YES == t->buffering)
713 t->max_recv_pid = t->last_recv_pid + INITIAL_WINDOW_SIZE;
715 t->max_recv_pid = t->last_recv_pid + 1;
716 LOG (GNUNET_ERROR_TYPE_DEBUG,
717 "Sending ACK on tunnel %X: %u\n",
718 t->tid, t->max_recv_pid);
719 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
720 msg.header.size = htons (sizeof (msg));
721 msg.tunnel_id = htonl (t->tid);
722 msg.max_pid = htonl (t->max_recv_pid);
725 t->mesh->acks_sent++;
728 send_packet (h, &msg.header, t);
735 * Reconnect callback: tries to reconnect again after a failer previous
737 * @param cls closure (mesh handle)
738 * @param tc task context
741 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
745 * Send a connect packet to the service with the applications and types
746 * requested by the user.
748 * @param h The mesh handle.
752 send_connect (struct GNUNET_MESH_Handle *h)
756 size = sizeof (struct GNUNET_MESH_ClientConnect);
757 size += h->n_handlers * sizeof (uint16_t);
759 char buf[size] GNUNET_ALIGN;
760 struct GNUNET_MESH_ClientConnect *msg;
765 /* build connection packet */
766 msg = (struct GNUNET_MESH_ClientConnect *) buf;
767 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
768 msg->header.size = htons (size);
769 types = (uint16_t *) & apps[napps];
770 for (ntypes = 0; ntypes < h->n_handlers; ntypes++)
772 types[ntypes] = htons (h->message_handlers[ntypes].type);
773 LOG (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
774 h->message_handlers[ntypes].type);
776 msg->applications = htons (napps);
777 msg->types = htons (ntypes);
778 LOG (GNUNET_ERROR_TYPE_DEBUG,
779 "Sending %lu bytes long message %d types and %d apps\n",
780 ntohs (msg->header.size), ntypes, napps);
781 send_packet (h, &msg->header, NULL);
787 * Reconnect to the service, retransmit all infomation to try to restore the
790 * @param h handle to the mesh
792 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
795 do_reconnect (struct GNUNET_MESH_Handle *h)
797 struct GNUNET_MESH_Tunnel *t;
800 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
801 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
802 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
803 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
804 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
809 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
812 if (NULL != h->client)
814 GNUNET_CLIENT_disconnect (h->client);
818 h->client = GNUNET_CLIENT_connect ("mesh", h->cfg);
819 if (h->client == NULL)
821 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
824 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
825 GNUNET_TIME_relative_multiply
826 (h->reconnect_time, 2));
827 LOG (GNUNET_ERROR_TYPE_DEBUG,
828 "Next retry in %s\n",
829 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
836 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
839 /* Rebuild all tunnels */
840 for (t = h->tunnels_head; NULL != t; t = t->next)
842 struct GNUNET_MESH_TunnelMessage tmsg;
843 struct GNUNET_MESH_PeerControl pmsg;
845 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
847 /* Tunnel was created by service (incoming tunnel) */
848 /* TODO: Notify service of missing tunnel, to request
849 * creator to recreate path (find a path to him via DHT?)
853 t->next_send_pid = 0;
854 t->max_send_pid = INITIAL_WINDOW_SIZE - 1;
855 t->last_recv_pid = (uint32_t) -1;
856 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
857 tmsg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
858 tmsg.tunnel_id = htonl (t->tid);
859 send_packet (h, &tmsg.header, t);
861 pmsg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
862 pmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
863 pmsg.tunnel_id = htonl (t->tid);
865 /* Reconnect all peers */
866 /* If the tunnel was "by type", dont connect individual peers */
867 for (i = 0; i < t->npeers && 0 == t->napps; i++)
869 GNUNET_PEER_resolve (t->peers[i]->id, &pmsg.peer);
870 if (NULL != t->disconnect_handler && t->peers[i]->connected)
871 t->disconnect_handler (t->cls, &pmsg.peer);
872 send_packet (t->mesh, &pmsg.header, t);
874 /* Reconnect all types, if any */
875 for (i = 0; i < t->napps; i++)
877 struct GNUNET_MESH_ConnectPeerByType msg;
879 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));
880 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE);
881 msg.tunnel_id = htonl (t->tid);
882 msg.type = htonl (t->apps[i]);
883 send_packet (t->mesh, &msg.header, t);
885 if (GNUNET_NO == t->buffering)
886 GNUNET_MESH_tunnel_buffer (t, GNUNET_NO);
887 if (GNUNET_YES == t->speed_min)
888 GNUNET_MESH_tunnel_speed_min (t);
894 * Reconnect callback: tries to reconnect again after a failer previous
896 * @param cls closure (mesh handle)
897 * @param tc task context
900 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
902 struct GNUNET_MESH_Handle *h = cls;
904 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
905 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
912 * Reconnect to the service, retransmit all infomation to try to restore the
915 * @param h handle to the mesh
917 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
920 reconnect (struct GNUNET_MESH_Handle *h)
922 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requested RECONNECT\n");
923 h->in_receive = GNUNET_NO;
924 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
925 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
930 /******************************************************************************/
931 /*********************** RECEIVE HANDLERS ****************************/
932 /******************************************************************************/
935 * Process the new tunnel notification and add it to the tunnels in the handle
937 * @param h The mesh handle
938 * @param msg A message with the details of the new incoming tunnel
941 process_tunnel_created (struct GNUNET_MESH_Handle *h,
942 const struct GNUNET_MESH_TunnelNotification *msg)
944 struct GNUNET_MESH_Tunnel *t;
945 MESH_TunnelNumber tid;
947 tid = ntohl (msg->tunnel_id);
948 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming tunnel %X\n", tid);
949 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
954 if (NULL != h->new_tunnel)
956 struct GNUNET_ATS_Information atsi;
958 t = create_tunnel (h, tid);
959 t->owner = GNUNET_PEER_intern (&msg->peer);
961 t->peers = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer *));
962 t->peers[0] = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
964 t->peers[0]->connected = 1;
965 t->peers[0]->id = t->owner;
966 GNUNET_PEER_change_rc (t->owner, 1);
969 if ((msg->opt & MESH_TUNNEL_OPT_NOBUFFER) != 0)
970 t->buffering = GNUNET_NO;
972 t->buffering = GNUNET_YES;
973 if ((msg->opt & MESH_TUNNEL_OPT_SPEED_MIN) != 0)
974 t->speed_min = GNUNET_YES;
977 LOG (GNUNET_ERROR_TYPE_DEBUG, " created tunnel %p\n", t);
978 t->ctx = h->new_tunnel (h->cls, t, &msg->peer, &atsi);
979 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
983 struct GNUNET_MESH_TunnelMessage d_msg;
985 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming tunnels\n");
987 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
988 d_msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
989 d_msg.tunnel_id = msg->tunnel_id;
991 send_packet (h, &d_msg.header, NULL);
998 * Process the tunnel destroy notification and free associated resources
1000 * @param h The mesh handle
1001 * @param msg A message with the details of the tunnel being destroyed
1004 process_tunnel_destroy (struct GNUNET_MESH_Handle *h,
1005 const struct GNUNET_MESH_TunnelMessage *msg)
1007 struct GNUNET_MESH_Tunnel *t;
1008 MESH_TunnelNumber tid;
1010 tid = ntohl (msg->tunnel_id);
1011 t = retrieve_tunnel (h, tid);
1021 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X destroyed\n", t->tid);
1022 destroy_tunnel (t, GNUNET_YES);
1028 * Process the new peer event and notify the upper level of it
1030 * @param h The mesh handle
1031 * @param msg A message with the details of the peer event
1034 process_peer_event (struct GNUNET_MESH_Handle *h,
1035 const struct GNUNET_MESH_PeerControl *msg)
1037 struct GNUNET_MESH_Tunnel *t;
1038 struct GNUNET_MESH_Peer *p;
1039 struct GNUNET_ATS_Information atsi;
1043 LOG (GNUNET_ERROR_TYPE_DEBUG, "processig peer event\n");
1044 size = ntohs (msg->header.size);
1045 if (size != sizeof (struct GNUNET_MESH_PeerControl))
1050 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
1056 id = GNUNET_PEER_search (&msg->peer);
1057 if ((p = retrieve_peer (t, id)) == NULL)
1058 p = add_peer_to_tunnel (t, &msg->peer);
1059 if (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD == ntohs (msg->header.type))
1061 LOG (GNUNET_ERROR_TYPE_DEBUG, "adding peer\n");
1062 if (NULL != t->connect_handler)
1066 t->connect_handler (t->cls, &msg->peer, &atsi);
1072 LOG (GNUNET_ERROR_TYPE_DEBUG, "removing peer\n");
1073 if (NULL != t->disconnect_handler && p->connected)
1075 t->disconnect_handler (t->cls, &msg->peer);
1077 remove_peer_from_tunnel (p);
1080 LOG (GNUNET_ERROR_TYPE_DEBUG, "processing peer event END\n");
1085 * Process the incoming data packets
1087 * @param h The mesh handle
1088 * @param message A message encapsulating the data
1090 * @return GNUNET_YES if everything went fine
1091 * GNUNET_NO if client closed connection (h no longer valid)
1094 process_incoming_data (struct GNUNET_MESH_Handle *h,
1095 const struct GNUNET_MessageHeader *message)
1097 const struct GNUNET_MessageHeader *payload;
1098 const struct GNUNET_MESH_MessageHandler *handler;
1099 const struct GNUNET_PeerIdentity *peer;
1100 struct GNUNET_MESH_Unicast *ucast;
1101 struct GNUNET_MESH_Multicast *mcast;
1102 struct GNUNET_MESH_ToOrigin *to_orig;
1103 struct GNUNET_MESH_Tunnel *t;
1108 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
1109 type = ntohs (message->type);
1112 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1113 ucast = (struct GNUNET_MESH_Unicast *) message;
1115 t = retrieve_tunnel (h, ntohl (ucast->tid));
1116 payload = (struct GNUNET_MessageHeader *) &ucast[1];
1118 pid = ntohl (ucast->pid);
1119 LOG (GNUNET_ERROR_TYPE_DEBUG, " ucast on tunnel %s [%X]\n",
1120 GNUNET_i2s (peer), ntohl (ucast->tid));
1122 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1123 mcast = (struct GNUNET_MESH_Multicast *) message;
1124 t = retrieve_tunnel (h, ntohl (mcast->tid));
1125 payload = (struct GNUNET_MessageHeader *) &mcast[1];
1127 pid = ntohl (mcast->pid);
1128 LOG (GNUNET_ERROR_TYPE_DEBUG, " mcast on tunnel %s [%X]\n",
1129 GNUNET_i2s (peer), ntohl (mcast->tid));
1131 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1132 to_orig = (struct GNUNET_MESH_ToOrigin *) message;
1133 t = retrieve_tunnel (h, ntohl (to_orig->tid));
1134 payload = (struct GNUNET_MessageHeader *) &to_orig[1];
1135 peer = &to_orig->sender;
1136 pid = ntohl (to_orig->pid);
1137 LOG (GNUNET_ERROR_TYPE_DEBUG, " torig on tunnel %s [%X]\n",
1138 GNUNET_i2s (peer), ntohl (to_orig->tid));
1144 LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", pid);
1147 /* Tunnel was ignored/destroyed, probably service didn't get it yet */
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
1152 GMC_is_pid_bigger(pid, t->max_recv_pid))
1155 LOG (GNUNET_ERROR_TYPE_WARNING,
1156 " unauthorized message! (%u, max %u)\n",
1157 pid, t->max_recv_pid);
1158 // FIXME fc what now? accept? reject?
1161 t->last_recv_pid = pid;
1162 type = ntohs (payload->type);
1164 for (i = 0; i < h->n_handlers; i++)
1166 handler = &h->message_handlers[i];
1167 if (handler->type == type)
1169 struct GNUNET_ATS_Information atsi;
1174 handler->callback (h->cls, t, &t->ctx, peer, payload, &atsi))
1176 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
1177 GNUNET_MESH_disconnect (h);
1182 LOG (GNUNET_ERROR_TYPE_DEBUG,
1183 "callback completed successfully\n");
1192 * Process a local ACK message, enabling the client to send
1193 * more data to the service.
1195 * @param h Mesh handle.
1196 * @param message Message itself.
1199 process_ack (struct GNUNET_MESH_Handle *h,
1200 const struct GNUNET_MessageHeader *message)
1202 struct GNUNET_MESH_LocalAck *msg;
1203 struct GNUNET_MESH_Tunnel *t;
1206 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
1208 msg = (struct GNUNET_MESH_LocalAck *) message;
1210 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
1214 LOG (GNUNET_ERROR_TYPE_WARNING,
1215 "ACK on unknown tunnel %X\n",
1216 ntohl (msg->tunnel_id));
1219 ack = ntohl (msg->max_pid);
1220 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X, ack %u!\n", t->tid, ack);
1221 if (GNUNET_YES == GMC_is_pid_bigger(ack, t->max_send_pid))
1222 t->max_send_pid = ack;
1225 if (NULL == h->th && 0 < t->packet_size)
1227 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n", t->tid, ack);
1229 GNUNET_CLIENT_notify_transmit_ready (h->client, t->packet_size,
1230 GNUNET_TIME_UNIT_FOREVER_REL,
1231 GNUNET_YES, &send_callback, h);
1237 * Process a local reply about info on all tunnels, pass info to the user.
1239 * @param h Mesh handle.
1240 * @param message Message itself.
1243 process_get_tunnels (struct GNUNET_MESH_Handle *h,
1244 const struct GNUNET_MessageHeader *message)
1246 struct GNUNET_MESH_LocalMonitor *msg;
1249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Tunnels messasge received\n");
1251 if (NULL == h->tunnels_cb)
1253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1257 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1258 npeers = ntohl (msg->npeers);
1259 if (ntohs (message->size) !=
1260 (sizeof (struct GNUNET_MESH_LocalMonitor) +
1261 npeers * sizeof (struct GNUNET_PeerIdentity)))
1263 GNUNET_break_op (0);
1264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1265 "Get tunnels message: size %hu - expected %u (%u peers)\n",
1266 ntohs (message->size),
1267 sizeof (struct GNUNET_MESH_LocalMonitor) +
1268 npeers * sizeof (struct GNUNET_PeerIdentity),
1272 h->tunnels_cb (h->tunnels_cls,
1274 ntohl (msg->tunnel_id),
1275 (struct GNUNET_PeerIdentity *) &msg[1],
1282 * Process a local monitor_tunnel reply, pass info to the user.
1284 * @param h Mesh handle.
1285 * @param message Message itself.
1288 process_show_tunnel (struct GNUNET_MESH_Handle *h,
1289 const struct GNUNET_MessageHeader *message)
1291 struct GNUNET_MESH_LocalMonitor *msg;
1292 struct GNUNET_PeerIdentity *new_peers;
1293 uint32_t *new_parents;
1298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Tunnel messasge received\n");
1300 if (NULL == h->tunnel_cb)
1302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1306 /* Verify message sanity */
1307 msg = (struct GNUNET_MESH_LocalMonitor *) message;
1308 npeers = ntohl (msg->npeers);
1309 esize = sizeof (struct GNUNET_MESH_LocalMonitor);
1310 esize += npeers * (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t));
1311 if (ntohs (message->size) != esize)
1313 GNUNET_break_op (0);
1314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1315 "Show tunnel message: size %hu - expected %u (%u peers)\n",
1316 ntohs (message->size),
1320 h->tunnel_cb (h->tunnel_cls, NULL, NULL);
1321 h->tunnel_cb = NULL;
1322 h->tunnel_cls = NULL;
1323 h->tunnel_npeers = 0;
1324 GNUNET_free_non_null (h->peers);
1330 new_peers = (struct GNUNET_PeerIdentity *) &msg[1];
1331 new_parents = (uint32_t *) &new_peers[npeers];
1333 h->peers = GNUNET_realloc (h->peers, h->tunnel_npeers + npeers);
1334 memcpy (&h->peers[h->tunnel_npeers],
1336 npeers * sizeof (struct GNUNET_PeerIdentity));
1337 h->tunnel_npeers += npeers;
1338 for (i = 0; i < npeers; i++)
1339 h->tunnel_cb (h->tunnel_cls,
1341 &h->peers[new_parents[i]]);
1346 * Function to process all messages received from the service
1348 * @param cls closure
1349 * @param msg message received, NULL on timeout or fatal error
1352 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1354 struct GNUNET_MESH_Handle *h = cls;
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "Mesh service disconnected, reconnecting\n", h);
1363 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n",
1364 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1365 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1366 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1367 switch (ntohs (msg->type))
1369 /* Notify of a new incoming tunnel */
1370 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
1371 process_tunnel_created (h, (struct GNUNET_MESH_TunnelNotification *) msg);
1373 /* Notify of a tunnel disconnection */
1374 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1375 process_tunnel_destroy (h, (struct GNUNET_MESH_TunnelMessage *) msg);
1377 /* Notify of a new peer or a peer disconnect in the tunnel */
1378 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD:
1379 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL:
1380 process_peer_event (h, (struct GNUNET_MESH_PeerControl *) msg);
1382 /* Notify of a new data packet in the tunnel */
1383 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1384 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1385 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1386 if (GNUNET_NO == process_incoming_data (h, msg))
1389 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
1390 process_ack (h, msg);
1392 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1393 process_get_tunnels (h, msg);
1395 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1396 process_show_tunnel (h, msg);
1399 /* We shouldn't get any other packages, log and ignore */
1400 LOG (GNUNET_ERROR_TYPE_WARNING,
1401 "unsolicited message form service (type %s)\n",
1402 GNUNET_MESH_DEBUG_M2S (ntohs (msg->type)));
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1405 if (GNUNET_YES == h->in_receive)
1407 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1408 GNUNET_TIME_UNIT_FOREVER_REL);
1412 LOG (GNUNET_ERROR_TYPE_DEBUG,
1413 "in receive off, not calling CLIENT_receive\n");
1418 /******************************************************************************/
1419 /************************ SEND FUNCTIONS ****************************/
1420 /******************************************************************************/
1423 * Function called to send a message to the service.
1424 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1427 * @param cls closure, the mesh handle
1428 * @param size number of bytes available in buf
1429 * @param buf where the callee should write the connect message
1430 * @return number of bytes written to buf
1433 send_callback (void *cls, size_t size, void *buf)
1435 struct GNUNET_MESH_Handle *h = cls;
1436 struct GNUNET_MESH_TransmitHandle *th;
1437 struct GNUNET_MESH_TransmitHandle *next;
1438 struct GNUNET_MESH_Tunnel *t;
1444 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1445 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() Buffer %u\n", size);
1446 if ((0 == size) || (NULL == buf))
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NULL send callback on %p\n", h);
1455 nsize = message_ready_size (h);
1456 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1459 if (GNUNET_YES == th_is_payload (th))
1461 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload\n");
1462 if (GNUNET_YES == GMC_is_pid_bigger(t->next_send_pid, t->max_send_pid))
1464 /* This tunnel is not ready to transmit yet, try next message */
1469 if (t->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1471 /* traffic to origin */
1472 struct GNUNET_MESH_ToOrigin to;
1473 struct GNUNET_MessageHeader *mh;
1475 GNUNET_assert (size >= th->size);
1476 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (to)];
1477 psize = th->notify (th->notify_cls, size - sizeof (to), mh);
1478 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin, type %s\n",
1479 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1482 psize += sizeof (to);
1483 GNUNET_assert (size >= psize);
1484 to.header.size = htons (psize);
1485 to.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
1486 to.tid = htonl (t->tid);
1487 to.pid = htonl (t->next_send_pid);
1489 memset (&to.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1490 memset (&to.sender, 0, sizeof (struct GNUNET_PeerIdentity));
1491 memcpy (cbuf, &to, sizeof (to));
1494 else if (th->target == 0)
1497 struct GNUNET_MESH_Multicast mc;
1498 struct GNUNET_MessageHeader *mh;
1500 GNUNET_assert (size >= th->size);
1501 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (mc)];
1502 psize = th->notify (th->notify_cls, size - sizeof (mc), mh);
1503 LOG (GNUNET_ERROR_TYPE_DEBUG, " multicast, type %s\n",
1504 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1507 psize += sizeof (mc);
1508 GNUNET_assert (size >= psize);
1509 mc.header.size = htons (psize);
1510 mc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
1511 mc.tid = htonl (t->tid);
1512 mc.pid = htonl (t->next_send_pid);
1514 memset (&mc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1515 memcpy (cbuf, &mc, sizeof (mc));
1521 struct GNUNET_MESH_Unicast uc;
1522 struct GNUNET_MessageHeader *mh;
1524 GNUNET_assert (size >= th->size);
1525 mh = (struct GNUNET_MessageHeader *) &cbuf[sizeof (uc)];
1526 psize = th->notify (th->notify_cls, size - sizeof (uc), mh);
1527 LOG (GNUNET_ERROR_TYPE_DEBUG, " unicast, type %s\n",
1528 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1531 psize += sizeof (uc);
1532 GNUNET_assert (size >= psize);
1533 uc.header.size = htons (psize);
1534 uc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_UNICAST);
1535 uc.tid = htonl (t->tid);
1536 uc.pid = htonl (t->next_send_pid);
1538 memset (&uc.oid, 0, sizeof (struct GNUNET_PeerIdentity));
1539 GNUNET_PEER_resolve (th->target, &uc.destination);
1540 memcpy (cbuf, &uc, sizeof (uc));
1547 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1549 LOG (GNUNET_ERROR_TYPE_DEBUG, " mesh traffic, type %s\n",
1550 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
1551 memcpy (cbuf, &th[1], th->size);
1554 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1555 GNUNET_SCHEDULER_cancel (th->timeout_task);
1556 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1559 nsize = message_ready_size (h);
1564 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size: %u\n", tsize);
1566 size = message_ready_size (h);
1569 LOG (GNUNET_ERROR_TYPE_DEBUG, " next size: %u\n", size);
1571 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1572 GNUNET_TIME_UNIT_FOREVER_REL,
1573 GNUNET_YES, &send_callback, h);
1577 if (NULL != h->th_head)
1578 LOG (GNUNET_ERROR_TYPE_DEBUG, " can't transmit any more\n");
1580 LOG (GNUNET_ERROR_TYPE_DEBUG, " nothing left to transmit\n");
1582 if (GNUNET_NO == h->in_receive)
1584 LOG (GNUNET_ERROR_TYPE_DEBUG, " start receiving from service\n");
1585 h->in_receive = GNUNET_YES;
1586 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1587 GNUNET_TIME_UNIT_FOREVER_REL);
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, "Send packet() END\n");
1595 * Auxiliary function to send an already constructed packet to the service.
1596 * Takes care of creating a new queue element, copying the message and
1597 * calling the tmt_rdy function if necessary.
1599 * @param h mesh handle
1600 * @param msg message to transmit
1601 * @param tunnel tunnel this send is related to (NULL if N/A)
1604 send_packet (struct GNUNET_MESH_Handle *h,
1605 const struct GNUNET_MessageHeader *msg,
1606 struct GNUNET_MESH_Tunnel *tunnel)
1608 struct GNUNET_MESH_TransmitHandle *th;
1611 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1612 GNUNET_MESH_DEBUG_M2S(ntohs(msg->type)));
1613 msize = ntohs (msg->size);
1614 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle) + msize);
1615 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1617 th->tunnel = tunnel;
1618 memcpy (&th[1], msg, msize);
1619 add_to_queue (h, th);
1620 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1623 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1625 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1626 GNUNET_TIME_UNIT_FOREVER_REL,
1627 GNUNET_YES, &send_callback, h);
1631 /******************************************************************************/
1632 /********************** API CALL DEFINITIONS *************************/
1633 /******************************************************************************/
1636 * Connect to the mesh service.
1638 * @param cfg configuration to use
1639 * @param cls closure for the various callbacks that follow
1640 * (including handlers in the handlers array)
1641 * @param new_tunnel function called when an *inbound* tunnel is created
1642 * @param cleaner function called when an *inbound* tunnel is destroyed by the
1643 * remote peer, it is *not* called if GNUNET_MESH_tunnel_destroy
1644 * is called on the tunnel
1645 * @param handlers callbacks for messages we care about, NULL-terminated
1646 * note that the mesh is allowed to drop notifications about
1647 * inbound messages if the client does not process them fast
1648 * enough (for this notification type, a bounded queue is used)
1649 * @param stypes list of the applications that this client claims to provide
1650 * @return handle to the mesh service NULL on error
1651 * (in this case, init is never called)
1653 struct GNUNET_MESH_Handle *
1654 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1655 GNUNET_MESH_InboundTunnelNotificationHandler new_tunnel,
1656 GNUNET_MESH_TunnelEndHandler cleaner,
1657 const struct GNUNET_MESH_MessageHandler *handlers,
1658 const GNUNET_MESH_ApplicationType *stypes)
1660 struct GNUNET_MESH_Handle *h;
1663 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect()\n");
1664 h = GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle));
1665 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1667 h->new_tunnel = new_tunnel;
1668 h->cleaner = cleaner;
1669 h->client = GNUNET_CLIENT_connect ("mesh", cfg);
1670 if (h->client == NULL)
1677 h->message_handlers = handlers;
1678 h->next_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
1679 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1680 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1683 for (h->n_applications = 0;
1684 stypes && stypes[h->n_applications];
1685 h->n_applications++) ;
1686 if (0 < h->n_applications)
1688 size = h->n_applications * sizeof (GNUNET_MESH_ApplicationType *);
1689 h->applications = GNUNET_malloc (size);
1690 memcpy (h->applications, stypes, size);
1692 /* count handlers */
1693 for (h->n_handlers = 0;
1694 handlers && handlers[h->n_handlers].type;
1697 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_MESH_connect() END\n");
1703 * Disconnect from the mesh service. All tunnels will be destroyed. All tunnel
1704 * disconnect callbacks will be called on any still connected peers, notifying
1705 * about their disconnection. The registered inbound tunnel cleaner will be
1706 * called should any inbound tunnels still exist.
1708 * @param handle connection to mesh to disconnect
1711 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
1713 struct GNUNET_MESH_Tunnel *t;
1714 struct GNUNET_MESH_Tunnel *aux;
1715 struct GNUNET_MESH_TransmitHandle *th;
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH DISCONNECT\n");
1720 LOG (GNUNET_ERROR_TYPE_INFO, "Sent %d ACKs\n", handle->acks_sent);
1721 LOG (GNUNET_ERROR_TYPE_INFO, "Recv %d ACKs\n\n", handle->acks_recv);
1724 t = handle->tunnels_head;
1728 if (t->tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1731 LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel %X not destroyed\n", t->tid);
1733 destroy_tunnel (t, GNUNET_YES);
1736 while ( (th = handle->th_head) != NULL)
1738 struct GNUNET_MessageHeader *msg;
1740 /* Make sure it is an allowed packet (everything else should have been
1741 * already canceled).
1743 GNUNET_break (GNUNET_NO == th_is_payload (th));
1744 msg = (struct GNUNET_MessageHeader *) &th[1];
1745 switch (ntohs(msg->type))
1747 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
1748 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY:
1749 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
1750 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
1754 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1758 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1762 if (NULL != handle->th)
1764 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1767 if (NULL != handle->client)
1769 GNUNET_CLIENT_disconnect (handle->client);
1770 handle->client = NULL;
1772 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1774 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1775 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1777 GNUNET_free_non_null (handle->applications);
1778 GNUNET_free (handle);
1783 * Announce to ther peer the availability of services described by the regex,
1784 * in order to be reachable to other peers via connect_by_string.
1786 * Note that the first 8 characters are considered to be part of a prefix,
1787 * (for instance 'gnunet://'). If you put a variable part in there (*, +. ()),
1788 * all matching strings will be stored in the DHT.
1790 * @param h Handle to mesh.
1791 * @param regex String with the regular expression describing local services.
1792 * @param compression_characters How many characters can be assigned to one
1793 * edge of the graph. The bigger the variability
1794 * of the data, the smaller this parameter should
1796 * For maximum compression, use strlen (regex)
1797 * or 0 (special value). Use with care!
1800 GNUNET_MESH_announce_regex (struct GNUNET_MESH_Handle *h,
1802 unsigned int compression_characters)
1804 struct GNUNET_MESH_RegexAnnounce *msg;
1809 char buffer[UINT16_MAX];
1811 len = strlen (regex);
1812 payload = UINT16_MAX - sizeof(struct GNUNET_MESH_RegexAnnounce);
1813 msg = (struct GNUNET_MESH_RegexAnnounce *) buffer;
1814 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX);
1815 msg->compression_characters = htons (compression_characters);
1819 msgsize = (len - offset > payload) ? payload : len - offset;
1820 memcpy (&msg[1], ®ex[offset], msgsize);
1822 msgsize += sizeof(struct GNUNET_MESH_RegexAnnounce);
1824 msg->header.size = htons (msgsize);
1825 msg->last = htons (offset >= len);
1827 send_packet (h, &msg->header, NULL);
1828 } while (len > offset);
1832 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
1833 * and to broadcast).
1835 * @param h mesh handle
1836 * @param tunnel_ctx client's tunnel context to associate with the tunnel
1837 * @param connect_handler function to call when peers are actually connected
1838 * @param disconnect_handler function to call when peers are disconnected
1839 * @param handler_cls closure for connect/disconnect handlers
1841 struct GNUNET_MESH_Tunnel *
1842 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h, void *tunnel_ctx,
1843 GNUNET_MESH_PeerConnectHandler connect_handler,
1844 GNUNET_MESH_PeerDisconnectHandler disconnect_handler,
1847 struct GNUNET_MESH_Tunnel *t;
1848 struct GNUNET_MESH_TunnelMessage msg;
1850 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new tunnel\n");
1851 t = create_tunnel (h, 0);
1852 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", t);
1853 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", t->tid);
1854 t->connect_handler = connect_handler;
1855 t->disconnect_handler = disconnect_handler;
1856 t->cls = handler_cls;
1857 t->ctx = tunnel_ctx;
1858 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1859 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1860 msg.tunnel_id = htonl (t->tid);
1861 send_packet (h, &msg.header, t);
1867 * Destroy an existing tunnel. The existing callback for the tunnel will NOT
1870 * @param tunnel tunnel handle
1873 GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tunnel)
1875 struct GNUNET_MESH_Handle *h;
1876 struct GNUNET_MESH_TunnelMessage msg;
1877 struct GNUNET_MESH_TransmitHandle *th;
1879 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying tunnel\n");
1882 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1883 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1884 msg.tunnel_id = htonl (tunnel->tid);
1888 struct GNUNET_MESH_TransmitHandle *aux;
1889 if (th->tunnel == tunnel)
1892 /* FIXME call the handler? */
1893 if (GNUNET_YES == th_is_payload (th))
1894 th->notify (th->notify_cls, 0, NULL);
1895 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1903 destroy_tunnel (tunnel, GNUNET_NO);
1904 send_packet (h, &msg.header, NULL);
1908 * Request that the tunnel data rate is limited to the speed of the slowest
1911 * @param tunnel Tunnel affected.
1914 GNUNET_MESH_tunnel_speed_min (struct GNUNET_MESH_Tunnel *tunnel)
1916 struct GNUNET_MESH_TunnelMessage msg;
1917 struct GNUNET_MESH_Handle *h;
1920 tunnel->speed_min = GNUNET_YES;
1922 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN);
1923 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1924 msg.tunnel_id = htonl (tunnel->tid);
1926 send_packet (h, &msg.header, NULL);
1931 * Request that the tunnel data rate is limited to the speed of the fastest
1932 * receiver. This is the default behavior.
1934 * @param tunnel Tunnel affected.
1937 GNUNET_MESH_tunnel_speed_max (struct GNUNET_MESH_Tunnel *tunnel)
1939 struct GNUNET_MESH_TunnelMessage msg;
1940 struct GNUNET_MESH_Handle *h;
1943 tunnel->speed_min = GNUNET_NO;
1945 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX);
1946 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1947 msg.tunnel_id = htonl (tunnel->tid);
1949 send_packet (h, &msg.header, NULL);
1953 * Turn on/off the buffering status of the tunnel.
1955 * @param tunnel Tunnel affected.
1956 * @param buffer GNUNET_YES to turn buffering on (default),
1957 * GNUNET_NO otherwise.
1960 GNUNET_MESH_tunnel_buffer (struct GNUNET_MESH_Tunnel *tunnel, int buffer)
1962 struct GNUNET_MESH_TunnelMessage msg;
1963 struct GNUNET_MESH_Handle *h;
1966 tunnel->buffering = buffer;
1967 tunnel->max_send_pid = tunnel->next_send_pid;
1969 if (GNUNET_YES == buffer)
1970 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER);
1972 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER);
1973 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1974 msg.tunnel_id = htonl (tunnel->tid);
1976 send_packet (h, &msg.header, NULL);
1981 * Request that a peer should be added to the tunnel. The existing
1982 * connect handler will be called ONCE with either success or failure.
1983 * This function should NOT be called again with the same peer before the
1984 * connect handler is called.
1985 * FIXME: I think the above documentation is false. I think it should
1986 * read: "The connect handler will be called once the peer was actually
1987 * successfully added to the multicast group. This function should
1988 * not be called twice for the same peer (unless, of course,
1989 * the peer was removed using GNUNET_MESH_peer_Request_connect_del in
1992 * @param tunnel handle to existing tunnel
1993 * @param peer peer to add
1996 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
1997 const struct GNUNET_PeerIdentity *peer)
1999 struct GNUNET_MESH_PeerControl msg;
2000 GNUNET_PEER_Id peer_id;
2003 peer_id = GNUNET_PEER_intern (peer);
2004 for (i = 0; i < tunnel->npeers; i++)
2006 if (tunnel->peers[i]->id == peer_id)
2008 /* Peer already exists in tunnel */
2009 GNUNET_PEER_change_rc (peer_id, -1);
2014 if (NULL == add_peer_to_tunnel (tunnel, peer))
2017 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2018 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2019 msg.tunnel_id = htonl (tunnel->tid);
2021 send_packet (tunnel->mesh, &msg.header, tunnel);
2026 * Request that a peer should be removed from the tunnel. The existing
2027 * disconnect handler will be called ONCE if we were connected.
2029 * @param tunnel handle to existing tunnel
2030 * @param peer peer to remove
2033 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
2034 const struct GNUNET_PeerIdentity *peer)
2036 struct GNUNET_MESH_PeerControl msg;
2037 GNUNET_PEER_Id peer_id;
2040 peer_id = GNUNET_PEER_search (peer);
2046 for (i = 0; i < tunnel->npeers; i++)
2047 if (tunnel->peers[i]->id == peer_id)
2049 if (i == tunnel->npeers)
2054 if (NULL != tunnel->disconnect_handler && tunnel->peers[i]->connected == 1)
2055 tunnel->disconnect_handler (tunnel->cls, peer);
2056 GNUNET_PEER_change_rc (peer_id, -1);
2057 GNUNET_free (tunnel->peers[i]);
2058 tunnel->peers[i] = tunnel->peers[tunnel->npeers - 1];
2059 GNUNET_array_grow (tunnel->peers, tunnel->npeers, tunnel->npeers - 1);
2061 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2062 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2063 msg.tunnel_id = htonl (tunnel->tid);
2064 memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
2065 send_packet (tunnel->mesh, &msg.header, tunnel);
2069 * Request that the mesh should try to connect to a peer matching the
2070 * description given in the service string.
2072 * FIXME: allow multiple? how to deal with reconnect?
2074 * @param tunnel handle to existing tunnel
2075 * @param description string describing the destination node requirements
2078 GNUNET_MESH_peer_request_connect_by_string (struct GNUNET_MESH_Tunnel *tunnel,
2079 const char *description)
2081 struct GNUNET_MESH_ConnectPeerByString *m;
2085 len = strlen (description);
2086 msgsize = sizeof(struct GNUNET_MESH_ConnectPeerByString) + len;
2087 GNUNET_assert (UINT16_MAX > msgsize);
2089 char buffer[msgsize];
2091 m = (struct GNUNET_MESH_ConnectPeerByString *) buffer;
2092 m->header.size = htons (msgsize);
2093 m->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING);
2094 m->tunnel_id = htonl (tunnel->tid);
2095 memcpy(&m[1], description, len);
2097 send_packet (tunnel->mesh, &m->header, tunnel);
2103 * Request that the given peer isn't added to this tunnel in calls to
2104 * connect_by_* calls, (due to misbehaviour, bad performance, ...).
2106 * @param tunnel handle to existing tunnel.
2107 * @param peer peer identity of the peer which should be blacklisted
2111 GNUNET_MESH_peer_blacklist (struct GNUNET_MESH_Tunnel *tunnel,
2112 const struct GNUNET_PeerIdentity *peer)
2114 struct GNUNET_MESH_PeerControl msg;
2116 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2117 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST);
2118 msg.tunnel_id = htonl (tunnel->tid);
2120 send_packet (tunnel->mesh, &msg.header, tunnel);
2127 * Request that the given peer isn't blacklisted anymore from this tunnel,
2128 * and therefore can be added in future calls to connect_by_*.
2129 * The peer must have been previously blacklisted for this tunnel.
2131 * @param tunnel handle to existing tunnel.
2132 * @param peer peer identity of the peer which shouldn't be blacklisted
2133 * for the tunnel anymore.
2136 GNUNET_MESH_peer_unblacklist (struct GNUNET_MESH_Tunnel *tunnel,
2137 const struct GNUNET_PeerIdentity *peer)
2139 struct GNUNET_MESH_PeerControl msg;
2141 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2142 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST);
2143 msg.tunnel_id = htonl (tunnel->tid);
2145 send_packet (tunnel->mesh, &msg.header, tunnel);
2151 struct GNUNET_MESH_TransmitHandle *
2152 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel, int cork,
2153 struct GNUNET_TIME_Relative maxdelay,
2155 GNUNET_CONNECTION_TransmitReadyNotify notify,
2158 struct GNUNET_MESH_TransmitHandle *th;
2161 GNUNET_assert (NULL != tunnel);
2162 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY\n");
2163 LOG (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tunnel->tid);
2164 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2165 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
2167 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
2168 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
2169 GNUNET_assert (NULL != notify);
2170 GNUNET_assert (0 == tunnel->packet_size); // Only one data packet allowed
2171 th = GNUNET_malloc (sizeof (struct GNUNET_MESH_TransmitHandle));
2172 th->tunnel = tunnel;
2173 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
2174 th->target = GNUNET_PEER_intern (target);
2175 if (tunnel->tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2176 overhead = sizeof (struct GNUNET_MESH_ToOrigin);
2177 else if (NULL == target)
2178 overhead = sizeof (struct GNUNET_MESH_Multicast);
2180 overhead = sizeof (struct GNUNET_MESH_Unicast);
2181 tunnel->packet_size = th->size = notify_size + overhead;
2182 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
2183 th->notify = notify;
2184 th->notify_cls = notify_cls;
2185 add_to_queue (tunnel->mesh, th);
2186 if (NULL != tunnel->mesh->th)
2188 if (GMC_is_pid_bigger(tunnel->next_send_pid, tunnel->max_send_pid))
2190 LOG (GNUNET_ERROR_TYPE_DEBUG, " call notify tmt rdy\n");
2192 GNUNET_CLIENT_notify_transmit_ready (tunnel->mesh->client, th->size,
2193 GNUNET_TIME_UNIT_FOREVER_REL,
2194 GNUNET_YES, &send_callback,
2196 LOG (GNUNET_ERROR_TYPE_DEBUG, "MESH NOTIFY TRANSMIT READY END\n");
2202 * Cancel the specified transmission-ready notification.
2204 * @param th handle that was returned by "notify_transmit_ready".
2207 GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th)
2209 struct GNUNET_MESH_Handle *mesh;
2211 th->tunnel->packet_size = 0;
2212 mesh = th->tunnel->mesh;
2213 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
2214 GNUNET_SCHEDULER_cancel (th->timeout_task);
2215 GNUNET_CONTAINER_DLL_remove (mesh->th_head, mesh->th_tail, th);
2217 if ((0 == message_ready_size (mesh)) && (NULL != mesh->th))
2219 /* queue empty, no point in asking for transmission */
2220 GNUNET_CLIENT_notify_transmit_ready_cancel (mesh->th);
2227 * Request information about the running mesh peer.
2228 * The callback will be called for every tunnel known to the service,
2229 * listing all active peers that blong to the tunnel.
2231 * If called again on the same handle, it will overwrite the previous
2232 * callback and cls. To retrieve the cls, monitor_cancel must be
2235 * WARNING: unstable API, likely to change in the future!
2237 * @param h Handle to the mesh peer.
2238 * @param callback Function to call with the requested data.
2239 * @param callback_cls Closure for @c callback.
2242 GNUNET_MESH_get_tunnels (struct GNUNET_MESH_Handle *h,
2243 GNUNET_MESH_TunnelsCB callback,
2246 struct GNUNET_MessageHeader msg;
2248 msg.size = htons (sizeof (msg));
2249 msg.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
2250 send_packet (h, &msg, NULL);
2251 h->tunnels_cb = callback;
2252 h->tunnels_cls = callback_cls;
2259 * Cancel a monitor request. The monitor callback will not be called.
2261 * @param h Mesh handle.
2263 * @return Closure given to GNUNET_MESH_monitor, if any.
2266 GNUNET_MESH_get_tunnels_cancel (struct GNUNET_MESH_Handle *h)
2270 cls = h->tunnels_cls;
2271 h->tunnels_cb = NULL;
2272 h->tunnels_cls = NULL;
2278 * Request information about a specific tunnel of the running mesh peer.
2280 * WARNING: unstable API, likely to change in the future!
2282 * @param h Handle to the mesh peer.
2283 * @param initiator ID of the owner of the tunnel.
2284 * @param tunnel_number Tunnel number.
2285 * @param callback Function to call with the requested data.
2286 * @param callback_cls Closure for @c callback.
2289 GNUNET_MESH_show_tunnel (struct GNUNET_MESH_Handle *h,
2290 struct GNUNET_PeerIdentity *initiator,
2291 unsigned int tunnel_number,
2292 GNUNET_MESH_TunnelCB callback,
2295 struct GNUNET_MESH_LocalMonitor msg;
2297 msg.header.size = htons (sizeof (msg));
2298 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
2299 msg.npeers = htonl (0);
2300 msg.owner = *initiator;
2301 msg.tunnel_id = htonl (tunnel_number);
2303 send_packet (h, &msg.header, NULL);
2304 h->tunnel_cb = callback;
2305 h->tunnel_cls = callback_cls;
2312 * Transition API for tunnel ctx management
2315 GNUNET_MESH_tunnel_set_data (struct GNUNET_MESH_Tunnel *tunnel, void *data)
2321 * Transition API for tunnel ctx management
2324 GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel)