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"
35 #include "gnunet-service-mesh_peer.h"
37 #define LOG(level, ...) GNUNET_log_from(level,"mesh-loc",__VA_ARGS__)
39 /******************************************************************************/
40 /******************************** STRUCTS **********************************/
41 /******************************************************************************/
44 * Struct containing information about a client of the service
46 * TODO: add a list of 'waiting' ports
53 struct MeshClient *next;
58 struct MeshClient *prev;
61 * Tunnels that belong to this client, indexed by local id
63 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
66 * Tunnels this client has accepted, indexed by incoming local id
68 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
71 * Channel ID for the next incoming channel.
73 MESH_ChannelNumber next_chid;
76 * Handle to communicate with the client
78 struct GNUNET_SERVER_Client *handle;
81 * Ports that this client has declared interest in.
82 * Indexed by port, contains *Client.
84 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
87 * Whether the client is active or shutting down (don't send confirmations
88 * to a client that is shutting down.
93 * ID of the client, mainly for debug messages
98 /******************************************************************************/
99 /******************************* GLOBALS ***********************************/
100 /******************************************************************************/
103 * Global handle to the statistics service.
105 extern struct GNUNET_STATISTICS_Handle *stats;
108 * Handle to server lib.
110 static struct GNUNET_SERVER_Handle *server_handle;
113 * DLL with all the clients, head.
115 static struct MeshClient *clients_head;
118 * DLL with all the clients, tail.
120 static struct MeshClient *clients_tail;
123 * Next ID to assign to a client.
125 unsigned int next_client_id;
128 * All ports clients of this peer have opened.
130 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
133 * Notification context, to send messages to local clients.
135 static struct GNUNET_SERVER_NotificationContext *nc;
138 /******************************************************************************/
139 /******************************** STATIC ***********************************/
140 /******************************************************************************/
143 * Remove client's ports from the global hashmap on disconnect.
145 * @param cls Closure (unused).
147 * @param value Client structure.
149 * @return GNUNET_OK, keep iterating.
152 client_release_ports (void *cls,
158 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
159 if (GNUNET_YES != res)
162 LOG (GNUNET_ERROR_TYPE_WARNING,
163 "Port %u by client %p was not registered.\n",
171 /******************************************************************************/
172 /******************************** HANDLES ***********************************/
173 /******************************************************************************/
177 * Handler for client connection.
179 * @param cls Closure (unused).
180 * @param client Client handler.
183 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
185 struct MeshClient *c;
187 LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
190 c = GNUNET_new (struct MeshClient);
192 c->id = next_client_id++; /* overflow not important: just for debug */
193 c->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
194 GNUNET_SERVER_client_keep (client);
195 GNUNET_SERVER_client_set_user_context (client, c);
196 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
201 * Iterator for deleting each channel whose client endpoint disconnected.
203 * @param cls Closure (client that has disconnected).
204 * @param key The local channel id (used to access the hashmap).
205 * @param value The value stored at the key (channel to destroy).
207 * @return GNUNET_OK, keep iterating.
210 channel_destroy_iterator (void *cls,
214 struct MeshChannel *ch = value;
215 struct MeshClient *c = cls;
217 LOG (GNUNET_ERROR_TYPE_DEBUG,
218 " Channel %s destroy, due to client %s shutdown.\n",
219 GMCH_2s (ch), GML_2s (c));
221 GMCH_handle_local_destroy (ch, c, key < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
226 * Handler for client disconnection
229 * @param client identification of the client; NULL
230 * for the last call when the server is destroyed
233 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
235 struct MeshClient *c;
237 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
240 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
244 c = GML_client_get (client);
247 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
249 GNUNET_SERVER_client_drop (c->handle);
250 c->shutting_down = GNUNET_YES;
251 if (NULL != c->own_channels)
253 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
254 &channel_destroy_iterator, c);
255 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
258 if (NULL != c->incoming_channels)
260 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
261 &channel_destroy_iterator, c);
262 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
265 if (NULL != c->ports)
267 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
268 &client_release_ports, c);
269 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
271 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
272 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
273 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
278 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
280 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
286 * Handler for new clients
289 * @param client identification of the client
290 * @param message the actual message, which includes messages the client wants
293 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
294 const struct GNUNET_MessageHeader *message)
296 struct GNUNET_MESH_ClientConnect *cc_msg;
297 struct MeshClient *c;
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
303 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
305 /* Check data sanity */
306 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
307 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
308 if (0 != (size % sizeof (uint32_t)))
311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
314 size /= sizeof (uint32_t);
316 /* Initialize new client structure */
317 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
318 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
319 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
324 p = (uint32_t *) &cc_msg[1];
325 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
326 for (i = 0; i < size; i++)
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
331 /* store in client's hashmap */
332 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
334 /* store in global hashmap */
335 /* FIXME only allow one client to have the port open,
336 * have a backup hashmap with waiting clients */
337 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
342 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
343 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
344 GNUNET_SERVER_notification_context_add (nc, client);
345 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
347 GNUNET_SERVER_receive_done (client, GNUNET_OK);
348 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
353 * Handler for requests of new tunnels
355 * @param cls Closure.
356 * @param client Identification of the client.
357 * @param message The actual message.
360 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
361 const struct GNUNET_MessageHeader *message)
363 struct MeshClient *c;
365 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
366 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
368 /* Sanity check for client registration */
369 if (NULL == (c = GML_client_get (client)))
372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
375 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
377 /* Message size sanity check */
378 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
386 GMCH_handle_local_create (c,
387 (struct GNUNET_MESH_ChannelMessage *) message))
389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
393 GNUNET_SERVER_receive_done (client, GNUNET_OK);
399 * Handler for requests of deleting tunnels
402 * @param client identification of the client
403 * @param message the actual message
406 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
407 const struct GNUNET_MessageHeader *message)
409 struct GNUNET_MESH_ChannelMessage *msg;
410 struct MeshClient *c;
411 struct MeshChannel *ch;
412 MESH_ChannelNumber chid;
414 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot a DESTROY CHANNEL from client!\n");
416 /* Sanity check for client registration */
417 if (NULL == (c = GML_client_get (client)))
420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
423 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
425 /* Message sanity check */
426 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
433 msg = (struct GNUNET_MESH_ChannelMessage *) message;
435 /* Retrieve tunnel */
436 chid = ntohl (msg->channel_id);
437 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
438 ch = GML_channel_get (c, chid);
441 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
442 GNUNET_STATISTICS_update (stats,
443 "# client destroy messages on unknown channel",
445 GNUNET_SERVER_receive_done (client, GNUNET_OK);
449 GMCH_handle_local_destroy (ch, c, chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV);
451 GNUNET_SERVER_receive_done (client, GNUNET_OK);
457 * Handler for client traffic
460 * @param client identification of the client
461 * @param message the actual message
464 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
465 const struct GNUNET_MessageHeader *message)
467 struct GNUNET_MESH_LocalData *msg;
468 struct MeshClient *c;
469 struct MeshChannel *ch;
470 MESH_ChannelNumber chid;
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\nGot data from a client!\n");
476 /* Sanity check for client registration */
477 if (NULL == (c = GML_client_get (client)))
480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
483 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
485 msg = (struct GNUNET_MESH_LocalData *) message;
487 /* Sanity check for message size */
488 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
489 if (size < sizeof (struct GNUNET_MessageHeader))
492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
496 /* Channel exists? */
497 chid = ntohl (msg->id);
498 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
499 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
500 ch = GML_channel_get (c, chid);
503 GNUNET_STATISTICS_update (stats,
504 "# client data messages on unknown channel",
506 GNUNET_SERVER_receive_done (client, GNUNET_OK);
511 GMCH_handle_local_data (ch, c,
512 (struct GNUNET_MessageHeader *)&msg[1], fwd))
514 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
518 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
519 GNUNET_SERVER_receive_done (client, GNUNET_OK);
526 * Handler for client's ACKs for payload traffic.
528 * @param cls Closure (unused).
529 * @param client Identification of the client.
530 * @param message The actual message.
533 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
534 const struct GNUNET_MessageHeader *message)
536 struct GNUNET_MESH_LocalAck *msg;
537 struct MeshChannel *ch;
538 struct MeshClient *c;
539 MESH_ChannelNumber chid;
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
543 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
545 /* Sanity check for client registration */
546 if (NULL == (c = GML_client_get (client)))
549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
552 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
554 msg = (struct GNUNET_MESH_LocalAck *) message;
556 /* Channel exists? */
557 chid = ntohl (msg->channel_id);
558 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
559 ch = GML_channel_get (c, chid);
560 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
563 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
564 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
565 GNUNET_STATISTICS_update (stats,
566 "# client ack messages on unknown channel",
568 GNUNET_SERVER_receive_done (client, GNUNET_OK);
572 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
573 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
574 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
576 GMCH_handle_local_ack (ch, fwd);
577 GNUNET_SERVER_receive_done (client, GNUNET_OK);
585 * Iterator over all peers to send a monitoring client info about each peer.
587 * @param cls Closure ().
588 * @param peer Peer ID (tunnel remote peer).
589 * @param value Peer info.
591 * @return #GNUNET_YES, to keep iterating.
594 get_all_peers_iterator (void *cls,
595 const struct GNUNET_PeerIdentity * peer,
598 struct GNUNET_SERVER_Client *client = cls;
599 struct MeshPeer *p = value;
600 struct GNUNET_MESH_LocalInfoPeer msg;
602 msg.header.size = htons (sizeof (msg));
603 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS);
604 msg.destination = *peer;
605 msg.paths = htons (GMP_count_paths (p));
606 msg.tunnel = htons (NULL != GMP_get_tunnel (p));
608 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
611 GNUNET_SERVER_notification_context_unicast (nc, client,
612 &msg.header, GNUNET_NO);
618 * Handler for client's INFO PEERS request.
620 * @param cls Closure (unused).
621 * @param client Identification of the client.
622 * @param message The actual message.
625 handle_get_peers (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 peers request from client %u (%p)\n",
643 GMP_iterate_all (get_all_peers_iterator, client);
644 reply.size = htons (sizeof (reply));
645 reply.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS);
646 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
648 LOG (GNUNET_ERROR_TYPE_DEBUG,
649 "Get peers request from client %u completed\n", c->id);
650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
655 * Iterator over all tunnels to send a monitoring client info about each tunnel.
657 * @param cls Closure ().
658 * @param peer Peer ID (tunnel remote peer).
659 * @param value Tunnel info.
661 * @return #GNUNET_YES, to keep iterating.
664 get_all_tunnels_iterator (void *cls,
665 const struct GNUNET_PeerIdentity * peer,
668 struct GNUNET_SERVER_Client *client = cls;
669 struct MeshTunnel3 *t = value;
670 struct GNUNET_MESH_LocalInfoTunnel msg;
672 msg.header.size = htons (sizeof (msg));
673 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
674 msg.destination = *peer;
675 msg.channels = htonl (GMT_count_channels (t));
676 msg.connections = htonl (GMT_count_connections (t));
677 msg.cstate = htons ((uint16_t) GMT_get_cstate (t));
678 msg.estate = htons ((uint16_t) GMT_get_estate (t));
680 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
683 GNUNET_SERVER_notification_context_unicast (nc, client,
684 &msg.header, GNUNET_NO);
690 * Handler for client's INFO TUNNELS request.
692 * @param cls Closure (unused).
693 * @param client Identification of the client.
694 * @param message The actual message.
697 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
698 const struct GNUNET_MessageHeader *message)
700 struct MeshClient *c;
701 struct GNUNET_MessageHeader reply;
703 /* Sanity check for client registration */
704 if (NULL == (c = GML_client_get (client)))
707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
711 LOG (GNUNET_ERROR_TYPE_DEBUG,
712 "Received get tunnels request from client %u (%p)\n",
715 GMT_iterate_all (get_all_tunnels_iterator, client);
716 reply.size = htons (sizeof (reply));
717 reply.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
718 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
720 LOG (GNUNET_ERROR_TYPE_DEBUG,
721 "Get tunnels request from client %u completed\n", c->id);
722 GNUNET_SERVER_receive_done (client, GNUNET_OK);
727 iter_connection (void *cls, struct MeshConnection *c)
729 struct GNUNET_MESH_LocalInfoTunnel *msg = cls;
730 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
732 h[msg->connections] = *(GMC_get_id (c));
737 iter_channel (void *cls, struct MeshChannel *ch)
739 struct GNUNET_MESH_LocalInfoTunnel *msg = cls;
740 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
741 MESH_ChannelNumber *chn = (MESH_ChannelNumber *) &h[msg->connections];
743 chn[msg->channels] = GMCH_get_id (ch);
749 * Handler for client's SHOW_TUNNEL request.
751 * @param cls Closure (unused).
752 * @param client Identification of the client.
753 * @param message The actual message.
756 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
757 const struct GNUNET_MessageHeader *message)
759 const struct GNUNET_MESH_LocalInfo *msg;
760 struct GNUNET_MESH_LocalInfoTunnel *resp;
761 struct MeshClient *c;
762 struct MeshTunnel3 *t;
767 /* Sanity check for client registration */
768 if (NULL == (c = GML_client_get (client)))
771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
775 msg = (struct GNUNET_MESH_LocalInfo *) message;
776 LOG (GNUNET_ERROR_TYPE_INFO,
777 "Received tunnel info request from client %u for tunnel %s\n",
778 c->id, GNUNET_i2s_full(&msg->peer));
780 t = GMP_get_tunnel (GMP_get (&msg->peer));
783 /* We don't know the tunnel */
784 struct GNUNET_MESH_LocalInfoTunnel warn;
786 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
787 GNUNET_i2s_full(&msg->peer), sizeof (warn));
788 warn.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
789 warn.header.size = htons (sizeof (warn));
790 warn.destination = msg->peer;
791 warn.channels = htonl (0);
792 warn.connections = htonl (0);
793 warn.cstate = htons (0);
794 warn.estate = htons (0);
796 GNUNET_SERVER_notification_context_unicast (nc, client,
799 GNUNET_SERVER_receive_done (client, GNUNET_OK);
803 /* Initialize context */
804 ch_n = GMT_count_channels (t);
805 c_n = GMT_count_connections (t);
807 size = sizeof (struct GNUNET_MESH_LocalInfoTunnel);
808 size += c_n * sizeof (struct GNUNET_HashCode);
809 size += ch_n * sizeof (MESH_ChannelNumber);
811 resp = GNUNET_malloc (size);
812 resp->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL);
813 resp->header.size = htons (size);
814 GMT_iterate_connections (t, &iter_connection, resp);
815 GMT_iterate_channels (t, &iter_channel, resp);
816 /* Do not interleave with iterators, iter_channel needs conn in HBO */
817 resp->destination = msg->peer;
818 resp->connections = htonl (resp->connections);
819 resp->channels = htonl (resp->channels);
820 resp->cstate = htons (GMT_get_cstate (t));
821 resp->estate = htons (GMT_get_estate (t));
822 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
823 &resp->header, GNUNET_NO);
826 LOG (GNUNET_ERROR_TYPE_INFO,
827 "Show tunnel request from client %u completed. %u conn, %u ch\n",
829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
834 * Functions to handle messages from clients
836 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
837 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
838 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
839 sizeof (struct GNUNET_MESH_ChannelMessage)},
840 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
841 sizeof (struct GNUNET_MESH_ChannelMessage)},
842 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
843 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
844 sizeof (struct GNUNET_MESH_LocalAck)},
845 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS,
846 sizeof (struct GNUNET_MessageHeader)},
847 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
848 sizeof (struct GNUNET_MessageHeader)},
849 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
850 sizeof (struct GNUNET_MESH_LocalInfo)},
856 /******************************************************************************/
857 /******************************** API ***********************************/
858 /******************************************************************************/
861 * Initialize server subsystem.
863 * @param handle Server handle.
866 GML_init (struct GNUNET_SERVER_Handle *handle)
868 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
869 server_handle = handle;
870 GNUNET_SERVER_suspend (server_handle);
871 ports = GNUNET_CONTAINER_multihashmap32_create (32);
876 * Install server (service) handlers and start listening to clients.
881 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
882 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
883 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
885 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
890 GNUNET_SERVER_resume (server_handle);
902 GNUNET_SERVER_notification_context_destroy (nc);
909 * Get a channel from a client.
911 * @param c Client to check.
912 * @param chid Channel ID, must be local (> 0x800...).
914 * @return non-NULL if channel exists in the clients lists
917 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
919 struct GNUNET_CONTAINER_MultiHashMap32 *map;
921 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
924 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
928 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
929 map = c->incoming_channels;
930 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
931 map = c->own_channels;
940 LOG (GNUNET_ERROR_TYPE_DEBUG,
941 "Client %s does no t have a valid map for CHID %X\n",
945 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
950 * Add a channel to a client
952 * @param client Client.
953 * @param chid Channel ID.
957 GML_channel_add (struct MeshClient *client,
959 struct MeshChannel *ch)
961 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
962 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
963 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
964 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
965 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
966 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
973 * Remove a channel from a client.
975 * @param client Client.
976 * @param chid Channel ID.
980 GML_channel_remove (struct MeshClient *client,
982 struct MeshChannel *ch)
984 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
985 GNUNET_break (GNUNET_YES ==
986 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
988 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= chid)
989 GNUNET_break (GNUNET_YES ==
990 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
998 * Get the tunnel's next free local channel ID.
1002 * @return LID of a channel free to use.
1005 GML_get_next_chid (struct MeshClient *c)
1007 MESH_ChannelNumber chid;
1009 while (NULL != GML_channel_get (c, c->next_chid))
1011 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1012 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
1014 chid = c->next_chid;
1015 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
1022 * Check if client has registered with the service and has not disconnected
1024 * @param client the client to check
1026 * @return non-NULL if client exists in the global DLL
1029 GML_client_get (struct GNUNET_SERVER_Client *client)
1031 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
1035 * Find a client that has opened a port
1037 * @param port Port to check.
1039 * @return non-NULL if a client has the port.
1042 GML_client_get_by_port (uint32_t port)
1044 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1049 * Deletes a channel from a client (either owner or destination).
1051 * @param c Client whose tunnel to delete.
1052 * @param ch Channel which should be deleted.
1053 * @param id Channel ID.
1056 GML_client_delete_channel (struct MeshClient *c,
1057 struct MeshChannel *ch,
1058 MESH_ChannelNumber id)
1062 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= id)
1064 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1066 if (GNUNET_YES != res)
1067 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1069 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= id)
1071 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1073 if (GNUNET_YES != res)
1074 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1083 * Build a local ACK message and send it to a local client, if needed.
1085 * If the client was already allowed to send data, do nothing.
1087 * @param c Client to whom send the ACK.
1088 * @param id Channel ID to use
1091 GML_send_ack (struct MeshClient *c, MESH_ChannelNumber id)
1093 struct GNUNET_MESH_LocalAck msg;
1095 LOG (GNUNET_ERROR_TYPE_DEBUG,
1096 "send local %s ack on %X towards %p\n",
1097 id < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1099 msg.header.size = htons (sizeof (msg));
1100 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1101 msg.channel_id = htonl (id);
1102 GNUNET_SERVER_notification_context_unicast (nc,
1112 * Notify the client that a new incoming channel was created.
1114 * @param c Client to notify.
1115 * @param id Channel ID.
1116 * @param port Channel's destination port.
1117 * @param opt Options (bit array).
1118 * @param peer Origin peer.
1121 GML_send_channel_create (struct MeshClient *c,
1122 uint32_t id, uint32_t port, uint32_t opt,
1123 const struct GNUNET_PeerIdentity *peer)
1125 struct GNUNET_MESH_ChannelMessage msg;
1127 msg.header.size = htons (sizeof (msg));
1128 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1129 msg.channel_id = htonl (id);
1130 msg.port = htonl (port);
1131 msg.opt = htonl (opt);
1133 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1134 &msg.header, GNUNET_NO);
1139 * Build a local channel NACK message and send it to a local client.
1141 * @param c Client to whom send the NACK.
1142 * @param id Channel ID to use
1145 GML_send_channel_nack (struct MeshClient *c, MESH_ChannelNumber id)
1147 struct GNUNET_MESH_LocalAck msg;
1149 LOG (GNUNET_ERROR_TYPE_DEBUG,
1150 "send local nack on %X towards %p\n",
1153 msg.header.size = htons (sizeof (msg));
1154 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK);
1155 msg.channel_id = htonl (id);
1156 GNUNET_SERVER_notification_context_unicast (nc,
1164 * Notify a client that a channel is no longer valid.
1167 * @param id ID of the channel that is destroyed.
1170 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
1172 struct GNUNET_MESH_ChannelMessage msg;
1179 if (GNUNET_YES == c->shutting_down)
1181 msg.header.size = htons (sizeof (msg));
1182 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1183 msg.channel_id = htonl (id);
1184 msg.port = htonl (0);
1185 memset (&msg.peer, 0, sizeof (msg.peer));
1186 msg.opt = htonl (0);
1187 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1188 &msg.header, GNUNET_NO);
1193 * Modify the mesh message ID from global to local and send to client.
1195 * @param c Client to send to.
1196 * @param msg Message to modify and send.
1197 * @param id Channel ID to use (c can be both owner and client).
1200 GML_send_data (struct MeshClient *c,
1201 const struct GNUNET_MESH_Data *msg,
1202 MESH_ChannelNumber id)
1204 struct GNUNET_MESH_LocalData *copy;
1205 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1206 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1208 if (size < sizeof (struct GNUNET_MessageHeader))
1210 GNUNET_break_op (0);
1218 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1219 memcpy (©[1], &msg[1], size);
1220 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1221 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1222 copy->id = htonl (id);
1223 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1224 ©->header, GNUNET_NO);
1229 * Get the static string to represent a client.
1233 * @return Static string for the client.
1236 GML_2s (const struct MeshClient *c)
1238 static char buf[32];
1240 sprintf (buf, "%u", c->id);