aboutsummaryrefslogtreecommitdiff
path: root/src/transport/tcp_connection_legacy.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/transport/tcp_connection_legacy.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/transport/tcp_connection_legacy.c')
-rw-r--r--src/transport/tcp_connection_legacy.c1304
1 files changed, 654 insertions, 650 deletions
diff --git a/src/transport/tcp_connection_legacy.c b/src/transport/tcp_connection_legacy.c
index 42fe6a8a6..8eea664c4 100644
--- a/src/transport/tcp_connection_legacy.c
+++ b/src/transport/tcp_connection_legacy.c
@@ -42,17 +42,18 @@
42 * Default is 5s. 42 * Default is 5s.
43 */ 43 */
44#define CONNECT_RETRY_TIMEOUT \ 44#define CONNECT_RETRY_TIMEOUT \
45 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) 45 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
46 46
47 47
48#define LOG_STRERROR(kind, syscall) \ 48#define LOG_STRERROR(kind, syscall) \
49 GNUNET_log_from_strerror(kind, "util-connection", syscall) 49 GNUNET_log_from_strerror (kind, "util-connection", syscall)
50 50
51 51
52/** 52/**
53 * Transmission handle. There can only be one for each connection. 53 * Transmission handle. There can only be one for each connection.
54 */ 54 */
55struct GNUNET_CONNECTION_TransmitHandle { 55struct GNUNET_CONNECTION_TransmitHandle
56{
56 /** 57 /**
57 * Function to call if the send buffer has notify_size 58 * Function to call if the send buffer has notify_size
58 * bytes available. 59 * bytes available.
@@ -91,7 +92,8 @@ struct GNUNET_CONNECTION_TransmitHandle {
91 * During connect, we try multiple possible IP addresses 92 * During connect, we try multiple possible IP addresses
92 * to find out which one might work. 93 * to find out which one might work.
93 */ 94 */
94struct AddressProbe { 95struct AddressProbe
96{
95 /** 97 /**
96 * This is a linked list. 98 * This is a linked list.
97 */ 99 */
@@ -132,7 +134,8 @@ struct AddressProbe {
132/** 134/**
133 * @brief handle for a network connection 135 * @brief handle for a network connection
134 */ 136 */
135struct GNUNET_CONNECTION_Handle { 137struct GNUNET_CONNECTION_Handle
138{
136 /** 139 /**
137 * Configuration to use. 140 * Configuration to use.
138 */ 141 */
@@ -268,7 +271,7 @@ struct GNUNET_CONNECTION_Handle {
268 * @param connection the connection to set persistent 271 * @param connection the connection to set persistent
269 */ 272 */
270void 273void
271GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *connection) 274GNUNET_CONNECTION_persist_ (struct GNUNET_CONNECTION_Handle *connection)
272{ 275{
273 connection->persist = GNUNET_YES; 276 connection->persist = GNUNET_YES;
274} 277}
@@ -286,9 +289,9 @@ GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *connection)
286 * @return #GNUNET_OK on success 289 * @return #GNUNET_OK on success
287 */ 290 */
288int 291int
289GNUNET_CONNECTION_disable_corking(struct GNUNET_CONNECTION_Handle *connection) 292GNUNET_CONNECTION_disable_corking (struct GNUNET_CONNECTION_Handle *connection)
290{ 293{
291 return GNUNET_NETWORK_socket_disable_corking(connection->sock); 294 return GNUNET_NETWORK_socket_disable_corking (connection->sock);
292} 295}
293 296
294 297
@@ -301,13 +304,13 @@ GNUNET_CONNECTION_disable_corking(struct GNUNET_CONNECTION_Handle *connection)
301 * @return the boxed connection handle 304 * @return the boxed connection handle
302 */ 305 */
303struct GNUNET_CONNECTION_Handle * 306struct GNUNET_CONNECTION_Handle *
304GNUNET_CONNECTION_create_from_existing(struct GNUNET_NETWORK_Handle *osSocket) 307GNUNET_CONNECTION_create_from_existing (struct GNUNET_NETWORK_Handle *osSocket)
305{ 308{
306 struct GNUNET_CONNECTION_Handle *connection; 309 struct GNUNET_CONNECTION_Handle *connection;
307 310
308 connection = GNUNET_new(struct GNUNET_CONNECTION_Handle); 311 connection = GNUNET_new (struct GNUNET_CONNECTION_Handle);
309 connection->write_buffer_size = GNUNET_MIN_MESSAGE_SIZE; 312 connection->write_buffer_size = GNUNET_MIN_MESSAGE_SIZE;
310 connection->write_buffer = GNUNET_malloc(connection->write_buffer_size); 313 connection->write_buffer = GNUNET_malloc (connection->write_buffer_size);
311 connection->sock = osSocket; 314 connection->sock = osSocket;
312 return connection; 315 return connection;
313} 316}
@@ -323,9 +326,9 @@ GNUNET_CONNECTION_create_from_existing(struct GNUNET_NETWORK_Handle *osSocket)
323 * @return the connection handle, NULL on error 326 * @return the connection handle, NULL on error
324 */ 327 */
325struct GNUNET_CONNECTION_Handle * 328struct GNUNET_CONNECTION_Handle *
326GNUNET_CONNECTION_create_from_accept(GNUNET_CONNECTION_AccessCheck access_cb, 329GNUNET_CONNECTION_create_from_accept (GNUNET_CONNECTION_AccessCheck access_cb,
327 void *access_cb_cls, 330 void *access_cb_cls,
328 struct GNUNET_NETWORK_Handle *lsock) 331 struct GNUNET_NETWORK_Handle *lsock)
329{ 332{
330 struct GNUNET_CONNECTION_Handle *connection; 333 struct GNUNET_CONNECTION_Handle *connection;
331 char addr[128]; 334 char addr[128];
@@ -351,107 +354,107 @@ GNUNET_CONNECTION_create_from_accept(GNUNET_CONNECTION_AccessCheck access_cb,
351 354
352 addrlen = sizeof(addr); 355 addrlen = sizeof(addr);
353 sock = 356 sock =
354 GNUNET_NETWORK_socket_accept(lsock, (struct sockaddr *)&addr, &addrlen); 357 GNUNET_NETWORK_socket_accept (lsock, (struct sockaddr *) &addr, &addrlen);
355 if (NULL == sock) 358 if (NULL == sock)
356 { 359 {
357 if (EAGAIN != errno) 360 if (EAGAIN != errno)
358 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "accept"); 361 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "accept");
359 return NULL; 362 return NULL;
360 } 363 }
361 if ((addrlen > sizeof(addr)) || (addrlen < sizeof(sa_family_t))) 364 if ((addrlen > sizeof(addr)) || (addrlen < sizeof(sa_family_t)))
362 { 365 {
363 GNUNET_break(0); 366 GNUNET_break (0);
364 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(sock)); 367 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
365 return NULL; 368 return NULL;
366 } 369 }
367 370
368 sa = (struct sockaddr *)addr; 371 sa = (struct sockaddr *) addr;
369 v6 = (struct sockaddr_in6 *)addr; 372 v6 = (struct sockaddr_in6 *) addr;
370 if ((AF_INET6 == sa->sa_family) && (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr))) 373 if ((AF_INET6 == sa->sa_family) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
371 { 374 {
372 /* convert to V4 address */ 375 /* convert to V4 address */
373 v4 = GNUNET_new(struct sockaddr_in); 376 v4 = GNUNET_new (struct sockaddr_in);
374 memset(v4, 0, sizeof(struct sockaddr_in)); 377 memset (v4, 0, sizeof(struct sockaddr_in));
375 v4->sin_family = AF_INET; 378 v4->sin_family = AF_INET;
376#if HAVE_SOCKADDR_IN_SIN_LEN 379#if HAVE_SOCKADDR_IN_SIN_LEN
377 v4->sin_len = (u_char)sizeof(struct sockaddr_in); 380 v4->sin_len = (u_char) sizeof(struct sockaddr_in);
378#endif 381#endif
379 GNUNET_memcpy(&v4->sin_addr, 382 GNUNET_memcpy (&v4->sin_addr,
380 &((char *)&v6->sin6_addr)[sizeof(struct in6_addr) - 383 &((char *) &v6->sin6_addr)[sizeof(struct in6_addr)
381 sizeof(struct in_addr)], 384 - sizeof(struct in_addr)],
382 sizeof(struct in_addr)); 385 sizeof(struct in_addr));
383 v4->sin_port = v6->sin6_port; 386 v4->sin_port = v6->sin6_port;
384 uaddr = v4; 387 uaddr = v4;
385 addrlen = sizeof(struct sockaddr_in); 388 addrlen = sizeof(struct sockaddr_in);
386 } 389 }
387 else 390 else
388 { 391 {
389 uaddr = GNUNET_malloc(addrlen); 392 uaddr = GNUNET_malloc (addrlen);
390 GNUNET_memcpy(uaddr, addr, addrlen); 393 GNUNET_memcpy (uaddr, addr, addrlen);
391 } 394 }
392 gcp = NULL; 395 gcp = NULL;
393 if (AF_UNIX == sa->sa_family) 396 if (AF_UNIX == sa->sa_family)
394 { 397 {
395#if HAVE_GETPEEREID 398#if HAVE_GETPEEREID
396 /* most BSDs */ 399 /* most BSDs */
397 if (0 == getpeereid(GNUNET_NETWORK_get_fd(sock), &gc.uid, &gc.gid)) 400 if (0 == getpeereid (GNUNET_NETWORK_get_fd (sock), &gc.uid, &gc.gid))
398 gcp = &gc; 401 gcp = &gc;
399#else 402#else
400#ifdef SO_PEERCRED 403#ifdef SO_PEERCRED
401 /* largely traditional GNU/Linux */ 404 /* largely traditional GNU/Linux */
402 olen = sizeof(uc); 405 olen = sizeof(uc);
403 if ((0 == getsockopt(GNUNET_NETWORK_get_fd(sock), 406 if ((0 == getsockopt (GNUNET_NETWORK_get_fd (sock),
404 SOL_SOCKET, 407 SOL_SOCKET,
405 SO_PEERCRED, 408 SO_PEERCRED,
406 &uc, 409 &uc,
407 &olen)) && 410 &olen)) &&
408 (olen == sizeof(uc))) 411 (olen == sizeof(uc)))
409 { 412 {
410 gc.uid = uc.uid; 413 gc.uid = uc.uid;
411 gc.gid = uc.gid; 414 gc.gid = uc.gid;
412 gcp = &gc; 415 gcp = &gc;
413 } 416 }
414#else 417#else
415#if HAVE_GETPEERUCRED 418#if HAVE_GETPEERUCRED
416 /* this is for Solaris 10 */ 419 /* this is for Solaris 10 */
417 ucred_t *uc; 420 ucred_t *uc;
418 421
419 uc = NULL; 422 uc = NULL;
420 if (0 == getpeerucred(GNUNET_NETWORK_get_fd(sock), &uc)) 423 if (0 == getpeerucred (GNUNET_NETWORK_get_fd (sock), &uc))
421 { 424 {
422 gc.uid = ucred_geteuid(uc); 425 gc.uid = ucred_geteuid (uc);
423 gc.gid = ucred_getegid(uc); 426 gc.gid = ucred_getegid (uc);
424 gcp = &gc; 427 gcp = &gc;
425 } 428 }
426 ucred_free(uc); 429 ucred_free (uc);
427#endif 430#endif
428#endif 431#endif
429#endif 432#endif
430 } 433 }
431 434
432 if ((NULL != access_cb) && 435 if ((NULL != access_cb) &&
433 (GNUNET_YES != (aret = access_cb(access_cb_cls, gcp, uaddr, addrlen)))) 436 (GNUNET_YES != (aret = access_cb (access_cb_cls, gcp, uaddr, addrlen))))
434 { 437 {
435 if (GNUNET_NO == aret) 438 if (GNUNET_NO == aret)
436 LOG(GNUNET_ERROR_TYPE_INFO, 439 LOG (GNUNET_ERROR_TYPE_INFO,
437 _("Access denied to `%s'\n"), 440 _ ("Access denied to `%s'\n"),
438 GNUNET_a2s(uaddr, addrlen)); 441 GNUNET_a2s (uaddr, addrlen));
439 GNUNET_break(GNUNET_OK == 442 GNUNET_break (GNUNET_OK ==
440 GNUNET_NETWORK_socket_shutdown(sock, SHUT_RDWR)); 443 GNUNET_NETWORK_socket_shutdown (sock, SHUT_RDWR));
441 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(sock)); 444 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
442 GNUNET_free(uaddr); 445 GNUNET_free (uaddr);
443 return NULL; 446 return NULL;
444 } 447 }
445 connection = GNUNET_new(struct GNUNET_CONNECTION_Handle); 448 connection = GNUNET_new (struct GNUNET_CONNECTION_Handle);
446 connection->write_buffer_size = GNUNET_MIN_MESSAGE_SIZE; 449 connection->write_buffer_size = GNUNET_MIN_MESSAGE_SIZE;
447 connection->write_buffer = GNUNET_malloc(connection->write_buffer_size); 450 connection->write_buffer = GNUNET_malloc (connection->write_buffer_size);
448 connection->addr = uaddr; 451 connection->addr = uaddr;
449 connection->addrlen = addrlen; 452 connection->addrlen = addrlen;
450 connection->sock = sock; 453 connection->sock = sock;
451 LOG(GNUNET_ERROR_TYPE_INFO, 454 LOG (GNUNET_ERROR_TYPE_INFO,
452 _("Accepting connection from `%s': %p\n"), 455 _ ("Accepting connection from `%s': %p\n"),
453 GNUNET_a2s(uaddr, addrlen), 456 GNUNET_a2s (uaddr, addrlen),
454 connection); 457 connection);
455 return connection; 458 return connection;
456} 459}
457 460
@@ -465,14 +468,14 @@ GNUNET_CONNECTION_create_from_accept(GNUNET_CONNECTION_AccessCheck access_cb,
465 * @return #GNUNET_OK on success 468 * @return #GNUNET_OK on success
466 */ 469 */
467int 470int
468GNUNET_CONNECTION_get_address(struct GNUNET_CONNECTION_Handle *connection, 471GNUNET_CONNECTION_get_address (struct GNUNET_CONNECTION_Handle *connection,
469 void **addr, 472 void **addr,
470 size_t *addrlen) 473 size_t *addrlen)
471{ 474{
472 if ((NULL == connection->addr) || (0 == connection->addrlen)) 475 if ((NULL == connection->addr) || (0 == connection->addrlen))
473 return GNUNET_NO; 476 return GNUNET_NO;
474 *addr = GNUNET_malloc(connection->addrlen); 477 *addr = GNUNET_malloc (connection->addrlen);
475 GNUNET_memcpy(*addr, connection->addr, connection->addrlen); 478 GNUNET_memcpy (*addr, connection->addr, connection->addrlen);
476 *addrlen = connection->addrlen; 479 *addrlen = connection->addrlen;
477 return GNUNET_OK; 480 return GNUNET_OK;
478} 481}
@@ -485,22 +488,22 @@ GNUNET_CONNECTION_get_address(struct GNUNET_CONNECTION_Handle *connection,
485 * @param errcode error code to send 488 * @param errcode error code to send
486 */ 489 */
487static void 490static void
488signal_receive_error(struct GNUNET_CONNECTION_Handle *connection, int errcode) 491signal_receive_error (struct GNUNET_CONNECTION_Handle *connection, int errcode)
489{ 492{
490 GNUNET_CONNECTION_Receiver receiver; 493 GNUNET_CONNECTION_Receiver receiver;
491 494
492 LOG(GNUNET_ERROR_TYPE_DEBUG, 495 LOG (GNUNET_ERROR_TYPE_DEBUG,
493 "Receive encounters error (%s), connection closed (%p)\n", 496 "Receive encounters error (%s), connection closed (%p)\n",
494 strerror(errcode), 497 strerror (errcode),
495 connection); 498 connection);
496 GNUNET_assert(NULL != (receiver = connection->receiver)); 499 GNUNET_assert (NULL != (receiver = connection->receiver));
497 connection->receiver = NULL; 500 connection->receiver = NULL;
498 receiver(connection->receiver_cls, 501 receiver (connection->receiver_cls,
499 NULL, 502 NULL,
500 0, 503 0,
501 connection->addr, 504 connection->addr,
502 connection->addrlen, 505 connection->addrlen,
503 errcode); 506 errcode);
504} 507}
505 508
506 509
@@ -510,16 +513,16 @@ signal_receive_error(struct GNUNET_CONNECTION_Handle *connection, int errcode)
510 * @param connection connection to signal for 513 * @param connection connection to signal for
511 */ 514 */
512static void 515static void
513signal_receive_timeout(struct GNUNET_CONNECTION_Handle *connection) 516signal_receive_timeout (struct GNUNET_CONNECTION_Handle *connection)
514{ 517{
515 GNUNET_CONNECTION_Receiver receiver; 518 GNUNET_CONNECTION_Receiver receiver;
516 519
517 LOG(GNUNET_ERROR_TYPE_DEBUG, 520 LOG (GNUNET_ERROR_TYPE_DEBUG,
518 "Connection signals timeout to receiver (%p)!\n", 521 "Connection signals timeout to receiver (%p)!\n",
519 connection); 522 connection);
520 GNUNET_assert(NULL != (receiver = connection->receiver)); 523 GNUNET_assert (NULL != (receiver = connection->receiver));
521 connection->receiver = NULL; 524 connection->receiver = NULL;
522 receiver(connection->receiver_cls, NULL, 0, NULL, 0, 0); 525 receiver (connection->receiver_cls, NULL, 0, NULL, 0, 0);
523} 526}
524 527
525 528
@@ -530,34 +533,34 @@ signal_receive_timeout(struct GNUNET_CONNECTION_Handle *connection)
530 * @param ecode error code (errno) 533 * @param ecode error code (errno)
531 */ 534 */
532static void 535static void
533signal_transmit_error(struct GNUNET_CONNECTION_Handle *connection, int ecode) 536signal_transmit_error (struct GNUNET_CONNECTION_Handle *connection, int ecode)
534{ 537{
535 GNUNET_CONNECTION_TransmitReadyNotify notify; 538 GNUNET_CONNECTION_TransmitReadyNotify notify;
536 539
537 LOG(GNUNET_ERROR_TYPE_DEBUG, 540 LOG (GNUNET_ERROR_TYPE_DEBUG,
538 "Transmission encounterd error (%s), connection closed (%p)\n", 541 "Transmission encounterd error (%s), connection closed (%p)\n",
539 strerror(ecode), 542 strerror (ecode),
540 connection); 543 connection);
541 if (NULL != connection->sock) 544 if (NULL != connection->sock)
542 { 545 {
543 (void)GNUNET_NETWORK_socket_shutdown(connection->sock, SHUT_RDWR); 546 (void) GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR);
544 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(connection->sock)); 547 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
545 connection->sock = NULL; 548 connection->sock = NULL;
546 GNUNET_assert(NULL == connection->write_task); 549 GNUNET_assert (NULL == connection->write_task);
547 } 550 }
548 if (NULL != connection->read_task) 551 if (NULL != connection->read_task)
549 { 552 {
550 /* send errors trigger read errors... */ 553 /* send errors trigger read errors... */
551 GNUNET_SCHEDULER_cancel(connection->read_task); 554 GNUNET_SCHEDULER_cancel (connection->read_task);
552 connection->read_task = NULL; 555 connection->read_task = NULL;
553 signal_receive_timeout(connection); 556 signal_receive_timeout (connection);
554 return; 557 return;
555 } 558 }
556 if (NULL == connection->nth.notify_ready) 559 if (NULL == connection->nth.notify_ready)
557 return; /* nobody to tell about it */ 560 return; /* nobody to tell about it */
558 notify = connection->nth.notify_ready; 561 notify = connection->nth.notify_ready;
559 connection->nth.notify_ready = NULL; 562 connection->nth.notify_ready = NULL;
560 notify(connection->nth.notify_ready_cls, 0, NULL); 563 notify (connection->nth.notify_ready_cls, 0, NULL);
561} 564}
562 565
563 566
@@ -568,37 +571,37 @@ signal_transmit_error(struct GNUNET_CONNECTION_Handle *connection, int ecode)
568 * @param connection the connection we tried to establish 571 * @param connection the connection we tried to establish
569 */ 572 */
570static void 573static void
571connect_fail_continuation(struct GNUNET_CONNECTION_Handle *connection) 574connect_fail_continuation (struct GNUNET_CONNECTION_Handle *connection)
572{ 575{
573 LOG(GNUNET_ERROR_TYPE_INFO, 576 LOG (GNUNET_ERROR_TYPE_INFO,
574 "Failed to establish TCP connection to `%s:%u', no further addresses to try.\n", 577 "Failed to establish TCP connection to `%s:%u', no further addresses to try.\n",
575 connection->hostname, 578 connection->hostname,
576 connection->port); 579 connection->port);
577 GNUNET_break(NULL == connection->ap_head); 580 GNUNET_break (NULL == connection->ap_head);
578 GNUNET_break(NULL == connection->ap_tail); 581 GNUNET_break (NULL == connection->ap_tail);
579 GNUNET_break(GNUNET_NO == connection->dns_active); 582 GNUNET_break (GNUNET_NO == connection->dns_active);
580 GNUNET_break(NULL == connection->sock); 583 GNUNET_break (NULL == connection->sock);
581 GNUNET_assert(NULL == connection->write_task); 584 GNUNET_assert (NULL == connection->write_task);
582 GNUNET_assert(NULL == connection->proxy_handshake); 585 GNUNET_assert (NULL == connection->proxy_handshake);
583 586
584 /* signal errors for jobs that used to wait on the connection */ 587 /* signal errors for jobs that used to wait on the connection */
585 connection->destroy_later = 1; 588 connection->destroy_later = 1;
586 if (NULL != connection->receiver) 589 if (NULL != connection->receiver)
587 signal_receive_error(connection, ECONNREFUSED); 590 signal_receive_error (connection, ECONNREFUSED);
588 if (NULL != connection->nth.notify_ready) 591 if (NULL != connection->nth.notify_ready)
589 { 592 {
590 GNUNET_assert(NULL != connection->nth.timeout_task); 593 GNUNET_assert (NULL != connection->nth.timeout_task);
591 GNUNET_SCHEDULER_cancel(connection->nth.timeout_task); 594 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
592 connection->nth.timeout_task = NULL; 595 connection->nth.timeout_task = NULL;
593 signal_transmit_error(connection, ECONNREFUSED); 596 signal_transmit_error (connection, ECONNREFUSED);
594 } 597 }
595 if (-1 == connection->destroy_later) 598 if (-1 == connection->destroy_later)
596 { 599 {
597 /* do it now */ 600 /* do it now */
598 connection->destroy_later = 0; 601 connection->destroy_later = 0;
599 GNUNET_CONNECTION_destroy(connection); 602 GNUNET_CONNECTION_destroy (connection);
600 return; 603 return;
601 } 604 }
602 connection->destroy_later = 0; 605 connection->destroy_later = 0;
603} 606}
604 607
@@ -609,7 +612,7 @@ connect_fail_continuation(struct GNUNET_CONNECTION_Handle *connection)
609 * @param cls our connection handle 612 * @param cls our connection handle
610 */ 613 */
611static void 614static void
612transmit_ready(void *cls); 615transmit_ready (void *cls);
613 616
614 617
615/** 618/**
@@ -619,7 +622,7 @@ transmit_ready(void *cls);
619 * @param cls connection to read from 622 * @param cls connection to read from
620 */ 623 */
621static void 624static void
622receive_ready(void *cls); 625receive_ready (void *cls);
623 626
624 627
625/** 628/**
@@ -628,42 +631,42 @@ receive_ready(void *cls);
628 * @param connection the connection we tried to establish 631 * @param connection the connection we tried to establish
629 */ 632 */
630static void 633static void
631connect_success_continuation(struct GNUNET_CONNECTION_Handle *connection) 634connect_success_continuation (struct GNUNET_CONNECTION_Handle *connection)
632{ 635{
633 LOG(GNUNET_ERROR_TYPE_DEBUG, 636 LOG (GNUNET_ERROR_TYPE_DEBUG,
634 "Connection to `%s' succeeded! (%p)\n", 637 "Connection to `%s' succeeded! (%p)\n",
635 GNUNET_a2s(connection->addr, connection->addrlen), 638 GNUNET_a2s (connection->addr, connection->addrlen),
636 connection); 639 connection);
637 /* trigger jobs that waited for the connection */ 640 /* trigger jobs that waited for the connection */
638 if (NULL != connection->receiver) 641 if (NULL != connection->receiver)
639 { 642 {
640 LOG(GNUNET_ERROR_TYPE_DEBUG, 643 LOG (GNUNET_ERROR_TYPE_DEBUG,
641 "Connection succeeded, starting with receiving data (%p)\n", 644 "Connection succeeded, starting with receiving data (%p)\n",
642 connection); 645 connection);
643 GNUNET_assert(NULL == connection->read_task); 646 GNUNET_assert (NULL == connection->read_task);
644 connection->read_task = 647 connection->read_task =
645 GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_absolute_get_remaining( 648 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining (
646 connection->receive_timeout), 649 connection->receive_timeout),
650 connection->sock,
651 &receive_ready,
652 connection);
653 }
654 if (NULL != connection->nth.notify_ready)
655 {
656 LOG (GNUNET_ERROR_TYPE_DEBUG,
657 "Connection succeeded, starting with sending data (%p)\n",
658 connection);
659 GNUNET_assert (connection->nth.timeout_task != NULL);
660 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
661 connection->nth.timeout_task = NULL;
662 GNUNET_assert (connection->write_task == NULL);
663 connection->write_task =
664 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining (
665 connection->nth.transmit_timeout),
647 connection->sock, 666 connection->sock,
648 &receive_ready, 667 &transmit_ready,
649 connection); 668 connection);
650 } 669 }
651 if (NULL != connection->nth.notify_ready)
652 {
653 LOG(GNUNET_ERROR_TYPE_DEBUG,
654 "Connection succeeded, starting with sending data (%p)\n",
655 connection);
656 GNUNET_assert(connection->nth.timeout_task != NULL);
657 GNUNET_SCHEDULER_cancel(connection->nth.timeout_task);
658 connection->nth.timeout_task = NULL;
659 GNUNET_assert(connection->write_task == NULL);
660 connection->write_task =
661 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_absolute_get_remaining(
662 connection->nth.transmit_timeout),
663 connection->sock,
664 &transmit_ready,
665 connection);
666 }
667} 670}
668 671
669 672
@@ -674,7 +677,7 @@ connect_success_continuation(struct GNUNET_CONNECTION_Handle *connection)
674 * @param cls the `struct AddressProbe *` with the address that we are probing 677 * @param cls the `struct AddressProbe *` with the address that we are probing
675 */ 678 */
676static void 679static void
677connect_probe_continuation(void *cls) 680connect_probe_continuation (void *cls)
678{ 681{
679 struct AddressProbe *ap = cls; 682 struct AddressProbe *ap = cls;
680 struct GNUNET_CONNECTION_Handle *connection = ap->connection; 683 struct GNUNET_CONNECTION_Handle *connection = ap->connection;
@@ -683,44 +686,44 @@ connect_probe_continuation(void *cls)
683 int error; 686 int error;
684 socklen_t len; 687 socklen_t len;
685 688
686 GNUNET_assert(NULL != ap->sock); 689 GNUNET_assert (NULL != ap->sock);
687 GNUNET_CONTAINER_DLL_remove(connection->ap_head, connection->ap_tail, ap); 690 GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, ap);
688 len = sizeof(error); 691 len = sizeof(error);
689 errno = 0; 692 errno = 0;
690 error = 0; 693 error = 0;
691 tc = GNUNET_SCHEDULER_get_task_context(); 694 tc = GNUNET_SCHEDULER_get_task_context ();
692 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || 695 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
693 (GNUNET_OK != GNUNET_NETWORK_socket_getsockopt(ap->sock, 696 (GNUNET_OK != GNUNET_NETWORK_socket_getsockopt (ap->sock,
694 SOL_SOCKET, 697 SOL_SOCKET,
695 SO_ERROR, 698 SO_ERROR,
696 &error, 699 &error,
697 &len)) || 700 &len)) ||
698 (0 != error)) 701 (0 != error))
699 { 702 {
700 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(ap->sock)); 703 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
701 GNUNET_free(ap); 704 GNUNET_free (ap);
702 if ((NULL == connection->ap_head) && 705 if ((NULL == connection->ap_head) &&
703 (GNUNET_NO == connection->dns_active) && 706 (GNUNET_NO == connection->dns_active) &&
704 (NULL == connection->proxy_handshake)) 707 (NULL == connection->proxy_handshake))
705 connect_fail_continuation(connection); 708 connect_fail_continuation (connection);
706 return; 709 return;
707 } 710 }
708 GNUNET_assert(NULL == connection->sock); 711 GNUNET_assert (NULL == connection->sock);
709 connection->sock = ap->sock; 712 connection->sock = ap->sock;
710 GNUNET_assert(NULL == connection->addr); 713 GNUNET_assert (NULL == connection->addr);
711 connection->addr = GNUNET_malloc(ap->addrlen); 714 connection->addr = GNUNET_malloc (ap->addrlen);
712 GNUNET_memcpy(connection->addr, ap->addr, ap->addrlen); 715 GNUNET_memcpy (connection->addr, ap->addr, ap->addrlen);
713 connection->addrlen = ap->addrlen; 716 connection->addrlen = ap->addrlen;
714 GNUNET_free(ap); 717 GNUNET_free (ap);
715 /* cancel all other attempts */ 718 /* cancel all other attempts */
716 while (NULL != (pos = connection->ap_head)) 719 while (NULL != (pos = connection->ap_head))
717 { 720 {
718 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(pos->sock)); 721 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
719 GNUNET_SCHEDULER_cancel(pos->task); 722 GNUNET_SCHEDULER_cancel (pos->task);
720 GNUNET_CONTAINER_DLL_remove(connection->ap_head, connection->ap_tail, pos); 723 GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, pos);
721 GNUNET_free(pos); 724 GNUNET_free (pos);
722 } 725 }
723 connect_success_continuation(connection); 726 connect_success_continuation (connection);
724} 727}
725 728
726 729
@@ -733,87 +736,87 @@ connect_probe_continuation(void *cls)
733 * @param addrlen length of @a addr 736 * @param addrlen length of @a addr
734 */ 737 */
735static void 738static void
736try_connect_using_address(void *cls, 739try_connect_using_address (void *cls,
737 const struct sockaddr *addr, 740 const struct sockaddr *addr,
738 socklen_t addrlen) 741 socklen_t addrlen)
739{ 742{
740 struct GNUNET_CONNECTION_Handle *connection = cls; 743 struct GNUNET_CONNECTION_Handle *connection = cls;
741 struct AddressProbe *ap; 744 struct AddressProbe *ap;
742 struct GNUNET_TIME_Relative delay; 745 struct GNUNET_TIME_Relative delay;
743 746
744 if (NULL == addr) 747 if (NULL == addr)
745 { 748 {
746 connection->dns_active = NULL; 749 connection->dns_active = NULL;
747 if ((NULL == connection->ap_head) && (NULL == connection->sock) && 750 if ((NULL == connection->ap_head) && (NULL == connection->sock) &&
748 (NULL == connection->proxy_handshake)) 751 (NULL == connection->proxy_handshake))
749 connect_fail_continuation(connection); 752 connect_fail_continuation (connection);
750 return; 753 return;
751 } 754 }
752 if (NULL != connection->sock) 755 if (NULL != connection->sock)
753 return; /* already connected */ 756 return; /* already connected */
754 GNUNET_assert(NULL == connection->addr); 757 GNUNET_assert (NULL == connection->addr);
755 /* try to connect */ 758 /* try to connect */
756 LOG(GNUNET_ERROR_TYPE_DEBUG, 759 LOG (GNUNET_ERROR_TYPE_DEBUG,
757 "Trying to connect using address `%s:%u/%s:%u'\n", 760 "Trying to connect using address `%s:%u/%s:%u'\n",
758 connection->hostname, 761 connection->hostname,
759 connection->port, 762 connection->port,
760 GNUNET_a2s(addr, addrlen), 763 GNUNET_a2s (addr, addrlen),
761 connection->port); 764 connection->port);
762 ap = GNUNET_malloc(sizeof(struct AddressProbe) + addrlen); 765 ap = GNUNET_malloc (sizeof(struct AddressProbe) + addrlen);
763 ap->addr = (const struct sockaddr *)&ap[1]; 766 ap->addr = (const struct sockaddr *) &ap[1];
764 GNUNET_memcpy(&ap[1], addr, addrlen); 767 GNUNET_memcpy (&ap[1], addr, addrlen);
765 ap->addrlen = addrlen; 768 ap->addrlen = addrlen;
766 ap->connection = connection; 769 ap->connection = connection;
767 770
768 switch (ap->addr->sa_family) 771 switch (ap->addr->sa_family)
769 { 772 {
770 case AF_INET: 773 case AF_INET:
771 ((struct sockaddr_in *)ap->addr)->sin_port = htons(connection->port); 774 ((struct sockaddr_in *) ap->addr)->sin_port = htons (connection->port);
772 break; 775 break;
773 776
774 case AF_INET6: 777 case AF_INET6:
775 ((struct sockaddr_in6 *)ap->addr)->sin6_port = htons(connection->port); 778 ((struct sockaddr_in6 *) ap->addr)->sin6_port = htons (connection->port);
776 break; 779 break;
777 780
778 default: 781 default:
779 GNUNET_break(0); 782 GNUNET_break (0);
780 GNUNET_free(ap); 783 GNUNET_free (ap);
781 return; /* not supported by us */ 784 return; /* not supported by us */
782 } 785 }
783 ap->sock = GNUNET_NETWORK_socket_create(ap->addr->sa_family, SOCK_STREAM, 0); 786 ap->sock = GNUNET_NETWORK_socket_create (ap->addr->sa_family, SOCK_STREAM, 0);
784 if (NULL == ap->sock) 787 if (NULL == ap->sock)
785 { 788 {
786 GNUNET_free(ap); 789 GNUNET_free (ap);
787 return; /* not supported by OS */ 790 return; /* not supported by OS */
788 } 791 }
789 LOG(GNUNET_ERROR_TYPE_INFO, 792 LOG (GNUNET_ERROR_TYPE_INFO,
790 "Trying to connect to `%s' (%p)\n", 793 "Trying to connect to `%s' (%p)\n",
791 GNUNET_a2s(ap->addr, ap->addrlen), 794 GNUNET_a2s (ap->addr, ap->addrlen),
792 connection); 795 connection);
793 if ((GNUNET_OK != 796 if ((GNUNET_OK !=
794 GNUNET_NETWORK_socket_connect(ap->sock, ap->addr, ap->addrlen)) && 797 GNUNET_NETWORK_socket_connect (ap->sock, ap->addr, ap->addrlen)) &&
795 (EINPROGRESS != errno)) 798 (EINPROGRESS != errno))
796 { 799 {
797 /* maybe refused / unsupported address, try next */ 800 /* maybe refused / unsupported address, try next */
798 LOG_STRERROR(GNUNET_ERROR_TYPE_INFO, "connect"); 801 LOG_STRERROR (GNUNET_ERROR_TYPE_INFO, "connect");
799 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(ap->sock)); 802 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
800 GNUNET_free(ap); 803 GNUNET_free (ap);
801 return; 804 return;
802 } 805 }
803 GNUNET_CONTAINER_DLL_insert(connection->ap_head, connection->ap_tail, ap); 806 GNUNET_CONTAINER_DLL_insert (connection->ap_head, connection->ap_tail, ap);
804 delay = CONNECT_RETRY_TIMEOUT; 807 delay = CONNECT_RETRY_TIMEOUT;
805 if (NULL != connection->nth.notify_ready) 808 if (NULL != connection->nth.notify_ready)
806 delay = GNUNET_TIME_relative_min(delay, 809 delay = GNUNET_TIME_relative_min (delay,
807 GNUNET_TIME_absolute_get_remaining( 810 GNUNET_TIME_absolute_get_remaining (
808 connection->nth.transmit_timeout)); 811 connection->nth.transmit_timeout));
809 if (NULL != connection->receiver) 812 if (NULL != connection->receiver)
810 delay = GNUNET_TIME_relative_min(delay, 813 delay = GNUNET_TIME_relative_min (delay,
811 GNUNET_TIME_absolute_get_remaining( 814 GNUNET_TIME_absolute_get_remaining (
812 connection->receive_timeout)); 815 connection->receive_timeout));
813 ap->task = GNUNET_SCHEDULER_add_write_net(delay, 816 ap->task = GNUNET_SCHEDULER_add_write_net (delay,
814 ap->sock, 817 ap->sock,
815 &connect_probe_continuation, 818 &connect_probe_continuation,
816 ap); 819 ap);
817} 820}
818 821
819 822
@@ -828,25 +831,25 @@ try_connect_using_address(void *cls,
828 * @return the connection handle 831 * @return the connection handle
829 */ 832 */
830struct GNUNET_CONNECTION_Handle * 833struct GNUNET_CONNECTION_Handle *
831GNUNET_CONNECTION_create_from_connect( 834GNUNET_CONNECTION_create_from_connect (
832 const struct GNUNET_CONFIGURATION_Handle *cfg, 835 const struct GNUNET_CONFIGURATION_Handle *cfg,
833 const char *hostname, 836 const char *hostname,
834 uint16_t port) 837 uint16_t port)
835{ 838{
836 struct GNUNET_CONNECTION_Handle *connection; 839 struct GNUNET_CONNECTION_Handle *connection;
837 840
838 GNUNET_assert(0 < strlen(hostname)); /* sanity check */ 841 GNUNET_assert (0 < strlen (hostname)); /* sanity check */
839 connection = GNUNET_new(struct GNUNET_CONNECTION_Handle); 842 connection = GNUNET_new (struct GNUNET_CONNECTION_Handle);
840 connection->cfg = cfg; 843 connection->cfg = cfg;
841 connection->write_buffer_size = GNUNET_MIN_MESSAGE_SIZE; 844 connection->write_buffer_size = GNUNET_MIN_MESSAGE_SIZE;
842 connection->write_buffer = GNUNET_malloc(connection->write_buffer_size); 845 connection->write_buffer = GNUNET_malloc (connection->write_buffer_size);
843 connection->port = port; 846 connection->port = port;
844 connection->hostname = GNUNET_strdup(hostname); 847 connection->hostname = GNUNET_strdup (hostname);
845 connection->dns_active = GNUNET_RESOLVER_ip_get(connection->hostname, 848 connection->dns_active = GNUNET_RESOLVER_ip_get (connection->hostname,
846 AF_UNSPEC, 849 AF_UNSPEC,
847 CONNECT_RETRY_TIMEOUT, 850 CONNECT_RETRY_TIMEOUT,
848 &try_connect_using_address, 851 &try_connect_using_address,
849 connection); 852 connection);
850 return connection; 853 return connection;
851} 854}
852 855
@@ -861,7 +864,7 @@ GNUNET_CONNECTION_create_from_connect(
861 * @return the connection handle, NULL on systems without UNIX support 864 * @return the connection handle, NULL on systems without UNIX support
862 */ 865 */
863struct GNUNET_CONNECTION_Handle * 866struct GNUNET_CONNECTION_Handle *
864GNUNET_CONNECTION_create_from_connect_to_unixpath( 867GNUNET_CONNECTION_create_from_connect_to_unixpath (
865 const struct GNUNET_CONFIGURATION_Handle *cfg, 868 const struct GNUNET_CONFIGURATION_Handle *cfg,
866 const char *unixpath) 869 const char *unixpath)
867{ 870{
@@ -869,51 +872,51 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath(
869 struct GNUNET_CONNECTION_Handle *connection; 872 struct GNUNET_CONNECTION_Handle *connection;
870 struct sockaddr_un *un; 873 struct sockaddr_un *un;
871 874
872 GNUNET_assert(0 < strlen(unixpath)); /* sanity check */ 875 GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
873 un = GNUNET_new(struct sockaddr_un); 876 un = GNUNET_new (struct sockaddr_un);
874 un->sun_family = AF_UNIX; 877 un->sun_family = AF_UNIX;
875 GNUNET_strlcpy(un->sun_path, unixpath, sizeof(un->sun_path)); 878 GNUNET_strlcpy (un->sun_path, unixpath, sizeof(un->sun_path));
876#ifdef LINUX 879#ifdef LINUX
877 { 880 {
878 int abstract; 881 int abstract;
879 882
880 abstract = GNUNET_CONFIGURATION_get_value_yesno(cfg, 883 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
881 "TESTING", 884 "TESTING",
882 "USE_ABSTRACT_SOCKETS"); 885 "USE_ABSTRACT_SOCKETS");
883 if (GNUNET_YES == abstract) 886 if (GNUNET_YES == abstract)
884 un->sun_path[0] = '\0'; 887 un->sun_path[0] = '\0';
885 } 888 }
886#endif 889#endif
887#if HAVE_SOCKADDR_UN_SUN_LEN 890#if HAVE_SOCKADDR_UN_SUN_LEN
888 un->sun_len = (u_char)sizeof(struct sockaddr_un); 891 un->sun_len = (u_char) sizeof(struct sockaddr_un);
889#endif 892#endif
890 connection = GNUNET_new(struct GNUNET_CONNECTION_Handle); 893 connection = GNUNET_new (struct GNUNET_CONNECTION_Handle);
891 connection->cfg = cfg; 894 connection->cfg = cfg;
892 connection->write_buffer_size = GNUNET_MIN_MESSAGE_SIZE; 895 connection->write_buffer_size = GNUNET_MIN_MESSAGE_SIZE;
893 connection->write_buffer = GNUNET_malloc(connection->write_buffer_size); 896 connection->write_buffer = GNUNET_malloc (connection->write_buffer_size);
894 connection->port = 0; 897 connection->port = 0;
895 connection->hostname = NULL; 898 connection->hostname = NULL;
896 connection->addr = (struct sockaddr *)un; 899 connection->addr = (struct sockaddr *) un;
897 connection->addrlen = sizeof(struct sockaddr_un); 900 connection->addrlen = sizeof(struct sockaddr_un);
898 connection->sock = GNUNET_NETWORK_socket_create(AF_UNIX, SOCK_STREAM, 0); 901 connection->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
899 if (NULL == connection->sock) 902 if (NULL == connection->sock)
900 { 903 {
901 GNUNET_free(connection->addr); 904 GNUNET_free (connection->addr);
902 GNUNET_free(connection->write_buffer); 905 GNUNET_free (connection->write_buffer);
903 GNUNET_free(connection); 906 GNUNET_free (connection);
904 return NULL; 907 return NULL;
905 } 908 }
906 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect(connection->sock, 909 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (connection->sock,
907 connection->addr, 910 connection->addr,
908 connection->addrlen)) && 911 connection->addrlen)) &&
909 (EINPROGRESS != errno)) 912 (EINPROGRESS != errno))
910 { 913 {
911 /* Just return; we expect everything to work eventually so don't fail HARD */ 914 /* Just return; we expect everything to work eventually so don't fail HARD */
912 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(connection->sock)); 915 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (connection->sock));
913 connection->sock = NULL; 916 connection->sock = NULL;
914 return connection; 917 return connection;
915 } 918 }
916 connect_success_continuation(connection); 919 connect_success_continuation (connection);
917 return connection; 920 return connection;
918#else 921#else
919 return NULL; 922 return NULL;
@@ -932,31 +935,31 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath(
932 * @return the connection handle 935 * @return the connection handle
933 */ 936 */
934struct GNUNET_CONNECTION_Handle * 937struct GNUNET_CONNECTION_Handle *
935GNUNET_CONNECTION_connect_socket(struct GNUNET_NETWORK_Handle *s, 938GNUNET_CONNECTION_connect_socket (struct GNUNET_NETWORK_Handle *s,
936 const struct sockaddr *serv_addr, 939 const struct sockaddr *serv_addr,
937 socklen_t addrlen) 940 socklen_t addrlen)
938{ 941{
939 struct GNUNET_CONNECTION_Handle *connection; 942 struct GNUNET_CONNECTION_Handle *connection;
940 943
941 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect(s, serv_addr, addrlen)) && 944 if ((GNUNET_OK != GNUNET_NETWORK_socket_connect (s, serv_addr, addrlen)) &&
942 (EINPROGRESS != errno)) 945 (EINPROGRESS != errno))
943 { 946 {
944 /* maybe refused / unsupported address, try next */ 947 /* maybe refused / unsupported address, try next */
945 LOG_STRERROR(GNUNET_ERROR_TYPE_DEBUG, "connect"); 948 LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "connect");
946 LOG(GNUNET_ERROR_TYPE_DEBUG, 949 LOG (GNUNET_ERROR_TYPE_DEBUG,
947 "Attempt to connect to `%s' failed\n", 950 "Attempt to connect to `%s' failed\n",
948 GNUNET_a2s(serv_addr, addrlen)); 951 GNUNET_a2s (serv_addr, addrlen));
949 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(s)); 952 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (s));
950 return NULL; 953 return NULL;
951 } 954 }
952 connection = GNUNET_CONNECTION_create_from_existing(s); 955 connection = GNUNET_CONNECTION_create_from_existing (s);
953 connection->addr = GNUNET_malloc(addrlen); 956 connection->addr = GNUNET_malloc (addrlen);
954 GNUNET_memcpy(connection->addr, serv_addr, addrlen); 957 GNUNET_memcpy (connection->addr, serv_addr, addrlen);
955 connection->addrlen = addrlen; 958 connection->addrlen = addrlen;
956 LOG(GNUNET_ERROR_TYPE_INFO, 959 LOG (GNUNET_ERROR_TYPE_INFO,
957 "Trying to connect to `%s' (%p)\n", 960 "Trying to connect to `%s' (%p)\n",
958 GNUNET_a2s(serv_addr, addrlen), 961 GNUNET_a2s (serv_addr, addrlen),
959 connection); 962 connection);
960 return connection; 963 return connection;
961} 964}
962 965
@@ -973,19 +976,19 @@ GNUNET_CONNECTION_connect_socket(struct GNUNET_NETWORK_Handle *s,
973 * @return the connection handle 976 * @return the connection handle
974 */ 977 */
975struct GNUNET_CONNECTION_Handle * 978struct GNUNET_CONNECTION_Handle *
976GNUNET_CONNECTION_create_from_sockaddr(int af_family, 979GNUNET_CONNECTION_create_from_sockaddr (int af_family,
977 const struct sockaddr *serv_addr, 980 const struct sockaddr *serv_addr,
978 socklen_t addrlen) 981 socklen_t addrlen)
979{ 982{
980 struct GNUNET_NETWORK_Handle *s; 983 struct GNUNET_NETWORK_Handle *s;
981 984
982 s = GNUNET_NETWORK_socket_create(af_family, SOCK_STREAM, 0); 985 s = GNUNET_NETWORK_socket_create (af_family, SOCK_STREAM, 0);
983 if (NULL == s) 986 if (NULL == s)
984 { 987 {
985 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket"); 988 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, "socket");
986 return NULL; 989 return NULL;
987 } 990 }
988 return GNUNET_CONNECTION_connect_socket(s, serv_addr, addrlen); 991 return GNUNET_CONNECTION_connect_socket (s, serv_addr, addrlen);
989} 992}
990 993
991 994
@@ -998,7 +1001,7 @@ GNUNET_CONNECTION_create_from_sockaddr(int af_family,
998 * @return #GNUNET_YES if valid, #GNUNET_NO otherwise 1001 * @return #GNUNET_YES if valid, #GNUNET_NO otherwise
999 */ 1002 */
1000int 1003int
1001GNUNET_CONNECTION_check(struct GNUNET_CONNECTION_Handle *connection) 1004GNUNET_CONNECTION_check (struct GNUNET_CONNECTION_Handle *connection)
1002{ 1005{
1003 if ((NULL != connection->ap_head) || (NULL != connection->dns_active) || 1006 if ((NULL != connection->ap_head) || (NULL != connection->dns_active) ||
1004 (NULL != connection->proxy_handshake)) 1007 (NULL != connection->proxy_handshake))
@@ -1017,78 +1020,78 @@ GNUNET_CONNECTION_check(struct GNUNET_CONNECTION_Handle *connection)
1017 * @param connection connection to destroy 1020 * @param connection connection to destroy
1018 */ 1021 */
1019void 1022void
1020GNUNET_CONNECTION_destroy(struct GNUNET_CONNECTION_Handle *connection) 1023GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection)
1021{ 1024{
1022 struct AddressProbe *pos; 1025 struct AddressProbe *pos;
1023 1026
1024 if (0 != connection->destroy_later) 1027 if (0 != connection->destroy_later)
1025 { 1028 {
1026 connection->destroy_later = -1; 1029 connection->destroy_later = -1;
1027 return; 1030 return;
1028 } 1031 }
1029 LOG(GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection); 1032 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connection (%p)\n", connection);
1030 GNUNET_assert(NULL == connection->nth.notify_ready); 1033 GNUNET_assert (NULL == connection->nth.notify_ready);
1031 GNUNET_assert(NULL == connection->receiver); 1034 GNUNET_assert (NULL == connection->receiver);
1032 if (NULL != connection->write_task) 1035 if (NULL != connection->write_task)
1033 { 1036 {
1034 GNUNET_SCHEDULER_cancel(connection->write_task); 1037 GNUNET_SCHEDULER_cancel (connection->write_task);
1035 connection->write_task = NULL; 1038 connection->write_task = NULL;
1036 connection->write_buffer_off = 0; 1039 connection->write_buffer_off = 0;
1037 } 1040 }
1038 if (NULL != connection->read_task) 1041 if (NULL != connection->read_task)
1039 { 1042 {
1040 GNUNET_SCHEDULER_cancel(connection->read_task); 1043 GNUNET_SCHEDULER_cancel (connection->read_task);
1041 connection->read_task = NULL; 1044 connection->read_task = NULL;
1042 } 1045 }
1043 if (NULL != connection->nth.timeout_task) 1046 if (NULL != connection->nth.timeout_task)
1044 { 1047 {
1045 GNUNET_SCHEDULER_cancel(connection->nth.timeout_task); 1048 GNUNET_SCHEDULER_cancel (connection->nth.timeout_task);
1046 connection->nth.timeout_task = NULL; 1049 connection->nth.timeout_task = NULL;
1047 } 1050 }
1048 connection->nth.notify_ready = NULL; 1051 connection->nth.notify_ready = NULL;
1049 if (NULL != connection->dns_active) 1052 if (NULL != connection->dns_active)
1050 { 1053 {
1051 GNUNET_RESOLVER_request_cancel(connection->dns_active); 1054 GNUNET_RESOLVER_request_cancel (connection->dns_active);
1052 connection->dns_active = NULL; 1055 connection->dns_active = NULL;
1053 } 1056 }
1054 if (NULL != connection->proxy_handshake) 1057 if (NULL != connection->proxy_handshake)
1055 { 1058 {
1056 /* GNUNET_CONNECTION_destroy (connection->proxy_handshake); */ 1059 /* GNUNET_CONNECTION_destroy (connection->proxy_handshake); */
1057 connection->proxy_handshake->destroy_later = -1; 1060 connection->proxy_handshake->destroy_later = -1;
1058 connection->proxy_handshake = NULL; /* Not leaked ??? */ 1061 connection->proxy_handshake = NULL; /* Not leaked ??? */
1059 } 1062 }
1060 while (NULL != (pos = connection->ap_head)) 1063 while (NULL != (pos = connection->ap_head))
1061 { 1064 {
1062 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(pos->sock)); 1065 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pos->sock));
1063 GNUNET_SCHEDULER_cancel(pos->task); 1066 GNUNET_SCHEDULER_cancel (pos->task);
1064 GNUNET_CONTAINER_DLL_remove(connection->ap_head, connection->ap_tail, pos); 1067 GNUNET_CONTAINER_DLL_remove (connection->ap_head, connection->ap_tail, pos);
1065 GNUNET_free(pos); 1068 GNUNET_free (pos);
1066 } 1069 }
1067 if ((NULL != connection->sock) && (GNUNET_YES != connection->persist)) 1070 if ((NULL != connection->sock) && (GNUNET_YES != connection->persist))
1071 {
1072 if ((GNUNET_OK !=
1073 GNUNET_NETWORK_socket_shutdown (connection->sock, SHUT_RDWR)) &&
1074 (ENOTCONN != errno) && (ECONNRESET != errno))
1075 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "shutdown");
1076 }
1077 if (NULL != connection->sock)
1078 {
1079 if (GNUNET_YES != connection->persist)
1068 { 1080 {
1069 if ((GNUNET_OK != 1081 GNUNET_break (GNUNET_OK ==
1070 GNUNET_NETWORK_socket_shutdown(connection->sock, SHUT_RDWR)) && 1082 GNUNET_NETWORK_socket_close (connection->sock));
1071 (ENOTCONN != errno) && (ECONNRESET != errno))
1072 LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "shutdown");
1073 } 1083 }
1074 if (NULL != connection->sock) 1084 else
1075 { 1085 {
1076 if (GNUNET_YES != connection->persist) 1086 GNUNET_NETWORK_socket_free_memory_only_ (
1077 { 1087 connection->sock); /* at least no memory leak (we deliberately
1078 GNUNET_break(GNUNET_OK ==
1079 GNUNET_NETWORK_socket_close(connection->sock));
1080 }
1081 else
1082 {
1083 GNUNET_NETWORK_socket_free_memory_only_(
1084 connection->sock); /* at least no memory leak (we deliberately
1085 * leak the socket in this special case) ... */ 1088 * leak the socket in this special case) ... */
1086 }
1087 } 1089 }
1088 GNUNET_free_non_null(connection->addr); 1090 }
1089 GNUNET_free_non_null(connection->hostname); 1091 GNUNET_free_non_null (connection->addr);
1090 GNUNET_free(connection->write_buffer); 1092 GNUNET_free_non_null (connection->hostname);
1091 GNUNET_free(connection); 1093 GNUNET_free (connection->write_buffer);
1094 GNUNET_free (connection);
1092} 1095}
1093 1096
1094 1097
@@ -1099,7 +1102,7 @@ GNUNET_CONNECTION_destroy(struct GNUNET_CONNECTION_Handle *connection)
1099 * @param cls connection to read from 1102 * @param cls connection to read from
1100 */ 1103 */
1101static void 1104static void
1102receive_ready(void *cls) 1105receive_ready (void *cls)
1103{ 1106{
1104 struct GNUNET_CONNECTION_Handle *connection = cls; 1107 struct GNUNET_CONNECTION_Handle *connection = cls;
1105 const struct GNUNET_SCHEDULER_TaskContext *tc; 1108 const struct GNUNET_SCHEDULER_TaskContext *tc;
@@ -1108,49 +1111,50 @@ receive_ready(void *cls)
1108 GNUNET_CONNECTION_Receiver receiver; 1111 GNUNET_CONNECTION_Receiver receiver;
1109 1112
1110 connection->read_task = NULL; 1113 connection->read_task = NULL;
1111 tc = GNUNET_SCHEDULER_get_task_context(); 1114 tc = GNUNET_SCHEDULER_get_task_context ();
1112 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) 1115 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1113 { 1116 {
1114 LOG(GNUNET_ERROR_TYPE_DEBUG, 1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1115 "Receive from `%s' encounters error: timeout (%s, %p)\n", 1118 "Receive from `%s' encounters error: timeout (%s, %p)\n",
1116 GNUNET_a2s(connection->addr, connection->addrlen), 1119 GNUNET_a2s (connection->addr, connection->addrlen),
1117 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration( 1120 GNUNET_STRINGS_relative_time_to_string (
1118 connection->receive_timeout), 1121 GNUNET_TIME_absolute_get_duration (
1119 GNUNET_YES), 1122 connection->receive_timeout),
1120 connection); 1123 GNUNET_YES),
1121 signal_receive_timeout(connection); 1124 connection);
1122 return; 1125 signal_receive_timeout (connection);
1123 } 1126 return;
1127 }
1124 if (NULL == connection->sock) 1128 if (NULL == connection->sock)
1125 { 1129 {
1126 /* connect failed for good */ 1130 /* connect failed for good */
1127 signal_receive_error(connection, ECONNREFUSED); 1131 signal_receive_error (connection, ECONNREFUSED);
1128 return; 1132 return;
1129 } 1133 }
1130 GNUNET_assert(GNUNET_NETWORK_fdset_isset(tc->read_ready, connection->sock)); 1134 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, connection->sock));
1131RETRY: 1135RETRY:
1132 ret = GNUNET_NETWORK_socket_recv(connection->sock, buffer, connection->max); 1136 ret = GNUNET_NETWORK_socket_recv (connection->sock, buffer, connection->max);
1133 if (-1 == ret) 1137 if (-1 == ret)
1134 { 1138 {
1135 if (EINTR == errno) 1139 if (EINTR == errno)
1136 goto RETRY; 1140 goto RETRY;
1137 signal_receive_error(connection, errno); 1141 signal_receive_error (connection, errno);
1138 return; 1142 return;
1139 } 1143 }
1140 LOG(GNUNET_ERROR_TYPE_DEBUG, 1144 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "receive_ready read %u/%u bytes from `%s' (%p)!\n", 1145 "receive_ready read %u/%u bytes from `%s' (%p)!\n",
1142 (unsigned int)ret, 1146 (unsigned int) ret,
1143 connection->max, 1147 connection->max,
1144 GNUNET_a2s(connection->addr, connection->addrlen), 1148 GNUNET_a2s (connection->addr, connection->addrlen),
1145 connection); 1149 connection);
1146 GNUNET_assert(NULL != (receiver = connection->receiver)); 1150 GNUNET_assert (NULL != (receiver = connection->receiver));
1147 connection->receiver = NULL; 1151 connection->receiver = NULL;
1148 receiver(connection->receiver_cls, 1152 receiver (connection->receiver_cls,
1149 buffer, 1153 buffer,
1150 ret, 1154 ret,
1151 connection->addr, 1155 connection->addr,
1152 connection->addrlen, 1156 connection->addrlen,
1153 0); 1157 0);
1154} 1158}
1155 1159
1156 1160
@@ -1170,36 +1174,36 @@ RETRY:
1170 * called with error) 1174 * called with error)
1171 */ 1175 */
1172int 1176int
1173GNUNET_CONNECTION_receive(struct GNUNET_CONNECTION_Handle *connection, 1177GNUNET_CONNECTION_receive (struct GNUNET_CONNECTION_Handle *connection,
1174 size_t max, 1178 size_t max,
1175 struct GNUNET_TIME_Relative timeout, 1179 struct GNUNET_TIME_Relative timeout,
1176 GNUNET_CONNECTION_Receiver receiver, 1180 GNUNET_CONNECTION_Receiver receiver,
1177 void *receiver_cls) 1181 void *receiver_cls)
1178{ 1182{
1179 GNUNET_assert((NULL == connection->read_task) && 1183 GNUNET_assert ((NULL == connection->read_task) &&
1180 (NULL == connection->receiver)); 1184 (NULL == connection->receiver));
1181 GNUNET_assert(NULL != receiver); 1185 GNUNET_assert (NULL != receiver);
1182 connection->receiver = receiver; 1186 connection->receiver = receiver;
1183 connection->receiver_cls = receiver_cls; 1187 connection->receiver_cls = receiver_cls;
1184 connection->receive_timeout = GNUNET_TIME_relative_to_absolute(timeout); 1188 connection->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1185 connection->max = max; 1189 connection->max = max;
1186 if (NULL != connection->sock) 1190 if (NULL != connection->sock)
1187 { 1191 {
1188 connection->read_task = 1192 connection->read_task =
1189 GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_absolute_get_remaining( 1193 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining (
1190 connection->receive_timeout), 1194 connection->receive_timeout),
1191 connection->sock, 1195 connection->sock,
1192 &receive_ready, 1196 &receive_ready,
1193 connection); 1197 connection);
1194 return GNUNET_OK; 1198 return GNUNET_OK;
1195 } 1199 }
1196 if ((NULL == connection->dns_active) && (NULL == connection->ap_head) && 1200 if ((NULL == connection->dns_active) && (NULL == connection->ap_head) &&
1197 (NULL == connection->proxy_handshake)) 1201 (NULL == connection->proxy_handshake))
1198 { 1202 {
1199 connection->receiver = NULL; 1203 connection->receiver = NULL;
1200 receiver(receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT); 1204 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1201 return GNUNET_SYSERR; 1205 return GNUNET_SYSERR;
1202 } 1206 }
1203 return GNUNET_OK; 1207 return GNUNET_OK;
1204} 1208}
1205 1209
@@ -1213,14 +1217,14 @@ GNUNET_CONNECTION_receive(struct GNUNET_CONNECTION_Handle *connection,
1213 * @return closure of the original receiver callback closure 1217 * @return closure of the original receiver callback closure
1214 */ 1218 */
1215void * 1219void *
1216GNUNET_CONNECTION_receive_cancel(struct GNUNET_CONNECTION_Handle *connection) 1220GNUNET_CONNECTION_receive_cancel (struct GNUNET_CONNECTION_Handle *connection)
1217{ 1221{
1218 if (NULL != connection->read_task) 1222 if (NULL != connection->read_task)
1219 { 1223 {
1220 GNUNET_assert(connection == 1224 GNUNET_assert (connection ==
1221 GNUNET_SCHEDULER_cancel(connection->read_task)); 1225 GNUNET_SCHEDULER_cancel (connection->read_task));
1222 connection->read_task = NULL; 1226 connection->read_task = NULL;
1223 } 1227 }
1224 connection->receiver = NULL; 1228 connection->receiver = NULL;
1225 return connection->receiver_cls; 1229 return connection->receiver_cls;
1226} 1230}
@@ -1234,44 +1238,44 @@ GNUNET_CONNECTION_receive_cancel(struct GNUNET_CONNECTION_Handle *connection)
1234 * @return #GNUNET_YES if we were able to call notify 1238 * @return #GNUNET_YES if we were able to call notify
1235 */ 1239 */
1236static int 1240static int
1237process_notify(struct GNUNET_CONNECTION_Handle *connection) 1241process_notify (struct GNUNET_CONNECTION_Handle *connection)
1238{ 1242{
1239 size_t used; 1243 size_t used;
1240 size_t avail; 1244 size_t avail;
1241 size_t size; 1245 size_t size;
1242 GNUNET_CONNECTION_TransmitReadyNotify notify; 1246 GNUNET_CONNECTION_TransmitReadyNotify notify;
1243 1247
1244 LOG(GNUNET_ERROR_TYPE_DEBUG, "process_notify is running\n"); 1248 LOG (GNUNET_ERROR_TYPE_DEBUG, "process_notify is running\n");
1245 GNUNET_assert(NULL == connection->write_task); 1249 GNUNET_assert (NULL == connection->write_task);
1246 if (NULL == (notify = connection->nth.notify_ready)) 1250 if (NULL == (notify = connection->nth.notify_ready))
1247 { 1251 {
1248 LOG(GNUNET_ERROR_TYPE_DEBUG, "No one to notify\n"); 1252 LOG (GNUNET_ERROR_TYPE_DEBUG, "No one to notify\n");
1249 return GNUNET_NO; 1253 return GNUNET_NO;
1250 } 1254 }
1251 used = connection->write_buffer_off - connection->write_buffer_pos; 1255 used = connection->write_buffer_off - connection->write_buffer_pos;
1252 avail = connection->write_buffer_size - used; 1256 avail = connection->write_buffer_size - used;
1253 size = connection->nth.notify_size; 1257 size = connection->nth.notify_size;
1254 if (size > avail) 1258 if (size > avail)
1255 { 1259 {
1256 LOG(GNUNET_ERROR_TYPE_DEBUG, "Not enough buffer\n"); 1260 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough buffer\n");
1257 return GNUNET_NO; 1261 return GNUNET_NO;
1258 } 1262 }
1259 connection->nth.notify_ready = NULL; 1263 connection->nth.notify_ready = NULL;
1260 if (connection->write_buffer_size - connection->write_buffer_off < size) 1264 if (connection->write_buffer_size - connection->write_buffer_off < size)
1261 { 1265 {
1262 /* need to compact */ 1266 /* need to compact */
1263 memmove(connection->write_buffer, 1267 memmove (connection->write_buffer,
1264 &connection->write_buffer[connection->write_buffer_pos], 1268 &connection->write_buffer[connection->write_buffer_pos],
1265 used); 1269 used);
1266 connection->write_buffer_off -= connection->write_buffer_pos; 1270 connection->write_buffer_off -= connection->write_buffer_pos;
1267 connection->write_buffer_pos = 0; 1271 connection->write_buffer_pos = 0;
1268 } 1272 }
1269 avail = connection->write_buffer_size - connection->write_buffer_off; 1273 avail = connection->write_buffer_size - connection->write_buffer_off;
1270 GNUNET_assert(avail >= size); 1274 GNUNET_assert (avail >= size);
1271 size = notify(connection->nth.notify_ready_cls, 1275 size = notify (connection->nth.notify_ready_cls,
1272 avail, 1276 avail,
1273 &connection->write_buffer[connection->write_buffer_off]); 1277 &connection->write_buffer[connection->write_buffer_off]);
1274 GNUNET_assert(size <= avail); 1278 GNUNET_assert (size <= avail);
1275 if (0 != size) 1279 if (0 != size)
1276 connection->write_buffer_off += size; 1280 connection->write_buffer_off += size;
1277 return GNUNET_YES; 1281 return GNUNET_YES;
@@ -1289,22 +1293,22 @@ process_notify(struct GNUNET_CONNECTION_Handle *connection)
1289 * @param cls the `struct GNUNET_CONNECTION_Handle` 1293 * @param cls the `struct GNUNET_CONNECTION_Handle`
1290 */ 1294 */
1291static void 1295static void
1292transmit_timeout(void *cls) 1296transmit_timeout (void *cls)
1293{ 1297{
1294 struct GNUNET_CONNECTION_Handle *connection = cls; 1298 struct GNUNET_CONNECTION_Handle *connection = cls;
1295 GNUNET_CONNECTION_TransmitReadyNotify notify; 1299 GNUNET_CONNECTION_TransmitReadyNotify notify;
1296 1300
1297 connection->nth.timeout_task = NULL; 1301 connection->nth.timeout_task = NULL;
1298 LOG(GNUNET_ERROR_TYPE_DEBUG, 1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1299 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n", 1303 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1300 connection->hostname, 1304 connection->hostname,
1301 connection->port, 1305 connection->port,
1302 GNUNET_a2s(connection->addr, connection->addrlen), 1306 GNUNET_a2s (connection->addr, connection->addrlen),
1303 connection); 1307 connection);
1304 notify = connection->nth.notify_ready; 1308 notify = connection->nth.notify_ready;
1305 GNUNET_assert(NULL != notify); 1309 GNUNET_assert (NULL != notify);
1306 connection->nth.notify_ready = NULL; 1310 connection->nth.notify_ready = NULL;
1307 notify(connection->nth.notify_ready_cls, 0, NULL); 1311 notify (connection->nth.notify_ready_cls, 0, NULL);
1308} 1312}
1309 1313
1310 1314
@@ -1317,21 +1321,21 @@ transmit_timeout(void *cls)
1317 * @param cls the `struct GNUNET_CONNECTION_Handle` 1321 * @param cls the `struct GNUNET_CONNECTION_Handle`
1318 */ 1322 */
1319static void 1323static void
1320connect_error(void *cls) 1324connect_error (void *cls)
1321{ 1325{
1322 struct GNUNET_CONNECTION_Handle *connection = cls; 1326 struct GNUNET_CONNECTION_Handle *connection = cls;
1323 GNUNET_CONNECTION_TransmitReadyNotify notify; 1327 GNUNET_CONNECTION_TransmitReadyNotify notify;
1324 1328
1325 LOG(GNUNET_ERROR_TYPE_DEBUG, 1329 LOG (GNUNET_ERROR_TYPE_DEBUG,
1326 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n", 1330 "Transmission request of size %u fails (%s/%u), connection failed (%p).\n",
1327 connection->nth.notify_size, 1331 connection->nth.notify_size,
1328 connection->hostname, 1332 connection->hostname,
1329 connection->port, 1333 connection->port,
1330 connection); 1334 connection);
1331 connection->write_task = NULL; 1335 connection->write_task = NULL;
1332 notify = connection->nth.notify_ready; 1336 notify = connection->nth.notify_ready;
1333 connection->nth.notify_ready = NULL; 1337 connection->nth.notify_ready = NULL;
1334 notify(connection->nth.notify_ready_cls, 0, NULL); 1338 notify (connection->nth.notify_ready_cls, 0, NULL);
1335} 1339}
1336 1340
1337 1341
@@ -1341,7 +1345,7 @@ connect_error(void *cls)
1341 * @param cls our connection handle 1345 * @param cls our connection handle
1342 */ 1346 */
1343static void 1347static void
1344transmit_ready(void *cls) 1348transmit_ready (void *cls)
1345{ 1349{
1346 struct GNUNET_CONNECTION_Handle *connection = cls; 1350 struct GNUNET_CONNECTION_Handle *connection = cls;
1347 GNUNET_CONNECTION_TransmitReadyNotify notify; 1351 GNUNET_CONNECTION_TransmitReadyNotify notify;
@@ -1349,107 +1353,107 @@ transmit_ready(void *cls)
1349 ssize_t ret; 1353 ssize_t ret;
1350 size_t have; 1354 size_t have;
1351 1355
1352 LOG(GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", connection); 1356 LOG (GNUNET_ERROR_TYPE_DEBUG, "transmit_ready running (%p).\n", connection);
1353 GNUNET_assert(NULL != connection->write_task); 1357 GNUNET_assert (NULL != connection->write_task);
1354 connection->write_task = NULL; 1358 connection->write_task = NULL;
1355 GNUNET_assert(NULL == connection->nth.timeout_task); 1359 GNUNET_assert (NULL == connection->nth.timeout_task);
1356 tc = GNUNET_SCHEDULER_get_task_context(); 1360 tc = GNUNET_SCHEDULER_get_task_context ();
1357 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT)) 1361 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1358 { 1362 {
1359 LOG(GNUNET_ERROR_TYPE_DEBUG, 1363 LOG (GNUNET_ERROR_TYPE_DEBUG,
1360 "Transmit to `%s' fails, time out reached (%p).\n", 1364 "Transmit to `%s' fails, time out reached (%p).\n",
1361 GNUNET_a2s(connection->addr, connection->addrlen), 1365 GNUNET_a2s (connection->addr, connection->addrlen),
1362 connection); 1366 connection);
1363 notify = connection->nth.notify_ready; 1367 notify = connection->nth.notify_ready;
1364 GNUNET_assert(NULL != notify); 1368 GNUNET_assert (NULL != notify);
1365 connection->nth.notify_ready = NULL; 1369 connection->nth.notify_ready = NULL;
1366 notify(connection->nth.notify_ready_cls, 0, NULL); 1370 notify (connection->nth.notify_ready_cls, 0, NULL);
1367 return; 1371 return;
1368 } 1372 }
1369 GNUNET_assert(NULL != connection->sock); 1373 GNUNET_assert (NULL != connection->sock);
1370 if (NULL == tc->write_ready) 1374 if (NULL == tc->write_ready)
1371 { 1375 {
1372 /* special circumstances (in particular, PREREQ_DONE after 1376 /* special circumstances (in particular, PREREQ_DONE after
1373 * connect): not yet ready to write, but no "fatal" error either. 1377 * connect): not yet ready to write, but no "fatal" error either.
1374 * Hence retry. */ 1378 * Hence retry. */
1375 goto SCHEDULE_WRITE; 1379 goto SCHEDULE_WRITE;
1376 } 1380 }
1377 if (!GNUNET_NETWORK_fdset_isset(tc->write_ready, connection->sock)) 1381 if (! GNUNET_NETWORK_fdset_isset (tc->write_ready, connection->sock))
1378 { 1382 {
1379 GNUNET_assert(NULL == connection->write_task); 1383 GNUNET_assert (NULL == connection->write_task);
1380 /* special circumstances (in particular, shutdown): not yet ready 1384 /* special circumstances (in particular, shutdown): not yet ready
1381 * to write, but no "fatal" error either. Hence retry. */ 1385 * to write, but no "fatal" error either. Hence retry. */
1382 goto SCHEDULE_WRITE; 1386 goto SCHEDULE_WRITE;
1383 } 1387 }
1384 GNUNET_assert(connection->write_buffer_off >= connection->write_buffer_pos); 1388 GNUNET_assert (connection->write_buffer_off >= connection->write_buffer_pos);
1385 if ((NULL != connection->nth.notify_ready) && 1389 if ((NULL != connection->nth.notify_ready) &&
1386 (connection->write_buffer_size < connection->nth.notify_size)) 1390 (connection->write_buffer_size < connection->nth.notify_size))
1387 { 1391 {
1388 connection->write_buffer = 1392 connection->write_buffer =
1389 GNUNET_realloc(connection->write_buffer, connection->nth.notify_size); 1393 GNUNET_realloc (connection->write_buffer, connection->nth.notify_size);
1390 connection->write_buffer_size = connection->nth.notify_size; 1394 connection->write_buffer_size = connection->nth.notify_size;
1391 } 1395 }
1392 process_notify(connection); 1396 process_notify (connection);
1393 have = connection->write_buffer_off - connection->write_buffer_pos; 1397 have = connection->write_buffer_off - connection->write_buffer_pos;
1394 if (0 == have) 1398 if (0 == have)
1395 { 1399 {
1396 /* no data ready for writing, terminate write loop */ 1400 /* no data ready for writing, terminate write loop */
1397 return; 1401 return;
1398 } 1402 }
1399 GNUNET_assert(have <= connection->write_buffer_size); 1403 GNUNET_assert (have <= connection->write_buffer_size);
1400 GNUNET_assert(have + connection->write_buffer_pos <= 1404 GNUNET_assert (have + connection->write_buffer_pos <=
1401 connection->write_buffer_size); 1405 connection->write_buffer_size);
1402 GNUNET_assert(connection->write_buffer_pos <= connection->write_buffer_size); 1406 GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_size);
1403RETRY: 1407RETRY:
1404 ret = 1408 ret =
1405 GNUNET_NETWORK_socket_send(connection->sock, 1409 GNUNET_NETWORK_socket_send (connection->sock,
1406 &connection 1410 &connection
1407 ->write_buffer[connection->write_buffer_pos], 1411 ->write_buffer[connection->write_buffer_pos],
1408 have); 1412 have);
1409 if (-1 == ret) 1413 if (-1 == ret)
1414 {
1415 if (EINTR == errno)
1416 goto RETRY;
1417 if (NULL != connection->write_task)
1410 { 1418 {
1411 if (EINTR == errno) 1419 GNUNET_SCHEDULER_cancel (connection->write_task);
1412 goto RETRY; 1420 connection->write_task = NULL;
1413 if (NULL != connection->write_task)
1414 {
1415 GNUNET_SCHEDULER_cancel(connection->write_task);
1416 connection->write_task = NULL;
1417 }
1418 signal_transmit_error(connection, errno);
1419 return;
1420 } 1421 }
1421 LOG(GNUNET_ERROR_TYPE_DEBUG, 1422 signal_transmit_error (connection, errno);
1422 "Connection transmitted %u/%u bytes to `%s' (%p)\n", 1423 return;
1423 (unsigned int)ret, 1424 }
1424 have, 1425 LOG (GNUNET_ERROR_TYPE_DEBUG,
1425 GNUNET_a2s(connection->addr, connection->addrlen), 1426 "Connection transmitted %u/%u bytes to `%s' (%p)\n",
1426 connection); 1427 (unsigned int) ret,
1428 have,
1429 GNUNET_a2s (connection->addr, connection->addrlen),
1430 connection);
1427 connection->write_buffer_pos += ret; 1431 connection->write_buffer_pos += ret;
1428 if (connection->write_buffer_pos == connection->write_buffer_off) 1432 if (connection->write_buffer_pos == connection->write_buffer_off)
1429 { 1433 {
1430 /* transmitted all pending data */ 1434 /* transmitted all pending data */
1431 connection->write_buffer_pos = 0; 1435 connection->write_buffer_pos = 0;
1432 connection->write_buffer_off = 0; 1436 connection->write_buffer_off = 0;
1433 } 1437 }
1434 if ((0 == connection->write_buffer_off) && 1438 if ((0 == connection->write_buffer_off) &&
1435 (NULL == connection->nth.notify_ready)) 1439 (NULL == connection->nth.notify_ready))
1436 return; /* all data sent! */ 1440 return; /* all data sent! */
1437 /* not done writing, schedule more */ 1441 /* not done writing, schedule more */
1438SCHEDULE_WRITE: 1442SCHEDULE_WRITE:
1439 LOG(GNUNET_ERROR_TYPE_DEBUG, 1443 LOG (GNUNET_ERROR_TYPE_DEBUG,
1440 "Re-scheduling transmit_ready (more to do) (%p).\n", 1444 "Re-scheduling transmit_ready (more to do) (%p).\n",
1441 connection); 1445 connection);
1442 have = connection->write_buffer_off - connection->write_buffer_pos; 1446 have = connection->write_buffer_off - connection->write_buffer_pos;
1443 GNUNET_assert((NULL != connection->nth.notify_ready) || (have > 0)); 1447 GNUNET_assert ((NULL != connection->nth.notify_ready) || (have > 0));
1444 if (NULL == connection->write_task) 1448 if (NULL == connection->write_task)
1445 connection->write_task = 1449 connection->write_task =
1446 GNUNET_SCHEDULER_add_write_net((connection->nth.notify_ready == NULL) 1450 GNUNET_SCHEDULER_add_write_net ((connection->nth.notify_ready == NULL)
1447 ? GNUNET_TIME_UNIT_FOREVER_REL 1451 ? GNUNET_TIME_UNIT_FOREVER_REL
1448 : GNUNET_TIME_absolute_get_remaining( 1452 : GNUNET_TIME_absolute_get_remaining (
1449 connection->nth.transmit_timeout), 1453 connection->nth.transmit_timeout),
1450 connection->sock, 1454 connection->sock,
1451 &transmit_ready, 1455 &transmit_ready,
1452 connection); 1456 connection);
1453} 1457}
1454 1458
1455 1459
@@ -1468,7 +1472,7 @@ SCHEDULE_WRITE:
1468 * NULL if we are already going to notify someone else (busy) 1472 * NULL if we are already going to notify someone else (busy)
1469 */ 1473 */
1470struct GNUNET_CONNECTION_TransmitHandle * 1474struct GNUNET_CONNECTION_TransmitHandle *
1471GNUNET_CONNECTION_notify_transmit_ready( 1475GNUNET_CONNECTION_notify_transmit_ready (
1472 struct GNUNET_CONNECTION_Handle *connection, 1476 struct GNUNET_CONNECTION_Handle *connection,
1473 size_t size, 1477 size_t size,
1474 struct GNUNET_TIME_Relative timeout, 1478 struct GNUNET_TIME_Relative timeout,
@@ -1476,52 +1480,52 @@ GNUNET_CONNECTION_notify_transmit_ready(
1476 void *notify_cls) 1480 void *notify_cls)
1477{ 1481{
1478 if (NULL != connection->nth.notify_ready) 1482 if (NULL != connection->nth.notify_ready)
1479 { 1483 {
1480 GNUNET_assert(0); 1484 GNUNET_assert (0);
1481 return NULL; 1485 return NULL;
1482 } 1486 }
1483 GNUNET_assert(NULL != notify); 1487 GNUNET_assert (NULL != notify);
1484 GNUNET_assert(size < GNUNET_MAX_MESSAGE_SIZE); 1488 GNUNET_assert (size < GNUNET_MAX_MESSAGE_SIZE);
1485 GNUNET_assert(connection->write_buffer_off <= connection->write_buffer_size); 1489 GNUNET_assert (connection->write_buffer_off <= connection->write_buffer_size);
1486 GNUNET_assert(connection->write_buffer_pos <= connection->write_buffer_size); 1490 GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_size);
1487 GNUNET_assert(connection->write_buffer_pos <= connection->write_buffer_off); 1491 GNUNET_assert (connection->write_buffer_pos <= connection->write_buffer_off);
1488 connection->nth.notify_ready = notify; 1492 connection->nth.notify_ready = notify;
1489 connection->nth.notify_ready_cls = notify_cls; 1493 connection->nth.notify_ready_cls = notify_cls;
1490 connection->nth.connection = connection; 1494 connection->nth.connection = connection;
1491 connection->nth.notify_size = size; 1495 connection->nth.notify_size = size;
1492 connection->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute(timeout); 1496 connection->nth.transmit_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1493 GNUNET_assert(NULL == connection->nth.timeout_task); 1497 GNUNET_assert (NULL == connection->nth.timeout_task);
1494 if ((NULL == connection->sock) && (NULL == connection->ap_head) && 1498 if ((NULL == connection->sock) && (NULL == connection->ap_head) &&
1495 (NULL == connection->dns_active) && (NULL == connection->proxy_handshake)) 1499 (NULL == connection->dns_active) && (NULL == connection->proxy_handshake))
1496 { 1500 {
1497 if (NULL != connection->write_task) 1501 if (NULL != connection->write_task)
1498 GNUNET_SCHEDULER_cancel(connection->write_task); 1502 GNUNET_SCHEDULER_cancel (connection->write_task);
1499 connection->write_task = 1503 connection->write_task =
1500 GNUNET_SCHEDULER_add_now(&connect_error, connection); 1504 GNUNET_SCHEDULER_add_now (&connect_error, connection);
1501 return &connection->nth; 1505 return &connection->nth;
1502 } 1506 }
1503 if (NULL != connection->write_task) 1507 if (NULL != connection->write_task)
1504 return &connection->nth; /* previous transmission still in progress */ 1508 return &connection->nth; /* previous transmission still in progress */
1505 if (NULL != connection->sock) 1509 if (NULL != connection->sock)
1506 { 1510 {
1507 /* connected, try to transmit now */ 1511 /* connected, try to transmit now */
1508 LOG(GNUNET_ERROR_TYPE_DEBUG, 1512 LOG (GNUNET_ERROR_TYPE_DEBUG,
1509 "Scheduling transmission (%p).\n", 1513 "Scheduling transmission (%p).\n",
1510 connection); 1514 connection);
1511 connection->write_task = 1515 connection->write_task =
1512 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_absolute_get_remaining( 1516 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_absolute_get_remaining (
1513 connection->nth.transmit_timeout), 1517 connection->nth.transmit_timeout),
1514 connection->sock, 1518 connection->sock,
1515 &transmit_ready, 1519 &transmit_ready,
1516 connection); 1520 connection);
1517 return &connection->nth; 1521 return &connection->nth;
1518 } 1522 }
1519 /* not yet connected, wait for connection */ 1523 /* not yet connected, wait for connection */
1520 LOG(GNUNET_ERROR_TYPE_DEBUG, 1524 LOG (GNUNET_ERROR_TYPE_DEBUG,
1521 "Need to wait to schedule transmission for connection, adding timeout task (%p).\n", 1525 "Need to wait to schedule transmission for connection, adding timeout task (%p).\n",
1522 connection); 1526 connection);
1523 connection->nth.timeout_task = 1527 connection->nth.timeout_task =
1524 GNUNET_SCHEDULER_add_delayed(timeout, &transmit_timeout, connection); 1528 GNUNET_SCHEDULER_add_delayed (timeout, &transmit_timeout, connection);
1525 return &connection->nth; 1529 return &connection->nth;
1526} 1530}
1527 1531
@@ -1532,21 +1536,21 @@ GNUNET_CONNECTION_notify_transmit_ready(
1532 * @param th notification to cancel 1536 * @param th notification to cancel
1533 */ 1537 */
1534void 1538void
1535GNUNET_CONNECTION_notify_transmit_ready_cancel( 1539GNUNET_CONNECTION_notify_transmit_ready_cancel (
1536 struct GNUNET_CONNECTION_TransmitHandle *th) 1540 struct GNUNET_CONNECTION_TransmitHandle *th)
1537{ 1541{
1538 GNUNET_assert(NULL != th->notify_ready); 1542 GNUNET_assert (NULL != th->notify_ready);
1539 th->notify_ready = NULL; 1543 th->notify_ready = NULL;
1540 if (NULL != th->timeout_task) 1544 if (NULL != th->timeout_task)
1541 { 1545 {
1542 GNUNET_SCHEDULER_cancel(th->timeout_task); 1546 GNUNET_SCHEDULER_cancel (th->timeout_task);
1543 th->timeout_task = NULL; 1547 th->timeout_task = NULL;
1544 } 1548 }
1545 if (NULL != th->connection->write_task) 1549 if (NULL != th->connection->write_task)
1546 { 1550 {
1547 GNUNET_SCHEDULER_cancel(th->connection->write_task); 1551 GNUNET_SCHEDULER_cancel (th->connection->write_task);
1548 th->connection->write_task = NULL; 1552 th->connection->write_task = NULL;
1549 } 1553 }
1550} 1554}
1551 1555
1552 1556
@@ -1557,11 +1561,11 @@ GNUNET_CONNECTION_notify_transmit_ready_cancel(
1557 * @return connection to be proxied 1561 * @return connection to be proxied
1558 */ 1562 */
1559struct GNUNET_CONNECTION_Handle * 1563struct GNUNET_CONNECTION_Handle *
1560GNUNET_CONNECTION_create_proxied_from_handshake( 1564GNUNET_CONNECTION_create_proxied_from_handshake (
1561 struct GNUNET_CONNECTION_Handle *cph) 1565 struct GNUNET_CONNECTION_Handle *cph)
1562{ 1566{
1563 struct GNUNET_CONNECTION_Handle *proxied = 1567 struct GNUNET_CONNECTION_Handle *proxied =
1564 GNUNET_CONNECTION_create_from_existing(NULL); 1568 GNUNET_CONNECTION_create_from_existing (NULL);
1565 1569
1566 proxied->proxy_handshake = cph; 1570 proxied->proxy_handshake = cph;
1567 return proxied; 1571 return proxied;
@@ -1576,17 +1580,17 @@ GNUNET_CONNECTION_create_proxied_from_handshake(
1576 * @param proxied connection connection to proxy server 1580 * @param proxied connection connection to proxy server
1577 */ 1581 */
1578void 1582void
1579GNUNET_CONNECTION_acivate_proxied(struct GNUNET_CONNECTION_Handle *proxied) 1583GNUNET_CONNECTION_acivate_proxied (struct GNUNET_CONNECTION_Handle *proxied)
1580{ 1584{
1581 struct GNUNET_CONNECTION_Handle *cph = proxied->proxy_handshake; 1585 struct GNUNET_CONNECTION_Handle *cph = proxied->proxy_handshake;
1582 1586
1583 GNUNET_assert(NULL != cph); 1587 GNUNET_assert (NULL != cph);
1584 GNUNET_assert(NULL == proxied->sock); 1588 GNUNET_assert (NULL == proxied->sock);
1585 GNUNET_assert(NULL != cph->sock); 1589 GNUNET_assert (NULL != cph->sock);
1586 proxied->sock = cph->sock; 1590 proxied->sock = cph->sock;
1587 cph->sock = NULL; 1591 cph->sock = NULL;
1588 GNUNET_CONNECTION_destroy(cph); 1592 GNUNET_CONNECTION_destroy (cph);
1589 connect_success_continuation(proxied); 1593 connect_success_continuation (proxied);
1590} 1594}
1591 1595
1592 1596