2 This file is part of GNUnet.
3 Copyright (C) 2009-2015 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.
21 * @file dht/gnunet-service-wdht_neighbours.c
22 * @brief GNUnet DHT service's finger and friend table management code
23 * @author Supriti Singh
26 #include "gnunet_util_lib.h"
27 #include "gnunet_block_lib.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet_core_service.h"
33 #include "gnunet_datacache_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_dht_service.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet-service-wdht.h"
38 #include "gnunet-service-wdht_clients.h"
39 #include "gnunet-service-wdht_datacache.h"
40 #include "gnunet-service-wdht_neighbours.h"
41 #include "gnunet-service-wdht_nse.h"
48 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
51 * Trail timeout. After what time do trails always die?
53 #define TRAIL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 42)
56 * Random walk delay. How often do we walk the overlay?
58 #define RANDOM_WALK_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 42)
61 * The number of layered ID to use.
63 #define NUMBER_LAYERED_ID 8
66 * The number of random walk to launch at the beginning of the initialization
68 /* FIXME: find a better value */
69 #define NUMBER_RANDOM_WALK 20
72 /******************* The db structure and related functions *******************/
75 * Entry in #friends_peermap.
81 * Information we keep per trail.
87 * MDLL entry in the list of all trails with the same predecessor.
89 struct Trail *prev_succ;
92 * MDLL entry in the list of all trails with the same predecessor.
94 struct Trail *next_succ;
97 * MDLL entry in the list of all trails with the same predecessor.
99 struct Trail *prev_pred;
102 * MDLL entry in the list of all trails with the same predecessor.
104 struct Trail *next_pred;
107 * Our predecessor in the trail, NULL if we are initiator (?).
109 struct FriendInfo *pred;
112 * Our successor in the trail, NULL if we are the last peer.
114 struct FriendInfo *succ;
117 * Identifier of the trail with the predecessor.
119 struct GNUNET_HashCode pred_id;
122 * Identifier of the trail with the successor.
124 struct GNUNET_HashCode succ_id;
127 * When does this trail expire.
129 struct GNUNET_TIME_Absolute expiration_time;
132 * Location of this trail in the heap.
134 struct GNUNET_CONTAINER_HeapNode *hn;
137 * If this peer started the to create a Finger (and thus @e pred is
138 * NULL), this is the Finger we are trying to intialize.
140 struct Finger **finger;
146 * Entry in #friends_peermap.
153 struct GNUNET_PeerIdentity id;
158 struct Trail *pred_head;
163 struct Trail *pred_tail;
168 struct Trail *succ_head;
173 struct Trail *succ_tail;
176 * Core handle for sending messages to this friend.
178 struct GNUNET_MQ_Handle *mq;
202 struct FingerTable *ft;
207 struct GNUNET_HashCode destination;
210 * #GNUNET_YES if a response has been received. Otherwise #GNUNET_NO.
219 * Array of our fingers, unsorted.
221 struct Finger **fingers;
224 * Array of sorted fingers (sorted by destination, valid fingers first).
226 struct Finger **sorted_fingers;
229 * Size of the finger array.
231 unsigned int finger_array_size;
234 * Number of valid entries in @e sorted_fingers (contiguous from offset 0)
236 unsigned int number_valid_fingers;
239 * Which offset in @e fingers will we redo next.
241 unsigned int walk_offset;
244 * Is the finger array sorted?
251 /*********************** end of the db structure part ***********************/
254 GNUNET_NETWORK_STRUCT_BEGIN
257 * Setup a finger using the underlay topology ("social network").
259 struct RandomWalkMessage
262 * Type: #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK
264 struct GNUNET_MessageHeader header;
267 * Number of hops this message has taken so far, we stop at
270 uint16_t hops_taken GNUNET_PACKED;
273 * Layer for the request, in NBO.
275 uint16_t layer GNUNET_PACKED;
278 * Unique (random) identifier this peer will use to
279 * identify the trail (in future messages).
281 struct GNUNET_HashCode trail_id;
286 * Response to a `struct RandomWalkMessage`.
288 struct RandomWalkResponseMessage
291 * Type: #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE
293 struct GNUNET_MessageHeader header;
296 * Zero, for alignment.
298 uint32_t reserved GNUNET_PACKED;
301 * Unique (random) identifier from the
302 * `struct RandomWalkMessage`.
304 struct GNUNET_HashCode trail_id;
307 * Random location in the respective layer where the
308 * random path of the finger setup terminated.
310 struct GNUNET_HashCode location;
315 * Response to an event that causes a trail to die.
317 struct TrailDestroyMessage
320 * Type: #GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY
322 struct GNUNET_MessageHeader header;
325 * Zero, for alignment.
327 uint32_t reserved GNUNET_PACKED;
330 * Unique (random) identifier this peer will use to
331 * identify the finger (in future messages).
333 struct GNUNET_HashCode trail_id;
339 * Send a message along a trail.
341 struct FindSuccessorMessage
344 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FIND_SUCCESSOR
346 struct GNUNET_MessageHeader header;
349 * Zero, for alignment.
351 uint32_t reserved GNUNET_PACKED;
354 * Unique (random) identifier this peer will use to
355 * identify the finger (in future messages).
357 struct GNUNET_HashCode trail_id;
360 * Key for which we would like close values returned.
361 * identify the finger (in future messages).
363 struct GNUNET_HashCode key;
369 * Send a message along a trail.
371 struct TrailRouteMessage
374 * Type: #GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE
376 struct GNUNET_MessageHeader header;
379 * #GNUNET_YES if the path should be recorded, #GNUNET_NO if not; in NBO.
381 uint16_t record_path GNUNET_PACKED;
384 * Length of the recorded trail, 0 if @e record_path is #GNUNET_NO; in NBO.
386 uint16_t path_length GNUNET_PACKED;
389 * Unique (random) identifier this peer will use to
390 * identify the finger (in future messages).
392 struct GNUNET_HashCode trail_id;
395 * Path the message has taken so far (excluding sender).
397 /* struct GNUNET_PeerIdentity path[path_length]; */
399 /* followed by payload (another `struct GNUNET_MessageHeader`) to
400 send along the trail */
407 struct PeerPutMessage
410 * Type: #GNUNET_MESSAGE_TYPE_WDHT_PUT
412 struct GNUNET_MessageHeader header;
417 uint32_t options GNUNET_PACKED;
422 uint32_t block_type GNUNET_PACKED;
427 uint32_t hop_count GNUNET_PACKED;
430 * Replication level for this message
431 * In the current implementation, this value is not used.
433 uint32_t desired_replication_level GNUNET_PACKED;
436 * Length of the PUT path that follows (if tracked).
438 uint32_t put_path_length GNUNET_PACKED;
441 * When does the content expire?
443 struct GNUNET_TIME_AbsoluteNBO expiration_time;
446 * The key to store the value under.
448 struct GNUNET_HashCode key GNUNET_PACKED;
450 /* put path (if tracked) */
459 struct PeerGetMessage
462 * Type: #GNUNET_MESSAGE_TYPE_WDHT_GET
464 struct GNUNET_MessageHeader header;
469 uint32_t options GNUNET_PACKED;
472 * Desired content type.
474 uint32_t block_type GNUNET_PACKED;
479 uint32_t hop_count GNUNET_PACKED;
482 * Desired replication level for this request.
483 * In the current implementation, this value is not used.
485 uint32_t desired_replication_level GNUNET_PACKED;
488 * Total number of peers in get path.
490 unsigned int get_path_length;
493 * The key we are looking for.
495 struct GNUNET_HashCode key;
498 /* struct GNUNET_PeerIdentity[]*/
505 struct PeerGetResultMessage
508 * Type: #GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT
510 struct GNUNET_MessageHeader header;
513 * The type for the data.
515 uint32_t type GNUNET_PACKED;
518 * Number of peers recorded in the outgoing path from source to the
519 * stored location of this message.
521 uint32_t put_path_length GNUNET_PACKED;
524 * Length of the GET path that follows (if tracked).
526 uint32_t get_path_length GNUNET_PACKED;
529 * Peer which queried for get and should get the result.
531 struct GNUNET_PeerIdentity querying_peer;
534 * When does the content expire?
536 struct GNUNET_TIME_Absolute expiration_time;
539 * The key of the corresponding GET request.
541 struct GNUNET_HashCode key;
543 /* put path (if tracked) */
545 /* get path (if tracked) */
551 GNUNET_NETWORK_STRUCT_END
555 * Contains all the layered IDs of this peer.
557 struct GNUNET_PeerIdentity layered_id[NUMBER_LAYERED_ID];
560 * Task to timeout trails that have expired.
562 static struct GNUNET_SCHEDULER_Task *trail_timeout_task;
565 * Task to perform random walks.
567 static struct GNUNET_SCHEDULER_Task *random_walk_task;
570 * Identity of this peer.
572 static struct GNUNET_PeerIdentity my_identity;
575 * Peer map of all the friends of a peer
577 static struct GNUNET_CONTAINER_MultiPeerMap *friends_peermap;
582 static struct FingerTable fingers[NUMBER_LAYERED_ID];
585 * Tail map, mapping tail identifiers to `struct Trail`s
587 static struct GNUNET_CONTAINER_MultiHashMap *trail_map;
590 * Tail heap, organizing trails by expiration time.
592 static struct GNUNET_CONTAINER_Heap *trail_heap;
597 static struct GNUNET_CORE_Handle *core_api;
601 * Handle the put request from the client.
603 * @param key Key for the content
604 * @param block_type Type of the block
605 * @param options Routing options
606 * @param desired_replication_level Desired replication count
607 * @param expiration_time When does the content expire
608 * @param data Content to store
609 * @param data_size Size of content @a data in bytes
612 GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
613 enum GNUNET_BLOCK_Type block_type,
614 enum GNUNET_DHT_RouteOption options,
615 uint32_t desired_replication_level,
616 struct GNUNET_TIME_Absolute expiration_time,
620 GDS_DATACACHE_handle_put (expiration_time,
627 GDS_CLIENTS_process_put (options,
639 * Handle the get request from the client file. If I am destination do
640 * datacache put and return. Else find the target friend and forward message
643 * @param key Key for the content
644 * @param block_type Type of the block
645 * @param options Routing options
646 * @param desired_replication_level Desired replication count
649 GDS_NEIGHBOURS_handle_get (const struct GNUNET_HashCode *key,
650 enum GNUNET_BLOCK_Type block_type,
651 enum GNUNET_DHT_RouteOption options,
652 uint32_t desired_replication_level)
654 // find closest finger(s) on all layers
655 // use TrailRoute with PeerGetMessage embedded to contact peer
660 * Delete a trail, it died (timeout, link failure, etc.).
662 * @param trail trail to delete from all data structures
663 * @param inform_pred should we notify the predecessor?
664 * @param inform_succ should we inform the successor?
667 delete_trail (struct Trail *trail,
671 struct FriendInfo *friend;
672 struct GNUNET_MQ_Envelope *env;
673 struct TrailDestroyMessage *tdm;
674 struct Finger *finger;
676 friend = trail->pred;
679 if (GNUNET_YES == inform_pred)
681 env = GNUNET_MQ_msg (tdm,
682 GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY);
683 tdm->trail_id = trail->pred_id;
684 GNUNET_MQ_send (friend->mq,
687 GNUNET_CONTAINER_MDLL_remove (pred,
692 friend = trail->succ;
695 if (GNUNET_YES == inform_succ)
697 env = GNUNET_MQ_msg (tdm,
698 GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY);
699 tdm->trail_id = trail->pred_id;
700 GNUNET_MQ_send (friend->mq,
703 GNUNET_CONTAINER_MDLL_remove (succ,
708 GNUNET_break (trail ==
709 GNUNET_CONTAINER_heap_remove_node (trail->hn));
710 finger = *trail->finger;
713 *trail->finger = NULL;
714 GNUNET_free (finger);
721 * Send the get result to requesting client.
723 * @param trail_id trail identifying where to send the result to, NULL for us
724 * @param key Key of the requested data.
725 * @param type Block type
726 * @param put_path_length Number of peers in @a put_path
727 * @param put_path Path taken to put the data at its stored location.
728 * @param expiration When will this result expire?
729 * @param data Payload to store
730 * @param data_size Size of the @a data
733 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *trail_id,
734 const struct GNUNET_HashCode *key,
735 enum GNUNET_BLOCK_Type type,
736 unsigned int put_path_length,
737 const struct GNUNET_PeerIdentity *put_path,
738 struct GNUNET_TIME_Absolute expiration,
742 // TRICKY: need to introduce some context to remember trail from
748 * Method called whenever a peer disconnects.
751 * @param peer peer identity this notification is about
754 handle_core_disconnect (void *cls,
755 const struct GNUNET_PeerIdentity *peer)
757 struct FriendInfo *remove_friend;
760 /* If disconnected to own identity, then return. */
761 if (0 == memcmp (&my_identity,
763 sizeof (struct GNUNET_PeerIdentity)))
766 if (NULL == (remove_friend =
767 GNUNET_CONTAINER_multipeermap_get (friends_peermap,
774 GNUNET_assert (GNUNET_YES ==
775 GNUNET_CONTAINER_multipeermap_remove (friends_peermap,
778 while (NULL != (t = remove_friend->succ_head))
782 while (NULL != (t = remove_friend->pred_head))
786 GNUNET_MQ_destroy (remove_friend->mq);
787 GNUNET_free (remove_friend);
789 GNUNET_CONTAINER_multipeermap_size (friends_peermap))
791 GNUNET_SCHEDULER_cancel (random_walk_task);
792 random_walk_task = NULL;
798 * Pick random friend from friends for random walk.
800 static struct FriendInfo *
801 pick_random_friend ()
803 // TODO: need to extend peermap API to return random entry...
804 // (Note: same extension exists for hashmap API).
805 return NULL; // FIXME...
810 * One of our trails might have timed out, check and
811 * possibly initiate cleanup.
817 trail_timeout_callback (void *cls,
818 const struct GNUNET_SCHEDULER_TaskContext *tc)
821 struct GNUNET_TIME_Relative left;
823 trail_timeout_task = NULL;
824 while (NULL != (trail = GNUNET_CONTAINER_heap_peek (trail_heap)))
826 left = GNUNET_TIME_absolute_get_remaining (trail->expiration_time);
827 if (0 != left.rel_value_us)
834 trail_timeout_task = GNUNET_SCHEDULER_add_delayed (left,
835 &trail_timeout_callback,
841 * Initiate a random walk.
847 do_random_walk (void *cls,
848 const struct GNUNET_SCHEDULER_TaskContext *tc)
850 static unsigned int walk_layer;
851 struct FriendInfo *friend;
852 struct GNUNET_MQ_Envelope *env;
853 struct RandomWalkMessage *rwm;
854 struct FingerTable *ft;
855 struct Finger *finger;
858 random_walk_task = NULL;
859 friend = pick_random_friend ();
861 trail = GNUNET_new (struct Trail);
862 /* We create the random walk so, no predecessor */
863 trail->succ = friend;
864 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
867 GNUNET_CONTAINER_multihashmap_put (trail_map,
870 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
876 GNUNET_CONTAINER_MDLL_insert (succ,
880 trail->expiration_time = GNUNET_TIME_relative_to_absolute (TRAIL_TIMEOUT);
881 trail->hn = GNUNET_CONTAINER_heap_insert (trail_heap,
883 trail->expiration_time.abs_value_us);
884 if (NULL == trail_timeout_task)
885 trail_timeout_task = GNUNET_SCHEDULER_add_delayed (TRAIL_TIMEOUT,
886 &trail_timeout_callback,
888 env = GNUNET_MQ_msg (rwm,
889 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK);
890 rwm->hops_taken = htonl (0);
891 rwm->trail_id = trail->succ_id;
892 GNUNET_MQ_send (friend->mq,
894 /* clean up 'old' entry (implicitly via trail cleanup) */
895 ft = &fingers[walk_layer];
897 if ( (NULL != ft->fingers) &&
898 (NULL != (finger = ft->fingers[ft->walk_offset])) )
899 delete_trail (finger->trail,
902 if (ft->finger_array_size < 42)
904 // FIXME: must have finger array of the right size here,
905 // FIXME: growing / shrinking are tricy -- with pointers
909 GNUNET_assert (NULL == ft->fingers[ft->walk_offset]);
911 finger = GNUNET_new (struct Finger);
912 finger->trail = trail;
913 trail->finger = &ft->fingers[ft->walk_offset];
915 ft->fingers[ft->walk_offset] = finger;
916 ft->is_sorted = GNUNET_NO;
917 ft->walk_offset = (ft->walk_offset + 1) % ft->finger_array_size;
919 walk_layer = (walk_layer + 1) % NUMBER_LAYERED_ID;
920 random_walk_task = GNUNET_SCHEDULER_add_delayed (RANDOM_WALK_DELAY,
927 * Method called whenever a peer connects.
930 * @param peer_identity peer identity this notification is about
933 handle_core_connect (void *cls,
934 const struct GNUNET_PeerIdentity *peer_identity)
936 struct FriendInfo *friend;
938 /* Check for connect to self message */
939 if (0 == memcmp (&my_identity,
941 sizeof (struct GNUNET_PeerIdentity)))
944 /* If peer already exists in our friend_peermap, then exit. */
946 GNUNET_CONTAINER_multipeermap_contains (friends_peermap,
953 friend = GNUNET_new (struct FriendInfo);
954 friend->id = *peer_identity;
955 friend->mq = GNUNET_CORE_mq_create (core_api,
957 GNUNET_assert (GNUNET_OK ==
958 GNUNET_CONTAINER_multipeermap_put (friends_peermap,
961 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
962 if (NULL == random_walk_task)
964 /* random walk needs to be started -- we have a first connection */
965 random_walk_task = GNUNET_SCHEDULER_add_now (&do_random_walk,
972 * To be called on core init/fail.
974 * @param cls service closure
975 * @param identity the public identity of this peer
978 core_init (void *cls,
979 const struct GNUNET_PeerIdentity *identity)
981 my_identity = *identity;
986 * Handle a `struct RandomWalkMessage` from a
987 * #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK message.
989 * @param cls closure (NULL)
990 * @param peer sender identity
991 * @param message the setup message
992 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
995 handle_dht_p2p_random_walk (void *cls,
996 const struct GNUNET_PeerIdentity *peer,
997 const struct GNUNET_MessageHeader *message)
999 const struct RandomWalkMessage *m;
1001 struct FriendInfo *pred;
1003 m = (const struct RandomWalkMessage *) message;
1004 pred = GNUNET_CONTAINER_multipeermap_get (friends_peermap,
1006 t = GNUNET_new (struct Trail);
1007 t->pred_id = m->trail_id;
1010 GNUNET_CONTAINER_multihashmap_put (trail_map,
1013 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1015 GNUNET_break_op (0);
1017 return GNUNET_SYSERR;
1019 GNUNET_CONTAINER_MDLL_insert (pred,
1023 t->expiration_time = GNUNET_TIME_relative_to_absolute (TRAIL_TIMEOUT);
1024 t->hn = GNUNET_CONTAINER_heap_insert (trail_heap,
1026 t->expiration_time.abs_value_us);
1027 if (NULL == trail_timeout_task)
1028 trail_timeout_task = GNUNET_SCHEDULER_add_delayed (TRAIL_TIMEOUT,
1029 &trail_timeout_callback,
1032 if (ntohl (m->hops_taken) > GDS_NSE_get ())
1034 /* We are the last hop, generate response */
1035 struct GNUNET_MQ_Envelope *env;
1036 struct RandomWalkResponseMessage *rwrm;
1039 env = GNUNET_MQ_msg (rwrm,
1040 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE);
1041 rwrm->reserved = htonl (0);
1042 rwrm->trail_id = m->trail_id;
1043 layer = ntohs (m->layer);
1045 (void) GDS_DATACACHE_get_random_key (&rwrm->location);
1048 struct FingerTable *ft;
1050 if (layer > NUMBER_LAYERED_ID)
1052 GNUNET_break_op (0);
1053 // FIXME: clean up 't'...
1054 return GNUNET_SYSERR;
1056 ft = &fingers[layer-1];
1057 if (0 == ft->number_valid_fingers)
1059 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
1066 f = ft->fingers[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1067 ft->number_valid_fingers)];
1068 rwrm->location = f->destination;
1071 GNUNET_MQ_send (pred->mq,
1076 struct GNUNET_MQ_Envelope *env;
1077 struct RandomWalkMessage *rwm;
1078 struct FriendInfo *succ;
1080 /* extend the trail by another random hop */
1081 succ = pick_random_friend ();
1082 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
1086 GNUNET_CONTAINER_multihashmap_put (trail_map,
1089 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1092 GNUNET_CONTAINER_MDLL_remove (pred,
1099 GNUNET_CONTAINER_MDLL_insert (succ,
1103 env = GNUNET_MQ_msg (rwm,
1104 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK);
1105 rwm->hops_taken = htons (1 + ntohs (m->hops_taken));
1106 rwm->layer = m->layer;
1107 rwm->trail_id = t->succ_id;
1108 GNUNET_MQ_send (succ->mq,
1116 * Handle a `struct RandomWalkResponseMessage` from a GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE
1119 * @param cls closure (NULL)
1120 * @param peer sender identity
1121 * @param message the setup response message
1122 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1125 handle_dht_p2p_random_walk_response (void *cls,
1126 const struct GNUNET_PeerIdentity *peer,
1127 const struct GNUNET_MessageHeader *message)
1129 const struct RandomWalkResponseMessage *rwrm;
1131 rwrm = (const struct RandomWalkResponseMessage *) message;
1132 // 1) lookup trail => find Finger entry => fill in 'destination' and mark valid, move to end of sorted array, mark unsorted, update links from 'trails'
1135 * 1 check if we are the correct layer
1136 * 1.a if true : add the returned value (finger) in the db structure
1137 * 1.b if true : do nothing
1139 /* FIXME: add the value in db structure 1.a */
1146 * Handle a `struct TrailDestroyMessage`.
1148 * @param cls closure (NULL)
1149 * @param peer sender identity
1150 * @param message the finger destroy message
1151 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1154 handle_dht_p2p_trail_destroy (void *cls,
1155 const struct GNUNET_PeerIdentity *peer,
1156 const struct GNUNET_MessageHeader *message)
1158 const struct TrailDestroyMessage *tdm;
1159 struct Trail *trail;
1161 tdm = (const struct TrailDestroyMessage *) message;
1162 trail = GNUNET_CONTAINER_multihashmap_get (trail_map,
1164 delete_trail (trail,
1167 sizeof (struct GNUNET_PeerIdentity)),
1170 sizeof (struct GNUNET_PeerIdentity)));
1176 * Handler for a message we received along some trail.
1178 * @param cls closure
1179 * @param trail_id trail identifier
1180 * @param message the message we got
1181 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1184 (*TrailHandlerCallback)(void *cls,
1185 const struct GNUNET_HashCode *trail_id,
1186 const struct GNUNET_MessageHandler *message);
1190 * Definition of a handler for a message received along some trail.
1195 * NULL for end-of-list.
1197 TrailHandlerCallback callback;
1200 * Closure for @e callback.
1205 * Message type this handler addresses.
1207 uint16_t message_type;
1210 * Use 0 for variable-size.
1212 uint16_t message_size;
1217 * Handle a `struct TrailRouteMessage`.
1219 * @param cls closure (NULL)
1220 * @param peer sender identity
1221 * @param message the finger destroy message
1222 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1225 handle_dht_p2p_trail_route (void *cls,
1226 const struct GNUNET_PeerIdentity *peer,
1227 const struct GNUNET_MessageHeader *message)
1229 const struct TrailRouteMessage *trm;
1230 const void *payload;
1232 unsigned int start_payload;
1234 /* msize = ntohs (message->size); */
1235 /* if (msize < sizeof (struct TrailRouteMessage)) */
1237 /* GNUNET_break_op (0); */
1238 /* return GNUNET_YES; */
1241 /* trm = (const struct TrailRouteMessage *) message; */
1242 /* /\* Retreive payload *\/ */
1243 /* start_payload = sizeof (struct PeerGetResultMessage); */
1244 /* payload = message[start_payload]; */
1251 * Handle a `struct FindSuccessorMessage` from a #GNUNET_MESSAGE_TYPE_WDHT_SUCCESSOR_FIND
1254 * @param cls closure (NULL)
1255 * @param trail_id path to the originator
1256 * @param message the finger setup message
1257 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1260 handle_dht_p2p_successor_find (void *cls,
1261 const struct GNUNET_HashCode *trail_id,
1262 const struct GNUNET_MessageHeader *message)
1264 const struct FindSuccessorMessage *fsm;
1266 fsm = (const struct FindSuccessorMessage *) message;
1267 // locate trail (for sending reply), if not exists, fail nicely.
1268 // otherwise, go to datacache and return 'top k' elements closest to 'key'
1269 // as "PUT" messages via the trail (need to extend DB API!)
1271 GDS_DATACACHE_get_successors (trail_id,
1279 * Handle a `struct PeerGetMessage`.
1281 * @param cls closure (NULL)
1282 * @param trail_id path to the originator
1283 * @param message the peer get message
1284 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1287 handle_dht_p2p_peer_get (void *cls,
1288 const struct GNUNET_HashCode *trail_id,
1289 const struct GNUNET_MessageHeader *message)
1291 const struct PeerGetMessage *pgm;
1293 // FIXME: note: never called like this, message embedded with trail route!
1294 pgm = (const struct PeerGetMessage *) message;
1295 // -> lookup in datacache (figure out way to remember trail!)
1298 * 1 extract the result
1300 * 3 send it using the good trail
1302 * What do i do when i don't have the key/value?
1310 * Handle a `struct PeerGetResultMessage`.
1312 * @param cls closure (NULL)
1313 * @param trail_id path to the originator
1314 * @param message the peer get result message
1315 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1318 handle_dht_p2p_peer_get_result (void *cls,
1319 const struct GNUNET_HashCode *trail_id,
1320 const struct GNUNET_MessageHeader *message)
1322 const struct PeerGetResultMessage *pgrm;
1324 pgrm = (const struct PeerGetResultMessage *) message;
1325 // pretty much: parse, & pass to client (there is some call for that...)
1328 GDS_CLIENTS_process_get (options,
1333 (void) GDS_DATACACHE_handle_get (trail_id,
1346 * Handle a `struct PeerPutMessage`.
1348 * @param cls closure (NULL)
1349 * @param trail_id path to the originator
1350 * @param message the peer put message
1351 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1354 handle_dht_p2p_peer_put (void *cls,
1355 const struct GNUNET_HashCode *trail_id,
1356 const struct GNUNET_MessageHeader *message)
1358 const struct PeerGetResultMessage *pgrm;
1360 pgrm = (const struct PeerGetResultMessage *) message;
1361 // parse & store in datacache, this is in response to us asking for successors.
1364 * 1 check the size of the message
1365 * 2 use the API to add the value in the "database". Check on the xdht file, how to do it.
1366 * 3 Did i a have to return a notification or did i have to return GNUNET_[OK|SYSERR]?
1369 GDS_DATACACHE_handle_put (expiration_time,
1375 GDS_CLIENTS_process_put (options,
1389 * Initialize neighbours subsystem.
1390 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1393 GDS_NEIGHBOURS_init (void)
1395 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
1396 { &handle_dht_p2p_random_walk,
1397 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK,
1398 sizeof (struct RandomWalkMessage) },
1399 { &handle_dht_p2p_random_walk_response,
1400 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE,
1401 sizeof (struct RandomWalkResponseMessage) },
1402 { &handle_dht_p2p_trail_destroy,
1403 GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY,
1404 sizeof (struct TrailDestroyMessage) },
1405 { &handle_dht_p2p_trail_route,
1406 GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE,
1412 GNUNET_CORE_connect (GDS_cfg, NULL,
1414 &handle_core_connect,
1415 &handle_core_disconnect,
1420 if (NULL == core_api)
1421 return GNUNET_SYSERR;
1422 friends_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
1423 trail_map = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1424 trail_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1430 * Shutdown neighbours subsystem.
1433 GDS_NEIGHBOURS_done (void)
1435 if (NULL == core_api)
1437 GNUNET_CORE_disconnect (core_api);
1439 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friends_peermap));
1440 GNUNET_CONTAINER_multipeermap_destroy (friends_peermap);
1441 friends_peermap = NULL;
1442 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (trail_map));
1443 GNUNET_CONTAINER_multihashmap_destroy (trail_map);
1445 GNUNET_CONTAINER_heap_destroy (trail_heap);
1447 if (NULL != trail_timeout_task)
1449 GNUNET_SCHEDULER_cancel (trail_timeout_task);
1450 trail_timeout_task = NULL;
1458 * @return my identity
1460 struct GNUNET_PeerIdentity
1461 GDS_NEIGHBOURS_get_my_id (void)
1466 /* end of gnunet-service-wdht_neighbours.c */