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;
373 * Location of this trail in the heap.
375 struct GNUNET_CONTAINER_HeapNode *hn;
381 * Entry in friend_peermap.
388 struct GNUNET_PeerIdentity id;
390 struct Tail *pred_head;
392 struct Tail *pred_tail;
394 struct Tail *succ_head;
396 struct Tail *succ_tail;
399 * Core handle for sending messages to this friend.
402 struct GNUNET_CORE_TransmitHandle *th;
409 * Task to timeout trails that have expired.
411 static struct GNUNET_SCHEDULER_Task *trail_timeout_task;
414 * Identity of this peer.
416 static struct GNUNET_PeerIdentity my_identity;
419 * Peer map of all the friends of a peer
421 static struct GNUNET_CONTAINER_MultiPeerMap *friend_peermap;
424 * Tail map, mapping tail identifiers to `struct Trail`s
426 static struct GNUNET_CONTAINER_MultiHashMap *tail_map;
429 * Tail heap, organizing trails by expiration time.
431 static struct GNUNET_CONTAINER_Heap *tail_heap;
436 static struct GNUNET_CORE_Handle *core_api;
440 * Handle the put request from the client.
442 * @param key Key for the content
443 * @param block_type Type of the block
444 * @param options Routing options
445 * @param desired_replication_level Desired replication count
446 * @param expiration_time When does the content expire
447 * @param data Content to store
448 * @param data_size Size of content @a data in bytes
451 GDS_NEIGHBOURS_handle_put (const struct GNUNET_HashCode *key,
452 enum GNUNET_BLOCK_Type block_type,
453 enum GNUNET_DHT_RouteOption options,
454 uint32_t desired_replication_level,
455 struct GNUNET_TIME_Absolute expiration_time,
456 const void *data, size_t data_size)
462 * Handle the get request from the client file. If I am destination do
463 * datacache put and return. Else find the target friend and forward message
466 * @param key Key for the content
467 * @param block_type Type of the block
468 * @param options Routing options
469 * @param desired_replication_level Desired replication count
472 GDS_NEIGHBOURS_handle_get (const struct GNUNET_HashCode *key,
473 enum GNUNET_BLOCK_Type block_type,
474 enum GNUNET_DHT_RouteOption options,
475 uint32_t desired_replication_level)
482 * Send the get result to requesting client.
484 * @param key Key of the requested data.
485 * @param type Block type
486 * @param target_peer Next peer to forward the message to.
487 * @param source_peer Peer which has the data for the key.
488 * @param put_path_length Number of peers in @a put_path
489 * @param put_path Path taken to put the data at its stored location.
490 * @param get_path_length Number of peers in @a get_path
491 * @param get_path Path taken to reach to the location of the key.
492 * @param expiration When will this result expire?
493 * @param data Payload to store
494 * @param data_size Size of the @a data
497 GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
498 enum GNUNET_BLOCK_Type type,
499 const struct GNUNET_PeerIdentity *target_peer,
500 const struct GNUNET_PeerIdentity *source_peer,
501 unsigned int put_path_length,
502 const struct GNUNET_PeerIdentity *put_path,
503 unsigned int get_path_length,
504 const struct GNUNET_PeerIdentity *get_path,
505 struct GNUNET_TIME_Absolute expiration,
506 const void *data, size_t data_size)
512 * Method called whenever a peer disconnects.
515 * @param peer peer identity this notification is about
518 handle_core_disconnect (void *cls,
519 const struct GNUNET_PeerIdentity *peer)
521 struct FriendInfo *remove_friend;
523 /* If disconnected to own identity, then return. */
524 if (0 == memcmp (&my_identity,
526 sizeof (struct GNUNET_PeerIdentity)))
529 if (NULL == (remove_friend =
530 GNUNET_CONTAINER_multipeermap_get (friend_peermap,
537 GNUNET_assert (GNUNET_YES ==
538 GNUNET_CONTAINER_multipeermap_remove (friend_peermap,
541 /* FIXME: do stuff */
546 * Method called whenever a peer connects.
549 * @param peer_identity peer identity this notification is about
552 handle_core_connect (void *cls,
553 const struct GNUNET_PeerIdentity *peer_identity)
555 struct FriendInfo *friend;
557 /* Check for connect to self message */
558 if (0 == memcmp (&my_identity,
560 sizeof (struct GNUNET_PeerIdentity)))
563 /* If peer already exists in our friend_peermap, then exit. */
565 GNUNET_CONTAINER_multipeermap_contains (friend_peermap,
572 friend = GNUNET_new (struct FriendInfo);
573 friend->id = *peer_identity;
575 GNUNET_assert (GNUNET_OK ==
576 GNUNET_CONTAINER_multipeermap_put (friend_peermap,
579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
585 * To be called on core init/fail.
587 * @param cls service closure
588 * @param identity the public identity of this peer
591 core_init (void *cls,
592 const struct GNUNET_PeerIdentity *identity)
594 my_identity = *identity;
599 * Handle a `struct FingerSetupMessage`.
601 * @param cls closure (NULL)
602 * @param peer sender identity
603 * @param message the setup message
604 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
607 handle_dht_p2p_finger_setup (void *cls,
608 const struct GNUNET_PeerIdentity *peer,
609 const struct GNUNET_MessageHeader *message)
611 const struct FingerSetupMessage *fsm;
613 fsm = (const struct FingerSetupMessage *) message;
619 * Handle a `struct FingerSetupResponseMessage`.
621 * @param cls closure (NULL)
622 * @param peer sender identity
623 * @param message the setup response message
624 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
627 handle_dht_p2p_finger_setup_response (void *cls,
628 const struct GNUNET_PeerIdentity *peer,
629 const struct GNUNET_MessageHeader *message)
631 const struct FingerSetupResponseMessage *fsrm;
633 fsm = (const struct *) message;
640 * Handle a `struct FingerDestroyMessage`.
642 * @param cls closure (NULL)
643 * @param peer sender identity
644 * @param message the finger destroy message
645 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
648 handle_dht_p2p_finger_destroy (void *cls,
649 const struct GNUNET_PeerIdentity *peer,
650 const struct GNUNET_MessageHeader *message)
652 const struct FingerDestroyMessage *fdm;
654 fsm = (const struct *) message;
660 * Handle a `struct FingerRouteMessage`.
662 * @param cls closure (NULL)
663 * @param peer sender identity
664 * @param message the finger route message
665 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
668 handle_dht_p2p_finger_route (void *cls,
669 const struct GNUNET_PeerIdentity *peer,
670 const struct GNUNET_MessageHeader *message)
672 const struct FingerRouteMessage *frm;
674 fsm = (const struct *) message;
675 /* FIXME: check the size of the message */
681 * Handle a `struct FingerRouteMessage`.
683 * @param cls closure (NULL)
684 * @param peer sender identity
685 * @param message the finger route message
686 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
689 handle_dht_p2p_neighbour_find (void *cls,
690 const struct GNUNET_PeerIdentity *peer,
691 const struct GNUNET_MessageHeader *message)
693 const struct FingerSetupMessage *fsm;
695 fsm = (const struct *) message;
701 * Initialize neighbours subsystem.
702 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
705 GDS_NEIGHBOURS_init (void)
707 static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
708 { &handle_dht_p2p_finger_setup,
709 GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP,
710 sizeof (struct FingerSetupMessage) },
711 { &handle_dht_p2p_finger_setup_response,
712 GNUNET_MESSAGE_TYPE_WDHT_FINGER_SETUP_RESPONSE,
713 sizeof (struct FingerSetupResponseMessage) },
714 { &handle_dht_p2p_finger_destroy,
715 GNUNET_MESSAGE_TYPE_WDHT_FINGER_DESTROY,
716 sizeof (struct FingerDestroyMessage) },
717 { &handle_dht_p2p_finger_route,
718 GNUNET_MESSAGE_TYPE_WDHT_FINGER_ROUTE,
720 { &handle_dht_p2p_neighbour_find,
721 GNUNET_MESSAGE_TYPE_WDHT_NEIGHBOUR_FIND,
722 sizeof (struct FingerSetupMessage) },
723 { &handle_dht_p2p_neighbour_find,
724 GNUNET_MESSAGE_TYPE_WDHT_NEIGHBOUR_FOUND,
725 sizeof (struct FingerSetupMessage) },
730 GNUNET_CORE_connect (GDS_cfg, NULL,
732 &handle_core_connect,
733 &handle_core_disconnect,
738 if (NULL == core_api)
739 return GNUNET_SYSERR;
741 friend_peermap = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO);
747 * Shutdown neighbours subsystem.
750 GDS_NEIGHBOURS_done (void)
752 if (NULL == core_api)
754 GNUNET_CORE_disconnect (core_api);
757 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (friend_peermap));
758 GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
759 friend_peermap = NULL;
766 * @return my identity
768 struct GNUNET_PeerIdentity
769 GDS_NEIGHBOURS_get_my_id (void)
774 /* end of gnunet-service-wdht_neighbours.c */