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);
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_INFO, " 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_INFO, "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_INFO, "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, "\n");
332 LOG (GNUNET_ERROR_TYPE_INFO, "new client registering %p\n", client);
334 /* Check data sanity */
335 size = ntohs (message->size);
336 if (size < sizeof (struct GNUNET_CADET_ClientConnect))
339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
342 size -= sizeof (struct GNUNET_CADET_ClientConnect); /* Array size */
343 if (0 != (size % sizeof (uint32_t)))
346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
349 size /= sizeof (uint32_t); /* Number of ports */
350 cc_msg = (struct GNUNET_CADET_ClientConnect *) message;
352 /* Retrieve client structure */
353 c = GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
361 LOG (GNUNET_ERROR_TYPE_INFO, " client %u has %u ports\n", c-> id, size);
366 p = (uint32_t *) &cc_msg[1];
367 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
368 for (i = 0; i < size; i++)
371 LOG (GNUNET_ERROR_TYPE_INFO, " port: %u\n", u32);
373 /* store in client's hashmap */
374 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
376 /* store in global hashmap */
377 /* FIXME only allow one client to have the port open,
378 * have a backup hashmap with waiting clients */
379 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
380 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
384 GNUNET_SERVER_receive_done (client, GNUNET_OK);
385 LOG (GNUNET_ERROR_TYPE_DEBUG, "new regitering processed\n");
390 * Handler for requests of new tunnels
392 * @param cls Closure.
393 * @param client Identification of the client.
394 * @param message The actual message.
397 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
398 const struct GNUNET_MessageHeader *message)
400 struct CadetClient *c;
402 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
403 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
405 /* Sanity check for client registration */
406 if (NULL == (c = GML_client_get (client)))
409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
412 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
414 /* Message size sanity check */
415 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
423 GCCH_handle_local_create (c,
424 (struct GNUNET_CADET_ChannelMessage *) message))
426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
436 * Handler for requests of deleting tunnels
439 * @param client identification of the client
440 * @param message the actual message
443 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
444 const struct GNUNET_MessageHeader *message)
446 struct GNUNET_CADET_ChannelMessage *msg;
447 struct CadetClient *c;
448 struct CadetChannel *ch;
449 CADET_ChannelNumber chid;
451 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
453 /* Sanity check for client registration */
454 if (NULL == (c = GML_client_get (client)))
457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
460 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
462 /* Message sanity check */
463 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
470 msg = (struct GNUNET_CADET_ChannelMessage *) message;
472 /* Retrieve tunnel */
473 chid = ntohl (msg->channel_id);
474 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
475 ch = GML_channel_get (c, chid);
478 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
479 GNUNET_STATISTICS_update (stats,
480 "# client destroy messages on unknown channel",
482 GNUNET_SERVER_receive_done (client, GNUNET_OK);
486 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
488 GNUNET_SERVER_receive_done (client, GNUNET_OK);
494 * Handler for client traffic
497 * @param client identification of the client
498 * @param message the actual message
501 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
502 const struct GNUNET_MessageHeader *message)
504 const struct GNUNET_MessageHeader *payload;
505 struct GNUNET_CADET_LocalData *msg;
506 struct CadetClient *c;
507 struct CadetChannel *ch;
508 CADET_ChannelNumber chid;
511 size_t payload_claimed_size;
514 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
515 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
516 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
518 /* Sanity check for client registration */
519 if (NULL == (c = GML_client_get (client)))
522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
526 /* Sanity check for message size */
527 message_size = ntohs (message->size);
528 if (sizeof (struct GNUNET_CADET_LocalData)
529 + sizeof (struct GNUNET_MessageHeader) > message_size
530 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
537 /* Sanity check for payload size */
538 payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
539 msg = (struct GNUNET_CADET_LocalData *) message;
540 payload = (struct GNUNET_MessageHeader *) &msg[1];
541 payload_claimed_size = ntohs (payload->size);
542 if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
543 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
544 || payload_claimed_size > payload_size)
546 LOG (GNUNET_ERROR_TYPE_WARNING,
547 "client claims to send %u bytes in %u payload\n",
548 payload_claimed_size, payload_size);
550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
554 chid = ntohl (msg->id);
555 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes (%u payload) by client %u\n",
556 payload_size, payload_claimed_size, c->id);
558 /* Channel exists? */
559 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
560 ch = GML_channel_get (c, chid);
563 GNUNET_STATISTICS_update (stats,
564 "# client data messages on unknown channel",
566 GNUNET_SERVER_receive_done (client, GNUNET_OK);
570 if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
572 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
576 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
577 GNUNET_SERVER_receive_done (client, GNUNET_OK);
584 * Handler for client's ACKs for payload traffic.
586 * @param cls Closure (unused).
587 * @param client Identification of the client.
588 * @param message The actual message.
591 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
592 const struct GNUNET_MessageHeader *message)
594 struct GNUNET_CADET_LocalAck *msg;
595 struct CadetChannel *ch;
596 struct CadetClient *c;
597 CADET_ChannelNumber chid;
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
601 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
603 /* Sanity check for client registration */
604 if (NULL == (c = GML_client_get (client)))
607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
610 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
612 msg = (struct GNUNET_CADET_LocalAck *) message;
614 /* Channel exists? */
615 chid = ntohl (msg->channel_id);
616 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
617 ch = GML_channel_get (c, chid);
618 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
621 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
622 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
623 GNUNET_STATISTICS_update (stats,
624 "# client ack messages on unknown channel",
626 GNUNET_SERVER_receive_done (client, GNUNET_OK);
630 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
631 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
632 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
634 GCCH_handle_local_ack (ch, fwd);
635 GNUNET_SERVER_receive_done (client, GNUNET_OK);
642 * Iterator over all peers to send a monitoring client info about each peer.
644 * @param cls Closure ().
645 * @param peer Peer ID (tunnel remote peer).
646 * @param value Peer info.
648 * @return #GNUNET_YES, to keep iterating.
651 get_all_peers_iterator (void *cls,
652 const struct GNUNET_PeerIdentity * peer,
655 struct GNUNET_SERVER_Client *client = cls;
656 struct CadetPeer *p = value;
657 struct GNUNET_CADET_LocalInfoPeer msg;
659 msg.header.size = htons (sizeof (msg));
660 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
661 msg.destination = *peer;
662 msg.paths = htons (GCP_count_paths (p));
663 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
665 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
668 GNUNET_SERVER_notification_context_unicast (nc, client,
669 &msg.header, GNUNET_NO);
675 * Iterator over all peers to dump info for each peer.
677 * @param cls Closure (unused).
678 * @param peer Peer ID (tunnel remote peer).
679 * @param value Peer info.
681 * @return #GNUNET_YES, to keep iterating.
684 show_peer_iterator (void *cls,
685 const struct GNUNET_PeerIdentity * peer,
688 struct CadetPeer *p = value;
689 struct CadetTunnel *t;
691 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
693 t = GCP_get_tunnel (p);
695 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
697 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
704 * Iterator over all paths of a peer to build an InfoPeer message.
706 * Message contains blocks of peers, first not included.
708 * @param cls Closure (message to build).
709 * @param peer Peer this path is towards.
710 * @param path Path itself
711 * @return #GNUNET_YES if should keep iterating.
712 * #GNUNET_NO otherwise.
715 path_info_iterator (void *cls,
716 struct CadetPeer *peer,
717 struct CadetPeerPath *path)
719 struct GNUNET_CADET_LocalInfoPeer *resp = cls;
720 struct GNUNET_PeerIdentity *id;
725 msg_size = ntohs (resp->header.size);
726 path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
728 LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
729 if (msg_size + path_size > UINT16_MAX)
731 LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
735 i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
736 i = i / sizeof (struct GNUNET_PeerIdentity);
738 /* Set id to the address of the first free peer slot. */
739 id = (struct GNUNET_PeerIdentity *) &resp[1];
742 /* Don't copy first peers.
743 * First peer is always the local one.
744 * Last peer is always the destination (leave as 0, EOL).
746 for (i = 0; i < path->length - 1; i++)
748 GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
749 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
752 resp->header.size = htons (msg_size + path_size);
759 * Handler for client's INFO PEERS request.
761 * @param cls Closure (unused).
762 * @param client Identification of the client.
763 * @param message The actual message.
766 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
767 const struct GNUNET_MessageHeader *message)
769 struct CadetClient *c;
770 struct GNUNET_MessageHeader reply;
772 /* Sanity check for client registration */
773 if (NULL == (c = GML_client_get (client)))
776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
780 LOG (GNUNET_ERROR_TYPE_DEBUG,
781 "Received get peers request from client %u (%p)\n",
784 GCP_iterate_all (get_all_peers_iterator, client);
785 reply.size = htons (sizeof (reply));
786 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
787 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
789 LOG (GNUNET_ERROR_TYPE_DEBUG,
790 "Get peers request from client %u completed\n", c->id);
791 GNUNET_SERVER_receive_done (client, GNUNET_OK);
796 * Handler for client's SHOW_PEER request.
798 * @param cls Closure (unused).
799 * @param client Identification of the client.
800 * @param message The actual message.
803 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
804 const struct GNUNET_MessageHeader *message)
806 const struct GNUNET_CADET_LocalInfo *msg;
807 struct GNUNET_CADET_LocalInfoPeer *resp;
809 struct CadetClient *c;
810 unsigned char cbuf[64 * 1024];
812 /* Sanity check for client registration */
813 if (NULL == (c = GML_client_get (client)))
816 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
820 msg = (struct GNUNET_CADET_LocalInfo *) message;
821 resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
822 LOG (GNUNET_ERROR_TYPE_INFO,
823 "Received peer info request from client %u for peer %s\n",
824 c->id, GNUNET_i2s_full (&msg->peer));
826 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
827 resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
828 resp->destination = msg->peer;
829 p = GCP_get (&msg->peer, GNUNET_NO);
832 /* We don't know the peer */
834 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
835 GNUNET_i2s_full (&msg->peer));
836 resp->paths = htons (0);
837 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
839 GNUNET_SERVER_notification_context_unicast (nc, client,
842 GNUNET_SERVER_receive_done (client, GNUNET_OK);
846 resp->paths = htons (GCP_count_paths (p));
847 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
848 GCP_iterate_paths (p, &path_info_iterator, resp);
850 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
851 &resp->header, GNUNET_NO);
853 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
854 GNUNET_SERVER_receive_done (client, GNUNET_OK);
859 * Iterator over all tunnels to send a monitoring client info about each tunnel.
861 * @param cls Closure ().
862 * @param peer Peer ID (tunnel remote peer).
863 * @param value Tunnel info.
865 * @return #GNUNET_YES, to keep iterating.
868 get_all_tunnels_iterator (void *cls,
869 const struct GNUNET_PeerIdentity * peer,
872 struct GNUNET_SERVER_Client *client = cls;
873 struct CadetTunnel *t = value;
874 struct GNUNET_CADET_LocalInfoTunnel msg;
876 msg.header.size = htons (sizeof (msg));
877 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
878 msg.destination = *peer;
879 msg.channels = htonl (GCT_count_channels (t));
880 msg.connections = htonl (GCT_count_any_connections (t));
881 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
882 msg.estate = htons ((uint16_t) GCT_get_estate (t));
884 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
887 GNUNET_SERVER_notification_context_unicast (nc, client,
888 &msg.header, GNUNET_NO);
894 * Handler for client's INFO TUNNELS request.
896 * @param cls Closure (unused).
897 * @param client Identification of the client.
898 * @param message The actual message.
901 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
902 const struct GNUNET_MessageHeader *message)
904 struct CadetClient *c;
905 struct GNUNET_MessageHeader reply;
907 /* Sanity check for client registration */
908 if (NULL == (c = GML_client_get (client)))
911 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
915 LOG (GNUNET_ERROR_TYPE_DEBUG,
916 "Received get tunnels request from client %u (%p)\n",
919 GCT_iterate_all (get_all_tunnels_iterator, client);
920 reply.size = htons (sizeof (reply));
921 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
922 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
924 LOG (GNUNET_ERROR_TYPE_DEBUG,
925 "Get tunnels request from client %u completed\n", c->id);
926 GNUNET_SERVER_receive_done (client, GNUNET_OK);
931 iter_connection (void *cls, struct CadetConnection *c)
933 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
934 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
936 h[msg->connections] = *(GCC_get_id (c));
941 iter_channel (void *cls, struct CadetChannel *ch)
943 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
944 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
945 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
947 chn[msg->channels] = htonl (GCCH_get_id (ch));
953 * Handler for client's SHOW_TUNNEL request.
955 * @param cls Closure (unused).
956 * @param client Identification of the client.
957 * @param message The actual message.
960 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
961 const struct GNUNET_MessageHeader *message)
963 const struct GNUNET_CADET_LocalInfo *msg;
964 struct GNUNET_CADET_LocalInfoTunnel *resp;
965 struct CadetClient *c;
966 struct CadetTunnel *t;
971 /* Sanity check for client registration */
972 if (NULL == (c = GML_client_get (client)))
975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
979 msg = (struct GNUNET_CADET_LocalInfo *) message;
980 LOG (GNUNET_ERROR_TYPE_DEBUG,
981 "Received tunnel info request from client %u for tunnel %s\n",
982 c->id, GNUNET_i2s_full(&msg->peer));
984 t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
987 /* We don't know the tunnel */
988 struct GNUNET_CADET_LocalInfoTunnel warn;
990 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
991 GNUNET_i2s_full(&msg->peer), sizeof (warn));
992 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
993 warn.header.size = htons (sizeof (warn));
994 warn.destination = msg->peer;
995 warn.channels = htonl (0);
996 warn.connections = htonl (0);
997 warn.cstate = htons (0);
998 warn.estate = htons (0);
1000 GNUNET_SERVER_notification_context_unicast (nc, client,
1003 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1007 /* Initialize context */
1008 ch_n = GCT_count_channels (t);
1009 c_n = GCT_count_any_connections (t);
1011 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1012 size += c_n * sizeof (struct GNUNET_CADET_Hash);
1013 size += ch_n * sizeof (CADET_ChannelNumber);
1015 resp = GNUNET_malloc (size);
1016 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1017 resp->header.size = htons (size);
1018 GCT_iterate_connections (t, &iter_connection, resp);
1019 GCT_iterate_channels (t, &iter_channel, resp);
1020 /* Do not interleave with iterators, iter_channel needs conn in HBO */
1021 resp->destination = msg->peer;
1022 resp->connections = htonl (resp->connections);
1023 resp->channels = htonl (resp->channels);
1024 resp->cstate = htons (GCT_get_cstate (t));
1025 resp->estate = htons (GCT_get_estate (t));
1026 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1027 &resp->header, GNUNET_NO);
1030 LOG (GNUNET_ERROR_TYPE_DEBUG,
1031 "Show tunnel request from client %u completed. %u conn, %u ch\n",
1033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1038 * Handler for client's INFO_DUMP request.
1040 * @param cls Closure (unused).
1041 * @param client Identification of the client.
1042 * @param message The actual message.
1045 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
1046 const struct GNUNET_MessageHeader *message)
1048 struct CadetClient *c;
1050 /* Sanity check for client registration */
1051 if (NULL == (c = GML_client_get (client)))
1054 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1058 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
1060 LOG (GNUNET_ERROR_TYPE_ERROR,
1061 "*************************** DUMP START ***************************\n");
1063 for (c = clients_head; NULL != c; c = c->next)
1065 LOG (GNUNET_ERROR_TYPE_ERROR, "Client %u (%p), handle: %p\n",
1066 c->id, c, c->handle);
1067 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u ports registered\n",
1068 GNUNET_CONTAINER_multihashmap32_size (c->ports));
1069 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u own channles\n",
1070 GNUNET_CONTAINER_multihashmap32_size (c->own_channels));
1071 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u incoming channles\n",
1072 GNUNET_CONTAINER_multihashmap32_size (c->incoming_channels));
1074 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1075 GCP_iterate_all (&show_peer_iterator, NULL);
1077 LOG (GNUNET_ERROR_TYPE_ERROR,
1078 "**************************** DUMP END ****************************\n");
1080 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1085 * Functions to handle messages from clients
1087 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1088 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1089 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1090 sizeof (struct GNUNET_CADET_ChannelMessage)},
1091 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1092 sizeof (struct GNUNET_CADET_ChannelMessage)},
1093 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1094 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1095 sizeof (struct GNUNET_CADET_LocalAck)},
1096 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1097 sizeof (struct GNUNET_MessageHeader)},
1098 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1099 sizeof (struct GNUNET_CADET_LocalInfo)},
1100 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1101 sizeof (struct GNUNET_MessageHeader)},
1102 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1103 sizeof (struct GNUNET_CADET_LocalInfo)},
1104 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1105 sizeof (struct GNUNET_MessageHeader)},
1111 /******************************************************************************/
1112 /******************************** API ***********************************/
1113 /******************************************************************************/
1116 * Initialize server subsystem.
1118 * @param handle Server handle.
1121 GML_init (struct GNUNET_SERVER_Handle *handle)
1123 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1124 server_handle = handle;
1125 GNUNET_SERVER_suspend (server_handle);
1126 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1131 * Install server (service) handlers and start listening to clients.
1136 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1137 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1138 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1140 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1142 clients_head = NULL;
1143 clients_tail = NULL;
1145 GNUNET_SERVER_resume (server_handle);
1155 struct CadetClient *c;
1157 for (c = clients_head; NULL != clients_head; c = clients_head)
1162 GNUNET_SERVER_notification_context_destroy (nc);
1170 * Get a channel from a client.
1172 * @param c Client to check.
1173 * @param chid Channel ID, must be local (> 0x800...).
1175 * @return non-NULL if channel exists in the clients lists
1177 struct CadetChannel *
1178 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1180 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1182 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1184 GNUNET_break_op (0);
1185 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1189 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1190 map = c->incoming_channels;
1191 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1192 map = c->own_channels;
1201 LOG (GNUNET_ERROR_TYPE_DEBUG,
1202 "Client %s does no t have a valid map for CHID %X\n",
1206 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1211 * Add a channel to a client
1213 * @param client Client.
1214 * @param chid Channel ID.
1215 * @param ch Channel.
1218 GML_channel_add (struct CadetClient *client,
1220 struct CadetChannel *ch)
1222 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1223 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1224 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1225 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1226 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1227 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1234 * Remove a channel from a client.
1236 * @param client Client.
1237 * @param chid Channel ID.
1238 * @param ch Channel.
1241 GML_channel_remove (struct CadetClient *client,
1243 struct CadetChannel *ch)
1245 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1246 GNUNET_break (GNUNET_YES ==
1247 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1249 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1250 GNUNET_break (GNUNET_YES ==
1251 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1259 * Get the tunnel's next free local channel ID.
1263 * @return LID of a channel free to use.
1266 GML_get_next_chid (struct CadetClient *c)
1268 CADET_ChannelNumber chid;
1270 while (NULL != GML_channel_get (c, c->next_chid))
1272 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1273 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1275 chid = c->next_chid;
1276 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1283 * Check if client has registered with the service and has not disconnected
1285 * @param client the client to check
1287 * @return non-NULL if client exists in the global DLL
1289 struct CadetClient *
1290 GML_client_get (struct GNUNET_SERVER_Client *client)
1292 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1296 * Find a client that has opened a port
1298 * @param port Port to check.
1300 * @return non-NULL if a client has the port.
1302 struct CadetClient *
1303 GML_client_get_by_port (uint32_t port)
1305 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1310 * Deletes a channel from a client (either owner or destination).
1312 * @param c Client whose tunnel to delete.
1313 * @param ch Channel which should be deleted.
1314 * @param id Channel ID.
1317 GML_client_delete_channel (struct CadetClient *c,
1318 struct CadetChannel *ch,
1319 CADET_ChannelNumber id)
1323 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1325 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1327 if (GNUNET_YES != res)
1328 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1330 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1332 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1334 if (GNUNET_YES != res)
1335 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1344 * Build a local ACK message and send it to a local client, if needed.
1346 * If the client was already allowed to send data, do nothing.
1348 * @param c Client to whom send the ACK.
1349 * @param id Channel ID to use
1352 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1354 struct GNUNET_CADET_LocalAck msg;
1356 LOG (GNUNET_ERROR_TYPE_DEBUG,
1357 "send local %s ack on %X towards %p\n",
1358 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1360 msg.header.size = htons (sizeof (msg));
1361 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1362 msg.channel_id = htonl (id);
1363 GNUNET_SERVER_notification_context_unicast (nc,
1373 * Notify the client that a new incoming channel was created.
1375 * @param c Client to notify.
1376 * @param id Channel ID.
1377 * @param port Channel's destination port.
1378 * @param opt Options (bit array).
1379 * @param peer Origin peer.
1382 GML_send_channel_create (struct CadetClient *c,
1383 uint32_t id, uint32_t port, uint32_t opt,
1384 const struct GNUNET_PeerIdentity *peer)
1386 struct GNUNET_CADET_ChannelMessage msg;
1388 msg.header.size = htons (sizeof (msg));
1389 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1390 msg.channel_id = htonl (id);
1391 msg.port = htonl (port);
1392 msg.opt = htonl (opt);
1394 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1395 &msg.header, GNUNET_NO);
1400 * Build a local channel NACK message and send it to a local client.
1402 * @param c Client to whom send the NACK.
1403 * @param id Channel ID to use
1406 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1408 struct GNUNET_CADET_LocalAck msg;
1410 LOG (GNUNET_ERROR_TYPE_DEBUG,
1411 "send local nack on %X towards %p\n",
1414 msg.header.size = htons (sizeof (msg));
1415 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1416 msg.channel_id = htonl (id);
1417 GNUNET_SERVER_notification_context_unicast (nc,
1425 * Notify a client that a channel is no longer valid.
1428 * @param id ID of the channel that is destroyed.
1431 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1433 struct GNUNET_CADET_ChannelMessage msg;
1440 if (GNUNET_YES == c->shutting_down)
1442 msg.header.size = htons (sizeof (msg));
1443 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1444 msg.channel_id = htonl (id);
1445 msg.port = htonl (0);
1446 memset (&msg.peer, 0, sizeof (msg.peer));
1447 msg.opt = htonl (0);
1448 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1449 &msg.header, GNUNET_NO);
1454 * Modify the cadet message ID from global to local and send to client.
1456 * @param c Client to send to.
1457 * @param msg Message to modify and send.
1458 * @param id Channel ID to use (c can be both owner and client).
1461 GML_send_data (struct CadetClient *c,
1462 const struct GNUNET_CADET_Data *msg,
1463 CADET_ChannelNumber id)
1465 struct GNUNET_CADET_LocalData *copy;
1466 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1467 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1469 if (size < sizeof (struct GNUNET_MessageHeader))
1471 GNUNET_break_op (0);
1479 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1480 memcpy (©[1], &msg[1], size);
1481 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1482 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1483 copy->id = htonl (id);
1484 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1485 ©->header, GNUNET_NO);
1490 * Get the static string to represent a client.
1494 * @return Static string for the client.
1497 GML_2s (const struct CadetClient *c)
1499 static char buf[32];
1501 SPRINTF (buf, "%u", c->id);