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 "mesh_protocol_enc.h" // GNUNET_MESH_Data is shared
30 #include "gnunet-service-mesh_local.h"
31 #include "gnunet-service-mesh_tunnel.h"
33 #define LOG(level, ...) GNUNET_log_from(level,"mesh-loc",__VA_ARGS__)
35 /******************************************************************************/
36 /******************************** STRUCTS **********************************/
37 /******************************************************************************/
40 * Struct containing information about a client of the service
42 * TODO: add a list of 'waiting' ports
49 struct MeshClient *next;
54 struct MeshClient *prev;
57 * Tunnels that belong to this client, indexed by local id
59 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
62 * Tunnels this client has accepted, indexed by incoming local id
64 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
67 * Channel ID for the next incoming channel.
69 MESH_ChannelNumber next_chid;
72 * Handle to communicate with the client
74 struct GNUNET_SERVER_Client *handle;
77 * Ports that this client has declared interest in.
78 * Indexed by port, contains *Client.
80 struct GNUNET_CONTAINER_MultiHashMap32 *ports;
83 * Whether the client is active or shutting down (don't send confirmations
84 * to a client that is shutting down.
89 * ID of the client, mainly for debug messages
94 /******************************************************************************/
95 /******************************* GLOBALS ***********************************/
96 /******************************************************************************/
99 * Global handle to the statistics service.
101 extern struct GNUNET_STATISTICS_Handle *stats;
104 * Handle to server lib.
106 static struct GNUNET_SERVER_Handle *server_handle;
109 * DLL with all the clients, head.
111 static struct MeshClient *clients_head;
114 * DLL with all the clients, tail.
116 static struct MeshClient *clients_tail;
119 * Next ID to assign to a client.
121 unsigned int next_client_id;
124 * All ports clients of this peer have opened.
126 static struct GNUNET_CONTAINER_MultiHashMap32 *ports;
129 * Notification context, to send messages to local clients.
131 static struct GNUNET_SERVER_NotificationContext *nc;
134 /******************************************************************************/
135 /******************************** STATIC ***********************************/
136 /******************************************************************************/
139 * Remove client's ports from the global hashmap on disconnect.
141 * @param cls Closure (unused).
143 * @param value Client structure.
145 * @return GNUNET_OK, keep iterating.
148 client_release_ports (void *cls,
154 res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value);
155 if (GNUNET_YES != res)
158 LOG (GNUNET_ERROR_TYPE_WARNING,
159 "Port %u by client %p was not registered.\n",
167 /******************************************************************************/
168 /******************************** HANDLES ***********************************/
169 /******************************************************************************/
173 * Handler for client connection.
175 * @param cls Closure (unused).
176 * @param client Client handler.
179 handle_client_connect (void *cls, struct GNUNET_SERVER_Client *client)
181 struct MeshClient *c;
185 c = GNUNET_new (struct MeshClient);
187 c->id = next_client_id++; /* overflow not important: just for debug */
188 c->next_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
189 GNUNET_SERVER_client_keep (client);
190 GNUNET_SERVER_client_set_user_context (client, c);
191 GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c);
196 * Handler for client disconnection
199 * @param client identification of the client; NULL
200 * for the last call when the server is destroyed
203 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
205 struct MeshClient *c;
207 LOG (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client);
210 LOG (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
214 c = client_get (client);
217 LOG (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n",
219 GNUNET_SERVER_client_drop (c->handle);
220 c->shutting_down = GNUNET_YES;
221 if (NULL != c->own_channels)
223 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
224 &channel_destroy_iterator, c);
225 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
228 if (NULL != c->incoming_channels)
230 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
231 &channel_destroy_iterator, c);
232 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
235 if (NULL != c->ports)
237 GNUNET_CONTAINER_multihashmap32_iterate (c->ports,
238 &client_release_ports, c);
239 GNUNET_CONTAINER_multihashmap32_destroy (c->ports);
241 GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c);
242 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
243 LOG (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c);
248 LOG (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n");
250 LOG (GNUNET_ERROR_TYPE_DEBUG, "done!\n");
256 * Handler for new clients
259 * @param client identification of the client
260 * @param message the actual message, which includes messages the client wants
263 handle_new_client (void *cls, struct GNUNET_SERVER_Client *client,
264 const struct GNUNET_MessageHeader *message)
266 struct GNUNET_MESH_ClientConnect *cc_msg;
267 struct MeshClient *c;
272 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client);
274 /* Check data sanity */
275 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
276 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
277 if (0 != (size % sizeof (uint32_t)))
280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
283 size /= sizeof (uint32_t);
285 /* Initialize new client structure */
286 c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
287 LOG (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id);
288 LOG (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size);
293 p = (uint32_t *) &cc_msg[1];
294 c->ports = GNUNET_CONTAINER_multihashmap32_create (size);
295 for (i = 0; i < size; i++)
298 LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32);
300 /* store in client's hashmap */
301 GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c,
302 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
303 /* store in global hashmap */
304 /* FIXME only allow one client to have the port open,
305 * have a backup hashmap with waiting clients */
306 GNUNET_CONTAINER_multihashmap32_put (ports, u32, c,
307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
311 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
312 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
313 GNUNET_SERVER_notification_context_add (nc, client);
314 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
316 GNUNET_SERVER_receive_done (client, GNUNET_OK);
317 LOG (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
322 * Handler for requests of new tunnels
324 * @param cls Closure.
325 * @param client Identification of the client.
326 * @param message The actual message.
329 handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
330 const struct GNUNET_MessageHeader *message)
332 struct GNUNET_MESH_ChannelMessage *msg;
333 struct MeshPeer *peer;
334 struct MeshTunnel2 *t;
335 struct MeshChannel *ch;
336 struct MeshClient *c;
337 MESH_ChannelNumber chid;
339 LOG (GNUNET_ERROR_TYPE_DEBUG, "new channel requested\n");
341 /* Sanity check for client registration */
342 if (NULL == (c = client_get (client)))
345 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
348 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
350 /* Message size sanity check */
351 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
358 msg = (struct GNUNET_MESH_ChannelMessage *) message;
359 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
360 GNUNET_i2s (&msg->peer), ntohl (msg->port));
361 chid = ntohl (msg->channel_id);
363 /* Sanity check for duplicate channel IDs */
364 if (NULL != channel_get_by_local_id (c, chid))
367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
371 peer = peer_get (&msg->peer);
372 if (NULL == peer->tunnel)
374 peer->tunnel = tunnel_new ();
375 peer->tunnel->peer = peer;
376 if (peer->id == myid)
378 tunnel_change_state (peer->tunnel, MESH_TUNNEL_READY);
388 ch = channel_new (t, c, chid);
392 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
395 ch->port = ntohl (msg->port);
396 channel_set_options (ch, ntohl (msg->opt));
398 /* In unreliable channels, we'll use the DLL to buffer BCK data */
399 ch->root_rel = GNUNET_new (struct MeshChannelReliability);
400 ch->root_rel->ch = ch;
401 ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
403 LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s[%x]:%u (%x)\n",
404 peer2s (t->peer), ch->gid, ch->port, ch->lid_root);
406 /* Send create channel */
408 struct GNUNET_MESH_ChannelCreate msgcc;
410 msgcc.header.size = htons (sizeof (msgcc));
411 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
412 msgcc.chid = htonl (ch->gid);
413 msgcc.port = msg->port;
414 msgcc.opt = msg->opt;
416 GMT_queue_data (t, ch, &msgcc.header, GNUNET_YES);
419 GNUNET_SERVER_receive_done (client, GNUNET_OK);
425 * Handler for requests of deleting tunnels
428 * @param client identification of the client
429 * @param message the actual message
432 handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
433 const struct GNUNET_MessageHeader *message)
435 struct GNUNET_MESH_ChannelMessage *msg;
436 struct MeshClient *c;
437 struct MeshChannel *ch;
438 struct MeshTunnel2 *t;
439 MESH_ChannelNumber chid;
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Got a DESTROY CHANNEL from client!\n");
444 /* Sanity check for client registration */
445 if (NULL == (c = client_get (client)))
448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
451 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
453 /* Message sanity check */
454 if (sizeof (struct GNUNET_MESH_ChannelMessage) != ntohs (message->size))
457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
461 msg = (struct GNUNET_MESH_ChannelMessage *) message;
463 /* Retrieve tunnel */
464 chid = ntohl (msg->channel_id);
465 ch = channel_get_by_local_id (c, chid);
468 LOG (GNUNET_ERROR_TYPE_ERROR, " channel %X not found\n", chid);
470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
474 /* Cleanup after the tunnel */
475 client_delete_channel (c, ch);
476 if (c == ch->dest && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
480 else if (c == ch->root && GNUNET_MESH_LOCAL_CHANNEL_ID_SERV > chid)
486 LOG (GNUNET_ERROR_TYPE_ERROR,
487 " channel %X client %p (%p, %p)\n",
488 chid, c, ch->root, ch->dest);
493 channel_destroy (ch);
494 tunnel_destroy_if_empty (t);
496 GNUNET_SERVER_receive_done (client, GNUNET_OK);
502 * Handler for client traffic
505 * @param client identification of the client
506 * @param message the actual message
509 handle_data (void *cls, struct GNUNET_SERVER_Client *client,
510 const struct GNUNET_MessageHeader *message)
512 struct GNUNET_MESH_LocalData *msg;
513 struct MeshClient *c;
514 struct MeshChannel *ch;
515 struct MeshChannelReliability *rel;
516 MESH_ChannelNumber chid;
520 LOG (GNUNET_ERROR_TYPE_DEBUG,
521 "Got data from a client!\n");
523 /* Sanity check for client registration */
524 if (NULL == (c = GML_client_get (client)))
527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
530 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
532 msg = (struct GNUNET_MESH_LocalData *) message;
534 /* Sanity check for message size */
535 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData);
536 if (size < sizeof (struct GNUNET_MessageHeader))
539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
543 /* Channel exists? */
544 chid = ntohl (msg->id);
545 fwd = chid < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
546 ch = GML_channel_get (c, chid);
550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
554 rel = fwd ? ch->root_rel : ch->dest_rel;
555 rel->client_ready = GNUNET_NO;
557 /* Ok, everything is correct, send the message. */
559 struct GNUNET_MESH_Data *payload;
560 uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
561 unsigned char cbuf[p2p_size];
563 payload = (struct GNUNET_MESH_Data *) cbuf;
564 payload->mid = htonl (rel->mid_send);
566 memcpy (&payload[1], &msg[1], size);
567 payload->header.size = htons (p2p_size);
568 payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
569 payload->chid = htonl (ch->gid);
570 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
571 send_prebuilt_message_channel (&payload->header, ch, fwd);
573 if (GNUNET_YES == ch->reliable)
574 channel_save_copy (ch, &payload->header, fwd);
576 if (tunnel_get_buffer (ch->t, fwd) > 0)
577 send_local_ack (ch, fwd);
578 LOG (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
579 GNUNET_SERVER_receive_done (client, GNUNET_OK);
586 * Handler for client's ACKs for payload traffic.
588 * @param cls Closure (unused).
589 * @param client Identification of the client.
590 * @param message The actual message.
593 handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
594 const struct GNUNET_MessageHeader *message)
596 struct GNUNET_MESH_LocalAck *msg;
597 struct MeshChannel *ch;
598 struct MeshClient *c;
599 MESH_ChannelNumber chid;
602 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
604 /* Sanity check for client registration */
605 if (NULL == (c = GML_client_get (client)))
608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
611 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
613 msg = (struct GNUNET_MESH_LocalAck *) message;
615 /* Channel exists? */
616 chid = ntohl (msg->channel_id);
617 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid);
618 ch = GML_channel_get (c, chid);
619 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
623 LOG (GNUNET_ERROR_TYPE_WARNING, "Channel %X unknown.\n", chid);
624 LOG (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
629 /* If client is root, the ACK is going FWD, therefore this is "BCK". */
630 /* If client is dest, the ACK is going BCK, therefore this is "FWD" */
631 fwd = chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
634 GMCH_handle_local_ack (ch, fwd);
641 * Iterator over all tunnels to send a monitoring client info about each tunnel.
643 * @param cls Closure (client handle).
644 * @param key Key (hashed tunnel ID, unused).
645 * @param value Tunnel info.
647 * @return GNUNET_YES, to keep iterating.
650 // monitor_all_tunnels_iterator (void *cls,
651 // const struct GNUNET_HashCode * key,
654 // struct GNUNET_SERVER_Client *client = cls;
655 // struct MeshChannel *ch = value;
656 // struct GNUNET_MESH_LocalMonitor *msg;
658 // msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
659 // msg->channel_id = htonl (ch->gid);
660 // msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
661 // msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
663 // LOG (GNUNET_ERROR_TYPE_INFO,
664 // "* sending info about tunnel %s\n",
665 // GNUNET_i2s (&msg->owner));
667 // GNUNET_SERVER_notification_context_unicast (nc, client,
668 // &msg->header, GNUNET_NO);
669 // return GNUNET_YES;
674 * Handler for client's MONITOR request.
676 * @param cls Closure (unused).
677 * @param client Identification of the client.
678 * @param message The actual message.
681 handle_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
682 const struct GNUNET_MessageHeader *message)
684 struct MeshClient *c;
686 /* Sanity check for client registration */
687 if (NULL == (c = GML_client_get (client)))
690 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
694 LOG (GNUNET_ERROR_TYPE_INFO,
695 "Received get tunnels request from client %u\n",
697 // GNUNET_CONTAINER_multihashmap_iterate (tunnels,
698 // monitor_all_tunnels_iterator,
700 LOG (GNUNET_ERROR_TYPE_INFO,
701 "Get tunnels request from client %u completed\n",
703 GNUNET_SERVER_receive_done (client, GNUNET_OK);
708 * Handler for client's MONITOR_TUNNEL request.
710 * @param cls Closure (unused).
711 * @param client Identification of the client.
712 * @param message The actual message.
715 handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
716 const struct GNUNET_MessageHeader *message)
718 const struct GNUNET_MESH_LocalMonitor *msg;
719 struct GNUNET_MESH_LocalMonitor *resp;
720 struct MeshClient *c;
721 struct MeshChannel *ch;
723 /* Sanity check for client registration */
724 if (NULL == (c = GML_client_get (client)))
727 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
731 msg = (struct GNUNET_MESH_LocalMonitor *) message;
732 LOG (GNUNET_ERROR_TYPE_INFO,
733 "Received tunnel info request from client %u for tunnel %s[%X]\n",
736 ntohl (msg->channel_id));
737 // ch = channel_get (&msg->owner, ntohl (msg->channel_id));
741 /* We don't know the tunnel */
742 struct GNUNET_MESH_LocalMonitor warn;
745 GNUNET_SERVER_notification_context_unicast (nc, client,
748 GNUNET_SERVER_receive_done (client, GNUNET_OK);
752 /* Initialize context */
753 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
755 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
756 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
757 &resp->header, GNUNET_NO);
760 LOG (GNUNET_ERROR_TYPE_INFO,
761 "Monitor tunnel request from client %u completed\n",
763 GNUNET_SERVER_receive_done (client, GNUNET_OK);
768 * Functions to handle messages from clients
770 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
771 {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
772 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE,
773 sizeof (struct GNUNET_MESH_ChannelMessage)},
774 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY,
775 sizeof (struct GNUNET_MESH_ChannelMessage)},
776 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0},
777 {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
778 sizeof (struct GNUNET_MESH_LocalAck)},
779 {&handle_get_tunnels, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
780 sizeof (struct GNUNET_MessageHeader)},
781 {&handle_show_tunnel, NULL, GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
782 sizeof (struct GNUNET_MESH_LocalMonitor)},
788 /******************************************************************************/
789 /******************************** API ***********************************/
790 /******************************************************************************/
793 * Initialize server subsystem.
795 * @param handle Server handle.
798 GML_init (struct GNUNET_SERVER_Handle *handle)
800 server_handle = handle;
801 GNUNET_SERVER_suspend (server_handle);
802 ports = GNUNET_CONTAINER_multihashmap32_create (32);
807 * Install server (service) handlers and start listening to clients.
812 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
813 GNUNET_SERVER_connect_notify (server_handle, &handle_client_connect, NULL);
814 GNUNET_SERVER_disconnect_notify (server_handle, &handle_client_disconnect,
816 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
821 GNUNET_SERVER_resume (server_handle);
833 GNUNET_SERVER_notification_context_destroy (nc);
840 * Get a chennel from a client
842 * @param c the client to check
843 * @param chid Channel ID, must be local (> 0x800...)
845 * @return non-NULL if channel exists in the clients lists
848 GML_channel_get (struct MeshClient *c, MESH_ChannelNumber chid)
850 if (0 == (chid & GNUNET_MESH_LOCAL_CHANNEL_ID_CLI))
853 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
856 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
857 return GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels, chid);
858 return GNUNET_CONTAINER_multihashmap32_get (c->own_channels, chid);
863 * Add a channel to a client
865 * @param client Client.
866 * @param chid Channel ID.
870 GML_channel_add (struct MeshClient *client,
872 struct MeshChannel *ch)
874 if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV)
875 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch,
876 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
877 else if (chid >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
878 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
886 * Remove a channel from a client
888 * @param client Client.
889 * @param chid Channel ID.
893 GML_channel_remove (struct MeshClient *client,
895 struct MeshChannel *ch)
897 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= chid)
898 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels, chid, ch);
899 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= chid)
900 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels, chid, ch);
907 * Get the tunnel's next free local channel ID.
911 * @return LID of a channel free to use.
914 GML_get_next_chid (struct MeshClient *c)
916 MESH_ChannelNumber chid;
918 while (NULL != GML_channel_get (c, c->next_chid))
920 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid);
921 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
924 c->next_chid = (c->next_chid + 1) | GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
931 * Check if client has registered with the service and has not disconnected
933 * @param client the client to check
935 * @return non-NULL if client exists in the global DLL
938 GML_client_get (struct GNUNET_SERVER_Client *client)
940 return GNUNET_SERVER_client_get_user_context (client, struct MeshClient);
944 * Find a client that has opened a port
946 * @param port Port to check.
948 * @return non-NULL if a client has the port.
951 GML_client_get_by_port (uint32_t port)
953 return GNUNET_CONTAINER_multihashmap32_get (ports, port);
958 * Deletes a tunnel from a client (either owner or destination).
960 * @param c Client whose tunnel to delete.
961 * @param ch Channel which should be deleted.
962 * @param id Channel ID.
965 GML_client_delete_channel (struct MeshClient *c,
966 struct MeshChannel *ch,
967 MESH_ChannelNumber id)
971 if (GNUNET_MESH_LOCAL_CHANNEL_ID_SERV <= id)
973 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
975 if (GNUNET_YES != res)
976 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
978 else if (GNUNET_MESH_LOCAL_CHANNEL_ID_CLI <= id)
980 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
982 if (GNUNET_YES != res)
983 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
992 * Build a local ACK message and send it to a local client, if needed.
994 * If the client was already allowed to send data, do nothing.
996 * @param c Client to whom send the ACK.
997 * @param id Channel ID to use
1000 GML_send_ack (struct MeshClient *c, MESH_ChannelNumber id)
1002 struct GNUNET_MESH_LocalAck msg;
1004 LOG (GNUNET_ERROR_TYPE_DEBUG,
1005 "send local %s ack on %X towards %p\n",
1006 id < GNUNET_MESH_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c);
1008 msg.header.size = htons (sizeof (msg));
1009 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
1010 msg.channel_id = htonl (id);
1011 GNUNET_SERVER_notification_context_unicast (nc,
1020 * Notify the client that a new incoming channel was created.
1022 * @param ch Channel that was created.
1025 GML_send_channel_create (struct MeshClient *c,
1026 uint32_t id, uint32_t port, uint32_t opt,
1027 const struct GNUNET_PeerIdentity *peer)
1029 struct GNUNET_MESH_ChannelMessage msg;
1031 msg.header.size = htons (sizeof (msg));
1032 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1033 msg.channel_id = htonl (id);
1034 msg.port = htonl (port);
1035 msg.opt = htonl (opt);
1037 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1038 &msg.header, GNUNET_NO);
1043 * Notify a client that a channel is no longer valid.
1046 * @param id ID of the channel that is destroyed.
1049 GML_send_channel_destroy (struct MeshClient *c, uint32_t id)
1051 struct GNUNET_MESH_ChannelMessage msg;
1058 msg.header.size = htons (sizeof (msg));
1059 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1060 msg.channel_id = htonl (id);
1061 msg.port = htonl (0);
1062 memset (&msg.peer, 0, sizeof (msg.peer));
1063 msg.opt = htonl (0);
1064 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1065 &msg.header, GNUNET_NO);
1070 * Modify the mesh message ID from global to local and send to client.
1072 * @param c Client to send to.
1073 * @param msg Message to modify and send.
1074 * @param id Channel ID to use (c can be both owner and client).
1077 GML_send_data (struct MeshClient *c,
1078 const struct GNUNET_MESH_Data *msg,
1079 MESH_ChannelNumber id)
1081 struct GNUNET_MESH_LocalData *copy;
1082 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data);
1083 char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)];
1085 if (size < sizeof (struct GNUNET_MessageHeader))
1087 GNUNET_break_op (0);
1095 copy = (struct GNUNET_MESH_LocalData *) cbuf;
1096 memcpy (©[1], &msg[1], size);
1097 copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size);
1098 copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA);
1099 copy->id = htonl (id);
1100 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1101 ©->header, GNUNET_NO);
1106 * Get the static string to represent a client.
1110 * @return Static string for the client.
1113 GML_2s (const struct MeshClient *c)
1115 static char buf[32];
1117 sprintf (buf, "%u", c->id);