mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 10:40:27 +09:00
connect: Code style formatting
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit da50b12051)
This commit is contained in:
committed by
Andreas Schneider
parent
466ca07626
commit
3bc5f88f77
@@ -90,7 +90,8 @@
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#ifndef gai_strerror
|
#ifndef gai_strerror
|
||||||
char WSAAPI *gai_strerrorA(int code) {
|
char WSAAPI *gai_strerrorA(int code)
|
||||||
|
{
|
||||||
static char buf[256];
|
static char buf[256];
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "Undetermined error code (%d)", code);
|
snprintf(buf, sizeof(buf), "Undetermined error code (%d)", code);
|
||||||
@@ -100,7 +101,8 @@ char WSAAPI *gai_strerrorA(int code) {
|
|||||||
#endif /* gai_strerror */
|
#endif /* gai_strerror */
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
static int ssh_connect_socket_close(socket_t s){
|
static int ssh_connect_socket_close(socket_t s)
|
||||||
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
return closesocket(s);
|
return closesocket(s);
|
||||||
#else
|
#else
|
||||||
@@ -108,8 +110,8 @@ static int ssh_connect_socket_close(socket_t s){
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int getai(const char *host, int port, struct addrinfo **ai)
|
||||||
static int getai(const char *host, int port, struct addrinfo **ai) {
|
{
|
||||||
const char *service = NULL;
|
const char *service = NULL;
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
char s_port[10];
|
char s_port[10];
|
||||||
@@ -160,16 +162,18 @@ static int set_tcp_nodelay(socket_t socket)
|
|||||||
* @warning very ugly !!!
|
* @warning very ugly !!!
|
||||||
*/
|
*/
|
||||||
socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
|
socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
|
||||||
const char *bind_addr, int port) {
|
const char *bind_addr, int port)
|
||||||
|
{
|
||||||
socket_t s = -1;
|
socket_t s = -1;
|
||||||
int rc;
|
int rc;
|
||||||
struct addrinfo *ai;
|
struct addrinfo *ai = NULL;
|
||||||
struct addrinfo *itr;
|
struct addrinfo *itr = NULL;
|
||||||
|
|
||||||
rc = getai(host, port, &ai);
|
rc = getai(host, port, &ai);
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
ssh_set_error(session, SSH_FATAL,
|
ssh_set_error(session, SSH_FATAL,
|
||||||
"Failed to resolve hostname %s (%s)", host, gai_strerror(rc));
|
"Failed to resolve hostname %s (%s)",
|
||||||
|
host, gai_strerror(rc));
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -200,7 +204,10 @@ socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (bind_itr = bind_ai; bind_itr != NULL; bind_itr = bind_itr->ai_next) {
|
for (bind_itr = bind_ai;
|
||||||
|
bind_itr != NULL;
|
||||||
|
bind_itr = bind_itr->ai_next)
|
||||||
|
{
|
||||||
if (bind(s, bind_itr->ai_addr, bind_itr->ai_addrlen) < 0) {
|
if (bind(s, bind_itr->ai_addr, bind_itr->ai_addrlen) < 0) {
|
||||||
ssh_set_error(session, SSH_FATAL,
|
ssh_set_error(session, SSH_FATAL,
|
||||||
"Binding local address: %s", strerror(errno));
|
"Binding local address: %s", strerror(errno));
|
||||||
@@ -222,7 +229,8 @@ socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
|
|||||||
rc = ssh_socket_set_nonblocking(s);
|
rc = ssh_socket_set_nonblocking(s);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
ssh_set_error(session, SSH_FATAL,
|
ssh_set_error(session, SSH_FATAL,
|
||||||
"Failed to set socket non-blocking for %s:%d", host, port);
|
"Failed to set socket non-blocking for %s:%d",
|
||||||
|
host, port);
|
||||||
ssh_connect_socket_close(s);
|
ssh_connect_socket_close(s);
|
||||||
s = -1;
|
s = -1;
|
||||||
continue;
|
continue;
|
||||||
@@ -233,7 +241,8 @@ socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
|
|||||||
rc = set_tcp_nodelay(s);
|
rc = set_tcp_nodelay(s);
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
ssh_set_error(session, SSH_FATAL,
|
ssh_set_error(session, SSH_FATAL,
|
||||||
"Failed to set TCP_NODELAY on socket: %s", strerror(errno));
|
"Failed to set TCP_NODELAY on socket: %s",
|
||||||
|
strerror(errno));
|
||||||
ssh_connect_socket_close(s);
|
ssh_connect_socket_close(s);
|
||||||
s = -1;
|
s = -1;
|
||||||
continue;
|
continue;
|
||||||
@@ -264,10 +273,12 @@ socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host,
|
|||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int ssh_select_cb (socket_t fd, int revents, void *userdata){
|
static int ssh_select_cb (socket_t fd, int revents, void *userdata)
|
||||||
|
{
|
||||||
fd_set *set = (fd_set *)userdata;
|
fd_set *set = (fd_set *)userdata;
|
||||||
if(revents & POLLIN)
|
if (revents & POLLIN) {
|
||||||
FD_SET(fd, set);
|
FD_SET(fd, set);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,7 +313,8 @@ static int ssh_select_cb (socket_t fd, int revents, void *userdata){
|
|||||||
* @see select(2)
|
* @see select(2)
|
||||||
*/
|
*/
|
||||||
int ssh_select(ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd,
|
int ssh_select(ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd,
|
||||||
fd_set *readfds, struct timeval *timeout) {
|
fd_set *readfds, struct timeval *timeout)
|
||||||
|
{
|
||||||
fd_set origfds;
|
fd_set origfds;
|
||||||
socket_t fd;
|
socket_t fd;
|
||||||
size_t i, j;
|
size_t i, j;
|
||||||
@@ -312,7 +324,7 @@ int ssh_select(ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd,
|
|||||||
ssh_event event = ssh_event_new();
|
ssh_event event = ssh_event_new();
|
||||||
int firstround = 1;
|
int firstround = 1;
|
||||||
|
|
||||||
base_tm = tm=timeout->tv_sec * 1000 + timeout->tv_usec/1000;
|
base_tm = tm = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
|
||||||
for (i = 0 ; channels[i] != NULL; ++i) {
|
for (i = 0 ; channels[i] != NULL; ++i) {
|
||||||
ssh_event_add_session(event, channels[i]->session);
|
ssh_event_add_session(event, channels[i]->session);
|
||||||
}
|
}
|
||||||
@@ -332,17 +344,24 @@ int ssh_select(ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd,
|
|||||||
/* Poll every channel */
|
/* Poll every channel */
|
||||||
j = 0;
|
j = 0;
|
||||||
for (i = 0; channels[i]; i++) {
|
for (i = 0; channels[i]; i++) {
|
||||||
if(ssh_channel_poll(channels[i], 0) != 0) {
|
rc = ssh_channel_poll(channels[i], 0);
|
||||||
|
if (rc != 0) {
|
||||||
outchannels[j] = channels[i];
|
outchannels[j] = channels[i];
|
||||||
j++;
|
j++;
|
||||||
} else if(ssh_channel_poll(channels[i], 1) != 0) {
|
} else {
|
||||||
|
rc = ssh_channel_poll(channels[i], 1);
|
||||||
|
if (rc != 0) {
|
||||||
outchannels[j] = channels[i];
|
outchannels[j] = channels[i];
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
outchannels[j] = NULL;
|
outchannels[j] = NULL;
|
||||||
if(j != 0)
|
if (j != 0) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/* watch if a user socket was triggered */
|
/* watch if a user socket was triggered */
|
||||||
for (fd = 0; fd < maxfd; fd++) {
|
for (fd = 0; fd < maxfd; fd++) {
|
||||||
if (FD_ISSET(fd, readfds)) {
|
if (FD_ISSET(fd, readfds)) {
|
||||||
@@ -351,13 +370,16 @@ int ssh_select(ssh_channel *channels, ssh_channel *outchannels, socket_t maxfd,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* If the timeout is elapsed, we should go out */
|
/* If the timeout is elapsed, we should go out */
|
||||||
if(!firstround && ssh_timeout_elapsed(&ts, base_tm))
|
if (!firstround && ssh_timeout_elapsed(&ts, base_tm)) {
|
||||||
goto out;
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* since there's nothing, let's fire the polling */
|
/* since there's nothing, let's fire the polling */
|
||||||
rc = ssh_event_dopoll(event,tm);
|
rc = ssh_event_dopoll(event,tm);
|
||||||
if (rc == SSH_ERROR) {
|
if (rc == SSH_ERROR) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
tm = ssh_timeout_update(&ts, base_tm);
|
tm = ssh_timeout_update(&ts, base_tm);
|
||||||
firstround = 0;
|
firstround = 0;
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|||||||
Reference in New Issue
Block a user