mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-11-29 01:03:57 +03:00
socket: Check if socket (non)blocking is working.
This commit is contained in:
@@ -55,8 +55,8 @@ int ssh_socket_get_status(ssh_socket s);
|
||||
int ssh_socket_buffered_write_bytes(ssh_socket s);
|
||||
int ssh_socket_data_available(ssh_socket s);
|
||||
int ssh_socket_data_writable(ssh_socket s);
|
||||
void ssh_socket_set_nonblocking(socket_t fd);
|
||||
void ssh_socket_set_blocking(socket_t fd);
|
||||
int ssh_socket_set_nonblocking(socket_t fd);
|
||||
int ssh_socket_set_blocking(socket_t fd);
|
||||
|
||||
void ssh_socket_set_callbacks(ssh_socket s, ssh_socket_callbacks callbacks);
|
||||
int ssh_socket_pollcallback(struct ssh_poll_handle_struct *p, socket_t fd, int revents, void *v_s);
|
||||
|
||||
@@ -149,7 +149,13 @@ static int ssh_connect_ai_timeout(ssh_session session, const char *host,
|
||||
*/
|
||||
timeout_ms=timeout * 1000 + usec / 1000;
|
||||
|
||||
ssh_socket_set_nonblocking(s);
|
||||
rc = ssh_socket_set_nonblocking(s);
|
||||
if (rc < 0) {
|
||||
ssh_set_error(session, SSH_FATAL,
|
||||
"Failed to set socket non-blocking for %s:%d", host, port);
|
||||
ssh_connect_socket_close(s);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssh_log(session, SSH_LOG_RARE, "Trying to connect to host: %s:%d with "
|
||||
"timeout %d ms", host, port, timeout_ms);
|
||||
@@ -196,7 +202,14 @@ static int ssh_connect_ai_timeout(ssh_session session, const char *host,
|
||||
|
||||
/* s is connected ? */
|
||||
ssh_log(session, SSH_LOG_PACKET, "Socket connected with timeout\n");
|
||||
ssh_socket_set_blocking(s);
|
||||
ret = ssh_socket_set_blocking(s);
|
||||
if (ret < 0) {
|
||||
ssh_set_error(session, SSH_FATAL,
|
||||
"Failed to set socket as blocking connecting to %s:%d failed: %s",
|
||||
host, port, strerror(errno));
|
||||
ssh_connect_socket_close(s);
|
||||
return -1;
|
||||
}
|
||||
|
||||
leave_function();
|
||||
return s;
|
||||
@@ -368,7 +381,13 @@ socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
|
||||
continue;
|
||||
}
|
||||
}
|
||||
ssh_socket_set_nonblocking(s);
|
||||
|
||||
rc = ssh_socket_set_nonblocking(s);
|
||||
if (rc < 0) {
|
||||
ssh_set_error(session, SSH_FATAL,
|
||||
"Failed to set socket non-blocking for %s:%d", host, port);
|
||||
ssh_connect_socket_close(s);
|
||||
}
|
||||
|
||||
connect(s, itr->ai_addr, itr->ai_addrlen);
|
||||
break;
|
||||
|
||||
21
src/socket.c
21
src/socket.c
@@ -308,7 +308,10 @@ int ssh_socket_pollcallback(struct ssh_poll_handle_struct *p, socket_t fd, int r
|
||||
ssh_log(s->session,SSH_LOG_PACKET,"Received POLLOUT in connecting state");
|
||||
s->state = SSH_SOCKET_CONNECTED;
|
||||
ssh_poll_set_events(p,POLLOUT | POLLIN);
|
||||
ssh_socket_set_blocking(ssh_socket_get_fd_in(s));
|
||||
r = ssh_socket_set_blocking(ssh_socket_get_fd_in(s));
|
||||
if (r < 0) {
|
||||
return -1;
|
||||
}
|
||||
if(s->callbacks && s->callbacks->connected)
|
||||
s->callbacks->connected(SSH_SOCKET_CONNECTED_OK,0,s->callbacks->userdata);
|
||||
return 0;
|
||||
@@ -714,23 +717,23 @@ int ssh_socket_get_status(ssh_socket s) {
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
void ssh_socket_set_nonblocking(socket_t fd) {
|
||||
int ssh_socket_set_nonblocking(socket_t fd) {
|
||||
u_long nonblocking = 1;
|
||||
ioctlsocket(fd, FIONBIO, &nonblocking);
|
||||
return ioctlsocket(fd, FIONBIO, &nonblocking);
|
||||
}
|
||||
|
||||
void ssh_socket_set_blocking(socket_t fd) {
|
||||
int ssh_socket_set_blocking(socket_t fd) {
|
||||
u_long nonblocking = 0;
|
||||
ioctlsocket(fd, FIONBIO, &nonblocking);
|
||||
return ioctlsocket(fd, FIONBIO, &nonblocking);
|
||||
}
|
||||
|
||||
#else /* _WIN32 */
|
||||
void ssh_socket_set_nonblocking(socket_t fd) {
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
int ssh_socket_set_nonblocking(socket_t fd) {
|
||||
return fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
|
||||
void ssh_socket_set_blocking(socket_t fd) {
|
||||
fcntl(fd, F_SETFL, 0);
|
||||
int ssh_socket_set_blocking(socket_t fd) {
|
||||
return fcntl(fd, F_SETFL, 0);
|
||||
}
|
||||
#endif /* _WIN32 */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user