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);
227 * Handler for client disconnection
230 * @param client identification of the client; NULL
231 * for the last call when the server is destroyed
234 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
236 struct CadetClient *c;
238 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
241 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
245 c = GML_client_get (client);
248 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
250 GNUNET_SERVER_client_drop (c->handle);
251 c->shutting_down = GNUNET_YES;
252 if (NULL != c->own_channels)
254 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
255 &channel_destroy_iterator, c);
256 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
259 if (NULL != c->incoming_channels)
261 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
262 &channel_destroy_iterator, c);
263 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
266 if (NULL != c->ports)
268 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
269 &client_release_ports, c);
270 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
272 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
273 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
274 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
279 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
287 * Handler for new clients
290 * @param client identification of the client
291 * @param message the actual message, which includes messages the client wants
294 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
295 const struct GNUNET_MessageHeader *message)
297 struct GNUNET_CADET_ClientConnect *cc_msg;
298 struct CadetClient *c;
303 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
304 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
306 /* Check data sanity */
307 size = ntohs (message->size) - sizeof (struct GNUNET_CADET_ClientConnect);
308 cc_msg = (struct GNUNET_CADET_ClientConnect *) message;
309 if (0 != (size % sizeof (uint32_t)))
312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
315 size /= sizeof (uint32_t);
317 /* Initialize new client structure */
318 c = GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
326 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
327 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
332 p = (uint32_t *) &cc_msg[1];
333 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
334 for (i = 0; i < size; i++)
337 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
339 /* store in client's hashmap */
340 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
342 /* store in global hashmap */
343 /* FIXME only allow one client to have the port open,
344 * have a backup hashmap with waiting clients */
345 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
346 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
350 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
351 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
352 GNUNET_SERVER_notification_context_add (nc, client);
353 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
355 GNUNET_SERVER_receive_done (client, GNUNET_OK);
356 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
361 * Handler for requests of new tunnels
363 * @param cls Closure.
364 * @param client Identification of the client.
365 * @param message The actual message.
368 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
369 const struct GNUNET_MessageHeader *message)
371 struct CadetClient *c;
373 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
374 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
376 /* Sanity check for client registration */
377 if (NULL == (c = GML_client_get (client)))
380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
383 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
385 /* Message size sanity check */
386 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
394 GCCH_handle_local_create (c,
395 (struct GNUNET_CADET_ChannelMessage *) message))
397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
401 GNUNET_SERVER_receive_done (client, GNUNET_OK);
407 * Handler for requests of deleting tunnels
410 * @param client identification of the client
411 * @param message the actual message
414 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
415 const struct GNUNET_MessageHeader *message)
417 struct GNUNET_CADET_ChannelMessage *msg;
418 struct CadetClient *c;
419 struct CadetChannel *ch;
420 CADET_ChannelNumber chid;
422 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
424 /* Sanity check for client registration */
425 if (NULL == (c = GML_client_get (client)))
428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
431 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
433 /* Message sanity check */
434 if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size))
437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
441 msg = (struct GNUNET_CADET_ChannelMessage *) message;
443 /* Retrieve tunnel */
444 chid = ntohl (msg->channel_id);
445 LOG (GNUNET_ERROR_TYPE_DEBUG, " for channel %X\n", chid);
446 ch = GML_channel_get (c, chid);
449 LOG (GNUNET_ERROR_TYPE_DEBUG, " channel %X not found\n", chid);
450 GNUNET_STATISTICS_update (stats,
451 "# client destroy messages on unknown channel",
453 GNUNET_SERVER_receive_done (client, GNUNET_OK);
457 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV);
459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
465 * Handler for client traffic
468 * @param client identification of the client
469 * @param message the actual message
472 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
473 const struct GNUNET_MessageHeader *message)
475 const struct GNUNET_MessageHeader *payload;
476 struct GNUNET_CADET_LocalData *msg;
477 struct CadetClient *c;
478 struct CadetChannel *ch;
479 CADET_ChannelNumber chid;
482 size_t payload_claimed_size;
485 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
486 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
487 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got data from a client\n");
489 /* Sanity check for client registration */
490 if (NULL == (c = GML_client_get (client)))
493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
497 /* Sanity check for message size */
498 message_size = ntohs (message->size);
499 if (sizeof (struct GNUNET_CADET_LocalData)
500 + sizeof (struct GNUNET_MessageHeader) > message_size
501 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < message_size)
504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
508 /* Sanity check for payload size */
509 payload_size = message_size - sizeof (struct GNUNET_CADET_LocalData);
510 msg = (struct GNUNET_CADET_LocalData *) message;
511 payload = (struct GNUNET_MessageHeader *) &msg[1];
512 payload_claimed_size = ntohs (payload->size);
513 if (sizeof (struct GNUNET_MessageHeader) > payload_claimed_size
514 || GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size
515 || payload_claimed_size > payload_size)
517 LOG (GNUNET_ERROR_TYPE_WARNING,
518 "client claims to send %u bytes in %u payload\n",
519 payload_claimed_size, payload_size);
521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
525 chid = ntohl (msg->id);
526 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
528 /* Channel exists? */
529 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
530 ch = GML_channel_get (c, chid);
533 GNUNET_STATISTICS_update (stats,
534 "# client data messages on unknown channel",
536 GNUNET_SERVER_receive_done (client, GNUNET_OK);
540 if (GNUNET_OK != GCCH_handle_local_data (ch, c, fwd, payload, payload_size))
542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
546 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
554 * Handler for client's ACKs for payload traffic.
556 * @param cls Closure (unused).
557 * @param client Identification of the client.
558 * @param message The actual message.
561 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
562 const struct GNUNET_MessageHeader *message)
564 struct GNUNET_CADET_LocalAck *msg;
565 struct CadetChannel *ch;
566 struct CadetClient *c;
567 CADET_ChannelNumber chid;
570 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
573 /* Sanity check for client registration */
574 if (NULL == (c = GML_client_get (client)))
577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
580 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
582 msg = (struct GNUNET_CADET_LocalAck *) message;
584 /* Channel exists? */
585 chid = ntohl (msg->channel_id);
586 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
587 ch = GML_channel_get (c, chid);
588 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid);
592 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
593 GNUNET_STATISTICS_update (stats,
594 "# client ack messages on unknown channel",
596 GNUNET_SERVER_receive_done (client, GNUNET_OK);
600 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
601 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
602 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
604 GCCH_handle_local_ack (ch, fwd);
605 GNUNET_SERVER_receive_done (client, GNUNET_OK);
612 * Iterator over all peers to send a monitoring client info about each peer.
614 * @param cls Closure ().
615 * @param peer Peer ID (tunnel remote peer).
616 * @param value Peer info.
618 * @return #GNUNET_YES, to keep iterating.
621 get_all_peers_iterator (void *cls,
622 const struct GNUNET_PeerIdentity * peer,
625 struct GNUNET_SERVER_Client *client = cls;
626 struct CadetPeer *p = value;
627 struct GNUNET_CADET_LocalInfoPeer msg;
629 msg.header.size = htons (sizeof (msg));
630 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
631 msg.destination = *peer;
632 msg.paths = htons (GCP_count_paths (p));
633 msg.tunnel = htons (NULL != GCP_get_tunnel (p));
635 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about peer %s\n",
638 GNUNET_SERVER_notification_context_unicast (nc, client,
639 &msg.header, GNUNET_NO);
645 * Iterator over all peers to dump info for each peer.
647 * @param cls Closure (unused).
648 * @param peer Peer ID (tunnel remote peer).
649 * @param value Peer info.
651 * @return #GNUNET_YES, to keep iterating.
654 show_peer_iterator (void *cls,
655 const struct GNUNET_PeerIdentity * peer,
658 struct CadetPeer *p = value;
659 struct CadetTunnel *t;
661 GCP_debug (p, GNUNET_ERROR_TYPE_ERROR);
663 t = GCP_get_tunnel (p);
665 GCT_debug (t, GNUNET_ERROR_TYPE_ERROR);
667 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
674 * Handler for client's INFO PEERS request.
676 * @param cls Closure (unused).
677 * @param client Identification of the client.
678 * @param message The actual message.
681 handle_get_peers (void *cls, struct GNUNET_SERVER_Client *client,
682 const struct GNUNET_MessageHeader *message)
684 struct CadetClient *c;
685 struct GNUNET_MessageHeader reply;
687 /* Sanity check for client registration */
688 if (NULL == (c = GML_client_get (client)))
691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "Received get peers request from client %u (%p)\n",
699 GCP_iterate_all (get_all_peers_iterator, client);
700 reply.size = htons (sizeof (reply));
701 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
702 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
704 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "Get peers request from client %u completed\n", c->id);
706 GNUNET_SERVER_receive_done (client, GNUNET_OK);
711 * Handler for client's SHOW_PEER request.
713 * @param cls Closure (unused).
714 * @param client Identification of the client.
715 * @param message The actual message.
718 handle_show_peer (void *cls, struct GNUNET_SERVER_Client *client,
719 const struct GNUNET_MessageHeader *message)
721 const struct GNUNET_CADET_LocalInfo *msg;
722 struct GNUNET_CADET_LocalInfoPeer *resp;
724 struct CadetClient *c;
727 /* Sanity check for client registration */
728 if (NULL == (c = GML_client_get (client)))
731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
735 msg = (struct GNUNET_CADET_LocalInfo *) message;
736 LOG (GNUNET_ERROR_TYPE_INFO,
737 "Received peer info request from client %u for peer %s\n",
738 c->id, GNUNET_i2s_full (&msg->peer));
740 p = GCP_get (&msg->peer);
743 /* We don't know the peer */
744 struct GNUNET_CADET_LocalInfoPeer warn;
746 LOG (GNUNET_ERROR_TYPE_INFO, "Peer %s unknown %u\n",
747 GNUNET_i2s_full (&msg->peer), sizeof (warn));
748 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
749 warn.header.size = htons (sizeof (warn));
750 warn.destination = msg->peer;
751 warn.paths = htons (0);
752 warn.tunnel = htons (NULL != GCP_get_tunnel (p));
754 GNUNET_SERVER_notification_context_unicast (nc, client,
757 GNUNET_SERVER_receive_done (client, GNUNET_OK);
761 size = sizeof (struct GNUNET_CADET_LocalInfoPeer);
762 // size += c_n * sizeof (struct GNUNET_CADET_Hash);
764 resp = GNUNET_malloc (size);
765 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
766 resp->header.size = htons (size);
767 resp->destination = msg->peer;
768 resp->paths = htons (0);
769 resp->tunnel = htons (0);
771 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
772 &resp->header, GNUNET_NO);
775 LOG (GNUNET_ERROR_TYPE_INFO, "Show peer request from client %u completed.\n");
776 GNUNET_SERVER_receive_done (client, GNUNET_OK);
781 * Iterator over all tunnels to send a monitoring client info about each tunnel.
783 * @param cls Closure ().
784 * @param peer Peer ID (tunnel remote peer).
785 * @param value Tunnel info.
787 * @return #GNUNET_YES, to keep iterating.
790 get_all_tunnels_iterator (void *cls,
791 const struct GNUNET_PeerIdentity * peer,
794 struct GNUNET_SERVER_Client *client = cls;
795 struct CadetTunnel *t = value;
796 struct GNUNET_CADET_LocalInfoTunnel msg;
798 msg.header.size = htons (sizeof (msg));
799 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
800 msg.destination = *peer;
801 msg.channels = htonl (GCT_count_channels (t));
802 msg.connections = htonl (GCT_count_any_connections (t));
803 msg.cstate = htons ((uint16_t) GCT_get_cstate (t));
804 msg.estate = htons ((uint16_t) GCT_get_estate (t));
806 LOG (GNUNET_ERROR_TYPE_DEBUG, "sending info about tunnel ->%s\n",
809 GNUNET_SERVER_notification_context_unicast (nc, client,
810 &msg.header, GNUNET_NO);
816 * Handler for client's INFO TUNNELS request.
818 * @param cls Closure (unused).
819 * @param client Identification of the client.
820 * @param message The actual message.
823 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
824 const struct GNUNET_MessageHeader *message)
826 struct CadetClient *c;
827 struct GNUNET_MessageHeader reply;
829 /* Sanity check for client registration */
830 if (NULL == (c = GML_client_get (client)))
833 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
837 LOG (GNUNET_ERROR_TYPE_DEBUG,
838 "Received get tunnels request from client %u (%p)\n",
841 GCT_iterate_all (get_all_tunnels_iterator, client);
842 reply.size = htons (sizeof (reply));
843 reply.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
844 GNUNET_SERVER_notification_context_unicast (nc, client, &reply, GNUNET_NO);
846 LOG (GNUNET_ERROR_TYPE_DEBUG,
847 "Get tunnels request from client %u completed\n", c->id);
848 GNUNET_SERVER_receive_done (client, GNUNET_OK);
853 iter_connection (void *cls, struct CadetConnection *c)
855 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
856 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1];
858 h[msg->connections] = *(GCC_get_id (c));
863 iter_channel (void *cls, struct CadetChannel *ch)
865 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
866 struct GNUNET_HashCode *h = (struct GNUNET_HashCode *) &msg[1];
867 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections];
869 chn[msg->channels] = GCCH_get_id (ch);
875 * Handler for client's SHOW_TUNNEL request.
877 * @param cls Closure (unused).
878 * @param client Identification of the client.
879 * @param message The actual message.
882 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
883 const struct GNUNET_MessageHeader *message)
885 const struct GNUNET_CADET_LocalInfo *msg;
886 struct GNUNET_CADET_LocalInfoTunnel *resp;
887 struct CadetClient *c;
888 struct CadetTunnel *t;
893 /* Sanity check for client registration */
894 if (NULL == (c = GML_client_get (client)))
897 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
901 msg = (struct GNUNET_CADET_LocalInfo *) message;
902 LOG (GNUNET_ERROR_TYPE_INFO,
903 "Received tunnel info request from client %u for tunnel %s\n",
904 c->id, GNUNET_i2s_full(&msg->peer));
906 t = GCP_get_tunnel (GCP_get (&msg->peer));
909 /* We don't know the tunnel */
910 struct GNUNET_CADET_LocalInfoTunnel warn;
912 LOG (GNUNET_ERROR_TYPE_INFO, "Tunnel %s unknown %u\n",
913 GNUNET_i2s_full(&msg->peer), sizeof (warn));
914 warn.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
915 warn.header.size = htons (sizeof (warn));
916 warn.destination = msg->peer;
917 warn.channels = htonl (0);
918 warn.connections = htonl (0);
919 warn.cstate = htons (0);
920 warn.estate = htons (0);
922 GNUNET_SERVER_notification_context_unicast (nc, client,
925 GNUNET_SERVER_receive_done (client, GNUNET_OK);
929 /* Initialize context */
930 ch_n = GCT_count_channels (t);
931 c_n = GCT_count_any_connections (t);
933 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
934 size += c_n * sizeof (struct GNUNET_CADET_Hash);
935 size += ch_n * sizeof (CADET_ChannelNumber);
937 resp = GNUNET_malloc (size);
938 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
939 resp->header.size = htons (size);
940 GCT_iterate_connections (t, &iter_connection, resp);
941 GCT_iterate_channels (t, &iter_channel, resp);
942 /* Do not interleave with iterators, iter_channel needs conn in HBO */
943 resp->destination = msg->peer;
944 resp->connections = htonl (resp->connections);
945 resp->channels = htonl (resp->channels);
946 resp->cstate = htons (GCT_get_cstate (t));
947 resp->estate = htons (GCT_get_estate (t));
948 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
949 &resp->header, GNUNET_NO);
952 LOG (GNUNET_ERROR_TYPE_INFO,
953 "Show tunnel request from client %u completed. %u conn, %u ch\n",
955 GNUNET_SERVER_receive_done (client, GNUNET_OK);
960 * Handler for client's INFO_DUMP request.
962 * @param cls Closure (unused).
963 * @param client Identification of the client.
964 * @param message The actual message.
967 handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client,
968 const struct GNUNET_MessageHeader *message)
970 struct CadetClient *c;
972 /* Sanity check for client registration */
973 if (NULL == (c = GML_client_get (client)))
976 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
980 LOG (GNUNET_ERROR_TYPE_INFO, "Received dump info request from client %u\n",
983 LOG (GNUNET_ERROR_TYPE_ERROR,
984 "*************************** DUMP START ***************************\n");
986 GCP_iterate_all (&show_peer_iterator, NULL);
988 LOG (GNUNET_ERROR_TYPE_ERROR,
989 "**************************** DUMP END ****************************\n");
991 GNUNET_SERVER_receive_done (client, GNUNET_OK);
996 * Functions to handle messages from clients
998 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
999 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0},
1000 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE,
1001 sizeof (struct GNUNET_CADET_ChannelMessage)},
1002 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1003 sizeof (struct GNUNET_CADET_ChannelMessage)},
1004 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
1005 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1006 sizeof (struct GNUNET_CADET_LocalAck)},
1007 {&handle_get_peers, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1008 sizeof (struct GNUNET_MessageHeader)},
1009 {&handle_show_peer, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1010 sizeof (struct GNUNET_CADET_LocalInfo)},
1011 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1012 sizeof (struct GNUNET_MessageHeader)},
1013 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1014 sizeof (struct GNUNET_CADET_LocalInfo)},
1015 {&handle_info_dump, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1016 sizeof (struct GNUNET_MessageHeader)},
1022 /******************************************************************************/
1023 /******************************** API ***********************************/
1024 /******************************************************************************/
1027 * Initialize server subsystem.
1029 * @param handle Server handle.
1032 GML_init (struct GNUNET_SERVER_Handle *handle)
1034 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
1035 server_handle = handle;
1036 GNUNET_SERVER_suspend (server_handle);
1037 ports = GNUNET_CONTAINER_multihashmap32_create (32);
1042 * Install server (service) handlers and start listening to clients.
1047 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
1048 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
1049 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
1051 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
1053 clients_head = NULL;
1054 clients_tail = NULL;
1056 GNUNET_SERVER_resume (server_handle);
1068 GNUNET_SERVER_notification_context_destroy (nc);
1075 * Get a channel from a client.
1077 * @param c Client to check.
1078 * @param chid Channel ID, must be local (> 0x800...).
1080 * @return non-NULL if channel exists in the clients lists
1082 struct CadetChannel *
1083 GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1085 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1087 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI))
1089 GNUNET_break_op (0);
1090 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1094 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1095 map = c->incoming_channels;
1096 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1097 map = c->own_channels;
1106 LOG (GNUNET_ERROR_TYPE_DEBUG,
1107 "Client %s does no t have a valid map for CHID %X\n",
1111 return GNUNET_CONTAINER_multihashmap32_get (map, chid);
1116 * Add a channel to a client
1118 * @param client Client.
1119 * @param chid Channel ID.
1120 * @param ch Channel.
1123 GML_channel_add (struct CadetClient *client,
1125 struct CadetChannel *ch)
1127 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1128 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
1129 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1130 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1131 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1132 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1139 * Remove a channel from a client.
1141 * @param client Client.
1142 * @param chid Channel ID.
1143 * @param ch Channel.
1146 GML_channel_remove (struct CadetClient *client,
1148 struct CadetChannel *ch)
1150 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid)
1151 GNUNET_break (GNUNET_YES ==
1152 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1154 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1155 GNUNET_break (GNUNET_YES ==
1156 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1164 * Get the tunnel's next free local channel ID.
1168 * @return LID of a channel free to use.
1171 GML_get_next_chid (struct CadetClient *c)
1173 CADET_ChannelNumber chid;
1175 while (NULL != GML_channel_get (c, c->next_chid))
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
1178 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1180 chid = c->next_chid;
1181 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
1188 * Check if client has registered with the service and has not disconnected
1190 * @param client the client to check
1192 * @return non-NULL if client exists in the global DLL
1194 struct CadetClient *
1195 GML_client_get (struct GNUNET_SERVER_Client *client)
1197 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient);
1201 * Find a client that has opened a port
1203 * @param port Port to check.
1205 * @return non-NULL if a client has the port.
1207 struct CadetClient *
1208 GML_client_get_by_port (uint32_t port)
1210 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
1215 * Deletes a channel from a client (either owner or destination).
1217 * @param c Client whose tunnel to delete.
1218 * @param ch Channel which should be deleted.
1219 * @param id Channel ID.
1222 GML_client_delete_channel (struct CadetClient *c,
1223 struct CadetChannel *ch,
1224 CADET_ChannelNumber id)
1228 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id)
1230 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1232 if (GNUNET_YES != res)
1233 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1235 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1237 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1239 if (GNUNET_YES != res)
1240 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1249 * Build a local ACK message and send it to a local client, if needed.
1251 * If the client was already allowed to send data, do nothing.
1253 * @param c Client to whom send the ACK.
1254 * @param id Channel ID to use
1257 GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1259 struct GNUNET_CADET_LocalAck msg;
1261 LOG (GNUNET_ERROR_TYPE_DEBUG,
1262 "send local %s ack on %X towards %p\n",
1263 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1265 msg.header.size = htons (sizeof (msg));
1266 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1267 msg.channel_id = htonl (id);
1268 GNUNET_SERVER_notification_context_unicast (nc,
1278 * Notify the client that a new incoming channel was created.
1280 * @param c Client to notify.
1281 * @param id Channel ID.
1282 * @param port Channel's destination port.
1283 * @param opt Options (bit array).
1284 * @param peer Origin peer.
1287 GML_send_channel_create (struct CadetClient *c,
1288 uint32_t id, uint32_t port, uint32_t opt,
1289 const struct GNUNET_PeerIdentity *peer)
1291 struct GNUNET_CADET_ChannelMessage msg;
1293 msg.header.size = htons (sizeof (msg));
1294 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1295 msg.channel_id = htonl (id);
1296 msg.port = htonl (port);
1297 msg.opt = htonl (opt);
1299 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1300 &msg.header, GNUNET_NO);
1305 * Build a local channel NACK message and send it to a local client.
1307 * @param c Client to whom send the NACK.
1308 * @param id Channel ID to use
1311 GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1313 struct GNUNET_CADET_LocalAck msg;
1315 LOG (GNUNET_ERROR_TYPE_DEBUG,
1316 "send local nack on %X towards %p\n",
1319 msg.header.size = htons (sizeof (msg));
1320 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK);
1321 msg.channel_id = htonl (id);
1322 GNUNET_SERVER_notification_context_unicast (nc,
1330 * Notify a client that a channel is no longer valid.
1333 * @param id ID of the channel that is destroyed.
1336 GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1338 struct GNUNET_CADET_ChannelMessage msg;
1345 if (GNUNET_YES == c->shutting_down)
1347 msg.header.size = htons (sizeof (msg));
1348 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1349 msg.channel_id = htonl (id);
1350 msg.port = htonl (0);
1351 memset (&msg.peer, 0, sizeof (msg.peer));
1352 msg.opt = htonl (0);
1353 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1354 &msg.header, GNUNET_NO);
1359 * Modify the cadet message ID from global to local and send to client.
1361 * @param c Client to send to.
1362 * @param msg Message to modify and send.
1363 * @param id Channel ID to use (c can be both owner and client).
1366 GML_send_data (struct CadetClient *c,
1367 const struct GNUNET_CADET_Data *msg,
1368 CADET_ChannelNumber id)
1370 struct GNUNET_CADET_LocalData *copy;
1371 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data);
1372 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1374 if (size < sizeof (struct GNUNET_MessageHeader))
1376 GNUNET_break_op (0);
1384 copy = (struct GNUNET_CADET_LocalData *) cbuf;
1385 memcpy (©[1], &msg[1], size);
1386 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1387 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1388 copy->id = htonl (id);
1389 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1390 ©->header, GNUNET_NO);
1395 * Get the static string to represent a client.
1399 * @return Static string for the client.
1402 GML_2s (const struct CadetClient *c)
1404 static char buf[32];
1406 SPRINTF (buf, "%u", c->id);