]> andersk Git - openssh.git/commitdiff
- djm@cvs.openbsd.org 2003/05/15 14:55:25
authordjm <djm>
Fri, 16 May 2003 01:39:04 +0000 (01:39 +0000)
committerdjm <djm>
Fri, 16 May 2003 01:39:04 +0000 (01:39 +0000)
     [readconf.c readconf.h ssh_config ssh_config.5 sshconnect.c]
     add a ConnectTimeout option to ssh, based on patch from
     Jean-Charles Longuet (jclonguet at free.fr); portable #207 ok markus@

ChangeLog
readconf.c
readconf.h
ssh_config
ssh_config.5
sshconnect.c

index d1fd2f28e6323c5b40e04d71d243f12b0b0e2793..e5c17156b909253b40462beee17a488d22157de0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
    - markus@cvs.openbsd.org 2003/05/15 14:09:21
      [auth2-krb5.c]
      fix 64bit issue; report itojun@
+   - djm@cvs.openbsd.org 2003/05/15 14:55:25
+     [readconf.c readconf.h ssh_config ssh_config.5 sshconnect.c]
+     add a ConnectTimeout option to ssh, based on patch from
+     Jean-Charles Longuet (jclonguet at free.fr); portable #207 ok markus@
 
 20030515
  - (djm) OpenBSD CVS Sync
index fee7a89934be8730d4ff45e950365ee0a151b851..a0cf3d6873215efbb6e6d876f21892b22e35eb91 100644 (file)
@@ -12,7 +12,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: readconf.c,v 1.110 2003/05/15 14:02:47 jakob Exp $");
+RCSID("$OpenBSD: readconf.c,v 1.111 2003/05/15 14:55:25 djm Exp $");
 
 #include "ssh.h"
 #include "xmalloc.h"
@@ -106,7 +106,7 @@ typedef enum {
        oDynamicForward, oPreferredAuthentications, oHostbasedAuthentication,
        oHostKeyAlgorithms, oBindAddress, oSmartcardDevice,
        oClearAllForwardings, oNoHostAuthenticationForLocalhost,
-       oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS,
+       oEnableSSHKeysign, oRekeyLimit, oVerifyHostKeyDNS, oConnectTimeout,
        oDeprecated, oUnsupported
 } OpCodes;
 
@@ -193,6 +193,7 @@ static struct {
 #endif
        { "nohostauthenticationforlocalhost", oNoHostAuthenticationForLocalhost },
        { "rekeylimit", oRekeyLimit },
+       { "connecttimeout", oConnectTimeout },
        { NULL, oBadOption }
 };
 
@@ -309,6 +310,20 @@ process_config_line(Options *options, const char *host,
                /* don't panic, but count bad options */
                return -1;
                /* NOTREACHED */
+       case oConnectTimeout:
+               intptr = &options->connection_timeout;
+/* parse_time: */
+               arg = strdelim(&s);
+               if (!arg || *arg == '\0')
+                       fatal("%s line %d: missing time value.",
+                           filename, linenum);
+               if ((value = convtime(arg)) == -1)
+                       fatal("%s line %d: invalid time value.",
+                           filename, linenum);
+               if (*intptr == -1)
+                       *intptr = value;
+               break;
+
        case oForwardAgent:
                intptr = &options->forward_agent;
 parse_flag:
@@ -808,6 +823,7 @@ initialize_options(Options * options)
        options->compression_level = -1;
        options->port = -1;
        options->connection_attempts = -1;
+       options->connection_timeout = -1;
        options->number_of_password_prompts = -1;
        options->cipher = -1;
        options->ciphers = NULL;
index 991e20091e57966fe4214a714618333c783dc973..c884de68b76facd334f3505a3e0f4c64c5209d16 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: readconf.h,v 1.49 2003/05/15 01:48:10 jakob Exp $     */
+/*     $OpenBSD: readconf.h,v 1.50 2003/05/15 14:55:25 djm Exp $       */
 
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -60,6 +60,8 @@ typedef struct {
        int     port;           /* Port to connect. */
        int     connection_attempts;    /* Max attempts (seconds) before
                                         * giving up */
+       int     connection_timeout;     /* Max time (seconds) before
+                                        * aborting connection attempt */
        int     number_of_password_prompts;     /* Max number of password
                                                 * prompts. */
        int     cipher;         /* Cipher to use. */
index 94cffbf395aa64ba8820e53cf415aadbc341d1d7..8a0acc17ffde0767ef638846c7d8f0d8a5fc320b 100644 (file)
@@ -1,4 +1,4 @@
-#      $OpenBSD: ssh_config,v 1.16 2002/07/03 14:21:05 markus Exp $
+#      $OpenBSD: ssh_config,v 1.17 2003/05/15 14:55:25 djm Exp $
 
 # This is the ssh client system-wide configuration file.  See
 # ssh_config(5) for more information.  This file provides defaults for
@@ -25,6 +25,7 @@
 #   HostbasedAuthentication no
 #   BatchMode no
 #   CheckHostIP yes
+#   ConnectTimeout 0
 #   StrictHostKeyChecking ask
 #   IdentityFile ~/.ssh/identity
 #   IdentityFile ~/.ssh/id_rsa
index 2f33aa3f3fdf2c17796a44103ea7ed98abbef726..c5de4a9de9284982fbcfbd1c30772302288d5d21 100644 (file)
@@ -34,7 +34,7 @@
 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.\" $OpenBSD: ssh_config.5,v 1.8 2003/05/14 18:16:20 jakob Exp $
+.\" $OpenBSD: ssh_config.5,v 1.9 2003/05/15 14:55:25 djm Exp $
 .Dd September 25, 1999
 .Dt SSH_CONFIG 5
 .Os
@@ -227,6 +227,11 @@ Specifies the number of tries (one per second) to make before exiting.
 The argument must be an integer.
 This may be useful in scripts if the connection sometimes fails.
 The default is 1.
+.It Cm ConnectTimeout
+Specifies the timeout (in seconds) used when connecting to the ssh
+server, instead of using the default system TCP timeout. This value is 
+used only when the target is down or really unreachable, not when it
+refuses the connection.
 .It Cm DynamicForward
 Specifies that a TCP/IP port on the local machine be forwarded
 over the secure channel, and the application
index 32bef7d079c197166c3faa633267b39ee91d2ff8..8aac221d33cb9dbca646acf716b398e70ddbc912 100644 (file)
@@ -13,7 +13,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: sshconnect.c,v 1.140 2003/05/14 18:16:21 jakob Exp $");
+RCSID("$OpenBSD: sshconnect.c,v 1.141 2003/05/15 14:55:25 djm Exp $");
 
 #include <openssl/bn.h>
 
@@ -218,6 +218,71 @@ ssh_create_socket(int privileged, struct addrinfo *ai)
        return sock;
 }
 
+static int
+timeout_connect(int sockfd, const struct sockaddr *serv_addr,
+    socklen_t addrlen, int timeout)
+{
+       fd_set *fdset;
+       struct timeval tv;
+       socklen_t optlen;
+       int fdsetsz, optval, rc;
+
+       if (timeout <= 0)
+               return (connect(sockfd, serv_addr, addrlen));
+
+       if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
+               return (-1);
+
+       rc = connect(sockfd, serv_addr, addrlen);
+       if (rc == 0)
+               return (0);
+       if (errno != EINPROGRESS)
+               return (-1);
+
+       fdsetsz = howmany(sockfd + 1, NFDBITS) * sizeof(fd_mask);
+       fdset = (fd_set *)xmalloc(fdsetsz);
+
+       memset(fdset, '\0', fdsetsz);
+       FD_SET(sockfd, fdset);
+       tv.tv_sec = timeout;
+       tv.tv_usec = 0;
+
+       for(;;) {
+               rc = select(sockfd + 1, NULL, fdset, NULL, &tv);
+               if (rc != -1 || errno != EINTR)
+                       break;
+       }
+
+       switch(rc) {
+       case 0:
+               /* Timed out */
+               errno = ETIMEDOUT;
+               return (-1);
+       case -1:
+               /* Select error */
+               debug("select: %s", strerror(errno));
+               return (-1);
+       case 1:
+               /* Completed or failed */
+               optval = 0;
+               optlen = sizeof(optval);
+               if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &optval, 
+                   &optlen) == -1)
+                       debug("getsockopt: %s", strerror(errno));
+                       return (-1);
+               if (optval != 0) {
+                       errno = optval;
+                       return (-1);
+               }
+               break;
+       default:
+               /* Should not occur */
+               fatal("Bogus return (%d) from select()", rc);
+       }
+
+       return (0);
+}
+
 /*
  * Opens a TCP/IP connection to the remote server on the given host.
  * The address of the remote host will be returned in hostaddr.
@@ -306,7 +371,8 @@ ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
                                /* Any error is already output */
                                continue;
 
-                       if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) {
+                       if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,
+                           options.connection_timeout) >= 0) {
                                /* Successful connection. */
                                memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
                                break;
This page took 0.285196 seconds and 5 git commands to generate.