diff options
Diffstat (limited to 'src/cli/dht/gnunet-dht-hello.c')
-rw-r--r-- | src/cli/dht/gnunet-dht-hello.c | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/src/cli/dht/gnunet-dht-hello.c b/src/cli/dht/gnunet-dht-hello.c new file mode 100644 index 000000000..369ed5643 --- /dev/null +++ b/src/cli/dht/gnunet-dht-hello.c | |||
@@ -0,0 +1,178 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2022 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 | * @file dht/gnunet-dht-hello.c | ||
22 | * @brief Obtain HELLO from DHT for bootstrapping | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_dht_service.h" | ||
27 | |||
28 | #define LOG(kind, ...) GNUNET_log_from (kind, "dht-clients", __VA_ARGS__) | ||
29 | |||
30 | /** | ||
31 | * Handle to the DHT | ||
32 | */ | ||
33 | static struct GNUNET_DHT_Handle *dht_handle; | ||
34 | |||
35 | /** | ||
36 | * Handle to the DHT hello get operation. | ||
37 | */ | ||
38 | static struct GNUNET_DHT_HelloGetHandle *get_hello_handle; | ||
39 | |||
40 | /** | ||
41 | * Global status value | ||
42 | */ | ||
43 | static int global_ret; | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Task run to clean up on shutdown. | ||
48 | * | ||
49 | * @param cls unused | ||
50 | */ | ||
51 | static void | ||
52 | cleanup_task (void *cls) | ||
53 | { | ||
54 | if (NULL != get_hello_handle) | ||
55 | { | ||
56 | GNUNET_DHT_hello_get_cancel (get_hello_handle); | ||
57 | get_hello_handle = NULL; | ||
58 | } | ||
59 | if (NULL != dht_handle) | ||
60 | { | ||
61 | GNUNET_DHT_disconnect (dht_handle); | ||
62 | dht_handle = NULL; | ||
63 | } | ||
64 | } | ||
65 | |||
66 | |||
67 | /** | ||
68 | * Task run when we are finished. Triggers shutdown. | ||
69 | * | ||
70 | * @param cls unused | ||
71 | */ | ||
72 | static void | ||
73 | hello_done_cb (void *cls) | ||
74 | { | ||
75 | GNUNET_SCHEDULER_shutdown (); | ||
76 | } | ||
77 | |||
78 | |||
79 | /** | ||
80 | * Function called on our HELLO. | ||
81 | * | ||
82 | * @param cls closure | ||
83 | * @param url the HELLO URL | ||
84 | */ | ||
85 | static void | ||
86 | hello_result_cb (void *cls, | ||
87 | const char *url) | ||
88 | { | ||
89 | get_hello_handle = NULL; | ||
90 | fprintf (stdout, | ||
91 | "%s\n", | ||
92 | url); | ||
93 | GNUNET_SCHEDULER_shutdown (); | ||
94 | } | ||
95 | |||
96 | |||
97 | /** | ||
98 | * Main function that will be run by the scheduler. | ||
99 | * | ||
100 | * @param cls closure, NULL | ||
101 | * @param args remaining command-line arguments | ||
102 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
103 | * @param cfg configuration | ||
104 | */ | ||
105 | static void | ||
106 | run (void *cls, | ||
107 | char *const *args, | ||
108 | const char *cfgfile, | ||
109 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
110 | { | ||
111 | (void) cls; | ||
112 | (void) cfgfile; | ||
113 | GNUNET_SCHEDULER_add_shutdown (&cleanup_task, | ||
114 | NULL); | ||
115 | if (NULL == (dht_handle = GNUNET_DHT_connect (cfg, | ||
116 | 1))) | ||
117 | { | ||
118 | fprintf (stderr, | ||
119 | _ ("Failed to connect to DHT service!\n")); | ||
120 | global_ret = EXIT_NOTCONFIGURED; | ||
121 | GNUNET_SCHEDULER_shutdown (); | ||
122 | return; | ||
123 | } | ||
124 | if (NULL == args[0]) | ||
125 | { | ||
126 | get_hello_handle = GNUNET_DHT_hello_get (dht_handle, | ||
127 | &hello_result_cb, | ||
128 | NULL); | ||
129 | GNUNET_break (NULL != get_hello_handle); | ||
130 | } | ||
131 | else | ||
132 | { | ||
133 | GNUNET_DHT_hello_offer (dht_handle, | ||
134 | args[0], | ||
135 | &hello_done_cb, | ||
136 | NULL); | ||
137 | } | ||
138 | } | ||
139 | |||
140 | |||
141 | /** | ||
142 | * Entry point for gnunet-dht-hello | ||
143 | * | ||
144 | * @param argc number of arguments from the command line | ||
145 | * @param argv command line arguments | ||
146 | * @return 0 ok, 1 on error | ||
147 | */ | ||
148 | int | ||
149 | main (int argc, | ||
150 | char *const *argv) | ||
151 | { | ||
152 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
153 | GNUNET_GETOPT_OPTION_END | ||
154 | }; | ||
155 | enum GNUNET_GenericReturnValue iret; | ||
156 | |||
157 | if (GNUNET_OK != | ||
158 | GNUNET_STRINGS_get_utf8_args (argc, argv, | ||
159 | &argc, &argv)) | ||
160 | return 2; | ||
161 | iret = GNUNET_PROGRAM_run ( | ||
162 | argc, | ||
163 | argv, | ||
164 | "gnunet-dht-hello [URL]", | ||
165 | gettext_noop ( | ||
166 | "Obtain HELLO from DHT or provide HELLO to DHT for bootstrapping"), | ||
167 | options, | ||
168 | &run, | ||
169 | NULL); | ||
170 | if (GNUNET_SYSERR == iret) | ||
171 | return EXIT_FAILURE; | ||
172 | if (GNUNET_NO == iret) | ||
173 | return EXIT_SUCCESS; | ||
174 | return global_ret; | ||
175 | } | ||
176 | |||
177 | |||
178 | /* end of gnunet-dht-hello.c */ | ||