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.
22 * @file dht/gnunet-service-wdht_neighbours.c
23 * @brief GNUnet DHT service's finger and friend table management code
24 * @author Supriti Singh
28 #include "gnunet_util_lib.h"
29 #include "gnunet_block_lib.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_ats_service.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_datacache_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "gnunet_dht_service.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet-service-xdht.h"
40 #include "gnunet-service-wdht_clients.h"
41 #include "gnunet-service-wdht_datacache.h"
42 #include "gnunet-service-wdht_neighbours.h"
47 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
52 #define FOO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
55 * The number of layered ID to use.
57 #define NUMBER_LAYERED_ID 8
60 * Contains all the layered ID.
62 struct GNUNET_PeerIdentity layered_id[NUMBER_LAYERED_ID];
65 GNUNET_NETWORK_STRUCT_BEGIN
68 * Setup a finger using the underlay topology ("social network").
70 struct FingerSetupMessage
73 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP
75 struct GNUNET_MessageHeader header;
78 * Number of hops this message has taken so far, we stop at
81 uint16_t hops_taken GNUNET_PACKED;
84 * Layer for the request, in NBO.
86 uint16_t layer GNUNET_PACKED;
89 * Unique (random) identifier this peer will use to
90 * identify the finger (in future messages).
92 struct GNUNET_HashCode finger_id;
98 * Response to a `struct FingerSetupMessage`.
100 struct FingerSetupResponseMessage
103 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP_RESPONSE
105 struct GNUNET_MessageHeader header;
108 * Zero, for alignment.
110 uint32_t reserved GNUNET_PACKED;
113 * Unique (random) identifier this peer will use to
114 * identify the finger (in future messages).
116 struct GNUNET_HashCode finger_id;
119 * Random location in the respective layer where the
120 * random path of the finger setup terminated.
122 struct GNUNET_HashCode location;
128 * Response to an event that causes a finger to die.
130 struct FingerDestroyMessage
133 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_DESTROY
135 struct GNUNET_MessageHeader header;
138 * Zero, for alignment.
140 uint32_t reserved GNUNET_PACKED;
143 * Unique (random) identifier this peer will use to
144 * identify the finger (in future messages).
146 struct GNUNET_HashCode finger_id;
152 * Send a message along a finger.
154 struct FingerRouteMessage
157 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_ROUTE
159 struct GNUNET_MessageHeader header;
162 * Zero, for alignment.
164 uint32_t reserved GNUNET_PACKED;
167 * Unique (random) identifier this peer will use to
168 * identify the finger (in future messages).
170 struct GNUNET_HashCode finger_id;
172 /* followed by payload to send along the finger */
179 struct PeerPutMessage
182 * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_PUT
184 struct GNUNET_MessageHeader header;
189 uint32_t options GNUNET_PACKED;
194 uint32_t block_type GNUNET_PACKED;
199 uint32_t hop_count GNUNET_PACKED;
202 * Replication level for this message
203 * In the current implementation, this value is not used.
205 uint32_t desired_replication_level GNUNET_PACKED;
208 * Length of the PUT path that follows (if tracked).
210 uint32_t put_path_length GNUNET_PACKED;
213 * When does the content expire?
215 struct GNUNET_TIME_AbsoluteNBO expiration_time;
218 * The key to store the value under.
220 struct GNUNET_HashCode key GNUNET_PACKED;
222 /* put path (if tracked) */
231 struct PeerGetMessage
234 * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_GET
236 struct GNUNET_MessageHeader header;
241 uint32_t options GNUNET_PACKED;
244 * Desired content type.
246 uint32_t block_type GNUNET_PACKED;
251 uint32_t hop_count GNUNET_PACKED;
254 * Desired replication level for this request.
255 * In the current implementation, this value is not used.
257 uint32_t desired_replication_level GNUNET_PACKED;
260 * Total number of peers in get path.
262 unsigned int get_path_length;
265 * The key we are looking for.
267 struct GNUNET_HashCode key;
270 /* struct GNUNET_PeerIdentity[]*/
277 struct PeerGetResultMessage
280 * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_GET_RESULT
282 struct GNUNET_MessageHeader header;
285 * The type for the data.
287 uint32_t type GNUNET_PACKED;
290 * Number of peers recorded in the outgoing path from source to the
291 * stored location of this message.
293 uint32_t put_path_length GNUNET_PACKED;
296 * Length of the GET path that follows (if tracked).
298 uint32_t get_path_length GNUNET_PACKED;
301 * Peer which queried for get and should get the result.
303 struct GNUNET_PeerIdentity querying_peer;
306 * When does the content expire?
308 struct GNUNET_TIME_Absolute expiration_time;
311 * The key of the corresponding GET request.
313 struct GNUNET_HashCode key;
315 /* put path (if tracked) */
317 /* get path (if tracked) */
323 GNUNET_NETWORK_STRUCT_END
326 * Entry in friend_peermap.
332 * Information we keep per trail.
338 * MDLL entry in the list of all trails with the same predecessor.
340 struct Tail *prev_succ;
343 * MDLL entry in the list of all trails with the same predecessor.
345 struct Tail *next_succ;
348 * MDLL entry in the list of all trails with the same predecessor.
350 struct Tail *prev_pred;
353 * MDLL entry in the list of all trails with the same predecessor.
355 struct Tail *next_pred;
358 * Our predecessor in the trail, NULL if we are initiator (?).
360 struct FriendInfo *pred;
363 * Our successor in the trail, NULL if we are the last peer.
365 struct FriendInfo *succ;
368 * Identifier of the trail with the predecessor.
370 struct GNUNET_HashCode pred_id;
373 * Identifier of the trail with the successor.
375 struct GNUNET_HashCode succ_id;
378 * When does this trail expire.
380 struct GNUNET_TIME_Absolute expiration_time;
383 * Location of this trail in the heap.
385 struct GNUNET_CONTAINER_HeapNode *hn;
391 * Entry in friend_peermap.
398 struct GNUNET_PeerIdentity id;
400 struct Tail *pred_head;
402 struct Tail *pred_tail;
404 struct Tail *succ_head;
406 struct Tail *succ_tail;
409 * Core handle for sending messages to this friend.
412 struct GNUNET_CORE_TransmitHandle *th;
419 * Task to timeout trails that have expired.
421 static struct GNUNET_SCHEDULER_Task *trail_timeout_task;
424 * Identity of this peer.
426 static struct GNUNET_PeerIdentity my_identity;
429 * Peer map of all the fingers of a peer
431 static struct GNUNET_CONTAINER_MultiPeerMap *fingers_peermap;
434 * Peer map of all the successors of a peer
436 static struct GNUNET_CONTAINER_MultiPeerMap *successors_peermap;
439 * Tail map, mapping tail identifiers to `struct Trail`s
441 static struct GNUNET_CONTAINER_MultiHashMap *tail_map;
444 * Tail heap, organizing trails by expiration time.
446 static struct GNUNET_CONTAINER_Heap *tail_heap;
451 static struct GNUNET_CORE_Handle *core_api;
455 * Handle the put request from the client.
457 * @param key Key for the content
458 * @param block_type Type of the block
459 * @param options Routing options
460 * @param desired_replication_level Desired replication count
461 * @param expiration_time When does the content expire
462 * @param data Content to store
463 * @param data_size Size of content @a data in bytes
466 GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
467 enum GNUNET_BLOCK_Type block_type,
468 enum GNUNET_DHT_RouteOption options,
469 uint32_t desired_replication_level,
470 struct GNUNET_TIME_Absolute expiration_time,
471 const void *data, size_t data_size)
477 * Handle the get request from the client file. If I am destination do
478 * datacache put and return. Else find the target friend and forward message
481 * @param key Key for the content
482 * @param block_type Type of the block
483 * @param options Routing options
484 * @param desired_replication_level Desired replication count
487 GDS_NEIGHBOURS_handle_get (const struct GNUNET_HashCode *key,
488 enum GNUNET_BLOCK_Type block_type,
489 enum GNUNET_DHT_RouteOption options,
490 uint32_t desired_replication_level)
497 * Send the get result to requesting client.
499 * @param key Key of the requested data.
500 * @param type Block type
501 * @param target_peer Next peer to forward the message to.
502 * @param source_peer Peer which has the data for the key.
503 * @param put_path_length Number of peers in @a put_path
504 * @param put_path Path taken to put the data at its stored location.
505 * @param get_path_length Number of peers in @a get_path
506 * @param get_path Path taken to reach to the location of the key.
507 * @param expiration When will this result expire?
508 * @param data Payload to store
509 * @param data_size Size of the @a data
512 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
513 enum GNUNET_BLOCK_Type type,
514 const struct GNUNET_PeerIdentity *target_peer,
515 const struct GNUNET_PeerIdentity *source_peer,
516 unsigned int put_path_length,
517 const struct GNUNET_PeerIdentity *put_path,
518 unsigned int get_path_length,
519 const struct GNUNET_PeerIdentity *get_path,
520 struct GNUNET_TIME_Absolute expiration,
521 const void *data, size_t data_size)
527 * Method called whenever a peer disconnects.
530 * @param peer peer identity this notification is about
533 handle_core_disconnect (void *cls,
534 const struct GNUNET_PeerIdentity *peer)
536 struct FriendInfo *remove_friend;
538 /* If disconnected to own identity, then return. */
539 if (0 == memcmp (&my_identity,
541 sizeof (struct GNUNET_PeerIdentity)))
544 if (NULL == (remove_friend =
545 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
552 GNUNET_assert (GNUNET_YES ==
553 GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
556 /* FIXME: do stuff */
561 * Method called whenever a peer connects.
564 * @param peer_identity peer identity this notification is about
567 handle_core_connect (void *cls,
568 const struct GNUNET_PeerIdentity *peer_identity)
570 struct FriendInfo *friend;
572 /* Check for connect to self message */
573 if (0 == memcmp (&my_identity,
575 sizeof (struct GNUNET_PeerIdentity)))
578 /* If peer already exists in our friend_peermap, then exit. */
580 GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
587 friend = GNUNET_new (struct FriendInfo);
588 friend->id = *peer_identity;
590 GNUNET_assert (GNUNET_OK ==
591 GNUNET_CONTAINER_multipeermap_put (friend_peermap,
594 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
600 * To be called on core init/fail.
602 * @param cls service closure
603 * @param identity the public identity of this peer
606 core_init (void *cls,
607 const struct GNUNET_PeerIdentity *identity)
609 my_identity = *identity;
614 * Handle a `struct FingerSetupMessage` from a GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP
617 * @param cls closure (NULL)
618 * @param peer sender identity
619 * @param message the setup message
620 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
623 handle_dht_p2p_finger_setup (void *cls,
624 const struct GNUNET_PeerIdentity *peer,
625 const struct GNUNET_MessageHeader *message)
627 const struct FingerSetupMessage *fsm;
629 fsm = (const struct FingerSetupMessage *) message;
633 * 1 check if the hops_taken is < to log(honest node)
634 * 1.a.1 if true : increments the hops_taken
635 * 1.a.2 send the same structure
636 * 1.b if false : drop the message
643 * Handle a `struct FingerSetupResponseMessage` from a GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP_RESPONSE
646 * @param cls closure (NULL)
647 * @param peer sender identity
648 * @param message the setup response message
649 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
652 handle_dht_p2p_finger_setup_response (void *cls,
653 const struct GNUNET_PeerIdentity *peer,
654 const struct GNUNET_MessageHeader *message)
656 const struct FingerSetupResponseMessage *fsrm;
658 fsm = (const struct FingerSetupResponseMessage *) message;
662 * 1 check if we are the correct layer
663 * 1.a if true : add the return value in the db structure
664 * 1.b if true : do nothing
672 * Handle a `struct FingerDestroyMessage`.
674 * @param cls closure (NULL)
675 * @param peer sender identity
676 * @param message the finger destroy message
677 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
680 handle_dht_p2p_finger_destroy (void *cls,
681 const struct GNUNET_PeerIdentity *peer,
682 const struct GNUNET_MessageHeader *message)
684 const struct FingerDestroyMessage *fdm;
686 fdm = (const struct FingerDestroyMessage *) message;
690 * 1 check if message comme from a trail
691 * 1.a.1 if true: send the destroy message to the rest trail
692 * 1.a.2 clean the trail structure
693 * 1.a.3 did i have to remove the trail and ID from the db structure?
694 * 1.b if false: do nothing
701 * Handle a `struct FingerRouteMessage`.
703 * @param cls closure (NULL)
704 * @param peer sender identity
705 * @param message the finger route message
706 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
709 handle_dht_p2p_finger_route (void *cls,
710 const struct GNUNET_PeerIdentity *peer,
711 const struct GNUNET_MessageHeader *message)
713 const struct FingerRouteMessage *frm;
715 frm = (const struct FingerRouteMessage *) message;
716 /* FIXME: check the size of the message */
720 * 1 find the good trail
721 * 2 send the finger route message
728 * Handle a `struct FingerSetupMessage` from a GNUNET_MESSAGE_TYPE_WDHT_NEIGHBOUR_FIND
731 * @param cls closure (NULL)
732 * @param peer sender identity
733 * @param message the finger setup message
734 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
737 handle_dht_p2p_neighbour_find (void *cls,
738 const struct GNUNET_PeerIdentity *peer,
739 const struct GNUNET_MessageHeader *message)
741 const struct FingerSetupMessage *fsm;
743 fsm = (const struct FingerSetupMessage *) message;
749 * Handle a `struct FingerSetupResponseMessage` from a GNUNET_MESSAGE_TYPE_WDHT_NEIGHBOUR_FIND
752 * @param cls closure (NULL)
753 * @param peer sender identity
754 * @param message the finger setup response message
755 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
758 handle_dht_p2p_neighbour_found (void *cls,
759 const struct GNUNET_PeerIdentity *peer,
760 const struct GNUNET_MessageHeader *message)
762 const struct FingerSetupResponseMessage *fsrm;
764 fsrm = (const struct FingerSetupResponseMessage *) message;
770 * Handle a `struct PeerGetMessage`.
772 * @param cls closure (NULL)
773 * @param peer sender identity
774 * @param message the peer get message
775 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
778 handle_dht_p2p_peer_get (void *cls,
779 const struct GNUNET_PeerIdentity *peer,
780 const struct GNUNET_MessageHeader *message)
782 const struct PeerGetMessage *pgm;
784 pgm = (const struct PeerGetMessage *) message;
788 * 1 extract the result
790 * 3 send it using the good trail
792 * What do i do when i don't have the key/value?
799 * Handle a `struct PeerGetResultMessage`.
801 * @param cls closure (NULL)
802 * @param peer sender identity
803 * @param message the peer get result message
804 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
807 handle_dht_p2p_peer_get_result (void *cls,
808 const struct GNUNET_PeerIdentity *peer,
809 const struct GNUNET_MessageHeader *message)
811 const struct PeerGetResultMessage *pgrm;
813 pgrm = (const struct PeerGetResultMessage *) message;
817 * 1 extract the result
818 * 2 create a peerGetResult struct
819 * 3 send it using the good trail
821 * What do i do when i don't have the key/value?
828 * Handle a `struct PeerPutMessage`.
830 * @param cls closure (NULL)
831 * @param peer sender identity
832 * @param message the peer put message
833 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
836 handle_dht_p2p_peer_put (void *cls,
837 const struct GNUNET_PeerIdentity *peer,
838 const struct GNUNET_MessageHeader *message)
840 const struct PeerGetResultMessage *pgrm;
842 pgrm = (const struct PeerGetResultMessage *) message;
846 * 1 check the size of the message
847 * 2 use the API to add the value in the "database". Check on the xdht file, how to do it.
848 * 3 Did i a have to return a notification or did i have to return GNUNET_[OK|SYSERR]?
854 * Initialize neighbours subsystem.
855 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
858 GDS_NEIGHBOURS_init (void)
860 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
861 { &handle_dht_p2p_finger_setup,
862 GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP,
863 sizeof (struct FingerSetupMessage) },
864 { &handle_dht_p2p_finger_setup_response,
865 GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP_RESPONSE,
866 sizeof (struct FingerSetupResponseMessage) },
867 { &handle_dht_p2p_finger_destroy,
868 GNUNET_MESSAGE_TYPE_WDHT_FINGER_DESTROY,
869 sizeof (struct FingerDestroyMessage) },
870 { &handle_dht_p2p_finger_route,
871 GNUNET_MESSAGE_TYPE_WDHT_FINGER_ROUTE,
873 { &handle_dht_p2p_neighbour_find,
874 GNUNET_MESSAGE_TYPE_WDHT_NEIGHBOUR_FIND,
875 sizeof (struct FingerSetupMessage) },
876 { &handle_dht_p2p_neighbour_found,
877 GNUNET_MESSAGE_TYPE_WDHT_NEIGHBOUR_FOUND,
878 sizeof (struct FingerSetupResponseMessage) },
879 { &handle_dht_p2p_peer_get,
880 GNUNET_MESSAGE_TYPE_WDHT_GET,
881 sizeof (struct PeerGetMessage) },
882 { &handle_dht_p2p_peer_get_result,
883 GNUNET_MESSAGE_TYPE_WDHT_GET_RESULT,
885 { &handle_dht_p2p_peer_put,
886 GNUNET_MESSAGE_TYPE_WDHT_PUT,
892 GNUNET_CORE_connect (GDS_cfg, NULL,
894 &handle_core_connect,
895 &handle_core_disconnect,
900 if (NULL == core_api)
901 return GNUNET_SYSERR;
903 fingers_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
904 successors_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
911 * Shutdown neighbours subsystem.
914 GDS_NEIGHBOURS_done (void)
916 if (NULL == core_api)
918 GNUNET_CORE_disconnect (core_api);
921 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
922 GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
923 friend_peermap = NULL;
930 * @return my identity
932 struct GNUNET_PeerIdentity
933 GDS_NEIGHBOURS_get_my_id (void)
938 /* end of gnunet-service-wdht_neighbours.c */