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 /******************************************************************************/
172 /******************************** HANDLES ***********************************/
173 /******************************************************************************/
177 * Handler for client connection.
179 * @param cls Closure (unused).
180 * @param client Client handler.
183 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
185 struct CadetClient *c;
187 LOG (GNUNET_ERROR_TYPE_DEBUG, "client connected: %p\n", client);
190 c = GNUNET_new (struct CadetClient);
192 c->id = next_client_id++; /* overflow not important: just for debug */
193 c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
194 GNUNET_SERVER_client_keep (client);
195 GNUNET_SERVER_client_set_user_context (client, c);
196 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
201 * Iterator for deleting each channel whose client endpoint disconnected.
203 * @param cls Closure (client that has disconnected).
204 * @param key The local channel id (used to access the hashmap).
205 * @param value The value stored at the key (channel to destroy).
207 * @return GNUNET_OK, keep iterating.
210 channel_destroy_iterator (void *cls,
214 struct CadetChannel *ch = value;
215 struct CadetClient *c = cls;
217 LOG (GNUNET_ERROR_TYPE_DEBUG,
218 " Channel %s destroy, due to client %s shutdown.\n",
219 GCCH_2s (ch), GML_2s (c));
221 GCCH_handle_local_destroy (ch, c, key < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
227 * Handler for client disconnection
230 * @param client identification of the client; NULL
231 * for the last call when the server is destroyed
234 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
236 struct CadetClient *c;
238 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
241 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
245 c = GML_client_get (client);
248 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
250 GNUNET_SERVER_client_drop (c->handle);
251 c->shutting_down = GNUNET_YES;
252 if (NULL != c->own_channels)
254 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
255 &channel_destroy_iterator, c);
256 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
259 if (NULL != c->incoming_channels)
261 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
262 &channel_destroy_iterator, c);
263 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
266 if (NULL != c->ports)
268 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
269 &client_release_ports, c);
270 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
272 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
273 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
274 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
279 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
287 * Handler for new clients
290 * @param client identification of the client
291 * @param message the actual message, which includes messages the client wants
294 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
295 const struct GNUNET_MessageHeader *message)
297 struct GNUNET_CADET_ClientConnect *cc_msg;
298 struct CadetClient *c;
303 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
304 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
306 /* Check data sanity */
307 size = ntohs (message->size) - sizeof (struct GNUNET_CADET_ClientConnect);
308 cc_msg = (struct GNUNET_CADET_ClientConnect *) message;
309 if (0 != (size % sizeof (uint32_t)))
312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
315 size /= sizeof (uint32_t);
317 /* Initialize new client structure */
318 c = GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
326 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
332 p = (uint32_t *) &cc_msg[1];
333 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
334 for (i = 0; i < size; i++)
337 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
339 /* store in client's hashmap */
340 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
342 /* store in global hashmap */
343 /* FIXME only allow one client to have the port open,
344 * have a backup hashmap with waiting clients */
345 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
346 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
350 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
351 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
352 GNUNET_SERVER_notification_context_add (nc, client);
353 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
355 GNUNET_SERVER_receive_done (client, GNUNET_OK);
356 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
361 * Handler for requests of new tunnels
363 * @param cls Closure.
364 * @param client Identification of the client.
365 * @param message The actual message.
368 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
369 const struct GNUNET_MessageHeader *message)
371 struct CadetClient *c;
373 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
374 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
376 /* Sanity check for client registration */
377 if (NULL == (c = GML_client_get (client)))
380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
383 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
385 /* Message size sanity check */
386 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
394 GCCH_handle_local_create (c,
395 (struct GNUNET_CADET_ChannelMessage *) message))
397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
401 GNUNET_SERVER_receive_done (client, GNUNET_OK);
407 * Handler for requests of deleting tunnels
410 * @param client identification of the client
411 * @param message the actual message
414 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
415 const struct GNUNET_MessageHeader *message)
417 struct GNUNET_CADET_ChannelMessage *msg;
418 struct CadetClient *c;
419 struct CadetChannel *ch;
420 CADET_ChannelNumber chid;
422 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
424 /* Sanity check for client registration */
425 if (NULL == (c = GML_client_get (client)))
428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
431 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
433 /* Message sanity check */
434 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
441 msg = (struct GNUNET_CADET_ChannelMessage *) message;
443 /* Retrieve tunnel */
444 chid = ntohl (msg->channel_id);
445 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
446 ch = GML_channel_get (c, chid);
449 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
450 GNUNET_STATISTICS_update (stats,
451 "# client destroy messages on unknown channel",
453 GNUNET_SERVER_receive_done (client, GNUNET_OK);
457 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
465 * Handler for client traffic
468 * @param client identification of the client
469 * @param message the actual message
472 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
473 const struct GNUNET_MessageHeader *message)
475 const struct GNUNET_MessageHeader *payload;
476 struct GNUNET_CADET_LocalData *msg;
477 struct CadetClient *c;
478 struct CadetChannel *ch;
479 CADET_ChannelNumber chid;
482 size_t payload_claimed_size;
485 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
486 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
487 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
489 /* Sanity check for client registration */
490 if (NULL == (c = GML_client_get (client)))
493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
497 /* Sanity check for message size */
498 message_size = ntohs (message->size);
499 if (sizeof (struct GNUNET_CADET_LocalData)
500 + sizeof (struct GNUNET_MessageHeader) > message_size
501 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
508 /* Sanity check for payload size */
509 payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
510 msg = (struct GNUNET_CADET_LocalData *) message;
511 payload = (struct GNUNET_MessageHeader *) &msg[1];
512 payload_claimed_size = ntohs (payload->size);
513 if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
514 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
515 || payload_claimed_size > payload_size)
517 LOG (GNUNET_ERROR_TYPE_WARNING,
518 "client claims to send %u bytes in %u payload\n",
519 payload_claimed_size, payload_size);
521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
525 chid = ntohl (msg->id);
526 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes (%u payload) by client %u\n",
527 payload_size, payload_claimed_size, c->id);
529 /* Channel exists? */
530 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
531 ch = GML_channel_get (c, chid);
534 GNUNET_STATISTICS_update (stats,
535 "# client data messages on unknown channel",
537 GNUNET_SERVER_receive_done (client, GNUNET_OK);
541 if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
547 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
548 GNUNET_SERVER_receive_done (client, GNUNET_OK);
555 * Handler for client's ACKs for payload traffic.
557 * @param cls Closure (unused).
558 * @param client Identification of the client.
559 * @param message The actual message.
562 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
563 const struct GNUNET_MessageHeader *message)
565 struct GNUNET_CADET_LocalAck *msg;
566 struct CadetChannel *ch;
567 struct CadetClient *c;
568 CADET_ChannelNumber chid;
571 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
572 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
574 /* Sanity check for client registration */
575 if (NULL == (c = GML_client_get (client)))
578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
581 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
583 msg = (struct GNUNET_CADET_LocalAck *) message;
585 /* Channel exists? */
586 chid = ntohl (msg->channel_id);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
588 ch = GML_channel_get (c, chid);
589 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
592 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
593 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
594 GNUNET_STATISTICS_update (stats,
595 "# client ack messages on unknown channel",
597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
601 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
602 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
603 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
605 GCCH_handle_local_ack (ch, fwd);
606 GNUNET_SERVER_receive_done (client, GNUNET_OK);
613 * Iterator over all peers to send a monitoring client info about each peer.
615 * @param cls Closure ().
616 * @param peer Peer ID (tunnel remote peer).
617 * @param value Peer info.
619 * @return #GNUNET_YES, to keep iterating.
622 get_all_peers_iterator (void *cls,
623 const struct GNUNET_PeerIdentity * peer,
626 struct GNUNET_SERVER_Client *client = cls;
627 struct CadetPeer *p = value;
628 struct GNUNET_CADET_LocalInfoPeer msg;
630 msg.header.size = htons (sizeof (msg));
631 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
632 msg.destination = *peer;
633 msg.paths = htons (GCP_count_paths (p));
634 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
636 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
639 GNUNET_SERVER_notification_context_unicast (nc, client,
640 &msg.header, GNUNET_NO);
646 * Iterator over all peers to dump info for each peer.
648 * @param cls Closure (unused).
649 * @param peer Peer ID (tunnel remote peer).
650 * @param value Peer info.
652 * @return #GNUNET_YES, to keep iterating.
655 show_peer_iterator (void *cls,
656 const struct GNUNET_PeerIdentity * peer,
659 struct CadetPeer *p = value;
660 struct CadetTunnel *t;
662 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
664 t = GCP_get_tunnel (p);
666 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
668 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
675 * Iterator over all paths of a peer to build an InfoPeer message.
677 * Message contains blocks of peers, first not included.
679 * @param cls Closure (message to build).
680 * @param peer Peer this path is towards.
681 * @param path Path itself
682 * @return #GNUNET_YES if should keep iterating.
683 * #GNUNET_NO otherwise.
686 path_info_iterator (void *cls,
687 struct CadetPeer *peer,
688 struct CadetPeerPath *path)
690 struct GNUNET_CADET_LocalInfoPeer *resp = cls;
691 struct GNUNET_PeerIdentity *id;
696 msg_size = ntohs (resp->header.size);
697 path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
699 LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
700 if (msg_size + path_size > UINT16_MAX)
702 LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
706 i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
707 i = i / sizeof (struct GNUNET_PeerIdentity);
709 /* Set id to the address of the first free peer slot. */
710 id = (struct GNUNET_PeerIdentity *) &resp[1];
713 /* Don't copy first peers.
714 * First peer is always the local one.
715 * Last peer is always the destination (leave as 0, EOL).
717 for (i = 0; i < path->length - 1; i++)
719 GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
720 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
723 resp->header.size = htons (msg_size + path_size);
730 * Handler for client's INFO PEERS request.
732 * @param cls Closure (unused).
733 * @param client Identification of the client.
734 * @param message The actual message.
737 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
738 const struct GNUNET_MessageHeader *message)
740 struct CadetClient *c;
741 struct GNUNET_MessageHeader reply;
743 /* Sanity check for client registration */
744 if (NULL == (c = GML_client_get (client)))
747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
751 LOG (GNUNET_ERROR_TYPE_DEBUG,
752 "Received get peers request from client %u (%p)\n",
755 GCP_iterate_all (get_all_peers_iterator, client);
756 reply.size = htons (sizeof (reply));
757 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
758 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
760 LOG (GNUNET_ERROR_TYPE_DEBUG,
761 "Get peers request from client %u completed\n", c->id);
762 GNUNET_SERVER_receive_done (client, GNUNET_OK);
767 * Handler for client's SHOW_PEER request.
769 * @param cls Closure (unused).
770 * @param client Identification of the client.
771 * @param message The actual message.
774 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
775 const struct GNUNET_MessageHeader *message)
777 const struct GNUNET_CADET_LocalInfo *msg;
778 struct GNUNET_CADET_LocalInfoPeer *resp;
780 struct CadetClient *c;
781 unsigned char cbuf[64 * 1024];
783 /* Sanity check for client registration */
784 if (NULL == (c = GML_client_get (client)))
787 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
791 msg = (struct GNUNET_CADET_LocalInfo *) message;
792 resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
793 LOG (GNUNET_ERROR_TYPE_INFO,
794 "Received peer info request from client %u for peer %s\n",
795 c->id, GNUNET_i2s_full (&msg->peer));
797 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
798 resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
799 resp->destination = msg->peer;
800 p = GCP_get (&msg->peer, GNUNET_NO);
803 /* We don't know the peer */
805 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
806 GNUNET_i2s_full (&msg->peer));
807 resp->paths = htons (0);
808 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
810 GNUNET_SERVER_notification_context_unicast (nc, client,
813 GNUNET_SERVER_receive_done (client, GNUNET_OK);
817 resp->paths = htons (GCP_count_paths (p));
818 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
819 GCP_iterate_paths (p, &path_info_iterator, resp);
821 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
822 &resp->header, GNUNET_NO);
824 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
825 GNUNET_SERVER_receive_done (client, GNUNET_OK);
830 * Iterator over all tunnels to send a monitoring client info about each tunnel.
832 * @param cls Closure ().
833 * @param peer Peer ID (tunnel remote peer).
834 * @param value Tunnel info.
836 * @return #GNUNET_YES, to keep iterating.
839 get_all_tunnels_iterator (void *cls,
840 const struct GNUNET_PeerIdentity * peer,
843 struct GNUNET_SERVER_Client *client = cls;
844 struct CadetTunnel *t = value;
845 struct GNUNET_CADET_LocalInfoTunnel msg;
847 msg.header.size = htons (sizeof (msg));
848 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
849 msg.destination = *peer;
850 msg.channels = htonl (GCT_count_channels (t));
851 msg.connections = htonl (GCT_count_any_connections (t));
852 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
853 msg.estate = htons ((uint16_t) GCT_get_estate (t));
855 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
858 GNUNET_SERVER_notification_context_unicast (nc, client,
859 &msg.header, GNUNET_NO);
865 * Handler for client's INFO TUNNELS request.
867 * @param cls Closure (unused).
868 * @param client Identification of the client.
869 * @param message The actual message.
872 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
873 const struct GNUNET_MessageHeader *message)
875 struct CadetClient *c;
876 struct GNUNET_MessageHeader reply;
878 /* Sanity check for client registration */
879 if (NULL == (c = GML_client_get (client)))
882 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
886 LOG (GNUNET_ERROR_TYPE_DEBUG,
887 "Received get tunnels request from client %u (%p)\n",
890 GCT_iterate_all (get_all_tunnels_iterator, client);
891 reply.size = htons (sizeof (reply));
892 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
893 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
895 LOG (GNUNET_ERROR_TYPE_DEBUG,
896 "Get tunnels request from client %u completed\n", c->id);
897 GNUNET_SERVER_receive_done (client, GNUNET_OK);
902 iter_connection (void *cls, struct CadetConnection *c)
904 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
905 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
907 h[msg->connections] = *(GCC_get_id (c));
912 iter_channel (void *cls, struct CadetChannel *ch)
914 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
915 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
916 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
918 chn[msg->channels] = GCCH_get_id (ch);
924 * Handler for client's SHOW_TUNNEL request.
926 * @param cls Closure (unused).
927 * @param client Identification of the client.
928 * @param message The actual message.
931 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
932 const struct GNUNET_MessageHeader *message)
934 const struct GNUNET_CADET_LocalInfo *msg;
935 struct GNUNET_CADET_LocalInfoTunnel *resp;
936 struct CadetClient *c;
937 struct CadetTunnel *t;
942 /* Sanity check for client registration */
943 if (NULL == (c = GML_client_get (client)))
946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
950 msg = (struct GNUNET_CADET_LocalInfo *) message;
951 LOG (GNUNET_ERROR_TYPE_INFO,
952 "Received tunnel info request from client %u for tunnel %s\n",
953 c->id, GNUNET_i2s_full(&msg->peer));
955 t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
958 /* We don't know the tunnel */
959 struct GNUNET_CADET_LocalInfoTunnel warn;
961 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
962 GNUNET_i2s_full(&msg->peer), sizeof (warn));
963 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
964 warn.header.size = htons (sizeof (warn));
965 warn.destination = msg->peer;
966 warn.channels = htonl (0);
967 warn.connections = htonl (0);
968 warn.cstate = htons (0);
969 warn.estate = htons (0);
971 GNUNET_SERVER_notification_context_unicast (nc, client,
974 GNUNET_SERVER_receive_done (client, GNUNET_OK);
978 /* Initialize context */
979 ch_n = GCT_count_channels (t);
980 c_n = GCT_count_any_connections (t);
982 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
983 size += c_n * sizeof (struct GNUNET_CADET_Hash);
984 size += ch_n * sizeof (CADET_ChannelNumber);
986 resp = GNUNET_malloc (size);
987 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
988 resp->header.size = htons (size);
989 GCT_iterate_connections (t, &iter_connection, resp);
990 GCT_iterate_channels (t, &iter_channel, resp);
991 /* Do not interleave with iterators, iter_channel needs conn in HBO */
992 resp->destination = msg->peer;
993 resp->connections = htonl (resp->connections);
994 resp->channels = htonl (resp->channels);
995 resp->cstate = htons (GCT_get_cstate (t));
996 resp->estate = htons (GCT_get_estate (t));
997 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
998 &resp->header, GNUNET_NO);
1001 LOG (GNUNET_ERROR_TYPE_INFO,
1002 "Show tunnel request from client %u completed. %u conn, %u ch\n",
1004 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1009 * Handler for client's INFO_DUMP request.
1011 * @param cls Closure (unused).
1012 * @param client Identification of the client.
1013 * @param message The actual message.
1016 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
1017 const struct GNUNET_MessageHeader *message)
1019 struct CadetClient *c;
1021 /* Sanity check for client registration */
1022 if (NULL == (c = GML_client_get (client)))
1025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1029 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
1032 LOG (GNUNET_ERROR_TYPE_ERROR,
1033 "*************************** DUMP START ***************************\n");
1035 GCP_iterate_all (&show_peer_iterator, NULL);
1037 LOG (GNUNET_ERROR_TYPE_ERROR,
1038 "**************************** DUMP END ****************************\n");
1040 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1045 * Functions to handle messages from clients
1047 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1048 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1049 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1050 sizeof (struct GNUNET_CADET_ChannelMessage)},
1051 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1052 sizeof (struct GNUNET_CADET_ChannelMessage)},
1053 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1054 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1055 sizeof (struct GNUNET_CADET_LocalAck)},
1056 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1057 sizeof (struct GNUNET_MessageHeader)},
1058 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1059 sizeof (struct GNUNET_CADET_LocalInfo)},
1060 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1061 sizeof (struct GNUNET_MessageHeader)},
1062 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1063 sizeof (struct GNUNET_CADET_LocalInfo)},
1064 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1065 sizeof (struct GNUNET_MessageHeader)},
1071 /******************************************************************************/
1072 /******************************** API ***********************************/
1073 /******************************************************************************/
1076 * Initialize server subsystem.
1078 * @param handle Server handle.
1081 GML_init (struct GNUNET_SERVER_Handle *handle)
1083 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1084 server_handle = handle;
1085 GNUNET_SERVER_suspend (server_handle);
1086 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1091 * Install server (service) handlers and start listening to clients.
1096 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1097 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1098 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1100 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1102 clients_head = NULL;
1103 clients_tail = NULL;
1105 GNUNET_SERVER_resume (server_handle);
1117 GNUNET_SERVER_notification_context_destroy (nc);
1124 * Get a channel from a client.
1126 * @param c Client to check.
1127 * @param chid Channel ID, must be local (> 0x800...).
1129 * @return non-NULL if channel exists in the clients lists
1131 struct CadetChannel *
1132 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1134 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1136 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1138 GNUNET_break_op (0);
1139 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1143 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1144 map = c->incoming_channels;
1145 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1146 map = c->own_channels;
1155 LOG (GNUNET_ERROR_TYPE_DEBUG,
1156 "Client %s does no t have a valid map for CHID %X\n",
1160 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1165 * Add a channel to a client
1167 * @param client Client.
1168 * @param chid Channel ID.
1169 * @param ch Channel.
1172 GML_channel_add (struct CadetClient *client,
1174 struct CadetChannel *ch)
1176 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1177 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1178 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1179 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1180 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1188 * Remove a channel from a client.
1190 * @param client Client.
1191 * @param chid Channel ID.
1192 * @param ch Channel.
1195 GML_channel_remove (struct CadetClient *client,
1197 struct CadetChannel *ch)
1199 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1200 GNUNET_break (GNUNET_YES ==
1201 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1203 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1204 GNUNET_break (GNUNET_YES ==
1205 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1213 * Get the tunnel's next free local channel ID.
1217 * @return LID of a channel free to use.
1220 GML_get_next_chid (struct CadetClient *c)
1222 CADET_ChannelNumber chid;
1224 while (NULL != GML_channel_get (c, c->next_chid))
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1227 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1229 chid = c->next_chid;
1230 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1237 * Check if client has registered with the service and has not disconnected
1239 * @param client the client to check
1241 * @return non-NULL if client exists in the global DLL
1243 struct CadetClient *
1244 GML_client_get (struct GNUNET_SERVER_Client *client)
1246 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1250 * Find a client that has opened a port
1252 * @param port Port to check.
1254 * @return non-NULL if a client has the port.
1256 struct CadetClient *
1257 GML_client_get_by_port (uint32_t port)
1259 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1264 * Deletes a channel from a client (either owner or destination).
1266 * @param c Client whose tunnel to delete.
1267 * @param ch Channel which should be deleted.
1268 * @param id Channel ID.
1271 GML_client_delete_channel (struct CadetClient *c,
1272 struct CadetChannel *ch,
1273 CADET_ChannelNumber id)
1277 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1279 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1281 if (GNUNET_YES != res)
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1284 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1286 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1288 if (GNUNET_YES != res)
1289 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1298 * Build a local ACK message and send it to a local client, if needed.
1300 * If the client was already allowed to send data, do nothing.
1302 * @param c Client to whom send the ACK.
1303 * @param id Channel ID to use
1306 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1308 struct GNUNET_CADET_LocalAck msg;
1310 LOG (GNUNET_ERROR_TYPE_DEBUG,
1311 "send local %s ack on %X towards %p\n",
1312 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1314 msg.header.size = htons (sizeof (msg));
1315 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1316 msg.channel_id = htonl (id);
1317 GNUNET_SERVER_notification_context_unicast (nc,
1327 * Notify the client that a new incoming channel was created.
1329 * @param c Client to notify.
1330 * @param id Channel ID.
1331 * @param port Channel's destination port.
1332 * @param opt Options (bit array).
1333 * @param peer Origin peer.
1336 GML_send_channel_create (struct CadetClient *c,
1337 uint32_t id, uint32_t port, uint32_t opt,
1338 const struct GNUNET_PeerIdentity *peer)
1340 struct GNUNET_CADET_ChannelMessage msg;
1342 msg.header.size = htons (sizeof (msg));
1343 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1344 msg.channel_id = htonl (id);
1345 msg.port = htonl (port);
1346 msg.opt = htonl (opt);
1348 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1349 &msg.header, GNUNET_NO);
1354 * Build a local channel NACK message and send it to a local client.
1356 * @param c Client to whom send the NACK.
1357 * @param id Channel ID to use
1360 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1362 struct GNUNET_CADET_LocalAck msg;
1364 LOG (GNUNET_ERROR_TYPE_DEBUG,
1365 "send local nack on %X towards %p\n",
1368 msg.header.size = htons (sizeof (msg));
1369 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1370 msg.channel_id = htonl (id);
1371 GNUNET_SERVER_notification_context_unicast (nc,
1379 * Notify a client that a channel is no longer valid.
1382 * @param id ID of the channel that is destroyed.
1385 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1387 struct GNUNET_CADET_ChannelMessage msg;
1394 if (GNUNET_YES == c->shutting_down)
1396 msg.header.size = htons (sizeof (msg));
1397 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1398 msg.channel_id = htonl (id);
1399 msg.port = htonl (0);
1400 memset (&msg.peer, 0, sizeof (msg.peer));
1401 msg.opt = htonl (0);
1402 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1403 &msg.header, GNUNET_NO);
1408 * Modify the cadet message ID from global to local and send to client.
1410 * @param c Client to send to.
1411 * @param msg Message to modify and send.
1412 * @param id Channel ID to use (c can be both owner and client).
1415 GML_send_data (struct CadetClient *c,
1416 const struct GNUNET_CADET_Data *msg,
1417 CADET_ChannelNumber id)
1419 struct GNUNET_CADET_LocalData *copy;
1420 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1421 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1423 if (size < sizeof (struct GNUNET_MessageHeader))
1425 GNUNET_break_op (0);
1433 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1434 memcpy (©[1], &msg[1], size);
1435 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1436 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1437 copy->id = htonl (id);
1438 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1439 ©->header, GNUNET_NO);
1444 * Get the static string to represent a client.
1448 * @return Static string for the client.
1451 GML_2s (const struct CadetClient *c)
1453 static char buf[32];
1455 SPRINTF (buf, "%u", c->id);