2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
28 #include "cadet_protocol.h" /* GNUNET_CADET_Data is shared */
30 #include "gnunet-service-cadet_local.h"
31 #include "gnunet-service-cadet_channel.h"
34 #include "gnunet-service-cadet_tunnel.h"
35 #include "gnunet-service-cadet_peer.h"
37 #define LOG(level, ...) GNUNET_log_from(level,"cadet-loc",__VA_ARGS__)
39 /******************************************************************************/
40 /******************************** STRUCTS **********************************/
41 /******************************************************************************/
44 * Struct containing information about a client of the service
46 * TODO: add a list of 'waiting' ports
53 struct CadetClient *next;
58 struct CadetClient *prev;
61 * Tunnels that belong to this client, indexed by local id
63 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
66 * Tunnels this client has accepted, indexed by incoming local id
68 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
71 * Channel ID for the next incoming channel.
73 CADET_ChannelNumber next_chid;
76 * Handle to communicate with the client
78 struct GNUNET_SERVER_Client *handle;
81 * Ports that this client has declared interest in.
82 * Indexed by port, contains *Client.
84 struct GNUNET_CONTAINER_MultiHashMap *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_MultiHashMap *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,
153 const struct GNUNET_HashCode *key,
158 res = GNUNET_CONTAINER_multihashmap_remove (ports, key, value);
159 if (GNUNET_YES != res)
162 LOG (GNUNET_ERROR_TYPE_WARNING,
163 "Port %s by client %p was not registered.\n",
164 GNUNET_h2s (key), value);
171 * Iterator for deleting each channel whose client endpoint disconnected.
173 * @param cls Closure (client that has disconnected).
174 * @param key The local channel id (used to access the hashmap).
175 * @param value The value stored at the key (channel to destroy).
177 * @return GNUNET_OK, keep iterating.
180 channel_destroy_iterator (void *cls,
184 struct CadetChannel *ch = value;
185 struct CadetClient *c = cls;
187 LOG (GNUNET_ERROR_TYPE_DEBUG,
188 " Channel %s destroy, due to client %s shutdown.\n",
189 GCCH_2s (ch), GML_2s (c));
191 GCCH_handle_local_destroy (ch, c, key < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
197 * Unregister data and free memory for a client.
199 * @param c Client to destroy. No longer valid after call.
202 client_destroy (struct CadetClient *c)
204 LOG (GNUNET_ERROR_TYPE_DEBUG, " client destroy: %p/%u\n", c, c->id);
205 GNUNET_SERVER_client_drop (c->handle);
206 c->shutting_down = GNUNET_YES;
208 if (NULL != c->own_channels)
210 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
211 &channel_destroy_iterator, c);
212 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
214 if (NULL != c->incoming_channels)
216 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
217 &channel_destroy_iterator, c);
218 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
220 if (NULL != c->ports)
222 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
223 &client_release_ports, c);
224 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
227 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
228 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
229 GNUNET_SERVER_client_set_user_context (c->handle, NULL);
234 * Create a client record, register data and initialize memory.
236 * @param client Client's handle.
238 static struct CadetClient *
239 client_new (struct GNUNET_SERVER_Client *client)
241 struct CadetClient *c;
243 GNUNET_SERVER_client_keep (client);
244 GNUNET_SERVER_notification_context_add (nc, client);
246 c = GNUNET_new (struct CadetClient);
248 c->id = next_client_id++; /* overflow not important: just for debug */
249 c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
251 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
252 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
254 GNUNET_SERVER_client_set_user_context (client, c);
255 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
256 GNUNET_STATISTICS_update (stats, "# clients", +1, GNUNET_NO);
258 LOG (GNUNET_ERROR_TYPE_DEBUG, " client created: %p/%u\n", c, c->id);
264 /******************************************************************************/
265 /******************************** HANDLES ***********************************/
266 /******************************************************************************/
269 * Handler for client connection.
271 * @param cls Closure (unused).
272 * @param client Client handler.
275 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
277 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client connected: %p\n", client);
281 (void) client_new (client);
286 * Handler for client disconnection
289 * @param client identification of the client; NULL
290 * for the last call when the server is destroyed
293 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
295 struct CadetClient *c;
297 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected: %p\n", client);
299 c = GML_client_get (client);
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
308 LOG (GNUNET_ERROR_TYPE_DEBUG, " disconnecting client's context NULL\n");
315 * Handler for port open requests.
317 * @param cls Closure.
318 * @param client Identification of the client.
319 * @param message The actual message.
322 handle_port_open (void *cls, struct GNUNET_SERVER_Client *client,
323 const struct GNUNET_MessageHeader *message)
325 struct CadetClient *c;
326 struct GNUNET_CADET_PortMessage *pmsg;
328 LOG (GNUNET_ERROR_TYPE_DEBUG, "open port requested\n");
330 /* Sanity check for client registration */
331 if (NULL == (c = GML_client_get (client)))
334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
337 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
339 /* Message size sanity check */
340 if (sizeof (struct GNUNET_CADET_PortMessage) != ntohs (message->size))
343 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
347 pmsg = (struct GNUNET_CADET_PortMessage *) message;
348 if (NULL == c->ports)
350 c->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
352 /* store in client's hashmap */
354 GNUNET_CONTAINER_multihashmap_put (c->ports, &pmsg->port, c,
355 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
361 /* store in global hashmap */
362 /* FIXME only allow one client to have the port open,
363 * have a backup hashmap with waiting clients */
364 GNUNET_CONTAINER_multihashmap_put (ports, &pmsg->port, c,
365 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
367 GNUNET_SERVER_receive_done (client, GNUNET_OK);
372 * Handler for port close requests.
374 * @param cls Closure.
375 * @param client Identification of the client.
376 * @param message The actual message.
379 handle_port_close (void *cls, struct GNUNET_SERVER_Client *client,
380 const struct GNUNET_MessageHeader *message)
382 struct CadetClient *c;
383 struct GNUNET_CADET_PortMessage *pmsg;
386 LOG (GNUNET_ERROR_TYPE_DEBUG, "close port requested\n");
388 /* Sanity check for client registration */
389 if (NULL == (c = GML_client_get (client)))
392 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
395 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
397 /* Message size sanity check */
398 if (sizeof (struct GNUNET_CADET_PortMessage) != ntohs (message->size))
401 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
405 pmsg = (struct GNUNET_CADET_PortMessage *) message;
406 removed = GNUNET_CONTAINER_multihashmap_remove (c->ports, &pmsg->port, c);
407 GNUNET_break_op (GNUNET_YES == removed);
408 removed = GNUNET_CONTAINER_multihashmap_remove (ports, &pmsg->port, c);
409 GNUNET_break_op (GNUNET_YES == removed);
411 GNUNET_SERVER_receive_done (client, GNUNET_OK);
416 * Handler for requests of new channels.
418 * @param cls Closure.
419 * @param client Identification of the client.
420 * @param message The actual message.
423 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
424 const struct GNUNET_MessageHeader *message)
426 struct CadetClient *c;
428 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
429 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
431 /* Sanity check for client registration */
432 if (NULL == (c = GML_client_get (client)))
435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
438 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
440 /* Message size sanity check */
441 if (sizeof (struct GNUNET_CADET_ChannelCreateMessage)
442 != ntohs (message->size))
445 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
450 GCCH_handle_local_create (c,
451 (struct GNUNET_CADET_ChannelCreateMessage *)
454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
458 GNUNET_SERVER_receive_done (client, GNUNET_OK);
463 * Handler for requests of deleting tunnels
466 * @param client identification of the client
467 * @param message the actual message
470 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
471 const struct GNUNET_MessageHeader *message)
473 struct GNUNET_CADET_ChannelDestroyMessage *msg;
474 struct CadetClient *c;
475 struct CadetChannel *ch;
476 CADET_ChannelNumber chid;
478 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
480 /* Sanity check for client registration */
481 if (NULL == (c = GML_client_get (client)))
484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
487 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
489 /* Message sanity check */
490 if (sizeof (struct GNUNET_CADET_ChannelDestroyMessage)
491 != ntohs (message->size))
494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
498 msg = (struct GNUNET_CADET_ChannelDestroyMessage *) message;
500 /* Retrieve tunnel */
501 chid = ntohl (msg->channel_id);
502 ch = GML_channel_get (c, chid);
504 LOG (GNUNET_ERROR_TYPE_INFO, "Client %u is destroying channel %X\n",
509 LOG (GNUNET_ERROR_TYPE_WARNING, " channel %X not found\n", chid);
510 GNUNET_STATISTICS_update (stats,
511 "# client destroy messages on unknown channel",
513 GNUNET_SERVER_receive_done (client, GNUNET_OK);
517 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
519 GNUNET_SERVER_receive_done (client, GNUNET_OK);
525 * Handler for client traffic
528 * @param client identification of the client
529 * @param message the actual message
532 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
533 const struct GNUNET_MessageHeader *message)
535 const struct GNUNET_MessageHeader *payload;
536 struct GNUNET_CADET_LocalData *msg;
537 struct CadetClient *c;
538 struct CadetChannel *ch;
539 CADET_ChannelNumber chid;
542 size_t payload_claimed_size;
545 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
546 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
547 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
549 /* Sanity check for client registration */
550 if (NULL == (c = GML_client_get (client)))
553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
557 /* Sanity check for message size */
558 message_size = ntohs (message->size);
559 if (sizeof (struct GNUNET_CADET_LocalData)
560 + sizeof (struct GNUNET_MessageHeader) > message_size
561 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
568 /* Sanity check for payload size */
569 payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
570 msg = (struct GNUNET_CADET_LocalData *) message;
571 payload = (struct GNUNET_MessageHeader *) &msg[1];
572 payload_claimed_size = ntohs (payload->size);
573 if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
574 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
575 || payload_claimed_size > payload_size)
577 LOG (GNUNET_ERROR_TYPE_WARNING,
578 "client claims to send %u bytes in %u payload\n",
579 payload_claimed_size, payload_size);
581 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
585 chid = ntohl (msg->id);
586 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes (%u payload) by client %u\n",
587 payload_size, payload_claimed_size, c->id);
589 /* Channel exists? */
590 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
591 ch = GML_channel_get (c, chid);
594 GNUNET_STATISTICS_update (stats,
595 "# client data messages on unknown channel",
597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
601 if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
607 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
608 GNUNET_SERVER_receive_done (client, GNUNET_OK);
615 * Handler for client's ACKs for payload traffic.
617 * @param cls Closure (unused).
618 * @param client Identification of the client.
619 * @param message The actual message.
622 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
623 const struct GNUNET_MessageHeader *message)
625 struct GNUNET_CADET_LocalAck *msg;
626 struct CadetChannel *ch;
627 struct CadetClient *c;
628 CADET_ChannelNumber chid;
631 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
632 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
634 /* Sanity check for client registration */
635 if (NULL == (c = GML_client_get (client)))
638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
641 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
643 msg = (struct GNUNET_CADET_LocalAck *) message;
645 /* Channel exists? */
646 chid = ntohl (msg->channel_id);
647 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
648 ch = GML_channel_get (c, chid);
649 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
652 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
653 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
654 GNUNET_STATISTICS_update (stats,
655 "# client ack messages on unknown channel",
657 GNUNET_SERVER_receive_done (client, GNUNET_OK);
661 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
662 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
663 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
665 GCCH_handle_local_ack (ch, fwd);
666 GNUNET_SERVER_receive_done (client, GNUNET_OK);
673 * Iterator over all peers to send a monitoring client info about each peer.
675 * @param cls Closure ().
676 * @param peer Peer ID (tunnel remote peer).
677 * @param value Peer info.
679 * @return #GNUNET_YES, to keep iterating.
682 get_all_peers_iterator (void *cls,
683 const struct GNUNET_PeerIdentity * peer,
686 struct GNUNET_SERVER_Client *client = cls;
687 struct CadetPeer *p = value;
688 struct GNUNET_CADET_LocalInfoPeer msg;
690 msg.header.size = htons (sizeof (msg));
691 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
692 msg.destination = *peer;
693 msg.paths = htons (GCP_count_paths (p));
694 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
696 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
699 GNUNET_SERVER_notification_context_unicast (nc, client,
700 &msg.header, GNUNET_NO);
706 * Iterator over all peers to dump info for each peer.
708 * @param cls Closure (unused).
709 * @param peer Peer ID (tunnel remote peer).
710 * @param value Peer info.
712 * @return #GNUNET_YES, to keep iterating.
715 show_peer_iterator (void *cls,
716 const struct GNUNET_PeerIdentity * peer,
719 struct CadetPeer *p = value;
720 struct CadetTunnel *t;
722 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
724 t = GCP_get_tunnel (p);
726 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
728 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
735 * Iterator over all paths of a peer to build an InfoPeer message.
737 * Message contains blocks of peers, first not included.
739 * @param cls Closure (message to build).
740 * @param peer Peer this path is towards.
741 * @param path Path itself
742 * @return #GNUNET_YES if should keep iterating.
743 * #GNUNET_NO otherwise.
746 path_info_iterator (void *cls,
747 struct CadetPeer *peer,
748 struct CadetPeerPath *path)
750 struct GNUNET_CADET_LocalInfoPeer *resp = cls;
751 struct GNUNET_PeerIdentity *id;
756 msg_size = ntohs (resp->header.size);
757 path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
759 LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
760 if (msg_size + path_size > UINT16_MAX)
762 LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
766 i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
767 i = i / sizeof (struct GNUNET_PeerIdentity);
769 /* Set id to the address of the first free peer slot. */
770 id = (struct GNUNET_PeerIdentity *) &resp[1];
773 /* Don't copy first peers.
774 * First peer is always the local one.
775 * Last peer is always the destination (leave as 0, EOL).
777 for (i = 0; i < path->length - 1; i++)
779 GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
780 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
783 resp->header.size = htons (msg_size + path_size);
790 * Handler for client's INFO PEERS request.
792 * @param cls Closure (unused).
793 * @param client Identification of the client.
794 * @param message The actual message.
797 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
798 const struct GNUNET_MessageHeader *message)
800 struct CadetClient *c;
801 struct GNUNET_MessageHeader reply;
803 /* Sanity check for client registration */
804 if (NULL == (c = GML_client_get (client)))
807 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
811 LOG (GNUNET_ERROR_TYPE_DEBUG,
812 "Received get peers request from client %u (%p)\n",
815 GCP_iterate_all (get_all_peers_iterator, client);
816 reply.size = htons (sizeof (reply));
817 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
818 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
820 LOG (GNUNET_ERROR_TYPE_DEBUG,
821 "Get peers request from client %u completed\n", c->id);
822 GNUNET_SERVER_receive_done (client, GNUNET_OK);
827 * Handler for client's SHOW_PEER request.
829 * @param cls Closure (unused).
830 * @param client Identification of the client.
831 * @param message The actual message.
834 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
835 const struct GNUNET_MessageHeader *message)
837 const struct GNUNET_CADET_LocalInfo *msg;
838 struct GNUNET_CADET_LocalInfoPeer *resp;
840 struct CadetClient *c;
841 unsigned char cbuf[64 * 1024];
843 /* Sanity check for client registration */
844 if (NULL == (c = GML_client_get (client)))
847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
851 msg = (struct GNUNET_CADET_LocalInfo *) message;
852 resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
853 LOG (GNUNET_ERROR_TYPE_INFO,
854 "Received peer info request from client %u for peer %s\n",
855 c->id, GNUNET_i2s_full (&msg->peer));
857 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
858 resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
859 resp->destination = msg->peer;
860 p = GCP_get (&msg->peer, GNUNET_NO);
863 /* We don't know the peer */
865 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
866 GNUNET_i2s_full (&msg->peer));
867 resp->paths = htons (0);
868 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
870 GNUNET_SERVER_notification_context_unicast (nc, client,
873 GNUNET_SERVER_receive_done (client, GNUNET_OK);
877 resp->paths = htons (GCP_count_paths (p));
878 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
879 GCP_iterate_paths (p, &path_info_iterator, resp);
881 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
882 &resp->header, GNUNET_NO);
884 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer from client %u completed.\n", c->id);
885 GNUNET_SERVER_receive_done (client, GNUNET_OK);
890 * Iterator over all tunnels to send a monitoring client info about each tunnel.
892 * @param cls Closure ().
893 * @param peer Peer ID (tunnel remote peer).
894 * @param value Tunnel info.
896 * @return #GNUNET_YES, to keep iterating.
899 get_all_tunnels_iterator (void *cls,
900 const struct GNUNET_PeerIdentity * peer,
903 struct GNUNET_SERVER_Client *client = cls;
904 struct CadetTunnel *t = value;
905 struct GNUNET_CADET_LocalInfoTunnel msg;
907 msg.header.size = htons (sizeof (msg));
908 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
909 msg.destination = *peer;
910 msg.channels = htonl (GCT_count_channels (t));
911 msg.connections = htonl (GCT_count_any_connections (t));
912 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
913 msg.estate = htons ((uint16_t) GCT_get_estate (t));
915 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
918 GNUNET_SERVER_notification_context_unicast (nc, client,
919 &msg.header, GNUNET_NO);
925 * Handler for client's INFO TUNNELS request.
927 * @param cls Closure (unused).
928 * @param client Identification of the client.
929 * @param message The actual message.
932 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
933 const struct GNUNET_MessageHeader *message)
935 struct CadetClient *c;
936 struct GNUNET_MessageHeader reply;
938 /* Sanity check for client registration */
939 if (NULL == (c = GML_client_get (client)))
942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
946 LOG (GNUNET_ERROR_TYPE_DEBUG,
947 "Received get tunnels request from client %u (%p)\n",
950 GCT_iterate_all (get_all_tunnels_iterator, client);
951 reply.size = htons (sizeof (reply));
952 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
953 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
955 LOG (GNUNET_ERROR_TYPE_DEBUG,
956 "Get tunnels request from client %u completed\n", c->id);
957 GNUNET_SERVER_receive_done (client, GNUNET_OK);
962 iter_connection (void *cls, struct CadetConnection *c)
964 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
965 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
967 h[msg->connections] = *(GCC_get_id (c));
972 iter_channel (void *cls, struct CadetChannel *ch)
974 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
975 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
976 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
978 chn[msg->channels] = htonl (GCCH_get_id (ch));
984 * Handler for client's SHOW_TUNNEL request.
986 * @param cls Closure (unused).
987 * @param client Identification of the client.
988 * @param message The actual message.
991 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
992 const struct GNUNET_MessageHeader *message)
994 const struct GNUNET_CADET_LocalInfo *msg;
995 struct GNUNET_CADET_LocalInfoTunnel *resp;
996 struct CadetClient *c;
997 struct CadetTunnel *t;
1002 /* Sanity check for client registration */
1003 if (NULL == (c = GML_client_get (client)))
1006 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1010 msg = (struct GNUNET_CADET_LocalInfo *) message;
1011 LOG (GNUNET_ERROR_TYPE_DEBUG,
1012 "Received tunnel info request from client %u for tunnel %s\n",
1013 c->id, GNUNET_i2s_full(&msg->peer));
1015 t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
1018 /* We don't know the tunnel */
1019 struct GNUNET_CADET_LocalInfoTunnel warn;
1021 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
1022 GNUNET_i2s_full(&msg->peer), sizeof (warn));
1023 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1024 warn.header.size = htons (sizeof (warn));
1025 warn.destination = msg->peer;
1026 warn.channels = htonl (0);
1027 warn.connections = htonl (0);
1028 warn.cstate = htons (0);
1029 warn.estate = htons (0);
1031 GNUNET_SERVER_notification_context_unicast (nc, client,
1034 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1038 /* Initialize context */
1039 ch_n = GCT_count_channels (t);
1040 c_n = GCT_count_any_connections (t);
1042 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1043 size += c_n * sizeof (struct GNUNET_CADET_Hash);
1044 size += ch_n * sizeof (CADET_ChannelNumber);
1046 resp = GNUNET_malloc (size);
1047 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1048 resp->header.size = htons (size);
1049 resp->destination = msg->peer;
1050 /* Do not interleave with iterators, iter_channel needs conn in HBO */
1051 GCT_iterate_connections (t, &iter_connection, resp);
1052 GCT_iterate_channels (t, &iter_channel, resp);
1053 resp->connections = htonl (resp->connections);
1054 resp->channels = htonl (resp->channels);
1055 /* Do not interleave end */
1056 resp->cstate = htons (GCT_get_cstate (t));
1057 resp->estate = htons (GCT_get_estate (t));
1058 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1059 &resp->header, GNUNET_NO);
1062 LOG (GNUNET_ERROR_TYPE_DEBUG,
1063 "Show tunnel request from client %u completed. %u conn, %u ch\n",
1065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1070 * Handler for client's INFO_DUMP request.
1072 * @param cls Closure (unused).
1073 * @param client Identification of the client.
1074 * @param message The actual message.
1077 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
1078 const struct GNUNET_MessageHeader *message)
1080 struct CadetClient *c;
1082 /* Sanity check for client registration */
1083 if (NULL == (c = GML_client_get (client)))
1086 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1090 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
1092 LOG (GNUNET_ERROR_TYPE_ERROR,
1093 "*************************** DUMP START ***************************\n");
1095 for (c = clients_head; NULL != c; c = c->next)
1097 LOG (GNUNET_ERROR_TYPE_ERROR, "Client %u (%p), handle: %p\n",
1098 c->id, c, c->handle);
1099 if (NULL != c->ports)
1100 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u ports registered\n",
1101 GNUNET_CONTAINER_multihashmap_size (c->ports));
1103 LOG (GNUNET_ERROR_TYPE_ERROR, "\t no ports registered\n");
1104 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u own channles\n",
1105 GNUNET_CONTAINER_multihashmap32_size (c->own_channels));
1106 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u incoming channles\n",
1107 GNUNET_CONTAINER_multihashmap32_size (c->incoming_channels));
1109 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1110 GCP_iterate_all (&show_peer_iterator, NULL);
1112 LOG (GNUNET_ERROR_TYPE_ERROR,
1113 "**************************** DUMP END ****************************\n");
1115 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1120 * Functions to handle messages from clients
1122 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1123 {&handle_port_open, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1124 sizeof (struct GNUNET_CADET_PortMessage)},
1125 {&handle_port_close, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1126 sizeof (struct GNUNET_CADET_PortMessage)},
1127 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1128 sizeof (struct GNUNET_CADET_ChannelCreateMessage)},
1129 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1130 sizeof (struct GNUNET_CADET_ChannelDestroyMessage)},
1131 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1132 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1133 sizeof (struct GNUNET_CADET_LocalAck)},
1134 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1135 sizeof (struct GNUNET_MessageHeader)},
1136 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1137 sizeof (struct GNUNET_CADET_LocalInfo)},
1138 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1139 sizeof (struct GNUNET_MessageHeader)},
1140 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1141 sizeof (struct GNUNET_CADET_LocalInfo)},
1142 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1143 sizeof (struct GNUNET_MessageHeader)},
1149 /******************************************************************************/
1150 /******************************** API ***********************************/
1151 /******************************************************************************/
1154 * Initialize server subsystem.
1156 * @param handle Server handle.
1159 GML_init (struct GNUNET_SERVER_Handle *handle)
1161 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1162 server_handle = handle;
1163 GNUNET_SERVER_suspend (server_handle);
1164 ports = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
1169 * Install server (service) handlers and start listening to clients.
1174 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1175 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1176 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1178 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1180 clients_head = NULL;
1181 clients_tail = NULL;
1183 GNUNET_SERVER_resume (server_handle);
1193 struct CadetClient *c;
1195 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down local\n");
1197 for (c = clients_head; NULL != clients_head; c = clients_head)
1202 GNUNET_SERVER_notification_context_destroy (nc);
1210 * Get a channel from a client.
1212 * @param c Client to check.
1213 * @param chid Channel ID, must be local (> 0x800...).
1215 * @return non-NULL if channel exists in the clients lists
1217 struct CadetChannel *
1218 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1220 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1222 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1224 GNUNET_break_op (0);
1225 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1229 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1230 map = c->incoming_channels;
1231 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1232 map = c->own_channels;
1241 LOG (GNUNET_ERROR_TYPE_DEBUG,
1242 "Client %s does no t have a valid map for CHID %X\n",
1246 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1251 * Add a channel to a client
1253 * @param client Client.
1254 * @param chid Channel ID.
1255 * @param ch Channel.
1258 GML_channel_add (struct CadetClient *client,
1260 struct CadetChannel *ch)
1262 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1263 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1264 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1265 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1266 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1267 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1274 * Remove a channel from a client.
1276 * @param client Client.
1277 * @param chid Channel ID.
1278 * @param ch Channel.
1281 GML_channel_remove (struct CadetClient *client,
1283 struct CadetChannel *ch)
1285 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1286 GNUNET_break (GNUNET_YES ==
1287 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1289 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1290 GNUNET_break (GNUNET_YES ==
1291 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1299 * Get the tunnel's next free local channel ID.
1303 * @return LID of a channel free to use.
1306 GML_get_next_chid (struct CadetClient *c)
1308 CADET_ChannelNumber chid;
1310 while (NULL != GML_channel_get (c, c->next_chid))
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1313 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1315 chid = c->next_chid;
1316 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1323 * Check if client has registered with the service and has not disconnected
1325 * @param client the client to check
1327 * @return non-NULL if client exists in the global DLL
1329 struct CadetClient *
1330 GML_client_get (struct GNUNET_SERVER_Client *client)
1334 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1338 * Find a client that has opened a port
1340 * @param port Port to check.
1342 * @return non-NULL if a client has the port.
1344 struct CadetClient *
1345 GML_client_get_by_port (const struct GNUNET_HashCode *port)
1347 return GNUNET_CONTAINER_multihashmap_get (ports, port);
1352 * Deletes a channel from a client (either owner or destination).
1354 * @param c Client whose tunnel to delete.
1355 * @param ch Channel which should be deleted.
1356 * @param id Channel ID.
1359 GML_client_delete_channel (struct CadetClient *c,
1360 struct CadetChannel *ch,
1361 CADET_ChannelNumber id)
1365 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1367 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1369 if (GNUNET_YES != res)
1370 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1372 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1374 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1376 if (GNUNET_YES != res)
1377 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1386 * Build a local ACK message and send it to a local client, if needed.
1388 * If the client was already allowed to send data, do nothing.
1390 * @param c Client to whom send the ACK.
1391 * @param id Channel ID to use
1394 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1396 struct GNUNET_CADET_LocalAck msg;
1398 LOG (GNUNET_ERROR_TYPE_DEBUG,
1399 "send local %s ack on %X towards %p\n",
1400 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1402 msg.header.size = htons (sizeof (msg));
1403 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1404 msg.channel_id = htonl (id);
1405 GNUNET_SERVER_notification_context_unicast (nc,
1415 * Notify the client that a new incoming channel was created.
1417 * @param c Client to notify.
1418 * @param id Channel ID.
1419 * @param port Channel's destination port.
1420 * @param opt Options (bit array).
1421 * @param peer Origin peer.
1424 GML_send_channel_create (struct CadetClient *c,
1425 uint32_t id, struct GNUNET_HashCode *port,
1426 uint32_t opt, const struct GNUNET_PeerIdentity *peer)
1428 struct GNUNET_CADET_ChannelCreateMessage msg;
1430 msg.header.size = htons (sizeof (msg));
1431 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1432 msg.channel_id = htonl (id);
1434 msg.opt = htonl (opt);
1436 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1437 &msg.header, GNUNET_NO);
1442 * Build a local channel NACK message and send it to a local client.
1444 * @param c Client to whom send the NACK.
1445 * @param id Channel ID to use
1448 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1450 struct GNUNET_CADET_LocalAck msg;
1452 LOG (GNUNET_ERROR_TYPE_DEBUG,
1453 "send local nack on %X towards %p\n",
1456 msg.header.size = htons (sizeof (msg));
1457 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1458 msg.channel_id = htonl (id);
1459 GNUNET_SERVER_notification_context_unicast (nc,
1467 * Notify a client that a channel is no longer valid.
1470 * @param id ID of the channel that is destroyed.
1473 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1475 struct GNUNET_CADET_ChannelDestroyMessage msg;
1482 if (GNUNET_YES == c->shutting_down)
1484 msg.header.size = htons (sizeof (msg));
1485 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1486 msg.channel_id = htonl (id);
1487 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1488 &msg.header, GNUNET_NO);
1493 * Modify the cadet message ID from global to local and send to client.
1495 * @param c Client to send to.
1496 * @param msg Message to modify and send.
1497 * @param id Channel ID to use (c can be both owner and client).
1500 GML_send_data (struct CadetClient *c,
1501 const struct GNUNET_CADET_Data *msg,
1502 CADET_ChannelNumber id)
1504 struct GNUNET_CADET_LocalData *copy;
1505 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1506 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1508 if (size < sizeof (struct GNUNET_MessageHeader))
1510 GNUNET_break_op (0);
1518 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1519 GNUNET_memcpy (©[1], &msg[1], size);
1520 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1521 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1522 copy->id = htonl (id);
1523 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1524 ©->header, GNUNET_NO);
1529 * Get the static string to represent a client.
1533 * @return Static string for the client.
1536 GML_2s (const struct CadetClient *c)
1538 static char buf[32];
1540 SPRINTF (buf, "%u", c->id);