aboutsummaryrefslogtreecommitdiff
path: root/src/util/client.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-09-27 22:33:50 +0200
committerChristian Grothoff <christian@grothoff.org>2019-09-27 22:40:19 +0200
commiteadf4b7379049d32d7cfc5cffd4c88613f8a2a93 (patch)
tree74ae03df0080c9b72c4c827438b912436c6fba9a /src/util/client.c
parentfa0907738dcc299ed62205a11c18241acd4761eb (diff)
downloadgnunet-eadf4b7379049d32d7cfc5cffd4c88613f8a2a93.tar.gz
gnunet-eadf4b7379049d32d7cfc5cffd4c88613f8a2a93.zip
indent
Diffstat (limited to 'src/util/client.c')
-rw-r--r--src/util/client.c845
1 files changed, 424 insertions, 421 deletions
diff --git a/src/util/client.c b/src/util/client.c
index e3585af2e..5a77b9238 100644
--- a/src/util/client.c
+++ b/src/util/client.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2001-2016 GNUnet e.V. 3 Copyright (C) 2001-2016, 2019 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 6 under the terms of the GNU Affero General Public License as published
@@ -33,7 +33,7 @@
33#include "gnunet_socks.h" 33#include "gnunet_socks.h"
34 34
35 35
36#define LOG(kind, ...) GNUNET_log_from(kind, "util-client", __VA_ARGS__) 36#define LOG(kind, ...) GNUNET_log_from (kind, "util-client", __VA_ARGS__)
37 37
38/** 38/**
39 * Timeout we use on TCP connect before trying another 39 * Timeout we use on TCP connect before trying another
@@ -41,7 +41,8 @@
41 * is this value divided by the number of address families. 41 * is this value divided by the number of address families.
42 * Default is 5s. 42 * Default is 5s.
43 */ 43 */
44#define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) 44#define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply ( \
45 GNUNET_TIME_UNIT_SECONDS, 5)
45 46
46 47
47 48
@@ -55,7 +56,8 @@ struct ClientState;
55 * During connect, we try multiple possible IP addresses 56 * During connect, we try multiple possible IP addresses
56 * to find out which one might work. 57 * to find out which one might work.
57 */ 58 */
58struct AddressProbe { 59struct AddressProbe
60{
59 /** 61 /**
60 * This is a linked list. 62 * This is a linked list.
61 */ 63 */
@@ -96,7 +98,8 @@ struct AddressProbe {
96/** 98/**
97 * Internal state for a client connected to a GNUnet service. 99 * Internal state for a client connected to a GNUnet service.
98 */ 100 */
99struct ClientState { 101struct ClientState
102{
100 /** 103 /**
101 * The connection handle, NULL if not live 104 * The connection handle, NULL if not live
102 */ 105 */
@@ -205,7 +208,7 @@ struct ClientState {
205 * @param cls the `struct ClientState` to try to connect to the service 208 * @param cls the `struct ClientState` to try to connect to the service
206 */ 209 */
207static void 210static void
208start_connect(void *cls); 211start_connect (void *cls);
209 212
210 213
211/** 214/**
@@ -215,26 +218,26 @@ start_connect(void *cls);
215 * @param cstate the connection we tried to establish 218 * @param cstate the connection we tried to establish
216 */ 219 */
217static void 220static void
218connect_fail_continuation(struct ClientState *cstate) 221connect_fail_continuation (struct ClientState *cstate)
219{ 222{
220 GNUNET_break(NULL == cstate->ap_head); 223 GNUNET_break (NULL == cstate->ap_head);
221 GNUNET_break(NULL == cstate->ap_tail); 224 GNUNET_break (NULL == cstate->ap_tail);
222 GNUNET_break(NULL == cstate->dns_active); 225 GNUNET_break (NULL == cstate->dns_active);
223 GNUNET_break(NULL == cstate->sock); 226 GNUNET_break (NULL == cstate->sock);
224 GNUNET_assert(NULL == cstate->send_task); 227 GNUNET_assert (NULL == cstate->send_task);
225 GNUNET_assert(NULL == cstate->recv_task); 228 GNUNET_assert (NULL == cstate->recv_task);
226 // GNUNET_assert (NULL == cstate->proxy_handshake); 229 // GNUNET_assert (NULL == cstate->proxy_handshake);
227 230
228 cstate->back_off = GNUNET_TIME_STD_BACKOFF(cstate->back_off); 231 cstate->back_off = GNUNET_TIME_STD_BACKOFF (cstate->back_off);
229 LOG(GNUNET_ERROR_TYPE_DEBUG, 232 LOG (GNUNET_ERROR_TYPE_DEBUG,
230 "Failed to establish connection to `%s', no further addresses to try, will try again in %s.\n", 233 "Failed to establish connection to `%s', no further addresses to try, will try again in %s.\n",
231 cstate->service_name, 234 cstate->service_name,
232 GNUNET_STRINGS_relative_time_to_string(cstate->back_off, 235 GNUNET_STRINGS_relative_time_to_string (cstate->back_off,
233 GNUNET_YES)); 236 GNUNET_YES));
234 cstate->retry_task 237 cstate->retry_task
235 = GNUNET_SCHEDULER_add_delayed(cstate->back_off, 238 = GNUNET_SCHEDULER_add_delayed (cstate->back_off,
236 &start_connect, 239 &start_connect,
237 cstate); 240 cstate);
238} 241}
239 242
240 243
@@ -244,7 +247,7 @@ connect_fail_continuation(struct ClientState *cstate)
244 * @param cls the `struct ClientState` with the `msg` to transmit 247 * @param cls the `struct ClientState` with the `msg` to transmit
245 */ 248 */
246static void 249static void
247transmit_ready(void *cls) 250transmit_ready (void *cls)
248{ 251{
249 struct ClientState *cstate = cls; 252 struct ClientState *cstate = cls;
250 ssize_t ret; 253 ssize_t ret;
@@ -255,56 +258,56 @@ transmit_ready(void *cls)
255 cstate->send_task = NULL; 258 cstate->send_task = NULL;
256 if (GNUNET_YES == cstate->in_destroy) 259 if (GNUNET_YES == cstate->in_destroy)
257 return; 260 return;
258 pos = (const char *)cstate->msg; 261 pos = (const char *) cstate->msg;
259 len = ntohs(cstate->msg->size); 262 len = ntohs (cstate->msg->size);
260 GNUNET_assert(cstate->msg_off < len); 263 GNUNET_assert (cstate->msg_off < len);
261 LOG(GNUNET_ERROR_TYPE_DEBUG, 264 LOG (GNUNET_ERROR_TYPE_DEBUG,
262 "message of type %u trying to send with socket %p (MQ: %p\n", 265 "message of type %u trying to send with socket %p (MQ: %p\n",
263 ntohs(cstate->msg->type), 266 ntohs (cstate->msg->type),
264 cstate->sock, 267 cstate->sock,
265 cstate->mq); 268 cstate->mq);
266 269
267RETRY: 270RETRY:
268 ret = GNUNET_NETWORK_socket_send(cstate->sock, 271 ret = GNUNET_NETWORK_socket_send (cstate->sock,
269 &pos[cstate->msg_off], 272 &pos[cstate->msg_off],
270 len - cstate->msg_off); 273 len - cstate->msg_off);
271 if (-1 == ret) 274 if (-1 == ret)
275 {
276 LOG (GNUNET_ERROR_TYPE_WARNING,
277 "Error during sending message of type %u\n",
278 ntohs (cstate->msg->type));
279 if (EINTR == errno)
272 { 280 {
273 LOG(GNUNET_ERROR_TYPE_WARNING, 281 LOG (GNUNET_ERROR_TYPE_DEBUG,
274 "Error during sending message of type %u\n", 282 "Retrying message of type %u\n",
275 ntohs(cstate->msg->type)); 283 ntohs (cstate->msg->type));
276 if (EINTR == errno) 284 goto RETRY;
277 {
278 LOG(GNUNET_ERROR_TYPE_DEBUG,
279 "Retrying message of type %u\n",
280 ntohs(cstate->msg->type));
281 goto RETRY;
282 }
283 GNUNET_MQ_inject_error(cstate->mq,
284 GNUNET_MQ_ERROR_WRITE);
285 return;
286 } 285 }
286 GNUNET_MQ_inject_error (cstate->mq,
287 GNUNET_MQ_ERROR_WRITE);
288 return;
289 }
287 notify_in_flight = (0 == cstate->msg_off); 290 notify_in_flight = (0 == cstate->msg_off);
288 cstate->msg_off += ret; 291 cstate->msg_off += ret;
289 if (cstate->msg_off < len) 292 if (cstate->msg_off < len)
290 { 293 {
291 LOG(GNUNET_ERROR_TYPE_DEBUG, 294 LOG (GNUNET_ERROR_TYPE_DEBUG,
292 "rescheduling message of type %u\n", 295 "rescheduling message of type %u\n",
293 ntohs(cstate->msg->type)); 296 ntohs (cstate->msg->type));
294 cstate->send_task 297 cstate->send_task
295 = GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_FOREVER_REL, 298 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
296 cstate->sock, 299 cstate->sock,
297 &transmit_ready, 300 &transmit_ready,
298 cstate); 301 cstate);
299 if (notify_in_flight) 302 if (notify_in_flight)
300 GNUNET_MQ_impl_send_in_flight(cstate->mq); 303 GNUNET_MQ_impl_send_in_flight (cstate->mq);
301 return; 304 return;
302 } 305 }
303 LOG(GNUNET_ERROR_TYPE_DEBUG, 306 LOG (GNUNET_ERROR_TYPE_DEBUG,
304 "sending message of type %u successful\n", 307 "sending message of type %u successful\n",
305 ntohs(cstate->msg->type)); 308 ntohs (cstate->msg->type));
306 cstate->msg = NULL; 309 cstate->msg = NULL;
307 GNUNET_MQ_impl_send_continue(cstate->mq); 310 GNUNET_MQ_impl_send_continue (cstate->mq);
308} 311}
309 312
310 313
@@ -319,20 +322,20 @@ RETRY:
319 * #GNUNET_SYSERR to stop further processing due to error 322 * #GNUNET_SYSERR to stop further processing due to error
320 */ 323 */
321static int 324static int
322recv_message(void *cls, 325recv_message (void *cls,
323 const struct GNUNET_MessageHeader *msg) 326 const struct GNUNET_MessageHeader *msg)
324{ 327{
325 struct ClientState *cstate = cls; 328 struct ClientState *cstate = cls;
326 329
327 if (GNUNET_YES == cstate->in_destroy) 330 if (GNUNET_YES == cstate->in_destroy)
328 return GNUNET_NO; 331 return GNUNET_NO;
329 LOG(GNUNET_ERROR_TYPE_DEBUG, 332 LOG (GNUNET_ERROR_TYPE_DEBUG,
330 "Received message of type %u and size %u from %s\n", 333 "Received message of type %u and size %u from %s\n",
331 ntohs(msg->type), 334 ntohs (msg->type),
332 ntohs(msg->size), 335 ntohs (msg->size),
333 cstate->service_name); 336 cstate->service_name);
334 GNUNET_MQ_inject_message(cstate->mq, 337 GNUNET_MQ_inject_message (cstate->mq,
335 msg); 338 msg);
336 if (GNUNET_YES == cstate->in_destroy) 339 if (GNUNET_YES == cstate->in_destroy)
337 return GNUNET_NO; 340 return GNUNET_NO;
338 return GNUNET_OK; 341 return GNUNET_OK;
@@ -345,20 +348,20 @@ recv_message(void *cls,
345 * @param cstate handle of the client state to process 348 * @param cstate handle of the client state to process
346 */ 349 */
347static void 350static void
348cancel_aps(struct ClientState *cstate) 351cancel_aps (struct ClientState *cstate)
349{ 352{
350 struct AddressProbe *pos; 353 struct AddressProbe *pos;
351 354
352 while (NULL != (pos = cstate->ap_head)) 355 while (NULL != (pos = cstate->ap_head))
353 { 356 {
354 GNUNET_break(GNUNET_OK == 357 GNUNET_break (GNUNET_OK ==
355 GNUNET_NETWORK_socket_close(pos->sock)); 358 GNUNET_NETWORK_socket_close (pos->sock));
356 GNUNET_SCHEDULER_cancel(pos->task); 359 GNUNET_SCHEDULER_cancel (pos->task);
357 GNUNET_CONTAINER_DLL_remove(cstate->ap_head, 360 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
358 cstate->ap_tail, 361 cstate->ap_tail,
359 pos); 362 pos);
360 GNUNET_free(pos); 363 GNUNET_free (pos);
361 } 364 }
362} 365}
363 366
364 367
@@ -370,51 +373,51 @@ cancel_aps(struct ClientState *cstate)
370 * @param impl_state our `struct ClientState` 373 * @param impl_state our `struct ClientState`
371 */ 374 */
372static void 375static void
373connection_client_destroy_impl(struct GNUNET_MQ_Handle *mq, 376connection_client_destroy_impl (struct GNUNET_MQ_Handle *mq,
374 void *impl_state) 377 void *impl_state)
375{ 378{
376 struct ClientState *cstate = impl_state; 379 struct ClientState *cstate = impl_state;
377 380
378 (void)mq; 381 (void) mq;
379 if (NULL != cstate->dns_active) 382 if (NULL != cstate->dns_active)
380 { 383 {
381 GNUNET_RESOLVER_request_cancel(cstate->dns_active); 384 GNUNET_RESOLVER_request_cancel (cstate->dns_active);
382 cstate->dns_active = NULL; 385 cstate->dns_active = NULL;
383 } 386 }
384 if (NULL != cstate->send_task) 387 if (NULL != cstate->send_task)
385 { 388 {
386 GNUNET_SCHEDULER_cancel(cstate->send_task); 389 GNUNET_SCHEDULER_cancel (cstate->send_task);
387 cstate->send_task = NULL; 390 cstate->send_task = NULL;
388 } 391 }
389 if (NULL != cstate->retry_task) 392 if (NULL != cstate->retry_task)
390 { 393 {
391 GNUNET_SCHEDULER_cancel(cstate->retry_task); 394 GNUNET_SCHEDULER_cancel (cstate->retry_task);
392 cstate->retry_task = NULL; 395 cstate->retry_task = NULL;
393 } 396 }
394 if (GNUNET_SYSERR == cstate->in_destroy) 397 if (GNUNET_SYSERR == cstate->in_destroy)
395 { 398 {
396 /* defer destruction */ 399 /* defer destruction */
397 cstate->in_destroy = GNUNET_YES; 400 cstate->in_destroy = GNUNET_YES;
398 cstate->mq = NULL; 401 cstate->mq = NULL;
399 return; 402 return;
400 } 403 }
401 if (NULL != cstate->recv_task) 404 if (NULL != cstate->recv_task)
402 { 405 {
403 GNUNET_SCHEDULER_cancel(cstate->recv_task); 406 GNUNET_SCHEDULER_cancel (cstate->recv_task);
404 cstate->recv_task = NULL; 407 cstate->recv_task = NULL;
405 } 408 }
406 if (NULL != cstate->sock) 409 if (NULL != cstate->sock)
407 { 410 {
408 LOG(GNUNET_ERROR_TYPE_DEBUG, 411 LOG (GNUNET_ERROR_TYPE_DEBUG,
409 "destroying socket: %p\n", 412 "destroying socket: %p\n",
410 cstate->sock); 413 cstate->sock);
411 GNUNET_NETWORK_socket_close(cstate->sock); 414 GNUNET_NETWORK_socket_close (cstate->sock);
412 } 415 }
413 cancel_aps(cstate); 416 cancel_aps (cstate);
414 GNUNET_free(cstate->service_name); 417 GNUNET_free (cstate->service_name);
415 GNUNET_free_non_null(cstate->hostname); 418 GNUNET_free_non_null (cstate->hostname);
416 GNUNET_MST_destroy(cstate->mst); 419 GNUNET_MST_destroy (cstate->mst);
417 GNUNET_free(cstate); 420 GNUNET_free (cstate);
418} 421}
419 422
420 423
@@ -424,39 +427,39 @@ connection_client_destroy_impl(struct GNUNET_MQ_Handle *mq,
424 * @param cls `struct ClientState` with connection to read from 427 * @param cls `struct ClientState` with connection to read from
425 */ 428 */
426static void 429static void
427receive_ready(void *cls) 430receive_ready (void *cls)
428{ 431{
429 struct ClientState *cstate = cls; 432 struct ClientState *cstate = cls;
430 int ret; 433 int ret;
431 434
432 cstate->recv_task = NULL; 435 cstate->recv_task = NULL;
433 cstate->in_destroy = GNUNET_SYSERR; 436 cstate->in_destroy = GNUNET_SYSERR;
434 ret = GNUNET_MST_read(cstate->mst, 437 ret = GNUNET_MST_read (cstate->mst,
435 cstate->sock, 438 cstate->sock,
436 GNUNET_NO, 439 GNUNET_NO,
437 GNUNET_NO); 440 GNUNET_NO);
438 if (GNUNET_SYSERR == ret) 441 if (GNUNET_SYSERR == ret)
439 { 442 {
440 if (NULL != cstate->mq) 443 if (NULL != cstate->mq)
441 GNUNET_MQ_inject_error(cstate->mq, 444 GNUNET_MQ_inject_error (cstate->mq,
442 GNUNET_MQ_ERROR_READ); 445 GNUNET_MQ_ERROR_READ);
443 if (GNUNET_YES == cstate->in_destroy) 446 if (GNUNET_YES == cstate->in_destroy)
444 connection_client_destroy_impl(cstate->mq, 447 connection_client_destroy_impl (cstate->mq,
445 cstate); 448 cstate);
446 return; 449 return;
447 } 450 }
448 if (GNUNET_YES == cstate->in_destroy) 451 if (GNUNET_YES == cstate->in_destroy)
449 { 452 {
450 connection_client_destroy_impl(cstate->mq, 453 connection_client_destroy_impl (cstate->mq,
451 cstate); 454 cstate);
452 return; 455 return;
453 } 456 }
454 cstate->in_destroy = GNUNET_NO; 457 cstate->in_destroy = GNUNET_NO;
455 cstate->recv_task 458 cstate->recv_task
456 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 459 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
457 cstate->sock, 460 cstate->sock,
458 &receive_ready, 461 &receive_ready,
459 cstate); 462 cstate);
460} 463}
461 464
462 465
@@ -466,23 +469,23 @@ receive_ready(void *cls)
466 * @param cstate the connection we tried to establish 469 * @param cstate the connection we tried to establish
467 */ 470 */
468static void 471static void
469connect_success_continuation(struct ClientState *cstate) 472connect_success_continuation (struct ClientState *cstate)
470{ 473{
471 GNUNET_assert(NULL == cstate->recv_task); 474 GNUNET_assert (NULL == cstate->recv_task);
472 cstate->recv_task 475 cstate->recv_task
473 = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 476 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
474 cstate->sock, 477 cstate->sock,
475 &receive_ready, 478 &receive_ready,
476 cstate); 479 cstate);
477 if (NULL != cstate->msg) 480 if (NULL != cstate->msg)
478 { 481 {
479 GNUNET_assert(NULL == cstate->send_task); 482 GNUNET_assert (NULL == cstate->send_task);
480 cstate->send_task 483 cstate->send_task
481 = GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_FOREVER_REL, 484 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
482 cstate->sock, 485 cstate->sock,
483 &transmit_ready, 486 &transmit_ready,
484 cstate); 487 cstate);
485 } 488 }
486} 489}
487 490
488 491
@@ -494,8 +497,8 @@ connect_success_continuation(struct ClientState *cstate)
494 * @return NULL on error, socket connected to UNIX otherwise 497 * @return NULL on error, socket connected to UNIX otherwise
495 */ 498 */
496static struct GNUNET_NETWORK_Handle * 499static struct GNUNET_NETWORK_Handle *
497try_unixpath(const char *service_name, 500try_unixpath (const char *service_name,
498 const struct GNUNET_CONFIGURATION_Handle *cfg) 501 const struct GNUNET_CONFIGURATION_Handle *cfg)
499{ 502{
500#if AF_UNIX 503#if AF_UNIX
501 struct GNUNET_NETWORK_Handle *sock; 504 struct GNUNET_NETWORK_Handle *sock;
@@ -504,67 +507,67 @@ try_unixpath(const char *service_name,
504 507
505 unixpath = NULL; 508 unixpath = NULL;
506 if ((GNUNET_OK == 509 if ((GNUNET_OK ==
507 GNUNET_CONFIGURATION_get_value_filename(cfg, 510 GNUNET_CONFIGURATION_get_value_filename (cfg,
508 service_name, 511 service_name,
509 "UNIXPATH", 512 "UNIXPATH",
510 &unixpath)) && 513 &unixpath)) &&
511 (0 < strlen(unixpath))) 514 (0 < strlen (unixpath)))
515 {
516 /* We have a non-NULL unixpath, need to validate it */
517 if (strlen (unixpath) >= sizeof(s_un.sun_path))
512 { 518 {
513 /* We have a non-NULL unixpath, need to validate it */ 519 LOG (GNUNET_ERROR_TYPE_WARNING,
514 if (strlen(unixpath) >= sizeof(s_un.sun_path)) 520 _ ("UNIXPATH `%s' too long, maximum length is %llu\n"),
515 { 521 unixpath,
516 LOG(GNUNET_ERROR_TYPE_WARNING, 522 (unsigned long long) sizeof(s_un.sun_path));
517 _("UNIXPATH `%s' too long, maximum length is %llu\n"), 523 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
518 unixpath, 524 LOG (GNUNET_ERROR_TYPE_INFO,
519 (unsigned long long)sizeof(s_un.sun_path)); 525 _ ("Using `%s' instead\n"),
520 unixpath = GNUNET_NETWORK_shorten_unixpath(unixpath); 526 unixpath);
521 LOG(GNUNET_ERROR_TYPE_INFO, 527 if (NULL == unixpath)
522 _("Using `%s' instead\n"), 528 return NULL;
523 unixpath); 529 }
524 if (NULL == unixpath) 530 memset (&s_un,
525 return NULL; 531 0,
526 } 532 sizeof(s_un));
527 memset(&s_un, 533 s_un.sun_family = AF_UNIX;
528 0, 534 GNUNET_strlcpy (s_un.sun_path,
529 sizeof(s_un)); 535 unixpath,
530 s_un.sun_family = AF_UNIX; 536 sizeof(s_un.sun_path));
531 GNUNET_strlcpy(s_un.sun_path,
532 unixpath,
533 sizeof(s_un.sun_path));
534#ifdef LINUX 537#ifdef LINUX
535 { 538 {
536 int abstract; 539 int abstract;
537 540
538 abstract = GNUNET_CONFIGURATION_get_value_yesno(cfg, 541 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
539 "TESTING", 542 "TESTING",
540 "USE_ABSTRACT_SOCKETS"); 543 "USE_ABSTRACT_SOCKETS");
541 if (GNUNET_YES == abstract) 544 if (GNUNET_YES == abstract)
542 s_un.sun_path[0] = '\0'; 545 s_un.sun_path[0] = '\0';
543 } 546 }
544#endif 547#endif
545#if HAVE_SOCKADDR_UN_SUN_LEN 548#if HAVE_SOCKADDR_UN_SUN_LEN
546 s_un.sun_len = (u_char)sizeof(struct sockaddr_un); 549 s_un.sun_len = (u_char) sizeof(struct sockaddr_un);
547#endif 550#endif
548 sock = GNUNET_NETWORK_socket_create(AF_UNIX, 551 sock = GNUNET_NETWORK_socket_create (AF_UNIX,
549 SOCK_STREAM, 552 SOCK_STREAM,
550 0); 553 0);
551 if ((NULL != sock) && 554 if ((NULL != sock) &&
552 ((GNUNET_OK == 555 ((GNUNET_OK ==
553 GNUNET_NETWORK_socket_connect(sock, 556 GNUNET_NETWORK_socket_connect (sock,
554 (struct sockaddr *)&s_un, 557 (struct sockaddr *) &s_un,
555 sizeof(s_un))) || 558 sizeof(s_un))) ||
556 (EINPROGRESS == errno))) 559 (EINPROGRESS == errno)))
557 { 560 {
558 LOG(GNUNET_ERROR_TYPE_DEBUG, 561 LOG (GNUNET_ERROR_TYPE_DEBUG,
559 "Successfully connected to unixpath `%s'!\n", 562 "Successfully connected to unixpath `%s'!\n",
560 unixpath); 563 unixpath);
561 GNUNET_free(unixpath); 564 GNUNET_free (unixpath);
562 return sock; 565 return sock;
563 }
564 if (NULL != sock)
565 GNUNET_NETWORK_socket_close(sock);
566 } 566 }
567 GNUNET_free_non_null(unixpath); 567 if (NULL != sock)
568 GNUNET_NETWORK_socket_close (sock);
569 }
570 GNUNET_free_non_null (unixpath);
568#endif 571#endif
569 return NULL; 572 return NULL;
570} 573}
@@ -577,7 +580,7 @@ try_unixpath(const char *service_name,
577 * @param cls the `struct AddressProbe *` with the address that we are probing 580 * @param cls the `struct AddressProbe *` with the address that we are probing
578 */ 581 */
579static void 582static void
580connect_probe_continuation(void *cls) 583connect_probe_continuation (void *cls)
581{ 584{
582 struct AddressProbe *ap = cls; 585 struct AddressProbe *ap = cls;
583 struct ClientState *cstate = ap->cstate; 586 struct ClientState *cstate = ap->cstate;
@@ -586,40 +589,40 @@ connect_probe_continuation(void *cls)
586 socklen_t len; 589 socklen_t len;
587 590
588 ap->task = NULL; 591 ap->task = NULL;
589 GNUNET_assert(NULL != ap->sock); 592 GNUNET_assert (NULL != ap->sock);
590 GNUNET_CONTAINER_DLL_remove(cstate->ap_head, 593 GNUNET_CONTAINER_DLL_remove (cstate->ap_head,
591 cstate->ap_tail, 594 cstate->ap_tail,
592 ap); 595 ap);
593 len = sizeof(error); 596 len = sizeof(error);
594 error = 0; 597 error = 0;
595 tc = GNUNET_SCHEDULER_get_task_context(); 598 tc = GNUNET_SCHEDULER_get_task_context ();
596 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || 599 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
597 (GNUNET_OK != 600 (GNUNET_OK !=
598 GNUNET_NETWORK_socket_getsockopt(ap->sock, 601 GNUNET_NETWORK_socket_getsockopt (ap->sock,
599 SOL_SOCKET, 602 SOL_SOCKET,
600 SO_ERROR, 603 SO_ERROR,
601 &error, 604 &error,
602 &len)) || 605 &len)) ||
603 (0 != error)) 606 (0 != error))
604 { 607 {
605 GNUNET_break(GNUNET_OK == 608 GNUNET_break (GNUNET_OK ==
606 GNUNET_NETWORK_socket_close(ap->sock)); 609 GNUNET_NETWORK_socket_close (ap->sock));
607 GNUNET_free(ap); 610 GNUNET_free (ap);
608 if ((NULL == cstate->ap_head) && 611 if ((NULL == cstate->ap_head) &&
609 // (NULL == cstate->proxy_handshake) && 612 // (NULL == cstate->proxy_handshake) &&
610 (NULL == cstate->dns_active)) 613 (NULL == cstate->dns_active))
611 connect_fail_continuation(cstate); 614 connect_fail_continuation (cstate);
612 return; 615 return;
613 } 616 }
614 LOG(GNUNET_ERROR_TYPE_DEBUG, 617 LOG (GNUNET_ERROR_TYPE_DEBUG,
615 "Connection to `%s' succeeded!\n", 618 "Connection to `%s' succeeded!\n",
616 cstate->service_name); 619 cstate->service_name);
617 /* trigger jobs that waited for the connection */ 620 /* trigger jobs that waited for the connection */
618 GNUNET_assert(NULL == cstate->sock); 621 GNUNET_assert (NULL == cstate->sock);
619 cstate->sock = ap->sock; 622 cstate->sock = ap->sock;
620 GNUNET_free(ap); 623 GNUNET_free (ap);
621 cancel_aps(cstate); 624 cancel_aps (cstate);
622 connect_success_continuation(cstate); 625 connect_success_continuation (cstate);
623} 626}
624 627
625 628
@@ -632,82 +635,82 @@ connect_probe_continuation(void *cls)
632 * @param addrlen length of @a addr 635 * @param addrlen length of @a addr
633 */ 636 */
634static void 637static void
635try_connect_using_address(void *cls, 638try_connect_using_address (void *cls,
636 const struct sockaddr *addr, 639 const struct sockaddr *addr,
637 socklen_t addrlen) 640 socklen_t addrlen)
638{ 641{
639 struct ClientState *cstate = cls; 642 struct ClientState *cstate = cls;
640 struct AddressProbe *ap; 643 struct AddressProbe *ap;
641 644
642 if (NULL == addr) 645 if (NULL == addr)
643 { 646 {
644 cstate->dns_active = NULL; 647 cstate->dns_active = NULL;
645 if ((NULL == cstate->ap_head) && 648 if ((NULL == cstate->ap_head) &&
646 // (NULL == cstate->proxy_handshake) && 649 // (NULL == cstate->proxy_handshake) &&
647 (NULL == cstate->sock)) 650 (NULL == cstate->sock))
648 connect_fail_continuation(cstate); 651 connect_fail_continuation (cstate);
649 return; 652 return;
650 } 653 }
651 if (NULL != cstate->sock) 654 if (NULL != cstate->sock)
652 return; /* already connected */ 655 return; /* already connected */
653 /* try to connect */ 656 /* try to connect */
654 LOG(GNUNET_ERROR_TYPE_DEBUG, 657 LOG (GNUNET_ERROR_TYPE_DEBUG,
655 "Trying to connect using address `%s:%u'\n", 658 "Trying to connect using address `%s:%u'\n",
656 GNUNET_a2s(addr, 659 GNUNET_a2s (addr,
657 addrlen), 660 addrlen),
658 cstate->port); 661 cstate->port);
659 ap = GNUNET_malloc(sizeof(struct AddressProbe) + addrlen); 662 ap = GNUNET_malloc (sizeof(struct AddressProbe) + addrlen);
660 ap->addr = (const struct sockaddr *)&ap[1]; 663 ap->addr = (const struct sockaddr *) &ap[1];
661 GNUNET_memcpy(&ap[1], 664 GNUNET_memcpy (&ap[1],
662 addr, 665 addr,
663 addrlen); 666 addrlen);
664 ap->addrlen = addrlen; 667 ap->addrlen = addrlen;
665 ap->cstate = cstate; 668 ap->cstate = cstate;
666 669
667 switch (ap->addr->sa_family) 670 switch (ap->addr->sa_family)
668 { 671 {
669 case AF_INET: 672 case AF_INET:
670 ((struct sockaddr_in *)ap->addr)->sin_port = htons(cstate->port); 673 ((struct sockaddr_in *) ap->addr)->sin_port = htons (cstate->port);
671 break; 674 break;
672 675
673 case AF_INET6: 676 case AF_INET6:
674 ((struct sockaddr_in6 *)ap->addr)->sin6_port = htons(cstate->port); 677 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (cstate->port);
675 break; 678 break;
676 679
677 default: 680 default:
678 GNUNET_break(0); 681 GNUNET_break (0);
679 GNUNET_free(ap); 682 GNUNET_free (ap);
680 return; /* not supported by us */ 683 return; /* not supported by us */
681 } 684 }
682 ap->sock = GNUNET_NETWORK_socket_create(ap->addr->sa_family, 685 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family,
683 SOCK_STREAM, 686 SOCK_STREAM,
684 0); 687 0);
685 if (NULL == ap->sock) 688 if (NULL == ap->sock)
686 { 689 {
687 GNUNET_free(ap); 690 GNUNET_free (ap);
688 return; /* not supported by OS */ 691 return; /* not supported by OS */
689 } 692 }
690 if ((GNUNET_OK != 693 if ((GNUNET_OK !=
691 GNUNET_NETWORK_socket_connect(ap->sock, 694 GNUNET_NETWORK_socket_connect (ap->sock,
692 ap->addr, 695 ap->addr,
693 ap->addrlen)) && 696 ap->addrlen)) &&
694 (EINPROGRESS != errno)) 697 (EINPROGRESS != errno))
695 { 698 {
696 /* maybe refused / unsupported address, try next */ 699 /* maybe refused / unsupported address, try next */
697 GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO, 700 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
698 "connect"); 701 "connect");
699 GNUNET_break(GNUNET_OK == 702 GNUNET_break (GNUNET_OK ==
700 GNUNET_NETWORK_socket_close(ap->sock)); 703 GNUNET_NETWORK_socket_close (ap->sock));
701 GNUNET_free(ap); 704 GNUNET_free (ap);
702 return; 705 return;
703 } 706 }
704 GNUNET_CONTAINER_DLL_insert(cstate->ap_head, 707 GNUNET_CONTAINER_DLL_insert (cstate->ap_head,
705 cstate->ap_tail, 708 cstate->ap_tail,
706 ap); 709 ap);
707 ap->task = GNUNET_SCHEDULER_add_write_net(CONNECT_RETRY_TIMEOUT, 710 ap->task = GNUNET_SCHEDULER_add_write_net (CONNECT_RETRY_TIMEOUT,
708 ap->sock, 711 ap->sock,
709 &connect_probe_continuation, 712 &connect_probe_continuation,
710 ap); 713 ap);
711} 714}
712 715
713 716
@@ -720,8 +723,8 @@ try_connect_using_address(void *cls,
720 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not 723 * @return #GNUNET_OK if the configuration is valid, #GNUNET_SYSERR if not
721 */ 724 */
722static int 725static int
723test_service_configuration(const char *service_name, 726test_service_configuration (const char *service_name,
724 const struct GNUNET_CONFIGURATION_Handle *cfg) 727 const struct GNUNET_CONFIGURATION_Handle *cfg)
725{ 728{
726 int ret = GNUNET_SYSERR; 729 int ret = GNUNET_SYSERR;
727 char *hostname = NULL; 730 char *hostname = NULL;
@@ -731,45 +734,45 @@ test_service_configuration(const char *service_name,
731 char *unixpath = NULL; 734 char *unixpath = NULL;
732 735
733 if ((GNUNET_OK == 736 if ((GNUNET_OK ==
734 GNUNET_CONFIGURATION_get_value_filename(cfg, 737 GNUNET_CONFIGURATION_get_value_filename (cfg,
735 service_name, 738 service_name,
736 "UNIXPATH", 739 "UNIXPATH",
737 &unixpath)) && 740 &unixpath)) &&
738 (0 < strlen(unixpath))) 741 (0 < strlen (unixpath)))
739 ret = GNUNET_OK; 742 ret = GNUNET_OK;
740 else if ((GNUNET_OK == 743 else if ((GNUNET_OK ==
741 GNUNET_CONFIGURATION_have_value(cfg, 744 GNUNET_CONFIGURATION_have_value (cfg,
742 service_name, 745 service_name,
743 "UNIXPATH"))) 746 "UNIXPATH")))
744 { 747 {
745 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_ERROR, 748 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
746 service_name, 749 service_name,
747 "UNIXPATH", 750 "UNIXPATH",
748 _("not a valid filename")); 751 _ ("not a valid filename"));
749 return GNUNET_SYSERR; /* UNIXPATH specified but invalid! */ 752 return GNUNET_SYSERR; /* UNIXPATH specified but invalid! */
750 } 753 }
751 GNUNET_free_non_null(unixpath); 754 GNUNET_free_non_null (unixpath);
752#endif 755#endif
753 756
754 if ((GNUNET_YES == 757 if ((GNUNET_YES ==
755 GNUNET_CONFIGURATION_have_value(cfg, 758 GNUNET_CONFIGURATION_have_value (cfg,
756 service_name, 759 service_name,
757 "PORT")) && 760 "PORT")) &&
758 (GNUNET_OK == 761 (GNUNET_OK ==
759 GNUNET_CONFIGURATION_get_value_number(cfg, 762 GNUNET_CONFIGURATION_get_value_number (cfg,
760 service_name, 763 service_name,
761 "PORT", 764 "PORT",
762 &port)) && 765 &port)) &&
763 (port <= 65535) && 766 (port <= 65535) &&
764 (0 != port) && 767 (0 != port) &&
765 (GNUNET_OK == 768 (GNUNET_OK ==
766 GNUNET_CONFIGURATION_get_value_string(cfg, 769 GNUNET_CONFIGURATION_get_value_string (cfg,
767 service_name, 770 service_name,
768 "HOSTNAME", 771 "HOSTNAME",
769 &hostname)) && 772 &hostname)) &&
770 (0 != strlen(hostname))) 773 (0 != strlen (hostname)))
771 ret = GNUNET_OK; 774 ret = GNUNET_OK;
772 GNUNET_free_non_null(hostname); 775 GNUNET_free_non_null (hostname);
773 return ret; 776 return ret;
774} 777}
775 778
@@ -780,7 +783,7 @@ test_service_configuration(const char *service_name,
780 * @param cls the `struct ClientState` to try to connect to the service 783 * @param cls the `struct ClientState` to try to connect to the service
781 */ 784 */
782static void 785static void
783start_connect(void *cls) 786start_connect (void *cls)
784{ 787{
785 struct ClientState *cstate = cls; 788 struct ClientState *cstate = cls;
786 789
@@ -788,41 +791,41 @@ start_connect(void *cls)
788#if 0 791#if 0
789 /* Never use a local source if a proxy is configured */ 792 /* Never use a local source if a proxy is configured */
790 if (GNUNET_YES == 793 if (GNUNET_YES ==
791 GNUNET_SOCKS_check_service(cstate->service_name, 794 GNUNET_SOCKS_check_service (cstate->service_name,
792 cstate->cfg)) 795 cstate->cfg))
793 { 796 {
794 socks_connect(cstate); 797 socks_connect (cstate);
795 return; 798 return;
796 } 799 }
797#endif 800#endif
798 801
799 if ((0 == (cstate->attempts++ % 2)) || 802 if ((0 == (cstate->attempts++ % 2)) ||
800 (0 == cstate->port) || 803 (0 == cstate->port) ||
801 (NULL == cstate->hostname)) 804 (NULL == cstate->hostname))
805 {
806 /* on even rounds, try UNIX first, or always
807 if we do not have a DNS name and TCP port. */
808 cstate->sock = try_unixpath (cstate->service_name,
809 cstate->cfg);
810 if (NULL != cstate->sock)
802 { 811 {
803 /* on even rounds, try UNIX first, or always 812 connect_success_continuation (cstate);
804 if we do not have a DNS name and TCP port. */ 813 return;
805 cstate->sock = try_unixpath(cstate->service_name,
806 cstate->cfg);
807 if (NULL != cstate->sock)
808 {
809 connect_success_continuation(cstate);
810 return;
811 }
812 } 814 }
815 }
813 if ((NULL == cstate->hostname) || 816 if ((NULL == cstate->hostname) ||
814 (0 == cstate->port)) 817 (0 == cstate->port))
815 { 818 {
816 /* All options failed. Boo! */ 819 /* All options failed. Boo! */
817 connect_fail_continuation(cstate); 820 connect_fail_continuation (cstate);
818 return; 821 return;
819 } 822 }
820 cstate->dns_active 823 cstate->dns_active
821 = GNUNET_RESOLVER_ip_get(cstate->hostname, 824 = GNUNET_RESOLVER_ip_get (cstate->hostname,
822 AF_UNSPEC, 825 AF_UNSPEC,
823 CONNECT_RETRY_TIMEOUT, 826 CONNECT_RETRY_TIMEOUT,
824 &try_connect_using_address, 827 &try_connect_using_address,
825 cstate); 828 cstate);
826} 829}
827 830
828 831
@@ -834,30 +837,30 @@ start_connect(void *cls)
834 * @param impl_state our `struct ClientState` 837 * @param impl_state our `struct ClientState`
835 */ 838 */
836static void 839static void
837connection_client_send_impl(struct GNUNET_MQ_Handle *mq, 840connection_client_send_impl (struct GNUNET_MQ_Handle *mq,
838 const struct GNUNET_MessageHeader *msg, 841 const struct GNUNET_MessageHeader *msg,
839 void *impl_state) 842 void *impl_state)
840{ 843{
841 struct ClientState *cstate = impl_state; 844 struct ClientState *cstate = impl_state;
842 845
843 (void)mq; 846 (void) mq;
844 /* only one message at a time allowed */ 847 /* only one message at a time allowed */
845 GNUNET_assert(NULL == cstate->msg); 848 GNUNET_assert (NULL == cstate->msg);
846 GNUNET_assert(NULL == cstate->send_task); 849 GNUNET_assert (NULL == cstate->send_task);
847 cstate->msg = msg; 850 cstate->msg = msg;
848 cstate->msg_off = 0; 851 cstate->msg_off = 0;
849 if (NULL == cstate->sock) 852 if (NULL == cstate->sock)
850 { 853 {
851 LOG(GNUNET_ERROR_TYPE_DEBUG, 854 LOG (GNUNET_ERROR_TYPE_DEBUG,
852 "message of type %u waiting for socket\n", 855 "message of type %u waiting for socket\n",
853 ntohs(msg->type)); 856 ntohs (msg->type));
854 return; /* still waiting for connection */ 857 return; /* still waiting for connection */
855 } 858 }
856 cstate->send_task 859 cstate->send_task
857 = GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_FOREVER_REL, 860 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
858 cstate->sock, 861 cstate->sock,
859 &transmit_ready, 862 &transmit_ready,
860 cstate); 863 cstate);
861} 864}
862 865
863 866
@@ -868,20 +871,20 @@ connection_client_send_impl(struct GNUNET_MQ_Handle *mq,
868 * @param impl_state our `struct ClientState` 871 * @param impl_state our `struct ClientState`
869 */ 872 */
870static void 873static void
871connection_client_cancel_impl(struct GNUNET_MQ_Handle *mq, 874connection_client_cancel_impl (struct GNUNET_MQ_Handle *mq,
872 void *impl_state) 875 void *impl_state)
873{ 876{
874 struct ClientState *cstate = impl_state; 877 struct ClientState *cstate = impl_state;
875 878
876 (void)mq; 879 (void) mq;
877 GNUNET_assert(NULL != cstate->msg); 880 GNUNET_assert (NULL != cstate->msg);
878 GNUNET_assert(0 == cstate->msg_off); 881 GNUNET_assert (0 == cstate->msg_off);
879 cstate->msg = NULL; 882 cstate->msg = NULL;
880 if (NULL != cstate->send_task) 883 if (NULL != cstate->send_task)
881 { 884 {
882 GNUNET_SCHEDULER_cancel(cstate->send_task); 885 GNUNET_SCHEDULER_cancel (cstate->send_task);
883 cstate->send_task = NULL; 886 cstate->send_task = NULL;
884 } 887 }
885} 888}
886 889
887 890
@@ -897,57 +900,57 @@ connection_client_cancel_impl(struct GNUNET_MQ_Handle *mq,
897 * @return the message queue, NULL on error 900 * @return the message queue, NULL on error
898 */ 901 */
899struct GNUNET_MQ_Handle * 902struct GNUNET_MQ_Handle *
900GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, 903GNUNET_CLIENT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
901 const char *service_name, 904 const char *service_name,
902 const struct GNUNET_MQ_MessageHandler *handlers, 905 const struct GNUNET_MQ_MessageHandler *handlers,
903 GNUNET_MQ_ErrorHandler error_handler, 906 GNUNET_MQ_ErrorHandler error_handler,
904 void *error_handler_cls) 907 void *error_handler_cls)
905{ 908{
906 struct ClientState *cstate; 909 struct ClientState *cstate;
907 910
908 if (GNUNET_OK != 911 if (GNUNET_OK !=
909 test_service_configuration(service_name, 912 test_service_configuration (service_name,
910 cfg)) 913 cfg))
911 return NULL; 914 return NULL;
912 cstate = GNUNET_new(struct ClientState); 915 cstate = GNUNET_new (struct ClientState);
913 cstate->service_name = GNUNET_strdup(service_name); 916 cstate->service_name = GNUNET_strdup (service_name);
914 cstate->cfg = cfg; 917 cstate->cfg = cfg;
915 cstate->retry_task = GNUNET_SCHEDULER_add_now(&start_connect, 918 cstate->retry_task = GNUNET_SCHEDULER_add_now (&start_connect,
916 cstate); 919 cstate);
917 cstate->mst = GNUNET_MST_create(&recv_message, 920 cstate->mst = GNUNET_MST_create (&recv_message,
918 cstate); 921 cstate);
919 if (GNUNET_YES == 922 if (GNUNET_YES ==
920 GNUNET_CONFIGURATION_have_value(cfg, 923 GNUNET_CONFIGURATION_have_value (cfg,
921 service_name, 924 service_name,
922 "PORT")) 925 "PORT"))
923 { 926 {
924 if (!((GNUNET_OK != 927 if (! ((GNUNET_OK !=
925 GNUNET_CONFIGURATION_get_value_number(cfg, 928 GNUNET_CONFIGURATION_get_value_number (cfg,
926 service_name, 929 service_name,
927 "PORT", 930 "PORT",
928 &cstate->port)) || 931 &cstate->port)) ||
929 (cstate->port > 65535) || 932 (cstate->port > 65535) ||
930 (GNUNET_OK != 933 (GNUNET_OK !=
931 GNUNET_CONFIGURATION_get_value_string(cfg, 934 GNUNET_CONFIGURATION_get_value_string (cfg,
932 service_name, 935 service_name,
933 "HOSTNAME", 936 "HOSTNAME",
934 &cstate->hostname))) && 937 &cstate->hostname))) &&
935 (0 == strlen(cstate->hostname))) 938 (0 == strlen (cstate->hostname)))
936 { 939 {
937 GNUNET_free(cstate->hostname); 940 GNUNET_free (cstate->hostname);
938 cstate->hostname = NULL; 941 cstate->hostname = NULL;
939 LOG(GNUNET_ERROR_TYPE_WARNING, 942 LOG (GNUNET_ERROR_TYPE_WARNING,
940 _("Need a non-empty hostname for service `%s'.\n"), 943 _ ("Need a non-empty hostname for service `%s'.\n"),
941 service_name); 944 service_name);
942 }
943 } 945 }
944 cstate->mq = GNUNET_MQ_queue_for_callbacks(&connection_client_send_impl, 946 }
945 &connection_client_destroy_impl, 947 cstate->mq = GNUNET_MQ_queue_for_callbacks (&connection_client_send_impl,
946 &connection_client_cancel_impl, 948 &connection_client_destroy_impl,
947 cstate, 949 &connection_client_cancel_impl,
948 handlers, 950 cstate,
949 error_handler, 951 handlers,
950 error_handler_cls); 952 error_handler,
953 error_handler_cls);
951 return cstate->mq; 954 return cstate->mq;
952} 955}
953 956