2 This file is part of GNUnet.
3 Copyright (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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
28 #include "cadet_protocol.h" /* GNUNET_CADET_Data is shared */
30 #include "gnunet-service-cadet_local.h"
31 #include "gnunet-service-cadet_channel.h"
34 #include "gnunet-service-cadet_tunnel.h"
35 #include "gnunet-service-cadet_peer.h"
37 #define LOG(level, ...) GNUNET_log_from(level,"cadet-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 CadetClient *next;
58 struct CadetClient *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 CADET_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 CadetClient *clients_head;
118 * DLL with all the clients, tail.
120 static struct CadetClient *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 * Iterator for deleting each channel whose client endpoint disconnected.
173 * @param cls Closure (client that has disconnected).
174 * @param key The local channel id (used to access the hashmap).
175 * @param value The value stored at the key (channel to destroy).
177 * @return GNUNET_OK, keep iterating.
180 channel_destroy_iterator (void *cls,
184 struct CadetChannel *ch = value;
185 struct CadetClient *c = cls;
187 LOG (GNUNET_ERROR_TYPE_DEBUG,
188 " Channel %s destroy, due to client %s shutdown.\n",
189 GCCH_2s (ch), GML_2s (c));
191 GCCH_handle_local_destroy (ch, c, key < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
197 * Unregister data and free memory for a client.
199 * @param c Client to destroy. No longer valid after call.
202 client_destroy (struct CadetClient *c)
204 LOG (GNUNET_ERROR_TYPE_INFO, " client destroy: %p/%u\n", c, c->id);
205 GNUNET_SERVER_client_drop (c->handle);
206 c->shutting_down = GNUNET_YES;
208 if (NULL != c->own_channels)
210 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
211 &channel_destroy_iterator, c);
212 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
214 if (NULL != c->incoming_channels)
216 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
217 &channel_destroy_iterator, c);
218 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
220 if (NULL != c->ports)
222 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
223 &client_release_ports, c);
224 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
227 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
228 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
233 * Create a client record, register data and initialize memory.
235 * @param client Client's handle.
237 static struct CadetClient *
238 client_new (struct GNUNET_SERVER_Client *client)
240 struct CadetClient *c;
242 GNUNET_SERVER_client_keep (client);
243 GNUNET_SERVER_notification_context_add (nc, client);
245 c = GNUNET_new (struct CadetClient);
247 c->id = next_client_id++; /* overflow not important: just for debug */
248 c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
250 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
251 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
253 GNUNET_SERVER_client_set_user_context (client, c);
254 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
255 GNUNET_STATISTICS_update (stats, "# clients", +1, GNUNET_NO);
257 LOG (GNUNET_ERROR_TYPE_INFO, " client created: %p/%u\n", c, c->id);
263 /******************************************************************************/
264 /******************************** HANDLES ***********************************/
265 /******************************************************************************/
268 * Handler for client connection.
270 * @param cls Closure (unused).
271 * @param client Client handler.
274 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
276 LOG (GNUNET_ERROR_TYPE_INFO, "Client connected: %p\n", client);
280 (void) client_new (client);
285 * Handler for client disconnection
288 * @param client identification of the client; NULL
289 * for the last call when the server is destroyed
292 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
294 struct CadetClient *c;
296 LOG (GNUNET_ERROR_TYPE_INFO, "Client disconnected: %p\n", client);
298 c = GML_client_get (client);
301 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
307 LOG (GNUNET_ERROR_TYPE_WARNING, " disconnecting client's context NULL\n");
314 * Handler for new clients
317 * @param client identification of the client
318 * @param message the actual message, which includes messages the client wants
321 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
322 const struct GNUNET_MessageHeader *message)
324 struct GNUNET_CADET_ClientConnect *cc_msg;
325 struct CadetClient *c;
330 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
331 LOG (GNUNET_ERROR_TYPE_INFO, "new client registering %p\n", client);
333 /* Check data sanity */
334 size = ntohs (message->size);
335 if (size < sizeof (struct GNUNET_CADET_ClientConnect))
338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
341 size -= sizeof (struct GNUNET_CADET_ClientConnect); /* Array size */
342 if (0 != (size % sizeof (uint32_t)))
345 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
348 size /= sizeof (uint32_t); /* Number of ports */
349 cc_msg = (struct GNUNET_CADET_ClientConnect *) message;
351 /* Retrieve client structure */
352 c = GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
360 LOG (GNUNET_ERROR_TYPE_INFO, " client %u has %u ports\n", c-> id, size);
365 p = (uint32_t *) &cc_msg[1];
366 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
367 for (i = 0; i < size; i++)
370 LOG (GNUNET_ERROR_TYPE_INFO, " port: %u\n", u32);
372 /* store in client's hashmap */
373 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
374 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
375 /* store in global hashmap */
376 /* FIXME only allow one client to have the port open,
377 * have a backup hashmap with waiting clients */
378 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
379 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
383 GNUNET_SERVER_receive_done (client, GNUNET_OK);
384 LOG (GNUNET_ERROR_TYPE_DEBUG, "new regitering processed\n");
389 * Handler for requests of new tunnels
391 * @param cls Closure.
392 * @param client Identification of the client.
393 * @param message The actual message.
396 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
397 const struct GNUNET_MessageHeader *message)
399 struct CadetClient *c;
401 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
402 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
404 /* Sanity check for client registration */
405 if (NULL == (c = GML_client_get (client)))
408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
411 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
413 /* Message size sanity check */
414 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
422 GCCH_handle_local_create (c,
423 (struct GNUNET_CADET_ChannelMessage *) message))
425 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
435 * Handler for requests of deleting tunnels
438 * @param client identification of the client
439 * @param message the actual message
442 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
443 const struct GNUNET_MessageHeader *message)
445 struct GNUNET_CADET_ChannelMessage *msg;
446 struct CadetClient *c;
447 struct CadetChannel *ch;
448 CADET_ChannelNumber chid;
450 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
452 /* Sanity check for client registration */
453 if (NULL == (c = GML_client_get (client)))
456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
459 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
461 /* Message sanity check */
462 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
469 msg = (struct GNUNET_CADET_ChannelMessage *) message;
471 /* Retrieve tunnel */
472 chid = ntohl (msg->channel_id);
473 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
474 ch = GML_channel_get (c, chid);
477 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
478 GNUNET_STATISTICS_update (stats,
479 "# client destroy messages on unknown channel",
481 GNUNET_SERVER_receive_done (client, GNUNET_OK);
485 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
487 GNUNET_SERVER_receive_done (client, GNUNET_OK);
493 * Handler for client traffic
496 * @param client identification of the client
497 * @param message the actual message
500 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
501 const struct GNUNET_MessageHeader *message)
503 const struct GNUNET_MessageHeader *payload;
504 struct GNUNET_CADET_LocalData *msg;
505 struct CadetClient *c;
506 struct CadetChannel *ch;
507 CADET_ChannelNumber chid;
510 size_t payload_claimed_size;
513 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
514 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
515 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
517 /* Sanity check for client registration */
518 if (NULL == (c = GML_client_get (client)))
521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
525 /* Sanity check for message size */
526 message_size = ntohs (message->size);
527 if (sizeof (struct GNUNET_CADET_LocalData)
528 + sizeof (struct GNUNET_MessageHeader) > message_size
529 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
532 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
536 /* Sanity check for payload size */
537 payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
538 msg = (struct GNUNET_CADET_LocalData *) message;
539 payload = (struct GNUNET_MessageHeader *) &msg[1];
540 payload_claimed_size = ntohs (payload->size);
541 if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
542 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
543 || payload_claimed_size > payload_size)
545 LOG (GNUNET_ERROR_TYPE_WARNING,
546 "client claims to send %u bytes in %u payload\n",
547 payload_claimed_size, payload_size);
549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
553 chid = ntohl (msg->id);
554 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes (%u payload) by client %u\n",
555 payload_size, payload_claimed_size, c->id);
557 /* Channel exists? */
558 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
559 ch = GML_channel_get (c, chid);
562 GNUNET_STATISTICS_update (stats,
563 "# client data messages on unknown channel",
565 GNUNET_SERVER_receive_done (client, GNUNET_OK);
569 if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
575 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
583 * Handler for client's ACKs for payload traffic.
585 * @param cls Closure (unused).
586 * @param client Identification of the client.
587 * @param message The actual message.
590 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
591 const struct GNUNET_MessageHeader *message)
593 struct GNUNET_CADET_LocalAck *msg;
594 struct CadetChannel *ch;
595 struct CadetClient *c;
596 CADET_ChannelNumber chid;
599 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
602 /* Sanity check for client registration */
603 if (NULL == (c = GML_client_get (client)))
606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
609 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
611 msg = (struct GNUNET_CADET_LocalAck *) message;
613 /* Channel exists? */
614 chid = ntohl (msg->channel_id);
615 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
616 ch = GML_channel_get (c, chid);
617 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
620 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
621 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
622 GNUNET_STATISTICS_update (stats,
623 "# client ack messages on unknown channel",
625 GNUNET_SERVER_receive_done (client, GNUNET_OK);
629 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
630 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
631 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
633 GCCH_handle_local_ack (ch, fwd);
634 GNUNET_SERVER_receive_done (client, GNUNET_OK);
641 * Iterator over all peers to send a monitoring client info about each peer.
643 * @param cls Closure ().
644 * @param peer Peer ID (tunnel remote peer).
645 * @param value Peer info.
647 * @return #GNUNET_YES, to keep iterating.
650 get_all_peers_iterator (void *cls,
651 const struct GNUNET_PeerIdentity * peer,
654 struct GNUNET_SERVER_Client *client = cls;
655 struct CadetPeer *p = value;
656 struct GNUNET_CADET_LocalInfoPeer msg;
658 msg.header.size = htons (sizeof (msg));
659 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
660 msg.destination = *peer;
661 msg.paths = htons (GCP_count_paths (p));
662 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
664 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
667 GNUNET_SERVER_notification_context_unicast (nc, client,
668 &msg.header, GNUNET_NO);
674 * Iterator over all peers to dump info for each peer.
676 * @param cls Closure (unused).
677 * @param peer Peer ID (tunnel remote peer).
678 * @param value Peer info.
680 * @return #GNUNET_YES, to keep iterating.
683 show_peer_iterator (void *cls,
684 const struct GNUNET_PeerIdentity * peer,
687 struct CadetPeer *p = value;
688 struct CadetTunnel *t;
690 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
692 t = GCP_get_tunnel (p);
694 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
696 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
703 * Iterator over all paths of a peer to build an InfoPeer message.
705 * Message contains blocks of peers, first not included.
707 * @param cls Closure (message to build).
708 * @param peer Peer this path is towards.
709 * @param path Path itself
710 * @return #GNUNET_YES if should keep iterating.
711 * #GNUNET_NO otherwise.
714 path_info_iterator (void *cls,
715 struct CadetPeer *peer,
716 struct CadetPeerPath *path)
718 struct GNUNET_CADET_LocalInfoPeer *resp = cls;
719 struct GNUNET_PeerIdentity *id;
724 msg_size = ntohs (resp->header.size);
725 path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
727 LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
728 if (msg_size + path_size > UINT16_MAX)
730 LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
734 i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
735 i = i / sizeof (struct GNUNET_PeerIdentity);
737 /* Set id to the address of the first free peer slot. */
738 id = (struct GNUNET_PeerIdentity *) &resp[1];
741 /* Don't copy first peers.
742 * First peer is always the local one.
743 * Last peer is always the destination (leave as 0, EOL).
745 for (i = 0; i < path->length - 1; i++)
747 GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
748 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
751 resp->header.size = htons (msg_size + path_size);
758 * Handler for client's INFO PEERS request.
760 * @param cls Closure (unused).
761 * @param client Identification of the client.
762 * @param message The actual message.
765 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
766 const struct GNUNET_MessageHeader *message)
768 struct CadetClient *c;
769 struct GNUNET_MessageHeader reply;
771 /* Sanity check for client registration */
772 if (NULL == (c = GML_client_get (client)))
775 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
779 LOG (GNUNET_ERROR_TYPE_DEBUG,
780 "Received get peers request from client %u (%p)\n",
783 GCP_iterate_all (get_all_peers_iterator, client);
784 reply.size = htons (sizeof (reply));
785 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
786 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "Get peers request from client %u completed\n", c->id);
790 GNUNET_SERVER_receive_done (client, GNUNET_OK);
795 * Handler for client's SHOW_PEER request.
797 * @param cls Closure (unused).
798 * @param client Identification of the client.
799 * @param message The actual message.
802 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
803 const struct GNUNET_MessageHeader *message)
805 const struct GNUNET_CADET_LocalInfo *msg;
806 struct GNUNET_CADET_LocalInfoPeer *resp;
808 struct CadetClient *c;
809 unsigned char cbuf[64 * 1024];
811 /* Sanity check for client registration */
812 if (NULL == (c = GML_client_get (client)))
815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
819 msg = (struct GNUNET_CADET_LocalInfo *) message;
820 resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
821 LOG (GNUNET_ERROR_TYPE_INFO,
822 "Received peer info request from client %u for peer %s\n",
823 c->id, GNUNET_i2s_full (&msg->peer));
825 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
826 resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
827 resp->destination = msg->peer;
828 p = GCP_get (&msg->peer, GNUNET_NO);
831 /* We don't know the peer */
833 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
834 GNUNET_i2s_full (&msg->peer));
835 resp->paths = htons (0);
836 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
838 GNUNET_SERVER_notification_context_unicast (nc, client,
841 GNUNET_SERVER_receive_done (client, GNUNET_OK);
845 resp->paths = htons (GCP_count_paths (p));
846 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
847 GCP_iterate_paths (p, &path_info_iterator, resp);
849 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
850 &resp->header, GNUNET_NO);
852 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
853 GNUNET_SERVER_receive_done (client, GNUNET_OK);
858 * Iterator over all tunnels to send a monitoring client info about each tunnel.
860 * @param cls Closure ().
861 * @param peer Peer ID (tunnel remote peer).
862 * @param value Tunnel info.
864 * @return #GNUNET_YES, to keep iterating.
867 get_all_tunnels_iterator (void *cls,
868 const struct GNUNET_PeerIdentity * peer,
871 struct GNUNET_SERVER_Client *client = cls;
872 struct CadetTunnel *t = value;
873 struct GNUNET_CADET_LocalInfoTunnel msg;
875 msg.header.size = htons (sizeof (msg));
876 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
877 msg.destination = *peer;
878 msg.channels = htonl (GCT_count_channels (t));
879 msg.connections = htonl (GCT_count_any_connections (t));
880 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
881 msg.estate = htons ((uint16_t) GCT_get_estate (t));
883 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
886 GNUNET_SERVER_notification_context_unicast (nc, client,
887 &msg.header, GNUNET_NO);
893 * Handler for client's INFO TUNNELS request.
895 * @param cls Closure (unused).
896 * @param client Identification of the client.
897 * @param message The actual message.
900 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
901 const struct GNUNET_MessageHeader *message)
903 struct CadetClient *c;
904 struct GNUNET_MessageHeader reply;
906 /* Sanity check for client registration */
907 if (NULL == (c = GML_client_get (client)))
910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
914 LOG (GNUNET_ERROR_TYPE_DEBUG,
915 "Received get tunnels request from client %u (%p)\n",
918 GCT_iterate_all (get_all_tunnels_iterator, client);
919 reply.size = htons (sizeof (reply));
920 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
921 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
923 LOG (GNUNET_ERROR_TYPE_DEBUG,
924 "Get tunnels request from client %u completed\n", c->id);
925 GNUNET_SERVER_receive_done (client, GNUNET_OK);
930 iter_connection (void *cls, struct CadetConnection *c)
932 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
933 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
935 h[msg->connections] = *(GCC_get_id (c));
940 iter_channel (void *cls, struct CadetChannel *ch)
942 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
943 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
944 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
946 chn[msg->channels] = htonl (GCCH_get_id (ch));
952 * Handler for client's SHOW_TUNNEL request.
954 * @param cls Closure (unused).
955 * @param client Identification of the client.
956 * @param message The actual message.
959 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
960 const struct GNUNET_MessageHeader *message)
962 const struct GNUNET_CADET_LocalInfo *msg;
963 struct GNUNET_CADET_LocalInfoTunnel *resp;
964 struct CadetClient *c;
965 struct CadetTunnel *t;
970 /* Sanity check for client registration */
971 if (NULL == (c = GML_client_get (client)))
974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
978 msg = (struct GNUNET_CADET_LocalInfo *) message;
979 LOG (GNUNET_ERROR_TYPE_DEBUG,
980 "Received tunnel info request from client %u for tunnel %s\n",
981 c->id, GNUNET_i2s_full(&msg->peer));
983 t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
986 /* We don't know the tunnel */
987 struct GNUNET_CADET_LocalInfoTunnel warn;
989 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
990 GNUNET_i2s_full(&msg->peer), sizeof (warn));
991 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
992 warn.header.size = htons (sizeof (warn));
993 warn.destination = msg->peer;
994 warn.channels = htonl (0);
995 warn.connections = htonl (0);
996 warn.cstate = htons (0);
997 warn.estate = htons (0);
999 GNUNET_SERVER_notification_context_unicast (nc, client,
1002 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1006 /* Initialize context */
1007 ch_n = GCT_count_channels (t);
1008 c_n = GCT_count_any_connections (t);
1010 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1011 size += c_n * sizeof (struct GNUNET_CADET_Hash);
1012 size += ch_n * sizeof (CADET_ChannelNumber);
1014 resp = GNUNET_malloc (size);
1015 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1016 resp->header.size = htons (size);
1017 GCT_iterate_connections (t, &iter_connection, resp);
1018 GCT_iterate_channels (t, &iter_channel, resp);
1019 /* Do not interleave with iterators, iter_channel needs conn in HBO */
1020 resp->destination = msg->peer;
1021 resp->connections = htonl (resp->connections);
1022 resp->channels = htonl (resp->channels);
1023 resp->cstate = htons (GCT_get_cstate (t));
1024 resp->estate = htons (GCT_get_estate (t));
1025 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1026 &resp->header, GNUNET_NO);
1029 LOG (GNUNET_ERROR_TYPE_DEBUG,
1030 "Show tunnel request from client %u completed. %u conn, %u ch\n",
1032 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1037 * Handler for client's INFO_DUMP request.
1039 * @param cls Closure (unused).
1040 * @param client Identification of the client.
1041 * @param message The actual message.
1044 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
1045 const struct GNUNET_MessageHeader *message)
1047 struct CadetClient *c;
1049 /* Sanity check for client registration */
1050 if (NULL == (c = GML_client_get (client)))
1053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1057 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
1060 LOG (GNUNET_ERROR_TYPE_ERROR,
1061 "*************************** DUMP START ***************************\n");
1063 GCP_iterate_all (&show_peer_iterator, NULL);
1065 LOG (GNUNET_ERROR_TYPE_ERROR,
1066 "**************************** DUMP END ****************************\n");
1068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1073 * Functions to handle messages from clients
1075 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1076 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1077 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1078 sizeof (struct GNUNET_CADET_ChannelMessage)},
1079 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1080 sizeof (struct GNUNET_CADET_ChannelMessage)},
1081 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1082 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1083 sizeof (struct GNUNET_CADET_LocalAck)},
1084 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1085 sizeof (struct GNUNET_MessageHeader)},
1086 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1087 sizeof (struct GNUNET_CADET_LocalInfo)},
1088 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1089 sizeof (struct GNUNET_MessageHeader)},
1090 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1091 sizeof (struct GNUNET_CADET_LocalInfo)},
1092 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1093 sizeof (struct GNUNET_MessageHeader)},
1099 /******************************************************************************/
1100 /******************************** API ***********************************/
1101 /******************************************************************************/
1104 * Initialize server subsystem.
1106 * @param handle Server handle.
1109 GML_init (struct GNUNET_SERVER_Handle *handle)
1111 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1112 server_handle = handle;
1113 GNUNET_SERVER_suspend (server_handle);
1114 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1119 * Install server (service) handlers and start listening to clients.
1124 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1125 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1126 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1128 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1130 clients_head = NULL;
1131 clients_tail = NULL;
1133 GNUNET_SERVER_resume (server_handle);
1143 struct CadetClient *c;
1145 for (c = clients_head; NULL != clients_head; c = clients_head)
1150 GNUNET_SERVER_notification_context_destroy (nc);
1158 * Get a channel from a client.
1160 * @param c Client to check.
1161 * @param chid Channel ID, must be local (> 0x800...).
1163 * @return non-NULL if channel exists in the clients lists
1165 struct CadetChannel *
1166 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1168 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1170 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1172 GNUNET_break_op (0);
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1177 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1178 map = c->incoming_channels;
1179 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1180 map = c->own_channels;
1189 LOG (GNUNET_ERROR_TYPE_DEBUG,
1190 "Client %s does no t have a valid map for CHID %X\n",
1194 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1199 * Add a channel to a client
1201 * @param client Client.
1202 * @param chid Channel ID.
1203 * @param ch Channel.
1206 GML_channel_add (struct CadetClient *client,
1208 struct CadetChannel *ch)
1210 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1211 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1213 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1214 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1215 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1222 * Remove a channel from a client.
1224 * @param client Client.
1225 * @param chid Channel ID.
1226 * @param ch Channel.
1229 GML_channel_remove (struct CadetClient *client,
1231 struct CadetChannel *ch)
1233 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1234 GNUNET_break (GNUNET_YES ==
1235 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1237 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1238 GNUNET_break (GNUNET_YES ==
1239 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1247 * Get the tunnel's next free local channel ID.
1251 * @return LID of a channel free to use.
1254 GML_get_next_chid (struct CadetClient *c)
1256 CADET_ChannelNumber chid;
1258 while (NULL != GML_channel_get (c, c->next_chid))
1260 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1261 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1263 chid = c->next_chid;
1264 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1271 * Check if client has registered with the service and has not disconnected
1273 * @param client the client to check
1275 * @return non-NULL if client exists in the global DLL
1277 struct CadetClient *
1278 GML_client_get (struct GNUNET_SERVER_Client *client)
1280 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1284 * Find a client that has opened a port
1286 * @param port Port to check.
1288 * @return non-NULL if a client has the port.
1290 struct CadetClient *
1291 GML_client_get_by_port (uint32_t port)
1293 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1298 * Deletes a channel from a client (either owner or destination).
1300 * @param c Client whose tunnel to delete.
1301 * @param ch Channel which should be deleted.
1302 * @param id Channel ID.
1305 GML_client_delete_channel (struct CadetClient *c,
1306 struct CadetChannel *ch,
1307 CADET_ChannelNumber id)
1311 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1313 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1315 if (GNUNET_YES != res)
1316 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1318 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1320 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1322 if (GNUNET_YES != res)
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1332 * Build a local ACK message and send it to a local client, if needed.
1334 * If the client was already allowed to send data, do nothing.
1336 * @param c Client to whom send the ACK.
1337 * @param id Channel ID to use
1340 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1342 struct GNUNET_CADET_LocalAck msg;
1344 LOG (GNUNET_ERROR_TYPE_DEBUG,
1345 "send local %s ack on %X towards %p\n",
1346 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1348 msg.header.size = htons (sizeof (msg));
1349 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1350 msg.channel_id = htonl (id);
1351 GNUNET_SERVER_notification_context_unicast (nc,
1361 * Notify the client that a new incoming channel was created.
1363 * @param c Client to notify.
1364 * @param id Channel ID.
1365 * @param port Channel's destination port.
1366 * @param opt Options (bit array).
1367 * @param peer Origin peer.
1370 GML_send_channel_create (struct CadetClient *c,
1371 uint32_t id, uint32_t port, uint32_t opt,
1372 const struct GNUNET_PeerIdentity *peer)
1374 struct GNUNET_CADET_ChannelMessage msg;
1376 msg.header.size = htons (sizeof (msg));
1377 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1378 msg.channel_id = htonl (id);
1379 msg.port = htonl (port);
1380 msg.opt = htonl (opt);
1382 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1383 &msg.header, GNUNET_NO);
1388 * Build a local channel NACK message and send it to a local client.
1390 * @param c Client to whom send the NACK.
1391 * @param id Channel ID to use
1394 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1396 struct GNUNET_CADET_LocalAck msg;
1398 LOG (GNUNET_ERROR_TYPE_DEBUG,
1399 "send local nack on %X towards %p\n",
1402 msg.header.size = htons (sizeof (msg));
1403 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1404 msg.channel_id = htonl (id);
1405 GNUNET_SERVER_notification_context_unicast (nc,
1413 * Notify a client that a channel is no longer valid.
1416 * @param id ID of the channel that is destroyed.
1419 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1421 struct GNUNET_CADET_ChannelMessage msg;
1428 if (GNUNET_YES == c->shutting_down)
1430 msg.header.size = htons (sizeof (msg));
1431 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1432 msg.channel_id = htonl (id);
1433 msg.port = htonl (0);
1434 memset (&msg.peer, 0, sizeof (msg.peer));
1435 msg.opt = htonl (0);
1436 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1437 &msg.header, GNUNET_NO);
1442 * Modify the cadet message ID from global to local and send to client.
1444 * @param c Client to send to.
1445 * @param msg Message to modify and send.
1446 * @param id Channel ID to use (c can be both owner and client).
1449 GML_send_data (struct CadetClient *c,
1450 const struct GNUNET_CADET_Data *msg,
1451 CADET_ChannelNumber id)
1453 struct GNUNET_CADET_LocalData *copy;
1454 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1455 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1457 if (size < sizeof (struct GNUNET_MessageHeader))
1459 GNUNET_break_op (0);
1467 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1468 memcpy (©[1], &msg[1], size);
1469 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1470 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1471 copy->id = htonl (id);
1472 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1473 ©->header, GNUNET_NO);
1478 * Get the static string to represent a client.
1482 * @return Static string for the client.
1485 GML_2s (const struct CadetClient *c)
1487 static char buf[32];
1489 SPRINTF (buf, "%u", c->id);