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