From: Pekka Riikonen Date: Sun, 26 Oct 2003 18:20:42 +0000 (+0000) Subject: Resolve true IP address of socket connection for FTP listener. X-Git-Tag: silc.client.0.9.15~16 X-Git-Url: http://git.silcnet.org/gitweb/?a=commitdiff_plain;h=3e7ffc3179932f9d22d2d8dfb8be1fda962a832e;p=silc.git Resolve true IP address of socket connection for FTP listener. --- diff --git a/CHANGES b/CHANGES index 0435ea88..89bd16d8 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,12 @@ +Sun Oct 26 21:19:02 EET 2003 Pekka Riikonen + + * Resolve the IP for file transfer listener from the connection + socket. Affected file lib/silcclient/client_ftp.c. + + * Accept NULL hostname parameter in the functions + silc_net_check_[local|remote]_by_socket. Do not reverse lookup + if hostname is not wanted. + Fri Oct 24 21:24:12 CEST 2003 Jochen Eisinger * Fixed race condition between silc_client_init and diff --git a/lib/silcclient/client_ftp.c b/lib/silcclient/client_ftp.c index ef727a85..1ce4e392 100644 --- a/lib/silcclient/client_ftp.c +++ b/lib/silcclient/client_ftp.c @@ -850,17 +850,22 @@ silc_client_file_send(SilcClient client, /* Create the listener for incoming key exchange protocol. */ if (!do_not_bind) { + session->listener = -1; if (local_ip) session->hostname = strdup(local_ip); else - session->hostname = silc_net_localip(); - session->listener = silc_net_create_server(local_port, session->hostname); + silc_net_check_local_by_sock(conn->sock->sock, NULL, + &session->hostname); + if (session->hostname) + session->listener = silc_net_create_server(local_port, + session->hostname); if (session->listener < 0) { /* Could not create listener. Do the second best thing; send empty key agreement packet and let the remote client provide the point for the key exchange. */ SILC_LOG_DEBUG(("Could not create listener")); silc_free(session->hostname); + session->listener = 0; session->hostname = NULL; session->port = 0; } else { @@ -957,10 +962,13 @@ silc_client_file_receive(SilcClient client, } else { /* Add the listener for the key agreement */ SILC_LOG_DEBUG(("Creating listener for file transfer")); - session->hostname = silc_net_localip(); - session->listener = silc_net_create_server(0, session->hostname); + session->listener = -1; + silc_net_check_local_by_sock(conn->sock->sock, NULL, &session->hostname); + if (session->hostname) + session->listener = silc_net_create_server(0, session->hostname); if (session->listener < 0) { SILC_LOG_DEBUG(("Could not create listener")); + session->listener = 0; client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR, "Cannot create listener on %s: %s", session->hostname, strerror(errno)); diff --git a/lib/silcutil/silcnet.c b/lib/silcutil/silcnet.c index c25637c0..d083656e 100644 --- a/lib/silcutil/silcnet.c +++ b/lib/silcutil/silcnet.c @@ -292,7 +292,8 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) struct sockaddr_storage remote; char s[NI_MAXHOST]; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving remote hostname and IP address")); @@ -315,7 +316,8 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) struct sockaddr_in remote; char *host_ip; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving remote hostname and IP address")); @@ -335,19 +337,22 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) return FALSE; #endif - /* Get host by address */ - if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) - return FALSE; + /* Do reverse lookup if we want hostname too. */ + if (hostname) { + /* Get host by address */ + if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) + return FALSE; - *hostname = silc_memdup(host, strlen(host)); - SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); + *hostname = silc_memdup(host, strlen(host)); + SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); - /* Reverse */ - if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) - return FALSE; + /* Reverse */ + if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) + return FALSE; - if (strcmp(*ip, host)) - return FALSE; + if (strcmp(*ip, host)) + return FALSE; + } SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); return TRUE; @@ -365,7 +370,8 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) struct sockaddr_storage local; char s[NI_MAXHOST]; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving local hostname and IP address")); @@ -388,7 +394,8 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) struct sockaddr_in local; char *host_ip; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving local hostname and IP address")); @@ -408,19 +415,22 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) return FALSE; #endif - /* Get host by address */ - if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) - return FALSE; + /* Do reverse lookup if we want hostname too. */ + if (hostname) { + /* Get host by address */ + if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) + return FALSE; - *hostname = silc_memdup(host, strlen(host)); - SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); + *hostname = silc_memdup(host, strlen(host)); + SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname)); - /* Reverse */ - if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) - return FALSE; + /* Reverse */ + if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) + return FALSE; - if (strcmp(*ip, host)) - return FALSE; + if (strcmp(*ip, host)) + return FALSE; + } SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); return TRUE;