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 * Channel ID for the next incoming channel.
69 MESH_ChannelNumber next_chid;
72 * Handle to communicate with the client
74 struct GNUNET_SERVER_Client *handle;
77 * Ports that this client has declared interest in.
78 * Indexed by port, contains *Client.
80 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
83 * Whether the client is active or shutting down (don't send confirmations
84 * to a client that is shutting down.
89 * ID of the client, mainly for debug messages
94 /******************************************************************************/
95 /******************************* GLOBALS ***********************************/
96 /******************************************************************************/
99 * Global handle to the statistics service.
101 extern struct GNUNET_STATISTICS_Handle *stats;
104 * Handle to server lib.
106 static struct GNUNET_SERVER_Handle *server_handle;
109 * DLL with all the clients, head.
111 static struct MeshClient *clients_head;
114 * DLL with all the clients, tail.
116 static struct MeshClient *clients_tail;
119 * Next ID to assign to a client.
121 unsigned int next_client_id;
124 * All ports clients of this peer have opened.
126 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
129 * Notification context, to send messages to local clients.
131 static struct GNUNET_SERVER_NotificationContext *nc;
134 /******************************************************************************/
135 /******************************** STATIC ***********************************/
136 /******************************************************************************/
139 * Remove client's ports from the global hashmap on disconnect.
141 * @param cls Closure (unused).
143 * @param value Client structure.
145 * @return GNUNET_OK, keep iterating.
148 client_release_ports (void *cls,
154 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
155 if (GNUNET_YES != res)
158 LOG (GNUNET_ERROR_TYPE_WARNING,
159 "Port %u by client %p was not registered.\n",
167 /******************************************************************************/
168 /******************************** HANDLES ***********************************/
169 /******************************************************************************/
173 * Handler for client connection.
175 * @param cls Closure (unused).
176 * @param client Client handler.
179 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
181 struct MeshClient *c;
185 c = GNUNET_new (struct MeshClient);
187 c->id = next_client_id++; /* overflow not important: just for debug */
188 c->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
189 GNUNET_SERVER_client_keep (client);
190 GNUNET_SERVER_client_set_user_context (client, c);
191 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
196 * Handler for client disconnection
199 * @param client identification of the client; NULL
200 * for the last call when the server is destroyed
203 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
205 struct MeshClient *c;
207 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
210 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
214 c = client_get (client);
217 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
219 GNUNET_SERVER_client_drop (c->handle);
220 c->shutting_down = GNUNET_YES;
221 if (NULL != c->own_channels)
223 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
224 &channel_destroy_iterator, c);
225 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
228 if (NULL != c->incoming_channels)
230 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
231 &channel_destroy_iterator, c);
232 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
235 if (NULL != c->ports)
237 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
238 &client_release_ports, c);
239 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
241 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
242 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
243 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
248 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
250 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
256 * Handler for new clients
259 * @param client identification of the client
260 * @param message the actual message, which includes messages the client wants
263 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
264 const struct GNUNET_MessageHeader *message)
266 struct GNUNET_MESH_ClientConnect *cc_msg;
267 struct MeshClient *c;
272 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
274 /* Check data sanity */
275 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
276 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
277 if (0 != (size % sizeof (uint32_t)))
280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
283 size /= sizeof (uint32_t);
285 /* Initialize new client structure */
286 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
287 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
288 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
293 p = (uint32_t *) &cc_msg[1];
294 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
295 for (i = 0; i < size; i++)
298 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
300 /* store in client's hashmap */
301 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
302 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
303 /* store in global hashmap */
304 /* FIXME only allow one client to have the port open,
305 * have a backup hashmap with waiting clients */
306 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
311 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
312 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
313 GNUNET_SERVER_notification_context_add (nc, client);
314 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
316 GNUNET_SERVER_receive_done (client, GNUNET_OK);
317 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
322 * Handler for requests of new tunnels
324 * @param cls Closure.
325 * @param client Identification of the client.
326 * @param message The actual message.
329 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
330 const struct GNUNET_MessageHeader *message)
332 struct GNUNET_MESH_ChannelMessage *msg;
333 struct MeshPeer *peer;
334 struct MeshTunnel2 *t;
335 struct MeshChannel *ch;
336 struct MeshClient *c;
337 MESH_ChannelNumber chid;
339 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
341 /* Sanity check for client registration */
342 if (NULL == (c = client_get (client)))
345 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
348 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
350 /* Message size sanity check */
351 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
358 msg = (struct GNUNET_MESH_ChannelMessage *) message;
359 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
360 GNUNET_i2s (&msg->peer), ntohl (msg->port));
361 chid = ntohl (msg->channel_id);
363 /* Sanity check for duplicate channel IDs */
364 if (NULL != channel_get_by_local_id (c, chid))
367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
371 peer = peer_get (&msg->peer);
372 if (NULL == peer->tunnel)
374 peer->tunnel = tunnel_new ();
375 peer->tunnel->peer = peer;
376 if (peer->id == myid)
378 tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY);
388 ch = channel_new (t, c, chid);
392 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
395 ch->port = ntohl (msg->port);
396 channel_set_options (ch, ntohl (msg->opt));
398 /* In unreliable channels, we'll use the DLL to buffer BCK data */
399 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
400 ch->root_rel->ch = ch;
401 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
403 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
404 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
406 /* Send create channel */
408 struct GNUNET_MESH_ChannelCreate msgcc;
410 msgcc.header.size = htons (sizeof (msgcc));
411 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
412 msgcc.chid = htonl (ch->gid);
413 msgcc.port = msg->port;
414 msgcc.opt = msg->opt;
416 GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
419 GNUNET_SERVER_receive_done (client, GNUNET_OK);
425 * Handler for requests of deleting tunnels
428 * @param client identification of the client
429 * @param message the actual message
432 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
433 const struct GNUNET_MessageHeader *message)
435 struct GNUNET_MESH_ChannelMessage *msg;
436 struct MeshClient *c;
437 struct MeshChannel *ch;
438 struct MeshTunnel2 *t;
439 MESH_ChannelNumber chid;
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Got a DESTROY CHANNEL from client!\n");
444 /* Sanity check for client registration */
445 if (NULL == (c = client_get (client)))
448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
451 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
453 /* Message sanity check */
454 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
461 msg = (struct GNUNET_MESH_ChannelMessage *) message;
463 /* Retrieve tunnel */
464 chid = ntohl (msg->channel_id);
465 ch = channel_get_by_local_id (c, chid);
468 LOG (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
474 /* Cleanup after the tunnel */
475 client_delete_channel (c, ch);
476 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
480 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
486 LOG (GNUNET_ERROR_TYPE_ERROR,
487 " channel %X client %p (%p, %p)\n",
488 chid, c, ch->root, ch->dest);
493 channel_destroy (ch);
494 tunnel_destroy_if_empty (t);
496 GNUNET_SERVER_receive_done (client, GNUNET_OK);
502 * Handler for client traffic
505 * @param client identification of the client
506 * @param message the actual message
509 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
510 const struct GNUNET_MessageHeader *message)
512 struct GNUNET_MESH_LocalData *msg;
513 struct MeshClient *c;
514 struct MeshChannel *ch;
515 struct MeshChannelReliability *rel;
516 MESH_ChannelNumber chid;
520 LOG (GNUNET_ERROR_TYPE_DEBUG,
521 "Got data from a client!\n");
523 /* Sanity check for client registration */
524 if (NULL == (c = client_get (client)))
527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
530 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
532 msg = (struct GNUNET_MESH_LocalData *) message;
534 /* Sanity check for message size */
535 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
536 if (size < sizeof (struct GNUNET_MessageHeader))
539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
543 /* Channel exists? */
544 chid = ntohl (msg->id);
545 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
546 ch = channel_get_by_local_id (c, chid);
550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
554 /* Is the client in the channel? */
557 ch->root->handle == client)
561 ch->dest->handle == client) ) )
564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
568 rel = fwd ? ch->root_rel : ch->dest_rel;
569 rel->client_ready = GNUNET_NO;
571 /* Ok, everything is correct, send the message. */
573 struct GNUNET_MESH_Data *payload;
574 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
575 unsigned char cbuf[p2p_size];
577 payload = (struct GNUNET_MESH_Data *) cbuf;
578 payload->mid = htonl (rel->mid_send);
580 memcpy (&payload[1], &msg[1], size);
581 payload->header.size = htons (p2p_size);
582 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
583 payload->chid = htonl (ch->gid);
584 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
585 send_prebuilt_message_channel (&payload->header, ch, fwd);
587 if (GNUNET_YES == ch->reliable)
588 channel_save_copy (ch, &payload->header, fwd);
590 if (tunnel_get_buffer (ch->t, fwd) > 0)
591 send_local_ack (ch, fwd);
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
593 GNUNET_SERVER_receive_done (client, GNUNET_OK);
600 * Handler for client's ACKs for payload traffic.
602 * @param cls Closure (unused).
603 * @param client Identification of the client.
604 * @param message The actual message.
607 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
608 const struct GNUNET_MessageHeader *message)
610 struct GNUNET_MESH_LocalAck *msg;
611 struct MeshChannelReliability *rel;
612 struct MeshChannel *ch;
613 struct MeshClient *c;
614 MESH_ChannelNumber chid;
617 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
619 /* Sanity check for client registration */
620 if (NULL == (c = client_get (client)))
623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
626 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
628 msg = (struct GNUNET_MESH_LocalAck *) message;
630 /* Channel exists? */
631 chid = ntohl (msg->channel_id);
632 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
633 ch = channel_get_by_local_id (c, chid);
634 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
638 LOG (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
639 LOG (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
640 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
644 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
645 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
646 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
647 rel = fwd ? ch->dest_rel : ch->root_rel;
649 rel->client_ready = GNUNET_YES;
650 channel_send_client_buffered_data (ch, c, fwd);
651 send_ack (NULL, ch, fwd);
653 GNUNET_SERVER_receive_done (client, GNUNET_OK);
660 * Iterator over all tunnels to send a monitoring client info about each tunnel.
662 * @param cls Closure (client handle).
663 * @param key Key (hashed tunnel ID, unused).
664 * @param value Tunnel info.
666 * @return GNUNET_YES, to keep iterating.
669 // monitor_all_tunnels_iterator (void *cls,
670 // const struct GNUNET_HashCode * key,
673 // struct GNUNET_SERVER_Client *client = cls;
674 // struct MeshChannel *ch = value;
675 // struct GNUNET_MESH_LocalMonitor *msg;
677 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
678 // msg->channel_id = htonl (ch->gid);
679 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
680 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
682 // LOG (GNUNET_ERROR_TYPE_INFO,
683 // "* sending info about tunnel %s\n",
684 // GNUNET_i2s (&msg->owner));
686 // GNUNET_SERVER_notification_context_unicast (nc, client,
687 // &msg->header, GNUNET_NO);
688 // return GNUNET_YES;
693 * Handler for client's MONITOR request.
695 * @param cls Closure (unused).
696 * @param client Identification of the client.
697 * @param message The actual message.
700 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
701 const struct GNUNET_MessageHeader *message)
703 struct MeshClient *c;
705 /* Sanity check for client registration */
706 if (NULL == (c = client_get (client)))
709 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
713 LOG (GNUNET_ERROR_TYPE_INFO,
714 "Received get tunnels request from client %u\n",
716 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
717 // monitor_all_tunnels_iterator,
719 LOG (GNUNET_ERROR_TYPE_INFO,
720 "Get tunnels request from client %u completed\n",
722 GNUNET_SERVER_receive_done (client, GNUNET_OK);
727 * Handler for client's MONITOR_TUNNEL request.
729 * @param cls Closure (unused).
730 * @param client Identification of the client.
731 * @param message The actual message.
734 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
735 const struct GNUNET_MessageHeader *message)
737 const struct GNUNET_MESH_LocalMonitor *msg;
738 struct GNUNET_MESH_LocalMonitor *resp;
739 struct MeshClient *c;
740 struct MeshChannel *ch;
742 /* Sanity check for client registration */
743 if (NULL == (c = client_get (client)))
746 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
750 msg = (struct GNUNET_MESH_LocalMonitor *) message;
751 LOG (GNUNET_ERROR_TYPE_INFO,
752 "Received tunnel info request from client %u for tunnel %s[%X]\n",
755 ntohl (msg->channel_id));
756 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
760 /* We don't know the tunnel */
761 struct GNUNET_MESH_LocalMonitor warn;
764 GNUNET_SERVER_notification_context_unicast (nc, client,
767 GNUNET_SERVER_receive_done (client, GNUNET_OK);
771 /* Initialize context */
772 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
774 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
775 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
776 &resp->header, GNUNET_NO);
779 LOG (GNUNET_ERROR_TYPE_INFO,
780 "Monitor tunnel request from client %u completed\n",
782 GNUNET_SERVER_receive_done (client, GNUNET_OK);
787 * Functions to handle messages from clients
789 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
790 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
791 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
792 sizeof (struct GNUNET_MESH_ChannelMessage)},
793 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
794 sizeof (struct GNUNET_MESH_ChannelMessage)},
795 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
796 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
797 sizeof (struct GNUNET_MESH_LocalAck)},
798 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
799 sizeof (struct GNUNET_MessageHeader)},
800 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
801 sizeof (struct GNUNET_MESH_LocalMonitor)},
807 /******************************************************************************/
808 /******************************** API ***********************************/
809 /******************************************************************************/
812 * Initialize server subsystem.
814 * @param handle Server handle.
817 GML_init (struct GNUNET_SERVER_Handle *handle)
819 server_handle = handle;
820 GNUNET_SERVER_suspend (server_handle);
821 ports = GNUNET_CONTAINER_multihashmap32_create (32);
826 * Install server (service) handlers and start listening to clients.
831 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
832 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
833 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
835 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
840 GNUNET_SERVER_resume (server_handle);
852 GNUNET_SERVER_notification_context_destroy (nc);
859 * Get a chennel from a client
861 * @param c the client to check
862 * @param chid Channel ID, must be local (> 0x800...)
864 * @return non-NULL if channel exists in the clients lists
867 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
869 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
872 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
875 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
876 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
877 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
882 * Add a channel to a client
884 * @param client Client.
885 * @param chid Channel ID.
889 GML_channel_add (struct MeshClient *client,
891 struct MeshChannel *ch)
893 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
894 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
895 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
896 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
897 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
898 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
905 * Remove a channel from a client
907 * @param client Client.
908 * @param chid Channel ID.
912 GML_channel_remove (struct MeshClient *client,
914 struct MeshChannel *ch)
916 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
917 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels, chid, ch);
918 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
919 GNUNET_CONTAINER_multihashmap32_remove (c->own_channels, chid, ch);
926 * Get the tunnel's next free local channel ID.
930 * @return LID of a channel free to use.
933 GML_get_next_chid (struct MeshClient *c)
935 MESH_ChannelNumber chid;
937 while (NULL != GML_channel_get (c, c->next_chid))
939 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
940 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
943 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
950 * Check if client has registered with the service and has not disconnected
952 * @param client the client to check
954 * @return non-NULL if client exists in the global DLL
957 GML_client_get (struct GNUNET_SERVER_Client *client)
959 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
963 * Find a client that has opened a port
965 * @param port Port to check.
967 * @return non-NULL if a client has the port.
970 GML_client_get_by_port (uint32_t port)
972 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
977 * Deletes a tunnel from a client (either owner or destination).
979 * @param c Client whose tunnel to delete.
980 * @param ch Channel which should be deleted.
983 GML_client_delete_channel (struct MeshClient *c, struct MeshChannel *ch)
989 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
991 if (GNUNET_YES != res)
992 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel owner KO\n");
996 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
998 if (GNUNET_YES != res)
999 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n");
1004 * Build a local ACK message and send it to a local client, if needed.
1006 * If the client was already allowed to send data, do nothing.
1008 * @param ch Channel on which to send the ACK.
1009 * @param c Client to whom send the ACK.
1010 * @param fwd Set to GNUNET_YES for FWD ACK (dest->root)
1013 GML_send_ack (struct MeshChannel *ch, int fwd)
1015 struct GNUNET_MESH_LocalAck msg;
1016 struct MeshChannelReliability *rel;
1017 struct MeshClient *c;
1019 c = fwd ? ch->root : ch->dest;
1020 rel = fwd ? ch->root_rel : ch->dest_rel;
1022 if (GNUNET_YES == rel->client_ready)
1023 return; /* don't send double ACKs to client */
1025 rel->client_ready = GNUNET_YES;
1027 LOG (GNUNET_ERROR_TYPE_DEBUG,
1028 "send local %s ack on %s:%X towards %p\n",
1029 fwd ? "FWD" : "BCK", peer2s (ch->t->peer), ch->gid, c);
1032 || ( fwd && (0 == ch->lid_root || c != ch->root))
1033 || (!fwd && (0 == ch->lid_dest || c != ch->dest)) )
1038 msg.header.size = htons (sizeof (msg));
1039 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1040 msg.channel_id = htonl (fwd ? ch->lid_root : ch->lid_dest);
1041 GNUNET_SERVER_notification_context_unicast (nc,
1050 * Notify the client that a new incoming channel was created.
1052 * @param ch Channel that was created.
1055 GML_send_channel_create (struct MeshClient *c,
1056 uint32_t id, uint32_t port, uint32_t opt,
1057 struct GNUNET_PeerIdentity *peer)
1059 struct GNUNET_MESH_ChannelMessage msg;
1061 msg.header.size = htons (sizeof (msg));
1062 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1063 msg.channel_id = htonl (id);
1064 msg.port = htonl (port);
1065 msg.opt = htonl (opt);
1067 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1068 &msg.header, GNUNET_NO);
1073 * Notify a client that a channel is no longer valid.
1076 * @param id ID of the channel that is destroyed.
1079 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
1081 struct GNUNET_MESH_ChannelMessage msg;
1088 msg.header.size = htons (sizeof (msg));
1089 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1090 msg.channel_id = htonl (id);
1091 msg.port = htonl (0);
1092 memset (&msg.peer, 0, sizeof (msg.peer));
1093 msg.opt = htonl (0);
1094 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1095 &msg.header, GNUNET_NO);
1100 * Modify the mesh message ID from global to local and send to client.
1102 * @param msg Message to modify and send.
1103 * @param c Client to send to.
1104 * @param tid Tunnel ID to use (c can be both owner and client).
1107 GML_send_data (struct MeshClient *c,
1108 const struct GNUNET_MESH_Data *msg,
1109 MESH_ChannelNumber id)
1111 struct GNUNET_MESH_LocalData *copy;
1112 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1113 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1115 if (size < sizeof (struct GNUNET_MessageHeader))
1117 GNUNET_break_op (0);
1125 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1126 memcpy (©[1], &msg[1], size);
1127 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1128 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1129 copy->id = htonl (id);
1130 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1131 ©->header, GNUNET_NO);
1136 * Get the static string to represent a client.
1140 * @return Static string for the client.
1143 GML_2s (const struct MeshClient *c)
1145 static char buf[32];
1147 sprintf (buf, "%u", c->id);