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
26 #include "gnunet-service-mesh_local.h"
27 #include "gnunet-service-mesh_tunnel.h"
29 #define LOG(level, ...) GNUNET_log_from(level,"mesh-loc",__VA_ARGS__)
31 /******************************************************************************/
32 /******************************** STRUCTS **********************************/
33 /******************************************************************************/
36 * Struct containing information about a client of the service
38 * TODO: add a list of 'waiting' ports
45 struct MeshClient *next;
50 struct MeshClient *prev;
53 * Tunnels that belong to this client, indexed by local id
55 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
58 * Tunnels this client has accepted, indexed by incoming local id
60 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
63 * Handle to communicate with the client
65 struct GNUNET_SERVER_Client *handle;
68 * Ports that this client has declared interest in.
69 * Indexed by port, contains *Client.
71 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
74 * Whether the client is active or shutting down (don't send confirmations
75 * to a client that is shutting down.
80 * ID of the client, mainly for debug messages
85 /******************************************************************************/
86 /******************************* GLOBALS ***********************************/
87 /******************************************************************************/
90 * Handle to server lib.
92 static struct GNUNET_SERVER_Handle *server_handle;
95 * DLL with all the clients, head.
97 static struct MeshClient *clients_head;
100 * DLL with all the clients, tail.
102 static struct MeshClient *clients_tail;
105 * Next ID to assign to a client.
107 unsigned int next_client_id;
110 * All ports clients of this peer have opened.
112 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
115 * Notification context, to send messages to local clients.
117 static struct GNUNET_SERVER_NotificationContext *nc;
120 /******************************************************************************/
121 /******************************** STATIC ***********************************/
122 /******************************************************************************/
125 * Remove client's ports from the global hashmap on disconnect.
127 * @param cls Closure (unused).
129 * @param value Client structure.
131 * @return GNUNET_OK, keep iterating.
134 client_release_ports (void *cls,
140 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
141 if (GNUNET_YES != res)
144 LOG (GNUNET_ERROR_TYPE_WARNING,
145 "Port %u by client %p was not registered.\n",
153 /******************************************************************************/
154 /******************************** HANDLES ***********************************/
155 /******************************************************************************/
159 * Handler for client connection.
161 * @param cls Closure (unused).
162 * @param client Client handler.
165 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
167 struct MeshClient *c;
171 c = GNUNET_malloc (sizeof (struct MeshClient));
173 c->id = next_client_id++; /* overflow not important: just for debug */
174 GNUNET_SERVER_client_keep (client);
175 GNUNET_SERVER_client_set_user_context (client, c);
176 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
181 * Handler for client disconnection
184 * @param client identification of the client; NULL
185 * for the last call when the server is destroyed
188 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
190 struct MeshClient *c;
192 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
195 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
199 c = client_get (client);
202 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
204 GNUNET_SERVER_client_drop (c->handle);
205 c->shutting_down = GNUNET_YES;
206 if (NULL != c->own_channels)
208 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
209 &channel_destroy_iterator, c);
210 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
213 if (NULL != c->incoming_channels)
215 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
216 &channel_destroy_iterator, c);
217 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
220 if (NULL != c->ports)
222 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
223 &client_release_ports, c);
224 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
226 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
227 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
228 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
233 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
235 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
241 * Handler for new clients
244 * @param client identification of the client
245 * @param message the actual message, which includes messages the client wants
248 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
249 const struct GNUNET_MessageHeader *message)
251 struct GNUNET_MESH_ClientConnect *cc_msg;
252 struct MeshClient *c;
257 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
259 /* Check data sanity */
260 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
261 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
262 if (0 != (size % sizeof (uint32_t)))
265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
268 size /= sizeof (uint32_t);
270 /* Initialize new client structure */
271 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
272 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
273 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
278 p = (uint32_t *) &cc_msg[1];
279 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
280 for (i = 0; i < size; i++)
283 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
285 /* store in client's hashmap */
286 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
287 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
288 /* store in global hashmap */
289 /* FIXME only allow one client to have the port open,
290 * have a backup hashmap with waiting clients */
291 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
296 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
297 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
298 GNUNET_SERVER_notification_context_add (nc, client);
299 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
301 GNUNET_SERVER_receive_done (client, GNUNET_OK);
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
307 * Handler for requests of new tunnels
309 * @param cls Closure.
310 * @param client Identification of the client.
311 * @param message The actual message.
314 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
315 const struct GNUNET_MessageHeader *message)
317 struct GNUNET_MESH_ChannelMessage *msg;
318 struct MeshPeer *peer;
319 struct MeshTunnel2 *t;
320 struct MeshChannel *ch;
321 struct MeshClient *c;
322 MESH_ChannelNumber chid;
324 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
326 /* Sanity check for client registration */
327 if (NULL == (c = client_get (client)))
330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
333 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
335 /* Message size sanity check */
336 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
343 msg = (struct GNUNET_MESH_ChannelMessage *) message;
344 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
345 GNUNET_i2s (&msg->peer), ntohl (msg->port));
346 chid = ntohl (msg->channel_id);
348 /* Sanity check for duplicate channel IDs */
349 if (NULL != channel_get_by_local_id (c, chid))
352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
356 peer = peer_get (&msg->peer);
357 if (NULL == peer->tunnel)
359 peer->tunnel = tunnel_new ();
360 peer->tunnel->peer = peer;
361 if (peer->id == myid)
363 tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY);
373 ch = channel_new (t, c, chid);
377 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
380 ch->port = ntohl (msg->port);
381 channel_set_options (ch, ntohl (msg->opt));
383 /* In unreliable channels, we'll use the DLL to buffer BCK data */
384 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
385 ch->root_rel->ch = ch;
386 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
388 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
389 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
391 /* Send create channel */
393 struct GNUNET_MESH_ChannelCreate msgcc;
395 msgcc.header.size = htons (sizeof (msgcc));
396 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
397 msgcc.chid = htonl (ch->gid);
398 msgcc.port = msg->port;
399 msgcc.opt = msg->opt;
401 GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
404 GNUNET_SERVER_receive_done (client, GNUNET_OK);
410 * Handler for requests of deleting tunnels
413 * @param client identification of the client
414 * @param message the actual message
417 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
418 const struct GNUNET_MessageHeader *message)
420 struct GNUNET_MESH_ChannelMessage *msg;
421 struct MeshClient *c;
422 struct MeshChannel *ch;
423 struct MeshTunnel2 *t;
424 MESH_ChannelNumber chid;
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "Got a DESTROY CHANNEL from client!\n");
429 /* Sanity check for client registration */
430 if (NULL == (c = client_get (client)))
433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
436 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
438 /* Message sanity check */
439 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
446 msg = (struct GNUNET_MESH_ChannelMessage *) message;
448 /* Retrieve tunnel */
449 chid = ntohl (msg->channel_id);
450 ch = channel_get_by_local_id (c, chid);
453 LOG (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
459 /* Cleanup after the tunnel */
460 client_delete_channel (c, ch);
461 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
465 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
471 LOG (GNUNET_ERROR_TYPE_ERROR,
472 " channel %X client %p (%p, %p)\n",
473 chid, c, ch->root, ch->dest);
478 channel_destroy (ch);
479 tunnel_destroy_if_empty (t);
481 GNUNET_SERVER_receive_done (client, GNUNET_OK);
487 * Handler for client traffic
490 * @param client identification of the client
491 * @param message the actual message
494 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
495 const struct GNUNET_MessageHeader *message)
497 struct GNUNET_MESH_LocalData *msg;
498 struct MeshClient *c;
499 struct MeshChannel *ch;
500 struct MeshChannelReliability *rel;
501 MESH_ChannelNumber chid;
505 LOG (GNUNET_ERROR_TYPE_DEBUG,
506 "Got data from a client!\n");
508 /* Sanity check for client registration */
509 if (NULL == (c = client_get (client)))
512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
515 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
517 msg = (struct GNUNET_MESH_LocalData *) message;
519 /* Sanity check for message size */
520 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
521 if (size < sizeof (struct GNUNET_MessageHeader))
524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
528 /* Channel exists? */
529 chid = ntohl (msg->id);
530 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
531 ch = channel_get_by_local_id (c, chid);
535 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
539 /* Is the client in the channel? */
542 ch->root->handle == client)
546 ch->dest->handle == client) ) )
549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
553 rel = fwd ? ch->root_rel : ch->dest_rel;
554 rel->client_ready = GNUNET_NO;
556 /* Ok, everything is correct, send the message. */
558 struct GNUNET_MESH_Data *payload;
559 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
560 unsigned char cbuf[p2p_size];
562 payload = (struct GNUNET_MESH_Data *) cbuf;
563 payload->mid = htonl (rel->mid_send);
565 memcpy (&payload[1], &msg[1], size);
566 payload->header.size = htons (p2p_size);
567 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
568 payload->chid = htonl (ch->gid);
569 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
570 send_prebuilt_message_channel (&payload->header, ch, fwd);
572 if (GNUNET_YES == ch->reliable)
573 channel_save_copy (ch, &payload->header, fwd);
575 if (tunnel_get_buffer (ch->t, fwd) > 0)
576 send_local_ack (ch, fwd);
577 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
578 GNUNET_SERVER_receive_done (client, GNUNET_OK);
585 * Handler for client's ACKs for payload traffic.
587 * @param cls Closure (unused).
588 * @param client Identification of the client.
589 * @param message The actual message.
592 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
593 const struct GNUNET_MessageHeader *message)
595 struct GNUNET_MESH_LocalAck *msg;
596 struct MeshChannelReliability *rel;
597 struct MeshChannel *ch;
598 struct MeshClient *c;
599 MESH_ChannelNumber chid;
602 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
604 /* Sanity check for client registration */
605 if (NULL == (c = client_get (client)))
608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
611 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
613 msg = (struct GNUNET_MESH_LocalAck *) message;
615 /* Channel exists? */
616 chid = ntohl (msg->channel_id);
617 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
618 ch = channel_get_by_local_id (c, chid);
619 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
623 LOG (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
624 LOG (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
629 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
630 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
631 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
632 rel = fwd ? ch->dest_rel : ch->root_rel;
634 rel->client_ready = GNUNET_YES;
635 channel_send_client_buffered_data (ch, c, fwd);
636 send_ack (NULL, ch, fwd);
638 GNUNET_SERVER_receive_done (client, GNUNET_OK);
645 * Iterator over all tunnels to send a monitoring client info about each tunnel.
647 * @param cls Closure (client handle).
648 * @param key Key (hashed tunnel ID, unused).
649 * @param value Tunnel info.
651 * @return GNUNET_YES, to keep iterating.
654 // monitor_all_tunnels_iterator (void *cls,
655 // const struct GNUNET_HashCode * key,
658 // struct GNUNET_SERVER_Client *client = cls;
659 // struct MeshChannel *ch = value;
660 // struct GNUNET_MESH_LocalMonitor *msg;
662 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
663 // msg->channel_id = htonl (ch->gid);
664 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
665 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
667 // LOG (GNUNET_ERROR_TYPE_INFO,
668 // "* sending info about tunnel %s\n",
669 // GNUNET_i2s (&msg->owner));
671 // GNUNET_SERVER_notification_context_unicast (nc, client,
672 // &msg->header, GNUNET_NO);
673 // return GNUNET_YES;
678 * Handler for client's MONITOR request.
680 * @param cls Closure (unused).
681 * @param client Identification of the client.
682 * @param message The actual message.
685 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
686 const struct GNUNET_MessageHeader *message)
688 struct MeshClient *c;
690 /* Sanity check for client registration */
691 if (NULL == (c = client_get (client)))
694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
698 LOG (GNUNET_ERROR_TYPE_INFO,
699 "Received get tunnels request from client %u\n",
701 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
702 // monitor_all_tunnels_iterator,
704 LOG (GNUNET_ERROR_TYPE_INFO,
705 "Get tunnels request from client %u completed\n",
707 GNUNET_SERVER_receive_done (client, GNUNET_OK);
712 * Handler for client's MONITOR_TUNNEL request.
714 * @param cls Closure (unused).
715 * @param client Identification of the client.
716 * @param message The actual message.
719 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
720 const struct GNUNET_MessageHeader *message)
722 const struct GNUNET_MESH_LocalMonitor *msg;
723 struct GNUNET_MESH_LocalMonitor *resp;
724 struct MeshClient *c;
725 struct MeshChannel *ch;
727 /* Sanity check for client registration */
728 if (NULL == (c = client_get (client)))
731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
735 msg = (struct GNUNET_MESH_LocalMonitor *) message;
736 LOG (GNUNET_ERROR_TYPE_INFO,
737 "Received tunnel info request from client %u for tunnel %s[%X]\n",
740 ntohl (msg->channel_id));
741 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
745 /* We don't know the tunnel */
746 struct GNUNET_MESH_LocalMonitor warn;
749 GNUNET_SERVER_notification_context_unicast (nc, client,
752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
756 /* Initialize context */
757 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
759 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
760 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
761 &resp->header, GNUNET_NO);
764 LOG (GNUNET_ERROR_TYPE_INFO,
765 "Monitor tunnel request from client %u completed\n",
767 GNUNET_SERVER_receive_done (client, GNUNET_OK);
772 * Functions to handle messages from clients
774 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
775 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
776 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
777 sizeof (struct GNUNET_MESH_ChannelMessage)},
778 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
779 sizeof (struct GNUNET_MESH_ChannelMessage)},
780 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
781 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
782 sizeof (struct GNUNET_MESH_LocalAck)},
783 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
784 sizeof (struct GNUNET_MessageHeader)},
785 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
786 sizeof (struct GNUNET_MESH_LocalMonitor)},
792 /******************************************************************************/
793 /******************************** API ***********************************/
794 /******************************************************************************/
797 * Initialize server subsystem.
799 * @param handle Server handle.
802 GML_init (struct GNUNET_SERVER_Handle *handle)
804 server_handle = handle;
805 GNUNET_SERVER_suspend (server_handle);
806 ports = GNUNET_CONTAINER_multihashmap32_create (32);
811 * Install server (service) handlers and start listening to clients.
816 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
817 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
818 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
820 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
825 GNUNET_SERVER_resume (server_handle);
837 GNUNET_SERVER_notification_context_destroy (nc);
844 * Check if client has registered with the service and has not disconnected
846 * @param client the client to check
848 * @return non-NULL if client exists in the global DLL
851 GML_client_get (struct GNUNET_SERVER_Client *client)
853 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
858 * Deletes a tunnel from a client (either owner or destination).
860 * @param c Client whose tunnel to delete.
861 * @param ch Channel which should be deleted.
864 GML_client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
870 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
872 if (GNUNET_YES != res)
873 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
877 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
879 if (GNUNET_YES != res)
880 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
885 * Build a local ACK message and send it to a local client, if needed.
887 * If the client was already allowed to send data, do nothing.
889 * @param ch Channel on which to send the ACK.
890 * @param c Client to whom send the ACK.
891 * @param fwd Set to GNUNET_YES for FWD ACK (dest->root)
894 GML_send_ack (struct MeshChannel *ch, int fwd)
896 struct GNUNET_MESH_LocalAck msg;
897 struct MeshChannelReliability *rel;
898 struct MeshClient *c;
900 c = fwd ? ch->root : ch->dest;
901 rel = fwd ? ch->root_rel : ch->dest_rel;
903 if (GNUNET_YES == rel->client_ready)
904 return; /* don't send double ACKs to client */
906 rel->client_ready = GNUNET_YES;
908 LOG (GNUNET_ERROR_TYPE_DEBUG,
909 "send local %s ack on %s:%X towards %p\n",
910 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
913 || ( fwd && (0 == ch->lid_root || c != ch->root))
914 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
919 msg.header.size = htons (sizeof (msg));
920 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
921 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
922 GNUNET_SERVER_notification_context_unicast (nc,
931 * Notify the client that a new incoming channel was created.
933 * @param ch Channel that was created.
936 GML_send_channel_create (struct MeshClient *c,
937 uint32_t id, uint32_t port, uint32_t opt,
938 struct GNUNET_PeerIdentity *peer)
940 struct GNUNET_MESH_ChannelMessage msg;
942 msg.header.size = htons (sizeof (msg));
943 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
944 msg.channel_id = htonl (id);
945 msg.port = htonl (port);
946 msg.opt = htonl (opt);
948 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
949 &msg.header, GNUNET_NO);
954 * Notify a client that a channel is no longer valid.
957 * @param id ID of the channel that is destroyed.
960 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
962 struct GNUNET_MESH_ChannelMessage msg;
969 msg.header.size = htons (sizeof (msg));
970 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
971 msg.channel_id = htonl (id);
972 msg.port = htonl (0);
973 memset (&msg.peer, 0, sizeof (msg.peer));
975 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
976 &msg.header, GNUNET_NO);
981 * Modify the mesh message ID from global to local and send to client.
983 * @param msg Message to modify and send.
984 * @param c Client to send to.
985 * @param tid Tunnel ID to use (c can be both owner and client).
988 GML_send_data (struct MeshClient *c,
989 const struct GNUNET_MESH_Data *msg,
990 MESH_ChannelNumber id)
992 struct GNUNET_MESH_LocalData *copy;
993 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
994 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
996 if (size < sizeof (struct GNUNET_MessageHeader))
1006 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1007 memcpy (©[1], &msg[1], size);
1008 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1009 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1010 copy->id = htonl (id);
1011 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1012 ©->header, GNUNET_NO);