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_DEBUG, " 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);
229 GNUNET_SERVER_client_set_user_context (c->handle, NULL);
234 * Create a client record, register data and initialize memory.
236 * @param client Client's handle.
238 static struct CadetClient *
239 client_new (struct GNUNET_SERVER_Client *client)
241 struct CadetClient *c;
243 GNUNET_SERVER_client_keep (client);
244 GNUNET_SERVER_notification_context_add (nc, client);
246 c = GNUNET_new (struct CadetClient);
248 c->id = next_client_id++; /* overflow not important: just for debug */
249 c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
251 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
252 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
254 GNUNET_SERVER_client_set_user_context (client, c);
255 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
256 GNUNET_STATISTICS_update (stats, "# clients", +1, GNUNET_NO);
258 LOG (GNUNET_ERROR_TYPE_DEBUG, " client created: %p/%u\n", c, c->id);
264 /******************************************************************************/
265 /******************************** HANDLES ***********************************/
266 /******************************************************************************/
269 * Handler for client connection.
271 * @param cls Closure (unused).
272 * @param client Client handler.
275 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
277 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client connected: %p\n", client);
281 (void) client_new (client);
286 * Handler for client disconnection
289 * @param client identification of the client; NULL
290 * for the last call when the server is destroyed
293 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
295 struct CadetClient *c;
297 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected: %p\n", client);
299 c = GML_client_get (client);
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
308 LOG (GNUNET_ERROR_TYPE_DEBUG, " disconnecting client's context NULL\n");
315 * Handler for new clients
318 * @param client identification of the client
319 * @param message the actual message, which includes messages the client wants
322 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
323 const struct GNUNET_MessageHeader *message)
325 struct GNUNET_CADET_ClientConnect *cc_msg;
326 struct CadetClient *c;
331 LOG (GNUNET_ERROR_TYPE_DEBUG, "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_DEBUG, " 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_DEBUG, " 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 ch = GML_channel_get (c, chid);
475 LOG (GNUNET_ERROR_TYPE_INFO, "Client %u is destroying channel %X\n",
480 LOG (GNUNET_ERROR_TYPE_WARNING, " channel %X not found\n", chid);
481 GNUNET_STATISTICS_update (stats,
482 "# client destroy messages on unknown channel",
484 GNUNET_SERVER_receive_done (client, GNUNET_OK);
488 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
496 * Handler for client traffic
499 * @param client identification of the client
500 * @param message the actual message
503 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
504 const struct GNUNET_MessageHeader *message)
506 const struct GNUNET_MessageHeader *payload;
507 struct GNUNET_CADET_LocalData *msg;
508 struct CadetClient *c;
509 struct CadetChannel *ch;
510 CADET_ChannelNumber chid;
513 size_t payload_claimed_size;
516 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
517 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
518 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
520 /* Sanity check for client registration */
521 if (NULL == (c = GML_client_get (client)))
524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
528 /* Sanity check for message size */
529 message_size = ntohs (message->size);
530 if (sizeof (struct GNUNET_CADET_LocalData)
531 + sizeof (struct GNUNET_MessageHeader) > message_size
532 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
535 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
539 /* Sanity check for payload size */
540 payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
541 msg = (struct GNUNET_CADET_LocalData *) message;
542 payload = (struct GNUNET_MessageHeader *) &msg[1];
543 payload_claimed_size = ntohs (payload->size);
544 if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
545 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
546 || payload_claimed_size > payload_size)
548 LOG (GNUNET_ERROR_TYPE_WARNING,
549 "client claims to send %u bytes in %u payload\n",
550 payload_claimed_size, payload_size);
552 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
556 chid = ntohl (msg->id);
557 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes (%u payload) by client %u\n",
558 payload_size, payload_claimed_size, c->id);
560 /* Channel exists? */
561 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
562 ch = GML_channel_get (c, chid);
565 GNUNET_STATISTICS_update (stats,
566 "# client data messages on unknown channel",
568 GNUNET_SERVER_receive_done (client, GNUNET_OK);
572 if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
578 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
579 GNUNET_SERVER_receive_done (client, GNUNET_OK);
586 * Handler for client's ACKs for payload traffic.
588 * @param cls Closure (unused).
589 * @param client Identification of the client.
590 * @param message The actual message.
593 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
594 const struct GNUNET_MessageHeader *message)
596 struct GNUNET_CADET_LocalAck *msg;
597 struct CadetChannel *ch;
598 struct CadetClient *c;
599 CADET_ChannelNumber chid;
602 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
603 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
605 /* Sanity check for client registration */
606 if (NULL == (c = GML_client_get (client)))
609 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
612 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
614 msg = (struct GNUNET_CADET_LocalAck *) message;
616 /* Channel exists? */
617 chid = ntohl (msg->channel_id);
618 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
619 ch = GML_channel_get (c, chid);
620 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
623 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
624 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
625 GNUNET_STATISTICS_update (stats,
626 "# client ack messages on unknown channel",
628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
632 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
633 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
634 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
636 GCCH_handle_local_ack (ch, fwd);
637 GNUNET_SERVER_receive_done (client, GNUNET_OK);
644 * Iterator over all peers to send a monitoring client info about each peer.
646 * @param cls Closure ().
647 * @param peer Peer ID (tunnel remote peer).
648 * @param value Peer info.
650 * @return #GNUNET_YES, to keep iterating.
653 get_all_peers_iterator (void *cls,
654 const struct GNUNET_PeerIdentity * peer,
657 struct GNUNET_SERVER_Client *client = cls;
658 struct CadetPeer *p = value;
659 struct GNUNET_CADET_LocalInfoPeer msg;
661 msg.header.size = htons (sizeof (msg));
662 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
663 msg.destination = *peer;
664 msg.paths = htons (GCP_count_paths (p));
665 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
667 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
670 GNUNET_SERVER_notification_context_unicast (nc, client,
671 &msg.header, GNUNET_NO);
677 * Iterator over all peers to dump info for each peer.
679 * @param cls Closure (unused).
680 * @param peer Peer ID (tunnel remote peer).
681 * @param value Peer info.
683 * @return #GNUNET_YES, to keep iterating.
686 show_peer_iterator (void *cls,
687 const struct GNUNET_PeerIdentity * peer,
690 struct CadetPeer *p = value;
691 struct CadetTunnel *t;
693 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
695 t = GCP_get_tunnel (p);
697 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
699 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
706 * Iterator over all paths of a peer to build an InfoPeer message.
708 * Message contains blocks of peers, first not included.
710 * @param cls Closure (message to build).
711 * @param peer Peer this path is towards.
712 * @param path Path itself
713 * @return #GNUNET_YES if should keep iterating.
714 * #GNUNET_NO otherwise.
717 path_info_iterator (void *cls,
718 struct CadetPeer *peer,
719 struct CadetPeerPath *path)
721 struct GNUNET_CADET_LocalInfoPeer *resp = cls;
722 struct GNUNET_PeerIdentity *id;
727 msg_size = ntohs (resp->header.size);
728 path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
730 LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
731 if (msg_size + path_size > UINT16_MAX)
733 LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
737 i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
738 i = i / sizeof (struct GNUNET_PeerIdentity);
740 /* Set id to the address of the first free peer slot. */
741 id = (struct GNUNET_PeerIdentity *) &resp[1];
744 /* Don't copy first peers.
745 * First peer is always the local one.
746 * Last peer is always the destination (leave as 0, EOL).
748 for (i = 0; i < path->length - 1; i++)
750 GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
751 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
754 resp->header.size = htons (msg_size + path_size);
761 * Handler for client's INFO PEERS request.
763 * @param cls Closure (unused).
764 * @param client Identification of the client.
765 * @param message The actual message.
768 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
769 const struct GNUNET_MessageHeader *message)
771 struct CadetClient *c;
772 struct GNUNET_MessageHeader reply;
774 /* Sanity check for client registration */
775 if (NULL == (c = GML_client_get (client)))
778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
782 LOG (GNUNET_ERROR_TYPE_DEBUG,
783 "Received get peers request from client %u (%p)\n",
786 GCP_iterate_all (get_all_peers_iterator, client);
787 reply.size = htons (sizeof (reply));
788 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
789 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
791 LOG (GNUNET_ERROR_TYPE_DEBUG,
792 "Get peers request from client %u completed\n", c->id);
793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
798 * Handler for client's SHOW_PEER request.
800 * @param cls Closure (unused).
801 * @param client Identification of the client.
802 * @param message The actual message.
805 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
806 const struct GNUNET_MessageHeader *message)
808 const struct GNUNET_CADET_LocalInfo *msg;
809 struct GNUNET_CADET_LocalInfoPeer *resp;
811 struct CadetClient *c;
812 unsigned char cbuf[64 * 1024];
814 /* Sanity check for client registration */
815 if (NULL == (c = GML_client_get (client)))
818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
822 msg = (struct GNUNET_CADET_LocalInfo *) message;
823 resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
824 LOG (GNUNET_ERROR_TYPE_INFO,
825 "Received peer info request from client %u for peer %s\n",
826 c->id, GNUNET_i2s_full (&msg->peer));
828 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
829 resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
830 resp->destination = msg->peer;
831 p = GCP_get (&msg->peer, GNUNET_NO);
834 /* We don't know the peer */
836 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
837 GNUNET_i2s_full (&msg->peer));
838 resp->paths = htons (0);
839 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
841 GNUNET_SERVER_notification_context_unicast (nc, client,
844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
848 resp->paths = htons (GCP_count_paths (p));
849 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
850 GCP_iterate_paths (p, &path_info_iterator, resp);
852 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
853 &resp->header, GNUNET_NO);
855 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer from client %u completed.\n", c->id);
856 GNUNET_SERVER_receive_done (client, GNUNET_OK);
861 * Iterator over all tunnels to send a monitoring client info about each tunnel.
863 * @param cls Closure ().
864 * @param peer Peer ID (tunnel remote peer).
865 * @param value Tunnel info.
867 * @return #GNUNET_YES, to keep iterating.
870 get_all_tunnels_iterator (void *cls,
871 const struct GNUNET_PeerIdentity * peer,
874 struct GNUNET_SERVER_Client *client = cls;
875 struct CadetTunnel *t = value;
876 struct GNUNET_CADET_LocalInfoTunnel msg;
878 msg.header.size = htons (sizeof (msg));
879 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
880 msg.destination = *peer;
881 msg.channels = htonl (GCT_count_channels (t));
882 msg.connections = htonl (GCT_count_any_connections (t));
883 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
884 msg.estate = htons ((uint16_t) GCT_get_estate (t));
886 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
889 GNUNET_SERVER_notification_context_unicast (nc, client,
890 &msg.header, GNUNET_NO);
896 * Handler for client's INFO TUNNELS request.
898 * @param cls Closure (unused).
899 * @param client Identification of the client.
900 * @param message The actual message.
903 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
904 const struct GNUNET_MessageHeader *message)
906 struct CadetClient *c;
907 struct GNUNET_MessageHeader reply;
909 /* Sanity check for client registration */
910 if (NULL == (c = GML_client_get (client)))
913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
917 LOG (GNUNET_ERROR_TYPE_DEBUG,
918 "Received get tunnels request from client %u (%p)\n",
921 GCT_iterate_all (get_all_tunnels_iterator, client);
922 reply.size = htons (sizeof (reply));
923 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
924 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
926 LOG (GNUNET_ERROR_TYPE_DEBUG,
927 "Get tunnels request from client %u completed\n", c->id);
928 GNUNET_SERVER_receive_done (client, GNUNET_OK);
933 iter_connection (void *cls, struct CadetConnection *c)
935 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
936 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
938 h[msg->connections] = *(GCC_get_id (c));
943 iter_channel (void *cls, struct CadetChannel *ch)
945 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
946 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
947 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
949 chn[msg->channels] = htonl (GCCH_get_id (ch));
955 * Handler for client's SHOW_TUNNEL request.
957 * @param cls Closure (unused).
958 * @param client Identification of the client.
959 * @param message The actual message.
962 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
963 const struct GNUNET_MessageHeader *message)
965 const struct GNUNET_CADET_LocalInfo *msg;
966 struct GNUNET_CADET_LocalInfoTunnel *resp;
967 struct CadetClient *c;
968 struct CadetTunnel *t;
973 /* Sanity check for client registration */
974 if (NULL == (c = GML_client_get (client)))
977 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
981 msg = (struct GNUNET_CADET_LocalInfo *) message;
982 LOG (GNUNET_ERROR_TYPE_DEBUG,
983 "Received tunnel info request from client %u for tunnel %s\n",
984 c->id, GNUNET_i2s_full(&msg->peer));
986 t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
989 /* We don't know the tunnel */
990 struct GNUNET_CADET_LocalInfoTunnel warn;
992 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
993 GNUNET_i2s_full(&msg->peer), sizeof (warn));
994 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
995 warn.header.size = htons (sizeof (warn));
996 warn.destination = msg->peer;
997 warn.channels = htonl (0);
998 warn.connections = htonl (0);
999 warn.cstate = htons (0);
1000 warn.estate = htons (0);
1002 GNUNET_SERVER_notification_context_unicast (nc, client,
1005 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1009 /* Initialize context */
1010 ch_n = GCT_count_channels (t);
1011 c_n = GCT_count_any_connections (t);
1013 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1014 size += c_n * sizeof (struct GNUNET_CADET_Hash);
1015 size += ch_n * sizeof (CADET_ChannelNumber);
1017 resp = GNUNET_malloc (size);
1018 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1019 resp->header.size = htons (size);
1020 resp->destination = msg->peer;
1021 /* Do not interleave with iterators, iter_channel needs conn in HBO */
1022 GCT_iterate_connections (t, &iter_connection, resp);
1023 GCT_iterate_channels (t, &iter_channel, resp);
1024 resp->connections = htonl (resp->connections);
1025 resp->channels = htonl (resp->channels);
1026 /* Do not interleave end */
1027 resp->cstate = htons (GCT_get_cstate (t));
1028 resp->estate = htons (GCT_get_estate (t));
1029 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1030 &resp->header, GNUNET_NO);
1033 LOG (GNUNET_ERROR_TYPE_DEBUG,
1034 "Show tunnel request from client %u completed. %u conn, %u ch\n",
1036 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1041 * Handler for client's INFO_DUMP request.
1043 * @param cls Closure (unused).
1044 * @param client Identification of the client.
1045 * @param message The actual message.
1048 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
1049 const struct GNUNET_MessageHeader *message)
1051 struct CadetClient *c;
1053 /* Sanity check for client registration */
1054 if (NULL == (c = GML_client_get (client)))
1057 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1061 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
1063 LOG (GNUNET_ERROR_TYPE_ERROR,
1064 "*************************** DUMP START ***************************\n");
1066 for (c = clients_head; NULL != c; c = c->next)
1068 LOG (GNUNET_ERROR_TYPE_ERROR, "Client %u (%p), handle: %p\n",
1069 c->id, c, c->handle);
1070 if (NULL != c->ports)
1071 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u ports registered\n",
1072 GNUNET_CONTAINER_multihashmap32_size (c->ports));
1074 LOG (GNUNET_ERROR_TYPE_ERROR, "\t no ports registered\n");
1075 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u own channles\n",
1076 GNUNET_CONTAINER_multihashmap32_size (c->own_channels));
1077 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u incoming channles\n",
1078 GNUNET_CONTAINER_multihashmap32_size (c->incoming_channels));
1080 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1081 GCP_iterate_all (&show_peer_iterator, NULL);
1083 LOG (GNUNET_ERROR_TYPE_ERROR,
1084 "**************************** DUMP END ****************************\n");
1086 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1091 * Functions to handle messages from clients
1093 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1094 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1095 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1096 sizeof (struct GNUNET_CADET_ChannelMessage)},
1097 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1098 sizeof (struct GNUNET_CADET_ChannelMessage)},
1099 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1100 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1101 sizeof (struct GNUNET_CADET_LocalAck)},
1102 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1103 sizeof (struct GNUNET_MessageHeader)},
1104 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1105 sizeof (struct GNUNET_CADET_LocalInfo)},
1106 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1107 sizeof (struct GNUNET_MessageHeader)},
1108 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1109 sizeof (struct GNUNET_CADET_LocalInfo)},
1110 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1111 sizeof (struct GNUNET_MessageHeader)},
1117 /******************************************************************************/
1118 /******************************** API ***********************************/
1119 /******************************************************************************/
1122 * Initialize server subsystem.
1124 * @param handle Server handle.
1127 GML_init (struct GNUNET_SERVER_Handle *handle)
1129 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1130 server_handle = handle;
1131 GNUNET_SERVER_suspend (server_handle);
1132 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1137 * Install server (service) handlers and start listening to clients.
1142 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1143 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1144 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1146 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1148 clients_head = NULL;
1149 clients_tail = NULL;
1151 GNUNET_SERVER_resume (server_handle);
1161 struct CadetClient *c;
1163 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down local\n");
1165 for (c = clients_head; NULL != clients_head; c = clients_head)
1170 GNUNET_SERVER_notification_context_destroy (nc);
1178 * Get a channel from a client.
1180 * @param c Client to check.
1181 * @param chid Channel ID, must be local (> 0x800...).
1183 * @return non-NULL if channel exists in the clients lists
1185 struct CadetChannel *
1186 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1188 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1190 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1192 GNUNET_break_op (0);
1193 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1197 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1198 map = c->incoming_channels;
1199 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1200 map = c->own_channels;
1209 LOG (GNUNET_ERROR_TYPE_DEBUG,
1210 "Client %s does no t have a valid map for CHID %X\n",
1214 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1219 * Add a channel to a client
1221 * @param client Client.
1222 * @param chid Channel ID.
1223 * @param ch Channel.
1226 GML_channel_add (struct CadetClient *client,
1228 struct CadetChannel *ch)
1230 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1231 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1232 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1233 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1234 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1235 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1242 * Remove a channel from a client.
1244 * @param client Client.
1245 * @param chid Channel ID.
1246 * @param ch Channel.
1249 GML_channel_remove (struct CadetClient *client,
1251 struct CadetChannel *ch)
1253 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1254 GNUNET_break (GNUNET_YES ==
1255 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1257 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1258 GNUNET_break (GNUNET_YES ==
1259 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1267 * Get the tunnel's next free local channel ID.
1271 * @return LID of a channel free to use.
1274 GML_get_next_chid (struct CadetClient *c)
1276 CADET_ChannelNumber chid;
1278 while (NULL != GML_channel_get (c, c->next_chid))
1280 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1281 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1283 chid = c->next_chid;
1284 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1291 * Check if client has registered with the service and has not disconnected
1293 * @param client the client to check
1295 * @return non-NULL if client exists in the global DLL
1297 struct CadetClient *
1298 GML_client_get (struct GNUNET_SERVER_Client *client)
1302 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1306 * Find a client that has opened a port
1308 * @param port Port to check.
1310 * @return non-NULL if a client has the port.
1312 struct CadetClient *
1313 GML_client_get_by_port (uint32_t port)
1315 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1320 * Deletes a channel from a client (either owner or destination).
1322 * @param c Client whose tunnel to delete.
1323 * @param ch Channel which should be deleted.
1324 * @param id Channel ID.
1327 GML_client_delete_channel (struct CadetClient *c,
1328 struct CadetChannel *ch,
1329 CADET_ChannelNumber id)
1333 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1335 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1337 if (GNUNET_YES != res)
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1340 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1342 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1344 if (GNUNET_YES != res)
1345 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1354 * Build a local ACK message and send it to a local client, if needed.
1356 * If the client was already allowed to send data, do nothing.
1358 * @param c Client to whom send the ACK.
1359 * @param id Channel ID to use
1362 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1364 struct GNUNET_CADET_LocalAck msg;
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "send local %s ack on %X towards %p\n",
1368 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1370 msg.header.size = htons (sizeof (msg));
1371 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1372 msg.channel_id = htonl (id);
1373 GNUNET_SERVER_notification_context_unicast (nc,
1383 * Notify the client that a new incoming channel was created.
1385 * @param c Client to notify.
1386 * @param id Channel ID.
1387 * @param port Channel's destination port.
1388 * @param opt Options (bit array).
1389 * @param peer Origin peer.
1392 GML_send_channel_create (struct CadetClient *c,
1393 uint32_t id, uint32_t port, uint32_t opt,
1394 const struct GNUNET_PeerIdentity *peer)
1396 struct GNUNET_CADET_ChannelMessage msg;
1398 msg.header.size = htons (sizeof (msg));
1399 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1400 msg.channel_id = htonl (id);
1401 msg.port = htonl (port);
1402 msg.opt = htonl (opt);
1404 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1405 &msg.header, GNUNET_NO);
1410 * Build a local channel NACK message and send it to a local client.
1412 * @param c Client to whom send the NACK.
1413 * @param id Channel ID to use
1416 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1418 struct GNUNET_CADET_LocalAck msg;
1420 LOG (GNUNET_ERROR_TYPE_DEBUG,
1421 "send local nack on %X towards %p\n",
1424 msg.header.size = htons (sizeof (msg));
1425 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1426 msg.channel_id = htonl (id);
1427 GNUNET_SERVER_notification_context_unicast (nc,
1435 * Notify a client that a channel is no longer valid.
1438 * @param id ID of the channel that is destroyed.
1441 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1443 struct GNUNET_CADET_ChannelMessage msg;
1450 if (GNUNET_YES == c->shutting_down)
1452 msg.header.size = htons (sizeof (msg));
1453 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1454 msg.channel_id = htonl (id);
1455 msg.port = htonl (0);
1456 memset (&msg.peer, 0, sizeof (msg.peer));
1457 msg.opt = htonl (0);
1458 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1459 &msg.header, GNUNET_NO);
1464 * Modify the cadet message ID from global to local and send to client.
1466 * @param c Client to send to.
1467 * @param msg Message to modify and send.
1468 * @param id Channel ID to use (c can be both owner and client).
1471 GML_send_data (struct CadetClient *c,
1472 const struct GNUNET_CADET_Data *msg,
1473 CADET_ChannelNumber id)
1475 struct GNUNET_CADET_LocalData *copy;
1476 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1477 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1479 if (size < sizeof (struct GNUNET_MessageHeader))
1481 GNUNET_break_op (0);
1489 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1490 memcpy (©[1], &msg[1], size);
1491 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1492 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1493 copy->id = htonl (id);
1494 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1495 ©->header, GNUNET_NO);
1500 * Get the static string to represent a client.
1504 * @return Static string for the client.
1507 GML_2s (const struct CadetClient *c)
1509 static char buf[32];
1511 SPRINTF (buf, "%u", c->id);