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"
32 #define LOG(level, ...) GNUNET_log_from(level,"mesh-loc",__VA_ARGS__)
34 /******************************************************************************/
35 /******************************** STRUCTS **********************************/
36 /******************************************************************************/
39 * Struct containing information about a client of the service
41 * TODO: add a list of 'waiting' ports
48 struct MeshClient *next;
53 struct MeshClient *prev;
56 * Tunnels that belong to this client, indexed by local id
58 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
61 * Tunnels this client has accepted, indexed by incoming local id
63 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
66 * Channel ID for the next incoming channel.
68 MESH_ChannelNumber next_chid;
71 * Handle to communicate with the client
73 struct GNUNET_SERVER_Client *handle;
76 * Ports that this client has declared interest in.
77 * Indexed by port, contains *Client.
79 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
82 * Whether the client is active or shutting down (don't send confirmations
83 * to a client that is shutting down.
88 * ID of the client, mainly for debug messages
93 /******************************************************************************/
94 /******************************* GLOBALS ***********************************/
95 /******************************************************************************/
98 * Global handle to the statistics service.
100 extern struct GNUNET_STATISTICS_Handle *stats;
103 * Handle to server lib.
105 static struct GNUNET_SERVER_Handle *server_handle;
108 * DLL with all the clients, head.
110 static struct MeshClient *clients_head;
113 * DLL with all the clients, tail.
115 static struct MeshClient *clients_tail;
118 * Next ID to assign to a client.
120 unsigned int next_client_id;
123 * All ports clients of this peer have opened.
125 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
128 * Notification context, to send messages to local clients.
130 static struct GNUNET_SERVER_NotificationContext *nc;
133 /******************************************************************************/
134 /******************************** STATIC ***********************************/
135 /******************************************************************************/
138 * Remove client's ports from the global hashmap on disconnect.
140 * @param cls Closure (unused).
142 * @param value Client structure.
144 * @return GNUNET_OK, keep iterating.
147 client_release_ports (void *cls,
153 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
154 if (GNUNET_YES != res)
157 LOG (GNUNET_ERROR_TYPE_WARNING,
158 "Port %u by client %p was not registered.\n",
166 /******************************************************************************/
167 /******************************** HANDLES ***********************************/
168 /******************************************************************************/
172 * Handler for client connection.
174 * @param cls Closure (unused).
175 * @param client Client handler.
178 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
180 struct MeshClient *c;
182 LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
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 * Iterator for deleting each channel whose client endpoint disconnected.
198 * @param cls Closure (client that has disconnected).
199 * @param key The local channel id (used to access the hashmap).
200 * @param value The value stored at the key (channel to destroy).
202 * @return GNUNET_OK, keep iterating.
205 channel_destroy_iterator (void *cls,
209 struct MeshChannel *ch = value;
210 struct MeshClient *c = cls;
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
213 " Channel %s destroy, due to client %s shutdown.\n",
214 GMCH_2s (ch), GML_2s (c));
216 GMCH_handle_local_destroy (ch, c);
221 * Handler for client disconnection
224 * @param client identification of the client; NULL
225 * for the last call when the server is destroyed
228 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
230 struct MeshClient *c;
232 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
235 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
239 c = GML_client_get (client);
242 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
244 GNUNET_SERVER_client_drop (c->handle);
245 c->shutting_down = GNUNET_YES;
246 if (NULL != c->own_channels)
248 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
249 &channel_destroy_iterator, c);
250 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
253 if (NULL != c->incoming_channels)
255 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
256 &channel_destroy_iterator, c);
257 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
260 if (NULL != c->ports)
262 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
263 &client_release_ports, c);
264 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
266 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
267 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
268 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
273 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
275 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
281 * Handler for new clients
284 * @param client identification of the client
285 * @param message the actual message, which includes messages the client wants
288 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
289 const struct GNUNET_MessageHeader *message)
291 struct GNUNET_MESH_ClientConnect *cc_msg;
292 struct MeshClient *c;
297 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
299 /* Check data sanity */
300 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
301 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
302 if (0 != (size % sizeof (uint32_t)))
305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
308 size /= sizeof (uint32_t);
310 /* Initialize new client structure */
311 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
312 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
313 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
318 p = (uint32_t *) &cc_msg[1];
319 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
320 for (i = 0; i < size; i++)
323 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
325 /* store in client's hashmap */
326 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
327 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
328 /* store in global hashmap */
329 /* FIXME only allow one client to have the port open,
330 * have a backup hashmap with waiting clients */
331 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
332 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
336 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
337 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
338 GNUNET_SERVER_notification_context_add (nc, client);
339 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
341 GNUNET_SERVER_receive_done (client, GNUNET_OK);
342 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
347 * Handler for requests of new tunnels
349 * @param cls Closure.
350 * @param client Identification of the client.
351 * @param message The actual message.
354 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
355 const struct GNUNET_MessageHeader *message)
357 struct MeshClient *c;
359 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
361 /* Sanity check for client registration */
362 if (NULL == (c = GML_client_get (client)))
365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
368 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
370 /* Message size sanity check */
371 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
379 GMCH_handle_local_create (c,
380 (struct GNUNET_MESH_ChannelMessage *) message))
382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
386 GNUNET_SERVER_receive_done (client, GNUNET_OK);
392 * Handler for requests of deleting tunnels
395 * @param client identification of the client
396 * @param message the actual message
399 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
400 const struct GNUNET_MessageHeader *message)
402 struct GNUNET_MESH_ChannelMessage *msg;
403 struct MeshClient *c;
404 struct MeshChannel *ch;
405 MESH_ChannelNumber chid;
407 LOG (GNUNET_ERROR_TYPE_DEBUG,
408 "Got a DESTROY CHANNEL from client!\n");
410 /* Sanity check for client registration */
411 if (NULL == (c = GML_client_get (client)))
414 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
417 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
419 /* Message sanity check */
420 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
427 msg = (struct GNUNET_MESH_ChannelMessage *) message;
429 /* Retrieve tunnel */
430 chid = ntohl (msg->channel_id);
431 ch = GML_channel_get (c, chid);
434 LOG (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
440 GMCH_handle_local_destroy (ch, c);
442 GNUNET_SERVER_receive_done (client, GNUNET_OK);
448 * Handler for client traffic
451 * @param client identification of the client
452 * @param message the actual message
455 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
456 const struct GNUNET_MessageHeader *message)
458 struct GNUNET_MESH_LocalData *msg;
459 struct MeshClient *c;
460 struct MeshChannel *ch;
461 MESH_ChannelNumber chid;
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 "Got data from a client!\n");
468 /* Sanity check for client registration */
469 if (NULL == (c = GML_client_get (client)))
472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
475 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
477 msg = (struct GNUNET_MESH_LocalData *) message;
479 /* Sanity check for message size */
480 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
481 if (size < sizeof (struct GNUNET_MessageHeader))
484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
488 /* Channel exists? */
489 chid = ntohl (msg->id);
490 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
491 ch = GML_channel_get (c, chid);
495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
500 GMCH_handle_local_data (ch, c,
501 (struct GNUNET_MessageHeader *)&msg[1], fwd))
503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
507 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
508 GNUNET_SERVER_receive_done (client, GNUNET_OK);
515 * Handler for client's ACKs for payload traffic.
517 * @param cls Closure (unused).
518 * @param client Identification of the client.
519 * @param message The actual message.
522 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
523 const struct GNUNET_MessageHeader *message)
525 struct GNUNET_MESH_LocalAck *msg;
526 struct MeshChannel *ch;
527 struct MeshClient *c;
528 MESH_ChannelNumber chid;
531 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
533 /* Sanity check for client registration */
534 if (NULL == (c = GML_client_get (client)))
537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
540 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
542 msg = (struct GNUNET_MESH_LocalAck *) message;
544 /* Channel exists? */
545 chid = ntohl (msg->channel_id);
546 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
547 ch = GML_channel_get (c, chid);
548 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
552 LOG (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
553 LOG (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
558 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
559 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
560 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
562 GMCH_handle_local_ack (ch, fwd);
563 GNUNET_SERVER_receive_done (client, GNUNET_OK);
570 * Iterator over all tunnels to send a monitoring client info about each tunnel.
572 * @param cls Closure (client handle).
573 * @param key Key (hashed tunnel ID, unused).
574 * @param value Tunnel info.
576 * @return GNUNET_YES, to keep iterating.
579 // monitor_all_tunnels_iterator (void *cls,
580 // const struct GNUNET_HashCode * key,
583 // struct GNUNET_SERVER_Client *client = cls;
584 // struct MeshChannel *ch = value;
585 // struct GNUNET_MESH_LocalMonitor *msg;
587 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
588 // msg->channel_id = htonl (ch->gid);
589 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
590 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
592 // LOG (GNUNET_ERROR_TYPE_INFO,
593 // "* sending info about tunnel %s\n",
594 // GNUNET_i2s (&msg->owner));
596 // GNUNET_SERVER_notification_context_unicast (nc, client,
597 // &msg->header, GNUNET_NO);
598 // return GNUNET_YES;
603 * Handler for client's MONITOR request.
605 * @param cls Closure (unused).
606 * @param client Identification of the client.
607 * @param message The actual message.
610 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
611 const struct GNUNET_MessageHeader *message)
613 struct MeshClient *c;
615 /* Sanity check for client registration */
616 if (NULL == (c = GML_client_get (client)))
619 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
623 LOG (GNUNET_ERROR_TYPE_INFO,
624 "Received get tunnels request from client %u\n",
626 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
627 // monitor_all_tunnels_iterator,
629 LOG (GNUNET_ERROR_TYPE_INFO,
630 "Get tunnels request from client %u completed\n",
632 GNUNET_SERVER_receive_done (client, GNUNET_OK);
637 * Handler for client's MONITOR_TUNNEL request.
639 * @param cls Closure (unused).
640 * @param client Identification of the client.
641 * @param message The actual message.
644 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
645 const struct GNUNET_MessageHeader *message)
647 const struct GNUNET_MESH_LocalMonitor *msg;
648 struct GNUNET_MESH_LocalMonitor *resp;
649 struct MeshClient *c;
650 struct MeshChannel *ch;
652 /* Sanity check for client registration */
653 if (NULL == (c = GML_client_get (client)))
656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
660 msg = (struct GNUNET_MESH_LocalMonitor *) message;
661 LOG (GNUNET_ERROR_TYPE_INFO,
662 "Received tunnel info request from client %u for tunnel %s[%X]\n",
665 ntohl (msg->channel_id));
666 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
670 /* We don't know the tunnel */
671 struct GNUNET_MESH_LocalMonitor warn;
674 GNUNET_SERVER_notification_context_unicast (nc, client,
677 GNUNET_SERVER_receive_done (client, GNUNET_OK);
681 /* Initialize context */
682 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
684 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
685 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
686 &resp->header, GNUNET_NO);
689 LOG (GNUNET_ERROR_TYPE_INFO,
690 "Monitor tunnel request from client %u completed\n",
692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
697 * Functions to handle messages from clients
699 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
700 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
701 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
702 sizeof (struct GNUNET_MESH_ChannelMessage)},
703 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
704 sizeof (struct GNUNET_MESH_ChannelMessage)},
705 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
706 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
707 sizeof (struct GNUNET_MESH_LocalAck)},
708 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
709 sizeof (struct GNUNET_MessageHeader)},
710 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
711 sizeof (struct GNUNET_MESH_LocalMonitor)},
717 /******************************************************************************/
718 /******************************** API ***********************************/
719 /******************************************************************************/
722 * Initialize server subsystem.
724 * @param handle Server handle.
727 GML_init (struct GNUNET_SERVER_Handle *handle)
729 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
730 server_handle = handle;
731 GNUNET_SERVER_suspend (server_handle);
732 ports = GNUNET_CONTAINER_multihashmap32_create (32);
737 * Install server (service) handlers and start listening to clients.
742 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
743 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
744 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
746 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
751 GNUNET_SERVER_resume (server_handle);
763 GNUNET_SERVER_notification_context_destroy (nc);
770 * Get a chennel from a client
772 * @param c the client to check
773 * @param chid Channel ID, must be local (> 0x800...)
775 * @return non-NULL if channel exists in the clients lists
778 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
780 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
783 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
786 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
787 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
788 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
793 * Add a channel to a client
795 * @param client Client.
796 * @param chid Channel ID.
800 GML_channel_add (struct MeshClient *client,
802 struct MeshChannel *ch)
804 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
805 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
806 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
807 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
808 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
809 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
816 * Remove a channel from a client
818 * @param client Client.
819 * @param chid Channel ID.
823 GML_channel_remove (struct MeshClient *client,
825 struct MeshChannel *ch)
827 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
828 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels, chid, ch);
829 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= chid)
830 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels, chid, ch);
837 * Get the tunnel's next free local channel ID.
841 * @return LID of a channel free to use.
844 GML_get_next_chid (struct MeshClient *c)
846 MESH_ChannelNumber chid;
848 while (NULL != GML_channel_get (c, c->next_chid))
850 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
851 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
854 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
861 * Check if client has registered with the service and has not disconnected
863 * @param client the client to check
865 * @return non-NULL if client exists in the global DLL
868 GML_client_get (struct GNUNET_SERVER_Client *client)
870 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
874 * Find a client that has opened a port
876 * @param port Port to check.
878 * @return non-NULL if a client has the port.
881 GML_client_get_by_port (uint32_t port)
883 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
888 * Deletes a channel from a client (either owner or destination).
890 * @param c Client whose tunnel to delete.
891 * @param ch Channel which should be deleted.
892 * @param id Channel ID.
895 GML_client_delete_channel (struct MeshClient *c,
896 struct MeshChannel *ch,
897 MESH_ChannelNumber id)
901 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= id)
903 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
905 if (GNUNET_YES != res)
906 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
908 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= id)
910 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
912 if (GNUNET_YES != res)
913 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
922 * Build a local ACK message and send it to a local client, if needed.
924 * If the client was already allowed to send data, do nothing.
926 * @param c Client to whom send the ACK.
927 * @param id Channel ID to use
930 GML_send_ack (struct MeshClient *c, MESH_ChannelNumber id)
932 struct GNUNET_MESH_LocalAck msg;
934 LOG (GNUNET_ERROR_TYPE_DEBUG,
935 "send local %s ack on %X towards %p\n",
936 id < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
938 msg.header.size = htons (sizeof (msg));
939 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
940 msg.channel_id = htonl (id);
941 GNUNET_SERVER_notification_context_unicast (nc,
950 * Notify the client that a new incoming channel was created.
952 * @param ch Channel that was created.
955 GML_send_channel_create (struct MeshClient *c,
956 uint32_t id, uint32_t port, uint32_t opt,
957 const struct GNUNET_PeerIdentity *peer)
959 struct GNUNET_MESH_ChannelMessage msg;
961 msg.header.size = htons (sizeof (msg));
962 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
963 msg.channel_id = htonl (id);
964 msg.port = htonl (port);
965 msg.opt = htonl (opt);
967 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
968 &msg.header, GNUNET_NO);
973 * Notify a client that a channel is no longer valid.
976 * @param id ID of the channel that is destroyed.
979 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
981 struct GNUNET_MESH_ChannelMessage msg;
988 msg.header.size = htons (sizeof (msg));
989 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
990 msg.channel_id = htonl (id);
991 msg.port = htonl (0);
992 memset (&msg.peer, 0, sizeof (msg.peer));
994 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
995 &msg.header, GNUNET_NO);
1000 * Modify the mesh message ID from global to local and send to client.
1002 * @param c Client to send to.
1003 * @param msg Message to modify and send.
1004 * @param id Channel ID to use (c can be both owner and client).
1007 GML_send_data (struct MeshClient *c,
1008 const struct GNUNET_MESH_Data *msg,
1009 MESH_ChannelNumber id)
1011 struct GNUNET_MESH_LocalData *copy;
1012 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1013 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1015 if (size < sizeof (struct GNUNET_MessageHeader))
1017 GNUNET_break_op (0);
1025 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1026 memcpy (©[1], &msg[1], size);
1027 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1028 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1029 copy->id = htonl (id);
1030 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1031 ©->header, GNUNET_NO);
1036 * Get the static string to represent a client.
1040 * @return Static string for the client.
1043 GML_2s (const struct MeshClient *c)
1045 static char buf[32];
1047 sprintf (buf, "%u", c->id);