2 This file is part of GNUnet.
3 (C) 2013 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.
24 #include "mesh_protocol_enc.h" // GNUNET_MESH_Data is shared
25 #include "gnunet-service-mesh_local.h"
27 /******************************************************************************/
28 /******************************** STRUCTS **********************************/
29 /******************************************************************************/
32 * Struct containing information about a client of the service
34 * TODO: add a list of 'waiting' ports
41 struct MeshClient *next;
46 struct MeshClient *prev;
49 * Tunnels that belong to this client, indexed by local id
51 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
54 * Tunnels this client has accepted, indexed by incoming local id
56 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
59 * Handle to communicate with the client
61 struct GNUNET_SERVER_Client *handle;
64 * Ports that this client has declared interest in.
65 * Indexed by port, contains *Client.
67 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
70 * Whether the client is active or shutting down (don't send confirmations
71 * to a client that is shutting down.
76 * ID of the client, mainly for debug messages
81 /******************************************************************************/
82 /******************************* GLOBALS ***********************************/
83 /******************************************************************************/
86 * Handle to server lib.
88 static struct GNUNET_SERVER_Handle *server_handle;
91 * DLL with all the clients, head.
93 static struct MeshClient *clients_head;
96 * DLL with all the clients, tail.
98 static struct MeshClient *clients_tail;
101 * Next ID to assign to a client.
103 unsigned int next_client_id;
106 * All ports clients of this peer have opened.
108 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
111 * Notification context, to send messages to local clients.
113 static struct GNUNET_SERVER_NotificationContext *nc;
116 /******************************************************************************/
117 /******************************** STATIC ***********************************/
118 /******************************************************************************/
121 * Remove client's ports from the global hashmap on disconnect.
123 * @param cls Closure (unused).
125 * @param value Client structure.
127 * @return GNUNET_OK, keep iterating.
130 client_release_ports (void *cls,
136 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
137 if (GNUNET_YES != res)
140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
141 "Port %u by client %p was not registered.\n",
149 /******************************************************************************/
150 /******************************** HANDLES ***********************************/
151 /******************************************************************************/
155 * Handler for client connection.
157 * @param cls Closure (unused).
158 * @param client Client handler.
161 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
163 struct MeshClient *c;
167 c = GNUNET_malloc (sizeof (struct MeshClient));
169 c->id = next_client_id++; /* overflow not important: just for debug */
170 GNUNET_SERVER_client_keep (client);
171 GNUNET_SERVER_client_set_user_context (client, c);
172 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
177 * Handler for client disconnection
180 * @param client identification of the client; NULL
181 * for the last call when the server is destroyed
184 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
186 struct MeshClient *c;
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
195 c = client_get (client);
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
200 GNUNET_SERVER_client_drop (c->handle);
201 c->shutting_down = GNUNET_YES;
202 if (NULL != c->own_channels)
204 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
205 &channel_destroy_iterator, c);
206 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
209 if (NULL != c->incoming_channels)
211 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
212 &channel_destroy_iterator, c);
213 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
216 if (NULL != c->ports)
218 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
219 &client_release_ports, c);
220 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
222 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
223 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
229 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
237 * Handler for new clients
240 * @param client identification of the client
241 * @param message the actual message, which includes messages the client wants
244 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
245 const struct GNUNET_MessageHeader *message)
247 struct GNUNET_MESH_ClientConnect *cc_msg;
248 struct MeshClient *c;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
255 /* Check data sanity */
256 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
257 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
258 if (0 != (size % sizeof (uint32_t)))
261 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
264 size /= sizeof (uint32_t);
266 /* Initialize new client structure */
267 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
274 p = (uint32_t *) &cc_msg[1];
275 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
276 for (i = 0; i < size; i++)
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
281 /* store in client's hashmap */
282 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
283 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
284 /* store in global hashmap */
285 /* FIXME only allow one client to have the port open,
286 * have a backup hashmap with waiting clients */
287 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
288 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
292 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
293 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
294 GNUNET_SERVER_notification_context_add (nc, client);
295 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
297 GNUNET_SERVER_receive_done (client, GNUNET_OK);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
303 * Handler for requests of new tunnels
305 * @param cls Closure.
306 * @param client Identification of the client.
307 * @param message The actual message.
310 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
311 const struct GNUNET_MessageHeader *message)
313 struct GNUNET_MESH_ChannelMessage *msg;
314 struct MeshPeer *peer;
315 struct MeshTunnel2 *t;
316 struct MeshChannel *ch;
317 struct MeshClient *c;
318 MESH_ChannelNumber chid;
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
322 /* Sanity check for client registration */
323 if (NULL == (c = client_get (client)))
326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
331 /* Message size sanity check */
332 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
335 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
339 msg = (struct GNUNET_MESH_ChannelMessage *) message;
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
341 GNUNET_i2s (&msg->peer), ntohl (msg->port));
342 chid = ntohl (msg->channel_id);
344 /* Sanity check for duplicate channel IDs */
345 if (NULL != channel_get_by_local_id (c, chid))
348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
352 peer = peer_get (&msg->peer);
353 if (NULL == peer->tunnel)
355 peer->tunnel = tunnel_new ();
356 peer->tunnel->peer = peer;
357 if (peer->id == myid)
359 tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY);
369 ch = channel_new (t, c, chid);
373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
376 ch->port = ntohl (msg->port);
377 channel_set_options (ch, ntohl (msg->opt));
379 /* In unreliable channels, we'll use the DLL to buffer BCK data */
380 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
381 ch->root_rel->ch = ch;
382 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
385 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
387 /* Send create channel */
389 struct GNUNET_MESH_ChannelCreate msgcc;
391 msgcc.header.size = htons (sizeof (msgcc));
392 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
393 msgcc.chid = htonl (ch->gid);
394 msgcc.port = msg->port;
395 msgcc.opt = msg->opt;
397 tunnel_queue_data (t, ch, &msgcc.header, GNUNET_YES);
400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
406 * Handler for requests of deleting tunnels
409 * @param client identification of the client
410 * @param message the actual message
413 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
414 const struct GNUNET_MessageHeader *message)
416 struct GNUNET_MESH_ChannelMessage *msg;
417 struct MeshClient *c;
418 struct MeshChannel *ch;
419 struct MeshTunnel2 *t;
420 MESH_ChannelNumber chid;
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Got a DESTROY CHANNEL from client!\n");
425 /* Sanity check for client registration */
426 if (NULL == (c = client_get (client)))
429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
434 /* Message sanity check */
435 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
442 msg = (struct GNUNET_MESH_ChannelMessage *) message;
444 /* Retrieve tunnel */
445 chid = ntohl (msg->channel_id);
446 ch = channel_get_by_local_id (c, chid);
449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
455 /* Cleanup after the tunnel */
456 client_delete_channel (c, ch);
457 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
461 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
467 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
468 " channel %X client %p (%p, %p)\n",
469 chid, c, ch->root, ch->dest);
474 channel_destroy (ch);
475 tunnel_destroy_if_empty (t);
477 GNUNET_SERVER_receive_done (client, GNUNET_OK);
483 * Handler for client traffic
486 * @param client identification of the client
487 * @param message the actual message
490 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
491 const struct GNUNET_MessageHeader *message)
493 struct GNUNET_MESH_LocalData *msg;
494 struct MeshClient *c;
495 struct MeshChannel *ch;
496 struct MeshChannelReliability *rel;
497 MESH_ChannelNumber chid;
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Got data from a client!\n");
504 /* Sanity check for client registration */
505 if (NULL == (c = client_get (client)))
508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
513 msg = (struct GNUNET_MESH_LocalData *) message;
515 /* Sanity check for message size */
516 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
517 if (size < sizeof (struct GNUNET_MessageHeader))
520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
524 /* Channel exists? */
525 chid = ntohl (msg->id);
526 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
527 ch = channel_get_by_local_id (c, chid);
531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
535 /* Is the client in the channel? */
538 ch->root->handle == client)
542 ch->dest->handle == client) ) )
545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
549 rel = fwd ? ch->root_rel : ch->dest_rel;
550 rel->client_ready = GNUNET_NO;
552 /* Ok, everything is correct, send the message. */
554 struct GNUNET_MESH_Data *payload;
555 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
556 unsigned char cbuf[p2p_size];
558 payload = (struct GNUNET_MESH_Data *) cbuf;
559 payload->mid = htonl (rel->mid_send);
561 memcpy (&payload[1], &msg[1], size);
562 payload->header.size = htons (p2p_size);
563 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
564 payload->chid = htonl (ch->gid);
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
566 send_prebuilt_message_channel (&payload->header, ch, fwd);
568 if (GNUNET_YES == ch->reliable)
569 channel_save_copy (ch, &payload->header, fwd);
571 if (tunnel_get_buffer (ch->t, fwd) > 0)
572 send_local_ack (ch, fwd);
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
574 GNUNET_SERVER_receive_done (client, GNUNET_OK);
581 * Handler for client's ACKs for payload traffic.
583 * @param cls Closure (unused).
584 * @param client Identification of the client.
585 * @param message The actual message.
588 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
589 const struct GNUNET_MessageHeader *message)
591 struct GNUNET_MESH_LocalAck *msg;
592 struct MeshChannelReliability *rel;
593 struct MeshChannel *ch;
594 struct MeshClient *c;
595 MESH_ChannelNumber chid;
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
600 /* Sanity check for client registration */
601 if (NULL == (c = client_get (client)))
604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
609 msg = (struct GNUNET_MESH_LocalAck *) message;
611 /* Channel exists? */
612 chid = ntohl (msg->channel_id);
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
614 ch = channel_get_by_local_id (c, chid);
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
625 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
626 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
627 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
628 rel = fwd ? ch->dest_rel : ch->root_rel;
630 rel->client_ready = GNUNET_YES;
631 channel_send_client_buffered_data (ch, c, fwd);
632 send_ack (NULL, ch, fwd);
634 GNUNET_SERVER_receive_done (client, GNUNET_OK);
641 * Iterator over all tunnels to send a monitoring client info about each tunnel.
643 * @param cls Closure (client handle).
644 * @param key Key (hashed tunnel ID, unused).
645 * @param value Tunnel info.
647 * @return GNUNET_YES, to keep iterating.
650 // monitor_all_tunnels_iterator (void *cls,
651 // const struct GNUNET_HashCode * key,
654 // struct GNUNET_SERVER_Client *client = cls;
655 // struct MeshChannel *ch = value;
656 // struct GNUNET_MESH_LocalMonitor *msg;
658 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
659 // msg->channel_id = htonl (ch->gid);
660 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
661 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
663 // GNUNET_log (GNUNET_ERROR_TYPE_INFO,
664 // "* sending info about tunnel %s\n",
665 // GNUNET_i2s (&msg->owner));
667 // GNUNET_SERVER_notification_context_unicast (nc, client,
668 // &msg->header, GNUNET_NO);
669 // return GNUNET_YES;
674 * Handler for client's MONITOR request.
676 * @param cls Closure (unused).
677 * @param client Identification of the client.
678 * @param message The actual message.
681 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
682 const struct GNUNET_MessageHeader *message)
684 struct MeshClient *c;
686 /* Sanity check for client registration */
687 if (NULL == (c = client_get (client)))
690 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
694 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
695 "Received get tunnels request from client %u\n",
697 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
698 // monitor_all_tunnels_iterator,
700 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
701 "Get tunnels request from client %u completed\n",
703 GNUNET_SERVER_receive_done (client, GNUNET_OK);
708 * Handler for client's MONITOR_TUNNEL request.
710 * @param cls Closure (unused).
711 * @param client Identification of the client.
712 * @param message The actual message.
715 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
716 const struct GNUNET_MessageHeader *message)
718 const struct GNUNET_MESH_LocalMonitor *msg;
719 struct GNUNET_MESH_LocalMonitor *resp;
720 struct MeshClient *c;
721 struct MeshChannel *ch;
723 /* Sanity check for client registration */
724 if (NULL == (c = client_get (client)))
727 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
731 msg = (struct GNUNET_MESH_LocalMonitor *) message;
732 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
733 "Received tunnel info request from client %u for tunnel %s[%X]\n",
736 ntohl (msg->channel_id));
737 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
741 /* We don't know the tunnel */
742 struct GNUNET_MESH_LocalMonitor warn;
745 GNUNET_SERVER_notification_context_unicast (nc, client,
748 GNUNET_SERVER_receive_done (client, GNUNET_OK);
752 /* Initialize context */
753 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
755 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
756 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
757 &resp->header, GNUNET_NO);
760 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
761 "Monitor tunnel request from client %u completed\n",
763 GNUNET_SERVER_receive_done (client, GNUNET_OK);
768 * Functions to handle messages from clients
770 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
771 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
772 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
773 sizeof (struct GNUNET_MESH_ChannelMessage)},
774 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
775 sizeof (struct GNUNET_MESH_ChannelMessage)},
776 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
777 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
778 sizeof (struct GNUNET_MESH_LocalAck)},
779 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
780 sizeof (struct GNUNET_MessageHeader)},
781 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
782 sizeof (struct GNUNET_MESH_LocalMonitor)},
788 /******************************************************************************/
789 /******************************** API ***********************************/
790 /******************************************************************************/
793 * Initialize server subsystem.
795 * @param handle Server handle.
798 GML_init (struct GNUNET_SERVER_Handle *handle)
800 server_handle = handle;
801 GNUNET_SERVER_suspend (server_handle);
802 ports = GNUNET_CONTAINER_multihashmap32_create (32);
807 * Install server (service) handlers and start listening to clients.
812 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
813 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
814 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
816 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
821 GNUNET_SERVER_resume (server_handle);
833 GNUNET_SERVER_notification_context_destroy (nc);
840 * Check if client has registered with the service and has not disconnected
842 * @param client the client to check
844 * @return non-NULL if client exists in the global DLL
847 GML_client_get (struct GNUNET_SERVER_Client *client)
849 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
854 * Deletes a tunnel from a client (either owner or destination).
856 * @param c Client whose tunnel to delete.
857 * @param ch Channel which should be deleted.
860 GML_client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
866 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
868 if (GNUNET_YES != res)
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
873 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
875 if (GNUNET_YES != res)
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
881 * Build a local ACK message and send it to a local client, if needed.
883 * If the client was already allowed to send data, do nothing.
885 * @param ch Channel on which to send the ACK.
886 * @param c Client to whom send the ACK.
887 * @param fwd Set to GNUNET_YES for FWD ACK (dest->root)
890 GML_send_ack (struct MeshChannel *ch, int fwd)
892 struct GNUNET_MESH_LocalAck msg;
893 struct MeshChannelReliability *rel;
894 struct MeshClient *c;
896 c = fwd ? ch->root : ch->dest;
897 rel = fwd ? ch->root_rel : ch->dest_rel;
899 if (GNUNET_YES == rel->client_ready)
900 return; /* don't send double ACKs to client */
902 rel->client_ready = GNUNET_YES;
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "send local %s ack on %s:%X towards %p\n",
906 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
909 || ( fwd && (0 == ch->lid_root || c != ch->root))
910 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
915 msg.header.size = htons (sizeof (msg));
916 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
917 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
918 GNUNET_SERVER_notification_context_unicast (nc,
927 * Notify the client that a new incoming channel was created.
929 * @param ch Channel that was created.
932 GML_send_channel_create (struct MeshClient *c,
933 uint32_t id, uint32_t port, uint32_t opt,
934 struct GNUNET_PeerIdentity *peer)
936 struct GNUNET_MESH_ChannelMessage msg;
938 msg.header.size = htons (sizeof (msg));
939 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
940 msg.channel_id = htonl (id);
941 msg.port = htonl (port);
942 msg.opt = htonl (opt);
944 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
945 &msg.header, GNUNET_NO);
950 * Notify a client that a channel is no longer valid.
953 * @param id ID of the channel that is destroyed.
956 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
958 struct GNUNET_MESH_ChannelMessage msg;
965 msg.header.size = htons (sizeof (msg));
966 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
967 msg.channel_id = htonl (id);
968 msg.port = htonl (0);
969 memset (&msg.peer, 0, sizeof (msg.peer));
971 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
972 &msg.header, GNUNET_NO);
977 * Modify the mesh message ID from global to local and send to client.
979 * @param msg Message to modify and send.
980 * @param c Client to send to.
981 * @param tid Tunnel ID to use (c can be both owner and client).
984 GML_send_data (struct MeshClient *c,
985 const struct GNUNET_MESH_Data *msg,
986 MESH_ChannelNumber id)
988 struct GNUNET_MESH_LocalData *copy;
989 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
990 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
992 if (size < sizeof (struct GNUNET_MessageHeader))
1002 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1003 memcpy (©[1], &msg[1], size);
1004 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1005 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1006 copy->id = htonl (id);
1007 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1008 ©->header, GNUNET_NO);