2 This file is part of GNUnet.
3 (C) 2009, 2010 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/dht_api_find_peer.c
23 * @brief library to access the DHT to find peers
24 * @author Christian Grothoff
25 * @author Nathan Evans
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_dht_service.h"
38 * Handle to control a find peer operation.
40 struct GNUNET_DHT_FindPeerHandle
44 * Handle to the actual route operation for the request
46 struct GNUNET_DHT_RouteHandle *route_handle;
49 * Iterator to call on data receipt
51 GNUNET_DHT_FindPeerProcessor proc;
54 * Closure for the iterator callback
63 * Iterator called on each result obtained from a generic route
67 * @param key key that was used
68 * @param get_path NULL-terminated array of pointers
69 * to the peers on reverse GET path (or NULL if not recorded)
70 * @param put_path NULL-terminated array of pointers
71 * to the peers on the PUT path (or NULL if not recorded)
72 * @param reply response
75 find_peer_reply_iterator (void *cls,
76 const GNUNET_HashCode *key,
77 const struct GNUNET_PeerIdentity * const *get_path,
78 const struct GNUNET_PeerIdentity * const *put_path,
79 const struct GNUNET_MessageHeader *reply)
81 struct GNUNET_DHT_FindPeerHandle *find_peer_handle = cls;
82 const struct GNUNET_MessageHeader *hello;
84 if (ntohs (reply->type) != GNUNET_MESSAGE_TYPE_DHT_FIND_PEER_RESULT)
89 GNUNET_assert (ntohs (reply->size) >= sizeof (struct GNUNET_MessageHeader));
90 hello = (const struct GNUNET_MessageHeader *)&reply[1];
91 if (ntohs(hello->type) != GNUNET_MESSAGE_TYPE_HELLO)
96 find_peer_handle->proc (find_peer_handle->proc_cls,
97 (const struct GNUNET_HELLO_Message *)hello);
103 * Perform an asynchronous FIND PEER operation on the DHT.
105 * @param handle handle to the DHT service
106 * @param timeout timeout for this request to be sent to the
108 * @param options routing options for this message
109 * @param key the key to look up
110 * @param proc function to call on each result
111 * @param proc_cls closure for proc
112 * @return handle to stop the async get, NULL on error
114 struct GNUNET_DHT_FindPeerHandle *
115 GNUNET_DHT_find_peer_start (struct GNUNET_DHT_Handle *handle,
116 struct GNUNET_TIME_Relative timeout,
117 const GNUNET_HashCode *key,
118 enum GNUNET_DHT_RouteOption options,
119 GNUNET_DHT_FindPeerProcessor proc,
122 struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
123 struct GNUNET_DHT_FindPeerMessage find_peer_msg;
126 GNUNET_malloc (sizeof (struct GNUNET_DHT_FindPeerHandle));
127 find_peer_handle->proc = proc;
128 find_peer_handle->proc_cls = proc_cls;
129 find_peer_msg.header.size = htons(sizeof(struct GNUNET_DHT_FindPeerMessage));
130 find_peer_msg.header.type = htons(GNUNET_MESSAGE_TYPE_DHT_FIND_PEER);
131 find_peer_handle->route_handle =
132 GNUNET_DHT_route_start (handle, key,
134 &find_peer_msg.header,
136 &find_peer_reply_iterator, find_peer_handle,
138 GNUNET_break (find_peer_handle->route_handle != NULL);
139 return find_peer_handle;
144 * Stop async find peer. Frees associated resources.
146 * @param find_peer_handle GET operation to stop.
149 GNUNET_DHT_find_peer_stop (struct GNUNET_DHT_FindPeerHandle *find_peer_handle)
151 GNUNET_DHT_route_stop (find_peer_handle->route_handle);
152 GNUNET_free (find_peer_handle);
156 /* end of dht_api_find_peer.c */