From 1ffa2cb519e58a8f4a8224292de859a4092bfb7a Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 26 May 2009 09:41:13 +0200 Subject: [PATCH] These examples no longer need to explictly set the socket to non-blocking state as libssh2 itself will always do that by itself. --- example/simple/scp_nonblock.c | 17 ------------ example/simple/scp_write_nonblock.c | 17 ------------ example/simple/sftp_RW_nonblock.c | 17 ------------ example/simple/sftp_mkdir_nonblock.c | 17 ------------ example/simple/sftp_nonblock.c | 17 ------------ example/simple/sftp_write_nonblock.c | 27 ++++-------------- example/simple/sftpdir_nonblock.c | 17 ------------ example/simple/ssh2.c | 41 +++++++++++++++------------- example/simple/ssh2_exec.c | 19 +------------ 9 files changed, 28 insertions(+), 161 deletions(-) diff --git a/example/simple/scp_nonblock.c b/example/simple/scp_nonblock.c index e17927d..8b3f7dc 100644 --- a/example/simple/scp_nonblock.c +++ b/example/simple/scp_nonblock.c @@ -132,23 +132,6 @@ int main(int argc, char *argv[]) return -1; } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ -#ifdef F_SETFL - /* FIXME: this can/should be done in a more portable manner */ - rc = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, rc | O_NONBLOCK); -#elif defined(HAVE_IOCTLSOCKET) - ioctlsocket(sock, FIONBIO, &flag); -#else -#ifdef WIN32 - u_long mode = 1; - ioctlsocket (sock, FIONBIO, &mode); -#else -#error "add support for setting the socket non-blocking here" -#endif -#endif - /* Create a session instance */ session = libssh2_session_init(); if (!session) diff --git a/example/simple/scp_write_nonblock.c b/example/simple/scp_write_nonblock.c index e11d203..34021e8 100644 --- a/example/simple/scp_write_nonblock.c +++ b/example/simple/scp_write_nonblock.c @@ -102,23 +102,6 @@ int main(int argc, char *argv[]) return -1; } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ -#ifdef F_SETFL - /* FIXME: this can/should be done in a more portable manner */ - rc = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, rc | O_NONBLOCK); -#elif defined(HAVE_IOCTLSOCKET) - ioctlsocket(sock, FIONBIO, &flag); -#else -#ifdef WIN32 - u_long mode = 1; - ioctlsocket (sock, FIONBIO, &mode); -#else -#error "add support for setting the socket non-blocking here" -#endif -#endif - /* Create a session instance */ session = libssh2_session_init(); diff --git a/example/simple/sftp_RW_nonblock.c b/example/simple/sftp_RW_nonblock.c index 9054b51..0d0cc86 100644 --- a/example/simple/sftp_RW_nonblock.c +++ b/example/simple/sftp_RW_nonblock.c @@ -84,23 +84,6 @@ int main(int argc, char *argv[]) return -1; } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ -#ifdef F_SETFL - /* FIXME: this can/should be done in a more portable manner */ - rc = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, rc | O_NONBLOCK); -#elif defined(HAVE_IOCTLSOCKET) - ioctlsocket(sock, FIONBIO, &flag); -#else -#ifdef WIN32 - u_long mode = 1; - ioctlsocket (sock, FIONBIO, &mode); -#else -#error "add support for setting the socket non-blocking here" -#endif -#endif - /* Create a session instance */ session = libssh2_session_init(); diff --git a/example/simple/sftp_mkdir_nonblock.c b/example/simple/sftp_mkdir_nonblock.c index e4d751b..aae8097 100644 --- a/example/simple/sftp_mkdir_nonblock.c +++ b/example/simple/sftp_mkdir_nonblock.c @@ -88,23 +88,6 @@ int main(int argc, char *argv[]) return -1; } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ -#ifdef F_SETFL - /* FIXME: this can/should be done in a more portable manner */ - rc = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, rc | O_NONBLOCK); -#elif defined(HAVE_IOCTLSOCKET) - ioctlsocket(sock, FIONBIO, &flag); -#else -#ifdef WIN32 - u_long mode = 1; - ioctlsocket (sock, FIONBIO, &mode); -#else -#error "add support for setting the socket non-blocking here" -#endif -#endif - /* Create a session instance */ session = libssh2_session_init(); diff --git a/example/simple/sftp_nonblock.c b/example/simple/sftp_nonblock.c index 9ec6891..4a0ad1f 100644 --- a/example/simple/sftp_nonblock.c +++ b/example/simple/sftp_nonblock.c @@ -133,23 +133,6 @@ int main(int argc, char *argv[]) return -1; } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ -#ifdef F_SETFL - /* FIXME: this can/should be done in a more portable manner */ - rc = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, rc | O_NONBLOCK); -#elif defined(HAVE_IOCTLSOCKET) - ioctlsocket(sock, FIONBIO, &flag); -#else -#ifdef WIN32 - u_long mode = 1; - ioctlsocket (sock, FIONBIO, &mode); -#else -#error "add support for setting the socket non-blocking here" -#endif -#endif - /* Create a session instance */ session = libssh2_session_init(); if (!session) diff --git a/example/simple/sftp_write_nonblock.c b/example/simple/sftp_write_nonblock.c index 49669c3..d1185e7 100644 --- a/example/simple/sftp_write_nonblock.c +++ b/example/simple/sftp_write_nonblock.c @@ -103,23 +103,6 @@ int main(int argc, char *argv[]) return -1; } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ -#ifdef F_SETFL - /* FIXME: this can/should be done in a more portable manner */ - rc = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, rc | O_NONBLOCK); -#elif defined(HAVE_IOCTLSOCKET) - ioctlsocket(sock, FIONBIO, &flag); -#else -#ifdef WIN32 - u_long mode = 1; - ioctlsocket (sock, FIONBIO, &mode); -#else -#error "add support for setting the socket non-blocking here" -#endif -#endif - /* Create a session instance */ session = libssh2_session_init(); @@ -139,11 +122,11 @@ int main(int argc, char *argv[]) return -1; } - /* At this point we havn't yet authenticated. The first thing to do - * is check the hostkey's fingerprint against our known hosts Your app - * may have it hard coded, may go to a file, may present it to the - * user, that's your call - */ + /* At this point we havn't yet authenticated. The first thing to do is + * check the hostkey's fingerprint against our known hosts Your app may + * have it hard coded, may go to a file, may present it to the user, + * that's your call + */ fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); printf("Fingerprint: "); for(i = 0; i < 16; i++) { diff --git a/example/simple/sftpdir_nonblock.c b/example/simple/sftpdir_nonblock.c index 269858d..585257e 100644 --- a/example/simple/sftpdir_nonblock.c +++ b/example/simple/sftpdir_nonblock.c @@ -88,23 +88,6 @@ int main(int argc, char *argv[]) return -1; } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ -#ifdef F_SETFL - /* FIXME: this can/should be done in a more portable manner */ - rc = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, rc | O_NONBLOCK); -#elif defined(HAVE_IOCTLSOCKET) - ioctlsocket(sock, FIONBIO, &flag); -#else -#ifdef WIN32 - u_long mode = 1; - ioctlsocket (sock, FIONBIO, &mode); -#else -#error "add support for setting the socket non-blocking here" -#endif -#endif - /* Create a session instance */ session = libssh2_session_init(); diff --git a/example/simple/ssh2.c b/example/simple/ssh2.c index 20bae39..98e64e2 100644 --- a/example/simple/ssh2.c +++ b/example/simple/ssh2.c @@ -45,11 +45,12 @@ const char *username="username"; const char *password="password"; -static void kbd_callback(const char *name, int name_len, - const char *instruction, int instruction_len, int num_prompts, - const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, - LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, - void **abstract) +static void kbd_callback(const char *name, int name_len, + const char *instruction, int instruction_len, + int num_prompts, + const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, + LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, + void **abstract) { (void)name; (void)name_len; @@ -92,13 +93,11 @@ int main(int argc, char *argv[]) password = argv[3]; } - /* Ultra basic "connect to port 22 on localhost" - * Your code is responsible for creating the socket establishing the connection + /* Ultra basic "connect to port 22 on localhost". Your code is + * responsible for creating the socket establishing the connection */ sock = socket(AF_INET, SOCK_STREAM, 0); -#ifndef WIN32 - fcntl(sock, F_SETFL, 0); -#endif + sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; @@ -108,8 +107,8 @@ int main(int argc, char *argv[]) return -1; } - /* Create a session instance and start it up - * This will trade welcome banners, exchange keys, and setup crypto, compression, and MAC layers + /* Create a session instance and start it up. This will trade welcome + * banners, exchange keys, and setup crypto, compression, and MAC layers */ session = libssh2_session_init(); if (libssh2_session_startup(session, sock)) { @@ -117,9 +116,10 @@ int main(int argc, char *argv[]) return -1; } - /* At this point we havn't authenticated, - * The first thing to do is check the hostkey's fingerprint against our known hosts - * Your app may have it hard coded, may go to a file, may present it to the user, that's your call + /* At this point we havn't authenticated. The first thing to do is check + * the hostkey's fingerprint against our known hosts Your app may have it + * hard coded, may go to a file, may present it to the user, that's your + * call */ fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); printf("Fingerprint: "); @@ -141,7 +141,7 @@ int main(int argc, char *argv[]) auth_pw |= 4; } - /* if we got an 4. argument we set this option if supported */ + /* if we got an 4. argument we set this option if supported */ if(argc > 4) { if ((auth_pw & 1) && !strcasecmp(argv[4], "-p")) { auth_pw = 1; @@ -164,7 +164,8 @@ int main(int argc, char *argv[]) } } else if (auth_pw & 2) { /* Or via keyboard-interactive */ - if (libssh2_userauth_keyboard_interactive(session, username, &kbd_callback) ) { + if (libssh2_userauth_keyboard_interactive(session, username, + &kbd_callback) ) { printf("\tAuthentication by keyboard-interactive failed!\n"); goto shutdown; } else { @@ -172,7 +173,8 @@ int main(int argc, char *argv[]) } } else if (auth_pw & 4) { /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, keyfile2, password)) { + if (libssh2_userauth_publickey_fromfile(session, username, keyfile1, + keyfile2, password)) { printf("\tAuthentication by public key failed!\n"); goto shutdown; } else { @@ -235,7 +237,8 @@ int main(int argc, char *argv[]) shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, + "Normal Shutdown, Thank you for playing"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/simple/ssh2_exec.c b/example/simple/ssh2_exec.c index 6056089..220d130 100644 --- a/example/simple/ssh2_exec.c +++ b/example/simple/ssh2_exec.c @@ -109,29 +109,12 @@ int main(int argc, char *argv[]) return -1; } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ -#ifdef F_SETFL - /* FIXME: this can/should be done in a more portable manner */ - rc = fcntl(sock, F_GETFL, 0); - fcntl(sock, F_SETFL, rc | O_NONBLOCK); -#elif defined(HAVE_IOCTLSOCKET) - ioctlsocket(sock, FIONBIO, &flag); -#else -#ifdef WIN32 - u_long mode = 1; - ioctlsocket (sock, FIONBIO, &mode); -#else -#error "add support for setting the socket non-blocking here" -#endif -#endif - /* Create a session instance */ session = libssh2_session_init(); if (!session) return -1; - /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + /* tell libssh2 we want it all done non-blocking */ libssh2_session_set_blocking(session, 0); /* ... start it up. This will trade welcome banners, exchange keys,