diff options
Diffstat (limited to 'src/rps/gnunet-rps.c')
-rw-r--r-- | src/rps/gnunet-rps.c | 229 |
1 files changed, 118 insertions, 111 deletions
diff --git a/src/rps/gnunet-rps.c b/src/rps/gnunet-rps.c index d731ffa55..84043341b 100644 --- a/src/rps/gnunet-rps.c +++ b/src/rps/gnunet-rps.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file rps/gnunet-rps.c | 22 | * @file rps/gnunet-rps.c |
@@ -68,13 +68,13 @@ static uint64_t num_view_updates; | |||
68 | * @param cls NULL | 68 | * @param cls NULL |
69 | */ | 69 | */ |
70 | static void | 70 | static void |
71 | do_shutdown (void *cls) | 71 | do_shutdown(void *cls) |
72 | { | 72 | { |
73 | (void) cls; | 73 | (void)cls; |
74 | 74 | ||
75 | if (NULL != req_handle) | 75 | if (NULL != req_handle) |
76 | GNUNET_RPS_request_cancel (req_handle); | 76 | GNUNET_RPS_request_cancel(req_handle); |
77 | GNUNET_RPS_disconnect (rps_handle); | 77 | GNUNET_RPS_disconnect(rps_handle); |
78 | } | 78 | } |
79 | 79 | ||
80 | 80 | ||
@@ -87,22 +87,23 @@ do_shutdown (void *cls) | |||
87 | * @param recv_peers the received peers | 87 | * @param recv_peers the received peers |
88 | */ | 88 | */ |
89 | static void | 89 | static void |
90 | reply_handle (void *cls, | 90 | reply_handle(void *cls, |
91 | uint64_t n, | 91 | uint64_t n, |
92 | const struct GNUNET_PeerIdentity *recv_peers) | 92 | const struct GNUNET_PeerIdentity *recv_peers) |
93 | { | 93 | { |
94 | uint64_t i; | 94 | uint64_t i; |
95 | (void) cls; | 95 | |
96 | (void)cls; | ||
96 | 97 | ||
97 | req_handle = NULL; | 98 | req_handle = NULL; |
98 | for (i = 0; i < n; i++) | 99 | for (i = 0; i < n; i++) |
99 | { | 100 | { |
100 | fprintf (stdout, "%s\n", | 101 | fprintf(stdout, "%s\n", |
101 | GNUNET_i2s_full (&recv_peers[i])); | 102 | GNUNET_i2s_full(&recv_peers[i])); |
102 | } | 103 | } |
103 | ret = 0; | 104 | ret = 0; |
104 | 105 | ||
105 | GNUNET_SCHEDULER_shutdown (); | 106 | GNUNET_SCHEDULER_shutdown(); |
106 | } | 107 | } |
107 | 108 | ||
108 | /** | 109 | /** |
@@ -113,33 +114,34 @@ reply_handle (void *cls, | |||
113 | * @param recv_peers the received peers | 114 | * @param recv_peers the received peers |
114 | */ | 115 | */ |
115 | static void | 116 | static void |
116 | view_update_handle (void *cls, | 117 | view_update_handle(void *cls, |
117 | uint64_t n, | 118 | uint64_t n, |
118 | const struct GNUNET_PeerIdentity *recv_peers) | 119 | const struct GNUNET_PeerIdentity *recv_peers) |
119 | { | 120 | { |
120 | uint64_t i; | 121 | uint64_t i; |
121 | (void) cls; | 122 | |
123 | (void)cls; | ||
122 | 124 | ||
123 | if (0 == n) | 125 | if (0 == n) |
124 | { | 126 | { |
125 | fprintf (stdout, "Empty view\n"); | 127 | fprintf(stdout, "Empty view\n"); |
126 | } | 128 | } |
127 | req_handle = NULL; | 129 | req_handle = NULL; |
128 | for (i = 0; i < n; i++) | 130 | for (i = 0; i < n; i++) |
129 | { | 131 | { |
130 | fprintf (stdout, "%s\n", | 132 | fprintf(stdout, "%s\n", |
131 | GNUNET_i2s_full (&recv_peers[i])); | 133 | GNUNET_i2s_full(&recv_peers[i])); |
132 | } | 134 | } |
133 | 135 | ||
134 | if (1 == num_view_updates) | 136 | if (1 == num_view_updates) |
135 | { | 137 | { |
136 | ret = 0; | 138 | ret = 0; |
137 | GNUNET_SCHEDULER_shutdown (); | 139 | GNUNET_SCHEDULER_shutdown(); |
138 | } | 140 | } |
139 | else if (1 < num_view_updates) | 141 | else if (1 < num_view_updates) |
140 | { | 142 | { |
141 | num_view_updates--; | 143 | num_view_updates--; |
142 | } | 144 | } |
143 | } | 145 | } |
144 | 146 | ||
145 | 147 | ||
@@ -150,23 +152,24 @@ view_update_handle (void *cls, | |||
150 | * @param recv_peers the received peers | 152 | * @param recv_peers the received peers |
151 | */ | 153 | */ |
152 | static void | 154 | static void |
153 | stream_input_handle (void *cls, | 155 | stream_input_handle(void *cls, |
154 | uint64_t num_peers, | 156 | uint64_t num_peers, |
155 | const struct GNUNET_PeerIdentity *recv_peers) | 157 | const struct GNUNET_PeerIdentity *recv_peers) |
156 | { | 158 | { |
157 | uint64_t i; | 159 | uint64_t i; |
158 | (void) cls; | 160 | |
161 | (void)cls; | ||
159 | 162 | ||
160 | if (0 == num_peers) | 163 | if (0 == num_peers) |
161 | { | 164 | { |
162 | fprintf (stdout, "No peer was returned\n"); | 165 | fprintf(stdout, "No peer was returned\n"); |
163 | } | 166 | } |
164 | req_handle = NULL; | 167 | req_handle = NULL; |
165 | for (i = 0; i < num_peers; i++) | 168 | for (i = 0; i < num_peers; i++) |
166 | { | 169 | { |
167 | fprintf (stdout, "%s\n", | 170 | fprintf(stdout, "%s\n", |
168 | GNUNET_i2s_full (&recv_peers[i])); | 171 | GNUNET_i2s_full(&recv_peers[i])); |
169 | } | 172 | } |
170 | } | 173 | } |
171 | 174 | ||
172 | 175 | ||
@@ -179,66 +182,69 @@ stream_input_handle (void *cls, | |||
179 | * @param cfg configuration | 182 | * @param cfg configuration |
180 | */ | 183 | */ |
181 | static void | 184 | static void |
182 | run (void *cls, | 185 | run(void *cls, |
183 | char *const *args, | 186 | char *const *args, |
184 | const char *cfgfile, | 187 | const char *cfgfile, |
185 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 188 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
186 | { | 189 | { |
187 | static uint64_t num_peers; | 190 | static uint64_t num_peers; |
188 | static struct GNUNET_PeerIdentity zero_pid; | 191 | static struct GNUNET_PeerIdentity zero_pid; |
189 | (void) cls; | ||
190 | (void) cfgfile; | ||
191 | 192 | ||
192 | rps_handle = GNUNET_RPS_connect (cfg); | 193 | (void)cls; |
194 | (void)cfgfile; | ||
195 | |||
196 | rps_handle = GNUNET_RPS_connect(cfg); | ||
193 | if (NULL == rps_handle) | 197 | if (NULL == rps_handle) |
194 | { | 198 | { |
195 | fprintf (stderr, "Failed to connect to the rps service\n"); | 199 | fprintf(stderr, "Failed to connect to the rps service\n"); |
196 | return; | 200 | return; |
197 | } | 201 | } |
198 | 202 | ||
199 | if ((0 == memcmp (&zero_pid, &peer_id, sizeof (peer_id))) && | 203 | if ((0 == memcmp(&zero_pid, &peer_id, sizeof(peer_id))) && |
200 | (!view_update) && | 204 | (!view_update) && |
201 | (!stream_input)) | 205 | (!stream_input)) |
202 | { /* Request n PeerIDs */ | 206 | { /* Request n PeerIDs */ |
203 | /* If number was specified use it, else request single peer. */ | 207 | /* If number was specified use it, else request single peer. */ |
204 | if (NULL == args[0] || | 208 | if (NULL == args[0] || |
205 | 0 == sscanf (args[0], "%lu", &num_peers)) | 209 | 0 == sscanf(args[0], "%lu", &num_peers)) |
210 | { | ||
211 | num_peers = 1; | ||
212 | } | ||
213 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
214 | "Requesting %" PRIu64 " PeerIDs\n", num_peers); | ||
215 | req_handle = GNUNET_RPS_request_peers(rps_handle, num_peers, reply_handle, NULL); | ||
216 | GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); | ||
217 | } | ||
218 | else if (view_update) | ||
206 | { | 219 | { |
207 | num_peers = 1; | 220 | /* Get updates of view */ |
221 | if (NULL == args[0] || | ||
222 | 0 == sscanf(args[0], "%lu", &num_view_updates)) | ||
223 | { | ||
224 | num_view_updates = 0; | ||
225 | } | ||
226 | GNUNET_RPS_view_request(rps_handle, num_view_updates, view_update_handle, NULL); | ||
227 | if (0 != num_view_updates) | ||
228 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
229 | "Requesting %" PRIu64 " view updates\n", num_view_updates); | ||
230 | else | ||
231 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
232 | "Requesting continuous view updates\n"); | ||
233 | GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); | ||
208 | } | 234 | } |
209 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 235 | else if (stream_input) |
210 | "Requesting %" PRIu64 " PeerIDs\n", num_peers); | ||
211 | req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL); | ||
212 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); | ||
213 | } else if (view_update) | ||
214 | { | ||
215 | /* Get updates of view */ | ||
216 | if (NULL == args[0] || | ||
217 | 0 == sscanf (args[0], "%lu", &num_view_updates)) | ||
218 | { | 236 | { |
219 | num_view_updates = 0; | 237 | /* Get updates of view */ |
238 | GNUNET_RPS_stream_request(rps_handle, stream_input_handle, NULL); | ||
239 | GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); | ||
220 | } | 240 | } |
221 | GNUNET_RPS_view_request (rps_handle, num_view_updates, view_update_handle, NULL); | ||
222 | if (0 != num_view_updates) | ||
223 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
224 | "Requesting %" PRIu64 " view updates\n", num_view_updates); | ||
225 | else | ||
226 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
227 | "Requesting continuous view updates\n"); | ||
228 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); | ||
229 | } else if (stream_input) | ||
230 | { | ||
231 | /* Get updates of view */ | ||
232 | GNUNET_RPS_stream_request (rps_handle, stream_input_handle, NULL); | ||
233 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); | ||
234 | } | ||
235 | else | 241 | else |
236 | { /* Seed PeerID */ | 242 | { /* Seed PeerID */ |
237 | GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id); | 243 | GNUNET_RPS_seed_ids(rps_handle, 1, &peer_id); |
238 | fprintf (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id)); | 244 | fprintf(stdout, "Seeded PeerID %s\n", GNUNET_i2s_full(&peer_id)); |
239 | ret = 0; | 245 | ret = 0; |
240 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | 246 | GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); |
241 | } | 247 | } |
242 | } | 248 | } |
243 | 249 | ||
244 | /** | 250 | /** |
@@ -249,33 +255,34 @@ run (void *cls, | |||
249 | * @return 0 ok, 1 on error | 255 | * @return 0 ok, 1 on error |
250 | */ | 256 | */ |
251 | int | 257 | int |
252 | main (int argc, char *const *argv) | 258 | main(int argc, char *const *argv) |
253 | { | 259 | { |
254 | const char helpstr[] = | 260 | const char helpstr[] = |
255 | "Get random GNUnet peers. If none is specified a single is requested."; | 261 | "Get random GNUnet peers. If none is specified a single is requested."; |
256 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 262 | struct GNUNET_GETOPT_CommandLineOption options[] = { |
257 | GNUNET_GETOPT_option_base32_auto ('s', | 263 | GNUNET_GETOPT_option_base32_auto('s', |
258 | "seed", | 264 | "seed", |
259 | "PEER_ID", | 265 | "PEER_ID", |
260 | gettext_noop ("Seed a PeerID"), | 266 | gettext_noop("Seed a PeerID"), |
261 | &peer_id), | 267 | &peer_id), |
262 | GNUNET_GETOPT_option_flag ('V', | 268 | GNUNET_GETOPT_option_flag('V', |
263 | "view", | 269 | "view", |
264 | gettext_noop ("Get updates of view (0 for infinite updates)"), | 270 | gettext_noop("Get updates of view (0 for infinite updates)"), |
265 | &view_update), | 271 | &view_update), |
266 | GNUNET_GETOPT_option_flag ('S', | 272 | GNUNET_GETOPT_option_flag('S', |
267 | "stream", | 273 | "stream", |
268 | gettext_noop ("Get peers from biased stream"), | 274 | gettext_noop("Get peers from biased stream"), |
269 | &stream_input), | 275 | &stream_input), |
270 | GNUNET_GETOPT_OPTION_END | 276 | GNUNET_GETOPT_OPTION_END |
271 | }; | 277 | }; |
278 | |||
272 | return (GNUNET_OK == | 279 | return (GNUNET_OK == |
273 | GNUNET_PROGRAM_run (argc, | 280 | GNUNET_PROGRAM_run(argc, |
274 | argv, | 281 | argv, |
275 | "gnunet-rps [NUMBER_OF_PEERS]", | 282 | "gnunet-rps [NUMBER_OF_PEERS]", |
276 | gettext_noop | 283 | gettext_noop |
277 | (helpstr), | 284 | (helpstr), |
278 | options, &run, NULL)) ? ret : 1; | 285 | options, &run, NULL)) ? ret : 1; |
279 | } | 286 | } |
280 | 287 | ||
281 | /* end of gnunet-rps.c */ | 288 | /* end of gnunet-rps.c */ |