aboutsummaryrefslogtreecommitdiff
path: root/src/util/connection.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-04-19 11:22:20 +0000
committerChristian Grothoff <christian@grothoff.org>2012-04-19 11:22:20 +0000
commit4071d1862fdcd9389e990784d0ea1fb9c4651f0f (patch)
treee2921e7132bb90fc4de5d7296387db4daa89beb8 /src/util/connection.c
parent92b983e311c77322914cc6164bc8d24f0eaffdf4 (diff)
downloadgnunet-4071d1862fdcd9389e990784d0ea1fb9c4651f0f.tar.gz
gnunet-4071d1862fdcd9389e990784d0ea1fb9c4651f0f.zip
-rename fest in connection.c
Diffstat (limited to 'src/util/connection.c')
-rw-r--r--src/util/connection.c795
1 files changed, 396 insertions, 399 deletions
diff --git a/src/util/connection.c b/src/util/connection.c
index 2953d9ca7..8d0042b7b 100644
--- a/src/util/connection.c
+++ b/src/util/connection.c
@@ -89,9 +89,9 @@ struct GNUNET_CONNECTION_TransmitHandle
89 void *notify_ready_cls; 89 void *notify_ready_cls;
90 90
91 /** 91 /**
92 * Our socket handle. 92 * Our connection handle.
93 */ 93 */
94 struct GNUNET_CONNECTION_Handle *sh; 94 struct GNUNET_CONNECTION_Handle *connection;
95 95
96 /** 96 /**
97 * Timeout for receiving (in absolute time). 97 * Timeout for receiving (in absolute time).
@@ -142,7 +142,7 @@ struct AddressProbe
142 /** 142 /**
143 * Connection for which we are probing. 143 * Connection for which we are probing.
144 */ 144 */
145 struct GNUNET_CONNECTION_Handle *h; 145 struct GNUNET_CONNECTION_Handle *connection;
146 146
147 /** 147 /**
148 * Lenth of addr. 148 * Lenth of addr.
@@ -150,14 +150,14 @@ struct AddressProbe
150 socklen_t addrlen; 150 socklen_t addrlen;
151 151
152 /** 152 /**
153 * Task waiting for the socket to finish connecting. 153 * Task waiting for the connection to finish connecting.
154 */ 154 */
155 GNUNET_SCHEDULER_TaskIdentifier task; 155 GNUNET_SCHEDULER_TaskIdentifier task;
156}; 156};
157 157
158 158
159/** 159/**
160 * @brief handle for a network socket 160 * @brief handle for a network connection
161 */ 161 */
162struct GNUNET_CONNECTION_Handle 162struct GNUNET_CONNECTION_Handle
163{ 163{
@@ -185,7 +185,7 @@ struct GNUNET_CONNECTION_Handle
185 struct sockaddr *addr; 185 struct sockaddr *addr;
186 186
187 /** 187 /**
188 * Pointer to the hostname if socket was 188 * Pointer to the hostname if connection was
189 * created using DNS lookup, otherwise NULL. 189 * created using DNS lookup, otherwise NULL.
190 */ 190 */
191 char *hostname; 191 char *hostname;
@@ -273,7 +273,7 @@ struct GNUNET_CONNECTION_Handle
273 size_t max; 273 size_t max;
274 274
275 /** 275 /**
276 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this socket. 276 * Ignore GNUNET_SCHEDULER_REASON_SHUTDOWN for this connection.
277 */ 277 */
278 int ignore_shutdown; 278 int ignore_shutdown;
279 279
@@ -297,69 +297,69 @@ struct GNUNET_CONNECTION_Handle
297 * that the underlying socket or fd should never really be closed. 297 * that the underlying socket or fd should never really be closed.
298 * Used for indicating process death. 298 * Used for indicating process death.
299 * 299 *
300 * @param sock the connection to set persistent 300 * @param connection the connection to set persistent
301 */ 301 */
302void 302void
303GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *sock) 303GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *connection)
304{ 304{
305 sock->persist = GNUNET_YES; 305 connection->persist = GNUNET_YES;
306} 306}
307 307
308 308
309/** 309/**
310 * Disable the "CORK" feature for communication with the given socket, 310 * Disable the "CORK" feature for communication with the given connection,
311 * forcing the OS to immediately flush the buffer on transmission 311 * forcing the OS to immediately flush the buffer on transmission
312 * instead of potentially buffering multiple messages. Essentially 312 * instead of potentially buffering multiple messages. Essentially
313 * reduces the OS send buffers to zero. 313 * reduces the OS send buffers to zero.
314 * Used to make sure that the last messages sent through the connection 314 * Used to make sure that the last messages sent through the connection
315 * reach the other side before the process is terminated. 315 * reach the other side before the process is terminated.
316 * 316 *
317 * @param sock the connection to make flushing and blocking 317 * @param connection the connection to make flushing and blocking
318 * @return GNUNET_OK on success 318 * @return GNUNET_OK on success
319 */ 319 */
320int 320int
321GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *sock) 321GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *connection)
322{ 322{
323 return GNUNET_NETWORK_socket_disable_corking (sock->sock); 323 return GNUNET_NETWORK_socket_disable_corking (connection->sock);
324} 324}
325 325
326 326
327/** 327/**
328 * Create a socket handle by boxing an existing OS socket. The OS 328 * Create a connection handle by boxing an existing OS socket. The OS
329 * socket should henceforth be no longer used directly. 329 * socket should henceforth be no longer used directly.
330 * GNUNET_socket_destroy will close it. 330 * GNUNET_connection_destroy will close it.
331 * 331 *
332 * @param osSocket existing socket to box 332 * @param osSocket existing socket to box
333 * @return the boxed socket handle 333 * @return the boxed connection handle
334 */ 334 */
335struct GNUNET_CONNECTION_Handle * 335struct GNUNET_CONNECTION_Handle *
336GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket) 336GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket)
337{ 337{
338 struct GNUNET_CONNECTION_Handle *ret; 338 struct GNUNET_CONNECTION_Handle *connection;
339 339
340 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 340 connection = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
341 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 341 connection->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
342 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size); 342 connection->write_buffer = GNUNET_malloc (connection->write_buffer_size);
343 ret->sock = osSocket; 343 connection->sock = osSocket;
344 return ret; 344 return connection;
345} 345}
346 346
347 347
348/** 348/**
349 * Create a socket handle by accepting on a listen socket. This 349 * Create a connection handle by accepting on a listen socket. This
350 * function may block if the listen socket has no connection ready. 350 * function may block if the listen socket has no connection ready.
351 * 351 *
352 * @param access function to use to check if access is allowed 352 * @param access function to use to check if access is allowed
353 * @param access_cls closure for access 353 * @param access_cls closure for access
354 * @param lsock listen socket 354 * @param lsock listen socket
355 * @return the socket handle, NULL on error 355 * @return the connection handle, NULL on error
356 */ 356 */
357struct GNUNET_CONNECTION_Handle * 357struct GNUNET_CONNECTION_Handle *
358GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access, 358GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
359 void *access_cls, 359 void *access_cls,
360 struct GNUNET_NETWORK_Handle *lsock) 360 struct GNUNET_NETWORK_Handle *lsock)
361{ 361{
362 struct GNUNET_CONNECTION_Handle *ret; 362 struct GNUNET_CONNECTION_Handle *connection;
363 char addr[128]; 363 char addr[128];
364 socklen_t addrlen; 364 socklen_t addrlen;
365 struct GNUNET_NETWORK_Handle *sock; 365 struct GNUNET_NETWORK_Handle *sock;
@@ -466,36 +466,36 @@ GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access,
466 GNUNET_free (uaddr); 466 GNUNET_free (uaddr);
467 return NULL; 467 return NULL;
468 } 468 }
469 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 469 connection = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
470 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 470 connection->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
471 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size); 471 connection->write_buffer = GNUNET_malloc (connection->write_buffer_size);
472 ret->addr = uaddr; 472 connection->addr = uaddr;
473 ret->addrlen = addrlen; 473 connection->addrlen = addrlen;
474 ret->sock = sock; 474 connection->sock = sock;
475 LOG (GNUNET_ERROR_TYPE_INFO, 475 LOG (GNUNET_ERROR_TYPE_INFO,
476 _("Accepting connection from `%s': %p\n"), 476 _("Accepting connection from `%s': %p\n"),
477 GNUNET_a2s (uaddr, addrlen), ret); 477 GNUNET_a2s (uaddr, addrlen), connection);
478 return ret; 478 return connection;
479} 479}
480 480
481 481
482/** 482/**
483 * Obtain the network address of the other party. 483 * Obtain the network address of the other party.
484 * 484 *
485 * @param sock the client to get the address for 485 * @param connection the client to get the address for
486 * @param addr where to store the address 486 * @param addr where to store the address
487 * @param addrlen where to store the length of the address 487 * @param addrlen where to store the length of the address
488 * @return GNUNET_OK on success 488 * @return GNUNET_OK on success
489 */ 489 */
490int 490int
491GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *sock, 491GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
492 void **addr, size_t * addrlen) 492 void **addr, size_t * addrlen)
493{ 493{
494 if ((sock->addr == NULL) || (sock->addrlen == 0)) 494 if ((connection->addr == NULL) || (connection->addrlen == 0))
495 return GNUNET_NO; 495 return GNUNET_NO;
496 *addr = GNUNET_malloc (sock->addrlen); 496 *addr = GNUNET_malloc (connection->addrlen);
497 memcpy (*addr, sock->addr, sock->addrlen); 497 memcpy (*addr, connection->addr, connection->addrlen);
498 *addrlen = sock->addrlen; 498 *addrlen = connection->addrlen;
499 return GNUNET_OK; 499 return GNUNET_OK;
500} 500}
501 501
@@ -524,82 +524,82 @@ receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
524static void 524static void
525destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 525destroy_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
526{ 526{
527 struct GNUNET_CONNECTION_Handle *sock = cls; 527 struct GNUNET_CONNECTION_Handle *connection = cls;
528 GNUNET_CONNECTION_TransmitReadyNotify notify; 528 GNUNET_CONNECTION_TransmitReadyNotify notify;
529 struct AddressProbe *pos; 529 struct AddressProbe *pos;
530 530
531 sock->destroy_task = GNUNET_SCHEDULER_NO_TASK; 531 connection->destroy_task = GNUNET_SCHEDULER_NO_TASK;
532 GNUNET_assert (sock->dns_active == NULL); 532 GNUNET_assert (connection->dns_active == NULL);
533 if (0 != (sock->ccs & COCO_TRANSMIT_READY)) 533 if (0 != (connection->ccs & COCO_TRANSMIT_READY))
534 { 534 {
535 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy waits for CCS-TR to be done (%p)\n", 535 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy waits for CCS-TR to be done (%p)\n",
536 sock); 536 connection);
537 sock->ccs |= COCO_DESTROY_CONTINUATION; 537 connection->ccs |= COCO_DESTROY_CONTINUATION;
538 return; 538 return;
539 } 539 }
540 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 540 if (connection->write_task != GNUNET_SCHEDULER_NO_TASK)
541 { 541 {
542 LOG (GNUNET_ERROR_TYPE_DEBUG, 542 LOG (GNUNET_ERROR_TYPE_DEBUG,
543 "Destroy waits for write_task to be done (%p)\n", sock); 543 "Destroy waits for write_task to be done (%p)\n", connection);
544 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 544 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
545 sock->destroy_task = 545 connection->destroy_task =
546 GNUNET_SCHEDULER_add_after (sock->write_task, &destroy_continuation, 546 GNUNET_SCHEDULER_add_after (connection->write_task, &destroy_continuation,
547 sock); 547 connection);
548 return; 548 return;
549 } 549 }
550 if (0 != (sock->ccs & COCO_RECEIVE_AGAIN)) 550 if (0 != (connection->ccs & COCO_RECEIVE_AGAIN))
551 { 551 {
552 sock->ccs |= COCO_DESTROY_CONTINUATION; 552 connection->ccs |= COCO_DESTROY_CONTINUATION;
553 return; 553 return;
554 } 554 }
555 if (sock->sock != NULL) 555 if (connection->sock != NULL)
556 { 556 {
557 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down socket (%p)\n", sock); 557 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection);
558 if (sock->persist != GNUNET_YES) 558 if (connection->persist != GNUNET_YES)
559 { 559 {
560 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR)) 560 if ((GNUNET_YES != GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR))
561 && (errno != ENOTCONN) && (errno != ECONNRESET)) 561 && (errno != ENOTCONN) && (errno != ECONNRESET))
562 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown"); 562 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
563 } 563 }
564 } 564 }
565 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 565 if (connection->read_task != GNUNET_SCHEDULER_NO_TASK)
566 { 566 {
567 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 567 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
568 sock->destroy_task = 568 connection->destroy_task =
569 GNUNET_SCHEDULER_add_after (sock->read_task, &destroy_continuation, 569 GNUNET_SCHEDULER_add_after (connection->read_task, &destroy_continuation,
570 sock); 570 connection);
571 return; 571 return;
572 } 572 }
573 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", sock); 573 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroy actually runs (%p)!\n", connection);
574 while (NULL != (pos = sock->ap_head)) 574 while (NULL != (pos = connection->ap_head))
575 { 575 {
576 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); 576 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
577 GNUNET_SCHEDULER_cancel (pos->task); 577 GNUNET_SCHEDULER_cancel (pos->task);
578 GNUNET_CONTAINER_DLL_remove (sock->ap_head, sock->ap_tail, pos); 578 GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, pos);
579 GNUNET_free (pos); 579 GNUNET_free (pos);
580 } 580 }
581 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 581 GNUNET_assert (connection->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
582 GNUNET_assert (sock->ccs == COCO_NONE); 582 GNUNET_assert (connection->ccs == COCO_NONE);
583 if (NULL != (notify = sock->nth.notify_ready)) 583 if (NULL != (notify = connection->nth.notify_ready))
584 { 584 {
585 sock->nth.notify_ready = NULL; 585 connection->nth.notify_ready = NULL;
586 notify (sock->nth.notify_ready_cls, 0, NULL); 586 notify (connection->nth.notify_ready_cls, 0, NULL);
587 } 587 }
588 588
589 if (sock->sock != NULL) 589 if (connection->sock != NULL)
590 { 590 {
591 if (sock->persist != GNUNET_YES) 591 if (connection->persist != GNUNET_YES)
592 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); 592 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
593 else 593 else
594 GNUNET_free (sock->sock); /* at least no memory leak (we deliberately 594 GNUNET_free (connection->sock); /* at least no memory leak (we deliberately
595 * leak the socket in this special case) ... */ 595 * leak the socket in this special case) ... */
596 } 596 }
597 GNUNET_free_non_null (sock->addr); 597 GNUNET_free_non_null (connection->addr);
598 GNUNET_free_non_null (sock->hostname); 598 GNUNET_free_non_null (connection->hostname);
599 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 599 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
600 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", sock); 600 LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing memory of connection %p.\n", connection);
601 GNUNET_free (sock->write_buffer); 601 GNUNET_free (connection->write_buffer);
602 GNUNET_free (sock); 602 GNUNET_free (connection);
603} 603}
604 604
605 605
@@ -668,44 +668,44 @@ connect_fail_continuation (struct GNUNET_CONNECTION_Handle *h)
668/** 668/**
669 * We've succeeded in establishing a connection. 669 * We've succeeded in establishing a connection.
670 * 670 *
671 * @param h the connection we tried to establish 671 * @param connection the connection we tried to establish
672 */ 672 */
673static void 673static void
674connect_success_continuation (struct GNUNET_CONNECTION_Handle *h) 674connect_success_continuation (struct GNUNET_CONNECTION_Handle *connection)
675{ 675{
676 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n", 676 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection to `%s' succeeded! (%p)\n",
677 GNUNET_a2s (h->addr, h->addrlen), h); 677 GNUNET_a2s (connection->addr, connection->addrlen), connection);
678 /* trigger jobs that waited for the connection */ 678 /* trigger jobs that waited for the connection */
679 if (0 != (h->ccs & COCO_RECEIVE_AGAIN)) 679 if (0 != (connection->ccs & COCO_RECEIVE_AGAIN))
680 { 680 {
681 LOG (GNUNET_ERROR_TYPE_DEBUG, 681 LOG (GNUNET_ERROR_TYPE_DEBUG,
682 "connect_success_continuation runs receive_again (%p)\n", h); 682 "connect_success_continuation runs receive_again (%p)\n", connection);
683 h->ccs -= COCO_RECEIVE_AGAIN; 683 connection->ccs -= COCO_RECEIVE_AGAIN;
684 h->read_task = GNUNET_SCHEDULER_add_now (&receive_again, h); 684 connection->read_task = GNUNET_SCHEDULER_add_now (&receive_again, connection);
685 } 685 }
686 if (0 != (h->ccs & COCO_TRANSMIT_READY)) 686 if (0 != (connection->ccs & COCO_TRANSMIT_READY))
687 { 687 {
688 LOG (GNUNET_ERROR_TYPE_DEBUG, 688 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n", 689 "connect_success_continuation runs transmit_ready, cancels timeout_task (%p)\n",
690 h); 690 connection);
691 GNUNET_assert (h->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK); 691 GNUNET_assert (connection->nth.timeout_task != GNUNET_SCHEDULER_NO_TASK);
692 GNUNET_SCHEDULER_cancel (h->nth.timeout_task); 692 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
693 h->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 693 connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
694 h->ccs -= COCO_TRANSMIT_READY; 694 connection->ccs -= COCO_TRANSMIT_READY;
695 GNUNET_assert (h->write_task == GNUNET_SCHEDULER_NO_TASK); 695 GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK);
696 GNUNET_assert (h->nth.notify_ready != NULL); 696 GNUNET_assert (connection->nth.notify_ready != NULL);
697 h->write_task = 697 connection->write_task =
698 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 698 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
699 (h->nth.transmit_timeout), h->sock, 699 (connection->nth.transmit_timeout), connection->sock,
700 &transmit_ready, h); 700 &transmit_ready, connection);
701 } 701 }
702 if (0 != (h->ccs & COCO_DESTROY_CONTINUATION)) 702 if (0 != (connection->ccs & COCO_DESTROY_CONTINUATION))
703 { 703 {
704 LOG (GNUNET_ERROR_TYPE_DEBUG, 704 LOG (GNUNET_ERROR_TYPE_DEBUG,
705 "connect_success_continuation runs destroy_continuation (%p)\n", h); 705 "connect_success_continuation runs destroy_continuation (%p)\n", connection);
706 h->ccs -= COCO_DESTROY_CONTINUATION; 706 connection->ccs -= COCO_DESTROY_CONTINUATION;
707 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->destroy_task); 707 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
708 h->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, h); 708 connection->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, connection);
709 } 709 }
710} 710}
711 711
@@ -722,13 +722,13 @@ connect_probe_continuation (void *cls,
722 const struct GNUNET_SCHEDULER_TaskContext *tc) 722 const struct GNUNET_SCHEDULER_TaskContext *tc)
723{ 723{
724 struct AddressProbe *ap = cls; 724 struct AddressProbe *ap = cls;
725 struct GNUNET_CONNECTION_Handle *h = ap->h; 725 struct GNUNET_CONNECTION_Handle *connection = ap->connection;
726 struct AddressProbe *pos; 726 struct AddressProbe *pos;
727 int error; 727 int error;
728 socklen_t len; 728 socklen_t len;
729 729
730 GNUNET_assert (ap->sock != NULL); 730 GNUNET_assert (ap->sock != NULL);
731 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, ap); 731 GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, ap);
732 len = sizeof (error); 732 len = sizeof (error);
733 errno = 0; 733 errno = 0;
734 error = 0; 734 error = 0;
@@ -739,31 +739,31 @@ connect_probe_continuation (void *cls,
739 { 739 {
740 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 740 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
741 GNUNET_free (ap); 741 GNUNET_free (ap);
742 if ((NULL == h->ap_head) && (h->dns_active == GNUNET_NO)) 742 if ((NULL == connection->ap_head) && (connection->dns_active == GNUNET_NO))
743 connect_fail_continuation (h); 743 connect_fail_continuation (connection);
744 return; 744 return;
745 } 745 }
746 GNUNET_assert (h->sock == NULL); 746 GNUNET_assert (connection->sock == NULL);
747 h->sock = ap->sock; 747 connection->sock = ap->sock;
748 GNUNET_assert (h->addr == NULL); 748 GNUNET_assert (connection->addr == NULL);
749 h->addr = GNUNET_malloc (ap->addrlen); 749 connection->addr = GNUNET_malloc (ap->addrlen);
750 memcpy (h->addr, ap->addr, ap->addrlen); 750 memcpy (connection->addr, ap->addr, ap->addrlen);
751 h->addrlen = ap->addrlen; 751 connection->addrlen = ap->addrlen;
752 GNUNET_free (ap); 752 GNUNET_free (ap);
753 /* cancel all other attempts */ 753 /* cancel all other attempts */
754 while (NULL != (pos = h->ap_head)) 754 while (NULL != (pos = connection->ap_head))
755 { 755 {
756 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock)); 756 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
757 GNUNET_SCHEDULER_cancel (pos->task); 757 GNUNET_SCHEDULER_cancel (pos->task);
758 GNUNET_CONTAINER_DLL_remove (h->ap_head, h->ap_tail, pos); 758 GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, pos);
759 GNUNET_free (pos); 759 GNUNET_free (pos);
760 } 760 }
761 connect_success_continuation (h); 761 connect_success_continuation (connection);
762} 762}
763 763
764 764
765/** 765/**
766 * Try to establish a socket connection given the specified address. 766 * Try to establish a connection given the specified address.
767 * This function is called by the resolver once we have a DNS reply. 767 * This function is called by the resolver once we have a DNS reply.
768 * 768 *
769 * @param cls our "struct GNUNET_CONNECTION_Handle *" 769 * @param cls our "struct GNUNET_CONNECTION_Handle *"
@@ -774,37 +774,37 @@ static void
774try_connect_using_address (void *cls, const struct sockaddr *addr, 774try_connect_using_address (void *cls, const struct sockaddr *addr,
775 socklen_t addrlen) 775 socklen_t addrlen)
776{ 776{
777 struct GNUNET_CONNECTION_Handle *h = cls; 777 struct GNUNET_CONNECTION_Handle *connection = cls;
778 struct AddressProbe *ap; 778 struct AddressProbe *ap;
779 struct GNUNET_TIME_Relative delay; 779 struct GNUNET_TIME_Relative delay;
780 780
781 if (addr == NULL) 781 if (addr == NULL)
782 { 782 {
783 h->dns_active = NULL; 783 connection->dns_active = NULL;
784 if ((NULL == h->ap_head) && (NULL == h->sock)) 784 if ((NULL == connection->ap_head) && (NULL == connection->sock))
785 connect_fail_continuation (h); 785 connect_fail_continuation (connection);
786 return; 786 return;
787 } 787 }
788 if (h->sock != NULL) 788 if (connection->sock != NULL)
789 return; /* already connected */ 789 return; /* already connected */
790 GNUNET_assert (h->addr == NULL); 790 GNUNET_assert (connection->addr == NULL);
791 /* try to connect */ 791 /* try to connect */
792 LOG (GNUNET_ERROR_TYPE_DEBUG, 792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "Trying to connect using address `%s:%u/%s:%u'\n", h->hostname, h->port, 793 "Trying to connect using address `%s:%u/%s:%u'\n", connection->hostname, connection->port,
794 GNUNET_a2s (addr, addrlen), h->port); 794 GNUNET_a2s (addr, addrlen), connection->port);
795 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen); 795 ap = GNUNET_malloc (sizeof (struct AddressProbe) + addrlen);
796 ap->addr = (const struct sockaddr *) &ap[1]; 796 ap->addr = (const struct sockaddr *) &ap[1];
797 memcpy (&ap[1], addr, addrlen); 797 memcpy (&ap[1], addr, addrlen);
798 ap->addrlen = addrlen; 798 ap->addrlen = addrlen;
799 ap->h = h; 799 ap->connection = connection;
800 800
801 switch (ap->addr->sa_family) 801 switch (ap->addr->sa_family)
802 { 802 {
803 case AF_INET: 803 case AF_INET:
804 ((struct sockaddr_in *) ap->addr)->sin_port = htons (h->port); 804 ((struct sockaddr_in *) ap->addr)->sin_port = htons (connection->port);
805 break; 805 break;
806 case AF_INET6: 806 case AF_INET6:
807 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (h->port); 807 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (connection->port);
808 break; 808 break;
809 default: 809 default:
810 GNUNET_break (0); 810 GNUNET_break (0);
@@ -818,7 +818,7 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
818 return; /* not supported by OS */ 818 return; /* not supported by OS */
819 } 819 }
820 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), 820 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
821 GNUNET_a2s (ap->addr, ap->addrlen), h); 821 GNUNET_a2s (ap->addr, ap->addrlen), connection);
822 if ((GNUNET_OK != 822 if ((GNUNET_OK !=
823 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) && 823 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
824 (errno != EINPROGRESS)) 824 (errno != EINPROGRESS))
@@ -827,24 +827,24 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
827 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect"); 827 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
828#if 0 828#if 0
829 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to `%s' (%p)\n"), 829 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to `%s' (%p)\n"),
830 GNUNET_a2s (ap->addr, ap->addrlen), h); 830 GNUNET_a2s (ap->addr, ap->addrlen), connection);
831#endif 831#endif
832 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 832 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
833 GNUNET_free (ap); 833 GNUNET_free (ap);
834 return; 834 return;
835 } 835 }
836 GNUNET_CONTAINER_DLL_insert (h->ap_head, h->ap_tail, ap); 836 GNUNET_CONTAINER_DLL_insert (connection->ap_head, connection->ap_tail, ap);
837 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT; 837 delay = GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT;
838 if (h->nth.notify_ready != NULL) 838 if (connection->nth.notify_ready != NULL)
839 delay = 839 delay =
840 GNUNET_TIME_relative_min (delay, 840 GNUNET_TIME_relative_min (delay,
841 GNUNET_TIME_absolute_get_remaining (h-> 841 GNUNET_TIME_absolute_get_remaining (connection->
842 nth.transmit_timeout)); 842 nth.transmit_timeout));
843 if (h->receiver != NULL) 843 if (connection->receiver != NULL)
844 delay = 844 delay =
845 GNUNET_TIME_relative_min (delay, 845 GNUNET_TIME_relative_min (delay,
846 GNUNET_TIME_absolute_get_remaining 846 GNUNET_TIME_absolute_get_remaining
847 (h->receive_timeout)); 847 (connection->receive_timeout));
848 ap->task = 848 ap->task =
849 GNUNET_SCHEDULER_add_write_net (delay, ap->sock, 849 GNUNET_SCHEDULER_add_write_net (delay, ap->sock,
850 &connect_probe_continuation, ap); 850 &connect_probe_continuation, ap);
@@ -852,45 +852,45 @@ try_connect_using_address (void *cls, const struct sockaddr *addr,
852 852
853 853
854/** 854/**
855 * Create a socket handle by (asynchronously) connecting to a host. 855 * Create a connection handle by (asynchronously) connecting to a host.
856 * This function returns immediately, even if the connection has not 856 * This function returns immediately, even if the connection has not
857 * yet been established. This function only creates TCP connections. 857 * yet been established. This function only creates TCP connections.
858 * 858 *
859 * @param cfg configuration to use 859 * @param cfg configuration to use
860 * @param hostname name of the host to connect to 860 * @param hostname name of the host to connect to
861 * @param port port to connect to 861 * @param port port to connect to
862 * @return the socket handle 862 * @return the connection handle
863 */ 863 */
864struct GNUNET_CONNECTION_Handle * 864struct GNUNET_CONNECTION_Handle *
865GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle 865GNUNET_CONNECTION_create_from_connect (const struct GNUNET_CONFIGURATION_Handle
866 *cfg, const char *hostname, 866 *cfg, const char *hostname,
867 uint16_t port) 867 uint16_t port)
868{ 868{
869 struct GNUNET_CONNECTION_Handle *ret; 869 struct GNUNET_CONNECTION_Handle *connection;
870 870
871 GNUNET_assert (0 < strlen (hostname)); /* sanity check */ 871 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
872 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 872 connection = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
873 ret->cfg = cfg; 873 connection->cfg = cfg;
874 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 874 connection->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
875 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size); 875 connection->write_buffer = GNUNET_malloc (connection->write_buffer_size);
876 ret->port = port; 876 connection->port = port;
877 ret->hostname = GNUNET_strdup (hostname); 877 connection->hostname = GNUNET_strdup (hostname);
878 ret->dns_active = 878 connection->dns_active =
879 GNUNET_RESOLVER_ip_get (ret->hostname, AF_UNSPEC, 879 GNUNET_RESOLVER_ip_get (connection->hostname, AF_UNSPEC,
880 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT, 880 GNUNET_CONNECTION_CONNECT_RETRY_TIMEOUT,
881 &try_connect_using_address, ret); 881 &try_connect_using_address, connection);
882 return ret; 882 return connection;
883} 883}
884 884
885 885
886/** 886/**
887 * Create a socket handle by connecting to a UNIX domain service. 887 * Create a connection handle by connecting to a UNIX domain service.
888 * This function returns immediately, even if the connection has not 888 * This function returns immediately, even if the connection has not
889 * yet been established. This function only creates UNIX connections. 889 * yet been established. This function only creates UNIX connections.
890 * 890 *
891 * @param cfg configuration to use 891 * @param cfg configuration to use
892 * @param unixpath path to connect to 892 * @param unixpath path to connect to
893 * @return the socket handle, NULL on systems without UNIX support 893 * @return the connection handle, NULL on systems without UNIX support
894 */ 894 */
895struct GNUNET_CONNECTION_Handle * 895struct GNUNET_CONNECTION_Handle *
896GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct 896GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
@@ -898,7 +898,7 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
898 *cfg, const char *unixpath) 898 *cfg, const char *unixpath)
899{ 899{
900#ifdef AF_UNIX 900#ifdef AF_UNIX
901 struct GNUNET_CONNECTION_Handle *ret; 901 struct GNUNET_CONNECTION_Handle *connection;
902 struct sockaddr_un *un; 902 struct sockaddr_un *un;
903 size_t slen; 903 size_t slen;
904 904
@@ -917,32 +917,32 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
917#if LINUX 917#if LINUX
918 un->sun_path[0] = '\0'; 918 un->sun_path[0] = '\0';
919#endif 919#endif
920 ret = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle)); 920 connection = GNUNET_malloc (sizeof (struct GNUNET_CONNECTION_Handle));
921 ret->cfg = cfg; 921 connection->cfg = cfg;
922 ret->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE; 922 connection->write_buffer_size = GNUNET_SERVER_MIN_BUFFER_SIZE;
923 ret->write_buffer = GNUNET_malloc (ret->write_buffer_size); 923 connection->write_buffer = GNUNET_malloc (connection->write_buffer_size);
924 ret->port = 0; 924 connection->port = 0;
925 ret->hostname = NULL; 925 connection->hostname = NULL;
926 ret->addr = (struct sockaddr *) un; 926 connection->addr = (struct sockaddr *) un;
927 ret->addrlen = slen; 927 connection->addrlen = slen;
928 ret->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); 928 connection->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
929 if (NULL == ret->sock) 929 if (NULL == connection->sock)
930 { 930 {
931 GNUNET_free (ret->addr); 931 GNUNET_free (connection->addr);
932 GNUNET_free (ret->write_buffer); 932 GNUNET_free (connection->write_buffer);
933 GNUNET_free (ret); 933 GNUNET_free (connection);
934 return NULL; 934 return NULL;
935 } 935 }
936 if (GNUNET_OK != 936 if (GNUNET_OK !=
937 GNUNET_NETWORK_socket_connect (ret->sock, ret->addr, ret->addrlen)) 937 GNUNET_NETWORK_socket_connect (connection->sock, connection->addr, connection->addrlen))
938 { 938 {
939 /* Just return; we expect everything to work eventually so don't fail HARD */ 939 /* Just return; we expect everything to work eventually so don't fail HARD */
940 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ret->sock)); 940 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
941 ret->sock = NULL; 941 connection->sock = NULL;
942 return ret; 942 return connection;
943 } 943 }
944 connect_success_continuation (ret); 944 connect_success_continuation (connection);
945 return ret; 945 return connection;
946#else 946#else
947 return NULL; 947 return NULL;
948#endif 948#endif
@@ -950,14 +950,14 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct
950 950
951 951
952/** 952/**
953 * Create a socket handle by (asynchronously) connecting to a host. 953 * Create a connection handle by (asynchronously) connecting to a host.
954 * This function returns immediately, even if the connection has not 954 * This function returns immediately, even if the connection has not
955 * yet been established. This function only creates TCP connections. 955 * yet been established. This function only creates TCP connections.
956 * 956 *
957 * @param af_family address family to use 957 * @param af_family address family to use
958 * @param serv_addr server address 958 * @param serv_addr server address
959 * @param addrlen length of server address 959 * @param addrlen length of server address
960 * @return the socket handle 960 * @return the connection handle
961 */ 961 */
962struct GNUNET_CONNECTION_Handle * 962struct GNUNET_CONNECTION_Handle *
963GNUNET_CONNECTION_create_from_sockaddr (int af_family, 963GNUNET_CONNECTION_create_from_sockaddr (int af_family,
@@ -965,7 +965,7 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
965 socklen_t addrlen) 965 socklen_t addrlen)
966{ 966{
967 struct GNUNET_NETWORK_Handle *s; 967 struct GNUNET_NETWORK_Handle *s;
968 struct GNUNET_CONNECTION_Handle *ret; 968 struct GNUNET_CONNECTION_Handle *connection;
969 969
970 970
971 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0); 971 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
@@ -984,22 +984,22 @@ GNUNET_CONNECTION_create_from_sockaddr (int af_family,
984 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s)); 984 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
985 return NULL; 985 return NULL;
986 } 986 }
987 ret = GNUNET_CONNECTION_create_from_existing (s); 987 connection = GNUNET_CONNECTION_create_from_existing (s);
988 ret->addr = GNUNET_malloc (addrlen); 988 connection->addr = GNUNET_malloc (addrlen);
989 memcpy (ret->addr, serv_addr, addrlen); 989 memcpy (connection->addr, serv_addr, addrlen);
990 ret->addrlen = addrlen; 990 connection->addrlen = addrlen;
991 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"), 991 LOG (GNUNET_ERROR_TYPE_INFO, _("Trying to connect to `%s' (%p)\n"),
992 GNUNET_a2s (serv_addr, addrlen), ret); 992 GNUNET_a2s (serv_addr, addrlen), connection);
993 return ret; 993 return connection;
994} 994}
995 995
996 996
997/** 997/**
998 * Check if socket is valid (no fatal errors have happened so far). 998 * Check if connection is valid (no fatal errors have happened so far).
999 * Note that a socket that is still trying to connect is considered 999 * Note that a connection that is still trying to connect is considered
1000 * valid. 1000 * valid.
1001 * 1001 *
1002 * @param sock socket to check 1002 * @param sock connection to check
1003 * @return GNUNET_YES if valid, GNUNET_NO otherwise 1003 * @return GNUNET_YES if valid, GNUNET_NO otherwise
1004 */ 1004 */
1005int 1005int
@@ -1012,35 +1012,32 @@ GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *sock)
1012 1012
1013 1013
1014/** 1014/**
1015 * Close the socket and free associated resources. Pending 1015 * Close the connection and free associated resources. A pending
1016 * transmissions may be completed or dropped depending on the 1016 * request for transmission is automatically cancelled (we might
1017 * arguments. If a receive call is pending and should 1017 * want to change this in the future). We require that there
1018 * NOT be completed, 'GNUNET_CONNECTION_receive_cancel' 1018 * are no active pending requests for reading from the connection.
1019 * should be called explicitly first.
1020 * 1019 *
1021 * @param sock socket to destroy 1020 * @param connection connection to destroy
1022 */ 1021 */
1023void 1022void
1024GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *sock) 1023GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection)
1025{ 1024{
1026 int finish_pending_write = GNUNET_NO; 1025 GNUNET_assert (NULL == connection->receiver);
1027 if (GNUNET_NO == finish_pending_write) 1026 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->read_task);
1027 if (connection->write_task != GNUNET_SCHEDULER_NO_TASK)
1028 { 1028 {
1029 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 1029 GNUNET_SCHEDULER_cancel (connection->write_task);
1030 { 1030 connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1031 GNUNET_SCHEDULER_cancel (sock->write_task); 1031 connection->write_buffer_off = 0;
1032 sock->write_task = GNUNET_SCHEDULER_NO_TASK;
1033 sock->write_buffer_off = 0;
1034 }
1035 sock->nth.notify_ready = NULL;
1036 } 1032 }
1037 if ((sock->write_buffer_off == 0) && (sock->dns_active != NULL)) 1033 connection->nth.notify_ready = NULL;
1034 if (connection->dns_active != NULL)
1038 { 1035 {
1039 GNUNET_RESOLVER_request_cancel (sock->dns_active); 1036 GNUNET_RESOLVER_request_cancel (connection->dns_active);
1040 sock->dns_active = NULL; 1037 connection->dns_active = NULL;
1041 } 1038 }
1042 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->destroy_task); 1039 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->destroy_task);
1043 sock->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, sock); 1040 connection->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_continuation, connection);
1044} 1041}
1045 1042
1046 1043
@@ -1081,64 +1078,64 @@ signal_error (struct GNUNET_CONNECTION_Handle *sh, int errcode)
1081static void 1078static void
1082receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 1079receive_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1083{ 1080{
1084 struct GNUNET_CONNECTION_Handle *sh = cls; 1081 struct GNUNET_CONNECTION_Handle *connection = cls;
1085 struct GNUNET_TIME_Absolute now; 1082 struct GNUNET_TIME_Absolute now;
1086 char buffer[sh->max]; 1083 char buffer[connection->max];
1087 ssize_t ret; 1084 ssize_t ret;
1088 GNUNET_CONNECTION_Receiver receiver; 1085 GNUNET_CONNECTION_Receiver receiver;
1089 1086
1090 sh->read_task = GNUNET_SCHEDULER_NO_TASK; 1087 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
1091 if ((GNUNET_YES == sh->ignore_shutdown) && 1088 if ((GNUNET_YES == connection->ignore_shutdown) &&
1092 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1089 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1093 { 1090 {
1094 /* ignore shutdown request, go again immediately */ 1091 /* ignore shutdown request, go again immediately */
1095 LOG (GNUNET_ERROR_TYPE_DEBUG, 1092 LOG (GNUNET_ERROR_TYPE_DEBUG,
1096 "Ignoring shutdown signal per configuration\n"); 1093 "Ignoring shutdown signal per configuration\n");
1097 sh->read_task = 1094 connection->read_task =
1098 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1095 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1099 (sh->receive_timeout), sh->sock, 1096 (connection->receive_timeout), connection->sock,
1100 &receive_ready, sh); 1097 &receive_ready, connection);
1101 return; 1098 return;
1102 } 1099 }
1103 now = GNUNET_TIME_absolute_get (); 1100 now = GNUNET_TIME_absolute_get ();
1104 if ((now.abs_value > sh->receive_timeout.abs_value) || 1101 if ((now.abs_value > connection->receive_timeout.abs_value) ||
1105 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) || 1102 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) ||
1106 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1103 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1107 { 1104 {
1108 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1105 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1109 LOG (GNUNET_ERROR_TYPE_DEBUG, 1106 LOG (GNUNET_ERROR_TYPE_DEBUG,
1110 "Receive from `%s' encounters error: time out by %llums... (%p)\n", 1107 "Receive from `%s' encounters error: time out by %llums... (%p)\n",
1111 GNUNET_a2s (sh->addr, sh->addrlen), 1108 GNUNET_a2s (connection->addr, connection->addrlen),
1112 GNUNET_TIME_absolute_get_duration (sh->receive_timeout).rel_value, 1109 GNUNET_TIME_absolute_get_duration (connection->receive_timeout).rel_value,
1113 sh); 1110 connection);
1114 signal_timeout (sh); 1111 signal_timeout (connection);
1115 return; 1112 return;
1116 } 1113 }
1117 if (sh->sock == NULL) 1114 if (connection->sock == NULL)
1118 { 1115 {
1119 /* connect failed for good */ 1116 /* connect failed for good */
1120 LOG (GNUNET_ERROR_TYPE_DEBUG, 1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1121 "Receive encounters error, socket closed... (%p)\n", sh); 1118 "Receive encounters error, connection closed... (%p)\n", connection);
1122 signal_error (sh, ECONNREFUSED); 1119 signal_error (connection, ECONNREFUSED);
1123 return; 1120 return;
1124 } 1121 }
1125 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, sh->sock)); 1122 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, connection->sock));
1126RETRY: 1123RETRY:
1127 ret = GNUNET_NETWORK_socket_recv (sh->sock, buffer, sh->max); 1124 ret = GNUNET_NETWORK_socket_recv (connection->sock, buffer, connection->max);
1128 if (ret == -1) 1125 if (ret == -1)
1129 { 1126 {
1130 if (errno == EINTR) 1127 if (errno == EINTR)
1131 goto RETRY; 1128 goto RETRY;
1132 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno)); 1129 LOG (GNUNET_ERROR_TYPE_DEBUG, "Error receiving: %s\n", STRERROR (errno));
1133 signal_error (sh, errno); 1130 signal_error (connection, errno);
1134 return; 1131 return;
1135 } 1132 }
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, 1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1137 "receive_ready read %u/%u bytes from `%s' (%p)!\n", (unsigned int) ret, 1134 "receive_ready read %u/%u bytes from `%s' (%p)!\n", (unsigned int) ret,
1138 sh->max, GNUNET_a2s (sh->addr, sh->addrlen), sh); 1135 connection->max, GNUNET_a2s (connection->addr, connection->addrlen), connection);
1139 GNUNET_assert (NULL != (receiver = sh->receiver)); 1136 GNUNET_assert (NULL != (receiver = connection->receiver));
1140 sh->receiver = NULL; 1137 connection->receiver = NULL;
1141 receiver (sh->receiver_cls, buffer, ret, sh->addr, sh->addrlen, 0); 1138 receiver (connection->receiver_cls, buffer, ret, connection->addr, connection->addrlen, 0);
1142} 1139}
1143 1140
1144 1141
@@ -1155,117 +1152,117 @@ RETRY:
1155static void 1152static void
1156receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 1153receive_again (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1157{ 1154{
1158 struct GNUNET_CONNECTION_Handle *sh = cls; 1155 struct GNUNET_CONNECTION_Handle *connection = cls;
1159 struct GNUNET_TIME_Absolute now; 1156 struct GNUNET_TIME_Absolute now;
1160 1157
1161 sh->read_task = GNUNET_SCHEDULER_NO_TASK; 1158 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
1162 if (sh->sock == NULL) 1159 if (connection->sock == NULL)
1163 { 1160 {
1164 /* not connected and no longer trying */ 1161 /* not connected and no longer trying */
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, 1162 LOG (GNUNET_ERROR_TYPE_DEBUG,
1166 "Receive encounters error, socket closed (%p)...\n", sh); 1163 "Receive encounters error, connection closed (%p)...\n", connection);
1167 signal_error (sh, ECONNREFUSED); 1164 signal_error (connection, ECONNREFUSED);
1168 return; 1165 return;
1169 } 1166 }
1170 now = GNUNET_TIME_absolute_get (); 1167 now = GNUNET_TIME_absolute_get ();
1171 if ((now.abs_value > sh->receive_timeout.abs_value) || 1168 if ((now.abs_value > connection->receive_timeout.abs_value) ||
1172 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))) 1169 (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)))
1173 { 1170 {
1174 LOG (GNUNET_ERROR_TYPE_DEBUG, 1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "Receive encounters error: time out (%p)...\n", sh); 1172 "Receive encounters error: time out (%p)...\n", connection);
1176 signal_timeout (sh); 1173 signal_timeout (connection);
1177 return; 1174 return;
1178 } 1175 }
1179 GNUNET_assert (sh->sock != NULL); 1176 GNUNET_assert (connection->sock != NULL);
1180 /* connect succeeded, wait for data! */ 1177 /* connect succeeded, wait for data! */
1181 sh->read_task = 1178 connection->read_task =
1182 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining 1179 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining
1183 (sh->receive_timeout), sh->sock, 1180 (connection->receive_timeout), connection->sock,
1184 &receive_ready, sh); 1181 &receive_ready, connection);
1185} 1182}
1186 1183
1187 1184
1188/** 1185/**
1189 * Receive data from the given socket. Note that this function will 1186 * Receive data from the given connection. Note that this function will
1190 * call "receiver" asynchronously using the scheduler. It will 1187 * call "receiver" asynchronously using the scheduler. It will
1191 * "immediately" return. Note that there MUST only be one active 1188 * "immediately" return. Note that there MUST only be one active
1192 * receive call per socket at any given point in time (so do not 1189 * receive call per connection at any given point in time (so do not
1193 * call receive again until the receiver callback has been invoked). 1190 * call receive again until the receiver callback has been invoked).
1194 * 1191 *
1195 * @param sock socket handle 1192 * @param connection connection handle
1196 * @param max maximum number of bytes to read 1193 * @param max maximum number of bytes to read
1197 * @param timeout maximum amount of time to wait (use -1 for "forever") 1194 * @param timeout maximum amount of time to wait (use -1 for "forever")
1198 * @param receiver function to call with received data 1195 * @param receiver function to call with received data
1199 * @param receiver_cls closure for receiver 1196 * @param receiver_cls closure for receiver
1200 */ 1197 */
1201void 1198void
1202GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *sock, size_t max, 1199GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *connection, size_t max,
1203 struct GNUNET_TIME_Relative timeout, 1200 struct GNUNET_TIME_Relative timeout,
1204 GNUNET_CONNECTION_Receiver receiver, 1201 GNUNET_CONNECTION_Receiver receiver,
1205 void *receiver_cls) 1202 void *receiver_cls)
1206{ 1203{
1207 struct GNUNET_SCHEDULER_TaskContext tc; 1204 struct GNUNET_SCHEDULER_TaskContext tc;
1208 1205
1209 GNUNET_assert ((sock->read_task == GNUNET_SCHEDULER_NO_TASK) && 1206 GNUNET_assert ((connection->read_task == GNUNET_SCHEDULER_NO_TASK) &&
1210 (0 == (sock->ccs & COCO_RECEIVE_AGAIN)) && 1207 (0 == (connection->ccs & COCO_RECEIVE_AGAIN)) &&
1211 (sock->receiver == NULL)); 1208 (connection->receiver == NULL));
1212 sock->receiver = receiver; 1209 connection->receiver = receiver;
1213 sock->receiver_cls = receiver_cls; 1210 connection->receiver_cls = receiver_cls;
1214 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout); 1211 connection->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1215 sock->max = max; 1212 connection->max = max;
1216 if (sock->sock != NULL) 1213 if (connection->sock != NULL)
1217 { 1214 {
1218 memset (&tc, 0, sizeof (tc)); 1215 memset (&tc, 0, sizeof (tc));
1219 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE; 1216 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1220 receive_again (sock, &tc); 1217 receive_again (connection, &tc);
1221 return; 1218 return;
1222 } 1219 }
1223 if ((sock->dns_active == NULL) && (sock->ap_head == NULL)) 1220 if ((connection->dns_active == NULL) && (connection->ap_head == NULL))
1224 { 1221 {
1225 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); 1222 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1226 return; 1223 return;
1227 } 1224 }
1228 sock->ccs += COCO_RECEIVE_AGAIN; 1225 connection->ccs += COCO_RECEIVE_AGAIN;
1229} 1226}
1230 1227
1231 1228
1232/** 1229/**
1233 * Configure this connection to ignore shutdown signals. 1230 * Configure this connection to ignore shutdown signals.
1234 * 1231 *
1235 * @param sock socket handle 1232 * @param connection connection handle
1236 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default 1233 * @param do_ignore GNUNET_YES to ignore, GNUNET_NO to restore default
1237 */ 1234 */
1238void 1235void
1239GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *sock, 1236GNUNET_CONNECTION_ignore_shutdown (struct GNUNET_CONNECTION_Handle *connection,
1240 int do_ignore) 1237 int do_ignore)
1241{ 1238{
1242 sock->ignore_shutdown = do_ignore; 1239 connection->ignore_shutdown = do_ignore;
1243} 1240}
1244 1241
1245 1242
1246/** 1243/**
1247 * Cancel receive job on the given socket. Note that the 1244 * Cancel receive job on the given connection. Note that the
1248 * receiver callback must not have been called yet in order 1245 * receiver callback must not have been called yet in order
1249 * for the cancellation to be valid. 1246 * for the cancellation to be valid.
1250 * 1247 *
1251 * @param sock socket handle 1248 * @param connection connection handle
1252 * @return closure of the original receiver callback closure 1249 * @return closure of the original receiver callback closure
1253 */ 1250 */
1254void * 1251void *
1255GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock) 1252GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection)
1256{ 1253{
1257 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 1254 if (connection->read_task != GNUNET_SCHEDULER_NO_TASK)
1258 { 1255 {
1259 GNUNET_assert (sock == GNUNET_SCHEDULER_cancel (sock->read_task)); 1256 GNUNET_assert (connection == GNUNET_SCHEDULER_cancel (connection->read_task));
1260 sock->read_task = GNUNET_SCHEDULER_NO_TASK; 1257 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
1261 } 1258 }
1262 else 1259 else
1263 { 1260 {
1264 GNUNET_assert (0 != (sock->ccs & COCO_RECEIVE_AGAIN)); 1261 GNUNET_assert (0 != (connection->ccs & COCO_RECEIVE_AGAIN));
1265 sock->ccs -= COCO_RECEIVE_AGAIN; 1262 connection->ccs -= COCO_RECEIVE_AGAIN;
1266 } 1263 }
1267 sock->receiver = NULL; 1264 connection->receiver = NULL;
1268 return sock->receiver_cls; 1265 return connection->receiver_cls;
1269} 1266}
1270 1267
1271 1268
@@ -1273,41 +1270,41 @@ GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *sock)
1273 * Try to call the transmit notify method (check if we do 1270 * Try to call the transmit notify method (check if we do
1274 * have enough space available first)! 1271 * have enough space available first)!
1275 * 1272 *
1276 * @param sock socket for which we should do this processing 1273 * @param connection connection for which we should do this processing
1277 * @return GNUNET_YES if we were able to call notify 1274 * @return GNUNET_YES if we were able to call notify
1278 */ 1275 */
1279static int 1276static int
1280process_notify (struct GNUNET_CONNECTION_Handle *sock) 1277process_notify (struct GNUNET_CONNECTION_Handle *connection)
1281{ 1278{
1282 size_t used; 1279 size_t used;
1283 size_t avail; 1280 size_t avail;
1284 size_t size; 1281 size_t size;
1285 GNUNET_CONNECTION_TransmitReadyNotify notify; 1282 GNUNET_CONNECTION_TransmitReadyNotify notify;
1286 1283
1287 GNUNET_assert (sock->write_task == GNUNET_SCHEDULER_NO_TASK); 1284 GNUNET_assert (connection->write_task == GNUNET_SCHEDULER_NO_TASK);
1288 if (NULL == (notify = sock->nth.notify_ready)) 1285 if (NULL == (notify = connection->nth.notify_ready))
1289 return GNUNET_NO; 1286 return GNUNET_NO;
1290 used = sock->write_buffer_off - sock->write_buffer_pos; 1287 used = connection->write_buffer_off - connection->write_buffer_pos;
1291 avail = sock->write_buffer_size - used; 1288 avail = connection->write_buffer_size - used;
1292 size = sock->nth.notify_size; 1289 size = connection->nth.notify_size;
1293 if (size > avail) 1290 if (size > avail)
1294 return GNUNET_NO; 1291 return GNUNET_NO;
1295 sock->nth.notify_ready = NULL; 1292 connection->nth.notify_ready = NULL;
1296 if (sock->write_buffer_size - sock->write_buffer_off < size) 1293 if (connection->write_buffer_size - connection->write_buffer_off < size)
1297 { 1294 {
1298 /* need to compact */ 1295 /* need to compact */
1299 memmove (sock->write_buffer, &sock->write_buffer[sock->write_buffer_pos], 1296 memmove (connection->write_buffer, &connection->write_buffer[connection->write_buffer_pos],
1300 used); 1297 used);
1301 sock->write_buffer_off -= sock->write_buffer_pos; 1298 connection->write_buffer_off -= connection->write_buffer_pos;
1302 sock->write_buffer_pos = 0; 1299 connection->write_buffer_pos = 0;
1303 } 1300 }
1304 avail = sock->write_buffer_size - sock->write_buffer_off; 1301 avail = connection->write_buffer_size - connection->write_buffer_off;
1305 GNUNET_assert (avail >= size); 1302 GNUNET_assert (avail >= size);
1306 size = 1303 size =
1307 notify (sock->nth.notify_ready_cls, avail, 1304 notify (connection->nth.notify_ready_cls, avail,
1308 &sock->write_buffer[sock->write_buffer_off]); 1305 &connection->write_buffer[connection->write_buffer_off]);
1309 GNUNET_assert (size <= avail); 1306 GNUNET_assert (size <= avail);
1310 sock->write_buffer_off += size; 1307 connection->write_buffer_off += size;
1311 return GNUNET_YES; 1308 return GNUNET_YES;
1312} 1309}
1313 1310
@@ -1326,19 +1323,19 @@ process_notify (struct GNUNET_CONNECTION_Handle *sock)
1326static void 1323static void
1327transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 1324transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1328{ 1325{
1329 struct GNUNET_CONNECTION_Handle *sock = cls; 1326 struct GNUNET_CONNECTION_Handle *connection = cls;
1330 GNUNET_CONNECTION_TransmitReadyNotify notify; 1327 GNUNET_CONNECTION_TransmitReadyNotify notify;
1331 1328
1332 sock->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK; 1329 connection->nth.timeout_task = GNUNET_SCHEDULER_NO_TASK;
1333 LOG (GNUNET_ERROR_TYPE_DEBUG, 1330 LOG (GNUNET_ERROR_TYPE_DEBUG,
1334 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", 1331 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1335 sock->hostname, 1332 connection->hostname,
1336 sock->port, GNUNET_a2s (sock->addr, sock->addrlen), sock); 1333 connection->port, GNUNET_a2s (connection->addr, connection->addrlen), connection);
1337 GNUNET_assert (0 != (sock->ccs & COCO_TRANSMIT_READY)); 1334 GNUNET_assert (0 != (connection->ccs & COCO_TRANSMIT_READY));
1338 sock->ccs -= COCO_TRANSMIT_READY; /* remove request */ 1335 connection->ccs -= COCO_TRANSMIT_READY; /* remove request */
1339 notify = sock->nth.notify_ready; 1336 notify = connection->nth.notify_ready;
1340 sock->nth.notify_ready = NULL; 1337 connection->nth.notify_ready = NULL;
1341 notify (sock->nth.notify_ready_cls, 0, NULL); 1338 notify (connection->nth.notify_ready_cls, 0, NULL);
1342} 1339}
1343 1340
1344 1341
@@ -1354,48 +1351,48 @@ transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1354static void 1351static void
1355connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 1352connect_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1356{ 1353{
1357 struct GNUNET_CONNECTION_Handle *sock = cls; 1354 struct GNUNET_CONNECTION_Handle *connection = cls;
1358 GNUNET_CONNECTION_TransmitReadyNotify notify; 1355 GNUNET_CONNECTION_TransmitReadyNotify notify;
1359 1356
1360 LOG (GNUNET_ERROR_TYPE_DEBUG, 1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1361 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n", 1358 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1362 sock->nth.notify_size, sock->hostname, sock->port, sock); 1359 connection->nth.notify_size, connection->hostname, connection->port, connection);
1363 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1360 connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1364 notify = sock->nth.notify_ready; 1361 notify = connection->nth.notify_ready;
1365 sock->nth.notify_ready = NULL; 1362 connection->nth.notify_ready = NULL;
1366 notify (sock->nth.notify_ready_cls, 0, NULL); 1363 notify (connection->nth.notify_ready_cls, 0, NULL);
1367} 1364}
1368 1365
1369 1366
1370/** 1367/**
1371 * FIXME 1368 * FIXME
1372 * 1369 *
1373 * @param sock FIXME 1370 * @param connection FIXME
1374 */ 1371 */
1375static void 1372static void
1376transmit_error (struct GNUNET_CONNECTION_Handle *sock) 1373transmit_error (struct GNUNET_CONNECTION_Handle *connection)
1377{ 1374{
1378 GNUNET_CONNECTION_TransmitReadyNotify notify; 1375 GNUNET_CONNECTION_TransmitReadyNotify notify;
1379 1376
1380 if (NULL != sock->sock) 1377 if (NULL != connection->sock)
1381 { 1378 {
1382 GNUNET_NETWORK_socket_shutdown (sock->sock, SHUT_RDWR); 1379 GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR);
1383 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock->sock)); 1380 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
1384 sock->sock = NULL; 1381 connection->sock = NULL;
1385 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task); 1382 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
1386 } 1383 }
1387 if (sock->read_task != GNUNET_SCHEDULER_NO_TASK) 1384 if (connection->read_task != GNUNET_SCHEDULER_NO_TASK)
1388 { 1385 {
1389 GNUNET_SCHEDULER_cancel (sock->read_task); 1386 GNUNET_SCHEDULER_cancel (connection->read_task);
1390 sock->read_task = GNUNET_SCHEDULER_NO_TASK; 1387 connection->read_task = GNUNET_SCHEDULER_NO_TASK;
1391 signal_timeout (sock); 1388 signal_timeout (connection);
1392 return; 1389 return;
1393 } 1390 }
1394 if (sock->nth.notify_ready == NULL) 1391 if (connection->nth.notify_ready == NULL)
1395 return; /* nobody to tell about it */ 1392 return; /* nobody to tell about it */
1396 notify = sock->nth.notify_ready; 1393 notify = connection->nth.notify_ready;
1397 sock->nth.notify_ready = NULL; 1394 connection->nth.notify_ready = NULL;
1398 notify (sock->nth.notify_ready_cls, 0, NULL); 1395 notify (connection->nth.notify_ready_cls, 0, NULL);
1399} 1396}
1400 1397
1401 1398
@@ -1410,27 +1407,27 @@ transmit_error (struct GNUNET_CONNECTION_Handle *sock)
1410static void 1407static void
1411transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 1408transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1412{ 1409{
1413 struct GNUNET_CONNECTION_Handle *sock = cls; 1410 struct GNUNET_CONNECTION_Handle *connection = cls;
1414 GNUNET_CONNECTION_TransmitReadyNotify notify; 1411 GNUNET_CONNECTION_TransmitReadyNotify notify;
1415 ssize_t ret; 1412 ssize_t ret;
1416 size_t have; 1413 size_t have;
1417 1414
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", sock); 1415 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", connection);
1419 GNUNET_assert (sock->write_task != GNUNET_SCHEDULER_NO_TASK); 1416 GNUNET_assert (connection->write_task != GNUNET_SCHEDULER_NO_TASK);
1420 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1417 connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1421 GNUNET_assert (sock->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK); 1418 GNUNET_assert (connection->nth.timeout_task == GNUNET_SCHEDULER_NO_TASK);
1422 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) 1419 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1423 { 1420 {
1424 if ((sock->ignore_shutdown == GNUNET_YES) && (NULL != sock->sock)) 1421 if ((connection->ignore_shutdown == GNUNET_YES) && (NULL != connection->sock))
1425 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */ 1422 goto SCHEDULE_WRITE; /* ignore shutdown, go again immediately */
1426 LOG (GNUNET_ERROR_TYPE_DEBUG, 1423 LOG (GNUNET_ERROR_TYPE_DEBUG,
1427 "Transmit to `%s' fails, shutdown happened (%p).\n", 1424 "Transmit to `%s' fails, shutdown happened (%p).\n",
1428 GNUNET_a2s (sock->addr, sock->addrlen), sock); 1425 GNUNET_a2s (connection->addr, connection->addrlen), connection);
1429 notify = sock->nth.notify_ready; 1426 notify = connection->nth.notify_ready;
1430 if (NULL != notify) 1427 if (NULL != notify)
1431 { 1428 {
1432 sock->nth.notify_ready = NULL; 1429 connection->nth.notify_ready = NULL;
1433 notify (sock->nth.notify_ready_cls, 0, NULL); 1430 notify (connection->nth.notify_ready_cls, 0, NULL);
1434 } 1431 }
1435 return; 1432 return;
1436 } 1433 }
@@ -1438,14 +1435,14 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1438 { 1435 {
1439 LOG (GNUNET_ERROR_TYPE_DEBUG, 1436 LOG (GNUNET_ERROR_TYPE_DEBUG,
1440 "Transmit to `%s' fails, time out reached (%p).\n", 1437 "Transmit to `%s' fails, time out reached (%p).\n",
1441 GNUNET_a2s (sock->addr, sock->addrlen), sock); 1438 GNUNET_a2s (connection->addr, connection->addrlen), connection);
1442 notify = sock->nth.notify_ready; 1439 notify = connection->nth.notify_ready;
1443 GNUNET_assert (NULL != notify); 1440 GNUNET_assert (NULL != notify);
1444 sock->nth.notify_ready = NULL; 1441 connection->nth.notify_ready = NULL;
1445 notify (sock->nth.notify_ready_cls, 0, NULL); 1442 notify (connection->nth.notify_ready_cls, 0, NULL);
1446 return; 1443 return;
1447 } 1444 }
1448 GNUNET_assert (NULL != sock->sock); 1445 GNUNET_assert (NULL != connection->sock);
1449 if (tc->write_ready == NULL) 1446 if (tc->write_ready == NULL)
1450 { 1447 {
1451 /* special circumstances (in particular, 1448 /* special circumstances (in particular,
@@ -1453,86 +1450,86 @@ transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1453 * but no "fatal" error either. Hence retry. */ 1450 * but no "fatal" error either. Hence retry. */
1454 goto SCHEDULE_WRITE; 1451 goto SCHEDULE_WRITE;
1455 } 1452 }
1456 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, sock->sock)) 1453 if (!GNUNET_NETWORK_fdset_isset (tc->write_ready, connection->sock))
1457 { 1454 {
1458 LOG (GNUNET_ERROR_TYPE_INFO, 1455 LOG (GNUNET_ERROR_TYPE_INFO,
1459 _ 1456 _
1460 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"), 1457 ("Could not satisfy pending transmission request, socket closed or connect failed (%p).\n"),
1461 sock); 1458 connection);
1462 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->write_task); 1459 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->write_task);
1463 transmit_error (sock); 1460 transmit_error (connection);
1464 return; /* connect failed for good, we're finished */ 1461 return; /* connect failed for good, we're finished */
1465 } 1462 }
1466 GNUNET_assert (sock->write_buffer_off >= sock->write_buffer_pos); 1463 GNUNET_assert (connection->write_buffer_off >= connection->write_buffer_pos);
1467 if ((sock->nth.notify_ready != NULL) && 1464 if ((connection->nth.notify_ready != NULL) &&
1468 (sock->write_buffer_size < sock->nth.notify_size)) 1465 (connection->write_buffer_size < connection->nth.notify_size))
1469 { 1466 {
1470 sock->write_buffer = 1467 connection->write_buffer =
1471 GNUNET_realloc (sock->write_buffer, sock->nth.notify_size); 1468 GNUNET_realloc (connection->write_buffer, connection->nth.notify_size);
1472 sock->write_buffer_size = sock->nth.notify_size; 1469 connection->write_buffer_size = connection->nth.notify_size;
1473 } 1470 }
1474 process_notify (sock); 1471 process_notify (connection);
1475 have = sock->write_buffer_off - sock->write_buffer_pos; 1472 have = connection->write_buffer_off - connection->write_buffer_pos;
1476 if (have == 0) 1473 if (have == 0)
1477 { 1474 {
1478 /* no data ready for writing, terminate write loop */ 1475 /* no data ready for writing, terminate write loop */
1479 return; 1476 return;
1480 } 1477 }
1481 GNUNET_assert (have <= sock->write_buffer_size); 1478 GNUNET_assert (have <= connection->write_buffer_size);
1482 GNUNET_assert (have + sock->write_buffer_pos <= sock->write_buffer_size); 1479 GNUNET_assert (have + connection->write_buffer_pos <= connection->write_buffer_size);
1483 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size); 1480 GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_size);
1484RETRY: 1481RETRY:
1485 ret = 1482 ret =
1486 GNUNET_NETWORK_socket_send (sock->sock, 1483 GNUNET_NETWORK_socket_send (connection->sock,
1487 &sock->write_buffer[sock->write_buffer_pos], 1484 &connection->write_buffer[connection->write_buffer_pos],
1488 have); 1485 have);
1489 if (ret == -1) 1486 if (ret == -1)
1490 { 1487 {
1491 if (errno == EINTR) 1488 if (errno == EINTR)
1492 goto RETRY; 1489 goto RETRY;
1493 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send"); 1490 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "send");
1494 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task) 1491 if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
1495 { 1492 {
1496 GNUNET_SCHEDULER_cancel (sock->write_task); 1493 GNUNET_SCHEDULER_cancel (connection->write_task);
1497 sock->write_task = GNUNET_SCHEDULER_NO_TASK; 1494 connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1498 } 1495 }
1499 transmit_error (sock); 1496 transmit_error (connection);
1500 return; 1497 return;
1501 } 1498 }
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, 1499 LOG (GNUNET_ERROR_TYPE_DEBUG,
1503 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n", 1500 "transmit_ready transmitted %u/%u bytes to `%s' (%p)\n",
1504 (unsigned int) ret, have, GNUNET_a2s (sock->addr, sock->addrlen), sock); 1501 (unsigned int) ret, have, GNUNET_a2s (connection->addr, connection->addrlen), connection);
1505 sock->write_buffer_pos += ret; 1502 connection->write_buffer_pos += ret;
1506 if (sock->write_buffer_pos == sock->write_buffer_off) 1503 if (connection->write_buffer_pos == connection->write_buffer_off)
1507 { 1504 {
1508 /* transmitted all pending data */ 1505 /* transmitted all pending data */
1509 sock->write_buffer_pos = 0; 1506 connection->write_buffer_pos = 0;
1510 sock->write_buffer_off = 0; 1507 connection->write_buffer_off = 0;
1511 } 1508 }
1512 if ((sock->write_buffer_off == 0) && (NULL == sock->nth.notify_ready)) 1509 if ((connection->write_buffer_off == 0) && (NULL == connection->nth.notify_ready))
1513 return; /* all data sent! */ 1510 return; /* all data sent! */
1514 /* not done writing, schedule more */ 1511 /* not done writing, schedule more */
1515SCHEDULE_WRITE: 1512SCHEDULE_WRITE:
1516 LOG (GNUNET_ERROR_TYPE_DEBUG, 1513 LOG (GNUNET_ERROR_TYPE_DEBUG,
1517 "Re-scheduling transmit_ready (more to do) (%p).\n", sock); 1514 "Re-scheduling transmit_ready (more to do) (%p).\n", connection);
1518 have = sock->write_buffer_off - sock->write_buffer_pos; 1515 have = connection->write_buffer_off - connection->write_buffer_pos;
1519 GNUNET_assert ((sock->nth.notify_ready != NULL) || (have > 0)); 1516 GNUNET_assert ((connection->nth.notify_ready != NULL) || (have > 0));
1520 if (sock->write_task == GNUNET_SCHEDULER_NO_TASK) 1517 if (connection->write_task == GNUNET_SCHEDULER_NO_TASK)
1521 sock->write_task = 1518 connection->write_task =
1522 GNUNET_SCHEDULER_add_write_net ((sock->nth.notify_ready == 1519 GNUNET_SCHEDULER_add_write_net ((connection->nth.notify_ready ==
1523 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL : 1520 NULL) ? GNUNET_TIME_UNIT_FOREVER_REL :
1524 GNUNET_TIME_absolute_get_remaining 1521 GNUNET_TIME_absolute_get_remaining
1525 (sock->nth.transmit_timeout), 1522 (connection->nth.transmit_timeout),
1526 sock->sock, &transmit_ready, sock); 1523 connection->sock, &transmit_ready, connection);
1527} 1524}
1528 1525
1529 1526
1530/** 1527/**
1531 * Ask the socket to call us once the specified number of bytes 1528 * Ask the connection to call us once the specified number of bytes
1532 * are free in the transmission buffer. May call the notify 1529 * are free in the transmission buffer. May call the notify
1533 * method immediately if enough space is available. 1530 * method immediately if enough space is available.
1534 * 1531 *
1535 * @param sock socket 1532 * @param connection connection
1536 * @param size number of bytes to send 1533 * @param size number of bytes to send
1537 * @param timeout after how long should we give up (and call 1534 * @param timeout after how long should we give up (and call
1538 * notify with buf NULL and size 0)? 1535 * notify with buf NULL and size 0)?
@@ -1542,55 +1539,55 @@ SCHEDULE_WRITE:
1542 * NULL if we are already going to notify someone else (busy) 1539 * NULL if we are already going to notify someone else (busy)
1543 */ 1540 */
1544struct GNUNET_CONNECTION_TransmitHandle * 1541struct GNUNET_CONNECTION_TransmitHandle *
1545GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *sock, 1542GNUNET_CONNECTION_notify_transmit_ready (struct GNUNET_CONNECTION_Handle *connection,
1546 size_t size, 1543 size_t size,
1547 struct GNUNET_TIME_Relative timeout, 1544 struct GNUNET_TIME_Relative timeout,
1548 GNUNET_CONNECTION_TransmitReadyNotify 1545 GNUNET_CONNECTION_TransmitReadyNotify
1549 notify, void *notify_cls) 1546 notify, void *notify_cls)
1550{ 1547{
1551 if (sock->nth.notify_ready != NULL) 1548 if (connection->nth.notify_ready != NULL)
1552 { 1549 {
1553 GNUNET_assert (0); 1550 GNUNET_assert (0);
1554 return NULL; 1551 return NULL;
1555 } 1552 }
1556 GNUNET_assert (notify != NULL); 1553 GNUNET_assert (notify != NULL);
1557 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); 1554 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1558 GNUNET_assert (sock->write_buffer_off <= sock->write_buffer_size); 1555 GNUNET_assert (connection->write_buffer_off <= connection->write_buffer_size);
1559 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_size); 1556 GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_size);
1560 GNUNET_assert (sock->write_buffer_pos <= sock->write_buffer_off); 1557 GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_off);
1561 sock->nth.notify_ready = notify; 1558 connection->nth.notify_ready = notify;
1562 sock->nth.notify_ready_cls = notify_cls; 1559 connection->nth.notify_ready_cls = notify_cls;
1563 sock->nth.sh = sock; 1560 connection->nth.connection = connection;
1564 sock->nth.notify_size = size; 1561 connection->nth.notify_size = size;
1565 sock->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout); 1562 connection->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1566 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == sock->nth.timeout_task); 1563 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == connection->nth.timeout_task);
1567 if ((sock->sock == NULL) && (sock->ap_head == NULL) && 1564 if ((connection->sock == NULL) && (connection->ap_head == NULL) &&
1568 (sock->dns_active == NULL)) 1565 (connection->dns_active == NULL))
1569 { 1566 {
1570 if (sock->write_task != GNUNET_SCHEDULER_NO_TASK) 1567 if (connection->write_task != GNUNET_SCHEDULER_NO_TASK)
1571 GNUNET_SCHEDULER_cancel (sock->write_task); 1568 GNUNET_SCHEDULER_cancel (connection->write_task);
1572 sock->write_task = GNUNET_SCHEDULER_add_now (&connect_error, sock); 1569 connection->write_task = GNUNET_SCHEDULER_add_now (&connect_error, connection);
1573 return &sock->nth; 1570 return &connection->nth;
1574 } 1571 }
1575 if (GNUNET_SCHEDULER_NO_TASK != sock->write_task) 1572 if (GNUNET_SCHEDULER_NO_TASK != connection->write_task)
1576 return &sock->nth; 1573 return &connection->nth;
1577 if (sock->sock != NULL) 1574 if (connection->sock != NULL)
1578 { 1575 {
1579 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", sock); 1576 LOG (GNUNET_ERROR_TYPE_DEBUG, "Scheduling transmit_ready (%p).\n", connection);
1580 sock->write_task = 1577 connection->write_task =
1581 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining 1578 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining
1582 (sock->nth.transmit_timeout), 1579 (connection->nth.transmit_timeout),
1583 sock->sock, &transmit_ready, sock); 1580 connection->sock, &transmit_ready, connection);
1584 } 1581 }
1585 else 1582 else
1586 { 1583 {
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, 1584 LOG (GNUNET_ERROR_TYPE_DEBUG,
1588 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", sock); 1585 "CCS-Scheduling transmit_ready, adding timeout task (%p).\n", connection);
1589 sock->ccs |= COCO_TRANSMIT_READY; 1586 connection->ccs |= COCO_TRANSMIT_READY;
1590 sock->nth.timeout_task = 1587 connection->nth.timeout_task =
1591 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, sock); 1588 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, connection);
1592 } 1589 }
1593 return &sock->nth; 1590 return &connection->nth;
1594} 1591}
1595 1592
1596 1593
@@ -1605,20 +1602,20 @@ GNUNET_CONNECTION_notify_transmit_ready_cancel (struct
1605 *th) 1602 *th)
1606{ 1603{
1607 GNUNET_assert (th->notify_ready != NULL); 1604 GNUNET_assert (th->notify_ready != NULL);
1608 if (0 != (th->sh->ccs & COCO_TRANSMIT_READY)) 1605 if (0 != (th->connection->ccs & COCO_TRANSMIT_READY))
1609 { 1606 {
1610 LOG (GNUNET_ERROR_TYPE_DEBUG, 1607 LOG (GNUNET_ERROR_TYPE_DEBUG,
1611 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th); 1608 "notify_transmit_ready_cancel cancels timeout_task (%p)\n", th);
1612 GNUNET_SCHEDULER_cancel (th->timeout_task); 1609 GNUNET_SCHEDULER_cancel (th->timeout_task);
1613 th->timeout_task = GNUNET_SCHEDULER_NO_TASK; 1610 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1614 th->sh->ccs -= COCO_TRANSMIT_READY; 1611 th->connection->ccs -= COCO_TRANSMIT_READY;
1615 } 1612 }
1616 else 1613 else
1617 { 1614 {
1618 if (th->sh->write_task != GNUNET_SCHEDULER_NO_TASK) 1615 if (th->connection->write_task != GNUNET_SCHEDULER_NO_TASK)
1619 { 1616 {
1620 GNUNET_SCHEDULER_cancel (th->sh->write_task); 1617 GNUNET_SCHEDULER_cancel (th->connection->write_task);
1621 th->sh->write_task = GNUNET_SCHEDULER_NO_TASK; 1618 th->connection->write_task = GNUNET_SCHEDULER_NO_TASK;
1622 } 1619 }
1623 } 1620 }
1624 th->notify_ready = NULL; 1621 th->notify_ready = NULL;