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.h" /* GNUNET_MESH_Data is shared */
30 #include "gnunet-service-mesh_local.h"
31 #include "gnunet-service-mesh_channel.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;
183 LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
186 c = GNUNET_new (struct MeshClient);
188 c->id = next_client_id++; /* overflow not important: just for debug */
189 c->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
190 GNUNET_SERVER_client_keep (client);
191 GNUNET_SERVER_client_set_user_context (client, c);
192 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
197 * Iterator for deleting each channel whose client endpoint disconnected.
199 * @param cls Closure (client that has disconnected).
200 * @param key The local channel id (used to access the hashmap).
201 * @param value The value stored at the key (channel to destroy).
203 * @return GNUNET_OK, keep iterating.
206 channel_destroy_iterator (void *cls,
210 struct MeshChannel *ch = value;
211 struct MeshClient *c = cls;
213 LOG (GNUNET_ERROR_TYPE_DEBUG,
214 " Channel %s destroy, due to client %s shutdown.\n",
215 GMCH_2s (ch), GML_2s (c));
217 GMCH_handle_local_destroy (ch, c, key < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
222 * Handler for client disconnection
225 * @param client identification of the client; NULL
226 * for the last call when the server is destroyed
229 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
231 struct MeshClient *c;
233 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
236 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
240 c = GML_client_get (client);
243 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
245 GNUNET_SERVER_client_drop (c->handle);
246 c->shutting_down = GNUNET_YES;
247 if (NULL != c->own_channels)
249 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
250 &channel_destroy_iterator, c);
251 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
254 if (NULL != c->incoming_channels)
256 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
257 &channel_destroy_iterator, c);
258 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
261 if (NULL != c->ports)
263 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
264 &client_release_ports, c);
265 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
267 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
268 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
269 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
274 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
276 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
282 * Handler for new clients
285 * @param client identification of the client
286 * @param message the actual message, which includes messages the client wants
289 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
290 const struct GNUNET_MessageHeader *message)
292 struct GNUNET_MESH_ClientConnect *cc_msg;
293 struct MeshClient *c;
298 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nnew client connected %p\n", client);
300 /* Check data sanity */
301 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
302 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
303 if (0 != (size % sizeof (uint32_t)))
306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
309 size /= sizeof (uint32_t);
311 /* Initialize new client structure */
312 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
313 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
314 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
319 p = (uint32_t *) &cc_msg[1];
320 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
321 for (i = 0; i < size; i++)
324 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
326 /* store in client's hashmap */
327 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
328 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
329 /* store in global hashmap */
330 /* FIXME only allow one client to have the port open,
331 * have a backup hashmap with waiting clients */
332 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
337 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
338 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
339 GNUNET_SERVER_notification_context_add (nc, client);
340 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
342 GNUNET_SERVER_receive_done (client, GNUNET_OK);
343 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
348 * Handler for requests of new tunnels
350 * @param cls Closure.
351 * @param client Identification of the client.
352 * @param message The actual message.
355 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
356 const struct GNUNET_MessageHeader *message)
358 struct MeshClient *c;
360 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
361 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
363 /* Sanity check for client registration */
364 if (NULL == (c = GML_client_get (client)))
367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
370 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
372 /* Message size sanity check */
373 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
381 GMCH_handle_local_create (c,
382 (struct GNUNET_MESH_ChannelMessage *) message))
384 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
388 GNUNET_SERVER_receive_done (client, GNUNET_OK);
394 * Handler for requests of deleting tunnels
397 * @param client identification of the client
398 * @param message the actual message
401 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
402 const struct GNUNET_MessageHeader *message)
404 struct GNUNET_MESH_ChannelMessage *msg;
405 struct MeshClient *c;
406 struct MeshChannel *ch;
407 MESH_ChannelNumber chid;
409 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot a DESTROY CHANNEL from client!\n");
411 /* Sanity check for client registration */
412 if (NULL == (c = GML_client_get (client)))
415 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
418 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
420 /* Message sanity check */
421 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
424 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
428 msg = (struct GNUNET_MESH_ChannelMessage *) message;
430 /* Retrieve tunnel */
431 chid = ntohl (msg->channel_id);
432 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
433 ch = GML_channel_get (c, chid);
436 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
437 GNUNET_STATISTICS_update (stats,
438 "# client destroy messages on unknown channel",
440 GNUNET_SERVER_receive_done (client, GNUNET_OK);
444 GMCH_handle_local_destroy (ch, c, chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
446 GNUNET_SERVER_receive_done (client, GNUNET_OK);
452 * Handler for client traffic
455 * @param client identification of the client
456 * @param message the actual message
459 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
460 const struct GNUNET_MessageHeader *message)
462 struct GNUNET_MESH_LocalData *msg;
463 struct MeshClient *c;
464 struct MeshChannel *ch;
465 MESH_ChannelNumber chid;
469 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot data from a client!\n");
471 /* Sanity check for client registration */
472 if (NULL == (c = GML_client_get (client)))
475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
478 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
480 msg = (struct GNUNET_MESH_LocalData *) message;
482 /* Sanity check for message size */
483 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
484 if (size < sizeof (struct GNUNET_MessageHeader))
487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
491 /* Channel exists? */
492 chid = ntohl (msg->id);
493 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
494 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
495 ch = GML_channel_get (c, chid);
498 GNUNET_STATISTICS_update (stats,
499 "# client data messages on unknown channel",
501 GNUNET_SERVER_receive_done (client, GNUNET_OK);
506 GMCH_handle_local_data (ch, c,
507 (struct GNUNET_MessageHeader *)&msg[1], fwd))
509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
513 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
521 * Handler for client's ACKs for payload traffic.
523 * @param cls Closure (unused).
524 * @param client Identification of the client.
525 * @param message The actual message.
528 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
529 const struct GNUNET_MessageHeader *message)
531 struct GNUNET_MESH_LocalAck *msg;
532 struct MeshChannel *ch;
533 struct MeshClient *c;
534 MESH_ChannelNumber chid;
537 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot a local ACK\n");
539 /* Sanity check for client registration */
540 if (NULL == (c = GML_client_get (client)))
543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
546 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
548 msg = (struct GNUNET_MESH_LocalAck *) message;
550 /* Channel exists? */
551 chid = ntohl (msg->channel_id);
552 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
553 ch = GML_channel_get (c, chid);
554 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
557 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
558 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
559 GNUNET_STATISTICS_update (stats,
560 "# client ack messages on unknown channel",
562 GNUNET_SERVER_receive_done (client, GNUNET_OK);
566 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
567 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
568 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
570 GMCH_handle_local_ack (ch, fwd);
571 GNUNET_SERVER_receive_done (client, GNUNET_OK);
578 * Iterator over all tunnels to send a monitoring client info about each tunnel.
580 * @param cls Closure (client handle).
581 * @param key Key (hashed tunnel ID, unused).
582 * @param value Tunnel info.
584 * @return #GNUNET_YES, to keep iterating.
587 // monitor_all_tunnels_iterator (void *cls,
588 // const struct GNUNET_HashCode * key,
591 // struct GNUNET_SERVER_Client *client = cls;
592 // struct MeshChannel *ch = value;
593 // struct GNUNET_MESH_LocalMonitor *msg;
595 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
596 // msg->channel_id = htonl (ch->gid);
597 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
598 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
600 // LOG (GNUNET_ERROR_TYPE_INFO,
601 // "* sending info about tunnel %s\n",
602 // GNUNET_i2s (&msg->owner));
604 // GNUNET_SERVER_notification_context_unicast (nc, client,
605 // &msg->header, GNUNET_NO);
606 // return GNUNET_YES;
611 * Handler for client's MONITOR request.
613 * @param cls Closure (unused).
614 * @param client Identification of the client.
615 * @param message The actual message.
618 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
619 const struct GNUNET_MessageHeader *message)
621 struct MeshClient *c;
623 /* Sanity check for client registration */
624 if (NULL == (c = GML_client_get (client)))
627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
631 LOG (GNUNET_ERROR_TYPE_INFO,
632 "Received get tunnels request from client %u\n",
634 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
635 // monitor_all_tunnels_iterator,
637 LOG (GNUNET_ERROR_TYPE_INFO,
638 "Get tunnels request from client %u completed\n",
640 GNUNET_SERVER_receive_done (client, GNUNET_OK);
645 * Handler for client's MONITOR_TUNNEL request.
647 * @param cls Closure (unused).
648 * @param client Identification of the client.
649 * @param message The actual message.
652 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
653 const struct GNUNET_MessageHeader *message)
655 const struct GNUNET_MESH_LocalMonitor *msg;
656 struct GNUNET_MESH_LocalMonitor *resp;
657 struct MeshClient *c;
658 struct MeshChannel *ch;
660 /* Sanity check for client registration */
661 if (NULL == (c = GML_client_get (client)))
664 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
668 msg = (struct GNUNET_MESH_LocalMonitor *) message;
669 LOG (GNUNET_ERROR_TYPE_INFO,
670 "Received tunnel info request from client %u for tunnel %s[%X]\n",
673 ntohl (msg->channel_id));
674 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
678 /* We don't know the tunnel */
679 struct GNUNET_MESH_LocalMonitor warn;
682 GNUNET_SERVER_notification_context_unicast (nc, client,
685 GNUNET_SERVER_receive_done (client, GNUNET_OK);
689 /* Initialize context */
690 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
692 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
693 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
694 &resp->header, GNUNET_NO);
697 LOG (GNUNET_ERROR_TYPE_INFO,
698 "Monitor tunnel request from client %u completed\n",
700 GNUNET_SERVER_receive_done (client, GNUNET_OK);
705 * Functions to handle messages from clients
707 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
708 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
709 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
710 sizeof (struct GNUNET_MESH_ChannelMessage)},
711 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
712 sizeof (struct GNUNET_MESH_ChannelMessage)},
713 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
714 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
715 sizeof (struct GNUNET_MESH_LocalAck)},
716 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
717 sizeof (struct GNUNET_MessageHeader)},
718 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
719 sizeof (struct GNUNET_MESH_LocalMonitor)},
725 /******************************************************************************/
726 /******************************** API ***********************************/
727 /******************************************************************************/
730 * Initialize server subsystem.
732 * @param handle Server handle.
735 GML_init (struct GNUNET_SERVER_Handle *handle)
737 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
738 server_handle = handle;
739 GNUNET_SERVER_suspend (server_handle);
740 ports = GNUNET_CONTAINER_multihashmap32_create (32);
745 * Install server (service) handlers and start listening to clients.
750 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
751 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
752 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
754 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
759 GNUNET_SERVER_resume (server_handle);
771 GNUNET_SERVER_notification_context_destroy (nc);
778 * Get a channel from a client.
780 * @param c Client to check.
781 * @param chid Channel ID, must be local (> 0x800...).
783 * @return non-NULL if channel exists in the clients lists
786 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
788 struct GNUNET_CONTAINER_MultiHashMap32 *map;
790 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
793 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
797 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
798 map = c->incoming_channels;
799 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
800 map = c->own_channels;
809 LOG (GNUNET_ERROR_TYPE_DEBUG,
810 "Client %s does no t have a valid map for CHID %X\n",
814 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
819 * Add a channel to a client
821 * @param client Client.
822 * @param chid Channel ID.
826 GML_channel_add (struct MeshClient *client,
828 struct MeshChannel *ch)
830 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
831 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
833 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
834 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
835 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
842 * Remove a channel from a client.
844 * @param client Client.
845 * @param chid Channel ID.
849 GML_channel_remove (struct MeshClient *client,
851 struct MeshChannel *ch)
853 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
854 GNUNET_break (GNUNET_YES ==
855 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
857 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= chid)
858 GNUNET_break (GNUNET_YES ==
859 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
867 * Get the tunnel's next free local channel ID.
871 * @return LID of a channel free to use.
874 GML_get_next_chid (struct MeshClient *c)
876 MESH_ChannelNumber chid;
878 while (NULL != GML_channel_get (c, c->next_chid))
880 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
881 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
884 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
891 * Check if client has registered with the service and has not disconnected
893 * @param client the client to check
895 * @return non-NULL if client exists in the global DLL
898 GML_client_get (struct GNUNET_SERVER_Client *client)
900 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
904 * Find a client that has opened a port
906 * @param port Port to check.
908 * @return non-NULL if a client has the port.
911 GML_client_get_by_port (uint32_t port)
913 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
918 * Deletes a channel from a client (either owner or destination).
920 * @param c Client whose tunnel to delete.
921 * @param ch Channel which should be deleted.
922 * @param id Channel ID.
925 GML_client_delete_channel (struct MeshClient *c,
926 struct MeshChannel *ch,
927 MESH_ChannelNumber id)
931 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= id)
933 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
935 if (GNUNET_YES != res)
936 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
938 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= id)
940 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
942 if (GNUNET_YES != res)
943 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
952 * Build a local ACK message and send it to a local client, if needed.
954 * If the client was already allowed to send data, do nothing.
956 * @param c Client to whom send the ACK.
957 * @param id Channel ID to use
960 GML_send_ack (struct MeshClient *c, MESH_ChannelNumber id)
962 struct GNUNET_MESH_LocalAck msg;
964 LOG (GNUNET_ERROR_TYPE_DEBUG,
965 "send local %s ack on %X towards %p\n",
966 id < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
968 msg.header.size = htons (sizeof (msg));
969 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
970 msg.channel_id = htonl (id);
971 GNUNET_SERVER_notification_context_unicast (nc,
980 * Build a local channel NACK message and send it to a local client.
982 * @param c Client to whom send the NACK.
983 * @param id Channel ID to use
986 GML_send_nack (struct MeshClient *c, MESH_ChannelNumber id)
988 struct GNUNET_MESH_LocalAck msg;
990 LOG (GNUNET_ERROR_TYPE_DEBUG,
991 "send local nack on %X towards %p\n",
994 msg.header.size = htons (sizeof (msg));
995 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK);
996 msg.channel_id = htonl (id);
997 GNUNET_SERVER_notification_context_unicast (nc,
1006 * Notify the client that a new incoming channel was created.
1008 * @param c Client to notify.
1009 * @param id Channel ID.
1010 * @param port Channel's destination port.
1011 * @param opt Options (bit array).
1012 * @param peer Origin peer.
1015 GML_send_channel_create (struct MeshClient *c,
1016 uint32_t id, uint32_t port, uint32_t opt,
1017 const struct GNUNET_PeerIdentity *peer)
1019 struct GNUNET_MESH_ChannelMessage msg;
1021 msg.header.size = htons (sizeof (msg));
1022 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1023 msg.channel_id = htonl (id);
1024 msg.port = htonl (port);
1025 msg.opt = htonl (opt);
1027 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1028 &msg.header, GNUNET_NO);
1033 * Notify a client that a channel is no longer valid.
1036 * @param id ID of the channel that is destroyed.
1039 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
1041 struct GNUNET_MESH_ChannelMessage msg;
1048 if (GNUNET_YES == c->shutting_down)
1050 msg.header.size = htons (sizeof (msg));
1051 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1052 msg.channel_id = htonl (id);
1053 msg.port = htonl (0);
1054 memset (&msg.peer, 0, sizeof (msg.peer));
1055 msg.opt = htonl (0);
1056 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1057 &msg.header, GNUNET_NO);
1062 * Modify the mesh message ID from global to local and send to client.
1064 * @param c Client to send to.
1065 * @param msg Message to modify and send.
1066 * @param id Channel ID to use (c can be both owner and client).
1069 GML_send_data (struct MeshClient *c,
1070 const struct GNUNET_MESH_Data *msg,
1071 MESH_ChannelNumber id)
1073 struct GNUNET_MESH_LocalData *copy;
1074 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1075 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1077 if (size < sizeof (struct GNUNET_MessageHeader))
1079 GNUNET_break_op (0);
1087 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1088 memcpy (©[1], &msg[1], size);
1089 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1090 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1091 copy->id = htonl (id);
1092 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1093 ©->header, GNUNET_NO);
1098 * Get the static string to represent a client.
1102 * @return Static string for the client.
1105 GML_2s (const struct MeshClient *c)
1107 static char buf[32];
1109 sprintf (buf, "%u", c->id);