mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-09 18:04:25 +09:00
client: Reformat ssh_connect()
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
committed by
Andreas Schneider
parent
dba2114ed7
commit
0f33eecc01
216
src/client.c
216
src/client.c
@@ -505,119 +505,131 @@ static int ssh_connect_termination(void *user){
|
|||||||
* @see ssh_new()
|
* @see ssh_new()
|
||||||
* @see ssh_disconnect()
|
* @see ssh_disconnect()
|
||||||
*/
|
*/
|
||||||
int ssh_connect(ssh_session session) {
|
int ssh_connect(ssh_session session)
|
||||||
int ret;
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
if (session == NULL) {
|
if (session == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
|
||||||
|
|
||||||
switch(session->pending_call_state){
|
|
||||||
case SSH_PENDING_CALL_NONE:
|
|
||||||
break;
|
|
||||||
case SSH_PENDING_CALL_CONNECT:
|
|
||||||
goto pending;
|
|
||||||
default:
|
|
||||||
ssh_set_error(session,SSH_FATAL,"Bad call during pending SSH call in ssh_connect");
|
|
||||||
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
session->alive = 0;
|
|
||||||
session->client = 1;
|
|
||||||
|
|
||||||
if (session->opts.fd == SSH_INVALID_SOCKET &&
|
|
||||||
session->opts.host == NULL &&
|
|
||||||
session->opts.ProxyCommand == NULL) {
|
|
||||||
ssh_set_error(session, SSH_FATAL, "Hostname required");
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If the system configuration files were not yet processed, do it now */
|
|
||||||
if (!session->opts.config_processed) {
|
|
||||||
ret = ssh_options_parse_config(session, NULL);
|
|
||||||
if (ret != 0) {
|
|
||||||
ssh_set_error(session, SSH_FATAL,
|
|
||||||
"Failed to process system configuration files");
|
|
||||||
return SSH_ERROR;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ret = ssh_options_apply(session);
|
switch(session->pending_call_state) {
|
||||||
if (ret < 0) {
|
case SSH_PENDING_CALL_NONE:
|
||||||
ssh_set_error(session, SSH_FATAL, "Couldn't apply options");
|
break;
|
||||||
return SSH_ERROR;
|
case SSH_PENDING_CALL_CONNECT:
|
||||||
}
|
goto pending;
|
||||||
|
default:
|
||||||
|
ssh_set_error(session, SSH_FATAL,
|
||||||
|
"Bad call during pending SSH call in ssh_connect");
|
||||||
|
|
||||||
SSH_LOG(SSH_LOG_PROTOCOL,
|
return SSH_ERROR;
|
||||||
"libssh %s, using threading %s",
|
}
|
||||||
ssh_copyright(),
|
session->alive = 0;
|
||||||
ssh_threads_get_type());
|
session->client = 1;
|
||||||
|
|
||||||
session->ssh_connection_callback = ssh_client_connection_callback;
|
if (session->opts.fd == SSH_INVALID_SOCKET &&
|
||||||
session->session_state=SSH_SESSION_STATE_CONNECTING;
|
session->opts.host == NULL &&
|
||||||
ssh_socket_set_callbacks(session->socket,&session->socket_callbacks);
|
session->opts.ProxyCommand == NULL)
|
||||||
session->socket_callbacks.connected=socket_callback_connected;
|
{
|
||||||
session->socket_callbacks.data=callback_receive_banner;
|
ssh_set_error(session, SSH_FATAL, "Hostname required");
|
||||||
session->socket_callbacks.exception=ssh_socket_exception_callback;
|
return SSH_ERROR;
|
||||||
session->socket_callbacks.userdata=session;
|
}
|
||||||
if (session->opts.fd != SSH_INVALID_SOCKET) {
|
|
||||||
session->session_state=SSH_SESSION_STATE_SOCKET_CONNECTED;
|
/* If the system configuration files were not yet processed, do it now */
|
||||||
ssh_socket_set_fd(session->socket, session->opts.fd);
|
if (!session->opts.config_processed) {
|
||||||
ret=SSH_OK;
|
ret = ssh_options_parse_config(session, NULL);
|
||||||
|
if (ret != 0) {
|
||||||
|
ssh_set_error(session, SSH_FATAL,
|
||||||
|
"Failed to process system configuration files");
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = ssh_options_apply(session);
|
||||||
|
if (ret < 0) {
|
||||||
|
ssh_set_error(session, SSH_FATAL, "Couldn't apply options");
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
SSH_LOG(SSH_LOG_PROTOCOL,
|
||||||
|
"libssh %s, using threading %s",
|
||||||
|
ssh_copyright(),
|
||||||
|
ssh_threads_get_type());
|
||||||
|
|
||||||
|
session->ssh_connection_callback = ssh_client_connection_callback;
|
||||||
|
session->session_state = SSH_SESSION_STATE_CONNECTING;
|
||||||
|
ssh_socket_set_callbacks(session->socket, &session->socket_callbacks);
|
||||||
|
session->socket_callbacks.connected = socket_callback_connected;
|
||||||
|
session->socket_callbacks.data = callback_receive_banner;
|
||||||
|
session->socket_callbacks.exception = ssh_socket_exception_callback;
|
||||||
|
session->socket_callbacks.userdata = session;
|
||||||
|
|
||||||
|
if (session->opts.fd != SSH_INVALID_SOCKET) {
|
||||||
|
session->session_state = SSH_SESSION_STATE_SOCKET_CONNECTED;
|
||||||
|
ssh_socket_set_fd(session->socket, session->opts.fd);
|
||||||
|
ret = SSH_OK;
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
} else if (session->opts.ProxyCommand != NULL){
|
} else if (session->opts.ProxyCommand != NULL) {
|
||||||
ret = ssh_socket_connect_proxycommand(session->socket,
|
ret = ssh_socket_connect_proxycommand(session->socket,
|
||||||
session->opts.ProxyCommand);
|
session->opts.ProxyCommand);
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
ret=ssh_socket_connect(session->socket,
|
ret = ssh_socket_connect(session->socket,
|
||||||
session->opts.host,
|
session->opts.host,
|
||||||
session->opts.port > 0 ? session->opts.port : 22,
|
session->opts.port > 0 ? session->opts.port : 22,
|
||||||
session->opts.bindaddr);
|
session->opts.bindaddr);
|
||||||
}
|
}
|
||||||
if (ret == SSH_ERROR) {
|
if (ret == SSH_ERROR) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
set_status(session, 0.2f);
|
set_status(session, 0.2f);
|
||||||
|
|
||||||
|
session->alive = 1;
|
||||||
|
SSH_LOG(SSH_LOG_PROTOCOL,
|
||||||
|
"Socket connecting, now waiting for the callbacks to work");
|
||||||
|
|
||||||
session->alive = 1;
|
|
||||||
SSH_LOG(SSH_LOG_PROTOCOL,"Socket connecting, now waiting for the callbacks to work");
|
|
||||||
pending:
|
pending:
|
||||||
session->pending_call_state=SSH_PENDING_CALL_CONNECT;
|
session->pending_call_state = SSH_PENDING_CALL_CONNECT;
|
||||||
if(ssh_is_blocking(session)) {
|
if(ssh_is_blocking(session)) {
|
||||||
int timeout = (session->opts.timeout * 1000) +
|
int timeout = (session->opts.timeout * 1000) +
|
||||||
(session->opts.timeout_usec / 1000);
|
(session->opts.timeout_usec / 1000);
|
||||||
if (timeout == 0) {
|
if (timeout == 0) {
|
||||||
timeout = 10 * 1000;
|
timeout = 10 * 1000;
|
||||||
}
|
}
|
||||||
SSH_LOG(SSH_LOG_PACKET,"Actual timeout : %d", timeout);
|
SSH_LOG(SSH_LOG_PACKET, "Actual timeout : %d", timeout);
|
||||||
ret = ssh_handle_packets_termination(session, timeout, ssh_connect_termination, session);
|
ret = ssh_handle_packets_termination(session, timeout,
|
||||||
if (session->session_state != SSH_SESSION_STATE_ERROR &&
|
ssh_connect_termination, session);
|
||||||
(ret == SSH_ERROR || !ssh_connect_termination(session))) {
|
if (session->session_state != SSH_SESSION_STATE_ERROR &&
|
||||||
ssh_set_error(session, SSH_FATAL,
|
(ret == SSH_ERROR || !ssh_connect_termination(session)))
|
||||||
"Timeout connecting to %s", session->opts.host);
|
{
|
||||||
session->session_state = SSH_SESSION_STATE_ERROR;
|
ssh_set_error(session, SSH_FATAL,
|
||||||
}
|
"Timeout connecting to %s", session->opts.host);
|
||||||
}
|
session->session_state = SSH_SESSION_STATE_ERROR;
|
||||||
else {
|
}
|
||||||
ret = ssh_handle_packets_termination(session,
|
} else {
|
||||||
SSH_TIMEOUT_NONBLOCKING,
|
ret = ssh_handle_packets_termination(session,
|
||||||
ssh_connect_termination,
|
SSH_TIMEOUT_NONBLOCKING,
|
||||||
session);
|
ssh_connect_termination,
|
||||||
if (ret == SSH_ERROR) {
|
session);
|
||||||
session->session_state = SSH_SESSION_STATE_ERROR;
|
if (ret == SSH_ERROR) {
|
||||||
}
|
session->session_state = SSH_SESSION_STATE_ERROR;
|
||||||
}
|
}
|
||||||
SSH_LOG(SSH_LOG_PACKET,"current state : %d",session->session_state);
|
}
|
||||||
if(!ssh_is_blocking(session) && !ssh_connect_termination(session)){
|
|
||||||
return SSH_AGAIN;
|
|
||||||
}
|
|
||||||
|
|
||||||
session->pending_call_state=SSH_PENDING_CALL_NONE;
|
SSH_LOG(SSH_LOG_PACKET, "current state : %d", session->session_state);
|
||||||
if(session->session_state == SSH_SESSION_STATE_ERROR || session->session_state == SSH_SESSION_STATE_DISCONNECTED)
|
if (!ssh_is_blocking(session) && !ssh_connect_termination(session)) {
|
||||||
return SSH_ERROR;
|
return SSH_AGAIN;
|
||||||
return SSH_OK;
|
}
|
||||||
|
|
||||||
|
session->pending_call_state = SSH_PENDING_CALL_NONE;
|
||||||
|
if (session->session_state == SSH_SESSION_STATE_ERROR ||
|
||||||
|
session->session_state == SSH_SESSION_STATE_DISCONNECTED)
|
||||||
|
{
|
||||||
|
return SSH_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SSH_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user