2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
28 #include "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);
226 * Handler for client disconnection
229 * @param client identification of the client; NULL
230 * for the last call when the server is destroyed
233 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
235 struct CadetClient *c;
237 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
240 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
244 c = GML_client_get (client);
247 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
249 GNUNET_SERVER_client_drop (c->handle);
250 c->shutting_down = GNUNET_YES;
251 if (NULL != c->own_channels)
253 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
254 &channel_destroy_iterator, c);
255 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
258 if (NULL != c->incoming_channels)
260 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
261 &channel_destroy_iterator, c);
262 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
265 if (NULL != c->ports)
267 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
268 &client_release_ports, c);
269 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
271 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
272 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
273 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
278 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
280 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
286 * Handler for new clients
289 * @param client identification of the client
290 * @param message the actual message, which includes messages the client wants
293 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
294 const struct GNUNET_MessageHeader *message)
296 struct GNUNET_CADET_ClientConnect *cc_msg;
297 struct CadetClient *c;
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
303 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
305 /* Check data sanity */
306 size = ntohs (message->size) - sizeof (struct GNUNET_CADET_ClientConnect);
307 cc_msg = (struct GNUNET_CADET_ClientConnect *) message;
308 if (0 != (size % sizeof (uint32_t)))
311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
314 size /= sizeof (uint32_t);
316 /* Initialize new client structure */
317 c = GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
325 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
326 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
331 p = (uint32_t *) &cc_msg[1];
332 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
333 for (i = 0; i < size; i++)
336 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
338 /* store in client's hashmap */
339 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
340 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
341 /* store in global hashmap */
342 /* FIXME only allow one client to have the port open,
343 * have a backup hashmap with waiting clients */
344 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
345 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
349 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
350 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
351 GNUNET_SERVER_notification_context_add (nc, client);
352 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
354 GNUNET_SERVER_receive_done (client, GNUNET_OK);
355 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
360 * Handler for requests of new tunnels
362 * @param cls Closure.
363 * @param client Identification of the client.
364 * @param message The actual message.
367 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
368 const struct GNUNET_MessageHeader *message)
370 struct CadetClient *c;
372 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
373 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
375 /* Sanity check for client registration */
376 if (NULL == (c = GML_client_get (client)))
379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
382 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
384 /* Message size sanity check */
385 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
393 GCCH_handle_local_create (c,
394 (struct GNUNET_CADET_ChannelMessage *) message))
396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
406 * Handler for requests of deleting tunnels
409 * @param client identification of the client
410 * @param message the actual message
413 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
414 const struct GNUNET_MessageHeader *message)
416 struct GNUNET_CADET_ChannelMessage *msg;
417 struct CadetClient *c;
418 struct CadetChannel *ch;
419 CADET_ChannelNumber chid;
421 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
423 /* Sanity check for client registration */
424 if (NULL == (c = GML_client_get (client)))
427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
430 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
432 /* Message sanity check */
433 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
440 msg = (struct GNUNET_CADET_ChannelMessage *) message;
442 /* Retrieve tunnel */
443 chid = ntohl (msg->channel_id);
444 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
445 ch = GML_channel_get (c, chid);
448 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
449 GNUNET_STATISTICS_update (stats,
450 "# client destroy messages on unknown channel",
452 GNUNET_SERVER_receive_done (client, GNUNET_OK);
456 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
458 GNUNET_SERVER_receive_done (client, GNUNET_OK);
464 * Handler for client traffic
467 * @param client identification of the client
468 * @param message the actual message
471 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
472 const struct GNUNET_MessageHeader *message)
474 const struct GNUNET_MessageHeader *payload;
475 struct GNUNET_CADET_LocalData *msg;
476 struct CadetClient *c;
477 struct CadetChannel *ch;
478 CADET_ChannelNumber chid;
481 size_t payload_claimed_size;
484 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
485 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
486 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
488 /* Sanity check for client registration */
489 if (NULL == (c = GML_client_get (client)))
492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
496 /* Sanity check for message size */
497 message_size = ntohs (message->size);
498 if (sizeof (struct GNUNET_CADET_LocalData)
499 + sizeof (struct GNUNET_MessageHeader) > message_size)
502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
505 payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
506 msg = (struct GNUNET_CADET_LocalData *) message;
507 payload = (struct GNUNET_MessageHeader *) &msg[1];
508 payload_claimed_size = ntohs (payload->size);
509 if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
510 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
511 || payload_claimed_size > payload_size)
513 LOG (GNUNET_ERROR_TYPE_WARNING,
514 "client claims to send %u bytes in %u payload\n",
515 payload_claimed_size, payload_size);
517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
521 chid = ntohl (msg->id);
522 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
524 /* Channel exists? */
525 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
526 ch = GML_channel_get (c, chid);
529 GNUNET_STATISTICS_update (stats,
530 "# client data messages on unknown channel",
532 GNUNET_SERVER_receive_done (client, GNUNET_OK);
536 if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
550 * Handler for client's ACKs for payload traffic.
552 * @param cls Closure (unused).
553 * @param client Identification of the client.
554 * @param message The actual message.
557 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
558 const struct GNUNET_MessageHeader *message)
560 struct GNUNET_CADET_LocalAck *msg;
561 struct CadetChannel *ch;
562 struct CadetClient *c;
563 CADET_ChannelNumber chid;
566 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
567 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
569 /* Sanity check for client registration */
570 if (NULL == (c = GML_client_get (client)))
573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
576 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
578 msg = (struct GNUNET_CADET_LocalAck *) message;
580 /* Channel exists? */
581 chid = ntohl (msg->channel_id);
582 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
583 ch = GML_channel_get (c, chid);
584 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
588 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
589 GNUNET_STATISTICS_update (stats,
590 "# client ack messages on unknown channel",
592 GNUNET_SERVER_receive_done (client, GNUNET_OK);
596 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
597 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
598 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
600 GCCH_handle_local_ack (ch, fwd);
601 GNUNET_SERVER_receive_done (client, GNUNET_OK);
608 * Iterator over all peers to send a monitoring client info about each peer.
610 * @param cls Closure ().
611 * @param peer Peer ID (tunnel remote peer).
612 * @param value Peer info.
614 * @return #GNUNET_YES, to keep iterating.
617 get_all_peers_iterator (void *cls,
618 const struct GNUNET_PeerIdentity * peer,
621 struct GNUNET_SERVER_Client *client = cls;
622 struct CadetPeer *p = value;
623 struct GNUNET_CADET_LocalInfoPeer msg;
625 msg.header.size = htons (sizeof (msg));
626 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
627 msg.destination = *peer;
628 msg.paths = htons (GCP_count_paths (p));
629 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
631 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
634 GNUNET_SERVER_notification_context_unicast (nc, client,
635 &msg.header, GNUNET_NO);
641 * Iterator over all peers to dump info for each peer.
643 * @param cls Closure (unused).
644 * @param peer Peer ID (tunnel remote peer).
645 * @param value Peer info.
647 * @return #GNUNET_YES, to keep iterating.
650 show_peer_iterator (void *cls,
651 const struct GNUNET_PeerIdentity * peer,
654 struct CadetPeer *p = value;
655 struct CadetTunnel *t;
657 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
659 t = GCP_get_tunnel (p);
661 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
663 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
670 * Handler for client's INFO PEERS request.
672 * @param cls Closure (unused).
673 * @param client Identification of the client.
674 * @param message The actual message.
677 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
678 const struct GNUNET_MessageHeader *message)
680 struct CadetClient *c;
681 struct GNUNET_MessageHeader reply;
683 /* Sanity check for client registration */
684 if (NULL == (c = GML_client_get (client)))
687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "Received get peers request from client %u (%p)\n",
695 GCP_iterate_all (get_all_peers_iterator, client);
696 reply.size = htons (sizeof (reply));
697 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
698 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
700 LOG (GNUNET_ERROR_TYPE_DEBUG,
701 "Get peers request from client %u completed\n", c->id);
702 GNUNET_SERVER_receive_done (client, GNUNET_OK);
707 * Handler for client's SHOW_PEER request.
709 * @param cls Closure (unused).
710 * @param client Identification of the client.
711 * @param message The actual message.
714 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
715 const struct GNUNET_MessageHeader *message)
717 const struct GNUNET_CADET_LocalInfo *msg;
718 struct GNUNET_CADET_LocalInfoPeer *resp;
720 struct CadetClient *c;
723 /* Sanity check for client registration */
724 if (NULL == (c = GML_client_get (client)))
727 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
731 msg = (struct GNUNET_CADET_LocalInfo *) message;
732 LOG (GNUNET_ERROR_TYPE_INFO,
733 "Received peer info request from client %u for peer %s\n",
734 c->id, GNUNET_i2s_full (&msg->peer));
736 p = GCP_get (&msg->peer);
739 /* We don't know the peer */
740 struct GNUNET_CADET_LocalInfoPeer warn;
742 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown %u\n",
743 GNUNET_i2s_full (&msg->peer), sizeof (warn));
744 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
745 warn.header.size = htons (sizeof (warn));
746 warn.destination = msg->peer;
747 warn.paths = htons (0);
748 warn.tunnel = htons (NULL != GCP_get_tunnel (p));
750 GNUNET_SERVER_notification_context_unicast (nc, client,
753 GNUNET_SERVER_receive_done (client, GNUNET_OK);
757 size = sizeof (struct GNUNET_CADET_LocalInfoPeer);
758 // size += c_n * sizeof (struct GNUNET_CADET_Hash);
760 resp = GNUNET_malloc (size);
761 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
762 resp->header.size = htons (size);
763 resp->destination = msg->peer;
764 resp->paths = htons (0);
765 resp->tunnel = htons (0);
767 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
768 &resp->header, GNUNET_NO);
771 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
772 GNUNET_SERVER_receive_done (client, GNUNET_OK);
777 * Iterator over all tunnels to send a monitoring client info about each tunnel.
779 * @param cls Closure ().
780 * @param peer Peer ID (tunnel remote peer).
781 * @param value Tunnel info.
783 * @return #GNUNET_YES, to keep iterating.
786 get_all_tunnels_iterator (void *cls,
787 const struct GNUNET_PeerIdentity * peer,
790 struct GNUNET_SERVER_Client *client = cls;
791 struct CadetTunnel *t = value;
792 struct GNUNET_CADET_LocalInfoTunnel msg;
794 msg.header.size = htons (sizeof (msg));
795 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
796 msg.destination = *peer;
797 msg.channels = htonl (GCT_count_channels (t));
798 msg.connections = htonl (GCT_count_any_connections (t));
799 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
800 msg.estate = htons ((uint16_t) GCT_get_estate (t));
802 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
805 GNUNET_SERVER_notification_context_unicast (nc, client,
806 &msg.header, GNUNET_NO);
812 * Handler for client's INFO TUNNELS request.
814 * @param cls Closure (unused).
815 * @param client Identification of the client.
816 * @param message The actual message.
819 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
820 const struct GNUNET_MessageHeader *message)
822 struct CadetClient *c;
823 struct GNUNET_MessageHeader reply;
825 /* Sanity check for client registration */
826 if (NULL == (c = GML_client_get (client)))
829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 "Received get tunnels request from client %u (%p)\n",
837 GCT_iterate_all (get_all_tunnels_iterator, client);
838 reply.size = htons (sizeof (reply));
839 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
840 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
842 LOG (GNUNET_ERROR_TYPE_DEBUG,
843 "Get tunnels request from client %u completed\n", c->id);
844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
849 iter_connection (void *cls, struct CadetConnection *c)
851 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
852 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
854 h[msg->connections] = *(GCC_get_id (c));
859 iter_channel (void *cls, struct CadetChannel *ch)
861 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
862 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
863 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
865 chn[msg->channels] = GCCH_get_id (ch);
871 * Handler for client's SHOW_TUNNEL request.
873 * @param cls Closure (unused).
874 * @param client Identification of the client.
875 * @param message The actual message.
878 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
879 const struct GNUNET_MessageHeader *message)
881 const struct GNUNET_CADET_LocalInfo *msg;
882 struct GNUNET_CADET_LocalInfoTunnel *resp;
883 struct CadetClient *c;
884 struct CadetTunnel *t;
889 /* Sanity check for client registration */
890 if (NULL == (c = GML_client_get (client)))
893 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
897 msg = (struct GNUNET_CADET_LocalInfo *) message;
898 LOG (GNUNET_ERROR_TYPE_INFO,
899 "Received tunnel info request from client %u for tunnel %s\n",
900 c->id, GNUNET_i2s_full(&msg->peer));
902 t = GCP_get_tunnel (GCP_get (&msg->peer));
905 /* We don't know the tunnel */
906 struct GNUNET_CADET_LocalInfoTunnel warn;
908 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
909 GNUNET_i2s_full(&msg->peer), sizeof (warn));
910 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
911 warn.header.size = htons (sizeof (warn));
912 warn.destination = msg->peer;
913 warn.channels = htonl (0);
914 warn.connections = htonl (0);
915 warn.cstate = htons (0);
916 warn.estate = htons (0);
918 GNUNET_SERVER_notification_context_unicast (nc, client,
921 GNUNET_SERVER_receive_done (client, GNUNET_OK);
925 /* Initialize context */
926 ch_n = GCT_count_channels (t);
927 c_n = GCT_count_any_connections (t);
929 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
930 size += c_n * sizeof (struct GNUNET_CADET_Hash);
931 size += ch_n * sizeof (CADET_ChannelNumber);
933 resp = GNUNET_malloc (size);
934 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
935 resp->header.size = htons (size);
936 GCT_iterate_connections (t, &iter_connection, resp);
937 GCT_iterate_channels (t, &iter_channel, resp);
938 /* Do not interleave with iterators, iter_channel needs conn in HBO */
939 resp->destination = msg->peer;
940 resp->connections = htonl (resp->connections);
941 resp->channels = htonl (resp->channels);
942 resp->cstate = htons (GCT_get_cstate (t));
943 resp->estate = htons (GCT_get_estate (t));
944 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
945 &resp->header, GNUNET_NO);
948 LOG (GNUNET_ERROR_TYPE_INFO,
949 "Show tunnel request from client %u completed. %u conn, %u ch\n",
951 GNUNET_SERVER_receive_done (client, GNUNET_OK);
956 * Handler for client's INFO_DUMP request.
958 * @param cls Closure (unused).
959 * @param client Identification of the client.
960 * @param message The actual message.
963 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
964 const struct GNUNET_MessageHeader *message)
966 struct CadetClient *c;
968 /* Sanity check for client registration */
969 if (NULL == (c = GML_client_get (client)))
972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
976 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
979 LOG (GNUNET_ERROR_TYPE_ERROR,
980 "*************************** DUMP START ***************************\n");
982 GCP_iterate_all (&show_peer_iterator, NULL);
984 LOG (GNUNET_ERROR_TYPE_ERROR,
985 "**************************** DUMP END ****************************\n");
987 GNUNET_SERVER_receive_done (client, GNUNET_OK);
992 * Functions to handle messages from clients
994 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
995 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
996 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
997 sizeof (struct GNUNET_CADET_ChannelMessage)},
998 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
999 sizeof (struct GNUNET_CADET_ChannelMessage)},
1000 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1001 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1002 sizeof (struct GNUNET_CADET_LocalAck)},
1003 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1004 sizeof (struct GNUNET_MessageHeader)},
1005 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1006 sizeof (struct GNUNET_CADET_LocalInfo)},
1007 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1008 sizeof (struct GNUNET_MessageHeader)},
1009 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1010 sizeof (struct GNUNET_CADET_LocalInfo)},
1011 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1012 sizeof (struct GNUNET_MessageHeader)},
1018 /******************************************************************************/
1019 /******************************** API ***********************************/
1020 /******************************************************************************/
1023 * Initialize server subsystem.
1025 * @param handle Server handle.
1028 GML_init (struct GNUNET_SERVER_Handle *handle)
1030 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1031 server_handle = handle;
1032 GNUNET_SERVER_suspend (server_handle);
1033 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1038 * Install server (service) handlers and start listening to clients.
1043 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1044 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1045 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1047 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1049 clients_head = NULL;
1050 clients_tail = NULL;
1052 GNUNET_SERVER_resume (server_handle);
1064 GNUNET_SERVER_notification_context_destroy (nc);
1071 * Get a channel from a client.
1073 * @param c Client to check.
1074 * @param chid Channel ID, must be local (> 0x800...).
1076 * @return non-NULL if channel exists in the clients lists
1078 struct CadetChannel *
1079 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1081 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1083 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1085 GNUNET_break_op (0);
1086 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1090 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1091 map = c->incoming_channels;
1092 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1093 map = c->own_channels;
1102 LOG (GNUNET_ERROR_TYPE_DEBUG,
1103 "Client %s does no t have a valid map for CHID %X\n",
1107 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1112 * Add a channel to a client
1114 * @param client Client.
1115 * @param chid Channel ID.
1116 * @param ch Channel.
1119 GML_channel_add (struct CadetClient *client,
1121 struct CadetChannel *ch)
1123 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1124 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1125 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1126 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1127 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1128 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1135 * Remove a channel from a client.
1137 * @param client Client.
1138 * @param chid Channel ID.
1139 * @param ch Channel.
1142 GML_channel_remove (struct CadetClient *client,
1144 struct CadetChannel *ch)
1146 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1147 GNUNET_break (GNUNET_YES ==
1148 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1150 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1151 GNUNET_break (GNUNET_YES ==
1152 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1160 * Get the tunnel's next free local channel ID.
1164 * @return LID of a channel free to use.
1167 GML_get_next_chid (struct CadetClient *c)
1169 CADET_ChannelNumber chid;
1171 while (NULL != GML_channel_get (c, c->next_chid))
1173 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1174 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1176 chid = c->next_chid;
1177 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1184 * Check if client has registered with the service and has not disconnected
1186 * @param client the client to check
1188 * @return non-NULL if client exists in the global DLL
1190 struct CadetClient *
1191 GML_client_get (struct GNUNET_SERVER_Client *client)
1193 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1197 * Find a client that has opened a port
1199 * @param port Port to check.
1201 * @return non-NULL if a client has the port.
1203 struct CadetClient *
1204 GML_client_get_by_port (uint32_t port)
1206 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1211 * Deletes a channel from a client (either owner or destination).
1213 * @param c Client whose tunnel to delete.
1214 * @param ch Channel which should be deleted.
1215 * @param id Channel ID.
1218 GML_client_delete_channel (struct CadetClient *c,
1219 struct CadetChannel *ch,
1220 CADET_ChannelNumber id)
1224 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1226 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1228 if (GNUNET_YES != res)
1229 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1231 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1233 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1235 if (GNUNET_YES != res)
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1245 * Build a local ACK message and send it to a local client, if needed.
1247 * If the client was already allowed to send data, do nothing.
1249 * @param c Client to whom send the ACK.
1250 * @param id Channel ID to use
1253 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1255 struct GNUNET_CADET_LocalAck msg;
1257 LOG (GNUNET_ERROR_TYPE_DEBUG,
1258 "send local %s ack on %X towards %p\n",
1259 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1261 msg.header.size = htons (sizeof (msg));
1262 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1263 msg.channel_id = htonl (id);
1264 GNUNET_SERVER_notification_context_unicast (nc,
1274 * Notify the client that a new incoming channel was created.
1276 * @param c Client to notify.
1277 * @param id Channel ID.
1278 * @param port Channel's destination port.
1279 * @param opt Options (bit array).
1280 * @param peer Origin peer.
1283 GML_send_channel_create (struct CadetClient *c,
1284 uint32_t id, uint32_t port, uint32_t opt,
1285 const struct GNUNET_PeerIdentity *peer)
1287 struct GNUNET_CADET_ChannelMessage msg;
1289 msg.header.size = htons (sizeof (msg));
1290 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1291 msg.channel_id = htonl (id);
1292 msg.port = htonl (port);
1293 msg.opt = htonl (opt);
1295 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1296 &msg.header, GNUNET_NO);
1301 * Build a local channel NACK message and send it to a local client.
1303 * @param c Client to whom send the NACK.
1304 * @param id Channel ID to use
1307 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1309 struct GNUNET_CADET_LocalAck msg;
1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1312 "send local nack on %X towards %p\n",
1315 msg.header.size = htons (sizeof (msg));
1316 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1317 msg.channel_id = htonl (id);
1318 GNUNET_SERVER_notification_context_unicast (nc,
1326 * Notify a client that a channel is no longer valid.
1329 * @param id ID of the channel that is destroyed.
1332 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1334 struct GNUNET_CADET_ChannelMessage msg;
1341 if (GNUNET_YES == c->shutting_down)
1343 msg.header.size = htons (sizeof (msg));
1344 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1345 msg.channel_id = htonl (id);
1346 msg.port = htonl (0);
1347 memset (&msg.peer, 0, sizeof (msg.peer));
1348 msg.opt = htonl (0);
1349 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1350 &msg.header, GNUNET_NO);
1355 * Modify the cadet message ID from global to local and send to client.
1357 * @param c Client to send to.
1358 * @param msg Message to modify and send.
1359 * @param id Channel ID to use (c can be both owner and client).
1362 GML_send_data (struct CadetClient *c,
1363 const struct GNUNET_CADET_Data *msg,
1364 CADET_ChannelNumber id)
1366 struct GNUNET_CADET_LocalData *copy;
1367 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1368 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1370 if (size < sizeof (struct GNUNET_MessageHeader))
1372 GNUNET_break_op (0);
1380 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1381 memcpy (©[1], &msg[1], size);
1382 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1383 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1384 copy->id = htonl (id);
1385 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1386 ©->header, GNUNET_NO);
1391 * Get the static string to represent a client.
1395 * @return Static string for the client.
1398 GML_2s (const struct CadetClient *c)
1400 static char buf[32];
1402 SPRINTF (buf, "%u", c->id);