From 52b496dfb5bcb072c1b5b8a1603be7008dc4e905 Mon Sep 17 00:00:00 2001 From: t3sserakt Date: Mon, 4 May 2020 08:29:03 +0200 Subject: [PATCH] fixed bugs --- src/transport/gnunet-communicator-tcp.c | 138 ++++++++++++++++-------- src/transport/transport-testing2.c | 17 ++- 2 files changed, 107 insertions(+), 48 deletions(-) diff --git a/src/transport/gnunet-communicator-tcp.c b/src/transport/gnunet-communicator-tcp.c index 3a830b7db..496ebc950 100644 --- a/src/transport/gnunet-communicator-tcp.c +++ b/src/transport/gnunet-communicator-tcp.c @@ -1151,7 +1151,7 @@ tcp_address_to_sockaddr_port_only (const char *bindto, unsigned int *port, sockl } static char * -extract_address (char *bindto) +extract_address (const char *bindto) { char *start; @@ -1181,13 +1181,13 @@ extract_address (char *bindto) } } - GNUNET_free(cp); + //GNUNET_free(cp); return start; } static unsigned int -extract_port(char *addr_and_port) +extract_port(const char *addr_and_port) { unsigned int port; char dummy[2]; @@ -1200,7 +1200,6 @@ extract_port(char *addr_and_port) { cp = GNUNET_strdup(addr_and_port); token = strtok(cp, "]"); - if (strlen(addr_and_port) == strlen(token)) { colon = strrchr (cp, ':'); @@ -1208,7 +1207,6 @@ extract_port(char *addr_and_port) { return 0; } - printf("%s\n", colon); addr = colon; addr++; } @@ -1226,6 +1224,7 @@ extract_port(char *addr_and_port) } } + if (1 == sscanf (addr, "%u%1s", &port, dummy)) { /* interpreting value as just a PORT number */ @@ -1254,6 +1253,7 @@ extract_port(char *addr_and_port) port = 0; } + return port; } @@ -1268,7 +1268,7 @@ tcp_address_to_sockaddr_numeric_v6 (socklen_t *sock_len, struct sockaddr_in6 v6, v6.sin6_len = sizeof(sizeof(struct sockaddr_in6)); #endif in = GNUNET_memdup (&v6, sizeof(v6)); - *sock_len = sizeof(v6); + *sock_len = sizeof(struct sockaddr_in6); return in; } @@ -1283,7 +1283,7 @@ tcp_address_to_sockaddr_numeric_v4 (socklen_t *sock_len, struct sockaddr_in v4, v4.sin_len = sizeof(struct sockaddr_in); #endif in = GNUNET_memdup (&v4, sizeof(v4)); - *sock_len = sizeof(v4); + *sock_len = sizeof(struct sockaddr_in); return in; } @@ -1300,14 +1300,12 @@ tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len) struct sockaddr *in; unsigned int port; char dummy[2]; - char *cp; struct sockaddr_in v4; struct sockaddr_in6 v6; const char *start; - socklen_t in_len; - cp = GNUNET_strdup (bindto); - start = extract_address (cp); + //cp = GNUNET_strdup (bindto); + start = extract_address (bindto); if (1 == sscanf (bindto, "%u%1s", &port, dummy)) { @@ -1318,14 +1316,14 @@ tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len) else if (1 == inet_pton (AF_INET, start, &v4.sin_addr)) { //colon = strrchr (cp, ':'); - port = extract_port(cp); - in = tcp_address_to_sockaddr_numeric_v4 (&in_len, v4, port); + port = extract_port(bindto); + in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port); } else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr)) { //colon = strrchr (cp, ':'); - port = extract_port(cp); - in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, port); + port = extract_port(bindto); + in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port); } return in; } @@ -2007,6 +2005,8 @@ mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address) socklen_t in_len; struct GNUNET_NETWORK_Handle *sock; + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Connecting to %s\n", address); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to %s\n", address); if (0 != strncmp (address, @@ -2026,7 +2026,6 @@ mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address) return GNUNET_SYSERR; } - sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP); if (NULL == sock) { @@ -2215,31 +2214,23 @@ nat_address_cb (void *cls, } static void -init_own_socket (void *cls, +init_socket (void *cls, const struct sockaddr *addr, socklen_t in_len) { - - //struct sockaddr *in; struct sockaddr_storage in_sto; socklen_t sto_len; - /*struct sockaddr_in *v4; - struct sockaddr_in6 *v6;*/ - - unsigned int *port = &port_global; - - char out_string [128]; - snprintf(out_string, 128, "%u", *port); if (NULL == addr) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Failed to setup TCP socket address %s on port %s\n", - GNUNET_a2s (addr, in_len), - out_string); + "Address is NULL.\n"); return; } + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "address %s\n", + GNUNET_a2s (addr, in_len)); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s\n", GNUNET_a2s (addr, in_len)); @@ -2258,9 +2249,9 @@ init_own_socket (void *cls, GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind"); GNUNET_NETWORK_socket_close (listen_sock); listen_sock = NULL; - GNUNET_free (addr); return; } + if (GNUNET_OK != GNUNET_NETWORK_socket_listen (listen_sock, 5)) @@ -2269,11 +2260,12 @@ init_own_socket (void *cls, "listen"); GNUNET_NETWORK_socket_close (listen_sock); listen_sock = NULL; - GNUNET_free (addr); } + /* We might have bound to port 0, allowing the OS to figure it out; thus, get the real IN-address from the socket */ sto_len = sizeof(in_sto); + if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock), (struct sockaddr *) &in_sto, &sto_len)) @@ -2281,8 +2273,7 @@ init_own_socket (void *cls, memcpy (&in_sto, addr, in_len); sto_len = in_len; } - GNUNET_free (addr); - //GNUNET_free (bindto); + addr = (struct sockaddr *) &in_sto; in_len = sto_len; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, @@ -2336,9 +2327,64 @@ init_own_socket (void *cls, &nat_address_cb, NULL /* FIXME: support reversal: #5529 */, NULL /* closure */); + + if (NULL == nat) + { + GNUNET_break (0); + GNUNET_RESOLVER_request_cancel (resolve_request_handle); + GNUNET_SCHEDULER_shutdown (); + return; + } + //TODO Remove this, if we like to handle more then one address. if (NULL != resolve_request_handle) GNUNET_RESOLVER_request_cancel (resolve_request_handle); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "init_own finished %s\n", + GNUNET_a2s (addr, in_len)); +} + + +static void +init_socket_resolv (void *cls, + const struct sockaddr *addr, + socklen_t in_len) +{ + struct sockaddr_in *v4; + struct sockaddr_in6 *v6; + + if (NULL != addr) + { + if (AF_INET == addr->sa_family) + { + v4 = (struct sockaddr_in *) addr; + v4->sin_port = htons ((uint16_t) port_global); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "ipv4 \n"); + }else if (AF_INET6 == addr->sa_family) + { + v6 = (struct sockaddr_in6 *) addr; + v6->sin6_port = htons ((uint16_t) port_global); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "ipv6 \n"); + }else + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Address family %u not suitable (not AF_INET %u nor AF_INET6 %u \n", + addr->sa_family, + AF_INET, + AF_INET6); + return; + } + }else + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "Address is NULL. This might be an error or the resolver finished resolving.\n"); + } + + init_socket (cls, + addr, + in_len); } /** @@ -2360,7 +2406,6 @@ run (void *cls, socklen_t in_len; struct sockaddr_in v4; struct sockaddr_in6 v6; - char *cp; char *start; unsigned int port; char dummy[2]; @@ -2392,39 +2437,40 @@ run (void *cls, &rekey_interval)) rekey_interval = DEFAULT_REKEY_INTERVAL; - cp = GNUNET_strdup (bindto); - start = extract_address (cp); + //cp = GNUNET_strdup (bindto); + start = extract_address (bindto); if (1 == sscanf (bindto, "%u%1s", &port, dummy)) { in = tcp_address_to_sockaddr_port_only (bindto, &port, &in_len); port_global = port; - init_own_socket (&port, in, in_len); + init_socket (&port, in, in_len); } else if (1 == inet_pton (AF_INET, start, &v4.sin_addr)) { - port = extract_port(cp); + port = extract_port(bindto); port_global = port; in = tcp_address_to_sockaddr_numeric_v4 (&in_len, v4, port); - init_own_socket (&port, in, in_len); + init_socket (&port, in, in_len); } else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr)) { - port = extract_port(cp); + port = extract_port(bindto); port_global = port; in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, port); - init_own_socket (&port, in, in_len); + init_socket (&port, in, in_len); }else { - port_global = extract_port(cp); - resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok(cp, ":"), + port = extract_port(bindto); + port_global = port; + resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok(bindto, ":"), AF_UNSPEC, GNUNET_TIME_UNIT_MINUTES, - &init_own_socket, - &port); + &init_socket_resolv, + NULL); } GNUNET_free (bindto); - GNUNET_free (cp); + //GNUNET_free (cp); } diff --git a/src/transport/transport-testing2.c b/src/transport/transport-testing2.c index bccbcb732..22ec0a42f 100644 --- a/src/transport/transport-testing2.c +++ b/src/transport/transport-testing2.c @@ -824,11 +824,11 @@ shutdown_process (struct GNUNET_OS_Process *proc) if (0 != GNUNET_OS_process_kill (proc, SIGTERM)) { LOG (GNUNET_ERROR_TYPE_WARNING, - "Error shutting down communicator with SIGERM, trying SIGKILL\n"); + "Error shutting down process with SIGERM, trying SIGKILL\n"); if (0 != GNUNET_OS_process_kill (proc, SIGKILL)) { LOG (GNUNET_ERROR_TYPE_ERROR, - "Error shutting down communicator with SIGERM and SIGKILL\n"); + "Error shutting down process with SIGERM and SIGKILL\n"); } } GNUNET_OS_process_destroy (proc); @@ -889,6 +889,18 @@ shutdown_nat (void *cls) shutdown_process (proc); } +/** + * @brief Task run at shutdown to kill the resolver process + * + * @param cls Closure - Process of communicator + */ +static void +shutdown_resolver (void *cls) +{ + struct GNUNET_OS_Process *proc = cls; + shutdown_process (proc); +} + static void resolver_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h) { @@ -1024,6 +1036,7 @@ GNUNET_TRANSPORT_TESTING_transport_communicator_service_stop ( shutdown_communicator (tc_h->c_proc); shutdown_service (tc_h->sh); shutdown_nat (tc_h->nat_proc); + shutdown_resolver (tc_h->resolver_proc); GNUNET_CONFIGURATION_destroy (tc_h->cfg); GNUNET_free (tc_h); } -- 2.25.1