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"
34 #include "gnunet-service-mesh_tunnel.h"
36 #define LOG(level, ...) GNUNET_log_from(level,"mesh-loc",__VA_ARGS__)
38 /******************************************************************************/
39 /******************************** STRUCTS **********************************/
40 /******************************************************************************/
43 * Struct containing information about a client of the service
45 * TODO: add a list of 'waiting' ports
52 struct MeshClient *next;
57 struct MeshClient *prev;
60 * Tunnels that belong to this client, indexed by local id
62 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
65 * Tunnels this client has accepted, indexed by incoming local id
67 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
70 * Channel ID for the next incoming channel.
72 MESH_ChannelNumber next_chid;
75 * Handle to communicate with the client
77 struct GNUNET_SERVER_Client *handle;
80 * Ports that this client has declared interest in.
81 * Indexed by port, contains *Client.
83 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
86 * Whether the client is active or shutting down (don't send confirmations
87 * to a client that is shutting down.
92 * ID of the client, mainly for debug messages
97 /******************************************************************************/
98 /******************************* GLOBALS ***********************************/
99 /******************************************************************************/
102 * Global handle to the statistics service.
104 extern struct GNUNET_STATISTICS_Handle *stats;
107 * Handle to server lib.
109 static struct GNUNET_SERVER_Handle *server_handle;
112 * DLL with all the clients, head.
114 static struct MeshClient *clients_head;
117 * DLL with all the clients, tail.
119 static struct MeshClient *clients_tail;
122 * Next ID to assign to a client.
124 unsigned int next_client_id;
127 * All ports clients of this peer have opened.
129 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
132 * Notification context, to send messages to local clients.
134 static struct GNUNET_SERVER_NotificationContext *nc;
137 /******************************************************************************/
138 /******************************** STATIC ***********************************/
139 /******************************************************************************/
142 * Remove client's ports from the global hashmap on disconnect.
144 * @param cls Closure (unused).
146 * @param value Client structure.
148 * @return GNUNET_OK, keep iterating.
151 client_release_ports (void *cls,
157 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
158 if (GNUNET_YES != res)
161 LOG (GNUNET_ERROR_TYPE_WARNING,
162 "Port %u by client %p was not registered.\n",
170 /******************************************************************************/
171 /******************************** HANDLES ***********************************/
172 /******************************************************************************/
176 * Handler for client connection.
178 * @param cls Closure (unused).
179 * @param client Client handler.
182 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
184 struct MeshClient *c;
186 LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
189 c = GNUNET_new (struct MeshClient);
191 c->id = next_client_id++; /* overflow not important: just for debug */
192 c->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
193 GNUNET_SERVER_client_keep (client);
194 GNUNET_SERVER_client_set_user_context (client, c);
195 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
200 * Iterator for deleting each channel whose client endpoint disconnected.
202 * @param cls Closure (client that has disconnected).
203 * @param key The local channel id (used to access the hashmap).
204 * @param value The value stored at the key (channel to destroy).
206 * @return GNUNET_OK, keep iterating.
209 channel_destroy_iterator (void *cls,
213 struct MeshChannel *ch = value;
214 struct MeshClient *c = cls;
216 LOG (GNUNET_ERROR_TYPE_DEBUG,
217 " Channel %s destroy, due to client %s shutdown.\n",
218 GMCH_2s (ch), GML_2s (c));
220 GMCH_handle_local_destroy (ch, c, key < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
225 * Handler for client disconnection
228 * @param client identification of the client; NULL
229 * for the last call when the server is destroyed
232 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
234 struct MeshClient *c;
236 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
239 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
243 c = GML_client_get (client);
246 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
248 GNUNET_SERVER_client_drop (c->handle);
249 c->shutting_down = GNUNET_YES;
250 if (NULL != c->own_channels)
252 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
253 &channel_destroy_iterator, c);
254 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
257 if (NULL != c->incoming_channels)
259 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
260 &channel_destroy_iterator, c);
261 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
264 if (NULL != c->ports)
266 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
267 &client_release_ports, c);
268 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
270 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
271 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
272 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
277 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
279 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
285 * Handler for new clients
288 * @param client identification of the client
289 * @param message the actual message, which includes messages the client wants
292 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
293 const struct GNUNET_MessageHeader *message)
295 struct GNUNET_MESH_ClientConnect *cc_msg;
296 struct MeshClient *c;
301 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nnew client connected %p\n", client);
303 /* Check data sanity */
304 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
305 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
306 if (0 != (size % sizeof (uint32_t)))
309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
312 size /= sizeof (uint32_t);
314 /* Initialize new client structure */
315 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
316 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
317 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
322 p = (uint32_t *) &cc_msg[1];
323 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
324 for (i = 0; i < size; i++)
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
329 /* store in client's hashmap */
330 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
331 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
332 /* store in global hashmap */
333 /* FIXME only allow one client to have the port open,
334 * have a backup hashmap with waiting clients */
335 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
340 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
341 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
342 GNUNET_SERVER_notification_context_add (nc, client);
343 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
345 GNUNET_SERVER_receive_done (client, GNUNET_OK);
346 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
351 * Handler for requests of new tunnels
353 * @param cls Closure.
354 * @param client Identification of the client.
355 * @param message The actual message.
358 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
359 const struct GNUNET_MessageHeader *message)
361 struct MeshClient *c;
363 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
364 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
366 /* Sanity check for client registration */
367 if (NULL == (c = GML_client_get (client)))
370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
373 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
375 /* Message size sanity check */
376 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
384 GMCH_handle_local_create (c,
385 (struct GNUNET_MESH_ChannelMessage *) message))
387 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
397 * Handler for requests of deleting tunnels
400 * @param client identification of the client
401 * @param message the actual message
404 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
405 const struct GNUNET_MessageHeader *message)
407 struct GNUNET_MESH_ChannelMessage *msg;
408 struct MeshClient *c;
409 struct MeshChannel *ch;
410 MESH_ChannelNumber chid;
412 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot a DESTROY CHANNEL from client!\n");
414 /* Sanity check for client registration */
415 if (NULL == (c = GML_client_get (client)))
418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
421 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
423 /* Message sanity check */
424 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
431 msg = (struct GNUNET_MESH_ChannelMessage *) message;
433 /* Retrieve tunnel */
434 chid = ntohl (msg->channel_id);
435 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
436 ch = GML_channel_get (c, chid);
439 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
440 GNUNET_STATISTICS_update (stats,
441 "# client destroy messages on unknown channel",
443 GNUNET_SERVER_receive_done (client, GNUNET_OK);
447 GMCH_handle_local_destroy (ch, c, chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
449 GNUNET_SERVER_receive_done (client, GNUNET_OK);
455 * Handler for client traffic
458 * @param client identification of the client
459 * @param message the actual message
462 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
463 const struct GNUNET_MessageHeader *message)
465 struct GNUNET_MESH_LocalData *msg;
466 struct MeshClient *c;
467 struct MeshChannel *ch;
468 MESH_ChannelNumber chid;
472 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot data from a client!\n");
474 /* Sanity check for client registration */
475 if (NULL == (c = GML_client_get (client)))
478 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
481 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
483 msg = (struct GNUNET_MESH_LocalData *) message;
485 /* Sanity check for message size */
486 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
487 if (size < sizeof (struct GNUNET_MessageHeader))
490 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
494 /* Channel exists? */
495 chid = ntohl (msg->id);
496 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
497 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
498 ch = GML_channel_get (c, chid);
501 GNUNET_STATISTICS_update (stats,
502 "# client data messages on unknown channel",
504 GNUNET_SERVER_receive_done (client, GNUNET_OK);
509 GMCH_handle_local_data (ch, c,
510 (struct GNUNET_MessageHeader *)&msg[1], fwd))
512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
516 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
517 GNUNET_SERVER_receive_done (client, GNUNET_OK);
524 * Handler for client's ACKs for payload traffic.
526 * @param cls Closure (unused).
527 * @param client Identification of the client.
528 * @param message The actual message.
531 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
532 const struct GNUNET_MessageHeader *message)
534 struct GNUNET_MESH_LocalAck *msg;
535 struct MeshChannel *ch;
536 struct MeshClient *c;
537 MESH_ChannelNumber chid;
540 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
541 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
543 /* Sanity check for client registration */
544 if (NULL == (c = GML_client_get (client)))
547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
550 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
552 msg = (struct GNUNET_MESH_LocalAck *) message;
554 /* Channel exists? */
555 chid = ntohl (msg->channel_id);
556 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
557 ch = GML_channel_get (c, chid);
558 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
561 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
562 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
563 GNUNET_STATISTICS_update (stats,
564 "# client ack messages on unknown channel",
566 GNUNET_SERVER_receive_done (client, GNUNET_OK);
570 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
571 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
572 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
574 GMCH_handle_local_ack (ch, fwd);
575 GNUNET_SERVER_receive_done (client, GNUNET_OK);
582 * Iterator over all tunnels to send a monitoring client info about each tunnel.
584 * @param cls Closure (client handle).
585 * @param peer Peer ID (tunnel remote peer).
586 * @param value Tunnel info.
588 * @return #GNUNET_YES, to keep iterating.
591 monitor_all_tunnels_iterator (void *cls,
592 const struct GNUNET_PeerIdentity * peer,
595 struct GNUNET_SERVER_Client *client = cls;
596 struct MeshChannel *ch = value;
597 struct GNUNET_MESH_LocalInfo *msg;
599 msg = GNUNET_new (struct GNUNET_MESH_LocalInfo);
600 msg->channel_id = htonl (GMCH_get_id (ch));
601 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalInfo));
602 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
604 LOG (GNUNET_ERROR_TYPE_INFO,
605 "* sending info about tunnel %s\n",
606 GNUNET_i2s (&msg->owner));
608 GNUNET_SERVER_notification_context_unicast (nc, client,
609 &msg->header, GNUNET_NO);
615 * Handler for client's INFO TUNNELS request.
617 * @param cls Closure (unused).
618 * @param client Identification of the client.
619 * @param message The actual message.
622 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
623 const struct GNUNET_MessageHeader *message)
625 struct MeshClient *c;
627 struct GNUNET_MessageHeader *reply;
629 /* Sanity check for client registration */
630 if (NULL == (c = GML_client_get (client)))
633 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
637 LOG (GNUNET_ERROR_TYPE_INFO, "Received get tunnels request from client %u\n",
640 size = GMT_count_all () + 1; /* Last one is all \0 to mark 'end' */
641 size *= sizeof (struct GNUNET_PeerIdentity);
642 size += sizeof (*reply);
643 reply = GNUNET_malloc (size);
644 GMT_iterate_all (reply, monitor_all_tunnels_iterator);
645 reply->size = htons (size);
646 reply->type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
647 GNUNET_SERVER_notification_context_unicast (nc, client, reply, GNUNET_NO);
649 LOG (GNUNET_ERROR_TYPE_INFO,
650 "Get tunnels request from client %u completed\n",
652 GNUNET_SERVER_receive_done (client, GNUNET_OK);
657 * Handler for client's MONITOR_TUNNEL request.
659 * @param cls Closure (unused).
660 * @param client Identification of the client.
661 * @param message The actual message.
664 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
665 const struct GNUNET_MessageHeader *message)
667 const struct GNUNET_MESH_LocalInfo *msg;
668 struct GNUNET_MESH_LocalInfo *resp;
669 struct MeshClient *c;
670 struct MeshChannel *ch;
672 /* Sanity check for client registration */
673 if (NULL == (c = GML_client_get (client)))
676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
680 msg = (struct GNUNET_MESH_LocalInfo *) message;
681 LOG (GNUNET_ERROR_TYPE_INFO,
682 "Received tunnel info request from client %u for tunnel %s[%X]\n",
685 ntohl (msg->channel_id));
686 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
690 /* We don't know the tunnel */
691 struct GNUNET_MESH_LocalInfo warn;
694 GNUNET_SERVER_notification_context_unicast (nc, client,
697 GNUNET_SERVER_receive_done (client, GNUNET_OK);
701 /* Initialize context */
702 resp = GNUNET_new (struct GNUNET_MESH_LocalInfo);
704 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalInfo));
705 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
706 &resp->header, GNUNET_NO);
709 LOG (GNUNET_ERROR_TYPE_INFO,
710 "Monitor tunnel request from client %u completed\n",
712 GNUNET_SERVER_receive_done (client, GNUNET_OK);
717 * Functions to handle messages from clients
719 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
720 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
721 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
722 sizeof (struct GNUNET_MESH_ChannelMessage)},
723 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
724 sizeof (struct GNUNET_MESH_ChannelMessage)},
725 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
726 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
727 sizeof (struct GNUNET_MESH_LocalAck)},
728 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
729 sizeof (struct GNUNET_MessageHeader)},
730 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
731 sizeof (struct GNUNET_MESH_LocalInfo)},
737 /******************************************************************************/
738 /******************************** API ***********************************/
739 /******************************************************************************/
742 * Initialize server subsystem.
744 * @param handle Server handle.
747 GML_init (struct GNUNET_SERVER_Handle *handle)
749 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
750 server_handle = handle;
751 GNUNET_SERVER_suspend (server_handle);
752 ports = GNUNET_CONTAINER_multihashmap32_create (32);
757 * Install server (service) handlers and start listening to clients.
762 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
763 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
764 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
766 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
771 GNUNET_SERVER_resume (server_handle);
783 GNUNET_SERVER_notification_context_destroy (nc);
790 * Get a channel from a client.
792 * @param c Client to check.
793 * @param chid Channel ID, must be local (> 0x800...).
795 * @return non-NULL if channel exists in the clients lists
798 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
800 struct GNUNET_CONTAINER_MultiHashMap32 *map;
802 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
805 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
809 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
810 map = c->incoming_channels;
811 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
812 map = c->own_channels;
821 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 "Client %s does no t have a valid map for CHID %X\n",
826 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
831 * Add a channel to a client
833 * @param client Client.
834 * @param chid Channel ID.
838 GML_channel_add (struct MeshClient *client,
840 struct MeshChannel *ch)
842 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
843 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
844 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
845 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
846 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
847 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
854 * Remove a channel from a client.
856 * @param client Client.
857 * @param chid Channel ID.
861 GML_channel_remove (struct MeshClient *client,
863 struct MeshChannel *ch)
865 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
866 GNUNET_break (GNUNET_YES ==
867 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
869 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= chid)
870 GNUNET_break (GNUNET_YES ==
871 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
879 * Get the tunnel's next free local channel ID.
883 * @return LID of a channel free to use.
886 GML_get_next_chid (struct MeshClient *c)
888 MESH_ChannelNumber chid;
890 while (NULL != GML_channel_get (c, c->next_chid))
892 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
893 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
896 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
903 * Check if client has registered with the service and has not disconnected
905 * @param client the client to check
907 * @return non-NULL if client exists in the global DLL
910 GML_client_get (struct GNUNET_SERVER_Client *client)
912 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
916 * Find a client that has opened a port
918 * @param port Port to check.
920 * @return non-NULL if a client has the port.
923 GML_client_get_by_port (uint32_t port)
925 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
930 * Deletes a channel from a client (either owner or destination).
932 * @param c Client whose tunnel to delete.
933 * @param ch Channel which should be deleted.
934 * @param id Channel ID.
937 GML_client_delete_channel (struct MeshClient *c,
938 struct MeshChannel *ch,
939 MESH_ChannelNumber id)
943 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= id)
945 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
947 if (GNUNET_YES != res)
948 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
950 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= id)
952 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
954 if (GNUNET_YES != res)
955 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
964 * Build a local ACK message and send it to a local client, if needed.
966 * If the client was already allowed to send data, do nothing.
968 * @param c Client to whom send the ACK.
969 * @param id Channel ID to use
972 GML_send_ack (struct MeshClient *c, MESH_ChannelNumber id)
974 struct GNUNET_MESH_LocalAck msg;
976 LOG (GNUNET_ERROR_TYPE_DEBUG,
977 "send local %s ack on %X towards %p\n",
978 id < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
980 msg.header.size = htons (sizeof (msg));
981 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
982 msg.channel_id = htonl (id);
983 GNUNET_SERVER_notification_context_unicast (nc,
992 * Build a local channel NACK message and send it to a local client.
994 * @param c Client to whom send the NACK.
995 * @param id Channel ID to use
998 GML_send_nack (struct MeshClient *c, MESH_ChannelNumber id)
1000 struct GNUNET_MESH_LocalAck msg;
1002 LOG (GNUNET_ERROR_TYPE_DEBUG,
1003 "send local nack on %X towards %p\n",
1006 msg.header.size = htons (sizeof (msg));
1007 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK);
1008 msg.channel_id = htonl (id);
1009 GNUNET_SERVER_notification_context_unicast (nc,
1018 * Notify the client that a new incoming channel was created.
1020 * @param c Client to notify.
1021 * @param id Channel ID.
1022 * @param port Channel's destination port.
1023 * @param opt Options (bit array).
1024 * @param peer Origin peer.
1027 GML_send_channel_create (struct MeshClient *c,
1028 uint32_t id, uint32_t port, uint32_t opt,
1029 const struct GNUNET_PeerIdentity *peer)
1031 struct GNUNET_MESH_ChannelMessage msg;
1033 msg.header.size = htons (sizeof (msg));
1034 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1035 msg.channel_id = htonl (id);
1036 msg.port = htonl (port);
1037 msg.opt = htonl (opt);
1039 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1040 &msg.header, GNUNET_NO);
1045 * Notify a client that a channel is no longer valid.
1048 * @param id ID of the channel that is destroyed.
1051 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
1053 struct GNUNET_MESH_ChannelMessage msg;
1060 if (GNUNET_YES == c->shutting_down)
1062 msg.header.size = htons (sizeof (msg));
1063 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1064 msg.channel_id = htonl (id);
1065 msg.port = htonl (0);
1066 memset (&msg.peer, 0, sizeof (msg.peer));
1067 msg.opt = htonl (0);
1068 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1069 &msg.header, GNUNET_NO);
1074 * Modify the mesh message ID from global to local and send to client.
1076 * @param c Client to send to.
1077 * @param msg Message to modify and send.
1078 * @param id Channel ID to use (c can be both owner and client).
1081 GML_send_data (struct MeshClient *c,
1082 const struct GNUNET_MESH_Data *msg,
1083 MESH_ChannelNumber id)
1085 struct GNUNET_MESH_LocalData *copy;
1086 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1087 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1089 if (size < sizeof (struct GNUNET_MessageHeader))
1091 GNUNET_break_op (0);
1099 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1100 memcpy (©[1], &msg[1], size);
1101 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1102 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1103 copy->id = htonl (id);
1104 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1105 ©->header, GNUNET_NO);
1110 * Get the static string to represent a client.
1114 * @return Static string for the client.
1117 GML_2s (const struct MeshClient *c)
1119 static char buf[32];
1121 sprintf (buf, "%u", c->id);