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");
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
304 /* Check data sanity */
305 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
306 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
307 if (0 != (size % sizeof (uint32_t)))
310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
313 size /= sizeof (uint32_t);
315 /* Initialize new client structure */
316 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
317 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
318 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
323 p = (uint32_t *) &cc_msg[1];
324 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
325 for (i = 0; i < size; i++)
328 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
330 /* store in client's hashmap */
331 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
332 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
333 /* store in global hashmap */
334 /* FIXME only allow one client to have the port open,
335 * have a backup hashmap with waiting clients */
336 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
337 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
341 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
342 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
343 GNUNET_SERVER_notification_context_add (nc, client);
344 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
346 GNUNET_SERVER_receive_done (client, GNUNET_OK);
347 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
352 * Handler for requests of new tunnels
354 * @param cls Closure.
355 * @param client Identification of the client.
356 * @param message The actual message.
359 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
360 const struct GNUNET_MessageHeader *message)
362 struct MeshClient *c;
364 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
365 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
367 /* Sanity check for client registration */
368 if (NULL == (c = GML_client_get (client)))
371 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
374 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
376 /* Message size sanity check */
377 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
385 GMCH_handle_local_create (c,
386 (struct GNUNET_MESH_ChannelMessage *) message))
388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
392 GNUNET_SERVER_receive_done (client, GNUNET_OK);
398 * Handler for requests of deleting tunnels
401 * @param client identification of the client
402 * @param message the actual message
405 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
406 const struct GNUNET_MessageHeader *message)
408 struct GNUNET_MESH_ChannelMessage *msg;
409 struct MeshClient *c;
410 struct MeshChannel *ch;
411 MESH_ChannelNumber chid;
413 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot a DESTROY CHANNEL from client!\n");
415 /* Sanity check for client registration */
416 if (NULL == (c = GML_client_get (client)))
419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
422 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
424 /* Message sanity check */
425 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
432 msg = (struct GNUNET_MESH_ChannelMessage *) message;
434 /* Retrieve tunnel */
435 chid = ntohl (msg->channel_id);
436 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
437 ch = GML_channel_get (c, chid);
440 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
441 GNUNET_STATISTICS_update (stats,
442 "# client destroy messages on unknown channel",
444 GNUNET_SERVER_receive_done (client, GNUNET_OK);
448 GMCH_handle_local_destroy (ch, c, chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
450 GNUNET_SERVER_receive_done (client, GNUNET_OK);
456 * Handler for client traffic
459 * @param client identification of the client
460 * @param message the actual message
463 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
464 const struct GNUNET_MessageHeader *message)
466 struct GNUNET_MESH_LocalData *msg;
467 struct MeshClient *c;
468 struct MeshChannel *ch;
469 MESH_ChannelNumber chid;
473 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot data from a client!\n");
475 /* Sanity check for client registration */
476 if (NULL == (c = GML_client_get (client)))
479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
482 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
484 msg = (struct GNUNET_MESH_LocalData *) message;
486 /* Sanity check for message size */
487 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
488 if (size < sizeof (struct GNUNET_MessageHeader))
491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
495 /* Channel exists? */
496 chid = ntohl (msg->id);
497 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
498 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
499 ch = GML_channel_get (c, chid);
502 GNUNET_STATISTICS_update (stats,
503 "# client data messages on unknown channel",
505 GNUNET_SERVER_receive_done (client, GNUNET_OK);
510 GMCH_handle_local_data (ch, c,
511 (struct GNUNET_MessageHeader *)&msg[1], fwd))
513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
517 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
518 GNUNET_SERVER_receive_done (client, GNUNET_OK);
525 * Handler for client's ACKs for payload traffic.
527 * @param cls Closure (unused).
528 * @param client Identification of the client.
529 * @param message The actual message.
532 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
533 const struct GNUNET_MessageHeader *message)
535 struct GNUNET_MESH_LocalAck *msg;
536 struct MeshChannel *ch;
537 struct MeshClient *c;
538 MESH_ChannelNumber chid;
541 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
544 /* Sanity check for client registration */
545 if (NULL == (c = GML_client_get (client)))
548 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
551 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
553 msg = (struct GNUNET_MESH_LocalAck *) message;
555 /* Channel exists? */
556 chid = ntohl (msg->channel_id);
557 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
558 ch = GML_channel_get (c, chid);
559 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
562 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
563 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
564 GNUNET_STATISTICS_update (stats,
565 "# client ack messages on unknown channel",
567 GNUNET_SERVER_receive_done (client, GNUNET_OK);
571 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
572 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
573 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
575 GMCH_handle_local_ack (ch, fwd);
576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
583 * Iterator over all tunnels to send a monitoring client info about each tunnel.
585 * @param cls Closure ().
586 * @param peer Peer ID (tunnel remote peer).
587 * @param value Tunnel info.
589 * @return #GNUNET_YES, to keep iterating.
592 get_all_tunnels_iterator (void *cls,
593 const struct GNUNET_PeerIdentity * peer,
596 struct GNUNET_SERVER_Client *client = cls;
597 struct MeshTunnel3 *t = value;
598 struct GNUNET_MESH_LocalInfoTunnel msg;
600 msg.header.size = htons (sizeof (msg));
601 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
602 msg.destination = *peer;
603 msg.channels = htonl (GMT_count_channels (t));
604 msg.connections = htonl (GMT_count_connections (t));
605 msg.cstate = htons ((uint16_t) GMT_get_cstate (t));
606 msg.estate = htons ((uint16_t) GMT_get_estate (t));
608 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
611 GNUNET_SERVER_notification_context_unicast (nc, client,
612 &msg.header, GNUNET_NO);
618 * Handler for client's INFO TUNNELS request.
620 * @param cls Closure (unused).
621 * @param client Identification of the client.
622 * @param message The actual message.
625 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
626 const struct GNUNET_MessageHeader *message)
628 struct MeshClient *c;
629 struct GNUNET_MessageHeader reply;
631 /* Sanity check for client registration */
632 if (NULL == (c = GML_client_get (client)))
635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "Received get tunnels request from client %u (%p)\n",
643 GMT_iterate_all (get_all_tunnels_iterator, client);
644 reply.size = htons (sizeof (reply));
645 reply.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
646 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
648 LOG (GNUNET_ERROR_TYPE_DEBUG,
649 "Get tunnels request from client %u completed\n", c->id);
650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
655 iter_connection (void *cls, struct MeshConnection *c)
657 struct GNUNET_MESH_LocalInfoTunnel *msg = cls;
658 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
660 h[msg->connections] = *(GMC_get_id (c));
665 iter_channel (void *cls, struct MeshChannel *ch)
667 struct GNUNET_MESH_LocalInfoTunnel *msg = cls;
668 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
669 MESH_ChannelNumber *chn = (MESH_ChannelNumber *) &h[msg->connections];
671 chn[msg->channels] = GMCH_get_id (ch);
677 * Handler for client's SHOW_TUNNEL request.
679 * @param cls Closure (unused).
680 * @param client Identification of the client.
681 * @param message The actual message.
684 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
685 const struct GNUNET_MessageHeader *message)
687 const struct GNUNET_MESH_LocalInfo *msg;
688 struct GNUNET_MESH_LocalInfoTunnel *resp;
689 struct MeshClient *c;
690 struct MeshTunnel3 *t;
695 /* Sanity check for client registration */
696 if (NULL == (c = GML_client_get (client)))
699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
703 msg = (struct GNUNET_MESH_LocalInfo *) message;
704 LOG (GNUNET_ERROR_TYPE_INFO,
705 "Received tunnel info request from client %u for tunnel %s\n",
706 c->id, GNUNET_i2s_full(&msg->peer));
708 t = GMP_get_tunnel (GMP_get (&msg->peer));
711 /* We don't know the tunnel */
712 struct GNUNET_MESH_LocalInfoTunnel warn;
714 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
715 GNUNET_i2s_full(&msg->peer), sizeof (warn));
716 warn.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
717 warn.header.size = htons (sizeof (warn));
718 warn.destination = msg->peer;
719 warn.channels = htonl (0);
720 warn.connections = htonl (0);
721 warn.cstate = htons (0);
722 warn.estate = htons (0);
724 GNUNET_SERVER_notification_context_unicast (nc, client,
727 GNUNET_SERVER_receive_done (client, GNUNET_OK);
731 /* Initialize context */
732 ch_n = GMT_count_channels (t);
733 c_n = GMT_count_connections (t);
735 size = sizeof (struct GNUNET_MESH_LocalInfoTunnel);
736 size += c_n * sizeof (struct GNUNET_HashCode);
737 size += ch_n * sizeof (MESH_ChannelNumber);
739 resp = GNUNET_malloc (size);
740 resp->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
741 resp->header.size = htons (size);
742 GMT_iterate_connections (t, &iter_connection, resp);
743 GMT_iterate_channels (t, &iter_channel, resp);
744 /* Do not interleave with iterators, iter_channel needs conn in HBO */
745 resp->destination = msg->peer;
746 resp->connections = htonl (resp->connections);
747 resp->channels = htonl (resp->channels);
748 resp->cstate = htons (GMT_get_cstate (t));
749 resp->estate = htons (GMT_get_estate (t));
750 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
751 &resp->header, GNUNET_NO);
754 LOG (GNUNET_ERROR_TYPE_INFO,
755 "Show tunnel request from client %u completed. %u conn, %u ch\n",
757 GNUNET_SERVER_receive_done (client, GNUNET_OK);
762 * Functions to handle messages from clients
764 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
765 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
766 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
767 sizeof (struct GNUNET_MESH_ChannelMessage)},
768 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
769 sizeof (struct GNUNET_MESH_ChannelMessage)},
770 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
771 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
772 sizeof (struct GNUNET_MESH_LocalAck)},
773 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
774 sizeof (struct GNUNET_MessageHeader)},
775 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
776 sizeof (struct GNUNET_MESH_LocalInfo)},
782 /******************************************************************************/
783 /******************************** API ***********************************/
784 /******************************************************************************/
787 * Initialize server subsystem.
789 * @param handle Server handle.
792 GML_init (struct GNUNET_SERVER_Handle *handle)
794 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
795 server_handle = handle;
796 GNUNET_SERVER_suspend (server_handle);
797 ports = GNUNET_CONTAINER_multihashmap32_create (32);
802 * Install server (service) handlers and start listening to clients.
807 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
808 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
809 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
811 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
816 GNUNET_SERVER_resume (server_handle);
828 GNUNET_SERVER_notification_context_destroy (nc);
835 * Get a channel from a client.
837 * @param c Client to check.
838 * @param chid Channel ID, must be local (> 0x800...).
840 * @return non-NULL if channel exists in the clients lists
843 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
845 struct GNUNET_CONTAINER_MultiHashMap32 *map;
847 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
850 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
854 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
855 map = c->incoming_channels;
856 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
857 map = c->own_channels;
866 LOG (GNUNET_ERROR_TYPE_DEBUG,
867 "Client %s does no t have a valid map for CHID %X\n",
871 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
876 * Add a channel to a client
878 * @param client Client.
879 * @param chid Channel ID.
883 GML_channel_add (struct MeshClient *client,
885 struct MeshChannel *ch)
887 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
888 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
889 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
890 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
891 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
892 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
899 * Remove a channel from a client.
901 * @param client Client.
902 * @param chid Channel ID.
906 GML_channel_remove (struct MeshClient *client,
908 struct MeshChannel *ch)
910 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
911 GNUNET_break (GNUNET_YES ==
912 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
914 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= chid)
915 GNUNET_break (GNUNET_YES ==
916 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
924 * Get the tunnel's next free local channel ID.
928 * @return LID of a channel free to use.
931 GML_get_next_chid (struct MeshClient *c)
933 MESH_ChannelNumber chid;
935 while (NULL != GML_channel_get (c, c->next_chid))
937 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
938 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
941 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
948 * Check if client has registered with the service and has not disconnected
950 * @param client the client to check
952 * @return non-NULL if client exists in the global DLL
955 GML_client_get (struct GNUNET_SERVER_Client *client)
957 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
961 * Find a client that has opened a port
963 * @param port Port to check.
965 * @return non-NULL if a client has the port.
968 GML_client_get_by_port (uint32_t port)
970 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
975 * Deletes a channel from a client (either owner or destination).
977 * @param c Client whose tunnel to delete.
978 * @param ch Channel which should be deleted.
979 * @param id Channel ID.
982 GML_client_delete_channel (struct MeshClient *c,
983 struct MeshChannel *ch,
984 MESH_ChannelNumber id)
988 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= id)
990 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
992 if (GNUNET_YES != res)
993 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
995 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= id)
997 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
999 if (GNUNET_YES != res)
1000 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1009 * Build a local ACK message and send it to a local client, if needed.
1011 * If the client was already allowed to send data, do nothing.
1013 * @param c Client to whom send the ACK.
1014 * @param id Channel ID to use
1017 GML_send_ack (struct MeshClient *c, MESH_ChannelNumber id)
1019 struct GNUNET_MESH_LocalAck msg;
1021 LOG (GNUNET_ERROR_TYPE_DEBUG,
1022 "send local %s ack on %X towards %p\n",
1023 id < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1025 msg.header.size = htons (sizeof (msg));
1026 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1027 msg.channel_id = htonl (id);
1028 GNUNET_SERVER_notification_context_unicast (nc,
1038 * Notify the client that a new incoming channel was created.
1040 * @param c Client to notify.
1041 * @param id Channel ID.
1042 * @param port Channel's destination port.
1043 * @param opt Options (bit array).
1044 * @param peer Origin peer.
1047 GML_send_channel_create (struct MeshClient *c,
1048 uint32_t id, uint32_t port, uint32_t opt,
1049 const struct GNUNET_PeerIdentity *peer)
1051 struct GNUNET_MESH_ChannelMessage msg;
1053 msg.header.size = htons (sizeof (msg));
1054 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1055 msg.channel_id = htonl (id);
1056 msg.port = htonl (port);
1057 msg.opt = htonl (opt);
1059 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1060 &msg.header, GNUNET_NO);
1065 * Build a local channel NACK message and send it to a local client.
1067 * @param c Client to whom send the NACK.
1068 * @param id Channel ID to use
1071 GML_send_channel_nack (struct MeshClient *c, MESH_ChannelNumber id)
1073 struct GNUNET_MESH_LocalAck msg;
1075 LOG (GNUNET_ERROR_TYPE_DEBUG,
1076 "send local nack on %X towards %p\n",
1079 msg.header.size = htons (sizeof (msg));
1080 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
1081 msg.channel_id = htonl (id);
1082 GNUNET_SERVER_notification_context_unicast (nc,
1090 * Notify a client that a channel is no longer valid.
1093 * @param id ID of the channel that is destroyed.
1096 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
1098 struct GNUNET_MESH_ChannelMessage msg;
1105 if (GNUNET_YES == c->shutting_down)
1107 msg.header.size = htons (sizeof (msg));
1108 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1109 msg.channel_id = htonl (id);
1110 msg.port = htonl (0);
1111 memset (&msg.peer, 0, sizeof (msg.peer));
1112 msg.opt = htonl (0);
1113 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1114 &msg.header, GNUNET_NO);
1119 * Modify the mesh message ID from global to local and send to client.
1121 * @param c Client to send to.
1122 * @param msg Message to modify and send.
1123 * @param id Channel ID to use (c can be both owner and client).
1126 GML_send_data (struct MeshClient *c,
1127 const struct GNUNET_MESH_Data *msg,
1128 MESH_ChannelNumber id)
1130 struct GNUNET_MESH_LocalData *copy;
1131 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1132 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1134 if (size < sizeof (struct GNUNET_MessageHeader))
1136 GNUNET_break_op (0);
1144 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1145 memcpy (©[1], &msg[1], size);
1146 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1147 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1148 copy->id = htonl (id);
1149 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1150 ©->header, GNUNET_NO);
1155 * Get the static string to represent a client.
1159 * @return Static string for the client.
1162 GML_2s (const struct MeshClient *c)
1164 static char buf[32];
1166 sprintf (buf, "%u", c->id);