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 * Pointer to info stucture used as cls.
150 * Size of the message
157 * Struct containing all info possibly needed to build a package when called
160 struct MeshTransmissionDescriptor
162 /** ID of the tunnel this packet travels in */
163 struct MESH_TunnelID *origin;
165 /** Who was this message being sent to */
166 struct MeshPeerInfo *peer;
168 /** Ultimate destination of the packet */
169 GNUNET_PEER_Id destination;
171 /** Data descriptor */
172 struct MeshData* mesh_data;
177 * Struct containing all information regarding a given peer
187 * Last time we heard from this peer
189 struct GNUNET_TIME_Absolute last_contact;
192 * Number of attempts to reconnect so far
194 int n_reconnect_attempts;
197 * Paths to reach the peer, ordered by ascending hop count
199 struct MeshPeerPath *path_head;
202 * Paths to reach the peer, ordered by ascending hop count
204 struct MeshPeerPath *path_tail;
207 * Handle to stop the DHT search for a path to this peer
209 struct GNUNET_DHT_GetHandle *dhtget;
212 * Closure given to the DHT GET
214 struct MeshPathInfo *dhtgetcls;
217 * Array of tunnels this peer participates in
218 * (most probably a small amount, therefore not a hashmap)
219 * When the path to the peer changes, notify these tunnels to let them
220 * re-adjust their path trees.
222 struct MeshTunnel **tunnels;
225 * Number of tunnels this peers participates in
227 unsigned int ntunnels;
232 * Data scheduled to transmit (to local client or remote peer)
239 struct MeshQueue *next;
240 struct MeshQueue *prev;
243 * Target of the data (NULL if target is client)
245 struct MeshPeerInfo *peer;
248 * Client to send the data to (NULL if target is peer)
250 struct MeshClient *client;
253 * Size of the message to transmit
258 * How old is the data?
260 struct GNUNET_TIME_Absolute timestamp;
265 struct GNUNET_MessageHeader *data;
269 * Globally unique tunnel identification (owner + number)
270 * DO NOT USE OVER THE NETWORK
275 * Node that owns the tunnel
280 * Tunnel number to differentiate all the tunnels owned by the node oid
281 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
283 MESH_TunnelNumber tid;
287 struct MeshClient; /* FWD declaration */
290 * Struct containing all information regarding a tunnel
291 * For an intermediate node the improtant info used will be:
292 * - id Tunnel unique identification
293 * - paths[0] To know where to send it next
294 * - metainfo: ready, speeds, accounting
301 struct MESH_TunnelID id;
304 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
306 MESH_TunnelNumber local_tid;
309 * Local tunnel number for local destination clients (incoming number)
310 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
313 MESH_TunnelNumber local_tid_dest;
316 * ID of the last multicast packet seen/sent.
321 * Last time the tunnel was used
323 struct GNUNET_TIME_Absolute timestamp;
326 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
327 * containing peers added by id or by type, not intermediate peers.
329 struct GNUNET_CONTAINER_MultiHashMap *peers;
332 * Number of peers that are connected and potentially ready to receive data
334 unsigned int peers_ready;
337 * Number of peers that have been added to the tunnel
339 unsigned int peers_total;
342 * Client owner of the tunnel, if any
344 struct MeshClient *owner;
347 * Clients that have been informed about the tunnel, if any
349 struct MeshClient **clients;
352 * Number of elements in clients
354 unsigned int nclients;
357 * Clients that have requested to leave the tunnel
359 struct MeshClient **ignore;
362 * Number of elements in clients
364 unsigned int nignore;
367 * Messages ready to transmit
369 struct MeshQueue *queue_head;
370 struct MeshQueue *queue_tail;
375 struct MeshTunnelTree *tree;
378 * Application type we are looking for in this tunnel
380 GNUNET_MESH_ApplicationType type;
383 * Used to search peers offering a service
385 struct GNUNET_DHT_GetHandle *dht_get_type;
388 * Task to keep the used paths alive
390 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
393 * Task to destroy the tunnel after timeout
395 * FIXME: merge the two? a tunnel will have either
396 * a path refresh OR a timeout, never both!
398 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
403 * Info needed to work with tunnel paths and peers
410 struct MeshTunnel *t;
413 * Neighbouring peer to whom we send the packet to
415 struct MeshPeerInfo *peer;
420 struct MeshPeerPath *path;
425 * Struct containing information about a client of the service
432 struct MeshClient *next;
437 struct MeshClient *prev;
440 * Tunnels that belong to this client, indexed by local id
442 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
445 * Tunnels this client has accepted, indexed by incoming local id
447 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
450 * Tunnels this client has rejected, indexed by incoming local id
452 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
454 * Handle to communicate with the client
456 struct GNUNET_SERVER_Client *handle;
459 * Applications that this client has claimed to provide
461 struct GNUNET_CONTAINER_MultiHashMap *apps;
464 * Messages that this client has declared interest in
466 struct GNUNET_CONTAINER_MultiHashMap *types;
469 * Whether the client is active or shutting down (don't send confirmations
470 * to a client that is shutting down.
475 * ID of the client, mainly for debug messages
483 /******************************************************************************/
484 /************************ DEBUG FUNCTIONS ****************************/
485 /******************************************************************************/
489 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
490 * @param cls string to print
491 * @param success GNUNET_OK if the PUT was transmitted,
492 * GNUNET_NO on timeout,
493 * GNUNET_SYSERR on disconnect from service
494 * after the PUT message was transmitted
495 * (so we don't know if it was received or not)
500 mesh_debug (void *cls, int success)
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
510 /******************************************************************************/
511 /*********************** GLOBAL VARIABLES ****************************/
512 /******************************************************************************/
517 static struct MeshClient *clients;
518 static struct MeshClient *clients_tail;
521 * Transmission queue to core
523 struct MeshPeerQueue *queue_head;
524 struct MeshPeerQueue *queue_tail;
527 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel)
529 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
532 * Tunnels incoming, indexed by MESH_TunnelNumber
533 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
535 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
538 * Peers known, indexed by PeerIdentity (MeshPeerInfo)
540 static struct GNUNET_CONTAINER_MultiHashMap *peers;
543 * Handle to communicate with transport
545 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
548 * Handle to communicate with core
550 static struct GNUNET_CORE_Handle *core_handle;
553 * Handle to for queued transmissions
555 struct GNUNET_CORE_TransmitHandle *core_transmit;
560 static struct GNUNET_DHT_Handle *dht_handle;
565 static struct GNUNET_SERVER_Handle *server_handle;
568 * Notification context, to send messages to local clients
570 static struct GNUNET_SERVER_NotificationContext *nc;
573 * Local peer own ID (memory efficient handle)
575 static GNUNET_PEER_Id myid;
578 * Local peer own ID (full value)
580 static struct GNUNET_PeerIdentity my_full_id;
585 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
590 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
593 * Tunnel ID for the next created tunnel (global tunnel number)
595 static MESH_TunnelNumber next_tid;
598 * Tunnel ID for the next incoming tunnel (local tunnel number)
600 static MESH_TunnelNumber next_local_tid;
603 * All application types provided by this peer
605 static struct GNUNET_CONTAINER_MultiHashMap *applications;
608 * All message types clients of this peer are interested in
610 static struct GNUNET_CONTAINER_MultiHashMap *types;
613 * Task to periodically announce provided applications
615 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
618 * Task to periodically announce itself in the network
620 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
623 * Next ID to assign to a client
625 unsigned int next_client_id;
629 /******************************************************************************/
630 /************************ ITERATORS ****************************/
631 /******************************************************************************/
633 /* FIXME move iterators here */
636 /******************************************************************************/
637 /************************ PERIODIC FUNCTIONS ****************************/
638 /******************************************************************************/
641 * Announce iterator over for each application provided by the peer
644 * @param key current key code
645 * @param value value in the hash map
646 * @return GNUNET_YES if we should continue to
651 announce_application (void *cls, const GNUNET_HashCode * key, void *value)
653 /* FIXME are hashes in multihash map equal on all aquitectures? */
654 /* FIXME: keep return value of 'put' to possibly cancel!? */
655 GNUNET_DHT_put (dht_handle, key, 10,
656 GNUNET_DHT_RO_RECORD_ROUTE |
657 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, GNUNET_BLOCK_TYPE_TEST,
658 sizeof (struct GNUNET_PeerIdentity),
659 (const char *) &my_full_id,
660 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
662 APP_ANNOUNCE_TIME, NULL, NULL);
668 * Periodically announce what applications are provided by local clients
671 * @param tc task context
674 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
676 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
678 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
682 DEBUG_DHT ("Starting PUT for apps\n");
684 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
686 announce_applications_task =
687 GNUNET_SCHEDULER_add_delayed (APP_ANNOUNCE_TIME, &announce_applications,
689 DEBUG_DHT ("Finished PUT for apps\n");
696 * Periodically announce self id in the DHT
699 * @param tc task context
702 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
704 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
706 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
710 * - Set data expiration in function of X
713 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
715 GNUNET_DHT_put (dht_handle, /* DHT handle */
716 &my_full_id.hashPubKey, /* Key to use */
717 10, /* Replication level */
718 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
719 GNUNET_BLOCK_TYPE_TEST, /* Block type */
720 sizeof (my_full_id), /* Size of the data */
721 (char *) &my_full_id, /* Data itself */
722 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
723 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
724 NULL, /* Continuation */
725 NULL); /* Continuation closure */
727 GNUNET_SCHEDULER_add_delayed (ID_ANNOUNCE_TIME, &announce_id, cls);
732 * Function to process paths received for a new peer addition. The recorded
733 * paths form the initial tunnel, which can be optimized later.
734 * Called on each result obtained for the DHT search.
737 * @param exp when will this value expire
738 * @param key key of the result
739 * @param type type of the result
740 * @param size number of bytes in data
741 * @param data pointer to the result data
744 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
745 const GNUNET_HashCode * key,
746 const struct GNUNET_PeerIdentity *get_path,
747 unsigned int get_path_length,
748 const struct GNUNET_PeerIdentity *put_path,
749 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
750 size_t size, const void *data);
753 /******************************************************************************/
754 /****************** GENERAL HELPER FUNCTIONS ************************/
755 /******************************************************************************/
758 * Search for a tunnel by global ID using full PeerIdentities
760 * @param oid owner of the tunnel
761 * @param tid global tunnel number
763 * @return tunnel handler, NULL if doesn't exist
765 static struct MeshTunnel *
766 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
770 * Delete an active client from the tunnel.
776 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
779 * Notify a tunnel that a connection has broken that affects at least
782 * @param t Tunnel affected.
783 * @param p1 Peer that got disconnected from p2.
784 * @param p2 Peer that got disconnected from p1.
786 * @return Short ID of the peer disconnected (either p1 or p2).
787 * 0 if the tunnel remained unaffected.
789 static GNUNET_PEER_Id
790 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
795 * Check if client has registered with the service and has not disconnected
797 * @param client the client to check
799 * @return non-NULL if client exists in the global DLL
801 static struct MeshClient *
802 client_get (struct GNUNET_SERVER_Client *client)
804 struct MeshClient *c;
809 if (c->handle == client)
818 * Checks if a given client has subscribed to certain message type
820 * @param message_type Type of message to check
821 * @param c Client to check
823 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
828 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
832 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
833 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
838 * Allow a client to send more data after transmitting a multicast message
839 * which some neighbor has not yet accepted altough a reasonable time has
842 * @param cls Closure (DataDescriptor containing the task identifier)
843 * @param tc Task Context
845 * FIXME reference counter cshould be just int
848 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
850 struct MeshData *mdata = cls;
852 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
854 GNUNET_assert (NULL != mdata->reference_counter);
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
857 *(mdata->reference_counter));
858 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
859 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
864 * Check whether client wants traffic from a tunnel.
866 * @param c Client to check.
867 * @param t Tunnel to be found.
869 * @return GNUNET_YES if client knows tunnel.
871 * TODO look in client hashmap
874 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
878 for (i = 0; i < t->nclients; i++)
879 if (t->clients[i] == c)
886 * Check whether client has been informed about a tunnel.
888 * @param c Client to check.
889 * @param t Tunnel to be found.
891 * @return GNUNET_YES if client knows tunnel.
893 * TODO look in client hashmap
896 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
900 for (i = 0; i < t->nignore; i++)
901 if (t->ignore[i] == c)
903 return client_wants_tunnel(c, t);
908 * Marks a client as uninterested in traffic from the tunnel, updating both
909 * client and tunnel to reflect this.
911 * @param c Client that doesn't want traffic anymore.
912 * @param t Tunnel which should be ignored.
914 * FIXME when to delete an incoming tunnel?
917 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
919 GNUNET_HashCode hash;
921 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
922 GNUNET_break (GNUNET_YES ==
923 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
925 GNUNET_break (GNUNET_YES ==
926 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
927 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
928 tunnel_delete_active_client (t, c);
929 GNUNET_array_append (t->ignore, t->nignore, c);
934 * Deletes a tunnel from a client (either owner or destination). To be used on
935 * tunnel destroy, otherwise, use client_ignore_tunnel.
937 * @param c Client whose tunnel to delete.
938 * @param t Tunnel which should be deleted.
941 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
943 GNUNET_HashCode hash;
947 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
948 GNUNET_assert (GNUNET_YES ==
949 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
955 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
957 GNUNET_assert (GNUNET_YES ==
958 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
962 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
971 * Send the message to all clients that have subscribed to its type
973 * @param msg Pointer to the message itself
974 * @param payload Pointer to the payload of the message.
975 * @return number of clients this message was sent to
978 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
979 const struct GNUNET_MessageHeader *payload)
981 struct GNUNET_PeerIdentity *oid;
982 struct MeshClient *c;
983 struct MeshTunnel *t;
984 MESH_TunnelNumber *tid;
987 char cbuf[htons (msg->size)];
989 type = ntohs (payload->type);
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
993 memcpy (cbuf, msg, sizeof (cbuf));
994 switch (htons (msg->type))
996 struct GNUNET_MESH_Unicast *uc;
997 struct GNUNET_MESH_Multicast *mc;
998 struct GNUNET_MESH_ToOrigin *to;
1000 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1001 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1005 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1006 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1010 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1011 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1019 t = tunnel_get (oid, ntohl (*tid));
1026 for (count = 0, c = clients; c != NULL; c = c->next)
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1029 if (client_is_subscribed (type, c))
1031 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1035 *tid = htonl (t->local_tid);
1039 if (GNUNET_NO == client_knows_tunnel (c, t))
1041 /* This client doesn't know the tunnel */
1042 struct GNUNET_MESH_TunnelNotification tmsg;
1043 GNUNET_HashCode hash;
1045 tmsg.header.size = htons (sizeof (tmsg));
1046 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1047 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1048 tmsg.tunnel_id = htonl (t->local_tid_dest);
1049 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1050 &tmsg.header, GNUNET_NO);
1051 GNUNET_array_append (t->clients, t->nclients, c);
1052 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1054 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1055 c->incoming_tunnels, &hash, t,
1056 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1058 *tid = htonl (t->local_tid_dest);
1061 /* Check if the client wants to get traffic from the tunnel */
1062 if (GNUNET_NO == client_wants_tunnel(c, t))
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1066 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1067 (struct GNUNET_MessageHeader
1068 *) cbuf, GNUNET_YES);
1076 * Notify the client that owns the tunnel that a peer has connected to it
1077 * (the requested path to it has been confirmed).
1079 * @param t Tunnel whose owner to notify
1080 * @param id Short id of the peer that has connected
1083 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1085 struct GNUNET_MESH_PeerControl pc;
1087 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1088 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1089 pc.tunnel_id = htonl (t->local_tid);
1090 GNUNET_PEER_resolve (id, &pc.peer);
1091 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1097 * Notify all clients (not depending on registration status) that the incoming
1098 * tunnel is no longer valid.
1100 * @param t Tunnel that was destroyed.
1103 send_clients_tunnel_destroy (struct MeshTunnel *t)
1105 struct GNUNET_MESH_TunnelMessage msg;
1107 msg.header.size = htons (sizeof (msg));
1108 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1109 msg.tunnel_id = htonl (t->local_tid_dest);
1110 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1115 * Notify clients of tunnel disconnections, if needed.
1116 * In case the origin disconnects, the destination clients get a tunnel destroy
1117 * notification. If the last destination disconnects (only one remaining client
1118 * in tunnel), the origin gets a (local ID) peer disconnected.
1119 * Note that the function must be called BEFORE removing the client from
1122 * @param t Tunnel that was destroyed.
1123 * @param c Client that disconnected.
1126 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1132 struct GNUNET_MESH_TunnelMessage msg;
1134 msg.header.size = htons (sizeof (msg));
1135 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1136 msg.tunnel_id = htonl (t->local_tid_dest);
1137 for (i = 0; i < t->nclients; i++)
1138 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1139 &msg.header, GNUNET_NO);
1141 // FIXME when to disconnect an incoming tunnel?
1142 else if (1 == t->nclients && NULL != t->owner)
1144 struct GNUNET_MESH_PeerControl msg;
1146 msg.header.size = htons (sizeof (msg));
1147 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1148 msg.tunnel_id = htonl (t->local_tid);
1149 msg.peer = my_full_id;
1150 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1151 &msg.header, GNUNET_NO);
1157 * Decrements the reference counter and frees all resources if needed
1159 * @param mesh_data Data Descriptor used in a multicast message.
1160 * Freed no longer needed (last message).
1163 data_descriptor_decrement_multicast (struct MeshData *mesh_data)
1165 /* Make sure it's a multicast packet */
1166 GNUNET_assert (NULL != mesh_data->reference_counter);
1168 if (0 == --(*(mesh_data->reference_counter)))
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1171 if (NULL != mesh_data->task)
1173 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1175 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1177 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1179 GNUNET_free (mesh_data->task);
1181 GNUNET_free (mesh_data->reference_counter);
1182 GNUNET_free (mesh_data->data);
1183 GNUNET_free (mesh_data);
1189 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1190 * and insert it in the appropiate structures if the peer is not known yet.
1192 * @param peer Full identity of the peer.
1194 * @return Existing or newly created peer info.
1196 static struct MeshPeerInfo *
1197 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1199 struct MeshPeerInfo *peer_info;
1201 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1202 if (NULL == peer_info)
1205 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1206 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1207 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1208 peer_info->id = GNUNET_PEER_intern (peer);
1216 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1217 * and insert it in the appropiate structures if the peer is not known yet.
1219 * @param peer Short identity of the peer.
1221 * @return Existing or newly created peer info.
1223 static struct MeshPeerInfo *
1224 peer_info_get_short (const GNUNET_PEER_Id peer)
1226 struct GNUNET_PeerIdentity id;
1228 GNUNET_PEER_resolve (peer, &id);
1229 return peer_info_get (&id);
1234 * Iterator to remove the tunnel from the list of tunnels a peer participates
1237 * @param cls Closure (tunnel info)
1238 * @param key GNUNET_PeerIdentity of the peer (unused)
1239 * @param value PeerInfo of the peer
1241 * @return always GNUNET_YES, to keep iterating
1244 peer_info_delete_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
1246 struct MeshTunnel *t = cls;
1247 struct MeshPeerInfo *peer = value;
1250 for (i = 0; i < peer->ntunnels; i++)
1253 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1256 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1257 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
1266 * Queue and pass message to core when possible.
1268 * @param cls Closure (type dependant).
1269 * @param type Type of the message.
1270 * @param size Size of the message.
1271 * @param dst Neighbor to send message to.
1274 queue_add (void *cls, uint16_t type, size_t size, struct MeshPeerInfo *dst);
1277 * Core callback to write a pre-constructed data packet to core buffer
1279 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1280 * @param size Number of bytes available in buf.
1281 * @param buf Where the to write the message.
1283 * @return number of bytes written to buf
1286 send_core_data_raw (void *cls, size_t size, void *buf)
1288 struct MeshTransmissionDescriptor *info = cls;
1289 struct GNUNET_MessageHeader *msg;
1292 GNUNET_assert (NULL != info);
1293 GNUNET_assert (NULL != info->mesh_data);
1294 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1295 total_size = ntohs (msg->size);
1297 if (total_size > size)
1302 memcpy (buf, msg, total_size);
1303 GNUNET_free (info->mesh_data);
1310 * Sends an already built unicast message to a peer, properly registrating
1311 * all used resources.
1313 * @param message Message to send. Function makes a copy of it.
1314 * @param peer Short ID of the neighbor whom to send the message.
1319 send_message (const struct GNUNET_MessageHeader *message,
1320 const struct GNUNET_PeerIdentity *peer)
1322 struct MeshTransmissionDescriptor *info;
1323 struct MeshPeerInfo *neighbor;
1324 struct MeshPeerPath *p;
1327 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1329 size = ntohs (message->size);
1330 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1331 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1332 info->mesh_data->data = GNUNET_malloc (size);
1333 memcpy (info->mesh_data->data, message, size);
1334 info->mesh_data->data_len = size;
1335 neighbor = peer_info_get (peer);
1336 for (p = neighbor->path_head; NULL != p; p = p->next)
1346 GNUNET_free (info->mesh_data->data);
1347 GNUNET_free (info->mesh_data);
1351 info->peer = neighbor;
1353 GNUNET_MESSAGE_TYPE_MESH_UNICAST,
1360 * Sends a CREATE PATH message for a path to a peer, properly registrating
1361 * all used resources.
1363 * @param peer PeerInfo of the final peer for whom this path is being created.
1364 * @param p Path itself.
1365 * @param t Tunnel for which the path is created.
1368 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1369 struct MeshTunnel *t)
1371 struct GNUNET_PeerIdentity id;
1372 struct MeshPathInfo *path_info;
1373 struct MeshPeerInfo *neighbor;
1379 p = tree_get_path_to_peer (t->tree, peer->id);
1386 for (i = 0; i < p->length; i++)
1388 if (p->peers[i] == myid)
1391 if (i >= p->length - 1)
1397 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1399 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1400 path_info->path = p;
1402 neighbor = peer_info_get (&id);
1403 path_info->peer = neighbor;
1404 queue_add (path_info,
1405 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1406 sizeof (struct GNUNET_MESH_ManipulatePath) +
1407 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1413 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1415 * @param t Tunnel whose path to destroy.
1416 * @param destination Short ID of the peer to whom the path to destroy.
1419 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1421 struct MeshPeerPath *p;
1424 p = tree_get_path_to_peer (t->tree, destination);
1430 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1431 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1433 struct GNUNET_MESH_ManipulatePath *msg;
1434 struct GNUNET_PeerIdentity *pi;
1438 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1439 msg->header.size = htons (size);
1440 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1441 msg->tid = htonl (t->id.tid);
1442 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1443 for (i = 0; i < p->length; i++)
1445 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1447 send_message (&msg->header, tree_get_first_hop (t->tree, destination));
1454 * Try to establish a new connection to this peer.
1455 * Use the best path for the given tunnel.
1456 * If the peer doesn't have any path to it yet, try to get one.
1457 * If the peer already has some path, send a CREATE PATH towards it.
1459 * @param peer PeerInfo of the peer.
1460 * @param t Tunnel for which to create the path, if possible.
1463 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1465 struct MeshPeerPath *p;
1466 struct MeshPathInfo *path_info;
1468 if (NULL != peer->path_head)
1470 p = tree_get_path_to_peer (t->tree, peer->id);
1477 // FIXME always send create path to self
1480 send_create_path (peer, p, t);
1484 GNUNET_HashCode hash;
1487 send_client_peer_connected (t, myid);
1488 t->local_tid_dest = next_local_tid++;
1489 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1492 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
1493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1500 else if (NULL == peer->dhtget)
1502 struct GNUNET_PeerIdentity id;
1504 GNUNET_PEER_resolve (peer->id, &id);
1505 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1506 path_info->peer = peer;
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
1510 peer->dhtgetcls = path_info;
1511 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
1512 GNUNET_BLOCK_TYPE_TEST, /* type */
1513 &id.hashPubKey, /* key to search */
1514 10, /* replication level */
1515 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
1516 0, /* xquery bits */
1517 &dht_get_id_handler, path_info);
1519 /* Otherwise, there is no path but the DHT get is already started. */
1524 * Task to delay the connection of a peer
1526 * @param cls Closure (path info with tunnel and peer to connect).
1527 * Will be free'd on exection.
1528 * @param tc TaskContext
1531 peer_info_connect_task (void *cls,
1532 const struct GNUNET_SCHEDULER_TaskContext *tc)
1534 struct MeshPathInfo *path_info = cls;
1536 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1541 peer_info_connect (path_info->peer, path_info->t);
1547 * Destroy the peer_info and free any allocated resources linked to it
1549 * @param pi The peer_info to destroy.
1551 * @return GNUNET_OK on success
1554 peer_info_destroy (struct MeshPeerInfo *pi)
1556 struct GNUNET_PeerIdentity id;
1557 struct MeshPeerPath *p;
1558 struct MeshPeerPath *nextp;
1560 GNUNET_PEER_resolve (pi->id, &id);
1561 GNUNET_PEER_change_rc (pi->id, -1);
1564 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
1567 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1568 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
1570 if (NULL != pi->dhtget)
1572 GNUNET_DHT_get_stop (pi->dhtget);
1573 GNUNET_free (pi->dhtgetcls);
1579 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
1589 * Remove all paths that rely on a direct connection between p1 and p2
1590 * from the peer itself and notify all tunnels about it.
1592 * @param peer PeerInfo of affected peer.
1593 * @param p1 GNUNET_PEER_Id of one peer.
1594 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
1597 * TODO: optimize (see below)
1600 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
1603 struct MeshPeerPath *p;
1604 struct MeshPeerPath *aux;
1605 struct MeshPeerInfo *peer_d;
1607 unsigned int destroyed;
1613 p = peer->path_head;
1617 for (i = 0; i < (p->length - 1); i++)
1619 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
1620 (p->peers[i] == p2 && p->peers[i + 1] == p1))
1622 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
1633 for (i = 0; i < peer->ntunnels; i++)
1635 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
1639 * Problem: one or more peers have been deleted from the tunnel tree.
1640 * We don't know who they are to try to add them again.
1641 * We need to try to find a new path for each of the disconnected peers.
1642 * Some of them might already have a path to reach them that does not
1643 * involve p1 and p2. Adding all anew might render in a better tree than
1644 * the trivial immediate fix.
1646 * Trivial immiediate fix: try to reconnect to the disconnected node. All
1647 * its children will be reachable trough him.
1649 peer_d = peer_info_get_short (d);
1652 for (p = peer_d->path_head; NULL != p; p = p->next)
1654 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
1662 /* No callback, as peer will be already disconnected and a connection
1663 * scheduled by tunnel_notify_connection_broken.
1665 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
1669 peer_info_connect (peer_d, peer->tunnels[i]);
1676 * Add the path to the peer and update the path used to reach it in case this
1679 * @param peer_info Destination peer to add the path to.
1680 * @param path New path to add. Last peer must be the peer in arg 1.
1681 * Path will be either used of freed if already known.
1682 * @param trusted Do we trust that this path is real?
1685 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
1688 struct MeshPeerPath *aux;
1692 if ((NULL == peer_info) || (NULL == path))
1695 path_destroy (path);
1698 if (path->peers[path->length - 1] != peer_info->id)
1701 path_destroy (path);
1704 if (path->length <= 2 && GNUNET_NO == trusted)
1706 /* Only allow CORE to tell us about direct paths */
1707 path_destroy (path);
1710 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1711 for (l = 1; l < path->length; l++)
1713 if (path->peers[l] == myid)
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
1716 for (l2 = 0; l2 < path->length - l; l2++)
1718 path->peers[l2] = path->peers[l + l2];
1723 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
1728 struct GNUNET_PeerIdentity id;
1730 GNUNET_PEER_resolve (peer_info->id, &id);
1731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
1732 path->length, GNUNET_i2s (&id));
1735 l = path_get_length (path);
1742 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
1743 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
1745 l2 = path_get_length (aux);
1748 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
1749 peer_info->path_tail, aux, path);
1754 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
1756 path_destroy (path);
1761 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
1768 * Add the path to the origin peer and update the path used to reach it in case
1769 * this is the shortest.
1770 * The path is given in peer_info -> destination, therefore we turn the path
1771 * upside down first.
1773 * @param peer_info Peer to add the path to, being the origin of the path.
1774 * @param path New path to add after being inversed.
1775 * @param trusted Do we trust that this path is real?
1778 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
1779 struct MeshPeerPath *path, int trusted)
1782 peer_info_add_path (peer_info, path, trusted);
1787 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1788 * to obtain a local peer -> destination path and interning the peer ids.
1790 * @return Newly allocated and created path
1792 static struct MeshPeerPath *
1793 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1794 unsigned int get_path_length,
1795 const struct GNUNET_PeerIdentity *put_path,
1796 unsigned int put_path_length)
1798 struct MeshPeerPath *p;
1804 GNUNET_PEER_change_rc (myid, 1);
1805 i = get_path_length;
1806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
1807 for (i--; i >= 0; i--)
1809 id = GNUNET_PEER_intern (&get_path[i]);
1810 if (p->length > 0 && id == p->peers[p->length - 1])
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1813 GNUNET_PEER_change_rc (id, -1);
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
1818 GNUNET_i2s (&get_path[i]));
1820 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1821 p->peers[p->length - 1] = id;
1824 i = put_path_length;
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
1826 for (i--; i >= 0; i--)
1828 id = GNUNET_PEER_intern (&put_path[i]);
1831 /* PUT path went through us, so discard the path up until now and start
1832 * from here to get a much shorter (and loop-free) path.
1837 if (p->length > 0 && id == p->peers[p->length - 1])
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
1840 GNUNET_PEER_change_rc (id, -1);
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
1845 GNUNET_i2s (&put_path[i]));
1847 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
1848 p->peers[p->length - 1] = id;
1852 if (get_path_length > 0)
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
1854 GNUNET_i2s (&get_path[0]));
1855 if (put_path_length > 0)
1856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
1857 GNUNET_i2s (&put_path[0]));
1858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
1860 for (i = 0; i < p->length; i++)
1862 struct GNUNET_PeerIdentity peer_id;
1864 GNUNET_PEER_resolve (p->peers[i], &peer_id);
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
1866 GNUNET_i2s (&peer_id));
1874 * Adds a path to the peer_infos of all the peers in the path
1876 * @param p Path to process.
1877 * @param confirmed Whether we know if the path works or not. FIXME use
1880 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
1884 /* TODO: invert and add */
1885 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
1886 for (i++; i < p->length; i++)
1888 struct MeshPeerInfo *aux;
1889 struct MeshPeerPath *copy;
1891 aux = peer_info_get_short (p->peers[i]);
1892 copy = path_duplicate (p);
1893 copy->length = i + 1;
1894 peer_info_add_path (aux, copy, GNUNET_NO);
1900 * Send keepalive packets for a peer
1902 * @param cls Closure (tunnel for which to send the keepalive).
1903 * @param tc Notification context.
1905 * TODO: implement explicit multicast keepalive?
1908 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1912 * Search for a tunnel among the incoming tunnels
1914 * @param tid the local id of the tunnel
1916 * @return tunnel handler, NULL if doesn't exist
1918 static struct MeshTunnel *
1919 tunnel_get_incoming (MESH_TunnelNumber tid)
1921 GNUNET_HashCode hash;
1923 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
1924 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1925 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
1930 * Search for a tunnel among the tunnels for a client
1932 * @param c the client whose tunnels to search in
1933 * @param tid the local id of the tunnel
1935 * @return tunnel handler, NULL if doesn't exist
1937 static struct MeshTunnel *
1938 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
1940 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
1942 return tunnel_get_incoming (tid);
1946 GNUNET_HashCode hash;
1948 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
1949 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
1955 * Search for a tunnel by global ID using PEER_ID
1957 * @param pi owner of the tunnel
1958 * @param tid global tunnel number
1960 * @return tunnel handler, NULL if doesn't exist
1962 static struct MeshTunnel *
1963 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
1965 struct MESH_TunnelID id;
1966 GNUNET_HashCode hash;
1971 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
1972 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
1977 * Search for a tunnel by global ID using full PeerIdentities
1979 * @param oid owner of the tunnel
1980 * @param tid global tunnel number
1982 * @return tunnel handler, NULL if doesn't exist
1984 static struct MeshTunnel *
1985 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
1987 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
1992 * Delete an active client from the tunnel.
1998 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2002 for (i = 0; i < t->nclients; i++)
2004 if (t->clients[i] == c)
2006 t->clients[i] = t->clients[t->nclients - 1];
2007 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2015 * Delete an ignored client from the tunnel.
2021 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2025 for (i = 0; i < t->nignore; i++)
2027 if (t->ignore[i] == c)
2029 t->ignore[i] = t->ignore[t->nignore - 1];
2030 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2038 * Delete a client from the tunnel. It should be only done on
2039 * client disconnection, otherwise use client_ignore_tunnel.
2045 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2047 tunnel_delete_ignored_client (t, c);
2048 tunnel_delete_active_client (t, c);
2053 * Callback used to notify a client owner of a tunnel that a peer has
2054 * disconnected, most likely because of a path change.
2056 * @param cls Closure (tunnel this notification is about).
2057 * @param peer_id Short ID of disconnected peer.
2060 notify_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2062 struct MeshTunnel *t = cls;
2063 struct MeshPeerInfo *peer;
2064 struct MeshPathInfo *path_info;
2066 if (NULL != t->owner && NULL != nc)
2068 struct GNUNET_MESH_PeerControl msg;
2070 msg.header.size = htons (sizeof (msg));
2071 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2072 msg.tunnel_id = htonl (t->local_tid);
2073 GNUNET_PEER_resolve (peer_id, &msg.peer);
2074 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2075 &msg.header, GNUNET_NO);
2077 peer = peer_info_get_short (peer_id);
2078 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2079 path_info->peer = peer;
2081 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
2086 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2087 * needed rescources.
2088 * If peer already exists, reevaluate shortest path and change if different.
2090 * @param t Tunnel we want to add a new peer to
2091 * @param peer PeerInfo of the peer being added
2095 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2097 struct GNUNET_PeerIdentity id;
2098 struct MeshPeerPath *best_p;
2099 struct MeshPeerPath *p;
2100 unsigned int best_cost;
2103 GNUNET_PEER_resolve (peer->id, &id);
2105 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
2108 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2109 GNUNET_assert (GNUNET_OK ==
2110 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
2112 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2115 if (NULL != (p = peer->path_head))
2118 best_cost = tree_get_path_cost (t->tree, p);
2121 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2128 tree_add_path (t->tree, best_p, ¬ify_peer_disconnected, t);
2129 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2130 t->path_refresh_task =
2131 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
2135 /* Start a DHT get */
2136 peer_info_connect (peer, t);
2141 * Add a path to a tunnel which we don't own, just to remember the next hop.
2142 * If destination node was already in the tunnel, the first hop information
2143 * will be replaced with the new path.
2145 * @param t Tunnel we want to add a new peer to
2146 * @param p Path to add
2147 * @param own_pos Position of local node in path.
2151 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2152 unsigned int own_pos)
2154 struct GNUNET_PeerIdentity id;
2156 GNUNET_assert (0 != own_pos);
2157 tree_add_path (t->tree, p, NULL, NULL);
2158 if (own_pos < p->length - 1)
2160 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2161 tree_update_first_hops (t->tree, myid, &id);
2167 * Notifies a tunnel that a connection has broken that affects at least
2168 * some of its peers. Sends a notification towards the root of the tree.
2169 * In case the peer is the owner of the tree, notifies the client that owns
2170 * the tunnel and tries to reconnect.
2172 * @param t Tunnel affected.
2173 * @param p1 Peer that got disconnected from p2.
2174 * @param p2 Peer that got disconnected from p1.
2176 * @return Short ID of the peer disconnected (either p1 or p2).
2177 * 0 if the tunnel remained unaffected.
2179 static GNUNET_PEER_Id
2180 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2186 tree_notify_connection_broken (t->tree, p1, p2, ¬ify_peer_disconnected,
2188 if (myid != p1 && myid != p2)
2194 if (tree_get_predecessor (t->tree) != 0)
2196 /* We are the peer still connected, notify owner of the disconnection. */
2197 struct GNUNET_MESH_PathBroken msg;
2198 struct GNUNET_PeerIdentity neighbor;
2200 msg.header.size = htons (sizeof (msg));
2201 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2202 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2203 msg.tid = htonl (t->id.tid);
2204 msg.peer1 = my_full_id;
2205 GNUNET_PEER_resolve (pid, &msg.peer2);
2206 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2207 send_message (&msg.header, &neighbor);
2215 * Send a multicast packet to a neighbor.
2217 * @param cls Closure (Info about the multicast packet)
2218 * @param neighbor_id Short ID of the neighbor to send the packet to.
2221 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
2223 struct MeshData *mdata = cls;
2224 struct MeshTransmissionDescriptor *info;
2225 struct GNUNET_PeerIdentity neighbor;
2227 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2229 info->mesh_data = mdata;
2230 (*(mdata->reference_counter)) ++;
2231 info->destination = neighbor_id;
2232 GNUNET_PEER_resolve (neighbor_id, &neighbor);
2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
2234 GNUNET_i2s (&neighbor));
2235 info->peer = peer_info_get (&neighbor);
2236 GNUNET_assert (NULL != info->peer);
2238 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
2239 info->mesh_data->data_len,
2245 * Send a message in a tunnel in multicast, sending a copy to each child node
2246 * down the local one in the tunnel tree.
2248 * @param t Tunnel in which to send the data.
2249 * @param msg Message to be sent.
2250 * @param internal Has the service generated this message?
2253 tunnel_send_multicast (struct MeshTunnel *t,
2254 const struct GNUNET_MessageHeader *msg,
2257 struct MeshData *mdata;
2259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2260 " sending a multicast packet...\n");
2261 mdata = GNUNET_malloc (sizeof (struct MeshData));
2262 mdata->data_len = ntohs (msg->size);
2263 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2265 mdata->data = GNUNET_malloc (mdata->data_len);
2266 memcpy (mdata->data, msg, mdata->data_len);
2267 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2269 struct GNUNET_MESH_Multicast *mcast;
2271 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
2272 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
2273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
2274 ntohl (mcast->ttl));
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
2280 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
2281 && GNUNET_NO == internal)
2283 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
2285 GNUNET_SCHEDULER_add_delayed (UNACKNOWLEDGED_WAIT, &client_allow_send,
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
2291 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
2292 if (*(mdata->reference_counter) == 0)
2294 GNUNET_free (mdata->data);
2295 GNUNET_free (mdata->reference_counter);
2296 if (NULL != mdata->task)
2298 GNUNET_SCHEDULER_cancel(*(mdata->task));
2299 GNUNET_free (mdata->task);
2300 GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
2302 // FIXME change order?
2303 GNUNET_free (mdata);
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 " sending a multicast packet done\n");
2312 * Send a message to all peers in this tunnel that the tunnel is no longer
2315 * @param t The tunnel whose peers to notify.
2318 tunnel_send_destroy (struct MeshTunnel *t)
2320 struct GNUNET_MESH_TunnelDestroy msg;
2322 msg.header.size = htons (sizeof (msg));
2323 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
2324 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2325 msg.tid = htonl (t->id.tid);
2326 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
2332 * Destroy the tunnel and free any allocated resources linked to it.
2334 * @param t the tunnel to destroy
2336 * @return GNUNET_OK on success
2339 tunnel_destroy (struct MeshTunnel *t)
2341 struct MeshClient *c;
2342 struct MeshQueue *q;
2343 struct MeshQueue *qn;
2344 GNUNET_HashCode hash;
2355 struct GNUNET_PeerIdentity id;
2357 GNUNET_PEER_resolve (t->id.oid, &id);
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
2359 GNUNET_i2s (&id), t->id.tid);
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
2365 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2366 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
2371 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2374 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
2378 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2379 for (i = 0; i < t->nclients; i++)
2383 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
2388 for (i = 0; i < t->nignore; i++)
2392 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
2397 if (t->nclients > 0)
2400 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
2404 GNUNET_free (t->clients);
2406 if (NULL != t->peers)
2408 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
2410 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
2415 if (NULL != q->data)
2416 GNUNET_free (q->data);
2420 /* TODO cancel core transmit ready in case it was active */
2422 tree_destroy (t->tree);
2423 if (NULL != t->dht_get_type)
2424 GNUNET_DHT_get_stop (t->dht_get_type);
2425 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2426 GNUNET_SCHEDULER_cancel (t->timeout_task);
2427 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
2428 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
2435 * Removes an explicit path from a tunnel, freeing all intermediate nodes
2436 * that are no longer needed, as well as nodes of no longer reachable peers.
2437 * The tunnel itself is also destoyed if results in a remote empty tunnel.
2439 * @param t Tunnel from which to remove the path.
2440 * @param peer Short id of the peer which should be removed.
2443 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
2445 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
2451 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
2452 * client when the client disconnects. If the client is not the owner, the
2453 * owner will get notified if no more clients are in the tunnel and the client
2454 * get removed from the tunnel's list.
2456 * @param cls closure (client that is disconnecting)
2457 * @param key the hash of the local tunnel id (used to access the hashmap)
2458 * @param value the value stored at the key (tunnel to destroy)
2460 * @return GNUNET_OK on success
2463 tunnel_destroy_iterator (void *cls, const GNUNET_HashCode * key, void *value)
2465 struct MeshTunnel *t = value;
2466 struct MeshClient *c = cls;
2469 send_client_tunnel_disconnect(t, c);
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2473 "Client %u is destination, keeping the tunnel alive.\n", c->id);
2474 tunnel_delete_client(t, c);
2475 client_delete_tunnel(c, t);
2478 tunnel_send_destroy(t);
2479 r = tunnel_destroy (t);
2485 * Timeout function, destroys tunnel if called
2487 * @param cls Closure (tunnel to destroy).
2488 * @param tc TaskContext
2491 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2493 struct MeshTunnel *t = cls;
2495 if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
2497 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2502 * Resets the tunnel timeout. Starts it if no timeout was running.
2504 * @param t Tunnel whose timeout to reset.
2507 tunnel_reset_timeout (struct MeshTunnel *t)
2509 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
2510 GNUNET_SCHEDULER_cancel (t->timeout_task);
2512 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2513 (REFRESH_PATH_TIME, 4), &tunnel_timeout, t);
2517 /******************************************************************************/
2518 /**************** MESH NETWORK HANDLER HELPERS ***********************/
2519 /******************************************************************************/
2522 * Function to send a create path packet to a peer.
2524 * @param cls closure
2525 * @param size number of bytes available in buf
2526 * @param buf where the callee should write the message
2527 * @return number of bytes written to buf
2530 send_core_path_create (void *cls, size_t size, void *buf)
2532 struct MeshPathInfo *info = cls;
2533 struct GNUNET_MESH_ManipulatePath *msg;
2534 struct GNUNET_PeerIdentity *peer_ptr;
2535 struct MeshTunnel *t = info->t;
2536 struct MeshPeerPath *p = info->path;
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
2542 sizeof (struct GNUNET_MESH_ManipulatePath) +
2543 p->length * sizeof (struct GNUNET_PeerIdentity);
2545 if (size < size_needed || NULL == buf)
2550 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
2551 msg->header.size = htons (size_needed);
2552 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
2553 msg->tid = ntohl (t->id.tid);
2555 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
2556 for (i = 0; i < p->length; i++)
2558 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2565 "CREATE PATH (%u bytes long) sent!\n", size_needed);
2571 * Fill the core buffer
2573 * @param cls closure (data itself)
2574 * @param size number of bytes available in buf
2575 * @param buf where the callee should write the message
2577 * @return number of bytes written to buf
2580 send_core_data_multicast (void *cls, size_t size, void *buf)
2582 struct MeshTransmissionDescriptor *info = cls;
2585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
2586 GNUNET_assert (NULL != info);
2587 GNUNET_assert (NULL != info->peer);
2588 total_size = info->mesh_data->data_len;
2589 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
2591 if (total_size > size)
2596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
2597 memcpy (buf, info->mesh_data->data, total_size);
2600 struct GNUNET_MESH_Multicast *mc;
2601 struct GNUNET_MessageHeader *mh;
2604 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
2606 mc = (struct GNUNET_MESH_Multicast *) mh;
2607 mh = (struct GNUNET_MessageHeader *) &mc[1];
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 " multicast, payload type %u\n", ntohs (mh->type));
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2611 " multicast, payload size %u\n", ntohs (mh->size));
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
2620 data_descriptor_decrement_multicast (info->mesh_data);
2621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
2623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
2629 * Creates a path ack message in buf and frees all unused resources.
2631 * @param cls closure (MeshTransmissionDescriptor)
2632 * @param size number of bytes available in buf
2633 * @param buf where the callee should write the message
2634 * @return number of bytes written to buf
2637 send_core_path_ack (void *cls, size_t size, void *buf)
2639 struct MeshTransmissionDescriptor *info = cls;
2640 struct GNUNET_MESH_PathACK *msg = buf;
2642 GNUNET_assert (NULL != info);
2643 if (sizeof (struct GNUNET_MESH_PathACK) > size)
2648 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
2649 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
2650 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
2651 msg->tid = htonl (info->origin->tid);
2652 msg->peer_id = my_full_id;
2655 /* TODO add signature */
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
2658 return sizeof (struct GNUNET_MESH_PathACK);
2663 * Free a transmission that was already queued with all resources
2664 * associated to the request.
2666 * @param queue Queue handler to cancel.
2667 * @param clear_cls Is it necessary to free associated cls?
2670 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
2672 struct MeshTransmissionDescriptor *dd;
2673 struct MeshPathInfo *path_info;
2675 if (GNUNET_YES == clear_cls)
2677 switch (queue->type)
2679 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2680 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
2683 data_descriptor_decrement_multicast (dd->mesh_data);
2685 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
2687 path_info = queue->cls;
2688 path_destroy (path_info->path);
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
2694 GNUNET_free_non_null (queue->cls);
2696 GNUNET_CONTAINER_DLL_remove (queue_head, queue_tail, queue);
2697 GNUNET_free (queue);
2702 * Core callback to write a queued packet to core buffer
2704 * @param cls Closure (peer info).
2705 * @param size Number of bytes available in buf.
2706 * @param buf Where the to write the message.
2708 * @return number of bytes written to buf
2711 queue_send (void *cls, size_t size, void *buf)
2713 struct MeshPeerInfo *peer = cls;
2714 struct MeshPeerQueue *queue;
2717 core_transmit = NULL;
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
2723 /* If queue is empty, send should have been cancelled */
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
2731 /* Check if buffer size is enough for the message */
2732 if (queue->size > size)
2734 struct GNUNET_PeerIdentity id;
2736 GNUNET_PEER_resolve (peer->id, &id);
2738 GNUNET_CORE_notify_transmit_ready(core_handle,
2741 GNUNET_TIME_UNIT_FOREVER_REL,
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
2751 switch (queue->type)
2753 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast\n");
2755 data_size = send_core_data_raw (queue->cls, size, buf);
2757 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
2759 data_size = send_core_data_multicast(queue->cls, size, buf);
2761 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
2763 data_size = send_core_path_create(queue->cls, size, buf);
2765 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
2767 data_size = send_core_path_ack(queue->cls, size, buf);
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
2775 /* Free queue, but cls was freed by send_core_* */
2776 queue_destroy(queue, GNUNET_NO);
2778 /* If more data in queue, send next */
2779 if (NULL != queue_head)
2781 struct GNUNET_PeerIdentity id;
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
2784 GNUNET_PEER_resolve (peer->id, &id);
2786 GNUNET_CORE_notify_transmit_ready(core_handle,
2789 GNUNET_TIME_UNIT_FOREVER_REL,
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
2801 * Queue and pass message to core when possible.
2803 * @param cls Closure (type dependant).
2804 * @param type Type of the message.
2805 * @param size Size of the message.
2806 * @param dst Neighbor to send message to.
2809 queue_add (void *cls, uint16_t type, size_t size, struct MeshPeerInfo *dst)
2811 struct MeshPeerQueue *queue;
2813 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
2818 GNUNET_CONTAINER_DLL_insert_tail (queue_head, queue_tail, queue);
2819 if (NULL == core_transmit)
2821 struct GNUNET_PeerIdentity id;
2823 GNUNET_PEER_resolve (dst->id, &id);
2825 GNUNET_CORE_notify_transmit_ready(core_handle,
2828 GNUNET_TIME_UNIT_FOREVER_REL,
2837 /******************************************************************************/
2838 /******************** MESH NETWORK HANDLERS **************************/
2839 /******************************************************************************/
2843 * Core handler for path creation
2845 * @param cls closure
2846 * @param message message
2847 * @param peer peer identity this notification is about
2848 * @param atsi performance data
2849 * @param atsi_count number of records in 'atsi'
2851 * @return GNUNET_OK to keep the connection open,
2852 * GNUNET_SYSERR to close it (signal serious error)
2855 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
2856 const struct GNUNET_MessageHeader *message,
2857 const struct GNUNET_ATS_Information *atsi,
2858 unsigned int atsi_count)
2860 unsigned int own_pos;
2863 MESH_TunnelNumber tid;
2864 struct GNUNET_MESH_ManipulatePath *msg;
2865 struct GNUNET_PeerIdentity *pi;
2866 GNUNET_HashCode hash;
2867 struct MeshPeerPath *path;
2868 struct MeshPeerInfo *dest_peer_info;
2869 struct MeshPeerInfo *orig_peer_info;
2870 struct MeshTunnel *t;
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2873 "Received a path create msg [%s]\n",
2874 GNUNET_i2s (&my_full_id));
2875 size = ntohs (message->size);
2876 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
2878 GNUNET_break_op (0);
2882 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
2883 if (size % sizeof (struct GNUNET_PeerIdentity))
2885 GNUNET_break_op (0);
2888 size /= sizeof (struct GNUNET_PeerIdentity);
2891 GNUNET_break_op (0);
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
2895 msg = (struct GNUNET_MESH_ManipulatePath *) message;
2897 tid = ntohl (msg->tid);
2898 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2900 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
2901 t = tunnel_get (pi, tid);
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
2905 t = GNUNET_malloc (sizeof (struct MeshTunnel));
2906 t->id.oid = GNUNET_PEER_intern (pi);
2908 while (NULL != tunnel_get_incoming (next_local_tid))
2909 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2910 t->local_tid_dest = next_local_tid++;
2911 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
2912 t->tree = tree_new (t->id.oid);
2914 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
2916 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
2917 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2923 tunnel_reset_timeout (t);
2924 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2926 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2927 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2935 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
2936 if (NULL == dest_peer_info)
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2939 " Creating PeerInfo for destination.\n");
2940 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2941 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
2942 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
2944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2946 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
2947 if (NULL == orig_peer_info)
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2950 " Creating PeerInfo for origin.\n");
2951 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
2952 orig_peer_info->id = GNUNET_PEER_intern (pi);
2953 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
2954 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
2957 path = path_new (size);
2959 for (i = 0; i < size; i++)
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
2962 GNUNET_i2s (&pi[i]));
2963 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
2964 if (path->peers[i] == myid)
2967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
2970 /* cannot be self, must be 'not found' */
2971 /* create path: self not found in path through self */
2972 GNUNET_break_op (0);
2973 path_destroy (path);
2974 /* FIXME error. destroy tunnel? leave for timeout? */
2977 path_add_to_peers (path, GNUNET_NO);
2978 tunnel_add_path (t, path, own_pos);
2979 if (own_pos == size - 1)
2981 /* It is for us! Send ack. */
2982 struct MeshTransmissionDescriptor *info;
2984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
2985 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
2986 if (NULL == t->peers)
2988 /* New tunnel! Notify clients on data. */
2989 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
2991 GNUNET_break (GNUNET_OK ==
2992 GNUNET_CONTAINER_multihashmap_put (t->peers,
2993 &my_full_id.hashPubKey,
2996 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
2997 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2998 info->origin = &t->id;
2999 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
3000 GNUNET_assert (NULL != info->peer);
3002 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3003 sizeof (struct GNUNET_MESH_PathACK),
3008 struct MeshPeerPath *path2;
3010 /* It's for somebody else! Retransmit. */
3011 path2 = path_duplicate (path);
3012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
3013 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
3014 path2 = path_duplicate (path);
3015 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
3016 send_create_path (dest_peer_info, path, t);
3023 * Core handler for path destruction
3025 * @param cls closure
3026 * @param message message
3027 * @param peer peer identity this notification is about
3028 * @param atsi performance data
3029 * @param atsi_count number of records in 'atsi'
3031 * @return GNUNET_OK to keep the connection open,
3032 * GNUNET_SYSERR to close it (signal serious error)
3035 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
3036 const struct GNUNET_MessageHeader *message,
3037 const struct GNUNET_ATS_Information *atsi,
3038 unsigned int atsi_count)
3040 struct GNUNET_MESH_ManipulatePath *msg;
3041 struct GNUNET_PeerIdentity *pi;
3042 struct MeshPeerPath *path;
3043 struct MeshTunnel *t;
3044 unsigned int own_pos;
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3049 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
3050 size = ntohs (message->size);
3051 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
3053 GNUNET_break_op (0);
3057 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
3058 if (size % sizeof (struct GNUNET_PeerIdentity))
3060 GNUNET_break_op (0);
3063 size /= sizeof (struct GNUNET_PeerIdentity);
3066 GNUNET_break_op (0);
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
3071 msg = (struct GNUNET_MESH_ManipulatePath *) message;
3072 pi = (struct GNUNET_PeerIdentity *) &msg[1];
3073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3074 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
3076 t = tunnel_get (pi, ntohl (msg->tid));
3079 /* TODO notify back: we don't know this tunnel */
3080 GNUNET_break_op (0);
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
3084 path = path_new (size);
3086 for (i = 0; i < size; i++)
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
3089 GNUNET_i2s (&pi[i]));
3090 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
3091 if (path->peers[i] == myid)
3094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
3095 if (own_pos < path->length - 1)
3096 send_message (message, &pi[own_pos + 1]);
3098 send_client_tunnel_disconnect(t, NULL);
3100 tunnel_delete_peer (t, path->peers[path->length - 1]);
3101 path_destroy (path);
3107 * Core handler for notifications of broken paths
3109 * @param cls closure
3110 * @param message message
3111 * @param peer peer identity this notification is about
3112 * @param atsi performance data
3113 * @param atsi_count number of records in 'atsi'
3115 * @return GNUNET_OK to keep the connection open,
3116 * GNUNET_SYSERR to close it (signal serious error)
3119 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
3120 const struct GNUNET_MessageHeader *message,
3121 const struct GNUNET_ATS_Information *atsi,
3122 unsigned int atsi_count)
3124 struct GNUNET_MESH_PathBroken *msg;
3125 struct MeshTunnel *t;
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3128 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
3129 msg = (struct GNUNET_MESH_PathBroken *) message;
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3131 GNUNET_i2s (&msg->peer1));
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
3133 GNUNET_i2s (&msg->peer2));
3134 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3137 GNUNET_break_op (0);
3140 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
3141 GNUNET_PEER_search (&msg->peer2));
3148 * Core handler for tunnel destruction
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_tunnel_destroy (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_TunnelDestroy *msg;
3166 struct MeshTunnel *t;
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
3170 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
3172 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
3173 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3176 /* Probably already got the message from another path,
3177 * destroyed the tunnel and retransmitted to children.
3182 if (t->id.oid == myid)
3184 GNUNET_break_op (0);
3187 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3189 /* Tunnel was incoming, notify clients */
3190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
3191 t->local_tid, t->local_tid_dest);
3192 send_clients_tunnel_destroy (t);
3194 tunnel_send_destroy (t);
3201 * Core handler for mesh network traffic going from the origin to a peer
3203 * @param cls closure
3204 * @param peer peer identity this notification is about
3205 * @param message message
3206 * @param atsi performance data
3207 * @param atsi_count number of records in 'atsi'
3208 * @return GNUNET_OK to keep the connection open,
3209 * GNUNET_SYSERR to close it (signal serious error)
3212 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3213 const struct GNUNET_MessageHeader *message,
3214 const struct GNUNET_ATS_Information *atsi,
3215 unsigned int atsi_count)
3217 struct GNUNET_MESH_Unicast *msg;
3218 struct MeshTunnel *t;
3222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
3224 size = ntohs (message->size);
3226 sizeof (struct GNUNET_MESH_Unicast) +
3227 sizeof (struct GNUNET_MessageHeader))
3232 msg = (struct GNUNET_MESH_Unicast *) message;
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3234 ntohs (msg[1].header.type));
3235 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3238 /* TODO notify back: we don't know this tunnel */
3239 GNUNET_break_op (0);
3242 tunnel_reset_timeout (t);
3243 pid = GNUNET_PEER_search (&msg->destination);
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3247 " it's for us! sending to clients...\n");
3248 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3252 " not for us, retransmitting...\n");
3253 send_message (message, tree_get_first_hop (t->tree, pid));
3259 * Core handler for mesh network traffic going from the origin to all peers
3261 * @param cls closure
3262 * @param message message
3263 * @param peer peer identity this notification is about
3264 * @param atsi performance data
3265 * @param atsi_count number of records in 'atsi'
3266 * @return GNUNET_OK to keep the connection open,
3267 * GNUNET_SYSERR to close it (signal serious error)
3269 * TODO: Check who we got this from, to validate route.
3272 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
3273 const struct GNUNET_MessageHeader *message,
3274 const struct GNUNET_ATS_Information *atsi,
3275 unsigned int atsi_count)
3277 struct GNUNET_MESH_Multicast *msg;
3278 struct MeshTunnel *t;
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
3283 size = ntohs (message->size);
3284 if (sizeof (struct GNUNET_MESH_Multicast) +
3285 sizeof (struct GNUNET_MessageHeader) > size)
3287 GNUNET_break_op (0);
3290 msg = (struct GNUNET_MESH_Multicast *) message;
3291 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3295 /* TODO notify that we dont know that tunnel */
3296 GNUNET_break_op (0);
3299 if (t->mid == ntohl (msg->mid))
3301 /* FIXME: already seen this packet, log dropping */
3302 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3303 " Already seen mid %u, DROPPING!\n", t->mid);
3308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3309 " mid %u not seen yet, forwarding\n", ntohl (msg->mid));
3311 t->mid = ntohl (msg->mid);
3312 tunnel_reset_timeout (t);
3314 /* Transmit to locally interested clients */
3315 if (NULL != t->peers &&
3316 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
3318 send_subscribed_clients (message, &msg[1].header);
3320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
3321 if (ntohl (msg->ttl) == 0)
3323 /* FIXME: ttl is 0, log dropping */
3324 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
3327 tunnel_send_multicast (t, message, GNUNET_NO);
3333 * Core handler for mesh network traffic toward the owner of a tunnel
3335 * @param cls closure
3336 * @param message message
3337 * @param peer peer identity this notification is about
3338 * @param atsi performance data
3339 * @param atsi_count number of records in 'atsi'
3341 * @return GNUNET_OK to keep the connection open,
3342 * GNUNET_SYSERR to close it (signal serious error)
3345 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
3346 const struct GNUNET_MessageHeader *message,
3347 const struct GNUNET_ATS_Information *atsi,
3348 unsigned int atsi_count)
3350 struct GNUNET_MESH_ToOrigin *msg;
3351 struct GNUNET_PeerIdentity id;
3352 struct MeshPeerInfo *peer_info;
3353 struct MeshTunnel *t;
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
3358 size = ntohs (message->size);
3359 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
3360 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
3362 GNUNET_break_op (0);
3365 msg = (struct GNUNET_MESH_ToOrigin *) message;
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
3367 ntohs (msg[1].header.type));
3368 t = tunnel_get (&msg->oid, ntohl (msg->tid));
3372 /* TODO notify that we dont know this tunnel (whom)? */
3373 GNUNET_break_op (0);
3377 if (t->id.oid == myid)
3380 struct GNUNET_MESH_ToOrigin *copy;
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3383 " it's for us! sending to clients...\n");
3384 if (NULL == t->owner)
3386 /* got data packet for ownerless tunnel */
3387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
3388 GNUNET_break_op (0);
3391 /* TODO signature verification */
3392 memcpy (cbuf, message, size);
3393 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
3394 copy->tid = htonl (t->local_tid);
3395 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3396 ©->header, GNUNET_YES);
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3400 " not for us, retransmitting...\n");
3402 peer_info = peer_info_get (&msg->oid);
3403 if (NULL == peer_info)
3405 /* unknown origin of tunnel */
3409 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3410 send_message (message, &id);
3417 * Core handler for path ACKs
3419 * @param cls closure
3420 * @param message message
3421 * @param peer peer identity this notification is about
3422 * @param atsi performance data
3423 * @param atsi_count number of records in 'atsi'
3425 * @return GNUNET_OK to keep the connection open,
3426 * GNUNET_SYSERR to close it (signal serious error)
3429 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
3430 const struct GNUNET_MessageHeader *message,
3431 const struct GNUNET_ATS_Information *atsi,
3432 unsigned int atsi_count)
3434 struct GNUNET_MESH_PathACK *msg;
3435 struct GNUNET_PeerIdentity id;
3436 struct MeshPeerInfo *peer_info;
3437 struct MeshPeerPath *p;
3438 struct MeshTunnel *t;
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
3441 GNUNET_i2s (&my_full_id));
3442 msg = (struct GNUNET_MESH_PathACK *) message;
3443 t = tunnel_get (&msg->oid, msg->tid);
3446 /* TODO notify that we don't know the tunnel */
3450 peer_info = peer_info_get (&msg->peer_id);
3452 /* Add paths to peers? */
3453 p = tree_get_path_to_peer (t->tree, peer_info->id);
3456 path_add_to_peers (p, GNUNET_YES);
3464 /* Message for us? */
3465 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
3467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
3468 if (NULL == t->owner)
3470 GNUNET_break_op (0);
3473 if (NULL != t->dht_get_type)
3475 GNUNET_DHT_get_stop (t->dht_get_type);
3476 t->dht_get_type = NULL;
3478 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
3480 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
3481 send_client_peer_connected (t, peer_info->id);
3486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3487 " not for us, retransmitting...\n");
3488 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3489 peer_info = peer_info_get (&msg->oid);
3490 if (NULL == peer_info)
3492 /* If we know the tunnel, we should DEFINITELY know the peer */
3496 send_message (message, &id);
3502 * Functions to handle messages from core
3504 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
3505 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
3506 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
3507 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
3508 sizeof (struct GNUNET_MESH_PathBroken)},
3509 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, 0},
3510 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
3511 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
3512 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
3513 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
3514 sizeof (struct GNUNET_MESH_PathACK)},
3520 /******************************************************************************/
3521 /**************** MESH LOCAL HANDLER HELPERS ***********************/
3522 /******************************************************************************/
3525 * deregister_app: iterator for removing each application registered by a client
3527 * @param cls closure
3528 * @param key the hash of the application id (used to access the hashmap)
3529 * @param value the value stored at the key (client)
3531 * @return GNUNET_OK on success
3534 deregister_app (void *cls, const GNUNET_HashCode * key, void *value)
3536 GNUNET_break (GNUNET_YES ==
3537 GNUNET_CONTAINER_multihashmap_remove (applications, key,
3544 * notify_client_connection_failure: notify a client that the connection to the
3545 * requested remote peer is not possible (for instance, no route found)
3546 * Function called when the socket is ready to queue more data. "buf" will be
3547 * NULL and "size" zero if the socket was closed for writing in the meantime.
3549 * @param cls closure
3550 * @param size number of bytes available in buf
3551 * @param buf where the callee should write the message
3552 * @return number of bytes written to buf
3555 notify_client_connection_failure (void *cls, size_t size, void *buf)
3558 struct MeshPeerInfo *peer_info;
3559 struct GNUNET_MESH_PeerControl *msg;
3560 struct GNUNET_PeerIdentity id;
3562 if (0 == size && NULL == buf)
3564 // TODO retry? cancel?
3568 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
3569 peer_info = (struct MeshPeerInfo *) cls;
3570 msg = (struct GNUNET_MESH_PeerControl *) buf;
3571 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
3572 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
3573 // msg->tunnel_id = htonl(peer_info->t->tid);
3574 GNUNET_PEER_resolve (peer_info->id, &id);
3575 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
3583 * Send keepalive packets for a peer
3585 * @param cls Closure (tunnel for which to send the keepalive).
3586 * @param tc Notification context.
3588 * TODO: implement explicit multicast keepalive?
3591 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3593 struct MeshTunnel *t = cls;
3594 struct GNUNET_MessageHeader *payload;
3595 struct GNUNET_MESH_Multicast *msg;
3597 sizeof (struct GNUNET_MESH_Multicast) +
3598 sizeof (struct GNUNET_MessageHeader);
3601 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
3605 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
3607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3608 "sending keepalive for tunnel %d\n", t->id.tid);
3610 msg = (struct GNUNET_MESH_Multicast *) cbuf;
3611 msg->header.size = htons (size);
3612 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
3613 msg->oid = my_full_id;
3614 msg->tid = htonl (t->id.tid);
3615 msg->ttl = htonl (DEFAULT_TTL);
3616 msg->mid = htonl (t->mid + 1);
3618 payload = (struct GNUNET_MessageHeader *) &msg[1];
3619 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
3620 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
3621 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
3623 t->path_refresh_task =
3624 GNUNET_SCHEDULER_add_delayed (REFRESH_PATH_TIME, &path_refresh, t);
3630 * Function to process paths received for a new peer addition. The recorded
3631 * paths form the initial tunnel, which can be optimized later.
3632 * Called on each result obtained for the DHT search.
3634 * @param cls closure
3635 * @param exp when will this value expire
3636 * @param key key of the result
3637 * @param get_path path of the get request
3638 * @param get_path_length lenght of get_path
3639 * @param put_path path of the put request
3640 * @param put_path_length length of the put_path
3641 * @param type type of the result
3642 * @param size number of bytes in data
3643 * @param data pointer to the result data
3645 * TODO: re-issue the request after certain time? cancel after X results?
3648 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3649 const GNUNET_HashCode * key,
3650 const struct GNUNET_PeerIdentity *get_path,
3651 unsigned int get_path_length,
3652 const struct GNUNET_PeerIdentity *put_path,
3653 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3654 size_t size, const void *data)
3656 struct MeshPathInfo *path_info = cls;
3657 struct MeshPeerPath *p;
3658 struct GNUNET_PeerIdentity pi;
3661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
3662 GNUNET_PEER_resolve (path_info->peer->id, &pi);
3663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
3665 p = path_build_from_dht (get_path, get_path_length, put_path,
3667 path_add_to_peers (p, GNUNET_NO);
3669 for (i = 0; i < path_info->peer->ntunnels; i++)
3671 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
3672 peer_info_connect (path_info->peer, path_info->t);
3680 * Function to process paths received for a new peer addition. The recorded
3681 * paths form the initial tunnel, which can be optimized later.
3682 * Called on each result obtained for the DHT search.
3684 * @param cls closure
3685 * @param exp when will this value expire
3686 * @param key key of the result
3687 * @param get_path path of the get request
3688 * @param get_path_length lenght of get_path
3689 * @param put_path path of the put request
3690 * @param put_path_length length of the put_path
3691 * @param type type of the result
3692 * @param size number of bytes in data
3693 * @param data pointer to the result data
3696 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
3697 const GNUNET_HashCode * key,
3698 const struct GNUNET_PeerIdentity *get_path,
3699 unsigned int get_path_length,
3700 const struct GNUNET_PeerIdentity *put_path,
3701 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
3702 size_t size, const void *data)
3704 const struct GNUNET_PeerIdentity *pi = data;
3705 struct MeshTunnel *t = cls;
3706 struct MeshPeerInfo *peer_info;
3707 struct MeshPeerPath *p;
3709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
3710 if (size != sizeof (struct GNUNET_PeerIdentity))
3712 GNUNET_break_op (0);
3715 GNUNET_assert (NULL != t->owner);
3716 peer_info = peer_info_get (pi);
3717 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
3719 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
3721 p = path_build_from_dht (get_path, get_path_length, put_path,
3723 path_add_to_peers (p, GNUNET_NO);
3725 tunnel_add_peer (t, peer_info);
3726 peer_info_connect (peer_info, t);
3730 /******************************************************************************/
3731 /********************* MESH LOCAL HANDLES **************************/
3732 /******************************************************************************/
3736 * Handler for client disconnection
3738 * @param cls closure
3739 * @param client identification of the client; NULL
3740 * for the last call when the server is destroyed
3743 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
3745 struct MeshClient *c;
3746 struct MeshClient *next;
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
3757 if (c->handle != client)
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
3763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
3765 GNUNET_SERVER_client_drop (c->handle);
3766 c->shutting_down = GNUNET_YES;
3767 GNUNET_assert (NULL != c->own_tunnels);
3768 GNUNET_assert (NULL != c->incoming_tunnels);
3769 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
3770 &tunnel_destroy_iterator, c);
3771 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
3772 &tunnel_destroy_iterator, c);
3773 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
3774 &tunnel_destroy_iterator, c);
3775 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
3776 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
3777 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
3779 /* deregister clients applications */
3780 if (NULL != c->apps)
3782 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, NULL);
3783 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
3785 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
3786 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
3788 GNUNET_SCHEDULER_cancel (announce_applications_task);
3789 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
3791 if (NULL != c->types)
3792 GNUNET_CONTAINER_multihashmap_destroy (c->types);
3794 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
3795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
3799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
3805 * Handler for new clients
3807 * @param cls closure
3808 * @param client identification of the client
3809 * @param message the actual message, which includes messages the client wants
3812 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
3813 const struct GNUNET_MessageHeader *message)
3815 struct GNUNET_MESH_ClientConnect *cc_msg;
3816 struct MeshClient *c;
3817 GNUNET_MESH_ApplicationType *a;
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
3825 /* Check data sanity */
3826 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
3827 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
3828 ntypes = ntohs (cc_msg->types);
3829 napps = ntohs (cc_msg->applications);
3831 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
3834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3838 /* Create new client structure */
3839 c = GNUNET_malloc (sizeof (struct MeshClient));
3840 c->id = next_client_id++;
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
3843 GNUNET_SERVER_client_keep (client);
3844 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
3847 GNUNET_MESH_ApplicationType at;
3850 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
3851 for (i = 0; i < napps; i++)
3854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
3855 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
3856 /* store in clients hashmap */
3857 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, c,
3858 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3859 /* store in global hashmap, for announcements */
3860 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
3861 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3863 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
3864 announce_applications_task =
3865 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
3873 t = (uint16_t *) & a[napps];
3874 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
3875 for (i = 0; i < ntypes; i++)
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
3879 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
3881 /* store in clients hashmap */
3882 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
3883 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3884 /* store in global hashmap */
3885 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
3886 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3890 " client has %u+%u subscriptions\n", napps, ntypes);
3892 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
3893 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3894 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3895 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
3896 GNUNET_SERVER_notification_context_add (nc, client);
3898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
3904 * Handler for requests of new tunnels
3906 * @param cls closure
3907 * @param client identification of the client
3908 * @param message the actual message
3911 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
3912 const struct GNUNET_MessageHeader *message)
3914 struct GNUNET_MESH_TunnelMessage *t_msg;
3915 struct MeshTunnel *t;
3916 struct MeshClient *c;
3917 GNUNET_HashCode hash;
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
3921 /* Sanity check for client registration */
3922 if (NULL == (c = client_get (client)))
3925 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3930 /* Message sanity check */
3931 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
3934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3938 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
3939 /* Sanity check for tunnel numbering */
3940 if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
3943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3946 /* Sanity check for duplicate tunnel IDs */
3947 if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
3950 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3954 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3955 while (NULL != tunnel_get_by_pi (myid, next_tid))
3956 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3957 t->id.tid = next_tid++;
3958 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
3960 t->local_tid = ntohl (t_msg->tunnel_id);
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
3962 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
3964 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
3966 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3968 GNUNET_CONTAINER_multihashmap_put (c->own_tunnels, &hash, t,
3969 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3976 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3978 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3979 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3985 t->tree = tree_new (myid);
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
3988 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3994 * Handler for requests of deleting tunnels
3996 * @param cls closure
3997 * @param client identification of the client
3998 * @param message the actual message
4001 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
4002 const struct GNUNET_MessageHeader *message)
4004 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
4005 struct MeshClient *c;
4006 struct MeshTunnel *t;
4007 MESH_TunnelNumber tid;
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4010 "Got a DESTROY TUNNEL from client!\n");
4012 /* Sanity check for client registration */
4013 if (NULL == (c = client_get (client)))
4016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4019 /* Message sanity check */
4020 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
4023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4027 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
4029 /* Retrieve tunnel */
4030 tid = ntohl (tunnel_msg->tunnel_id);
4031 t = tunnel_get_by_local_id(c, tid);
4035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
4036 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4039 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4041 client_ignore_tunnel (c, t);
4043 // TODO: when to destroy incoming tunnel?
4044 if (t->nclients == 0)
4046 GNUNET_assert (GNUNET_YES ==
4047 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
4049 GNUNET_assert (GNUNET_YES ==
4050 GNUNET_CONTAINER_multihashmap_remove (t->peers,
4051 &my_full_id.hashPubKey,
4055 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4058 send_client_tunnel_disconnect(t, c);
4059 client_delete_tunnel(c, t);
4061 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
4063 tunnel_send_destroy (t);
4065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4071 * Handler for connection requests to new peers
4073 * @param cls closure
4074 * @param client identification of the client
4075 * @param message the actual message (PeerControl)
4078 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
4079 const struct GNUNET_MessageHeader *message)
4081 struct GNUNET_MESH_PeerControl *peer_msg;
4082 struct MeshPeerInfo *peer_info;
4083 struct MeshClient *c;
4084 struct MeshTunnel *t;
4085 MESH_TunnelNumber tid;
4087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
4088 /* Sanity check for client registration */
4089 if (NULL == (c = client_get (client)))
4092 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4096 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4097 /* Sanity check for message size */
4098 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4105 /* Tunnel exists? */
4106 tid = ntohl (peer_msg->tunnel_id);
4107 t = tunnel_get_by_local_id (c, tid);
4111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4115 /* Does client own tunnel? */
4116 if (t->owner->handle != client)
4119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
4123 GNUNET_i2s (&peer_msg->peer));
4124 peer_info = peer_info_get (&peer_msg->peer);
4126 tunnel_add_peer (t, peer_info);
4127 peer_info_connect (peer_info, t);
4129 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4135 * Handler for disconnection requests of peers in a tunnel
4137 * @param cls closure
4138 * @param client identification of the client
4139 * @param message the actual message (PeerControl)
4142 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
4143 const struct GNUNET_MessageHeader *message)
4145 struct GNUNET_MESH_PeerControl *peer_msg;
4146 struct MeshPeerInfo *peer_info;
4147 struct MeshClient *c;
4148 struct MeshTunnel *t;
4149 MESH_TunnelNumber tid;
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
4152 /* Sanity check for client registration */
4153 if (NULL == (c = client_get (client)))
4156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4159 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
4160 /* Sanity check for message size */
4161 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
4164 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4168 /* Tunnel exists? */
4169 tid = ntohl (peer_msg->tunnel_id);
4170 t = tunnel_get_by_local_id (c, tid);
4174 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
4179 /* Does client own tunnel? */
4180 if (t->owner->handle != client)
4183 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
4188 GNUNET_i2s (&peer_msg->peer));
4189 /* Is the peer in the tunnel? */
4191 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
4192 if (NULL == peer_info)
4195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4199 /* Ok, delete peer from tunnel */
4200 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
4201 &peer_msg->peer.hashPubKey);
4203 send_destroy_path (t, peer_info->id);
4204 tunnel_delete_peer (t, peer_info->id);
4205 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4211 * Handler for connection requests to new peers by type
4213 * @param cls closure
4214 * @param client identification of the client
4215 * @param message the actual message (ConnectPeerByType)
4218 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
4219 const struct GNUNET_MessageHeader *message)
4221 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
4222 struct MeshClient *c;
4223 struct MeshTunnel *t;
4224 GNUNET_HashCode hash;
4225 MESH_TunnelNumber tid;
4227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
4228 /* Sanity check for client registration */
4229 if (NULL == (c = client_get (client)))
4232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4236 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
4237 /* Sanity check for message size */
4238 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
4239 ntohs (connect_msg->header.size))
4242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4246 /* Tunnel exists? */
4247 tid = ntohl (connect_msg->tunnel_id);
4248 t = tunnel_get_by_local_id (c, tid);
4252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4256 /* Does client own tunnel? */
4257 if (t->owner->handle != client)
4260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4264 /* Do WE have the service? */
4265 t->type = ntohl (connect_msg->type);
4266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
4267 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
4268 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
4271 /* Yes! Fast forward, add ourselves to the tunnel and send the
4272 * good news to the client, and alert the destination client of
4273 * an incoming tunnel.
4275 * FIXME send a path create to self, avoid code duplication
4277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
4278 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
4279 peer_info_get (&my_full_id),
4280 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
4283 send_client_peer_connected (t, myid);
4284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
4285 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4287 t->local_tid_dest = next_local_tid++;
4288 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4289 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4290 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
4294 /* Ok, lets find a peer offering the service */
4295 if (NULL != t->dht_get_type)
4297 GNUNET_DHT_get_stop (t->dht_get_type);
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
4300 GNUNET_h2s (&hash));
4302 GNUNET_DHT_get_start (dht_handle,
4303 GNUNET_BLOCK_TYPE_TEST, &hash, 10,
4304 GNUNET_DHT_RO_RECORD_ROUTE |
4305 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, 0,
4306 &dht_get_type_handler, t);
4308 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4314 * Handler for client traffic directed to one peer
4316 * @param cls closure
4317 * @param client identification of the client
4318 * @param message the actual message
4321 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
4322 const struct GNUNET_MessageHeader *message)
4324 struct MeshClient *c;
4325 struct MeshTunnel *t;
4326 struct MeshPeerInfo *pi;
4327 struct GNUNET_MESH_Unicast *data_msg;
4328 MESH_TunnelNumber tid;
4331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4332 "Got a unicast request from a client!\n");
4334 /* Sanity check for client registration */
4335 if (NULL == (c = client_get (client)))
4338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4341 data_msg = (struct GNUNET_MESH_Unicast *) message;
4342 /* Sanity check for message size */
4343 size = ntohs (message->size);
4344 if (sizeof (struct GNUNET_MESH_Unicast) +
4345 sizeof (struct GNUNET_MessageHeader) > size)
4348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4352 /* Tunnel exists? */
4353 tid = ntohl (data_msg->tid);
4354 t = tunnel_get_by_local_id (c, tid);
4358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4362 /* Is it a local tunnel? Then, does client own the tunnel? */
4363 if (t->owner->handle != client)
4366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4370 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
4371 &data_msg->destination.hashPubKey);
4372 /* Is the selected peer in the tunnel? */
4376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4380 /* Ok, everything is correct, send the message
4381 * (pretend we got it from a mesh peer)
4384 char buf[ntohs (message->size)] GNUNET_ALIGN;
4385 struct GNUNET_MESH_Unicast *copy;
4387 /* Work around const limitation */
4388 copy = (struct GNUNET_MESH_Unicast *) buf;
4389 memcpy (buf, data_msg, size);
4390 copy->oid = my_full_id;
4391 copy->tid = htonl (t->id.tid);
4392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4393 " calling generic handler...\n");
4394 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
4396 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4402 * Handler for client traffic directed to the origin
4404 * @param cls closure
4405 * @param client identification of the client
4406 * @param message the actual message
4409 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
4410 const struct GNUNET_MessageHeader *message)
4412 struct GNUNET_MESH_ToOrigin *data_msg;
4413 struct GNUNET_PeerIdentity id;
4414 struct MeshClient *c;
4415 struct MeshTunnel *t;
4416 MESH_TunnelNumber tid;
4419 /* Sanity check for client registration */
4420 if (NULL == (c = client_get (client)))
4423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4426 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
4427 /* Sanity check for message size */
4428 size = ntohs (message->size);
4429 if (sizeof (struct GNUNET_MESH_ToOrigin) +
4430 sizeof (struct GNUNET_MessageHeader) > size)
4433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4437 /* Tunnel exists? */
4438 tid = ntohl (data_msg->tid);
4439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4440 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
4441 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4447 t = tunnel_get_by_local_id (c, tid);
4451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4455 /* It should be sent by someone who has this as incoming tunnel. */
4456 if (-1 == client_knows_tunnel (c, t))
4459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4462 GNUNET_PEER_resolve (t->id.oid, &id);
4464 /* Ok, everything is correct, send the message
4465 * (pretend we got it from a mesh peer)
4468 char buf[ntohs (message->size)] GNUNET_ALIGN;
4469 struct GNUNET_MESH_ToOrigin *copy;
4471 /* Work around const limitation */
4472 copy = (struct GNUNET_MESH_ToOrigin *) buf;
4473 memcpy (buf, data_msg, size);
4475 copy->tid = htonl (t->id.tid);
4476 copy->sender = my_full_id;
4477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4478 " calling generic handler...\n");
4479 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
4481 GNUNET_SERVER_receive_done (client, GNUNET_OK);
4487 * Handler for client traffic directed to all peers in a tunnel
4489 * @param cls closure
4490 * @param client identification of the client
4491 * @param message the actual message
4494 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
4495 const struct GNUNET_MessageHeader *message)
4497 struct MeshClient *c;
4498 struct MeshTunnel *t;
4499 struct GNUNET_MESH_Multicast *data_msg;
4500 MESH_TunnelNumber tid;
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4503 "Got a multicast request from a client!\n");
4505 /* Sanity check for client registration */
4506 if (NULL == (c = client_get (client)))
4509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4512 data_msg = (struct GNUNET_MESH_Multicast *) message;
4513 /* Sanity check for message size */
4514 if (sizeof (struct GNUNET_MESH_Multicast) +
4515 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
4518 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4522 /* Tunnel exists? */
4523 tid = ntohl (data_msg->tid);
4524 t = tunnel_get_by_local_id (c, tid);
4528 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4532 /* Does client own tunnel? */
4533 if (t->owner->handle != client)
4536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
4541 char buf[ntohs (message->size)] GNUNET_ALIGN;
4542 struct GNUNET_MESH_Multicast *copy;
4544 copy = (struct GNUNET_MESH_Multicast *) buf;
4545 memcpy (buf, message, ntohs (message->size));
4546 copy->oid = my_full_id;
4547 copy->tid = htonl (t->id.tid);
4548 copy->ttl = htonl (DEFAULT_TTL);
4549 copy->mid = htonl (t->mid + 1);
4550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4551 " calling generic handler...\n");
4552 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
4555 /* receive done gets called when last copy is sent to a neighbor */
4560 * Functions to handle messages from clients
4562 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
4563 {&handle_local_new_client, NULL,
4564 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
4565 {&handle_local_tunnel_create, NULL,
4566 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
4567 sizeof (struct GNUNET_MESH_TunnelMessage)},
4568 {&handle_local_tunnel_destroy, NULL,
4569 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
4570 sizeof (struct GNUNET_MESH_TunnelMessage)},
4571 {&handle_local_connect_add, NULL,
4572 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
4573 sizeof (struct GNUNET_MESH_PeerControl)},
4574 {&handle_local_connect_del, NULL,
4575 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
4576 sizeof (struct GNUNET_MESH_PeerControl)},
4577 {&handle_local_connect_by_type, NULL,
4578 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
4579 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
4580 {&handle_local_unicast, NULL,
4581 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
4582 {&handle_local_to_origin, NULL,
4583 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
4584 {&handle_local_multicast, NULL,
4585 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
4591 * To be called on core init/fail.
4593 * @param cls service closure
4594 * @param server handle to the server for this service
4595 * @param identity the public identity of this peer
4598 core_init (void *cls, struct GNUNET_CORE_Handle *server,
4599 const struct GNUNET_PeerIdentity *identity)
4601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
4602 core_handle = server;
4603 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
4606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
4607 GNUNET_SCHEDULER_shutdown ();
4613 * Method called whenever a given peer connects.
4615 * @param cls closure
4616 * @param peer peer identity this notification is about
4617 * @param atsi performance data for the connection
4618 * @param atsi_count number of records in 'atsi'
4621 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
4622 const struct GNUNET_ATS_Information *atsi,
4623 unsigned int atsi_count)
4625 struct MeshPeerInfo *peer_info;
4626 struct MeshPeerPath *path;
4628 DEBUG_CONN ("Peer connected\n");
4629 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
4630 peer_info = peer_info_get (peer);
4631 if (myid == peer_info->id)
4633 DEBUG_CONN (" (self)\n");
4638 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
4640 path = path_new (2);
4641 path->peers[0] = myid;
4642 path->peers[1] = peer_info->id;
4643 GNUNET_PEER_change_rc (myid, 1);
4644 GNUNET_PEER_change_rc (peer_info->id, 1);
4645 peer_info_add_path (peer_info, path, GNUNET_YES);
4650 * Method called whenever a peer disconnects.
4652 * @param cls closure
4653 * @param peer peer identity this notification is about
4656 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
4658 struct MeshPeerInfo *pi;
4659 struct MeshPeerQueue *q;
4660 struct MeshPeerQueue *n;
4662 DEBUG_CONN ("Peer disconnected\n");
4663 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4675 /* try to reroute this traffic instead */
4676 queue_destroy(q, GNUNET_YES);
4680 peer_info_remove_path (pi, pi->id, myid);
4683 DEBUG_CONN (" (self)\n");
4689 /******************************************************************************/
4690 /************************ MAIN FUNCTIONS ****************************/
4691 /******************************************************************************/
4694 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
4696 * @param cls closure
4697 * @param key current key code
4698 * @param value value in the hash map
4699 * @return GNUNET_YES if we should continue to iterate,
4703 shutdown_tunnel (void *cls, const GNUNET_HashCode * key, void *value)
4705 struct MeshTunnel *t = value;
4712 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
4714 * @param cls closure
4715 * @param key current key code
4716 * @param value value in the hash map
4717 * @return GNUNET_YES if we should continue to iterate,
4721 shutdown_peer (void *cls, const GNUNET_HashCode * key, void *value)
4723 struct MeshPeerInfo *p = value;
4724 struct MeshPeerQueue *q;
4725 struct MeshPeerQueue *n;
4733 queue_destroy(q, GNUNET_YES);
4737 peer_info_destroy (p);
4742 * Task run during shutdown.
4748 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
4752 if (core_handle != NULL)
4754 GNUNET_CORE_disconnect (core_handle);
4757 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
4758 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
4759 if (dht_handle != NULL)
4761 GNUNET_DHT_disconnect (dht_handle);
4766 GNUNET_SERVER_notification_context_destroy (nc);
4769 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
4771 GNUNET_SCHEDULER_cancel (announce_id_task);
4772 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
4778 * Process mesh requests.
4780 * @param cls closure
4781 * @param server the initialized server
4782 * @param c configuration to use
4785 run (void *cls, struct GNUNET_SERVER_Handle *server,
4786 const struct GNUNET_CONFIGURATION_Handle *c)
4788 struct MeshPeerInfo *peer;
4789 struct MeshPeerPath *p;
4792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
4793 server_handle = server;
4794 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
4796 NULL, /* Closure passed to MESH functions */
4797 &core_init, /* Call core_init once connected */
4798 &core_connect, /* Handle connects */
4799 &core_disconnect, /* remove peers on disconnects */
4800 NULL, /* Don't notify about all incoming messages */
4801 GNUNET_NO, /* For header only in notification */
4802 NULL, /* Don't notify about all outbound messages */
4803 GNUNET_NO, /* For header-only out notification */
4804 core_handlers); /* Register these handlers */
4806 if (core_handle == NULL)
4809 GNUNET_SCHEDULER_shutdown ();
4814 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4817 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4819 ("Mesh service is lacking key configuration settings. Exiting.\n"));
4820 GNUNET_SCHEDULER_shutdown ();
4823 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4824 GNUNET_free (keyfile);
4825 if (my_private_key == NULL)
4827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4828 _("Mesh service could not access hostkey. Exiting.\n"));
4829 GNUNET_SCHEDULER_shutdown ();
4832 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4833 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4834 &my_full_id.hashPubKey);
4835 myid = GNUNET_PEER_intern (&my_full_id);
4837 // transport_handle = GNUNET_TRANSPORT_connect(c,
4844 dht_handle = GNUNET_DHT_connect (c, 64);
4845 if (dht_handle == NULL)
4851 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4853 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4854 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
4855 peers = GNUNET_CONTAINER_multihashmap_create (32);
4856 applications = GNUNET_CONTAINER_multihashmap_create (32);
4857 types = GNUNET_CONTAINER_multihashmap_create (32);
4859 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
4860 nc = GNUNET_SERVER_notification_context_create (server_handle,
4862 GNUNET_SERVER_disconnect_notify (server_handle,
4863 &handle_local_client_disconnect, NULL);
4867 clients_tail = NULL;
4870 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
4871 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
4873 /* Create a peer_info for the local peer */
4874 peer = peer_info_get (&my_full_id);
4877 GNUNET_PEER_change_rc (myid, 1);
4878 peer_info_add_path (peer, p, GNUNET_YES);
4880 /* Scheduled the task to clean up when shutdown is called */
4881 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
4884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
4888 * The main function for the mesh service.
4890 * @param argc number of arguments from the command line
4891 * @param argv command line arguments
4892 * @return 0 ok, 1 on error
4895 main (int argc, char *const *argv)
4899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
4902 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
4904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");