aboutsummaryrefslogtreecommitdiff
path: root/src/util/connection.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/connection.c')
-rw-r--r--src/util/connection.c1086
1 files changed, 545 insertions, 541 deletions
diff --git a/src/util/connection.c b/src/util/connection.c
index 6b2cf75e0..4beafa3f3 100644
--- a/src/util/connection.c
+++ b/src/util/connection.c
@@ -41,6 +41,10 @@
41 41
42#define DEBUG_CONNECTION GNUNET_EXTRA_LOGGING 42#define DEBUG_CONNECTION GNUNET_EXTRA_LOGGING
43 43
44#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
45
46#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
47
44/** 48/**
45 * Possible functions to call after connect failed or succeeded. 49 * Possible functions to call after connect failed or succeeded.
46 */ 50 */
@@ -329,7 +333,8 @@ GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock)
329 * @return the boxed socket handle 333 * @return the boxed socket handle
330 */ 334 */
331struct GNUNET_CONNECTION_Handle * 335struct GNUNET_CONNECTION_Handle *
332GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket) 336GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle
337 *osSocket)
333{ 338{
334 struct GNUNET_CONNECTION_Handle *ret; 339 struct GNUNET_CONNECTION_Handle *ret;
335 340
@@ -352,8 +357,8 @@ GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket)
352 */ 357 */
353struct GNUNET_CONNECTION_Handle * 358struct GNUNET_CONNECTION_Handle *
354GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, 359GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
355 void *access_cls, 360 void *access_cls,
356 struct GNUNET_NETWORK_Handle *lsock) 361 struct GNUNET_NETWORK_Handle *lsock)
357{ 362{
358 struct GNUNET_CONNECTION_Handle *ret; 363 struct GNUNET_CONNECTION_Handle *ret;
359 char addr[128]; 364 char addr[128];
@@ -374,94 +379,94 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
374 379
375 addrlen = sizeof (addr); 380 addrlen = sizeof (addr);
376 sock = 381 sock =
377 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen); 382 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
378 if (NULL == sock) 383 if (NULL == sock)
379 { 384 {
380 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept"); 385 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept");
381 return NULL; 386 return NULL;
382 } 387 }
383 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t))) 388 if ((addrlen > sizeof (addr)) || (addrlen < sizeof (sa_family_t)))
384 { 389 {
385 GNUNET_break (0); 390 GNUNET_break (0);
386 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); 391 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
387 return NULL; 392 return NULL;
388 } 393 }
389 394
390 sa = (struct sockaddr *) addr; 395 sa = (struct sockaddr *) addr;
391 v6 = (struct sockaddr_in6 *) addr; 396 v6 = (struct sockaddr_in6 *) addr;
392 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr))) 397 if ((sa->sa_family == AF_INET6) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
393 { 398 {
394 /* convert to V4 address */ 399 /* convert to V4 address */
395 v4 = GNUNET_malloc (sizeof (struct sockaddr_in)); 400 v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
396 memset (v4, 0, sizeof (struct sockaddr_in)); 401 memset (v4, 0, sizeof (struct sockaddr_in));
397 v4->sin_family = AF_INET; 402 v4->sin_family = AF_INET;
398#if HAVE_SOCKADDR_IN_SIN_LEN 403#if HAVE_SOCKADDR_IN_SIN_LEN
399 v4->sin_len = (u_char) sizeof (struct sockaddr_in); 404 v4->sin_len = (u_char) sizeof (struct sockaddr_in);
400#endif 405#endif
401 memcpy (&v4->sin_addr, 406 memcpy (&v4->sin_addr,
402 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) - 407 &((char *) &v6->sin6_addr)[sizeof (struct in6_addr) -
403 sizeof (struct in_addr)], 408 sizeof (struct in_addr)],
404 sizeof (struct in_addr)); 409 sizeof (struct in_addr));
405 v4->sin_port = v6->sin6_port; 410 v4->sin_port = v6->sin6_port;
406 uaddr = v4; 411 uaddr = v4;
407 addrlen = sizeof (struct sockaddr_in); 412 addrlen = sizeof (struct sockaddr_in);
408 } 413 }
409 else 414 else
410 { 415 {
411 uaddr = GNUNET_malloc (addrlen); 416 uaddr = GNUNET_malloc (addrlen);
412 memcpy (uaddr, addr, addrlen); 417 memcpy (uaddr, addr, addrlen);
413 } 418 }
414 gcp = NULL; 419 gcp = NULL;
415 gc.uid = 0; 420 gc.uid = 0;
416 gc.gid = 0; 421 gc.gid = 0;
417 if (sa->sa_family == AF_UNIX) 422 if (sa->sa_family == AF_UNIX)
418 { 423 {
419#if HAVE_GETPEEREID 424#if HAVE_GETPEEREID
420 /* most BSDs */ 425 /* most BSDs */
421 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid)) 426 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid))
422 gcp = &gc; 427 gcp = &gc;
423#else 428#else
424#ifdef SO_PEERCRED 429#ifdef SO_PEERCRED
425 /* largely traditional GNU/Linux */ 430 /* largely traditional GNU/Linux */
426 olen = sizeof (uc); 431 olen = sizeof (uc);
427 if ((0 == 432 if ((0 ==
428 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED, &uc, 433 getsockopt (GNUNET_NETWORK_get_fd (sock), SOL_SOCKET, SO_PEERCRED,
429 &olen)) && (olen == sizeof (uc))) 434 &uc, &olen)) && (olen == sizeof (uc)))
430 { 435 {
431 gc.uid = uc.uid; 436 gc.uid = uc.uid;
432 gc.gid = uc.gid; 437 gc.gid = uc.gid;
433 gcp = &gc; 438 gcp = &gc;
434 } 439 }
435#else 440#else
436#if HAVE_GETPEERUCRED 441#if HAVE_GETPEERUCRED
437 /* this is for Solaris 10 */ 442 /* this is for Solaris 10 */
438 ucred_t *uc; 443 ucred_t *uc;
439 444
440 uc = NULL; 445 uc = NULL;
441 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc)) 446 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
442 { 447 {
443 gc.uid = ucred_geteuid (uc); 448 gc.uid = ucred_geteuid (uc);
444 gc.gid = ucred_getegid (uc); 449 gc.gid = ucred_getegid (uc);
445 gcp = &gc; 450 gcp = &gc;
446 } 451 }
447 ucred_free (uc); 452 ucred_free (uc);
448#endif 453#endif
449#endif 454#endif
450#endif 455#endif
451 } 456 }
452 457
453 if ((access != NULL) && 458 if ((access != NULL) &&
454 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen)))) 459 (GNUNET_YES != (aret = access (access_cls, gcp, uaddr, addrlen))))
455 { 460 {
456 if (aret == GNUNET_NO) 461 if (aret == GNUNET_NO)
457 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"), 462 LOG (GNUNET_ERROR_TYPE_INFO, _("Access denied to `%s'\n"),
458 GNUNET_a2s (uaddr, addrlen)); 463 GNUNET_a2s (uaddr, addrlen));
459 GNUNET_break (GNUNET_OK == 464 GNUNET_break (GNUNET_OK ==
460 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR)); 465 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
461 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock)); 466 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
462 GNUNET_free (uaddr); 467 GNUNET_free (uaddr);
463 return NULL; 468 return NULL;
464 } 469 }
465 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 470 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
466 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 471 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
467 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size); 472 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size);
@@ -469,8 +474,8 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
469 ret->addrlen = addrlen; 474 ret->addrlen = addrlen;
470 ret->sock = sock; 475 ret->sock = sock;
471#if DEBUG_CONNECTION 476#if DEBUG_CONNECTION
472 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Accepting connection from `%s': %p\n"), 477 LOG (GNUNET_ERROR_TYPE_INFO, _("Accepting connection from `%s': %p\n"),
473 GNUNET_a2s (uaddr, addrlen), ret); 478 GNUNET_a2s (uaddr, addrlen), ret);
474#endif 479#endif
475 return ret; 480 return ret;
476} 481}
@@ -485,7 +490,7 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
485 */ 490 */
486int 491int
487GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock, 492GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock,
488 void **addr, size_t * addrlen) 493 void **addr, size_t * addrlen)
489{ 494{
490 if ((sock->addr == NULL) || (sock->addrlen == 0)) 495 if ((sock->addr == NULL) || (sock->addrlen == 0))
491 return GNUNET_NO; 496 return GNUNET_NO;
@@ -518,7 +523,8 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
518 * @param tc unused 523 * @param tc unused
519 */ 524 */
520static void 525static void
521destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 526destroy_continuation (void *cls,
527 const struct GNUNET_SCHEDULER_TaskContext *tc)
522{ 528{
523 struct GNUNET_CONNECTION_Handle *sock = cls; 529 struct GNUNET_CONNECTION_Handle *sock = cls;
524 GNUNET_CONNECTION_TransmitReadyNotify notify; 530 GNUNET_CONNECTION_TransmitReadyNotify notify;
@@ -527,83 +533,83 @@ destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
527 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK; 533 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK;
528 GNUNET_assert (sock->dns_active == NULL); 534 GNUNET_assert (sock->dns_active == NULL);
529 if (0 != (sock->ccs & COCO_TRANSMIT_READY)) 535 if (0 != (sock->ccs & COCO_TRANSMIT_READY))
530 { 536 {
531#if DEBUG_CONNECTION 537#if DEBUG_CONNECTION
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 538 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 "Destroy waits for CCS-TR to be done (%p)\n", sock); 539 "Destroy waits for CCS-TR to be done (%p)\n", sock);
534#endif 540#endif
535 sock->ccs |= COCO_DESTROY_CONTINUATION; 541 sock->ccs |= COCO_DESTROY_CONTINUATION;
536 return; 542 return;
537 } 543 }
538 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 544 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
539 { 545 {
540#if DEBUG_CONNECTION 546#if DEBUG_CONNECTION
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 547 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "Destroy waits for write_task to be done (%p)\n", sock); 548 "Destroy waits for write_task to be done (%p)\n", sock);
543#endif 549#endif
544 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 550 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
545 sock->destroy_task = 551 sock->destroy_task =
546 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation, 552 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation,
547 sock); 553 sock);
548 return; 554 return;
549 } 555 }
550 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN)) 556 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN))
551 { 557 {
552 sock->ccs |= COCO_DESTROY_CONTINUATION; 558 sock->ccs |= COCO_DESTROY_CONTINUATION;
553 return; 559 return;
554 } 560 }
555 if (sock->sock != NULL) 561 if (sock->sock != NULL)
556 { 562 {
557#if DEBUG_CONNECTION 563#if DEBUG_CONNECTION
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock); 564 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock);
559#endif 565#endif
560 if (sock->persist != GNUNET_YES) 566 if (sock->persist != GNUNET_YES)
561 { 567 {
562 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) 568 if ((GNUNET_YES !=
563 && (errno != ENOTCONN) && (errno != ECONNRESET)) 569 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR))
564 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "shutdown"); 570 && (errno != ENOTCONN) && (errno != ECONNRESET))
571 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
572 }
565 } 573 }
566 }
567 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 574 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
568 { 575 {
569 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 576 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
570 sock->destroy_task = 577 sock->destroy_task =
571 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation, 578 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation,
572 sock); 579 sock);
573 return; 580 return;
574 } 581 }
575#if DEBUG_CONNECTION 582#if DEBUG_CONNECTION
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock); 583 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock);
577#endif 584#endif
578 while (NULL != (pos = sock->ap_head)) 585 while (NULL != (pos = sock->ap_head))
579 { 586 {
580 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); 587 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
581 GNUNET_SCHEDULER_cancel (pos->task); 588 GNUNET_SCHEDULER_cancel (pos->task);
582 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos); 589 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos);
583 GNUNET_free (pos); 590 GNUNET_free (pos);
584 } 591 }
585 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 592 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
586 GNUNET_assert (sock->ccs == COCO_NONE); 593 GNUNET_assert (sock->ccs == COCO_NONE);
587 if (NULL != (notify = sock->nth.notify_ready)) 594 if (NULL != (notify = sock->nth.notify_ready))
588 { 595 {
589 sock->nth.notify_ready = NULL; 596 sock->nth.notify_ready = NULL;
590 notify (sock->nth.notify_ready_cls, 0, NULL); 597 notify (sock->nth.notify_ready_cls, 0, NULL);
591 } 598 }
592 599
593 if (sock->sock != NULL) 600 if (sock->sock != NULL)
594 { 601 {
595 if (sock->persist != GNUNET_YES) 602 if (sock->persist != GNUNET_YES)
596 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); 603 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
597 else 604 else
598 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately 605 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately
599 * leak the socket in this special case) ... */ 606 * leak the socket in this special case) ... */
600 } 607 }
601 GNUNET_free_non_null (sock->addr); 608 GNUNET_free_non_null (sock->addr);
602 GNUNET_free_non_null (sock->hostname); 609 GNUNET_free_non_null (sock->hostname);
603 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 610 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
604#if DEBUG_CONNECTION 611#if DEBUG_CONNECTION
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", 612 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", sock);
606 sock);
607#endif 613#endif
608 GNUNET_free (sock->write_buffer); 614 GNUNET_free (sock->write_buffer);
609 GNUNET_free (sock); 615 GNUNET_free (sock);
@@ -632,13 +638,13 @@ static void
632connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h) 638connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
633{ 639{
634#if DEBUG_CONNECTION 640#if DEBUG_CONNECTION
635 GNUNET_log ((0 != 641 LOG ((0 !=
636 strncmp (h->hostname, "localhost:", 642 strncmp (h->hostname, "localhost:",
637 10)) ? GNUNET_ERROR_TYPE_INFO : 643 10)) ? GNUNET_ERROR_TYPE_INFO :
638 GNUNET_ERROR_TYPE_WARNING, 644 GNUNET_ERROR_TYPE_WARNING,
639 _ 645 _
640 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"), 646 ("Failed to establish TCP connection to `%s:%u', no further addresses to try.\n"),
641 h->hostname, h->port); 647 h->hostname, h->port);
642#endif 648#endif
643 /* connect failed / timed out */ 649 /* connect failed / timed out */
644 GNUNET_break (h->ap_head == NULL); 650 GNUNET_break (h->ap_head == NULL);
@@ -648,40 +654,39 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
648 654
649 /* trigger jobs that used to wait on "connect_task" */ 655 /* trigger jobs that used to wait on "connect_task" */
650 if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) 656 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
651 { 657 {
652#if DEBUG_CONNECTION 658#if DEBUG_CONNECTION
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 659 LOG (GNUNET_ERROR_TYPE_DEBUG,
654 "connect_fail_continuation triggers receive_again (%p)\n", h); 660 "connect_fail_continuation triggers receive_again (%p)\n", h);
655#endif 661#endif
656 h->ccs -= COCO_RECEIVE_AGAIN; 662 h->ccs -= COCO_RECEIVE_AGAIN;
657 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); 663 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
658 } 664 }
659 if (0 != (h->ccs & COCO_TRANSMIT_READY)) 665 if (0 != (h->ccs & COCO_TRANSMIT_READY))
660 { 666 {
661#if DEBUG_CONNECTION 667#if DEBUG_CONNECTION
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 668 LOG (GNUNET_ERROR_TYPE_DEBUG,
663 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n", 669 "connect_fail_continuation cancels timeout_task, triggers transmit_ready (%p)\n",
664 h); 670 h);
665#endif 671#endif
666 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 672 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
667 GNUNET_SCHEDULER_cancel (h->nth.timeout_task); 673 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
668 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 674 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
669 h->ccs -= COCO_TRANSMIT_READY; 675 h->ccs -= COCO_TRANSMIT_READY;
670 GNUNET_assert (h->nth.notify_ready != NULL); 676 GNUNET_assert (h->nth.notify_ready != NULL);
671 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); 677 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
672 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h); 678 h->write_task = GNUNET_SCHEDULER_add_now (&transmit_ready, h);
673 } 679 }
674 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) 680 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
675 { 681 {
676#if DEBUG_CONNECTION 682#if DEBUG_CONNECTION
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 683 LOG (GNUNET_ERROR_TYPE_DEBUG,
678 "connect_fail_continuation runs destroy_continuation (%p)\n", 684 "connect_fail_continuation runs destroy_continuation (%p)\n", h);
679 h);
680#endif 685#endif
681 h->ccs -= COCO_DESTROY_CONTINUATION; 686 h->ccs -= COCO_DESTROY_CONTINUATION;
682 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); 687 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
683 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); 688 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
684 } 689 }
685} 690}
686 691
687 692
@@ -694,48 +699,48 @@ static void
694connect_success_continuation (struct GNUNET_CONNECTION_Handle *h) 699connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
695{ 700{
696#if DEBUG_CONNECTION 701#if DEBUG_CONNECTION
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n", 702 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
698 GNUNET_a2s (h->addr, h->addrlen), h); 703 GNUNET_a2s (h->addr, h->addrlen), h);
699#endif 704#endif
700 /* trigger jobs that waited for the connection */ 705 /* trigger jobs that waited for the connection */
701 if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) 706 if (0 != (h->ccs & COCO_RECEIVE_AGAIN))
702 { 707 {
703#if DEBUG_CONNECTION 708#if DEBUG_CONNECTION
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 709 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "connect_success_continuation runs receive_again (%p)\n", h); 710 "connect_success_continuation runs receive_again (%p)\n", h);
706#endif 711#endif
707 h->ccs -= COCO_RECEIVE_AGAIN; 712 h->ccs -= COCO_RECEIVE_AGAIN;
708 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); 713 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h);
709 } 714 }
710 if (0 != (h->ccs & COCO_TRANSMIT_READY)) 715 if (0 != (h->ccs & COCO_TRANSMIT_READY))
711 { 716 {
712#if DEBUG_CONNECTION 717#if DEBUG_CONNECTION
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 718 LOG (GNUNET_ERROR_TYPE_DEBUG,
714 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n", 719 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
715 h); 720 h);
716#endif 721#endif
717 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 722 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
718 GNUNET_SCHEDULER_cancel (h->nth.timeout_task); 723 GNUNET_SCHEDULER_cancel (h->nth.timeout_task);
719 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 724 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
720 h->ccs -= COCO_TRANSMIT_READY; 725 h->ccs -= COCO_TRANSMIT_READY;
721 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); 726 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK);
722 GNUNET_assert (h->nth.notify_ready != NULL); 727 GNUNET_assert (h->nth.notify_ready != NULL);
723 h->write_task = 728 h->write_task =
724 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 729 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
725 (h->nth.transmit_timeout), h->sock, 730 (h->nth.transmit_timeout), h->sock,
726 &transmit_ready, h); 731 &transmit_ready, h);
727 } 732 }
728 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) 733 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION))
729 { 734 {
730#if DEBUG_CONNECTION 735#if DEBUG_CONNECTION
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 736 LOG (GNUNET_ERROR_TYPE_DEBUG,
732 "connect_success_continuation runs destroy_continuation (%p)\n", 737 "connect_success_continuation runs destroy_continuation (%p)\n",
733 h); 738 h);
734#endif 739#endif
735 h->ccs -= COCO_DESTROY_CONTINUATION; 740 h->ccs -= COCO_DESTROY_CONTINUATION;
736 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); 741 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task);
737 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); 742 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h);
738 } 743 }
739} 744}
740 745
741 746
@@ -748,7 +753,7 @@ connect_success_continuation (struct GNUNET_CONNECTION_Handle *h)
748 */ 753 */
749static void 754static void
750connect_probe_continuation (void *cls, 755connect_probe_continuation (void *cls,
751 const struct GNUNET_SCHEDULER_TaskContext *tc) 756 const struct GNUNET_SCHEDULER_TaskContext *tc)
752{ 757{
753 struct AddressProbe *ap = cls; 758 struct AddressProbe *ap = cls;
754 struct GNUNET_CONNECTION_Handle *h = ap->h; 759 struct GNUNET_CONNECTION_Handle *h = ap->h;
@@ -763,15 +768,15 @@ connect_probe_continuation (void *cls,
763 error = 0; 768 error = 0;
764 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || 769 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
765 (GNUNET_OK != 770 (GNUNET_OK !=
766 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, &error, 771 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
767 &len)) || (error != 0)) 772 &error, &len)) || (error != 0))
768 { 773 {
769 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 774 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
770 GNUNET_free (ap); 775 GNUNET_free (ap);
771 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO)) 776 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO))
772 connect_fail_continuation (h); 777 connect_fail_continuation (h);
773 return; 778 return;
774 } 779 }
775 GNUNET_assert (h->sock == NULL); 780 GNUNET_assert (h->sock == NULL);
776 h->sock = ap->sock; 781 h->sock = ap->sock;
777 GNUNET_assert (h->addr == NULL); 782 GNUNET_assert (h->addr == NULL);
@@ -781,12 +786,12 @@ connect_probe_continuation (void *cls,
781 GNUNET_free (ap); 786 GNUNET_free (ap);
782 /* cancel all other attempts */ 787 /* cancel all other attempts */
783 while (NULL != (pos = h->ap_head)) 788 while (NULL != (pos = h->ap_head))
784 { 789 {
785 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); 790 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
786 GNUNET_SCHEDULER_cancel (pos->task); 791 GNUNET_SCHEDULER_cancel (pos->task);
787 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos); 792 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos);
788 GNUNET_free (pos); 793 GNUNET_free (pos);
789 } 794 }
790 connect_success_continuation (h); 795 connect_success_continuation (h);
791} 796}
792 797
@@ -801,28 +806,27 @@ connect_probe_continuation (void *cls,
801 */ 806 */
802static void 807static void
803try_connect_using_address (void *cls, const struct sockaddr *addr, 808try_connect_using_address (void *cls, const struct sockaddr *addr,
804 socklen_t addrlen) 809 socklen_t addrlen)
805{ 810{
806 struct GNUNET_CONNECTION_Handle *h = cls; 811 struct GNUNET_CONNECTION_Handle *h = cls;
807 struct AddressProbe *ap; 812 struct AddressProbe *ap;
808 struct GNUNET_TIME_Relative delay; 813 struct GNUNET_TIME_Relative delay;
809 814
810 if (addr == NULL) 815 if (addr == NULL)
811 { 816 {
812 h->dns_active = NULL; 817 h->dns_active = NULL;
813 if ( (NULL == h->ap_head) && 818 if ((NULL == h->ap_head) && (NULL == h->sock))
814 (NULL == h->sock) ) 819 connect_fail_continuation (h);
815 connect_fail_continuation (h); 820 return;
816 return; 821 }
817 }
818 if (h->sock != NULL) 822 if (h->sock != NULL)
819 return; /* already connected */ 823 return; /* already connected */
820 GNUNET_assert (h->addr == NULL); 824 GNUNET_assert (h->addr == NULL);
821 /* try to connect */ 825 /* try to connect */
822#if DEBUG_CONNECTION 826#if DEBUG_CONNECTION
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 827 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, 828 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname,
825 h->port, GNUNET_a2s (addr, addrlen), h->port); 829 h->port, GNUNET_a2s (addr, addrlen), h->port);
826#endif 830#endif
827 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen); 831 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
828 ap->addr = (const struct sockaddr *) &ap[1]; 832 ap->addr = (const struct sockaddr *) &ap[1];
@@ -831,58 +835,59 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
831 ap->h = h; 835 ap->h = h;
832 836
833 switch (ap->addr->sa_family) 837 switch (ap->addr->sa_family)
834 { 838 {
835 case AF_INET: 839 case AF_INET:
836 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port); 840 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port);
837 break; 841 break;
838 case AF_INET6: 842 case AF_INET6:
839 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port); 843 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port);
840 break; 844 break;
841 default: 845 default:
842 GNUNET_break (0); 846 GNUNET_break (0);
843 GNUNET_free (ap); 847 GNUNET_free (ap);
844 return; /* not supported by us */ 848 return; /* not supported by us */
845 } 849 }
846 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0); 850 ap->sock =
851 GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
847 if (ap->sock == NULL) 852 if (ap->sock == NULL)
848 { 853 {
849 GNUNET_free (ap); 854 GNUNET_free (ap);
850 return; /* not supported by OS */ 855 return; /* not supported by OS */
851 } 856 }
852#if DEBUG_CONNECTION 857#if DEBUG_CONNECTION
853 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), 858 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
854 GNUNET_a2s (ap->addr, ap->addrlen), h); 859 GNUNET_a2s (ap->addr, ap->addrlen), h);
855#endif 860#endif
856 if ((GNUNET_OK != 861 if ((GNUNET_OK !=
857 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) && 862 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
858 (errno != EINPROGRESS)) 863 (errno != EINPROGRESS))
859 { 864 {
860 /* maybe refused / unsupported address, try next */ 865 /* maybe refused / unsupported address, try next */
861 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); 866 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
862#if 0 867#if 0
863 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 868 LOG (GNUNET_ERROR_TYPE_INFO,
864 _("Failed to connect to `%s' (%p)\n"), 869 _("Failed to connect to `%s' (%p)\n"),
865 GNUNET_a2s (ap->addr, ap->addrlen), h); 870 GNUNET_a2s (ap->addr, ap->addrlen), h);
866#endif 871#endif
867 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 872 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
868 GNUNET_free (ap); 873 GNUNET_free (ap);
869 return; 874 return;
870 } 875 }
871 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap); 876 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap);
872 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT; 877 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
873 if (h->nth.notify_ready != NULL) 878 if (h->nth.notify_ready != NULL)
874 delay = 879 delay =
875 GNUNET_TIME_relative_min (delay, 880 GNUNET_TIME_relative_min (delay,
876 GNUNET_TIME_absolute_get_remaining (h-> 881 GNUNET_TIME_absolute_get_remaining (h->
877 nth.transmit_timeout)); 882 nth.transmit_timeout));
878 if (h->receiver != NULL) 883 if (h->receiver != NULL)
879 delay = 884 delay =
880 GNUNET_TIME_relative_min (delay, 885 GNUNET_TIME_relative_min (delay,
881 GNUNET_TIME_absolute_get_remaining 886 GNUNET_TIME_absolute_get_remaining
882 (h->receive_timeout)); 887 (h->receive_timeout));
883 ap->task = 888 ap->task =
884 GNUNET_SCHEDULER_add_write_net (delay, ap->sock, 889 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
885 &connect_probe_continuation, ap); 890 &connect_probe_continuation, ap);
886} 891}
887 892
888 893
@@ -897,13 +902,13 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
897 * @return the socket handle 902 * @return the socket handle
898 */ 903 */
899struct GNUNET_CONNECTION_Handle * 904struct GNUNET_CONNECTION_Handle *
900GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle 905GNUNET_CONNECTION_create_from_connect (const struct
901 *cfg, const char *hostname, 906 GNUNET_CONFIGURATION_Handle *cfg,
902 uint16_t port) 907 const char *hostname, uint16_t port)
903{ 908{
904 struct GNUNET_CONNECTION_Handle *ret; 909 struct GNUNET_CONNECTION_Handle *ret;
905 910
906 GNUNET_assert (0 < strlen (hostname)); /* sanity check */ 911 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
907 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 912 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
908 ret->cfg = cfg; 913 ret->cfg = cfg;
909 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 914 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
@@ -911,9 +916,9 @@ GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
911 ret->port = port; 916 ret->port = port;
912 ret->hostname = GNUNET_strdup (hostname); 917 ret->hostname = GNUNET_strdup (hostname);
913 ret->dns_active = 918 ret->dns_active =
914 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC, 919 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC,
915 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT, 920 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
916 &try_connect_using_address, ret); 921 &try_connect_using_address, ret);
917 return ret; 922 return ret;
918} 923}
919 924
@@ -929,15 +934,15 @@ GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
929 */ 934 */
930struct GNUNET_CONNECTION_Handle * 935struct GNUNET_CONNECTION_Handle *
931GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct 936GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
932 GNUNET_CONFIGURATION_Handle 937 GNUNET_CONFIGURATION_Handle
933 *cfg, const char *unixpath) 938 *cfg, const char *unixpath)
934{ 939{
935#ifdef AF_UNIX 940#ifdef AF_UNIX
936 struct GNUNET_CONNECTION_Handle *ret; 941 struct GNUNET_CONNECTION_Handle *ret;
937 struct sockaddr_un *un; 942 struct sockaddr_un *un;
938 size_t slen; 943 size_t slen;
939 944
940 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */ 945 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
941 un = GNUNET_malloc (sizeof (struct sockaddr_un)); 946 un = GNUNET_malloc (sizeof (struct sockaddr_un));
942 un->sun_family = AF_UNIX; 947 un->sun_family = AF_UNIX;
943 slen = strlen (unixpath); 948 slen = strlen (unixpath);
@@ -962,20 +967,20 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
962 ret->addrlen = slen; 967 ret->addrlen = slen;
963 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); 968 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
964 if (NULL == ret->sock) 969 if (NULL == ret->sock)
965 { 970 {
966 GNUNET_free (ret->addr); 971 GNUNET_free (ret->addr);
967 GNUNET_free (ret->write_buffer); 972 GNUNET_free (ret->write_buffer);
968 GNUNET_free (ret); 973 GNUNET_free (ret);
969 return NULL; 974 return NULL;
970 } 975 }
971 if (GNUNET_OK != 976 if (GNUNET_OK !=
972 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen)) 977 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen))
973 { 978 {
974 /* Just return; we expect everything to work eventually so don't fail HARD */ 979 /* Just return; we expect everything to work eventually so don't fail HARD */
975 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock)); 980 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock));
976 ret->sock = NULL; 981 ret->sock = NULL;
977 return ret; 982 return ret;
978 } 983 }
979 connect_success_continuation (ret); 984 connect_success_continuation (ret);
980 return ret; 985 return ret;
981#else 986#else
@@ -996,8 +1001,8 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
996 */ 1001 */
997struct GNUNET_CONNECTION_Handle * 1002struct GNUNET_CONNECTION_Handle *
998GNUNET_CONNECTION_create_from_sockaddr (int af_family, 1003GNUNET_CONNECTION_create_from_sockaddr (int af_family,
999 const struct sockaddr *serv_addr, 1004 const struct sockaddr *serv_addr,
1000 socklen_t addrlen) 1005 socklen_t addrlen)
1001{ 1006{
1002 struct GNUNET_NETWORK_Handle *s; 1007 struct GNUNET_NETWORK_Handle *s;
1003 struct GNUNET_CONNECTION_Handle *ret; 1008 struct GNUNET_CONNECTION_Handle *ret;
@@ -1005,29 +1010,29 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
1005 1010
1006 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0); 1011 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
1007 if (s == NULL) 1012 if (s == NULL)
1008 { 1013 {
1009 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 1014 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1010 "socket"); 1015 "socket");
1011 return NULL; 1016 return NULL;
1012 } 1017 }
1013 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) && 1018 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
1014 (errno != EINPROGRESS)) 1019 (errno != EINPROGRESS))
1015 { 1020 {
1016 /* maybe refused / unsupported address, try next */ 1021 /* maybe refused / unsupported address, try next */
1017 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "connect"); 1022 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
1018 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1023 LOG (GNUNET_ERROR_TYPE_INFO,
1019 _("Attempt to connect to `%s' failed\n"), GNUNET_a2s (serv_addr, 1024 _("Attempt to connect to `%s' failed\n"), GNUNET_a2s (serv_addr,
1020 addrlen)); 1025 addrlen));
1021 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); 1026 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
1022 return NULL; 1027 return NULL;
1023 } 1028 }
1024 ret = GNUNET_CONNECTION_create_from_existing (s); 1029 ret = GNUNET_CONNECTION_create_from_existing (s);
1025 ret->addr = GNUNET_malloc (addrlen); 1030 ret->addr = GNUNET_malloc (addrlen);
1026 memcpy (ret->addr, serv_addr, addrlen); 1031 memcpy (ret->addr, serv_addr, addrlen);
1027 ret->addrlen = addrlen; 1032 ret->addrlen = addrlen;
1028#if DEBUG_CONNECTION 1033#if DEBUG_CONNECTION
1029 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), 1034 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
1030 GNUNET_a2s (serv_addr, addrlen), ret); 1035 GNUNET_a2s (serv_addr, addrlen), ret);
1031#endif 1036#endif
1032 return ret; 1037 return ret;
1033} 1038}
@@ -1045,7 +1050,7 @@ int
1045GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock) 1050GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1046{ 1051{
1047 if ((sock->ap_head != NULL) || (sock->dns_active != NULL)) 1052 if ((sock->ap_head != NULL) || (sock->dns_active != NULL))
1048 return GNUNET_YES; /* still trying to connect */ 1053 return GNUNET_YES; /* still trying to connect */
1049 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES; 1054 return (sock->sock == NULL) ? GNUNET_NO : GNUNET_YES;
1050} 1055}
1051 1056
@@ -1065,23 +1070,23 @@ GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1065 */ 1070 */
1066void 1071void
1067GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock, 1072GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock,
1068 int finish_pending_write) 1073 int finish_pending_write)
1069{ 1074{
1070 if (GNUNET_NO == finish_pending_write) 1075 if (GNUNET_NO == finish_pending_write)
1071 {
1072 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1073 { 1076 {
1074 GNUNET_SCHEDULER_cancel (sock->write_task); 1077 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1075 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1078 {
1076 sock->write_buffer_off = 0; 1079 GNUNET_SCHEDULER_cancel (sock->write_task);
1080 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1081 sock->write_buffer_off = 0;
1082 }
1083 sock->nth.notify_ready = NULL;
1077 } 1084 }
1078 sock->nth.notify_ready = NULL;
1079 }
1080 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL)) 1085 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL))
1081 { 1086 {
1082 GNUNET_RESOLVER_request_cancel (sock->dns_active); 1087 GNUNET_RESOLVER_request_cancel (sock->dns_active);
1083 sock->dns_active = NULL; 1088 sock->dns_active = NULL;
1084 } 1089 }
1085 1090
1086 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 1091 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task);
1087 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock); 1092 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock);
@@ -1097,8 +1102,8 @@ signal_timeout (struct GNUNET_CONNECTION_Handle *sh)
1097 GNUNET_CONNECTION_Receiver receiver; 1102 GNUNET_CONNECTION_Receiver receiver;
1098 1103
1099#if DEBUG_CONNECTION 1104#if DEBUG_CONNECTION
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1105 LOG (GNUNET_ERROR_TYPE_DEBUG,
1101 "Network signals time out to receiver (%p)!\n", sh); 1106 "Network signals time out to receiver (%p)!\n", sh);
1102#endif 1107#endif
1103 GNUNET_assert (NULL != (receiver = sh->receiver)); 1108 GNUNET_assert (NULL != (receiver = sh->receiver));
1104 sh->receiver = NULL; 1109 sh->receiver = NULL;
@@ -1136,63 +1141,62 @@ receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1136 sh->read_task = GNUNET_SCHEDULER_NO_TASK; 1141 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1137 if ((GNUNET_YES == sh->ignore_shutdown) && 1142 if ((GNUNET_YES == sh->ignore_shutdown) &&
1138 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1143 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1139 { 1144 {
1140 /* ignore shutdown request, go again immediately */ 1145 /* ignore shutdown request, go again immediately */
1141#if DEBUG_CONNECTION 1146#if DEBUG_CONNECTION
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1147 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "Ignoring shutdown signal per configuration\n"); 1148 "Ignoring shutdown signal per configuration\n");
1144#endif 1149#endif
1145 sh->read_task = 1150 sh->read_task =
1146 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1151 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1147 (sh->receive_timeout), sh->sock, 1152 (sh->receive_timeout), sh->sock,
1148 &receive_ready, sh); 1153 &receive_ready, sh);
1149 return; 1154 return;
1150 } 1155 }
1151 now = GNUNET_TIME_absolute_get (); 1156 now = GNUNET_TIME_absolute_get ();
1152 if ((now.abs_value > sh->receive_timeout.abs_value) || 1157 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1153 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) || 1158 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1154 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1159 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1155 { 1160 {
1156#if DEBUG_CONNECTION 1161#if DEBUG_CONNECTION
1157 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1162 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1163 LOG (GNUNET_ERROR_TYPE_DEBUG,
1159 "Receive from `%s' encounters error: time out by %llums... (%p)\n", 1164 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1160 GNUNET_a2s (sh->addr, sh->addrlen), 1165 GNUNET_a2s (sh->addr, sh->addrlen),
1161 GNUNET_TIME_absolute_get_duration (sh->receive_timeout). 1166 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).
1162 rel_value, sh); 1167 rel_value, sh);
1163#endif 1168#endif
1164 signal_timeout (sh); 1169 signal_timeout (sh);
1165 return; 1170 return;
1166 } 1171 }
1167 if (sh->sock == NULL) 1172 if (sh->sock == NULL)
1168 { 1173 {
1169 /* connect failed for good */ 1174 /* connect failed for good */
1170#if DEBUG_CONNECTION 1175#if DEBUG_CONNECTION
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1176 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Receive encounters error, socket closed... (%p)\n", sh); 1177 "Receive encounters error, socket closed... (%p)\n", sh);
1173#endif 1178#endif
1174 signal_error (sh, ECONNREFUSED); 1179 signal_error (sh, ECONNREFUSED);
1175 return; 1180 return;
1176 } 1181 }
1177 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock)); 1182 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock));
1178RETRY: 1183RETRY:
1179 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max); 1184 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max);
1180 if (ret == -1) 1185 if (ret == -1)
1181 { 1186 {
1182 if (errno == EINTR) 1187 if (errno == EINTR)
1183 goto RETRY; 1188 goto RETRY;
1184#if DEBUG_CONNECTION 1189#if DEBUG_CONNECTION
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", 1190 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n",
1186 STRERROR (errno)); 1191 STRERROR (errno));
1187#endif 1192#endif
1188 signal_error (sh, errno); 1193 signal_error (sh, errno);
1189 return; 1194 return;
1190 } 1195 }
1191#if DEBUG_CONNECTION 1196#if DEBUG_CONNECTION
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1193 "receive_ready read %u/%u bytes from `%s' (%p)!\n", 1198 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1194 (unsigned int) ret, sh->max, GNUNET_a2s (sh->addr, sh->addrlen), 1199 (unsigned int) ret, sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh);
1195 sh);
1196#endif 1200#endif
1197 GNUNET_assert (NULL != (receiver = sh->receiver)); 1201 GNUNET_assert (NULL != (receiver = sh->receiver));
1198 sh->receiver = NULL; 1202 sh->receiver = NULL;
@@ -1218,32 +1222,32 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1218 1222
1219 sh->read_task = GNUNET_SCHEDULER_NO_TASK; 1223 sh->read_task = GNUNET_SCHEDULER_NO_TASK;
1220 if (sh->sock == NULL) 1224 if (sh->sock == NULL)
1221 { 1225 {
1222 /* not connected and no longer trying */ 1226 /* not connected and no longer trying */
1223#if DEBUG_CONNECTION 1227#if DEBUG_CONNECTION
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "Receive encounters error, socket closed (%p)...\n", sh); 1229 "Receive encounters error, socket closed (%p)...\n", sh);
1226#endif 1230#endif
1227 signal_error (sh, ECONNREFUSED); 1231 signal_error (sh, ECONNREFUSED);
1228 return; 1232 return;
1229 } 1233 }
1230 now = GNUNET_TIME_absolute_get (); 1234 now = GNUNET_TIME_absolute_get ();
1231 if ((now.abs_value > sh->receive_timeout.abs_value) || 1235 if ((now.abs_value > sh->receive_timeout.abs_value) ||
1232 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1236 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1233 { 1237 {
1234#if DEBUG_CONNECTION 1238#if DEBUG_CONNECTION
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1236 "Receive encounters error: time out (%p)...\n", sh); 1240 "Receive encounters error: time out (%p)...\n", sh);
1237#endif 1241#endif
1238 signal_timeout (sh); 1242 signal_timeout (sh);
1239 return; 1243 return;
1240 } 1244 }
1241 GNUNET_assert (sh->sock != NULL); 1245 GNUNET_assert (sh->sock != NULL);
1242 /* connect succeeded, wait for data! */ 1246 /* connect succeeded, wait for data! */
1243 sh->read_task = 1247 sh->read_task =
1244 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1248 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1245 (sh->receive_timeout), sh->sock, 1249 (sh->receive_timeout), sh->sock,
1246 &receive_ready, sh); 1250 &receive_ready, sh);
1247} 1251}
1248 1252
1249 1253
@@ -1262,31 +1266,31 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1262 */ 1266 */
1263void 1267void
1264GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max, 1268GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1265 struct GNUNET_TIME_Relative timeout, 1269 struct GNUNET_TIME_Relative timeout,
1266 GNUNET_CONNECTION_Receiver receiver, 1270 GNUNET_CONNECTION_Receiver receiver,
1267 void *receiver_cls) 1271 void *receiver_cls)
1268{ 1272{
1269 struct GNUNET_SCHEDULER_TaskContext tc; 1273 struct GNUNET_SCHEDULER_TaskContext tc;
1270 1274
1271 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) && 1275 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1272 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) && 1276 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) &&
1273 (sock->receiver == NULL)); 1277 (sock->receiver == NULL));
1274 sock->receiver = receiver; 1278 sock->receiver = receiver;
1275 sock->receiver_cls = receiver_cls; 1279 sock->receiver_cls = receiver_cls;
1276 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); 1280 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1277 sock->max = max; 1281 sock->max = max;
1278 if (sock->sock != NULL) 1282 if (sock->sock != NULL)
1279 { 1283 {
1280 memset (&tc, 0, sizeof (tc)); 1284 memset (&tc, 0, sizeof (tc));
1281 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE; 1285 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1282 receive_again (sock, &tc); 1286 receive_again (sock, &tc);
1283 return; 1287 return;
1284 } 1288 }
1285 if ((sock->dns_active == NULL) && (sock->ap_head == NULL)) 1289 if ((sock->dns_active == NULL) && (sock->ap_head == NULL))
1286 { 1290 {
1287 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); 1291 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1288 return; 1292 return;
1289 } 1293 }
1290 sock->ccs += COCO_RECEIVE_AGAIN; 1294 sock->ccs += COCO_RECEIVE_AGAIN;
1291} 1295}
1292 1296
@@ -1299,7 +1303,7 @@ GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max,
1299 */ 1303 */
1300void 1304void
1301GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock, 1305GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock,
1302 int do_ignore) 1306 int do_ignore)
1303{ 1307{
1304 sock->ignore_shutdown = do_ignore; 1308 sock->ignore_shutdown = do_ignore;
1305} 1309}
@@ -1317,15 +1321,15 @@ void *
1317GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock) 1321GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1318{ 1322{
1319 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 1323 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1320 { 1324 {
1321 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task)); 1325 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task));
1322 sock->read_task = GNUNET_SCHEDULER_NO_TASK; 1326 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1323 } 1327 }
1324 else 1328 else
1325 { 1329 {
1326 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN)); 1330 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN));
1327 sock->ccs -= COCO_RECEIVE_AGAIN; 1331 sock->ccs -= COCO_RECEIVE_AGAIN;
1328 } 1332 }
1329 sock->receiver = NULL; 1333 sock->receiver = NULL;
1330 return sock->receiver_cls; 1334 return sock->receiver_cls;
1331} 1335}
@@ -1356,18 +1360,18 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
1356 return GNUNET_NO; 1360 return GNUNET_NO;
1357 sock->nth.notify_ready = NULL; 1361 sock->nth.notify_ready = NULL;
1358 if (sock->write_buffer_size - sock->write_buffer_off < size) 1362 if (sock->write_buffer_size - sock->write_buffer_off < size)
1359 { 1363 {
1360 /* need to compact */ 1364 /* need to compact */
1361 memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos], 1365 memmove (sock->write_buffer,
1362 used); 1366 &sock->write_buffer[sock->write_buffer_pos], used);
1363 sock->write_buffer_off -= sock->write_buffer_pos; 1367 sock->write_buffer_off -= sock->write_buffer_pos;
1364 sock->write_buffer_pos = 0; 1368 sock->write_buffer_pos = 0;
1365 } 1369 }
1366 avail = sock->write_buffer_size - sock->write_buffer_off; 1370 avail = sock->write_buffer_size - sock->write_buffer_off;
1367 GNUNET_assert (avail >= size); 1371 GNUNET_assert (avail >= size);
1368 size = 1372 size =
1369 notify (sock->nth.notify_ready_cls, avail, 1373 notify (sock->nth.notify_ready_cls, avail,
1370 &sock->write_buffer[sock->write_buffer_off]); 1374 &sock->write_buffer[sock->write_buffer_off]);
1371 GNUNET_assert (size <= avail); 1375 GNUNET_assert (size <= avail);
1372 sock->write_buffer_off += size; 1376 sock->write_buffer_off += size;
1373 return GNUNET_YES; 1377 return GNUNET_YES;
@@ -1392,17 +1396,17 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1392 GNUNET_CONNECTION_TransmitReadyNotify notify; 1396 GNUNET_CONNECTION_TransmitReadyNotify notify;
1393 1397
1394#if DEBUG_CONNECTION 1398#if DEBUG_CONNECTION
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_timeout running (%p)\n", sock); 1399 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_timeout running (%p)\n", sock);
1396#endif 1400#endif
1397 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 1401 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1398#if DEBUG_CONNECTION 1402#if DEBUG_CONNECTION
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1403 LOG (GNUNET_ERROR_TYPE_DEBUG,
1400 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", 1404 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1401 sock->hostname, sock->port, GNUNET_a2s (sock->addr, 1405 sock->hostname, sock->port, GNUNET_a2s (sock->addr,
1402 sock->addrlen), sock); 1406 sock->addrlen), sock);
1403#endif 1407#endif
1404 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY)); 1408 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY));
1405 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */ 1409 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */
1406 notify = sock->nth.notify_ready; 1410 notify = sock->nth.notify_ready;
1407 sock->nth.notify_ready = NULL; 1411 sock->nth.notify_ready = NULL;
1408 notify (sock->nth.notify_ready_cls, 0, NULL); 1412 notify (sock->nth.notify_ready_cls, 0, NULL);
@@ -1425,9 +1429,9 @@ connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1425 GNUNET_CONNECTION_TransmitReadyNotify notify; 1429 GNUNET_CONNECTION_TransmitReadyNotify notify;
1426 1430
1427#if DEBUG_CONNECTION 1431#if DEBUG_CONNECTION
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1432 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n", 1433 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1430 sock->nth.notify_size, sock->hostname, sock->port, sock); 1434 sock->nth.notify_size, sock->hostname, sock->port, sock);
1431#endif 1435#endif
1432 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1436 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1433 notify = sock->nth.notify_ready; 1437 notify = sock->nth.notify_ready;
@@ -1447,20 +1451,20 @@ transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1447 GNUNET_CONNECTION_TransmitReadyNotify notify; 1451 GNUNET_CONNECTION_TransmitReadyNotify notify;
1448 1452
1449 if (NULL != sock->sock) 1453 if (NULL != sock->sock)
1450 { 1454 {
1451 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); 1455 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR);
1452 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); 1456 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock));
1453 sock->sock = NULL; 1457 sock->sock = NULL;
1454 } 1458 }
1455 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 1459 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK)
1456 { 1460 {
1457 GNUNET_SCHEDULER_cancel (sock->read_task); 1461 GNUNET_SCHEDULER_cancel (sock->read_task);
1458 sock->read_task = GNUNET_SCHEDULER_NO_TASK; 1462 sock->read_task = GNUNET_SCHEDULER_NO_TASK;
1459 signal_timeout (sock); 1463 signal_timeout (sock);
1460 return; 1464 return;
1461 } 1465 }
1462 if (sock->nth.notify_ready == NULL) 1466 if (sock->nth.notify_ready == NULL)
1463 return; /* nobody to tell about it */ 1467 return; /* nobody to tell about it */
1464 notify = sock->nth.notify_ready; 1468 notify = sock->nth.notify_ready;
1465 sock->nth.notify_ready = NULL; 1469 sock->nth.notify_ready = NULL;
1466 notify (sock->nth.notify_ready_cls, 0, NULL); 1470 notify (sock->nth.notify_ready_cls, 0, NULL);
@@ -1484,129 +1488,129 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1484 size_t have; 1488 size_t have;
1485 1489
1486#if DEBUG_CONNECTION 1490#if DEBUG_CONNECTION
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock); 1491 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock);
1488#endif 1492#endif
1489 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK); 1493 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK);
1490 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1494 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1491 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 1495 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1492 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1496 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1493 { 1497 {
1494 if (sock->ignore_shutdown == GNUNET_YES) 1498 if (sock->ignore_shutdown == GNUNET_YES)
1495 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ 1499 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1496#if DEBUG_CONNECTION 1500#if DEBUG_CONNECTION
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1501 LOG (GNUNET_ERROR_TYPE_DEBUG,
1498 "Transmit to `%s' fails, shutdown happened (%p).\n", 1502 "Transmit to `%s' fails, shutdown happened (%p).\n",
1499 GNUNET_a2s (sock->addr, sock->addrlen), sock); 1503 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1500#endif 1504#endif
1501 notify = sock->nth.notify_ready; 1505 notify = sock->nth.notify_ready;
1502 if (NULL != notify) 1506 if (NULL != notify)
1503 { 1507 {
1504 sock->nth.notify_ready = NULL; 1508 sock->nth.notify_ready = NULL;
1505 notify (sock->nth.notify_ready_cls, 0, NULL); 1509 notify (sock->nth.notify_ready_cls, 0, NULL);
1510 }
1511 return;
1506 } 1512 }
1507 return;
1508 }
1509 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) 1513 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1510 { 1514 {
1511#if DEBUG_CONNECTION 1515#if DEBUG_CONNECTION
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1516 LOG (GNUNET_ERROR_TYPE_DEBUG,
1513 "Transmit to `%s' fails, time out reached (%p).\n", 1517 "Transmit to `%s' fails, time out reached (%p).\n",
1514 GNUNET_a2s (sock->addr, sock->addrlen), sock); 1518 GNUNET_a2s (sock->addr, sock->addrlen), sock);
1515#endif 1519#endif
1516 notify = sock->nth.notify_ready; 1520 notify = sock->nth.notify_ready;
1517 GNUNET_assert (NULL != notify); 1521 GNUNET_assert (NULL != notify);
1518 sock->nth.notify_ready = NULL; 1522 sock->nth.notify_ready = NULL;
1519 notify (sock->nth.notify_ready_cls, 0, NULL); 1523 notify (sock->nth.notify_ready_cls, 0, NULL);
1520 return; 1524 return;
1521 } 1525 }
1522 GNUNET_assert (NULL != sock->sock); 1526 GNUNET_assert (NULL != sock->sock);
1523 if (tc->write_ready == NULL) 1527 if (tc->write_ready == NULL)
1524 { 1528 {
1525 /* special circumstances (in particular, 1529 /* special circumstances (in particular,
1526 * PREREQ_DONE after connect): not yet ready to write, 1530 * PREREQ_DONE after connect): not yet ready to write,
1527 * but no "fatal" error either. Hence retry. */ 1531 * but no "fatal" error either. Hence retry. */
1528 goto SCHEDULE_WRITE; 1532 goto SCHEDULE_WRITE;
1529 } 1533 }
1530 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock)) 1534 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock))
1531 { 1535 {
1532#if DEBUG_CONNECTION 1536#if DEBUG_CONNECTION
1533 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1537 LOG (GNUNET_ERROR_TYPE_INFO,
1534 _ 1538 _
1535 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"), 1539 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1536 sock); 1540 sock);
1537#endif 1541#endif
1538 transmit_error (sock); 1542 transmit_error (sock);
1539 return; /* connect failed for good, we're finished */ 1543 return; /* connect failed for good, we're finished */
1540 } 1544 }
1541 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos); 1545 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos);
1542 if ((sock->nth.notify_ready != NULL) && 1546 if ((sock->nth.notify_ready != NULL) &&
1543 (sock->write_buffer_size < sock->nth.notify_size)) 1547 (sock->write_buffer_size < sock->nth.notify_size))
1544 { 1548 {
1545 sock->write_buffer = 1549 sock->write_buffer =
1546 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size); 1550 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size);
1547 sock->write_buffer_size = sock->nth.notify_size; 1551 sock->write_buffer_size = sock->nth.notify_size;
1548 } 1552 }
1549 process_notify (sock); 1553 process_notify (sock);
1550 have = sock->write_buffer_off - sock->write_buffer_pos; 1554 have = sock->write_buffer_off - sock->write_buffer_pos;
1551 if (have == 0) 1555 if (have == 0)
1552 { 1556 {
1553 /* no data ready for writing, terminate write loop */ 1557 /* no data ready for writing, terminate write loop */
1554 return; 1558 return;
1555 } 1559 }
1556 GNUNET_assert (have <= sock->write_buffer_size); 1560 GNUNET_assert (have <= sock->write_buffer_size);
1557 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size); 1561 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size);
1558 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size); 1562 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size);
1559RETRY: 1563RETRY:
1560 ret = 1564 ret =
1561 GNUNET_NETWORK_socket_send (sock->sock, 1565 GNUNET_NETWORK_socket_send (sock->sock,
1562 &sock->write_buffer[sock->write_buffer_pos], 1566 &sock->write_buffer[sock->write_buffer_pos],
1563 have); 1567 have);
1564 if (ret == -1) 1568 if (ret == -1)
1565 { 1569 {
1566 if (errno == EINTR) 1570 if (errno == EINTR)
1567 goto RETRY; 1571 goto RETRY;
1568#if 0 1572#if 0
1569 int en = errno; 1573 int en = errno;
1570 1574
1571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"), 1575 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to send to `%s': %s\n"),
1572 GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en)); 1576 GNUNET_a2s (sock->addr, sock->addrlen), STRERROR (en));
1573#endif 1577#endif
1574#if DEBUG_CONNECTION 1578#if DEBUG_CONNECTION
1575 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send"); 1579 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
1576#endif 1580#endif
1577 transmit_error (sock); 1581 transmit_error (sock);
1578 return; 1582 return;
1579 } 1583 }
1580#if DEBUG_CONNECTION 1584#if DEBUG_CONNECTION
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1585 LOG (GNUNET_ERROR_TYPE_DEBUG,
1582 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n", 1586 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1583 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), 1587 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen),
1584 sock); 1588 sock);
1585#endif 1589#endif
1586 sock->write_buffer_pos += ret; 1590 sock->write_buffer_pos += ret;
1587 if (sock->write_buffer_pos == sock->write_buffer_off) 1591 if (sock->write_buffer_pos == sock->write_buffer_off)
1588 { 1592 {
1589 /* transmitted all pending data */ 1593 /* transmitted all pending data */
1590 sock->write_buffer_pos = 0; 1594 sock->write_buffer_pos = 0;
1591 sock->write_buffer_off = 0; 1595 sock->write_buffer_off = 0;
1592 } 1596 }
1593 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready)) 1597 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready))
1594 return; /* all data sent! */ 1598 return; /* all data sent! */
1595 /* not done writing, schedule more */ 1599 /* not done writing, schedule more */
1596SCHEDULE_WRITE: 1600SCHEDULE_WRITE:
1597#if DEBUG_CONNECTION 1601#if DEBUG_CONNECTION
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1602 LOG (GNUNET_ERROR_TYPE_DEBUG,
1599 "Re-scheduling transmit_ready (more to do) (%p).\n", sock); 1603 "Re-scheduling transmit_ready (more to do) (%p).\n", sock);
1600#endif 1604#endif
1601 have = sock->write_buffer_off - sock->write_buffer_pos; 1605 have = sock->write_buffer_off - sock->write_buffer_pos;
1602 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0)); 1606 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0));
1603 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) 1607 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK)
1604 sock->write_task = 1608 sock->write_task =
1605 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == 1609 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready ==
1606 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL : 1610 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
1607 GNUNET_TIME_absolute_get_remaining 1611 GNUNET_TIME_absolute_get_remaining
1608 (sock->nth.transmit_timeout), 1612 (sock->nth.transmit_timeout),
1609 sock->sock, &transmit_ready, sock); 1613 sock->sock, &transmit_ready, sock);
1610} 1614}
1611 1615
1612 1616
@@ -1625,17 +1629,17 @@ SCHEDULE_WRITE:
1625 * NULL if we are already going to notify someone else (busy) 1629 * NULL if we are already going to notify someone else (busy)
1626 */ 1630 */
1627struct GNUNET_CONNECTION_TransmitHandle * 1631struct GNUNET_CONNECTION_TransmitHandle *
1628GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock, 1632GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle
1629 size_t size, 1633 *sock, size_t size,
1630 struct GNUNET_TIME_Relative timeout, 1634 struct GNUNET_TIME_Relative timeout,
1631 GNUNET_CONNECTION_TransmitReadyNotify 1635 GNUNET_CONNECTION_TransmitReadyNotify
1632 notify, void *notify_cls) 1636 notify, void *notify_cls)
1633{ 1637{
1634 if (sock->nth.notify_ready != NULL) 1638 if (sock->nth.notify_ready != NULL)
1635 { 1639 {
1636 GNUNET_assert (0); 1640 GNUNET_assert (0);
1637 return NULL; 1641 return NULL;
1638 } 1642 }
1639 GNUNET_assert (notify != NULL); 1643 GNUNET_assert (notify != NULL);
1640 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); 1644 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1641 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size); 1645 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size);
@@ -1649,36 +1653,36 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1649 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task); 1653 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task);
1650 if ((sock->sock == NULL) && (sock->ap_head == NULL) && 1654 if ((sock->sock == NULL) && (sock->ap_head == NULL) &&
1651 (sock->dns_active == NULL)) 1655 (sock->dns_active == NULL))
1652 { 1656 {
1653 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 1657 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK)
1654 GNUNET_SCHEDULER_cancel (sock->write_task); 1658 GNUNET_SCHEDULER_cancel (sock->write_task);
1655 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock); 1659 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock);
1656 return &sock->nth; 1660 return &sock->nth;
1657 } 1661 }
1658 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task) 1662 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task)
1659 return &sock->nth; 1663 return &sock->nth;
1660 if (sock->sock != NULL) 1664 if (sock->sock != NULL)
1661 { 1665 {
1662#if DEBUG_CONNECTION 1666#if DEBUG_CONNECTION
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", 1667 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n",
1664 sock); 1668 sock);
1665#endif 1669#endif
1666 sock->write_task = 1670 sock->write_task =
1667 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 1671 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1668 (sock->nth.transmit_timeout), 1672 (sock->nth.transmit_timeout),
1669 sock->sock, &transmit_ready, sock); 1673 sock->sock, &transmit_ready, sock);
1670 } 1674 }
1671 else 1675 else
1672 { 1676 {
1673#if DEBUG_CONNECTION 1677#if DEBUG_CONNECTION
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1678 LOG (GNUNET_ERROR_TYPE_DEBUG,
1675 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", 1679 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n",
1676 sock); 1680 sock);
1677#endif 1681#endif
1678 sock->ccs |= COCO_TRANSMIT_READY; 1682 sock->ccs |= COCO_TRANSMIT_READY;
1679 sock->nth.timeout_task = 1683 sock->nth.timeout_task =
1680 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock); 1684 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock);
1681 } 1685 }
1682 return &sock->nth; 1686 return &sock->nth;
1683} 1687}
1684 1688
@@ -1690,28 +1694,28 @@ GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock,
1690 */ 1694 */
1691void 1695void
1692GNUNET_CONNECTION_notify_transmit_ready_cancel (struct 1696GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1693 GNUNET_CONNECTION_TransmitHandle 1697 GNUNET_CONNECTION_TransmitHandle
1694 *th) 1698 *th)
1695{ 1699{
1696 GNUNET_assert (th->notify_ready != NULL); 1700 GNUNET_assert (th->notify_ready != NULL);
1697 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY)) 1701 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY))
1698 { 1702 {
1699#if DEBUG_CONNECTION 1703#if DEBUG_CONNECTION
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1704 LOG (GNUNET_ERROR_TYPE_DEBUG,
1701 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th); 1705 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1702#endif 1706#endif
1703 GNUNET_SCHEDULER_cancel (th->timeout_task); 1707 GNUNET_SCHEDULER_cancel (th->timeout_task);
1704 th->timeout_task = GNUNET_SCHEDULER_NO_TASK; 1708 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1705 th->sh->ccs -= COCO_TRANSMIT_READY; 1709 th->sh->ccs -= COCO_TRANSMIT_READY;
1706 } 1710 }
1707 else 1711 else
1708 {
1709 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1710 { 1712 {
1711 GNUNET_SCHEDULER_cancel (th->sh->write_task); 1713 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK)
1712 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK; 1714 {
1715 GNUNET_SCHEDULER_cancel (th->sh->write_task);
1716 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK;
1717 }
1713 } 1718 }
1714 }
1715 th->notify_ready = NULL; 1719 th->notify_ready = NULL;
1716} 1720}
1717 1721