Resolve true IP address of socket connection for FTP listener.
authorPekka Riikonen <priikone@silcnet.org>
Sun, 26 Oct 2003 18:20:42 +0000 (18:20 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 26 Oct 2003 18:20:42 +0000 (18:20 +0000)
CHANGES
lib/silcclient/client_ftp.c
lib/silcutil/silcnet.c

diff --git a/CHANGES b/CHANGES
index 0435ea881a01d315a362d5b8840a470f2132453d..89bd16d8ec684fb74d5c6cd5f680e55a06004244 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,12 @@
+Sun Oct 26 21:19:02 EET 2003 Pekka Riikonen <priikone@silcnet.org>
+
+       * 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 <jochen@penguin-breeder.org>
 
        * Fixed race condition between silc_client_init and 
index ef727a8584d128e6a2946914bf864647deaea487..1ce4e392f1a003301421c87678a9a0871cddd3b3 100644 (file)
@@ -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));
index c25637c08021c4966430656984ec6ece6ea6406f..d083656eba63e379357d64acdc60848ec9aad701 100644 (file)
@@ -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;