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);
235 * Create a client record, register data and initialize memory.
237 * @param client Client's handle.
239 static struct CadetClient *
240 client_new (struct GNUNET_SERVER_Client *client)
242 struct CadetClient *c;
244 GNUNET_SERVER_client_keep (client);
245 GNUNET_SERVER_notification_context_add (nc, client);
247 c = GNUNET_new (struct CadetClient);
249 c->id = next_client_id++; /* overflow not important: just for debug */
250 c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
252 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
253 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
255 GNUNET_SERVER_client_set_user_context (client, c);
256 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
257 GNUNET_STATISTICS_update (stats, "# clients", +1, GNUNET_NO);
259 LOG (GNUNET_ERROR_TYPE_DEBUG, " client created: %p/%u\n", c, c->id);
265 /******************************************************************************/
266 /******************************** HANDLES ***********************************/
267 /******************************************************************************/
270 * Handler for client connection.
272 * @param cls Closure (unused).
273 * @param client Client handler.
276 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
278 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client connected: %p\n", client);
282 (void) client_new (client);
287 * Handler for client disconnection
290 * @param client identification of the client; NULL
291 * for the last call when the server is destroyed
294 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
296 struct CadetClient *c;
298 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client disconnected: %p\n", client);
300 c = GML_client_get (client);
303 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
309 LOG (GNUNET_ERROR_TYPE_DEBUG, " disconnecting client's context NULL\n");
316 * Handler for port open requests.
318 * @param cls Closure.
319 * @param client Identification of the client.
320 * @param message The actual message.
323 handle_port_open (void *cls, struct GNUNET_SERVER_Client *client,
324 const struct GNUNET_MessageHeader *message)
326 struct CadetClient *c;
327 struct GNUNET_CADET_PortMessage *pmsg;
329 LOG (GNUNET_ERROR_TYPE_DEBUG, "open port requested\n");
331 /* Sanity check for client registration */
332 if (NULL == (c = GML_client_get (client)))
335 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
338 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
340 /* Message size sanity check */
341 if (sizeof (struct GNUNET_CADET_PortMessage) != ntohs (message->size))
344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
348 pmsg = (struct GNUNET_CADET_PortMessage *) message;
349 if (NULL == c->ports)
351 c->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
353 /* store in client's hashmap */
355 GNUNET_CONTAINER_multihashmap_put (c->ports, &pmsg->port, c,
356 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
359 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
362 /* store in global hashmap */
363 /* FIXME only allow one client to have the port open,
364 * have a backup hashmap with waiting clients */
365 GNUNET_CONTAINER_multihashmap_put (ports, &pmsg->port, c,
366 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
368 GNUNET_SERVER_receive_done (client, GNUNET_OK);
373 * Handler for port close requests.
375 * @param cls Closure.
376 * @param client Identification of the client.
377 * @param message The actual message.
380 handle_port_close (void *cls, struct GNUNET_SERVER_Client *client,
381 const struct GNUNET_MessageHeader *message)
383 struct CadetClient *c;
384 struct GNUNET_CADET_PortMessage *pmsg;
387 LOG (GNUNET_ERROR_TYPE_DEBUG, "close port requested\n");
389 /* Sanity check for client registration */
390 if (NULL == (c = GML_client_get (client)))
393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
396 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
398 /* Message size sanity check */
399 if (sizeof (struct GNUNET_CADET_PortMessage) != ntohs (message->size))
402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
406 pmsg = (struct GNUNET_CADET_PortMessage *) message;
407 removed = GNUNET_CONTAINER_multihashmap_remove (c->ports, &pmsg->port, c);
408 GNUNET_break_op (GNUNET_YES == removed);
409 removed = GNUNET_CONTAINER_multihashmap_remove (ports, &pmsg->port, c);
410 GNUNET_break_op (GNUNET_YES == removed);
412 GNUNET_SERVER_receive_done (client, GNUNET_OK);
417 * Handler for requests of new channels.
419 * @param cls Closure.
420 * @param client Identification of the client.
421 * @param message The actual message.
424 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
425 const struct GNUNET_MessageHeader *message)
427 struct CadetClient *c;
429 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
430 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
432 /* Sanity check for client registration */
433 if (NULL == (c = GML_client_get (client)))
436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
439 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
441 /* Message size sanity check */
442 if (sizeof (struct GNUNET_CADET_ChannelCreateMessage)
443 != ntohs (message->size))
446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
451 GCCH_handle_local_create (c,
452 (struct GNUNET_CADET_ChannelCreateMessage *)
455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
464 * Handler for requests of deleting tunnels
467 * @param client identification of the client
468 * @param message the actual message
471 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
472 const struct GNUNET_MessageHeader *message)
474 struct GNUNET_CADET_ChannelDestroyMessage *msg;
475 struct CadetClient *c;
476 struct CadetChannel *ch;
477 CADET_ChannelNumber chid;
479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
481 /* Sanity check for client registration */
482 if (NULL == (c = GML_client_get (client)))
485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
488 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
490 /* Message sanity check */
491 if (sizeof (struct GNUNET_CADET_ChannelDestroyMessage)
492 != ntohs (message->size))
495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
499 msg = (struct GNUNET_CADET_ChannelDestroyMessage *) message;
501 /* Retrieve tunnel */
502 chid = ntohl (msg->channel_id);
503 ch = GML_channel_get (c, chid);
505 LOG (GNUNET_ERROR_TYPE_INFO, "Client %u is destroying channel %X\n",
510 LOG (GNUNET_ERROR_TYPE_WARNING, " channel %X not found\n", chid);
511 GNUNET_STATISTICS_update (stats,
512 "# client destroy messages on unknown channel",
514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
518 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
520 GNUNET_SERVER_receive_done (client, GNUNET_OK);
526 * Handler for client traffic
529 * @param client identification of the client
530 * @param message the actual message
533 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
534 const struct GNUNET_MessageHeader *message)
536 const struct GNUNET_MessageHeader *payload;
537 struct GNUNET_CADET_LocalData *msg;
538 struct CadetClient *c;
539 struct CadetChannel *ch;
540 CADET_ChannelNumber chid;
543 size_t payload_claimed_size;
546 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
547 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
548 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
550 /* Sanity check for client registration */
551 if (NULL == (c = GML_client_get (client)))
554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
558 /* Sanity check for message size */
559 message_size = ntohs (message->size);
560 if (sizeof (struct GNUNET_CADET_LocalData)
561 + sizeof (struct GNUNET_MessageHeader) > message_size
562 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
569 /* Sanity check for payload size */
570 payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
571 msg = (struct GNUNET_CADET_LocalData *) message;
572 payload = (struct GNUNET_MessageHeader *) &msg[1];
573 payload_claimed_size = ntohs (payload->size);
574 if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
575 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
576 || payload_claimed_size > payload_size)
578 LOG (GNUNET_ERROR_TYPE_WARNING,
579 "client claims to send %u bytes in %u payload\n",
580 payload_claimed_size, payload_size);
582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
586 chid = ntohl (msg->id);
587 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes (%u payload) by client %u\n",
588 payload_size, payload_claimed_size, c->id);
590 /* Channel exists? */
591 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
592 ch = GML_channel_get (c, chid);
595 GNUNET_STATISTICS_update (stats,
596 "# client data messages on unknown channel",
598 GNUNET_SERVER_receive_done (client, GNUNET_OK);
602 if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
608 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
609 GNUNET_SERVER_receive_done (client, GNUNET_OK);
616 * Handler for client's ACKs for payload traffic.
618 * @param cls Closure (unused).
619 * @param client Identification of the client.
620 * @param message The actual message.
623 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
624 const struct GNUNET_MessageHeader *message)
626 struct GNUNET_CADET_LocalAck *msg;
627 struct CadetChannel *ch;
628 struct CadetClient *c;
629 CADET_ChannelNumber chid;
632 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
633 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
635 /* Sanity check for client registration */
636 if (NULL == (c = GML_client_get (client)))
639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
642 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
644 msg = (struct GNUNET_CADET_LocalAck *) message;
646 /* Channel exists? */
647 chid = ntohl (msg->channel_id);
648 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
649 ch = GML_channel_get (c, chid);
650 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
653 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
654 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
655 GNUNET_STATISTICS_update (stats,
656 "# client ack messages on unknown channel",
658 GNUNET_SERVER_receive_done (client, GNUNET_OK);
662 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
663 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
664 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
666 GCCH_handle_local_ack (ch, fwd);
667 GNUNET_SERVER_receive_done (client, GNUNET_OK);
674 * Iterator over all peers to send a monitoring client info about each peer.
676 * @param cls Closure ().
677 * @param peer Peer ID (tunnel remote peer).
678 * @param value Peer info.
680 * @return #GNUNET_YES, to keep iterating.
683 get_all_peers_iterator (void *cls,
684 const struct GNUNET_PeerIdentity * peer,
687 struct GNUNET_SERVER_Client *client = cls;
688 struct CadetPeer *p = value;
689 struct GNUNET_CADET_LocalInfoPeer msg;
691 msg.header.size = htons (sizeof (msg));
692 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
693 msg.destination = *peer;
694 msg.paths = htons (GCP_count_paths (p));
695 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
697 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
700 GNUNET_SERVER_notification_context_unicast (nc, client,
701 &msg.header, GNUNET_NO);
707 * Iterator over all peers to dump info for each peer.
709 * @param cls Closure (unused).
710 * @param peer Peer ID (tunnel remote peer).
711 * @param value Peer info.
713 * @return #GNUNET_YES, to keep iterating.
716 show_peer_iterator (void *cls,
717 const struct GNUNET_PeerIdentity * peer,
720 struct CadetPeer *p = value;
721 struct CadetTunnel *t;
723 t = GCP_get_tunnel (p);
725 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
727 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
734 * Iterator over all paths of a peer to build an InfoPeer message.
736 * Message contains blocks of peers, first not included.
738 * @param cls Closure (message to build).
739 * @param peer Peer this path is towards.
740 * @param path Path itself
741 * @return #GNUNET_YES if should keep iterating.
742 * #GNUNET_NO otherwise.
745 path_info_iterator (void *cls,
746 struct CadetPeer *peer,
747 struct CadetPeerPath *path)
749 struct GNUNET_CADET_LocalInfoPeer *resp = cls;
750 struct GNUNET_PeerIdentity *id;
755 msg_size = ntohs (resp->header.size);
756 path_size = sizeof (struct GNUNET_PeerIdentity) * (path->length - 1);
758 LOG (GNUNET_ERROR_TYPE_DEBUG, "Info Path %u\n", path->length);
759 if (msg_size + path_size > UINT16_MAX)
761 LOG (GNUNET_ERROR_TYPE_WARNING, "path too long for info message\n");
765 i = msg_size - sizeof (struct GNUNET_CADET_LocalInfoPeer);
766 i = i / sizeof (struct GNUNET_PeerIdentity);
768 /* Set id to the address of the first free peer slot. */
769 id = (struct GNUNET_PeerIdentity *) &resp[1];
772 /* Don't copy first peers.
773 * First peer is always the local one.
774 * Last peer is always the destination (leave as 0, EOL).
776 for (i = 0; i < path->length - 1; i++)
778 GNUNET_PEER_resolve (path->peers[i + 1], &id[i]);
779 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
782 resp->header.size = htons (msg_size + path_size);
789 * Handler for client's INFO PEERS request.
791 * @param cls Closure (unused).
792 * @param client Identification of the client.
793 * @param message The actual message.
796 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
797 const struct GNUNET_MessageHeader *message)
799 struct CadetClient *c;
800 struct GNUNET_MessageHeader reply;
802 /* Sanity check for client registration */
803 if (NULL == (c = GML_client_get (client)))
806 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
810 LOG (GNUNET_ERROR_TYPE_DEBUG,
811 "Received get peers request from client %u (%p)\n",
814 GCP_iterate_all (get_all_peers_iterator, client);
815 reply.size = htons (sizeof (reply));
816 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
817 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
819 LOG (GNUNET_ERROR_TYPE_DEBUG,
820 "Get peers request from client %u completed\n", c->id);
821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
826 * Handler for client's SHOW_PEER request.
828 * @param cls Closure (unused).
829 * @param client Identification of the client.
830 * @param message The actual message.
833 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
834 const struct GNUNET_MessageHeader *message)
836 const struct GNUNET_CADET_LocalInfo *msg;
837 struct GNUNET_CADET_LocalInfoPeer *resp;
839 struct CadetClient *c;
840 unsigned char cbuf[64 * 1024];
842 /* Sanity check for client registration */
843 if (NULL == (c = GML_client_get (client)))
846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
850 msg = (struct GNUNET_CADET_LocalInfo *) message;
851 resp = (struct GNUNET_CADET_LocalInfoPeer *) cbuf;
852 LOG (GNUNET_ERROR_TYPE_INFO,
853 "Received peer info request from client %u for peer %s\n",
854 c->id, GNUNET_i2s_full (&msg->peer));
856 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
857 resp->header.size = htons (sizeof (struct GNUNET_CADET_LocalInfoPeer));
858 resp->destination = msg->peer;
859 p = GCP_get (&msg->peer, GNUNET_NO);
862 /* We don't know the peer */
864 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown\n",
865 GNUNET_i2s_full (&msg->peer));
866 resp->paths = htons (0);
867 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
869 GNUNET_SERVER_notification_context_unicast (nc, client,
872 GNUNET_SERVER_receive_done (client, GNUNET_OK);
876 resp->paths = htons (GCP_count_paths (p));
877 resp->tunnel = htons (NULL != GCP_get_tunnel (p));
878 GCP_iterate_paths (p, &path_info_iterator, resp);
880 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
881 &resp->header, GNUNET_NO);
883 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer from client %u completed.\n", c->id);
884 GNUNET_SERVER_receive_done (client, GNUNET_OK);
889 * Iterator over all tunnels to send a monitoring client info about each tunnel.
891 * @param cls Closure ().
892 * @param peer Peer ID (tunnel remote peer).
893 * @param value Tunnel info.
895 * @return #GNUNET_YES, to keep iterating.
898 get_all_tunnels_iterator (void *cls,
899 const struct GNUNET_PeerIdentity * peer,
902 struct GNUNET_SERVER_Client *client = cls;
903 struct CadetTunnel *t = value;
904 struct GNUNET_CADET_LocalInfoTunnel msg;
906 msg.header.size = htons (sizeof (msg));
907 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
908 msg.destination = *peer;
909 msg.channels = htonl (GCT_count_channels (t));
910 msg.connections = htonl (GCT_count_any_connections (t));
911 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
912 msg.estate = htons ((uint16_t) GCT_get_estate (t));
914 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
917 GNUNET_SERVER_notification_context_unicast (nc, client,
918 &msg.header, GNUNET_NO);
924 * Handler for client's INFO TUNNELS request.
926 * @param cls Closure (unused).
927 * @param client Identification of the client.
928 * @param message The actual message.
931 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
932 const struct GNUNET_MessageHeader *message)
934 struct CadetClient *c;
935 struct GNUNET_MessageHeader reply;
937 /* Sanity check for client registration */
938 if (NULL == (c = GML_client_get (client)))
941 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 "Received get tunnels request from client %u (%p)\n",
949 GCT_iterate_all (get_all_tunnels_iterator, client);
950 reply.size = htons (sizeof (reply));
951 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
952 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
954 LOG (GNUNET_ERROR_TYPE_DEBUG,
955 "Get tunnels request from client %u completed\n", c->id);
956 GNUNET_SERVER_receive_done (client, GNUNET_OK);
961 iter_connection (void *cls, struct CadetConnection *c)
963 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
964 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
966 h[msg->connections] = *(GCC_get_id (c));
971 iter_channel (void *cls, struct CadetChannel *ch)
973 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
974 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
975 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
977 chn[msg->channels] = htonl (GCCH_get_id (ch));
983 * Handler for client's SHOW_TUNNEL request.
985 * @param cls Closure (unused).
986 * @param client Identification of the client.
987 * @param message The actual message.
990 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
991 const struct GNUNET_MessageHeader *message)
993 const struct GNUNET_CADET_LocalInfo *msg;
994 struct GNUNET_CADET_LocalInfoTunnel *resp;
995 struct CadetClient *c;
996 struct CadetTunnel *t;
1001 /* Sanity check for client registration */
1002 if (NULL == (c = GML_client_get (client)))
1005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1009 msg = (struct GNUNET_CADET_LocalInfo *) message;
1010 LOG (GNUNET_ERROR_TYPE_DEBUG,
1011 "Received tunnel info request from client %u for tunnel %s\n",
1012 c->id, GNUNET_i2s_full(&msg->peer));
1014 t = GCP_get_tunnel (GCP_get (&msg->peer, GNUNET_NO));
1017 /* We don't know the tunnel */
1018 struct GNUNET_CADET_LocalInfoTunnel warn;
1020 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
1021 GNUNET_i2s_full(&msg->peer), sizeof (warn));
1022 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1023 warn.header.size = htons (sizeof (warn));
1024 warn.destination = msg->peer;
1025 warn.channels = htonl (0);
1026 warn.connections = htonl (0);
1027 warn.cstate = htons (0);
1028 warn.estate = htons (0);
1030 GNUNET_SERVER_notification_context_unicast (nc, client,
1033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1037 /* Initialize context */
1038 ch_n = GCT_count_channels (t);
1039 c_n = GCT_count_any_connections (t);
1041 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1042 size += c_n * sizeof (struct GNUNET_CADET_Hash);
1043 size += ch_n * sizeof (CADET_ChannelNumber);
1045 resp = GNUNET_malloc (size);
1046 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1047 resp->header.size = htons (size);
1048 resp->destination = msg->peer;
1049 /* Do not interleave with iterators, iter_channel needs conn in HBO */
1050 GCT_iterate_connections (t, &iter_connection, resp);
1051 GCT_iterate_channels (t, &iter_channel, resp);
1052 resp->connections = htonl (resp->connections);
1053 resp->channels = htonl (resp->channels);
1054 /* Do not interleave end */
1055 resp->cstate = htons (GCT_get_cstate (t));
1056 resp->estate = htons (GCT_get_estate (t));
1057 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1058 &resp->header, GNUNET_NO);
1061 LOG (GNUNET_ERROR_TYPE_DEBUG,
1062 "Show tunnel request from client %u completed. %u conn, %u ch\n",
1064 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1069 * Handler for client's INFO_DUMP request.
1071 * @param cls Closure (unused).
1072 * @param client Identification of the client.
1073 * @param message The actual message.
1076 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
1077 const struct GNUNET_MessageHeader *message)
1079 struct CadetClient *c;
1081 /* Sanity check for client registration */
1082 if (NULL == (c = GML_client_get (client)))
1085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1089 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
1091 LOG (GNUNET_ERROR_TYPE_ERROR,
1092 "*************************** DUMP START ***************************\n");
1094 for (c = clients_head; NULL != c; c = c->next)
1096 LOG (GNUNET_ERROR_TYPE_ERROR, "Client %u (%p), handle: %p\n",
1097 c->id, c, c->handle);
1098 if (NULL != c->ports)
1099 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u ports registered\n",
1100 GNUNET_CONTAINER_multihashmap_size (c->ports));
1102 LOG (GNUNET_ERROR_TYPE_ERROR, "\t no ports registered\n");
1103 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u own channles\n",
1104 GNUNET_CONTAINER_multihashmap32_size (c->own_channels));
1105 LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u incoming channles\n",
1106 GNUNET_CONTAINER_multihashmap32_size (c->incoming_channels));
1108 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1109 GCP_iterate_all (&show_peer_iterator, NULL);
1111 LOG (GNUNET_ERROR_TYPE_ERROR,
1112 "**************************** DUMP END ****************************\n");
1114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1119 * Functions to handle messages from clients
1121 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1122 {&handle_port_open, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1123 sizeof (struct GNUNET_CADET_PortMessage)},
1124 {&handle_port_close, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1125 sizeof (struct GNUNET_CADET_PortMessage)},
1126 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1127 sizeof (struct GNUNET_CADET_ChannelCreateMessage)},
1128 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1129 sizeof (struct GNUNET_CADET_ChannelDestroyMessage)},
1130 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1131 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1132 sizeof (struct GNUNET_CADET_LocalAck)},
1133 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1134 sizeof (struct GNUNET_MessageHeader)},
1135 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1136 sizeof (struct GNUNET_CADET_LocalInfo)},
1137 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1138 sizeof (struct GNUNET_MessageHeader)},
1139 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1140 sizeof (struct GNUNET_CADET_LocalInfo)},
1141 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1142 sizeof (struct GNUNET_MessageHeader)},
1148 /******************************************************************************/
1149 /******************************** API ***********************************/
1150 /******************************************************************************/
1153 * Initialize server subsystem.
1155 * @param handle Server handle.
1158 GML_init (struct GNUNET_SERVER_Handle *handle)
1160 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1161 server_handle = handle;
1162 GNUNET_SERVER_suspend (server_handle);
1163 ports = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
1168 * Install server (service) handlers and start listening to clients.
1173 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1174 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1175 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1177 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1179 clients_head = NULL;
1180 clients_tail = NULL;
1182 GNUNET_SERVER_resume (server_handle);
1192 struct CadetClient *c;
1194 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down local\n");
1196 for (c = clients_head; NULL != clients_head; c = clients_head)
1201 GNUNET_SERVER_notification_context_destroy (nc);
1209 * Get a channel from a client.
1211 * @param c Client to check.
1212 * @param chid Channel ID, must be local (> 0x800...).
1214 * @return non-NULL if channel exists in the clients lists
1216 struct CadetChannel *
1217 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1219 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1221 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1223 GNUNET_break_op (0);
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1228 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1229 map = c->incoming_channels;
1230 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1231 map = c->own_channels;
1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1241 "Client %s does no t have a valid map for CHID %X\n",
1245 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1250 * Add a channel to a client
1252 * @param client Client.
1253 * @param chid Channel ID.
1254 * @param ch Channel.
1257 GML_channel_add (struct CadetClient *client,
1259 struct CadetChannel *ch)
1261 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1262 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1263 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1264 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1265 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1273 * Remove a channel from a client.
1275 * @param client Client.
1276 * @param chid Channel ID.
1277 * @param ch Channel.
1280 GML_channel_remove (struct CadetClient *client,
1282 struct CadetChannel *ch)
1284 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1285 GNUNET_break (GNUNET_YES ==
1286 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1288 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1289 GNUNET_break (GNUNET_YES ==
1290 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1298 * Get the tunnel's next free local channel ID.
1302 * @return LID of a channel free to use.
1305 GML_get_next_chid (struct CadetClient *c)
1307 CADET_ChannelNumber chid;
1309 while (NULL != GML_channel_get (c, c->next_chid))
1311 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1312 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1314 chid = c->next_chid;
1315 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1322 * Check if client has registered with the service and has not disconnected
1324 * @param client the client to check
1326 * @return non-NULL if client exists in the global DLL
1328 struct CadetClient *
1329 GML_client_get (struct GNUNET_SERVER_Client *client)
1333 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1337 * Find a client that has opened a port
1339 * @param port Port to check.
1341 * @return non-NULL if a client has the port.
1343 struct CadetClient *
1344 GML_client_get_by_port (const struct GNUNET_HashCode *port)
1346 return GNUNET_CONTAINER_multihashmap_get (ports, port);
1351 * Deletes a channel from a client (either owner or destination).
1353 * @param c Client whose tunnel to delete.
1354 * @param ch Channel which should be deleted.
1355 * @param id Channel ID.
1358 GML_client_delete_channel (struct CadetClient *c,
1359 struct CadetChannel *ch,
1360 CADET_ChannelNumber id)
1364 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1366 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1368 if (GNUNET_YES != res)
1369 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1371 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1373 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1375 if (GNUNET_YES != res)
1376 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1385 * Build a local ACK message and send it to a local client, if needed.
1387 * If the client was already allowed to send data, do nothing.
1389 * @param c Client to whom send the ACK.
1390 * @param id Channel ID to use
1393 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1395 struct GNUNET_CADET_LocalAck msg;
1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "send local %s ack on %X towards %p\n",
1399 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1401 msg.header.size = htons (sizeof (msg));
1402 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1403 msg.channel_id = htonl (id);
1404 GNUNET_SERVER_notification_context_unicast (nc,
1414 * Notify the client that a new incoming channel was created.
1416 * @param c Client to notify.
1417 * @param id Channel ID.
1418 * @param port Channel's destination port.
1419 * @param opt Options (bit array).
1420 * @param peer Origin peer.
1423 GML_send_channel_create (struct CadetClient *c,
1424 uint32_t id, struct GNUNET_HashCode *port,
1425 uint32_t opt, const struct GNUNET_PeerIdentity *peer)
1427 struct GNUNET_CADET_ChannelCreateMessage msg;
1429 msg.header.size = htons (sizeof (msg));
1430 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1431 msg.channel_id = htonl (id);
1433 msg.opt = htonl (opt);
1435 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1436 &msg.header, GNUNET_NO);
1441 * Build a local channel NACK message and send it to a local client.
1443 * @param c Client to whom send the NACK.
1444 * @param id Channel ID to use
1447 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1449 struct GNUNET_CADET_LocalAck msg;
1451 LOG (GNUNET_ERROR_TYPE_DEBUG,
1452 "send local nack on %X towards %p\n",
1455 msg.header.size = htons (sizeof (msg));
1456 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1457 msg.channel_id = htonl (id);
1458 GNUNET_SERVER_notification_context_unicast (nc,
1466 * Notify a client that a channel is no longer valid.
1469 * @param id ID of the channel that is destroyed.
1472 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1474 struct GNUNET_CADET_ChannelDestroyMessage msg;
1481 if (GNUNET_YES == c->shutting_down)
1483 msg.header.size = htons (sizeof (msg));
1484 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1485 msg.channel_id = htonl (id);
1486 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1487 &msg.header, GNUNET_NO);
1492 * Modify the cadet message ID from global to local and send to client.
1494 * @param c Client to send to.
1495 * @param msg Message to modify and send.
1496 * @param id Channel ID to use (c can be both owner and client).
1499 GML_send_data (struct CadetClient *c,
1500 const struct GNUNET_CADET_Data *msg,
1501 CADET_ChannelNumber id)
1503 struct GNUNET_CADET_LocalData *copy;
1504 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1505 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1507 if (size < sizeof (struct GNUNET_MessageHeader))
1509 GNUNET_break_op (0);
1517 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1518 GNUNET_memcpy (©[1], &msg[1], size);
1519 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1520 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1521 copy->id = htonl (id);
1522 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1523 ©->header, GNUNET_NO);
1528 * Get the static string to represent a client.
1532 * @return Static string for the client.
1535 GML_2s (const struct CadetClient *c)
1537 static char buf[32];
1539 SPRINTF (buf, "%u", c->id);