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 struct GNUNET_CADET_LocalData *msg;
475 struct CadetClient *c;
476 struct CadetChannel *ch;
477 CADET_ChannelNumber chid;
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client!\n");
483 /* Sanity check for client registration */
484 if (NULL == (c = GML_client_get (client)))
487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
490 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
492 msg = (struct GNUNET_CADET_LocalData *) message;
494 /* Sanity check for message size */
495 size = ntohs (message->size) - sizeof (struct GNUNET_CADET_LocalData);
496 if (sizeof (struct GNUNET_MessageHeader) > size)
499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
503 /* Channel exists? */
504 chid = ntohl (msg->id);
505 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
506 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
507 ch = GML_channel_get (c, chid);
510 GNUNET_STATISTICS_update (stats,
511 "# client data messages on unknown channel",
513 GNUNET_SERVER_receive_done (client, GNUNET_OK);
518 GCCH_handle_local_data (ch, c,
519 (struct GNUNET_MessageHeader *)&msg[1], fwd))
521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
525 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
526 GNUNET_SERVER_receive_done (client, GNUNET_OK);
533 * Handler for client's ACKs for payload traffic.
535 * @param cls Closure (unused).
536 * @param client Identification of the client.
537 * @param message The actual message.
540 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
541 const struct GNUNET_MessageHeader *message)
543 struct GNUNET_CADET_LocalAck *msg;
544 struct CadetChannel *ch;
545 struct CadetClient *c;
546 CADET_ChannelNumber chid;
549 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
550 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
552 /* Sanity check for client registration */
553 if (NULL == (c = GML_client_get (client)))
556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
559 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
561 msg = (struct GNUNET_CADET_LocalAck *) message;
563 /* Channel exists? */
564 chid = ntohl (msg->channel_id);
565 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
566 ch = GML_channel_get (c, chid);
567 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
570 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
571 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
572 GNUNET_STATISTICS_update (stats,
573 "# client ack messages on unknown channel",
575 GNUNET_SERVER_receive_done (client, GNUNET_OK);
579 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
580 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
581 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
583 GCCH_handle_local_ack (ch, fwd);
584 GNUNET_SERVER_receive_done (client, GNUNET_OK);
591 * Iterator over all peers to send a monitoring client info about each peer.
593 * @param cls Closure ().
594 * @param peer Peer ID (tunnel remote peer).
595 * @param value Peer info.
597 * @return #GNUNET_YES, to keep iterating.
600 get_all_peers_iterator (void *cls,
601 const struct GNUNET_PeerIdentity * peer,
604 struct GNUNET_SERVER_Client *client = cls;
605 struct CadetPeer *p = value;
606 struct GNUNET_CADET_LocalInfoPeer msg;
608 msg.header.size = htons (sizeof (msg));
609 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
610 msg.destination = *peer;
611 msg.paths = htons (GCP_count_paths (p));
612 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
614 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
617 GNUNET_SERVER_notification_context_unicast (nc, client,
618 &msg.header, GNUNET_NO);
624 * Iterator over all peers to dump info for each peer.
626 * @param cls Closure (unused).
627 * @param peer Peer ID (tunnel remote peer).
628 * @param value Peer info.
630 * @return #GNUNET_YES, to keep iterating.
633 show_peer_iterator (void *cls,
634 const struct GNUNET_PeerIdentity * peer,
637 struct CadetPeer *p = value;
638 struct CadetTunnel *t;
640 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
642 t = GCP_get_tunnel (p);
644 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
646 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
653 * Handler for client's INFO PEERS request.
655 * @param cls Closure (unused).
656 * @param client Identification of the client.
657 * @param message The actual message.
660 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
661 const struct GNUNET_MessageHeader *message)
663 struct CadetClient *c;
664 struct GNUNET_MessageHeader reply;
666 /* Sanity check for client registration */
667 if (NULL == (c = GML_client_get (client)))
670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
674 LOG (GNUNET_ERROR_TYPE_DEBUG,
675 "Received get peers request from client %u (%p)\n",
678 GCP_iterate_all (get_all_peers_iterator, client);
679 reply.size = htons (sizeof (reply));
680 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
681 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
683 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "Get peers request from client %u completed\n", c->id);
685 GNUNET_SERVER_receive_done (client, GNUNET_OK);
690 * Handler for client's SHOW_PEER request.
692 * @param cls Closure (unused).
693 * @param client Identification of the client.
694 * @param message The actual message.
697 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
698 const struct GNUNET_MessageHeader *message)
700 const struct GNUNET_CADET_LocalInfo *msg;
701 struct GNUNET_CADET_LocalInfoPeer *resp;
703 struct CadetClient *c;
706 /* Sanity check for client registration */
707 if (NULL == (c = GML_client_get (client)))
710 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
714 msg = (struct GNUNET_CADET_LocalInfo *) message;
715 LOG (GNUNET_ERROR_TYPE_INFO,
716 "Received peer info request from client %u for peer %s\n",
717 c->id, GNUNET_i2s_full (&msg->peer));
719 p = GCP_get (&msg->peer);
722 /* We don't know the peer */
723 struct GNUNET_CADET_LocalInfoPeer warn;
725 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown %u\n",
726 GNUNET_i2s_full (&msg->peer), sizeof (warn));
727 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
728 warn.header.size = htons (sizeof (warn));
729 warn.destination = msg->peer;
730 warn.paths = htons (0);
731 warn.tunnel = htons (NULL != GCP_get_tunnel (p));
733 GNUNET_SERVER_notification_context_unicast (nc, client,
736 GNUNET_SERVER_receive_done (client, GNUNET_OK);
740 size = sizeof (struct GNUNET_CADET_LocalInfoPeer);
741 // size += c_n * sizeof (struct GNUNET_CADET_Hash);
743 resp = GNUNET_malloc (size);
744 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
745 resp->header.size = htons (size);
746 resp->destination = msg->peer;
747 resp->paths = htons (0);
748 resp->tunnel = htons (0);
750 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
751 &resp->header, GNUNET_NO);
754 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
755 GNUNET_SERVER_receive_done (client, GNUNET_OK);
760 * Iterator over all tunnels to send a monitoring client info about each tunnel.
762 * @param cls Closure ().
763 * @param peer Peer ID (tunnel remote peer).
764 * @param value Tunnel info.
766 * @return #GNUNET_YES, to keep iterating.
769 get_all_tunnels_iterator (void *cls,
770 const struct GNUNET_PeerIdentity * peer,
773 struct GNUNET_SERVER_Client *client = cls;
774 struct CadetTunnel *t = value;
775 struct GNUNET_CADET_LocalInfoTunnel msg;
777 msg.header.size = htons (sizeof (msg));
778 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
779 msg.destination = *peer;
780 msg.channels = htonl (GCT_count_channels (t));
781 msg.connections = htonl (GCT_count_any_connections (t));
782 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
783 msg.estate = htons ((uint16_t) GCT_get_estate (t));
785 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
788 GNUNET_SERVER_notification_context_unicast (nc, client,
789 &msg.header, GNUNET_NO);
795 * Handler for client's INFO TUNNELS request.
797 * @param cls Closure (unused).
798 * @param client Identification of the client.
799 * @param message The actual message.
802 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
803 const struct GNUNET_MessageHeader *message)
805 struct CadetClient *c;
806 struct GNUNET_MessageHeader reply;
808 /* Sanity check for client registration */
809 if (NULL == (c = GML_client_get (client)))
812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
816 LOG (GNUNET_ERROR_TYPE_DEBUG,
817 "Received get tunnels request from client %u (%p)\n",
820 GCT_iterate_all (get_all_tunnels_iterator, client);
821 reply.size = htons (sizeof (reply));
822 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
823 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Get tunnels request from client %u completed\n", c->id);
827 GNUNET_SERVER_receive_done (client, GNUNET_OK);
832 iter_connection (void *cls, struct CadetConnection *c)
834 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
835 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
837 h[msg->connections] = *(GCC_get_id (c));
842 iter_channel (void *cls, struct CadetChannel *ch)
844 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
845 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
846 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
848 chn[msg->channels] = GCCH_get_id (ch);
854 * Handler for client's SHOW_TUNNEL request.
856 * @param cls Closure (unused).
857 * @param client Identification of the client.
858 * @param message The actual message.
861 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
862 const struct GNUNET_MessageHeader *message)
864 const struct GNUNET_CADET_LocalInfo *msg;
865 struct GNUNET_CADET_LocalInfoTunnel *resp;
866 struct CadetClient *c;
867 struct CadetTunnel *t;
872 /* Sanity check for client registration */
873 if (NULL == (c = GML_client_get (client)))
876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
880 msg = (struct GNUNET_CADET_LocalInfo *) message;
881 LOG (GNUNET_ERROR_TYPE_INFO,
882 "Received tunnel info request from client %u for tunnel %s\n",
883 c->id, GNUNET_i2s_full(&msg->peer));
885 t = GCP_get_tunnel (GCP_get (&msg->peer));
888 /* We don't know the tunnel */
889 struct GNUNET_CADET_LocalInfoTunnel warn;
891 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
892 GNUNET_i2s_full(&msg->peer), sizeof (warn));
893 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
894 warn.header.size = htons (sizeof (warn));
895 warn.destination = msg->peer;
896 warn.channels = htonl (0);
897 warn.connections = htonl (0);
898 warn.cstate = htons (0);
899 warn.estate = htons (0);
901 GNUNET_SERVER_notification_context_unicast (nc, client,
904 GNUNET_SERVER_receive_done (client, GNUNET_OK);
908 /* Initialize context */
909 ch_n = GCT_count_channels (t);
910 c_n = GCT_count_any_connections (t);
912 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
913 size += c_n * sizeof (struct GNUNET_CADET_Hash);
914 size += ch_n * sizeof (CADET_ChannelNumber);
916 resp = GNUNET_malloc (size);
917 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
918 resp->header.size = htons (size);
919 GCT_iterate_connections (t, &iter_connection, resp);
920 GCT_iterate_channels (t, &iter_channel, resp);
921 /* Do not interleave with iterators, iter_channel needs conn in HBO */
922 resp->destination = msg->peer;
923 resp->connections = htonl (resp->connections);
924 resp->channels = htonl (resp->channels);
925 resp->cstate = htons (GCT_get_cstate (t));
926 resp->estate = htons (GCT_get_estate (t));
927 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
928 &resp->header, GNUNET_NO);
931 LOG (GNUNET_ERROR_TYPE_INFO,
932 "Show tunnel request from client %u completed. %u conn, %u ch\n",
934 GNUNET_SERVER_receive_done (client, GNUNET_OK);
939 * Handler for client's INFO_DUMP request.
941 * @param cls Closure (unused).
942 * @param client Identification of the client.
943 * @param message The actual message.
946 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
947 const struct GNUNET_MessageHeader *message)
949 struct CadetClient *c;
951 /* Sanity check for client registration */
952 if (NULL == (c = GML_client_get (client)))
955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
959 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
962 LOG (GNUNET_ERROR_TYPE_ERROR,
963 "*************************** DUMP START ***************************\n");
965 GCP_iterate_all (&show_peer_iterator, NULL);
967 LOG (GNUNET_ERROR_TYPE_ERROR,
968 "**************************** DUMP END ****************************\n");
970 GNUNET_SERVER_receive_done (client, GNUNET_OK);
975 * Functions to handle messages from clients
977 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
978 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
979 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
980 sizeof (struct GNUNET_CADET_ChannelMessage)},
981 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
982 sizeof (struct GNUNET_CADET_ChannelMessage)},
983 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
984 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
985 sizeof (struct GNUNET_CADET_LocalAck)},
986 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
987 sizeof (struct GNUNET_MessageHeader)},
988 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
989 sizeof (struct GNUNET_CADET_LocalInfo)},
990 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
991 sizeof (struct GNUNET_MessageHeader)},
992 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
993 sizeof (struct GNUNET_CADET_LocalInfo)},
994 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
995 sizeof (struct GNUNET_MessageHeader)},
1001 /******************************************************************************/
1002 /******************************** API ***********************************/
1003 /******************************************************************************/
1006 * Initialize server subsystem.
1008 * @param handle Server handle.
1011 GML_init (struct GNUNET_SERVER_Handle *handle)
1013 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1014 server_handle = handle;
1015 GNUNET_SERVER_suspend (server_handle);
1016 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1021 * Install server (service) handlers and start listening to clients.
1026 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1027 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1028 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1030 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1032 clients_head = NULL;
1033 clients_tail = NULL;
1035 GNUNET_SERVER_resume (server_handle);
1047 GNUNET_SERVER_notification_context_destroy (nc);
1054 * Get a channel from a client.
1056 * @param c Client to check.
1057 * @param chid Channel ID, must be local (> 0x800...).
1059 * @return non-NULL if channel exists in the clients lists
1061 struct CadetChannel *
1062 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1064 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1066 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1068 GNUNET_break_op (0);
1069 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1073 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1074 map = c->incoming_channels;
1075 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1076 map = c->own_channels;
1085 LOG (GNUNET_ERROR_TYPE_DEBUG,
1086 "Client %s does no t have a valid map for CHID %X\n",
1090 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1095 * Add a channel to a client
1097 * @param client Client.
1098 * @param chid Channel ID.
1099 * @param ch Channel.
1102 GML_channel_add (struct CadetClient *client,
1104 struct CadetChannel *ch)
1106 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1107 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1108 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1109 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1110 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1111 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1118 * Remove a channel from a client.
1120 * @param client Client.
1121 * @param chid Channel ID.
1122 * @param ch Channel.
1125 GML_channel_remove (struct CadetClient *client,
1127 struct CadetChannel *ch)
1129 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1130 GNUNET_break (GNUNET_YES ==
1131 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1133 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1134 GNUNET_break (GNUNET_YES ==
1135 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1143 * Get the tunnel's next free local channel ID.
1147 * @return LID of a channel free to use.
1150 GML_get_next_chid (struct CadetClient *c)
1152 CADET_ChannelNumber chid;
1154 while (NULL != GML_channel_get (c, c->next_chid))
1156 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1157 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1159 chid = c->next_chid;
1160 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1167 * Check if client has registered with the service and has not disconnected
1169 * @param client the client to check
1171 * @return non-NULL if client exists in the global DLL
1173 struct CadetClient *
1174 GML_client_get (struct GNUNET_SERVER_Client *client)
1176 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1180 * Find a client that has opened a port
1182 * @param port Port to check.
1184 * @return non-NULL if a client has the port.
1186 struct CadetClient *
1187 GML_client_get_by_port (uint32_t port)
1189 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1194 * Deletes a channel from a client (either owner or destination).
1196 * @param c Client whose tunnel to delete.
1197 * @param ch Channel which should be deleted.
1198 * @param id Channel ID.
1201 GML_client_delete_channel (struct CadetClient *c,
1202 struct CadetChannel *ch,
1203 CADET_ChannelNumber id)
1207 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1209 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1211 if (GNUNET_YES != res)
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1214 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1216 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1218 if (GNUNET_YES != res)
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1228 * Build a local ACK message and send it to a local client, if needed.
1230 * If the client was already allowed to send data, do nothing.
1232 * @param c Client to whom send the ACK.
1233 * @param id Channel ID to use
1236 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1238 struct GNUNET_CADET_LocalAck msg;
1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1241 "send local %s ack on %X towards %p\n",
1242 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1244 msg.header.size = htons (sizeof (msg));
1245 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1246 msg.channel_id = htonl (id);
1247 GNUNET_SERVER_notification_context_unicast (nc,
1257 * Notify the client that a new incoming channel was created.
1259 * @param c Client to notify.
1260 * @param id Channel ID.
1261 * @param port Channel's destination port.
1262 * @param opt Options (bit array).
1263 * @param peer Origin peer.
1266 GML_send_channel_create (struct CadetClient *c,
1267 uint32_t id, uint32_t port, uint32_t opt,
1268 const struct GNUNET_PeerIdentity *peer)
1270 struct GNUNET_CADET_ChannelMessage msg;
1272 msg.header.size = htons (sizeof (msg));
1273 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1274 msg.channel_id = htonl (id);
1275 msg.port = htonl (port);
1276 msg.opt = htonl (opt);
1278 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1279 &msg.header, GNUNET_NO);
1284 * Build a local channel NACK message and send it to a local client.
1286 * @param c Client to whom send the NACK.
1287 * @param id Channel ID to use
1290 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1292 struct GNUNET_CADET_LocalAck msg;
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "send local nack on %X towards %p\n",
1298 msg.header.size = htons (sizeof (msg));
1299 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1300 msg.channel_id = htonl (id);
1301 GNUNET_SERVER_notification_context_unicast (nc,
1309 * Notify a client that a channel is no longer valid.
1312 * @param id ID of the channel that is destroyed.
1315 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1317 struct GNUNET_CADET_ChannelMessage msg;
1324 if (GNUNET_YES == c->shutting_down)
1326 msg.header.size = htons (sizeof (msg));
1327 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1328 msg.channel_id = htonl (id);
1329 msg.port = htonl (0);
1330 memset (&msg.peer, 0, sizeof (msg.peer));
1331 msg.opt = htonl (0);
1332 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1333 &msg.header, GNUNET_NO);
1338 * Modify the cadet message ID from global to local and send to client.
1340 * @param c Client to send to.
1341 * @param msg Message to modify and send.
1342 * @param id Channel ID to use (c can be both owner and client).
1345 GML_send_data (struct CadetClient *c,
1346 const struct GNUNET_CADET_Data *msg,
1347 CADET_ChannelNumber id)
1349 struct GNUNET_CADET_LocalData *copy;
1350 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1351 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1353 if (size < sizeof (struct GNUNET_MessageHeader))
1355 GNUNET_break_op (0);
1363 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1364 memcpy (©[1], &msg[1], size);
1365 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1366 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1367 copy->id = htonl (id);
1368 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1369 ©->header, GNUNET_NO);
1374 * Get the static string to represent a client.
1378 * @return Static string for the client.
1381 GML_2s (const struct CadetClient *c)
1383 static char buf[32];
1385 SPRINTF (buf, "%u", c->id);