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
48 #include "mesh_protocol.h"
49 #include "gnunet_dht_service.h"
50 #include "mesh_tunnel_tree.h"
52 /* TODO: move into configuration file */
53 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
54 GNUNET_TIME_UNIT_SECONDS,\
56 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
57 GNUNET_TIME_UNIT_SECONDS,\
60 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
61 GNUNET_TIME_UNIT_SECONDS,\
64 #define GET_RESTART_TIME GNUNET_TIME_relative_multiply(\
65 GNUNET_TIME_UNIT_SECONDS,\
68 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
69 GNUNET_TIME_UNIT_SECONDS,\
72 /******************************************************************************/
73 /************************ DATA STRUCTURES ****************************/
74 /******************************************************************************/
76 /** FWD declaration */
80 * Struct containing all info possibly needed to build a package when called
83 struct MeshDataDescriptor
85 /** ID of the tunnel this packet travels in */
86 struct MESH_TunnelID *origin;
91 /** Client that asked for the transmission, if any */
92 struct GNUNET_SERVER_Client *client;
94 /** Who was this message being sent to */
95 struct MeshPeerInfo *peer;
97 /** Ultimate destination of the packet */
98 GNUNET_PEER_Id destination;
100 /** Number of identical messages sent to different hops (multicast) */
101 unsigned int *copies;
103 /** Which handler was used to request the transmission */
104 unsigned int handler_n;
106 /** Size of the data */
109 /** Used to allow a client send more traffic to the service after a
110 * previous packet was tried to be sent to a neighbor and couldn't */
111 GNUNET_SCHEDULER_TaskIdentifier *timeout_task;
116 * Struct containing all information regarding a given peer
126 * Last time we heard from this peer
128 struct GNUNET_TIME_Absolute last_contact;
131 * Number of attempts to reconnect so far
133 int n_reconnect_attempts;
136 * Paths to reach the peer, ordered by ascending hop count
138 struct MeshPeerPath *path_head;
141 * Paths to reach the peer, ordered by ascending hop count
143 struct MeshPeerPath *path_tail;
146 * Handle to stop the DHT search for a path to this peer
148 struct GNUNET_DHT_GetHandle *dhtget;
151 * Closure given to the DHT GET
153 struct MeshPathInfo *dhtgetcls;
156 * Handles to stop queued transmissions for this peer
158 struct GNUNET_CORE_TransmitHandle *core_transmit[CORE_QUEUE_SIZE];
161 * Pointer to info stuctures used as cls for queued transmissions
163 void *infos[CORE_QUEUE_SIZE];
166 * Type of message being in each transmission
168 uint16_t types[CORE_QUEUE_SIZE];
171 * Array of tunnels this peer participates in
172 * (most probably a small amount, therefore not a hashmap)
173 * When the path to the peer changes, notify these tunnels to let them
174 * re-adjust their path trees.
176 struct MeshTunnel **tunnels;
179 * Number of tunnels this peers participates in
181 unsigned int ntunnels;
186 * Data scheduled to transmit (to local client or remote peer)
193 struct MeshQueue *next;
194 struct MeshQueue *prev;
197 * Target of the data (NULL if target is client)
199 struct MeshPeerInfo *peer;
202 * Client to send the data to (NULL if target is peer)
204 struct MeshClient *client;
207 * Size of the message to transmit
212 * How old is the data?
214 struct GNUNET_TIME_Absolute timestamp;
219 struct GNUNET_MessageHeader *data;
223 * Globally unique tunnel identification (owner + number)
224 * DO NOT USE OVER THE NETWORK
229 * Node that owns the tunnel
234 * Tunnel number to differentiate all the tunnels owned by the node oid
235 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
237 MESH_TunnelNumber tid;
241 struct MeshClient; /* FWD declaration */
244 * Struct containing all information regarding a tunnel
245 * For an intermediate node the improtant info used will be:
246 * - id Tunnel unique identification
247 * - paths[0] To know where to send it next
248 * - metainfo: ready, speeds, accounting
255 struct MESH_TunnelID id;
258 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
260 MESH_TunnelNumber local_tid;
263 * Last time the tunnel was used
265 struct GNUNET_TIME_Absolute timestamp;
268 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
269 * containing peers added by id or by type, not intermediate peers.
271 struct GNUNET_CONTAINER_MultiHashMap *peers;
274 * Number of peers that are connected and potentially ready to receive data
276 unsigned int peers_ready;
279 * Number of peers that have been added to the tunnel
281 unsigned int peers_total;
284 * Client owner of the tunnel, if any
286 struct MeshClient *client;
289 * Messages ready to transmit
291 struct MeshQueue *queue_head;
292 struct MeshQueue *queue_tail;
297 struct MeshTunnelTree *tree;
300 * Application type we are looking for in this tunnel
302 GNUNET_MESH_ApplicationType type;
305 * Used to search peers offering a service
307 struct GNUNET_DHT_GetHandle *dht_get_type;
310 * Task to keep the used paths alive
312 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
315 * Task to destroy the tunnel after timeout
317 * FIXME: merge the two? a tunnel will have either
318 * a path refresh OR a timeout, never both!
320 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
325 * Info needed to work with tunnel paths and peers
332 struct MeshTunnel *t;
335 * Neighbouring peer to whom we send the packet to
337 struct MeshPeerInfo *peer;
342 struct MeshPeerPath *path;
345 * Position in peer's transmit queue
352 * Struct containing information about a client of the service
359 struct MeshClient *next;
360 struct MeshClient *prev;
363 * Tunnels that belong to this client, indexed by local id
365 struct GNUNET_CONTAINER_MultiHashMap *tunnels;
368 * Handle to communicate with the client
370 struct GNUNET_SERVER_Client *handle;
373 * Applications that this client has claimed to provide
375 struct GNUNET_CONTAINER_MultiHashMap *apps;
378 * Messages that this client has declared interest in
380 struct GNUNET_CONTAINER_MultiHashMap *types;
384 * ID of the client, for debug messages
393 /******************************************************************************/
394 /************************ DEBUG FUNCTIONS ****************************/
395 /******************************************************************************/
399 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
400 * @param cls string to print
401 * @param tc task context
404 mesh_debug (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
408 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n", s);
416 /******************************************************************************/
417 /*********************** GLOBAL VARIABLES ****************************/
418 /******************************************************************************/
423 static struct MeshClient *clients;
424 static struct MeshClient *clients_tail;
427 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
429 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
432 * Tunnels incoming, indexed by MESH_TunnelNumber
433 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
435 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
438 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
440 static struct GNUNET_CONTAINER_MultiHashMap *peers;
443 * Handle to communicate with core
445 static struct GNUNET_CORE_Handle *core_handle;
450 static struct GNUNET_DHT_Handle *dht_handle;
455 static struct GNUNET_SERVER_Handle *server_handle;
458 * Notification context, to send messages to local clients
460 static struct GNUNET_SERVER_NotificationContext *nc;
463 * Local peer own ID (memory efficient handle)
465 static GNUNET_PEER_Id myid;
468 * Local peer own ID (full value)
470 static struct GNUNET_PeerIdentity my_full_id;
475 static struct GNUNET_CRYPTO_RsaPrivateKey* my_private_key;
480 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
483 * Tunnel ID for the next created tunnel (global tunnel number)
485 static MESH_TunnelNumber next_tid;
488 * Tunnel ID for the next incoming tunnel (local tunnel number)
490 static MESH_TunnelNumber next_local_tid;
493 * All application types provided by this peer
495 static struct GNUNET_CONTAINER_MultiHashMap *applications;
498 * All message types clients of this peer are interested in
500 static struct GNUNET_CONTAINER_MultiHashMap *types;
503 * Task to periodically announce provided applications
505 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
508 * Task to periodically announce itself in the network
510 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
513 unsigned int next_client_id;
517 /******************************************************************************/
518 /************************ ITERATORS ****************************/
519 /******************************************************************************/
521 /* FIXME move iterators here */
524 /******************************************************************************/
525 /************************ PERIODIC FUNCTIONS ****************************/
526 /******************************************************************************/
529 * Announce iterator over for each application provided by the peer
532 * @param key current key code
533 * @param value value in the hash map
534 * @return GNUNET_YES if we should continue to
539 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
541 /* FIXME are hashes in multihash map equal on all aquitectures? */
542 GNUNET_DHT_put (dht_handle,
545 GNUNET_DHT_RO_RECORD_ROUTE |
546 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
547 GNUNET_BLOCK_TYPE_TEST,
548 sizeof (struct GNUNET_PeerIdentity),
549 (const char *) &my_full_id,
551 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
552 &mesh_debug, "DHT_put for app completed");
554 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
556 APP_ANNOUNCE_TIME, NULL, NULL);
563 * Periodically announce what applications are provided by local clients
566 * @param tc task context
569 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
571 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
573 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Starting PUT for apps\n");
577 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
579 announce_applications_task =
580 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Finished PUT for apps\n");
588 * Periodically announce self id in the DHT
591 * @param tc task context
594 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
596 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
598 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
602 * - Set data expiration in function of X
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
606 GNUNET_i2s (&my_full_id));
607 GNUNET_DHT_put (dht_handle, /* DHT handle */
608 &my_full_id.hashPubKey, /* Key to use */
609 10U, /* Replication level */
610 GNUNET_DHT_RO_RECORD_ROUTE |
611 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
612 GNUNET_BLOCK_TYPE_TEST, /* Block type */
613 sizeof(my_full_id), /* Size of the data */
614 (char *)&my_full_id, /* Data itself */
615 GNUNET_TIME_absolute_get_forever (), /* Data expiration */
616 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
618 &mesh_debug, "DHT_put for id completed");
620 NULL, /* Continuation */
621 NULL); /* Continuation closure */
624 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
629 * Function to process paths received for a new peer addition. The recorded
630 * paths form the initial tunnel, which can be optimized later.
631 * Called on each result obtained for the DHT search.
634 * @param exp when will this value expire
635 * @param key key of the result
636 * @param type type of the result
637 * @param size number of bytes in data
638 * @param data pointer to the result data
641 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
642 const GNUNET_HashCode * key,
643 const struct GNUNET_PeerIdentity *get_path,
644 unsigned int get_path_length,
645 const struct GNUNET_PeerIdentity *put_path,
646 unsigned int put_path_length,
647 enum GNUNET_BLOCK_Type type, size_t size, const void *data);
650 /******************************************************************************/
651 /****************** GENERAL HELPER FUNCTIONS ************************/
652 /******************************************************************************/
655 * Check if client has registered with the service and has not disconnected
657 * @param client the client to check
659 * @return non-NULL if client exists in the global DLL
661 static struct MeshClient *
662 client_get (struct GNUNET_SERVER_Client *client)
664 struct MeshClient *c;
669 if (c->handle == client)
678 * Checks if a given client has subscribed to certain message type
680 * @param message_type Type of message to check
681 * @param c Client to check
683 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
688 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
692 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
693 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
698 * Allow a client to send more data after transmitting a multicast message
699 * which some neighbor has not yet accepted altough a reasonable time has
702 * @param cls Closure (DataDescriptor containing the task identifier)
703 * @param tc Task Context
706 client_allow_send(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
708 struct MeshDataDescriptor *info = cls;
710 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "MESH: CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
717 *(info->timeout_task) = GNUNET_SCHEDULER_NO_TASK;
718 GNUNET_SERVER_receive_done(info->client, GNUNET_OK);
723 * Search for a tunnel by global ID using full PeerIdentities
725 * @param oid owner of the tunnel
726 * @param tid global tunnel number
728 * @return tunnel handler, NULL if doesn't exist
730 static struct MeshTunnel *
731 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
735 * Notify a tunnel that a connection has broken that affects at least
738 * @param t Tunnel affected.
739 * @param p1 Peer that got disconnected from p2.
740 * @param p2 Peer that got disconnected from p1.
742 * @return Short ID of the peer disconnected (either p1 or p2).
743 * 0 if the tunnel remained unaffected.
745 static GNUNET_PEER_Id
746 tunnel_notify_connection_broken (struct MeshTunnel *t,
752 * Send the message to all clients that have subscribed to its type
754 * @param msg Pointer to the message itself
755 * @return number of clients this message was sent to
758 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
759 const struct GNUNET_MessageHeader *payload)
761 struct GNUNET_PeerIdentity *oid;
762 struct MeshClient *c;
763 struct MeshTunnel *t;
764 MESH_TunnelNumber *tid;
767 char cbuf[htons(msg->size)];
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Sending to clients...\n");
770 type = ntohs (payload->type);
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: message of type %u\n", type);
773 memcpy (cbuf, msg, sizeof(cbuf));
774 switch (htons(msg->type))
776 struct GNUNET_MESH_Unicast *uc;
777 struct GNUNET_MESH_Multicast *mc;
778 struct GNUNET_MESH_ToOrigin *to;
780 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
781 uc = (struct GNUNET_MESH_Unicast *) cbuf;
785 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
786 mc = (struct GNUNET_MESH_Multicast *) cbuf;
790 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
791 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
799 t = tunnel_get (oid, ntohl(*tid));
805 *tid = htonl (t->local_tid);
806 for (count = 0, c = clients; c != NULL; c = c->next)
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client %u\n", c->id);
809 if (client_is_subscribed (type, c))
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: sending\n");
813 GNUNET_SERVER_notification_context_unicast (
816 (struct GNUNET_MessageHeader *) cbuf,
825 * Notify the client that owns the tunnel that a peer has connected to it
826 * (the requested path to it has been confirmed).
828 * @param t Tunnel whose owner to notify
829 * @param id Short id of the peer that has connected
832 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
834 struct GNUNET_MESH_PeerControl pc;
836 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
837 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
838 pc.tunnel_id = htonl (t->local_tid);
839 GNUNET_PEER_resolve (id, &pc.peer);
840 GNUNET_SERVER_notification_context_unicast (nc, t->client->handle,
841 &pc.header, GNUNET_NO);
846 * Notify all clients (not depending on registration status) that the incoming
847 * tunnel is no longer valid.
849 * @param t Tunnel that was destroyed.
852 send_clients_tunnel_destroy (struct MeshTunnel *t)
854 struct GNUNET_MESH_TunnelMessage msg;
856 msg.header.size = htons (sizeof (msg));
857 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
858 msg.tunnel_id = htonl (t->local_tid);
859 GNUNET_SERVER_notification_context_broadcast(nc, &msg.header, GNUNET_NO);
864 * Function called to notify a client about the socket
865 * being ready to queue more data. "buf" will be
866 * NULL and "size" zero if the socket was closed for
867 * writing in the meantime.
870 * @param size number of bytes available in buf
871 * @param buf where the callee should write the message
872 * @return number of bytes written to buf
875 send_core_create_path (void *cls, size_t size, void *buf);
879 * Function called to notify a client about the socket
880 * being ready to queue more data. "buf" will be
881 * NULL and "size" zero if the socket was closed for
882 * writing in the meantime.
884 * @param cls closure (data itself)
885 * @param size number of bytes available in buf
886 * @param buf where the callee should write the message
888 * @return number of bytes written to buf
891 send_core_data_multicast (void *cls, size_t size, void *buf);
895 * Decrements the reference counter and frees all resources if needed
897 * @param dd Data Descriptor used in a multicast message
900 data_descriptor_decrement_multicast (struct MeshDataDescriptor *dd)
902 if (0 == --(*(dd->copies)))
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Last copy!\n");
905 if (NULL != dd->client)
907 if (GNUNET_SCHEDULER_NO_TASK != *(dd->timeout_task))
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
910 "MESH: cancelling client timeout (%u)...\n",
911 *(dd->timeout_task));
912 GNUNET_SCHEDULER_cancel(*(dd->timeout_task));
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client...\n");
914 GNUNET_SERVER_receive_done (dd->client, GNUNET_OK);
916 GNUNET_free (dd->timeout_task);
918 GNUNET_free (dd->copies);
919 GNUNET_free (dd->data);
925 * Cancel a core transmission that was already requested and free all resources
926 * associated to the request.
928 * @param peer PeeInfo of the peer whose transmission is cancelled.
929 * @param i Position of the transmission to be cancelled.
932 peer_info_cancel_transmission(struct MeshPeerInfo *peer, unsigned int i)
934 if (NULL != peer->core_transmit[i])
936 struct MeshDataDescriptor *dd;
937 struct MeshPathInfo *path_info;
941 struct GNUNET_PeerIdentity id;
943 GNUNET_PEER_resolve (peer->id, &id);
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "MESH: Cancelling data transmission at %s [%u]\n",
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "MESH: message type %u\n",
953 /* TODO: notify that tranmission has failed */
954 switch (peer->types[i])
956 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
957 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
958 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type payload\n");
961 data_descriptor_decrement_multicast (dd);
963 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type create path\n");
965 path_info = peer->infos[i];
966 path_destroy(path_info->path);
970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type unknown!\n");
972 GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit[i]);
973 peer->core_transmit[i] = NULL;
974 GNUNET_free (peer->infos[i]);
980 * Get a unused CORE slot to transmit a message to a peer. If all the slots
981 * are used, cancel one and return it's position.
983 * @param peer PeerInfo of the neighbor we want to transmit to.
985 * @return The index of an available slot to transmit to the neighbor.
988 peer_info_transmit_slot (struct MeshPeerInfo *peer)
992 for (i = 0; peer->core_transmit[i]; i++)
994 if (i == (CORE_QUEUE_SIZE - 1))
996 /* All positions are taken! Overwriting! */
998 peer_info_cancel_transmission(peer, 0);
1007 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1008 * and insert it in the appropiate structures if the peer is not known yet.
1010 * @param peer Full identity of the peer.
1012 * @return Existing or newly created peer info.
1014 static struct MeshPeerInfo *
1015 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1017 struct MeshPeerInfo *peer_info;
1019 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1020 if (NULL == peer_info)
1023 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1024 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1025 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1026 peer_info->id = GNUNET_PEER_intern (peer);
1034 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1035 * and insert it in the appropiate structures if the peer is not known yet.
1037 * @param peer Short identity of the peer.
1039 * @return Existing or newly created peer info.
1041 static struct MeshPeerInfo *
1042 peer_info_get_short (const GNUNET_PEER_Id peer)
1044 struct GNUNET_PeerIdentity id;
1046 GNUNET_PEER_resolve(peer, &id);
1047 return peer_info_get(&id);
1052 * Iterator to remove the tunnel from the list of tunnels a peer participates
1055 * @param cls Closure (tunnel info)
1056 * @param key GNUNET_PeerIdentity of the peer (unused)
1057 * @param value PeerInfo of the peer
1059 * @return always GNUNET_YES, to keep iterating
1062 peer_info_delete_tunnel (void* cls, const GNUNET_HashCode* key, void* value)
1064 struct MeshTunnel *t = cls;
1065 struct MeshPeerInfo *peer = value;
1068 for (i = 0; i < peer->ntunnels; i++)
1070 if (0 == memcmp(&peer->tunnels[i]->id,
1072 sizeof(struct MESH_TunnelID)))
1075 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1076 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1085 * Core callback to write a
1087 * @param cls Closure (MeshDataDescriptor with data in "data" member).
1088 * @param size Number of bytes available in buf.
1089 * @param buf Where the to write the message.
1091 * @return number of bytes written to buf
1094 send_core_data_raw (void *cls, size_t size, void *buf)
1096 struct MeshDataDescriptor *info = cls;
1097 struct GNUNET_MessageHeader *msg;
1100 GNUNET_assert (NULL != info);
1101 GNUNET_assert (NULL != info->data);
1102 msg = (struct GNUNET_MessageHeader *) info->data;
1103 total_size = ntohs (msg->size);
1105 if (total_size > size)
1107 struct GNUNET_PeerIdentity id;
1109 GNUNET_PEER_resolve (info->peer->id, &id);
1110 info->peer->core_transmit[info->handler_n] =
1111 GNUNET_CORE_notify_transmit_ready(core_handle,
1114 GNUNET_TIME_UNIT_FOREVER_REL,
1117 &send_core_data_raw,
1121 info->peer->core_transmit[info->handler_n] = NULL;
1122 memcpy (buf, msg, total_size);
1123 GNUNET_free (info->data);
1130 * Sends an already built message to a peer, properly registrating
1131 * all used resources.
1133 * @param message Message to send. Fucntion makes a copy of it.
1134 * @param peer Short ID of the neighbor whom to send the message.
1137 send_message (const struct GNUNET_MessageHeader *message,
1138 const struct GNUNET_PeerIdentity *peer)
1140 struct MeshDataDescriptor *info;
1141 struct MeshPeerInfo *neighbor;
1145 size = ntohs (message->size);
1146 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1147 info->data = GNUNET_malloc (size);
1148 memcpy (info->data, message, size);
1149 neighbor = peer_info_get (peer);
1150 i = peer_info_transmit_slot (neighbor);
1151 info->handler_n = i;
1152 info->peer = neighbor;
1153 neighbor->types[i] = GNUNET_MESSAGE_TYPE_MESH_UNICAST;
1154 neighbor->infos[i] = info;
1155 neighbor->core_transmit[i] =
1156 GNUNET_CORE_notify_transmit_ready(core_handle,
1159 GNUNET_TIME_UNIT_FOREVER_REL,
1162 &send_core_data_raw,
1169 * Sends a CREATE PATH message for a path to a peer, properly registrating
1170 * all used resources.
1172 * @param peer PeerInfo of the final peer for whom this path is being created.
1173 * @param p Path itself.
1174 * @param t Tunnel for which the path is created.
1177 send_create_path (struct MeshPeerInfo *peer,
1178 struct MeshPeerPath *p,
1179 struct MeshTunnel *t)
1181 struct GNUNET_PeerIdentity id;
1182 struct MeshPathInfo *path_info;
1183 struct MeshPeerInfo *neighbor;
1188 p = tree_get_path_to_peer(t->tree, peer->id);
1195 for (i = 0; i < p->length; i++)
1197 if (p->peers[i] == myid)
1200 if (i >= p->length - 1)
1206 GNUNET_PEER_resolve(p->peers[i + 1], &id);
1208 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1209 path_info->path = p;
1211 neighbor = peer_info_get(&id);
1212 path_info->peer = neighbor;
1213 path_info->pos = peer_info_transmit_slot(neighbor);
1214 neighbor->types[path_info->pos] = GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE;
1215 neighbor->infos[path_info->pos] = path_info;
1216 neighbor->core_transmit[path_info->pos] =
1217 GNUNET_CORE_notify_transmit_ready (
1218 core_handle, /* handle */
1221 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1223 sizeof (struct GNUNET_MESH_ManipulatePath)
1224 + (p->length * sizeof (struct GNUNET_PeerIdentity)), /*size */
1225 &send_core_create_path, /* callback */
1226 path_info); /* cls */
1231 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1233 * @param t Tunnel whose path to destroy.
1234 * @param destination Short ID of the peer to whom the path to destroy.
1237 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1239 struct MeshPeerPath *p;
1242 p = tree_get_path_to_peer(t->tree, destination);
1248 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1249 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1251 struct GNUNET_MESH_ManipulatePath *msg;
1252 struct GNUNET_PeerIdentity *pi;
1256 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1257 msg->header.size = htons (size);
1258 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1259 msg->tid = htonl (t->id.tid);
1260 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1261 for (i = 0; i < p->length; i++)
1263 GNUNET_PEER_resolve(p->peers[i], &pi[i]);
1265 send_message (&msg->header, path_get_first_hop(t->tree, destination));
1272 * Try to establish a new connection to this peer.
1273 * Use the best path for the given tunnel.
1274 * If the peer doesn't have any path to it yet, try to get one.
1275 * If the peer already has some path, send a CREATE PATH towards it.
1277 * @param peer PeerInfo of the peer.
1278 * @param t Tunnel for which to create the path, if possible.
1281 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1283 struct MeshPeerPath *p;
1284 struct MeshPathInfo *path_info;
1286 if (NULL != peer->path_head)
1288 p = tree_get_path_to_peer(t->tree, peer->id);
1297 send_create_path(peer, p, t);
1302 send_client_peer_connected(t, myid);
1305 else if (NULL == peer->dhtget)
1307 struct GNUNET_PeerIdentity id;
1309 GNUNET_PEER_resolve(peer->id, &id);
1310 path_info = GNUNET_malloc(sizeof(struct MeshPathInfo));
1311 path_info->peer = peer;
1313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314 "MESH: Starting DHT GET for peer %s\n",
1316 peer->dhtgetcls = path_info;
1318 GNUNET_DHT_get_start(dht_handle, /* handle */
1319 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
1320 GNUNET_BLOCK_TYPE_TEST, /* type */
1321 &id.hashPubKey, /* key to search */
1322 4, /* replication level */
1323 GNUNET_DHT_RO_RECORD_ROUTE |
1324 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1326 0, /* xquery bits */
1327 &dht_get_id_handler,
1330 /* Otherwise, there is no path but the DHT get is already started. */
1335 * Task to delay the connection of a peer
1337 * @param cls Closure (path info with tunnel and peer to connect).
1338 * Will be free'd on exection.
1339 * @param tc TaskContext
1342 peer_info_connect_task (void *cls,
1343 const struct GNUNET_SCHEDULER_TaskContext *tc)
1345 struct MeshPathInfo *path_info = cls;
1347 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1352 peer_info_connect (path_info->peer, path_info->t);
1358 * Destroy the peer_info and free any allocated resources linked to it
1360 * @param pi The peer_info to destroy.
1362 * @return GNUNET_OK on success
1365 peer_info_destroy (struct MeshPeerInfo *pi)
1367 struct GNUNET_PeerIdentity id;
1368 struct MeshPeerPath *p;
1369 struct MeshPeerPath *nextp;
1372 GNUNET_PEER_resolve (pi->id, &id);
1373 GNUNET_PEER_change_rc (pi->id, -1);
1376 GNUNET_CONTAINER_multihashmap_remove (peers,&id.hashPubKey, pi))
1379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1380 "MESH: removing peer %s, not in hashmap\n",
1383 if (NULL != pi->dhtget)
1385 GNUNET_DHT_get_stop(pi->dhtget);
1386 GNUNET_free (pi->dhtgetcls);
1388 for (i = 0; i < CORE_QUEUE_SIZE; i++)
1390 peer_info_cancel_transmission(pi, i);
1396 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1406 * Remove all paths that rely on a direct connection between p1 and p2
1407 * from the peer itself and notify all tunnels about it.
1409 * @param peer PeerInfo of affected peer.
1410 * @param p1 GNUNET_PEER_Id of one peer.
1411 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1414 * TODO: optimize (see below)
1417 path_remove_from_peer (struct MeshPeerInfo *peer,
1421 struct MeshPeerPath *p;
1422 struct MeshPeerPath *aux;
1423 struct MeshPeerInfo *peer_d;
1425 unsigned int destroyed;
1431 p = peer->path_head;
1435 for (i = 0; i < (p->length - 1); i++)
1437 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1438 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1440 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1451 for (i = 0; i < peer->ntunnels; i++)
1453 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1455 * Problem: one or more peers have been deleted from the tunnel tree.
1456 * We don't know who they are to try to add them again.
1457 * We need to try to find a new path for each of the disconnected peers.
1458 * Some of them might already have a path to reach them that does not
1459 * involve p1 and p2. Adding all anew might render in a better tree than
1460 * the trivial immediate fix.
1462 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1463 * its children will be reachable trough him.
1465 peer_d = peer_info_get_short(d);
1468 for (p = peer_d->path_head; NULL != p; p = p->next)
1470 if ((cost = path_get_cost(peer->tunnels[i]->tree, p)) < best)
1478 /* No callback, as peer will be already disconnected */
1479 tree_add_path(peer->tunnels[i]->tree, aux, NULL);
1483 peer_info_connect (peer_d, peer->tunnels[i]);
1490 * Add the path to the peer and update the path used to reach it in case this
1493 * @param peer_info Destination peer to add the path to.
1494 * @param path New path to add. Last peer must be the peer in arg 1.
1495 * Path will be either used of freed if already known.
1497 * TODO: trim the part from origin to us? Add it as path to origin?
1500 path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1502 struct MeshPeerPath *aux;
1507 struct GNUNET_PeerIdentity id;
1509 GNUNET_PEER_resolve (peer_info->id, &id);
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "MESH: adding path [%u] to peer %s\n",
1515 if (NULL == peer_info || NULL == path)
1518 path_destroy (path);
1522 l = path_get_length (path);
1529 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1531 l2 = path_get_length (aux);
1534 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1535 peer_info->path_tail,
1542 if (l2 == l && memcmp(path->peers, aux->peers, l) == 0)
1549 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head,
1550 peer_info->path_tail,
1557 * Add the path to the origin peer and update the path used to reach it in case
1558 * this is the shortest.
1559 * The path is given in peer_info -> destination, therefore we turn the path
1560 * upside down first.
1562 * @param peer_info Peer to add the path to, being the origin of the path.
1563 * @param path New path to add after being inversed.
1566 path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
1569 path_add_to_peer (peer_info, path);
1574 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1575 * to obtain a local peer -> destination path and interning the peer ids.
1577 * @return Newly allocated and created path
1579 static struct MeshPeerPath *
1580 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1581 unsigned int get_path_length,
1582 const struct GNUNET_PeerIdentity *put_path,
1583 unsigned int put_path_length)
1585 struct MeshPeerPath *p;
1591 GNUNET_PEER_change_rc(myid, 1);
1592 i = get_path_length;
1593 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: GET has %d hops.\n", i);
1594 for (i--; i >= 0; i--)
1596 id = GNUNET_PEER_intern (&get_path[i]);
1597 if (p->length > 0 && id == p->peers[p->length - 1])
1599 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1600 GNUNET_PEER_change_rc(id, -1);
1604 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1605 "MESH: Adding from GET: %s.\n",
1606 GNUNET_i2s(&get_path[i]));
1608 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1609 p->peers[p->length - 1] = id;
1612 i = put_path_length;
1613 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: PUT has %d hops.\n", i);
1614 for (i--; i >= 0; i--)
1616 id = GNUNET_PEER_intern (&put_path[i]);
1619 /* PUT path went through us, so discard the path up until now and start
1620 * from here to get a much shorter (and loop-free) path.
1625 if (p->length > 0 && id == p->peers[p->length - 1])
1627 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Optimizing 1 hop out.\n");
1628 GNUNET_PEER_change_rc(id, -1);
1632 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1633 "MESH: Adding from PUT: %s.\n",
1634 GNUNET_i2s(&put_path[i]));
1636 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1637 p->peers[p->length - 1] = id;
1641 if (get_path_length > 0)
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1643 "MESH: (first of GET: %s)\n",
1644 GNUNET_i2s(&get_path[0]));
1645 if (put_path_length > 0)
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1647 "MESH: (first of PUT: %s)\n",
1648 GNUNET_i2s(&put_path[0]));
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1650 "MESH: In total: %d hops\n",
1652 for (i = 0; i < p->length; i++)
1654 struct GNUNET_PeerIdentity peer_id;
1656 GNUNET_PEER_resolve(p->peers[i], &peer_id);
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1660 GNUNET_i2s(&peer_id));
1668 * Send keepalive packets for a peer
1670 * @param cls Closure (tunnel for which to send the keepalive).
1671 * @param tc Notification context.
1673 * TODO: implement explicit multicast keepalive?
1676 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1680 * Search for a tunnel among the tunnels for a client
1682 * @param c the client whose tunnels to search in
1683 * @param tid the local id of the tunnel
1685 * @return tunnel handler, NULL if doesn't exist
1687 static struct MeshTunnel *
1688 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1690 GNUNET_HashCode hash;
1692 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1693 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1695 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1697 return GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
1702 * Search for a tunnel by global ID using PEER_ID
1704 * @param pi owner of the tunnel
1705 * @param tid global tunnel number
1707 * @return tunnel handler, NULL if doesn't exist
1709 static struct MeshTunnel *
1710 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1712 struct MESH_TunnelID id;
1713 GNUNET_HashCode hash;
1718 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1719 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1724 * Search for a tunnel by global ID using full PeerIdentities
1726 * @param oid owner of the tunnel
1727 * @param tid global tunnel number
1729 * @return tunnel handler, NULL if doesn't exist
1731 static struct MeshTunnel *
1732 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1734 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1739 * Callback used to notify a client owner of a tunnel that a peer has
1740 * disconnected, most likely because of a path change.
1742 * @param n Node in the tree representing the disconnected peer
1744 * FIXME: pass tunnel via cls, make param just a peer identity
1747 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
1749 struct MeshPeerInfo *peer;
1750 struct MeshPathInfo *path_info;
1752 if (NULL != n->t->client && NULL != nc)
1754 struct GNUNET_MESH_PeerControl msg;
1755 msg.header.size = htons (sizeof (msg));
1756 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1757 msg.tunnel_id = htonl (n->t->local_tid);
1758 GNUNET_PEER_resolve (n->peer, &msg.peer);
1759 GNUNET_SERVER_notification_context_unicast (nc, n->t->client->handle,
1760 &msg.header, GNUNET_NO);
1762 peer = peer_info_get_short(n->peer);
1763 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1764 path_info->peer = peer;
1765 path_info->t = n->t;
1766 GNUNET_SCHEDULER_add_now(&peer_info_connect_task, path_info);
1771 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1772 * needed rescources.
1773 * If peer already exists, reevaluate shortest path and change if different.
1775 * @param t Tunnel we want to add a new peer to
1776 * @param peer PeerInfo of the peer being added
1780 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
1782 struct GNUNET_PeerIdentity id;
1783 struct MeshPeerPath *best_p;
1784 struct MeshPeerPath *p;
1785 unsigned int best_cost;
1788 GNUNET_PEER_resolve(peer->id, &id);
1790 GNUNET_CONTAINER_multihashmap_contains(t->peers, &id.hashPubKey))
1793 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
1794 GNUNET_CONTAINER_multihashmap_put(
1798 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1801 if (NULL != (p = peer->path_head))
1804 best_cost = path_get_cost(t->tree, p);
1807 if ((cost = path_get_cost (t->tree, p)) < best_cost)
1814 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected);
1815 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
1816 t->path_refresh_task =
1817 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
1821 /* Start a DHT get if necessary */
1822 peer_info_connect(peer, t);
1827 * Add a path to a tunnel which we don't own, just to remember the next hop.
1828 * If destination node was already in the tunnel, the first hop information
1829 * will be replaced with the new path.
1831 * @param t Tunnel we want to add a new peer to
1832 * @param p Path to add
1833 * @param own_pos Position of local node in path.
1837 tunnel_add_path (struct MeshTunnel *t,
1838 struct MeshPeerPath *p,
1839 unsigned int own_pos)
1841 struct GNUNET_PeerIdentity id;
1843 GNUNET_assert (0 != own_pos);
1844 tree_add_path(t->tree, p, NULL);
1845 if (NULL == t->tree->me)
1846 t->tree->me = tree_find_peer(t->tree->root, p->peers[own_pos]);
1847 if (own_pos < p->length - 1)
1849 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
1850 tree_update_first_hops(t->tree, t->tree->me, &id);
1856 * Notifies a tunnel that a connection has broken that affects at least
1857 * some of its peers. Sends a notification towards the root of the tree.
1858 * In case the peer is the owner of the tree, notifies the client that owns
1859 * the tunnel and tries to reconnect.
1861 * @param t Tunnel affected.
1862 * @param p1 Peer that got disconnected from p2.
1863 * @param p2 Peer that got disconnected from p1.
1865 * @return Short ID of the peer disconnected (either p1 or p2).
1866 * 0 if the tunnel remained unaffected.
1868 static GNUNET_PEER_Id
1869 tunnel_notify_connection_broken (struct MeshTunnel *t,
1875 pid = tree_notify_connection_broken (t->tree,
1878 ¬ify_peer_disconnected);
1879 if (myid != p1 && myid != p2)
1885 if (NULL != t->tree->me->parent)
1887 /* We are the peer still connected, notify owner of the disconnection. */
1888 struct GNUNET_MESH_PathBroken msg;
1889 struct GNUNET_PeerIdentity neighbor;
1891 msg.header.size = htons (sizeof (msg));
1892 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
1893 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
1894 msg.tid = htonl (t->id.tid);
1895 msg.peer1 = my_full_id;
1896 GNUNET_PEER_resolve (pid, &msg.peer2);
1897 GNUNET_PEER_resolve (t->tree->me->parent->peer, &neighbor);
1898 send_message (&msg.header, &neighbor);
1906 * Send a message in a tunnel in multicast, sending a copy to each child node
1907 * down the local one in the tunnel tree.
1909 * @param t Tunnel in which to send the data.
1910 * @param msg Message to be sent
1912 * @return Number of copies sent.
1914 * TODO: unifiy shared resources and reference counter management
1917 tunnel_send_multicast (struct MeshTunnel *t,
1918 const struct GNUNET_MessageHeader *msg)
1920 struct GNUNET_PeerIdentity neighbor;
1921 struct MeshDataDescriptor *info;
1922 struct MeshTunnelTreeNode *n;
1923 struct MeshTunnelTreeNode *counter;
1924 GNUNET_SCHEDULER_TaskIdentifier *task;
1925 unsigned int *copies;
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 "MESH: sending a multicast packet...\n");
1932 size = ntohs (msg->size);
1933 GNUNET_assert (NULL != t->tree->me);
1934 n = t->tree->me->children_head;
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1938 "MESH: no children in the tree, no one to send.\n");
1941 copies = GNUNET_malloc (sizeof (unsigned int));
1942 for (counter = n; NULL != counter; counter = counter->next)
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (%u copies)\n", *copies);
1945 data = GNUNET_malloc (size);
1946 memcpy (data, msg, size);
1947 if (NULL != t->client)
1949 task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
1950 *task = GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT,
1955 task = NULL; // So GCC shuts up about task being potentially uninitialized
1958 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
1961 info->copies = copies;
1962 if (NULL != t->client)
1964 info->client = t->client->handle;
1965 info->timeout_task = task;
1967 info->destination = n->peer;
1968 GNUNET_PEER_resolve (n->peer, &neighbor);
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1970 "MESH: sending to %s...\n",
1971 GNUNET_i2s (&neighbor));
1972 info->peer = peer_info_get(&neighbor);
1973 GNUNET_assert (NULL != info->peer);
1974 i = peer_info_transmit_slot(info->peer);
1975 info->handler_n = i;
1976 info->peer->infos[i] = info;
1977 info->peer->types[i] = GNUNET_MESSAGE_TYPE_MESH_MULTICAST;
1978 info->peer->core_transmit[i] =
1979 GNUNET_CORE_notify_transmit_ready (core_handle,
1982 GNUNET_TIME_UNIT_FOREVER_REL,
1985 &send_core_data_multicast, info);
1993 * Send a message to all peers in this tunnel that the tunnel is no longer
1996 * @param t The tunnel whose peers to notify.
1999 tunnel_send_destroy (struct MeshTunnel *t)
2001 struct GNUNET_MESH_TunnelDestroy msg;
2003 msg.header.size = htons (sizeof (msg));
2004 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2005 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2006 msg.tid = htonl (t->id.tid);
2007 tunnel_send_multicast (t, &msg.header);
2013 * Destroy the tunnel and free any allocated resources linked to it
2015 * @param t the tunnel to destroy
2017 * @return GNUNET_OK on success
2020 tunnel_destroy (struct MeshTunnel *t)
2022 struct MeshClient *c;
2023 struct MeshQueue *q;
2024 struct MeshQueue *qn;
2025 GNUNET_HashCode hash;
2034 struct GNUNET_PeerIdentity id;
2036 GNUNET_PEER_resolve(t->id.oid, &id);
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 "MESH: destroying tunnel %s [%x]\n",
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
2046 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2047 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2052 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2054 GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t))
2058 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2060 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2061 GNUNET_break (GNUNET_YES ==
2062 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t));
2065 if (NULL != t->peers)
2067 GNUNET_CONTAINER_multihashmap_iterate(t->peers,
2068 &peer_info_delete_tunnel, t);
2069 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2074 if (NULL != q->data)
2075 GNUNET_free (q->data);
2079 /* TODO cancel core transmit ready in case it was active */
2081 tree_destroy(t->tree);
2082 if (NULL != t->dht_get_type)
2083 GNUNET_DHT_get_stop(t->dht_get_type);
2084 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2085 GNUNET_SCHEDULER_cancel(t->timeout_task);
2086 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2087 GNUNET_SCHEDULER_cancel(t->path_refresh_task);
2094 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2095 * that are no longer needed, as well as nodes of no longer reachable peers.
2096 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2098 * @param t Tunnel from which to remove the path.
2099 * @param p Peer which should be removed.
2102 tunnel_delete_peer (struct MeshTunnel *t,
2103 GNUNET_PEER_Id peer)
2105 GNUNET_break (GNUNET_OK == tree_del_peer (t->tree, peer, NULL));
2106 if (NULL == t->tree->root)
2112 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2113 * client when the client disconnects.
2115 * @param cls closure (client that is disconnecting)
2116 * @param key the hash of the local tunnel id (used to access the hashmap)
2117 * @param value the value stored at the key (tunnel to destroy)
2119 * @return GNUNET_OK on success
2122 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2124 struct MeshTunnel *t = value;
2127 if (NULL != t->dht_get_type)
2129 GNUNET_DHT_get_stop (t->dht_get_type);
2131 r = tunnel_destroy (t);
2137 * Timeout function, destroys tunnel if called
2139 * @param cls Closure (tunnel to destroy).
2140 * @param tc TaskContext
2143 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2145 struct MeshTunnel *t = cls;
2147 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2149 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2154 * Resets the tunnel timeout. Starts it if no timeout was running.
2156 * @param t Tunnel whose timeout to reset.
2159 tunnel_reset_timeout (struct MeshTunnel *t)
2161 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2162 GNUNET_SCHEDULER_cancel (t->timeout_task);
2163 t->timeout_task = GNUNET_SCHEDULER_add_delayed (
2164 GNUNET_TIME_relative_multiply(REFRESH_PATH_TIME, 4),
2170 /******************************************************************************/
2171 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2172 /******************************************************************************/
2175 * Function called to notify a client about the socket
2176 * being ready to queue more data. "buf" will be
2177 * NULL and "size" zero if the socket was closed for
2178 * writing in the meantime.
2180 * @param cls closure
2181 * @param size number of bytes available in buf
2182 * @param buf where the callee should write the message
2183 * @return number of bytes written to buf
2186 send_core_create_path (void *cls, size_t size, void *buf)
2188 struct MeshPathInfo *info = cls;
2189 struct GNUNET_MESH_ManipulatePath *msg;
2190 struct GNUNET_PeerIdentity *peer_ptr;
2191 struct MeshPeerInfo *peer = info->peer;
2192 struct MeshTunnel *t = info->t;
2193 struct MeshPeerPath *p = info->path;
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "MESH: CREATE PATH sending...\n");
2200 sizeof (struct GNUNET_MESH_ManipulatePath) +
2201 p->length * sizeof (struct GNUNET_PeerIdentity);
2203 if (size < size_needed || NULL == buf)
2205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: create path retransmit!\n");
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: buf: %p\n", buf);
2207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: size: (%u/%u)\n",
2209 info->peer->core_transmit[info->pos] =
2210 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 0,
2211 GNUNET_TIME_UNIT_FOREVER_REL,
2212 path_get_first_hop (t->tree, peer->id),
2214 &send_core_create_path,
2218 info->peer->core_transmit[info->pos] = NULL;
2221 struct GNUNET_PeerIdentity id;
2223 GNUNET_PEER_resolve (peer->id, &id);
2224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2225 "MESH: setting core_transmit %s [%u] to NULL\n",
2230 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2231 msg->header.size = htons (size_needed);
2232 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2233 msg->tid = ntohl (t->id.tid);
2235 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2236 for (i = 0; i < p->length; i++)
2238 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2245 "MESH: CREATE PATH (%u bytes long) sent!\n",
2252 * Function called to notify a client about the socket
2253 * being ready to queue more data. "buf" will be
2254 * NULL and "size" zero if the socket was closed for
2255 * writing in the meantime.
2257 * @param cls closure (data itself)
2258 * @param size number of bytes available in buf
2259 * @param buf where the callee should write the message
2261 * @return number of bytes written to buf
2264 send_core_data_multicast (void *cls, size_t size, void *buf)
2266 struct MeshDataDescriptor *info = cls;
2269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Multicast callback.\n");
2270 GNUNET_assert (NULL != info);
2271 GNUNET_assert (NULL != info->peer);
2272 total_size = info->size;
2273 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2275 if (total_size > size)
2278 struct GNUNET_PeerIdentity id;
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2281 "MESH: Multicast: retransmitting... (%u/%u)\n",
2283 GNUNET_PEER_resolve(info->peer->id, &id);
2284 info->peer->core_transmit[info->handler_n] =
2285 GNUNET_CORE_notify_transmit_ready (core_handle,
2288 GNUNET_TIME_UNIT_FOREVER_REL,
2291 &send_core_data_multicast,
2295 info->peer->core_transmit[info->handler_n] = NULL;
2296 info->peer->infos[info->handler_n] = NULL;
2297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: copying data...\n");
2298 memcpy (buf, info->data, total_size);
2301 struct GNUNET_MESH_Multicast *mc;
2302 struct GNUNET_MessageHeader *mh;
2305 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2307 mc = (struct GNUNET_MESH_Multicast *) mh;
2308 mh = (struct GNUNET_MessageHeader *) &mc[1];
2309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2310 "MESH: multicast, payload type %u\n",
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2313 "MESH: multicast, payload size %u\n",
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 data_descriptor_decrement_multicast (info);
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: freeing info...\n");
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: return %u\n", total_size);
2333 * Function called to notify a client about the socket
2334 * being ready to queue more data. "buf" will be
2335 * NULL and "size" zero if the socket was closed for
2336 * writing in the meantime.
2338 * @param cls closure (MeshDataDescriptor)
2339 * @param size number of bytes available in buf
2340 * @param buf where the callee should write the message
2341 * @return number of bytes written to buf
2344 send_core_path_ack (void *cls, size_t size, void *buf)
2346 struct MeshDataDescriptor *info = cls;
2347 struct GNUNET_MESH_PathACK *msg = buf;
2349 GNUNET_assert (NULL != info);
2352 info->peer->core_transmit[info->handler_n] = NULL;
2354 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2359 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2360 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2361 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2362 msg->tid = htonl (info->origin->tid);
2363 msg->peer_id = my_full_id;
2365 /* TODO add signature */
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: PATH ACK sent!\n");
2368 return sizeof (struct GNUNET_MESH_PathACK);
2372 /******************************************************************************/
2373 /******************** MESH NETWORK HANDLERS **************************/
2374 /******************************************************************************/
2378 * Core handler for path creation
2380 * @param cls closure
2381 * @param message message
2382 * @param peer peer identity this notification is about
2383 * @param atsi performance data
2384 * @param atsi_count number of records in 'atsi'
2386 * @return GNUNET_OK to keep the connection open,
2387 * GNUNET_SYSERR to close it (signal serious error)
2390 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2391 const struct GNUNET_MessageHeader *message,
2392 const struct GNUNET_ATS_Information *atsi,
2393 unsigned int atsi_count)
2395 unsigned int own_pos;
2398 MESH_TunnelNumber tid;
2399 struct GNUNET_MESH_ManipulatePath *msg;
2400 struct GNUNET_PeerIdentity *pi;
2401 GNUNET_HashCode hash;
2402 struct MeshPeerPath *path;
2403 struct MeshPeerInfo *dest_peer_info;
2404 struct MeshPeerInfo *orig_peer_info;
2405 struct MeshTunnel *t;
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 "MESH: Received a path create msg [%s]\n",
2409 GNUNET_i2s(&my_full_id));
2410 size = ntohs (message->size);
2411 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2413 GNUNET_break_op (0);
2417 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2418 if (size % sizeof (struct GNUNET_PeerIdentity))
2420 GNUNET_break_op (0);
2423 size /= sizeof (struct GNUNET_PeerIdentity);
2426 GNUNET_break_op (0);
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2430 "MESH: path has %u hops.\n",
2432 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2434 tid = ntohl (msg->tid);
2435 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2436 t = tunnel_get (pi, tid);
2437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2438 "MESH: path is for tunnel %s [%X].\n",
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating tunnel\n");
2444 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2445 t->id.oid = GNUNET_PEER_intern (pi);
2447 t->local_tid = next_local_tid++;
2448 /* FIXME test if taken */
2449 next_local_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2450 t->tree = tree_new(t, t->id.oid);
2452 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2454 GNUNET_CONTAINER_multihashmap_put (
2458 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2464 tunnel_reset_timeout (t);
2465 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2467 GNUNET_CONTAINER_multihashmap_put (
2471 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2479 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2480 if (NULL == dest_peer_info)
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 "MESH: Creating PeerInfo for destination.\n");
2484 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2485 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2486 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2488 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2490 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2491 if (NULL == orig_peer_info)
2493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 "MESH: Creating PeerInfo for origin.\n");
2495 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2496 orig_peer_info->id = GNUNET_PEER_intern (pi);
2497 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2498 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2501 path = path_new (size);
2503 for (i = 0; i < size; i++)
2505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2506 "MESH: ... adding %s\n",
2507 GNUNET_i2s(&pi[i]));
2508 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2509 if (path->peers[i] == myid)
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2513 "MESH: Own position: %u\n", own_pos);
2516 /* cannot be self, must be 'not found' */
2517 /* create path: self not found in path through self */
2518 GNUNET_break_op (0);
2519 path_destroy (path);
2520 /* FIXME error. destroy tunnel? leave for timeout? */
2523 tunnel_add_path (t, path, own_pos);
2524 if (own_pos == size - 1)
2526 /* It is for us! Send ack. */
2527 struct GNUNET_MESH_TunnelNotification cmsg;
2528 struct MeshDataDescriptor *info;
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 "MESH: It's for us!\n");
2533 path_add_to_origin (orig_peer_info, path);
2534 if (NULL == t->peers)
2535 t->peers = GNUNET_CONTAINER_multihashmap_create(4);
2536 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2538 &my_full_id.hashPubKey,
2539 peer_info_get(&my_full_id),
2540 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2541 info = GNUNET_malloc (sizeof (struct MeshDataDescriptor));
2542 info->origin = &t->id;
2543 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2544 GNUNET_assert (NULL != info->peer);
2545 j = peer_info_transmit_slot(info->peer);
2546 info->handler_n = j;
2547 info->peer->types[j] = GNUNET_MESSAGE_TYPE_MESH_PATH_ACK;
2548 info->peer->infos[j] = info;
2549 info->peer->core_transmit[j] =
2550 GNUNET_CORE_notify_transmit_ready (core_handle, 0, 100,
2551 GNUNET_TIME_UNIT_FOREVER_REL, peer,
2552 sizeof (struct GNUNET_MESH_PathACK),
2553 &send_core_path_ack, info);
2554 cmsg.header.size = htons(sizeof(cmsg));
2555 cmsg.header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2556 GNUNET_PEER_resolve(t->id.oid, &cmsg.peer);
2557 cmsg.tunnel_id = htonl(t->local_tid);
2558 GNUNET_SERVER_notification_context_broadcast(nc, &cmsg.header, GNUNET_NO);
2562 struct MeshPeerPath *path2;
2564 /* It's for somebody else! Retransmit. */
2565 path2 = path_duplicate(path);
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2567 "MESH: Retransmitting.\n");
2568 path_add_to_peer(dest_peer_info, path);
2569 path = path_duplicate(path2);
2570 path_add_to_origin(orig_peer_info, path2);
2571 send_create_path(dest_peer_info, path, t);
2578 * Core handler for path destruction
2580 * @param cls closure
2581 * @param message message
2582 * @param peer peer identity this notification is about
2583 * @param atsi performance data
2584 * @param atsi_count number of records in 'atsi'
2586 * @return GNUNET_OK to keep the connection open,
2587 * GNUNET_SYSERR to close it (signal serious error)
2590 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2591 const struct GNUNET_MessageHeader *message,
2592 const struct GNUNET_ATS_Information *atsi,
2593 unsigned int atsi_count)
2595 struct GNUNET_MESH_ManipulatePath *msg;
2596 struct GNUNET_PeerIdentity *pi;
2597 struct MeshPeerPath *path;
2598 struct MeshTunnel *t;
2599 unsigned int own_pos;
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2604 "MESH: Received a PATH DESTROY msg from %s\n",
2606 size = ntohs (message->size);
2607 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2609 GNUNET_break_op (0);
2613 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2614 if (size % sizeof (struct GNUNET_PeerIdentity))
2616 GNUNET_break_op (0);
2619 size /= sizeof (struct GNUNET_PeerIdentity);
2622 GNUNET_break_op (0);
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 "MESH: path has %u hops.\n",
2629 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2630 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 "MESH: path is for tunnel %s [%X].\n",
2635 t = tunnel_get (pi, ntohl (msg->tid));
2638 /* TODO notify back: we don't know this tunnel */
2639 GNUNET_break_op (0);
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Creating path...\n");
2643 path = path_new (size);
2645 for (i = 0; i < size; i++)
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 "MESH: ... adding %s\n",
2649 GNUNET_i2s(&pi[i]));
2650 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2651 if (path->peers[i] == myid)
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2655 "MESH: Own position: %u\n", own_pos);
2656 if (own_pos < path->length - 1)
2657 send_message (message, &pi[own_pos + 1]);
2658 tunnel_delete_peer (t, path->peers[path->length - 1]);
2659 path_destroy (path);
2665 * Core handler for notifications of broken paths
2667 * @param cls closure
2668 * @param message message
2669 * @param peer peer identity this notification is about
2670 * @param atsi performance data
2671 * @param atsi_count number of records in 'atsi'
2673 * @return GNUNET_OK to keep the connection open,
2674 * GNUNET_SYSERR to close it (signal serious error)
2677 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
2678 const struct GNUNET_MessageHeader *message,
2679 const struct GNUNET_ATS_Information *atsi,
2680 unsigned int atsi_count)
2682 struct GNUNET_MESH_PathBroken *msg;
2683 struct MeshTunnel *t;
2685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2686 "MESH: Received a PATH BROKEN msg from %s\n",
2688 msg = (struct GNUNET_MESH_PathBroken *) message;
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2690 "MESH: regarding %s\n",
2691 GNUNET_i2s(&msg->peer1));
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2693 "MESH: regarding %s\n",
2694 GNUNET_i2s(&msg->peer2));
2695 t = tunnel_get(&msg->oid, ntohl (msg->tid));
2698 GNUNET_break_op (0);
2701 tunnel_notify_connection_broken(t,
2702 GNUNET_PEER_search(&msg->peer1),
2703 GNUNET_PEER_search(&msg->peer2));
2710 * Core handler for tunnel destruction
2712 * @param cls closure
2713 * @param message message
2714 * @param peer peer identity this notification is about
2715 * @param atsi performance data
2716 * @param atsi_count number of records in 'atsi'
2718 * @return GNUNET_OK to keep the connection open,
2719 * GNUNET_SYSERR to close it (signal serious error)
2722 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
2723 const struct GNUNET_MessageHeader *message,
2724 const struct GNUNET_ATS_Information *atsi,
2725 unsigned int atsi_count)
2727 struct GNUNET_MESH_TunnelDestroy *msg;
2728 struct MeshTunnel *t;
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2731 "MESH: Got a TUNNEL DESTROY packet from %s\n",
2733 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2735 "MESH: for tunnel %s [%u]\n",
2736 GNUNET_i2s (&msg->oid),
2738 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2741 /* Probably already got the message from another path,
2742 * destroyed the tunnel and retransmitted to children.
2747 if (t->id.oid == myid)
2749 GNUNET_break_op (0);
2752 if (t->local_tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2754 /* Tunnel was incoming, notify clients */
2755 send_clients_tunnel_destroy (t);
2757 tunnel_send_destroy (t);
2764 * Core handler for mesh network traffic going from the origin to a peer
2766 * @param cls closure
2767 * @param peer peer identity this notification is about
2768 * @param message message
2769 * @param atsi performance data
2770 * @param atsi_count number of records in 'atsi'
2771 * @return GNUNET_OK to keep the connection open,
2772 * GNUNET_SYSERR to close it (signal serious error)
2775 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2776 const struct GNUNET_MessageHeader *message,
2777 const struct GNUNET_ATS_Information *atsi,
2778 unsigned int atsi_count)
2780 struct GNUNET_MESH_Unicast *msg;
2781 struct MeshTunnel *t;
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2786 "MESH: got a unicast packet from %s\n",
2788 size = ntohs (message->size);
2790 sizeof (struct GNUNET_MESH_Unicast) +
2791 sizeof (struct GNUNET_MessageHeader))
2796 msg = (struct GNUNET_MESH_Unicast *) message;
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2798 "MESH: of type %u\n",
2799 ntohs (msg[1].header.type));
2800 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2803 /* TODO notify back: we don't know this tunnel */
2804 GNUNET_break_op (0);
2807 tunnel_reset_timeout (t);
2808 pid = GNUNET_PEER_search(&msg->destination);
2811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2812 "MESH: it's for us! sending to clients...\n");
2813 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2817 "MESH: not for us, retransmitting...\n");
2818 send_message (message, path_get_first_hop(t->tree, pid));
2824 * Core handler for mesh network traffic going from the origin to all peers
2826 * @param cls closure
2827 * @param message message
2828 * @param peer peer identity this notification is about
2829 * @param atsi performance data
2830 * @param atsi_count number of records in 'atsi'
2831 * @return GNUNET_OK to keep the connection open,
2832 * GNUNET_SYSERR to close it (signal serious error)
2834 * TODO: Check who we got this from, to validate route.
2837 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
2838 const struct GNUNET_MessageHeader *message,
2839 const struct GNUNET_ATS_Information *atsi,
2840 unsigned int atsi_count)
2842 struct GNUNET_MESH_Multicast *msg;
2843 struct MeshTunnel *t;
2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2847 "MESH: got a multicast packet from %s\n",
2849 size = ntohs (message->size);
2850 if (sizeof (struct GNUNET_MESH_Multicast) +
2851 sizeof (struct GNUNET_MessageHeader) > size)
2853 GNUNET_break_op (0);
2856 msg = (struct GNUNET_MESH_Multicast *) message;
2857 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2861 /* TODO notify that we dont know that tunnel */
2862 GNUNET_break_op (0);
2865 tunnel_reset_timeout (t);
2867 /* Transmit to locally interested clients */
2868 if (NULL != t->peers &&
2869 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
2871 send_subscribed_clients (message, &msg[1].header);
2873 tunnel_send_multicast(t, message);
2879 * Core handler for mesh network traffic toward the owner of a tunnel
2881 * @param cls closure
2882 * @param message message
2883 * @param peer peer identity this notification is about
2884 * @param atsi performance data
2885 * @param atsi_count number of records in 'atsi'
2887 * @return GNUNET_OK to keep the connection open,
2888 * GNUNET_SYSERR to close it (signal serious error)
2891 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
2892 const struct GNUNET_MessageHeader *message,
2893 const struct GNUNET_ATS_Information *atsi,
2894 unsigned int atsi_count)
2896 struct GNUNET_MESH_ToOrigin *msg;
2897 struct GNUNET_PeerIdentity id;
2898 struct MeshPeerInfo *peer_info;
2899 struct MeshTunnel *t;
2902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2903 "MESH: got a ToOrigin packet from %s\n",
2905 size = ntohs (message->size);
2906 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
2907 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
2909 GNUNET_break_op (0);
2912 msg = (struct GNUNET_MESH_ToOrigin *) message;
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 "MESH: of type %u\n",
2915 ntohs (msg[1].header.type));
2916 t = tunnel_get (&msg->oid, ntohl (msg->tid));
2920 /* TODO notify that we dont know this tunnel (whom)? */
2921 GNUNET_break_op (0);
2925 if (t->id.oid == myid)
2928 struct GNUNET_MESH_ToOrigin *copy;
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2931 "MESH: it's for us! sending to clients...\n");
2932 if (NULL == t->client)
2934 /* got data packet for ownerless tunnel */
2935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2936 "MESH: no clients!\n");
2937 GNUNET_break_op (0);
2940 /* TODO signature verification */
2941 memcpy (cbuf, message, size);
2942 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
2943 copy->tid = htonl (t->local_tid);
2944 GNUNET_SERVER_notification_context_unicast (nc,
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2951 "MESH: not for us, retransmitting...\n");
2953 peer_info = peer_info_get (&msg->oid);
2954 if (NULL == peer_info)
2956 /* unknown origin of tunnel */
2960 GNUNET_PEER_resolve (t->tree->me->parent->peer, &id);
2961 send_message (message, &id);
2968 * Core handler for path ACKs
2970 * @param cls closure
2971 * @param message message
2972 * @param peer peer identity this notification is about
2973 * @param atsi performance data
2974 * @param atsi_count number of records in 'atsi'
2976 * @return GNUNET_OK to keep the connection open,
2977 * GNUNET_SYSERR to close it (signal serious error)
2980 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
2981 const struct GNUNET_MessageHeader *message,
2982 const struct GNUNET_ATS_Information *atsi,
2983 unsigned int atsi_count)
2985 struct GNUNET_MESH_PathACK *msg;
2986 struct GNUNET_PeerIdentity id;
2987 struct MeshTunnelTreeNode *n;
2988 struct MeshPeerInfo *peer_info;
2989 struct MeshTunnel *t;
2991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2992 "MESH: Received a path ACK msg [%s]\n",
2993 GNUNET_i2s(&my_full_id));
2994 msg = (struct GNUNET_MESH_PathACK *) message;
2995 t = tunnel_get (&msg->oid, msg->tid);
2998 /* TODO notify that we don't know the tunnel */
3002 /* Message for us? */
3003 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: It's for us!\n");
3006 if (NULL == t->client)
3008 GNUNET_break_op (0);
3011 if (NULL != t->dht_get_type)
3013 GNUNET_DHT_get_stop (t->dht_get_type);
3014 t->dht_get_type = NULL;
3016 peer_info = peer_info_get (&msg->peer_id);
3017 n = tree_find_peer(t->tree->root, peer_info->id);
3020 GNUNET_break_op (0);
3023 n->status = MESH_PEER_READY;
3024 send_client_peer_connected(t, peer_info->id);
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3029 "MESH: not for us, retransmitting...\n");
3030 GNUNET_PEER_resolve(t->tree->me->parent->peer, &id);
3031 peer_info = peer_info_get (&msg->oid);
3032 if (NULL == peer_info)
3034 /* If we know the tunnel, we should DEFINITELY know the peer */
3038 send_message (message, &id);
3044 * Functions to handle messages from core
3046 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3047 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3048 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3049 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3050 sizeof (struct GNUNET_MESH_PathBroken)},
3051 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3052 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3053 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3054 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3055 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3056 sizeof (struct GNUNET_MESH_PathACK)},
3062 /******************************************************************************/
3063 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3064 /******************************************************************************/
3067 * deregister_app: iterator for removing each application registered by a client
3069 * @param cls closure
3070 * @param key the hash of the application id (used to access the hashmap)
3071 * @param value the value stored at the key (client)
3073 * @return GNUNET_OK on success
3076 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3078 GNUNET_break (GNUNET_YES ==
3079 GNUNET_CONTAINER_multihashmap_remove (applications, key, value));
3085 * notify_client_connection_failure: notify a client that the connection to the
3086 * requested remote peer is not possible (for instance, no route found)
3087 * Function called when the socket is ready to queue more data. "buf" will be
3088 * NULL and "size" zero if the socket was closed for writing in the meantime.
3090 * @param cls closure
3091 * @param size number of bytes available in buf
3092 * @param buf where the callee should write the message
3093 * @return number of bytes written to buf
3096 notify_client_connection_failure (void *cls, size_t size, void *buf)
3099 struct MeshPeerInfo *peer_info;
3100 struct GNUNET_MESH_PeerControl *msg;
3101 struct GNUNET_PeerIdentity id;
3103 if (0 == size && NULL == buf)
3105 // TODO retry? cancel?
3109 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3110 peer_info = (struct MeshPeerInfo *) cls;
3111 msg = (struct GNUNET_MESH_PeerControl *) buf;
3112 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3113 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3114 // msg->tunnel_id = htonl(peer_info->t->tid);
3115 GNUNET_PEER_resolve (peer_info->id, &id);
3116 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3124 * Send keepalive packets for a peer
3126 * @param cls Closure (tunnel for which to send the keepalive).
3127 * @param tc Notification context.
3129 * TODO: implement explicit multicast keepalive?
3132 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3134 struct MeshTunnel *t = cls;
3135 struct GNUNET_MessageHeader *payload;
3136 struct GNUNET_MESH_Multicast *msg;
3137 size_t size = sizeof(struct GNUNET_MESH_Multicast) +
3138 sizeof(struct GNUNET_MessageHeader);
3141 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3145 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3148 "MESH: sending keepalive for tunnel %d\n",
3151 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3152 msg->header.size = htons (size);
3153 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3154 msg->oid = my_full_id;
3155 msg->tid = htonl(t->id.tid);
3156 payload = (struct GNUNET_MessageHeader *) &msg[1];
3157 payload->size = htons (sizeof(struct GNUNET_MessageHeader));
3158 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3159 tunnel_send_multicast (t, &msg->header);
3161 t->path_refresh_task =
3162 GNUNET_SCHEDULER_add_delayed (t->tree->refresh, &path_refresh, t);
3168 * Function to process paths received for a new peer addition. The recorded
3169 * paths form the initial tunnel, which can be optimized later.
3170 * Called on each result obtained for the DHT search.
3172 * @param cls closure
3173 * @param exp when will this value expire
3174 * @param key key of the result
3175 * @param type type of the result
3176 * @param size number of bytes in data
3177 * @param data pointer to the result data
3179 * TODO: re-issue the request after certain time? cancel after X results?
3182 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3183 const GNUNET_HashCode * key,
3184 const struct GNUNET_PeerIdentity *get_path,
3185 unsigned int get_path_length,
3186 const struct GNUNET_PeerIdentity *put_path,
3187 unsigned int put_path_length,
3188 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
3190 struct MeshPathInfo *path_info = cls;
3191 struct MeshPeerPath *p;
3192 struct GNUNET_PeerIdentity pi;
3195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3196 "MESH: Got results from DHT!\n");
3197 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3198 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
3201 // GNUNET_DHT_get_stop(path_info->peer->dhtget);
3202 // path_info->peer->dhtget = NULL;
3204 p = path_build_from_dht (get_path, get_path_length,
3205 put_path, put_path_length);
3206 path_add_to_peer (path_info->peer, p);
3207 for (i = 0; i < path_info->peer->ntunnels; i++)
3209 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3210 peer_info_connect(path_info->peer, path_info->t);
3212 // GNUNET_free (path_info);
3219 * Function to process paths received for a new peer addition. The recorded
3220 * paths form the initial tunnel, which can be optimized later.
3221 * Called on each result obtained for the DHT search.
3223 * @param cls closure
3224 * @param exp when will this value expire
3225 * @param key key of the result
3226 * @param type type of the result
3227 * @param size number of bytes in data
3228 * @param data pointer to the result data
3231 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3232 const GNUNET_HashCode * key,
3233 const struct GNUNET_PeerIdentity *get_path,
3234 unsigned int get_path_length,
3235 const struct GNUNET_PeerIdentity *put_path,
3236 unsigned int put_path_length,
3237 enum GNUNET_BLOCK_Type type, size_t size,
3240 const struct GNUNET_PeerIdentity *pi = data;
3241 struct MeshTunnel *t = cls;
3242 struct MeshPeerInfo *peer_info;
3243 struct MeshPeerPath *p;
3245 if (size != sizeof (struct GNUNET_PeerIdentity))
3247 GNUNET_break_op (0);
3250 GNUNET_assert (NULL != t->client);
3251 peer_info = peer_info_get (pi);
3252 (void) GNUNET_CONTAINER_multihashmap_put (
3256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3258 p = path_build_from_dht (get_path, get_path_length,
3259 put_path, put_path_length);
3260 path_add_to_peer (peer_info, p);
3261 tunnel_add_peer(t, peer_info);
3262 peer_info_connect (peer_info, t);
3266 /******************************************************************************/
3267 /********************* MESH LOCAL HANDLES **************************/
3268 /******************************************************************************/
3272 * Handler for client disconnection
3274 * @param cls closure
3275 * @param client identification of the client; NULL
3276 * for the last call when the server is destroyed
3279 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3281 struct MeshClient *c;
3282 struct MeshClient *next;
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: client disconnected\n");
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (SERVER DOWN)\n");
3293 if (c->handle != client && NULL != client)
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: ... searching\n");
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
3300 GNUNET_SERVER_client_drop (c->handle);
3301 if (NULL != c->tunnels)
3303 GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
3304 &tunnel_destroy_iterator,
3306 GNUNET_CONTAINER_multihashmap_destroy (c->tunnels);
3309 /* deregister clients applications */
3310 if (NULL != c->apps)
3312 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3313 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3315 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3316 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3318 GNUNET_SCHEDULER_cancel (announce_applications_task);
3319 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3321 if (NULL != c->types)
3322 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3324 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT FREE at %p\n", c);
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: done!\n");
3336 * Handler for new clients
3338 * @param cls closure
3339 * @param client identification of the client
3340 * @param message the actual message, which includes messages the client wants
3343 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3344 const struct GNUNET_MessageHeader *message)
3346 struct GNUNET_MESH_ClientConnect *cc_msg;
3347 struct MeshClient *c;
3348 GNUNET_MESH_ApplicationType *a;
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
3356 /* Check data sanity */
3357 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3358 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3359 ntypes = ntohs (cc_msg->types);
3360 napps = ntohs (cc_msg->applications);
3362 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3369 /* Create new client structure */
3370 c = GNUNET_malloc (sizeof (struct MeshClient));
3372 c->id = next_client_id++;
3374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: CLIENT NEW %u at %p\n", c->id, c);
3376 GNUNET_SERVER_client_keep (client);
3377 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3380 GNUNET_MESH_ApplicationType at;
3383 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3384 for (i = 0; i < napps; i++)
3387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: app type: %u\n", at);
3388 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3389 /* store in clients hashmap */
3390 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3391 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3392 /* store in global hashmap, for announcements */
3393 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3394 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3396 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3397 announce_applications_task =
3398 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3406 t = (uint16_t *) & a[napps];
3407 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3408 for (i = 0; i < ntypes; i++)
3411 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3413 /* store in clients hashmap */
3414 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3416 /* store in global hashmap */
3417 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3418 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3422 "MESH: client has %u+%u subscriptions\n", napps, ntypes);
3424 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3425 c->tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3426 GNUNET_SERVER_notification_context_add (nc, client);
3428 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client processed\n");
3436 * Handler for requests of new tunnels
3438 * @param cls closure
3439 * @param client identification of the client
3440 * @param message the actual message
3443 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3444 const struct GNUNET_MessageHeader *message)
3446 struct GNUNET_MESH_TunnelMessage *t_msg;
3447 struct MeshTunnel *t;
3448 struct MeshClient *c;
3449 GNUNET_HashCode hash;
3451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new tunnel requested\n");
3453 /* Sanity check for client registration */
3454 if (NULL == (c = client_get (client)))
3457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3464 /* Message sanity check */
3465 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3472 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3473 /* Sanity check for tunnel numbering */
3474 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3480 /* Sanity check for duplicate tunnel IDs */
3481 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3488 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3489 while (NULL != tunnel_get_by_pi (myid, next_tid))
3490 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3491 t->id.tid = next_tid++;
3493 t->local_tid = ntohl (t_msg->tunnel_id);
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3496 "MESH: CREATED TUNNEL %s [%x] (%x)\n",
3497 GNUNET_i2s (&my_full_id),
3502 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3504 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3506 GNUNET_CONTAINER_multihashmap_put (
3507 c->tunnels, &hash, t,
3508 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3511 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3515 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3517 GNUNET_CONTAINER_multihashmap_put (
3519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3525 t->tree = tree_new (t, myid);
3526 t->tree->refresh = REFRESH_PATH_TIME;
3527 t->tree->root->status = MESH_PEER_READY;
3528 t->tree->me = t->tree->root;
3530 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3536 * Handler for requests of deleting tunnels
3538 * @param cls closure
3539 * @param client identification of the client
3540 * @param message the actual message
3543 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
3544 const struct GNUNET_MessageHeader *message)
3546 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
3547 struct MeshClient *c;
3548 struct MeshTunnel *t;
3549 MESH_TunnelNumber tid;
3550 GNUNET_HashCode hash;
3552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3553 "MESH: Got a DESTROY TUNNEL from client!\n");
3555 /* Sanity check for client registration */
3556 if (NULL == (c = client_get (client)))
3559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3562 /* Message sanity check */
3563 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: by client %u\n", c->id);
3572 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3574 /* Retrieve tunnel */
3575 tid = ntohl (tunnel_msg->tunnel_id);
3577 /* Remove from local id hashmap */
3578 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3579 t = GNUNET_CONTAINER_multihashmap_get (c->tunnels, &hash);
3580 GNUNET_CONTAINER_multihashmap_remove (c->tunnels, &hash, t);
3583 tunnel_send_destroy (t);
3585 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3591 * Handler for connection requests to new peers
3593 * @param cls closure
3594 * @param client identification of the client
3595 * @param message the actual message (PeerControl)
3598 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
3599 const struct GNUNET_MessageHeader *message)
3601 struct GNUNET_MESH_PeerControl *peer_msg;
3602 struct MeshPeerInfo *peer_info;
3603 struct MeshClient *c;
3604 struct MeshTunnel *t;
3605 MESH_TunnelNumber tid;
3607 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: Got connection request\n");
3608 /* Sanity check for client registration */
3609 if (NULL == (c = client_get (client)))
3612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3616 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3617 /* Sanity check for message size */
3618 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3625 /* Tunnel exists? */
3626 tid = ntohl (peer_msg->tunnel_id);
3627 t = tunnel_get_by_local_id (c, tid);
3631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3635 /* Does client own tunnel? */
3636 if (t->client->handle != client)
3639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3642 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH: for %s\n",
3643 GNUNET_i2s(&peer_msg->peer));
3644 peer_info = peer_info_get (&peer_msg->peer);
3646 tunnel_add_peer(t, peer_info);
3647 peer_info_connect(peer_info, t);
3649 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3655 * Handler for disconnection requests of peers in a tunnel
3657 * @param cls closure
3658 * @param client identification of the client
3659 * @param message the actual message (PeerControl)
3662 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
3663 const struct GNUNET_MessageHeader *message)
3665 struct GNUNET_MESH_PeerControl *peer_msg;
3666 struct MeshPeerInfo *peer_info;
3667 struct MeshClient *c;
3668 struct MeshTunnel *t;
3669 MESH_TunnelNumber tid;
3671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Got a PEER DEL request\n");
3672 /* Sanity check for client registration */
3673 if (NULL == (c = client_get (client)))
3676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3679 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
3680 /* Sanity check for message size */
3681 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
3684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3688 /* Tunnel exists? */
3689 tid = ntohl (peer_msg->tunnel_id);
3690 t = tunnel_get_by_local_id (c, tid);
3694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: on tunnel %X\n", t->id.tid);
3699 /* Does client own tunnel? */
3700 if (t->client->handle != client)
3703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3708 "MESH: for peer %s\n",
3709 GNUNET_i2s(&peer_msg->peer));
3710 /* Is the peer in the tunnel? */
3711 peer_info = GNUNET_CONTAINER_multihashmap_get(t->peers,
3712 &peer_msg->peer.hashPubKey);
3713 if (NULL == peer_info)
3716 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3720 /* Ok, delete peer from tunnel */
3721 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
3722 &peer_msg->peer.hashPubKey);
3724 send_destroy_path (t, peer_info->id);
3725 tunnel_delete_peer(t, peer_info->id);
3726 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3732 * Handler for connection requests to new peers by type
3734 * @param cls closure
3735 * @param client identification of the client
3736 * @param message the actual message (ConnectPeerByType)
3739 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
3740 const struct GNUNET_MessageHeader *message)
3742 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
3743 struct MeshClient *c;
3744 struct MeshTunnel *t;
3745 GNUNET_HashCode hash;
3746 MESH_TunnelNumber tid;
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: got connect by type request\n");
3749 /* Sanity check for client registration */
3750 if (NULL == (c = client_get (client)))
3753 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3757 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
3758 /* Sanity check for message size */
3759 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
3760 ntohs (connect_msg->header.size))
3763 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3767 /* Tunnel exists? */
3768 tid = ntohl (connect_msg->tunnel_id);
3769 t = tunnel_get_by_local_id (c, tid);
3773 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3777 /* Does client own tunnel? */
3778 if (t->client->handle != client)
3781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3785 /* Do WE have the service? */
3786 t->type = ntohl (connect_msg->type);
3787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: type requested: %u\n", t->type);
3788 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
3789 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
3792 /* Yes! Fast forward, add ourselves to the tunnel and send the
3793 * good news to the client
3795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: available locally\n");
3796 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
3797 peer_info_get (&my_full_id),
3798 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: notifying client\n");
3801 send_client_peer_connected(t, myid);
3802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Done\n");
3803 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3806 /* Ok, lets find a peer offering the service */
3807 if (NULL != t->dht_get_type)
3809 GNUNET_DHT_get_stop (t->dht_get_type);
3811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: looking in DHT for %s\n",
3812 GNUNET_h2s (&hash));
3814 GNUNET_DHT_get_start (dht_handle,
3815 GNUNET_TIME_UNIT_FOREVER_REL,
3816 GNUNET_BLOCK_TYPE_TEST,
3819 GNUNET_DHT_RO_RECORD_ROUTE |
3820 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
3822 &dht_get_type_handler, t);
3824 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3830 * Handler for client traffic directed to one peer
3832 * @param cls closure
3833 * @param client identification of the client
3834 * @param message the actual message
3837 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
3838 const struct GNUNET_MessageHeader *message)
3840 struct MeshClient *c;
3841 struct MeshTunnel *t;
3842 struct MeshPeerInfo *pi;
3843 struct GNUNET_MESH_Unicast *data_msg;
3844 MESH_TunnelNumber tid;
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 "MESH: Got a unicast request from a client!\n");
3850 /* Sanity check for client registration */
3851 if (NULL == (c = client_get (client)))
3854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3857 data_msg = (struct GNUNET_MESH_Unicast *) message;
3858 /* Sanity check for message size */
3859 size = ntohs (message->size);
3860 if (sizeof (struct GNUNET_MESH_Unicast) +
3861 sizeof (struct GNUNET_MessageHeader) > size)
3864 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3868 /* Tunnel exists? */
3869 tid = ntohl (data_msg->tid);
3870 t = tunnel_get_by_local_id (c, tid);
3874 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3878 /* Is it a local tunnel? Then, does client own the tunnel? */
3879 if (NULL != t->client &&
3880 NULL != t->client->handle &&
3881 t->client->handle != client)
3884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3888 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
3889 &data_msg->destination.hashPubKey);
3890 /* Is the selected peer in the tunnel? */
3894 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3898 /* Ok, everything is correct, send the message
3899 * (pretend we got it from a mesh peer)
3902 char buf[ntohs (message->size)];
3903 struct GNUNET_MESH_Unicast *copy;
3905 /* Work around const limitation */
3906 copy = (struct GNUNET_MESH_Unicast *) buf;
3907 memcpy (buf, data_msg, size);
3908 copy->oid = my_full_id;
3909 copy->tid = htonl (t->id.tid);
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3911 "MESH: calling generic handler...\n");
3912 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
3914 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3920 * Handler for client traffic directed to the origin
3922 * @param cls closure
3923 * @param client identification of the client
3924 * @param message the actual message
3927 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
3928 const struct GNUNET_MessageHeader *message)
3930 struct GNUNET_MESH_ToOrigin *data_msg;
3931 struct GNUNET_PeerIdentity id;
3932 struct MeshClient *c;
3933 struct MeshTunnel *t;
3934 MESH_TunnelNumber tid;
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3938 "MESH: Got a ToOrigin request from a client!\n");
3940 /* Sanity check for client registration */
3941 if (NULL == (c = client_get (client)))
3944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3947 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
3948 /* Sanity check for message size */
3949 size = ntohs (message->size);
3950 if (sizeof (struct GNUNET_MESH_ToOrigin) +
3951 sizeof (struct GNUNET_MessageHeader) > size)
3954 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3958 /* Tunnel exists? */
3959 tid = ntohl (data_msg->tid);
3960 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3966 t = tunnel_get_by_local_id (c, tid);
3970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3974 /* It shouldn't be a local tunnel. */
3975 if (NULL != t->client)
3978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3981 GNUNET_PEER_resolve(t->id.oid, &id);
3983 /* Ok, everything is correct, send the message
3984 * (pretend we got it from a mesh peer)
3987 char buf[ntohs (message->size)];
3988 struct GNUNET_MESH_ToOrigin *copy;
3990 /* Work around const limitation */
3991 copy = (struct GNUNET_MESH_ToOrigin *) buf;
3992 memcpy (buf, data_msg, size);
3994 copy->tid = htonl (t->id.tid);
3995 copy->sender = my_full_id;
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3997 "MESH: calling generic handler...\n");
3998 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4000 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4006 * Handler for client traffic directed to all peers in a tunnel
4008 * @param cls closure
4009 * @param client identification of the client
4010 * @param message the actual message
4013 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4014 const struct GNUNET_MessageHeader *message)
4016 struct MeshClient *c;
4017 struct MeshTunnel *t;
4018 struct GNUNET_MESH_Multicast *data_msg;
4019 MESH_TunnelNumber tid;
4021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4022 "MESH: Got a multicast request from a client!\n");
4024 /* Sanity check for client registration */
4025 if (NULL == (c = client_get (client)))
4028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4031 data_msg = (struct GNUNET_MESH_Multicast *) message;
4032 /* Sanity check for message size */
4033 if (sizeof (struct GNUNET_MESH_Multicast) +
4034 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4041 /* Tunnel exists? */
4042 tid = ntohl (data_msg->tid);
4043 t = tunnel_get_by_local_id (c, tid);
4047 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4051 /* Does client own tunnel? */
4052 if (t->client->handle != client)
4055 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4060 char buf[ntohs(message->size)];
4061 struct GNUNET_MESH_Multicast *copy;
4063 copy = (struct GNUNET_MESH_Multicast *)buf;
4064 memcpy(buf, message, ntohs(message->size));
4065 copy->oid = my_full_id;
4066 copy->tid = htonl(t->id.tid);
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4068 "MESH: calling generic handler...\n");
4069 handle_mesh_data_multicast(client, &my_full_id, ©->header, NULL, 0);
4072 /* receive done gets called when last copy is sent to a neighbor */
4077 * Functions to handle messages from clients
4079 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4080 {&handle_local_new_client, NULL,
4081 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4082 {&handle_local_tunnel_create, NULL,
4083 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4084 sizeof (struct GNUNET_MESH_TunnelMessage)},
4085 {&handle_local_tunnel_destroy, NULL,
4086 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4087 sizeof (struct GNUNET_MESH_TunnelMessage)},
4088 {&handle_local_connect_add, NULL,
4089 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4090 sizeof (struct GNUNET_MESH_PeerControl)},
4091 {&handle_local_connect_del, NULL,
4092 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4093 sizeof (struct GNUNET_MESH_PeerControl)},
4094 {&handle_local_connect_by_type, NULL,
4095 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4096 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4097 {&handle_local_unicast, NULL,
4098 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4099 {&handle_local_to_origin, NULL,
4100 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4101 {&handle_local_multicast, NULL,
4102 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4108 * To be called on core init/fail.
4110 * @param cls service closure
4111 * @param server handle to the server for this service
4112 * @param identity the public identity of this peer
4115 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4116 const struct GNUNET_PeerIdentity *identity)
4118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Core init\n");
4119 core_handle = server;
4120 if (0 != memcmp(identity, &my_full_id, sizeof(my_full_id)) || NULL == server)
4122 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("MESH: Wrong CORE service\n"));
4123 GNUNET_SCHEDULER_shutdown();
4129 * Method called whenever a given peer connects.
4131 * @param cls closure
4132 * @param peer peer identity this notification is about
4133 * @param atsi performance data for the connection
4134 * @param atsi_count number of records in 'atsi'
4137 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4138 const struct GNUNET_ATS_Information *atsi,
4139 unsigned int atsi_count)
4141 struct MeshPeerInfo *peer_info;
4142 struct MeshPeerPath *path;
4144 #if MESH_DEBUG_CONNECTION
4145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
4146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4147 GNUNET_i2s(&my_full_id));
4149 peer_info = peer_info_get (peer);
4150 if (myid == peer_info->id)
4152 #if MESH_DEBUG_CONNECTION
4153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4157 #if MESH_DEBUG_CONNECTION
4160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: %s\n",
4164 path = path_new (2);
4165 path->peers[0] = myid;
4166 path->peers[1] = peer_info->id;
4167 GNUNET_PEER_change_rc(myid, 1);
4168 GNUNET_PEER_change_rc(peer_info->id, 1);
4169 path_add_to_peer (peer_info, path);
4174 * Method called whenever a peer disconnects.
4176 * @param cls closure
4177 * @param peer peer identity this notification is about
4180 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4182 struct MeshPeerInfo *pi;
4185 #if MESH_DEBUG_CONNECTION
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer disconnected\n");
4188 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4194 for (i = 0; i < CORE_QUEUE_SIZE; i++)
4196 /* TODO: notify that the transmission failed */
4197 peer_info_cancel_transmission(pi, i);
4199 path_remove_from_peer (pi, pi->id, myid);
4200 #if MESH_DEBUG_CONNECTION
4203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: (self)\n");
4210 /******************************************************************************/
4211 /************************ MAIN FUNCTIONS ****************************/
4212 /******************************************************************************/
4215 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4217 * @param cls closure
4218 * @param key current key code
4219 * @param value value in the hash map
4220 * @return GNUNET_YES if we should continue to iterate,
4224 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4226 struct MeshTunnel *t = value;
4232 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4234 * @param cls closure
4235 * @param key current key code
4236 * @param value value in the hash map
4237 * @return GNUNET_YES if we should continue to iterate,
4241 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4243 struct MeshPeerInfo *p = value;
4244 peer_info_destroy (p);
4249 * Task run during shutdown.
4255 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shutting down\n");
4258 /* TODO: destroy tunnels? */
4259 if (core_handle != NULL)
4261 GNUNET_CORE_disconnect (core_handle);
4264 GNUNET_CONTAINER_multihashmap_iterate(tunnels, &shutdown_tunnel, NULL);
4265 GNUNET_CONTAINER_multihashmap_iterate(peers, &shutdown_peer, NULL);
4266 if (dht_handle != NULL)
4268 GNUNET_DHT_disconnect (dht_handle);
4273 GNUNET_SERVER_notification_context_destroy (nc);
4276 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4278 GNUNET_SCHEDULER_cancel (announce_id_task);
4279 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: shut down\n");
4285 * Process mesh requests.
4287 * @param cls closure
4288 * @param server the initialized server
4289 * @param c configuration to use
4292 run (void *cls, struct GNUNET_SERVER_Handle *server,
4293 const struct GNUNET_CONFIGURATION_Handle *c)
4295 struct MeshPeerInfo *peer;
4296 struct MeshPeerPath *p;
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: starting to run\n");
4300 server_handle = server;
4301 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4302 CORE_QUEUE_SIZE, /* queue size */
4303 NULL, /* Closure passed to MESH functions */
4304 &core_init, /* Call core_init once connected */
4305 &core_connect, /* Handle connects */
4306 &core_disconnect, /* remove peers on disconnects */
4307 NULL, /* Don't notify about all incoming messages */
4308 GNUNET_NO, /* For header only in notification */
4309 NULL, /* Don't notify about all outbound messages */
4310 GNUNET_NO, /* For header-only out notification */
4311 core_handlers); /* Register these handlers */
4312 if (core_handle == NULL)
4315 GNUNET_SCHEDULER_shutdown ();
4320 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4325 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4326 GNUNET_SCHEDULER_shutdown ();
4329 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4330 GNUNET_free (keyfile);
4331 if (my_private_key == NULL)
4333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4334 _("Mesh service could not access hostkey. Exiting.\n"));
4335 GNUNET_SCHEDULER_shutdown ();
4338 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4339 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4340 &my_full_id.hashPubKey);
4341 myid = GNUNET_PEER_intern (&my_full_id);
4343 dht_handle = GNUNET_DHT_connect (c, 64);
4344 if (dht_handle == NULL)
4350 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4352 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4353 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4354 peers = GNUNET_CONTAINER_multihashmap_create (32);
4355 applications = GNUNET_CONTAINER_multihashmap_create (32);
4356 types = GNUNET_CONTAINER_multihashmap_create (32);
4358 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4359 nc = GNUNET_SERVER_notification_context_create (server_handle,
4361 GNUNET_SERVER_disconnect_notify (server_handle,
4362 &handle_local_client_disconnect,
4367 clients_tail = NULL;
4372 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4373 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4375 /* Create a peer_info for the local peer */
4376 peer = peer_info_get(&my_full_id);
4379 path_add_to_peer(peer, p);
4381 /* Scheduled the task to clean up when shutdown is called */
4382 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: end of run()\n");
4389 * The main function for the mesh service.
4391 * @param argc number of arguments from the command line
4392 * @param argv command line arguments
4393 * @return 0 ok, 1 on error
4396 main (int argc, char *const *argv)
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main()\n");
4403 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: main() END\n");