summaryrefslogtreecommitdiff
path: root/src/hostlist/gnunet-daemon-hostlist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hostlist/gnunet-daemon-hostlist.c')
-rw-r--r--src/hostlist/gnunet-daemon-hostlist.c282
1 files changed, 141 insertions, 141 deletions
diff --git a/src/hostlist/gnunet-daemon-hostlist.c b/src/hostlist/gnunet-daemon-hostlist.c
index 760d5f543..7c1863dc0 100644
--- a/src/hostlist/gnunet-daemon-hostlist.c
+++ b/src/hostlist/gnunet-daemon-hostlist.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 hostlist/gnunet-daemon-hostlist.c 22 * @file hostlist/gnunet-daemon-hostlist.c
@@ -102,8 +102,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
102 * unaligned!) 102 * unaligned!)
103 * 4) address (address-length bytes; possibly unaligned!) 103 * 4) address (address-length bytes; possibly unaligned!)
104 */ 104 */
105struct GNUNET_HOSTLIST_ADV_Message 105struct GNUNET_HOSTLIST_ADV_Message {
106{
107 /** 106 /**
108 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT. 107 * Type will be GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT.
109 */ 108 */
@@ -130,8 +129,8 @@ static struct GNUNET_PeerIdentity me;
130 * @param my_identity our peer's identity 129 * @param my_identity our peer's identity
131 */ 130 */
132static void 131static void
133core_init (void *cls, 132core_init(void *cls,
134 const struct GNUNET_PeerIdentity *my_identity) 133 const struct GNUNET_PeerIdentity *my_identity)
135{ 134{
136 me = *my_identity; 135 me = *my_identity;
137} 136}
@@ -145,26 +144,26 @@ core_init (void *cls,
145 * @return #GNUNET_OK if message is well-formed 144 * @return #GNUNET_OK if message is well-formed
146 */ 145 */
147static int 146static int
148check_advertisement (void *cls, 147check_advertisement(void *cls,
149 const struct GNUNET_MessageHeader *message) 148 const struct GNUNET_MessageHeader *message)
150{ 149{
151 size_t size; 150 size_t size;
152 size_t uri_size; 151 size_t uri_size;
153 const char *uri; 152 const char *uri;
154 153
155 size = ntohs (message->size); 154 size = ntohs(message->size);
156 if (size <= sizeof (struct GNUNET_MessageHeader)) 155 if (size <= sizeof(struct GNUNET_MessageHeader))
157 { 156 {
158 GNUNET_break_op (0); 157 GNUNET_break_op(0);
159 return GNUNET_SYSERR; 158 return GNUNET_SYSERR;
160 } 159 }
161 uri = (const char *) &message[1]; 160 uri = (const char *)&message[1];
162 uri_size = size - sizeof (struct GNUNET_MessageHeader); 161 uri_size = size - sizeof(struct GNUNET_MessageHeader);
163 if (uri[uri_size - 1] != '\0') 162 if (uri[uri_size - 1] != '\0')
164 { 163 {
165 GNUNET_break_op (0); 164 GNUNET_break_op(0);
166 return GNUNET_SYSERR; 165 return GNUNET_SYSERR;
167 } 166 }
168 return GNUNET_OK; 167 return GNUNET_OK;
169} 168}
170 169
@@ -177,13 +176,13 @@ check_advertisement (void *cls,
177 * @return #GNUNET_OK on success 176 * @return #GNUNET_OK on success
178 */ 177 */
179static void 178static void
180handle_advertisement (void *cls, 179handle_advertisement(void *cls,
181 const struct GNUNET_MessageHeader *message) 180 const struct GNUNET_MessageHeader *message)
182{ 181{
183 const char *uri = (const char *) &message[1]; 182 const char *uri = (const char *)&message[1];
184 183
185 GNUNET_assert (NULL != client_adv_handler); 184 GNUNET_assert(NULL != client_adv_handler);
186 (void) (*client_adv_handler) (uri); 185 (void)(*client_adv_handler)(uri);
187} 186}
188 187
189 188
@@ -197,28 +196,28 @@ handle_advertisement (void *cls,
197 * @return peer 196 * @return peer
198 */ 197 */
199static void * 198static void *
200connect_handler (void *cls, 199connect_handler(void *cls,
201 const struct GNUNET_PeerIdentity *peer, 200 const struct GNUNET_PeerIdentity *peer,
202 struct GNUNET_MQ_Handle *mq) 201 struct GNUNET_MQ_Handle *mq)
203{ 202{
204 if (0 == GNUNET_memcmp (&me, 203 if (0 == GNUNET_memcmp(&me,
205 peer)) 204 peer))
206 return NULL; 205 return NULL;
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
208 "A new peer connected, notifying client and server\n"); 207 "A new peer connected, notifying client and server\n");
209 if (NULL != client_ch) 208 if (NULL != client_ch)
210 GNUNET_assert (NULL == 209 GNUNET_assert(NULL ==
211 (*client_ch) (cls, 210 (*client_ch)(cls,
212 peer, 211 peer,
213 mq)); 212 mq));
214#if HAVE_MHD 213#if HAVE_MHD
215 if (NULL != server_ch) 214 if (NULL != server_ch)
216 GNUNET_assert (NULL == 215 GNUNET_assert(NULL ==
217 (*server_ch) (cls, 216 (*server_ch)(cls,
218 peer, 217 peer,
219 mq)); 218 mq));
220#endif 219#endif
221 return (void *) peer; 220 return (void *)peer;
222} 221}
223 222
224 223
@@ -230,18 +229,18 @@ connect_handler (void *cls,
230 * @param peer peer identity this notification is about 229 * @param peer peer identity this notification is about
231 */ 230 */
232static void 231static void
233disconnect_handler (void *cls, 232disconnect_handler(void *cls,
234 const struct GNUNET_PeerIdentity *peer, 233 const struct GNUNET_PeerIdentity *peer,
235 void *internal_cls) 234 void *internal_cls)
236{ 235{
237 if (0 == GNUNET_memcmp (&me, 236 if (0 == GNUNET_memcmp(&me,
238 peer)) 237 peer))
239 return; 238 return;
240 /* call hostlist client disconnect handler */ 239 /* call hostlist client disconnect handler */
241 if (NULL != client_dh) 240 if (NULL != client_dh)
242 (*client_dh) (cls, 241 (*client_dh)(cls,
243 peer, 242 peer,
244 NULL); 243 NULL);
245} 244}
246 245
247 246
@@ -252,31 +251,31 @@ disconnect_handler (void *cls,
252 * @param cls NULL 251 * @param cls NULL
253 */ 252 */
254static void 253static void
255cleaning_task (void *cls) 254cleaning_task(void *cls)
256{ 255{
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 256 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
258 "Hostlist daemon is shutting down\n"); 257 "Hostlist daemon is shutting down\n");
259 if (NULL != core) 258 if (NULL != core)
260 { 259 {
261 GNUNET_CORE_disconnect (core); 260 GNUNET_CORE_disconnect(core);
262 core = NULL; 261 core = NULL;
263 } 262 }
264 if (bootstrapping) 263 if (bootstrapping)
265 { 264 {
266 GNUNET_HOSTLIST_client_stop (); 265 GNUNET_HOSTLIST_client_stop();
267 } 266 }
268#if HAVE_MHD 267#if HAVE_MHD
269 if (provide_hostlist) 268 if (provide_hostlist)
270 { 269 {
271 GNUNET_HOSTLIST_server_stop (); 270 GNUNET_HOSTLIST_server_stop();
272 } 271 }
273#endif 272#endif
274 if (NULL != stats) 273 if (NULL != stats)
275 { 274 {
276 GNUNET_STATISTICS_destroy (stats, 275 GNUNET_STATISTICS_destroy(stats,
277 GNUNET_NO); 276 GNUNET_NO);
278 stats = NULL; 277 stats = NULL;
279 } 278 }
280} 279}
281 280
282 281
@@ -289,71 +288,72 @@ cleaning_task (void *cls)
289 * @param cfg configuration 288 * @param cfg configuration
290 */ 289 */
291static void 290static void
292run (void *cls, 291run(void *cls,
293 char *const *args, 292 char *const *args,
294 const char *cfgfile, 293 const char *cfgfile,
295 const struct GNUNET_CONFIGURATION_Handle *cfg) 294 const struct GNUNET_CONFIGURATION_Handle *cfg)
296{ 295{
297 struct GNUNET_MQ_MessageHandler learn_handlers[] = { 296 struct GNUNET_MQ_MessageHandler learn_handlers[] = {
298 GNUNET_MQ_hd_var_size (advertisement, 297 GNUNET_MQ_hd_var_size(advertisement,
299 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 298 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT,
300 struct GNUNET_MessageHeader, 299 struct GNUNET_MessageHeader,
301 NULL), 300 NULL),
302 GNUNET_MQ_handler_end () 301 GNUNET_MQ_handler_end()
303 }; 302 };
304 struct GNUNET_MQ_MessageHandler no_learn_handlers[] = { 303 struct GNUNET_MQ_MessageHandler no_learn_handlers[] = {
305 GNUNET_MQ_handler_end () 304 GNUNET_MQ_handler_end()
306 }; 305 };
307 if ((! bootstrapping) && (! learning) 306
307 if ((!bootstrapping) && (!learning)
308#if HAVE_MHD 308#if HAVE_MHD
309 && (! provide_hostlist) 309 && (!provide_hostlist)
310#endif 310#endif
311 ) 311 )
312 { 312 {
313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 313 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
314 _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n")); 314 _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
315 return; 315 return;
316 } 316 }
317 stats = GNUNET_STATISTICS_create ("hostlist", cfg); 317 stats = GNUNET_STATISTICS_create("hostlist", cfg);
318 if (NULL == stats) 318 if (NULL == stats)
319 { 319 {
320 GNUNET_break (0); 320 GNUNET_break(0);
321 return; 321 return;
322 } 322 }
323 if (bootstrapping) 323 if (bootstrapping)
324 GNUNET_HOSTLIST_client_start (cfg, 324 GNUNET_HOSTLIST_client_start(cfg,
325 stats, 325 stats,
326 &client_ch, 326 &client_ch,
327 &client_dh, 327 &client_dh,
328 &client_adv_handler, 328 &client_adv_handler,
329 learning); 329 learning);
330 core = 330 core =
331 GNUNET_CORE_connect (cfg, 331 GNUNET_CORE_connect(cfg,
332 NULL, 332 NULL,
333 &core_init, 333 &core_init,
334 &connect_handler, 334 &connect_handler,
335 &disconnect_handler, 335 &disconnect_handler,
336 learning ? learn_handlers : no_learn_handlers); 336 learning ? learn_handlers : no_learn_handlers);
337 337
338 338
339#if HAVE_MHD 339#if HAVE_MHD
340 if (provide_hostlist) 340 if (provide_hostlist)
341 GNUNET_HOSTLIST_server_start (cfg, 341 GNUNET_HOSTLIST_server_start(cfg,
342 stats, 342 stats,
343 core, 343 core,
344 &server_ch, 344 &server_ch,
345 advertising); 345 advertising);
346#endif 346#endif
347 GNUNET_SCHEDULER_add_shutdown (&cleaning_task, 347 GNUNET_SCHEDULER_add_shutdown(&cleaning_task,
348 NULL); 348 NULL);
349 349
350 if (NULL == core) 350 if (NULL == core)
351 { 351 {
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 352 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
353 _("Failed to connect to `%s' service.\n"), "core"); 353 _("Failed to connect to `%s' service.\n"), "core");
354 GNUNET_SCHEDULER_shutdown (); 354 GNUNET_SCHEDULER_shutdown();
355 return; 355 return;
356 } 356 }
357} 357}
358 358
359 359
@@ -365,46 +365,46 @@ run (void *cls,
365 * @return 0 ok, 1 on error 365 * @return 0 ok, 1 on error
366 */ 366 */
367int 367int
368main (int argc, char *const *argv) 368main(int argc, char *const *argv)
369{ 369{
370 struct GNUNET_GETOPT_CommandLineOption options[] = { 370 struct GNUNET_GETOPT_CommandLineOption options[] = {
371#if HAVE_MHD 371#if HAVE_MHD
372 GNUNET_GETOPT_option_flag ('a', 372 GNUNET_GETOPT_option_flag('a',
373 "advertise", 373 "advertise",
374 gettext_noop ("advertise our hostlist to other peers"), 374 gettext_noop("advertise our hostlist to other peers"),
375 &advertising), 375 &advertising),
376#endif 376#endif
377 GNUNET_GETOPT_option_flag ('b', 377 GNUNET_GETOPT_option_flag('b',
378 "bootstrap", 378 "bootstrap",
379 gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"), 379 gettext_noop("bootstrap using hostlists (it is highly recommended that you always use this option)"),
380 &bootstrapping), 380 &bootstrapping),
381 GNUNET_GETOPT_option_flag ('e', 381 GNUNET_GETOPT_option_flag('e',
382 "enable-learning", 382 "enable-learning",
383 gettext_noop ("enable learning about hostlist servers from other peers"), 383 gettext_noop("enable learning about hostlist servers from other peers"),
384 &learning), 384 &learning),
385#if HAVE_MHD 385#if HAVE_MHD
386 GNUNET_GETOPT_option_flag ('p', 386 GNUNET_GETOPT_option_flag('p',
387 "provide-hostlist", 387 "provide-hostlist",
388 gettext_noop ("provide a hostlist server"), 388 gettext_noop("provide a hostlist server"),
389 &provide_hostlist), 389 &provide_hostlist),
390#endif 390#endif
391 GNUNET_GETOPT_OPTION_END 391 GNUNET_GETOPT_OPTION_END
392 }; 392 };
393 393
394 int ret; 394 int ret;
395 395
396 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 396 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
397 return 2; 397 return 2;
398 398
399 GNUNET_log_setup ("hostlist", "WARNING", NULL); 399 GNUNET_log_setup("hostlist", "WARNING", NULL);
400 ret = 400 ret =
401 (GNUNET_OK == 401 (GNUNET_OK ==
402 GNUNET_PROGRAM_run (argc, argv, 402 GNUNET_PROGRAM_run(argc, argv,
403 "hostlist", 403 "hostlist",
404 _("GNUnet hostlist server and client"), 404 _("GNUnet hostlist server and client"),
405 options, 405 options,
406 &run, NULL)) ? 0 : 1; 406 &run, NULL)) ? 0 : 1;
407 GNUNET_free ((void*) argv); 407 GNUNET_free((void*)argv);
408 return ret; 408 return ret;
409} 409}
410 410