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.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
28 #include "mesh_protocol_enc.h" // GNUNET_MESH_Data is shared
30 #include "gnunet-service-mesh_local.h"
31 #include "gnunet-service-mesh_tunnel.h"
33 #define LOG(level, ...) GNUNET_log_from(level,"mesh-loc",__VA_ARGS__)
35 /******************************************************************************/
36 /******************************** STRUCTS **********************************/
37 /******************************************************************************/
40 * Struct containing information about a client of the service
42 * TODO: add a list of 'waiting' ports
49 struct MeshClient *next;
54 struct MeshClient *prev;
57 * Tunnels that belong to this client, indexed by local id
59 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
62 * Tunnels this client has accepted, indexed by incoming local id
64 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
67 * Handle to communicate with the client
69 struct GNUNET_SERVER_Client *handle;
72 * Ports that this client has declared interest in.
73 * Indexed by port, contains *Client.
75 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
78 * Whether the client is active or shutting down (don't send confirmations
79 * to a client that is shutting down.
84 * ID of the client, mainly for debug messages
89 /******************************************************************************/
90 /******************************* GLOBALS ***********************************/
91 /******************************************************************************/
94 * Global handle to the statistics service.
96 extern struct GNUNET_STATISTICS_Handle *stats;
99 * Handle to server lib.
101 static struct GNUNET_SERVER_Handle *server_handle;
104 * DLL with all the clients, head.
106 static struct MeshClient *clients_head;
109 * DLL with all the clients, tail.
111 static struct MeshClient *clients_tail;
114 * Next ID to assign to a client.
116 unsigned int next_client_id;
119 * All ports clients of this peer have opened.
121 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
124 * Notification context, to send messages to local clients.
126 static struct GNUNET_SERVER_NotificationContext *nc;
129 /******************************************************************************/
130 /******************************** STATIC ***********************************/
131 /******************************************************************************/
134 * Remove client's ports from the global hashmap on disconnect.
136 * @param cls Closure (unused).
138 * @param value Client structure.
140 * @return GNUNET_OK, keep iterating.
143 client_release_ports (void *cls,
149 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
150 if (GNUNET_YES != res)
153 LOG (GNUNET_ERROR_TYPE_WARNING,
154 "Port %u by client %p was not registered.\n",
162 /******************************************************************************/
163 /******************************** HANDLES ***********************************/
164 /******************************************************************************/
168 * Handler for client connection.
170 * @param cls Closure (unused).
171 * @param client Client handler.
174 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
176 struct MeshClient *c;
180 c = GNUNET_malloc (sizeof (struct MeshClient));
182 c->id = next_client_id++; /* overflow not important: just for debug */
183 GNUNET_SERVER_client_keep (client);
184 GNUNET_SERVER_client_set_user_context (client, c);
185 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
190 * Handler for client disconnection
193 * @param client identification of the client; NULL
194 * for the last call when the server is destroyed
197 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
199 struct MeshClient *c;
201 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
204 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
208 c = client_get (client);
211 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
213 GNUNET_SERVER_client_drop (c->handle);
214 c->shutting_down = GNUNET_YES;
215 if (NULL != c->own_channels)
217 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
218 &channel_destroy_iterator, c);
219 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
222 if (NULL != c->incoming_channels)
224 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
225 &channel_destroy_iterator, c);
226 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
229 if (NULL != c->ports)
231 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
232 &client_release_ports, c);
233 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
235 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
236 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
237 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
242 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
244 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
250 * Handler for new clients
253 * @param client identification of the client
254 * @param message the actual message, which includes messages the client wants
257 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
258 const struct GNUNET_MessageHeader *message)
260 struct GNUNET_MESH_ClientConnect *cc_msg;
261 struct MeshClient *c;
266 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
268 /* Check data sanity */
269 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
270 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
271 if (0 != (size % sizeof (uint32_t)))
274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
277 size /= sizeof (uint32_t);
279 /* Initialize new client structure */
280 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
281 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
282 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
287 p = (uint32_t *) &cc_msg[1];
288 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
289 for (i = 0; i < size; i++)
292 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
294 /* store in client's hashmap */
295 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
296 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
297 /* store in global hashmap */
298 /* FIXME only allow one client to have the port open,
299 * have a backup hashmap with waiting clients */
300 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
305 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
306 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
307 GNUNET_SERVER_notification_context_add (nc, client);
308 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
310 GNUNET_SERVER_receive_done (client, GNUNET_OK);
311 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
316 * Handler for requests of new tunnels
318 * @param cls Closure.
319 * @param client Identification of the client.
320 * @param message The actual message.
323 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
324 const struct GNUNET_MessageHeader *message)
326 struct GNUNET_MESH_ChannelMessage *msg;
327 struct MeshPeer *peer;
328 struct MeshTunnel2 *t;
329 struct MeshChannel *ch;
330 struct MeshClient *c;
331 MESH_ChannelNumber chid;
333 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
335 /* Sanity check for client registration */
336 if (NULL == (c = client_get (client)))
339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
342 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
344 /* Message size sanity check */
345 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
352 msg = (struct GNUNET_MESH_ChannelMessage *) message;
353 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
354 GNUNET_i2s (&msg->peer), ntohl (msg->port));
355 chid = ntohl (msg->channel_id);
357 /* Sanity check for duplicate channel IDs */
358 if (NULL != channel_get_by_local_id (c, chid))
361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
365 peer = peer_get (&msg->peer);
366 if (NULL == peer->tunnel)
368 peer->tunnel = tunnel_new ();
369 peer->tunnel->peer = peer;
370 if (peer->id == myid)
372 tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY);
382 ch = channel_new (t, c, chid);
386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
389 ch->port = ntohl (msg->port);
390 channel_set_options (ch, ntohl (msg->opt));
392 /* In unreliable channels, we'll use the DLL to buffer BCK data */
393 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
394 ch->root_rel->ch = ch;
395 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
397 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
398 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
400 /* Send create channel */
402 struct GNUNET_MESH_ChannelCreate msgcc;
404 msgcc.header.size = htons (sizeof (msgcc));
405 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
406 msgcc.chid = htonl (ch->gid);
407 msgcc.port = msg->port;
408 msgcc.opt = msg->opt;
410 GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
413 GNUNET_SERVER_receive_done (client, GNUNET_OK);
419 * Handler for requests of deleting tunnels
422 * @param client identification of the client
423 * @param message the actual message
426 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
427 const struct GNUNET_MessageHeader *message)
429 struct GNUNET_MESH_ChannelMessage *msg;
430 struct MeshClient *c;
431 struct MeshChannel *ch;
432 struct MeshTunnel2 *t;
433 MESH_ChannelNumber chid;
435 LOG (GNUNET_ERROR_TYPE_DEBUG,
436 "Got a DESTROY CHANNEL from client!\n");
438 /* Sanity check for client registration */
439 if (NULL == (c = client_get (client)))
442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
445 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
447 /* Message sanity check */
448 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
455 msg = (struct GNUNET_MESH_ChannelMessage *) message;
457 /* Retrieve tunnel */
458 chid = ntohl (msg->channel_id);
459 ch = channel_get_by_local_id (c, chid);
462 LOG (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
468 /* Cleanup after the tunnel */
469 client_delete_channel (c, ch);
470 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
474 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
480 LOG (GNUNET_ERROR_TYPE_ERROR,
481 " channel %X client %p (%p, %p)\n",
482 chid, c, ch->root, ch->dest);
487 channel_destroy (ch);
488 tunnel_destroy_if_empty (t);
490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
496 * Handler for client traffic
499 * @param client identification of the client
500 * @param message the actual message
503 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
504 const struct GNUNET_MessageHeader *message)
506 struct GNUNET_MESH_LocalData *msg;
507 struct MeshClient *c;
508 struct MeshChannel *ch;
509 struct MeshChannelReliability *rel;
510 MESH_ChannelNumber chid;
514 LOG (GNUNET_ERROR_TYPE_DEBUG,
515 "Got data from a client!\n");
517 /* Sanity check for client registration */
518 if (NULL == (c = client_get (client)))
521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
524 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
526 msg = (struct GNUNET_MESH_LocalData *) message;
528 /* Sanity check for message size */
529 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
530 if (size < sizeof (struct GNUNET_MessageHeader))
533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
537 /* Channel exists? */
538 chid = ntohl (msg->id);
539 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
540 ch = channel_get_by_local_id (c, chid);
544 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
548 /* Is the client in the channel? */
551 ch->root->handle == client)
555 ch->dest->handle == client) ) )
558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
562 rel = fwd ? ch->root_rel : ch->dest_rel;
563 rel->client_ready = GNUNET_NO;
565 /* Ok, everything is correct, send the message. */
567 struct GNUNET_MESH_Data *payload;
568 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
569 unsigned char cbuf[p2p_size];
571 payload = (struct GNUNET_MESH_Data *) cbuf;
572 payload->mid = htonl (rel->mid_send);
574 memcpy (&payload[1], &msg[1], size);
575 payload->header.size = htons (p2p_size);
576 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
577 payload->chid = htonl (ch->gid);
578 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
579 send_prebuilt_message_channel (&payload->header, ch, fwd);
581 if (GNUNET_YES == ch->reliable)
582 channel_save_copy (ch, &payload->header, fwd);
584 if (tunnel_get_buffer (ch->t, fwd) > 0)
585 send_local_ack (ch, fwd);
586 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
587 GNUNET_SERVER_receive_done (client, GNUNET_OK);
594 * Handler for client's ACKs for payload traffic.
596 * @param cls Closure (unused).
597 * @param client Identification of the client.
598 * @param message The actual message.
601 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
602 const struct GNUNET_MessageHeader *message)
604 struct GNUNET_MESH_LocalAck *msg;
605 struct MeshChannelReliability *rel;
606 struct MeshChannel *ch;
607 struct MeshClient *c;
608 MESH_ChannelNumber chid;
611 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
613 /* Sanity check for client registration */
614 if (NULL == (c = client_get (client)))
617 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
620 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
622 msg = (struct GNUNET_MESH_LocalAck *) message;
624 /* Channel exists? */
625 chid = ntohl (msg->channel_id);
626 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
627 ch = channel_get_by_local_id (c, chid);
628 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
632 LOG (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
633 LOG (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
638 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
639 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
640 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
641 rel = fwd ? ch->dest_rel : ch->root_rel;
643 rel->client_ready = GNUNET_YES;
644 channel_send_client_buffered_data (ch, c, fwd);
645 send_ack (NULL, ch, fwd);
647 GNUNET_SERVER_receive_done (client, GNUNET_OK);
654 * Iterator over all tunnels to send a monitoring client info about each tunnel.
656 * @param cls Closure (client handle).
657 * @param key Key (hashed tunnel ID, unused).
658 * @param value Tunnel info.
660 * @return GNUNET_YES, to keep iterating.
663 // monitor_all_tunnels_iterator (void *cls,
664 // const struct GNUNET_HashCode * key,
667 // struct GNUNET_SERVER_Client *client = cls;
668 // struct MeshChannel *ch = value;
669 // struct GNUNET_MESH_LocalMonitor *msg;
671 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
672 // msg->channel_id = htonl (ch->gid);
673 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
674 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
676 // LOG (GNUNET_ERROR_TYPE_INFO,
677 // "* sending info about tunnel %s\n",
678 // GNUNET_i2s (&msg->owner));
680 // GNUNET_SERVER_notification_context_unicast (nc, client,
681 // &msg->header, GNUNET_NO);
682 // return GNUNET_YES;
687 * Handler for client's MONITOR request.
689 * @param cls Closure (unused).
690 * @param client Identification of the client.
691 * @param message The actual message.
694 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
695 const struct GNUNET_MessageHeader *message)
697 struct MeshClient *c;
699 /* Sanity check for client registration */
700 if (NULL == (c = client_get (client)))
703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
707 LOG (GNUNET_ERROR_TYPE_INFO,
708 "Received get tunnels request from client %u\n",
710 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
711 // monitor_all_tunnels_iterator,
713 LOG (GNUNET_ERROR_TYPE_INFO,
714 "Get tunnels request from client %u completed\n",
716 GNUNET_SERVER_receive_done (client, GNUNET_OK);
721 * Handler for client's MONITOR_TUNNEL request.
723 * @param cls Closure (unused).
724 * @param client Identification of the client.
725 * @param message The actual message.
728 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
729 const struct GNUNET_MessageHeader *message)
731 const struct GNUNET_MESH_LocalMonitor *msg;
732 struct GNUNET_MESH_LocalMonitor *resp;
733 struct MeshClient *c;
734 struct MeshChannel *ch;
736 /* Sanity check for client registration */
737 if (NULL == (c = client_get (client)))
740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
744 msg = (struct GNUNET_MESH_LocalMonitor *) message;
745 LOG (GNUNET_ERROR_TYPE_INFO,
746 "Received tunnel info request from client %u for tunnel %s[%X]\n",
749 ntohl (msg->channel_id));
750 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
754 /* We don't know the tunnel */
755 struct GNUNET_MESH_LocalMonitor warn;
758 GNUNET_SERVER_notification_context_unicast (nc, client,
761 GNUNET_SERVER_receive_done (client, GNUNET_OK);
765 /* Initialize context */
766 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
768 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
769 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
770 &resp->header, GNUNET_NO);
773 LOG (GNUNET_ERROR_TYPE_INFO,
774 "Monitor tunnel request from client %u completed\n",
776 GNUNET_SERVER_receive_done (client, GNUNET_OK);
781 * Functions to handle messages from clients
783 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
784 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
785 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
786 sizeof (struct GNUNET_MESH_ChannelMessage)},
787 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
788 sizeof (struct GNUNET_MESH_ChannelMessage)},
789 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
790 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
791 sizeof (struct GNUNET_MESH_LocalAck)},
792 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
793 sizeof (struct GNUNET_MessageHeader)},
794 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
795 sizeof (struct GNUNET_MESH_LocalMonitor)},
801 /******************************************************************************/
802 /******************************** API ***********************************/
803 /******************************************************************************/
806 * Initialize server subsystem.
808 * @param handle Server handle.
811 GML_init (struct GNUNET_SERVER_Handle *handle)
813 server_handle = handle;
814 GNUNET_SERVER_suspend (server_handle);
815 ports = GNUNET_CONTAINER_multihashmap32_create (32);
820 * Install server (service) handlers and start listening to clients.
825 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
826 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
827 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
829 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
834 GNUNET_SERVER_resume (server_handle);
846 GNUNET_SERVER_notification_context_destroy (nc);
852 * Get a chennel from a client
854 * @param c the client to check
855 * @param chid Channel ID, must be local (> 0x800...)
857 * @return non-NULL if channel exists in the clients lists
860 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
862 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
865 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
868 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
869 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
870 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
874 * Check if client has registered with the service and has not disconnected
876 * @param client the client to check
878 * @return non-NULL if client exists in the global DLL
881 GML_client_get (struct GNUNET_SERVER_Client *client)
883 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
887 * Find a client that has opened a port
889 * @param port Port to check.
891 * @return non-NULL if a client has the port.
894 GML_client_get_by_port (uint32_t port)
896 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
901 * Deletes a tunnel from a client (either owner or destination).
903 * @param c Client whose tunnel to delete.
904 * @param ch Channel which should be deleted.
907 GML_client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
913 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
915 if (GNUNET_YES != res)
916 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
920 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
922 if (GNUNET_YES != res)
923 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
928 * Build a local ACK message and send it to a local client, if needed.
930 * If the client was already allowed to send data, do nothing.
932 * @param ch Channel on which to send the ACK.
933 * @param c Client to whom send the ACK.
934 * @param fwd Set to GNUNET_YES for FWD ACK (dest->root)
937 GML_send_ack (struct MeshChannel *ch, int fwd)
939 struct GNUNET_MESH_LocalAck msg;
940 struct MeshChannelReliability *rel;
941 struct MeshClient *c;
943 c = fwd ? ch->root : ch->dest;
944 rel = fwd ? ch->root_rel : ch->dest_rel;
946 if (GNUNET_YES == rel->client_ready)
947 return; /* don't send double ACKs to client */
949 rel->client_ready = GNUNET_YES;
951 LOG (GNUNET_ERROR_TYPE_DEBUG,
952 "send local %s ack on %s:%X towards %p\n",
953 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
956 || ( fwd && (0 == ch->lid_root || c != ch->root))
957 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
962 msg.header.size = htons (sizeof (msg));
963 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
964 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
965 GNUNET_SERVER_notification_context_unicast (nc,
974 * Notify the client that a new incoming channel was created.
976 * @param ch Channel that was created.
979 GML_send_channel_create (struct MeshClient *c,
980 uint32_t id, uint32_t port, uint32_t opt,
981 struct GNUNET_PeerIdentity *peer)
983 struct GNUNET_MESH_ChannelMessage msg;
985 msg.header.size = htons (sizeof (msg));
986 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
987 msg.channel_id = htonl (id);
988 msg.port = htonl (port);
989 msg.opt = htonl (opt);
991 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
992 &msg.header, GNUNET_NO);
997 * Notify a client that a channel is no longer valid.
1000 * @param id ID of the channel that is destroyed.
1003 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
1005 struct GNUNET_MESH_ChannelMessage msg;
1012 msg.header.size = htons (sizeof (msg));
1013 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1014 msg.channel_id = htonl (id);
1015 msg.port = htonl (0);
1016 memset (&msg.peer, 0, sizeof (msg.peer));
1017 msg.opt = htonl (0);
1018 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1019 &msg.header, GNUNET_NO);
1024 * Modify the mesh message ID from global to local and send to client.
1026 * @param msg Message to modify and send.
1027 * @param c Client to send to.
1028 * @param tid Tunnel ID to use (c can be both owner and client).
1031 GML_send_data (struct MeshClient *c,
1032 const struct GNUNET_MESH_Data *msg,
1033 MESH_ChannelNumber id)
1035 struct GNUNET_MESH_LocalData *copy;
1036 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1037 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1039 if (size < sizeof (struct GNUNET_MessageHeader))
1041 GNUNET_break_op (0);
1049 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1050 memcpy (©[1], &msg[1], size);
1051 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1052 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1053 copy->id = htonl (id);
1054 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1055 ©->header, GNUNET_NO);