diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-06-01 21:07:39 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-06-01 21:07:39 +0000 |
commit | e3e29a9dce520c5ebc372d890f8f59c19d83e65d (patch) | |
tree | 2d537b352931d17106f2cf22e67c19ea71c84bb6 /src/transport/plugin_transport_tcp.c | |
parent | 2415f7e6c1043ceab64e7190efd5258f4704a804 (diff) | |
download | gnunet-e3e29a9dce520c5ebc372d890f8f59c19d83e65d.tar.gz gnunet-e3e29a9dce520c5ebc372d890f8f59c19d83e65d.zip |
-minor code cleanups
Diffstat (limited to 'src/transport/plugin_transport_tcp.c')
-rw-r--r-- | src/transport/plugin_transport_tcp.c | 96 |
1 files changed, 52 insertions, 44 deletions
diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c index 88950ea79..4d742eced 100644 --- a/src/transport/plugin_transport_tcp.c +++ b/src/transport/plugin_transport_tcp.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet | 2 | This file is part of GNUnet |
3 | (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors) | 3 | (C) 2002--2012 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
@@ -339,7 +339,10 @@ struct Plugin | |||
339 | */ | 339 | */ |
340 | struct GNUNET_NAT_Handle *nat; | 340 | struct GNUNET_NAT_Handle *nat; |
341 | 341 | ||
342 | struct GNUNET_CONTAINER_MultiHashMap * sessionmap; | 342 | /** |
343 | * Map from peer identities to sessions for the given peer. | ||
344 | */ | ||
345 | struct GNUNET_CONTAINER_MultiHashMap *sessionmap; | ||
343 | 346 | ||
344 | /** | 347 | /** |
345 | * Handle to the network service. | 348 | * Handle to the network service. |
@@ -407,12 +410,14 @@ struct Plugin | |||
407 | static void | 410 | static void |
408 | start_session_timeout (struct Session *s); | 411 | start_session_timeout (struct Session *s); |
409 | 412 | ||
413 | |||
410 | /** | 414 | /** |
411 | * Increment session timeout due to activity | 415 | * Increment session timeout due to activity |
412 | */ | 416 | */ |
413 | static void | 417 | static void |
414 | reschedule_session_timeout (struct Session *s); | 418 | reschedule_session_timeout (struct Session *s); |
415 | 419 | ||
420 | |||
416 | /** | 421 | /** |
417 | * Cancel timeout | 422 | * Cancel timeout |
418 | */ | 423 | */ |
@@ -424,11 +429,14 @@ stop_session_timeout (struct Session *s); | |||
424 | static const char * | 429 | static const char * |
425 | tcp_address_to_string (void *cls, const void *addr, size_t addrlen); | 430 | tcp_address_to_string (void *cls, const void *addr, size_t addrlen); |
426 | 431 | ||
432 | |||
427 | static unsigned int sessions; | 433 | static unsigned int sessions; |
428 | 434 | ||
429 | static void inc_sessions (struct Plugin *plugin, struct Session *session, int line) | 435 | |
436 | static void | ||
437 | inc_sessions (struct Plugin *plugin, struct Session *session, int line) | ||
430 | { | 438 | { |
431 | sessions ++; | 439 | sessions++; |
432 | unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap); | 440 | unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap); |
433 | if (sessions != size) | 441 | if (sessions != size) |
434 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n", | 442 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n", |
@@ -441,11 +449,13 @@ static void inc_sessions (struct Plugin *plugin, struct Session *session, int li | |||
441 | tcp_address_to_string (NULL, session->addr, session->addrlen)); | 449 | tcp_address_to_string (NULL, session->addr, session->addrlen)); |
442 | } | 450 | } |
443 | 451 | ||
444 | static void dec_sessions (struct Plugin *plugin, struct Session *session, int line) | 452 | |
453 | static void | ||
454 | dec_sessions (struct Plugin *plugin, struct Session *session, int line) | ||
445 | { | 455 | { |
446 | GNUNET_assert (sessions > 0); | 456 | GNUNET_assert (sessions > 0); |
447 | unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap); | 457 | unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap); |
448 | sessions --; | 458 | sessions--; |
449 | if (sessions != size) | 459 | if (sessions != size) |
450 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n", | 460 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n", |
451 | sessions, size); | 461 | sessions, size); |
@@ -560,24 +570,23 @@ tcp_address_to_string (void *cls, const void *addr, size_t addrlen) | |||
560 | int af; | 570 | int af; |
561 | uint16_t port; | 571 | uint16_t port; |
562 | 572 | ||
563 | if (addrlen == sizeof (struct IPv6TcpAddress)) | 573 | switch (addrlen) |
564 | { | 574 | { |
575 | case sizeof (struct IPv6TcpAddress): | ||
565 | t6 = addr; | 576 | t6 = addr; |
566 | af = AF_INET6; | 577 | af = AF_INET6; |
567 | port = ntohs (t6->t6_port); | 578 | port = ntohs (t6->t6_port); |
568 | memcpy (&a6, &t6->ipv6_addr, sizeof (a6)); | 579 | memcpy (&a6, &t6->ipv6_addr, sizeof (a6)); |
569 | sb = &a6; | 580 | sb = &a6; |
570 | } | 581 | break; |
571 | else if (addrlen == sizeof (struct IPv4TcpAddress)) | 582 | case sizeof (struct IPv4TcpAddress): |
572 | { | ||
573 | t4 = addr; | 583 | t4 = addr; |
574 | af = AF_INET; | 584 | af = AF_INET; |
575 | port = ntohs (t4->t4_port); | 585 | port = ntohs (t4->t4_port); |
576 | memcpy (&a4, &t4->ipv4_addr, sizeof (a4)); | 586 | memcpy (&a4, &t4->ipv4_addr, sizeof (a4)); |
577 | sb = &a4; | 587 | sb = &a4; |
578 | } | 588 | break; |
579 | else | 589 | default: |
580 | { | ||
581 | LOG (GNUNET_ERROR_TYPE_ERROR, | 590 | LOG (GNUNET_ERROR_TYPE_ERROR, |
582 | _("Unexpected address length: %u bytes\n"), | 591 | _("Unexpected address length: %u bytes\n"), |
583 | (unsigned int) addrlen); | 592 | (unsigned int) addrlen); |
@@ -618,41 +627,36 @@ tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen, | |||
618 | GNUNET_break (0); | 627 | GNUNET_break (0); |
619 | return GNUNET_SYSERR; | 628 | return GNUNET_SYSERR; |
620 | } | 629 | } |
621 | |||
622 | if ('\0' != addr[addrlen - 1]) | 630 | if ('\0' != addr[addrlen - 1]) |
623 | { | 631 | { |
624 | GNUNET_break (0); | 632 | GNUNET_break (0); |
625 | return GNUNET_SYSERR; | 633 | return GNUNET_SYSERR; |
626 | } | 634 | } |
627 | |||
628 | if (strlen (addr) != addrlen - 1) | 635 | if (strlen (addr) != addrlen - 1) |
629 | { | 636 | { |
630 | GNUNET_break (0); | 637 | GNUNET_break (0); |
631 | return GNUNET_SYSERR; | 638 | return GNUNET_SYSERR; |
632 | } | 639 | } |
633 | 640 | if (GNUNET_OK != | |
634 | int ret = GNUNET_STRINGS_to_address_ip (addr, strlen (addr), | 641 | GNUNET_STRINGS_to_address_ip (addr, strlen (addr), |
635 | &socket_address); | 642 | &socket_address)) |
636 | |||
637 | if (ret != GNUNET_OK) | ||
638 | { | 643 | { |
639 | GNUNET_break (0); | 644 | GNUNET_break (0); |
640 | return GNUNET_SYSERR; | 645 | return GNUNET_SYSERR; |
641 | } | 646 | } |
642 | 647 | switch (socket_address.ss_family) | |
643 | if (socket_address.ss_family == AF_INET) | ||
644 | { | 648 | { |
649 | case AF_INET: | ||
645 | struct IPv4TcpAddress *t4; | 650 | struct IPv4TcpAddress *t4; |
646 | struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address; | 651 | struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address; |
652 | |||
647 | t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress)); | 653 | t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress)); |
648 | t4->ipv4_addr = in4->sin_addr.s_addr; | 654 | t4->ipv4_addr = in4->sin_addr.s_addr; |
649 | t4->t4_port = in4->sin_port; | 655 | t4->t4_port = in4->sin_port; |
650 | *buf = t4; | 656 | *buf = t4; |
651 | *added = sizeof (struct IPv4TcpAddress); | 657 | *added = sizeof (struct IPv4TcpAddress); |
652 | return GNUNET_OK; | 658 | return GNUNET_OK; |
653 | } | 659 | case AF_INET6: |
654 | else if (socket_address.ss_family == AF_INET6) | ||
655 | { | ||
656 | struct IPv6TcpAddress *t6; | 660 | struct IPv6TcpAddress *t6; |
657 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address; | 661 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address; |
658 | t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress)); | 662 | t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress)); |
@@ -661,8 +665,9 @@ tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen, | |||
661 | *buf = t6; | 665 | *buf = t6; |
662 | *added = sizeof (struct IPv6TcpAddress); | 666 | *added = sizeof (struct IPv6TcpAddress); |
663 | return GNUNET_OK; | 667 | return GNUNET_OK; |
668 | default: | ||
669 | return GNUNET_SYSERR; | ||
664 | } | 670 | } |
665 | return GNUNET_SYSERR; | ||
666 | } | 671 | } |
667 | 672 | ||
668 | 673 | ||
@@ -675,8 +680,8 @@ struct SessionClientCtx | |||
675 | 680 | ||
676 | static int | 681 | static int |
677 | session_lookup_by_client_it (void *cls, | 682 | session_lookup_by_client_it (void *cls, |
678 | const GNUNET_HashCode * key, | 683 | const GNUNET_HashCode * key, |
679 | void *value) | 684 | void *value) |
680 | { | 685 | { |
681 | struct SessionClientCtx *sc_ctx = cls; | 686 | struct SessionClientCtx *sc_ctx = cls; |
682 | struct Session *s = value; | 687 | struct Session *s = value; |
@@ -699,7 +704,7 @@ session_lookup_by_client_it (void *cls, | |||
699 | */ | 704 | */ |
700 | static struct Session * | 705 | static struct Session * |
701 | lookup_session_by_client (struct Plugin *plugin, | 706 | lookup_session_by_client (struct Plugin *plugin, |
702 | const struct GNUNET_SERVER_Client *client) | 707 | const struct GNUNET_SERVER_Client *client) |
703 | { | 708 | { |
704 | struct SessionClientCtx sc_ctx; | 709 | struct SessionClientCtx sc_ctx; |
705 | 710 | ||
@@ -715,7 +720,7 @@ lookup_session_by_client (struct Plugin *plugin, | |||
715 | * | 720 | * |
716 | * @param plugin the plugin | 721 | * @param plugin the plugin |
717 | * @param target peer to connect to | 722 | * @param target peer to connect to |
718 | * @param client client to use | 723 | * @param client client to use, reference counter must have already been increased |
719 | * @param is_nat this a NAT session, we should wait for a client to | 724 | * @param is_nat this a NAT session, we should wait for a client to |
720 | * connect to us from an address, then assign that to | 725 | * connect to us from an address, then assign that to |
721 | * the session | 726 | * the session |
@@ -729,10 +734,10 @@ create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target, | |||
729 | struct PendingMessage *pm; | 734 | struct PendingMessage *pm; |
730 | struct WelcomeMessage welcome; | 735 | struct WelcomeMessage welcome; |
731 | 736 | ||
732 | if (is_nat != GNUNET_YES) | 737 | if (GNUNET_YES != is_nat) |
733 | GNUNET_assert (client != NULL); | 738 | GNUNET_assert (NULL != client); |
734 | else | 739 | else |
735 | GNUNET_assert (client == NULL); | 740 | GNUNET_assert (NULL == client); |
736 | 741 | ||
737 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 742 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
738 | "Creating new session for peer `%4s'\n", | 743 | "Creating new session for peer `%4s'\n", |
@@ -759,7 +764,7 @@ create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target, | |||
759 | pm->message_size, GNUNET_NO); | 764 | pm->message_size, GNUNET_NO); |
760 | GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head, | 765 | GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head, |
761 | ret->pending_messages_tail, pm); | 766 | ret->pending_messages_tail, pm); |
762 | if (is_nat != GNUNET_YES) | 767 | if (GNUNET_YES != is_nat) |
763 | { | 768 | { |
764 | GNUNET_STATISTICS_update (plugin->env->stats, | 769 | GNUNET_STATISTICS_update (plugin->env->stats, |
765 | gettext_noop ("# TCP sessions active"), 1, | 770 | gettext_noop ("# TCP sessions active"), 1, |
@@ -805,10 +810,10 @@ do_transmit (void *cls, size_t size, void *buf) | |||
805 | char *cbuf; | 810 | char *cbuf; |
806 | size_t ret; | 811 | size_t ret; |
807 | 812 | ||
808 | GNUNET_assert (session != NULL); | 813 | GNUNET_assert (NULL != session); |
809 | session->transmit_handle = NULL; | 814 | session->transmit_handle = NULL; |
810 | plugin = session->plugin; | 815 | plugin = session->plugin; |
811 | if (buf == NULL) | 816 | if (NULL == buf) |
812 | { | 817 | { |
813 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 818 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
814 | "Timeout trying to transmit to peer `%4s', discarding message queue.\n", | 819 | "Timeout trying to transmit to peer `%4s', discarding message queue.\n", |
@@ -1049,8 +1054,8 @@ tcp_plugin_send (void *cls, | |||
1049 | struct Plugin * plugin = cls; | 1054 | struct Plugin * plugin = cls; |
1050 | struct PendingMessage *pm; | 1055 | struct PendingMessage *pm; |
1051 | 1056 | ||
1052 | GNUNET_assert (plugin != NULL); | 1057 | GNUNET_assert (NULL != plugin); |
1053 | GNUNET_assert (session != NULL); | 1058 | GNUNET_assert (NULL != session); |
1054 | 1059 | ||
1055 | /* create new message entry */ | 1060 | /* create new message entry */ |
1056 | pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size); | 1061 | pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size); |
@@ -1101,7 +1106,7 @@ tcp_plugin_send (void *cls, | |||
1101 | } | 1106 | } |
1102 | else | 1107 | else |
1103 | { | 1108 | { |
1104 | if (cont != NULL) | 1109 | if (NULL != cont) |
1105 | cont (cont_cls, &session->target, GNUNET_SYSERR); | 1110 | cont (cont_cls, &session->target, GNUNET_SYSERR); |
1106 | GNUNET_break (0); | 1111 | GNUNET_break (0); |
1107 | GNUNET_free (pm); | 1112 | GNUNET_free (pm); |
@@ -1109,16 +1114,18 @@ tcp_plugin_send (void *cls, | |||
1109 | } | 1114 | } |
1110 | } | 1115 | } |
1111 | 1116 | ||
1117 | |||
1112 | struct SessionItCtx | 1118 | struct SessionItCtx |
1113 | { | 1119 | { |
1114 | void * addr; | 1120 | void *addr; |
1115 | size_t addrlen; | 1121 | size_t addrlen; |
1116 | struct Session * result; | 1122 | struct Session *result; |
1117 | }; | 1123 | }; |
1118 | 1124 | ||
1125 | |||
1119 | static int | 1126 | static int |
1120 | session_lookup_it (void *cls, | 1127 | session_lookup_it (void *cls, |
1121 | const GNUNET_HashCode * key, | 1128 | const GNUNET_HashCode *key, |
1122 | void *value) | 1129 | void *value) |
1123 | { | 1130 | { |
1124 | struct SessionItCtx * si_ctx = cls; | 1131 | struct SessionItCtx * si_ctx = cls; |
@@ -1187,7 +1194,7 @@ nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
1187 | */ | 1194 | */ |
1188 | static struct Session * | 1195 | static struct Session * |
1189 | tcp_plugin_get_session (void *cls, | 1196 | tcp_plugin_get_session (void *cls, |
1190 | const struct GNUNET_HELLO_Address *address) | 1197 | const struct GNUNET_HELLO_Address *address) |
1191 | { | 1198 | { |
1192 | struct Plugin * plugin = cls; | 1199 | struct Plugin * plugin = cls; |
1193 | struct Session * session = NULL; | 1200 | struct Session * session = NULL; |
@@ -1393,6 +1400,7 @@ session_disconnect_it (void *cls, | |||
1393 | return GNUNET_YES; | 1400 | return GNUNET_YES; |
1394 | } | 1401 | } |
1395 | 1402 | ||
1403 | |||
1396 | /** | 1404 | /** |
1397 | * Function that can be called to force a disconnect from the | 1405 | * Function that can be called to force a disconnect from the |
1398 | * specified neighbour. This should also cancel all previously | 1406 | * specified neighbour. This should also cancel all previously |
@@ -1736,12 +1744,12 @@ handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client, | |||
1736 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1744 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1737 | "Bad address for incoming connection!\n"); | 1745 | "Bad address for incoming connection!\n"); |
1738 | GNUNET_free (vaddr); | 1746 | GNUNET_free (vaddr); |
1739 | GNUNET_SERVER_client_drop (client); | ||
1740 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | 1747 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); |
1741 | disconnect_session (session); | 1748 | disconnect_session (session); |
1742 | return; | 1749 | return; |
1743 | } | 1750 | } |
1744 | GNUNET_free (vaddr); | 1751 | GNUNET_free (vaddr); |
1752 | GNUNET_break (NULL == session->client); | ||
1745 | GNUNET_SERVER_client_keep (client); | 1753 | GNUNET_SERVER_client_keep (client); |
1746 | session->client = client; | 1754 | session->client = client; |
1747 | inc_sessions (plugin, session, __LINE__); | 1755 | inc_sessions (plugin, session, __LINE__); |