1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-09-02 17:21:16 +03:00

These examples no longer need to explictly set the socket to non-blocking state

as libssh2 itself will always do that by itself.
This commit is contained in:
Daniel Stenberg
2009-05-26 09:41:13 +02:00
parent 279d6dd82a
commit 1ffa2cb519
9 changed files with 28 additions and 161 deletions

View File

@@ -132,23 +132,6 @@ int main(int argc, char *argv[])
return -1; 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 */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if (!session)

View File

@@ -102,23 +102,6 @@ int main(int argc, char *argv[])
return -1; 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 /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();

View File

@@ -84,23 +84,6 @@ int main(int argc, char *argv[])
return -1; 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 /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();

View File

@@ -88,23 +88,6 @@ int main(int argc, char *argv[])
return -1; 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 /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();

View File

@@ -133,23 +133,6 @@ int main(int argc, char *argv[])
return -1; 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 */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if (!session)

View File

@@ -103,23 +103,6 @@ int main(int argc, char *argv[])
return -1; 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 /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
@@ -139,11 +122,11 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
/* At this point we havn't yet authenticated. The first thing to do /* At this point we havn't yet authenticated. The first thing to do is
* is check the hostkey's fingerprint against our known hosts Your app * check the hostkey's fingerprint against our known hosts Your app may
* may have it hard coded, may go to a file, may present it to the * have it hard coded, may go to a file, may present it to the user,
* user, that's your call * that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
printf("Fingerprint: "); printf("Fingerprint: ");
for(i = 0; i < 16; i++) { for(i = 0; i < 16; i++) {

View File

@@ -88,23 +88,6 @@ int main(int argc, char *argv[])
return -1; 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 /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();

View File

@@ -45,11 +45,12 @@ const char *username="username";
const char *password="password"; const char *password="password";
static void kbd_callback(const char *name, int name_len, static void kbd_callback(const char *name, int name_len,
const char *instruction, int instruction_len, int num_prompts, const char *instruction, int instruction_len,
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, int num_prompts,
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
void **abstract) LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
void **abstract)
{ {
(void)name; (void)name;
(void)name_len; (void)name_len;
@@ -92,13 +93,11 @@ int main(int argc, char *argv[])
password = argv[3]; password = argv[3];
} }
/* Ultra basic "connect to port 22 on localhost" /* Ultra basic "connect to port 22 on localhost". Your code is
* Your code is responsible for creating the socket establishing the connection * responsible for creating the socket establishing the connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
#ifndef WIN32
fcntl(sock, F_SETFL, 0);
#endif
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
@@ -108,8 +107,8 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
/* Create a session instance and start it up /* Create a session instance and start it up. This will trade welcome
* This will trade welcome banners, exchange keys, and setup crypto, compression, and MAC layers * banners, exchange keys, and setup crypto, compression, and MAC layers
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if (libssh2_session_startup(session, sock)) { if (libssh2_session_startup(session, sock)) {
@@ -117,9 +116,10 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
/* At this point we havn't authenticated, /* At this point we havn't authenticated. The first thing to do is check
* The first thing to do is check the hostkey's fingerprint against our known hosts * the hostkey's fingerprint against our known hosts Your app may have it
* Your app may have it hard coded, may go to a file, may present it to the user, that's your call * 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); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
printf("Fingerprint: "); printf("Fingerprint: ");
@@ -141,7 +141,7 @@ int main(int argc, char *argv[])
auth_pw |= 4; 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(argc > 4) {
if ((auth_pw & 1) && !strcasecmp(argv[4], "-p")) { if ((auth_pw & 1) && !strcasecmp(argv[4], "-p")) {
auth_pw = 1; auth_pw = 1;
@@ -164,7 +164,8 @@ int main(int argc, char *argv[])
} }
} else if (auth_pw & 2) { } else if (auth_pw & 2) {
/* Or via keyboard-interactive */ /* 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"); printf("\tAuthentication by keyboard-interactive failed!\n");
goto shutdown; goto shutdown;
} else { } else {
@@ -172,7 +173,8 @@ int main(int argc, char *argv[])
} }
} else if (auth_pw & 4) { } else if (auth_pw & 4) {
/* Or by public key */ /* 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"); printf("\tAuthentication by public key failed!\n");
goto shutdown; goto shutdown;
} else { } else {
@@ -235,7 +237,8 @@ int main(int argc, char *argv[])
shutdown: 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); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32

View File

@@ -109,29 +109,12 @@ int main(int argc, char *argv[])
return -1; 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 */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if (!session)
return -1; 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); libssh2_session_set_blocking(session, 0);
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,