diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-04 22:52:00 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-04 22:52:00 +0200 |
commit | 5df04510362413a6c215365879058f5c445e1f3c (patch) | |
tree | 3c17f355f98529ba36cd4626b723240a7ef429d2 /src/peerinfo-tool | |
parent | 9ff663cc02f81cd842fb07e7c29c44a809368423 (diff) | |
download | gnunet-5df04510362413a6c215365879058f5c445e1f3c.tar.gz gnunet-5df04510362413a6c215365879058f5c445e1f3c.zip |
Remove most of old transport, ATS and PEERINFO. Disable TESTBED and related tests
Diffstat (limited to 'src/peerinfo-tool')
-rw-r--r-- | src/peerinfo-tool/.gitignore | 2 | ||||
-rw-r--r-- | src/peerinfo-tool/Makefile.am | 64 | ||||
-rw-r--r-- | src/peerinfo-tool/gnunet-peerinfo.c | 864 | ||||
-rw-r--r-- | src/peerinfo-tool/gnunet-peerinfo_plugins.c | 196 | ||||
-rw-r--r-- | src/peerinfo-tool/gnunet-peerinfo_plugins.h | 58 | ||||
-rw-r--r-- | src/peerinfo-tool/meson.build | 31 | ||||
-rw-r--r-- | src/peerinfo-tool/plugin_rest_peerinfo.c | 842 | ||||
-rwxr-xr-x | src/peerinfo-tool/test_gnunet_peerinfo.py.in | 143 | ||||
-rw-r--r-- | src/peerinfo-tool/test_gnunet_peerinfo_data.conf | 13 |
9 files changed, 0 insertions, 2213 deletions
diff --git a/src/peerinfo-tool/.gitignore b/src/peerinfo-tool/.gitignore deleted file mode 100644 index d4ed4f801..000000000 --- a/src/peerinfo-tool/.gitignore +++ /dev/null | |||
@@ -1,2 +0,0 @@ | |||
1 | gnunet-peerinfo | ||
2 | test_gnunet_peerinfo.py | ||
diff --git a/src/peerinfo-tool/Makefile.am b/src/peerinfo-tool/Makefile.am deleted file mode 100644 index 825ad5452..000000000 --- a/src/peerinfo-tool/Makefile.am +++ /dev/null | |||
@@ -1,64 +0,0 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
3 | |||
4 | if USE_COVERAGE | ||
5 | AM_CFLAGS = --coverage -O0 | ||
6 | XLIB = -lgcov | ||
7 | endif | ||
8 | |||
9 | REST_PLUGIN = libgnunet_plugin_rest_peerinfo.la | ||
10 | |||
11 | plugindir = $(libdir)/gnunet | ||
12 | |||
13 | plugin_LTLIBRARIES = $(REST_PLUGIN) | ||
14 | |||
15 | bin_PROGRAMS = \ | ||
16 | gnunet-peerinfo | ||
17 | |||
18 | libgnunet_plugin_rest_peerinfo_la_SOURCES = \ | ||
19 | plugin_rest_peerinfo.c | ||
20 | libgnunet_plugin_rest_peerinfo_la_LIBADD = \ | ||
21 | $(top_builddir)/src/hello/libgnunethello.la \ | ||
22 | $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \ | ||
23 | $(top_builddir)/src/transport/libgnunettransport.la \ | ||
24 | $(top_builddir)/src/ats/libgnunetats.la \ | ||
25 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
26 | $(top_builddir)/src/json/libgnunetjson.la \ | ||
27 | $(top_builddir)/src/util/libgnunetutil.la $(XLIBS) \ | ||
28 | $(LTLIBINTL) -ljansson $(MHD_LIBS) | ||
29 | libgnunet_plugin_rest_peerinfo_la_LDFLAGS = \ | ||
30 | $(GN_PLUGIN_LDFLAGS) | ||
31 | libgnunet_plugin_rest_peerinfo_la_CFLAGS = $(MHD_CFLAGS) $(AM_CFLAGS) | ||
32 | |||
33 | |||
34 | gnunet_peerinfo_SOURCES = \ | ||
35 | gnunet-peerinfo.c \ | ||
36 | gnunet-peerinfo_plugins.c gnunet-peerinfo_plugins.h | ||
37 | gnunet_peerinfo_LDADD = \ | ||
38 | $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \ | ||
39 | $(top_builddir)/src/transport/libgnunettransport.la \ | ||
40 | $(top_builddir)/src/hello/libgnunethello.la \ | ||
41 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | ||
42 | $(top_builddir)/src/util/libgnunetutil.la | ||
43 | gnunet_peerinfo_LDFLAGS = \ | ||
44 | $(GN_LIBINTL) | ||
45 | |||
46 | if HAVE_PYTHON | ||
47 | check_SCRIPTS = \ | ||
48 | test_gnunet_peerinfo.py | ||
49 | endif | ||
50 | |||
51 | if ENABLE_TEST_RUN | ||
52 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; | ||
53 | TESTS = $(check_SCRIPTS) | ||
54 | endif | ||
55 | |||
56 | test_gnunet_peerinfo.py: test_gnunet_peerinfo.py.in Makefile | ||
57 | $(AWK) -v bdir="$(bindir)" -v py="$(PYTHON)" -v awkay="$(AWK_BINARY)" -v pfx="$(prefix)" -v prl="$(PERL)" -v sysconfdirectory="$(sysconfdir)" -v pkgdatadirectory="$(pkgdatadir)" -f $(top_srcdir)/bin/dosubst.awk < $(srcdir)/test_gnunet_peerinfo.py.in > test_gnunet_peerinfo.py | ||
58 | chmod +x test_gnunet_peerinfo.py | ||
59 | |||
60 | EXTRA_DIST = \ | ||
61 | test_gnunet_peerinfo.py.in \ | ||
62 | test_gnunet_peerinfo_data.conf | ||
63 | |||
64 | CLEANFILES = $(check_SCRIPTS) | ||
diff --git a/src/peerinfo-tool/gnunet-peerinfo.c b/src/peerinfo-tool/gnunet-peerinfo.c deleted file mode 100644 index 8b149c98e..000000000 --- a/src/peerinfo-tool/gnunet-peerinfo.c +++ /dev/null | |||
@@ -1,864 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2001-2014, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file peerinfo-tool/gnunet-peerinfo.c | ||
23 | * @brief Print information about other known peers. | ||
24 | * @author Christian Grothoff | ||
25 | * @author Matthias Wachs | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "gnunet_hello_lib.h" | ||
30 | #include "gnunet_transport_service.h" | ||
31 | #include "gnunet_transport_hello_service.h" | ||
32 | #include "gnunet_peerinfo_service.h" | ||
33 | #include "gnunet-peerinfo_plugins.h" | ||
34 | |||
35 | /** | ||
36 | * How long until we time out during address lookup? | ||
37 | */ | ||
38 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
39 | |||
40 | /** | ||
41 | * Structure we use to collect printable address information. | ||
42 | */ | ||
43 | struct PrintContext; | ||
44 | |||
45 | /** | ||
46 | * Record we keep for each printable address. | ||
47 | */ | ||
48 | struct AddressRecord | ||
49 | { | ||
50 | /** | ||
51 | * Current address-to-string context (if active, otherwise NULL). | ||
52 | */ | ||
53 | struct GNUNET_TRANSPORT_AddressToStringContext *atsc; | ||
54 | |||
55 | /** | ||
56 | * Address expiration time | ||
57 | */ | ||
58 | struct GNUNET_TIME_Absolute expiration; | ||
59 | |||
60 | /** | ||
61 | * Printable address. | ||
62 | */ | ||
63 | char *result; | ||
64 | |||
65 | /** | ||
66 | * Print context this address record belongs to. | ||
67 | */ | ||
68 | struct PrintContext *pc; | ||
69 | }; | ||
70 | |||
71 | |||
72 | /** | ||
73 | * Structure we use to collect printable address information. | ||
74 | */ | ||
75 | struct PrintContext | ||
76 | { | ||
77 | /** | ||
78 | * Kept in DLL. | ||
79 | */ | ||
80 | struct PrintContext *next; | ||
81 | |||
82 | /** | ||
83 | * Kept in DLL. | ||
84 | */ | ||
85 | struct PrintContext *prev; | ||
86 | |||
87 | /** | ||
88 | * Identity of the peer. | ||
89 | */ | ||
90 | struct GNUNET_PeerIdentity peer; | ||
91 | |||
92 | /** | ||
93 | * List of printable addresses. | ||
94 | */ | ||
95 | struct AddressRecord *address_list; | ||
96 | |||
97 | /** | ||
98 | * Number of completed addresses in @e address_list. | ||
99 | */ | ||
100 | unsigned int num_addresses; | ||
101 | |||
102 | /** | ||
103 | * Number of addresses allocated in @e address_list. | ||
104 | */ | ||
105 | unsigned int address_list_size; | ||
106 | |||
107 | /** | ||
108 | * Current offset in @e address_list (counted down). | ||
109 | */ | ||
110 | unsigned int off; | ||
111 | |||
112 | /** | ||
113 | * Hello was friend only, #GNUNET_YES or #GNUNET_NO | ||
114 | */ | ||
115 | int friend_only; | ||
116 | }; | ||
117 | |||
118 | |||
119 | /** | ||
120 | * Option '-n' | ||
121 | */ | ||
122 | static int no_resolve; | ||
123 | |||
124 | /** | ||
125 | * Option '-q' | ||
126 | */ | ||
127 | static int be_quiet; | ||
128 | |||
129 | /** | ||
130 | * Option '-f' | ||
131 | */ | ||
132 | static int include_friend_only; | ||
133 | |||
134 | /** | ||
135 | * Option '-s' | ||
136 | */ | ||
137 | static int get_self; | ||
138 | |||
139 | /** | ||
140 | * Option | ||
141 | */ | ||
142 | static int get_uri; | ||
143 | |||
144 | /** | ||
145 | * Option | ||
146 | */ | ||
147 | static int default_operation; | ||
148 | |||
149 | /** | ||
150 | * Option '-i' | ||
151 | */ | ||
152 | static int get_info; | ||
153 | |||
154 | /** | ||
155 | * Option | ||
156 | */ | ||
157 | static char *put_uri; | ||
158 | |||
159 | /** | ||
160 | * Option -d | ||
161 | */ | ||
162 | static char *dump_hello; | ||
163 | |||
164 | /** | ||
165 | * Handle to peerinfo service. | ||
166 | */ | ||
167 | static struct GNUNET_PEERINFO_Handle *peerinfo; | ||
168 | |||
169 | /** | ||
170 | * Configuration handle. | ||
171 | */ | ||
172 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
173 | |||
174 | /** | ||
175 | * Main state machine task (if active). | ||
176 | */ | ||
177 | static struct GNUNET_SCHEDULER_Task *tt; | ||
178 | |||
179 | /** | ||
180 | * Pending #GNUNET_TRANSPORT_hello_get() operation. | ||
181 | */ | ||
182 | static struct GNUNET_TRANSPORT_HelloGetHandle *gh; | ||
183 | |||
184 | /** | ||
185 | * Current iterator context (if active, otherwise NULL). | ||
186 | */ | ||
187 | static struct GNUNET_PEERINFO_IteratorContext *pic; | ||
188 | |||
189 | /** | ||
190 | * My peer identity. | ||
191 | */ | ||
192 | static struct GNUNET_PeerIdentity my_peer_identity; | ||
193 | |||
194 | /** | ||
195 | * Head of list of print contexts. | ||
196 | */ | ||
197 | static struct PrintContext *pc_head; | ||
198 | |||
199 | /** | ||
200 | * Tail of list of print contexts. | ||
201 | */ | ||
202 | static struct PrintContext *pc_tail; | ||
203 | |||
204 | /** | ||
205 | * Handle to current #GNUNET_PEERINFO_add_peer() operation. | ||
206 | */ | ||
207 | static struct GNUNET_MQ_Envelope *ac; | ||
208 | |||
209 | /** | ||
210 | * Hello of this peer (if initialized). | ||
211 | */ | ||
212 | static struct GNUNET_HELLO_Message *my_hello; | ||
213 | |||
214 | |||
215 | /** | ||
216 | * Main state machine that goes over all options and | ||
217 | * runs the next requested function. | ||
218 | * | ||
219 | * @param cls unused | ||
220 | */ | ||
221 | static void | ||
222 | state_machine (void *cls); | ||
223 | |||
224 | |||
225 | /* ********************* 'get_info' ******************* */ | ||
226 | |||
227 | /** | ||
228 | * Print the collected address information to the console and free @a pc. | ||
229 | * | ||
230 | * @param pc printing context | ||
231 | */ | ||
232 | static void | ||
233 | dump_pc (struct PrintContext *pc) | ||
234 | { | ||
235 | unsigned int i; | ||
236 | |||
237 | printf (_ ("%sPeer `%s'\n"), | ||
238 | (GNUNET_YES == pc->friend_only) ? "F2F: " : "", | ||
239 | GNUNET_i2s_full (&pc->peer)); | ||
240 | for (i = 0; i < pc->num_addresses; i++) | ||
241 | { | ||
242 | if (NULL != pc->address_list[i].result) | ||
243 | { | ||
244 | printf (_ ("\tExpires: %s \t %s\n"), | ||
245 | GNUNET_STRINGS_absolute_time_to_string ( | ||
246 | pc->address_list[i].expiration), | ||
247 | pc->address_list[i].result); | ||
248 | GNUNET_free (pc->address_list[i].result); | ||
249 | } | ||
250 | } | ||
251 | printf ("\n"); | ||
252 | GNUNET_free (pc->address_list); | ||
253 | GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc); | ||
254 | GNUNET_free (pc); | ||
255 | if ((NULL == pc_head) && (NULL == pic)) | ||
256 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
257 | } | ||
258 | |||
259 | |||
260 | /* ************************* list all known addresses **************** */ | ||
261 | |||
262 | |||
263 | /** | ||
264 | * Function to call with a human-readable format of an address | ||
265 | * | ||
266 | * @param cls closure | ||
267 | * @param address NULL on error, otherwise 0-terminated printable UTF-8 string | ||
268 | * @param res result of the address to string conversion: | ||
269 | * if #GNUNET_OK: address was valid (conversion to | ||
270 | * string might still have failed) | ||
271 | * if #GNUNET_SYSERR: address is invalid | ||
272 | */ | ||
273 | static void | ||
274 | process_resolved_address (void *cls, const char *address, int res) | ||
275 | { | ||
276 | struct AddressRecord *ar = cls; | ||
277 | struct PrintContext *pc = ar->pc; | ||
278 | |||
279 | if (NULL != address) | ||
280 | { | ||
281 | if (0 != strlen (address)) | ||
282 | { | ||
283 | if (NULL != ar->result) | ||
284 | GNUNET_free (ar->result); | ||
285 | ar->result = GNUNET_strdup (address); | ||
286 | } | ||
287 | return; | ||
288 | } | ||
289 | ar->atsc = NULL; | ||
290 | if (GNUNET_SYSERR == res) | ||
291 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
292 | _ ("Failure: Cannot convert address to string for peer `%s'\n"), | ||
293 | GNUNET_i2s (&ar->pc->peer)); | ||
294 | pc->num_addresses++; | ||
295 | if (pc->num_addresses == pc->address_list_size) | ||
296 | dump_pc (pc); | ||
297 | } | ||
298 | |||
299 | |||
300 | /** | ||
301 | * Iterator callback to go over all addresses and count them. | ||
302 | * | ||
303 | * @param cls `struct PrintContext *` with `off` to increment | ||
304 | * @param address the address | ||
305 | * @param expiration expiration time | ||
306 | * @return #GNUNET_OK to keep the address and continue | ||
307 | */ | ||
308 | static int | ||
309 | count_address (void *cls, | ||
310 | const struct GNUNET_HELLO_Address *address, | ||
311 | struct GNUNET_TIME_Absolute expiration) | ||
312 | { | ||
313 | struct PrintContext *pc = cls; | ||
314 | |||
315 | pc->off++; | ||
316 | return GNUNET_OK; | ||
317 | } | ||
318 | |||
319 | |||
320 | /** | ||
321 | * Iterator callback to go over all addresses. | ||
322 | * | ||
323 | * @param cls closure | ||
324 | * @param address the address | ||
325 | * @param expiration expiration time | ||
326 | * @return #GNUNET_OK to keep the address and continue | ||
327 | */ | ||
328 | static int | ||
329 | print_address (void *cls, | ||
330 | const struct GNUNET_HELLO_Address *address, | ||
331 | struct GNUNET_TIME_Absolute expiration) | ||
332 | { | ||
333 | struct PrintContext *pc = cls; | ||
334 | struct AddressRecord *ar; | ||
335 | |||
336 | GNUNET_assert (0 < pc->off); | ||
337 | ar = &pc->address_list[--pc->off]; | ||
338 | ar->pc = pc; | ||
339 | ar->expiration = expiration; | ||
340 | GNUNET_asprintf (&ar->result, | ||
341 | "%s:%lu:%u", | ||
342 | address->transport_name, | ||
343 | (unsigned long) address->address_length, | ||
344 | address->local_info); | ||
345 | ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, | ||
346 | address, | ||
347 | no_resolve, | ||
348 | TIMEOUT, | ||
349 | &process_resolved_address, | ||
350 | ar); | ||
351 | return GNUNET_OK; | ||
352 | } | ||
353 | |||
354 | |||
355 | /** | ||
356 | * Print information about the peer. Currently prints the `struct | ||
357 | * GNUNET_PeerIdentity` and the transport address. | ||
358 | * | ||
359 | * @param cls the `struct PrintContext *` | ||
360 | * @param peer identity of the peer | ||
361 | * @param hello addresses of the peer | ||
362 | * @param err_msg error message | ||
363 | */ | ||
364 | static void | ||
365 | print_peer_info (void *cls, | ||
366 | const struct GNUNET_PeerIdentity *peer, | ||
367 | const struct GNUNET_HELLO_Message *hello, | ||
368 | const char *err_msg) | ||
369 | { | ||
370 | struct PrintContext *pc; | ||
371 | int friend_only; | ||
372 | |||
373 | if (NULL == peer) | ||
374 | { | ||
375 | pic = NULL; /* end of iteration */ | ||
376 | if (NULL != err_msg) | ||
377 | { | ||
378 | fprintf (stderr, | ||
379 | _ ("Error in communication with PEERINFO service: %s\n"), | ||
380 | err_msg); | ||
381 | } | ||
382 | if (NULL == pc_head) | ||
383 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
384 | return; | ||
385 | } | ||
386 | friend_only = GNUNET_NO; | ||
387 | if (NULL != hello) | ||
388 | friend_only = GNUNET_HELLO_is_friend_only (hello); | ||
389 | if ((GNUNET_YES == be_quiet) || (NULL == hello)) | ||
390 | { | ||
391 | printf ("%s%s\n", | ||
392 | (GNUNET_YES == friend_only) ? "F2F: " : "", | ||
393 | GNUNET_i2s_full (peer)); | ||
394 | return; | ||
395 | } | ||
396 | pc = GNUNET_new (struct PrintContext); | ||
397 | GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc); | ||
398 | pc->peer = *peer; | ||
399 | pc->friend_only = friend_only; | ||
400 | GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &count_address, pc); | ||
401 | if (0 == pc->off) | ||
402 | { | ||
403 | dump_pc (pc); | ||
404 | return; | ||
405 | } | ||
406 | pc->address_list_size = pc->off; | ||
407 | pc->address_list = GNUNET_malloc (sizeof(struct AddressRecord) * pc->off); | ||
408 | GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &print_address, pc); | ||
409 | } | ||
410 | |||
411 | |||
412 | /* ************************* DUMP Hello ************************** */ | ||
413 | |||
414 | /** | ||
415 | * Count the number of addresses in the HELLO. | ||
416 | * | ||
417 | * @param cls pointer to an `int *` used for the counter | ||
418 | * @param address an address to count | ||
419 | * @param expiration (unused) | ||
420 | * @return #GNUNET_OK | ||
421 | */ | ||
422 | static int | ||
423 | count_addr (void *cls, | ||
424 | const struct GNUNET_HELLO_Address *address, | ||
425 | struct GNUNET_TIME_Absolute expiration) | ||
426 | { | ||
427 | int *c = cls; | ||
428 | |||
429 | (*c)++; | ||
430 | return GNUNET_OK; | ||
431 | } | ||
432 | |||
433 | |||
434 | /** | ||
435 | * Write HELLO of my peer to a file. | ||
436 | * | ||
437 | * @param cls the `struct GetUriContext *` | ||
438 | * @param peer identity of the peer (unused) | ||
439 | * @param hello addresses of the peer | ||
440 | * @param err_msg error message | ||
441 | */ | ||
442 | static void | ||
443 | dump_my_hello () | ||
444 | { | ||
445 | unsigned int size; | ||
446 | unsigned int c_addr; | ||
447 | |||
448 | size = GNUNET_HELLO_size (my_hello); | ||
449 | if (0 == size) | ||
450 | { | ||
451 | fprintf (stderr, _ ("Failure: Received invalid %s\n"), "HELLO"); | ||
452 | return; | ||
453 | } | ||
454 | if (GNUNET_SYSERR == | ||
455 | GNUNET_DISK_fn_write (dump_hello, | ||
456 | my_hello, | ||
457 | size, | ||
458 | GNUNET_DISK_PERM_USER_READ | ||
459 | | GNUNET_DISK_PERM_USER_WRITE | ||
460 | | GNUNET_DISK_PERM_GROUP_READ | ||
461 | | GNUNET_DISK_PERM_OTHER_READ)) | ||
462 | { | ||
463 | fprintf (stderr, | ||
464 | _ ("Failed to write HELLO with %u bytes to file `%s'\n"), | ||
465 | size, | ||
466 | dump_hello); | ||
467 | if (0 != unlink (dump_hello)) | ||
468 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING | ||
469 | | GNUNET_ERROR_TYPE_BULK, | ||
470 | "unlink", | ||
471 | dump_hello); | ||
472 | } | ||
473 | c_addr = 0; | ||
474 | GNUNET_HELLO_iterate_addresses (my_hello, GNUNET_NO, count_addr, &c_addr); | ||
475 | |||
476 | if (! be_quiet) | ||
477 | { | ||
478 | fprintf ( | ||
479 | stderr, | ||
480 | _ ("Wrote %s HELLO containing %u addresses with %u bytes to file `%s'\n"), | ||
481 | (GNUNET_YES == GNUNET_HELLO_is_friend_only (my_hello)) ? "friend-only" | ||
482 | : "public", | ||
483 | c_addr, | ||
484 | size, | ||
485 | dump_hello); | ||
486 | } | ||
487 | GNUNET_free (dump_hello); | ||
488 | dump_hello = NULL; | ||
489 | } | ||
490 | |||
491 | |||
492 | /* ************************* GET URI ************************** */ | ||
493 | |||
494 | |||
495 | /** | ||
496 | * Print URI of the peer. | ||
497 | * | ||
498 | * @param cls the `struct GetUriContext *` | ||
499 | * @param peer identity of the peer (unused) | ||
500 | * @param hello addresses of the peer | ||
501 | * @param err_msg error message | ||
502 | */ | ||
503 | static void | ||
504 | print_my_uri (void *cls, | ||
505 | const struct GNUNET_PeerIdentity *peer, | ||
506 | const struct GNUNET_HELLO_Message *hello, | ||
507 | const char *err_msg) | ||
508 | { | ||
509 | char *uri; | ||
510 | |||
511 | if (NULL == peer) | ||
512 | { | ||
513 | pic = NULL; | ||
514 | if (NULL != err_msg) | ||
515 | fprintf (stderr, | ||
516 | _ ("Error in communication with PEERINFO service: %s\n"), | ||
517 | err_msg); | ||
518 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
519 | return; | ||
520 | } | ||
521 | |||
522 | if (NULL == hello) | ||
523 | return; | ||
524 | uri = GNUNET_HELLO_compose_uri (hello, &GPI_plugins_find); | ||
525 | if (NULL != uri) | ||
526 | { | ||
527 | printf ("%s\n", (const char *) uri); | ||
528 | GNUNET_free (uri); | ||
529 | } | ||
530 | } | ||
531 | |||
532 | |||
533 | /* ************************* import HELLO by URI ********************* */ | ||
534 | |||
535 | |||
536 | /** | ||
537 | * Continuation called from #GNUNET_PEERINFO_add_peer() | ||
538 | * | ||
539 | * @param cls closure, NULL | ||
540 | */ | ||
541 | static void | ||
542 | add_continuation (void *cls) | ||
543 | { | ||
544 | ac = NULL; | ||
545 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
546 | } | ||
547 | |||
548 | |||
549 | /** | ||
550 | * Parse the PUT URI given at the command line and add it to our peerinfo | ||
551 | * database. | ||
552 | * | ||
553 | * @param put_uri URI string to parse | ||
554 | * @return #GNUNET_OK on success, | ||
555 | * #GNUNET_SYSERR if the URI was invalid, | ||
556 | * #GNUNET_NO on other errors | ||
557 | */ | ||
558 | static int | ||
559 | parse_hello_uri (const char *put_uri) | ||
560 | { | ||
561 | struct GNUNET_HELLO_Message *hello = NULL; | ||
562 | |||
563 | int ret = GNUNET_HELLO_parse_uri (put_uri, | ||
564 | &my_peer_identity.public_key, | ||
565 | &hello, | ||
566 | &GPI_plugins_find); | ||
567 | |||
568 | if (NULL != hello) | ||
569 | { | ||
570 | /* WARNING: this adds the address from URI WITHOUT verification! */ | ||
571 | if (GNUNET_OK == ret) | ||
572 | ac = GNUNET_PEERINFO_add_peer (peerinfo, hello, &add_continuation, NULL); | ||
573 | else | ||
574 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
575 | GNUNET_free (hello); | ||
576 | } | ||
577 | return ret; | ||
578 | } | ||
579 | |||
580 | |||
581 | /* ************************ Main state machine ********************* */ | ||
582 | |||
583 | |||
584 | /** | ||
585 | * Main state machine that goes over all options and | ||
586 | * runs the next requested function. | ||
587 | * | ||
588 | * @param cls unused | ||
589 | */ | ||
590 | static void | ||
591 | shutdown_task (void *cls) | ||
592 | { | ||
593 | struct PrintContext *pc; | ||
594 | struct AddressRecord *ar; | ||
595 | unsigned int i; | ||
596 | |||
597 | if (NULL != ac) | ||
598 | { | ||
599 | GNUNET_MQ_send_cancel (ac); | ||
600 | ac = NULL; | ||
601 | } | ||
602 | if (NULL != tt) | ||
603 | { | ||
604 | GNUNET_SCHEDULER_cancel (tt); | ||
605 | tt = NULL; | ||
606 | } | ||
607 | if (NULL != pic) | ||
608 | { | ||
609 | GNUNET_PEERINFO_iterate_cancel (pic); | ||
610 | pic = NULL; | ||
611 | } | ||
612 | if (NULL != gh) | ||
613 | { | ||
614 | GNUNET_TRANSPORT_hello_get_cancel (gh); | ||
615 | gh = NULL; | ||
616 | } | ||
617 | while (NULL != (pc = pc_head)) | ||
618 | { | ||
619 | GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc); | ||
620 | for (i = 0; i < pc->address_list_size; i++) | ||
621 | { | ||
622 | ar = &pc->address_list[i]; | ||
623 | GNUNET_free (ar->result); | ||
624 | if (NULL != ar->atsc) | ||
625 | { | ||
626 | GNUNET_TRANSPORT_address_to_string_cancel (ar->atsc); | ||
627 | ar->atsc = NULL; | ||
628 | } | ||
629 | } | ||
630 | GNUNET_free (pc->address_list); | ||
631 | GNUNET_free (pc); | ||
632 | } | ||
633 | GPI_plugins_unload (); | ||
634 | if (NULL != peerinfo) | ||
635 | { | ||
636 | GNUNET_PEERINFO_disconnect (peerinfo); | ||
637 | peerinfo = NULL; | ||
638 | } | ||
639 | if (NULL != my_hello) | ||
640 | { | ||
641 | GNUNET_free (my_hello); | ||
642 | my_hello = NULL; | ||
643 | } | ||
644 | } | ||
645 | |||
646 | |||
647 | /** | ||
648 | * Function called with our peer's HELLO message. | ||
649 | * Used to obtain our peer's public key. | ||
650 | * | ||
651 | * @param cls NULL | ||
652 | * @param hello the HELLO message | ||
653 | */ | ||
654 | static void | ||
655 | hello_callback (void *cls, const struct GNUNET_MessageHeader *hello) | ||
656 | { | ||
657 | if (NULL == hello) | ||
658 | { | ||
659 | fprintf (stderr, "Failed to get my own HELLO from this peer!\n"); | ||
660 | GNUNET_SCHEDULER_shutdown (); | ||
661 | return; | ||
662 | } | ||
663 | my_hello = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (hello); | ||
664 | GNUNET_assert (GNUNET_OK == | ||
665 | GNUNET_HELLO_get_id (my_hello, &my_peer_identity)); | ||
666 | GNUNET_TRANSPORT_hello_get_cancel (gh); | ||
667 | gh = NULL; | ||
668 | if (NULL != dump_hello) | ||
669 | dump_my_hello (); | ||
670 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
671 | } | ||
672 | |||
673 | |||
674 | /** | ||
675 | * Main function that will be run by the scheduler. | ||
676 | * | ||
677 | * @param cls closure | ||
678 | * @param args remaining command-line arguments | ||
679 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
680 | * @param c configuration | ||
681 | */ | ||
682 | static void | ||
683 | run (void *cls, | ||
684 | char *const *args, | ||
685 | const char *cfgfile, | ||
686 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
687 | { | ||
688 | cfg = c; | ||
689 | if ((NULL != args[0]) && (NULL == put_uri) && | ||
690 | (args[0] == strcasestr (args[0], "gnunet://hello/"))) | ||
691 | { | ||
692 | put_uri = GNUNET_strdup (args[0]); | ||
693 | args++; | ||
694 | } | ||
695 | if (NULL != args[0]) | ||
696 | { | ||
697 | fprintf (stderr, _ ("Invalid command line argument `%s'\n"), args[0]); | ||
698 | return; | ||
699 | } | ||
700 | if (NULL == (peerinfo = GNUNET_PEERINFO_connect (cfg))) | ||
701 | { | ||
702 | fprintf (stderr, "%s", "Could not access PEERINFO service. Exiting.\n"); | ||
703 | return; | ||
704 | } | ||
705 | if ((GNUNET_YES == get_self) || (GNUNET_YES == get_uri) || | ||
706 | (NULL != dump_hello)) | ||
707 | { | ||
708 | gh = GNUNET_TRANSPORT_hello_get (cfg, | ||
709 | GNUNET_TRANSPORT_AC_ANY, | ||
710 | &hello_callback, | ||
711 | NULL); | ||
712 | } | ||
713 | else | ||
714 | { | ||
715 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
716 | } | ||
717 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | ||
718 | } | ||
719 | |||
720 | |||
721 | /** | ||
722 | * Main state machine that goes over all options and | ||
723 | * runs the next requested function. | ||
724 | * | ||
725 | * @param cls unused | ||
726 | */ | ||
727 | static void | ||
728 | state_machine (void *cls) | ||
729 | { | ||
730 | tt = NULL; | ||
731 | |||
732 | if (NULL != put_uri) | ||
733 | { | ||
734 | GPI_plugins_load (cfg); | ||
735 | if (GNUNET_SYSERR == parse_hello_uri (put_uri)) | ||
736 | { | ||
737 | fprintf (stderr, _ ("Invalid URI `%s'\n"), put_uri); | ||
738 | GNUNET_SCHEDULER_shutdown (); | ||
739 | } | ||
740 | GNUNET_free (put_uri); | ||
741 | put_uri = NULL; | ||
742 | } | ||
743 | else if (GNUNET_YES == get_info) | ||
744 | { | ||
745 | get_info = GNUNET_NO; | ||
746 | GPI_plugins_load (cfg); | ||
747 | pic = GNUNET_PEERINFO_iterate (peerinfo, | ||
748 | include_friend_only, | ||
749 | NULL, | ||
750 | &print_peer_info, | ||
751 | NULL); | ||
752 | } | ||
753 | else if (GNUNET_YES == get_self) | ||
754 | { | ||
755 | get_self = GNUNET_NO; | ||
756 | if (be_quiet) | ||
757 | printf ("%s\n", GNUNET_i2s_full (&my_peer_identity)); | ||
758 | else | ||
759 | printf (_ ("I am peer `%s'.\n"), GNUNET_i2s_full (&my_peer_identity)); | ||
760 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
761 | } | ||
762 | else if (GNUNET_YES == get_uri) | ||
763 | { | ||
764 | GPI_plugins_load (cfg); | ||
765 | pic = GNUNET_PEERINFO_iterate (peerinfo, | ||
766 | include_friend_only, | ||
767 | &my_peer_identity, | ||
768 | &print_my_uri, | ||
769 | NULL); | ||
770 | get_uri = GNUNET_NO; | ||
771 | } | ||
772 | else if (GNUNET_YES == default_operation) | ||
773 | { | ||
774 | /* default operation list all */ | ||
775 | default_operation = GNUNET_NO; | ||
776 | get_info = GNUNET_YES; | ||
777 | tt = GNUNET_SCHEDULER_add_now (&state_machine, NULL); | ||
778 | } | ||
779 | else | ||
780 | { | ||
781 | GNUNET_SCHEDULER_shutdown (); | ||
782 | } | ||
783 | default_operation = GNUNET_NO; | ||
784 | } | ||
785 | |||
786 | |||
787 | /** | ||
788 | * The main function to obtain peer information. | ||
789 | * | ||
790 | * @param argc number of arguments from the command line | ||
791 | * @param argv command line arguments | ||
792 | * @return 0 ok, 1 on error | ||
793 | */ | ||
794 | int | ||
795 | main (int argc, char *const *argv) | ||
796 | { | ||
797 | struct GNUNET_GETOPT_CommandLineOption options[] = | ||
798 | { GNUNET_GETOPT_option_flag ('n', | ||
799 | "numeric", | ||
800 | gettext_noop ("don't resolve host names"), | ||
801 | &no_resolve), | ||
802 | |||
803 | GNUNET_GETOPT_option_flag ('q', | ||
804 | "quiet", | ||
805 | gettext_noop ( | ||
806 | "output only the identity strings"), | ||
807 | &be_quiet), | ||
808 | GNUNET_GETOPT_option_flag ('f', | ||
809 | "friends", | ||
810 | gettext_noop ( | ||
811 | "include friend-only information"), | ||
812 | &include_friend_only), | ||
813 | |||
814 | GNUNET_GETOPT_option_flag ('s', | ||
815 | "self", | ||
816 | gettext_noop ("output our own identity only"), | ||
817 | &get_self), | ||
818 | |||
819 | GNUNET_GETOPT_option_flag ('i', | ||
820 | "info", | ||
821 | gettext_noop ("list all known peers"), | ||
822 | &get_info), | ||
823 | |||
824 | GNUNET_GETOPT_option_string ('d', | ||
825 | "dump-hello", | ||
826 | NULL, | ||
827 | gettext_noop ("dump hello to file"), | ||
828 | &dump_hello), | ||
829 | |||
830 | GNUNET_GETOPT_option_flag ('g', | ||
831 | "get-hello", | ||
832 | gettext_noop ("also output HELLO uri(s)"), | ||
833 | &get_uri), | ||
834 | |||
835 | GNUNET_GETOPT_option_string ('p', | ||
836 | "put-hello", | ||
837 | "HELLO", | ||
838 | gettext_noop ( | ||
839 | "add given HELLO uri to the database"), | ||
840 | &put_uri), | ||
841 | |||
842 | GNUNET_GETOPT_OPTION_END }; | ||
843 | int ret; | ||
844 | |||
845 | default_operation = GNUNET_YES; | ||
846 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
847 | return 2; | ||
848 | |||
849 | ret = (GNUNET_OK == | ||
850 | GNUNET_PROGRAM_run (argc, | ||
851 | argv, | ||
852 | "gnunet-peerinfo", | ||
853 | gettext_noop ("Print information about peers."), | ||
854 | options, | ||
855 | &run, | ||
856 | NULL)) | ||
857 | ? 0 | ||
858 | : 1; | ||
859 | GNUNET_free_nz ((void *) argv); | ||
860 | return ret; | ||
861 | } | ||
862 | |||
863 | |||
864 | /* end of gnunet-peerinfo.c */ | ||
diff --git a/src/peerinfo-tool/gnunet-peerinfo_plugins.c b/src/peerinfo-tool/gnunet-peerinfo_plugins.c deleted file mode 100644 index e196ec58a..000000000 --- a/src/peerinfo-tool/gnunet-peerinfo_plugins.c +++ /dev/null | |||
@@ -1,196 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2010,2011 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file peerinfo-tool/gnunet-peerinfo_plugins.c | ||
23 | * @brief plugin management | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet-peerinfo_plugins.h" | ||
28 | #include "gnunet_transport_plugin.h" | ||
29 | #include "gnunet_hello_lib.h" | ||
30 | |||
31 | /** | ||
32 | * Entry in doubly-linked list of all of our plugins. | ||
33 | */ | ||
34 | struct TransportPlugin | ||
35 | { | ||
36 | /** | ||
37 | * This is a doubly-linked list. | ||
38 | */ | ||
39 | struct TransportPlugin *next; | ||
40 | |||
41 | /** | ||
42 | * This is a doubly-linked list. | ||
43 | */ | ||
44 | struct TransportPlugin *prev; | ||
45 | |||
46 | /** | ||
47 | * API of the transport as returned by the plugin's | ||
48 | * initialization function. | ||
49 | */ | ||
50 | struct GNUNET_TRANSPORT_PluginFunctions *api; | ||
51 | |||
52 | /** | ||
53 | * Short name for the plugin (e.g. "tcp"). | ||
54 | */ | ||
55 | char *short_name; | ||
56 | |||
57 | /** | ||
58 | * Name of the library (e.g. "gnunet_plugin_transport_tcp"). | ||
59 | */ | ||
60 | char *lib_name; | ||
61 | |||
62 | /** | ||
63 | * Environment this transport service is using | ||
64 | * for this plugin. | ||
65 | */ | ||
66 | struct GNUNET_TRANSPORT_PluginEnvironment env; | ||
67 | }; | ||
68 | |||
69 | /** | ||
70 | * Head of DLL of all loaded plugins. | ||
71 | */ | ||
72 | static struct TransportPlugin *plugins_head; | ||
73 | |||
74 | /** | ||
75 | * Head of DLL of all loaded plugins. | ||
76 | */ | ||
77 | static struct TransportPlugin *plugins_tail; | ||
78 | |||
79 | |||
80 | /** | ||
81 | * Load and initialize all plugins. The respective functions will be | ||
82 | * invoked by the plugins when the respective events happen. The | ||
83 | * closure will be set to a 'const char*' containing the name of the | ||
84 | * plugin that caused the call. | ||
85 | * | ||
86 | * @param cfg configuration to use | ||
87 | */ | ||
88 | void | ||
89 | GPI_plugins_load (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
90 | { | ||
91 | struct TransportPlugin *plug; | ||
92 | struct TransportPlugin *next; | ||
93 | char *libname; | ||
94 | char *plugs; | ||
95 | char *pos; | ||
96 | |||
97 | if (NULL != plugins_head) | ||
98 | return; /* already loaded */ | ||
99 | if (GNUNET_OK != | ||
100 | GNUNET_CONFIGURATION_get_value_string (cfg, "TRANSPORT", "PLUGINS", | ||
101 | &plugs)) | ||
102 | return; | ||
103 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Starting transport plugins `%s'\n"), | ||
104 | plugs); | ||
105 | for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " ")) | ||
106 | { | ||
107 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' transport plugin\n"), | ||
108 | pos); | ||
109 | GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", pos); | ||
110 | plug = GNUNET_new (struct TransportPlugin); | ||
111 | plug->short_name = GNUNET_strdup (pos); | ||
112 | plug->lib_name = libname; | ||
113 | plug->env.cfg = cfg; | ||
114 | plug->env.cls = plug->short_name; | ||
115 | GNUNET_CONTAINER_DLL_insert (plugins_head, plugins_tail, plug); | ||
116 | } | ||
117 | GNUNET_free (plugs); | ||
118 | next = plugins_head; | ||
119 | while (next != NULL) | ||
120 | { | ||
121 | plug = next; | ||
122 | next = plug->next; | ||
123 | plug->api = GNUNET_PLUGIN_load (plug->lib_name, &plug->env); | ||
124 | if (plug->api == NULL) | ||
125 | { | ||
126 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
127 | _ ("Failed to load transport plugin for `%s'\n"), | ||
128 | plug->lib_name); | ||
129 | GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug); | ||
130 | GNUNET_free (plug->short_name); | ||
131 | GNUNET_free (plug->lib_name); | ||
132 | GNUNET_free (plug); | ||
133 | } | ||
134 | } | ||
135 | } | ||
136 | |||
137 | |||
138 | /** | ||
139 | * Unload all plugins | ||
140 | */ | ||
141 | void | ||
142 | GPI_plugins_unload () | ||
143 | { | ||
144 | struct TransportPlugin *plug; | ||
145 | |||
146 | while (NULL != (plug = plugins_head)) | ||
147 | { | ||
148 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api)); | ||
149 | GNUNET_free (plug->lib_name); | ||
150 | GNUNET_free (plug->short_name); | ||
151 | GNUNET_CONTAINER_DLL_remove (plugins_head, plugins_tail, plug); | ||
152 | GNUNET_free (plug); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | |||
157 | /** | ||
158 | * Obtain the plugin API based on a plugin name. | ||
159 | * | ||
160 | * @param name name of the plugin | ||
161 | * @return the plugin's API, NULL if the plugin is not loaded | ||
162 | */ | ||
163 | struct GNUNET_TRANSPORT_PluginFunctions * | ||
164 | GPI_plugins_find (const char *name) | ||
165 | { | ||
166 | struct TransportPlugin *head = plugins_head; | ||
167 | |||
168 | char *stripped = GNUNET_strdup (name); | ||
169 | char *head_stripped; | ||
170 | char *sep = strchr (stripped, '_'); | ||
171 | |||
172 | if (NULL != sep) | ||
173 | sep[0] = '\0'; | ||
174 | |||
175 | while (head != NULL) | ||
176 | { | ||
177 | head_stripped = GNUNET_strdup (head->short_name); | ||
178 | char *head_sep = strchr (head_stripped, '_'); | ||
179 | if (NULL != head_sep) | ||
180 | head_sep[0] = '\0'; | ||
181 | if (0 == strcmp (head_stripped, stripped)) | ||
182 | { | ||
183 | GNUNET_free (head_stripped); | ||
184 | break; | ||
185 | } | ||
186 | GNUNET_free (head_stripped); | ||
187 | head = head->next; | ||
188 | } | ||
189 | GNUNET_free (stripped); | ||
190 | if (NULL == head) | ||
191 | return NULL; | ||
192 | return head->api; | ||
193 | } | ||
194 | |||
195 | |||
196 | /* end of file gnunet-peerinfo_plugins.c */ | ||
diff --git a/src/peerinfo-tool/gnunet-peerinfo_plugins.h b/src/peerinfo-tool/gnunet-peerinfo_plugins.h deleted file mode 100644 index ed8da87d5..000000000 --- a/src/peerinfo-tool/gnunet-peerinfo_plugins.h +++ /dev/null | |||
@@ -1,58 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2010,2011 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file peerinfo-tool/gnunet-peerinfo_plugins.h | ||
23 | * @brief plugin management API | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #ifndef GNUNET_PEERINFO_PLUGINS_H | ||
27 | #define GNUNET_PEERINFO_PLUGINS_H | ||
28 | |||
29 | #include "gnunet_util_lib.h" | ||
30 | |||
31 | /** | ||
32 | * Load transport plugins. | ||
33 | * | ||
34 | * @param cfg configuration to use | ||
35 | */ | ||
36 | void | ||
37 | GPI_plugins_load (const struct GNUNET_CONFIGURATION_Handle *cfg); | ||
38 | |||
39 | |||
40 | /** | ||
41 | * Unload all plugins | ||
42 | */ | ||
43 | void | ||
44 | GPI_plugins_unload (void); | ||
45 | |||
46 | |||
47 | /** | ||
48 | * Obtain the plugin API based on a plugin name. | ||
49 | * | ||
50 | * @param name name of the plugin | ||
51 | * @return the plugin's API, NULL if the plugin is not loaded | ||
52 | */ | ||
53 | struct GNUNET_TRANSPORT_PluginFunctions * | ||
54 | GPI_plugins_find (const char *name); | ||
55 | |||
56 | |||
57 | #endif | ||
58 | /* end of file gnunet-peerinfo_plugins.h */ | ||
diff --git a/src/peerinfo-tool/meson.build b/src/peerinfo-tool/meson.build deleted file mode 100644 index 2d5386eba..000000000 --- a/src/peerinfo-tool/meson.build +++ /dev/null | |||
@@ -1,31 +0,0 @@ | |||
1 | gnunetpeerinfotool_src = ['gnunet-peerinfo.c', 'gnunet-peerinfo_plugins.c'] | ||
2 | |||
3 | |||
4 | if get_option('monolith') | ||
5 | subdir_done() | ||
6 | endif | ||
7 | |||
8 | shared_module('gnunet_plugin_rest_peerinfo', | ||
9 | ['plugin_rest_peerinfo.c'], | ||
10 | dependencies: [libgnunetrest_dep, | ||
11 | libgnunetpeerinfo_dep, | ||
12 | libgnunettransport_dep, | ||
13 | libgnunethello_dep, | ||
14 | libgnunetutil_dep, | ||
15 | json_dep, | ||
16 | mhd_dep], | ||
17 | include_directories: [incdir, configuration_inc], | ||
18 | install: true, | ||
19 | install_dir: get_option('libdir') / 'gnunet') | ||
20 | |||
21 | |||
22 | executable ('gnunet-peerinfo', | ||
23 | gnunetpeerinfotool_src, | ||
24 | dependencies: [libgnunetutil_dep, | ||
25 | libgnunetpeerinfo_dep, | ||
26 | libgnunettransport_dep, | ||
27 | libgnunethello_dep, | ||
28 | libgnunetstatistics_dep], | ||
29 | include_directories: [incdir, configuration_inc], | ||
30 | install: true, | ||
31 | install_dir: get_option('bindir')) | ||
diff --git a/src/peerinfo-tool/plugin_rest_peerinfo.c b/src/peerinfo-tool/plugin_rest_peerinfo.c deleted file mode 100644 index 13e2e863b..000000000 --- a/src/peerinfo-tool/plugin_rest_peerinfo.c +++ /dev/null | |||
@@ -1,842 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012-2015 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @author Martin Schanzenbach | ||
22 | * @author Philippe Buschmann | ||
23 | * @file peerinfo/plugin_rest_peerinfo.c | ||
24 | * @brief GNUnet Peerinfo REST plugin | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_rest_plugin.h" | ||
29 | #include "gnunet_peerinfo_service.h" | ||
30 | #include "gnunet_transport_service.h" | ||
31 | #include "gnunet_rest_lib.h" | ||
32 | #include "gnunet_json_lib.h" | ||
33 | #include "microhttpd.h" | ||
34 | #include <jansson.h> | ||
35 | |||
36 | /** | ||
37 | * Peerinfo Namespace | ||
38 | */ | ||
39 | #define GNUNET_REST_API_NS_PEERINFO "/peerinfo" | ||
40 | |||
41 | /** | ||
42 | * Peerinfo parameter peer | ||
43 | */ | ||
44 | #define GNUNET_REST_PEERINFO_PEER "peer" | ||
45 | |||
46 | /** | ||
47 | * Peerinfo parameter friend | ||
48 | */ | ||
49 | #define GNUNET_REST_PEERINFO_FRIEND "friend" | ||
50 | |||
51 | /** | ||
52 | * Peerinfo parameter array | ||
53 | */ | ||
54 | #define GNUNET_REST_PEERINFO_ARRAY "array" | ||
55 | |||
56 | /** | ||
57 | * Error message Unknown Error | ||
58 | */ | ||
59 | #define GNUNET_REST_PEERINFO_ERROR_UNKNOWN "Unknown Error" | ||
60 | |||
61 | /** | ||
62 | * How long until we time out during address lookup? | ||
63 | */ | ||
64 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) | ||
65 | /** | ||
66 | * The configuration handle | ||
67 | */ | ||
68 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
69 | |||
70 | /** | ||
71 | * HTTP methods allows for this plugin | ||
72 | */ | ||
73 | static char *allow_methods; | ||
74 | |||
75 | /** | ||
76 | * Handle to PEERINFO | ||
77 | */ | ||
78 | static struct GNUNET_PEERINFO_Handle *peerinfo_handle; | ||
79 | |||
80 | /** | ||
81 | * @brief struct returned by the initialization function of the plugin | ||
82 | */ | ||
83 | struct Plugin | ||
84 | { | ||
85 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
86 | }; | ||
87 | |||
88 | |||
89 | /** | ||
90 | * Record we keep for each printable address. | ||
91 | */ | ||
92 | struct AddressRecord | ||
93 | { | ||
94 | /** | ||
95 | * Current address-to-string context (if active, otherwise NULL). | ||
96 | */ | ||
97 | struct GNUNET_TRANSPORT_AddressToStringContext *atsc; | ||
98 | |||
99 | /** | ||
100 | * Address expiration time | ||
101 | */ | ||
102 | struct GNUNET_TIME_Absolute expiration; | ||
103 | |||
104 | /** | ||
105 | * Printable address. | ||
106 | */ | ||
107 | char *result; | ||
108 | |||
109 | /** | ||
110 | * Print context this address record belongs to. | ||
111 | */ | ||
112 | struct PrintContext *pc; | ||
113 | }; | ||
114 | |||
115 | |||
116 | /** | ||
117 | * Structure we use to collect printable address information. | ||
118 | */ | ||
119 | struct PrintContext | ||
120 | { | ||
121 | /** | ||
122 | * Kept in DLL. | ||
123 | */ | ||
124 | struct PrintContext *next; | ||
125 | |||
126 | /** | ||
127 | * Kept in DLL. | ||
128 | */ | ||
129 | struct PrintContext *prev; | ||
130 | |||
131 | /** | ||
132 | * Identity of the peer. | ||
133 | */ | ||
134 | struct GNUNET_PeerIdentity peer; | ||
135 | |||
136 | /** | ||
137 | * List of printable addresses. | ||
138 | */ | ||
139 | struct AddressRecord *address_list; | ||
140 | |||
141 | /** | ||
142 | * Number of completed addresses in @e address_list. | ||
143 | */ | ||
144 | unsigned int num_addresses; | ||
145 | |||
146 | /** | ||
147 | * Number of addresses allocated in @e address_list. | ||
148 | */ | ||
149 | unsigned int address_list_size; | ||
150 | |||
151 | /** | ||
152 | * Current offset in @e address_list (counted down). | ||
153 | */ | ||
154 | unsigned int off; | ||
155 | |||
156 | /** | ||
157 | * Hello was friend only, #GNUNET_YES or #GNUNET_NO | ||
158 | */ | ||
159 | int friend_only; | ||
160 | |||
161 | /** | ||
162 | * RequestHandle | ||
163 | */ | ||
164 | struct RequestHandle *handle; | ||
165 | }; | ||
166 | |||
167 | /** | ||
168 | * Head of list of print contexts. | ||
169 | */ | ||
170 | static struct PrintContext *pc_head; | ||
171 | |||
172 | /** | ||
173 | * Tail of list of print contexts. | ||
174 | */ | ||
175 | static struct PrintContext *pc_tail; | ||
176 | |||
177 | /** | ||
178 | * The request handle | ||
179 | */ | ||
180 | struct RequestHandle | ||
181 | { | ||
182 | /** | ||
183 | * DLL | ||
184 | */ | ||
185 | struct RequestHandle *next; | ||
186 | |||
187 | /** | ||
188 | * DLL | ||
189 | */ | ||
190 | struct RequestHandle *prev; | ||
191 | |||
192 | /** | ||
193 | * JSON temporary array | ||
194 | */ | ||
195 | json_t *temp_array; | ||
196 | |||
197 | /** | ||
198 | * Expiration time string | ||
199 | */ | ||
200 | char *expiration_str; | ||
201 | |||
202 | /** | ||
203 | * Address string | ||
204 | */ | ||
205 | const char *address; | ||
206 | |||
207 | /** | ||
208 | * Iteration peer public key | ||
209 | */ | ||
210 | char *pubkey; | ||
211 | |||
212 | /** | ||
213 | * JSON response | ||
214 | */ | ||
215 | json_t *response; | ||
216 | |||
217 | /** | ||
218 | * Handle to PEERINFO it | ||
219 | */ | ||
220 | struct GNUNET_PEERINFO_IteratorContext *list_it; | ||
221 | |||
222 | |||
223 | /** | ||
224 | * Rest connection | ||
225 | */ | ||
226 | struct GNUNET_REST_RequestHandle *rest_handle; | ||
227 | |||
228 | /** | ||
229 | * Desired timeout for the lookup (default is no timeout). | ||
230 | */ | ||
231 | struct GNUNET_TIME_Relative timeout; | ||
232 | |||
233 | /** | ||
234 | * ID of a task associated with the resolution process. | ||
235 | */ | ||
236 | struct GNUNET_SCHEDULER_Task *timeout_task; | ||
237 | |||
238 | /** | ||
239 | * The plugin result processor | ||
240 | */ | ||
241 | GNUNET_REST_ResultProcessor proc; | ||
242 | |||
243 | /** | ||
244 | * The closure of the result processor | ||
245 | */ | ||
246 | void *proc_cls; | ||
247 | |||
248 | /** | ||
249 | * The url | ||
250 | */ | ||
251 | char *url; | ||
252 | |||
253 | /** | ||
254 | * Error response message | ||
255 | */ | ||
256 | char *emsg; | ||
257 | |||
258 | /** | ||
259 | * Response code | ||
260 | */ | ||
261 | int response_code; | ||
262 | }; | ||
263 | |||
264 | /** | ||
265 | * DLL | ||
266 | */ | ||
267 | static struct RequestHandle *requests_head; | ||
268 | |||
269 | /** | ||
270 | * DLL | ||
271 | */ | ||
272 | static struct RequestHandle *requests_tail; | ||
273 | |||
274 | /** | ||
275 | * Cleanup lookup handle | ||
276 | * @param handle Handle to clean up | ||
277 | */ | ||
278 | static void | ||
279 | cleanup_handle (void *cls) | ||
280 | { | ||
281 | struct RequestHandle *handle = cls; | ||
282 | |||
283 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
284 | "Cleaning up\n"); | ||
285 | if (NULL != handle->timeout_task) | ||
286 | { | ||
287 | GNUNET_SCHEDULER_cancel (handle->timeout_task); | ||
288 | handle->timeout_task = NULL; | ||
289 | } | ||
290 | if (NULL != handle->url) | ||
291 | GNUNET_free (handle->url); | ||
292 | if (NULL != handle->emsg) | ||
293 | GNUNET_free (handle->emsg); | ||
294 | if (NULL != handle->address) | ||
295 | GNUNET_free_nz ((char *) handle->address); | ||
296 | if (NULL != handle->expiration_str) | ||
297 | GNUNET_free (handle->expiration_str); | ||
298 | if (NULL != handle->pubkey) | ||
299 | GNUNET_free (handle->pubkey); | ||
300 | |||
301 | if (NULL != handle->temp_array) | ||
302 | { | ||
303 | json_decref (handle->temp_array); | ||
304 | handle->temp_array = NULL; | ||
305 | } | ||
306 | if (NULL != handle->response) | ||
307 | { | ||
308 | json_decref (handle->response); | ||
309 | handle->response = NULL; | ||
310 | } | ||
311 | |||
312 | if (NULL != handle->list_it) | ||
313 | { | ||
314 | GNUNET_PEERINFO_iterate_cancel (handle->list_it); | ||
315 | handle->list_it = NULL; | ||
316 | } | ||
317 | if (NULL != peerinfo_handle) | ||
318 | { | ||
319 | GNUNET_PEERINFO_disconnect (peerinfo_handle); | ||
320 | peerinfo_handle = NULL; | ||
321 | } | ||
322 | GNUNET_CONTAINER_DLL_remove (requests_head, | ||
323 | requests_tail, | ||
324 | handle); | ||
325 | GNUNET_free (handle); | ||
326 | } | ||
327 | |||
328 | |||
329 | /** | ||
330 | * Task run on errors. Reports an error and cleans up everything. | ||
331 | * | ||
332 | * @param cls the `struct RequestHandle` | ||
333 | */ | ||
334 | static void | ||
335 | do_error (void *cls) | ||
336 | { | ||
337 | struct RequestHandle *handle = cls; | ||
338 | struct MHD_Response *resp; | ||
339 | json_t *json_error = json_object (); | ||
340 | char *response; | ||
341 | |||
342 | if (NULL == handle->emsg) | ||
343 | handle->emsg = GNUNET_strdup (GNUNET_REST_PEERINFO_ERROR_UNKNOWN); | ||
344 | |||
345 | json_object_set_new (json_error, "error", json_string (handle->emsg)); | ||
346 | |||
347 | if (0 == handle->response_code) | ||
348 | handle->response_code = MHD_HTTP_OK; | ||
349 | response = json_dumps (json_error, 0); | ||
350 | resp = GNUNET_REST_create_response (response); | ||
351 | MHD_add_response_header (resp, "Content-Type", "application/json"); | ||
352 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
353 | json_decref (json_error); | ||
354 | GNUNET_free (response); | ||
355 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
356 | } | ||
357 | |||
358 | |||
359 | /** | ||
360 | * Function that assembles the response. | ||
361 | * | ||
362 | * @param cls the `struct RequestHandle` | ||
363 | */ | ||
364 | static void | ||
365 | peerinfo_list_finished (void *cls) | ||
366 | { | ||
367 | struct RequestHandle *handle = cls; | ||
368 | char *result_str; | ||
369 | struct MHD_Response *resp; | ||
370 | |||
371 | if (NULL == handle->response) | ||
372 | { | ||
373 | handle->response_code = MHD_HTTP_NOT_FOUND; | ||
374 | handle->emsg = GNUNET_strdup ("No peers found"); | ||
375 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
376 | return; | ||
377 | } | ||
378 | |||
379 | result_str = json_dumps (handle->response, 0); | ||
380 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); | ||
381 | resp = GNUNET_REST_create_response (result_str); | ||
382 | GNUNET_assert (MHD_NO != MHD_add_response_header (resp, | ||
383 | "Content-Type", | ||
384 | "application/json")); | ||
385 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
386 | GNUNET_free (result_str); | ||
387 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
388 | } | ||
389 | |||
390 | |||
391 | /** | ||
392 | * Iterator callback to go over all addresses and count them. | ||
393 | * | ||
394 | * @param cls `struct PrintContext *` with `off` to increment | ||
395 | * @param address the address | ||
396 | * @param expiration expiration time | ||
397 | * @return #GNUNET_OK to keep the address and continue | ||
398 | */ | ||
399 | static int | ||
400 | count_address (void *cls, | ||
401 | const struct GNUNET_HELLO_Address *address, | ||
402 | struct GNUNET_TIME_Absolute expiration) | ||
403 | { | ||
404 | struct PrintContext *pc = cls; | ||
405 | |||
406 | if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us) | ||
407 | { | ||
408 | return GNUNET_OK; /* ignore expired address */ | ||
409 | } | ||
410 | |||
411 | pc->off++; | ||
412 | return GNUNET_OK; | ||
413 | } | ||
414 | |||
415 | |||
416 | /** | ||
417 | * Print the collected address information to the console and free @a pc. | ||
418 | * | ||
419 | * @param pc printing context | ||
420 | */ | ||
421 | static void | ||
422 | dump_pc (struct PrintContext *pc) | ||
423 | { | ||
424 | struct RequestHandle *handle; | ||
425 | unsigned int i; | ||
426 | json_t *response_entry; | ||
427 | json_t *temp_array; | ||
428 | json_t *object; | ||
429 | json_t *address; | ||
430 | json_t *expires; | ||
431 | json_t *friend_and_peer_json; | ||
432 | char *friend_and_peer; | ||
433 | |||
434 | temp_array = json_array (); | ||
435 | response_entry = json_object (); | ||
436 | |||
437 | for (i = 0; i < pc->num_addresses; i++) | ||
438 | { | ||
439 | if (NULL != pc->address_list[i].result) | ||
440 | { | ||
441 | object = json_object (); | ||
442 | address = json_string (pc->address_list[i].result); | ||
443 | expires = json_string ( | ||
444 | GNUNET_STRINGS_absolute_time_to_string ( | ||
445 | pc->address_list[i].expiration)); | ||
446 | json_object_set (object, "address", address); | ||
447 | json_object_set (object, "expires", expires); | ||
448 | |||
449 | json_decref (address); | ||
450 | json_decref (expires); | ||
451 | |||
452 | json_array_append (temp_array, object); | ||
453 | json_decref (object); | ||
454 | GNUNET_free (pc->address_list[i].result); | ||
455 | } | ||
456 | } | ||
457 | |||
458 | if (0 < json_array_size (temp_array)) | ||
459 | { | ||
460 | GNUNET_asprintf (&friend_and_peer, | ||
461 | "%s%s", | ||
462 | (GNUNET_YES == pc->friend_only) ? "F2F:" : "", | ||
463 | GNUNET_i2s_full (&pc->peer)); | ||
464 | friend_and_peer_json = json_string (friend_and_peer); | ||
465 | json_object_set (response_entry, | ||
466 | GNUNET_REST_PEERINFO_PEER, | ||
467 | friend_and_peer_json); | ||
468 | json_object_set (response_entry, | ||
469 | GNUNET_REST_PEERINFO_ARRAY, | ||
470 | temp_array); | ||
471 | json_array_append (pc->handle->response, response_entry); | ||
472 | json_decref (friend_and_peer_json); | ||
473 | GNUNET_free (friend_and_peer); | ||
474 | } | ||
475 | |||
476 | json_decref (temp_array); | ||
477 | json_decref (response_entry); | ||
478 | |||
479 | GNUNET_free (pc->address_list); | ||
480 | GNUNET_CONTAINER_DLL_remove (pc_head, | ||
481 | pc_tail, | ||
482 | pc); | ||
483 | handle = pc->handle; | ||
484 | GNUNET_free (pc); | ||
485 | |||
486 | if ((NULL == pc_head) && | ||
487 | (NULL == handle->list_it)) | ||
488 | { | ||
489 | GNUNET_SCHEDULER_add_now (&peerinfo_list_finished, handle); | ||
490 | } | ||
491 | } | ||
492 | |||
493 | |||
494 | /** | ||
495 | * Function to call with a human-readable format of an address | ||
496 | * | ||
497 | * @param cls closure | ||
498 | * @param address NULL on error, otherwise 0-terminated printable UTF-8 string | ||
499 | * @param res result of the address to string conversion: | ||
500 | * if #GNUNET_OK: address was valid (conversion to | ||
501 | * string might still have failed) | ||
502 | * if #GNUNET_SYSERR: address is invalid | ||
503 | */ | ||
504 | static void | ||
505 | process_resolved_address (void *cls, | ||
506 | const char *address, | ||
507 | int res) | ||
508 | { | ||
509 | struct AddressRecord *ar = cls; | ||
510 | struct PrintContext *pc = ar->pc; | ||
511 | |||
512 | if (NULL != address) | ||
513 | { | ||
514 | if (0 != strlen (address)) | ||
515 | { | ||
516 | if (NULL != ar->result) | ||
517 | GNUNET_free (ar->result); | ||
518 | ar->result = GNUNET_strdup (address); | ||
519 | } | ||
520 | return; | ||
521 | } | ||
522 | ar->atsc = NULL; | ||
523 | if (GNUNET_SYSERR == res) | ||
524 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
525 | _ ("Failure: Cannot convert address to string for peer `%s'\n"), | ||
526 | GNUNET_i2s (&ar->pc->peer)); | ||
527 | pc->num_addresses++; | ||
528 | if (pc->num_addresses == pc->address_list_size) | ||
529 | dump_pc (ar->pc); | ||
530 | } | ||
531 | |||
532 | |||
533 | /** | ||
534 | * Iterator callback to go over all addresses. | ||
535 | * | ||
536 | * @param cls closure | ||
537 | * @param address the address | ||
538 | * @param expiration expiration time | ||
539 | * @return #GNUNET_OK to keep the address and continue | ||
540 | */ | ||
541 | static int | ||
542 | print_address (void *cls, | ||
543 | const struct GNUNET_HELLO_Address *address, | ||
544 | struct GNUNET_TIME_Absolute expiration) | ||
545 | { | ||
546 | struct PrintContext *pc = cls; | ||
547 | struct AddressRecord *ar; | ||
548 | |||
549 | if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us) | ||
550 | { | ||
551 | return GNUNET_OK; /* ignore expired address */ | ||
552 | } | ||
553 | |||
554 | GNUNET_assert (0 < pc->off); | ||
555 | ar = &pc->address_list[--pc->off]; | ||
556 | ar->pc = pc; | ||
557 | ar->expiration = expiration; | ||
558 | GNUNET_asprintf (&ar->result, | ||
559 | "%s:%lu:%u", | ||
560 | address->transport_name, | ||
561 | (unsigned long) address->address_length, | ||
562 | address->local_info); | ||
563 | ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg, | ||
564 | address, | ||
565 | GNUNET_NO, | ||
566 | TIMEOUT, | ||
567 | &process_resolved_address, | ||
568 | ar); | ||
569 | return GNUNET_OK; | ||
570 | } | ||
571 | |||
572 | |||
573 | /** | ||
574 | * Callback that processes each of the known HELLOs for the | ||
575 | * iteration response construction. | ||
576 | * | ||
577 | * @param cls closure, NULL | ||
578 | * @param peer id of the peer, NULL for last call | ||
579 | * @param hello hello message for the peer (can be NULL) | ||
580 | * @param err_msg message | ||
581 | */ | ||
582 | void | ||
583 | peerinfo_list_iteration (void *cls, | ||
584 | const struct GNUNET_PeerIdentity *peer, | ||
585 | const struct GNUNET_HELLO_Message *hello, | ||
586 | const char *err_msg) | ||
587 | { | ||
588 | struct RequestHandle *handle = cls; | ||
589 | struct PrintContext *pc; | ||
590 | int friend_only; | ||
591 | |||
592 | if (NULL == handle->response) | ||
593 | { | ||
594 | handle->response = json_array (); | ||
595 | } | ||
596 | |||
597 | if (NULL == peer) | ||
598 | { | ||
599 | handle->list_it = NULL; | ||
600 | handle->emsg = GNUNET_strdup ("Error in communication with peerinfo"); | ||
601 | if (NULL != err_msg) | ||
602 | { | ||
603 | GNUNET_free (handle->emsg); | ||
604 | handle->emsg = GNUNET_strdup (err_msg); | ||
605 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | ||
606 | } | ||
607 | if (NULL == pc_head) | ||
608 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
609 | return; | ||
610 | } | ||
611 | if (NULL == hello) | ||
612 | return; | ||
613 | |||
614 | friend_only = GNUNET_NO; | ||
615 | if (NULL != hello) | ||
616 | friend_only = GNUNET_HELLO_is_friend_only (hello); | ||
617 | |||
618 | pc = GNUNET_new (struct PrintContext); | ||
619 | GNUNET_CONTAINER_DLL_insert (pc_head, | ||
620 | pc_tail, | ||
621 | pc); | ||
622 | pc->peer = *peer; | ||
623 | pc->friend_only = friend_only; | ||
624 | pc->handle = handle; | ||
625 | GNUNET_HELLO_iterate_addresses (hello, | ||
626 | GNUNET_NO, | ||
627 | &count_address, | ||
628 | pc); | ||
629 | if (0 == pc->off) | ||
630 | { | ||
631 | dump_pc (pc); | ||
632 | return; | ||
633 | } | ||
634 | pc->address_list_size = pc->off; | ||
635 | pc->address_list = GNUNET_malloc ( | ||
636 | sizeof(struct AddressRecord) * pc->off); | ||
637 | GNUNET_HELLO_iterate_addresses (hello, | ||
638 | GNUNET_NO, | ||
639 | &print_address, | ||
640 | pc); | ||
641 | } | ||
642 | |||
643 | |||
644 | /** | ||
645 | * Handle peerinfo GET request | ||
646 | * | ||
647 | * @param con_handle the connection handle | ||
648 | * @param url the url | ||
649 | * @param cls the RequestHandle | ||
650 | */ | ||
651 | void | ||
652 | peerinfo_get (struct GNUNET_REST_RequestHandle *con_handle, | ||
653 | const char*url, | ||
654 | void *cls) | ||
655 | { | ||
656 | struct RequestHandle *handle = cls; | ||
657 | struct GNUNET_HashCode key; | ||
658 | const struct GNUNET_PeerIdentity *specific_peer; | ||
659 | // GNUNET_PEER_Id peer_id; | ||
660 | int include_friend_only; | ||
661 | char*include_friend_only_str; | ||
662 | |||
663 | include_friend_only = GNUNET_NO; | ||
664 | GNUNET_CRYPTO_hash (GNUNET_REST_PEERINFO_FRIEND, | ||
665 | strlen (GNUNET_REST_PEERINFO_FRIEND), | ||
666 | &key); | ||
667 | if (GNUNET_YES | ||
668 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
669 | &key)) | ||
670 | { | ||
671 | include_friend_only_str = GNUNET_CONTAINER_multihashmap_get ( | ||
672 | con_handle->url_param_map, &key); | ||
673 | if (0 == strcmp (include_friend_only_str, "yes")) | ||
674 | { | ||
675 | include_friend_only = GNUNET_YES; | ||
676 | } | ||
677 | } | ||
678 | |||
679 | specific_peer = NULL; | ||
680 | GNUNET_CRYPTO_hash (GNUNET_REST_PEERINFO_PEER, | ||
681 | strlen (GNUNET_REST_PEERINFO_PEER), | ||
682 | &key); | ||
683 | if (GNUNET_YES | ||
684 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
685 | &key)) | ||
686 | { | ||
687 | // peer_id = *(unsigned int*)GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key); | ||
688 | // specific_peer = GNUNET_PEER_resolve2(peer_id); | ||
689 | } | ||
690 | |||
691 | handle->list_it = GNUNET_PEERINFO_iterate (peerinfo_handle, | ||
692 | include_friend_only, | ||
693 | specific_peer, | ||
694 | &peerinfo_list_iteration, | ||
695 | handle); | ||
696 | } | ||
697 | |||
698 | |||
699 | /** | ||
700 | * Respond to OPTIONS request | ||
701 | * | ||
702 | * @param con_handle the connection handle | ||
703 | * @param url the url | ||
704 | * @param cls the RequestHandle | ||
705 | */ | ||
706 | static void | ||
707 | options_cont (struct GNUNET_REST_RequestHandle *con_handle, | ||
708 | const char*url, | ||
709 | void *cls) | ||
710 | { | ||
711 | struct MHD_Response *resp; | ||
712 | struct RequestHandle *handle = cls; | ||
713 | |||
714 | // independent of path return all options | ||
715 | resp = GNUNET_REST_create_response (NULL); | ||
716 | MHD_add_response_header (resp, | ||
717 | "Access-Control-Allow-Methods", | ||
718 | allow_methods); | ||
719 | handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); | ||
720 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
721 | return; | ||
722 | } | ||
723 | |||
724 | |||
725 | /** | ||
726 | * Function processing the REST call | ||
727 | * | ||
728 | * @param method HTTP method | ||
729 | * @param url URL of the HTTP request | ||
730 | * @param data body of the HTTP request (optional) | ||
731 | * @param data_size length of the body | ||
732 | * @param proc callback function for the result | ||
733 | * @param proc_cls closure for callback function | ||
734 | * @return GNUNET_OK if request accepted | ||
735 | */ | ||
736 | static enum GNUNET_GenericReturnValue | ||
737 | rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle, | ||
738 | GNUNET_REST_ResultProcessor proc, | ||
739 | void *proc_cls) | ||
740 | { | ||
741 | struct RequestHandle *handle = GNUNET_new (struct RequestHandle); | ||
742 | struct GNUNET_REST_RequestHandlerError err; | ||
743 | static const struct GNUNET_REST_RequestHandler handlers[] = { | ||
744 | { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_PEERINFO, &peerinfo_get }, | ||
745 | { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_PEERINFO, &options_cont }, | ||
746 | GNUNET_REST_HANDLER_END | ||
747 | }; | ||
748 | |||
749 | handle->response_code = 0; | ||
750 | handle->timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, | ||
751 | 60); | ||
752 | handle->proc_cls = proc_cls; | ||
753 | handle->proc = proc; | ||
754 | handle->rest_handle = rest_handle; | ||
755 | |||
756 | handle->url = GNUNET_strdup (rest_handle->url); | ||
757 | if (handle->url[strlen (handle->url) - 1] == '/') | ||
758 | handle->url[strlen (handle->url) - 1] = '\0'; | ||
759 | handle->timeout_task = | ||
760 | GNUNET_SCHEDULER_add_delayed (handle->timeout, | ||
761 | &do_error, | ||
762 | handle); | ||
763 | GNUNET_CONTAINER_DLL_insert (requests_head, | ||
764 | requests_tail, | ||
765 | handle); | ||
766 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); | ||
767 | if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle, | ||
768 | handlers, | ||
769 | &err, | ||
770 | handle)) | ||
771 | { | ||
772 | cleanup_handle (handle); | ||
773 | return GNUNET_NO; | ||
774 | } | ||
775 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); | ||
776 | return GNUNET_YES; | ||
777 | } | ||
778 | |||
779 | |||
780 | /** | ||
781 | * Entry point for the plugin. | ||
782 | * | ||
783 | * @param cls Config info | ||
784 | * @return NULL on error, otherwise the plugin context | ||
785 | */ | ||
786 | void * | ||
787 | libgnunet_plugin_rest_peerinfo_init (void *cls) | ||
788 | { | ||
789 | static struct Plugin plugin; | ||
790 | struct GNUNET_REST_Plugin *api; | ||
791 | |||
792 | cfg = cls; | ||
793 | if (NULL != plugin.cfg) | ||
794 | return NULL; /* can only initialize once! */ | ||
795 | memset (&plugin, 0, sizeof(struct Plugin)); | ||
796 | plugin.cfg = cfg; | ||
797 | api = GNUNET_new (struct GNUNET_REST_Plugin); | ||
798 | api->cls = &plugin; | ||
799 | api->name = GNUNET_REST_API_NS_PEERINFO; | ||
800 | api->process_request = &rest_process_request; | ||
801 | GNUNET_asprintf (&allow_methods, | ||
802 | "%s, %s, %s, %s, %s", | ||
803 | MHD_HTTP_METHOD_GET, | ||
804 | MHD_HTTP_METHOD_POST, | ||
805 | MHD_HTTP_METHOD_PUT, | ||
806 | MHD_HTTP_METHOD_DELETE, | ||
807 | MHD_HTTP_METHOD_OPTIONS); | ||
808 | peerinfo_handle = GNUNET_PEERINFO_connect (cfg); | ||
809 | |||
810 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
811 | _ ("Peerinfo REST API initialized\n")); | ||
812 | return api; | ||
813 | } | ||
814 | |||
815 | |||
816 | /** | ||
817 | * Exit point from the plugin. | ||
818 | * | ||
819 | * @param cls the plugin context (as returned by "init") | ||
820 | * @return always NULL | ||
821 | */ | ||
822 | void * | ||
823 | libgnunet_plugin_rest_peerinfo_done (void *cls) | ||
824 | { | ||
825 | struct GNUNET_REST_Plugin *api = cls; | ||
826 | struct Plugin *plugin = api->cls; | ||
827 | |||
828 | plugin->cfg = NULL; | ||
829 | while (NULL != requests_head) | ||
830 | cleanup_handle (requests_head); | ||
831 | if (NULL != peerinfo_handle) | ||
832 | GNUNET_PEERINFO_disconnect (peerinfo_handle); | ||
833 | |||
834 | GNUNET_free (allow_methods); | ||
835 | GNUNET_free (api); | ||
836 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
837 | "Peerinfo REST plugin is finished\n"); | ||
838 | return NULL; | ||
839 | } | ||
840 | |||
841 | |||
842 | /* end of plugin_rest_peerinfo.c */ | ||
diff --git a/src/peerinfo-tool/test_gnunet_peerinfo.py.in b/src/peerinfo-tool/test_gnunet_peerinfo.py.in deleted file mode 100755 index 709556f70..000000000 --- a/src/peerinfo-tool/test_gnunet_peerinfo.py.in +++ /dev/null | |||
@@ -1,143 +0,0 @@ | |||
1 | #!@PYTHONEXE@ | ||
2 | # This file is part of GNUnet. | ||
3 | # (C) 2010, 2018 Christian Grothoff (and other contributing authors) | ||
4 | # | ||
5 | # GNUnet is free software: you can redistribute it and/or modify it | ||
6 | # under the terms of the GNU Affero General Public License as published | ||
7 | # by the Free Software Foundation, either version 3 of the License, | ||
8 | # or (at your 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 | # Affero General Public License for more details. | ||
14 | # | ||
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/>. | ||
17 | # | ||
18 | # SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | # | ||
20 | # Testcase for gnunet-peerinfo | ||
21 | import sys | ||
22 | import os | ||
23 | import subprocess | ||
24 | import re | ||
25 | import shutil | ||
26 | import time | ||
27 | |||
28 | srcdir = "../.." | ||
29 | gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts") | ||
30 | if gnunet_pyexpect_dir not in sys.path: | ||
31 | sys.path.append(gnunet_pyexpect_dir) | ||
32 | |||
33 | from gnunet_pyexpect import pexpect | ||
34 | |||
35 | #save LANG and set it to C | ||
36 | mylang = os.environ.get('LANG') | ||
37 | os.environ['LANG'] = 'C' | ||
38 | |||
39 | if os.name == 'posix': | ||
40 | peerinfo = './gnunet-peerinfo' | ||
41 | gnunetarm = 'gnunet-arm' | ||
42 | gnunettesting = 'gnunet-testing' | ||
43 | elif os.name == 'nt': | ||
44 | peerinfo = './gnunet-peerinfo.exe' | ||
45 | gnunetarm = 'gnunet-arm.exe' | ||
46 | gnunettesting = 'gnunet-testing.exe' | ||
47 | |||
48 | pinfo = pexpect() | ||
49 | |||
50 | if os.name == "nt": | ||
51 | shutil.rmtree(os.path.join(os.getenv("TEMP"), "gnunet-test-peerinfo"), True) | ||
52 | else: | ||
53 | shutil.rmtree("/tmp/gnunet-test-peerinfo", True) | ||
54 | |||
55 | # create hostkey via testing lib # FIXME: The /tmp/ location needs to be adjusted to the TMP variable! | ||
56 | hkk = subprocess.Popen([ | ||
57 | gnunettesting, '-n', '1', '-c', 'test_gnunet_peerinfo_data.conf', '-k', | ||
58 | '/tmp/gnunet-test-peerinfo/.hostkey' | ||
59 | ]) | ||
60 | hkk.communicate() | ||
61 | |||
62 | arm = subprocess.Popen([ | ||
63 | gnunetarm, '-sq', '-c', 'test_gnunet_peerinfo_data.conf' | ||
64 | ]) | ||
65 | arm.communicate() | ||
66 | |||
67 | try: | ||
68 | pinfo.spawn( | ||
69 | None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-s'], | ||
70 | stdout=subprocess.PIPE, | ||
71 | stderr=subprocess.STDOUT | ||
72 | ) | ||
73 | pinfo.expect("stdout", re.compile(r'I am peer `.*\'.\r?\n')) | ||
74 | |||
75 | pinfo.spawn( | ||
76 | None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], | ||
77 | stdout=subprocess.PIPE, | ||
78 | stderr=subprocess.STDOUT | ||
79 | ) | ||
80 | pinfo.expect( | ||
81 | "stdout", | ||
82 | re. | ||
83 | compile(r'....................................................\r?\n') | ||
84 | ) | ||
85 | |||
86 | pinfo.spawn( | ||
87 | None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', 'invalid'], | ||
88 | stdout=subprocess.PIPE, | ||
89 | stderr=subprocess.STDOUT | ||
90 | ) | ||
91 | pinfo.expect( | ||
92 | "stdout", re.compile(r'Invalid command line argument `invalid\'\r?\n') | ||
93 | ) | ||
94 | |||
95 | arm = subprocess.Popen([ | ||
96 | gnunetarm, '-q', '-i', 'transport', '-c', | ||
97 | 'test_gnunet_peerinfo_data.conf' | ||
98 | ]) | ||
99 | arm.communicate() | ||
100 | time.sleep(1) | ||
101 | |||
102 | pinfo.spawn( | ||
103 | None, [peerinfo, '-i', '-c', 'test_gnunet_peerinfo_data.conf'], | ||
104 | stdout=subprocess.PIPE, | ||
105 | stderr=subprocess.STDOUT | ||
106 | ) | ||
107 | pinfo.expect("stdout", re.compile("Peer `.*'\r?\n")) | ||
108 | m = pinfo.expect("stdout", re.compile("\s.*:24357\r?\n")) | ||
109 | while len(m.group(0)) > 0: | ||
110 | m = pinfo.expect("stdout", re.compile("(\s.*:24357\r?\n|\r?\n|)")) | ||
111 | |||
112 | pinfo.spawn( | ||
113 | None, [peerinfo, '-i', '-c', 'test_gnunet_peerinfo_data.conf', '-n'], | ||
114 | stdout=subprocess.PIPE, | ||
115 | stderr=subprocess.STDOUT | ||
116 | ) | ||
117 | pinfo.expect("stdout", re.compile("Peer `.*'\r?\n")) | ||
118 | m = pinfo.expect("stdout", re.compile("\s.*:24357\r?\n")) | ||
119 | while len(m.group(0)) > 0: | ||
120 | m = pinfo.expect("stdout", re.compile("(\s.*:24357\r?\n|\r?\n|)")) | ||
121 | |||
122 | pinfo.spawn( | ||
123 | None, [peerinfo, '-c', 'test_gnunet_peerinfo_data.conf', '-qs'], | ||
124 | stdout=subprocess.PIPE, | ||
125 | stderr=subprocess.STDOUT | ||
126 | ) | ||
127 | pid = pinfo.read("stdout") | ||
128 | pid.strip() | ||
129 | |||
130 | finally: | ||
131 | arm = subprocess.Popen([ | ||
132 | gnunetarm, '-eq', '-c', 'test_gnunet_peerinfo_data.conf' | ||
133 | ]) | ||
134 | arm.communicate() | ||
135 | if os.name == "nt": | ||
136 | shutil.rmtree( | ||
137 | os.path.join(os.getenv("TEMP"), "gnunet-test-peerinfo"), True | ||
138 | ) | ||
139 | else: | ||
140 | shutil.rmtree("/tmp/gnunet-test-peerinfo", True) | ||
141 | #Reset LANG | ||
142 | if type(mylang) == str: | ||
143 | os.environ['LANG'] = mylang | ||
diff --git a/src/peerinfo-tool/test_gnunet_peerinfo_data.conf b/src/peerinfo-tool/test_gnunet_peerinfo_data.conf deleted file mode 100644 index d18d3cf96..000000000 --- a/src/peerinfo-tool/test_gnunet_peerinfo_data.conf +++ /dev/null | |||
@@ -1,13 +0,0 @@ | |||
1 | @INLINE@ ../../contrib/conf/gnunet/no_forcestart.conf | ||
2 | |||
3 | [PATHS] | ||
4 | GNUNET_TEST_HOME = $GNUNET_TMP/gnunet-test-peerinfo/ | ||
5 | |||
6 | [transport] | ||
7 | plugins = tcp | ||
8 | |||
9 | [nat] | ||
10 | RETURN_LOCAL_ADDRESSES = YES | ||
11 | |||
12 | [transport-tcp] | ||
13 | PORT = 24357 | ||