diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-04-05 20:06:26 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-04-05 20:06:26 +0000 |
commit | 2700c5079ab27189edb42bb195e9511efc5658d1 (patch) | |
tree | c7ea80a48d01dfb1aaae4865ecc2e29e4ab69ad2 | |
parent | 602dd5a348d139cdc65095c21f3f05cf92013a54 (diff) | |
download | gnunet-2700c5079ab27189edb42bb195e9511efc5658d1.tar.gz gnunet-2700c5079ab27189edb42bb195e9511efc5658d1.zip |
dht find peer binary
-rw-r--r-- | src/dht/Makefile.am | 9 | ||||
-rw-r--r-- | src/dht/gnunet-dht-get-peer.c | 234 | ||||
-rw-r--r-- | src/dht/gnunet-dht-get.c | 2 |
3 files changed, 244 insertions, 1 deletions
diff --git a/src/dht/Makefile.am b/src/dht/Makefile.am index f941b0dfb..1a50800b6 100644 --- a/src/dht/Makefile.am +++ b/src/dht/Makefile.am | |||
@@ -24,6 +24,7 @@ libgnunetdht_la_LDFLAGS = \ | |||
24 | bin_PROGRAMS = \ | 24 | bin_PROGRAMS = \ |
25 | gnunet-service-dht \ | 25 | gnunet-service-dht \ |
26 | gnunet-dht-get \ | 26 | gnunet-dht-get \ |
27 | gnunet-dht-get-peer \ | ||
27 | gnunet-dht-put | 28 | gnunet-dht-put |
28 | 29 | ||
29 | gnunet_service_dht_SOURCES = \ | 30 | gnunet_service_dht_SOURCES = \ |
@@ -42,6 +43,14 @@ gnunet_dht_get_LDADD = \ | |||
42 | $(top_builddir)/src/dht/libgnunetdht.la \ | 43 | $(top_builddir)/src/dht/libgnunetdht.la \ |
43 | $(top_builddir)/src/core/libgnunetcore.la \ | 44 | $(top_builddir)/src/core/libgnunetcore.la \ |
44 | $(top_builddir)/src/util/libgnunetutil.la | 45 | $(top_builddir)/src/util/libgnunetutil.la |
46 | |||
47 | gnunet_dht_get_peer_SOURCES = \ | ||
48 | gnunet-dht-get-peer.c | ||
49 | gnunet_dht_get_peer_LDADD = \ | ||
50 | $(top_builddir)/src/dht/libgnunetdht.la \ | ||
51 | $(top_builddir)/src/hello/libgnunethello.la \ | ||
52 | $(top_builddir)/src/core/libgnunetcore.la \ | ||
53 | $(top_builddir)/src/util/libgnunetutil.la | ||
45 | 54 | ||
46 | gnunet_dht_put_SOURCES = \ | 55 | gnunet_dht_put_SOURCES = \ |
47 | gnunet-dht-put.c | 56 | gnunet-dht-put.c |
diff --git a/src/dht/gnunet-dht-get-peer.c b/src/dht/gnunet-dht-get-peer.c new file mode 100644 index 000000000..7949ea899 --- /dev/null +++ b/src/dht/gnunet-dht-get-peer.c | |||
@@ -0,0 +1,234 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors) | ||
4 | |||
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 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | /** | ||
21 | * @file dht/gnunet-dht-get-peer.c | ||
22 | * @brief search for peers close to key using the DHT | ||
23 | * @author Christian Grothoff | ||
24 | * @author Nathan Evans | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_dht_service.h" | ||
28 | #include "gnunet_hello_lib.h" | ||
29 | |||
30 | /** | ||
31 | * The key for the query | ||
32 | */ | ||
33 | static char *query_key; | ||
34 | |||
35 | /** | ||
36 | * User supplied timeout value (in seconds) | ||
37 | */ | ||
38 | static unsigned long long timeout_request = 5; | ||
39 | |||
40 | /** | ||
41 | * When this request should really die | ||
42 | */ | ||
43 | struct GNUNET_TIME_Absolute absolute_timeout; | ||
44 | |||
45 | /** | ||
46 | * Be verbose | ||
47 | */ | ||
48 | static int verbose; | ||
49 | |||
50 | /** | ||
51 | * Handle to the DHT | ||
52 | */ | ||
53 | static struct GNUNET_DHT_Handle *dht_handle; | ||
54 | |||
55 | /** | ||
56 | * Global handle of the scheduler | ||
57 | */ | ||
58 | static struct GNUNET_SCHEDULER_Handle *sched; | ||
59 | |||
60 | /** | ||
61 | * Global handle of the configuration | ||
62 | */ | ||
63 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
64 | |||
65 | /** | ||
66 | * Handle for the request | ||
67 | */ | ||
68 | static struct GNUNET_DHT_FindPeerHandle *find_peer_handle; | ||
69 | |||
70 | /** | ||
71 | * Count of results found | ||
72 | */ | ||
73 | static unsigned int result_count; | ||
74 | |||
75 | /** | ||
76 | * Global status value | ||
77 | */ | ||
78 | static int ret; | ||
79 | |||
80 | static void | ||
81 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
82 | { | ||
83 | |||
84 | if (dht_handle != NULL) | ||
85 | GNUNET_DHT_disconnect (dht_handle); | ||
86 | |||
87 | dht_handle = NULL; | ||
88 | } | ||
89 | |||
90 | static void | ||
91 | cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
92 | { | ||
93 | if (find_peer_handle != NULL) | ||
94 | GNUNET_DHT_find_peer_stop (find_peer_handle, &shutdown_task, NULL); | ||
95 | else | ||
96 | GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL); | ||
97 | } | ||
98 | |||
99 | /** | ||
100 | * Iterator called on each result obtained from a find peer | ||
101 | * operation | ||
102 | * | ||
103 | * @param cls closure (NULL) | ||
104 | * @param peer the peer we learned about | ||
105 | * @param reply the response message, should be a HELLO | ||
106 | */ | ||
107 | void find_peer_processor (void *cls, | ||
108 | const struct GNUNET_PeerIdentity *peer, | ||
109 | const struct GNUNET_MessageHeader *reply) | ||
110 | { | ||
111 | |||
112 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
113 | "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(peer)); | ||
114 | |||
115 | } | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Signature of the main function of a task. | ||
120 | * | ||
121 | * @param cls closure | ||
122 | * @param tc context information (why was this task triggered now) | ||
123 | */ | ||
124 | void | ||
125 | message_sent_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
126 | { | ||
127 | if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT) | ||
128 | { | ||
129 | if (verbose) | ||
130 | fprintf (stderr, | ||
131 | "Failed to send FIND PEER request to service, quitting.\n"); | ||
132 | ret = 1; | ||
133 | GNUNET_SCHEDULER_add_now (sched, &shutdown_task, NULL); | ||
134 | } | ||
135 | else | ||
136 | { | ||
137 | if (verbose) | ||
138 | fprintf (stderr, "FIND PEER request sent, awaiting results!\n"); | ||
139 | GNUNET_SCHEDULER_add_delayed (sched, | ||
140 | GNUNET_TIME_absolute_get_remaining | ||
141 | (absolute_timeout), &cleanup_task, NULL); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | /** | ||
146 | * Main function that will be run by the scheduler. | ||
147 | * | ||
148 | * @param cls closure | ||
149 | * @param s the scheduler to use | ||
150 | * @param args remaining command-line arguments | ||
151 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
152 | * @param c configuration | ||
153 | */ | ||
154 | static void | ||
155 | run (void *cls, | ||
156 | struct GNUNET_SCHEDULER_Handle *s, | ||
157 | char *const *args, | ||
158 | const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c) | ||
159 | { | ||
160 | struct GNUNET_TIME_Relative timeout; | ||
161 | GNUNET_HashCode key; | ||
162 | sched = s; | ||
163 | cfg = c; | ||
164 | |||
165 | if (query_key == NULL) | ||
166 | { | ||
167 | if (verbose) | ||
168 | fprintf (stderr, "Must provide key for DHT GET!\n"); | ||
169 | ret = 1; | ||
170 | return; | ||
171 | } | ||
172 | |||
173 | dht_handle = GNUNET_DHT_connect (sched, cfg, 1); | ||
174 | |||
175 | if (dht_handle == NULL) | ||
176 | { | ||
177 | if (verbose) | ||
178 | fprintf (stderr, "Couldn't connect to DHT service!\n"); | ||
179 | ret = 1; | ||
180 | return; | ||
181 | } | ||
182 | else if (verbose) | ||
183 | fprintf (stderr, "Connected to DHT service!\n"); | ||
184 | |||
185 | GNUNET_CRYPTO_hash (query_key, strlen (query_key), &key); | ||
186 | |||
187 | timeout = | ||
188 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, timeout_request); | ||
189 | absolute_timeout = GNUNET_TIME_relative_to_absolute (timeout); | ||
190 | |||
191 | if (verbose) | ||
192 | fprintf (stderr, "Issuing FIND PEER request for %s!\n", query_key); | ||
193 | |||
194 | find_peer_handle = GNUNET_DHT_find_peer_start (dht_handle, timeout, 0, NULL, &key, | ||
195 | &find_peer_processor, NULL, &message_sent_cont, NULL); | ||
196 | |||
197 | } | ||
198 | |||
199 | |||
200 | /** | ||
201 | * gnunet-dht-get command line options | ||
202 | */ | ||
203 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
204 | {'k', "key", "KEY", | ||
205 | gettext_noop ("the query key"), | ||
206 | 1, &GNUNET_GETOPT_set_string, &query_key}, | ||
207 | {'T', "timeout", "TIMEOUT", | ||
208 | gettext_noop ("how long to execute this query before giving up?"), | ||
209 | 1, &GNUNET_GETOPT_set_ulong, &timeout_request}, | ||
210 | {'V', "verbose", NULL, | ||
211 | gettext_noop ("be verbose (print progress information)"), | ||
212 | 0, &GNUNET_GETOPT_set_one, &verbose}, | ||
213 | GNUNET_GETOPT_OPTION_END | ||
214 | }; | ||
215 | |||
216 | |||
217 | /** | ||
218 | * Entry point for gnunet-dht-get-peer | ||
219 | * | ||
220 | * @param argc number of arguments from the command line | ||
221 | * @param argv command line arguments | ||
222 | * @return 0 ok, 1 on error | ||
223 | */ | ||
224 | int | ||
225 | main (int argc, char *const *argv) | ||
226 | { | ||
227 | return (GNUNET_OK == | ||
228 | GNUNET_PROGRAM_run (argc, | ||
229 | argv, | ||
230 | "gnunet-dht-get", | ||
231 | gettext_noop | ||
232 | ("Issue a GET request to the GNUnet DHT, prints results."), | ||
233 | options, &run, NULL)) ? ret : 1; | ||
234 | } | ||
diff --git a/src/dht/gnunet-dht-get.c b/src/dht/gnunet-dht-get.c index 684f7c308..1a84ab0c2 100644 --- a/src/dht/gnunet-dht-get.c +++ b/src/dht/gnunet-dht-get.c | |||
@@ -198,7 +198,7 @@ run (void *cls, | |||
198 | 198 | ||
199 | if (verbose) | 199 | if (verbose) |
200 | fprintf (stderr, "Issuing GET request for %s!\n", query_key); | 200 | fprintf (stderr, "Issuing GET request for %s!\n", query_key); |
201 | GNUNET_DHT_get_start (dht_handle, timeout, query_type, &key, | 201 | get_handle = GNUNET_DHT_get_start (dht_handle, timeout, query_type, &key, |
202 | &get_result_iterator, NULL, &message_sent_cont, NULL); | 202 | &get_result_iterator, NULL, &message_sent_cont, NULL); |
203 | 203 | ||
204 | } | 204 | } |