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 tc task context
480 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
484 if (NULL != tc && GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s\n", s);
492 /******************************************************************************/
493 /*********************** GLOBAL VARIABLES ****************************/
494 /******************************************************************************/
499 static struct MeshClient *clients;
500 static struct MeshClient *clients_tail;
503 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
505 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
508 * Tunnels incoming, indexed by MESH_TunnelNumber
509 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
511 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
514 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
516 static struct GNUNET_CONTAINER_MultiHashMap *peers;
519 * Handle to communicate with core
521 static struct GNUNET_CORE_Handle *core_handle;
524 * Handle to communicate with transport
526 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
531 static struct GNUNET_DHT_Handle *dht_handle;
536 static struct GNUNET_SERVER_Handle *server_handle;
539 * Notification context, to send messages to local clients
541 static struct GNUNET_SERVER_NotificationContext *nc;
544 * Local peer own ID (memory efficient handle)
546 static GNUNET_PEER_Id myid;
549 * Local peer own ID (full value)
551 static struct GNUNET_PeerIdentity my_full_id;
556 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
561 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
564 * Tunnel ID for the next created tunnel (global tunnel number)
566 static MESH_TunnelNumber next_tid;
569 * Tunnel ID for the next incoming tunnel (local tunnel number)
571 static MESH_TunnelNumber next_local_tid;
574 * All application types provided by this peer
576 static struct GNUNET_CONTAINER_MultiHashMap *applications;
579 * All message types clients of this peer are interested in
581 static struct GNUNET_CONTAINER_MultiHashMap *types;
584 * Task to periodically announce provided applications
586 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
589 * Task to periodically announce itself in the network
591 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
594 * Next ID to assign to a client
596 unsigned int next_client_id;
600 /******************************************************************************/
601 /************************ ITERATORS ****************************/
602 /******************************************************************************/
604 /* FIXME move iterators here */
607 /******************************************************************************/
608 /************************ PERIODIC FUNCTIONS ****************************/
609 /******************************************************************************/
612 * Announce iterator over for each application provided by the peer
615 * @param key current key code
616 * @param value value in the hash map
617 * @return GNUNET_YES if we should continue to
622 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
624 /* FIXME are hashes in multihash map equal on all aquitectures? */
625 GNUNET_DHT_put (dht_handle, key, 10U,
626 GNUNET_DHT_RO_RECORD_ROUTE |
627 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
628 sizeof (struct GNUNET_PeerIdentity),
629 (const char *) &my_full_id,
631 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
632 &mesh_debug, "DHT_put for app completed");
634 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
636 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 10U, /* 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 */
700 &mesh_debug, "DHT_put for id completed");
702 NULL, /* Continuation */
703 NULL); /* Continuation closure */
706 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
711 * Function to process paths received for a new peer addition. The recorded
712 * paths form the initial tunnel, which can be optimized later.
713 * Called on each result obtained for the DHT search.
716 * @param exp when will this value expire
717 * @param key key of the result
718 * @param type type of the result
719 * @param size number of bytes in data
720 * @param data pointer to the result data
723 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
724 const GNUNET_HashCode * key,
725 const struct GNUNET_PeerIdentity *get_path,
726 unsigned int get_path_length,
727 const struct GNUNET_PeerIdentity *put_path,
728 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
729 size_t size, const void *data);
732 /******************************************************************************/
733 /****************** GENERAL HELPER FUNCTIONS ************************/
734 /******************************************************************************/
737 * Search for a tunnel by global ID using full PeerIdentities
739 * @param oid owner of the tunnel
740 * @param tid global tunnel number
742 * @return tunnel handler, NULL if doesn't exist
744 static struct MeshTunnel *
745 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
749 * Delete an active client from the tunnel.
755 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
758 * Notify a tunnel that a connection has broken that affects at least
761 * @param t Tunnel affected.
762 * @param p1 Peer that got disconnected from p2.
763 * @param p2 Peer that got disconnected from p1.
765 * @return Short ID of the peer disconnected (either p1 or p2).
766 * 0 if the tunnel remained unaffected.
768 static GNUNET_PEER_Id
769 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
774 * Check if client has registered with the service and has not disconnected
776 * @param client the client to check
778 * @return non-NULL if client exists in the global DLL
780 static struct MeshClient *
781 client_get (struct GNUNET_SERVER_Client *client)
783 struct MeshClient *c;
788 if (c->handle == client)
797 * Checks if a given client has subscribed to certain message type
799 * @param message_type Type of message to check
800 * @param c Client to check
802 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
807 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
811 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
812 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
817 * Allow a client to send more data after transmitting a multicast message
818 * which some neighbor has not yet accepted altough a reasonable time has
821 * @param cls Closure (DataDescriptor containing the task identifier)
822 * @param tc Task Context
824 * FIXME reference counter cshould be just int
827 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
829 struct MeshData *mdata = cls;
831 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
833 GNUNET_assert (NULL != mdata->reference_counter);
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
836 *(mdata->reference_counter));
837 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
838 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
843 * Check whether client wants traffic from a tunnel.
845 * @param c Client to check.
846 * @param t Tunnel to be found.
848 * @return GNUNET_YES if client knows tunnel.
850 * TODO look in client hashmap
853 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
857 for (i = 0; i < t->nclients; i++)
858 if (t->clients[i] == c)
865 * Check whether client has been informed about a tunnel.
867 * @param c Client to check.
868 * @param t Tunnel to be found.
870 * @return GNUNET_YES if client knows tunnel.
872 * TODO look in client hashmap
875 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
879 for (i = 0; i < t->nignore; i++)
880 if (t->ignore[i] == c)
882 return client_wants_tunnel(c, t);
887 * Marks a client as uninterested in traffic from the tunnel, updating both
888 * client and tunnel to reflect this.
890 * @param c Client that doesn't want traffic anymore.
891 * @param t Tunnel which should be ignored.
893 * FIXME when to delete an incoming tunnel?
896 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
898 GNUNET_HashCode hash;
900 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
901 GNUNET_break (GNUNET_YES ==
902 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
904 GNUNET_break (GNUNET_YES ==
905 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
906 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
907 tunnel_delete_active_client (t, c);
908 GNUNET_array_append (t->ignore, t->nignore, c);
913 * Deletes a tunnel from a client (either owner or destination). To be used on
914 * tunnel destroy, otherwise, use client_ignore_tunnel.
916 * @param c Client whose tunnel to delete.
917 * @param t Tunnel which should be deleted.
920 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
922 GNUNET_HashCode hash;
926 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
927 GNUNET_assert (GNUNET_YES ==
928 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
934 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
936 GNUNET_assert (GNUNET_YES ==
937 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
941 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
950 * Send the message to all clients that have subscribed to its type
952 * @param msg Pointer to the message itself
953 * @param payload Pointer to the payload of the message.
954 * @return number of clients this message was sent to
957 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
958 const struct GNUNET_MessageHeader *payload)
960 struct GNUNET_PeerIdentity *oid;
961 struct MeshClient *c;
962 struct MeshTunnel *t;
963 MESH_TunnelNumber *tid;
966 char cbuf[htons (msg->size)];
968 type = ntohs (payload->type);
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
972 memcpy (cbuf, msg, sizeof (cbuf));
973 switch (htons (msg->type))
975 struct GNUNET_MESH_Unicast *uc;
976 struct GNUNET_MESH_Multicast *mc;
977 struct GNUNET_MESH_ToOrigin *to;
979 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
980 uc = (struct GNUNET_MESH_Unicast *) cbuf;
984 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
985 mc = (struct GNUNET_MESH_Multicast *) cbuf;
989 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
990 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
998 t = tunnel_get (oid, ntohl (*tid));
1005 for (count = 0, c = clients; c != NULL; c = c->next)
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1008 if (client_is_subscribed (type, c))
1010 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1014 *tid = htonl (t->local_tid);
1018 if (GNUNET_NO == client_knows_tunnel (c, t))
1020 /* This client doesn't know the tunnel */
1021 struct GNUNET_MESH_TunnelNotification tmsg;
1022 GNUNET_HashCode hash;
1024 tmsg.header.size = htons (sizeof (tmsg));
1025 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1026 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1027 tmsg.tunnel_id = htonl (t->local_tid_dest);
1028 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1029 &tmsg.header, GNUNET_NO);
1030 GNUNET_array_append (t->clients, t->nclients, c);
1031 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1033 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1034 c->incoming_tunnels, &hash, t,
1035 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1037 *tid = htonl (t->local_tid_dest);
1040 /* Check if the client wants to get traffic from the tunnel */
1041 if (GNUNET_NO == client_wants_tunnel(c, t))
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1045 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1046 (struct GNUNET_MessageHeader
1047 *) cbuf, GNUNET_YES);
1055 * Notify the client that owns the tunnel that a peer has connected to it
1056 * (the requested path to it has been confirmed).
1058 * @param t Tunnel whose owner to notify
1059 * @param id Short id of the peer that has connected
1062 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1064 struct GNUNET_MESH_PeerControl pc;
1066 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1067 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1068 pc.tunnel_id = htonl (t->local_tid);
1069 GNUNET_PEER_resolve (id, &pc.peer);
1070 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1076 * Notify all clients (not depending on registration status) that the incoming
1077 * tunnel is no longer valid.
1079 * @param t Tunnel that was destroyed.
1082 send_clients_tunnel_destroy (struct MeshTunnel *t)
1084 struct GNUNET_MESH_TunnelMessage msg;
1086 msg.header.size = htons (sizeof (msg));
1087 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1088 msg.tunnel_id = htonl (t->local_tid_dest);
1089 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1094 * Notify clients of tunnel disconnections, if needed.
1095 * In case the origin disconnects, the destination clients get a tunnel destroy
1096 * notification. If the last destination disconnects (only one remaining client
1097 * in tunnel), the origin gets a (local ID) peer disconnected.
1098 * Note that the function must be called BEFORE removing the client from
1101 * @param t Tunnel that was destroyed.
1102 * @param c Client that disconnected.
1105 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1111 struct GNUNET_MESH_TunnelMessage msg;
1113 msg.header.size = htons (sizeof (msg));
1114 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1115 msg.tunnel_id = htonl (t->local_tid_dest);
1116 for (i = 0; i < t->nclients; i++)
1117 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1118 &msg.header, GNUNET_NO);
1120 // FIXME when to disconnect an incoming tunnel?
1121 else if (1 == t->nclients && NULL != t->owner)
1123 struct GNUNET_MESH_PeerControl msg;
1125 msg.header.size = htons (sizeof (msg));
1126 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1127 msg.tunnel_id = htonl (t->local_tid);
1128 msg.peer = my_full_id;
1129 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1130 &msg.header, GNUNET_NO);
1136 * Function called to notify a client about the socket
1137 * being ready to queue more data. "buf" will be
1138 * NULL and "size" zero if the socket was closed for
1139 * writing in the meantime.
1141 * @param cls closure
1142 * @param size number of bytes available in buf
1143 * @param buf where the callee should write the message
1144 * @return number of bytes written to buf
1147 send_core_create_path (void *cls, size_t size, void *buf);
1151 * Function called to notify a client about the socket
1152 * being ready to queue more data. "buf" will be
1153 * NULL and "size" zero if the socket was closed for
1154 * writing in the meantime.
1156 * @param cls closure (data itself)
1157 * @param size number of bytes available in buf
1158 * @param buf where the callee should write the message
1160 * @return number of bytes written to buf
1163 send_core_data_multicast (void *cls, size_t size, void *buf);
1167 * Decrements the reference counter and frees all resources if needed
1169 * @param mesh_data Data Descriptor used in a multicast message.
1170 * Freed no longer needed (last message).
1173 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1175 /* Make sure it's a multicast packet */
1176 GNUNET_assert (NULL != mesh_data->reference_counter);
1178 if (0 == --(*(mesh_data->reference_counter)))
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1181 if (NULL != mesh_data->task)
1183 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1185 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1187 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1189 GNUNET_free (mesh_data->task);
1191 GNUNET_free (mesh_data->reference_counter);
1192 GNUNET_free (mesh_data->data);
1193 GNUNET_free (mesh_data);
1199 * Cancel a core transmission that was already requested and free all resources
1200 * associated to the request.
1202 * @param peer PeeInfo of the peer whose transmission is cancelled.
1203 * @param i Position of the transmission to be cancelled.
1206 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
1208 if (NULL != peer->core_transmit[i])
1210 struct MeshTransmissionDescriptor *dd;
1211 struct MeshPathInfo *path_info;
1215 struct GNUNET_PeerIdentity id;
1217 GNUNET_PEER_resolve (peer->id, &id);
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 " Cancelling data transmission at %s [%u]\n",
1220 GNUNET_i2s (&id), i);
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " message type %u\n",
1225 /* TODO: notify that tranmission has failed */
1226 switch (peer->types[i])
1228 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1229 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1230 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
1232 dd = peer->infos[i];
1233 data_descriptor_decrement_multicast (dd->mesh_data);
1235 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
1237 path_info = peer->infos[i];
1238 path_destroy (path_info->path);
1242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
1244 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
1245 peer->core_transmit[i] = NULL;
1246 GNUNET_free (peer->infos[i]);
1252 * Get a unused CORE slot to transmit a message to a peer. If all the slots
1253 * are used, cancel one and return it's position.
1255 * @param peer PeerInfo of the neighbor we want to transmit to.
1257 * @return The index of an available slot to transmit to the neighbor.
1260 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1264 for (i = 0; peer->core_transmit[i]; i++)
1266 if (i == (CORE_QUEUE_SIZE - 1))
1268 /* All positions are taken! Overwriting! */
1270 peer_info_cancel_transmission (peer, 0);
1279 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1280 * and insert it in the appropiate structures if the peer is not known yet.
1282 * @param peer Full identity of the peer.
1284 * @return Existing or newly created peer info.
1286 static struct MeshPeerInfo *
1287 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1289 struct MeshPeerInfo *peer_info;
1291 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1292 if (NULL == peer_info)
1295 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1296 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1297 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1298 peer_info->id = GNUNET_PEER_intern (peer);
1306 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1307 * and insert it in the appropiate structures if the peer is not known yet.
1309 * @param peer Short identity of the peer.
1311 * @return Existing or newly created peer info.
1313 static struct MeshPeerInfo *
1314 peer_info_get_short (const GNUNET_PEER_Id peer)
1316 struct GNUNET_PeerIdentity id;
1318 GNUNET_PEER_resolve (peer, &id);
1319 return peer_info_get (&id);
1324 * Iterator to remove the tunnel from the list of tunnels a peer participates
1327 * @param cls Closure (tunnel info)
1328 * @param key GNUNET_PeerIdentity of the peer (unused)
1329 * @param value PeerInfo of the peer
1331 * @return always GNUNET_YES, to keep iterating
1334 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1336 struct MeshTunnel *t = cls;
1337 struct MeshPeerInfo *peer = value;
1340 for (i = 0; i < peer->ntunnels; i++)
1343 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1346 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1347 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1356 * Core callback to write a
1358 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1359 * @param size Number of bytes available in buf.
1360 * @param buf Where the to write the message.
1362 * @return number of bytes written to buf
1365 send_core_data_raw (void *cls, size_t size, void *buf)
1367 struct MeshTransmissionDescriptor *info = cls;
1368 struct GNUNET_MessageHeader *msg;
1371 GNUNET_assert (NULL != info);
1372 GNUNET_assert (NULL != info->mesh_data);
1373 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1374 total_size = ntohs (msg->size);
1376 if (total_size > size)
1378 struct GNUNET_PeerIdentity id;
1380 GNUNET_PEER_resolve (info->peer->id, &id);
1381 info->peer->core_transmit[info->handler_n] =
1382 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1383 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1384 size, &send_core_data_raw, info);
1387 info->peer->core_transmit[info->handler_n] = NULL;
1388 memcpy (buf, msg, total_size);
1389 GNUNET_free (info->mesh_data);
1396 * Sends an already built message to a peer, properly registrating
1397 * all used resources.
1399 * @param message Message to send. Fucntion makes a copy of it.
1400 * @param peer Short ID of the neighbor whom to send the message.
1405 send_message (const struct GNUNET_MessageHeader *message,
1406 const struct GNUNET_PeerIdentity *peer)
1408 struct MeshTransmissionDescriptor *info;
1409 struct MeshPeerInfo *neighbor;
1410 struct MeshPeerPath *p;
1414 // GNUNET_TRANSPORT_try_connect();
1416 size = ntohs (message->size);
1417 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1418 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1419 info->mesh_data->data = GNUNET_malloc (size);
1420 memcpy (info->mesh_data->data, message, size);
1421 info->mesh_data->data_len = size;
1422 neighbor = peer_info_get (peer);
1423 for (p = neighbor->path_head; NULL != p; p = p->next)
1433 GNUNET_free (info->mesh_data->data);
1434 GNUNET_free (info->mesh_data);
1438 i = peer_info_transmit_slot (neighbor);
1439 info->handler_n = i;
1440 info->peer = neighbor;
1441 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1442 neighbor->infos[i] = info;
1443 neighbor->core_transmit[i] =
1444 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1445 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1446 size, &send_core_data_raw, info);
1452 * Sends a CREATE PATH message for a path to a peer, properly registrating
1453 * all used resources.
1455 * @param peer PeerInfo of the final peer for whom this path is being created.
1456 * @param p Path itself.
1457 * @param t Tunnel for which the path is created.
1460 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1461 struct MeshTunnel *t)
1463 struct GNUNET_PeerIdentity id;
1464 struct MeshPathInfo *path_info;
1465 struct MeshPeerInfo *neighbor;
1470 p = tree_get_path_to_peer (t->tree, peer->id);
1477 for (i = 0; i < p->length; i++)
1479 if (p->peers[i] == myid)
1482 if (i >= p->length - 1)
1488 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1490 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1491 path_info->path = p;
1493 neighbor = peer_info_get (&id);
1494 path_info->peer = neighbor;
1495 path_info->pos = peer_info_transmit_slot (neighbor);
1496 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1497 neighbor->infos[path_info->pos] = path_info;
1498 neighbor->core_transmit[path_info->pos] =
1499 GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1502 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1504 sizeof (struct GNUNET_MESH_ManipulatePath) +
1505 (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1506 &send_core_create_path, /* callback */
1507 path_info); /* cls */
1512 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1514 * @param t Tunnel whose path to destroy.
1515 * @param destination Short ID of the peer to whom the path to destroy.
1518 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1520 struct MeshPeerPath *p;
1523 p = tree_get_path_to_peer (t->tree, destination);
1529 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1530 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1532 struct GNUNET_MESH_ManipulatePath *msg;
1533 struct GNUNET_PeerIdentity *pi;
1537 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1538 msg->header.size = htons (size);
1539 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1540 msg->tid = htonl (t->id.tid);
1541 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1542 for (i = 0; i < p->length; i++)
1544 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1546 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1553 * Try to establish a new connection to this peer.
1554 * Use the best path for the given tunnel.
1555 * If the peer doesn't have any path to it yet, try to get one.
1556 * If the peer already has some path, send a CREATE PATH towards it.
1558 * @param peer PeerInfo of the peer.
1559 * @param t Tunnel for which to create the path, if possible.
1562 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1564 struct MeshPeerPath *p;
1565 struct MeshPathInfo *path_info;
1567 if (NULL != peer->path_head)
1569 p = tree_get_path_to_peer (t->tree, peer->id);
1576 // FIXME always send create path to self
1579 send_create_path (peer, p, t);
1583 GNUNET_HashCode hash;
1586 send_client_peer_connected (t, myid);
1587 t->local_tid_dest = next_local_tid++;
1588 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1591 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1592 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1599 else if (NULL == peer->dhtget)
1601 struct GNUNET_PeerIdentity id;
1603 GNUNET_PEER_resolve (peer->id, &id);
1604 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1605 path_info->peer = peer;
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1609 peer->dhtgetcls = path_info;
1610 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1611 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1612 GNUNET_BLOCK_TYPE_TEST, /* type */
1613 &id.hashPubKey, /* key to search */
1614 10U, /* replication level */
1615 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1616 0, /* xquery bits */
1617 &dht_get_id_handler, path_info);
1619 /* Otherwise, there is no path but the DHT get is already started. */
1624 * Task to delay the connection of a peer
1626 * @param cls Closure (path info with tunnel and peer to connect).
1627 * Will be free'd on exection.
1628 * @param tc TaskContext
1631 peer_info_connect_task (void *cls,
1632 const struct GNUNET_SCHEDULER_TaskContext *tc)
1634 struct MeshPathInfo *path_info = cls;
1636 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1641 peer_info_connect (path_info->peer, path_info->t);
1647 * Destroy the peer_info and free any allocated resources linked to it
1649 * @param pi The peer_info to destroy.
1651 * @return GNUNET_OK on success
1654 peer_info_destroy (struct MeshPeerInfo *pi)
1656 struct GNUNET_PeerIdentity id;
1657 struct MeshPeerPath *p;
1658 struct MeshPeerPath *nextp;
1661 GNUNET_PEER_resolve (pi->id, &id);
1662 GNUNET_PEER_change_rc (pi->id, -1);
1665 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1668 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1669 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1671 if (NULL != pi->dhtget)
1673 GNUNET_DHT_get_stop (pi->dhtget);
1674 GNUNET_free (pi->dhtgetcls);
1676 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1678 peer_info_cancel_transmission (pi, i);
1684 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1694 * Remove all paths that rely on a direct connection between p1 and p2
1695 * from the peer itself and notify all tunnels about it.
1697 * @param peer PeerInfo of affected peer.
1698 * @param p1 GNUNET_PEER_Id of one peer.
1699 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1702 * TODO: optimize (see below)
1705 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1708 struct MeshPeerPath *p;
1709 struct MeshPeerPath *aux;
1710 struct MeshPeerInfo *peer_d;
1712 unsigned int destroyed;
1718 p = peer->path_head;
1722 for (i = 0; i < (p->length - 1); i++)
1724 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1725 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1727 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1738 for (i = 0; i < peer->ntunnels; i++)
1740 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1744 * Problem: one or more peers have been deleted from the tunnel tree.
1745 * We don't know who they are to try to add them again.
1746 * We need to try to find a new path for each of the disconnected peers.
1747 * Some of them might already have a path to reach them that does not
1748 * involve p1 and p2. Adding all anew might render in a better tree than
1749 * the trivial immediate fix.
1751 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1752 * its children will be reachable trough him.
1754 peer_d = peer_info_get_short (d);
1757 for (p = peer_d->path_head; NULL != p; p = p->next)
1759 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1767 /* No callback, as peer will be already disconnected and a connection
1768 * scheduled by tunnel_notify_connection_broken.
1770 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1774 peer_info_connect (peer_d, peer->tunnels[i]);
1781 * Add the path to the peer and update the path used to reach it in case this
1784 * @param peer_info Destination peer to add the path to.
1785 * @param path New path to add. Last peer must be the peer in arg 1.
1786 * Path will be either used of freed if already known.
1787 * @param trusted Do we trust that this path is real?
1790 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1793 struct MeshPeerPath *aux;
1797 if ((NULL == peer_info) || (NULL == path))
1800 path_destroy (path);
1803 if (path->peers[path->length - 1] != peer_info->id)
1806 path_destroy (path);
1809 if (path->length <= 2 && GNUNET_NO == trusted)
1811 /* Only allow CORE to tell us about direct paths */
1812 path_destroy (path);
1815 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1816 for (l = 1; l < path->length; l++)
1818 if (path->peers[l] == myid)
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1821 for (l2 = 0; l2 < path->length - l; l2++)
1823 path->peers[l2] = path->peers[l + l2];
1828 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1833 struct GNUNET_PeerIdentity id;
1835 GNUNET_PEER_resolve (peer_info->id, &id);
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1837 path->length, GNUNET_i2s (&id));
1840 l = path_get_length (path);
1847 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1848 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1850 l2 = path_get_length (aux);
1853 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1854 peer_info->path_tail, aux, path);
1859 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1861 path_destroy (path);
1866 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1873 * Add the path to the origin peer and update the path used to reach it in case
1874 * this is the shortest.
1875 * The path is given in peer_info -> destination, therefore we turn the path
1876 * upside down first.
1878 * @param peer_info Peer to add the path to, being the origin of the path.
1879 * @param path New path to add after being inversed.
1880 * @param trusted Do we trust that this path is real?
1883 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1884 struct MeshPeerPath *path, int trusted)
1887 peer_info_add_path (peer_info, path, trusted);
1892 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1893 * to obtain a local peer -> destination path and interning the peer ids.
1895 * @return Newly allocated and created path
1897 static struct MeshPeerPath *
1898 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1899 unsigned int get_path_length,
1900 const struct GNUNET_PeerIdentity *put_path,
1901 unsigned int put_path_length)
1903 struct MeshPeerPath *p;
1909 GNUNET_PEER_change_rc (myid, 1);
1910 i = get_path_length;
1911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1912 for (i--; i >= 0; i--)
1914 id = GNUNET_PEER_intern (&get_path[i]);
1915 if (p->length > 0 && id == p->peers[p->length - 1])
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1918 GNUNET_PEER_change_rc (id, -1);
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1923 GNUNET_i2s (&get_path[i]));
1925 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1926 p->peers[p->length - 1] = id;
1929 i = put_path_length;
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1931 for (i--; i >= 0; i--)
1933 id = GNUNET_PEER_intern (&put_path[i]);
1936 /* PUT path went through us, so discard the path up until now and start
1937 * from here to get a much shorter (and loop-free) path.
1942 if (p->length > 0 && id == p->peers[p->length - 1])
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1945 GNUNET_PEER_change_rc (id, -1);
1949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1950 GNUNET_i2s (&put_path[i]));
1952 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1953 p->peers[p->length - 1] = id;
1957 if (get_path_length > 0)
1958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1959 GNUNET_i2s (&get_path[0]));
1960 if (put_path_length > 0)
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1962 GNUNET_i2s (&put_path[0]));
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1965 for (i = 0; i < p->length; i++)
1967 struct GNUNET_PeerIdentity peer_id;
1969 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1971 GNUNET_i2s (&peer_id));
1979 * Adds a path to the peer_infos of all the peers in the path
1981 * @param p Path to process.
1982 * @param confirmed Whether we know if the path works or not. FIXME use
1985 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1989 /* TODO: invert and add */
1990 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1991 for (i++; i < p->length; i++)
1993 struct MeshPeerInfo *aux;
1994 struct MeshPeerPath *copy;
1996 aux = peer_info_get_short (p->peers[i]);
1997 copy = path_duplicate (p);
1998 copy->length = i + 1;
1999 peer_info_add_path (aux, copy, GNUNET_NO);
2005 * Send keepalive packets for a peer
2007 * @param cls Closure (tunnel for which to send the keepalive).
2008 * @param tc Notification context.
2010 * TODO: implement explicit multicast keepalive?
2013 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2017 * Search for a tunnel among the incoming tunnels
2019 * @param tid the local id of the tunnel
2021 * @return tunnel handler, NULL if doesn't exist
2023 static struct MeshTunnel *
2024 tunnel_get_incoming (MESH_TunnelNumber tid)
2026 GNUNET_HashCode hash;
2028 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2029 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2030 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2035 * Search for a tunnel among the tunnels for a client
2037 * @param c the client whose tunnels to search in
2038 * @param tid the local id of the tunnel
2040 * @return tunnel handler, NULL if doesn't exist
2042 static struct MeshTunnel *
2043 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2045 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2047 return tunnel_get_incoming (tid);
2051 GNUNET_HashCode hash;
2053 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2054 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2060 * Search for a tunnel by global ID using PEER_ID
2062 * @param pi owner of the tunnel
2063 * @param tid global tunnel number
2065 * @return tunnel handler, NULL if doesn't exist
2067 static struct MeshTunnel *
2068 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2070 struct MESH_TunnelID id;
2071 GNUNET_HashCode hash;
2076 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2077 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2082 * Search for a tunnel by global ID using full PeerIdentities
2084 * @param oid owner of the tunnel
2085 * @param tid global tunnel number
2087 * @return tunnel handler, NULL if doesn't exist
2089 static struct MeshTunnel *
2090 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2092 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2097 * Delete an active client from the tunnel.
2103 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2107 for (i = 0; i < t->nclients; i++)
2109 if (t->clients[i] == c)
2111 t->clients[i] = t->clients[t->nclients - 1];
2112 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2120 * Delete an ignored client from the tunnel.
2126 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2130 for (i = 0; i < t->nignore; i++)
2132 if (t->ignore[i] == c)
2134 t->ignore[i] = t->ignore[t->nignore - 1];
2135 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2143 * Delete a client from the tunnel. It should be only done on
2144 * client disconnection, otherwise use client_ignore_tunnel.
2150 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2152 tunnel_delete_ignored_client (t, c);
2153 tunnel_delete_active_client (t, c);
2158 * Callback used to notify a client owner of a tunnel that a peer has
2159 * disconnected, most likely because of a path change.
2161 * @param cls Closure (tunnel this notification is about).
2162 * @param peer_id Short ID of disconnected peer.
2165 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2167 struct MeshTunnel *t = cls;
2168 struct MeshPeerInfo *peer;
2169 struct MeshPathInfo *path_info;
2171 if (NULL != t->owner && NULL != nc)
2173 struct GNUNET_MESH_PeerControl msg;
2175 msg.header.size = htons (sizeof (msg));
2176 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2177 msg.tunnel_id = htonl (t->local_tid);
2178 GNUNET_PEER_resolve (peer_id, &msg.peer);
2179 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2180 &msg.header, GNUNET_NO);
2182 peer = peer_info_get_short (peer_id);
2183 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2184 path_info->peer = peer;
2186 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2191 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2192 * needed rescources.
2193 * If peer already exists, reevaluate shortest path and change if different.
2195 * @param t Tunnel we want to add a new peer to
2196 * @param peer PeerInfo of the peer being added
2200 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2202 struct GNUNET_PeerIdentity id;
2203 struct MeshPeerPath *best_p;
2204 struct MeshPeerPath *p;
2205 unsigned int best_cost;
2208 GNUNET_PEER_resolve (peer->id, &id);
2210 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2213 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2214 GNUNET_assert (GNUNET_OK ==
2215 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2220 if (NULL != (p = peer->path_head))
2223 best_cost = tree_get_path_cost (t->tree, p);
2226 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2233 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2234 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2235 t->path_refresh_task =
2236 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2240 /* Start a DHT get */
2241 peer_info_connect (peer, t);
2246 * Add a path to a tunnel which we don't own, just to remember the next hop.
2247 * If destination node was already in the tunnel, the first hop information
2248 * will be replaced with the new path.
2250 * @param t Tunnel we want to add a new peer to
2251 * @param p Path to add
2252 * @param own_pos Position of local node in path.
2256 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2257 unsigned int own_pos)
2259 struct GNUNET_PeerIdentity id;
2261 GNUNET_assert (0 != own_pos);
2262 tree_add_path (t->tree, p, NULL, NULL);
2263 if (own_pos < p->length - 1)
2265 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2266 tree_update_first_hops (t->tree, myid, &id);
2272 * Notifies a tunnel that a connection has broken that affects at least
2273 * some of its peers. Sends a notification towards the root of the tree.
2274 * In case the peer is the owner of the tree, notifies the client that owns
2275 * the tunnel and tries to reconnect.
2277 * @param t Tunnel affected.
2278 * @param p1 Peer that got disconnected from p2.
2279 * @param p2 Peer that got disconnected from p1.
2281 * @return Short ID of the peer disconnected (either p1 or p2).
2282 * 0 if the tunnel remained unaffected.
2284 static GNUNET_PEER_Id
2285 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2291 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2293 if (myid != p1 && myid != p2)
2299 if (tree_get_predecessor (t->tree) != 0)
2301 /* We are the peer still connected, notify owner of the disconnection. */
2302 struct GNUNET_MESH_PathBroken msg;
2303 struct GNUNET_PeerIdentity neighbor;
2305 msg.header.size = htons (sizeof (msg));
2306 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2307 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2308 msg.tid = htonl (t->id.tid);
2309 msg.peer1 = my_full_id;
2310 GNUNET_PEER_resolve (pid, &msg.peer2);
2311 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2312 send_message (&msg.header, &neighbor);
2320 * Send a multicast packet to a neighbor.
2322 * @param cls Closure (Info about the multicast packet)
2323 * @param neighbor_id Short ID of the neighbor to send the packet to.
2326 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2328 struct MeshData *mdata = cls;
2329 struct MeshTransmissionDescriptor *info;
2330 struct GNUNET_PeerIdentity neighbor;
2333 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2335 info->mesh_data = mdata;
2336 (*(mdata->reference_counter)) ++;
2337 info->destination = neighbor_id;
2338 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2340 GNUNET_i2s (&neighbor));
2341 info->peer = peer_info_get (&neighbor);
2342 GNUNET_assert (NULL != info->peer);
2343 i = peer_info_transmit_slot (info->peer);
2344 info->handler_n = i;
2345 info->peer->infos[i] = info;
2346 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2347 info->peer->core_transmit[i] =
2348 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2349 GNUNET_TIME_UNIT_FOREVER_REL,
2350 &neighbor, info->mesh_data->data_len,
2351 &send_core_data_multicast, info);
2355 * Send a message in a tunnel in multicast, sending a copy to each child node
2356 * down the local one in the tunnel tree.
2358 * @param t Tunnel in which to send the data.
2359 * @param msg Message to be sent.
2360 * @param internal Has the service generated this message?
2363 tunnel_send_multicast (struct MeshTunnel *t,
2364 const struct GNUNET_MessageHeader *msg,
2367 struct MeshData *mdata;
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2370 " sending a multicast packet...\n");
2371 mdata = GNUNET_malloc (sizeof (struct MeshData));
2372 mdata->data_len = ntohs (msg->size);
2373 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2375 mdata->data = GNUNET_malloc (mdata->data_len);
2376 memcpy (mdata->data, msg, mdata->data_len);
2377 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2379 struct GNUNET_MESH_Multicast *mcast;
2381 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2382 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2384 ntohl (mcast->ttl));
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2390 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2391 && GNUNET_NO == internal)
2393 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2395 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2401 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2402 if (*(mdata->reference_counter) == 0)
2404 GNUNET_free (mdata->data);
2405 GNUNET_free (mdata->reference_counter);
2406 if (NULL != mdata->task)
2408 GNUNET_SCHEDULER_cancel(*(mdata->task));
2409 GNUNET_free (mdata->task);
2410 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2412 // FIXME change order?
2413 GNUNET_free (mdata);
2415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2416 " sending a multicast packet done\n");
2422 * Send a message to all peers in this tunnel that the tunnel is no longer
2425 * @param t The tunnel whose peers to notify.
2428 tunnel_send_destroy (struct MeshTunnel *t)
2430 struct GNUNET_MESH_TunnelDestroy msg;
2432 msg.header.size = htons (sizeof (msg));
2433 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2434 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2435 msg.tid = htonl (t->id.tid);
2436 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2442 * Destroy the tunnel and free any allocated resources linked to it.
2444 * @param t the tunnel to destroy
2446 * @return GNUNET_OK on success
2449 tunnel_destroy (struct MeshTunnel *t)
2451 struct MeshClient *c;
2452 struct MeshQueue *q;
2453 struct MeshQueue *qn;
2454 GNUNET_HashCode hash;
2465 struct GNUNET_PeerIdentity id;
2467 GNUNET_PEER_resolve (t->id.oid, &id);
2468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2469 GNUNET_i2s (&id), t->id.tid);
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2475 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2476 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2481 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2484 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2488 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2489 for (i = 0; i < t->nclients; i++)
2493 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2498 for (i = 0; i < t->nignore; i++)
2502 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2507 if (t->nclients > 0)
2510 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2514 GNUNET_free (t->clients);
2516 if (NULL != t->peers)
2518 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2520 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2525 if (NULL != q->data)
2526 GNUNET_free (q->data);
2530 /* TODO cancel core transmit ready in case it was active */
2532 tree_destroy (t->tree);
2533 if (NULL != t->dht_get_type)
2534 GNUNET_DHT_get_stop (t->dht_get_type);
2535 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2536 GNUNET_SCHEDULER_cancel (t->timeout_task);
2537 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2538 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2545 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2546 * that are no longer needed, as well as nodes of no longer reachable peers.
2547 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2549 * @param t Tunnel from which to remove the path.
2550 * @param peer Short id of the peer which should be removed.
2553 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2555 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2561 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2562 * client when the client disconnects. If the client is not the owner, the
2563 * owner will get notified if no more clients are in the tunnel and the client
2564 * get removed from the tunnel's list.
2566 * @param cls closure (client that is disconnecting)
2567 * @param key the hash of the local tunnel id (used to access the hashmap)
2568 * @param value the value stored at the key (tunnel to destroy)
2570 * @return GNUNET_OK on success
2573 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2575 struct MeshTunnel *t = value;
2576 struct MeshClient *c = cls;
2579 send_client_tunnel_disconnect(t, c);
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2584 tunnel_delete_client(t, c);
2585 client_delete_tunnel(c, t);
2588 tunnel_send_destroy(t);
2589 r = tunnel_destroy (t);
2595 * Timeout function, destroys tunnel if called
2597 * @param cls Closure (tunnel to destroy).
2598 * @param tc TaskContext
2601 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2603 struct MeshTunnel *t = cls;
2605 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2607 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2612 * Resets the tunnel timeout. Starts it if no timeout was running.
2614 * @param t Tunnel whose timeout to reset.
2617 tunnel_reset_timeout (struct MeshTunnel *t)
2619 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2620 GNUNET_SCHEDULER_cancel (t->timeout_task);
2622 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2623 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2627 /******************************************************************************/
2628 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2629 /******************************************************************************/
2632 * Function called to notify a client about the socket
2633 * being ready to queue more data. "buf" will be
2634 * NULL and "size" zero if the socket was closed for
2635 * writing in the meantime.
2637 * @param cls closure
2638 * @param size number of bytes available in buf
2639 * @param buf where the callee should write the message
2640 * @return number of bytes written to buf
2643 send_core_create_path (void *cls, size_t size, void *buf)
2645 struct MeshPathInfo *info = cls;
2646 struct GNUNET_MESH_ManipulatePath *msg;
2647 struct GNUNET_PeerIdentity *peer_ptr;
2648 struct MeshPeerInfo *peer = info->peer;
2649 struct MeshTunnel *t = info->t;
2650 struct MeshPeerPath *p = info->path;
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2656 sizeof (struct GNUNET_MESH_ManipulatePath) +
2657 p->length * sizeof (struct GNUNET_PeerIdentity);
2659 if (size < size_needed || NULL == buf)
2661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "create path retransmit!\n");
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " buf: %p\n", buf);
2663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " size: (%u/%u)\n", size,
2665 info->peer->core_transmit[info->pos] =
2666 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2667 GNUNET_TIME_UNIT_FOREVER_REL,
2668 tree_get_first_hop (t->tree,
2670 size_needed, &send_core_create_path,
2674 info->peer->core_transmit[info->pos] = NULL;
2677 struct GNUNET_PeerIdentity id;
2679 GNUNET_PEER_resolve (peer->id, &id);
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2681 " setting core_transmit %s [%u] to NULL\n",
2682 GNUNET_i2s (&id), info->pos);
2685 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2686 msg->header.size = htons (size_needed);
2687 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2688 msg->tid = ntohl (t->id.tid);
2690 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2691 for (i = 0; i < p->length; i++)
2693 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2700 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2706 * Function called to notify a client about the socket
2707 * being ready to queue more data. "buf" will be
2708 * NULL and "size" zero if the socket was closed for
2709 * writing in the meantime.
2711 * @param cls closure (data itself)
2712 * @param size number of bytes available in buf
2713 * @param buf where the callee should write the message
2715 * @return number of bytes written to buf
2718 send_core_data_multicast (void *cls, size_t size, void *buf)
2720 struct MeshTransmissionDescriptor *info = cls;
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2724 GNUNET_assert (NULL != info);
2725 GNUNET_assert (NULL != info->peer);
2726 total_size = info->mesh_data->data_len;
2727 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2729 if (total_size > size)
2732 struct GNUNET_PeerIdentity id;
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2735 "Multicast: retransmitting... (%u/%u)\n", size,
2737 GNUNET_PEER_resolve (info->peer->id, &id);
2738 info->peer->core_transmit[info->handler_n] =
2739 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2740 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2742 &send_core_data_multicast, info);
2745 info->peer->core_transmit[info->handler_n] = NULL;
2746 info->peer->infos[info->handler_n] = NULL;
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2748 memcpy (buf, info->mesh_data->data, total_size);
2751 struct GNUNET_MESH_Multicast *mc;
2752 struct GNUNET_MessageHeader *mh;
2755 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2757 mc = (struct GNUNET_MESH_Multicast *) mh;
2758 mh = (struct GNUNET_MessageHeader *) &mc[1];
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 " multicast, payload type %u\n", ntohs (mh->type));
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2762 " multicast, payload size %u\n", ntohs (mh->size));
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2771 data_descriptor_decrement_multicast (info->mesh_data);
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2780 * Function called to notify a client about the socket
2781 * being ready to queue more data. "buf" will be
2782 * NULL and "size" zero if the socket was closed for
2783 * writing in the meantime.
2785 * @param cls closure (MeshTransmissionDescriptor)
2786 * @param size number of bytes available in buf
2787 * @param buf where the callee should write the message
2788 * @return number of bytes written to buf
2791 send_core_path_ack (void *cls, size_t size, void *buf)
2793 struct MeshTransmissionDescriptor *info = cls;
2794 struct GNUNET_MESH_PathACK *msg = buf;
2796 GNUNET_assert (NULL != info);
2799 info->peer->core_transmit[info->handler_n] = NULL;
2801 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2806 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2807 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2808 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2809 msg->tid = htonl (info->origin->tid);
2810 msg->peer_id = my_full_id;
2812 /* TODO add signature */
2814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2815 return sizeof (struct GNUNET_MESH_PathACK);
2819 /******************************************************************************/
2820 /******************** MESH NETWORK HANDLERS **************************/
2821 /******************************************************************************/
2825 * Core handler for path creation
2827 * @param cls closure
2828 * @param message message
2829 * @param peer peer identity this notification is about
2830 * @param atsi performance data
2831 * @param atsi_count number of records in 'atsi'
2833 * @return GNUNET_OK to keep the connection open,
2834 * GNUNET_SYSERR to close it (signal serious error)
2837 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2838 const struct GNUNET_MessageHeader *message,
2839 const struct GNUNET_ATS_Information *atsi,
2840 unsigned int atsi_count)
2842 unsigned int own_pos;
2845 MESH_TunnelNumber tid;
2846 struct GNUNET_MESH_ManipulatePath *msg;
2847 struct GNUNET_PeerIdentity *pi;
2848 GNUNET_HashCode hash;
2849 struct MeshPeerPath *path;
2850 struct MeshPeerInfo *dest_peer_info;
2851 struct MeshPeerInfo *orig_peer_info;
2852 struct MeshTunnel *t;
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2855 "Received a path create msg [%s]\n",
2856 GNUNET_i2s (&my_full_id));
2857 size = ntohs (message->size);
2858 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2860 GNUNET_break_op (0);
2864 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2865 if (size % sizeof (struct GNUNET_PeerIdentity))
2867 GNUNET_break_op (0);
2870 size /= sizeof (struct GNUNET_PeerIdentity);
2873 GNUNET_break_op (0);
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2877 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2879 tid = ntohl (msg->tid);
2880 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2882 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2883 t = tunnel_get (pi, tid);
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2887 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2888 t->id.oid = GNUNET_PEER_intern (pi);
2890 while (NULL != tunnel_get_incoming (next_local_tid))
2891 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2892 t->local_tid_dest = next_local_tid++;
2893 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2894 t->tree = tree_new (t->id.oid);
2896 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2898 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2899 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2905 tunnel_reset_timeout (t);
2906 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2908 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2909 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2917 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2918 if (NULL == dest_peer_info)
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2921 " Creating PeerInfo for destination.\n");
2922 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2923 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2924 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2926 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2928 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2929 if (NULL == orig_peer_info)
2931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2932 " Creating PeerInfo for origin.\n");
2933 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2934 orig_peer_info->id = GNUNET_PEER_intern (pi);
2935 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2936 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2939 path = path_new (size);
2941 for (i = 0; i < size; i++)
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2944 GNUNET_i2s (&pi[i]));
2945 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2946 if (path->peers[i] == myid)
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2952 /* cannot be self, must be 'not found' */
2953 /* create path: self not found in path through self */
2954 GNUNET_break_op (0);
2955 path_destroy (path);
2956 /* FIXME error. destroy tunnel? leave for timeout? */
2959 path_add_to_peers (path, GNUNET_NO);
2960 tunnel_add_path (t, path, own_pos);
2961 if (own_pos == size - 1)
2963 /* It is for us! Send ack. */
2964 struct MeshTransmissionDescriptor *info;
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2968 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2969 if (NULL == t->peers)
2971 /* New tunnel! Notify clients on data. */
2972 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2974 GNUNET_break (GNUNET_OK ==
2975 GNUNET_CONTAINER_multihashmap_put (t->peers,
2976 &my_full_id.hashPubKey,
2979 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2980 /* FIXME use send_message */
2981 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2982 info->origin = &t->id;
2983 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2984 GNUNET_assert (NULL != info->peer);
2985 j = peer_info_transmit_slot (info->peer);
2986 info->handler_n = j;
2987 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2988 info->peer->infos[j] = info;
2989 info->peer->core_transmit[j] =
2990 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2991 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2992 sizeof (struct GNUNET_MESH_PathACK),
2993 &send_core_path_ack, info);
2997 struct MeshPeerPath *path2;
2999 /* It's for somebody else! Retransmit. */
3000 path2 = path_duplicate (path);
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3002 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3003 path2 = path_duplicate (path);
3004 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3005 send_create_path (dest_peer_info, path, t);
3012 * Core handler for path destruction
3014 * @param cls closure
3015 * @param message message
3016 * @param peer peer identity this notification is about
3017 * @param atsi performance data
3018 * @param atsi_count number of records in 'atsi'
3020 * @return GNUNET_OK to keep the connection open,
3021 * GNUNET_SYSERR to close it (signal serious error)
3024 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3025 const struct GNUNET_MessageHeader *message,
3026 const struct GNUNET_ATS_Information *atsi,
3027 unsigned int atsi_count)
3029 struct GNUNET_MESH_ManipulatePath *msg;
3030 struct GNUNET_PeerIdentity *pi;
3031 struct MeshPeerPath *path;
3032 struct MeshTunnel *t;
3033 unsigned int own_pos;
3037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3038 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3039 size = ntohs (message->size);
3040 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3042 GNUNET_break_op (0);
3046 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3047 if (size % sizeof (struct GNUNET_PeerIdentity))
3049 GNUNET_break_op (0);
3052 size /= sizeof (struct GNUNET_PeerIdentity);
3055 GNUNET_break_op (0);
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3060 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3061 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3063 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3065 t = tunnel_get (pi, ntohl (msg->tid));
3068 /* TODO notify back: we don't know this tunnel */
3069 GNUNET_break_op (0);
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3073 path = path_new (size);
3075 for (i = 0; i < size; i++)
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3078 GNUNET_i2s (&pi[i]));
3079 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3080 if (path->peers[i] == myid)
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3084 if (own_pos < path->length - 1)
3085 send_message (message, &pi[own_pos + 1]);
3087 send_client_tunnel_disconnect(t, NULL);
3089 tunnel_delete_peer (t, path->peers[path->length - 1]);
3090 path_destroy (path);
3096 * Core handler for notifications of broken paths
3098 * @param cls closure
3099 * @param message message
3100 * @param peer peer identity this notification is about
3101 * @param atsi performance data
3102 * @param atsi_count number of records in 'atsi'
3104 * @return GNUNET_OK to keep the connection open,
3105 * GNUNET_SYSERR to close it (signal serious error)
3108 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3109 const struct GNUNET_MessageHeader *message,
3110 const struct GNUNET_ATS_Information *atsi,
3111 unsigned int atsi_count)
3113 struct GNUNET_MESH_PathBroken *msg;
3114 struct MeshTunnel *t;
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3117 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3118 msg = (struct GNUNET_MESH_PathBroken *) message;
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3120 GNUNET_i2s (&msg->peer1));
3121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3122 GNUNET_i2s (&msg->peer2));
3123 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3126 GNUNET_break_op (0);
3129 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3130 GNUNET_PEER_search (&msg->peer2));
3137 * Core handler for tunnel destruction
3139 * @param cls closure
3140 * @param message message
3141 * @param peer peer identity this notification is about
3142 * @param atsi performance data
3143 * @param atsi_count number of records in 'atsi'
3145 * @return GNUNET_OK to keep the connection open,
3146 * GNUNET_SYSERR to close it (signal serious error)
3149 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3150 const struct GNUNET_MessageHeader *message,
3151 const struct GNUNET_ATS_Information *atsi,
3152 unsigned int atsi_count)
3154 struct GNUNET_MESH_TunnelDestroy *msg;
3155 struct MeshTunnel *t;
3157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3158 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3159 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3161 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3162 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3165 /* Probably already got the message from another path,
3166 * destroyed the tunnel and retransmitted to children.
3171 if (t->id.oid == myid)
3173 GNUNET_break_op (0);
3176 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3178 /* Tunnel was incoming, notify clients */
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3180 t->local_tid, t->local_tid_dest);
3181 send_clients_tunnel_destroy (t);
3183 tunnel_send_destroy (t);
3190 * Core handler for mesh network traffic going from the origin to a peer
3192 * @param cls closure
3193 * @param peer peer identity this notification is about
3194 * @param message message
3195 * @param atsi performance data
3196 * @param atsi_count number of records in 'atsi'
3197 * @return GNUNET_OK to keep the connection open,
3198 * GNUNET_SYSERR to close it (signal serious error)
3201 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3202 const struct GNUNET_MessageHeader *message,
3203 const struct GNUNET_ATS_Information *atsi,
3204 unsigned int atsi_count)
3206 struct GNUNET_MESH_Unicast *msg;
3207 struct MeshTunnel *t;
3211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3213 size = ntohs (message->size);
3215 sizeof (struct GNUNET_MESH_Unicast) +
3216 sizeof (struct GNUNET_MessageHeader))
3221 msg = (struct GNUNET_MESH_Unicast *) message;
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3223 ntohs (msg[1].header.type));
3224 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3227 /* TODO notify back: we don't know this tunnel */
3228 GNUNET_break_op (0);
3231 tunnel_reset_timeout (t);
3232 pid = GNUNET_PEER_search (&msg->destination);
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3236 " it's for us! sending to clients...\n");
3237 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3241 " not for us, retransmitting...\n");
3242 send_message (message, tree_get_first_hop (t->tree, pid));
3248 * Core handler for mesh network traffic going from the origin to all peers
3250 * @param cls closure
3251 * @param message message
3252 * @param peer peer identity this notification is about
3253 * @param atsi performance data
3254 * @param atsi_count number of records in 'atsi'
3255 * @return GNUNET_OK to keep the connection open,
3256 * GNUNET_SYSERR to close it (signal serious error)
3258 * TODO: Check who we got this from, to validate route.
3261 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3262 const struct GNUNET_MessageHeader *message,
3263 const struct GNUNET_ATS_Information *atsi,
3264 unsigned int atsi_count)
3266 struct GNUNET_MESH_Multicast *msg;
3267 struct MeshTunnel *t;
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3272 size = ntohs (message->size);
3273 if (sizeof (struct GNUNET_MESH_Multicast) +
3274 sizeof (struct GNUNET_MessageHeader) > size)
3276 GNUNET_break_op (0);
3279 msg = (struct GNUNET_MESH_Multicast *) message;
3280 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3284 /* TODO notify that we dont know that tunnel */
3285 GNUNET_break_op (0);
3288 if (t->mid == ntohl (msg->mid))
3290 /* FIXME: already seen this packet, log dropping */
3291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3292 " Already seen mid %u, DROPPING!\n", t->mid);
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3300 t->mid = ntohl (msg->mid);
3301 tunnel_reset_timeout (t);
3303 /* Transmit to locally interested clients */
3304 if (NULL != t->peers &&
3305 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3307 send_subscribed_clients (message, &msg[1].header);
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3310 if (ntohl (msg->ttl) == 0)
3312 /* FIXME: ttl is 0, log dropping */
3313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3316 tunnel_send_multicast (t, message, GNUNET_NO);
3322 * Core handler for mesh network traffic toward the owner of a tunnel
3324 * @param cls closure
3325 * @param message message
3326 * @param peer peer identity this notification is about
3327 * @param atsi performance data
3328 * @param atsi_count number of records in 'atsi'
3330 * @return GNUNET_OK to keep the connection open,
3331 * GNUNET_SYSERR to close it (signal serious error)
3334 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3335 const struct GNUNET_MessageHeader *message,
3336 const struct GNUNET_ATS_Information *atsi,
3337 unsigned int atsi_count)
3339 struct GNUNET_MESH_ToOrigin *msg;
3340 struct GNUNET_PeerIdentity id;
3341 struct MeshPeerInfo *peer_info;
3342 struct MeshTunnel *t;
3345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3347 size = ntohs (message->size);
3348 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3349 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3351 GNUNET_break_op (0);
3354 msg = (struct GNUNET_MESH_ToOrigin *) message;
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3356 ntohs (msg[1].header.type));
3357 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3361 /* TODO notify that we dont know this tunnel (whom)? */
3362 GNUNET_break_op (0);
3366 if (t->id.oid == myid)
3369 struct GNUNET_MESH_ToOrigin *copy;
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3372 " it's for us! sending to clients...\n");
3373 if (NULL == t->owner)
3375 /* got data packet for ownerless tunnel */
3376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3377 GNUNET_break_op (0);
3380 /* TODO signature verification */
3381 memcpy (cbuf, message, size);
3382 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3383 copy->tid = htonl (t->local_tid);
3384 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3385 ©->header, GNUNET_YES);
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3389 " not for us, retransmitting...\n");
3391 peer_info = peer_info_get (&msg->oid);
3392 if (NULL == peer_info)
3394 /* unknown origin of tunnel */
3398 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3399 send_message (message, &id);
3406 * Core handler for path ACKs
3408 * @param cls closure
3409 * @param message message
3410 * @param peer peer identity this notification is about
3411 * @param atsi performance data
3412 * @param atsi_count number of records in 'atsi'
3414 * @return GNUNET_OK to keep the connection open,
3415 * GNUNET_SYSERR to close it (signal serious error)
3418 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3419 const struct GNUNET_MessageHeader *message,
3420 const struct GNUNET_ATS_Information *atsi,
3421 unsigned int atsi_count)
3423 struct GNUNET_MESH_PathACK *msg;
3424 struct GNUNET_PeerIdentity id;
3425 struct MeshPeerInfo *peer_info;
3426 struct MeshPeerPath *p;
3427 struct MeshTunnel *t;
3429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3430 GNUNET_i2s (&my_full_id));
3431 msg = (struct GNUNET_MESH_PathACK *) message;
3432 t = tunnel_get (&msg->oid, msg->tid);
3435 /* TODO notify that we don't know the tunnel */
3439 peer_info = peer_info_get (&msg->peer_id);
3441 /* Add paths to peers? */
3442 p = tree_get_path_to_peer (t->tree, peer_info->id);
3445 path_add_to_peers (p, GNUNET_YES);
3453 /* Message for us? */
3454 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3457 if (NULL == t->owner)
3459 GNUNET_break_op (0);
3462 if (NULL != t->dht_get_type)
3464 GNUNET_DHT_get_stop (t->dht_get_type);
3465 t->dht_get_type = NULL;
3467 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3469 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3470 send_client_peer_connected (t, peer_info->id);
3475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3476 " not for us, retransmitting...\n");
3477 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3478 peer_info = peer_info_get (&msg->oid);
3479 if (NULL == peer_info)
3481 /* If we know the tunnel, we should DEFINITELY know the peer */
3485 send_message (message, &id);
3491 * Functions to handle messages from core
3493 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3494 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3495 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3496 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3497 sizeof (struct GNUNET_MESH_PathBroken)},
3498 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3499 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3500 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3501 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3502 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3503 sizeof (struct GNUNET_MESH_PathACK)},
3509 /******************************************************************************/
3510 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3511 /******************************************************************************/
3514 * deregister_app: iterator for removing each application registered by a client
3516 * @param cls closure
3517 * @param key the hash of the application id (used to access the hashmap)
3518 * @param value the value stored at the key (client)
3520 * @return GNUNET_OK on success
3523 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3525 GNUNET_break (GNUNET_YES ==
3526 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3533 * notify_client_connection_failure: notify a client that the connection to the
3534 * requested remote peer is not possible (for instance, no route found)
3535 * Function called when the socket is ready to queue more data. "buf" will be
3536 * NULL and "size" zero if the socket was closed for writing in the meantime.
3538 * @param cls closure
3539 * @param size number of bytes available in buf
3540 * @param buf where the callee should write the message
3541 * @return number of bytes written to buf
3544 notify_client_connection_failure (void *cls, size_t size, void *buf)
3547 struct MeshPeerInfo *peer_info;
3548 struct GNUNET_MESH_PeerControl *msg;
3549 struct GNUNET_PeerIdentity id;
3551 if (0 == size && NULL == buf)
3553 // TODO retry? cancel?
3557 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3558 peer_info = (struct MeshPeerInfo *) cls;
3559 msg = (struct GNUNET_MESH_PeerControl *) buf;
3560 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3561 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3562 // msg->tunnel_id = htonl(peer_info->t->tid);
3563 GNUNET_PEER_resolve (peer_info->id, &id);
3564 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3572 * Send keepalive packets for a peer
3574 * @param cls Closure (tunnel for which to send the keepalive).
3575 * @param tc Notification context.
3577 * TODO: implement explicit multicast keepalive?
3580 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3582 struct MeshTunnel *t = cls;
3583 struct GNUNET_MessageHeader *payload;
3584 struct GNUNET_MESH_Multicast *msg;
3586 sizeof (struct GNUNET_MESH_Multicast) +
3587 sizeof (struct GNUNET_MessageHeader);
3590 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3594 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3597 "sending keepalive for tunnel %d\n", t->id.tid);
3599 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3600 msg->header.size = htons (size);
3601 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3602 msg->oid = my_full_id;
3603 msg->tid = htonl (t->id.tid);
3604 msg->ttl = htonl (DEFAULT_TTL);
3605 msg->mid = htonl (t->mid + 1);
3607 payload = (struct GNUNET_MessageHeader *) &msg[1];
3608 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3609 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3610 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3612 t->path_refresh_task =
3613 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3619 * Function to process paths received for a new peer addition. The recorded
3620 * paths form the initial tunnel, which can be optimized later.
3621 * Called on each result obtained for the DHT search.
3623 * @param cls closure
3624 * @param exp when will this value expire
3625 * @param key key of the result
3626 * @param get_path path of the get request
3627 * @param get_path_length lenght of get_path
3628 * @param put_path path of the put request
3629 * @param put_path_length length of the put_path
3630 * @param type type of the result
3631 * @param size number of bytes in data
3632 * @param data pointer to the result data
3634 * TODO: re-issue the request after certain time? cancel after X results?
3637 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3638 const GNUNET_HashCode * key,
3639 const struct GNUNET_PeerIdentity *get_path,
3640 unsigned int get_path_length,
3641 const struct GNUNET_PeerIdentity *put_path,
3642 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3643 size_t size, const void *data)
3645 struct MeshPathInfo *path_info = cls;
3646 struct MeshPeerPath *p;
3647 struct GNUNET_PeerIdentity pi;
3650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3651 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3654 p = path_build_from_dht (get_path, get_path_length, put_path,
3656 path_add_to_peers (p, GNUNET_NO);
3658 for (i = 0; i < path_info->peer->ntunnels; i++)
3660 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3661 peer_info_connect (path_info->peer, path_info->t);
3669 * Function to process paths received for a new peer addition. The recorded
3670 * paths form the initial tunnel, which can be optimized later.
3671 * Called on each result obtained for the DHT search.
3673 * @param cls closure
3674 * @param exp when will this value expire
3675 * @param key key of the result
3676 * @param get_path path of the get request
3677 * @param get_path_length lenght of get_path
3678 * @param put_path path of the put request
3679 * @param put_path_length length of the put_path
3680 * @param type type of the result
3681 * @param size number of bytes in data
3682 * @param data pointer to the result data
3685 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3686 const GNUNET_HashCode * key,
3687 const struct GNUNET_PeerIdentity *get_path,
3688 unsigned int get_path_length,
3689 const struct GNUNET_PeerIdentity *put_path,
3690 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3691 size_t size, const void *data)
3693 const struct GNUNET_PeerIdentity *pi = data;
3694 struct MeshTunnel *t = cls;
3695 struct MeshPeerInfo *peer_info;
3696 struct MeshPeerPath *p;
3698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3699 if (size != sizeof (struct GNUNET_PeerIdentity))
3701 GNUNET_break_op (0);
3704 GNUNET_assert (NULL != t->owner);
3705 peer_info = peer_info_get (pi);
3706 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3708 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3710 p = path_build_from_dht (get_path, get_path_length, put_path,
3712 path_add_to_peers (p, GNUNET_NO);
3714 tunnel_add_peer (t, peer_info);
3715 peer_info_connect (peer_info, t);
3719 /******************************************************************************/
3720 /********************* MESH LOCAL HANDLES **************************/
3721 /******************************************************************************/
3725 * Handler for client disconnection
3727 * @param cls closure
3728 * @param client identification of the client; NULL
3729 * for the last call when the server is destroyed
3732 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3734 struct MeshClient *c;
3735 struct MeshClient *next;
3737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3746 if (c->handle != client)
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3754 GNUNET_SERVER_client_drop (c->handle);
3755 c->shutting_down = GNUNET_YES;
3756 GNUNET_assert (NULL != c->own_tunnels);
3757 GNUNET_assert (NULL != c->incoming_tunnels);
3758 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3759 &tunnel_destroy_iterator, c);
3760 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3761 &tunnel_destroy_iterator, c);
3762 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3763 &tunnel_destroy_iterator, c);
3764 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3765 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3766 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3768 /* deregister clients applications */
3769 if (NULL != c->apps)
3771 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3772 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3774 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3775 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3777 GNUNET_SCHEDULER_cancel (announce_applications_task);
3778 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3780 if (NULL != c->types)
3781 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3783 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3794 * Handler for new clients
3796 * @param cls closure
3797 * @param client identification of the client
3798 * @param message the actual message, which includes messages the client wants
3801 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3802 const struct GNUNET_MessageHeader *message)
3804 struct GNUNET_MESH_ClientConnect *cc_msg;
3805 struct MeshClient *c;
3806 GNUNET_MESH_ApplicationType *a;
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3814 /* Check data sanity */
3815 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3816 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3817 ntypes = ntohs (cc_msg->types);
3818 napps = ntohs (cc_msg->applications);
3820 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3823 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3827 /* Create new client structure */
3828 c = GNUNET_malloc (sizeof (struct MeshClient));
3829 c->id = next_client_id++;
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3832 GNUNET_SERVER_client_keep (client);
3833 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3836 GNUNET_MESH_ApplicationType at;
3839 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3840 for (i = 0; i < napps; i++)
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3844 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3845 /* store in clients hashmap */
3846 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3847 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3848 /* store in global hashmap, for announcements */
3849 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3850 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3852 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3853 announce_applications_task =
3854 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3862 t = (uint16_t *) & a[napps];
3863 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3864 for (i = 0; i < ntypes; i++)
3867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3868 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3870 /* store in clients hashmap */
3871 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3872 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3873 /* store in global hashmap */
3874 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3875 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3879 " client has %u+%u subscriptions\n", napps, ntypes);
3881 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3882 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3883 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3884 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3885 GNUNET_SERVER_notification_context_add (nc, client);
3887 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3893 * Handler for requests of new tunnels
3895 * @param cls closure
3896 * @param client identification of the client
3897 * @param message the actual message
3900 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3901 const struct GNUNET_MessageHeader *message)
3903 struct GNUNET_MESH_TunnelMessage *t_msg;
3904 struct MeshTunnel *t;
3905 struct MeshClient *c;
3906 GNUNET_HashCode hash;
3908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3910 /* Sanity check for client registration */
3911 if (NULL == (c = client_get (client)))
3914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3919 /* Message sanity check */
3920 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3927 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3928 /* Sanity check for tunnel numbering */
3929 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3935 /* Sanity check for duplicate tunnel IDs */
3936 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3943 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3944 while (NULL != tunnel_get_by_pi (myid, next_tid))
3945 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3946 t->id.tid = next_tid++;
3947 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3949 t->local_tid = ntohl (t_msg->tunnel_id);
3950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3951 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3953 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3955 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3957 GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
3958 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3965 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3967 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3968 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3971 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3974 t->tree = tree_new (myid);
3976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
3977 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3983 * Handler for requests of deleting tunnels
3985 * @param cls closure
3986 * @param client identification of the client
3987 * @param message the actual message
3990 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3991 const struct GNUNET_MessageHeader *message)
3993 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3994 struct MeshClient *c;
3995 struct MeshTunnel *t;
3996 MESH_TunnelNumber tid;
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3999 "Got a DESTROY TUNNEL from client!\n");
4001 /* Sanity check for client registration */
4002 if (NULL == (c = client_get (client)))
4005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4008 /* Message sanity check */
4009 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4016 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4018 /* Retrieve tunnel */
4019 tid = ntohl (tunnel_msg->tunnel_id);
4020 t = tunnel_get_by_local_id(c, tid);
4024 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4028 send_client_tunnel_disconnect(t, c);
4031 client_ignore_tunnel (c, t);
4033 // TODO: when to destroy incoming tunnel?
4034 if (t->nclients == 0)
4036 GNUNET_assert (GNUNET_YES ==
4037 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4039 GNUNET_assert (GNUNET_YES ==
4040 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4041 &my_full_id.hashPubKey,
4045 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4048 client_delete_tunnel(c, t);
4050 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4052 tunnel_send_destroy (t);
4054 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4060 * Handler for connection requests to new peers
4062 * @param cls closure
4063 * @param client identification of the client
4064 * @param message the actual message (PeerControl)
4067 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4068 const struct GNUNET_MessageHeader *message)
4070 struct GNUNET_MESH_PeerControl *peer_msg;
4071 struct MeshPeerInfo *peer_info;
4072 struct MeshClient *c;
4073 struct MeshTunnel *t;
4074 MESH_TunnelNumber tid;
4076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4077 /* Sanity check for client registration */
4078 if (NULL == (c = client_get (client)))
4081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4085 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4086 /* Sanity check for message size */
4087 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4090 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4094 /* Tunnel exists? */
4095 tid = ntohl (peer_msg->tunnel_id);
4096 t = tunnel_get_by_local_id (c, tid);
4100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4104 /* Does client own tunnel? */
4105 if (t->owner->handle != client)
4108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4112 GNUNET_i2s (&peer_msg->peer));
4113 peer_info = peer_info_get (&peer_msg->peer);
4115 tunnel_add_peer (t, peer_info);
4116 peer_info_connect (peer_info, t);
4118 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4124 * Handler for disconnection requests of peers in a tunnel
4126 * @param cls closure
4127 * @param client identification of the client
4128 * @param message the actual message (PeerControl)
4131 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4132 const struct GNUNET_MessageHeader *message)
4134 struct GNUNET_MESH_PeerControl *peer_msg;
4135 struct MeshPeerInfo *peer_info;
4136 struct MeshClient *c;
4137 struct MeshTunnel *t;
4138 MESH_TunnelNumber tid;
4140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4141 /* Sanity check for client registration */
4142 if (NULL == (c = client_get (client)))
4145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4148 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4149 /* Sanity check for message size */
4150 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4157 /* Tunnel exists? */
4158 tid = ntohl (peer_msg->tunnel_id);
4159 t = tunnel_get_by_local_id (c, tid);
4163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4168 /* Does client own tunnel? */
4169 if (t->owner->handle != client)
4172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4177 GNUNET_i2s (&peer_msg->peer));
4178 /* Is the peer in the tunnel? */
4180 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4181 if (NULL == peer_info)
4184 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4188 /* Ok, delete peer from tunnel */
4189 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4190 &peer_msg->peer.hashPubKey);
4192 send_destroy_path (t, peer_info->id);
4193 tunnel_delete_peer (t, peer_info->id);
4194 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4200 * Handler for connection requests to new peers by type
4202 * @param cls closure
4203 * @param client identification of the client
4204 * @param message the actual message (ConnectPeerByType)
4207 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4208 const struct GNUNET_MessageHeader *message)
4210 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4211 struct MeshClient *c;
4212 struct MeshTunnel *t;
4213 GNUNET_HashCode hash;
4214 MESH_TunnelNumber tid;
4216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4217 /* Sanity check for client registration */
4218 if (NULL == (c = client_get (client)))
4221 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4225 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4226 /* Sanity check for message size */
4227 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4228 ntohs (connect_msg->header.size))
4231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4235 /* Tunnel exists? */
4236 tid = ntohl (connect_msg->tunnel_id);
4237 t = tunnel_get_by_local_id (c, tid);
4241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4245 /* Does client own tunnel? */
4246 if (t->owner->handle != client)
4249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4253 /* Do WE have the service? */
4254 t->type = ntohl (connect_msg->type);
4255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4256 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4257 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4260 /* Yes! Fast forward, add ourselves to the tunnel and send the
4261 * good news to the client, and alert the destination client of
4262 * an incoming tunnel.
4264 * FIXME send a path create to self, avoid code duplication
4266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4267 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4268 peer_info_get (&my_full_id),
4269 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4272 send_client_peer_connected (t, myid);
4273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4274 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4276 t->local_tid_dest = next_local_tid++;
4277 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4278 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4279 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4283 /* Ok, lets find a peer offering the service */
4284 if (NULL != t->dht_get_type)
4286 GNUNET_DHT_get_stop (t->dht_get_type);
4288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4289 GNUNET_h2s (&hash));
4291 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
4292 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
4293 GNUNET_DHT_RO_RECORD_ROUTE |
4294 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4295 &dht_get_type_handler, t);
4297 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4303 * Handler for client traffic directed to one peer
4305 * @param cls closure
4306 * @param client identification of the client
4307 * @param message the actual message
4310 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4311 const struct GNUNET_MessageHeader *message)
4313 struct MeshClient *c;
4314 struct MeshTunnel *t;
4315 struct MeshPeerInfo *pi;
4316 struct GNUNET_MESH_Unicast *data_msg;
4317 MESH_TunnelNumber tid;
4320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4321 "Got a unicast request from a client!\n");
4323 /* Sanity check for client registration */
4324 if (NULL == (c = client_get (client)))
4327 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4330 data_msg = (struct GNUNET_MESH_Unicast *) message;
4331 /* Sanity check for message size */
4332 size = ntohs (message->size);
4333 if (sizeof (struct GNUNET_MESH_Unicast) +
4334 sizeof (struct GNUNET_MessageHeader) > size)
4337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4341 /* Tunnel exists? */
4342 tid = ntohl (data_msg->tid);
4343 t = tunnel_get_by_local_id (c, tid);
4347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4351 /* Is it a local tunnel? Then, does client own the tunnel? */
4352 if (t->owner->handle != client)
4355 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4359 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4360 &data_msg->destination.hashPubKey);
4361 /* Is the selected peer in the tunnel? */
4365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4369 /* Ok, everything is correct, send the message
4370 * (pretend we got it from a mesh peer)
4373 char buf[ntohs (message->size)];
4374 struct GNUNET_MESH_Unicast *copy;
4376 /* Work around const limitation */
4377 copy = (struct GNUNET_MESH_Unicast *) buf;
4378 memcpy (buf, data_msg, size);
4379 copy->oid = my_full_id;
4380 copy->tid = htonl (t->id.tid);
4381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4382 " calling generic handler...\n");
4383 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4385 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4391 * Handler for client traffic directed to the origin
4393 * @param cls closure
4394 * @param client identification of the client
4395 * @param message the actual message
4398 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4399 const struct GNUNET_MessageHeader *message)
4401 struct GNUNET_MESH_ToOrigin *data_msg;
4402 struct GNUNET_PeerIdentity id;
4403 struct MeshClient *c;
4404 struct MeshTunnel *t;
4405 MESH_TunnelNumber tid;
4408 /* Sanity check for client registration */
4409 if (NULL == (c = client_get (client)))
4412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4415 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4416 /* Sanity check for message size */
4417 size = ntohs (message->size);
4418 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4419 sizeof (struct GNUNET_MessageHeader) > size)
4422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4426 /* Tunnel exists? */
4427 tid = ntohl (data_msg->tid);
4428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4429 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4430 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4436 t = tunnel_get_by_local_id (c, tid);
4440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4444 /* It should be sent by someone who has this as incoming tunnel. */
4445 if (-1 == client_knows_tunnel (c, t))
4448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4451 GNUNET_PEER_resolve (t->id.oid, &id);
4453 /* Ok, everything is correct, send the message
4454 * (pretend we got it from a mesh peer)
4457 char buf[ntohs (message->size)];
4458 struct GNUNET_MESH_ToOrigin *copy;
4460 /* Work around const limitation */
4461 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4462 memcpy (buf, data_msg, size);
4464 copy->tid = htonl (t->id.tid);
4465 copy->sender = my_full_id;
4466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4467 " calling generic handler...\n");
4468 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4470 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4476 * Handler for client traffic directed to all peers in a tunnel
4478 * @param cls closure
4479 * @param client identification of the client
4480 * @param message the actual message
4483 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4484 const struct GNUNET_MessageHeader *message)
4486 struct MeshClient *c;
4487 struct MeshTunnel *t;
4488 struct GNUNET_MESH_Multicast *data_msg;
4489 MESH_TunnelNumber tid;
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4492 "Got a multicast request from a client!\n");
4494 /* Sanity check for client registration */
4495 if (NULL == (c = client_get (client)))
4498 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4501 data_msg = (struct GNUNET_MESH_Multicast *) message;
4502 /* Sanity check for message size */
4503 if (sizeof (struct GNUNET_MESH_Multicast) +
4504 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4511 /* Tunnel exists? */
4512 tid = ntohl (data_msg->tid);
4513 t = tunnel_get_by_local_id (c, tid);
4517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4521 /* Does client own tunnel? */
4522 if (t->owner->handle != client)
4525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4530 char buf[ntohs (message->size)];
4531 struct GNUNET_MESH_Multicast *copy;
4533 copy = (struct GNUNET_MESH_Multicast *) buf;
4534 memcpy (buf, message, ntohs (message->size));
4535 copy->oid = my_full_id;
4536 copy->tid = htonl (t->id.tid);
4537 copy->ttl = htonl (DEFAULT_TTL);
4538 copy->mid = htonl (t->mid + 1);
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4540 " calling generic handler...\n");
4541 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4544 /* receive done gets called when last copy is sent to a neighbor */
4549 * Functions to handle messages from clients
4551 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4552 {&handle_local_new_client, NULL,
4553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4554 {&handle_local_tunnel_create, NULL,
4555 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4556 sizeof (struct GNUNET_MESH_TunnelMessage)},
4557 {&handle_local_tunnel_destroy, NULL,
4558 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4559 sizeof (struct GNUNET_MESH_TunnelMessage)},
4560 {&handle_local_connect_add, NULL,
4561 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4562 sizeof (struct GNUNET_MESH_PeerControl)},
4563 {&handle_local_connect_del, NULL,
4564 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4565 sizeof (struct GNUNET_MESH_PeerControl)},
4566 {&handle_local_connect_by_type, NULL,
4567 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4568 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4569 {&handle_local_unicast, NULL,
4570 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4571 {&handle_local_to_origin, NULL,
4572 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4573 {&handle_local_multicast, NULL,
4574 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4580 * To be called on core init/fail.
4582 * @param cls service closure
4583 * @param server handle to the server for this service
4584 * @param identity the public identity of this peer
4587 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4588 const struct GNUNET_PeerIdentity *identity)
4590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4591 core_handle = server;
4592 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4596 GNUNET_SCHEDULER_shutdown ();
4602 * Method called whenever a given peer connects.
4604 * @param cls closure
4605 * @param peer peer identity this notification is about
4606 * @param atsi performance data for the connection
4607 * @param atsi_count number of records in 'atsi'
4610 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4611 const struct GNUNET_ATS_Information *atsi,
4612 unsigned int atsi_count)
4614 struct MeshPeerInfo *peer_info;
4615 struct MeshPeerPath *path;
4617 DEBUG_CONN ("Peer connected\n");
4618 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4619 peer_info = peer_info_get (peer);
4620 if (myid == peer_info->id)
4622 DEBUG_CONN (" (self)\n");
4627 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4629 path = path_new (2);
4630 path->peers[0] = myid;
4631 path->peers[1] = peer_info->id;
4632 GNUNET_PEER_change_rc (myid, 1);
4633 GNUNET_PEER_change_rc (peer_info->id, 1);
4634 peer_info_add_path (peer_info, path, GNUNET_YES);
4639 * Method called whenever a peer disconnects.
4641 * @param cls closure
4642 * @param peer peer identity this notification is about
4645 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4647 struct MeshPeerInfo *pi;
4650 DEBUG_CONN ("Peer disconnected\n");
4651 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4657 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4659 /* TODO: notify that the transmission failed */
4660 peer_info_cancel_transmission (pi, i);
4662 peer_info_remove_path (pi, pi->id, myid);
4665 DEBUG_CONN (" (self)\n");
4671 /******************************************************************************/
4672 /************************ MAIN FUNCTIONS ****************************/
4673 /******************************************************************************/
4676 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4678 * @param cls closure
4679 * @param key current key code
4680 * @param value value in the hash map
4681 * @return GNUNET_YES if we should continue to iterate,
4685 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4687 struct MeshTunnel *t = value;
4694 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4696 * @param cls closure
4697 * @param key current key code
4698 * @param value value in the hash map
4699 * @return GNUNET_YES if we should continue to iterate,
4703 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4705 struct MeshPeerInfo *p = value;
4707 peer_info_destroy (p);
4712 * Task run during shutdown.
4718 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4722 if (core_handle != NULL)
4724 GNUNET_CORE_disconnect (core_handle);
4727 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4728 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4729 if (dht_handle != NULL)
4731 GNUNET_DHT_disconnect (dht_handle);
4736 GNUNET_SERVER_notification_context_destroy (nc);
4739 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4741 GNUNET_SCHEDULER_cancel (announce_id_task);
4742 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4748 * Process mesh requests.
4750 * @param cls closure
4751 * @param server the initialized server
4752 * @param c configuration to use
4755 run (void *cls, struct GNUNET_SERVER_Handle *server,
4756 const struct GNUNET_CONFIGURATION_Handle *c)
4758 struct MeshPeerInfo *peer;
4759 struct MeshPeerPath *p;
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4763 server_handle = server;
4764 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4765 CORE_QUEUE_SIZE, /* queue size */
4766 NULL, /* Closure passed to MESH functions */
4767 &core_init, /* Call core_init once connected */
4768 &core_connect, /* Handle connects */
4769 &core_disconnect, /* remove peers on disconnects */
4770 NULL, /* Don't notify about all incoming messages */
4771 GNUNET_NO, /* For header only in notification */
4772 NULL, /* Don't notify about all outbound messages */
4773 GNUNET_NO, /* For header-only out notification */
4774 core_handlers); /* Register these handlers */
4776 if (core_handle == NULL)
4779 GNUNET_SCHEDULER_shutdown ();
4784 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4789 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4790 GNUNET_SCHEDULER_shutdown ();
4793 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4794 GNUNET_free (keyfile);
4795 if (my_private_key == NULL)
4797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4798 _("Mesh service could not access hostkey. Exiting.\n"));
4799 GNUNET_SCHEDULER_shutdown ();
4802 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4803 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4804 &my_full_id.hashPubKey);
4805 myid = GNUNET_PEER_intern (&my_full_id);
4807 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4814 dht_handle = GNUNET_DHT_connect (c, 64);
4815 if (dht_handle == NULL)
4821 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4823 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4824 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4825 peers = GNUNET_CONTAINER_multihashmap_create (32);
4826 applications = GNUNET_CONTAINER_multihashmap_create (32);
4827 types = GNUNET_CONTAINER_multihashmap_create (32);
4829 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4830 nc = GNUNET_SERVER_notification_context_create (server_handle,
4832 GNUNET_SERVER_disconnect_notify (server_handle,
4833 &handle_local_client_disconnect, NULL);
4837 clients_tail = NULL;
4840 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4841 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4843 /* Create a peer_info for the local peer */
4844 peer = peer_info_get (&my_full_id);
4847 GNUNET_PEER_change_rc (myid, 1);
4848 peer_info_add_path (peer, p, GNUNET_YES);
4850 /* Scheduled the task to clean up when shutdown is called */
4851 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4858 * The main function for the mesh service.
4860 * @param argc number of arguments from the command line
4861 * @param argv command line arguments
4862 * @return 0 ok, 1 on error
4865 main (int argc, char *const *argv)
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4872 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");