aboutsummaryrefslogtreecommitdiff
path: root/src/hostlist/gnunet-daemon-hostlist_server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hostlist/gnunet-daemon-hostlist_server.c')
-rw-r--r--src/hostlist/gnunet-daemon-hostlist_server.c887
1 files changed, 443 insertions, 444 deletions
diff --git a/src/hostlist/gnunet-daemon-hostlist_server.c b/src/hostlist/gnunet-daemon-hostlist_server.c
index 8764586d6..b4e0df34e 100644
--- a/src/hostlist/gnunet-daemon-hostlist_server.c
+++ b/src/hostlist/gnunet-daemon-hostlist_server.c
@@ -16,7 +16,7 @@
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_server.c 22 * @file hostlist/gnunet-daemon-hostlist_server.c
@@ -39,7 +39,7 @@
39 * time out? 39 * time out?
40 */ 40 */
41#define GNUNET_ADV_TIMEOUT \ 41#define GNUNET_ADV_TIMEOUT \
42 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 42 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
43 43
44 44
45/** 45/**
@@ -106,8 +106,7 @@ static char *hostlist_uri;
106/** 106/**
107 * Context for #host_processor(). 107 * Context for #host_processor().
108 */ 108 */
109struct HostSet 109struct HostSet {
110{
111 /** 110 /**
112 * Iterator used to build @e data (NULL when done). 111 * Iterator used to build @e data (NULL when done).
113 */ 112 */
@@ -138,13 +137,13 @@ static struct HostSet *builder;
138 * @param response response to add headers to 137 * @param response response to add headers to
139 */ 138 */
140static void 139static void
141add_cors_headers (struct MHD_Response *response) 140add_cors_headers(struct MHD_Response *response)
142{ 141{
143 MHD_add_response_header (response, "Access-Control-Allow-Origin", "*"); 142 MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
144 MHD_add_response_header (response, 143 MHD_add_response_header(response,
145 "Access-Control-Allow-Methods", 144 "Access-Control-Allow-Methods",
146 "GET, OPTIONS"); 145 "GET, OPTIONS");
147 MHD_add_response_header (response, "Access-Control-Max-Age", "86400"); 146 MHD_add_response_header(response, "Access-Control-Max-Age", "86400");
148} 147}
149 148
150 149
@@ -152,27 +151,27 @@ add_cors_headers (struct MHD_Response *response)
152 * Function that assembles our response. 151 * Function that assembles our response.
153 */ 152 */
154static void 153static void
155finish_response () 154finish_response()
156{ 155{
157 if (NULL != response) 156 if (NULL != response)
158 MHD_destroy_response (response); 157 MHD_destroy_response(response);
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
160 "Creating hostlist response with %u bytes\n", 159 "Creating hostlist response with %u bytes\n",
161 (unsigned int) builder->size); 160 (unsigned int)builder->size);
162 response = MHD_create_response_from_buffer (builder->size, 161 response = MHD_create_response_from_buffer(builder->size,
163 builder->data, 162 builder->data,
164 MHD_RESPMEM_MUST_FREE); 163 MHD_RESPMEM_MUST_FREE);
165 add_cors_headers (response); 164 add_cors_headers(response);
166 if ((NULL == daemon_handle_v4) && (NULL == daemon_handle_v6)) 165 if ((NULL == daemon_handle_v4) && (NULL == daemon_handle_v6))
167 { 166 {
168 MHD_destroy_response (response); 167 MHD_destroy_response(response);
169 response = NULL; 168 response = NULL;
170 } 169 }
171 GNUNET_STATISTICS_set (stats, 170 GNUNET_STATISTICS_set(stats,
172 gettext_noop ("bytes in hostlist"), 171 gettext_noop("bytes in hostlist"),
173 builder->size, 172 builder->size,
174 GNUNET_YES); 173 GNUNET_YES);
175 GNUNET_free (builder); 174 GNUNET_free(builder);
176 builder = NULL; 175 builder = NULL;
177} 176}
178 177
@@ -186,20 +185,20 @@ finish_response ()
186 * @return #GNUNET_SYSERR to stop iterating (unless expiration has occured) 185 * @return #GNUNET_SYSERR to stop iterating (unless expiration has occured)
187 */ 186 */
188static int 187static int
189check_has_addr (void *cls, 188check_has_addr(void *cls,
190 const struct GNUNET_HELLO_Address *address, 189 const struct GNUNET_HELLO_Address *address,
191 struct GNUNET_TIME_Absolute expiration) 190 struct GNUNET_TIME_Absolute expiration)
192{ 191{
193 int *arg = cls; 192 int *arg = cls;
194 193
195 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us) 194 if (0 == GNUNET_TIME_absolute_get_remaining(expiration).rel_value_us)
196 { 195 {
197 GNUNET_STATISTICS_update (stats, 196 GNUNET_STATISTICS_update(stats,
198 gettext_noop ("expired addresses encountered"), 197 gettext_noop("expired addresses encountered"),
199 1, 198 1,
200 GNUNET_YES); 199 GNUNET_YES);
201 return GNUNET_YES; /* ignore this address */ 200 return GNUNET_YES; /* ignore this address */
202 } 201 }
203 *arg = GNUNET_YES; 202 *arg = GNUNET_YES;
204 return GNUNET_SYSERR; 203 return GNUNET_SYSERR;
205} 204}
@@ -215,73 +214,73 @@ check_has_addr (void *cls,
215 * @param err_msg message 214 * @param err_msg message
216 */ 215 */
217static void 216static void
218host_processor (void *cls, 217host_processor(void *cls,
219 const struct GNUNET_PeerIdentity *peer, 218 const struct GNUNET_PeerIdentity *peer,
220 const struct GNUNET_HELLO_Message *hello, 219 const struct GNUNET_HELLO_Message *hello,
221 const char *err_msg) 220 const char *err_msg)
222{ 221{
223 size_t old; 222 size_t old;
224 size_t s; 223 size_t s;
225 int has_addr; 224 int has_addr;
226 225
227 if (NULL != err_msg) 226 if (NULL != err_msg)
228 { 227 {
229 GNUNET_assert (NULL == peer); 228 GNUNET_assert(NULL == peer);
230 builder->pitr = NULL; 229 builder->pitr = NULL;
231 GNUNET_free_non_null (builder->data); 230 GNUNET_free_non_null(builder->data);
232 GNUNET_free (builder); 231 GNUNET_free(builder);
233 builder = NULL; 232 builder = NULL;
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 233 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
235 _ ("Error in communication with PEERINFO service: %s\n"), 234 _("Error in communication with PEERINFO service: %s\n"),
236 err_msg); 235 err_msg);
237 return; 236 return;
238 } 237 }
239 if (NULL == peer) 238 if (NULL == peer)
240 { 239 {
241 builder->pitr = NULL; 240 builder->pitr = NULL;
242 finish_response (); 241 finish_response();
243 return; 242 return;
244 } 243 }
245 if (NULL == hello) 244 if (NULL == hello)
246 return; 245 return;
247 has_addr = GNUNET_NO; 246 has_addr = GNUNET_NO;
248 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_has_addr, &has_addr); 247 GNUNET_HELLO_iterate_addresses(hello, GNUNET_NO, &check_has_addr, &has_addr);
249 if (GNUNET_NO == has_addr) 248 if (GNUNET_NO == has_addr)
250 { 249 {
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 250 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
252 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n", 251 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
253 GNUNET_i2s (peer)); 252 GNUNET_i2s(peer));
254 GNUNET_STATISTICS_update (stats, 253 GNUNET_STATISTICS_update(stats,
255 gettext_noop ( 254 gettext_noop(
256 "HELLOs without addresses encountered (ignored)"), 255 "HELLOs without addresses encountered (ignored)"),
257 1, 256 1,
258 GNUNET_NO); 257 GNUNET_NO);
259 return; 258 return;
260 } 259 }
261 old = builder->size; 260 old = builder->size;
262 s = GNUNET_HELLO_size (hello); 261 s = GNUNET_HELLO_size(hello);
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 262 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
264 "Received %u bytes of `%s' from peer `%s' for hostlist.\n", 263 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
265 (unsigned int) s, 264 (unsigned int)s,
266 "HELLO", 265 "HELLO",
267 GNUNET_i2s (peer)); 266 GNUNET_i2s(peer));
268 if ((old + s >= GNUNET_MAX_MALLOC_CHECKED) || 267 if ((old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
269 (old + s >= MAX_BYTES_PER_HOSTLISTS)) 268 (old + s >= MAX_BYTES_PER_HOSTLISTS))
270 { 269 {
271 /* too large, skip! */ 270 /* too large, skip! */
272 GNUNET_STATISTICS_update (stats, 271 GNUNET_STATISTICS_update(stats,
273 gettext_noop ( 272 gettext_noop(
274 "bytes not included in hostlist (size limit)"), 273 "bytes not included in hostlist (size limit)"),
275 s, 274 s,
276 GNUNET_NO); 275 GNUNET_NO);
277 return; 276 return;
278 } 277 }
279 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 278 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
280 "Adding peer `%s' to hostlist (%u bytes)\n", 279 "Adding peer `%s' to hostlist (%u bytes)\n",
281 GNUNET_i2s (peer), 280 GNUNET_i2s(peer),
282 (unsigned int) s); 281 (unsigned int)s);
283 GNUNET_array_grow (builder->data, builder->size, old + s); 282 GNUNET_array_grow(builder->data, builder->size, old + s);
284 GNUNET_memcpy (&builder->data[old], hello, s); 283 GNUNET_memcpy(&builder->data[old], hello, s);
285} 284}
286 285
287 286
@@ -295,17 +294,17 @@ host_processor (void *cls,
295 * @return #MHD_YES if connection is allowed, #MHD_NO if not (we are not ready) 294 * @return #MHD_YES if connection is allowed, #MHD_NO if not (we are not ready)
296 */ 295 */
297static int 296static int
298accept_policy_callback (void *cls, 297accept_policy_callback(void *cls,
299 const struct sockaddr *addr, 298 const struct sockaddr *addr,
300 socklen_t addrlen) 299 socklen_t addrlen)
301{ 300{
302 if (NULL == response) 301 if (NULL == response)
303 { 302 {
304 GNUNET_log ( 303 GNUNET_log(
305 GNUNET_ERROR_TYPE_DEBUG, 304 GNUNET_ERROR_TYPE_DEBUG,
306 "Received request for hostlist, but I am not yet ready; rejecting!\n"); 305 "Received request for hostlist, but I am not yet ready; rejecting!\n");
307 return MHD_NO; 306 return MHD_NO;
308 } 307 }
309 return MHD_YES; /* accept all */ 308 return MHD_YES; /* accept all */
310} 309}
311 310
@@ -346,80 +345,80 @@ accept_policy_callback (void *cls,
346 * error while handling the request 345 * error while handling the request
347 */ 346 */
348static int 347static int
349access_handler_callback (void *cls, 348access_handler_callback(void *cls,
350 struct MHD_Connection *connection, 349 struct MHD_Connection *connection,
351 const char *url, 350 const char *url,
352 const char *method, 351 const char *method,
353 const char *version, 352 const char *version,
354 const char *upload_data, 353 const char *upload_data,
355 size_t *upload_data_size, 354 size_t *upload_data_size,
356 void **con_cls) 355 void **con_cls)
357{ 356{
358 static int dummy; 357 static int dummy;
359 358
360 /* CORS pre-flight request */ 359 /* CORS pre-flight request */
361 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method)) 360 if (0 == strcmp(MHD_HTTP_METHOD_OPTIONS, method))
362 { 361 {
363 struct MHD_Response *options_response; 362 struct MHD_Response *options_response;
364 int rc; 363 int rc;
365 364
366 options_response = 365 options_response =
367 MHD_create_response_from_buffer (0, NULL, MHD_RESPMEM_PERSISTENT); 366 MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
368 add_cors_headers (options_response); 367 add_cors_headers(options_response);
369 rc = MHD_queue_response (connection, MHD_HTTP_OK, options_response); 368 rc = MHD_queue_response(connection, MHD_HTTP_OK, options_response);
370 MHD_destroy_response (options_response); 369 MHD_destroy_response(options_response);
371 return rc; 370 return rc;
372 } 371 }
373 if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) 372 if (0 != strcmp(method, MHD_HTTP_METHOD_GET))
374 { 373 {
375 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 374 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
376 _ ("Refusing `%s' request to hostlist server\n"), 375 _("Refusing `%s' request to hostlist server\n"),
377 method); 376 method);
378 GNUNET_STATISTICS_update (stats, 377 GNUNET_STATISTICS_update(stats,
379 gettext_noop ( 378 gettext_noop(
380 "hostlist requests refused (not HTTP GET)"), 379 "hostlist requests refused (not HTTP GET)"),
381 1, 380 1,
382 GNUNET_YES); 381 GNUNET_YES);
383 return MHD_NO; 382 return MHD_NO;
384 } 383 }
385 if (NULL == *con_cls) 384 if (NULL == *con_cls)
386 { 385 {
387 (*con_cls) = &dummy; 386 (*con_cls) = &dummy;
388 return MHD_YES; 387 return MHD_YES;
389 } 388 }
390 if (0 != *upload_data_size) 389 if (0 != *upload_data_size)
391 { 390 {
392 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 391 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
393 _ ("Refusing `%s' request with %llu bytes of upload data\n"), 392 _("Refusing `%s' request with %llu bytes of upload data\n"),
394 method, 393 method,
395 (unsigned long long) *upload_data_size); 394 (unsigned long long)*upload_data_size);
396 GNUNET_STATISTICS_update (stats, 395 GNUNET_STATISTICS_update(stats,
397 gettext_noop ( 396 gettext_noop(
398 "hostlist requests refused (upload data)"), 397 "hostlist requests refused (upload data)"),
399 1, 398 1,
400 GNUNET_YES); 399 GNUNET_YES);
401 return MHD_NO; /* do not support upload data */ 400 return MHD_NO; /* do not support upload data */
402 } 401 }
403 if (NULL == response) 402 if (NULL == response)
404 { 403 {
405 GNUNET_log ( 404 GNUNET_log(
406 GNUNET_ERROR_TYPE_WARNING, 405 GNUNET_ERROR_TYPE_WARNING,
407 _ ( 406 _(
408 "Could not handle hostlist request since I do not have a response yet\n")); 407 "Could not handle hostlist request since I do not have a response yet\n"));
409 GNUNET_STATISTICS_update (stats, 408 GNUNET_STATISTICS_update(stats,
410 gettext_noop ( 409 gettext_noop(
411 "hostlist requests refused (not ready)"), 410 "hostlist requests refused (not ready)"),
412 1, 411 1,
413 GNUNET_YES); 412 GNUNET_YES);
414 return MHD_NO; /* internal error, no response yet */ 413 return MHD_NO; /* internal error, no response yet */
415 } 414 }
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 415 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
417 _ ("Received request for our hostlist\n")); 416 _("Received request for our hostlist\n"));
418 GNUNET_STATISTICS_update (stats, 417 GNUNET_STATISTICS_update(stats,
419 gettext_noop ("hostlist requests processed"), 418 gettext_noop("hostlist requests processed"),
420 1, 419 1,
421 GNUNET_YES); 420 GNUNET_YES);
422 return MHD_queue_response (connection, MHD_HTTP_OK, response); 421 return MHD_queue_response(connection, MHD_HTTP_OK, response);
423} 422}
424 423
425 424
@@ -433,33 +432,33 @@ access_handler_callback (void *cls,
433 * @return number of bytes copied to @a buf 432 * @return number of bytes copied to @a buf
434 */ 433 */
435static void 434static void
436adv_transmit (struct GNUNET_MQ_Handle *mq) 435adv_transmit(struct GNUNET_MQ_Handle *mq)
437{ 436{
438 static uint64_t hostlist_adv_count; 437 static uint64_t hostlist_adv_count;
439 size_t uri_size; /* Including \0 termination! */ 438 size_t uri_size; /* Including \0 termination! */
440 struct GNUNET_MessageHeader *header; 439 struct GNUNET_MessageHeader *header;
441 struct GNUNET_MQ_Envelope *env; 440 struct GNUNET_MQ_Envelope *env;
442 441
443 uri_size = strlen (hostlist_uri) + 1; 442 uri_size = strlen(hostlist_uri) + 1;
444 env = GNUNET_MQ_msg_extra (header, 443 env = GNUNET_MQ_msg_extra(header,
445 uri_size, 444 uri_size,
446 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT); 445 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
447 GNUNET_memcpy (&header[1], hostlist_uri, uri_size); 446 GNUNET_memcpy(&header[1], hostlist_uri, uri_size);
448 GNUNET_MQ_env_set_options (env, 447 GNUNET_MQ_env_set_options(env,
449 GNUNET_MQ_PREF_CORK_ALLOWED | 448 GNUNET_MQ_PREF_CORK_ALLOWED |
450 GNUNET_MQ_PREF_UNRELIABLE); 449 GNUNET_MQ_PREF_UNRELIABLE);
451 GNUNET_MQ_send (mq, env); 450 GNUNET_MQ_send(mq, env);
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 451 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
453 "Sent advertisement message: Copied %u bytes into buffer!\n", 452 "Sent advertisement message: Copied %u bytes into buffer!\n",
454 (unsigned int) uri_size); 453 (unsigned int)uri_size);
455 hostlist_adv_count++; 454 hostlist_adv_count++;
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 455 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
457 " # Sent advertisement message: %llu\n", 456 " # Sent advertisement message: %llu\n",
458 (unsigned long long) hostlist_adv_count); 457 (unsigned long long)hostlist_adv_count);
459 GNUNET_STATISTICS_update (stats, 458 GNUNET_STATISTICS_update(stats,
460 gettext_noop ("# hostlist advertisements send"), 459 gettext_noop("# hostlist advertisements send"),
461 1, 460 1,
462 GNUNET_NO); 461 GNUNET_NO);
463} 462}
464 463
465 464
@@ -472,34 +471,34 @@ adv_transmit (struct GNUNET_MQ_Handle *mq)
472 * @return NULL (must!) 471 * @return NULL (must!)
473 */ 472 */
474static void * 473static void *
475connect_handler (void *cls, 474connect_handler(void *cls,
476 const struct GNUNET_PeerIdentity *peer, 475 const struct GNUNET_PeerIdentity *peer,
477 struct GNUNET_MQ_Handle *mq) 476 struct GNUNET_MQ_Handle *mq)
478{ 477{
479 size_t size; 478 size_t size;
480 479
481 if (! advertising) 480 if (!advertising)
482 return NULL; 481 return NULL;
483 if (NULL == hostlist_uri) 482 if (NULL == hostlist_uri)
484 return NULL; 483 return NULL;
485 size = strlen (hostlist_uri) + 1; 484 size = strlen(hostlist_uri) + 1;
486 if (size + sizeof (struct GNUNET_MessageHeader) >= GNUNET_MAX_MESSAGE_SIZE) 485 if (size + sizeof(struct GNUNET_MessageHeader) >= GNUNET_MAX_MESSAGE_SIZE)
487 { 486 {
488 GNUNET_break (0); 487 GNUNET_break(0);
489 return NULL; 488 return NULL;
490 } 489 }
491 size += sizeof (struct GNUNET_MessageHeader); 490 size += sizeof(struct GNUNET_MessageHeader);
492 if (NULL == core) 491 if (NULL == core)
493 { 492 {
494 GNUNET_break (0); 493 GNUNET_break(0);
495 return NULL; 494 return NULL;
496 } 495 }
497 GNUNET_log ( 496 GNUNET_log(
498 GNUNET_ERROR_TYPE_DEBUG, 497 GNUNET_ERROR_TYPE_DEBUG,
499 "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n", 498 "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n",
500 (unsigned int) size, 499 (unsigned int)size,
501 GNUNET_i2s (peer)); 500 GNUNET_i2s(peer));
502 adv_transmit (mq); 501 adv_transmit(mq);
503 return NULL; 502 return NULL;
504} 503}
505 504
@@ -514,36 +513,36 @@ connect_handler (void *cls,
514 * @param err_msg NULL if successful, otherwise contains error message 513 * @param err_msg NULL if successful, otherwise contains error message
515 */ 514 */
516static void 515static void
517process_notify (void *cls, 516process_notify(void *cls,
518 const struct GNUNET_PeerIdentity *peer, 517 const struct GNUNET_PeerIdentity *peer,
519 const struct GNUNET_HELLO_Message *hello, 518 const struct GNUNET_HELLO_Message *hello,
520 const char *err_msg) 519 const char *err_msg)
521{ 520{
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
523 "Peerinfo is notifying us to rebuild our hostlist\n"); 522 "Peerinfo is notifying us to rebuild our hostlist\n");
524 if (NULL != err_msg) 523 if (NULL != err_msg)
525 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 524 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
526 _ ("Error in communication with PEERINFO service: %s\n"), 525 _("Error in communication with PEERINFO service: %s\n"),
527 err_msg); 526 err_msg);
528 if (NULL != builder) 527 if (NULL != builder)
529 {
530 /* restart re-build already in progress ... */
531 if (NULL != builder->pitr)
532 { 528 {
533 GNUNET_PEERINFO_iterate_cancel (builder->pitr); 529 /* restart re-build already in progress ... */
534 builder->pitr = NULL; 530 if (NULL != builder->pitr)
531 {
532 GNUNET_PEERINFO_iterate_cancel(builder->pitr);
533 builder->pitr = NULL;
534 }
535 GNUNET_free_non_null(builder->data);
536 builder->size = 0;
537 builder->data = NULL;
535 } 538 }
536 GNUNET_free_non_null (builder->data);
537 builder->size = 0;
538 builder->data = NULL;
539 }
540 else 539 else
541 { 540 {
542 builder = GNUNET_new (struct HostSet); 541 builder = GNUNET_new(struct HostSet);
543 } 542 }
544 GNUNET_assert (NULL != peerinfo); 543 GNUNET_assert(NULL != peerinfo);
545 builder->pitr = 544 builder->pitr =
546 GNUNET_PEERINFO_iterate (peerinfo, GNUNET_NO, NULL, &host_processor, NULL); 545 GNUNET_PEERINFO_iterate(peerinfo, GNUNET_NO, NULL, &host_processor, NULL);
547} 546}
548 547
549 548
@@ -552,7 +551,7 @@ process_notify (void *cls,
552 * starts the task waiting for them. 551 * starts the task waiting for them.
553 */ 552 */
554static struct GNUNET_SCHEDULER_Task * 553static struct GNUNET_SCHEDULER_Task *
555prepare_daemon (struct MHD_Daemon *daemon_handle); 554prepare_daemon(struct MHD_Daemon *daemon_handle);
556 555
557 556
558/** 557/**
@@ -562,7 +561,7 @@ prepare_daemon (struct MHD_Daemon *daemon_handle);
562 * @param cls the `struct MHD_Daemon` of the HTTP server to run 561 * @param cls the `struct MHD_Daemon` of the HTTP server to run
563 */ 562 */
564static void 563static void
565run_daemon (void *cls) 564run_daemon(void *cls)
566{ 565{
567 struct MHD_Daemon *daemon_handle = cls; 566 struct MHD_Daemon *daemon_handle = cls;
568 567
@@ -570,11 +569,11 @@ run_daemon (void *cls)
570 hostlist_task_v4 = NULL; 569 hostlist_task_v4 = NULL;
571 else 570 else
572 hostlist_task_v6 = NULL; 571 hostlist_task_v6 = NULL;
573 GNUNET_assert (MHD_YES == MHD_run (daemon_handle)); 572 GNUNET_assert(MHD_YES == MHD_run(daemon_handle));
574 if (daemon_handle == daemon_handle_v4) 573 if (daemon_handle == daemon_handle_v4)
575 hostlist_task_v4 = prepare_daemon (daemon_handle); 574 hostlist_task_v4 = prepare_daemon(daemon_handle);
576 else 575 else
577 hostlist_task_v6 = prepare_daemon (daemon_handle); 576 hostlist_task_v6 = prepare_daemon(daemon_handle);
578} 577}
579 578
580 579
@@ -585,7 +584,7 @@ run_daemon (void *cls)
585 * @param daemon_handle HTTP server to prepare to run 584 * @param daemon_handle HTTP server to prepare to run
586 */ 585 */
587static struct GNUNET_SCHEDULER_Task * 586static struct GNUNET_SCHEDULER_Task *
588prepare_daemon (struct MHD_Daemon *daemon_handle) 587prepare_daemon(struct MHD_Daemon *daemon_handle)
589{ 588{
590 struct GNUNET_SCHEDULER_Task *ret; 589 struct GNUNET_SCHEDULER_Task *ret;
591 fd_set rs; 590 fd_set rs;
@@ -598,28 +597,28 @@ prepare_daemon (struct MHD_Daemon *daemon_handle)
598 int haveto; 597 int haveto;
599 struct GNUNET_TIME_Relative tv; 598 struct GNUNET_TIME_Relative tv;
600 599
601 FD_ZERO (&rs); 600 FD_ZERO(&rs);
602 FD_ZERO (&ws); 601 FD_ZERO(&ws);
603 FD_ZERO (&es); 602 FD_ZERO(&es);
604 wrs = GNUNET_NETWORK_fdset_create (); 603 wrs = GNUNET_NETWORK_fdset_create();
605 wws = GNUNET_NETWORK_fdset_create (); 604 wws = GNUNET_NETWORK_fdset_create();
606 max = -1; 605 max = -1;
607 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max)); 606 GNUNET_assert(MHD_YES == MHD_get_fdset(daemon_handle, &rs, &ws, &es, &max));
608 haveto = MHD_get_timeout (daemon_handle, &timeout); 607 haveto = MHD_get_timeout(daemon_handle, &timeout);
609 if (haveto == MHD_YES) 608 if (haveto == MHD_YES)
610 tv.rel_value_us = (uint64_t) timeout * 1000LL; 609 tv.rel_value_us = (uint64_t)timeout * 1000LL;
611 else 610 else
612 tv = GNUNET_TIME_UNIT_FOREVER_REL; 611 tv = GNUNET_TIME_UNIT_FOREVER_REL;
613 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1); 612 GNUNET_NETWORK_fdset_copy_native(wrs, &rs, max + 1);
614 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1); 613 GNUNET_NETWORK_fdset_copy_native(wws, &ws, max + 1);
615 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 614 ret = GNUNET_SCHEDULER_add_select(GNUNET_SCHEDULER_PRIORITY_HIGH,
616 tv, 615 tv,
617 wrs, 616 wrs,
618 wws, 617 wws,
619 &run_daemon, 618 &run_daemon,
620 daemon_handle); 619 daemon_handle);
621 GNUNET_NETWORK_fdset_destroy (wrs); 620 GNUNET_NETWORK_fdset_destroy(wrs);
622 GNUNET_NETWORK_fdset_destroy (wws); 621 GNUNET_NETWORK_fdset_destroy(wws);
623 return ret; 622 return ret;
624} 623}
625 624
@@ -635,11 +634,11 @@ prepare_daemon (struct MHD_Daemon *daemon_handle)
635 * @return #GNUNET_OK on success 634 * @return #GNUNET_OK on success
636 */ 635 */
637int 636int
638GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c, 637GNUNET_HOSTLIST_server_start(const struct GNUNET_CONFIGURATION_Handle *c,
639 struct GNUNET_STATISTICS_Handle *st, 638 struct GNUNET_STATISTICS_Handle *st,
640 struct GNUNET_CORE_Handle *co, 639 struct GNUNET_CORE_Handle *co,
641 GNUNET_CORE_ConnectEventHandler *server_ch, 640 GNUNET_CORE_ConnectEventHandler *server_ch,
642 int advertise) 641 int advertise)
643{ 642{
644 unsigned long long port; 643 unsigned long long port;
645 char *hostname; 644 char *hostname;
@@ -654,189 +653,189 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
654 const struct sockaddr *sa6; 653 const struct sockaddr *sa6;
655 654
656 advertising = advertise; 655 advertising = advertise;
657 if (! advertising) 656 if (!advertising)
658 { 657 {
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 658 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
660 "Advertising not enabled on this hostlist server\n"); 659 "Advertising not enabled on this hostlist server\n");
661 } 660 }
662 else 661 else
663 { 662 {
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 663 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
665 "Advertising enabled on this hostlist server\n"); 664 "Advertising enabled on this hostlist server\n");
666 } 665 }
667 cfg = c; 666 cfg = c;
668 stats = st; 667 stats = st;
669 peerinfo = GNUNET_PEERINFO_connect (cfg); 668 peerinfo = GNUNET_PEERINFO_connect(cfg);
670 if (NULL == peerinfo) 669 if (NULL == peerinfo)
671 { 670 {
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 671 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
673 _ ("Could not access PEERINFO service. Exiting.\n")); 672 _("Could not access PEERINFO service. Exiting.\n"));
674 return GNUNET_SYSERR; 673 return GNUNET_SYSERR;
675 } 674 }
676 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, 675 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg,
677 "HOSTLIST", 676 "HOSTLIST",
678 "HTTPPORT", 677 "HTTPPORT",
679 &port)) 678 &port))
680 return GNUNET_SYSERR; 679 return GNUNET_SYSERR;
681 if ((0 == port) || (port > UINT16_MAX)) 680 if ((0 == port) || (port > UINT16_MAX))
682 { 681 {
683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 682 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
684 _ ("Invalid port number %llu. Exiting.\n"), 683 _("Invalid port number %llu. Exiting.\n"),
685 port); 684 port);
686 return GNUNET_SYSERR; 685 return GNUNET_SYSERR;
687 } 686 }
688 687
689 if (GNUNET_SYSERR == 688 if (GNUNET_SYSERR ==
690 GNUNET_CONFIGURATION_get_value_string (cfg, 689 GNUNET_CONFIGURATION_get_value_string(cfg,
691 "HOSTLIST", 690 "HOSTLIST",
692 "EXTERNAL_DNS_NAME", 691 "EXTERNAL_DNS_NAME",
693 &hostname)) 692 &hostname))
694 hostname = GNUNET_RESOLVER_local_fqdn_get (); 693 hostname = GNUNET_RESOLVER_local_fqdn_get();
695 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 694 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
696 _ ("Hostlist service starts on %s:%llu\n"), 695 _("Hostlist service starts on %s:%llu\n"),
697 hostname, 696 hostname,
698 port); 697 port);
699 if (NULL != hostname) 698 if (NULL != hostname)
700 {
701 size = strlen (hostname);
702 if (size + 15 > MAX_URL_LEN)
703 { 699 {
704 GNUNET_break (0); 700 size = strlen(hostname);
701 if (size + 15 > MAX_URL_LEN)
702 {
703 GNUNET_break(0);
704 }
705 else
706 {
707 GNUNET_asprintf(&hostlist_uri,
708 "http://%s:%u/",
709 hostname,
710 (unsigned int)port);
711 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
712 _("Address to obtain hostlist: `%s'\n"),
713 hostlist_uri);
714 }
715 GNUNET_free(hostname);
705 } 716 }
706 else
707 {
708 GNUNET_asprintf (&hostlist_uri,
709 "http://%s:%u/",
710 hostname,
711 (unsigned int) port);
712 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
713 _ ("Address to obtain hostlist: `%s'\n"),
714 hostlist_uri);
715 }
716 GNUNET_free (hostname);
717 }
718 717
719 if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV4")) 718 if (GNUNET_CONFIGURATION_have_value(cfg, "HOSTLIST", "BINDTOIPV4"))
720 {
721 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
722 "HOSTLIST",
723 "BINDTOIP",
724 &ipv4))
725 { 719 {
726 GNUNET_log ( 720 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg,
727 GNUNET_ERROR_TYPE_WARNING, 721 "HOSTLIST",
728 _ ("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV4.\n")); 722 "BINDTOIP",
723 &ipv4))
724 {
725 GNUNET_log(
726 GNUNET_ERROR_TYPE_WARNING,
727 _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV4.\n"));
728 }
729 } 729 }
730 }
731 else 730 else
732 ipv4 = NULL; 731 ipv4 = NULL;
733 if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV6")) 732 if (GNUNET_CONFIGURATION_have_value(cfg, "HOSTLIST", "BINDTOIPV6"))
734 { 733 {
735 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, 734 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg,
736 "HOSTLIST", 735 "HOSTLIST",
737 "BINDTOIP", 736 "BINDTOIP",
738 &ipv6)) 737 &ipv6))
739 { 738 {
740 GNUNET_log ( 739 GNUNET_log(
741 GNUNET_ERROR_TYPE_WARNING, 740 GNUNET_ERROR_TYPE_WARNING,
742 _ ("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n")); 741 _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n"));
742 }
743 } 743 }
744 }
745 else 744 else
746 ipv6 = NULL; 745 ipv6 = NULL;
747 sa4 = NULL; 746 sa4 = NULL;
748 if (NULL != ipv4) 747 if (NULL != ipv4)
749 {
750 if (1 == inet_pton (AF_INET, ipv4, &i4))
751 { 748 {
752 memset (&v4, 0, sizeof (v4)); 749 if (1 == inet_pton(AF_INET, ipv4, &i4))
753 v4.sin_family = AF_INET; 750 {
754 v4.sin_addr = i4; 751 memset(&v4, 0, sizeof(v4));
755 v4.sin_port = htons (port); 752 v4.sin_family = AF_INET;
753 v4.sin_addr = i4;
754 v4.sin_port = htons(port);
756#if HAVE_SOCKADDR_IN_SIN_LEN 755#if HAVE_SOCKADDR_IN_SIN_LEN
757 v4.sin_len = sizeof (v4); 756 v4.sin_len = sizeof(v4);
758#endif 757#endif
759 sa4 = (const struct sockaddr *) &v4; 758 sa4 = (const struct sockaddr *)&v4;
760 } 759 }
761 else 760 else
762 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 761 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
763 _ ( 762 _(
764 "`%s' is not a valid IPv4 address! Ignoring BINDTOIPV4.\n"), 763 "`%s' is not a valid IPv4 address! Ignoring BINDTOIPV4.\n"),
765 ipv4); 764 ipv4);
766 GNUNET_free (ipv4); 765 GNUNET_free(ipv4);
767 } 766 }
768 sa6 = NULL; 767 sa6 = NULL;
769 if (NULL != ipv6) 768 if (NULL != ipv6)
770 {
771 if (1 == inet_pton (AF_INET6, ipv6, &i6))
772 { 769 {
773 memset (&v6, 0, sizeof (v6)); 770 if (1 == inet_pton(AF_INET6, ipv6, &i6))
774 v6.sin6_family = AF_INET6; 771 {
775 v6.sin6_addr = i6; 772 memset(&v6, 0, sizeof(v6));
776 v6.sin6_port = htons (port); 773 v6.sin6_family = AF_INET6;
774 v6.sin6_addr = i6;
775 v6.sin6_port = htons(port);
777#if HAVE_SOCKADDR_IN_SIN_LEN 776#if HAVE_SOCKADDR_IN_SIN_LEN
778 v6.sin6_len = sizeof (v6); 777 v6.sin6_len = sizeof(v6);
779#endif 778#endif
780 sa6 = (const struct sockaddr *) &v6; 779 sa6 = (const struct sockaddr *)&v6;
781 } 780 }
782 else 781 else
783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 782 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
784 _ ( 783 _(
785 "`%s' is not a valid IPv6 address! Ignoring BINDTOIPV6.\n"), 784 "`%s' is not a valid IPv6 address! Ignoring BINDTOIPV6.\n"),
786 ipv6); 785 ipv6);
787 GNUNET_free (ipv6); 786 GNUNET_free(ipv6);
788 } 787 }
789 788
790 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 | MHD_USE_DEBUG, 789 daemon_handle_v6 = MHD_start_daemon(MHD_USE_IPv6 | MHD_USE_DEBUG,
791 (uint16_t) port, 790 (uint16_t)port,
792 &accept_policy_callback, 791 &accept_policy_callback,
793 NULL, 792 NULL,
794 &access_handler_callback, 793 &access_handler_callback,
795 NULL, 794 NULL,
796 MHD_OPTION_CONNECTION_LIMIT, 795 MHD_OPTION_CONNECTION_LIMIT,
797 (unsigned int) 128, 796 (unsigned int)128,
798 MHD_OPTION_PER_IP_CONNECTION_LIMIT, 797 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
799 (unsigned int) 32, 798 (unsigned int)32,
800 MHD_OPTION_CONNECTION_TIMEOUT, 799 MHD_OPTION_CONNECTION_TIMEOUT,
801 (unsigned int) 16, 800 (unsigned int)16,
802 MHD_OPTION_CONNECTION_MEMORY_LIMIT, 801 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
803 (size_t) (16 * 1024), 802 (size_t)(16 * 1024),
804 MHD_OPTION_SOCK_ADDR, 803 MHD_OPTION_SOCK_ADDR,
805 sa6, 804 sa6,
806 MHD_OPTION_END); 805 MHD_OPTION_END);
807 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG | MHD_USE_DEBUG, 806 daemon_handle_v4 = MHD_start_daemon(MHD_NO_FLAG | MHD_USE_DEBUG,
808 (uint16_t) port, 807 (uint16_t)port,
809 &accept_policy_callback, 808 &accept_policy_callback,
810 NULL, 809 NULL,
811 &access_handler_callback, 810 &access_handler_callback,
812 NULL, 811 NULL,
813 MHD_OPTION_CONNECTION_LIMIT, 812 MHD_OPTION_CONNECTION_LIMIT,
814 (unsigned int) 128, 813 (unsigned int)128,
815 MHD_OPTION_PER_IP_CONNECTION_LIMIT, 814 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
816 (unsigned int) 32, 815 (unsigned int)32,
817 MHD_OPTION_CONNECTION_TIMEOUT, 816 MHD_OPTION_CONNECTION_TIMEOUT,
818 (unsigned int) 16, 817 (unsigned int)16,
819 MHD_OPTION_CONNECTION_MEMORY_LIMIT, 818 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
820 (size_t) (16 * 1024), 819 (size_t)(16 * 1024),
821 MHD_OPTION_SOCK_ADDR, 820 MHD_OPTION_SOCK_ADDR,
822 sa4, 821 sa4,
823 MHD_OPTION_END); 822 MHD_OPTION_END);
824 823
825 if ((NULL == daemon_handle_v6) && (NULL == daemon_handle_v4)) 824 if ((NULL == daemon_handle_v6) && (NULL == daemon_handle_v4))
826 { 825 {
827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 826 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
828 _ ("Could not start hostlist HTTP server on port %u\n"), 827 _("Could not start hostlist HTTP server on port %u\n"),
829 (unsigned short) port); 828 (unsigned short)port);
830 return GNUNET_SYSERR; 829 return GNUNET_SYSERR;
831 } 830 }
832 831
833 core = co; 832 core = co;
834 *server_ch = &connect_handler; 833 *server_ch = &connect_handler;
835 if (NULL != daemon_handle_v4) 834 if (NULL != daemon_handle_v4)
836 hostlist_task_v4 = prepare_daemon (daemon_handle_v4); 835 hostlist_task_v4 = prepare_daemon(daemon_handle_v4);
837 if (NULL != daemon_handle_v6) 836 if (NULL != daemon_handle_v6)
838 hostlist_task_v6 = prepare_daemon (daemon_handle_v6); 837 hostlist_task_v6 = prepare_daemon(daemon_handle_v6);
839 notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_notify, NULL); 838 notify = GNUNET_PEERINFO_notify(cfg, GNUNET_NO, &process_notify, NULL);
840 return GNUNET_OK; 839 return GNUNET_OK;
841} 840}
842 841
@@ -845,55 +844,55 @@ GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
845 * Stop server offering our hostlist. 844 * Stop server offering our hostlist.
846 */ 845 */
847void 846void
848GNUNET_HOSTLIST_server_stop () 847GNUNET_HOSTLIST_server_stop()
849{ 848{
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist server shutdown\n"); 849 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Hostlist server shutdown\n");
851 if (NULL != hostlist_task_v6) 850 if (NULL != hostlist_task_v6)
852 { 851 {
853 GNUNET_SCHEDULER_cancel (hostlist_task_v6); 852 GNUNET_SCHEDULER_cancel(hostlist_task_v6);
854 hostlist_task_v6 = NULL; 853 hostlist_task_v6 = NULL;
855 } 854 }
856 if (NULL != hostlist_task_v4) 855 if (NULL != hostlist_task_v4)
857 { 856 {
858 GNUNET_SCHEDULER_cancel (hostlist_task_v4); 857 GNUNET_SCHEDULER_cancel(hostlist_task_v4);
859 hostlist_task_v4 = NULL; 858 hostlist_task_v4 = NULL;
860 } 859 }
861 if (NULL != daemon_handle_v4) 860 if (NULL != daemon_handle_v4)
862 { 861 {
863 MHD_stop_daemon (daemon_handle_v4); 862 MHD_stop_daemon(daemon_handle_v4);
864 daemon_handle_v4 = NULL; 863 daemon_handle_v4 = NULL;
865 } 864 }
866 if (NULL != daemon_handle_v6) 865 if (NULL != daemon_handle_v6)
867 { 866 {
868 MHD_stop_daemon (daemon_handle_v6); 867 MHD_stop_daemon(daemon_handle_v6);
869 daemon_handle_v6 = NULL; 868 daemon_handle_v6 = NULL;
870 } 869 }
871 if (NULL != response) 870 if (NULL != response)
872 { 871 {
873 MHD_destroy_response (response); 872 MHD_destroy_response(response);
874 response = NULL; 873 response = NULL;
875 } 874 }
876 if (NULL != notify) 875 if (NULL != notify)
877 { 876 {
878 GNUNET_PEERINFO_notify_cancel (notify); 877 GNUNET_PEERINFO_notify_cancel(notify);
879 notify = NULL; 878 notify = NULL;
880 } 879 }
881 if (NULL != builder) 880 if (NULL != builder)
882 {
883 if (NULL != builder->pitr)
884 { 881 {
885 GNUNET_PEERINFO_iterate_cancel (builder->pitr); 882 if (NULL != builder->pitr)
886 builder->pitr = NULL; 883 {
884 GNUNET_PEERINFO_iterate_cancel(builder->pitr);
885 builder->pitr = NULL;
886 }
887 GNUNET_free_non_null(builder->data);
888 GNUNET_free(builder);
889 builder = NULL;
887 } 890 }
888 GNUNET_free_non_null (builder->data);
889 GNUNET_free (builder);
890 builder = NULL;
891 }
892 if (NULL != peerinfo) 891 if (NULL != peerinfo)
893 { 892 {
894 GNUNET_PEERINFO_disconnect (peerinfo); 893 GNUNET_PEERINFO_disconnect(peerinfo);
895 peerinfo = NULL; 894 peerinfo = NULL;
896 } 895 }
897 cfg = NULL; 896 cfg = NULL;
898 stats = NULL; 897 stats = NULL;
899 core = NULL; 898 core = NULL;