aboutsummaryrefslogtreecommitdiff
path: root/libssh
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2005-12-06 18:10:30 +0000
committerAris Adamantiadis <aris@0xbadc0de.be>2005-12-06 18:10:30 +0000
commit4fd1df0586ed08911467141e605afa0c0c610e6a (patch)
treea0c37057fb22d1398f17a686ec1c58e84e4ab51d /libssh
parent3edfd105b37c71f10816f32022e6baf54625d9c0 (diff)
downloadlibssh-4fd1df0586ed08911467141e605afa0c0c610e6a.tar.gz
libssh-4fd1df0586ed08911467141e605afa0c0c610e6a.tar.xz
libssh-4fd1df0586ed08911467141e605afa0c0c610e6a.zip
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@53 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'libssh')
-rw-r--r--libssh/connect.c74
1 files changed, 40 insertions, 34 deletions
diff --git a/libssh/connect.c b/libssh/connect.c
index afe1dadf..73e956a6 100644
--- a/libssh/connect.c
+++ b/libssh/connect.c
@@ -63,6 +63,45 @@ static int getai(const char *host, int port, struct addrinfo **ai)
return getaddrinfo(host,service,&hints,ai);
}
+int ssh_connect_ai_timeout(SSH_SESSION *session, char *host, int port, struct addrinfo *ai,
+ long timeout, long usec,int s)
+{
+ struct timeval to;
+ fd_set set;
+ int ret=0;
+ unsigned int len=sizeof(ret);
+ to.tv_sec=timeout;
+ to.tv_usec=usec;
+ sock_set_nonblocking(s);
+ connect(s,ai->ai_addr,ai->ai_addrlen);
+ freeaddrinfo(ai);
+ FD_ZERO(&set);
+ FD_SET(s,&set);
+ ret=select(s+1,NULL,&set,NULL,&to);
+ if(ret==0){
+ /* timeout */
+ ssh_set_error(session,SSH_FATAL,"Timeout while connecting to %s:%d",host,port);
+ close(s);
+ return -1;
+ }
+ if(ret<0){
+ ssh_set_error(session,SSH_FATAL,"Select error : %s",strerror(errno));
+ close(s);
+ return -1;
+ }
+ /* get connect(2) return code. zero means no error */
+ getsockopt(s,SOL_SOCKET,SO_ERROR,&ret,&len);
+ if (ret!=0){
+ ssh_set_error(session,SSH_FATAL,"Connecting : %s",strerror(ret));
+ close(s);
+ return -1;
+ }
+ /* s is connected ? */
+ ssh_say(3,"socket connected with timeout\n");
+ sock_set_blocking(s);
+ return s;
+}
+
/* connect_host connects to an IPv4 (or IPv6) host */
/* specified by its IP address or hostname. */
/* output is the file descriptor, <0 if failed. */
@@ -108,40 +147,7 @@ int ssh_connect_host(SSH_SESSION *session, const char *host, const char
freeaddrinfo(bind_ai);
}
if(timeout){
- struct timeval to;
- fd_set set;
- int ret=0;
- unsigned int len=sizeof(ret);
- to.tv_sec=timeout;
- to.tv_usec=usec;
- sock_set_nonblocking(s);
- connect(s,ai->ai_addr,ai->ai_addrlen);
- freeaddrinfo(ai);
- FD_ZERO(&set);
- FD_SET(s,&set);
- ret=select(s+1,NULL,&set,NULL,&to);
- if(ret==0){
- /* timeout */
- ssh_set_error(session,SSH_FATAL,"Timeout while connecting to %s:%d",host,port);
- close(s);
- return -1;
- }
- if(ret<0){
- ssh_set_error(session,SSH_FATAL,"Select error : %s",strerror(errno));
- close(s);
- return -1;
- }
- /* get connect(2) return code. zero means no error */
- getsockopt(s,SOL_SOCKET,SO_ERROR,&ret,&len);
- if (ret!=0){
- ssh_set_error(session,SSH_FATAL,"Connecting : %s",strerror(ret));
- close(s);
- return -1;
- }
- /* s is connected ? */
- ssh_say(3,"socket connected with timeout\n");
- sock_set_blocking(s);
- return s;
+ return ssh_connect_ai_timeout(session,host,port,ai,timeout,usec,s);
}
if(connect(s,ai->ai_addr,ai->ai_addrlen)<0){
ssh_set_error(session,SSH_FATAL,"connect: %s",strerror(errno));