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;
155 struct Trail *pred_head;
157 struct Trail *pred_tail;
159 struct Trail *succ_head;
161 struct Trail *succ_tail;
164 * Core handle for sending messages to this friend.
166 struct GNUNET_MQ_Handle *mq;
178 struct FingerTable *ft;
180 struct GNUNET_HashCode destination;
183 * #GNUNET_YES if a response has been received. Otherwise #GNUNET_NO.
192 * Array of our fingers, unsorted.
194 struct Finger **fingers;
197 * Array of sorted fingers (sorted by destination, valid fingers first).
199 struct Finger **sorted_fingers;
202 * Size of the finger array.
204 unsigned int finger_array_size;
207 * Number of valid entries in @e sorted_fingers (contiguous from offset 0)
209 unsigned int number_valid_fingers;
212 * Which offset in @e fingers will we redo next.
214 unsigned int walk_offset;
217 * Is the finger array sorted?
224 /*********************** end of the db structure part ***********************/
227 GNUNET_NETWORK_STRUCT_BEGIN
230 * Setup a finger using the underlay topology ("social network").
232 struct RandomWalkMessage
235 * Type: #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK
237 struct GNUNET_MessageHeader header;
240 * Number of hops this message has taken so far, we stop at
243 uint16_t hops_taken GNUNET_PACKED;
246 * Layer for the request, in NBO.
248 uint16_t layer GNUNET_PACKED;
251 * Unique (random) identifier this peer will use to
252 * identify the trail (in future messages).
254 struct GNUNET_HashCode trail_id;
259 * Response to a `struct RandomWalkMessage`.
261 struct RandomWalkResponseMessage
264 * Type: #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE
266 struct GNUNET_MessageHeader header;
269 * Zero, for alignment.
271 uint32_t reserved GNUNET_PACKED;
274 * Unique (random) identifier from the
275 * `struct RandomWalkMessage`.
277 struct GNUNET_HashCode trail_id;
280 * Random location in the respective layer where the
281 * random path of the finger setup terminated.
283 struct GNUNET_HashCode location;
288 * Response to an event that causes a trail to die.
290 struct TrailDestroyMessage
293 * Type: #GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY
295 struct GNUNET_MessageHeader header;
298 * Zero, for alignment.
300 uint32_t reserved GNUNET_PACKED;
303 * Unique (random) identifier this peer will use to
304 * identify the finger (in future messages).
306 struct GNUNET_HashCode trail_id;
312 * Send a message along a trail.
314 struct FindSuccessorMessage
317 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FIND_SUCCESSOR
319 struct GNUNET_MessageHeader header;
322 * Zero, for alignment.
324 uint32_t reserved GNUNET_PACKED;
327 * Unique (random) identifier this peer will use to
328 * identify the finger (in future messages).
330 struct GNUNET_HashCode trail_id;
333 * Key for which we would like close values returned.
334 * identify the finger (in future messages).
336 struct GNUNET_HashCode key;
342 * Send a message along a trail.
344 struct TrailRouteMessage
347 * Type: #GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE
349 struct GNUNET_MessageHeader header;
352 * Zero, for alignment.
354 uint32_t reserved GNUNET_PACKED;
357 * Unique (random) identifier this peer will use to
358 * identify the finger (in future messages).
360 struct GNUNET_HashCode trail_id;
362 /* followed by payload to send along the trail */
369 struct PeerPutMessage
372 * Type: #GNUNET_MESSAGE_TYPE_WDHT_PUT
374 struct GNUNET_MessageHeader header;
379 uint32_t options GNUNET_PACKED;
384 uint32_t block_type GNUNET_PACKED;
389 uint32_t hop_count GNUNET_PACKED;
392 * Replication level for this message
393 * In the current implementation, this value is not used.
395 uint32_t desired_replication_level GNUNET_PACKED;
398 * Length of the PUT path that follows (if tracked).
400 uint32_t put_path_length GNUNET_PACKED;
403 * When does the content expire?
405 struct GNUNET_TIME_AbsoluteNBO expiration_time;
408 * The key to store the value under.
410 struct GNUNET_HashCode key GNUNET_PACKED;
412 /* put path (if tracked) */
421 struct PeerGetMessage
424 * Type: #GNUNET_MESSAGE_TYPE_WDHT_GET
426 struct GNUNET_MessageHeader header;
431 uint32_t options GNUNET_PACKED;
434 * Desired content type.
436 uint32_t block_type GNUNET_PACKED;
441 uint32_t hop_count GNUNET_PACKED;
444 * Desired replication level for this request.
445 * In the current implementation, this value is not used.
447 uint32_t desired_replication_level GNUNET_PACKED;
450 * Total number of peers in get path.
452 unsigned int get_path_length;
455 * The key we are looking for.
457 struct GNUNET_HashCode key;
460 /* struct GNUNET_PeerIdentity[]*/
467 struct PeerGetResultMessage
470 * Type: #GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT
472 struct GNUNET_MessageHeader header;
475 * The type for the data.
477 uint32_t type GNUNET_PACKED;
480 * Number of peers recorded in the outgoing path from source to the
481 * stored location of this message.
483 uint32_t put_path_length GNUNET_PACKED;
486 * Length of the GET path that follows (if tracked).
488 uint32_t get_path_length GNUNET_PACKED;
491 * Peer which queried for get and should get the result.
493 struct GNUNET_PeerIdentity querying_peer;
496 * When does the content expire?
498 struct GNUNET_TIME_Absolute expiration_time;
501 * The key of the corresponding GET request.
503 struct GNUNET_HashCode key;
505 /* put path (if tracked) */
507 /* get path (if tracked) */
513 GNUNET_NETWORK_STRUCT_END
517 * Contains all the layered IDs of this peer.
519 struct GNUNET_PeerIdentity layered_id[NUMBER_LAYERED_ID];
522 * Task to timeout trails that have expired.
524 static struct GNUNET_SCHEDULER_Task *trail_timeout_task;
527 * Task to perform random walks.
529 static struct GNUNET_SCHEDULER_Task *random_walk_task;
532 * Identity of this peer.
534 static struct GNUNET_PeerIdentity my_identity;
537 * Peer map of all the friends of a peer
539 static struct GNUNET_CONTAINER_MultiPeerMap *friends_peermap;
544 static struct FingerTable fingers[NUMBER_LAYERED_ID];
547 * Tail map, mapping tail identifiers to `struct Trail`s
549 static struct GNUNET_CONTAINER_MultiHashMap *trail_map;
552 * Tail heap, organizing trails by expiration time.
554 static struct GNUNET_CONTAINER_Heap *trail_heap;
559 static struct GNUNET_CORE_Handle *core_api;
563 * Handle the put request from the client.
565 * @param key Key for the content
566 * @param block_type Type of the block
567 * @param options Routing options
568 * @param desired_replication_level Desired replication count
569 * @param expiration_time When does the content expire
570 * @param data Content to store
571 * @param data_size Size of content @a data in bytes
574 GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
575 enum GNUNET_BLOCK_Type block_type,
576 enum GNUNET_DHT_RouteOption options,
577 uint32_t desired_replication_level,
578 struct GNUNET_TIME_Absolute expiration_time,
579 const void *data, size_t data_size)
581 GDS_DATACACHE_handle_put (expiration_time,
591 * Handle the get request from the client file. If I am destination do
592 * datacache put and return. Else find the target friend and forward message
595 * @param key Key for the content
596 * @param block_type Type of the block
597 * @param options Routing options
598 * @param desired_replication_level Desired replication count
601 GDS_NEIGHBOURS_handle_get (const struct GNUNET_HashCode *key,
602 enum GNUNET_BLOCK_Type block_type,
603 enum GNUNET_DHT_RouteOption options,
604 uint32_t desired_replication_level)
606 // find closest finger(s) on all layers
607 // use TrailRoute with PeerGetMessage embedded to contact peer
612 * Delete a trail, it died (timeout, link failure, etc.).
614 * @param trail trail to delete from all data structures
615 * @param inform_pred should we notify the predecessor?
616 * @param inform_succ should we inform the successor?
619 delete_trail (struct Trail *trail,
623 struct FriendInfo *friend;
624 struct GNUNET_MQ_Envelope *env;
625 struct TrailDestroyMessage *tdm;
626 struct Finger *finger;
628 friend = trail->pred;
631 if (GNUNET_YES == inform_pred)
633 env = GNUNET_MQ_msg (tdm,
634 GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY);
635 tdm->trail_id = trail->pred_id;
636 GNUNET_MQ_send (friend->mq,
639 GNUNET_CONTAINER_MDLL_remove (pred,
644 friend = trail->succ;
647 if (GNUNET_YES == inform_succ)
649 env = GNUNET_MQ_msg (tdm,
650 GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY);
651 tdm->trail_id = trail->pred_id;
652 GNUNET_MQ_send (friend->mq,
655 GNUNET_CONTAINER_MDLL_remove (succ,
660 GNUNET_break (trail ==
661 GNUNET_CONTAINER_heap_remove_node (trail->hn));
662 finger = *trail->finger;
665 *trail->finger = NULL;
666 GNUNET_free (finger);
673 * Send the get result to requesting client.
675 * @param trail_id trail identifying where to send the result to, NULL for us
676 * @param key Key of the requested data.
677 * @param type Block type
678 * @param put_path_length Number of peers in @a put_path
679 * @param put_path Path taken to put the data at its stored location.
680 * @param expiration When will this result expire?
681 * @param data Payload to store
682 * @param data_size Size of the @a data
685 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *trail_id,
686 const struct GNUNET_HashCode *key,
687 enum GNUNET_BLOCK_Type type,
688 unsigned int put_path_length,
689 const struct GNUNET_PeerIdentity *put_path,
690 struct GNUNET_TIME_Absolute expiration,
694 // TRICKY: need to introduce some context to remember trail from
700 * Method called whenever a peer disconnects.
703 * @param peer peer identity this notification is about
706 handle_core_disconnect (void *cls,
707 const struct GNUNET_PeerIdentity *peer)
709 struct FriendInfo *remove_friend;
712 /* If disconnected to own identity, then return. */
713 if (0 == memcmp (&my_identity,
715 sizeof (struct GNUNET_PeerIdentity)))
718 if (NULL == (remove_friend =
719 GNUNET_CONTAINER_multipeermap_get (friends_peermap,
726 GNUNET_assert (GNUNET_YES ==
727 GNUNET_CONTAINER_multipeermap_remove (friends_peermap,
730 while (NULL != (t = remove_friend->succ_head))
734 while (NULL != (t = remove_friend->pred_head))
738 GNUNET_MQ_destroy (remove_friend->mq);
739 GNUNET_free (remove_friend);
741 GNUNET_CONTAINER_multipeermap_size (friends_peermap))
743 GNUNET_SCHEDULER_cancel (random_walk_task);
744 random_walk_task = NULL;
750 * Pick random friend from friends for random walk.
752 static struct FriendInfo *
753 pick_random_friend ()
755 // TODO: need to extend peermap API to return random entry...
756 // (Note: same extension exists for hashmap API).
757 return NULL; // FIXME...
762 * Initiate a random walk.
768 do_random_walk (void *cls,
769 const struct GNUNET_SCHEDULER_TaskContext *tc)
771 static unsigned int walk_layer;
772 struct FriendInfo *friend;
773 struct GNUNET_MQ_Envelope *env;
774 struct RandomWalkMessage *rwm;
775 struct FingerTable *ft;
776 struct Finger *finger;
779 random_walk_task = NULL;
780 friend = pick_random_friend ();
782 trail = GNUNET_new (struct Trail);
783 /* We create the random walk so, no predecessor */
784 trail->succ = friend;
785 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
788 GNUNET_CONTAINER_multihashmap_put (trail_map,
791 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
797 GNUNET_CONTAINER_MDLL_insert (succ,
801 env = GNUNET_MQ_msg (rwm,
802 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK);
803 rwm->hops_taken = htonl (0);
804 rwm->trail_id = trail->succ_id;
805 GNUNET_MQ_send (friend->mq,
807 /* clean up 'old' entry (implicitly via trail cleanup) */
808 ft = &fingers[walk_layer];
810 if ( (NULL != ft->fingers) &&
811 (NULL != (finger = ft->fingers[ft->walk_offset])) )
812 delete_trail (finger->trail,
815 if (ft->finger_array_size < 42)
817 // FIXME: must have finger array of the right size here,
818 // FIXME: growing / shrinking are tricy -- with pointers
822 GNUNET_assert (NULL == ft->fingers[ft->walk_offset]);
824 finger = GNUNET_new (struct Finger);
825 finger->trail = trail;
826 trail->finger = &ft->fingers[ft->walk_offset];
828 ft->fingers[ft->walk_offset] = finger;
829 ft->is_sorted = GNUNET_NO;
830 ft->walk_offset = (ft->walk_offset + 1) % ft->finger_array_size;
832 walk_layer = (walk_layer + 1) % NUMBER_LAYERED_ID;
833 random_walk_task = GNUNET_SCHEDULER_add_delayed (RANDOM_WALK_DELAY,
840 * Method called whenever a peer connects.
843 * @param peer_identity peer identity this notification is about
846 handle_core_connect (void *cls,
847 const struct GNUNET_PeerIdentity *peer_identity)
849 struct FriendInfo *friend;
851 /* Check for connect to self message */
852 if (0 == memcmp (&my_identity,
854 sizeof (struct GNUNET_PeerIdentity)))
857 /* If peer already exists in our friend_peermap, then exit. */
859 GNUNET_CONTAINER_multipeermap_contains (friends_peermap,
866 friend = GNUNET_new (struct FriendInfo);
867 friend->id = *peer_identity;
868 friend->mq = GNUNET_CORE_mq_create (core_api,
870 GNUNET_assert (GNUNET_OK ==
871 GNUNET_CONTAINER_multipeermap_put (friends_peermap,
874 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
875 if (NULL == random_walk_task)
877 /* random walk needs to be started -- we have a first connection */
878 random_walk_task = GNUNET_SCHEDULER_add_now (&do_random_walk,
885 * To be called on core init/fail.
887 * @param cls service closure
888 * @param identity the public identity of this peer
891 core_init (void *cls,
892 const struct GNUNET_PeerIdentity *identity)
894 my_identity = *identity;
899 * Handle a `struct RandomWalkMessage` from a
900 * #GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK message.
902 * @param cls closure (NULL)
903 * @param peer sender identity
904 * @param message the setup message
905 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
908 handle_dht_p2p_random_walk (void *cls,
909 const struct GNUNET_PeerIdentity *peer,
910 const struct GNUNET_MessageHeader *message)
912 const struct RandomWalkMessage *m;
914 struct FriendInfo *pred;
916 m = (const struct RandomWalkMessage *) message;
917 pred = GNUNET_CONTAINER_multipeermap_get (friends_peermap,
919 t = GNUNET_new (struct Trail);
920 t->pred_id = m->trail_id;
922 t->expiration_time = GNUNET_TIME_relative_to_absolute (TRAIL_TIMEOUT);
924 GNUNET_CONTAINER_multihashmap_put (trail_map,
927 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
931 return GNUNET_SYSERR;
933 GNUNET_CONTAINER_MDLL_insert (pred,
937 if (ntohl (m->hops_taken) > GDS_NSE_get ())
939 /* We are the last hop, generate response */
940 struct GNUNET_MQ_Envelope *env;
941 struct RandomWalkResponseMessage *rwrm;
944 env = GNUNET_MQ_msg (rwrm,
945 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE);
946 rwrm->reserved = htonl (0);
947 rwrm->trail_id = m->trail_id;
948 layer = ntohs (m->layer);
950 (void) GDS_DATACACHE_get_random_key (&rwrm->location);
953 struct FingerTable *ft;
955 if (layer > NUMBER_LAYERED_ID)
958 // FIXME: clean up 't'...
959 return GNUNET_SYSERR;
961 ft = &fingers[layer-1];
962 if (0 == ft->number_valid_fingers)
964 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
971 f = ft->fingers[GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
972 ft->number_valid_fingers)];
973 rwrm->location = f->destination;
976 GNUNET_MQ_send (pred->mq,
981 struct GNUNET_MQ_Envelope *env;
982 struct RandomWalkMessage *rwm;
983 struct FriendInfo *succ;
985 /* extend the trail by another random hop */
986 succ = pick_random_friend ();
987 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
991 GNUNET_CONTAINER_multihashmap_put (trail_map,
994 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
997 GNUNET_CONTAINER_MDLL_remove (pred,
1004 GNUNET_CONTAINER_MDLL_insert (succ,
1008 env = GNUNET_MQ_msg (rwm,
1009 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK);
1010 rwm->hops_taken = htons (1 + ntohs (m->hops_taken));
1011 rwm->layer = m->layer;
1012 rwm->trail_id = t->succ_id;
1013 GNUNET_MQ_send (succ->mq,
1021 * Handle a `struct RandomWalkResponseMessage` from a GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE
1024 * @param cls closure (NULL)
1025 * @param peer sender identity
1026 * @param message the setup response message
1027 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1030 handle_dht_p2p_random_walk_response (void *cls,
1031 const struct GNUNET_PeerIdentity *peer,
1032 const struct GNUNET_MessageHeader *message)
1034 const struct RandomWalkResponseMessage *rwrm;
1036 rwrm = (const struct RandomWalkResponseMessage *) message;
1037 // 1) lookup trail => find Finger entry => fill in 'destination' and mark valid, move to end of sorted array, mark unsorted, update links from 'trails'
1040 * 1 check if we are the correct layer
1041 * 1.a if true : add the returned value (finger) in the db structure
1042 * 1.b if true : do nothing
1044 /* FIXME: add the value in db structure 1.a */
1051 * Handle a `struct TrailDestroyMessage`.
1053 * @param cls closure (NULL)
1054 * @param peer sender identity
1055 * @param message the finger destroy message
1056 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1059 handle_dht_p2p_trail_destroy (void *cls,
1060 const struct GNUNET_PeerIdentity *peer,
1061 const struct GNUNET_MessageHeader *message)
1063 const struct TrailDestroyMessage *tdm;
1065 tdm = (const struct TrailDestroyMessage *) message;
1069 * 1 check if message comme from a trail (that we still remember...)
1070 * 1.a.1 if true: send the destroy message to the rest trail
1071 * 1.a.2 clean the trail structure
1072 * 1.a.3 did i have to remove the trail and ID from the db structure?
1073 * 1.b if false: do nothing
1081 * Handle a `struct TrailRouteMessage`.
1083 * @param cls closure (NULL)
1084 * @param peer sender identity
1085 * @param message the finger destroy message
1086 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1089 handle_dht_p2p_trail_route (void *cls,
1090 const struct GNUNET_PeerIdentity *peer,
1091 const struct GNUNET_MessageHeader *message)
1093 const struct TrailRouteMessage *trm;
1095 trm = (const struct TrailRouteMessage *) message;
1099 * 1 check if message comme from a trail
1100 * 1.a.1 if trail not finished with us, continue to forward
1101 * 1.a.2 otherwise handle body message embedded in trail
1109 * Handle a `struct FindSuccessorMessage` from a #GNUNET_MESSAGE_TYPE_WDHT_SUCCESSOR_FIND
1112 * @param cls closure (NULL)
1113 * @param peer sender identity
1114 * @param message the finger setup message
1115 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1118 handle_dht_p2p_successor_find (void *cls,
1119 const struct GNUNET_PeerIdentity *peer,
1120 const struct GNUNET_MessageHeader *message)
1122 const struct FindSuccessorMessage *fsm;
1124 fsm = (const struct FindSuccessorMessage *) message;
1125 // locate trail (for sending reply), if not exists, fail nicely.
1126 // otherwise, go to datacache and return 'top k' elements closest to 'key'
1127 // as "PUT" messages via the trail (need to extend DB API!)
1134 * Handle a `struct PeerGetMessage`.
1136 * @param cls closure (NULL)
1137 * @param peer sender identity
1138 * @param message the peer get message
1139 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1142 handle_dht_p2p_peer_get (void *cls,
1143 const struct GNUNET_PeerIdentity *peer,
1144 const struct GNUNET_MessageHeader *message)
1146 const struct PeerGetMessage *pgm;
1148 // FIXME: note: never called like this, message embedded with trail route!
1149 pgm = (const struct PeerGetMessage *) message;
1150 // -> lookup in datacache (figure out way to remember trail!)
1153 * 1 extract the result
1155 * 3 send it using the good trail
1157 * What do i do when i don't have the key/value?
1165 * Handle a `struct PeerGetResultMessage`.
1167 * @param cls closure (NULL)
1168 * @param peer sender identity
1169 * @param message the peer get result message
1170 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1173 handle_dht_p2p_peer_get_result (void *cls,
1174 const struct GNUNET_PeerIdentity *peer,
1175 const struct GNUNET_MessageHeader *message)
1177 const struct PeerGetResultMessage *pgrm;
1179 pgrm = (const struct PeerGetResultMessage *) message;
1180 // pretty much: parse, & pass to client (there is some call for that...)
1187 * Handle a `struct PeerPutMessage`.
1189 * @param cls closure (NULL)
1190 * @param peer sender identity
1191 * @param message the peer put message
1192 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1195 handle_dht_p2p_peer_put (void *cls,
1196 const struct GNUNET_PeerIdentity *peer,
1197 const struct GNUNET_MessageHeader *message)
1199 const struct PeerGetResultMessage *pgrm;
1201 pgrm = (const struct PeerGetResultMessage *) message;
1202 // parse & store in datacache, this is in response to us asking for successors.
1205 * 1 check the size of the message
1206 * 2 use the API to add the value in the "database". Check on the xdht file, how to do it.
1207 * 3 Did i a have to return a notification or did i have to return GNUNET_[OK|SYSERR]?
1214 * Initialize neighbours subsystem.
1215 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1218 GDS_NEIGHBOURS_init (void)
1220 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
1221 { &handle_dht_p2p_random_walk,
1222 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK,
1223 sizeof (struct RandomWalkMessage) },
1224 { &handle_dht_p2p_random_walk_response,
1225 GNUNET_MESSAGE_TYPE_WDHT_RANDOM_WALK_RESPONSE,
1226 sizeof (struct RandomWalkResponseMessage) },
1227 { &handle_dht_p2p_trail_destroy,
1228 GNUNET_MESSAGE_TYPE_WDHT_TRAIL_DESTROY,
1229 sizeof (struct TrailDestroyMessage) },
1230 { &handle_dht_p2p_trail_route,
1231 GNUNET_MESSAGE_TYPE_WDHT_TRAIL_ROUTE,
1233 { &handle_dht_p2p_successor_find,
1234 GNUNET_MESSAGE_TYPE_WDHT_SUCCESSOR_FIND,
1235 sizeof (struct FindSuccessorMessage) },
1236 { &handle_dht_p2p_peer_get,
1237 GNUNET_MESSAGE_TYPE_WDHT_GET,
1238 sizeof (struct PeerGetMessage) },
1239 { &handle_dht_p2p_peer_get_result,
1240 GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT,
1242 { &handle_dht_p2p_peer_put,
1243 GNUNET_MESSAGE_TYPE_WDHT_PUT,
1249 GNUNET_CORE_connect (GDS_cfg, NULL,
1251 &handle_core_connect,
1252 &handle_core_disconnect,
1257 if (NULL == core_api)
1258 return GNUNET_SYSERR;
1259 friends_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
1260 trail_map = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1261 trail_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1267 * Shutdown neighbours subsystem.
1270 GDS_NEIGHBOURS_done (void)
1272 if (NULL == core_api)
1274 GNUNET_CORE_disconnect (core_api);
1276 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friends_peermap));
1277 GNUNET_CONTAINER_multipeermap_destroy (friends_peermap);
1278 friends_peermap = NULL;
1279 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (trail_map));
1280 GNUNET_CONTAINER_multihashmap_destroy (trail_map);
1282 GNUNET_CONTAINER_heap_destroy (trail_heap);
1290 * @return my identity
1292 struct GNUNET_PeerIdentity
1293 GDS_NEIGHBOURS_get_my_id (void)
1298 /* end of gnunet-service-wdht_neighbours.c */