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);
318 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
319 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
324 p = (uint32_t *) &cc_msg[1];
325 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
326 for (i = 0; i < size; i++)
329 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
331 /* store in client's hashmap */
332 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
334 /* store in global hashmap */
335 /* FIXME only allow one client to have the port open,
336 * have a backup hashmap with waiting clients */
337 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
342 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
343 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
344 GNUNET_SERVER_notification_context_add (nc, client);
345 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
347 GNUNET_SERVER_receive_done (client, GNUNET_OK);
348 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
353 * Handler for requests of new tunnels
355 * @param cls Closure.
356 * @param client Identification of the client.
357 * @param message The actual message.
360 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
361 const struct GNUNET_MessageHeader *message)
363 struct CadetClient *c;
365 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
366 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
368 /* Sanity check for client registration */
369 if (NULL == (c = GML_client_get (client)))
372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
375 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
377 /* Message size sanity check */
378 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
386 GCCH_handle_local_create (c,
387 (struct GNUNET_CADET_ChannelMessage *) message))
389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
393 GNUNET_SERVER_receive_done (client, GNUNET_OK);
399 * Handler for requests of deleting tunnels
402 * @param client identification of the client
403 * @param message the actual message
406 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
407 const struct GNUNET_MessageHeader *message)
409 struct GNUNET_CADET_ChannelMessage *msg;
410 struct CadetClient *c;
411 struct CadetChannel *ch;
412 CADET_ChannelNumber chid;
414 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
416 /* Sanity check for client registration */
417 if (NULL == (c = GML_client_get (client)))
420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
423 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
425 /* Message sanity check */
426 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
433 msg = (struct GNUNET_CADET_ChannelMessage *) message;
435 /* Retrieve tunnel */
436 chid = ntohl (msg->channel_id);
437 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
438 ch = GML_channel_get (c, chid);
441 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
442 GNUNET_STATISTICS_update (stats,
443 "# client destroy messages on unknown channel",
445 GNUNET_SERVER_receive_done (client, GNUNET_OK);
449 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
451 GNUNET_SERVER_receive_done (client, GNUNET_OK);
457 * Handler for client traffic
460 * @param client identification of the client
461 * @param message the actual message
464 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
465 const struct GNUNET_MessageHeader *message)
467 struct GNUNET_CADET_LocalData *msg;
468 struct CadetClient *c;
469 struct CadetChannel *ch;
470 CADET_ChannelNumber chid;
474 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client!\n");
476 /* Sanity check for client registration */
477 if (NULL == (c = GML_client_get (client)))
480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
483 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
485 msg = (struct GNUNET_CADET_LocalData *) message;
487 /* Sanity check for message size */
488 size = ntohs (message->size) - sizeof (struct GNUNET_CADET_LocalData);
489 if (size < sizeof (struct GNUNET_MessageHeader))
492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
496 /* Channel exists? */
497 chid = ntohl (msg->id);
498 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
499 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
500 ch = GML_channel_get (c, chid);
503 GNUNET_STATISTICS_update (stats,
504 "# client data messages on unknown channel",
506 GNUNET_SERVER_receive_done (client, GNUNET_OK);
511 GCCH_handle_local_data (ch, c,
512 (struct GNUNET_MessageHeader *)&msg[1], fwd))
514 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
518 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
519 GNUNET_SERVER_receive_done (client, GNUNET_OK);
526 * Handler for client's ACKs for payload traffic.
528 * @param cls Closure (unused).
529 * @param client Identification of the client.
530 * @param message The actual message.
533 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
534 const struct GNUNET_MessageHeader *message)
536 struct GNUNET_CADET_LocalAck *msg;
537 struct CadetChannel *ch;
538 struct CadetClient *c;
539 CADET_ChannelNumber chid;
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
543 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
545 /* Sanity check for client registration */
546 if (NULL == (c = GML_client_get (client)))
549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
552 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
554 msg = (struct GNUNET_CADET_LocalAck *) message;
556 /* Channel exists? */
557 chid = ntohl (msg->channel_id);
558 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
559 ch = GML_channel_get (c, chid);
560 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
563 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
564 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
565 GNUNET_STATISTICS_update (stats,
566 "# client ack messages on unknown channel",
568 GNUNET_SERVER_receive_done (client, GNUNET_OK);
572 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
573 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
574 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
576 GCCH_handle_local_ack (ch, fwd);
577 GNUNET_SERVER_receive_done (client, GNUNET_OK);
584 * Iterator over all peers to send a monitoring client info about each peer.
586 * @param cls Closure ().
587 * @param peer Peer ID (tunnel remote peer).
588 * @param value Peer info.
590 * @return #GNUNET_YES, to keep iterating.
593 get_all_peers_iterator (void *cls,
594 const struct GNUNET_PeerIdentity * peer,
597 struct GNUNET_SERVER_Client *client = cls;
598 struct CadetPeer *p = value;
599 struct GNUNET_CADET_LocalInfoPeer msg;
601 msg.header.size = htons (sizeof (msg));
602 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
603 msg.destination = *peer;
604 msg.paths = htons (GCP_count_paths (p));
605 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
607 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
610 GNUNET_SERVER_notification_context_unicast (nc, client,
611 &msg.header, GNUNET_NO);
617 * Iterator over all peers to dump info for each peer.
619 * @param cls Closure (unused).
620 * @param peer Peer ID (tunnel remote peer).
621 * @param value Peer info.
623 * @return #GNUNET_YES, to keep iterating.
626 show_peer_iterator (void *cls,
627 const struct GNUNET_PeerIdentity * peer,
630 struct CadetPeer *p = value;
631 struct CadetTunnel *t;
633 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
635 t = GCP_get_tunnel (p);
637 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
639 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
646 * Handler for client's INFO PEERS request.
648 * @param cls Closure (unused).
649 * @param client Identification of the client.
650 * @param message The actual message.
653 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
654 const struct GNUNET_MessageHeader *message)
656 struct CadetClient *c;
657 struct GNUNET_MessageHeader reply;
659 /* Sanity check for client registration */
660 if (NULL == (c = GML_client_get (client)))
663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
667 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "Received get peers request from client %u (%p)\n",
671 GCP_iterate_all (get_all_peers_iterator, client);
672 reply.size = htons (sizeof (reply));
673 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
674 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
676 LOG (GNUNET_ERROR_TYPE_DEBUG,
677 "Get peers request from client %u completed\n", c->id);
678 GNUNET_SERVER_receive_done (client, GNUNET_OK);
683 * Handler for client's SHOW_PEER request.
685 * @param cls Closure (unused).
686 * @param client Identification of the client.
687 * @param message The actual message.
690 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
691 const struct GNUNET_MessageHeader *message)
693 const struct GNUNET_CADET_LocalInfo *msg;
694 struct GNUNET_CADET_LocalInfoPeer *resp;
696 struct CadetClient *c;
699 /* Sanity check for client registration */
700 if (NULL == (c = GML_client_get (client)))
703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
707 msg = (struct GNUNET_CADET_LocalInfo *) message;
708 LOG (GNUNET_ERROR_TYPE_INFO,
709 "Received peer info request from client %u for peer %s\n",
710 c->id, GNUNET_i2s_full (&msg->peer));
712 p = GCP_get (&msg->peer);
715 /* We don't know the peer */
716 struct GNUNET_CADET_LocalInfoPeer warn;
718 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown %u\n",
719 GNUNET_i2s_full (&msg->peer), sizeof (warn));
720 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
721 warn.header.size = htons (sizeof (warn));
722 warn.destination = msg->peer;
723 warn.paths = htons (0);
724 warn.tunnel = htons (NULL != GCP_get_tunnel (p));
726 GNUNET_SERVER_notification_context_unicast (nc, client,
729 GNUNET_SERVER_receive_done (client, GNUNET_OK);
733 size = sizeof (struct GNUNET_CADET_LocalInfoPeer);
734 // size += c_n * sizeof (struct GNUNET_CADET_Hash);
736 resp = GNUNET_malloc (size);
737 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
738 resp->header.size = htons (size);
739 resp->destination = msg->peer;
740 resp->paths = htons (0);
741 resp->tunnel = htons (0);
743 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
744 &resp->header, GNUNET_NO);
747 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
748 GNUNET_SERVER_receive_done (client, GNUNET_OK);
753 * Iterator over all tunnels to send a monitoring client info about each tunnel.
755 * @param cls Closure ().
756 * @param peer Peer ID (tunnel remote peer).
757 * @param value Tunnel info.
759 * @return #GNUNET_YES, to keep iterating.
762 get_all_tunnels_iterator (void *cls,
763 const struct GNUNET_PeerIdentity * peer,
766 struct GNUNET_SERVER_Client *client = cls;
767 struct CadetTunnel *t = value;
768 struct GNUNET_CADET_LocalInfoTunnel msg;
770 msg.header.size = htons (sizeof (msg));
771 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
772 msg.destination = *peer;
773 msg.channels = htonl (GCT_count_channels (t));
774 msg.connections = htonl (GCT_count_any_connections (t));
775 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
776 msg.estate = htons ((uint16_t) GCT_get_estate (t));
778 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
781 GNUNET_SERVER_notification_context_unicast (nc, client,
782 &msg.header, GNUNET_NO);
788 * Handler for client's INFO TUNNELS request.
790 * @param cls Closure (unused).
791 * @param client Identification of the client.
792 * @param message The actual message.
795 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
796 const struct GNUNET_MessageHeader *message)
798 struct CadetClient *c;
799 struct GNUNET_MessageHeader reply;
801 /* Sanity check for client registration */
802 if (NULL == (c = GML_client_get (client)))
805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
809 LOG (GNUNET_ERROR_TYPE_DEBUG,
810 "Received get tunnels request from client %u (%p)\n",
813 GCT_iterate_all (get_all_tunnels_iterator, client);
814 reply.size = htons (sizeof (reply));
815 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
816 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 "Get tunnels request from client %u completed\n", c->id);
820 GNUNET_SERVER_receive_done (client, GNUNET_OK);
825 iter_connection (void *cls, struct CadetConnection *c)
827 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
828 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
830 h[msg->connections] = *(GCC_get_id (c));
835 iter_channel (void *cls, struct CadetChannel *ch)
837 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
838 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
839 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
841 chn[msg->channels] = GCCH_get_id (ch);
847 * Handler for client's SHOW_TUNNEL request.
849 * @param cls Closure (unused).
850 * @param client Identification of the client.
851 * @param message The actual message.
854 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
855 const struct GNUNET_MessageHeader *message)
857 const struct GNUNET_CADET_LocalInfo *msg;
858 struct GNUNET_CADET_LocalInfoTunnel *resp;
859 struct CadetClient *c;
860 struct CadetTunnel *t;
865 /* Sanity check for client registration */
866 if (NULL == (c = GML_client_get (client)))
869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
873 msg = (struct GNUNET_CADET_LocalInfo *) message;
874 LOG (GNUNET_ERROR_TYPE_INFO,
875 "Received tunnel info request from client %u for tunnel %s\n",
876 c->id, GNUNET_i2s_full(&msg->peer));
878 t = GCP_get_tunnel (GCP_get (&msg->peer));
881 /* We don't know the tunnel */
882 struct GNUNET_CADET_LocalInfoTunnel warn;
884 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
885 GNUNET_i2s_full(&msg->peer), sizeof (warn));
886 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
887 warn.header.size = htons (sizeof (warn));
888 warn.destination = msg->peer;
889 warn.channels = htonl (0);
890 warn.connections = htonl (0);
891 warn.cstate = htons (0);
892 warn.estate = htons (0);
894 GNUNET_SERVER_notification_context_unicast (nc, client,
897 GNUNET_SERVER_receive_done (client, GNUNET_OK);
901 /* Initialize context */
902 ch_n = GCT_count_channels (t);
903 c_n = GCT_count_any_connections (t);
905 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
906 size += c_n * sizeof (struct GNUNET_CADET_Hash);
907 size += ch_n * sizeof (CADET_ChannelNumber);
909 resp = GNUNET_malloc (size);
910 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
911 resp->header.size = htons (size);
912 GCT_iterate_connections (t, &iter_connection, resp);
913 GCT_iterate_channels (t, &iter_channel, resp);
914 /* Do not interleave with iterators, iter_channel needs conn in HBO */
915 resp->destination = msg->peer;
916 resp->connections = htonl (resp->connections);
917 resp->channels = htonl (resp->channels);
918 resp->cstate = htons (GCT_get_cstate (t));
919 resp->estate = htons (GCT_get_estate (t));
920 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
921 &resp->header, GNUNET_NO);
924 LOG (GNUNET_ERROR_TYPE_INFO,
925 "Show tunnel request from client %u completed. %u conn, %u ch\n",
927 GNUNET_SERVER_receive_done (client, GNUNET_OK);
932 * Handler for client's INFO_DUMP request.
934 * @param cls Closure (unused).
935 * @param client Identification of the client.
936 * @param message The actual message.
939 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
940 const struct GNUNET_MessageHeader *message)
942 struct CadetClient *c;
944 /* Sanity check for client registration */
945 if (NULL == (c = GML_client_get (client)))
948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
952 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
955 LOG (GNUNET_ERROR_TYPE_ERROR,
956 "*************************** DUMP START ***************************\n");
958 GCP_iterate_all (&show_peer_iterator, NULL);
960 LOG (GNUNET_ERROR_TYPE_ERROR,
961 "**************************** DUMP END ****************************\n");
963 GNUNET_SERVER_receive_done (client, GNUNET_OK);
968 * Functions to handle messages from clients
970 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
971 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
972 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
973 sizeof (struct GNUNET_CADET_ChannelMessage)},
974 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
975 sizeof (struct GNUNET_CADET_ChannelMessage)},
976 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
977 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
978 sizeof (struct GNUNET_CADET_LocalAck)},
979 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
980 sizeof (struct GNUNET_MessageHeader)},
981 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
982 sizeof (struct GNUNET_CADET_LocalInfo)},
983 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
984 sizeof (struct GNUNET_MessageHeader)},
985 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
986 sizeof (struct GNUNET_CADET_LocalInfo)},
987 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
988 sizeof (struct GNUNET_MessageHeader)},
994 /******************************************************************************/
995 /******************************** API ***********************************/
996 /******************************************************************************/
999 * Initialize server subsystem.
1001 * @param handle Server handle.
1004 GML_init (struct GNUNET_SERVER_Handle *handle)
1006 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1007 server_handle = handle;
1008 GNUNET_SERVER_suspend (server_handle);
1009 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1014 * Install server (service) handlers and start listening to clients.
1019 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1020 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1021 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1023 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1025 clients_head = NULL;
1026 clients_tail = NULL;
1028 GNUNET_SERVER_resume (server_handle);
1040 GNUNET_SERVER_notification_context_destroy (nc);
1047 * Get a channel from a client.
1049 * @param c Client to check.
1050 * @param chid Channel ID, must be local (> 0x800...).
1052 * @return non-NULL if channel exists in the clients lists
1054 struct CadetChannel *
1055 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1057 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1059 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1061 GNUNET_break_op (0);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1066 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1067 map = c->incoming_channels;
1068 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1069 map = c->own_channels;
1078 LOG (GNUNET_ERROR_TYPE_DEBUG,
1079 "Client %s does no t have a valid map for CHID %X\n",
1083 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1088 * Add a channel to a client
1090 * @param client Client.
1091 * @param chid Channel ID.
1092 * @param ch Channel.
1095 GML_channel_add (struct CadetClient *client,
1097 struct CadetChannel *ch)
1099 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1100 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1101 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1102 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1103 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1104 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1111 * Remove a channel from a client.
1113 * @param client Client.
1114 * @param chid Channel ID.
1115 * @param ch Channel.
1118 GML_channel_remove (struct CadetClient *client,
1120 struct CadetChannel *ch)
1122 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1123 GNUNET_break (GNUNET_YES ==
1124 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1126 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1127 GNUNET_break (GNUNET_YES ==
1128 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1136 * Get the tunnel's next free local channel ID.
1140 * @return LID of a channel free to use.
1143 GML_get_next_chid (struct CadetClient *c)
1145 CADET_ChannelNumber chid;
1147 while (NULL != GML_channel_get (c, c->next_chid))
1149 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1150 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1152 chid = c->next_chid;
1153 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1160 * Check if client has registered with the service and has not disconnected
1162 * @param client the client to check
1164 * @return non-NULL if client exists in the global DLL
1166 struct CadetClient *
1167 GML_client_get (struct GNUNET_SERVER_Client *client)
1169 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1173 * Find a client that has opened a port
1175 * @param port Port to check.
1177 * @return non-NULL if a client has the port.
1179 struct CadetClient *
1180 GML_client_get_by_port (uint32_t port)
1182 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1187 * Deletes a channel from a client (either owner or destination).
1189 * @param c Client whose tunnel to delete.
1190 * @param ch Channel which should be deleted.
1191 * @param id Channel ID.
1194 GML_client_delete_channel (struct CadetClient *c,
1195 struct CadetChannel *ch,
1196 CADET_ChannelNumber id)
1200 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1202 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1204 if (GNUNET_YES != res)
1205 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1207 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1209 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1211 if (GNUNET_YES != res)
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1221 * Build a local ACK message and send it to a local client, if needed.
1223 * If the client was already allowed to send data, do nothing.
1225 * @param c Client to whom send the ACK.
1226 * @param id Channel ID to use
1229 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1231 struct GNUNET_CADET_LocalAck msg;
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "send local %s ack on %X towards %p\n",
1235 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1237 msg.header.size = htons (sizeof (msg));
1238 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1239 msg.channel_id = htonl (id);
1240 GNUNET_SERVER_notification_context_unicast (nc,
1250 * Notify the client that a new incoming channel was created.
1252 * @param c Client to notify.
1253 * @param id Channel ID.
1254 * @param port Channel's destination port.
1255 * @param opt Options (bit array).
1256 * @param peer Origin peer.
1259 GML_send_channel_create (struct CadetClient *c,
1260 uint32_t id, uint32_t port, uint32_t opt,
1261 const struct GNUNET_PeerIdentity *peer)
1263 struct GNUNET_CADET_ChannelMessage msg;
1265 msg.header.size = htons (sizeof (msg));
1266 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1267 msg.channel_id = htonl (id);
1268 msg.port = htonl (port);
1269 msg.opt = htonl (opt);
1271 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1272 &msg.header, GNUNET_NO);
1277 * Build a local channel NACK message and send it to a local client.
1279 * @param c Client to whom send the NACK.
1280 * @param id Channel ID to use
1283 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1285 struct GNUNET_CADET_LocalAck msg;
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "send local nack on %X towards %p\n",
1291 msg.header.size = htons (sizeof (msg));
1292 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1293 msg.channel_id = htonl (id);
1294 GNUNET_SERVER_notification_context_unicast (nc,
1302 * Notify a client that a channel is no longer valid.
1305 * @param id ID of the channel that is destroyed.
1308 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1310 struct GNUNET_CADET_ChannelMessage msg;
1317 if (GNUNET_YES == c->shutting_down)
1319 msg.header.size = htons (sizeof (msg));
1320 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1321 msg.channel_id = htonl (id);
1322 msg.port = htonl (0);
1323 memset (&msg.peer, 0, sizeof (msg.peer));
1324 msg.opt = htonl (0);
1325 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1326 &msg.header, GNUNET_NO);
1331 * Modify the cadet message ID from global to local and send to client.
1333 * @param c Client to send to.
1334 * @param msg Message to modify and send.
1335 * @param id Channel ID to use (c can be both owner and client).
1338 GML_send_data (struct CadetClient *c,
1339 const struct GNUNET_CADET_Data *msg,
1340 CADET_ChannelNumber id)
1342 struct GNUNET_CADET_LocalData *copy;
1343 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1344 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1346 if (size < sizeof (struct GNUNET_MessageHeader))
1348 GNUNET_break_op (0);
1356 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1357 memcpy (©[1], &msg[1], size);
1358 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1359 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1360 copy->id = htonl (id);
1361 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1362 ©->header, GNUNET_NO);
1367 * Get the static string to represent a client.
1371 * @return Static string for the client.
1374 GML_2s (const struct CadetClient *c)
1376 static char buf[32];
1378 sprintf (buf, "%u", c->id);