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 GNUNET_NETWORK_STRUCT_BEGIN
58 * Setup a finger using the underlay topology ("social network").
60 struct FingerSetupMessage
63 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP
65 struct GNUNET_MessageHeader header;
68 * Number of hops this message has taken so far, we stop at
71 uint16_t hops_taken GNUNET_PACKED;
74 * Layer for the request, in NBO.
76 uint16_t layer GNUNET_PACKED;
79 * Unique (random) identifier this peer will use to
80 * identify the finger (in future messages).
82 struct GNUNET_HashCode finger_id;
88 * Response to a `struct FingerSetupMessage`.
90 struct FingerSetupResponseMessage
93 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP_RESPONSE
95 struct GNUNET_MessageHeader header;
98 * Zero, for alignment.
100 uint32_t reserved GNUNET_PACKED;
103 * Unique (random) identifier this peer will use to
104 * identify the finger (in future messages).
106 struct GNUNET_HashCode finger_id;
109 * Random location in the respective layer where the
110 * random path of the finger setup terminated.
112 struct GNUNET_HashCode location;
118 * Response to an event that causes a finger to die.
120 struct FingerDestroyMessage
123 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_DESTROY
125 struct GNUNET_MessageHeader header;
128 * Zero, for alignment.
130 uint32_t reserved GNUNET_PACKED;
133 * Unique (random) identifier this peer will use to
134 * identify the finger (in future messages).
136 struct GNUNET_HashCode finger_id;
142 * Send a message along a finger.
144 struct FingerRouteMessage
147 * Type: #GNUNET_MESSAGE_TYPE_WDHT_FINGER_ROUTE
149 struct GNUNET_MessageHeader header;
152 * Zero, for alignment.
154 uint32_t reserved GNUNET_PACKED;
157 * Unique (random) identifier this peer will use to
158 * identify the finger (in future messages).
160 struct GNUNET_HashCode finger_id;
162 /* followed by payload to send along the finger */
169 struct PeerPutMessage
172 * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_PUT
174 struct GNUNET_MessageHeader header;
179 uint32_t options GNUNET_PACKED;
184 uint32_t block_type GNUNET_PACKED;
189 uint32_t hop_count GNUNET_PACKED;
192 * Replication level for this message
193 * In the current implementation, this value is not used.
195 uint32_t desired_replication_level GNUNET_PACKED;
198 * Length of the PUT path that follows (if tracked).
200 uint32_t put_path_length GNUNET_PACKED;
203 * When does the content expire?
205 struct GNUNET_TIME_AbsoluteNBO expiration_time;
208 * The key to store the value under.
210 struct GNUNET_HashCode key GNUNET_PACKED;
212 /* put path (if tracked) */
221 struct PeerGetMessage
224 * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_GET
226 struct GNUNET_MessageHeader header;
231 uint32_t options GNUNET_PACKED;
234 * Desired content type.
236 uint32_t block_type GNUNET_PACKED;
241 uint32_t hop_count GNUNET_PACKED;
244 * Desired replication level for this request.
245 * In the current implementation, this value is not used.
247 uint32_t desired_replication_level GNUNET_PACKED;
250 * Total number of peers in get path.
252 unsigned int get_path_length;
255 * The key we are looking for.
257 struct GNUNET_HashCode key;
260 /* struct GNUNET_PeerIdentity[]*/
267 struct PeerGetResultMessage
270 * Type: #GNUNET_MESSAGE_TYPE_WDHT_P2P_GET_RESULT
272 struct GNUNET_MessageHeader header;
275 * The type for the data.
277 uint32_t type GNUNET_PACKED;
280 * Number of peers recorded in the outgoing path from source to the
281 * stored location of this message.
283 uint32_t put_path_length GNUNET_PACKED;
286 * Length of the GET path that follows (if tracked).
288 uint32_t get_path_length GNUNET_PACKED;
291 * Peer which queried for get and should get the result.
293 struct GNUNET_PeerIdentity querying_peer;
296 * When does the content expire?
298 struct GNUNET_TIME_Absolute expiration_time;
301 * The key of the corresponding GET request.
303 struct GNUNET_HashCode key;
305 /* put path (if tracked) */
307 /* get path (if tracked) */
313 GNUNET_NETWORK_STRUCT_END
316 * Entry in friend_peermap.
322 * Information we keep per trail.
328 * MDLL entry in the list of all trails with the same predecessor.
330 struct Tail *prev_succ;
333 * MDLL entry in the list of all trails with the same predecessor.
335 struct Tail *next_succ;
338 * MDLL entry in the list of all trails with the same predecessor.
340 struct Tail *prev_pred;
343 * MDLL entry in the list of all trails with the same predecessor.
345 struct Tail *next_pred;
348 * Our predecessor in the trail, NULL if we are initiator (?).
350 struct FriendInfo *pred;
353 * Our successor in the trail, NULL if we are the last peer.
355 struct FriendInfo *succ;
358 * Identifier of the trail with the predecessor.
360 struct GNUNET_HashCode pred_id;
363 * Identifier of the trail with the successor.
365 struct GNUNET_HashCode succ_id;
368 * When does this trail expire.
370 struct GNUNET_TIME_Absolute expiration_time;
376 * Entry in friend_peermap.
383 struct GNUNET_PeerIdentity id;
385 struct Tail *pred_head;
387 struct Tail *pred_tail;
389 struct Tail *succ_head;
391 struct Tail *succ_tail;
394 * Core handle for sending messages to this friend.
397 struct GNUNET_CORE_TransmitHandle *th;
404 * Task to timeout trails that have expired.
406 static struct GNUNET_SCHEDULER_Task *trail_timeout_task;
409 * Identity of this peer.
411 static struct GNUNET_PeerIdentity my_identity;
414 * Peer map of all the friends of a peer
416 static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
419 * Tail map, mapping tail identifiers to `struct Trail`s
421 static struct GNUNET_CONTAINER_MultiHashMap *tail_map;
424 * Tail heap, organizing trails by expiration time.
426 static struct GNUNET_CONTAINER_Heap *tail_heap;
431 static struct GNUNET_CORE_Handle *core_api;
435 * Handle the put request from the client.
437 * @param key Key for the content
438 * @param block_type Type of the block
439 * @param options Routing options
440 * @param desired_replication_level Desired replication count
441 * @param expiration_time When does the content expire
442 * @param data Content to store
443 * @param data_size Size of content @a data in bytes
446 GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
447 enum GNUNET_BLOCK_Type block_type,
448 enum GNUNET_DHT_RouteOption options,
449 uint32_t desired_replication_level,
450 struct GNUNET_TIME_Absolute expiration_time,
451 const void *data, size_t data_size)
457 * Handle the get request from the client file. If I am destination do
458 * datacache put and return. Else find the target friend and forward message
461 * @param key Key for the content
462 * @param block_type Type of the block
463 * @param options Routing options
464 * @param desired_replication_level Desired replication count
467 GDS_NEIGHBOURS_handle_get (const struct GNUNET_HashCode *key,
468 enum GNUNET_BLOCK_Type block_type,
469 enum GNUNET_DHT_RouteOption options,
470 uint32_t desired_replication_level)
477 * Send the get result to requesting client.
479 * @param key Key of the requested data.
480 * @param type Block type
481 * @param target_peer Next peer to forward the message to.
482 * @param source_peer Peer which has the data for the key.
483 * @param put_path_length Number of peers in @a put_path
484 * @param put_path Path taken to put the data at its stored location.
485 * @param get_path_length Number of peers in @a get_path
486 * @param get_path Path taken to reach to the location of the key.
487 * @param expiration When will this result expire?
488 * @param data Payload to store
489 * @param data_size Size of the @a data
492 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
493 enum GNUNET_BLOCK_Type type,
494 const struct GNUNET_PeerIdentity *target_peer,
495 const struct GNUNET_PeerIdentity *source_peer,
496 unsigned int put_path_length,
497 const struct GNUNET_PeerIdentity *put_path,
498 unsigned int get_path_length,
499 const struct GNUNET_PeerIdentity *get_path,
500 struct GNUNET_TIME_Absolute expiration,
501 const void *data, size_t data_size)
507 * Method called whenever a peer disconnects.
510 * @param peer peer identity this notification is about
513 handle_core_disconnect (void *cls,
514 const struct GNUNET_PeerIdentity *peer)
516 struct FriendInfo *remove_friend;
518 /* If disconnected to own identity, then return. */
519 if (0 == memcmp (&my_identity,
521 sizeof (struct GNUNET_PeerIdentity)))
524 if (NULL == (remove_friend =
525 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
532 GNUNET_assert (GNUNET_YES ==
533 GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
536 /* FIXME: do stuff */
541 * Method called whenever a peer connects.
544 * @param peer_identity peer identity this notification is about
547 handle_core_connect (void *cls,
548 const struct GNUNET_PeerIdentity *peer_identity)
550 struct FriendInfo *friend;
552 /* Check for connect to self message */
553 if (0 == memcmp (&my_identity,
555 sizeof (struct GNUNET_PeerIdentity)))
558 /* If peer already exists in our friend_peermap, then exit. */
560 GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
567 friend = GNUNET_new (struct FriendInfo);
568 friend->id = *peer_identity;
570 GNUNET_assert (GNUNET_OK ==
571 GNUNET_CONTAINER_multipeermap_put (friend_peermap,
574 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
580 * To be called on core init/fail.
582 * @param cls service closure
583 * @param identity the public identity of this peer
586 core_init (void *cls,
587 const struct GNUNET_PeerIdentity *identity)
589 my_identity = *identity;
594 * Handle a `struct FingerSetupMessage`.
596 * @param cls closure (NULL)
597 * @param peer sender identity
598 * @param message the setup message
599 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
602 handle_dht_p2p_finger_setup (void *cls,
603 const struct GNUNET_PeerIdentity *peer,
604 const struct GNUNET_MessageHeader *message)
606 const struct FingerSetupMessage *fsm;
608 fsm = (const struct FingerSetupMessage *) message;
614 * Handle a `struct FingerSetupResponseMessage`.
616 * @param cls closure (NULL)
617 * @param peer sender identity
618 * @param message the setup response message
619 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
622 handle_dht_p2p_finger_setup_response (void *cls,
623 const struct GNUNET_PeerIdentity *peer,
624 const struct GNUNET_MessageHeader *message)
626 const struct FingerSetupResponseMessage *fsrm;
628 fsm = (const struct *) message;
635 * Handle a `struct FingerDestroyMessage`.
637 * @param cls closure (NULL)
638 * @param peer sender identity
639 * @param message the finger destroy message
640 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
643 handle_dht_p2p_finger_destroy (void *cls,
644 const struct GNUNET_PeerIdentity *peer,
645 const struct GNUNET_MessageHeader *message)
647 const struct FingerDestroyMessage *fdm;
649 fsm = (const struct *) message;
655 * Handle a `struct FingerRouteMessage`.
657 * @param cls closure (NULL)
658 * @param peer sender identity
659 * @param message the finger route message
660 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
663 handle_dht_p2p_finger_route (void *cls,
664 const struct GNUNET_PeerIdentity *peer,
665 const struct GNUNET_MessageHeader *message)
667 const struct FingerRouteMessage *frm;
669 fsm = (const struct *) message;
670 /* FIXME: check the size of the message */
676 * Handle a `struct FingerRouteMessage`.
678 * @param cls closure (NULL)
679 * @param peer sender identity
680 * @param message the finger route message
681 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
684 handle_dht_p2p_neighbour_find (void *cls,
685 const struct GNUNET_PeerIdentity *peer,
686 const struct GNUNET_MessageHeader *message)
688 const struct FingerSetupMessage *fsm;
690 fsm = (const struct *) message;
696 * Initialize neighbours subsystem.
697 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
700 GDS_NEIGHBOURS_init (void)
702 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
703 { &handle_dht_p2p_finger_setup,
704 GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP,
705 sizeof (struct FingerSetupMessage) },
706 { &handle_dht_p2p_finger_setup_response,
707 GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP_RESPONSE,
708 sizeof (struct FingerSetupResponseMessage) },
709 { &handle_dht_p2p_finger_destroy,
710 GNUNET_MESSAGE_TYPE_WDHT_FINGER_DESTROY,
711 sizeof (struct FingerDestroyMessage) },
712 { &handle_dht_p2p_finger_route,
713 GNUNET_MESSAGE_TYPE_WDHT_FINGER_ROUTE,
715 { &handle_dht_p2p_neighbour_find,
716 GNUNET_MESSAGE_TYPE_WDHT_NEIGHBOUR_FIND,
717 sizeof (struct FingerSetupMessage) },
718 { &handle_dht_p2p_neighbour_find,
719 GNUNET_MESSAGE_TYPE_WDHT_NEIGHBOUR_FOUND,
720 sizeof (struct FingerSetupMessage) },
725 GNUNET_CORE_connect (GDS_cfg, NULL,
727 &handle_core_connect,
728 &handle_core_disconnect,
733 if (NULL == core_api)
734 return GNUNET_SYSERR;
736 friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
742 * Shutdown neighbours subsystem.
745 GDS_NEIGHBOURS_done (void)
747 if (NULL == core_api)
749 GNUNET_CORE_disconnect (core_api);
752 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
753 GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
754 friend_peermap = NULL;
761 * @return my identity
763 struct GNUNET_PeerIdentity
764 GDS_NEIGHBOURS_get_my_id (void)
769 /* end of gnunet-service-wdht_neighbours.c */