2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
41 * - speed requirement specification (change?) in mesh API -- API call
43 * - relay corking down to core
44 * - set ttl relative to tree depth
50 #include "mesh_protocol.h"
51 #include "gnunet_dht_service.h"
52 #include "mesh_tunnel_tree.h"
54 /* TODO: move into configuration file */
55 #define REFRESH_PATH_TIME GNUNET_TIME_relative_multiply(\
56 GNUNET_TIME_UNIT_SECONDS,\
58 #define APP_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
59 GNUNET_TIME_UNIT_SECONDS,\
62 #define ID_ANNOUNCE_TIME GNUNET_TIME_relative_multiply(\
63 GNUNET_TIME_UNIT_SECONDS,\
66 #define UNACKNOWLEDGED_WAIT GNUNET_TIME_relative_multiply(\
67 GNUNET_TIME_UNIT_SECONDS,\
69 #define DEFAULT_TTL 64
73 #define MESH_DEBUG_DHT GNUNET_YES
74 #define MESH_DEBUG_CONNECTION GNUNET_NO
76 #if MESH_DEBUG_CONNECTION
77 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
79 #define DEBUG_CONN(...)
83 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
85 #define DEBUG_DHT(...)
88 /******************************************************************************/
89 /************************ DATA STRUCTURES ****************************/
90 /******************************************************************************/
92 /** FWD declaration */
97 * Struct representing a piece of data being sent to other peers
101 /** Tunnel it belongs to. */
102 struct MeshTunnel *t;
104 /** In case of a multicast, task to allow a client to send more data if
105 * some neighbor is too slow. */
106 GNUNET_SCHEDULER_TaskIdentifier *task;
108 /** How many remaining neighbors we need to send this to. */
109 unsigned int *reference_counter;
111 /** Size of the data. */
120 * Struct containing info about a queued transmission to this peer
127 struct MeshPeerQueue *next;
132 struct MeshPeerQueue *prev;
135 * Peer this transmission is directed to.
137 struct MeshPeerInfo *peer;
140 * Tunnel this message belongs to.
142 struct MeshTunnel *tunnel;
145 * Pointer to info stucture used as cls.
155 * Size of the message
162 * Struct containing all info possibly needed to build a package when called
165 struct MeshTransmissionDescriptor
167 /** ID of the tunnel this packet travels in */
168 struct MESH_TunnelID *origin;
170 /** Who was this message being sent to */
171 struct MeshPeerInfo *peer;
173 /** Ultimate destination of the packet */
174 GNUNET_PEER_Id destination;
176 /** Data descriptor */
177 struct MeshData* mesh_data;
182 * Struct containing all information regarding a given peer
192 * Last time we heard from this peer
194 struct GNUNET_TIME_Absolute last_contact;
197 * Number of attempts to reconnect so far
199 int n_reconnect_attempts;
202 * Paths to reach the peer, ordered by ascending hop count
204 struct MeshPeerPath *path_head;
207 * Paths to reach the peer, ordered by ascending hop count
209 struct MeshPeerPath *path_tail;
212 * Handle to stop the DHT search for a path to this peer
214 struct GNUNET_DHT_GetHandle *dhtget;
217 * Closure given to the DHT GET
219 struct MeshPathInfo *dhtgetcls;
222 * Array of tunnels this peer participates in
223 * (most probably a small amount, therefore not a hashmap)
224 * When the path to the peer changes, notify these tunnels to let them
225 * re-adjust their path trees.
227 struct MeshTunnel **tunnels;
230 * Number of tunnels this peers participates in
232 unsigned int ntunnels;
235 * Transmission queue to core DLL head
237 struct MeshPeerQueue *queue_head;
240 * Transmission queue to core DLL tail
242 struct MeshPeerQueue *queue_tail;
245 * How many messages are in the queue to this peer.
247 unsigned int queue_n;
250 * Handle to for queued transmissions
252 struct GNUNET_CORE_TransmitHandle *core_transmit;
257 * Globally unique tunnel identification (owner + number)
258 * DO NOT USE OVER THE NETWORK
263 * Node that owns the tunnel
268 * Tunnel number to differentiate all the tunnels owned by the node oid
269 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
271 MESH_TunnelNumber tid;
275 struct MeshClient; /* FWD declaration */
278 * Struct containing all information regarding a tunnel
279 * For an intermediate node the improtant info used will be:
280 * - id Tunnel unique identification
281 * - paths[0] To know where to send it next
282 * - metainfo: ready, speeds, accounting
289 struct MESH_TunnelID id;
292 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
294 MESH_TunnelNumber local_tid;
297 * Local tunnel number for local destination clients (incoming number)
298 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
301 MESH_TunnelNumber local_tid_dest;
304 * Global count ID of the last *multicast* packet seen/sent.
309 * Local count ID of the last packet seen/sent.
314 * SKIP value for this tunnel.
319 * How many messages are in the queue.
321 unsigned int queue_n;
324 * How many messages do we accept in the queue.
326 unsigned int queue_max;
329 * Last time the tunnel was used
331 struct GNUNET_TIME_Absolute timestamp;
334 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
335 * containing peers added by id or by type, not intermediate peers.
337 struct GNUNET_CONTAINER_MultiHashMap *peers;
340 * Number of peers that are connected and potentially ready to receive data
342 unsigned int peers_ready;
345 * Number of peers that have been added to the tunnel
347 unsigned int peers_total;
350 * Client owner of the tunnel, if any
352 struct MeshClient *owner;
355 * Clients that have been informed about the tunnel, if any
357 struct MeshClient **clients;
360 * Number of elements in clients
362 unsigned int nclients;
365 * Clients that have requested to leave the tunnel
367 struct MeshClient **ignore;
370 * Number of elements in clients
372 unsigned int nignore;
377 struct MeshTunnelTree *tree;
380 * Application type we are looking for in this tunnel
382 GNUNET_MESH_ApplicationType type;
385 * Used to search peers offering a service
387 struct GNUNET_DHT_GetHandle *dht_get_type;
390 * Task to keep the used paths alive
392 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
395 * Task to destroy the tunnel after timeout
397 * FIXME: merge the two? a tunnel will have either
398 * a path refresh OR a timeout, never both!
400 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
405 * Info needed to work with tunnel paths and peers
412 struct MeshTunnel *t;
415 * Neighbouring peer to whom we send the packet to
417 struct MeshPeerInfo *peer;
422 struct MeshPeerPath *path;
427 * Struct containing information about a client of the service
434 struct MeshClient *next;
439 struct MeshClient *prev;
442 * Tunnels that belong to this client, indexed by local id
444 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
447 * Tunnels this client has accepted, indexed by incoming local id
449 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
452 * Tunnels this client has rejected, indexed by incoming local id
454 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
456 * Handle to communicate with the client
458 struct GNUNET_SERVER_Client *handle;
461 * Applications that this client has claimed to provide
463 struct GNUNET_CONTAINER_MultiHashMap *apps;
466 * Messages that this client has declared interest in
468 struct GNUNET_CONTAINER_MultiHashMap *types;
471 * Whether the client is active or shutting down (don't send confirmations
472 * to a client that is shutting down.
477 * ID of the client, mainly for debug messages
485 /******************************************************************************/
486 /************************ DEBUG FUNCTIONS ****************************/
487 /******************************************************************************/
491 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
492 * @param cls string to print
493 * @param success GNUNET_OK if the PUT was transmitted,
494 * GNUNET_NO on timeout,
495 * GNUNET_SYSERR on disconnect from service
496 * after the PUT message was transmitted
497 * (so we don't know if it was received or not)
502 mesh_debug (void *cls, int success)
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
512 /******************************************************************************/
513 /*********************** GLOBAL VARIABLES ****************************/
514 /******************************************************************************/
519 static struct MeshClient *clients;
520 static struct MeshClient *clients_tail;
523 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
525 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
528 * Tunnels incoming, indexed by MESH_TunnelNumber
529 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
531 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
534 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
536 static struct GNUNET_CONTAINER_MultiHashMap *peers;
539 * Handle to communicate with transport
541 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
544 * Handle to communicate with core
546 static struct GNUNET_CORE_Handle *core_handle;
551 static struct GNUNET_DHT_Handle *dht_handle;
556 static struct GNUNET_SERVER_Handle *server_handle;
559 * Notification context, to send messages to local clients
561 static struct GNUNET_SERVER_NotificationContext *nc;
564 * Local peer own ID (memory efficient handle)
566 static GNUNET_PEER_Id myid;
569 * Local peer own ID (full value)
571 static struct GNUNET_PeerIdentity my_full_id;
576 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
581 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
584 * Tunnel ID for the next created tunnel (global tunnel number)
586 static MESH_TunnelNumber next_tid;
589 * Tunnel ID for the next incoming tunnel (local tunnel number)
591 static MESH_TunnelNumber next_local_tid;
594 * All application types provided by this peer
596 static struct GNUNET_CONTAINER_MultiHashMap *applications;
599 * All message types clients of this peer are interested in
601 static struct GNUNET_CONTAINER_MultiHashMap *types;
604 * Task to periodically announce provided applications
606 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
609 * Task to periodically announce itself in the network
611 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
614 * Next ID to assign to a client
616 unsigned int next_client_id;
620 /******************************************************************************/
621 /************************ ITERATORS ****************************/
622 /******************************************************************************/
624 /* FIXME move iterators here */
627 /******************************************************************************/
628 /************************ PERIODIC FUNCTIONS ****************************/
629 /******************************************************************************/
632 * Announce iterator over for each application provided by the peer
635 * @param key current key code
636 * @param value value in the hash map
637 * @return GNUNET_YES if we should continue to
642 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
644 /* FIXME are hashes in multihash map equal on all aquitectures? */
645 /* FIXME: keep return value of 'put' to possibly cancel!? */
646 GNUNET_DHT_put (dht_handle, key, 10,
647 GNUNET_DHT_RO_RECORD_ROUTE |
648 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
649 sizeof (struct GNUNET_PeerIdentity),
650 (const char *) &my_full_id,
651 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
653 APP_ANNOUNCE_TIME, NULL, NULL);
659 * Periodically announce what applications are provided by local clients
662 * @param tc task context
665 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
667 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
669 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
673 DEBUG_DHT ("Starting PUT for apps\n");
675 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
677 announce_applications_task =
678 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
680 DEBUG_DHT ("Finished PUT for apps\n");
687 * Periodically announce self id in the DHT
690 * @param tc task context
693 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
695 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
697 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
701 * - Set data expiration in function of X
704 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
706 GNUNET_DHT_put (dht_handle, /* DHT handle */
707 &my_full_id.hashPubKey, /* Key to use */
708 10, /* Replication level */
709 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
710 GNUNET_BLOCK_TYPE_TEST, /* Block type */
711 sizeof (my_full_id), /* Size of the data */
712 (char *) &my_full_id, /* Data itself */
713 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
714 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
715 NULL, /* Continuation */
716 NULL); /* Continuation closure */
718 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
723 * Function to process paths received for a new peer addition. The recorded
724 * paths form the initial tunnel, which can be optimized later.
725 * Called on each result obtained for the DHT search.
728 * @param exp when will this value expire
729 * @param key key of the result
730 * @param type type of the result
731 * @param size number of bytes in data
732 * @param data pointer to the result data
735 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
736 const GNUNET_HashCode * key,
737 const struct GNUNET_PeerIdentity *get_path,
738 unsigned int get_path_length,
739 const struct GNUNET_PeerIdentity *put_path,
740 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
741 size_t size, const void *data);
744 /******************************************************************************/
745 /****************** GENERAL HELPER FUNCTIONS ************************/
746 /******************************************************************************/
749 * Search for a tunnel by global ID using full PeerIdentities
751 * @param oid owner of the tunnel
752 * @param tid global tunnel number
754 * @return tunnel handler, NULL if doesn't exist
756 static struct MeshTunnel *
757 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
761 * Delete an active client from the tunnel.
767 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
770 * Notify a tunnel that a connection has broken that affects at least
773 * @param t Tunnel affected.
774 * @param p1 Peer that got disconnected from p2.
775 * @param p2 Peer that got disconnected from p1.
777 * @return Short ID of the peer disconnected (either p1 or p2).
778 * 0 if the tunnel remained unaffected.
780 static GNUNET_PEER_Id
781 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
786 * Check if client has registered with the service and has not disconnected
788 * @param client the client to check
790 * @return non-NULL if client exists in the global DLL
792 static struct MeshClient *
793 client_get (struct GNUNET_SERVER_Client *client)
795 struct MeshClient *c;
800 if (c->handle == client)
809 * Checks if a given client has subscribed to certain message type
811 * @param message_type Type of message to check
812 * @param c Client to check
814 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
819 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
823 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
824 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
829 * Allow a client to send more data after transmitting a multicast message
830 * which some neighbor has not yet accepted altough a reasonable time has
833 * @param cls Closure (DataDescriptor containing the task identifier)
834 * @param tc Task Context
836 * FIXME reference counter cshould be just int
839 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
841 struct MeshData *mdata = cls;
843 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
845 GNUNET_assert (NULL != mdata->reference_counter);
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
848 *(mdata->reference_counter));
849 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
850 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
855 * Check whether client wants traffic from a tunnel.
857 * @param c Client to check.
858 * @param t Tunnel to be found.
860 * @return GNUNET_YES if client knows tunnel.
862 * TODO look in client hashmap
865 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
869 for (i = 0; i < t->nclients; i++)
870 if (t->clients[i] == c)
877 * Check whether client has been informed about a tunnel.
879 * @param c Client to check.
880 * @param t Tunnel to be found.
882 * @return GNUNET_YES if client knows tunnel.
884 * TODO look in client hashmap
887 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
891 for (i = 0; i < t->nignore; i++)
892 if (t->ignore[i] == c)
894 return client_wants_tunnel(c, t);
899 * Marks a client as uninterested in traffic from the tunnel, updating both
900 * client and tunnel to reflect this.
902 * @param c Client that doesn't want traffic anymore.
903 * @param t Tunnel which should be ignored.
905 * FIXME when to delete an incoming tunnel?
908 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
910 GNUNET_HashCode hash;
912 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
913 GNUNET_break (GNUNET_YES ==
914 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
916 GNUNET_break (GNUNET_YES ==
917 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
918 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
919 tunnel_delete_active_client (t, c);
920 GNUNET_array_append (t->ignore, t->nignore, c);
925 * Deletes a tunnel from a client (either owner or destination). To be used on
926 * tunnel destroy, otherwise, use client_ignore_tunnel.
928 * @param c Client whose tunnel to delete.
929 * @param t Tunnel which should be deleted.
932 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
934 GNUNET_HashCode hash;
938 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
939 GNUNET_assert (GNUNET_YES ==
940 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
946 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
948 GNUNET_assert (GNUNET_YES ==
949 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
953 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
962 * Send the message to all clients that have subscribed to its type
964 * @param msg Pointer to the message itself
965 * @param payload Pointer to the payload of the message.
966 * @return number of clients this message was sent to
969 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
970 const struct GNUNET_MessageHeader *payload)
972 struct GNUNET_PeerIdentity *oid;
973 struct MeshClient *c;
974 struct MeshTunnel *t;
975 MESH_TunnelNumber *tid;
978 char cbuf[htons (msg->size)];
980 type = ntohs (payload->type);
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
984 memcpy (cbuf, msg, sizeof (cbuf));
985 switch (htons (msg->type))
987 struct GNUNET_MESH_Unicast *uc;
988 struct GNUNET_MESH_Multicast *mc;
989 struct GNUNET_MESH_ToOrigin *to;
991 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
992 uc = (struct GNUNET_MESH_Unicast *) cbuf;
996 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
997 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1001 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1002 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1010 t = tunnel_get (oid, ntohl (*tid));
1017 for (count = 0, c = clients; c != NULL; c = c->next)
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1020 if (client_is_subscribed (type, c))
1022 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1026 *tid = htonl (t->local_tid);
1030 if (GNUNET_NO == client_knows_tunnel (c, t))
1032 /* This client doesn't know the tunnel */
1033 struct GNUNET_MESH_TunnelNotification tmsg;
1034 GNUNET_HashCode hash;
1036 tmsg.header.size = htons (sizeof (tmsg));
1037 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1038 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1039 tmsg.tunnel_id = htonl (t->local_tid_dest);
1040 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1041 &tmsg.header, GNUNET_NO);
1042 GNUNET_array_append (t->clients, t->nclients, c);
1043 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1045 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1046 c->incoming_tunnels, &hash, t,
1047 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1049 *tid = htonl (t->local_tid_dest);
1052 /* Check if the client wants to get traffic from the tunnel */
1053 if (GNUNET_NO == client_wants_tunnel(c, t))
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1057 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1058 (struct GNUNET_MessageHeader
1059 *) cbuf, GNUNET_YES);
1067 * Notify the client that owns the tunnel that a peer has connected to it
1068 * (the requested path to it has been confirmed).
1070 * @param t Tunnel whose owner to notify
1071 * @param id Short id of the peer that has connected
1074 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1076 struct GNUNET_MESH_PeerControl pc;
1078 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1079 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1080 pc.tunnel_id = htonl (t->local_tid);
1081 GNUNET_PEER_resolve (id, &pc.peer);
1082 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1088 * Notify all clients (not depending on registration status) that the incoming
1089 * tunnel is no longer valid.
1091 * @param t Tunnel that was destroyed.
1094 send_clients_tunnel_destroy (struct MeshTunnel *t)
1096 struct GNUNET_MESH_TunnelMessage msg;
1098 msg.header.size = htons (sizeof (msg));
1099 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1100 msg.tunnel_id = htonl (t->local_tid_dest);
1101 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1106 * Notify clients of tunnel disconnections, if needed.
1107 * In case the origin disconnects, the destination clients get a tunnel destroy
1108 * notification. If the last destination disconnects (only one remaining client
1109 * in tunnel), the origin gets a (local ID) peer disconnected.
1110 * Note that the function must be called BEFORE removing the client from
1113 * @param t Tunnel that was destroyed.
1114 * @param c Client that disconnected.
1117 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1123 struct GNUNET_MESH_TunnelMessage msg;
1125 msg.header.size = htons (sizeof (msg));
1126 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1127 msg.tunnel_id = htonl (t->local_tid_dest);
1128 for (i = 0; i < t->nclients; i++)
1129 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1130 &msg.header, GNUNET_NO);
1132 // FIXME when to disconnect an incoming tunnel?
1133 else if (1 == t->nclients && NULL != t->owner)
1135 struct GNUNET_MESH_PeerControl msg;
1137 msg.header.size = htons (sizeof (msg));
1138 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1139 msg.tunnel_id = htonl (t->local_tid);
1140 msg.peer = my_full_id;
1141 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1142 &msg.header, GNUNET_NO);
1148 * Decrements the reference counter and frees all resources if needed
1150 * @param mesh_data Data Descriptor used in a multicast message.
1151 * Freed no longer needed (last message).
1154 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1156 /* Make sure it's a multicast packet */
1157 GNUNET_assert (NULL != mesh_data->reference_counter);
1159 if (0 == --(*(mesh_data->reference_counter)))
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1162 if (NULL != mesh_data->task)
1164 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1166 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1168 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1170 GNUNET_free (mesh_data->task);
1172 GNUNET_free (mesh_data->reference_counter);
1173 GNUNET_free (mesh_data->data);
1174 GNUNET_free (mesh_data);
1180 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1181 * and insert it in the appropiate structures if the peer is not known yet.
1183 * @param peer Full identity of the peer.
1185 * @return Existing or newly created peer info.
1187 static struct MeshPeerInfo *
1188 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1190 struct MeshPeerInfo *peer_info;
1192 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1193 if (NULL == peer_info)
1196 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1197 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1198 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1199 peer_info->id = GNUNET_PEER_intern (peer);
1207 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1208 * and insert it in the appropiate structures if the peer is not known yet.
1210 * @param peer Short identity of the peer.
1212 * @return Existing or newly created peer info.
1214 static struct MeshPeerInfo *
1215 peer_info_get_short (const GNUNET_PEER_Id peer)
1217 struct GNUNET_PeerIdentity id;
1219 GNUNET_PEER_resolve (peer, &id);
1220 return peer_info_get (&id);
1225 * Iterator to remove the tunnel from the list of tunnels a peer participates
1228 * @param cls Closure (tunnel info)
1229 * @param key GNUNET_PeerIdentity of the peer (unused)
1230 * @param value PeerInfo of the peer
1232 * @return always GNUNET_YES, to keep iterating
1235 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1237 struct MeshTunnel *t = cls;
1238 struct MeshPeerInfo *peer = value;
1241 for (i = 0; i < peer->ntunnels; i++)
1244 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1247 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1248 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1257 * Queue and pass message to core when possible.
1259 * @param cls Closure (type dependant).
1260 * @param type Type of the message.
1261 * @param size Size of the message.
1262 * @param dst Neighbor to send message to.
1263 * @param t Tunnel this message belongs to.
1266 queue_add (void *cls, uint16_t type, size_t size,
1267 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1270 * Core callback to write a pre-constructed data packet to core buffer
1272 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1273 * @param size Number of bytes available in buf.
1274 * @param buf Where the to write the message.
1276 * @return number of bytes written to buf
1279 send_core_data_raw (void *cls, size_t size, void *buf)
1281 struct MeshTransmissionDescriptor *info = cls;
1282 struct GNUNET_MessageHeader *msg;
1285 GNUNET_assert (NULL != info);
1286 GNUNET_assert (NULL != info->mesh_data);
1287 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1288 total_size = ntohs (msg->size);
1290 if (total_size > size)
1295 memcpy (buf, msg, total_size);
1296 GNUNET_free (info->mesh_data);
1303 * Sends an already built unicast message to a peer, properly registrating
1304 * all used resources.
1306 * @param message Message to send. Function makes a copy of it.
1307 * @param peer Short ID of the neighbor whom to send the message.
1308 * @param t Tunnel on which this message is transmitted.
1311 send_message (const struct GNUNET_MessageHeader *message,
1312 const struct GNUNET_PeerIdentity *peer,
1313 struct MeshTunnel *t)
1315 struct MeshTransmissionDescriptor *info;
1316 struct MeshPeerInfo *neighbor;
1317 struct MeshPeerPath *p;
1320 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1322 size = ntohs (message->size);
1323 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1324 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1325 info->mesh_data->data = GNUNET_malloc (size);
1326 memcpy (info->mesh_data->data, message, size);
1327 info->mesh_data->data_len = size;
1328 neighbor = peer_info_get (peer);
1329 for (p = neighbor->path_head; NULL != p; p = p->next)
1339 GNUNET_free (info->mesh_data->data);
1340 GNUNET_free (info->mesh_data);
1344 info->peer = neighbor;
1346 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1354 * Sends a CREATE PATH message for a path to a peer, properly registrating
1355 * all used resources.
1357 * @param peer PeerInfo of the final peer for whom this path is being created.
1358 * @param p Path itself.
1359 * @param t Tunnel for which the path is created.
1362 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1363 struct MeshTunnel *t)
1365 struct GNUNET_PeerIdentity id;
1366 struct MeshPathInfo *path_info;
1367 struct MeshPeerInfo *neighbor;
1373 p = tree_get_path_to_peer (t->tree, peer->id);
1380 for (i = 0; i < p->length; i++)
1382 if (p->peers[i] == myid)
1385 if (i >= p->length - 1)
1391 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1393 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1394 path_info->path = p;
1396 neighbor = peer_info_get (&id);
1397 path_info->peer = neighbor;
1398 queue_add (path_info,
1399 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1400 sizeof (struct GNUNET_MESH_ManipulatePath) +
1401 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1408 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1410 * @param t Tunnel whose path to destroy.
1411 * @param destination Short ID of the peer to whom the path to destroy.
1414 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1416 struct MeshPeerPath *p;
1419 p = tree_get_path_to_peer (t->tree, destination);
1425 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1426 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1428 struct GNUNET_MESH_ManipulatePath *msg;
1429 struct GNUNET_PeerIdentity *pi;
1433 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1434 msg->header.size = htons (size);
1435 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1436 msg->tid = htonl (t->id.tid);
1437 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1438 for (i = 0; i < p->length; i++)
1440 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1442 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1449 * Try to establish a new connection to this peer.
1450 * Use the best path for the given tunnel.
1451 * If the peer doesn't have any path to it yet, try to get one.
1452 * If the peer already has some path, send a CREATE PATH towards it.
1454 * @param peer PeerInfo of the peer.
1455 * @param t Tunnel for which to create the path, if possible.
1458 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1460 struct MeshPeerPath *p;
1461 struct MeshPathInfo *path_info;
1463 if (NULL != peer->path_head)
1465 p = tree_get_path_to_peer (t->tree, peer->id);
1472 // FIXME always send create path to self
1475 send_create_path (peer, p, t);
1479 GNUNET_HashCode hash;
1482 send_client_peer_connected (t, myid);
1483 t->local_tid_dest = next_local_tid++;
1484 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1487 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1488 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1495 else if (NULL == peer->dhtget)
1497 struct GNUNET_PeerIdentity id;
1499 GNUNET_PEER_resolve (peer->id, &id);
1500 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1501 path_info->peer = peer;
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1505 peer->dhtgetcls = path_info;
1506 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1507 GNUNET_BLOCK_TYPE_TEST, /* type */
1508 &id.hashPubKey, /* key to search */
1509 10, /* replication level */
1510 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1511 0, /* xquery bits */
1512 &dht_get_id_handler, path_info);
1514 /* Otherwise, there is no path but the DHT get is already started. */
1519 * Task to delay the connection of a peer
1521 * @param cls Closure (path info with tunnel and peer to connect).
1522 * Will be free'd on exection.
1523 * @param tc TaskContext
1526 peer_info_connect_task (void *cls,
1527 const struct GNUNET_SCHEDULER_TaskContext *tc)
1529 struct MeshPathInfo *path_info = cls;
1531 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1536 peer_info_connect (path_info->peer, path_info->t);
1542 * Destroy the peer_info and free any allocated resources linked to it
1544 * @param pi The peer_info to destroy.
1546 * @return GNUNET_OK on success
1549 peer_info_destroy (struct MeshPeerInfo *pi)
1551 struct GNUNET_PeerIdentity id;
1552 struct MeshPeerPath *p;
1553 struct MeshPeerPath *nextp;
1555 GNUNET_PEER_resolve (pi->id, &id);
1556 GNUNET_PEER_change_rc (pi->id, -1);
1559 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1562 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1563 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1565 if (NULL != pi->dhtget)
1567 GNUNET_DHT_get_stop (pi->dhtget);
1568 GNUNET_free (pi->dhtgetcls);
1574 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1584 * Remove all paths that rely on a direct connection between p1 and p2
1585 * from the peer itself and notify all tunnels about it.
1587 * @param peer PeerInfo of affected peer.
1588 * @param p1 GNUNET_PEER_Id of one peer.
1589 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1592 * TODO: optimize (see below)
1595 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1598 struct MeshPeerPath *p;
1599 struct MeshPeerPath *aux;
1600 struct MeshPeerInfo *peer_d;
1602 unsigned int destroyed;
1608 p = peer->path_head;
1612 for (i = 0; i < (p->length - 1); i++)
1614 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1615 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1617 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1628 for (i = 0; i < peer->ntunnels; i++)
1630 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1634 * Problem: one or more peers have been deleted from the tunnel tree.
1635 * We don't know who they are to try to add them again.
1636 * We need to try to find a new path for each of the disconnected peers.
1637 * Some of them might already have a path to reach them that does not
1638 * involve p1 and p2. Adding all anew might render in a better tree than
1639 * the trivial immediate fix.
1641 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1642 * its children will be reachable trough him.
1644 peer_d = peer_info_get_short (d);
1647 for (p = peer_d->path_head; NULL != p; p = p->next)
1649 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1657 /* No callback, as peer will be already disconnected and a connection
1658 * scheduled by tunnel_notify_connection_broken.
1660 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1664 peer_info_connect (peer_d, peer->tunnels[i]);
1671 * Add the path to the peer and update the path used to reach it in case this
1674 * @param peer_info Destination peer to add the path to.
1675 * @param path New path to add. Last peer must be the peer in arg 1.
1676 * Path will be either used of freed if already known.
1677 * @param trusted Do we trust that this path is real?
1680 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1683 struct MeshPeerPath *aux;
1687 if ((NULL == peer_info) || (NULL == path))
1690 path_destroy (path);
1693 if (path->peers[path->length - 1] != peer_info->id)
1696 path_destroy (path);
1699 if (path->length <= 2 && GNUNET_NO == trusted)
1701 /* Only allow CORE to tell us about direct paths */
1702 path_destroy (path);
1705 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1706 for (l = 1; l < path->length; l++)
1708 if (path->peers[l] == myid)
1710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1711 for (l2 = 0; l2 < path->length - l; l2++)
1713 path->peers[l2] = path->peers[l + l2];
1718 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1723 struct GNUNET_PeerIdentity id;
1725 GNUNET_PEER_resolve (peer_info->id, &id);
1726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1727 path->length, GNUNET_i2s (&id));
1730 l = path_get_length (path);
1737 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1738 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1740 l2 = path_get_length (aux);
1743 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1744 peer_info->path_tail, aux, path);
1749 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1751 path_destroy (path);
1756 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1763 * Add the path to the origin peer and update the path used to reach it in case
1764 * this is the shortest.
1765 * The path is given in peer_info -> destination, therefore we turn the path
1766 * upside down first.
1768 * @param peer_info Peer to add the path to, being the origin of the path.
1769 * @param path New path to add after being inversed.
1770 * @param trusted Do we trust that this path is real?
1773 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1774 struct MeshPeerPath *path, int trusted)
1777 peer_info_add_path (peer_info, path, trusted);
1782 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1783 * to obtain a local peer -> destination path and interning the peer ids.
1785 * @return Newly allocated and created path
1787 static struct MeshPeerPath *
1788 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1789 unsigned int get_path_length,
1790 const struct GNUNET_PeerIdentity *put_path,
1791 unsigned int put_path_length)
1793 struct MeshPeerPath *p;
1799 GNUNET_PEER_change_rc (myid, 1);
1800 i = get_path_length;
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1802 for (i--; i >= 0; i--)
1804 id = GNUNET_PEER_intern (&get_path[i]);
1805 if (p->length > 0 && id == p->peers[p->length - 1])
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1808 GNUNET_PEER_change_rc (id, -1);
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1813 GNUNET_i2s (&get_path[i]));
1815 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1816 p->peers[p->length - 1] = id;
1819 i = put_path_length;
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1821 for (i--; i >= 0; i--)
1823 id = GNUNET_PEER_intern (&put_path[i]);
1826 /* PUT path went through us, so discard the path up until now and start
1827 * from here to get a much shorter (and loop-free) path.
1832 if (p->length > 0 && id == p->peers[p->length - 1])
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1835 GNUNET_PEER_change_rc (id, -1);
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1840 GNUNET_i2s (&put_path[i]));
1842 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1843 p->peers[p->length - 1] = id;
1847 if (get_path_length > 0)
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1849 GNUNET_i2s (&get_path[0]));
1850 if (put_path_length > 0)
1851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1852 GNUNET_i2s (&put_path[0]));
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1855 for (i = 0; i < p->length; i++)
1857 struct GNUNET_PeerIdentity peer_id;
1859 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1861 GNUNET_i2s (&peer_id));
1869 * Adds a path to the peer_infos of all the peers in the path
1871 * @param p Path to process.
1872 * @param confirmed Whether we know if the path works or not. FIXME use
1875 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1879 /* TODO: invert and add */
1880 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1881 for (i++; i < p->length; i++)
1883 struct MeshPeerInfo *aux;
1884 struct MeshPeerPath *copy;
1886 aux = peer_info_get_short (p->peers[i]);
1887 copy = path_duplicate (p);
1888 copy->length = i + 1;
1889 peer_info_add_path (aux, copy, GNUNET_NO);
1895 * Send keepalive packets for a peer
1897 * @param cls Closure (tunnel for which to send the keepalive).
1898 * @param tc Notification context.
1900 * TODO: implement explicit multicast keepalive?
1903 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1907 * Search for a tunnel among the incoming tunnels
1909 * @param tid the local id of the tunnel
1911 * @return tunnel handler, NULL if doesn't exist
1913 static struct MeshTunnel *
1914 tunnel_get_incoming (MESH_TunnelNumber tid)
1916 GNUNET_HashCode hash;
1918 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1919 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1920 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1925 * Search for a tunnel among the tunnels for a client
1927 * @param c the client whose tunnels to search in
1928 * @param tid the local id of the tunnel
1930 * @return tunnel handler, NULL if doesn't exist
1932 static struct MeshTunnel *
1933 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1935 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1937 return tunnel_get_incoming (tid);
1941 GNUNET_HashCode hash;
1943 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1944 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1950 * Search for a tunnel by global ID using PEER_ID
1952 * @param pi owner of the tunnel
1953 * @param tid global tunnel number
1955 * @return tunnel handler, NULL if doesn't exist
1957 static struct MeshTunnel *
1958 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1960 struct MESH_TunnelID id;
1961 GNUNET_HashCode hash;
1966 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1967 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1972 * Search for a tunnel by global ID using full PeerIdentities
1974 * @param oid owner of the tunnel
1975 * @param tid global tunnel number
1977 * @return tunnel handler, NULL if doesn't exist
1979 static struct MeshTunnel *
1980 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1982 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1987 * Delete an active client from the tunnel.
1993 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
1997 for (i = 0; i < t->nclients; i++)
1999 if (t->clients[i] == c)
2001 t->clients[i] = t->clients[t->nclients - 1];
2002 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2010 * Delete an ignored client from the tunnel.
2016 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2020 for (i = 0; i < t->nignore; i++)
2022 if (t->ignore[i] == c)
2024 t->ignore[i] = t->ignore[t->nignore - 1];
2025 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2033 * Delete a client from the tunnel. It should be only done on
2034 * client disconnection, otherwise use client_ignore_tunnel.
2040 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2042 tunnel_delete_ignored_client (t, c);
2043 tunnel_delete_active_client (t, c);
2048 * Callback used to notify a client owner of a tunnel that a peer has
2049 * disconnected, most likely because of a path change.
2051 * @param cls Closure (tunnel this notification is about).
2052 * @param peer_id Short ID of disconnected peer.
2055 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2057 struct MeshTunnel *t = cls;
2058 struct MeshPeerInfo *peer;
2059 struct MeshPathInfo *path_info;
2061 if (NULL != t->owner && NULL != nc)
2063 struct GNUNET_MESH_PeerControl msg;
2065 msg.header.size = htons (sizeof (msg));
2066 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2067 msg.tunnel_id = htonl (t->local_tid);
2068 GNUNET_PEER_resolve (peer_id, &msg.peer);
2069 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2070 &msg.header, GNUNET_NO);
2072 peer = peer_info_get_short (peer_id);
2073 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2074 path_info->peer = peer;
2076 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2081 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2082 * needed rescources.
2083 * If peer already exists, reevaluate shortest path and change if different.
2085 * @param t Tunnel we want to add a new peer to
2086 * @param peer PeerInfo of the peer being added
2090 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2092 struct GNUNET_PeerIdentity id;
2093 struct MeshPeerPath *best_p;
2094 struct MeshPeerPath *p;
2095 unsigned int best_cost;
2098 GNUNET_PEER_resolve (peer->id, &id);
2100 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2103 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2104 GNUNET_assert (GNUNET_OK ==
2105 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2107 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2110 if (NULL != (p = peer->path_head))
2113 best_cost = tree_get_path_cost (t->tree, p);
2116 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2123 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2124 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2125 t->path_refresh_task =
2126 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2130 /* Start a DHT get */
2131 peer_info_connect (peer, t);
2136 * Add a path to a tunnel which we don't own, just to remember the next hop.
2137 * If destination node was already in the tunnel, the first hop information
2138 * will be replaced with the new path.
2140 * @param t Tunnel we want to add a new peer to
2141 * @param p Path to add
2142 * @param own_pos Position of local node in path.
2146 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2147 unsigned int own_pos)
2149 struct GNUNET_PeerIdentity id;
2151 GNUNET_assert (0 != own_pos);
2152 tree_add_path (t->tree, p, NULL, NULL);
2153 if (own_pos < p->length - 1)
2155 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2156 tree_update_first_hops (t->tree, myid, &id);
2162 * Notifies a tunnel that a connection has broken that affects at least
2163 * some of its peers. Sends a notification towards the root of the tree.
2164 * In case the peer is the owner of the tree, notifies the client that owns
2165 * the tunnel and tries to reconnect.
2167 * @param t Tunnel affected.
2168 * @param p1 Peer that got disconnected from p2.
2169 * @param p2 Peer that got disconnected from p1.
2171 * @return Short ID of the peer disconnected (either p1 or p2).
2172 * 0 if the tunnel remained unaffected.
2174 static GNUNET_PEER_Id
2175 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2181 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2183 if (myid != p1 && myid != p2)
2189 if (tree_get_predecessor (t->tree) != 0)
2191 /* We are the peer still connected, notify owner of the disconnection. */
2192 struct GNUNET_MESH_PathBroken msg;
2193 struct GNUNET_PeerIdentity neighbor;
2195 msg.header.size = htons (sizeof (msg));
2196 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2197 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2198 msg.tid = htonl (t->id.tid);
2199 msg.peer1 = my_full_id;
2200 GNUNET_PEER_resolve (pid, &msg.peer2);
2201 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2202 send_message (&msg.header, &neighbor, t);
2210 * Send a multicast packet to a neighbor.
2212 * @param cls Closure (Info about the multicast packet)
2213 * @param neighbor_id Short ID of the neighbor to send the packet to.
2216 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2218 struct MeshData *mdata = cls;
2219 struct MeshTransmissionDescriptor *info;
2220 struct GNUNET_PeerIdentity neighbor;
2222 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2224 info->mesh_data = mdata;
2225 (*(mdata->reference_counter)) ++;
2226 info->destination = neighbor_id;
2227 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2229 GNUNET_i2s (&neighbor));
2230 info->peer = peer_info_get (&neighbor);
2231 GNUNET_assert (NULL != info->peer);
2233 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2234 info->mesh_data->data_len,
2240 * Send a message in a tunnel in multicast, sending a copy to each child node
2241 * down the local one in the tunnel tree.
2243 * @param t Tunnel in which to send the data.
2244 * @param msg Message to be sent.
2245 * @param internal Has the service generated this message?
2248 tunnel_send_multicast (struct MeshTunnel *t,
2249 const struct GNUNET_MessageHeader *msg,
2252 struct MeshData *mdata;
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 " sending a multicast packet...\n");
2256 mdata = GNUNET_malloc (sizeof (struct MeshData));
2257 mdata->data_len = ntohs (msg->size);
2258 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2260 mdata->data = GNUNET_malloc (mdata->data_len);
2261 memcpy (mdata->data, msg, mdata->data_len);
2262 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2264 struct GNUNET_MESH_Multicast *mcast;
2266 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2267 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2269 ntohl (mcast->ttl));
2273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2275 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2276 && GNUNET_NO == internal)
2278 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2280 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2286 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2287 if (*(mdata->reference_counter) == 0)
2289 GNUNET_free (mdata->data);
2290 GNUNET_free (mdata->reference_counter);
2291 if (NULL != mdata->task)
2293 GNUNET_SCHEDULER_cancel(*(mdata->task));
2294 GNUNET_free (mdata->task);
2295 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2297 // FIXME change order?
2298 GNUNET_free (mdata);
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2301 " sending a multicast packet done\n");
2307 * Send a message to all peers in this tunnel that the tunnel is no longer
2310 * @param t The tunnel whose peers to notify.
2313 tunnel_send_destroy (struct MeshTunnel *t)
2315 struct GNUNET_MESH_TunnelDestroy msg;
2317 msg.header.size = htons (sizeof (msg));
2318 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2319 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2320 msg.tid = htonl (t->id.tid);
2321 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2327 * Destroy the tunnel and free any allocated resources linked to it.
2329 * @param t the tunnel to destroy
2331 * @return GNUNET_OK on success
2334 tunnel_destroy (struct MeshTunnel *t)
2336 struct MeshClient *c;
2337 GNUNET_HashCode hash;
2348 struct GNUNET_PeerIdentity id;
2350 GNUNET_PEER_resolve (t->id.oid, &id);
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2352 GNUNET_i2s (&id), t->id.tid);
2354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2358 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2359 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2364 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2367 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2371 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2372 for (i = 0; i < t->nclients; i++)
2376 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2381 for (i = 0; i < t->nignore; i++)
2385 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2390 if (t->nclients > 0)
2393 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2397 GNUNET_free (t->clients);
2399 if (NULL != t->peers)
2401 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2403 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2406 tree_destroy (t->tree);
2407 if (NULL != t->dht_get_type)
2408 GNUNET_DHT_get_stop (t->dht_get_type);
2409 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2410 GNUNET_SCHEDULER_cancel (t->timeout_task);
2411 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2412 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2419 * Create a new tunnel
2421 * @param owner Who is the owner of the tunnel (short ID).
2422 * @param id Tunnel Number of the tunnel.
2425 static struct MeshTunnel *
2426 tunnel_new (GNUNET_PEER_Id owner,
2427 MESH_TunnelNumber tid,
2428 struct MeshClient *client,
2429 MESH_TunnelNumber local)
2431 struct MeshTunnel *t;
2432 struct GNUNET_HashCode hash;
2434 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2437 t->queue_max = 1000; // FIXME API parameter
2438 t->tree = tree_new (owner);
2440 t->local_tid = local;
2442 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2444 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2445 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2450 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2456 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2458 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
2459 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2463 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
2473 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2474 * that are no longer needed, as well as nodes of no longer reachable peers.
2475 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2477 * @param t Tunnel from which to remove the path.
2478 * @param peer Short id of the peer which should be removed.
2481 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2483 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2489 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2490 * client when the client disconnects. If the client is not the owner, the
2491 * owner will get notified if no more clients are in the tunnel and the client
2492 * get removed from the tunnel's list.
2494 * @param cls closure (client that is disconnecting)
2495 * @param key the hash of the local tunnel id (used to access the hashmap)
2496 * @param value the value stored at the key (tunnel to destroy)
2498 * @return GNUNET_OK on success
2501 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2503 struct MeshTunnel *t = value;
2504 struct MeshClient *c = cls;
2507 send_client_tunnel_disconnect(t, c);
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2511 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2512 tunnel_delete_client(t, c);
2513 client_delete_tunnel(c, t);
2516 tunnel_send_destroy(t);
2517 r = tunnel_destroy (t);
2523 * Timeout function, destroys tunnel if called
2525 * @param cls Closure (tunnel to destroy).
2526 * @param tc TaskContext
2529 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2531 struct MeshTunnel *t = cls;
2533 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2535 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2540 * Resets the tunnel timeout. Starts it if no timeout was running.
2542 * @param t Tunnel whose timeout to reset.
2545 tunnel_reset_timeout (struct MeshTunnel *t)
2547 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2548 GNUNET_SCHEDULER_cancel (t->timeout_task);
2550 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2551 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2555 /******************************************************************************/
2556 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2557 /******************************************************************************/
2560 * Function to send a create path packet to a peer.
2562 * @param cls closure
2563 * @param size number of bytes available in buf
2564 * @param buf where the callee should write the message
2565 * @return number of bytes written to buf
2568 send_core_path_create (void *cls, size_t size, void *buf)
2570 struct MeshPathInfo *info = cls;
2571 struct GNUNET_MESH_ManipulatePath *msg;
2572 struct GNUNET_PeerIdentity *peer_ptr;
2573 struct MeshTunnel *t = info->t;
2574 struct MeshPeerPath *p = info->path;
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2580 sizeof (struct GNUNET_MESH_ManipulatePath) +
2581 p->length * sizeof (struct GNUNET_PeerIdentity);
2583 if (size < size_needed || NULL == buf)
2588 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2589 msg->header.size = htons (size_needed);
2590 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2591 msg->tid = ntohl (t->id.tid);
2593 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2594 for (i = 0; i < p->length; i++)
2596 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2603 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2609 * Fill the core buffer
2611 * @param cls closure (data itself)
2612 * @param size number of bytes available in buf
2613 * @param buf where the callee should write the message
2615 * @return number of bytes written to buf
2618 send_core_data_multicast (void *cls, size_t size, void *buf)
2620 struct MeshTransmissionDescriptor *info = cls;
2623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2624 GNUNET_assert (NULL != info);
2625 GNUNET_assert (NULL != info->peer);
2626 total_size = info->mesh_data->data_len;
2627 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2629 if (total_size > size)
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2635 memcpy (buf, info->mesh_data->data, total_size);
2638 struct GNUNET_MESH_Multicast *mc;
2639 struct GNUNET_MessageHeader *mh;
2642 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2644 mc = (struct GNUNET_MESH_Multicast *) mh;
2645 mh = (struct GNUNET_MessageHeader *) &mc[1];
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 " multicast, payload type %u\n", ntohs (mh->type));
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2649 " multicast, payload size %u\n", ntohs (mh->size));
2653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2658 data_descriptor_decrement_multicast (info->mesh_data);
2659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2667 * Creates a path ack message in buf and frees all unused resources.
2669 * @param cls closure (MeshTransmissionDescriptor)
2670 * @param size number of bytes available in buf
2671 * @param buf where the callee should write the message
2672 * @return number of bytes written to buf
2675 send_core_path_ack (void *cls, size_t size, void *buf)
2677 struct MeshTransmissionDescriptor *info = cls;
2678 struct GNUNET_MESH_PathACK *msg = buf;
2680 GNUNET_assert (NULL != info);
2681 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2686 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2687 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2688 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2689 msg->tid = htonl (info->origin->tid);
2690 msg->peer_id = my_full_id;
2693 /* TODO add signature */
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2696 return sizeof (struct GNUNET_MESH_PathACK);
2701 * Free a transmission that was already queued with all resources
2702 * associated to the request.
2704 * @param queue Queue handler to cancel.
2705 * @param clear_cls Is it necessary to free associated cls?
2708 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2710 struct MeshTransmissionDescriptor *dd;
2711 struct MeshPathInfo *path_info;
2713 if (GNUNET_YES == clear_cls)
2715 switch (queue->type)
2717 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2718 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
2721 data_descriptor_decrement_multicast (dd->mesh_data);
2723 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2725 path_info = queue->cls;
2726 path_destroy (path_info->path);
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
2732 GNUNET_free_non_null (queue->cls);
2734 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
2735 queue->peer->queue_tail,
2737 GNUNET_free (queue);
2742 * Core callback to write a queued packet to core buffer
2744 * @param cls Closure (peer info).
2745 * @param size Number of bytes available in buf.
2746 * @param buf Where the to write the message.
2748 * @return number of bytes written to buf
2751 queue_send (void *cls, size_t size, void *buf)
2753 struct MeshPeerInfo *peer = cls;
2754 struct MeshPeerQueue *queue;
2757 peer->core_transmit = NULL;
2758 queue = peer->queue_head;
2760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2763 /* If queue is empty, send should have been cancelled */
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2771 /* Check if buffer size is enough for the message */
2772 if (queue->size > size)
2774 struct GNUNET_PeerIdentity id;
2776 GNUNET_PEER_resolve (peer->id, &id);
2777 peer->core_transmit =
2778 GNUNET_CORE_notify_transmit_ready(core_handle,
2781 GNUNET_TIME_UNIT_FOREVER_REL,
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2791 switch (queue->type)
2793 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
2795 data_size = send_core_data_raw (queue->cls, size, buf);
2797 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
2799 data_size = send_core_data_multicast(queue->cls, size, buf);
2801 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2803 data_size = send_core_path_create(queue->cls, size, buf);
2805 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2807 data_size = send_core_path_ack(queue->cls, size, buf);
2811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
2815 /* Free queue, but cls was freed by send_core_* */
2816 queue_destroy(queue, GNUNET_NO);
2818 /* If more data in queue, send next */
2819 if (NULL != peer->queue_head)
2821 struct GNUNET_PeerIdentity id;
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2824 GNUNET_PEER_resolve (peer->id, &id);
2825 peer->core_transmit =
2826 GNUNET_CORE_notify_transmit_ready(core_handle,
2829 GNUNET_TIME_UNIT_FOREVER_REL,
2831 peer->queue_head->size,
2835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
2841 * Queue and pass message to core when possible.
2843 * @param cls Closure (type dependant).
2844 * @param type Type of the message.
2845 * @param size Size of the message.
2846 * @param dst Neighbor to send message to.
2847 * @param t Tunnel this message belongs to.
2850 queue_add (void *cls, uint16_t type, size_t size,
2851 struct MeshPeerInfo *dst, struct MeshTunnel *t)
2853 struct MeshPeerQueue *queue;
2855 if (t->queue_n >= t->queue_max)
2857 if (NULL == t->owner)
2858 GNUNET_break_op(0); // TODO: kill connection?
2861 return; // Drop message
2863 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2869 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
2870 if (NULL == dst->core_transmit)
2872 struct GNUNET_PeerIdentity id;
2874 GNUNET_PEER_resolve (dst->id, &id);
2875 dst->core_transmit =
2876 GNUNET_CORE_notify_transmit_ready(core_handle,
2879 GNUNET_TIME_UNIT_FOREVER_REL,
2888 /******************************************************************************/
2889 /******************** MESH NETWORK HANDLERS **************************/
2890 /******************************************************************************/
2894 * Core handler for path creation
2896 * @param cls closure
2897 * @param message message
2898 * @param peer peer identity this notification is about
2899 * @param atsi performance data
2900 * @param atsi_count number of records in 'atsi'
2902 * @return GNUNET_OK to keep the connection open,
2903 * GNUNET_SYSERR to close it (signal serious error)
2906 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2907 const struct GNUNET_MessageHeader *message,
2908 const struct GNUNET_ATS_Information *atsi,
2909 unsigned int atsi_count)
2911 unsigned int own_pos;
2914 MESH_TunnelNumber tid;
2915 struct GNUNET_MESH_ManipulatePath *msg;
2916 struct GNUNET_PeerIdentity *pi;
2917 GNUNET_HashCode hash;
2918 struct MeshPeerPath *path;
2919 struct MeshPeerInfo *dest_peer_info;
2920 struct MeshPeerInfo *orig_peer_info;
2921 struct MeshTunnel *t;
2923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2924 "Received a path create msg [%s]\n",
2925 GNUNET_i2s (&my_full_id));
2926 size = ntohs (message->size);
2927 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2929 GNUNET_break_op (0);
2933 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2934 if (size % sizeof (struct GNUNET_PeerIdentity))
2936 GNUNET_break_op (0);
2939 size /= sizeof (struct GNUNET_PeerIdentity);
2942 GNUNET_break_op (0);
2945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2946 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2948 tid = ntohl (msg->tid);
2949 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2951 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2952 t = tunnel_get (pi, tid);
2953 if (NULL == t) // FIXME only for INCOMING tunnels?
2955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2956 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
2958 while (NULL != tunnel_get_incoming (next_local_tid))
2959 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2960 t->local_tid_dest = next_local_tid++;
2961 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2963 tunnel_reset_timeout (t);
2964 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2966 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2975 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2976 if (NULL == dest_peer_info)
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2979 " Creating PeerInfo for destination.\n");
2980 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2981 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2982 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2984 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2986 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2987 if (NULL == orig_peer_info)
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2990 " Creating PeerInfo for origin.\n");
2991 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2992 orig_peer_info->id = GNUNET_PEER_intern (pi);
2993 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2994 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2997 path = path_new (size);
2999 for (i = 0; i < size; i++)
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3002 GNUNET_i2s (&pi[i]));
3003 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3004 if (path->peers[i] == myid)
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3010 /* cannot be self, must be 'not found' */
3011 /* create path: self not found in path through self */
3012 GNUNET_break_op (0);
3013 path_destroy (path);
3014 /* FIXME error. destroy tunnel? leave for timeout? */
3017 path_add_to_peers (path, GNUNET_NO);
3018 tunnel_add_path (t, path, own_pos);
3019 if (own_pos == size - 1)
3021 /* It is for us! Send ack. */
3022 struct MeshTransmissionDescriptor *info;
3024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3025 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
3026 if (NULL == t->peers)
3028 /* New tunnel! Notify clients on data. */
3029 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
3031 GNUNET_break (GNUNET_OK ==
3032 GNUNET_CONTAINER_multihashmap_put (t->peers,
3033 &my_full_id.hashPubKey,
3036 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
3037 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3038 info->origin = &t->id;
3039 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3040 GNUNET_assert (NULL != info->peer);
3042 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3043 sizeof (struct GNUNET_MESH_PathACK),
3049 struct MeshPeerPath *path2;
3051 /* It's for somebody else! Retransmit. */
3052 path2 = path_duplicate (path);
3053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3054 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3055 path2 = path_duplicate (path);
3056 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3057 send_create_path (dest_peer_info, path, t);
3064 * Core handler for path destruction
3066 * @param cls closure
3067 * @param message message
3068 * @param peer peer identity this notification is about
3069 * @param atsi performance data
3070 * @param atsi_count number of records in 'atsi'
3072 * @return GNUNET_OK to keep the connection open,
3073 * GNUNET_SYSERR to close it (signal serious error)
3076 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3077 const struct GNUNET_MessageHeader *message,
3078 const struct GNUNET_ATS_Information *atsi,
3079 unsigned int atsi_count)
3081 struct GNUNET_MESH_ManipulatePath *msg;
3082 struct GNUNET_PeerIdentity *pi;
3083 struct MeshPeerPath *path;
3084 struct MeshTunnel *t;
3085 unsigned int own_pos;
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3091 size = ntohs (message->size);
3092 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3094 GNUNET_break_op (0);
3098 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3099 if (size % sizeof (struct GNUNET_PeerIdentity))
3101 GNUNET_break_op (0);
3104 size /= sizeof (struct GNUNET_PeerIdentity);
3107 GNUNET_break_op (0);
3110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3112 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3113 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3115 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3117 t = tunnel_get (pi, ntohl (msg->tid));
3120 /* TODO notify back: we don't know this tunnel */
3121 GNUNET_break_op (0);
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3125 path = path_new (size);
3127 for (i = 0; i < size; i++)
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3130 GNUNET_i2s (&pi[i]));
3131 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3132 if (path->peers[i] == myid)
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3136 if (own_pos < path->length - 1)
3137 send_message (message, &pi[own_pos + 1], t);
3139 send_client_tunnel_disconnect(t, NULL);
3141 tunnel_delete_peer (t, path->peers[path->length - 1]);
3142 path_destroy (path);
3148 * Core handler for notifications of broken paths
3150 * @param cls closure
3151 * @param message message
3152 * @param peer peer identity this notification is about
3153 * @param atsi performance data
3154 * @param atsi_count number of records in 'atsi'
3156 * @return GNUNET_OK to keep the connection open,
3157 * GNUNET_SYSERR to close it (signal serious error)
3160 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3161 const struct GNUNET_MessageHeader *message,
3162 const struct GNUNET_ATS_Information *atsi,
3163 unsigned int atsi_count)
3165 struct GNUNET_MESH_PathBroken *msg;
3166 struct MeshTunnel *t;
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3170 msg = (struct GNUNET_MESH_PathBroken *) message;
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3172 GNUNET_i2s (&msg->peer1));
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3174 GNUNET_i2s (&msg->peer2));
3175 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3178 GNUNET_break_op (0);
3181 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3182 GNUNET_PEER_search (&msg->peer2));
3189 * Core handler for tunnel destruction
3191 * @param cls closure
3192 * @param message message
3193 * @param peer peer identity this notification is about
3194 * @param atsi performance data
3195 * @param atsi_count number of records in 'atsi'
3197 * @return GNUNET_OK to keep the connection open,
3198 * GNUNET_SYSERR to close it (signal serious error)
3201 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3202 const struct GNUNET_MessageHeader *message,
3203 const struct GNUNET_ATS_Information *atsi,
3204 unsigned int atsi_count)
3206 struct GNUNET_MESH_TunnelDestroy *msg;
3207 struct MeshTunnel *t;
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3211 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3213 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3214 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3217 /* Probably already got the message from another path,
3218 * destroyed the tunnel and retransmitted to children.
3223 if (t->id.oid == myid)
3225 GNUNET_break_op (0);
3228 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3230 /* Tunnel was incoming, notify clients */
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3232 t->local_tid, t->local_tid_dest);
3233 send_clients_tunnel_destroy (t);
3235 tunnel_send_destroy (t);
3242 * Core handler for mesh network traffic going from the origin to a peer
3244 * @param cls closure
3245 * @param peer peer identity this notification is about
3246 * @param message message
3247 * @param atsi performance data
3248 * @param atsi_count number of records in 'atsi'
3249 * @return GNUNET_OK to keep the connection open,
3250 * GNUNET_SYSERR to close it (signal serious error)
3253 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3254 const struct GNUNET_MessageHeader *message,
3255 const struct GNUNET_ATS_Information *atsi,
3256 unsigned int atsi_count)
3258 struct GNUNET_MESH_Unicast *msg;
3259 struct MeshTunnel *t;
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3265 size = ntohs (message->size);
3267 sizeof (struct GNUNET_MESH_Unicast) +
3268 sizeof (struct GNUNET_MessageHeader))
3273 msg = (struct GNUNET_MESH_Unicast *) message;
3274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3275 ntohs (msg[1].header.type));
3276 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3279 /* TODO notify back: we don't know this tunnel */
3280 GNUNET_break_op (0);
3283 tunnel_reset_timeout (t);
3284 pid = GNUNET_PEER_search (&msg->destination);
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 " it's for us! sending to clients...\n");
3289 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3293 " not for us, retransmitting...\n");
3294 send_message (message, tree_get_first_hop (t->tree, pid), t);
3300 * Core handler for mesh network traffic going from the origin to all peers
3302 * @param cls closure
3303 * @param message message
3304 * @param peer peer identity this notification is about
3305 * @param atsi performance data
3306 * @param atsi_count number of records in 'atsi'
3307 * @return GNUNET_OK to keep the connection open,
3308 * GNUNET_SYSERR to close it (signal serious error)
3310 * TODO: Check who we got this from, to validate route.
3313 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3314 const struct GNUNET_MessageHeader *message,
3315 const struct GNUNET_ATS_Information *atsi,
3316 unsigned int atsi_count)
3318 struct GNUNET_MESH_Multicast *msg;
3319 struct MeshTunnel *t;
3322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3324 size = ntohs (message->size);
3325 if (sizeof (struct GNUNET_MESH_Multicast) +
3326 sizeof (struct GNUNET_MessageHeader) > size)
3328 GNUNET_break_op (0);
3331 msg = (struct GNUNET_MESH_Multicast *) message;
3332 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3336 /* TODO notify that we dont know that tunnel */
3337 GNUNET_break_op (0);
3340 if (t->mid == ntohl (msg->mid))
3342 /* FIXME: already seen this packet, log dropping */
3343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3344 " Already seen mid %u, DROPPING!\n", t->mid);
3349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3350 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3352 t->mid = ntohl (msg->mid);
3353 tunnel_reset_timeout (t);
3355 /* Transmit to locally interested clients */
3356 if (NULL != t->peers &&
3357 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3359 send_subscribed_clients (message, &msg[1].header);
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3362 if (ntohl (msg->ttl) == 0)
3364 /* FIXME: ttl is 0, log dropping */
3365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3368 tunnel_send_multicast (t, message, GNUNET_NO);
3374 * Core handler for mesh network traffic toward the owner of a tunnel
3376 * @param cls closure
3377 * @param message message
3378 * @param peer peer identity this notification is about
3379 * @param atsi performance data
3380 * @param atsi_count number of records in 'atsi'
3382 * @return GNUNET_OK to keep the connection open,
3383 * GNUNET_SYSERR to close it (signal serious error)
3386 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3387 const struct GNUNET_MessageHeader *message,
3388 const struct GNUNET_ATS_Information *atsi,
3389 unsigned int atsi_count)
3391 struct GNUNET_MESH_ToOrigin *msg;
3392 struct GNUNET_PeerIdentity id;
3393 struct MeshPeerInfo *peer_info;
3394 struct MeshTunnel *t;
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3399 size = ntohs (message->size);
3400 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3401 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3403 GNUNET_break_op (0);
3406 msg = (struct GNUNET_MESH_ToOrigin *) message;
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3408 ntohs (msg[1].header.type));
3409 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3413 /* TODO notify that we dont know this tunnel (whom)? */
3414 GNUNET_break_op (0);
3418 if (t->id.oid == myid)
3421 struct GNUNET_MESH_ToOrigin *copy;
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3424 " it's for us! sending to clients...\n");
3425 if (NULL == t->owner)
3427 /* got data packet for ownerless tunnel */
3428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3429 GNUNET_break_op (0);
3432 /* TODO signature verification */
3433 memcpy (cbuf, message, size);
3434 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3435 copy->tid = htonl (t->local_tid);
3436 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3437 ©->header, GNUNET_YES);
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3441 " not for us, retransmitting...\n");
3443 peer_info = peer_info_get (&msg->oid);
3444 if (NULL == peer_info)
3446 /* unknown origin of tunnel */
3450 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3451 send_message (message, &id, t);
3458 * Core handler for path ACKs
3460 * @param cls closure
3461 * @param message message
3462 * @param peer peer identity this notification is about
3463 * @param atsi performance data
3464 * @param atsi_count number of records in 'atsi'
3466 * @return GNUNET_OK to keep the connection open,
3467 * GNUNET_SYSERR to close it (signal serious error)
3470 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3471 const struct GNUNET_MessageHeader *message,
3472 const struct GNUNET_ATS_Information *atsi,
3473 unsigned int atsi_count)
3475 struct GNUNET_MESH_PathACK *msg;
3476 struct GNUNET_PeerIdentity id;
3477 struct MeshPeerInfo *peer_info;
3478 struct MeshPeerPath *p;
3479 struct MeshTunnel *t;
3481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3482 GNUNET_i2s (&my_full_id));
3483 msg = (struct GNUNET_MESH_PathACK *) message;
3484 t = tunnel_get (&msg->oid, msg->tid);
3487 /* TODO notify that we don't know the tunnel */
3491 peer_info = peer_info_get (&msg->peer_id);
3493 /* Add paths to peers? */
3494 p = tree_get_path_to_peer (t->tree, peer_info->id);
3497 path_add_to_peers (p, GNUNET_YES);
3505 /* Message for us? */
3506 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3509 if (NULL == t->owner)
3511 GNUNET_break_op (0);
3514 if (NULL != t->dht_get_type)
3516 GNUNET_DHT_get_stop (t->dht_get_type);
3517 t->dht_get_type = NULL;
3519 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3521 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3522 send_client_peer_connected (t, peer_info->id);
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3528 " not for us, retransmitting...\n");
3529 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3530 peer_info = peer_info_get (&msg->oid);
3531 if (NULL == peer_info)
3533 /* If we know the tunnel, we should DEFINITELY know the peer */
3537 send_message (message, &id, t);
3543 * Functions to handle messages from core
3545 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3546 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3547 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3548 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3549 sizeof (struct GNUNET_MESH_PathBroken)},
3550 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3551 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3552 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3553 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3554 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3555 sizeof (struct GNUNET_MESH_PathACK)},
3561 /******************************************************************************/
3562 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3563 /******************************************************************************/
3566 * deregister_app: iterator for removing each application registered by a client
3568 * @param cls closure
3569 * @param key the hash of the application id (used to access the hashmap)
3570 * @param value the value stored at the key (client)
3572 * @return GNUNET_OK on success
3575 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3577 GNUNET_break (GNUNET_YES ==
3578 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3585 * notify_client_connection_failure: notify a client that the connection to the
3586 * requested remote peer is not possible (for instance, no route found)
3587 * Function called when the socket is ready to queue more data. "buf" will be
3588 * NULL and "size" zero if the socket was closed for writing in the meantime.
3590 * @param cls closure
3591 * @param size number of bytes available in buf
3592 * @param buf where the callee should write the message
3593 * @return number of bytes written to buf
3596 notify_client_connection_failure (void *cls, size_t size, void *buf)
3599 struct MeshPeerInfo *peer_info;
3600 struct GNUNET_MESH_PeerControl *msg;
3601 struct GNUNET_PeerIdentity id;
3603 if (0 == size && NULL == buf)
3605 // TODO retry? cancel?
3609 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3610 peer_info = (struct MeshPeerInfo *) cls;
3611 msg = (struct GNUNET_MESH_PeerControl *) buf;
3612 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3613 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3614 // msg->tunnel_id = htonl(peer_info->t->tid);
3615 GNUNET_PEER_resolve (peer_info->id, &id);
3616 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3624 * Send keepalive packets for a peer
3626 * @param cls Closure (tunnel for which to send the keepalive).
3627 * @param tc Notification context.
3629 * TODO: implement explicit multicast keepalive?
3632 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3634 struct MeshTunnel *t = cls;
3635 struct GNUNET_MessageHeader *payload;
3636 struct GNUNET_MESH_Multicast *msg;
3638 sizeof (struct GNUNET_MESH_Multicast) +
3639 sizeof (struct GNUNET_MessageHeader);
3642 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3646 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3649 "sending keepalive for tunnel %d\n", t->id.tid);
3651 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3652 msg->header.size = htons (size);
3653 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3654 msg->oid = my_full_id;
3655 msg->tid = htonl (t->id.tid);
3656 msg->ttl = htonl (DEFAULT_TTL);
3657 msg->mid = htonl (t->mid + 1);
3659 payload = (struct GNUNET_MessageHeader *) &msg[1];
3660 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3661 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3662 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3664 t->path_refresh_task =
3665 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3671 * Function to process paths received for a new peer addition. The recorded
3672 * paths form the initial tunnel, which can be optimized later.
3673 * Called on each result obtained for the DHT search.
3675 * @param cls closure
3676 * @param exp when will this value expire
3677 * @param key key of the result
3678 * @param get_path path of the get request
3679 * @param get_path_length lenght of get_path
3680 * @param put_path path of the put request
3681 * @param put_path_length length of the put_path
3682 * @param type type of the result
3683 * @param size number of bytes in data
3684 * @param data pointer to the result data
3686 * TODO: re-issue the request after certain time? cancel after X results?
3689 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3690 const GNUNET_HashCode * key,
3691 const struct GNUNET_PeerIdentity *get_path,
3692 unsigned int get_path_length,
3693 const struct GNUNET_PeerIdentity *put_path,
3694 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3695 size_t size, const void *data)
3697 struct MeshPathInfo *path_info = cls;
3698 struct MeshPeerPath *p;
3699 struct GNUNET_PeerIdentity pi;
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3703 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3706 p = path_build_from_dht (get_path, get_path_length, put_path,
3708 path_add_to_peers (p, GNUNET_NO);
3710 for (i = 0; i < path_info->peer->ntunnels; i++)
3712 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3713 peer_info_connect (path_info->peer, path_info->t);
3721 * Function to process paths received for a new peer addition. The recorded
3722 * paths form the initial tunnel, which can be optimized later.
3723 * Called on each result obtained for the DHT search.
3725 * @param cls closure
3726 * @param exp when will this value expire
3727 * @param key key of the result
3728 * @param get_path path of the get request
3729 * @param get_path_length lenght of get_path
3730 * @param put_path path of the put request
3731 * @param put_path_length length of the put_path
3732 * @param type type of the result
3733 * @param size number of bytes in data
3734 * @param data pointer to the result data
3737 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3738 const GNUNET_HashCode * key,
3739 const struct GNUNET_PeerIdentity *get_path,
3740 unsigned int get_path_length,
3741 const struct GNUNET_PeerIdentity *put_path,
3742 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3743 size_t size, const void *data)
3745 const struct GNUNET_PeerIdentity *pi = data;
3746 struct MeshTunnel *t = cls;
3747 struct MeshPeerInfo *peer_info;
3748 struct MeshPeerPath *p;
3750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3751 if (size != sizeof (struct GNUNET_PeerIdentity))
3753 GNUNET_break_op (0);
3756 GNUNET_assert (NULL != t->owner);
3757 peer_info = peer_info_get (pi);
3758 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3760 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3762 p = path_build_from_dht (get_path, get_path_length, put_path,
3764 path_add_to_peers (p, GNUNET_NO);
3766 tunnel_add_peer (t, peer_info);
3767 peer_info_connect (peer_info, t);
3771 /******************************************************************************/
3772 /********************* MESH LOCAL HANDLES **************************/
3773 /******************************************************************************/
3777 * Handler for client disconnection
3779 * @param cls closure
3780 * @param client identification of the client; NULL
3781 * for the last call when the server is destroyed
3784 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3786 struct MeshClient *c;
3787 struct MeshClient *next;
3789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3798 if (c->handle != client)
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3806 GNUNET_SERVER_client_drop (c->handle);
3807 c->shutting_down = GNUNET_YES;
3808 GNUNET_assert (NULL != c->own_tunnels);
3809 GNUNET_assert (NULL != c->incoming_tunnels);
3810 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3811 &tunnel_destroy_iterator, c);
3812 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3813 &tunnel_destroy_iterator, c);
3814 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3815 &tunnel_destroy_iterator, c);
3816 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3817 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3818 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3820 /* deregister clients applications */
3821 if (NULL != c->apps)
3823 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3824 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3826 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3827 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3829 GNUNET_SCHEDULER_cancel (announce_applications_task);
3830 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3832 if (NULL != c->types)
3833 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3835 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3846 * Handler for new clients
3848 * @param cls closure
3849 * @param client identification of the client
3850 * @param message the actual message, which includes messages the client wants
3853 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3854 const struct GNUNET_MessageHeader *message)
3856 struct GNUNET_MESH_ClientConnect *cc_msg;
3857 struct MeshClient *c;
3858 GNUNET_MESH_ApplicationType *a;
3865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3866 /* Check data sanity */
3867 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3868 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3869 ntypes = ntohs (cc_msg->types);
3870 napps = ntohs (cc_msg->applications);
3872 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3875 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3879 /* Create new client structure */
3880 c = GNUNET_malloc (sizeof (struct MeshClient));
3881 c->id = next_client_id++;
3882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3884 GNUNET_SERVER_client_keep (client);
3885 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3888 GNUNET_MESH_ApplicationType at;
3891 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3892 for (i = 0; i < napps; i++)
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3896 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3897 /* store in clients hashmap */
3898 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3899 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3900 /* store in global hashmap, for announcements */
3901 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3902 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3904 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3905 announce_applications_task =
3906 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3914 t = (uint16_t *) & a[napps];
3915 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3916 for (i = 0; i < ntypes; i++)
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3920 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3922 /* store in clients hashmap */
3923 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3924 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3925 /* store in global hashmap */
3926 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3927 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3931 " client has %u+%u subscriptions\n", napps, ntypes);
3933 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3934 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3935 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3936 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3937 GNUNET_SERVER_notification_context_add (nc, client);
3939 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3945 * Handler for requests of new tunnels
3947 * @param cls closure
3948 * @param client identification of the client
3949 * @param message the actual message
3952 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3953 const struct GNUNET_MessageHeader *message)
3955 struct GNUNET_MESH_TunnelMessage *t_msg;
3956 struct MeshTunnel *t;
3957 struct MeshClient *c;
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3961 /* Sanity check for client registration */
3962 if (NULL == (c = client_get (client)))
3965 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3970 /* Message sanity check */
3971 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3978 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3979 /* Sanity check for tunnel numbering */
3980 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3986 /* Sanity check for duplicate tunnel IDs */
3987 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3994 while (NULL != tunnel_get_by_pi (myid, next_tid))
3995 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3996 t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
3997 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3999 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
4000 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
4002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
4003 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4009 * Handler for requests of deleting tunnels
4011 * @param cls closure
4012 * @param client identification of the client
4013 * @param message the actual message
4016 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4017 const struct GNUNET_MessageHeader *message)
4019 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4020 struct MeshClient *c;
4021 struct MeshTunnel *t;
4022 MESH_TunnelNumber tid;
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4025 "Got a DESTROY TUNNEL from client!\n");
4027 /* Sanity check for client registration */
4028 if (NULL == (c = client_get (client)))
4031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4034 /* Message sanity check */
4035 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4042 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4044 /* Retrieve tunnel */
4045 tid = ntohl (tunnel_msg->tunnel_id);
4046 t = tunnel_get_by_local_id(c, tid);
4050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4051 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4054 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4056 client_ignore_tunnel (c, t);
4058 // TODO: when to destroy incoming tunnel?
4059 if (t->nclients == 0)
4061 GNUNET_assert (GNUNET_YES ==
4062 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4064 GNUNET_assert (GNUNET_YES ==
4065 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4066 &my_full_id.hashPubKey,
4070 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4073 send_client_tunnel_disconnect(t, c);
4074 client_delete_tunnel(c, t);
4076 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4078 tunnel_send_destroy (t);
4080 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4086 * Handler for connection requests to new peers
4088 * @param cls closure
4089 * @param client identification of the client
4090 * @param message the actual message (PeerControl)
4093 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4094 const struct GNUNET_MessageHeader *message)
4096 struct GNUNET_MESH_PeerControl *peer_msg;
4097 struct MeshPeerInfo *peer_info;
4098 struct MeshClient *c;
4099 struct MeshTunnel *t;
4100 MESH_TunnelNumber tid;
4102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4103 /* Sanity check for client registration */
4104 if (NULL == (c = client_get (client)))
4107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4111 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4112 /* Sanity check for message size */
4113 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4120 /* Tunnel exists? */
4121 tid = ntohl (peer_msg->tunnel_id);
4122 t = tunnel_get_by_local_id (c, tid);
4126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4130 /* Does client own tunnel? */
4131 if (t->owner->handle != client)
4134 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4138 GNUNET_i2s (&peer_msg->peer));
4139 peer_info = peer_info_get (&peer_msg->peer);
4141 tunnel_add_peer (t, peer_info);
4142 peer_info_connect (peer_info, t);
4144 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4150 * Handler for disconnection requests of peers in a tunnel
4152 * @param cls closure
4153 * @param client identification of the client
4154 * @param message the actual message (PeerControl)
4157 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4158 const struct GNUNET_MessageHeader *message)
4160 struct GNUNET_MESH_PeerControl *peer_msg;
4161 struct MeshPeerInfo *peer_info;
4162 struct MeshClient *c;
4163 struct MeshTunnel *t;
4164 MESH_TunnelNumber tid;
4166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4167 /* Sanity check for client registration */
4168 if (NULL == (c = client_get (client)))
4171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4174 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4175 /* Sanity check for message size */
4176 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4183 /* Tunnel exists? */
4184 tid = ntohl (peer_msg->tunnel_id);
4185 t = tunnel_get_by_local_id (c, tid);
4189 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4194 /* Does client own tunnel? */
4195 if (t->owner->handle != client)
4198 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4203 GNUNET_i2s (&peer_msg->peer));
4204 /* Is the peer in the tunnel? */
4206 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4207 if (NULL == peer_info)
4210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4214 /* Ok, delete peer from tunnel */
4215 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4216 &peer_msg->peer.hashPubKey);
4218 send_destroy_path (t, peer_info->id);
4219 tunnel_delete_peer (t, peer_info->id);
4220 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4226 * Handler for connection requests to new peers by type
4228 * @param cls closure
4229 * @param client identification of the client
4230 * @param message the actual message (ConnectPeerByType)
4233 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4234 const struct GNUNET_MessageHeader *message)
4236 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4237 struct MeshClient *c;
4238 struct MeshTunnel *t;
4239 GNUNET_HashCode hash;
4240 MESH_TunnelNumber tid;
4242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4243 /* Sanity check for client registration */
4244 if (NULL == (c = client_get (client)))
4247 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4251 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4252 /* Sanity check for message size */
4253 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4254 ntohs (connect_msg->header.size))
4257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4261 /* Tunnel exists? */
4262 tid = ntohl (connect_msg->tunnel_id);
4263 t = tunnel_get_by_local_id (c, tid);
4267 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4271 /* Does client own tunnel? */
4272 if (t->owner->handle != client)
4275 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4279 /* Do WE have the service? */
4280 t->type = ntohl (connect_msg->type);
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4282 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4283 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4286 /* Yes! Fast forward, add ourselves to the tunnel and send the
4287 * good news to the client, and alert the destination client of
4288 * an incoming tunnel.
4290 * FIXME send a path create to self, avoid code duplication
4292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4293 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4294 peer_info_get (&my_full_id),
4295 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4298 send_client_peer_connected (t, myid);
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4300 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4302 t->local_tid_dest = next_local_tid++;
4303 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4304 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4305 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4309 /* Ok, lets find a peer offering the service */
4310 if (NULL != t->dht_get_type)
4312 GNUNET_DHT_get_stop (t->dht_get_type);
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4315 GNUNET_h2s (&hash));
4317 GNUNET_DHT_get_start (dht_handle,
4318 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4319 GNUNET_DHT_RO_RECORD_ROUTE |
4320 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4321 &dht_get_type_handler, t);
4323 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4329 * Handler for client traffic directed to one peer
4331 * @param cls closure
4332 * @param client identification of the client
4333 * @param message the actual message
4336 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4337 const struct GNUNET_MessageHeader *message)
4339 struct MeshClient *c;
4340 struct MeshTunnel *t;
4341 struct MeshPeerInfo *pi;
4342 struct GNUNET_MESH_Unicast *data_msg;
4343 MESH_TunnelNumber tid;
4346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4347 "Got a unicast request from a client!\n");
4349 /* Sanity check for client registration */
4350 if (NULL == (c = client_get (client)))
4353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4356 data_msg = (struct GNUNET_MESH_Unicast *) message;
4357 /* Sanity check for message size */
4358 size = ntohs (message->size);
4359 if (sizeof (struct GNUNET_MESH_Unicast) +
4360 sizeof (struct GNUNET_MessageHeader) > size)
4363 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4367 /* Tunnel exists? */
4368 tid = ntohl (data_msg->tid);
4369 t = tunnel_get_by_local_id (c, tid);
4373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4377 /* Is it a local tunnel? Then, does client own the tunnel? */
4378 if (t->owner->handle != client)
4381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4385 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4386 &data_msg->destination.hashPubKey);
4387 /* Is the selected peer in the tunnel? */
4391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4395 /* Ok, everything is correct, send the message
4396 * (pretend we got it from a mesh peer)
4399 char buf[ntohs (message->size)] GNUNET_ALIGN;
4400 struct GNUNET_MESH_Unicast *copy;
4402 /* Work around const limitation */
4403 copy = (struct GNUNET_MESH_Unicast *) buf;
4404 memcpy (buf, data_msg, size);
4405 copy->oid = my_full_id;
4406 copy->tid = htonl (t->id.tid);
4407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4408 " calling generic handler...\n");
4409 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4411 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4417 * Handler for client traffic directed to the origin
4419 * @param cls closure
4420 * @param client identification of the client
4421 * @param message the actual message
4424 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4425 const struct GNUNET_MessageHeader *message)
4427 struct GNUNET_MESH_ToOrigin *data_msg;
4428 struct GNUNET_PeerIdentity id;
4429 struct MeshClient *c;
4430 struct MeshTunnel *t;
4431 MESH_TunnelNumber tid;
4434 /* Sanity check for client registration */
4435 if (NULL == (c = client_get (client)))
4438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4441 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4442 /* Sanity check for message size */
4443 size = ntohs (message->size);
4444 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4445 sizeof (struct GNUNET_MessageHeader) > size)
4448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4452 /* Tunnel exists? */
4453 tid = ntohl (data_msg->tid);
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4455 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4456 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4462 t = tunnel_get_by_local_id (c, tid);
4466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4470 /* It should be sent by someone who has this as incoming tunnel. */
4471 if (-1 == client_knows_tunnel (c, t))
4474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4477 GNUNET_PEER_resolve (t->id.oid, &id);
4479 /* Ok, everything is correct, send the message
4480 * (pretend we got it from a mesh peer)
4483 char buf[ntohs (message->size)] GNUNET_ALIGN;
4484 struct GNUNET_MESH_ToOrigin *copy;
4486 /* Work around const limitation */
4487 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4488 memcpy (buf, data_msg, size);
4490 copy->tid = htonl (t->id.tid);
4491 copy->sender = my_full_id;
4492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4493 " calling generic handler...\n");
4494 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4496 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4502 * Handler for client traffic directed to all peers in a tunnel
4504 * @param cls closure
4505 * @param client identification of the client
4506 * @param message the actual message
4509 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4510 const struct GNUNET_MessageHeader *message)
4512 struct MeshClient *c;
4513 struct MeshTunnel *t;
4514 struct GNUNET_MESH_Multicast *data_msg;
4515 MESH_TunnelNumber tid;
4517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4518 "Got a multicast request from a client!\n");
4520 /* Sanity check for client registration */
4521 if (NULL == (c = client_get (client)))
4524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4527 data_msg = (struct GNUNET_MESH_Multicast *) message;
4528 /* Sanity check for message size */
4529 if (sizeof (struct GNUNET_MESH_Multicast) +
4530 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4537 /* Tunnel exists? */
4538 tid = ntohl (data_msg->tid);
4539 t = tunnel_get_by_local_id (c, tid);
4543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4547 /* Does client own tunnel? */
4548 if (t->owner->handle != client)
4551 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4556 char buf[ntohs (message->size)] GNUNET_ALIGN;
4557 struct GNUNET_MESH_Multicast *copy;
4559 copy = (struct GNUNET_MESH_Multicast *) buf;
4560 memcpy (buf, message, ntohs (message->size));
4561 copy->oid = my_full_id;
4562 copy->tid = htonl (t->id.tid);
4563 copy->ttl = htonl (DEFAULT_TTL);
4564 copy->mid = htonl (t->mid + 1);
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 " calling generic handler...\n");
4567 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4570 /* receive done gets called when last copy is sent to a neighbor */
4575 * Functions to handle messages from clients
4577 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4578 {&handle_local_new_client, NULL,
4579 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4580 {&handle_local_tunnel_create, NULL,
4581 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4582 sizeof (struct GNUNET_MESH_TunnelMessage)},
4583 {&handle_local_tunnel_destroy, NULL,
4584 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4585 sizeof (struct GNUNET_MESH_TunnelMessage)},
4586 {&handle_local_connect_add, NULL,
4587 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4588 sizeof (struct GNUNET_MESH_PeerControl)},
4589 {&handle_local_connect_del, NULL,
4590 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4591 sizeof (struct GNUNET_MESH_PeerControl)},
4592 {&handle_local_connect_by_type, NULL,
4593 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4594 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4595 {&handle_local_unicast, NULL,
4596 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4597 {&handle_local_to_origin, NULL,
4598 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4599 {&handle_local_multicast, NULL,
4600 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4606 * To be called on core init/fail.
4608 * @param cls service closure
4609 * @param server handle to the server for this service
4610 * @param identity the public identity of this peer
4613 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4614 const struct GNUNET_PeerIdentity *identity)
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4617 core_handle = server;
4618 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4622 GNUNET_SCHEDULER_shutdown ();
4628 * Method called whenever a given peer connects.
4630 * @param cls closure
4631 * @param peer peer identity this notification is about
4632 * @param atsi performance data for the connection
4633 * @param atsi_count number of records in 'atsi'
4636 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4637 const struct GNUNET_ATS_Information *atsi,
4638 unsigned int atsi_count)
4640 struct MeshPeerInfo *peer_info;
4641 struct MeshPeerPath *path;
4643 DEBUG_CONN ("Peer connected\n");
4644 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4645 peer_info = peer_info_get (peer);
4646 if (myid == peer_info->id)
4648 DEBUG_CONN (" (self)\n");
4653 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4655 path = path_new (2);
4656 path->peers[0] = myid;
4657 path->peers[1] = peer_info->id;
4658 GNUNET_PEER_change_rc (myid, 1);
4659 GNUNET_PEER_change_rc (peer_info->id, 1);
4660 peer_info_add_path (peer_info, path, GNUNET_YES);
4665 * Method called whenever a peer disconnects.
4667 * @param cls closure
4668 * @param peer peer identity this notification is about
4671 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4673 struct MeshPeerInfo *pi;
4674 struct MeshPeerQueue *q;
4675 struct MeshPeerQueue *n;
4677 DEBUG_CONN ("Peer disconnected\n");
4678 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4690 /* try to reroute this traffic instead */
4691 queue_destroy(q, GNUNET_YES);
4695 peer_info_remove_path (pi, pi->id, myid);
4698 DEBUG_CONN (" (self)\n");
4704 /******************************************************************************/
4705 /************************ MAIN FUNCTIONS ****************************/
4706 /******************************************************************************/
4709 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4711 * @param cls closure
4712 * @param key current key code
4713 * @param value value in the hash map
4714 * @return GNUNET_YES if we should continue to iterate,
4718 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4720 struct MeshTunnel *t = value;
4727 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4729 * @param cls closure
4730 * @param key current key code
4731 * @param value value in the hash map
4732 * @return GNUNET_YES if we should continue to iterate,
4736 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4738 struct MeshPeerInfo *p = value;
4739 struct MeshPeerQueue *q;
4740 struct MeshPeerQueue *n;
4748 queue_destroy(q, GNUNET_YES);
4752 peer_info_destroy (p);
4757 * Task run during shutdown.
4763 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4767 if (core_handle != NULL)
4769 GNUNET_CORE_disconnect (core_handle);
4772 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4773 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4774 if (dht_handle != NULL)
4776 GNUNET_DHT_disconnect (dht_handle);
4781 GNUNET_SERVER_notification_context_destroy (nc);
4784 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4786 GNUNET_SCHEDULER_cancel (announce_id_task);
4787 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4793 * Process mesh requests.
4795 * @param cls closure
4796 * @param server the initialized server
4797 * @param c configuration to use
4800 run (void *cls, struct GNUNET_SERVER_Handle *server,
4801 const struct GNUNET_CONFIGURATION_Handle *c)
4803 struct MeshPeerInfo *peer;
4804 struct MeshPeerPath *p;
4807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4808 server_handle = server;
4809 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4810 NULL, /* Closure passed to MESH functions */
4811 &core_init, /* Call core_init once connected */
4812 &core_connect, /* Handle connects */
4813 &core_disconnect, /* remove peers on disconnects */
4814 NULL, /* Don't notify about all incoming messages */
4815 GNUNET_NO, /* For header only in notification */
4816 NULL, /* Don't notify about all outbound messages */
4817 GNUNET_NO, /* For header-only out notification */
4818 core_handlers); /* Register these handlers */
4820 if (core_handle == NULL)
4823 GNUNET_SCHEDULER_shutdown ();
4828 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4831 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4833 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4834 GNUNET_SCHEDULER_shutdown ();
4837 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4838 GNUNET_free (keyfile);
4839 if (my_private_key == NULL)
4841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4842 _("Mesh service could not access hostkey. Exiting.\n"));
4843 GNUNET_SCHEDULER_shutdown ();
4846 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4847 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4848 &my_full_id.hashPubKey);
4849 myid = GNUNET_PEER_intern (&my_full_id);
4851 // transport_handle = GNUNET_TRANSPORT_connect(c,
4858 dht_handle = GNUNET_DHT_connect (c, 64);
4859 if (dht_handle == NULL)
4865 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4867 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4868 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4869 peers = GNUNET_CONTAINER_multihashmap_create (32);
4870 applications = GNUNET_CONTAINER_multihashmap_create (32);
4871 types = GNUNET_CONTAINER_multihashmap_create (32);
4873 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4874 nc = GNUNET_SERVER_notification_context_create (server_handle,
4876 GNUNET_SERVER_disconnect_notify (server_handle,
4877 &handle_local_client_disconnect, NULL);
4881 clients_tail = NULL;
4884 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4885 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4887 /* Create a peer_info for the local peer */
4888 peer = peer_info_get (&my_full_id);
4891 GNUNET_PEER_change_rc (myid, 1);
4892 peer_info_add_path (peer, p, GNUNET_YES);
4894 /* Scheduled the task to clean up when shutdown is called */
4895 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4902 * The main function for the mesh service.
4904 * @param argc number of arguments from the command line
4905 * @param argv command line arguments
4906 * @return 0 ok, 1 on error
4909 main (int argc, char *const *argv)
4913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4916 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");