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
49 #include "mesh_protocol.h"
50 #include "gnunet_dht_service.h"
51 #include "mesh_tunnel_tree.h"
53 /* TODO: move into configuration file */
54 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
55 GNUNET_TIME_UNIT_SECONDS,\
57 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
58 GNUNET_TIME_UNIT_SECONDS,\
61 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
62 GNUNET_TIME_UNIT_SECONDS,\
65 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
66 GNUNET_TIME_UNIT_SECONDS,\
69 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
70 GNUNET_TIME_UNIT_SECONDS,\
72 #define DEFAULT_TTL 64
74 #define MESH_DEBUG_DHT GNUNET_NO
78 /******************************************************************************/
79 /************************ DATA STRUCTURES ****************************/
80 /******************************************************************************/
82 /** FWD declaration */
87 * Struct representing a piece of data being sent to other peers
91 /** Tunnel it belongs to. */
94 /** In case of a multicast, task to allow a client to send more data if
95 * some neighbor is too slow. */
96 GNUNET_SCHEDULER_TaskIdentifier *task;
98 /** How many remaining neighbors we need to send this to. */
99 unsigned int *reference_counter;
101 /** Size of the data. */
110 * Struct containing all info possibly needed to build a package when called
113 struct MeshTransmissionDescriptor
115 /** ID of the tunnel this packet travels in */
116 struct MESH_TunnelID *origin;
118 /** Who was this message being sent to */
119 struct MeshPeerInfo *peer;
121 /** Ultimate destination of the packet */
122 GNUNET_PEER_Id destination;
124 /** Which handler was used to request the transmission */
125 unsigned int handler_n;
127 /** Data descriptor */
128 struct MeshData* mesh_data;
133 * Struct containing all information regarding a given peer
143 * Last time we heard from this peer
145 struct GNUNET_TIME_Absolute last_contact;
148 * Number of attempts to reconnect so far
150 int n_reconnect_attempts;
153 * Paths to reach the peer, ordered by ascending hop count
155 struct MeshPeerPath *path_head;
158 * Paths to reach the peer, ordered by ascending hop count
160 struct MeshPeerPath *path_tail;
163 * Handle to stop the DHT search for a path to this peer
165 struct GNUNET_DHT_GetHandle *dhtget;
168 * Closure given to the DHT GET
170 struct MeshPathInfo *dhtgetcls;
173 * Handles to stop queued transmissions for this peer
175 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
178 * Pointer to info stuctures used as cls for queued transmissions
180 void *infos[CORE_QUEUE_SIZE];
183 * Type of message being in each transmission
185 uint16_t types[CORE_QUEUE_SIZE];
188 * Array of tunnels this peer participates in
189 * (most probably a small amount, therefore not a hashmap)
190 * When the path to the peer changes, notify these tunnels to let them
191 * re-adjust their path trees.
193 struct MeshTunnel **tunnels;
196 * Number of tunnels this peers participates in
198 unsigned int ntunnels;
203 * Data scheduled to transmit (to local client or remote peer)
210 struct MeshQueue *next;
211 struct MeshQueue *prev;
214 * Target of the data (NULL if target is client)
216 struct MeshPeerInfo *peer;
219 * Client to send the data to (NULL if target is peer)
221 struct MeshClient *client;
224 * Size of the message to transmit
229 * How old is the data?
231 struct GNUNET_TIME_Absolute timestamp;
236 struct GNUNET_MessageHeader *data;
240 * Globally unique tunnel identification (owner + number)
241 * DO NOT USE OVER THE NETWORK
246 * Node that owns the tunnel
251 * Tunnel number to differentiate all the tunnels owned by the node oid
252 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
254 MESH_TunnelNumber tid;
258 struct MeshClient; /* FWD declaration */
261 * Struct containing all information regarding a tunnel
262 * For an intermediate node the improtant info used will be:
263 * - id Tunnel unique identification
264 * - paths[0] To know where to send it next
265 * - metainfo: ready, speeds, accounting
272 struct MESH_TunnelID id;
275 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
277 MESH_TunnelNumber local_tid;
280 * ID of the last multicast packet seen/sent.
285 * Last time the tunnel was used
287 struct GNUNET_TIME_Absolute timestamp;
290 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
291 * containing peers added by id or by type, not intermediate peers.
293 struct GNUNET_CONTAINER_MultiHashMap *peers;
296 * Number of peers that are connected and potentially ready to receive data
298 unsigned int peers_ready;
301 * Number of peers that have been added to the tunnel
303 unsigned int peers_total;
306 * Client owner of the tunnel, if any
308 struct MeshClient *client;
311 * Messages ready to transmit
313 struct MeshQueue *queue_head;
314 struct MeshQueue *queue_tail;
319 struct MeshTunnelTree *tree;
322 * Application type we are looking for in this tunnel
324 GNUNET_MESH_ApplicationType type;
327 * Used to search peers offering a service
329 struct GNUNET_DHT_GetHandle *dht_get_type;
332 * Task to keep the used paths alive
334 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
337 * Task to destroy the tunnel after timeout
339 * FIXME: merge the two? a tunnel will have either
340 * a path refresh OR a timeout, never both!
342 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
347 * Info needed to work with tunnel paths and peers
354 struct MeshTunnel *t;
357 * Neighbouring peer to whom we send the packet to
359 struct MeshPeerInfo *peer;
364 struct MeshPeerPath *path;
367 * Position in peer's transmit queue
374 * Struct containing information about a client of the service
381 struct MeshClient *next;
382 struct MeshClient *prev;
385 * Tunnels that belong to this client, indexed by local id
387 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
390 * Handle to communicate with the client
392 struct GNUNET_SERVER_Client *handle;
395 * Applications that this client has claimed to provide
397 struct GNUNET_CONTAINER_MultiHashMap *apps;
400 * Messages that this client has declared interest in
402 struct GNUNET_CONTAINER_MultiHashMap *types;
406 * ID of the client, for debug messages
415 /******************************************************************************/
416 /************************ DEBUG FUNCTIONS ****************************/
417 /******************************************************************************/
421 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
422 * @param cls string to print
423 * @param tc task context
426 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
430 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
438 /******************************************************************************/
439 /*********************** GLOBAL VARIABLES ****************************/
440 /******************************************************************************/
445 static struct MeshClient *clients;
446 static struct MeshClient *clients_tail;
449 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
451 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
454 * Tunnels incoming, indexed by MESH_TunnelNumber
455 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
457 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
460 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
462 static struct GNUNET_CONTAINER_MultiHashMap *peers;
465 * Handle to communicate with core
467 static struct GNUNET_CORE_Handle *core_handle;
470 * Handle to communicate with transport
472 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
477 static struct GNUNET_DHT_Handle *dht_handle;
482 static struct GNUNET_SERVER_Handle *server_handle;
485 * Notification context, to send messages to local clients
487 static struct GNUNET_SERVER_NotificationContext *nc;
490 * Local peer own ID (memory efficient handle)
492 static GNUNET_PEER_Id myid;
495 * Local peer own ID (full value)
497 static struct GNUNET_PeerIdentity my_full_id;
502 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
507 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
510 * Tunnel ID for the next created tunnel (global tunnel number)
512 static MESH_TunnelNumber next_tid;
515 * Tunnel ID for the next incoming tunnel (local tunnel number)
517 static MESH_TunnelNumber next_local_tid;
520 * All application types provided by this peer
522 static struct GNUNET_CONTAINER_MultiHashMap *applications;
525 * All message types clients of this peer are interested in
527 static struct GNUNET_CONTAINER_MultiHashMap *types;
530 * Task to periodically announce provided applications
532 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
535 * Task to periodically announce itself in the network
537 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
540 unsigned int next_client_id;
544 /******************************************************************************/
545 /************************ ITERATORS ****************************/
546 /******************************************************************************/
548 /* FIXME move iterators here */
551 /******************************************************************************/
552 /************************ PERIODIC FUNCTIONS ****************************/
553 /******************************************************************************/
556 * Announce iterator over for each application provided by the peer
559 * @param key current key code
560 * @param value value in the hash map
561 * @return GNUNET_YES if we should continue to
566 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
568 /* FIXME are hashes in multihash map equal on all aquitectures? */
569 GNUNET_DHT_put (dht_handle, key, 10U,
570 GNUNET_DHT_RO_RECORD_ROUTE |
571 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
572 sizeof (struct GNUNET_PeerIdentity),
573 (const char *) &my_full_id,
575 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
576 &mesh_debug, "DHT_put for app completed");
578 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
580 APP_ANNOUNCE_TIME, NULL, NULL);
587 * Periodically announce what applications are provided by local clients
590 * @param tc task context
593 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
595 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
597 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
603 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
605 announce_applications_task =
606 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
616 * Periodically announce self id in the DHT
619 * @param tc task context
622 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
624 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
626 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
630 * - Set data expiration in function of X
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
635 GNUNET_i2s (&my_full_id));
637 GNUNET_DHT_put (dht_handle, /* DHT handle */
638 &my_full_id.hashPubKey, /* Key to use */
639 10U, /* Replication level */
640 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
641 GNUNET_BLOCK_TYPE_TEST, /* Block type */
642 sizeof (my_full_id), /* Size of the data */
643 (char *) &my_full_id, /* Data itself */
644 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
645 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
647 &mesh_debug, "DHT_put for id completed");
649 NULL, /* Continuation */
650 NULL); /* Continuation closure */
653 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
658 * Function to process paths received for a new peer addition. The recorded
659 * paths form the initial tunnel, which can be optimized later.
660 * Called on each result obtained for the DHT search.
663 * @param exp when will this value expire
664 * @param key key of the result
665 * @param type type of the result
666 * @param size number of bytes in data
667 * @param data pointer to the result data
670 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
671 const GNUNET_HashCode * key,
672 const struct GNUNET_PeerIdentity *get_path,
673 unsigned int get_path_length,
674 const struct GNUNET_PeerIdentity *put_path,
675 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
676 size_t size, const void *data);
679 /******************************************************************************/
680 /****************** GENERAL HELPER FUNCTIONS ************************/
681 /******************************************************************************/
684 * Check if client has registered with the service and has not disconnected
686 * @param client the client to check
688 * @return non-NULL if client exists in the global DLL
690 static struct MeshClient *
691 client_get (struct GNUNET_SERVER_Client *client)
693 struct MeshClient *c;
698 if (c->handle == client)
707 * Checks if a given client has subscribed to certain message type
709 * @param message_type Type of message to check
710 * @param c Client to check
712 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
717 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
721 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
722 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
727 * Allow a client to send more data after transmitting a multicast message
728 * which some neighbor has not yet accepted altough a reasonable time has
731 * @param cls Closure (DataDescriptor containing the task identifier)
732 * @param tc Task Context
735 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
737 struct MeshData *mdata = cls;
739 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
742 GNUNET_assert (NULL != mdata->reference_counter);
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
745 mdata->reference_counter);
747 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
748 GNUNET_SERVER_receive_done (mdata->t->client->handle, GNUNET_OK);
753 * Search for a tunnel by global ID using full PeerIdentities
755 * @param oid owner of the tunnel
756 * @param tid global tunnel number
758 * @return tunnel handler, NULL if doesn't exist
760 static struct MeshTunnel *
761 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
765 * Notify a tunnel that a connection has broken that affects at least
768 * @param t Tunnel affected.
769 * @param p1 Peer that got disconnected from p2.
770 * @param p2 Peer that got disconnected from p1.
772 * @return Short ID of the peer disconnected (either p1 or p2).
773 * 0 if the tunnel remained unaffected.
775 static GNUNET_PEER_Id
776 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
781 * Send the message to all clients that have subscribed to its type
783 * @param msg Pointer to the message itself
784 * @param payload Pointer to the payload of the message.
785 * @return number of clients this message was sent to
788 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
789 const struct GNUNET_MessageHeader *payload)
791 struct GNUNET_PeerIdentity *oid;
792 struct MeshClient *c;
793 struct MeshTunnel *t;
794 MESH_TunnelNumber *tid;
797 char cbuf[htons (msg->size)];
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
801 type = ntohs (payload->type);
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
805 memcpy (cbuf, msg, sizeof (cbuf));
806 switch (htons (msg->type))
808 struct GNUNET_MESH_Unicast *uc;
809 struct GNUNET_MESH_Multicast *mc;
810 struct GNUNET_MESH_ToOrigin *to;
812 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
813 uc = (struct GNUNET_MESH_Unicast *) cbuf;
817 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
818 mc = (struct GNUNET_MESH_Multicast *) cbuf;
822 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
823 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
831 t = tunnel_get (oid, ntohl (*tid));
837 *tid = htonl (t->local_tid);
838 for (count = 0, c = clients; c != NULL; c = c->next)
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
843 if (client_is_subscribed (type, c))
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
847 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
848 (struct GNUNET_MessageHeader
849 *) cbuf, GNUNET_YES);
857 * Notify the client that owns the tunnel that a peer has connected to it
858 * (the requested path to it has been confirmed).
860 * @param t Tunnel whose owner to notify
861 * @param id Short id of the peer that has connected
864 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
866 struct GNUNET_MESH_PeerControl pc;
868 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
869 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
870 pc.tunnel_id = htonl (t->local_tid);
871 GNUNET_PEER_resolve (id, &pc.peer);
872 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, &pc.header,
878 * Notify all clients (not depending on registration status) that the incoming
879 * tunnel is no longer valid.
881 * @param t Tunnel that was destroyed.
884 send_clients_tunnel_destroy (struct MeshTunnel *t)
886 struct GNUNET_MESH_TunnelMessage msg;
888 msg.header.size = htons (sizeof (msg));
889 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
890 msg.tunnel_id = htonl (t->local_tid);
891 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
896 * Function called to notify a client about the socket
897 * being ready to queue more data. "buf" will be
898 * NULL and "size" zero if the socket was closed for
899 * writing in the meantime.
902 * @param size number of bytes available in buf
903 * @param buf where the callee should write the message
904 * @return number of bytes written to buf
907 send_core_create_path (void *cls, size_t size, void *buf);
911 * Function called to notify a client about the socket
912 * being ready to queue more data. "buf" will be
913 * NULL and "size" zero if the socket was closed for
914 * writing in the meantime.
916 * @param cls closure (data itself)
917 * @param size number of bytes available in buf
918 * @param buf where the callee should write the message
920 * @return number of bytes written to buf
923 send_core_data_multicast (void *cls, size_t size, void *buf);
927 * Decrements the reference counter and frees all resources if needed
929 * @param dd Data Descriptor used in a multicast message. Freed if needed.
932 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
934 /* Make sure it's a multicast packet */
935 GNUNET_assert (NULL != mesh_data->reference_counter);
937 if (0 == --(*(mesh_data->reference_counter)))
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
940 if (NULL != mesh_data->task)
942 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
944 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
946 GNUNET_SERVER_receive_done (mesh_data->t->client->handle, GNUNET_OK);
948 GNUNET_free (mesh_data->task);
950 GNUNET_free (mesh_data->reference_counter);
951 GNUNET_free (mesh_data->data);
952 GNUNET_free (mesh_data);
958 * Cancel a core transmission that was already requested and free all resources
959 * associated to the request.
961 * @param peer PeeInfo of the peer whose transmission is cancelled.
962 * @param i Position of the transmission to be cancelled.
965 peer_info_cancel_transmission (struct MeshPeerInfo *peer, unsigned int i)
967 if (NULL != peer->core_transmit[i])
969 struct MeshTransmissionDescriptor *dd;
970 struct MeshPathInfo *path_info;
974 struct GNUNET_PeerIdentity id;
976 GNUNET_PEER_resolve (peer->id, &id);
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "MESH: Cancelling data transmission at %s [%u]\n",
979 GNUNET_i2s (&id), i);
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message type %u\n",
984 /* TODO: notify that tranmission has failed */
985 switch (peer->types[i])
987 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
988 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
989 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
992 data_descriptor_decrement_multicast (dd->mesh_data);
994 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
996 path_info = peer->infos[i];
997 path_destroy (path_info->path);
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
1003 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
1004 peer->core_transmit[i] = NULL;
1005 GNUNET_free (peer->infos[i]);
1011 * Get a unused CORE slot to transmit a message to a peer. If all the slots
1012 * are used, cancel one and return it's position.
1014 * @param peer PeerInfo of the neighbor we want to transmit to.
1016 * @return The index of an available slot to transmit to the neighbor.
1019 peer_info_transmit_slot (struct MeshPeerInfo *peer)
1023 for (i = 0; peer->core_transmit[i]; i++)
1025 if (i == (CORE_QUEUE_SIZE - 1))
1027 /* All positions are taken! Overwriting! */
1029 peer_info_cancel_transmission (peer, 0);
1038 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1039 * and insert it in the appropiate structures if the peer is not known yet.
1041 * @param peer Full identity of the peer.
1043 * @return Existing or newly created peer info.
1045 static struct MeshPeerInfo *
1046 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1048 struct MeshPeerInfo *peer_info;
1050 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1051 if (NULL == peer_info)
1054 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1055 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1056 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1057 peer_info->id = GNUNET_PEER_intern (peer);
1065 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1066 * and insert it in the appropiate structures if the peer is not known yet.
1068 * @param peer Short identity of the peer.
1070 * @return Existing or newly created peer info.
1072 static struct MeshPeerInfo *
1073 peer_info_get_short (const GNUNET_PEER_Id peer)
1075 struct GNUNET_PeerIdentity id;
1077 GNUNET_PEER_resolve (peer, &id);
1078 return peer_info_get (&id);
1083 * Iterator to remove the tunnel from the list of tunnels a peer participates
1086 * @param cls Closure (tunnel info)
1087 * @param key GNUNET_PeerIdentity of the peer (unused)
1088 * @param value PeerInfo of the peer
1090 * @return always GNUNET_YES, to keep iterating
1093 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1095 struct MeshTunnel *t = cls;
1096 struct MeshPeerInfo *peer = value;
1099 for (i = 0; i < peer->ntunnels; i++)
1102 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1105 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1106 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1115 * Core callback to write a
1117 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1118 * @param size Number of bytes available in buf.
1119 * @param buf Where the to write the message.
1121 * @return number of bytes written to buf
1124 send_core_data_raw (void *cls, size_t size, void *buf)
1126 struct MeshTransmissionDescriptor *info = cls;
1127 struct GNUNET_MessageHeader *msg;
1130 GNUNET_assert (NULL != info);
1131 GNUNET_assert (NULL != info->mesh_data);
1132 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1133 total_size = ntohs (msg->size);
1135 if (total_size > size)
1137 struct GNUNET_PeerIdentity id;
1139 GNUNET_PEER_resolve (info->peer->id, &id);
1140 info->peer->core_transmit[info->handler_n] =
1141 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1142 GNUNET_TIME_UNIT_FOREVER_REL, &id,
1143 size, &send_core_data_raw, info);
1146 info->peer->core_transmit[info->handler_n] = NULL;
1147 memcpy (buf, msg, total_size);
1148 GNUNET_free (info->mesh_data);
1155 * Sends an already built message to a peer, properly registrating
1156 * all used resources.
1158 * @param message Message to send. Fucntion makes a copy of it.
1159 * @param peer Short ID of the neighbor whom to send the message.
1164 send_message (const struct GNUNET_MessageHeader *message,
1165 const struct GNUNET_PeerIdentity *peer)
1167 struct MeshTransmissionDescriptor *info;
1168 struct MeshPeerInfo *neighbor;
1169 struct MeshPeerPath *p;
1173 // GNUNET_TRANSPORT_try_connect();
1175 size = ntohs (message->size);
1176 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1177 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1178 info->mesh_data->data = GNUNET_malloc (size);
1179 memcpy (info->mesh_data->data, message, size);
1180 info->mesh_data->data_len = size;
1181 neighbor = peer_info_get (peer);
1182 for (p = neighbor->path_head; NULL != p; p = p->next)
1192 GNUNET_free (info->mesh_data->data);
1193 GNUNET_free (info->mesh_data);
1197 i = peer_info_transmit_slot (neighbor);
1198 info->handler_n = i;
1199 info->peer = neighbor;
1200 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1201 neighbor->infos[i] = info;
1202 neighbor->core_transmit[i] =
1203 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
1204 GNUNET_TIME_UNIT_FOREVER_REL, peer,
1205 size, &send_core_data_raw, info);
1211 * Sends a CREATE PATH message for a path to a peer, properly registrating
1212 * all used resources.
1214 * @param peer PeerInfo of the final peer for whom this path is being created.
1215 * @param p Path itself.
1216 * @param t Tunnel for which the path is created.
1219 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1220 struct MeshTunnel *t)
1222 struct GNUNET_PeerIdentity id;
1223 struct MeshPathInfo *path_info;
1224 struct MeshPeerInfo *neighbor;
1229 p = tree_get_path_to_peer (t->tree, peer->id);
1236 for (i = 0; i < p->length; i++)
1238 if (p->peers[i] == myid)
1241 if (i >= p->length - 1)
1247 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1249 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1250 path_info->path = p;
1252 neighbor = peer_info_get (&id);
1253 path_info->peer = neighbor;
1254 path_info->pos = peer_info_transmit_slot (neighbor);
1255 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1256 neighbor->infos[path_info->pos] = path_info;
1257 neighbor->core_transmit[path_info->pos] = GNUNET_CORE_notify_transmit_ready (core_handle, /* handle */
1260 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1262 sizeof (struct GNUNET_MESH_ManipulatePath) + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1263 &send_core_create_path, /* callback */
1264 path_info); /* cls */
1269 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1271 * @param t Tunnel whose path to destroy.
1272 * @param destination Short ID of the peer to whom the path to destroy.
1275 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1277 struct MeshPeerPath *p;
1280 p = tree_get_path_to_peer (t->tree, destination);
1286 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1287 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1289 struct GNUNET_MESH_ManipulatePath *msg;
1290 struct GNUNET_PeerIdentity *pi;
1294 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1295 msg->header.size = htons (size);
1296 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1297 msg->tid = htonl (t->id.tid);
1298 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1299 for (i = 0; i < p->length; i++)
1301 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1303 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1310 * Try to establish a new connection to this peer.
1311 * Use the best path for the given tunnel.
1312 * If the peer doesn't have any path to it yet, try to get one.
1313 * If the peer already has some path, send a CREATE PATH towards it.
1315 * @param peer PeerInfo of the peer.
1316 * @param t Tunnel for which to create the path, if possible.
1319 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1321 struct MeshPeerPath *p;
1322 struct MeshPathInfo *path_info;
1324 if (NULL != peer->path_head)
1326 p = tree_get_path_to_peer (t->tree, peer->id);
1335 send_create_path (peer, p, t);
1340 send_client_peer_connected (t, myid);
1343 else if (NULL == peer->dhtget)
1345 struct GNUNET_PeerIdentity id;
1347 GNUNET_PEER_resolve (peer->id, &id);
1348 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1349 path_info->peer = peer;
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 "MESH: Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1353 peer->dhtgetcls = path_info;
1354 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1355 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1356 GNUNET_BLOCK_TYPE_TEST, /* type */
1357 &id.hashPubKey, /* key to search */
1358 4, /* replication level */
1359 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1360 0, /* xquery bits */
1361 &dht_get_id_handler, path_info);
1363 /* Otherwise, there is no path but the DHT get is already started. */
1368 * Task to delay the connection of a peer
1370 * @param cls Closure (path info with tunnel and peer to connect).
1371 * Will be free'd on exection.
1372 * @param tc TaskContext
1375 peer_info_connect_task (void *cls,
1376 const struct GNUNET_SCHEDULER_TaskContext *tc)
1378 struct MeshPathInfo *path_info = cls;
1380 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1385 peer_info_connect (path_info->peer, path_info->t);
1391 * Destroy the peer_info and free any allocated resources linked to it
1393 * @param pi The peer_info to destroy.
1395 * @return GNUNET_OK on success
1398 peer_info_destroy (struct MeshPeerInfo *pi)
1400 struct GNUNET_PeerIdentity id;
1401 struct MeshPeerPath *p;
1402 struct MeshPeerPath *nextp;
1405 GNUNET_PEER_resolve (pi->id, &id);
1406 GNUNET_PEER_change_rc (pi->id, -1);
1409 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1413 "MESH: removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1415 if (NULL != pi->dhtget)
1417 GNUNET_DHT_get_stop (pi->dhtget);
1418 GNUNET_free (pi->dhtgetcls);
1420 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1422 peer_info_cancel_transmission (pi, i);
1428 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1438 * Remove all paths that rely on a direct connection between p1 and p2
1439 * from the peer itself and notify all tunnels about it.
1441 * @param peer PeerInfo of affected peer.
1442 * @param p1 GNUNET_PEER_Id of one peer.
1443 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1446 * TODO: optimize (see below)
1449 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1452 struct MeshPeerPath *p;
1453 struct MeshPeerPath *aux;
1454 struct MeshPeerInfo *peer_d;
1456 unsigned int destroyed;
1462 p = peer->path_head;
1466 for (i = 0; i < (p->length - 1); i++)
1468 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1469 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1471 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1482 for (i = 0; i < peer->ntunnels; i++)
1484 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1488 * Problem: one or more peers have been deleted from the tunnel tree.
1489 * We don't know who they are to try to add them again.
1490 * We need to try to find a new path for each of the disconnected peers.
1491 * Some of them might already have a path to reach them that does not
1492 * involve p1 and p2. Adding all anew might render in a better tree than
1493 * the trivial immediate fix.
1495 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1496 * its children will be reachable trough him.
1498 peer_d = peer_info_get_short (d);
1501 for (p = peer_d->path_head; NULL != p; p = p->next)
1503 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1511 /* No callback, as peer will be already disconnected and a connection
1512 * scheduled by tunnel_notify_connection_broken.
1514 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1518 peer_info_connect (peer_d, peer->tunnels[i]);
1525 * Add the path to the peer and update the path used to reach it in case this
1528 * @param peer_info Destination peer to add the path to.
1529 * @param path New path to add. Last peer must be the peer in arg 1.
1530 * Path will be either used of freed if already known.
1531 * @param trusted Do we trust that this path is real?
1534 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1537 struct MeshPeerPath *aux;
1541 if ((NULL == peer_info) || (NULL == path))
1544 path_destroy (path);
1547 if (path->peers[path->length - 1] != peer_info->id)
1550 path_destroy (path);
1553 if (path->length <= 2 && GNUNET_NO == trusted)
1555 /* Only allow CORE to tell us about direct paths */
1556 path_destroy (path);
1559 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1560 for (l = 1; l < path->length; l++)
1562 if (path->peers[l] == myid)
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shortening path by %u\n", l);
1565 for (l2 = 0; l2 < path->length - l; l2++)
1567 path->peers[l2] = path->peers[l + l2];
1572 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1577 struct GNUNET_PeerIdentity id;
1579 GNUNET_PEER_resolve (peer_info->id, &id);
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: adding path [%u] to peer %s\n",
1581 path->length, GNUNET_i2s (&id));
1584 l = path_get_length (path);
1591 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1592 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1594 l2 = path_get_length (aux);
1597 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1598 peer_info->path_tail, aux, path);
1603 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1605 path_destroy (path);
1610 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1617 * Add the path to the origin peer and update the path used to reach it in case
1618 * this is the shortest.
1619 * The path is given in peer_info -> destination, therefore we turn the path
1620 * upside down first.
1622 * @param peer_info Peer to add the path to, being the origin of the path.
1623 * @param path New path to add after being inversed.
1624 * @param trusted Do we trust that this path is real?
1627 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1628 struct MeshPeerPath *path, int trusted)
1631 peer_info_add_path (peer_info, path, trusted);
1636 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1637 * to obtain a local peer -> destination path and interning the peer ids.
1639 * @return Newly allocated and created path
1641 static struct MeshPeerPath *
1642 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1643 unsigned int get_path_length,
1644 const struct GNUNET_PeerIdentity *put_path,
1645 unsigned int put_path_length)
1647 struct MeshPeerPath *p;
1653 GNUNET_PEER_change_rc (myid, 1);
1654 i = get_path_length;
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1656 for (i--; i >= 0; i--)
1658 id = GNUNET_PEER_intern (&get_path[i]);
1659 if (p->length > 0 && id == p->peers[p->length - 1])
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1662 GNUNET_PEER_change_rc (id, -1);
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from GET: %s.\n",
1667 GNUNET_i2s (&get_path[i]));
1669 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1670 p->peers[p->length - 1] = id;
1673 i = put_path_length;
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1675 for (i--; i >= 0; i--)
1677 id = GNUNET_PEER_intern (&put_path[i]);
1680 /* PUT path went through us, so discard the path up until now and start
1681 * from here to get a much shorter (and loop-free) path.
1686 if (p->length > 0 && id == p->peers[p->length - 1])
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1689 GNUNET_PEER_change_rc (id, -1);
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Adding from PUT: %s.\n",
1694 GNUNET_i2s (&put_path[i]));
1696 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1697 p->peers[p->length - 1] = id;
1701 if (get_path_length > 0)
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of GET: %s)\n",
1703 GNUNET_i2s (&get_path[0]));
1704 if (put_path_length > 0)
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (first of PUT: %s)\n",
1706 GNUNET_i2s (&put_path[0]));
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: In total: %d hops\n",
1709 for (i = 0; i < p->length; i++)
1711 struct GNUNET_PeerIdentity peer_id;
1713 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %u: %s\n", p->peers[i],
1715 GNUNET_i2s (&peer_id));
1723 * Adds a path to the peer_infos of all the peers in the path
1725 * @param p Path to process.
1726 * @param confirmed Whether we know if the path works or not. FIXME use
1729 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1733 /* TODO: invert and add */
1734 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1735 for (i++; i < p->length; i++)
1737 struct MeshPeerInfo *aux;
1738 struct MeshPeerPath *copy;
1740 aux = peer_info_get_short (p->peers[i]);
1741 copy = path_duplicate (p);
1742 copy->length = i + 1;
1743 peer_info_add_path (aux, copy, GNUNET_NO);
1749 * Send keepalive packets for a peer
1751 * @param cls Closure (tunnel for which to send the keepalive).
1752 * @param tc Notification context.
1754 * TODO: implement explicit multicast keepalive?
1757 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1761 * Search for a tunnel among the incoming tunnels
1763 * @param tid the local id of the tunnel
1765 * @return tunnel handler, NULL if doesn't exist
1767 static struct MeshTunnel *
1768 tunnel_get_incoming (MESH_TunnelNumber tid)
1770 GNUNET_HashCode hash;
1772 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1773 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1774 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1779 * Search for a tunnel among the tunnels for a client
1781 * @param c the client whose tunnels to search in
1782 * @param tid the local id of the tunnel
1784 * @return tunnel handler, NULL if doesn't exist
1786 static struct MeshTunnel *
1787 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1789 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1791 return tunnel_get_incoming (tid);
1795 GNUNET_HashCode hash;
1797 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1798 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1804 * Search for a tunnel by global ID using PEER_ID
1806 * @param pi owner of the tunnel
1807 * @param tid global tunnel number
1809 * @return tunnel handler, NULL if doesn't exist
1811 static struct MeshTunnel *
1812 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1814 struct MESH_TunnelID id;
1815 GNUNET_HashCode hash;
1820 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1821 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1826 * Search for a tunnel by global ID using full PeerIdentities
1828 * @param oid owner of the tunnel
1829 * @param tid global tunnel number
1831 * @return tunnel handler, NULL if doesn't exist
1833 static struct MeshTunnel *
1834 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1836 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1841 * Callback used to notify a client owner of a tunnel that a peer has
1842 * disconnected, most likely because of a path change.
1844 * @param cls Closure (tunnel this notification is about).
1845 * @param peer_id Short ID of disconnected peer.
1848 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
1850 struct MeshTunnel *t = cls;
1851 struct MeshPeerInfo *peer;
1852 struct MeshPathInfo *path_info;
1854 if (NULL != t->client && NULL != nc)
1856 struct GNUNET_MESH_PeerControl msg;
1858 msg.header.size = htons (sizeof (msg));
1859 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1860 msg.tunnel_id = htonl (t->local_tid);
1861 GNUNET_PEER_resolve (peer_id, &msg.peer);
1862 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
1863 &msg.header, GNUNET_NO);
1865 peer = peer_info_get_short (peer_id);
1866 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1867 path_info->peer = peer;
1869 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
1874 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1875 * needed rescources.
1876 * If peer already exists, reevaluate shortest path and change if different.
1878 * @param t Tunnel we want to add a new peer to
1879 * @param peer PeerInfo of the peer being added
1883 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1885 struct GNUNET_PeerIdentity id;
1886 struct MeshPeerPath *best_p;
1887 struct MeshPeerPath *p;
1888 unsigned int best_cost;
1891 GNUNET_PEER_resolve (peer->id, &id);
1893 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
1896 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1897 GNUNET_assert (GNUNET_OK ==
1898 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
1900 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1903 if (NULL != (p = peer->path_head))
1906 best_cost = tree_get_path_cost (t->tree, p);
1909 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
1916 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
1917 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1918 t->path_refresh_task =
1919 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
1923 /* Start a DHT get */
1924 peer_info_connect (peer, t);
1929 * Add a path to a tunnel which we don't own, just to remember the next hop.
1930 * If destination node was already in the tunnel, the first hop information
1931 * will be replaced with the new path.
1933 * @param t Tunnel we want to add a new peer to
1934 * @param p Path to add
1935 * @param own_pos Position of local node in path.
1939 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
1940 unsigned int own_pos)
1942 struct GNUNET_PeerIdentity id;
1944 GNUNET_assert (0 != own_pos);
1945 tree_add_path (t->tree, p, NULL, NULL);
1946 if (own_pos < p->length - 1)
1948 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1949 tree_update_first_hops (t->tree, myid, &id);
1955 * Notifies a tunnel that a connection has broken that affects at least
1956 * some of its peers. Sends a notification towards the root of the tree.
1957 * In case the peer is the owner of the tree, notifies the client that owns
1958 * the tunnel and tries to reconnect.
1960 * @param t Tunnel affected.
1961 * @param p1 Peer that got disconnected from p2.
1962 * @param p2 Peer that got disconnected from p1.
1964 * @return Short ID of the peer disconnected (either p1 or p2).
1965 * 0 if the tunnel remained unaffected.
1967 static GNUNET_PEER_Id
1968 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1974 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
1976 if (myid != p1 && myid != p2)
1982 if (tree_get_predecessor (t->tree) != 0)
1984 /* We are the peer still connected, notify owner of the disconnection. */
1985 struct GNUNET_MESH_PathBroken msg;
1986 struct GNUNET_PeerIdentity neighbor;
1988 msg.header.size = htons (sizeof (msg));
1989 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1990 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1991 msg.tid = htonl (t->id.tid);
1992 msg.peer1 = my_full_id;
1993 GNUNET_PEER_resolve (pid, &msg.peer2);
1994 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
1995 send_message (&msg.header, &neighbor);
2003 * Send a multicast packet to a neighbor.
2005 * @param cls Closure (Info about the multicast packet)
2006 * @param neighbor_id Short ID of the neighbor to send the packet to.
2009 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2011 struct MeshData *mdata = cls;
2012 struct MeshTransmissionDescriptor *info;
2013 struct GNUNET_PeerIdentity neighbor;
2016 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2018 info->mesh_data = mdata;
2019 (*(mdata->reference_counter)) ++;
2020 info->destination = neighbor_id;
2021 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending to %s...\n",
2024 GNUNET_i2s (&neighbor));
2026 info->peer = peer_info_get (&neighbor);
2027 GNUNET_assert (NULL != info->peer);
2028 i = peer_info_transmit_slot (info->peer);
2029 info->handler_n = i;
2030 info->peer->infos[i] = info;
2031 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
2032 info->peer->core_transmit[i] =
2033 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2034 GNUNET_TIME_UNIT_FOREVER_REL,
2035 &neighbor, info->mesh_data->data_len,
2036 &send_core_data_multicast, info);
2040 * Send a message in a tunnel in multicast, sending a copy to each child node
2041 * down the local one in the tunnel tree.
2043 * @param t Tunnel in which to send the data.
2044 * @param msg Message to be sent
2047 tunnel_send_multicast (struct MeshTunnel *t,
2048 const struct GNUNET_MessageHeader *msg)
2050 struct MeshData *mdata;
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2054 "MESH: sending a multicast packet...\n");
2056 mdata = GNUNET_malloc (sizeof (struct MeshData));
2057 mdata->data_len = ntohs (msg->size);
2058 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2060 mdata->data = GNUNET_malloc (mdata->data_len);
2061 memcpy (mdata->data, msg, mdata->data_len);
2062 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2064 struct GNUNET_MESH_Multicast *mcast;
2066 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2067 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: data packet, ttl: %u\n",
2070 ntohl (mcast->ttl));
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: not a data packet, no ttl\n");
2077 if (NULL != t->client)
2079 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2081 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: timeout task %u\n",
2087 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2088 if (*(mdata->reference_counter) == 0)
2090 GNUNET_free (mdata->data);
2091 GNUNET_free (mdata->reference_counter);
2092 if (NULL != mdata->task)
2094 GNUNET_SCHEDULER_cancel(*(mdata->task));
2095 GNUNET_free (mdata->task);
2097 // FIXME change order?
2098 GNUNET_free (mdata);
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2102 "MESH: sending a multicast packet done\n");
2109 * Send a message to all peers in this tunnel that the tunnel is no longer
2112 * @param t The tunnel whose peers to notify.
2115 tunnel_send_destroy (struct MeshTunnel *t)
2117 struct GNUNET_MESH_TunnelDestroy msg;
2119 msg.header.size = htons (sizeof (msg));
2120 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2121 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2122 msg.tid = htonl (t->id.tid);
2123 tunnel_send_multicast (t, &msg.header);
2129 * Destroy the tunnel and free any allocated resources linked to it
2131 * @param t the tunnel to destroy
2133 * @return GNUNET_OK on success
2136 tunnel_destroy (struct MeshTunnel *t)
2138 struct MeshClient *c;
2139 struct MeshQueue *q;
2140 struct MeshQueue *qn;
2141 GNUNET_HashCode hash;
2150 struct GNUNET_PeerIdentity id;
2152 GNUNET_PEER_resolve (t->id.oid, &id);
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: destroying tunnel %s [%x]\n",
2154 GNUNET_i2s (&id), t->id.tid);
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2160 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2161 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2166 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2168 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2172 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2174 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2175 GNUNET_break (GNUNET_YES ==
2176 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash,
2179 if (NULL != t->peers)
2181 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2183 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2188 if (NULL != q->data)
2189 GNUNET_free (q->data);
2193 /* TODO cancel core transmit ready in case it was active */
2195 tree_destroy (t->tree);
2196 if (NULL != t->dht_get_type)
2197 GNUNET_DHT_get_stop (t->dht_get_type);
2198 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2199 GNUNET_SCHEDULER_cancel (t->timeout_task);
2200 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2201 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2208 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2209 * that are no longer needed, as well as nodes of no longer reachable peers.
2210 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2212 * @param t Tunnel from which to remove the path.
2213 * @param peer Short id of the peer which should be removed.
2216 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2218 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2224 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2225 * client when the client disconnects.
2227 * @param cls closure (client that is disconnecting)
2228 * @param key the hash of the local tunnel id (used to access the hashmap)
2229 * @param value the value stored at the key (tunnel to destroy)
2231 * @return GNUNET_OK on success
2234 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2236 struct MeshTunnel *t = value;
2239 r = tunnel_destroy (t);
2245 * Timeout function, destroys tunnel if called
2247 * @param cls Closure (tunnel to destroy).
2248 * @param tc TaskContext
2251 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2253 struct MeshTunnel *t = cls;
2255 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2257 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2262 * Resets the tunnel timeout. Starts it if no timeout was running.
2264 * @param t Tunnel whose timeout to reset.
2267 tunnel_reset_timeout (struct MeshTunnel *t)
2269 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2270 GNUNET_SCHEDULER_cancel (t->timeout_task);
2272 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2273 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2277 /******************************************************************************/
2278 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2279 /******************************************************************************/
2282 * Function called to notify a client about the socket
2283 * being ready to queue more data. "buf" will be
2284 * NULL and "size" zero if the socket was closed for
2285 * writing in the meantime.
2287 * @param cls closure
2288 * @param size number of bytes available in buf
2289 * @param buf where the callee should write the message
2290 * @return number of bytes written to buf
2293 send_core_create_path (void *cls, size_t size, void *buf)
2295 struct MeshPathInfo *info = cls;
2296 struct GNUNET_MESH_ManipulatePath *msg;
2297 struct GNUNET_PeerIdentity *peer_ptr;
2298 struct MeshPeerInfo *peer = info->peer;
2299 struct MeshTunnel *t = info->t;
2300 struct MeshPeerPath *p = info->path;
2304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATE PATH sending...\n");
2306 sizeof (struct GNUNET_MESH_ManipulatePath) +
2307 p->length * sizeof (struct GNUNET_PeerIdentity);
2309 if (size < size_needed || NULL == buf)
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n", size,
2315 info->peer->core_transmit[info->pos] =
2316 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2317 GNUNET_TIME_UNIT_FOREVER_REL,
2318 tree_get_first_hop (t->tree,
2320 size_needed, &send_core_create_path,
2324 info->peer->core_transmit[info->pos] = NULL;
2327 struct GNUNET_PeerIdentity id;
2329 GNUNET_PEER_resolve (peer->id, &id);
2330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 "MESH: setting core_transmit %s [%u] to NULL\n",
2332 GNUNET_i2s (&id), info->pos);
2335 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2336 msg->header.size = htons (size_needed);
2337 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2338 msg->tid = ntohl (t->id.tid);
2340 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2341 for (i = 0; i < p->length; i++)
2343 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "MESH: CREATE PATH (%u bytes long) sent!\n", size_needed);
2356 * Function called to notify a client about the socket
2357 * being ready to queue more data. "buf" will be
2358 * NULL and "size" zero if the socket was closed for
2359 * writing in the meantime.
2361 * @param cls closure (data itself)
2362 * @param size number of bytes available in buf
2363 * @param buf where the callee should write the message
2365 * @return number of bytes written to buf
2368 send_core_data_multicast (void *cls, size_t size, void *buf)
2370 struct MeshTransmissionDescriptor *info = cls;
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2374 GNUNET_assert (NULL != info);
2375 GNUNET_assert (NULL != info->peer);
2376 total_size = info->mesh_data->data_len;
2377 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2379 if (total_size > size)
2382 struct GNUNET_PeerIdentity id;
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385 "MESH: Multicast: retransmitting... (%u/%u)\n", size,
2387 GNUNET_PEER_resolve (info->peer->id, &id);
2388 info->peer->core_transmit[info->handler_n] =
2389 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2390 GNUNET_TIME_UNIT_FOREVER_REL, &id,
2392 &send_core_data_multicast, info);
2395 info->peer->core_transmit[info->handler_n] = NULL;
2396 info->peer->infos[info->handler_n] = NULL;
2397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2398 memcpy (buf, info->mesh_data->data, total_size);
2401 struct GNUNET_MESH_Multicast *mc;
2402 struct GNUNET_MessageHeader *mh;
2405 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2407 mc = (struct GNUNET_MESH_Multicast *) mh;
2408 mh = (struct GNUNET_MessageHeader *) &mc[1];
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "MESH: multicast, payload type %u\n", ntohs (mh->type));
2411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2412 "MESH: multicast, payload size %u\n", ntohs (mh->size));
2416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type %u\n",
2421 data_descriptor_decrement_multicast (info->mesh_data);
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2430 * Function called to notify a client about the socket
2431 * being ready to queue more data. "buf" will be
2432 * NULL and "size" zero if the socket was closed for
2433 * writing in the meantime.
2435 * @param cls closure (MeshTransmissionDescriptor)
2436 * @param size number of bytes available in buf
2437 * @param buf where the callee should write the message
2438 * @return number of bytes written to buf
2441 send_core_path_ack (void *cls, size_t size, void *buf)
2443 struct MeshTransmissionDescriptor *info = cls;
2444 struct GNUNET_MESH_PathACK *msg = buf;
2446 GNUNET_assert (NULL != info);
2449 info->peer->core_transmit[info->handler_n] = NULL;
2451 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2456 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2457 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2458 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2459 msg->tid = htonl (info->origin->tid);
2460 msg->peer_id = my_full_id;
2462 /* TODO add signature */
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2465 return sizeof (struct GNUNET_MESH_PathACK);
2469 /******************************************************************************/
2470 /******************** MESH NETWORK HANDLERS **************************/
2471 /******************************************************************************/
2475 * Core handler for path creation
2477 * @param cls closure
2478 * @param message message
2479 * @param peer peer identity this notification is about
2480 * @param atsi performance data
2481 * @param atsi_count number of records in 'atsi'
2483 * @return GNUNET_OK to keep the connection open,
2484 * GNUNET_SYSERR to close it (signal serious error)
2487 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2488 const struct GNUNET_MessageHeader *message,
2489 const struct GNUNET_ATS_Information *atsi,
2490 unsigned int atsi_count)
2492 unsigned int own_pos;
2495 MESH_TunnelNumber tid;
2496 struct GNUNET_MESH_ManipulatePath *msg;
2497 struct GNUNET_PeerIdentity *pi;
2498 GNUNET_HashCode hash;
2499 struct MeshPeerPath *path;
2500 struct MeshPeerInfo *dest_peer_info;
2501 struct MeshPeerInfo *orig_peer_info;
2502 struct MeshTunnel *t;
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "MESH: Received a path create msg [%s]\n",
2506 GNUNET_i2s (&my_full_id));
2507 size = ntohs (message->size);
2508 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2510 GNUNET_break_op (0);
2514 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2515 if (size % sizeof (struct GNUNET_PeerIdentity))
2517 GNUNET_break_op (0);
2520 size /= sizeof (struct GNUNET_PeerIdentity);
2523 GNUNET_break_op (0);
2526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2527 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2529 tid = ntohl (msg->tid);
2530 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2531 t = tunnel_get (pi, tid);
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2537 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2538 t->id.oid = GNUNET_PEER_intern (pi);
2540 while (NULL != tunnel_get_incoming (next_local_tid))
2541 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2542 t->local_tid = next_local_tid++;
2543 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2544 t->tree = tree_new (t->id.oid);
2546 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2548 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2549 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2555 tunnel_reset_timeout (t);
2556 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2558 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2567 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2568 if (NULL == dest_peer_info)
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571 "MESH: Creating PeerInfo for destination.\n");
2572 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2573 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2574 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2578 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2579 if (NULL == orig_peer_info)
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2582 "MESH: Creating PeerInfo for origin.\n");
2583 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2584 orig_peer_info->id = GNUNET_PEER_intern (pi);
2585 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2586 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2589 path = path_new (size);
2591 for (i = 0; i < size; i++)
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2594 GNUNET_i2s (&pi[i]));
2595 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2596 if (path->peers[i] == myid)
2599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2602 /* cannot be self, must be 'not found' */
2603 /* create path: self not found in path through self */
2604 GNUNET_break_op (0);
2605 path_destroy (path);
2606 /* FIXME error. destroy tunnel? leave for timeout? */
2609 path_add_to_peers (path, GNUNET_NO);
2610 tunnel_add_path (t, path, own_pos);
2611 if (own_pos == size - 1)
2613 /* It is for us! Send ack. */
2614 struct MeshTransmissionDescriptor *info;
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
2618 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2619 if (NULL == t->peers)
2621 /* New tunnel! Notify clients! */
2622 struct GNUNET_MESH_TunnelNotification cmsg;
2624 cmsg.header.size = htons (sizeof (cmsg));
2625 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2626 GNUNET_PEER_resolve (t->id.oid, &cmsg.peer);
2627 cmsg.tunnel_id = htonl (t->local_tid);
2628 GNUNET_SERVER_notification_context_broadcast (nc, &cmsg.header,
2631 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2633 GNUNET_break (GNUNET_OK ==
2634 GNUNET_CONTAINER_multihashmap_put (t->peers,
2635 &my_full_id.hashPubKey,
2638 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2639 /* FIXME use send_message */
2640 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2641 info->origin = &t->id;
2642 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2643 GNUNET_assert (NULL != info->peer);
2644 j = peer_info_transmit_slot (info->peer);
2645 info->handler_n = j;
2646 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2647 info->peer->infos[j] = info;
2648 info->peer->core_transmit[j] =
2649 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2650 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2651 sizeof (struct GNUNET_MESH_PathACK),
2652 &send_core_path_ack, info);
2656 struct MeshPeerPath *path2;
2658 /* It's for somebody else! Retransmit. */
2659 path2 = path_duplicate (path);
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Retransmitting.\n");
2661 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
2662 path2 = path_duplicate (path);
2663 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
2664 send_create_path (dest_peer_info, path, t);
2671 * Core handler for path destruction
2673 * @param cls closure
2674 * @param message message
2675 * @param peer peer identity this notification is about
2676 * @param atsi performance data
2677 * @param atsi_count number of records in 'atsi'
2679 * @return GNUNET_OK to keep the connection open,
2680 * GNUNET_SYSERR to close it (signal serious error)
2683 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2684 const struct GNUNET_MessageHeader *message,
2685 const struct GNUNET_ATS_Information *atsi,
2686 unsigned int atsi_count)
2688 struct GNUNET_MESH_ManipulatePath *msg;
2689 struct GNUNET_PeerIdentity *pi;
2690 struct MeshPeerPath *path;
2691 struct MeshTunnel *t;
2692 unsigned int own_pos;
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2697 "MESH: Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
2698 size = ntohs (message->size);
2699 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2701 GNUNET_break_op (0);
2705 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2706 if (size % sizeof (struct GNUNET_PeerIdentity))
2708 GNUNET_break_op (0);
2711 size /= sizeof (struct GNUNET_PeerIdentity);
2714 GNUNET_break_op (0);
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: path has %u hops.\n", size);
2719 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2720 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2722 "MESH: path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
2724 t = tunnel_get (pi, ntohl (msg->tid));
2727 /* TODO notify back: we don't know this tunnel */
2728 GNUNET_break_op (0);
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2732 path = path_new (size);
2734 for (i = 0; i < size; i++)
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... adding %s\n",
2737 GNUNET_i2s (&pi[i]));
2738 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2739 if (path->peers[i] == myid)
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Own position: %u\n", own_pos);
2743 if (own_pos < path->length - 1)
2744 send_message (message, &pi[own_pos + 1]);
2745 tunnel_delete_peer (t, path->peers[path->length - 1]);
2746 path_destroy (path);
2752 * Core handler for notifications of broken paths
2754 * @param cls closure
2755 * @param message message
2756 * @param peer peer identity this notification is about
2757 * @param atsi performance data
2758 * @param atsi_count number of records in 'atsi'
2760 * @return GNUNET_OK to keep the connection open,
2761 * GNUNET_SYSERR to close it (signal serious error)
2764 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2765 const struct GNUNET_MessageHeader *message,
2766 const struct GNUNET_ATS_Information *atsi,
2767 unsigned int atsi_count)
2769 struct GNUNET_MESH_PathBroken *msg;
2770 struct MeshTunnel *t;
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 "MESH: Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
2774 msg = (struct GNUNET_MESH_PathBroken *) message;
2775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2776 GNUNET_i2s (&msg->peer1));
2777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: regarding %s\n",
2778 GNUNET_i2s (&msg->peer2));
2779 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2782 GNUNET_break_op (0);
2785 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
2786 GNUNET_PEER_search (&msg->peer2));
2793 * Core handler for tunnel destruction
2795 * @param cls closure
2796 * @param message message
2797 * @param peer peer identity this notification is about
2798 * @param atsi performance data
2799 * @param atsi_count number of records in 'atsi'
2801 * @return GNUNET_OK to keep the connection open,
2802 * GNUNET_SYSERR to close it (signal serious error)
2805 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2806 const struct GNUNET_MessageHeader *message,
2807 const struct GNUNET_ATS_Information *atsi,
2808 unsigned int atsi_count)
2810 struct GNUNET_MESH_TunnelDestroy *msg;
2811 struct MeshTunnel *t;
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2814 "MESH: Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
2815 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for tunnel %s [%u]\n",
2817 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
2818 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2821 /* Probably already got the message from another path,
2822 * destroyed the tunnel and retransmitted to children.
2827 if (t->id.oid == myid)
2829 GNUNET_break_op (0);
2832 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2834 /* Tunnel was incoming, notify clients */
2835 send_clients_tunnel_destroy (t);
2837 tunnel_send_destroy (t);
2844 * Core handler for mesh network traffic going from the origin to a peer
2846 * @param cls closure
2847 * @param peer peer identity this notification is about
2848 * @param message message
2849 * @param atsi performance data
2850 * @param atsi_count number of records in 'atsi'
2851 * @return GNUNET_OK to keep the connection open,
2852 * GNUNET_SYSERR to close it (signal serious error)
2855 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2856 const struct GNUNET_MessageHeader *message,
2857 const struct GNUNET_ATS_Information *atsi,
2858 unsigned int atsi_count)
2860 struct GNUNET_MESH_Unicast *msg;
2861 struct MeshTunnel *t;
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a unicast packet from %s\n",
2867 size = ntohs (message->size);
2869 sizeof (struct GNUNET_MESH_Unicast) +
2870 sizeof (struct GNUNET_MessageHeader))
2875 msg = (struct GNUNET_MESH_Unicast *) message;
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
2877 ntohs (msg[1].header.type));
2878 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2881 /* TODO notify back: we don't know this tunnel */
2882 GNUNET_break_op (0);
2885 tunnel_reset_timeout (t);
2886 pid = GNUNET_PEER_search (&msg->destination);
2889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2890 "MESH: it's for us! sending to clients...\n");
2891 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2895 "MESH: not for us, retransmitting...\n");
2896 send_message (message, tree_get_first_hop (t->tree, pid));
2902 * Core handler for mesh network traffic going from the origin to all peers
2904 * @param cls closure
2905 * @param message message
2906 * @param peer peer identity this notification is about
2907 * @param atsi performance data
2908 * @param atsi_count number of records in 'atsi'
2909 * @return GNUNET_OK to keep the connection open,
2910 * GNUNET_SYSERR to close it (signal serious error)
2912 * TODO: Check who we got this from, to validate route.
2915 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2916 const struct GNUNET_MessageHeader *message,
2917 const struct GNUNET_ATS_Information *atsi,
2918 unsigned int atsi_count)
2920 struct GNUNET_MESH_Multicast *msg;
2921 struct MeshTunnel *t;
2924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a multicast packet from %s\n",
2926 size = ntohs (message->size);
2927 if (sizeof (struct GNUNET_MESH_Multicast) +
2928 sizeof (struct GNUNET_MessageHeader) > size)
2930 GNUNET_break_op (0);
2933 msg = (struct GNUNET_MESH_Multicast *) message;
2934 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2938 /* TODO notify that we dont know that tunnel */
2939 GNUNET_break_op (0);
2942 if (t->mid == ntohl (msg->mid))
2944 /* FIXME: already seen this packet, log dropping */
2945 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2946 "MESH: Already seen mid %u, DROPPING!\n", t->mid);
2952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2953 "MESH: mid %u not seen yet, forwarding\n", ntohl (msg->mid));
2956 t->mid = ntohl (msg->mid);
2957 tunnel_reset_timeout (t);
2959 /* Transmit to locally interested clients */
2960 if (NULL != t->peers &&
2961 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2963 send_subscribed_clients (message, &msg[1].header);
2966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ttl: %u\n", ntohl (msg->ttl));
2968 if (ntohl (msg->ttl) == 0)
2970 /* FIXME: ttl is 0, log dropping */
2971 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "MESH: TTL is 0, DROPPING!\n");
2974 tunnel_send_multicast (t, message);
2980 * Core handler for mesh network traffic toward the owner of a tunnel
2982 * @param cls closure
2983 * @param message message
2984 * @param peer peer identity this notification is about
2985 * @param atsi performance data
2986 * @param atsi_count number of records in 'atsi'
2988 * @return GNUNET_OK to keep the connection open,
2989 * GNUNET_SYSERR to close it (signal serious error)
2992 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2993 const struct GNUNET_MessageHeader *message,
2994 const struct GNUNET_ATS_Information *atsi,
2995 unsigned int atsi_count)
2997 struct GNUNET_MESH_ToOrigin *msg;
2998 struct GNUNET_PeerIdentity id;
2999 struct MeshPeerInfo *peer_info;
3000 struct MeshTunnel *t;
3003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got a ToOrigin packet from %s\n",
3005 size = ntohs (message->size);
3006 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3007 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3009 GNUNET_break_op (0);
3012 msg = (struct GNUNET_MESH_ToOrigin *) message;
3013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: of type %u\n",
3014 ntohs (msg[1].header.type));
3015 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3019 /* TODO notify that we dont know this tunnel (whom)? */
3020 GNUNET_break_op (0);
3024 if (t->id.oid == myid)
3027 struct GNUNET_MESH_ToOrigin *copy;
3029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3030 "MESH: it's for us! sending to clients...\n");
3031 if (NULL == t->client)
3033 /* got data packet for ownerless tunnel */
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: no clients!\n");
3035 GNUNET_break_op (0);
3038 /* TODO signature verification */
3039 memcpy (cbuf, message, size);
3040 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3041 copy->tid = htonl (t->local_tid);
3042 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
3043 ©->header, GNUNET_YES);
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 "MESH: not for us, retransmitting...\n");
3049 peer_info = peer_info_get (&msg->oid);
3050 if (NULL == peer_info)
3052 /* unknown origin of tunnel */
3056 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3057 send_message (message, &id);
3064 * Core handler for path ACKs
3066 * @param cls closure
3067 * @param message message
3068 * @param peer peer identity this notification is about
3069 * @param atsi performance data
3070 * @param atsi_count number of records in 'atsi'
3072 * @return GNUNET_OK to keep the connection open,
3073 * GNUNET_SYSERR to close it (signal serious error)
3076 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3077 const struct GNUNET_MessageHeader *message,
3078 const struct GNUNET_ATS_Information *atsi,
3079 unsigned int atsi_count)
3081 struct GNUNET_MESH_PathACK *msg;
3082 struct GNUNET_PeerIdentity id;
3083 struct MeshPeerInfo *peer_info;
3084 struct MeshPeerPath *p;
3085 struct MeshTunnel *t;
3087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Received a path ACK msg [%s]\n",
3088 GNUNET_i2s (&my_full_id));
3089 msg = (struct GNUNET_MESH_PathACK *) message;
3090 t = tunnel_get (&msg->oid, msg->tid);
3093 /* TODO notify that we don't know the tunnel */
3097 peer_info = peer_info_get (&msg->peer_id);
3099 /* Add paths to peers? */
3100 p = tree_get_path_to_peer (t->tree, peer_info->id);
3103 path_add_to_peers (p, GNUNET_YES);
3111 /* Message for us? */
3112 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3115 if (NULL == t->client)
3117 GNUNET_break_op (0);
3120 if (NULL != t->dht_get_type)
3122 GNUNET_DHT_get_stop (t->dht_get_type);
3123 t->dht_get_type = NULL;
3125 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3127 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3128 send_client_peer_connected (t, peer_info->id);
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3134 "MESH: not for us, retransmitting...\n");
3135 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3136 peer_info = peer_info_get (&msg->oid);
3137 if (NULL == peer_info)
3139 /* If we know the tunnel, we should DEFINITELY know the peer */
3143 send_message (message, &id);
3149 * Functions to handle messages from core
3151 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3152 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3153 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3154 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3155 sizeof (struct GNUNET_MESH_PathBroken)},
3156 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3157 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3158 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3159 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3160 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3161 sizeof (struct GNUNET_MESH_PathACK)},
3167 /******************************************************************************/
3168 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3169 /******************************************************************************/
3172 * deregister_app: iterator for removing each application registered by a client
3174 * @param cls closure
3175 * @param key the hash of the application id (used to access the hashmap)
3176 * @param value the value stored at the key (client)
3178 * @return GNUNET_OK on success
3181 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3183 GNUNET_break (GNUNET_YES ==
3184 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3191 * notify_client_connection_failure: notify a client that the connection to the
3192 * requested remote peer is not possible (for instance, no route found)
3193 * Function called when the socket is ready to queue more data. "buf" will be
3194 * NULL and "size" zero if the socket was closed for writing in the meantime.
3196 * @param cls closure
3197 * @param size number of bytes available in buf
3198 * @param buf where the callee should write the message
3199 * @return number of bytes written to buf
3202 notify_client_connection_failure (void *cls, size_t size, void *buf)
3205 struct MeshPeerInfo *peer_info;
3206 struct GNUNET_MESH_PeerControl *msg;
3207 struct GNUNET_PeerIdentity id;
3209 if (0 == size && NULL == buf)
3211 // TODO retry? cancel?
3215 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3216 peer_info = (struct MeshPeerInfo *) cls;
3217 msg = (struct GNUNET_MESH_PeerControl *) buf;
3218 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3219 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3220 // msg->tunnel_id = htonl(peer_info->t->tid);
3221 GNUNET_PEER_resolve (peer_info->id, &id);
3222 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3230 * Send keepalive packets for a peer
3232 * @param cls Closure (tunnel for which to send the keepalive).
3233 * @param tc Notification context.
3235 * TODO: implement explicit multicast keepalive?
3238 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3240 struct MeshTunnel *t = cls;
3241 struct GNUNET_MessageHeader *payload;
3242 struct GNUNET_MESH_Multicast *msg;
3244 sizeof (struct GNUNET_MESH_Multicast) +
3245 sizeof (struct GNUNET_MessageHeader);
3248 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3252 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3255 "MESH: sending keepalive for tunnel %d\n", t->id.tid);
3257 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3258 msg->header.size = htons (size);
3259 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3260 msg->oid = my_full_id;
3261 msg->tid = htonl (t->id.tid);
3262 payload = (struct GNUNET_MessageHeader *) &msg[1];
3263 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3264 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3265 tunnel_send_multicast (t, &msg->header);
3267 t->path_refresh_task =
3268 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3274 * Function to process paths received for a new peer addition. The recorded
3275 * paths form the initial tunnel, which can be optimized later.
3276 * Called on each result obtained for the DHT search.
3278 * @param cls closure
3279 * @param exp when will this value expire
3280 * @param key key of the result
3281 * @param get_path path of the get request
3282 * @param get_path_length lenght of get_path
3283 * @param put_path path of the put request
3284 * @param put_path_length length of the put_path
3285 * @param type type of the result
3286 * @param size number of bytes in data
3287 * @param data pointer to the result data
3289 * TODO: re-issue the request after certain time? cancel after X results?
3292 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3293 const GNUNET_HashCode * key,
3294 const struct GNUNET_PeerIdentity *get_path,
3295 unsigned int get_path_length,
3296 const struct GNUNET_PeerIdentity *put_path,
3297 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3298 size_t size, const void *data)
3300 struct MeshPathInfo *path_info = cls;
3301 struct MeshPeerPath *p;
3302 struct GNUNET_PeerIdentity pi;
3305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got results from DHT!\n");
3306 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n", GNUNET_i2s (&pi));
3309 p = path_build_from_dht (get_path, get_path_length, put_path,
3311 path_add_to_peers (p, GNUNET_NO);
3313 for (i = 0; i < path_info->peer->ntunnels; i++)
3315 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3316 peer_info_connect (path_info->peer, path_info->t);
3324 * Function to process paths received for a new peer addition. The recorded
3325 * paths form the initial tunnel, which can be optimized later.
3326 * Called on each result obtained for the DHT search.
3328 * @param cls closure
3329 * @param exp when will this value expire
3330 * @param key key of the result
3331 * @param get_path path of the get request
3332 * @param get_path_length lenght of get_path
3333 * @param put_path path of the put request
3334 * @param put_path_length length of the put_path
3335 * @param type type of the result
3336 * @param size number of bytes in data
3337 * @param data pointer to the result data
3340 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3341 const GNUNET_HashCode * key,
3342 const struct GNUNET_PeerIdentity *get_path,
3343 unsigned int get_path_length,
3344 const struct GNUNET_PeerIdentity *put_path,
3345 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3346 size_t size, const void *data)
3348 const struct GNUNET_PeerIdentity *pi = data;
3349 struct MeshTunnel *t = cls;
3350 struct MeshPeerInfo *peer_info;
3351 struct MeshPeerPath *p;
3353 if (size != sizeof (struct GNUNET_PeerIdentity))
3355 GNUNET_break_op (0);
3358 GNUNET_assert (NULL != t->client);
3359 peer_info = peer_info_get (pi);
3360 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3362 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3364 p = path_build_from_dht (get_path, get_path_length, put_path,
3366 path_add_to_peers (p, GNUNET_NO);
3368 tunnel_add_peer (t, peer_info);
3369 peer_info_connect (peer_info, t);
3373 /******************************************************************************/
3374 /********************* MESH LOCAL HANDLES **************************/
3375 /******************************************************************************/
3379 * Handler for client disconnection
3381 * @param cls closure
3382 * @param client identification of the client; NULL
3383 * for the last call when the server is destroyed
3386 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3388 struct MeshClient *c;
3389 struct MeshClient *next;
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3400 if (c->handle != client && NULL != client)
3402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3407 GNUNET_SERVER_client_drop (c->handle);
3408 if (NULL != c->tunnels)
3410 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3411 &tunnel_destroy_iterator, c);
3412 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3415 /* deregister clients applications */
3416 if (NULL != c->apps)
3418 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3419 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3421 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3422 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3424 GNUNET_SCHEDULER_cancel (announce_applications_task);
3425 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3427 if (NULL != c->types)
3428 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3430 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3442 * Handler for new clients
3444 * @param cls closure
3445 * @param client identification of the client
3446 * @param message the actual message, which includes messages the client wants
3449 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3450 const struct GNUNET_MessageHeader *message)
3452 struct GNUNET_MESH_ClientConnect *cc_msg;
3453 struct MeshClient *c;
3454 GNUNET_MESH_ApplicationType *a;
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3462 /* Check data sanity */
3463 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3464 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3465 ntypes = ntohs (cc_msg->types);
3466 napps = ntohs (cc_msg->applications);
3468 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3475 /* Create new client structure */
3476 c = GNUNET_malloc (sizeof (struct MeshClient));
3478 c->id = next_client_id++;
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id,
3483 GNUNET_SERVER_client_keep (client);
3484 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3487 GNUNET_MESH_ApplicationType at;
3490 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3491 for (i = 0; i < napps; i++)
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3495 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3496 /* store in clients hashmap */
3497 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3499 /* store in global hashmap, for announcements */
3500 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3501 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3503 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3504 announce_applications_task =
3505 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3513 t = (uint16_t *) & a[napps];
3514 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3515 for (i = 0; i < ntypes; i++)
3518 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3520 /* store in clients hashmap */
3521 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3522 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3523 /* store in global hashmap */
3524 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3525 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3529 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3531 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3532 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3533 GNUNET_SERVER_notification_context_add (nc, client);
3535 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3543 * Handler for requests of new tunnels
3545 * @param cls closure
3546 * @param client identification of the client
3547 * @param message the actual message
3550 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3551 const struct GNUNET_MessageHeader *message)
3553 struct GNUNET_MESH_TunnelMessage *t_msg;
3554 struct MeshTunnel *t;
3555 struct MeshClient *c;
3556 GNUNET_HashCode hash;
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3560 /* Sanity check for client registration */
3561 if (NULL == (c = client_get (client)))
3564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3571 /* Message sanity check */
3572 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3579 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3580 /* Sanity check for tunnel numbering */
3581 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3584 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3587 /* Sanity check for duplicate tunnel IDs */
3588 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3591 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3595 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3596 while (NULL != tunnel_get_by_pi (myid, next_tid))
3597 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3598 t->id.tid = next_tid++;
3599 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3601 t->local_tid = ntohl (t_msg->tunnel_id);
3603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3604 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3607 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3609 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3611 GNUNET_CONTAINER_multihashmap_put (c->tunnels, &hash, t,
3612 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3615 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3619 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3621 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3628 t->tree = tree_new (myid);
3630 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3636 * Handler for requests of deleting tunnels
3638 * @param cls closure
3639 * @param client identification of the client
3640 * @param message the actual message
3643 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3644 const struct GNUNET_MessageHeader *message)
3646 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3647 struct MeshClient *c;
3648 struct MeshTunnel *t;
3649 MESH_TunnelNumber tid;
3650 GNUNET_HashCode hash;
3652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3653 "MESH: Got a DESTROY TUNNEL from client!\n");
3655 /* Sanity check for client registration */
3656 if (NULL == (c = client_get (client)))
3659 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3662 /* Message sanity check */
3663 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3672 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3674 /* Retrieve tunnel */
3675 tid = ntohl (tunnel_msg->tunnel_id);
3677 /* Remove from local id hashmap */
3678 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3679 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3680 GNUNET_assert (GNUNET_YES ==
3681 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t));
3684 tunnel_send_destroy (t);
3686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3692 * Handler for connection requests to new peers
3694 * @param cls closure
3695 * @param client identification of the client
3696 * @param message the actual message (PeerControl)
3699 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3700 const struct GNUNET_MessageHeader *message)
3702 struct GNUNET_MESH_PeerControl *peer_msg;
3703 struct MeshPeerInfo *peer_info;
3704 struct MeshClient *c;
3705 struct MeshTunnel *t;
3706 MESH_TunnelNumber tid;
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3709 /* Sanity check for client registration */
3710 if (NULL == (c = client_get (client)))
3713 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3717 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3718 /* Sanity check for message size */
3719 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3726 /* Tunnel exists? */
3727 tid = ntohl (peer_msg->tunnel_id);
3728 t = tunnel_get_by_local_id (c, tid);
3732 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3736 /* Does client own tunnel? */
3737 if (t->client->handle != client)
3740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3744 GNUNET_i2s (&peer_msg->peer));
3745 peer_info = peer_info_get (&peer_msg->peer);
3747 tunnel_add_peer (t, peer_info);
3748 peer_info_connect (peer_info, t);
3750 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3756 * Handler for disconnection requests of peers in a tunnel
3758 * @param cls closure
3759 * @param client identification of the client
3760 * @param message the actual message (PeerControl)
3763 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3764 const struct GNUNET_MessageHeader *message)
3766 struct GNUNET_MESH_PeerControl *peer_msg;
3767 struct MeshPeerInfo *peer_info;
3768 struct MeshClient *c;
3769 struct MeshTunnel *t;
3770 MESH_TunnelNumber tid;
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3773 /* Sanity check for client registration */
3774 if (NULL == (c = client_get (client)))
3777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3780 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3781 /* Sanity check for message size */
3782 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3789 /* Tunnel exists? */
3790 tid = ntohl (peer_msg->tunnel_id);
3791 t = tunnel_get_by_local_id (c, tid);
3795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3800 /* Does client own tunnel? */
3801 if (t->client->handle != client)
3804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: for peer %s\n",
3809 GNUNET_i2s (&peer_msg->peer));
3810 /* Is the peer in the tunnel? */
3812 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
3813 if (NULL == peer_info)
3816 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3820 /* Ok, delete peer from tunnel */
3821 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3822 &peer_msg->peer.hashPubKey);
3824 send_destroy_path (t, peer_info->id);
3825 tunnel_delete_peer (t, peer_info->id);
3826 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3832 * Handler for connection requests to new peers by type
3834 * @param cls closure
3835 * @param client identification of the client
3836 * @param message the actual message (ConnectPeerByType)
3839 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3840 const struct GNUNET_MessageHeader *message)
3842 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3843 struct MeshClient *c;
3844 struct MeshTunnel *t;
3845 GNUNET_HashCode hash;
3846 MESH_TunnelNumber tid;
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3849 /* Sanity check for client registration */
3850 if (NULL == (c = client_get (client)))
3853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3857 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3858 /* Sanity check for message size */
3859 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3860 ntohs (connect_msg->header.size))
3863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3867 /* Tunnel exists? */
3868 tid = ntohl (connect_msg->tunnel_id);
3869 t = tunnel_get_by_local_id (c, tid);
3873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3877 /* Does client own tunnel? */
3878 if (t->client->handle != client)
3881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3885 /* Do WE have the service? */
3886 t->type = ntohl (connect_msg->type);
3887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3888 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3889 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3892 /* Yes! Fast forward, add ourselves to the tunnel and send the
3893 * good news to the client
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3896 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3897 peer_info_get (&my_full_id),
3898 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3901 send_client_peer_connected (t, myid);
3902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3903 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3906 /* Ok, lets find a peer offering the service */
3907 if (NULL != t->dht_get_type)
3909 GNUNET_DHT_get_stop (t->dht_get_type);
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3912 GNUNET_h2s (&hash));
3914 GNUNET_DHT_get_start (dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
3915 GNUNET_BLOCK_TYPE_TEST, &hash, 10U,
3916 GNUNET_DHT_RO_RECORD_ROUTE |
3917 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
3918 &dht_get_type_handler, t);
3920 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3926 * Handler for client traffic directed to one peer
3928 * @param cls closure
3929 * @param client identification of the client
3930 * @param message the actual message
3933 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3934 const struct GNUNET_MessageHeader *message)
3936 struct MeshClient *c;
3937 struct MeshTunnel *t;
3938 struct MeshPeerInfo *pi;
3939 struct GNUNET_MESH_Unicast *data_msg;
3940 MESH_TunnelNumber tid;
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3944 "MESH: Got a unicast request from a client!\n");
3946 /* Sanity check for client registration */
3947 if (NULL == (c = client_get (client)))
3950 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3953 data_msg = (struct GNUNET_MESH_Unicast *) message;
3954 /* Sanity check for message size */
3955 size = ntohs (message->size);
3956 if (sizeof (struct GNUNET_MESH_Unicast) +
3957 sizeof (struct GNUNET_MessageHeader) > size)
3960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3964 /* Tunnel exists? */
3965 tid = ntohl (data_msg->tid);
3966 t = tunnel_get_by_local_id (c, tid);
3970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3974 /* Is it a local tunnel? Then, does client own the tunnel? */
3975 if (NULL != t->client && NULL != t->client->handle &&
3976 t->client->handle != client)
3979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3983 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3984 &data_msg->destination.hashPubKey);
3985 /* Is the selected peer in the tunnel? */
3989 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3993 /* Ok, everything is correct, send the message
3994 * (pretend we got it from a mesh peer)
3997 char buf[ntohs (message->size)];
3998 struct GNUNET_MESH_Unicast *copy;
4000 /* Work around const limitation */
4001 copy = (struct GNUNET_MESH_Unicast *) buf;
4002 memcpy (buf, data_msg, size);
4003 copy->oid = my_full_id;
4004 copy->tid = htonl (t->id.tid);
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 "MESH: calling generic handler...\n");
4007 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4009 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4015 * Handler for client traffic directed to the origin
4017 * @param cls closure
4018 * @param client identification of the client
4019 * @param message the actual message
4022 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4023 const struct GNUNET_MessageHeader *message)
4025 struct GNUNET_MESH_ToOrigin *data_msg;
4026 struct GNUNET_PeerIdentity id;
4027 struct MeshClient *c;
4028 struct MeshTunnel *t;
4029 MESH_TunnelNumber tid;
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4033 "MESH: Got a ToOrigin request from a client!\n");
4035 /* Sanity check for client registration */
4036 if (NULL == (c = client_get (client)))
4039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4042 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4043 /* Sanity check for message size */
4044 size = ntohs (message->size);
4045 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4046 sizeof (struct GNUNET_MessageHeader) > size)
4049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4053 /* Tunnel exists? */
4054 tid = ntohl (data_msg->tid);
4055 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4061 t = tunnel_get_by_local_id (c, tid);
4065 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4069 /* It shouldn't be a local tunnel. */
4070 if (NULL != t->client)
4073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4076 GNUNET_PEER_resolve (t->id.oid, &id);
4078 /* Ok, everything is correct, send the message
4079 * (pretend we got it from a mesh peer)
4082 char buf[ntohs (message->size)];
4083 struct GNUNET_MESH_ToOrigin *copy;
4085 /* Work around const limitation */
4086 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4087 memcpy (buf, data_msg, size);
4089 copy->tid = htonl (t->id.tid);
4090 copy->sender = my_full_id;
4091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4092 "MESH: calling generic handler...\n");
4093 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4095 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4101 * Handler for client traffic directed to all peers in a tunnel
4103 * @param cls closure
4104 * @param client identification of the client
4105 * @param message the actual message
4108 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4109 const struct GNUNET_MessageHeader *message)
4111 struct MeshClient *c;
4112 struct MeshTunnel *t;
4113 struct GNUNET_MESH_Multicast *data_msg;
4114 MESH_TunnelNumber tid;
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4117 "MESH: Got a multicast request from a client!\n");
4119 /* Sanity check for client registration */
4120 if (NULL == (c = client_get (client)))
4123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4126 data_msg = (struct GNUNET_MESH_Multicast *) message;
4127 /* Sanity check for message size */
4128 if (sizeof (struct GNUNET_MESH_Multicast) +
4129 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4136 /* Tunnel exists? */
4137 tid = ntohl (data_msg->tid);
4138 t = tunnel_get_by_local_id (c, tid);
4142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4146 /* Does client own tunnel? */
4147 if (t->client->handle != client)
4150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4155 char buf[ntohs (message->size)];
4156 struct GNUNET_MESH_Multicast *copy;
4158 copy = (struct GNUNET_MESH_Multicast *) buf;
4159 memcpy (buf, message, ntohs (message->size));
4160 copy->oid = my_full_id;
4161 copy->tid = htonl (t->id.tid);
4162 copy->ttl = htonl (DEFAULT_TTL);
4163 copy->mid = htonl (t->mid + 1);
4164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4165 "MESH: calling generic handler...\n");
4166 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4169 /* receive done gets called when last copy is sent to a neighbor */
4174 * Functions to handle messages from clients
4176 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4177 {&handle_local_new_client, NULL,
4178 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4179 {&handle_local_tunnel_create, NULL,
4180 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4181 sizeof (struct GNUNET_MESH_TunnelMessage)},
4182 {&handle_local_tunnel_destroy, NULL,
4183 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4184 sizeof (struct GNUNET_MESH_TunnelMessage)},
4185 {&handle_local_connect_add, NULL,
4186 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4187 sizeof (struct GNUNET_MESH_PeerControl)},
4188 {&handle_local_connect_del, NULL,
4189 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4190 sizeof (struct GNUNET_MESH_PeerControl)},
4191 {&handle_local_connect_by_type, NULL,
4192 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4193 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4194 {&handle_local_unicast, NULL,
4195 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4196 {&handle_local_to_origin, NULL,
4197 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4198 {&handle_local_multicast, NULL,
4199 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4205 * To be called on core init/fail.
4207 * @param cls service closure
4208 * @param server handle to the server for this service
4209 * @param identity the public identity of this peer
4212 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4213 const struct GNUNET_PeerIdentity *identity)
4215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4216 core_handle = server;
4217 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4221 GNUNET_SCHEDULER_shutdown ();
4227 * Method called whenever a given peer connects.
4229 * @param cls closure
4230 * @param peer peer identity this notification is about
4231 * @param atsi performance data for the connection
4232 * @param atsi_count number of records in 'atsi'
4235 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4236 const struct GNUNET_ATS_Information *atsi,
4237 unsigned int atsi_count)
4239 struct MeshPeerInfo *peer_info;
4240 struct MeshPeerPath *path;
4242 #if MESH_DEBUG_CONNECTION
4243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4245 GNUNET_i2s (&my_full_id));
4247 peer_info = peer_info_get (peer);
4248 if (myid == peer_info->id)
4250 #if MESH_DEBUG_CONNECTION
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4255 #if MESH_DEBUG_CONNECTION
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", GNUNET_i2s (peer));
4261 path = path_new (2);
4262 path->peers[0] = myid;
4263 path->peers[1] = peer_info->id;
4264 GNUNET_PEER_change_rc (myid, 1);
4265 GNUNET_PEER_change_rc (peer_info->id, 1);
4266 peer_info_add_path (peer_info, path, GNUNET_YES);
4271 * Method called whenever a peer disconnects.
4273 * @param cls closure
4274 * @param peer peer identity this notification is about
4277 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4279 struct MeshPeerInfo *pi;
4282 #if MESH_DEBUG_CONNECTION
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4285 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4291 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4293 /* TODO: notify that the transmission failed */
4294 peer_info_cancel_transmission (pi, i);
4296 peer_info_remove_path (pi, pi->id, myid);
4297 #if MESH_DEBUG_CONNECTION
4300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4307 /******************************************************************************/
4308 /************************ MAIN FUNCTIONS ****************************/
4309 /******************************************************************************/
4312 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4314 * @param cls closure
4315 * @param key current key code
4316 * @param value value in the hash map
4317 * @return GNUNET_YES if we should continue to iterate,
4321 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4323 struct MeshTunnel *t = value;
4330 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4332 * @param cls closure
4333 * @param key current key code
4334 * @param value value in the hash map
4335 * @return GNUNET_YES if we should continue to iterate,
4339 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4341 struct MeshPeerInfo *p = value;
4343 peer_info_destroy (p);
4348 * Task run during shutdown.
4354 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4358 if (core_handle != NULL)
4360 GNUNET_CORE_disconnect (core_handle);
4363 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4364 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4365 if (dht_handle != NULL)
4367 GNUNET_DHT_disconnect (dht_handle);
4372 GNUNET_SERVER_notification_context_destroy (nc);
4375 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4377 GNUNET_SCHEDULER_cancel (announce_id_task);
4378 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4384 * Process mesh requests.
4386 * @param cls closure
4387 * @param server the initialized server
4388 * @param c configuration to use
4391 run (void *cls, struct GNUNET_SERVER_Handle *server,
4392 const struct GNUNET_CONFIGURATION_Handle *c)
4394 struct MeshPeerInfo *peer;
4395 struct MeshPeerPath *p;
4398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4399 server_handle = server;
4400 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4401 CORE_QUEUE_SIZE, /* queue size */
4402 NULL, /* Closure passed to MESH functions */
4403 &core_init, /* Call core_init once connected */
4404 &core_connect, /* Handle connects */
4405 &core_disconnect, /* remove peers on disconnects */
4406 NULL, /* Don't notify about all incoming messages */
4407 GNUNET_NO, /* For header only in notification */
4408 NULL, /* Don't notify about all outbound messages */
4409 GNUNET_NO, /* For header-only out notification */
4410 core_handlers); /* Register these handlers */
4412 if (core_handle == NULL)
4415 GNUNET_SCHEDULER_shutdown ();
4420 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4425 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4426 GNUNET_SCHEDULER_shutdown ();
4429 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4430 GNUNET_free (keyfile);
4431 if (my_private_key == NULL)
4433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4434 _("Mesh service could not access hostkey. Exiting.\n"));
4435 GNUNET_SCHEDULER_shutdown ();
4438 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4439 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4440 &my_full_id.hashPubKey);
4441 myid = GNUNET_PEER_intern (&my_full_id);
4443 // // transport_handle = GNUNET_TRANSPORT_connect(c,
4450 dht_handle = GNUNET_DHT_connect (c, 64);
4451 if (dht_handle == NULL)
4457 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4459 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4460 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4461 peers = GNUNET_CONTAINER_multihashmap_create (32);
4462 applications = GNUNET_CONTAINER_multihashmap_create (32);
4463 types = GNUNET_CONTAINER_multihashmap_create (32);
4465 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4466 nc = GNUNET_SERVER_notification_context_create (server_handle,
4468 GNUNET_SERVER_disconnect_notify (server_handle,
4469 &handle_local_client_disconnect, NULL);
4473 clients_tail = NULL;
4478 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4479 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4481 /* Create a peer_info for the local peer */
4482 peer = peer_info_get (&my_full_id);
4485 GNUNET_PEER_change_rc (myid, 1);
4486 peer_info_add_path (peer, p, GNUNET_YES);
4488 /* Scheduled the task to clean up when shutdown is called */
4489 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4496 * The main function for the mesh service.
4498 * @param argc number of arguments from the command line
4499 * @param argv command line arguments
4500 * @return 0 ok, 1 on error
4503 main (int argc, char *const *argv)
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4510 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");