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., 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);
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 * Handler for client's INFO PEERS request.
677 * @param cls Closure (unused).
678 * @param client Identification of the client.
679 * @param message The actual message.
682 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
683 const struct GNUNET_MessageHeader *message)
685 struct CadetClient *c;
686 struct GNUNET_MessageHeader reply;
688 /* Sanity check for client registration */
689 if (NULL == (c = GML_client_get (client)))
692 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
696 LOG (GNUNET_ERROR_TYPE_DEBUG,
697 "Received get peers request from client %u (%p)\n",
700 GCP_iterate_all (get_all_peers_iterator, client);
701 reply.size = htons (sizeof (reply));
702 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
703 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
705 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 "Get peers request from client %u completed\n", c->id);
707 GNUNET_SERVER_receive_done (client, GNUNET_OK);
712 * Handler for client's SHOW_PEER request.
714 * @param cls Closure (unused).
715 * @param client Identification of the client.
716 * @param message The actual message.
719 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
720 const struct GNUNET_MessageHeader *message)
722 const struct GNUNET_CADET_LocalInfo *msg;
723 struct GNUNET_CADET_LocalInfoPeer *resp;
725 struct CadetClient *c;
728 /* Sanity check for client registration */
729 if (NULL == (c = GML_client_get (client)))
732 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
736 msg = (struct GNUNET_CADET_LocalInfo *) message;
737 LOG (GNUNET_ERROR_TYPE_INFO,
738 "Received peer info request from client %u for peer %s\n",
739 c->id, GNUNET_i2s_full (&msg->peer));
741 p = GCP_get (&msg->peer);
744 /* We don't know the peer */
745 struct GNUNET_CADET_LocalInfoPeer warn;
747 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown %u\n",
748 GNUNET_i2s_full (&msg->peer), sizeof (warn));
749 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
750 warn.header.size = htons (sizeof (warn));
751 warn.destination = msg->peer;
752 warn.paths = htons (0);
753 warn.tunnel = htons (NULL != GCP_get_tunnel (p));
755 GNUNET_SERVER_notification_context_unicast (nc, client,
758 GNUNET_SERVER_receive_done (client, GNUNET_OK);
762 size = sizeof (struct GNUNET_CADET_LocalInfoPeer);
763 // size += c_n * sizeof (struct GNUNET_CADET_Hash);
765 resp = GNUNET_malloc (size);
766 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
767 resp->header.size = htons (size);
768 resp->destination = msg->peer;
769 resp->paths = htons (0);
770 resp->tunnel = htons (0);
772 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
773 &resp->header, GNUNET_NO);
776 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
777 GNUNET_SERVER_receive_done (client, GNUNET_OK);
782 * Iterator over all tunnels to send a monitoring client info about each tunnel.
784 * @param cls Closure ().
785 * @param peer Peer ID (tunnel remote peer).
786 * @param value Tunnel info.
788 * @return #GNUNET_YES, to keep iterating.
791 get_all_tunnels_iterator (void *cls,
792 const struct GNUNET_PeerIdentity * peer,
795 struct GNUNET_SERVER_Client *client = cls;
796 struct CadetTunnel *t = value;
797 struct GNUNET_CADET_LocalInfoTunnel msg;
799 msg.header.size = htons (sizeof (msg));
800 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
801 msg.destination = *peer;
802 msg.channels = htonl (GCT_count_channels (t));
803 msg.connections = htonl (GCT_count_any_connections (t));
804 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
805 msg.estate = htons ((uint16_t) GCT_get_estate (t));
807 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
810 GNUNET_SERVER_notification_context_unicast (nc, client,
811 &msg.header, GNUNET_NO);
817 * Handler for client's INFO TUNNELS request.
819 * @param cls Closure (unused).
820 * @param client Identification of the client.
821 * @param message The actual message.
824 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
825 const struct GNUNET_MessageHeader *message)
827 struct CadetClient *c;
828 struct GNUNET_MessageHeader reply;
830 /* Sanity check for client registration */
831 if (NULL == (c = GML_client_get (client)))
834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
838 LOG (GNUNET_ERROR_TYPE_DEBUG,
839 "Received get tunnels request from client %u (%p)\n",
842 GCT_iterate_all (get_all_tunnels_iterator, client);
843 reply.size = htons (sizeof (reply));
844 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
845 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
847 LOG (GNUNET_ERROR_TYPE_DEBUG,
848 "Get tunnels request from client %u completed\n", c->id);
849 GNUNET_SERVER_receive_done (client, GNUNET_OK);
854 iter_connection (void *cls, struct CadetConnection *c)
856 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
857 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
859 h[msg->connections] = *(GCC_get_id (c));
864 iter_channel (void *cls, struct CadetChannel *ch)
866 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
867 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
868 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
870 chn[msg->channels] = GCCH_get_id (ch);
876 * Handler for client's SHOW_TUNNEL request.
878 * @param cls Closure (unused).
879 * @param client Identification of the client.
880 * @param message The actual message.
883 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
884 const struct GNUNET_MessageHeader *message)
886 const struct GNUNET_CADET_LocalInfo *msg;
887 struct GNUNET_CADET_LocalInfoTunnel *resp;
888 struct CadetClient *c;
889 struct CadetTunnel *t;
894 /* Sanity check for client registration */
895 if (NULL == (c = GML_client_get (client)))
898 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
902 msg = (struct GNUNET_CADET_LocalInfo *) message;
903 LOG (GNUNET_ERROR_TYPE_INFO,
904 "Received tunnel info request from client %u for tunnel %s\n",
905 c->id, GNUNET_i2s_full(&msg->peer));
907 t = GCP_get_tunnel (GCP_get (&msg->peer));
910 /* We don't know the tunnel */
911 struct GNUNET_CADET_LocalInfoTunnel warn;
913 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
914 GNUNET_i2s_full(&msg->peer), sizeof (warn));
915 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
916 warn.header.size = htons (sizeof (warn));
917 warn.destination = msg->peer;
918 warn.channels = htonl (0);
919 warn.connections = htonl (0);
920 warn.cstate = htons (0);
921 warn.estate = htons (0);
923 GNUNET_SERVER_notification_context_unicast (nc, client,
926 GNUNET_SERVER_receive_done (client, GNUNET_OK);
930 /* Initialize context */
931 ch_n = GCT_count_channels (t);
932 c_n = GCT_count_any_connections (t);
934 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
935 size += c_n * sizeof (struct GNUNET_CADET_Hash);
936 size += ch_n * sizeof (CADET_ChannelNumber);
938 resp = GNUNET_malloc (size);
939 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
940 resp->header.size = htons (size);
941 GCT_iterate_connections (t, &iter_connection, resp);
942 GCT_iterate_channels (t, &iter_channel, resp);
943 /* Do not interleave with iterators, iter_channel needs conn in HBO */
944 resp->destination = msg->peer;
945 resp->connections = htonl (resp->connections);
946 resp->channels = htonl (resp->channels);
947 resp->cstate = htons (GCT_get_cstate (t));
948 resp->estate = htons (GCT_get_estate (t));
949 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
950 &resp->header, GNUNET_NO);
953 LOG (GNUNET_ERROR_TYPE_INFO,
954 "Show tunnel request from client %u completed. %u conn, %u ch\n",
956 GNUNET_SERVER_receive_done (client, GNUNET_OK);
961 * Handler for client's INFO_DUMP request.
963 * @param cls Closure (unused).
964 * @param client Identification of the client.
965 * @param message The actual message.
968 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
969 const struct GNUNET_MessageHeader *message)
971 struct CadetClient *c;
973 /* Sanity check for client registration */
974 if (NULL == (c = GML_client_get (client)))
977 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
981 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
984 LOG (GNUNET_ERROR_TYPE_ERROR,
985 "*************************** DUMP START ***************************\n");
987 GCP_iterate_all (&show_peer_iterator, NULL);
989 LOG (GNUNET_ERROR_TYPE_ERROR,
990 "**************************** DUMP END ****************************\n");
992 GNUNET_SERVER_receive_done (client, GNUNET_OK);
997 * Functions to handle messages from clients
999 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1000 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1001 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1002 sizeof (struct GNUNET_CADET_ChannelMessage)},
1003 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1004 sizeof (struct GNUNET_CADET_ChannelMessage)},
1005 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1006 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1007 sizeof (struct GNUNET_CADET_LocalAck)},
1008 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1009 sizeof (struct GNUNET_MessageHeader)},
1010 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1011 sizeof (struct GNUNET_CADET_LocalInfo)},
1012 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1013 sizeof (struct GNUNET_MessageHeader)},
1014 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1015 sizeof (struct GNUNET_CADET_LocalInfo)},
1016 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1017 sizeof (struct GNUNET_MessageHeader)},
1023 /******************************************************************************/
1024 /******************************** API ***********************************/
1025 /******************************************************************************/
1028 * Initialize server subsystem.
1030 * @param handle Server handle.
1033 GML_init (struct GNUNET_SERVER_Handle *handle)
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1036 server_handle = handle;
1037 GNUNET_SERVER_suspend (server_handle);
1038 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1043 * Install server (service) handlers and start listening to clients.
1048 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1049 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1050 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1052 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1054 clients_head = NULL;
1055 clients_tail = NULL;
1057 GNUNET_SERVER_resume (server_handle);
1069 GNUNET_SERVER_notification_context_destroy (nc);
1076 * Get a channel from a client.
1078 * @param c Client to check.
1079 * @param chid Channel ID, must be local (> 0x800...).
1081 * @return non-NULL if channel exists in the clients lists
1083 struct CadetChannel *
1084 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1086 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1088 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1090 GNUNET_break_op (0);
1091 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1095 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1096 map = c->incoming_channels;
1097 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1098 map = c->own_channels;
1107 LOG (GNUNET_ERROR_TYPE_DEBUG,
1108 "Client %s does no t have a valid map for CHID %X\n",
1112 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1117 * Add a channel to a client
1119 * @param client Client.
1120 * @param chid Channel ID.
1121 * @param ch Channel.
1124 GML_channel_add (struct CadetClient *client,
1126 struct CadetChannel *ch)
1128 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1129 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1130 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1131 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1132 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1133 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1140 * Remove a channel from a client.
1142 * @param client Client.
1143 * @param chid Channel ID.
1144 * @param ch Channel.
1147 GML_channel_remove (struct CadetClient *client,
1149 struct CadetChannel *ch)
1151 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1152 GNUNET_break (GNUNET_YES ==
1153 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1155 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1156 GNUNET_break (GNUNET_YES ==
1157 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1165 * Get the tunnel's next free local channel ID.
1169 * @return LID of a channel free to use.
1172 GML_get_next_chid (struct CadetClient *c)
1174 CADET_ChannelNumber chid;
1176 while (NULL != GML_channel_get (c, c->next_chid))
1178 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1179 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1181 chid = c->next_chid;
1182 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1189 * Check if client has registered with the service and has not disconnected
1191 * @param client the client to check
1193 * @return non-NULL if client exists in the global DLL
1195 struct CadetClient *
1196 GML_client_get (struct GNUNET_SERVER_Client *client)
1198 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1202 * Find a client that has opened a port
1204 * @param port Port to check.
1206 * @return non-NULL if a client has the port.
1208 struct CadetClient *
1209 GML_client_get_by_port (uint32_t port)
1211 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1216 * Deletes a channel from a client (either owner or destination).
1218 * @param c Client whose tunnel to delete.
1219 * @param ch Channel which should be deleted.
1220 * @param id Channel ID.
1223 GML_client_delete_channel (struct CadetClient *c,
1224 struct CadetChannel *ch,
1225 CADET_ChannelNumber id)
1229 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1231 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1233 if (GNUNET_YES != res)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1236 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1238 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1240 if (GNUNET_YES != res)
1241 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1250 * Build a local ACK message and send it to a local client, if needed.
1252 * If the client was already allowed to send data, do nothing.
1254 * @param c Client to whom send the ACK.
1255 * @param id Channel ID to use
1258 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1260 struct GNUNET_CADET_LocalAck msg;
1262 LOG (GNUNET_ERROR_TYPE_DEBUG,
1263 "send local %s ack on %X towards %p\n",
1264 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1266 msg.header.size = htons (sizeof (msg));
1267 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1268 msg.channel_id = htonl (id);
1269 GNUNET_SERVER_notification_context_unicast (nc,
1279 * Notify the client that a new incoming channel was created.
1281 * @param c Client to notify.
1282 * @param id Channel ID.
1283 * @param port Channel's destination port.
1284 * @param opt Options (bit array).
1285 * @param peer Origin peer.
1288 GML_send_channel_create (struct CadetClient *c,
1289 uint32_t id, uint32_t port, uint32_t opt,
1290 const struct GNUNET_PeerIdentity *peer)
1292 struct GNUNET_CADET_ChannelMessage msg;
1294 msg.header.size = htons (sizeof (msg));
1295 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1296 msg.channel_id = htonl (id);
1297 msg.port = htonl (port);
1298 msg.opt = htonl (opt);
1300 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1301 &msg.header, GNUNET_NO);
1306 * Build a local channel NACK message and send it to a local client.
1308 * @param c Client to whom send the NACK.
1309 * @param id Channel ID to use
1312 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1314 struct GNUNET_CADET_LocalAck msg;
1316 LOG (GNUNET_ERROR_TYPE_DEBUG,
1317 "send local nack on %X towards %p\n",
1320 msg.header.size = htons (sizeof (msg));
1321 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1322 msg.channel_id = htonl (id);
1323 GNUNET_SERVER_notification_context_unicast (nc,
1331 * Notify a client that a channel is no longer valid.
1334 * @param id ID of the channel that is destroyed.
1337 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1339 struct GNUNET_CADET_ChannelMessage msg;
1346 if (GNUNET_YES == c->shutting_down)
1348 msg.header.size = htons (sizeof (msg));
1349 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1350 msg.channel_id = htonl (id);
1351 msg.port = htonl (0);
1352 memset (&msg.peer, 0, sizeof (msg.peer));
1353 msg.opt = htonl (0);
1354 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1355 &msg.header, GNUNET_NO);
1360 * Modify the cadet message ID from global to local and send to client.
1362 * @param c Client to send to.
1363 * @param msg Message to modify and send.
1364 * @param id Channel ID to use (c can be both owner and client).
1367 GML_send_data (struct CadetClient *c,
1368 const struct GNUNET_CADET_Data *msg,
1369 CADET_ChannelNumber id)
1371 struct GNUNET_CADET_LocalData *copy;
1372 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1373 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1375 if (size < sizeof (struct GNUNET_MessageHeader))
1377 GNUNET_break_op (0);
1385 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1386 memcpy (©[1], &msg[1], size);
1387 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1388 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1389 copy->id = htonl (id);
1390 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1391 ©->header, GNUNET_NO);
1396 * Get the static string to represent a client.
1400 * @return Static string for the client.
1403 GML_2s (const struct CadetClient *c)
1405 static char buf[32];
1407 SPRINTF (buf, "%u", c->id);