2 This file is part of GNUnet.
3 (C) 2001 - 2011 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.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "gnunet_dht_service.h"
52 #include "mesh_tunnel_tree.h"
54 /* TODO: move into configuration file */
55 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
56 GNUNET_TIME_UNIT_SECONDS,\
58 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
59 GNUNET_TIME_UNIT_SECONDS,\
62 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
63 GNUNET_TIME_UNIT_SECONDS,\
66 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
67 GNUNET_TIME_UNIT_SECONDS,\
69 #define DEFAULT_TTL 64
73 #define MESH_DEBUG_DHT GNUNET_YES
74 #define MESH_DEBUG_CONNECTION GNUNET_NO
76 #if MESH_DEBUG_CONNECTION
77 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
79 #define DEBUG_CONN(...)
83 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
85 #define DEBUG_DHT(...)
88 /******************************************************************************/
89 /************************ DATA STRUCTURES ****************************/
90 /******************************************************************************/
92 /** FWD declaration */
97 * Struct representing a piece of data being sent to other peers
101 /** Tunnel it belongs to. */
102 struct MeshTunnel *t;
104 /** In case of a multicast, task to allow a client to send more data if
105 * some neighbor is too slow. */
106 GNUNET_SCHEDULER_TaskIdentifier *task;
108 /** How many remaining neighbors we need to send this to. */
109 unsigned int *reference_counter;
111 /** Size of the data. */
120 * Struct containing all info possibly needed to build a package when called
123 struct MeshTransmissionDescriptor
125 /** ID of the tunnel this packet travels in */
126 struct MESH_TunnelID *origin;
128 /** Who was this message being sent to */
129 struct MeshPeerInfo *peer;
131 /** Ultimate destination of the packet */
132 GNUNET_PEER_Id destination;
134 /** Which handler was used to request the transmission */
135 unsigned int handler_n;
137 /** Data descriptor */
138 struct MeshData* mesh_data;
143 * Struct containing all information regarding a given peer
153 * Last time we heard from this peer
155 struct GNUNET_TIME_Absolute last_contact;
158 * Number of attempts to reconnect so far
160 int n_reconnect_attempts;
163 * Paths to reach the peer, ordered by ascending hop count
165 struct MeshPeerPath *path_head;
168 * Paths to reach the peer, ordered by ascending hop count
170 struct MeshPeerPath *path_tail;
173 * Handle to stop the DHT search for a path to this peer
175 struct GNUNET_DHT_GetHandle *dhtget;
178 * Closure given to the DHT GET
180 struct MeshPathInfo *dhtgetcls;
183 * Handles to stop queued transmissions for this peer
185 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
188 * Pointer to info stuctures used as cls for queued transmissions
190 void *infos[CORE_QUEUE_SIZE];
193 * Type of message being in each transmission
195 uint16_t types[CORE_QUEUE_SIZE];
198 * Array of tunnels this peer participates in
199 * (most probably a small amount, therefore not a hashmap)
200 * When the path to the peer changes, notify these tunnels to let them
201 * re-adjust their path trees.
203 struct MeshTunnel **tunnels;
206 * Number of tunnels this peers participates in
208 unsigned int ntunnels;
213 * Data scheduled to transmit (to local client or remote peer)
220 struct MeshQueue *next;
221 struct MeshQueue *prev;
224 * Target of the data (NULL if target is client)
226 struct MeshPeerInfo *peer;
229 * Client to send the data to (NULL if target is peer)
231 struct MeshClient *client;
234 * Size of the message to transmit
239 * How old is the data?
241 struct GNUNET_TIME_Absolute timestamp;
246 struct GNUNET_MessageHeader *data;
250 * Globally unique tunnel identification (owner + number)
251 * DO NOT USE OVER THE NETWORK
256 * Node that owns the tunnel
261 * Tunnel number to differentiate all the tunnels owned by the node oid
262 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
264 MESH_TunnelNumber tid;
268 struct MeshClient; /* FWD declaration */
271 * Struct containing all information regarding a tunnel
272 * For an intermediate node the improtant info used will be:
273 * - id Tunnel unique identification
274 * - paths[0] To know where to send it next
275 * - metainfo: ready, speeds, accounting
282 struct MESH_TunnelID id;
285 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
287 MESH_TunnelNumber local_tid;
290 * Local tunnel number for local destination clients (incoming number)
291 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
294 MESH_TunnelNumber local_tid_dest;
297 * ID of the last multicast packet seen/sent.
302 * Last time the tunnel was used
304 struct GNUNET_TIME_Absolute timestamp;
307 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
308 * containing peers added by id or by type, not intermediate peers.
310 struct GNUNET_CONTAINER_MultiHashMap *peers;
313 * Number of peers that are connected and potentially ready to receive data
315 unsigned int peers_ready;
318 * Number of peers that have been added to the tunnel
320 unsigned int peers_total;
323 * Client owner of the tunnel, if any
325 struct MeshClient *owner;
328 * Clients that have been informed about the tunnel, if any
330 struct MeshClient **clients;
333 * Number of elements in clients
335 unsigned int nclients;
338 * Clients that have requested to leave the tunnel
340 struct MeshClient **ignore;
343 * Number of elements in clients
345 unsigned int nignore;
348 * Messages ready to transmit
350 struct MeshQueue *queue_head;
351 struct MeshQueue *queue_tail;
356 struct MeshTunnelTree *tree;
359 * Application type we are looking for in this tunnel
361 GNUNET_MESH_ApplicationType type;
364 * Used to search peers offering a service
366 struct GNUNET_DHT_GetHandle *dht_get_type;
369 * Task to keep the used paths alive
371 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
374 * Task to destroy the tunnel after timeout
376 * FIXME: merge the two? a tunnel will have either
377 * a path refresh OR a timeout, never both!
379 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
384 * Info needed to work with tunnel paths and peers
391 struct MeshTunnel *t;
394 * Neighbouring peer to whom we send the packet to
396 struct MeshPeerInfo *peer;
401 struct MeshPeerPath *path;
404 * Position in peer's transmit queue
411 * Struct containing information about a client of the service
418 struct MeshClient *next;
423 struct MeshClient *prev;
426 * Tunnels that belong to this client, indexed by local id
428 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
431 * Tunnels this client has accepted, indexed by incoming local id
433 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
436 * Tunnels this client has rejected, indexed by incoming local id
438 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
440 * Handle to communicate with the client
442 struct GNUNET_SERVER_Client *handle;
445 * Applications that this client has claimed to provide
447 struct GNUNET_CONTAINER_MultiHashMap *apps;
450 * Messages that this client has declared interest in
452 struct GNUNET_CONTAINER_MultiHashMap *types;
455 * Whether the client is active or shutting down (don't send confirmations
456 * to a client that is shutting down.
461 * ID of the client, mainly for debug messages
469 /******************************************************************************/
470 /************************ DEBUG FUNCTIONS ****************************/
471 /******************************************************************************/
475 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
476 * @param cls string to print
477 * @param success GNUNET_OK if the PUT was transmitted,
478 * GNUNET_NO on timeout,
479 * GNUNET_SYSERR on disconnect from service
480 * after the PUT message was transmitted
481 * (so we don't know if it was received or not)
486 mesh_debug (void *cls, int success)
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
496 /******************************************************************************/
497 /*********************** GLOBAL VARIABLES ****************************/
498 /******************************************************************************/
503 static struct MeshClient *clients;
504 static struct MeshClient *clients_tail;
507 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
509 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
512 * Tunnels incoming, indexed by MESH_TunnelNumber
513 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
515 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
518 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
520 static struct GNUNET_CONTAINER_MultiHashMap *peers;
523 * Handle to communicate with core
525 static struct GNUNET_CORE_Handle *core_handle;
528 * Handle to communicate with transport
530 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
535 static struct GNUNET_DHT_Handle *dht_handle;
540 static struct GNUNET_SERVER_Handle *server_handle;
543 * Notification context, to send messages to local clients
545 static struct GNUNET_SERVER_NotificationContext *nc;
548 * Local peer own ID (memory efficient handle)
550 static GNUNET_PEER_Id myid;
553 * Local peer own ID (full value)
555 static struct GNUNET_PeerIdentity my_full_id;
560 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
565 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
568 * Tunnel ID for the next created tunnel (global tunnel number)
570 static MESH_TunnelNumber next_tid;
573 * Tunnel ID for the next incoming tunnel (local tunnel number)
575 static MESH_TunnelNumber next_local_tid;
578 * All application types provided by this peer
580 static struct GNUNET_CONTAINER_MultiHashMap *applications;
583 * All message types clients of this peer are interested in
585 static struct GNUNET_CONTAINER_MultiHashMap *types;
588 * Task to periodically announce provided applications
590 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
593 * Task to periodically announce itself in the network
595 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
598 * Next ID to assign to a client
600 unsigned int next_client_id;
604 /******************************************************************************/
605 /************************ ITERATORS ****************************/
606 /******************************************************************************/
608 /* FIXME move iterators here */
611 /******************************************************************************/
612 /************************ PERIODIC FUNCTIONS ****************************/
613 /******************************************************************************/
616 * Announce iterator over for each application provided by the peer
619 * @param key current key code
620 * @param value value in the hash map
621 * @return GNUNET_YES if we should continue to
626 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
628 /* FIXME are hashes in multihash map equal on all aquitectures? */
629 /* FIXME: keep return value of 'put' to possibly cancel!? */
630 GNUNET_DHT_put (dht_handle, key, 10,
631 GNUNET_DHT_RO_RECORD_ROUTE |
632 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
633 sizeof (struct GNUNET_PeerIdentity),
634 (const char *) &my_full_id,
635 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
637 APP_ANNOUNCE_TIME, NULL, NULL);
643 * Periodically announce what applications are provided by local clients
646 * @param tc task context
649 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
651 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
653 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
657 DEBUG_DHT ("Starting PUT for apps\n");
659 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
661 announce_applications_task =
662 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
664 DEBUG_DHT ("Finished PUT for apps\n");
671 * Periodically announce self id in the DHT
674 * @param tc task context
677 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
679 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
681 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
685 * - Set data expiration in function of X
688 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
690 GNUNET_DHT_put (dht_handle, /* DHT handle */
691 &my_full_id.hashPubKey, /* Key to use */
692 10, /* Replication level */
693 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
694 GNUNET_BLOCK_TYPE_TEST, /* Block type */
695 sizeof (my_full_id), /* Size of the data */
696 (char *) &my_full_id, /* Data itself */
697 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
698 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
699 NULL, /* Continuation */
700 NULL); /* Continuation closure */
702 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
707 * Function to process paths received for a new peer addition. The recorded
708 * paths form the initial tunnel, which can be optimized later.
709 * Called on each result obtained for the DHT search.
712 * @param exp when will this value expire
713 * @param key key of the result
714 * @param type type of the result
715 * @param size number of bytes in data
716 * @param data pointer to the result data
719 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
720 const GNUNET_HashCode * key,
721 const struct GNUNET_PeerIdentity *get_path,
722 unsigned int get_path_length,
723 const struct GNUNET_PeerIdentity *put_path,
724 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
725 size_t size, const void *data);
728 /******************************************************************************/
729 /****************** GENERAL HELPER FUNCTIONS ************************/
730 /******************************************************************************/
733 * Search for a tunnel by global ID using full PeerIdentities
735 * @param oid owner of the tunnel
736 * @param tid global tunnel number
738 * @return tunnel handler, NULL if doesn't exist
740 static struct MeshTunnel *
741 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
745 * Delete an active client from the tunnel.
751 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
754 * Notify a tunnel that a connection has broken that affects at least
757 * @param t Tunnel affected.
758 * @param p1 Peer that got disconnected from p2.
759 * @param p2 Peer that got disconnected from p1.
761 * @return Short ID of the peer disconnected (either p1 or p2).
762 * 0 if the tunnel remained unaffected.
764 static GNUNET_PEER_Id
765 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
770 * Check if client has registered with the service and has not disconnected
772 * @param client the client to check
774 * @return non-NULL if client exists in the global DLL
776 static struct MeshClient *
777 client_get (struct GNUNET_SERVER_Client *client)
779 struct MeshClient *c;
784 if (c->handle == client)
793 * Checks if a given client has subscribed to certain message type
795 * @param message_type Type of message to check
796 * @param c Client to check
798 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
803 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
807 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
808 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
813 * Allow a client to send more data after transmitting a multicast message
814 * which some neighbor has not yet accepted altough a reasonable time has
817 * @param cls Closure (DataDescriptor containing the task identifier)
818 * @param tc Task Context
820 * FIXME reference counter cshould be just int
823 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
825 struct MeshData *mdata = cls;
827 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
829 GNUNET_assert (NULL != mdata->reference_counter);
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
832 *(mdata->reference_counter));
833 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
834 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
839 * Check whether client wants traffic from a tunnel.
841 * @param c Client to check.
842 * @param t Tunnel to be found.
844 * @return GNUNET_YES if client knows tunnel.
846 * TODO look in client hashmap
849 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
853 for (i = 0; i < t->nclients; i++)
854 if (t->clients[i] == c)
861 * Check whether client has been informed about a tunnel.
863 * @param c Client to check.
864 * @param t Tunnel to be found.
866 * @return GNUNET_YES if client knows tunnel.
868 * TODO look in client hashmap
871 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
875 for (i = 0; i < t->nignore; i++)
876 if (t->ignore[i] == c)
878 return client_wants_tunnel(c, t);
883 * Marks a client as uninterested in traffic from the tunnel, updating both
884 * client and tunnel to reflect this.
886 * @param c Client that doesn't want traffic anymore.
887 * @param t Tunnel which should be ignored.
889 * FIXME when to delete an incoming tunnel?
892 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
894 GNUNET_HashCode hash;
896 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
897 GNUNET_break (GNUNET_YES ==
898 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
900 GNUNET_break (GNUNET_YES ==
901 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
902 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
903 tunnel_delete_active_client (t, c);
904 GNUNET_array_append (t->ignore, t->nignore, c);
909 * Deletes a tunnel from a client (either owner or destination). To be used on
910 * tunnel destroy, otherwise, use client_ignore_tunnel.
912 * @param c Client whose tunnel to delete.
913 * @param t Tunnel which should be deleted.
916 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
918 GNUNET_HashCode hash;
922 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
923 GNUNET_assert (GNUNET_YES ==
924 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
930 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
932 GNUNET_assert (GNUNET_YES ==
933 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
937 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
946 * Send the message to all clients that have subscribed to its type
948 * @param msg Pointer to the message itself
949 * @param payload Pointer to the payload of the message.
950 * @return number of clients this message was sent to
953 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
954 const struct GNUNET_MessageHeader *payload)
956 struct GNUNET_PeerIdentity *oid;
957 struct MeshClient *c;
958 struct MeshTunnel *t;
959 MESH_TunnelNumber *tid;
962 char cbuf[htons (msg->size)];
964 type = ntohs (payload->type);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
968 memcpy (cbuf, msg, sizeof (cbuf));
969 switch (htons (msg->type))
971 struct GNUNET_MESH_Unicast *uc;
972 struct GNUNET_MESH_Multicast *mc;
973 struct GNUNET_MESH_ToOrigin *to;
975 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
976 uc = (struct GNUNET_MESH_Unicast *) cbuf;
980 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
981 mc = (struct GNUNET_MESH_Multicast *) cbuf;
985 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
986 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
994 t = tunnel_get (oid, ntohl (*tid));
1001 for (count = 0, c = clients; c != NULL; c = c->next)
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1004 if (client_is_subscribed (type, c))
1006 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1010 *tid = htonl (t->local_tid);
1014 if (GNUNET_NO == client_knows_tunnel (c, t))
1016 /* This client doesn't know the tunnel */
1017 struct GNUNET_MESH_TunnelNotification tmsg;
1018 GNUNET_HashCode hash;
1020 tmsg.header.size = htons (sizeof (tmsg));
1021 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1022 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1023 tmsg.tunnel_id = htonl (t->local_tid_dest);
1024 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1025 &tmsg.header, GNUNET_NO);
1026 GNUNET_array_append (t->clients, t->nclients, c);
1027 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1029 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1030 c->incoming_tunnels, &hash, t,
1031 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1033 *tid = htonl (t->local_tid_dest);
1036 /* Check if the client wants to get traffic from the tunnel */
1037 if (GNUNET_NO == client_wants_tunnel(c, t))
1040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1041 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1042 (struct GNUNET_MessageHeader
1043 *) cbuf, GNUNET_YES);
1051 * Notify the client that owns the tunnel that a peer has connected to it
1052 * (the requested path to it has been confirmed).
1054 * @param t Tunnel whose owner to notify
1055 * @param id Short id of the peer that has connected
1058 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1060 struct GNUNET_MESH_PeerControl pc;
1062 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1063 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1064 pc.tunnel_id = htonl (t->local_tid);
1065 GNUNET_PEER_resolve (id, &pc.peer);
1066 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1072 * Notify all clients (not depending on registration status) that the incoming
1073 * tunnel is no longer valid.
1075 * @param t Tunnel that was destroyed.
1078 send_clients_tunnel_destroy (struct MeshTunnel *t)
1080 struct GNUNET_MESH_TunnelMessage msg;
1082 msg.header.size = htons (sizeof (msg));
1083 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1084 msg.tunnel_id = htonl (t->local_tid_dest);
1085 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1090 * Notify clients of tunnel disconnections, if needed.
1091 * In case the origin disconnects, the destination clients get a tunnel destroy
1092 * notification. If the last destination disconnects (only one remaining client
1093 * in tunnel), the origin gets a (local ID) peer disconnected.
1094 * Note that the function must be called BEFORE removing the client from
1097 * @param t Tunnel that was destroyed.
1098 * @param c Client that disconnected.
1101 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1107 struct GNUNET_MESH_TunnelMessage msg;
1109 msg.header.size = htons (sizeof (msg));
1110 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1111 msg.tunnel_id = htonl (t->local_tid_dest);
1112 for (i = 0; i < t->nclients; i++)
1113 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1114 &msg.header, GNUNET_NO);
1116 // FIXME when to disconnect an incoming tunnel?
1117 else if (1 == t->nclients && NULL != t->owner)
1119 struct GNUNET_MESH_PeerControl msg;
1121 msg.header.size = htons (sizeof (msg));
1122 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1123 msg.tunnel_id = htonl (t->local_tid);
1124 msg.peer = my_full_id;
1125 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1126 &msg.header, GNUNET_NO);
1132 * Function called to notify a client about the socket
1133 * being ready to queue more data. "buf" will be
1134 * NULL and "size" zero if the socket was closed for
1135 * writing in the meantime.
1137 * @param cls closure
1138 * @param size number of bytes available in buf
1139 * @param buf where the callee should write the message
1140 * @return number of bytes written to buf
1143 send_core_create_path (void *cls, size_t size, void *buf);
1147 * Function called to notify a client about the socket
1148 * being ready to queue more data. "buf" will be
1149 * NULL and "size" zero if the socket was closed for
1150 * writing in the meantime.
1152 * @param cls closure (data itself)
1153 * @param size number of bytes available in buf
1154 * @param buf where the callee should write the message
1156 * @return number of bytes written to buf
1159 send_core_data_multicast (void *cls, size_t size, void *buf);
1163 * Decrements the reference counter and frees all resources if needed
1165 * @param mesh_data Data Descriptor used in a multicast message.
1166 * Freed no longer needed (last message).
1169 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1171 /* Make sure it's a multicast packet */
1172 GNUNET_assert (NULL != mesh_data->reference_counter);
1174 if (0 == --(*(mesh_data->reference_counter)))
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1177 if (NULL != mesh_data->task)
1179 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1181 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1183 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1185 GNUNET_free (mesh_data->task);
1187 GNUNET_free (mesh_data->reference_counter);
1188 GNUNET_free (mesh_data->data);
1189 GNUNET_free (mesh_data);
1195 * Cancel a core transmission that was already requested and free all resources
1196 * associated to the request.
1198 * @param peer PeeInfo of the peer whose transmission is cancelled.
1199 * @param i Position of the transmission to be cancelled.
1202 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
1204 if (NULL != peer->core_transmit[i])
1206 struct MeshTransmissionDescriptor *dd;
1207 struct MeshPathInfo *path_info;
1211 struct GNUNET_PeerIdentity id;
1213 GNUNET_PEER_resolve (peer->id, &id);
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 " Cancelling data transmission at %s [%u]\n",
1216 GNUNET_i2s (&id), i);
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message type %u\n",
1221 /* TODO: notify that tranmission has failed */
1222 switch (peer->types[i])
1224 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1225 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1226 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
1228 dd = peer->infos[i];
1229 data_descriptor_decrement_multicast (dd->mesh_data);
1231 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
1233 path_info = peer->infos[i];
1234 path_destroy (path_info->path);
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
1240 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
1241 peer->core_transmit[i] = NULL;
1242 GNUNET_free (peer->infos[i]);
1248 * Get a unused CORE slot to transmit a message to a peer. If all the slots
1249 * are used, cancel one and return it's position.
1251 * @param peer PeerInfo of the neighbor we want to transmit to.
1253 * @return The index of an available slot to transmit to the neighbor.
1256 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1260 for (i = 0; peer->core_transmit[i]; i++)
1262 if (i == (CORE_QUEUE_SIZE - 1))
1264 /* All positions are taken! Overwriting! */
1266 peer_info_cancel_transmission (peer, 0);
1275 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1276 * and insert it in the appropiate structures if the peer is not known yet.
1278 * @param peer Full identity of the peer.
1280 * @return Existing or newly created peer info.
1282 static struct MeshPeerInfo *
1283 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1285 struct MeshPeerInfo *peer_info;
1287 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1288 if (NULL == peer_info)
1291 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1292 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1293 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1294 peer_info->id = GNUNET_PEER_intern (peer);
1302 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1303 * and insert it in the appropiate structures if the peer is not known yet.
1305 * @param peer Short identity of the peer.
1307 * @return Existing or newly created peer info.
1309 static struct MeshPeerInfo *
1310 peer_info_get_short (const GNUNET_PEER_Id peer)
1312 struct GNUNET_PeerIdentity id;
1314 GNUNET_PEER_resolve (peer, &id);
1315 return peer_info_get (&id);
1320 * Iterator to remove the tunnel from the list of tunnels a peer participates
1323 * @param cls Closure (tunnel info)
1324 * @param key GNUNET_PeerIdentity of the peer (unused)
1325 * @param value PeerInfo of the peer
1327 * @return always GNUNET_YES, to keep iterating
1330 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1332 struct MeshTunnel *t = cls;
1333 struct MeshPeerInfo *peer = value;
1336 for (i = 0; i < peer->ntunnels; i++)
1339 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1342 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1343 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1352 * Core callback to write a
1354 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1355 * @param size Number of bytes available in buf.
1356 * @param buf Where the to write the message.
1358 * @return number of bytes written to buf
1361 send_core_data_raw (void *cls, size_t size, void *buf)
1363 struct MeshTransmissionDescriptor *info = cls;
1364 struct GNUNET_MessageHeader *msg;
1367 GNUNET_assert (NULL != info);
1368 GNUNET_assert (NULL != info->mesh_data);
1369 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1370 total_size = ntohs (msg->size);
1372 if (total_size > size)
1374 struct GNUNET_PeerIdentity id;
1376 GNUNET_PEER_resolve (info->peer->id, &id);
1377 info->peer->core_transmit[info->handler_n] =
1378 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1379 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1380 size, &send_core_data_raw, info);
1383 info->peer->core_transmit[info->handler_n] = NULL;
1384 memcpy (buf, msg, total_size);
1385 GNUNET_free (info->mesh_data);
1392 * Sends an already built message to a peer, properly registrating
1393 * all used resources.
1395 * @param message Message to send. Fucntion makes a copy of it.
1396 * @param peer Short ID of the neighbor whom to send the message.
1401 send_message (const struct GNUNET_MessageHeader *message,
1402 const struct GNUNET_PeerIdentity *peer)
1404 struct MeshTransmissionDescriptor *info;
1405 struct MeshPeerInfo *neighbor;
1406 struct MeshPeerPath *p;
1410 // GNUNET_TRANSPORT_try_connect();
1412 size = ntohs (message->size);
1413 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1414 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1415 info->mesh_data->data = GNUNET_malloc (size);
1416 memcpy (info->mesh_data->data, message, size);
1417 info->mesh_data->data_len = size;
1418 neighbor = peer_info_get (peer);
1419 for (p = neighbor->path_head; NULL != p; p = p->next)
1429 GNUNET_free (info->mesh_data->data);
1430 GNUNET_free (info->mesh_data);
1434 i = peer_info_transmit_slot (neighbor);
1435 info->handler_n = i;
1436 info->peer = neighbor;
1437 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1438 neighbor->infos[i] = info;
1439 neighbor->core_transmit[i] =
1440 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1441 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1442 size, &send_core_data_raw, info);
1448 * Sends a CREATE PATH message for a path to a peer, properly registrating
1449 * all used resources.
1451 * @param peer PeerInfo of the final peer for whom this path is being created.
1452 * @param p Path itself.
1453 * @param t Tunnel for which the path is created.
1456 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1457 struct MeshTunnel *t)
1459 struct GNUNET_PeerIdentity id;
1460 struct MeshPathInfo *path_info;
1461 struct MeshPeerInfo *neighbor;
1466 p = tree_get_path_to_peer (t->tree, peer->id);
1473 for (i = 0; i < p->length; i++)
1475 if (p->peers[i] == myid)
1478 if (i >= p->length - 1)
1484 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1486 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1487 path_info->path = p;
1489 neighbor = peer_info_get (&id);
1490 path_info->peer = neighbor;
1491 path_info->pos = peer_info_transmit_slot (neighbor);
1492 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1493 neighbor->infos[path_info->pos] = path_info;
1494 neighbor->core_transmit[path_info->pos] =
1495 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1498 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1500 sizeof (struct GNUNET_MESH_ManipulatePath) +
1501 (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1502 &send_core_create_path, /* callback */
1503 path_info); /* cls */
1508 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1510 * @param t Tunnel whose path to destroy.
1511 * @param destination Short ID of the peer to whom the path to destroy.
1514 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1516 struct MeshPeerPath *p;
1519 p = tree_get_path_to_peer (t->tree, destination);
1525 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1526 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1528 struct GNUNET_MESH_ManipulatePath *msg;
1529 struct GNUNET_PeerIdentity *pi;
1533 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1534 msg->header.size = htons (size);
1535 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1536 msg->tid = htonl (t->id.tid);
1537 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1538 for (i = 0; i < p->length; i++)
1540 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1542 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1549 * Try to establish a new connection to this peer.
1550 * Use the best path for the given tunnel.
1551 * If the peer doesn't have any path to it yet, try to get one.
1552 * If the peer already has some path, send a CREATE PATH towards it.
1554 * @param peer PeerInfo of the peer.
1555 * @param t Tunnel for which to create the path, if possible.
1558 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1560 struct MeshPeerPath *p;
1561 struct MeshPathInfo *path_info;
1563 if (NULL != peer->path_head)
1565 p = tree_get_path_to_peer (t->tree, peer->id);
1572 // FIXME always send create path to self
1575 send_create_path (peer, p, t);
1579 GNUNET_HashCode hash;
1582 send_client_peer_connected (t, myid);
1583 t->local_tid_dest = next_local_tid++;
1584 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1587 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1588 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1595 else if (NULL == peer->dhtget)
1597 struct GNUNET_PeerIdentity id;
1599 GNUNET_PEER_resolve (peer->id, &id);
1600 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1601 path_info->peer = peer;
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1605 peer->dhtgetcls = path_info;
1606 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1607 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1608 GNUNET_BLOCK_TYPE_TEST, /* type */
1609 &id.hashPubKey, /* key to search */
1610 10, /* replication level */
1611 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1612 0, /* xquery bits */
1613 &dht_get_id_handler, path_info);
1615 /* Otherwise, there is no path but the DHT get is already started. */
1620 * Task to delay the connection of a peer
1622 * @param cls Closure (path info with tunnel and peer to connect).
1623 * Will be free'd on exection.
1624 * @param tc TaskContext
1627 peer_info_connect_task (void *cls,
1628 const struct GNUNET_SCHEDULER_TaskContext *tc)
1630 struct MeshPathInfo *path_info = cls;
1632 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1637 peer_info_connect (path_info->peer, path_info->t);
1643 * Destroy the peer_info and free any allocated resources linked to it
1645 * @param pi The peer_info to destroy.
1647 * @return GNUNET_OK on success
1650 peer_info_destroy (struct MeshPeerInfo *pi)
1652 struct GNUNET_PeerIdentity id;
1653 struct MeshPeerPath *p;
1654 struct MeshPeerPath *nextp;
1657 GNUNET_PEER_resolve (pi->id, &id);
1658 GNUNET_PEER_change_rc (pi->id, -1);
1661 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1665 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1667 if (NULL != pi->dhtget)
1669 GNUNET_DHT_get_stop (pi->dhtget);
1670 GNUNET_free (pi->dhtgetcls);
1672 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1674 peer_info_cancel_transmission (pi, i);
1680 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1690 * Remove all paths that rely on a direct connection between p1 and p2
1691 * from the peer itself and notify all tunnels about it.
1693 * @param peer PeerInfo of affected peer.
1694 * @param p1 GNUNET_PEER_Id of one peer.
1695 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1698 * TODO: optimize (see below)
1701 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1704 struct MeshPeerPath *p;
1705 struct MeshPeerPath *aux;
1706 struct MeshPeerInfo *peer_d;
1708 unsigned int destroyed;
1714 p = peer->path_head;
1718 for (i = 0; i < (p->length - 1); i++)
1720 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1721 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1723 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1734 for (i = 0; i < peer->ntunnels; i++)
1736 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1740 * Problem: one or more peers have been deleted from the tunnel tree.
1741 * We don't know who they are to try to add them again.
1742 * We need to try to find a new path for each of the disconnected peers.
1743 * Some of them might already have a path to reach them that does not
1744 * involve p1 and p2. Adding all anew might render in a better tree than
1745 * the trivial immediate fix.
1747 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1748 * its children will be reachable trough him.
1750 peer_d = peer_info_get_short (d);
1753 for (p = peer_d->path_head; NULL != p; p = p->next)
1755 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1763 /* No callback, as peer will be already disconnected and a connection
1764 * scheduled by tunnel_notify_connection_broken.
1766 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1770 peer_info_connect (peer_d, peer->tunnels[i]);
1777 * Add the path to the peer and update the path used to reach it in case this
1780 * @param peer_info Destination peer to add the path to.
1781 * @param path New path to add. Last peer must be the peer in arg 1.
1782 * Path will be either used of freed if already known.
1783 * @param trusted Do we trust that this path is real?
1786 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1789 struct MeshPeerPath *aux;
1793 if ((NULL == peer_info) || (NULL == path))
1796 path_destroy (path);
1799 if (path->peers[path->length - 1] != peer_info->id)
1802 path_destroy (path);
1805 if (path->length <= 2 && GNUNET_NO == trusted)
1807 /* Only allow CORE to tell us about direct paths */
1808 path_destroy (path);
1811 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1812 for (l = 1; l < path->length; l++)
1814 if (path->peers[l] == myid)
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1817 for (l2 = 0; l2 < path->length - l; l2++)
1819 path->peers[l2] = path->peers[l + l2];
1824 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1829 struct GNUNET_PeerIdentity id;
1831 GNUNET_PEER_resolve (peer_info->id, &id);
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1833 path->length, GNUNET_i2s (&id));
1836 l = path_get_length (path);
1843 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1844 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1846 l2 = path_get_length (aux);
1849 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1850 peer_info->path_tail, aux, path);
1855 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1857 path_destroy (path);
1862 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1869 * Add the path to the origin peer and update the path used to reach it in case
1870 * this is the shortest.
1871 * The path is given in peer_info -> destination, therefore we turn the path
1872 * upside down first.
1874 * @param peer_info Peer to add the path to, being the origin of the path.
1875 * @param path New path to add after being inversed.
1876 * @param trusted Do we trust that this path is real?
1879 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1880 struct MeshPeerPath *path, int trusted)
1883 peer_info_add_path (peer_info, path, trusted);
1888 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1889 * to obtain a local peer -> destination path and interning the peer ids.
1891 * @return Newly allocated and created path
1893 static struct MeshPeerPath *
1894 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1895 unsigned int get_path_length,
1896 const struct GNUNET_PeerIdentity *put_path,
1897 unsigned int put_path_length)
1899 struct MeshPeerPath *p;
1905 GNUNET_PEER_change_rc (myid, 1);
1906 i = get_path_length;
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1908 for (i--; i >= 0; i--)
1910 id = GNUNET_PEER_intern (&get_path[i]);
1911 if (p->length > 0 && id == p->peers[p->length - 1])
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1914 GNUNET_PEER_change_rc (id, -1);
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1919 GNUNET_i2s (&get_path[i]));
1921 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1922 p->peers[p->length - 1] = id;
1925 i = put_path_length;
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1927 for (i--; i >= 0; i--)
1929 id = GNUNET_PEER_intern (&put_path[i]);
1932 /* PUT path went through us, so discard the path up until now and start
1933 * from here to get a much shorter (and loop-free) path.
1938 if (p->length > 0 && id == p->peers[p->length - 1])
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1941 GNUNET_PEER_change_rc (id, -1);
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1946 GNUNET_i2s (&put_path[i]));
1948 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1949 p->peers[p->length - 1] = id;
1953 if (get_path_length > 0)
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1955 GNUNET_i2s (&get_path[0]));
1956 if (put_path_length > 0)
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1958 GNUNET_i2s (&put_path[0]));
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1961 for (i = 0; i < p->length; i++)
1963 struct GNUNET_PeerIdentity peer_id;
1965 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1967 GNUNET_i2s (&peer_id));
1975 * Adds a path to the peer_infos of all the peers in the path
1977 * @param p Path to process.
1978 * @param confirmed Whether we know if the path works or not. FIXME use
1981 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1985 /* TODO: invert and add */
1986 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1987 for (i++; i < p->length; i++)
1989 struct MeshPeerInfo *aux;
1990 struct MeshPeerPath *copy;
1992 aux = peer_info_get_short (p->peers[i]);
1993 copy = path_duplicate (p);
1994 copy->length = i + 1;
1995 peer_info_add_path (aux, copy, GNUNET_NO);
2001 * Send keepalive packets for a peer
2003 * @param cls Closure (tunnel for which to send the keepalive).
2004 * @param tc Notification context.
2006 * TODO: implement explicit multicast keepalive?
2009 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2013 * Search for a tunnel among the incoming tunnels
2015 * @param tid the local id of the tunnel
2017 * @return tunnel handler, NULL if doesn't exist
2019 static struct MeshTunnel *
2020 tunnel_get_incoming (MESH_TunnelNumber tid)
2022 GNUNET_HashCode hash;
2024 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2025 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2026 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2031 * Search for a tunnel among the tunnels for a client
2033 * @param c the client whose tunnels to search in
2034 * @param tid the local id of the tunnel
2036 * @return tunnel handler, NULL if doesn't exist
2038 static struct MeshTunnel *
2039 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2041 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2043 return tunnel_get_incoming (tid);
2047 GNUNET_HashCode hash;
2049 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2050 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2056 * Search for a tunnel by global ID using PEER_ID
2058 * @param pi owner of the tunnel
2059 * @param tid global tunnel number
2061 * @return tunnel handler, NULL if doesn't exist
2063 static struct MeshTunnel *
2064 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2066 struct MESH_TunnelID id;
2067 GNUNET_HashCode hash;
2072 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2073 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2078 * Search for a tunnel by global ID using full PeerIdentities
2080 * @param oid owner of the tunnel
2081 * @param tid global tunnel number
2083 * @return tunnel handler, NULL if doesn't exist
2085 static struct MeshTunnel *
2086 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2088 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2093 * Delete an active client from the tunnel.
2099 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2103 for (i = 0; i < t->nclients; i++)
2105 if (t->clients[i] == c)
2107 t->clients[i] = t->clients[t->nclients - 1];
2108 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2116 * Delete an ignored client from the tunnel.
2122 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2126 for (i = 0; i < t->nignore; i++)
2128 if (t->ignore[i] == c)
2130 t->ignore[i] = t->ignore[t->nignore - 1];
2131 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2139 * Delete a client from the tunnel. It should be only done on
2140 * client disconnection, otherwise use client_ignore_tunnel.
2146 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2148 tunnel_delete_ignored_client (t, c);
2149 tunnel_delete_active_client (t, c);
2154 * Callback used to notify a client owner of a tunnel that a peer has
2155 * disconnected, most likely because of a path change.
2157 * @param cls Closure (tunnel this notification is about).
2158 * @param peer_id Short ID of disconnected peer.
2161 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2163 struct MeshTunnel *t = cls;
2164 struct MeshPeerInfo *peer;
2165 struct MeshPathInfo *path_info;
2167 if (NULL != t->owner && NULL != nc)
2169 struct GNUNET_MESH_PeerControl msg;
2171 msg.header.size = htons (sizeof (msg));
2172 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2173 msg.tunnel_id = htonl (t->local_tid);
2174 GNUNET_PEER_resolve (peer_id, &msg.peer);
2175 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2176 &msg.header, GNUNET_NO);
2178 peer = peer_info_get_short (peer_id);
2179 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2180 path_info->peer = peer;
2182 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2187 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2188 * needed rescources.
2189 * If peer already exists, reevaluate shortest path and change if different.
2191 * @param t Tunnel we want to add a new peer to
2192 * @param peer PeerInfo of the peer being added
2196 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2198 struct GNUNET_PeerIdentity id;
2199 struct MeshPeerPath *best_p;
2200 struct MeshPeerPath *p;
2201 unsigned int best_cost;
2204 GNUNET_PEER_resolve (peer->id, &id);
2206 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2209 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2210 GNUNET_assert (GNUNET_OK ==
2211 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2213 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2216 if (NULL != (p = peer->path_head))
2219 best_cost = tree_get_path_cost (t->tree, p);
2222 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2229 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2230 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2231 t->path_refresh_task =
2232 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2236 /* Start a DHT get */
2237 peer_info_connect (peer, t);
2242 * Add a path to a tunnel which we don't own, just to remember the next hop.
2243 * If destination node was already in the tunnel, the first hop information
2244 * will be replaced with the new path.
2246 * @param t Tunnel we want to add a new peer to
2247 * @param p Path to add
2248 * @param own_pos Position of local node in path.
2252 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2253 unsigned int own_pos)
2255 struct GNUNET_PeerIdentity id;
2257 GNUNET_assert (0 != own_pos);
2258 tree_add_path (t->tree, p, NULL, NULL);
2259 if (own_pos < p->length - 1)
2261 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2262 tree_update_first_hops (t->tree, myid, &id);
2268 * Notifies a tunnel that a connection has broken that affects at least
2269 * some of its peers. Sends a notification towards the root of the tree.
2270 * In case the peer is the owner of the tree, notifies the client that owns
2271 * the tunnel and tries to reconnect.
2273 * @param t Tunnel affected.
2274 * @param p1 Peer that got disconnected from p2.
2275 * @param p2 Peer that got disconnected from p1.
2277 * @return Short ID of the peer disconnected (either p1 or p2).
2278 * 0 if the tunnel remained unaffected.
2280 static GNUNET_PEER_Id
2281 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2287 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2289 if (myid != p1 && myid != p2)
2295 if (tree_get_predecessor (t->tree) != 0)
2297 /* We are the peer still connected, notify owner of the disconnection. */
2298 struct GNUNET_MESH_PathBroken msg;
2299 struct GNUNET_PeerIdentity neighbor;
2301 msg.header.size = htons (sizeof (msg));
2302 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2303 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2304 msg.tid = htonl (t->id.tid);
2305 msg.peer1 = my_full_id;
2306 GNUNET_PEER_resolve (pid, &msg.peer2);
2307 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2308 send_message (&msg.header, &neighbor);
2316 * Send a multicast packet to a neighbor.
2318 * @param cls Closure (Info about the multicast packet)
2319 * @param neighbor_id Short ID of the neighbor to send the packet to.
2322 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2324 struct MeshData *mdata = cls;
2325 struct MeshTransmissionDescriptor *info;
2326 struct GNUNET_PeerIdentity neighbor;
2329 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2331 info->mesh_data = mdata;
2332 (*(mdata->reference_counter)) ++;
2333 info->destination = neighbor_id;
2334 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2336 GNUNET_i2s (&neighbor));
2337 info->peer = peer_info_get (&neighbor);
2338 GNUNET_assert (NULL != info->peer);
2339 i = peer_info_transmit_slot (info->peer);
2340 info->handler_n = i;
2341 info->peer->infos[i] = info;
2342 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2343 info->peer->core_transmit[i] =
2344 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2345 GNUNET_TIME_UNIT_FOREVER_REL,
2346 &neighbor, info->mesh_data->data_len,
2347 &send_core_data_multicast, info);
2351 * Send a message in a tunnel in multicast, sending a copy to each child node
2352 * down the local one in the tunnel tree.
2354 * @param t Tunnel in which to send the data.
2355 * @param msg Message to be sent.
2356 * @param internal Has the service generated this message?
2359 tunnel_send_multicast (struct MeshTunnel *t,
2360 const struct GNUNET_MessageHeader *msg,
2363 struct MeshData *mdata;
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 " sending a multicast packet...\n");
2367 mdata = GNUNET_malloc (sizeof (struct MeshData));
2368 mdata->data_len = ntohs (msg->size);
2369 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2371 mdata->data = GNUNET_malloc (mdata->data_len);
2372 memcpy (mdata->data, msg, mdata->data_len);
2373 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2375 struct GNUNET_MESH_Multicast *mcast;
2377 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2378 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2380 ntohl (mcast->ttl));
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2386 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2387 && GNUNET_NO == internal)
2389 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2391 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2397 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2398 if (*(mdata->reference_counter) == 0)
2400 GNUNET_free (mdata->data);
2401 GNUNET_free (mdata->reference_counter);
2402 if (NULL != mdata->task)
2404 GNUNET_SCHEDULER_cancel(*(mdata->task));
2405 GNUNET_free (mdata->task);
2406 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2408 // FIXME change order?
2409 GNUNET_free (mdata);
2411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2412 " sending a multicast packet done\n");
2418 * Send a message to all peers in this tunnel that the tunnel is no longer
2421 * @param t The tunnel whose peers to notify.
2424 tunnel_send_destroy (struct MeshTunnel *t)
2426 struct GNUNET_MESH_TunnelDestroy msg;
2428 msg.header.size = htons (sizeof (msg));
2429 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2430 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2431 msg.tid = htonl (t->id.tid);
2432 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2438 * Destroy the tunnel and free any allocated resources linked to it.
2440 * @param t the tunnel to destroy
2442 * @return GNUNET_OK on success
2445 tunnel_destroy (struct MeshTunnel *t)
2447 struct MeshClient *c;
2448 struct MeshQueue *q;
2449 struct MeshQueue *qn;
2450 GNUNET_HashCode hash;
2461 struct GNUNET_PeerIdentity id;
2463 GNUNET_PEER_resolve (t->id.oid, &id);
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2465 GNUNET_i2s (&id), t->id.tid);
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2471 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2472 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2477 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2480 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2484 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2485 for (i = 0; i < t->nclients; i++)
2489 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2494 for (i = 0; i < t->nignore; i++)
2498 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2503 if (t->nclients > 0)
2506 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2510 GNUNET_free (t->clients);
2512 if (NULL != t->peers)
2514 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2516 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2521 if (NULL != q->data)
2522 GNUNET_free (q->data);
2526 /* TODO cancel core transmit ready in case it was active */
2528 tree_destroy (t->tree);
2529 if (NULL != t->dht_get_type)
2530 GNUNET_DHT_get_stop (t->dht_get_type);
2531 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2532 GNUNET_SCHEDULER_cancel (t->timeout_task);
2533 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2534 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2541 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2542 * that are no longer needed, as well as nodes of no longer reachable peers.
2543 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2545 * @param t Tunnel from which to remove the path.
2546 * @param peer Short id of the peer which should be removed.
2549 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2551 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2557 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2558 * client when the client disconnects. If the client is not the owner, the
2559 * owner will get notified if no more clients are in the tunnel and the client
2560 * get removed from the tunnel's list.
2562 * @param cls closure (client that is disconnecting)
2563 * @param key the hash of the local tunnel id (used to access the hashmap)
2564 * @param value the value stored at the key (tunnel to destroy)
2566 * @return GNUNET_OK on success
2569 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2571 struct MeshTunnel *t = value;
2572 struct MeshClient *c = cls;
2575 send_client_tunnel_disconnect(t, c);
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2580 tunnel_delete_client(t, c);
2581 client_delete_tunnel(c, t);
2584 tunnel_send_destroy(t);
2585 r = tunnel_destroy (t);
2591 * Timeout function, destroys tunnel if called
2593 * @param cls Closure (tunnel to destroy).
2594 * @param tc TaskContext
2597 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2599 struct MeshTunnel *t = cls;
2601 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2603 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2608 * Resets the tunnel timeout. Starts it if no timeout was running.
2610 * @param t Tunnel whose timeout to reset.
2613 tunnel_reset_timeout (struct MeshTunnel *t)
2615 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2616 GNUNET_SCHEDULER_cancel (t->timeout_task);
2618 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2619 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2623 /******************************************************************************/
2624 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2625 /******************************************************************************/
2628 * Function called to notify a client about the socket
2629 * being ready to queue more data. "buf" will be
2630 * NULL and "size" zero if the socket was closed for
2631 * writing in the meantime.
2633 * @param cls closure
2634 * @param size number of bytes available in buf
2635 * @param buf where the callee should write the message
2636 * @return number of bytes written to buf
2639 send_core_create_path (void *cls, size_t size, void *buf)
2641 struct MeshPathInfo *info = cls;
2642 struct GNUNET_MESH_ManipulatePath *msg;
2643 struct GNUNET_PeerIdentity *peer_ptr;
2644 struct MeshPeerInfo *peer = info->peer;
2645 struct MeshTunnel *t = info->t;
2646 struct MeshPeerPath *p = info->path;
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2652 sizeof (struct GNUNET_MESH_ManipulatePath) +
2653 p->length * sizeof (struct GNUNET_PeerIdentity);
2655 if (size < size_needed || NULL == buf)
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "create path retransmit!\n");
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buf: %p\n", buf);
2659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " size: (%u/%u)\n", size,
2661 info->peer->core_transmit[info->pos] =
2662 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2663 GNUNET_TIME_UNIT_FOREVER_REL,
2664 tree_get_first_hop (t->tree,
2666 size_needed, &send_core_create_path,
2670 info->peer->core_transmit[info->pos] = NULL;
2673 struct GNUNET_PeerIdentity id;
2675 GNUNET_PEER_resolve (peer->id, &id);
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 " setting core_transmit %s [%u] to NULL\n",
2678 GNUNET_i2s (&id), info->pos);
2681 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2682 msg->header.size = htons (size_needed);
2683 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2684 msg->tid = ntohl (t->id.tid);
2686 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2687 for (i = 0; i < p->length; i++)
2689 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2696 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2702 * Function called to notify a client about the socket
2703 * being ready to queue more data. "buf" will be
2704 * NULL and "size" zero if the socket was closed for
2705 * writing in the meantime.
2707 * @param cls closure (data itself)
2708 * @param size number of bytes available in buf
2709 * @param buf where the callee should write the message
2711 * @return number of bytes written to buf
2714 send_core_data_multicast (void *cls, size_t size, void *buf)
2716 struct MeshTransmissionDescriptor *info = cls;
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2720 GNUNET_assert (NULL != info);
2721 GNUNET_assert (NULL != info->peer);
2722 total_size = info->mesh_data->data_len;
2723 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2725 if (total_size > size)
2728 struct GNUNET_PeerIdentity id;
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2731 "Multicast: retransmitting... (%u/%u)\n", size,
2733 GNUNET_PEER_resolve (info->peer->id, &id);
2734 info->peer->core_transmit[info->handler_n] =
2735 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2736 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2738 &send_core_data_multicast, info);
2741 info->peer->core_transmit[info->handler_n] = NULL;
2742 info->peer->infos[info->handler_n] = NULL;
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2744 memcpy (buf, info->mesh_data->data, total_size);
2747 struct GNUNET_MESH_Multicast *mc;
2748 struct GNUNET_MessageHeader *mh;
2751 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2753 mc = (struct GNUNET_MESH_Multicast *) mh;
2754 mh = (struct GNUNET_MessageHeader *) &mc[1];
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2756 " multicast, payload type %u\n", ntohs (mh->type));
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2758 " multicast, payload size %u\n", ntohs (mh->size));
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2767 data_descriptor_decrement_multicast (info->mesh_data);
2768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2776 * Function called to notify a client about the socket
2777 * being ready to queue more data. "buf" will be
2778 * NULL and "size" zero if the socket was closed for
2779 * writing in the meantime.
2781 * @param cls closure (MeshTransmissionDescriptor)
2782 * @param size number of bytes available in buf
2783 * @param buf where the callee should write the message
2784 * @return number of bytes written to buf
2787 send_core_path_ack (void *cls, size_t size, void *buf)
2789 struct MeshTransmissionDescriptor *info = cls;
2790 struct GNUNET_MESH_PathACK *msg = buf;
2792 GNUNET_assert (NULL != info);
2795 info->peer->core_transmit[info->handler_n] = NULL;
2797 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2802 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2803 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2804 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2805 msg->tid = htonl (info->origin->tid);
2806 msg->peer_id = my_full_id;
2808 /* TODO add signature */
2810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2811 return sizeof (struct GNUNET_MESH_PathACK);
2815 /******************************************************************************/
2816 /******************** MESH NETWORK HANDLERS **************************/
2817 /******************************************************************************/
2821 * Core handler for path creation
2823 * @param cls closure
2824 * @param message message
2825 * @param peer peer identity this notification is about
2826 * @param atsi performance data
2827 * @param atsi_count number of records in 'atsi'
2829 * @return GNUNET_OK to keep the connection open,
2830 * GNUNET_SYSERR to close it (signal serious error)
2833 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2834 const struct GNUNET_MessageHeader *message,
2835 const struct GNUNET_ATS_Information *atsi,
2836 unsigned int atsi_count)
2838 unsigned int own_pos;
2841 MESH_TunnelNumber tid;
2842 struct GNUNET_MESH_ManipulatePath *msg;
2843 struct GNUNET_PeerIdentity *pi;
2844 GNUNET_HashCode hash;
2845 struct MeshPeerPath *path;
2846 struct MeshPeerInfo *dest_peer_info;
2847 struct MeshPeerInfo *orig_peer_info;
2848 struct MeshTunnel *t;
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2851 "Received a path create msg [%s]\n",
2852 GNUNET_i2s (&my_full_id));
2853 size = ntohs (message->size);
2854 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2856 GNUNET_break_op (0);
2860 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2861 if (size % sizeof (struct GNUNET_PeerIdentity))
2863 GNUNET_break_op (0);
2866 size /= sizeof (struct GNUNET_PeerIdentity);
2869 GNUNET_break_op (0);
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2873 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2875 tid = ntohl (msg->tid);
2876 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2878 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2879 t = tunnel_get (pi, tid);
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2883 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2884 t->id.oid = GNUNET_PEER_intern (pi);
2886 while (NULL != tunnel_get_incoming (next_local_tid))
2887 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2888 t->local_tid_dest = next_local_tid++;
2889 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2890 t->tree = tree_new (t->id.oid);
2892 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2894 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2895 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2901 tunnel_reset_timeout (t);
2902 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2904 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2905 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2913 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2914 if (NULL == dest_peer_info)
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2917 " Creating PeerInfo for destination.\n");
2918 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2919 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2920 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2922 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2924 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2925 if (NULL == orig_peer_info)
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2928 " Creating PeerInfo for origin.\n");
2929 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2930 orig_peer_info->id = GNUNET_PEER_intern (pi);
2931 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2932 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2935 path = path_new (size);
2937 for (i = 0; i < size; i++)
2939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2940 GNUNET_i2s (&pi[i]));
2941 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2942 if (path->peers[i] == myid)
2945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2948 /* cannot be self, must be 'not found' */
2949 /* create path: self not found in path through self */
2950 GNUNET_break_op (0);
2951 path_destroy (path);
2952 /* FIXME error. destroy tunnel? leave for timeout? */
2955 path_add_to_peers (path, GNUNET_NO);
2956 tunnel_add_path (t, path, own_pos);
2957 if (own_pos == size - 1)
2959 /* It is for us! Send ack. */
2960 struct MeshTransmissionDescriptor *info;
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2964 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2965 if (NULL == t->peers)
2967 /* New tunnel! Notify clients on data. */
2968 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2970 GNUNET_break (GNUNET_OK ==
2971 GNUNET_CONTAINER_multihashmap_put (t->peers,
2972 &my_full_id.hashPubKey,
2975 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2976 /* FIXME use send_message */
2977 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2978 info->origin = &t->id;
2979 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2980 GNUNET_assert (NULL != info->peer);
2981 j = peer_info_transmit_slot (info->peer);
2982 info->handler_n = j;
2983 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2984 info->peer->infos[j] = info;
2985 info->peer->core_transmit[j] =
2986 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 10,
2987 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2988 sizeof (struct GNUNET_MESH_PathACK),
2989 &send_core_path_ack, info);
2993 struct MeshPeerPath *path2;
2995 /* It's for somebody else! Retransmit. */
2996 path2 = path_duplicate (path);
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
2998 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2999 path2 = path_duplicate (path);
3000 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3001 send_create_path (dest_peer_info, path, t);
3008 * Core handler for path destruction
3010 * @param cls closure
3011 * @param message message
3012 * @param peer peer identity this notification is about
3013 * @param atsi performance data
3014 * @param atsi_count number of records in 'atsi'
3016 * @return GNUNET_OK to keep the connection open,
3017 * GNUNET_SYSERR to close it (signal serious error)
3020 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3021 const struct GNUNET_MessageHeader *message,
3022 const struct GNUNET_ATS_Information *atsi,
3023 unsigned int atsi_count)
3025 struct GNUNET_MESH_ManipulatePath *msg;
3026 struct GNUNET_PeerIdentity *pi;
3027 struct MeshPeerPath *path;
3028 struct MeshTunnel *t;
3029 unsigned int own_pos;
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3034 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3035 size = ntohs (message->size);
3036 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3038 GNUNET_break_op (0);
3042 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3043 if (size % sizeof (struct GNUNET_PeerIdentity))
3045 GNUNET_break_op (0);
3048 size /= sizeof (struct GNUNET_PeerIdentity);
3051 GNUNET_break_op (0);
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3056 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3057 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3059 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3061 t = tunnel_get (pi, ntohl (msg->tid));
3064 /* TODO notify back: we don't know this tunnel */
3065 GNUNET_break_op (0);
3068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3069 path = path_new (size);
3071 for (i = 0; i < size; i++)
3073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3074 GNUNET_i2s (&pi[i]));
3075 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3076 if (path->peers[i] == myid)
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3080 if (own_pos < path->length - 1)
3081 send_message (message, &pi[own_pos + 1]);
3083 send_client_tunnel_disconnect(t, NULL);
3085 tunnel_delete_peer (t, path->peers[path->length - 1]);
3086 path_destroy (path);
3092 * Core handler for notifications of broken paths
3094 * @param cls closure
3095 * @param message message
3096 * @param peer peer identity this notification is about
3097 * @param atsi performance data
3098 * @param atsi_count number of records in 'atsi'
3100 * @return GNUNET_OK to keep the connection open,
3101 * GNUNET_SYSERR to close it (signal serious error)
3104 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3105 const struct GNUNET_MessageHeader *message,
3106 const struct GNUNET_ATS_Information *atsi,
3107 unsigned int atsi_count)
3109 struct GNUNET_MESH_PathBroken *msg;
3110 struct MeshTunnel *t;
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3113 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3114 msg = (struct GNUNET_MESH_PathBroken *) message;
3115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3116 GNUNET_i2s (&msg->peer1));
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3118 GNUNET_i2s (&msg->peer2));
3119 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3122 GNUNET_break_op (0);
3125 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3126 GNUNET_PEER_search (&msg->peer2));
3133 * Core handler for tunnel destruction
3135 * @param cls closure
3136 * @param message message
3137 * @param peer peer identity this notification is about
3138 * @param atsi performance data
3139 * @param atsi_count number of records in 'atsi'
3141 * @return GNUNET_OK to keep the connection open,
3142 * GNUNET_SYSERR to close it (signal serious error)
3145 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3146 const struct GNUNET_MessageHeader *message,
3147 const struct GNUNET_ATS_Information *atsi,
3148 unsigned int atsi_count)
3150 struct GNUNET_MESH_TunnelDestroy *msg;
3151 struct MeshTunnel *t;
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3154 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3155 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3157 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3158 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3161 /* Probably already got the message from another path,
3162 * destroyed the tunnel and retransmitted to children.
3167 if (t->id.oid == myid)
3169 GNUNET_break_op (0);
3172 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3174 /* Tunnel was incoming, notify clients */
3175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3176 t->local_tid, t->local_tid_dest);
3177 send_clients_tunnel_destroy (t);
3179 tunnel_send_destroy (t);
3186 * Core handler for mesh network traffic going from the origin to a peer
3188 * @param cls closure
3189 * @param peer peer identity this notification is about
3190 * @param message message
3191 * @param atsi performance data
3192 * @param atsi_count number of records in 'atsi'
3193 * @return GNUNET_OK to keep the connection open,
3194 * GNUNET_SYSERR to close it (signal serious error)
3197 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3198 const struct GNUNET_MessageHeader *message,
3199 const struct GNUNET_ATS_Information *atsi,
3200 unsigned int atsi_count)
3202 struct GNUNET_MESH_Unicast *msg;
3203 struct MeshTunnel *t;
3207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3209 size = ntohs (message->size);
3211 sizeof (struct GNUNET_MESH_Unicast) +
3212 sizeof (struct GNUNET_MessageHeader))
3217 msg = (struct GNUNET_MESH_Unicast *) message;
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3219 ntohs (msg[1].header.type));
3220 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3223 /* TODO notify back: we don't know this tunnel */
3224 GNUNET_break_op (0);
3227 tunnel_reset_timeout (t);
3228 pid = GNUNET_PEER_search (&msg->destination);
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3232 " it's for us! sending to clients...\n");
3233 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3237 " not for us, retransmitting...\n");
3238 send_message (message, tree_get_first_hop (t->tree, pid));
3244 * Core handler for mesh network traffic going from the origin to all peers
3246 * @param cls closure
3247 * @param message message
3248 * @param peer peer identity this notification is about
3249 * @param atsi performance data
3250 * @param atsi_count number of records in 'atsi'
3251 * @return GNUNET_OK to keep the connection open,
3252 * GNUNET_SYSERR to close it (signal serious error)
3254 * TODO: Check who we got this from, to validate route.
3257 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3258 const struct GNUNET_MessageHeader *message,
3259 const struct GNUNET_ATS_Information *atsi,
3260 unsigned int atsi_count)
3262 struct GNUNET_MESH_Multicast *msg;
3263 struct MeshTunnel *t;
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3268 size = ntohs (message->size);
3269 if (sizeof (struct GNUNET_MESH_Multicast) +
3270 sizeof (struct GNUNET_MessageHeader) > size)
3272 GNUNET_break_op (0);
3275 msg = (struct GNUNET_MESH_Multicast *) message;
3276 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3280 /* TODO notify that we dont know that tunnel */
3281 GNUNET_break_op (0);
3284 if (t->mid == ntohl (msg->mid))
3286 /* FIXME: already seen this packet, log dropping */
3287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3288 " Already seen mid %u, DROPPING!\n", t->mid);
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3294 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3296 t->mid = ntohl (msg->mid);
3297 tunnel_reset_timeout (t);
3299 /* Transmit to locally interested clients */
3300 if (NULL != t->peers &&
3301 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3303 send_subscribed_clients (message, &msg[1].header);
3305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3306 if (ntohl (msg->ttl) == 0)
3308 /* FIXME: ttl is 0, log dropping */
3309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3312 tunnel_send_multicast (t, message, GNUNET_NO);
3318 * Core handler for mesh network traffic toward the owner of a tunnel
3320 * @param cls closure
3321 * @param message message
3322 * @param peer peer identity this notification is about
3323 * @param atsi performance data
3324 * @param atsi_count number of records in 'atsi'
3326 * @return GNUNET_OK to keep the connection open,
3327 * GNUNET_SYSERR to close it (signal serious error)
3330 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3331 const struct GNUNET_MessageHeader *message,
3332 const struct GNUNET_ATS_Information *atsi,
3333 unsigned int atsi_count)
3335 struct GNUNET_MESH_ToOrigin *msg;
3336 struct GNUNET_PeerIdentity id;
3337 struct MeshPeerInfo *peer_info;
3338 struct MeshTunnel *t;
3341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3343 size = ntohs (message->size);
3344 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3345 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3347 GNUNET_break_op (0);
3350 msg = (struct GNUNET_MESH_ToOrigin *) message;
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3352 ntohs (msg[1].header.type));
3353 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3357 /* TODO notify that we dont know this tunnel (whom)? */
3358 GNUNET_break_op (0);
3362 if (t->id.oid == myid)
3365 struct GNUNET_MESH_ToOrigin *copy;
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3368 " it's for us! sending to clients...\n");
3369 if (NULL == t->owner)
3371 /* got data packet for ownerless tunnel */
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3373 GNUNET_break_op (0);
3376 /* TODO signature verification */
3377 memcpy (cbuf, message, size);
3378 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3379 copy->tid = htonl (t->local_tid);
3380 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3381 ©->header, GNUNET_YES);
3384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3385 " not for us, retransmitting...\n");
3387 peer_info = peer_info_get (&msg->oid);
3388 if (NULL == peer_info)
3390 /* unknown origin of tunnel */
3394 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3395 send_message (message, &id);
3402 * Core handler for path ACKs
3404 * @param cls closure
3405 * @param message message
3406 * @param peer peer identity this notification is about
3407 * @param atsi performance data
3408 * @param atsi_count number of records in 'atsi'
3410 * @return GNUNET_OK to keep the connection open,
3411 * GNUNET_SYSERR to close it (signal serious error)
3414 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3415 const struct GNUNET_MessageHeader *message,
3416 const struct GNUNET_ATS_Information *atsi,
3417 unsigned int atsi_count)
3419 struct GNUNET_MESH_PathACK *msg;
3420 struct GNUNET_PeerIdentity id;
3421 struct MeshPeerInfo *peer_info;
3422 struct MeshPeerPath *p;
3423 struct MeshTunnel *t;
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3426 GNUNET_i2s (&my_full_id));
3427 msg = (struct GNUNET_MESH_PathACK *) message;
3428 t = tunnel_get (&msg->oid, msg->tid);
3431 /* TODO notify that we don't know the tunnel */
3435 peer_info = peer_info_get (&msg->peer_id);
3437 /* Add paths to peers? */
3438 p = tree_get_path_to_peer (t->tree, peer_info->id);
3441 path_add_to_peers (p, GNUNET_YES);
3449 /* Message for us? */
3450 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3453 if (NULL == t->owner)
3455 GNUNET_break_op (0);
3458 if (NULL != t->dht_get_type)
3460 GNUNET_DHT_get_stop (t->dht_get_type);
3461 t->dht_get_type = NULL;
3463 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3465 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3466 send_client_peer_connected (t, peer_info->id);
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3472 " not for us, retransmitting...\n");
3473 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3474 peer_info = peer_info_get (&msg->oid);
3475 if (NULL == peer_info)
3477 /* If we know the tunnel, we should DEFINITELY know the peer */
3481 send_message (message, &id);
3487 * Functions to handle messages from core
3489 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3490 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3491 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3492 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3493 sizeof (struct GNUNET_MESH_PathBroken)},
3494 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3495 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3496 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3497 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3498 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3499 sizeof (struct GNUNET_MESH_PathACK)},
3505 /******************************************************************************/
3506 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3507 /******************************************************************************/
3510 * deregister_app: iterator for removing each application registered by a client
3512 * @param cls closure
3513 * @param key the hash of the application id (used to access the hashmap)
3514 * @param value the value stored at the key (client)
3516 * @return GNUNET_OK on success
3519 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3521 GNUNET_break (GNUNET_YES ==
3522 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3529 * notify_client_connection_failure: notify a client that the connection to the
3530 * requested remote peer is not possible (for instance, no route found)
3531 * Function called when the socket is ready to queue more data. "buf" will be
3532 * NULL and "size" zero if the socket was closed for writing in the meantime.
3534 * @param cls closure
3535 * @param size number of bytes available in buf
3536 * @param buf where the callee should write the message
3537 * @return number of bytes written to buf
3540 notify_client_connection_failure (void *cls, size_t size, void *buf)
3543 struct MeshPeerInfo *peer_info;
3544 struct GNUNET_MESH_PeerControl *msg;
3545 struct GNUNET_PeerIdentity id;
3547 if (0 == size && NULL == buf)
3549 // TODO retry? cancel?
3553 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3554 peer_info = (struct MeshPeerInfo *) cls;
3555 msg = (struct GNUNET_MESH_PeerControl *) buf;
3556 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3557 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3558 // msg->tunnel_id = htonl(peer_info->t->tid);
3559 GNUNET_PEER_resolve (peer_info->id, &id);
3560 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3568 * Send keepalive packets for a peer
3570 * @param cls Closure (tunnel for which to send the keepalive).
3571 * @param tc Notification context.
3573 * TODO: implement explicit multicast keepalive?
3576 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3578 struct MeshTunnel *t = cls;
3579 struct GNUNET_MessageHeader *payload;
3580 struct GNUNET_MESH_Multicast *msg;
3582 sizeof (struct GNUNET_MESH_Multicast) +
3583 sizeof (struct GNUNET_MessageHeader);
3586 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3590 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 "sending keepalive for tunnel %d\n", t->id.tid);
3595 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3596 msg->header.size = htons (size);
3597 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3598 msg->oid = my_full_id;
3599 msg->tid = htonl (t->id.tid);
3600 msg->ttl = htonl (DEFAULT_TTL);
3601 msg->mid = htonl (t->mid + 1);
3603 payload = (struct GNUNET_MessageHeader *) &msg[1];
3604 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3605 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3606 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3608 t->path_refresh_task =
3609 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3615 * Function to process paths received for a new peer addition. The recorded
3616 * paths form the initial tunnel, which can be optimized later.
3617 * Called on each result obtained for the DHT search.
3619 * @param cls closure
3620 * @param exp when will this value expire
3621 * @param key key of the result
3622 * @param get_path path of the get request
3623 * @param get_path_length lenght of get_path
3624 * @param put_path path of the put request
3625 * @param put_path_length length of the put_path
3626 * @param type type of the result
3627 * @param size number of bytes in data
3628 * @param data pointer to the result data
3630 * TODO: re-issue the request after certain time? cancel after X results?
3633 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3634 const GNUNET_HashCode * key,
3635 const struct GNUNET_PeerIdentity *get_path,
3636 unsigned int get_path_length,
3637 const struct GNUNET_PeerIdentity *put_path,
3638 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3639 size_t size, const void *data)
3641 struct MeshPathInfo *path_info = cls;
3642 struct MeshPeerPath *p;
3643 struct GNUNET_PeerIdentity pi;
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3647 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3650 p = path_build_from_dht (get_path, get_path_length, put_path,
3652 path_add_to_peers (p, GNUNET_NO);
3654 for (i = 0; i < path_info->peer->ntunnels; i++)
3656 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3657 peer_info_connect (path_info->peer, path_info->t);
3665 * Function to process paths received for a new peer addition. The recorded
3666 * paths form the initial tunnel, which can be optimized later.
3667 * Called on each result obtained for the DHT search.
3669 * @param cls closure
3670 * @param exp when will this value expire
3671 * @param key key of the result
3672 * @param get_path path of the get request
3673 * @param get_path_length lenght of get_path
3674 * @param put_path path of the put request
3675 * @param put_path_length length of the put_path
3676 * @param type type of the result
3677 * @param size number of bytes in data
3678 * @param data pointer to the result data
3681 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3682 const GNUNET_HashCode * key,
3683 const struct GNUNET_PeerIdentity *get_path,
3684 unsigned int get_path_length,
3685 const struct GNUNET_PeerIdentity *put_path,
3686 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3687 size_t size, const void *data)
3689 const struct GNUNET_PeerIdentity *pi = data;
3690 struct MeshTunnel *t = cls;
3691 struct MeshPeerInfo *peer_info;
3692 struct MeshPeerPath *p;
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3695 if (size != sizeof (struct GNUNET_PeerIdentity))
3697 GNUNET_break_op (0);
3700 GNUNET_assert (NULL != t->owner);
3701 peer_info = peer_info_get (pi);
3702 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3704 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3706 p = path_build_from_dht (get_path, get_path_length, put_path,
3708 path_add_to_peers (p, GNUNET_NO);
3710 tunnel_add_peer (t, peer_info);
3711 peer_info_connect (peer_info, t);
3715 /******************************************************************************/
3716 /********************* MESH LOCAL HANDLES **************************/
3717 /******************************************************************************/
3721 * Handler for client disconnection
3723 * @param cls closure
3724 * @param client identification of the client; NULL
3725 * for the last call when the server is destroyed
3728 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3730 struct MeshClient *c;
3731 struct MeshClient *next;
3733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3742 if (c->handle != client)
3744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3750 GNUNET_SERVER_client_drop (c->handle);
3751 c->shutting_down = GNUNET_YES;
3752 GNUNET_assert (NULL != c->own_tunnels);
3753 GNUNET_assert (NULL != c->incoming_tunnels);
3754 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3755 &tunnel_destroy_iterator, c);
3756 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3757 &tunnel_destroy_iterator, c);
3758 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3759 &tunnel_destroy_iterator, c);
3760 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3761 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3762 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3764 /* deregister clients applications */
3765 if (NULL != c->apps)
3767 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3768 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3770 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3771 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3773 GNUNET_SCHEDULER_cancel (announce_applications_task);
3774 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3776 if (NULL != c->types)
3777 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3779 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3790 * Handler for new clients
3792 * @param cls closure
3793 * @param client identification of the client
3794 * @param message the actual message, which includes messages the client wants
3797 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3798 const struct GNUNET_MessageHeader *message)
3800 struct GNUNET_MESH_ClientConnect *cc_msg;
3801 struct MeshClient *c;
3802 GNUNET_MESH_ApplicationType *a;
3809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3810 /* Check data sanity */
3811 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3812 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3813 ntypes = ntohs (cc_msg->types);
3814 napps = ntohs (cc_msg->applications);
3816 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3823 /* Create new client structure */
3824 c = GNUNET_malloc (sizeof (struct MeshClient));
3825 c->id = next_client_id++;
3826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3828 GNUNET_SERVER_client_keep (client);
3829 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3832 GNUNET_MESH_ApplicationType at;
3835 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3836 for (i = 0; i < napps; i++)
3839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3840 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3841 /* store in clients hashmap */
3842 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3843 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3844 /* store in global hashmap, for announcements */
3845 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3846 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3848 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3849 announce_applications_task =
3850 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3858 t = (uint16_t *) & a[napps];
3859 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3860 for (i = 0; i < ntypes; i++)
3863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3864 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3866 /* store in clients hashmap */
3867 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3868 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3869 /* store in global hashmap */
3870 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3871 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3875 " client has %u+%u subscriptions\n", napps, ntypes);
3877 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3878 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3879 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3880 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3881 GNUNET_SERVER_notification_context_add (nc, client);
3883 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3889 * Handler for requests of new tunnels
3891 * @param cls closure
3892 * @param client identification of the client
3893 * @param message the actual message
3896 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3897 const struct GNUNET_MessageHeader *message)
3899 struct GNUNET_MESH_TunnelMessage *t_msg;
3900 struct MeshTunnel *t;
3901 struct MeshClient *c;
3902 GNUNET_HashCode hash;
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3906 /* Sanity check for client registration */
3907 if (NULL == (c = client_get (client)))
3910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3915 /* Message sanity check */
3916 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3923 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3924 /* Sanity check for tunnel numbering */
3925 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3928 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3931 /* Sanity check for duplicate tunnel IDs */
3932 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3939 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3940 while (NULL != tunnel_get_by_pi (myid, next_tid))
3941 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3942 t->id.tid = next_tid++;
3943 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3945 t->local_tid = ntohl (t_msg->tunnel_id);
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3947 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3949 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3951 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3953 GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
3954 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3961 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3963 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3964 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3970 t->tree = tree_new (myid);
3972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
3973 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3979 * Handler for requests of deleting tunnels
3981 * @param cls closure
3982 * @param client identification of the client
3983 * @param message the actual message
3986 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3987 const struct GNUNET_MessageHeader *message)
3989 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3990 struct MeshClient *c;
3991 struct MeshTunnel *t;
3992 MESH_TunnelNumber tid;
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3995 "Got a DESTROY TUNNEL from client!\n");
3997 /* Sanity check for client registration */
3998 if (NULL == (c = client_get (client)))
4001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4004 /* Message sanity check */
4005 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4008 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4012 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4014 /* Retrieve tunnel */
4015 tid = ntohl (tunnel_msg->tunnel_id);
4016 t = tunnel_get_by_local_id(c, tid);
4020 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4024 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4026 client_ignore_tunnel (c, t);
4028 // TODO: when to destroy incoming tunnel?
4029 if (t->nclients == 0)
4031 GNUNET_assert (GNUNET_YES ==
4032 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4034 GNUNET_assert (GNUNET_YES ==
4035 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4036 &my_full_id.hashPubKey,
4040 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4043 send_client_tunnel_disconnect(t, c);
4044 client_delete_tunnel(c, t);
4046 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4048 tunnel_send_destroy (t);
4050 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4056 * Handler for connection requests to new peers
4058 * @param cls closure
4059 * @param client identification of the client
4060 * @param message the actual message (PeerControl)
4063 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4064 const struct GNUNET_MessageHeader *message)
4066 struct GNUNET_MESH_PeerControl *peer_msg;
4067 struct MeshPeerInfo *peer_info;
4068 struct MeshClient *c;
4069 struct MeshTunnel *t;
4070 MESH_TunnelNumber tid;
4072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4073 /* Sanity check for client registration */
4074 if (NULL == (c = client_get (client)))
4077 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4081 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4082 /* Sanity check for message size */
4083 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4086 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4090 /* Tunnel exists? */
4091 tid = ntohl (peer_msg->tunnel_id);
4092 t = tunnel_get_by_local_id (c, tid);
4096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4100 /* Does client own tunnel? */
4101 if (t->owner->handle != client)
4104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4108 GNUNET_i2s (&peer_msg->peer));
4109 peer_info = peer_info_get (&peer_msg->peer);
4111 tunnel_add_peer (t, peer_info);
4112 peer_info_connect (peer_info, t);
4114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4120 * Handler for disconnection requests of peers in a tunnel
4122 * @param cls closure
4123 * @param client identification of the client
4124 * @param message the actual message (PeerControl)
4127 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4128 const struct GNUNET_MessageHeader *message)
4130 struct GNUNET_MESH_PeerControl *peer_msg;
4131 struct MeshPeerInfo *peer_info;
4132 struct MeshClient *c;
4133 struct MeshTunnel *t;
4134 MESH_TunnelNumber tid;
4136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4137 /* Sanity check for client registration */
4138 if (NULL == (c = client_get (client)))
4141 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4144 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4145 /* Sanity check for message size */
4146 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4153 /* Tunnel exists? */
4154 tid = ntohl (peer_msg->tunnel_id);
4155 t = tunnel_get_by_local_id (c, tid);
4159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4164 /* Does client own tunnel? */
4165 if (t->owner->handle != client)
4168 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4173 GNUNET_i2s (&peer_msg->peer));
4174 /* Is the peer in the tunnel? */
4176 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4177 if (NULL == peer_info)
4180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4184 /* Ok, delete peer from tunnel */
4185 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4186 &peer_msg->peer.hashPubKey);
4188 send_destroy_path (t, peer_info->id);
4189 tunnel_delete_peer (t, peer_info->id);
4190 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4196 * Handler for connection requests to new peers by type
4198 * @param cls closure
4199 * @param client identification of the client
4200 * @param message the actual message (ConnectPeerByType)
4203 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4204 const struct GNUNET_MessageHeader *message)
4206 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4207 struct MeshClient *c;
4208 struct MeshTunnel *t;
4209 GNUNET_HashCode hash;
4210 MESH_TunnelNumber tid;
4212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4213 /* Sanity check for client registration */
4214 if (NULL == (c = client_get (client)))
4217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4221 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4222 /* Sanity check for message size */
4223 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4224 ntohs (connect_msg->header.size))
4227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4231 /* Tunnel exists? */
4232 tid = ntohl (connect_msg->tunnel_id);
4233 t = tunnel_get_by_local_id (c, tid);
4237 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4241 /* Does client own tunnel? */
4242 if (t->owner->handle != client)
4245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4249 /* Do WE have the service? */
4250 t->type = ntohl (connect_msg->type);
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4252 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4253 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4256 /* Yes! Fast forward, add ourselves to the tunnel and send the
4257 * good news to the client, and alert the destination client of
4258 * an incoming tunnel.
4260 * FIXME send a path create to self, avoid code duplication
4262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4263 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4264 peer_info_get (&my_full_id),
4265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4268 send_client_peer_connected (t, myid);
4269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4270 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4272 t->local_tid_dest = next_local_tid++;
4273 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4274 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4275 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4279 /* Ok, lets find a peer offering the service */
4280 if (NULL != t->dht_get_type)
4282 GNUNET_DHT_get_stop (t->dht_get_type);
4284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4285 GNUNET_h2s (&hash));
4287 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
4288 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4289 GNUNET_DHT_RO_RECORD_ROUTE |
4290 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4291 &dht_get_type_handler, t);
4293 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4299 * Handler for client traffic directed to one peer
4301 * @param cls closure
4302 * @param client identification of the client
4303 * @param message the actual message
4306 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4307 const struct GNUNET_MessageHeader *message)
4309 struct MeshClient *c;
4310 struct MeshTunnel *t;
4311 struct MeshPeerInfo *pi;
4312 struct GNUNET_MESH_Unicast *data_msg;
4313 MESH_TunnelNumber tid;
4316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4317 "Got a unicast request from a client!\n");
4319 /* Sanity check for client registration */
4320 if (NULL == (c = client_get (client)))
4323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4326 data_msg = (struct GNUNET_MESH_Unicast *) message;
4327 /* Sanity check for message size */
4328 size = ntohs (message->size);
4329 if (sizeof (struct GNUNET_MESH_Unicast) +
4330 sizeof (struct GNUNET_MessageHeader) > size)
4333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4337 /* Tunnel exists? */
4338 tid = ntohl (data_msg->tid);
4339 t = tunnel_get_by_local_id (c, tid);
4343 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4347 /* Is it a local tunnel? Then, does client own the tunnel? */
4348 if (t->owner->handle != client)
4351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4355 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4356 &data_msg->destination.hashPubKey);
4357 /* Is the selected peer in the tunnel? */
4361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4365 /* Ok, everything is correct, send the message
4366 * (pretend we got it from a mesh peer)
4369 char buf[ntohs (message->size)] GNUNET_ALIGN;
4370 struct GNUNET_MESH_Unicast *copy;
4372 /* Work around const limitation */
4373 copy = (struct GNUNET_MESH_Unicast *) buf;
4374 memcpy (buf, data_msg, size);
4375 copy->oid = my_full_id;
4376 copy->tid = htonl (t->id.tid);
4377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4378 " calling generic handler...\n");
4379 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4381 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4387 * Handler for client traffic directed to the origin
4389 * @param cls closure
4390 * @param client identification of the client
4391 * @param message the actual message
4394 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4395 const struct GNUNET_MessageHeader *message)
4397 struct GNUNET_MESH_ToOrigin *data_msg;
4398 struct GNUNET_PeerIdentity id;
4399 struct MeshClient *c;
4400 struct MeshTunnel *t;
4401 MESH_TunnelNumber tid;
4404 /* Sanity check for client registration */
4405 if (NULL == (c = client_get (client)))
4408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4411 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4412 /* Sanity check for message size */
4413 size = ntohs (message->size);
4414 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4415 sizeof (struct GNUNET_MessageHeader) > size)
4418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4422 /* Tunnel exists? */
4423 tid = ntohl (data_msg->tid);
4424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4425 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4426 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4432 t = tunnel_get_by_local_id (c, tid);
4436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4440 /* It should be sent by someone who has this as incoming tunnel. */
4441 if (-1 == client_knows_tunnel (c, t))
4444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4447 GNUNET_PEER_resolve (t->id.oid, &id);
4449 /* Ok, everything is correct, send the message
4450 * (pretend we got it from a mesh peer)
4453 char buf[ntohs (message->size)] GNUNET_ALIGN;
4454 struct GNUNET_MESH_ToOrigin *copy;
4456 /* Work around const limitation */
4457 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4458 memcpy (buf, data_msg, size);
4460 copy->tid = htonl (t->id.tid);
4461 copy->sender = my_full_id;
4462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4463 " calling generic handler...\n");
4464 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4472 * Handler for client traffic directed to all peers in a tunnel
4474 * @param cls closure
4475 * @param client identification of the client
4476 * @param message the actual message
4479 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4480 const struct GNUNET_MessageHeader *message)
4482 struct MeshClient *c;
4483 struct MeshTunnel *t;
4484 struct GNUNET_MESH_Multicast *data_msg;
4485 MESH_TunnelNumber tid;
4487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4488 "Got a multicast request from a client!\n");
4490 /* Sanity check for client registration */
4491 if (NULL == (c = client_get (client)))
4494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4497 data_msg = (struct GNUNET_MESH_Multicast *) message;
4498 /* Sanity check for message size */
4499 if (sizeof (struct GNUNET_MESH_Multicast) +
4500 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4507 /* Tunnel exists? */
4508 tid = ntohl (data_msg->tid);
4509 t = tunnel_get_by_local_id (c, tid);
4513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4517 /* Does client own tunnel? */
4518 if (t->owner->handle != client)
4521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4526 char buf[ntohs (message->size)] GNUNET_ALIGN;
4527 struct GNUNET_MESH_Multicast *copy;
4529 copy = (struct GNUNET_MESH_Multicast *) buf;
4530 memcpy (buf, message, ntohs (message->size));
4531 copy->oid = my_full_id;
4532 copy->tid = htonl (t->id.tid);
4533 copy->ttl = htonl (DEFAULT_TTL);
4534 copy->mid = htonl (t->mid + 1);
4535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4536 " calling generic handler...\n");
4537 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4540 /* receive done gets called when last copy is sent to a neighbor */
4545 * Functions to handle messages from clients
4547 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4548 {&handle_local_new_client, NULL,
4549 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4550 {&handle_local_tunnel_create, NULL,
4551 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4552 sizeof (struct GNUNET_MESH_TunnelMessage)},
4553 {&handle_local_tunnel_destroy, NULL,
4554 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4555 sizeof (struct GNUNET_MESH_TunnelMessage)},
4556 {&handle_local_connect_add, NULL,
4557 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4558 sizeof (struct GNUNET_MESH_PeerControl)},
4559 {&handle_local_connect_del, NULL,
4560 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4561 sizeof (struct GNUNET_MESH_PeerControl)},
4562 {&handle_local_connect_by_type, NULL,
4563 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4564 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4565 {&handle_local_unicast, NULL,
4566 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4567 {&handle_local_to_origin, NULL,
4568 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4569 {&handle_local_multicast, NULL,
4570 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4576 * To be called on core init/fail.
4578 * @param cls service closure
4579 * @param server handle to the server for this service
4580 * @param identity the public identity of this peer
4583 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4584 const struct GNUNET_PeerIdentity *identity)
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4587 core_handle = server;
4588 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4592 GNUNET_SCHEDULER_shutdown ();
4598 * Method called whenever a given peer connects.
4600 * @param cls closure
4601 * @param peer peer identity this notification is about
4602 * @param atsi performance data for the connection
4603 * @param atsi_count number of records in 'atsi'
4606 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4607 const struct GNUNET_ATS_Information *atsi,
4608 unsigned int atsi_count)
4610 struct MeshPeerInfo *peer_info;
4611 struct MeshPeerPath *path;
4613 DEBUG_CONN ("Peer connected\n");
4614 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4615 peer_info = peer_info_get (peer);
4616 if (myid == peer_info->id)
4618 DEBUG_CONN (" (self)\n");
4623 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4625 path = path_new (2);
4626 path->peers[0] = myid;
4627 path->peers[1] = peer_info->id;
4628 GNUNET_PEER_change_rc (myid, 1);
4629 GNUNET_PEER_change_rc (peer_info->id, 1);
4630 peer_info_add_path (peer_info, path, GNUNET_YES);
4635 * Method called whenever a peer disconnects.
4637 * @param cls closure
4638 * @param peer peer identity this notification is about
4641 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4643 struct MeshPeerInfo *pi;
4646 DEBUG_CONN ("Peer disconnected\n");
4647 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4653 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4655 /* TODO: notify that the transmission failed */
4656 peer_info_cancel_transmission (pi, i);
4658 peer_info_remove_path (pi, pi->id, myid);
4661 DEBUG_CONN (" (self)\n");
4667 /******************************************************************************/
4668 /************************ MAIN FUNCTIONS ****************************/
4669 /******************************************************************************/
4672 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4674 * @param cls closure
4675 * @param key current key code
4676 * @param value value in the hash map
4677 * @return GNUNET_YES if we should continue to iterate,
4681 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4683 struct MeshTunnel *t = value;
4690 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4692 * @param cls closure
4693 * @param key current key code
4694 * @param value value in the hash map
4695 * @return GNUNET_YES if we should continue to iterate,
4699 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4701 struct MeshPeerInfo *p = value;
4703 peer_info_destroy (p);
4708 * Task run during shutdown.
4714 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4718 if (core_handle != NULL)
4720 GNUNET_CORE_disconnect (core_handle);
4723 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4724 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4725 if (dht_handle != NULL)
4727 GNUNET_DHT_disconnect (dht_handle);
4732 GNUNET_SERVER_notification_context_destroy (nc);
4735 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4737 GNUNET_SCHEDULER_cancel (announce_id_task);
4738 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4744 * Process mesh requests.
4746 * @param cls closure
4747 * @param server the initialized server
4748 * @param c configuration to use
4751 run (void *cls, struct GNUNET_SERVER_Handle *server,
4752 const struct GNUNET_CONFIGURATION_Handle *c)
4754 struct MeshPeerInfo *peer;
4755 struct MeshPeerPath *p;
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4759 server_handle = server;
4760 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4761 CORE_QUEUE_SIZE, /* queue size */
4762 NULL, /* Closure passed to MESH functions */
4763 &core_init, /* Call core_init once connected */
4764 &core_connect, /* Handle connects */
4765 &core_disconnect, /* remove peers on disconnects */
4766 NULL, /* Don't notify about all incoming messages */
4767 GNUNET_NO, /* For header only in notification */
4768 NULL, /* Don't notify about all outbound messages */
4769 GNUNET_NO, /* For header-only out notification */
4770 core_handlers); /* Register these handlers */
4772 if (core_handle == NULL)
4775 GNUNET_SCHEDULER_shutdown ();
4780 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4783 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4785 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4786 GNUNET_SCHEDULER_shutdown ();
4789 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4790 GNUNET_free (keyfile);
4791 if (my_private_key == NULL)
4793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4794 _("Mesh service could not access hostkey. Exiting.\n"));
4795 GNUNET_SCHEDULER_shutdown ();
4798 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4799 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4800 &my_full_id.hashPubKey);
4801 myid = GNUNET_PEER_intern (&my_full_id);
4803 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4810 dht_handle = GNUNET_DHT_connect (c, 64);
4811 if (dht_handle == NULL)
4817 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4819 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4820 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4821 peers = GNUNET_CONTAINER_multihashmap_create (32);
4822 applications = GNUNET_CONTAINER_multihashmap_create (32);
4823 types = GNUNET_CONTAINER_multihashmap_create (32);
4825 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4826 nc = GNUNET_SERVER_notification_context_create (server_handle,
4828 GNUNET_SERVER_disconnect_notify (server_handle,
4829 &handle_local_client_disconnect, NULL);
4833 clients_tail = NULL;
4836 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4837 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4839 /* Create a peer_info for the local peer */
4840 peer = peer_info_get (&my_full_id);
4843 GNUNET_PEER_change_rc (myid, 1);
4844 peer_info_add_path (peer, p, GNUNET_YES);
4846 /* Scheduled the task to clean up when shutdown is called */
4847 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4854 * The main function for the mesh service.
4856 * @param argc number of arguments from the command line
4857 * @param argv command line arguments
4858 * @return 0 ok, 1 on error
4861 main (int argc, char *const *argv)
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4868 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");