diff --git a/example/simple/scp.c b/example/simple/scp.c index a2c9cdc..cedacb2 100644 --- a/example/simple/scp.c +++ b/example/simple/scp.c @@ -1,5 +1,5 @@ /* - * $Id: scp.c,v 1.7 2007/06/06 12:34:08 jehousley Exp $ + * $Id: scp.c,v 1.8 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do a simple SCP transfer. */ @@ -67,7 +67,7 @@ int main(int argc, char *argv[]) if (argc > 4) { scppath = argv[4]; } - + /* Ultra basic "connect to port 22 on localhost" * Your code is responsible for creating the socket establishing the * connection diff --git a/example/simple/scp_nonblock.c b/example/simple/scp_nonblock.c index 138eccd..f9d98d8 100644 --- a/example/simple/scp_nonblock.c +++ b/example/simple/scp_nonblock.c @@ -1,5 +1,5 @@ /* - * $Id: scp_nonblock.c,v 1.6 2007/07/05 15:31:19 jehousley Exp $ + * $Id: scp_nonblock.c,v 1.7 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SCP transfers in a non-blocking manner. */ @@ -46,13 +46,13 @@ int main(int argc, char *argv[]) struct stat fileinfo; int rc; off_t got=0; - + #ifdef WIN32 WSADATA wsadata; - + WSAStartup(WINSOCK_VERSION, &wsadata); #endif - + if (argc > 1) { hostaddr = inet_addr(argv[1]); } else { @@ -67,13 +67,13 @@ int main(int argc, char *argv[]) if (argc > 4) { scppath = argv[4]; } - + /* 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); - + sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "failed to connect!\n"); return -1; } - + /* We set the socket non-blocking. We do it after the connect just to simplify the example code. */ #ifdef F_SETFL @@ -91,15 +91,15 @@ int main(int argc, char *argv[]) #else #error "add support for setting the socket non-blocking here" #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 */ libssh2_session_set_blocking(session, 0); - + /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ @@ -108,7 +108,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Failure establishing SSH session: %d\n", rc); 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 @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); } fprintf(stderr, "\n"); - + if (auth_pw) { /* We could authenticate via password */ while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); @@ -139,36 +139,36 @@ int main(int argc, char *argv[]) goto shutdown; } } - + /* Request a file via SCP */ fprintf(stderr, "libssh2_scp_recv()!\n"); do { channel = libssh2_scp_recv(session, scppath, &fileinfo); - - if ((!channel) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { - char *err_msg; - libssh2_session_last_error(session, &err_msg, NULL, 0); + if ((!channel) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { + char *err_msg; + + libssh2_session_last_error(session, &err_msg, NULL, 0); fprintf(stderr, "%s\n", err_msg); goto shutdown; } } while (!channel); fprintf(stderr, "libssh2_scp_recv() is done, now receive data!\n"); - + while(got < fileinfo.st_size) { char mem[1000]; - + struct timeval timeout; int rc; fd_set fd; - + do { int amount=sizeof(mem); - + if ((fileinfo.st_size -got) < amount) { amount = fileinfo.st_size - got; } - + /* loop until we block */ rc = libssh2_channel_read(channel, mem, amount); if (rc > 0) { @@ -176,18 +176,18 @@ int main(int argc, char *argv[]) got += rc; } } while (rc > 0); - + if (rc == LIBSSH2_ERROR_EAGAIN) { /* this is due to blocking that would occur otherwise so we loop on this condition */ - + timeout.tv_sec = 10; timeout.tv_usec = 0; - + FD_ZERO(&fd); - + FD_SET(sock, &fd); - + rc = select(sock+1, &fd, &fd, NULL, &timeout); if (rc <= 0) { /* negative is error @@ -198,15 +198,15 @@ int main(int argc, char *argv[]) } break; } - + libssh2_channel_free(channel); channel = NULL; - + shutdown: - + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_free(session); - + #ifdef WIN32 Sleep(1000); closesocket(sock); diff --git a/example/simple/scp_write.c b/example/simple/scp_write.c index 9ff41a9..61c401b 100644 --- a/example/simple/scp_write.c +++ b/example/simple/scp_write.c @@ -1,5 +1,5 @@ /* - * $Id: scp_write.c,v 1.2 2007/07/14 20:54:47 bagder Exp $ + * $Id: scp_write.c,v 1.3 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do a simple SCP transfer. */ @@ -58,27 +58,27 @@ int main(int argc, char *argv[]) #endif if (argc > 1) { - hostaddr = inet_addr(argv[1]); + hostaddr = inet_addr(argv[1]); } else { - hostaddr = htonl(0x7F000001); + hostaddr = htonl(0x7F000001); } if (argc > 2) { - username = argv[2]; + username = argv[2]; } if (argc > 3) { - password = argv[3]; + password = argv[3]; } if(argc > 4) { - loclfile = argv[4]; + loclfile = argv[4]; } if (argc > 5) { - scppath = argv[5]; + scppath = argv[5]; } local = fopen(loclfile, "rb"); if (!local) { - fprintf(stderr, "Can't local file %s\n", loclfile); - goto shutdown; + fprintf(stderr, "Can't local file %s\n", loclfile); + goto shutdown; } stat(loclfile, &fileinfo); @@ -93,24 +93,24 @@ int main(int argc, char *argv[]) sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); - return -1; + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; } /* Create a session instance */ session = libssh2_session_init(); if(!session) - return -1; + return -1; /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ rc = libssh2_session_startup(session, sock); if(rc) { - fprintf(stderr, "Failure establishing SSH session: %d\n", rc); - return -1; + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; } /* At this point we havn't yet authenticated. The first thing to do @@ -121,25 +121,25 @@ int main(int argc, char *argv[]) fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fprintf(stderr, "Fingerprint: "); for(i = 0; i < 16; i++) { - fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); } fprintf(stderr, "\n"); if (auth_pw) { - /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { - fprintf(stderr, "Authentication by password failed.\n"); - goto shutdown; - } + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } } else { - /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { - fprintf(stderr, "\tAuthentication by public key failed\n"); - goto shutdown; - } + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } } /* Request a file via SCP */ @@ -147,25 +147,25 @@ int main(int argc, char *argv[]) (unsigned long)fileinfo.st_size); if (!channel) { - fprintf(stderr, "Unable to open a session\n"); - goto shutdown; + fprintf(stderr, "Unable to open a session\n"); + goto shutdown; } fprintf(stderr, "SCP session waiting to send file\n"); do { - nread = fread(mem, 1, sizeof(mem), local); - if (nread <= 0) { - /* end of file */ - break; - } - ptr = mem; + nread = fread(mem, 1, sizeof(mem), local); + if (nread <= 0) { + /* end of file */ + break; + } + ptr = mem; - do { - /* write data in a loop until we block */ - rc = libssh2_channel_write(channel, ptr, nread); - ptr += rc; - nread -= nread; - } while (rc > 0); + do { + /* write data in a loop until we block */ + rc = libssh2_channel_write(channel, ptr, nread); + ptr += rc; + nread -= nread; + } while (rc > 0); } while (1); fprintf(stderr, "Sending EOF\n"); diff --git a/example/simple/scp_write_nonblock.c b/example/simple/scp_write_nonblock.c index 7827b7d..87ea21b 100644 --- a/example/simple/scp_write_nonblock.c +++ b/example/simple/scp_write_nonblock.c @@ -1,5 +1,5 @@ /* - * $Id: scp_write_nonblock.c,v 1.4 2007/07/14 20:54:47 bagder Exp $ + * $Id: scp_write_nonblock.c,v 1.5 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do a simple SCP transfer. */ @@ -58,27 +58,27 @@ int main(int argc, char *argv[]) #endif if (argc > 1) { - hostaddr = inet_addr(argv[1]); + hostaddr = inet_addr(argv[1]); } else { - hostaddr = htonl(0x7F000001); + hostaddr = htonl(0x7F000001); } if (argc > 2) { - username = argv[2]; + username = argv[2]; } if (argc > 3) { - password = argv[3]; + password = argv[3]; } if(argc > 4) { - loclfile = argv[4]; + loclfile = argv[4]; } if (argc > 5) { - scppath = argv[5]; + scppath = argv[5]; } local = fopen(loclfile, "rb"); if (!local) { - fprintf(stderr, "Can't local file %s\n", loclfile); - goto shutdown; + fprintf(stderr, "Can't local file %s\n", loclfile); + goto shutdown; } stat(loclfile, &fileinfo); @@ -93,13 +93,13 @@ int main(int argc, char *argv[]) sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); - return -1; + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; } /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ + simplify the example code. */ #ifdef F_SETFL /* FIXME: this can/should be done in a more portable manner */ rc = fcntl(sock, F_GETFL, 0); @@ -112,7 +112,7 @@ int main(int argc, char *argv[]) */ session = libssh2_session_init(); if(!session) - return -1; + return -1; /* Since we have set non-blocking, tell libssh2 we are non-blocking */ libssh2_session_set_blocking(session, 0); @@ -121,10 +121,10 @@ int main(int argc, char *argv[]) * and setup crypto, compression, and MAC layers */ while ((rc = libssh2_session_startup(session, sock)) - == LIBSSH2_ERROR_EAGAIN); + == LIBSSH2_ERROR_EAGAIN); if(rc) { - fprintf(stderr, "Failure establishing SSH session: %d\n", rc); - return -1; + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; } /* At this point we havn't yet authenticated. The first thing to do @@ -135,64 +135,64 @@ int main(int argc, char *argv[]) fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fprintf(stderr, "Fingerprint: "); for(i = 0; i < 16; i++) { - fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); } fprintf(stderr, "\n"); if (auth_pw) { - /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == + /* We could authenticate via password */ + while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { - fprintf(stderr, "Authentication by password failed.\n"); - goto shutdown; - } + if (rc) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; + } } else { - /* Or by public key */ - while ((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { - fprintf(stderr, "\tAuthentication by public key failed\n"); - goto shutdown; - } + /* Or by public key */ + while ((rc = libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) == LIBSSH2_ERROR_EAGAIN); + if (rc) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } } /* Request a file via SCP */ do { - channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode, + channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode, (unsigned long)fileinfo.st_size); - if ((!channel) && (libssh2_session_last_errno(session) != + if ((!channel) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { - char *err_msg; + char *err_msg; - libssh2_session_last_error(session, &err_msg, NULL, 0); - fprintf(stderr, "%s\n", err_msg); - goto shutdown; - } + libssh2_session_last_error(session, &err_msg, NULL, 0); + fprintf(stderr, "%s\n", err_msg); + goto shutdown; + } } while (!channel); fprintf(stderr, "SCP session waiting to send file\n"); do { - nread = fread(mem, 1, sizeof(mem), local); - if (nread <= 0) { - /* end of file */ - break; - } - ptr = mem; + nread = fread(mem, 1, sizeof(mem), local); + if (nread <= 0) { + /* end of file */ + break; + } + ptr = mem; - do { - /* write data in a loop until we block */ - while ((rc = libssh2_channel_write(channel, ptr, nread)) == + do { + /* write data in a loop until we block */ + while ((rc = libssh2_channel_write(channel, ptr, nread)) == LIBSSH2_ERROR_EAGAIN); - if (rc < 0) { - fprintf(stderr, "ERROR %d\n", rc); - } - ptr += rc; - nread -= rc; - } while (nread > 0); + if (rc < 0) { + fprintf(stderr, "ERROR %d\n", rc); + } + ptr += rc; + nread -= rc; + } while (nread > 0); } while (1); fprintf(stderr, "Sending EOF\n"); diff --git a/example/simple/sftp.c b/example/simple/sftp.c index 553ec6c..ebcf82e 100644 --- a/example/simple/sftp.c +++ b/example/simple/sftp.c @@ -1,5 +1,5 @@ /* - * $Id: sftp.c,v 1.8 2007/06/06 12:34:08 jehousley Exp $ + * $Id: sftp.c,v 1.9 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SFTP transfers. * @@ -40,153 +40,153 @@ int main(int argc, char *argv[]) { - unsigned long hostaddr; - int sock, i, auth_pw = 1; - struct sockaddr_in sin; - const char *fingerprint; - LIBSSH2_SESSION *session; - char *username=(char *)"username"; - char *password=(char *)"password"; - char *sftppath=(char *)"/tmp/TEST"; - int rc; - LIBSSH2_SFTP *sftp_session; - LIBSSH2_SFTP_HANDLE *sftp_handle; + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + char *username=(char *)"username"; + char *password=(char *)"password"; + char *sftppath=(char *)"/tmp/TEST"; + int rc; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; #ifdef WIN32 - WSADATA wsadata; + WSADATA wsadata; - WSAStartup(WINSOCK_VERSION, &wsadata); + WSAStartup(WINSOCK_VERSION, &wsadata); #endif - if (argc > 1) { - hostaddr = inet_addr(argv[1]); - } else { - hostaddr = htonl(0x7F000001); - } + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } - if(argc > 2) { - username = argv[2]; - } - if(argc > 3) { - password = argv[3]; - } - if(argc > 4) { - sftppath = argv[4]; - } - /* - * The application code is responsible for creating the socket - * and establishing the connection - */ - sock = socket(AF_INET, SOCK_STREAM, 0); + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + sftppath = argv[4]; + } + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); - sin.sin_family = AF_INET; - sin.sin_port = htons(22); - sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); - return -1; - } + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } - /* Create a session instance - */ - session = libssh2_session_init(); - if(!session) - return -1; + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; - /* Since we have set non-blocking, tell libssh2 we are blocking */ - libssh2_session_set_blocking(session, 1); - - /* ... start it up. This will trade welcome banners, exchange keys, - * and setup crypto, compression, and MAC layers - */ - rc = libssh2_session_startup(session, sock); - if(rc) { - fprintf(stderr, "Failure establishing SSH session: %d\n", rc); - return -1; - } + /* Since we have set non-blocking, tell libssh2 we are blocking */ + libssh2_session_set_blocking(session, 1); - /* Since we have not set non-blocking, tell libssh2 we are blocking */ - libssh2_session_set_blocking(session, 1); + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_startup(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + 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 - */ - fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); - fprintf(stderr, "Fingerprint: "); - for(i = 0; i < 16; i++) { - fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); - } - fprintf(stderr, "\n"); + /* Since we have not set non-blocking, tell libssh2 we are blocking */ + libssh2_session_set_blocking(session, 1); - if (auth_pw) { - /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { - fprintf(stderr, "Authentication by password failed.\n"); - goto shutdown; - } - } else { - /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { - fprintf(stderr, "\tAuthentication by public key failed\n"); - goto shutdown; - } - } + /* 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); + fprintf(stderr, "Fingerprint: "); + for(i = 0; i < 16; i++) { + fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); + } + fprintf(stderr, "\n"); - fprintf(stderr, "libssh2_sftp_init()!\n"); - sftp_session = libssh2_sftp_init(session); - - if (!sftp_session) { - fprintf(stderr, "Unable to init SFTP session\n"); - goto shutdown; - } - - /* Since we have not set non-blocking, tell libssh2 we are blocking */ - libssh2_session_set_blocking(session, 1); - - fprintf(stderr, "libssh2_sftp_open()!\n"); - /* Request a file via SFTP */ - sftp_handle = - libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); - - if (!sftp_handle) { - fprintf(stderr, "Unable to open file with SFTP\n"); - goto shutdown; - } - fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); - do { - char mem[1024]; - - /* loop until we fail */ - fprintf(stderr, "libssh2_sftp_read()!\n"); - rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); - if (rc > 0) { - write(1, mem, rc); - } else { - break; + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + fprintf(stderr, "Authentication by password failed.\n"); + goto shutdown; } - } while (1); + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + fprintf(stderr, "\tAuthentication by public key failed\n"); + goto shutdown; + } + } - libssh2_sftp_close(sftp_handle); - libssh2_sftp_shutdown(sftp_session); + fprintf(stderr, "libssh2_sftp_init()!\n"); + sftp_session = libssh2_sftp_init(session); - shutdown: + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); - libssh2_session_free(session); + /* Since we have not set non-blocking, tell libssh2 we are blocking */ + libssh2_session_set_blocking(session, 1); + + fprintf(stderr, "libssh2_sftp_open()!\n"); + /* Request a file via SFTP */ + sftp_handle = + libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); + + if (!sftp_handle) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); + do { + char mem[1024]; + + /* loop until we fail */ + fprintf(stderr, "libssh2_sftp_read()!\n"); + rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); + if (rc > 0) { + write(1, mem, rc); + } else { + break; + } + } while (1); + + libssh2_sftp_close(sftp_handle); + libssh2_sftp_shutdown(sftp_session); + + shutdown: + + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); #ifdef WIN32 - Sleep(1000); - closesocket(sock); + Sleep(1000); + closesocket(sock); #else - sleep(1); - close(sock); + sleep(1); + close(sock); #endif -fprintf(stderr, "all done\n"); - return 0; + fprintf(stderr, "all done\n"); + return 0; } diff --git a/example/simple/sftp_RW_nonblock.c b/example/simple/sftp_RW_nonblock.c index cc0df40..e7cf6b7 100644 --- a/example/simple/sftp_RW_nonblock.c +++ b/example/simple/sftp_RW_nonblock.c @@ -1,5 +1,5 @@ /* - * $Id: sftp_RW_nonblock.c,v 1.7 2007/06/08 13:33:08 jehousley Exp $ + * $Id: sftp_RW_nonblock.c,v 1.8 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SFTP transfers in a non-blocking manner. * @@ -44,251 +44,251 @@ int main(int argc, char *argv[]) { - int sock, i, auth_pw = 1; - struct sockaddr_in sin; - const char *fingerprint; - LIBSSH2_SESSION *session; - char *username=(char *)"username"; - char *password=(char *)"password"; - char *sftppath=(char *)"/tmp/TEST"; /* source path */ - char *dest=(char *)"/tmp/TEST2"; /* destination path */ - int rc; - LIBSSH2_SFTP *sftp_session; - LIBSSH2_SFTP_HANDLE *sftp_handle; - FILE *tempstorage; - char mem[1000]; - struct timeval timeout; - fd_set fd; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + char *username=(char *)"username"; + char *password=(char *)"password"; + char *sftppath=(char *)"/tmp/TEST"; /* source path */ + char *dest=(char *)"/tmp/TEST2"; /* destination path */ + int rc; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; + FILE *tempstorage; + char mem[1000]; + struct timeval timeout; + fd_set fd; #ifdef WIN32 - WSADATA wsadata; + WSADATA wsadata; - WSAStartup(WINSOCK_VERSION, &wsadata); + WSAStartup(WINSOCK_VERSION, &wsadata); #endif - /* Ultra basic "connect to port 22 on localhost" - * The application is responsible for creating the socket establishing - * the connection - */ - sock = socket(AF_INET, SOCK_STREAM, 0); + /* Ultra basic "connect to port 22 on localhost" + * The application is responsible for creating the socket establishing + * the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); - sin.sin_family = AF_INET; - sin.sin_port = htons(22); - sin.sin_addr.s_addr = htonl(0x7F000001); - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); - return -1; - } + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = htonl(0x7F000001); + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ + /* 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); + /* FIXME: this can/should be done in a more portable manner */ + rc = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, rc | O_NONBLOCK); #else #error "add support for setting the socket non-blocking here" #endif - /* Create a session instance - */ - session = libssh2_session_init(); - if(!session) - return -1; + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; - /* ... start it up. This will trade welcome banners, exchange keys, - * and setup crypto, compression, and MAC layers - */ - rc = libssh2_session_startup(session, sock); - if(rc) { - fprintf(stderr, "Failure establishing SSH session: %d\n", rc); - return -1; - } + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + rc = libssh2_session_startup(session, sock); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; + } - libssh2_session_set_blocking(session, 0); + libssh2_session_set_blocking(session, 0); - /* 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++) { - printf("%02X ", (unsigned char)fingerprint[i]); - } - printf("\n"); + /* 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++) { + printf("%02X ", (unsigned char)fingerprint[i]); + } + printf("\n"); - if(argc > 1) { - username = argv[1]; - } - if(argc > 2) { - password = argv[2]; - } - if(argc > 3) { - sftppath = argv[3]; - } - if(argc > 4) { - dest = argv[4]; - } + if(argc > 1) { + username = argv[1]; + } + if(argc > 2) { + password = argv[2]; + } + if(argc > 3) { + sftppath = argv[3]; + } + if(argc > 4) { + dest = argv[4]; + } - tempstorage = fopen(STORAGE, "wb"); - if(!tempstorage) { - printf("Can't open temp storage file %s\n", STORAGE); - goto shutdown; + tempstorage = fopen(STORAGE, "wb"); + if(!tempstorage) { + printf("Can't open temp storage file %s\n", STORAGE); + goto shutdown; + } + + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + printf("Authentication by password failed.\n"); + goto shutdown; } - - if (auth_pw) { - /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { - printf("Authentication by password failed.\n"); - goto shutdown; - } - } else { - /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { - printf("\tAuthentication by public key failed\n"); - goto shutdown; - } + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + printf("\tAuthentication by public key failed\n"); + goto shutdown; } + } - sftp_session = libssh2_sftp_init(session); + sftp_session = libssh2_sftp_init(session); - if (!sftp_session) { - fprintf(stderr, "Unable to init SFTP session\n"); - goto shutdown; - } + if (!sftp_session) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } - /* Request a file via SFTP */ - sftp_handle = - libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); + /* Request a file via SFTP */ + sftp_handle = + libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); - if (!sftp_handle) { - fprintf(stderr, "Unable to open file with SFTP\n"); - goto shutdown; - } - fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); + if (!sftp_handle) { + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; + } + fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); + do { do { - do { - /* read in a loop until we block */ - rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); - fprintf(stderr, "libssh2_sftp_read returned %d\n", - rc); + /* read in a loop until we block */ + rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); + fprintf(stderr, "libssh2_sftp_read returned %d\n", + rc); - if(rc > 0) { - /* write to stderr */ - write(2, mem, rc); - /* write to temporary storage area */ - fwrite(mem, rc, 1, tempstorage); - } - } while (rc > 0); + if(rc > 0) { + /* write to stderr */ + write(2, mem, rc); + /* write to temporary storage area */ + fwrite(mem, rc, 1, tempstorage); + } + } while (rc > 0); - if(rc != LIBSSH2_ERROR_EAGAIN) { - /* error or end of file */ - break; - } + if(rc != LIBSSH2_ERROR_EAGAIN) { + /* error or end of file */ + break; + } - timeout.tv_sec = 10; - timeout.tv_usec = 0; + timeout.tv_sec = 10; + timeout.tv_usec = 0; - FD_ZERO(&fd); + FD_ZERO(&fd); - FD_SET(sock, &fd); + FD_SET(sock, &fd); - /* wait for readable or writeable */ - rc = select(sock+1, &fd, &fd, NULL, &timeout); - if(rc <= 0) { - /* negative is error - 0 is timeout */ - fprintf(stderr, "SFTP download timed out: %d\n", rc); - break; - } + /* wait for readable or writeable */ + rc = select(sock+1, &fd, &fd, NULL, &timeout); + if(rc <= 0) { + /* negative is error + 0 is timeout */ + fprintf(stderr, "SFTP download timed out: %d\n", rc); + break; + } + } while (1); + + libssh2_sftp_close(sftp_handle); + fclose(tempstorage); + + tempstorage = fopen(STORAGE, "rb"); + if(!tempstorage) { + /* weird, we can't read the file we just wrote to... */ + fprintf(stderr, "can't open %s for reading\n", STORAGE); + goto shutdown; + } + + /* we're done downloading, now reverse the process and upload the + temporarily stored data to the destination path */ + sftp_handle = + libssh2_sftp_open(sftp_session, dest, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + if(sftp_handle) { + size_t nread; + char *ptr; + do { + nread = fread(mem, 1, sizeof(mem), tempstorage); + if(nread <= 0) { + /* end of file */ + break; + } + ptr = mem; + + do { + /* write data in a loop until we block */ + rc = libssh2_sftp_write(sftp_handle, ptr, + nread); + ptr += rc; + nread -= nread; + } while (rc > 0); + + if(rc != LIBSSH2_ERROR_EAGAIN) { + /* error or end of file */ + break; + } + + timeout.tv_sec = 10; + timeout.tv_usec = 0; + + FD_ZERO(&fd); + + FD_SET(sock, &fd); + + /* wait for readable or writeable */ + rc = select(sock+1, &fd, &fd, NULL, &timeout); + if(rc <= 0) { + /* negative is error + 0 is timeout */ + fprintf(stderr, "SFTP upload timed out: %d\n", + rc); + break; + } } while (1); + fprintf(stderr, "SFTP upload done!\n"); + } + else { + fprintf(stderr, "SFTP failed to open destination path: %s\n", + dest); + } - libssh2_sftp_close(sftp_handle); - fclose(tempstorage); + libssh2_sftp_shutdown(sftp_session); - tempstorage = fopen(STORAGE, "rb"); - if(!tempstorage) { - /* weird, we can't read the file we just wrote to... */ - fprintf(stderr, "can't open %s for reading\n", STORAGE); - goto shutdown; - } + shutdown: - /* we're done downloading, now reverse the process and upload the - temporarily stored data to the destination path */ - sftp_handle = - libssh2_sftp_open(sftp_session, dest, - LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT, - LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| - LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); - if(sftp_handle) { - size_t nread; - char *ptr; - do { - nread = fread(mem, 1, sizeof(mem), tempstorage); - if(nread <= 0) { - /* end of file */ - break; - } - ptr = mem; - - do { - /* write data in a loop until we block */ - rc = libssh2_sftp_write(sftp_handle, ptr, - nread); - ptr += rc; - nread -= nread; - } while (rc > 0); - - if(rc != LIBSSH2_ERROR_EAGAIN) { - /* error or end of file */ - break; - } - - timeout.tv_sec = 10; - timeout.tv_usec = 0; - - FD_ZERO(&fd); - - FD_SET(sock, &fd); - - /* wait for readable or writeable */ - rc = select(sock+1, &fd, &fd, NULL, &timeout); - if(rc <= 0) { - /* negative is error - 0 is timeout */ - fprintf(stderr, "SFTP upload timed out: %d\n", - rc); - break; - } - } while (1); - fprintf(stderr, "SFTP upload done!\n"); - } - else { - fprintf(stderr, "SFTP failed to open destination path: %s\n", - dest); - } - - libssh2_sftp_shutdown(sftp_session); - - shutdown: - - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); - libssh2_session_free(session); + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); #ifdef WIN32 - Sleep(1000); - closesocket(sock); + Sleep(1000); + closesocket(sock); #else - sleep(1); - close(sock); + sleep(1); + close(sock); #endif -printf("all done\n"); - return 0; + printf("all done\n"); + return 0; } diff --git a/example/simple/sftp_mkdir.c b/example/simple/sftp_mkdir.c index ad46000..2cff47e 100644 --- a/example/simple/sftp_mkdir.c +++ b/example/simple/sftp_mkdir.c @@ -1,5 +1,5 @@ /* - * $Id: sftp_mkdir.c,v 1.4 2007/06/06 12:34:08 jehousley Exp $ + * $Id: sftp_mkdir.c,v 1.5 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SFTP mkdir * @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) if(argc > 4) { sftppath = argv[4]; } - + /* * The application code is responsible for creating the socket * and establishing the connection @@ -79,7 +79,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -139,7 +139,7 @@ int main(int argc, char *argv[]) /* Since we have not set non-blocking, tell libssh2 we are blocking */ libssh2_session_set_blocking(session, 1); - + fprintf(stderr, "libssh2_sftp_mkdir()!\n"); /* Make a directory via SFTP */ rc = libssh2_sftp_mkdir(sftp_session, sftppath, diff --git a/example/simple/sftp_mkdir_nonblock.c b/example/simple/sftp_mkdir_nonblock.c index de8774a..d35860a 100644 --- a/example/simple/sftp_mkdir_nonblock.c +++ b/example/simple/sftp_mkdir_nonblock.c @@ -1,5 +1,5 @@ /* - * $Id: sftp_mkdir_nonblock.c,v 1.6 2007/06/08 13:33:08 jehousley Exp $ + * $Id: sftp_mkdir_nonblock.c,v 1.7 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SFTP non-blocking mkdir. * @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) if(argc > 4) { sftppath = argv[4]; } - + /* * The application code is responsible for creating the socket * and establishing the connection @@ -79,7 +79,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; @@ -149,7 +149,7 @@ int main(int argc, char *argv[]) /* Since we have set non-blocking, tell libssh2 we are non-blocking */ libssh2_session_set_blocking(session, 0); - + fprintf(stderr, "libssh2_sftp_mkdirnb()!\n"); /* Make a directory via SFTP */ while ((rc = libssh2_sftp_mkdir(sftp_session, sftppath, diff --git a/example/simple/sftp_nonblock.c b/example/simple/sftp_nonblock.c index ab1f0cd..ce77933 100644 --- a/example/simple/sftp_nonblock.c +++ b/example/simple/sftp_nonblock.c @@ -1,5 +1,5 @@ /* - * $Id: sftp_nonblock.c,v 1.10 2007/06/08 13:33:08 jehousley Exp $ + * $Id: sftp_nonblock.c,v 1.11 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SFTP non-blocking transfers. * @@ -51,19 +51,19 @@ int main(int argc, char *argv[]) int rc; LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP_HANDLE *sftp_handle; - + #ifdef WIN32 WSADATA wsadata; - + WSAStartup(WINSOCK_VERSION, &wsadata); #endif - + if (argc > 1) { hostaddr = inet_addr(argv[1]); } else { hostaddr = htonl(0x7F000001); } - + if (argc > 2) { username = argv[2]; } @@ -78,7 +78,7 @@ int main(int argc, char *argv[]) * and establishing the connection */ sock = socket(AF_INET, SOCK_STREAM, 0); - + sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "failed to connect!\n"); return -1; } - + /* We set the socket non-blocking. We do it after the connect just to simplify the example code. */ #ifdef F_SETFL @@ -97,15 +97,15 @@ int main(int argc, char *argv[]) #else #error "add support for setting the socket non-blocking here" #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 */ libssh2_session_set_blocking(session, 0); - + /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ @@ -114,7 +114,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Failure establishing SSH session: %d\n", rc); 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 @@ -126,11 +126,11 @@ int main(int argc, char *argv[]) fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); } fprintf(stderr, "\n"); - + if (auth_pw) { /* We could authenticate via password */ while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if (rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } @@ -140,12 +140,12 @@ int main(int argc, char *argv[]) "/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa", password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + if (rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } } - + fprintf(stderr, "libssh2_sftp_init()!\n"); do { sftp_session = libssh2_sftp_init(session); @@ -155,22 +155,22 @@ int main(int argc, char *argv[]) goto shutdown; } } while (!sftp_session); - + fprintf(stderr, "libssh2_sftp_open()!\n"); /* Request a file via SFTP */ do { sftp_handle = libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); - + if ((!sftp_handle) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } } while (!sftp_handle); - + fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); do { char mem[1024]; - + /* loop until we fail */ fprintf(stderr, "libssh2_sftp_readnb()!\n"); while ((rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) { @@ -182,15 +182,15 @@ int main(int argc, char *argv[]) break; } } while (1); - + libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session); - + shutdown: - - while ((rc = libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN); + + while ((rc = libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); - + #ifdef WIN32 Sleep(1000); closesocket(sock); diff --git a/example/simple/sftp_write.c b/example/simple/sftp_write.c index e7d4281..f7c29fc 100644 --- a/example/simple/sftp_write.c +++ b/example/simple/sftp_write.c @@ -1,5 +1,5 @@ /* - * $Id: sftp_write.c,v 1.5 2007/07/14 20:54:47 bagder Exp $ + * $Id: sftp_write.c,v 1.6 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SFTP write transfers. * @@ -61,28 +61,28 @@ int main(int argc, char *argv[]) #endif if (argc > 1) { - hostaddr = inet_addr(argv[1]); + hostaddr = inet_addr(argv[1]); } else { - hostaddr = htonl(0x7F000001); + hostaddr = htonl(0x7F000001); } if(argc > 2) { - username = argv[2]; + username = argv[2]; } if(argc > 3) { - password = argv[3]; + password = argv[3]; } if(argc > 4) { - loclfile = argv[4]; + loclfile = argv[4]; } if(argc > 5) { - sftppath = argv[5]; + sftppath = argv[5]; } local = fopen(loclfile, "rb"); if (!local) { - printf("Can't local file %s\n", loclfile); - goto shutdown; + printf("Can't local file %s\n", loclfile); + goto shutdown; } /* @@ -95,16 +95,16 @@ int main(int argc, char *argv[]) sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); - return -1; + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; } /* Create a session instance */ session = libssh2_session_init(); if(!session) - return -1; + return -1; /* Since we have set non-blocking, tell libssh2 we are blocking */ libssh2_session_set_blocking(session, 1); @@ -114,8 +114,8 @@ int main(int argc, char *argv[]) */ rc = libssh2_session_startup(session, sock); if(rc) { - fprintf(stderr, "Failure establishing SSH session: %d\n", rc); - return -1; + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + return -1; } /* At this point we havn't yet authenticated. The first thing to do @@ -126,33 +126,33 @@ int main(int argc, char *argv[]) fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); printf("Fingerprint: "); for(i = 0; i < 16; i++) { - printf("%02X ", (unsigned char)fingerprint[i]); + printf("%02X ", (unsigned char)fingerprint[i]); } printf("\n"); if (auth_pw) { - /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { - printf("Authentication by password failed.\n"); - goto shutdown; - } + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + printf("Authentication by password failed.\n"); + goto shutdown; + } } else { - /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { - printf("\tAuthentication by public key failed\n"); - goto shutdown; - } + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) { + printf("\tAuthentication by public key failed\n"); + goto shutdown; + } } fprintf(stderr, "libssh2_sftp_init()!\n"); sftp_session = libssh2_sftp_init(session); if (!sftp_session) { - fprintf(stderr, "Unable to init SFTP session\n"); - goto shutdown; + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; } /* Since we have not set non-blocking, tell libssh2 we are blocking */ @@ -161,30 +161,30 @@ int main(int argc, char *argv[]) fprintf(stderr, "libssh2_sftp_open()!\n"); /* Request a file via SFTP */ sftp_handle = - libssh2_sftp_open(sftp_session, sftppath, - LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, - LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| - LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); + libssh2_sftp_open(sftp_session, sftppath, + LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, + LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| + LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); if (!sftp_handle) { - fprintf(stderr, "Unable to open file with SFTP\n"); - goto shutdown; + fprintf(stderr, "Unable to open file with SFTP\n"); + goto shutdown; } fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); do { - nread = fread(mem, 1, sizeof(mem), local); - if (nread <= 0) { - /* end of file */ - break; - } - ptr = mem; + nread = fread(mem, 1, sizeof(mem), local); + if (nread <= 0) { + /* end of file */ + break; + } + ptr = mem; - do { - /* write data in a loop until we block */ - rc = libssh2_sftp_write(sftp_handle, ptr, nread); - ptr += rc; - nread -= nread; - } while (rc > 0); + do { + /* write data in a loop until we block */ + rc = libssh2_sftp_write(sftp_handle, ptr, nread); + ptr += rc; + nread -= nread; + } while (rc > 0); } while (1); fclose(local); diff --git a/example/simple/sftp_write_nonblock.c b/example/simple/sftp_write_nonblock.c index e9fce97..1c12c42 100644 --- a/example/simple/sftp_write_nonblock.c +++ b/example/simple/sftp_write_nonblock.c @@ -1,5 +1,5 @@ /* - * $Id: sftp_write_nonblock.c,v 1.7 2007/06/08 13:33:08 jehousley Exp $ + * $Id: sftp_write_nonblock.c,v 1.8 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SFTP non-blocking write transfers. * @@ -53,19 +53,19 @@ int main(int argc, char *argv[]) char mem[1024]; size_t nread; char *ptr; - + #ifdef WIN32 WSADATA wsadata; - + WSAStartup(WINSOCK_VERSION, &wsadata); #endif - + if (argc > 1) { hostaddr = inet_addr(argv[1]); } else { hostaddr = htonl(0x7F000001); } - + if (argc > 2) { username = argv[2]; } @@ -78,28 +78,28 @@ int main(int argc, char *argv[]) if (argc > 5) { sftppath = argv[5]; } - + local = fopen(loclfile, "rb"); if (!local) { printf("Can't local file %s\n", loclfile); goto shutdown; } - + /* * The application code is responsible for creating the socket * and establishing the connection */ sock = socket(AF_INET, SOCK_STREAM, 0); - + sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), + if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "failed to connect!\n"); return -1; } - + /* We set the socket non-blocking. We do it after the connect just to simplify the example code. */ #ifdef F_SETFL @@ -109,26 +109,26 @@ int main(int argc, char *argv[]) #else #error "add support for setting the socket non-blocking here" #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 */ libssh2_session_set_blocking(session, 0); - + /* ... start it up. This will trade welcome banners, exchange keys, * and setup crypto, compression, and MAC layers */ - while ((rc = libssh2_session_startup(session, sock)) + while ((rc = libssh2_session_startup(session, sock)) == LIBSSH2_ERROR_EAGAIN); if (rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); 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 @@ -140,7 +140,7 @@ int main(int argc, char *argv[]) printf("%02X ", (unsigned char)fingerprint[i]); } printf("\n"); - + if (auth_pw) { /* We could authenticate via password */ while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); @@ -159,20 +159,20 @@ int main(int argc, char *argv[]) goto shutdown; } } - + fprintf(stderr, "libssh2_sftp_init()!\n"); do { sftp_session = libssh2_sftp_init(session); - + if ((!sftp_session) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown; } } while (!sftp_session); - + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ libssh2_session_set_blocking(session, 0); - + fprintf(stderr, "libssh2_sftp_open()!\n"); /* Request a file via SFTP */ do { @@ -181,13 +181,13 @@ int main(int argc, char *argv[]) LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); - + if ((!sftp_handle) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to open file with SFTP\n"); goto shutdown; } } while (!sftp_handle); - + fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); do { nread = fread(mem, 1, sizeof(mem), local); @@ -196,7 +196,7 @@ int main(int argc, char *argv[]) break; } ptr = mem; - + do { /* write data in a loop until we block */ while ((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) == LIBSSH2_ERROR_EAGAIN) { @@ -206,16 +206,16 @@ int main(int argc, char *argv[]) nread -= nread; } while (rc > 0); } while (1); - + fclose(local); libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session); - + shutdown: - + while ((rc = libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); - + #ifdef WIN32 Sleep(1000); closesocket(sock); diff --git a/example/simple/sftpdir.c b/example/simple/sftpdir.c index 068a666..8b85c6f 100644 --- a/example/simple/sftpdir.c +++ b/example/simple/sftpdir.c @@ -1,5 +1,5 @@ /* - * $Id: sftpdir.c,v 1.6 2007/06/15 17:22:49 jehousley Exp $ + * $Id: sftpdir.c,v 1.7 2007/07/14 21:24:38 bagder Exp $ * * Sample doing an SFTP directory listing. * @@ -139,7 +139,7 @@ int main(int argc, char *argv[]) /* Since we have not set non-blocking, tell libssh2 we are blocking */ libssh2_session_set_blocking(session, 1); - + fprintf(stderr, "libssh2_sftp_opendir()!\n"); /* Request a dir listing via SFTP */ sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); diff --git a/example/simple/sftpdir_nonblock.c b/example/simple/sftpdir_nonblock.c index 4646d0c..e175abd 100644 --- a/example/simple/sftpdir_nonblock.c +++ b/example/simple/sftpdir_nonblock.c @@ -1,5 +1,5 @@ /* - * $Id: sftpdir_nonblock.c,v 1.7 2007/06/08 13:33:08 jehousley Exp $ + * $Id: sftpdir_nonblock.c,v 1.8 2007/07/14 21:24:38 bagder Exp $ * * Sample doing an SFTP directory listing. * @@ -37,197 +37,201 @@ int main(int argc, char *argv[]) { - unsigned long hostaddr; - int sock, i, auth_pw = 1; - struct sockaddr_in sin; - const char *fingerprint; - LIBSSH2_SESSION *session; - char *username=(char *)"username"; - char *password=(char *)"password"; - char *sftppath=(char *)"/tmp/secretdir"; - int rc; - LIBSSH2_SFTP *sftp_session; - LIBSSH2_SFTP_HANDLE *sftp_handle; + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + char *username=(char *)"username"; + char *password=(char *)"password"; + char *sftppath=(char *)"/tmp/secretdir"; + int rc; + LIBSSH2_SFTP *sftp_session; + LIBSSH2_SFTP_HANDLE *sftp_handle; #ifdef WIN32 - WSADATA wsadata; + WSADATA wsadata; - WSAStartup(WINSOCK_VERSION, &wsadata); + WSAStartup(WINSOCK_VERSION, &wsadata); #endif - if (argc > 1) { - hostaddr = inet_addr(argv[1]); - } else { - hostaddr = htonl(0x7F000001); - } + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } - if(argc > 2) { - username = argv[2]; - } - if(argc > 3) { - password = argv[3]; - } - if(argc > 4) { - sftppath = argv[4]; - } - /* - * The application code is responsible for creating the socket - * and establishing the connection - */ - sock = socket(AF_INET, SOCK_STREAM, 0); + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } + if(argc > 4) { + sftppath = argv[4]; + } + /* + * The application code is responsible for creating the socket + * and establishing the connection + */ + sock = socket(AF_INET, SOCK_STREAM, 0); - sin.sin_family = AF_INET; - sin.sin_port = htons(22); - sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); - return -1; - } + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } - /* We set the socket non-blocking. We do it after the connect just to - simplify the example code. */ + /* 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); + /* FIXME: this can/should be done in a more portable manner */ + rc = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, rc | O_NONBLOCK); #else #error "add support for setting the socket non-blocking here" #endif - /* Create a session instance - */ - session = libssh2_session_init(); - if(!session) - return -1; + /* Create a session instance + */ + session = libssh2_session_init(); + if(!session) + return -1; - /* Since we have set non-blocking, tell libssh2 we are non-blocking */ - libssh2_session_set_blocking(session, 0); - - /* ... start it up. This will trade welcome banners, exchange keys, - * and setup crypto, compression, and MAC layers - */ - while ((rc = libssh2_session_startup(session, sock)) == LIBSSH2_ERROR_EAGAIN); - if(rc) { - fprintf(stderr, "Failure establishing SSH session: %d\n", rc); - return -1; - } + /* Since we have set non-blocking, tell libssh2 we are non-blocking */ + libssh2_session_set_blocking(session, 0); - /* 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++) { - printf("%02X ", (unsigned char)fingerprint[i]); - } - printf("\n"); + /* ... start it up. This will trade welcome banners, exchange keys, + * and setup crypto, compression, and MAC layers + */ + while ((rc = libssh2_session_startup(session, sock)) == LIBSSH2_ERROR_EAGAIN); + if(rc) { + fprintf(stderr, "Failure establishing SSH session: %d\n", rc); + 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 + */ + fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); + printf("Fingerprint: "); + for(i = 0; i < 16; i++) { + printf("%02X ", (unsigned char)fingerprint[i]); + } + printf("\n"); if (auth_pw) { /* We could authenticate via password */ - while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + while ((rc = libssh2_userauth_password(session, username, password)) == + LIBSSH2_ERROR_EAGAIN); + if (rc) { fprintf(stderr, "Authentication by password failed.\n"); goto shutdown; } } else { /* Or by public key */ while ((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) == LIBSSH2_ERROR_EAGAIN); - if (rc) { + "/home/username/.ssh/id_rsa.pub", + "/home/username/.ssh/id_rsa", + password)) == LIBSSH2_ERROR_EAGAIN); + if (rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } } - fprintf(stderr, "libssh2_sftp_init()!\n"); - do { - sftp_session = libssh2_sftp_init(session); - - if ((!sftp_session) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { - fprintf(stderr, "Unable to init SFTP session\n"); - goto shutdown; + fprintf(stderr, "libssh2_sftp_init()!\n"); + do { + sftp_session = libssh2_sftp_init(session); + + if ((!sftp_session) && (libssh2_session_last_errno(session) != + LIBSSH2_ERROR_EAGAIN)) { + fprintf(stderr, "Unable to init SFTP session\n"); + goto shutdown; + } + } while (!sftp_session); + + fprintf(stderr, "libssh2_sftp_opendir()!\n"); + /* Request a dir listing via SFTP */ + do { + sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); + + if ((!sftp_handle) && (libssh2_session_last_errno(session) != + LIBSSH2_ERROR_EAGAIN)) { + fprintf(stderr, "Unable to open dir with SFTP\n"); + goto shutdown; + } + } while (!sftp_handle); + + fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n"); + do { + char mem[512]; + LIBSSH2_SFTP_ATTRIBUTES attrs; + + /* loop until we fail */ + while ((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem), + &attrs)) == LIBSSH2_ERROR_EAGAIN) { + ; + } + if(rc > 0) { + /* rc is the length of the file name in the mem + buffer */ + + if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { + /* this should check what permissions it + is and print the output accordingly */ + printf("--fix----- "); + } else { + printf("---------- "); } - } while (!sftp_session); - - fprintf(stderr, "libssh2_sftp_opendir()!\n"); - /* Request a dir listing via SFTP */ - do { - sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); - - if ((!sftp_handle) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { - fprintf(stderr, "Unable to open dir with SFTP\n"); - goto shutdown; + + if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) { + printf("%4ld %4ld ", attrs.uid, attrs.gid); + } else { + printf(" - - "); } - } while (!sftp_handle); - - fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n"); - do { - char mem[512]; - LIBSSH2_SFTP_ATTRIBUTES attrs; - /* loop until we fail */ - while ((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem), &attrs)) == LIBSSH2_ERROR_EAGAIN) { - ; - } - if(rc > 0) { - /* rc is the length of the file name in the mem - buffer */ + if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) { + /* attrs.filesize is an uint64_t according to + the docs but there is no really good and + portable 64bit type for C before C99, and + correspondingly there was no good printf() + option for it... */ - if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { - /* this should check what permissions it - is and print the output accordingly */ - printf("--fix----- "); - } else { - printf("---------- "); - } + printf("%8lld ", attrs.filesize); + } - if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) { - printf("%4ld %4ld ", attrs.uid, attrs.gid); - } else { - printf(" - - "); - } + printf("%s\n", mem); + } + else if (rc == LIBSSH2_ERROR_EAGAIN) { + /* blocking */ + fprintf(stderr, "Blocking\n"); + } else { + break; + } - if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) { - /* attrs.filesize is an uint64_t according to - the docs but there is no really good and - portable 64bit type for C before C99, and - correspondingly there was no good printf() - option for it... */ + } while (1); - printf("%8lld ", attrs.filesize); - } + libssh2_sftp_closedir(sftp_handle); + libssh2_sftp_shutdown(sftp_session); - printf("%s\n", mem); - } - else if (rc == LIBSSH2_ERROR_EAGAIN) { - /* blocking */ - fprintf(stderr, "Blocking\n"); - } else { - break; - } + shutdown: - } while (1); - - libssh2_sftp_closedir(sftp_handle); - libssh2_sftp_shutdown(sftp_session); - - shutdown: - - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); - libssh2_session_free(session); + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); #ifdef WIN32 - Sleep(1000); - closesocket(sock); + Sleep(1000); + closesocket(sock); #else - sleep(1); - close(sock); + sleep(1); + close(sock); #endif -printf("all done\n"); - return 0; + printf("all done\n"); + return 0; } diff --git a/example/simple/ssh2.c b/example/simple/ssh2.c index 706ba66..fe94420 100644 --- a/example/simple/ssh2.c +++ b/example/simple/ssh2.c @@ -1,5 +1,5 @@ /* - * $Id: ssh2.c,v 1.4 2007/07/14 20:54:47 bagder Exp $ + * $Id: ssh2.c,v 1.5 2007/07/14 21:24:38 bagder Exp $ * * Sample showing how to do SSH2 connect. * @@ -37,145 +37,145 @@ int main(int argc, char *argv[]) { - unsigned long hostaddr; - int sock, i, auth_pw = 1; - struct sockaddr_in sin; - const char *fingerprint; - LIBSSH2_SESSION *session; - LIBSSH2_CHANNEL *channel; - char *username=(char *)"username"; - char *password=(char *)"password"; + unsigned long hostaddr; + int sock, i, auth_pw = 1; + struct sockaddr_in sin; + const char *fingerprint; + LIBSSH2_SESSION *session; + LIBSSH2_CHANNEL *channel; + char *username=(char *)"username"; + char *password=(char *)"password"; #ifdef WIN32 - WSADATA wsadata; + WSADATA wsadata; - WSAStartup(WINSOCK_VERSION, &wsadata); + WSAStartup(WINSOCK_VERSION, &wsadata); #endif - if (argc > 1) { - hostaddr = inet_addr(argv[1]); - } else { - hostaddr = htonl(0x7F000001); - } + if (argc > 1) { + hostaddr = inet_addr(argv[1]); + } else { + hostaddr = htonl(0x7F000001); + } - if(argc > 2) { - username = argv[2]; - } - if(argc > 3) { - password = argv[3]; - } + if(argc > 2) { + username = argv[2]; + } + if(argc > 3) { + password = argv[3]; + } - /* 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); + /* 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); + fcntl(sock, F_SETFL, 0); #endif - sin.sin_family = AF_INET; - sin.sin_port = htons(22); - sin.sin_addr.s_addr = hostaddr; - if (connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); - return -1; - } + sin.sin_family = AF_INET; + sin.sin_port = htons(22); + sin.sin_addr.s_addr = hostaddr; + if (connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in)) != 0) { + fprintf(stderr, "failed to connect!\n"); + return -1; + } - /* 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)) { - fprintf(stderr, "Failure establishing SSH session\n"); - return -1; - } + /* 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)) { + fprintf(stderr, "Failure establishing SSH session\n"); + 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 - */ - fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); - printf("Fingerprint: "); - for(i = 0; i < 16; i++) { - printf("%02X ", (unsigned char)fingerprint[i]); - } - printf("\n"); + /* 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: "); + for(i = 0; i < 16; i++) { + printf("%02X ", (unsigned char)fingerprint[i]); + } + printf("\n"); - if (auth_pw) { - /* We could authenticate via password */ - if (libssh2_userauth_password(session, username, password)) { - printf("Authentication by password failed.\n"); - goto shutdown; - } - } else { - /* Or by public key */ - if (libssh2_userauth_publickey_fromfile(session, username, "/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa", password)) { - printf("\tAuthentication by public key failed\n"); - goto shutdown; - } - } + if (auth_pw) { + /* We could authenticate via password */ + if (libssh2_userauth_password(session, username, password)) { + printf("Authentication by password failed.\n"); + goto shutdown; + } + } else { + /* Or by public key */ + if (libssh2_userauth_publickey_fromfile(session, username, "/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa", password)) { + printf("\tAuthentication by public key failed\n"); + goto shutdown; + } + } - /* Request a shell */ - if (!(channel = libssh2_channel_open_session(session))) { - fprintf(stderr, "Unable to open a session\n"); - goto shutdown; - } + /* Request a shell */ + if (!(channel = libssh2_channel_open_session(session))) { + fprintf(stderr, "Unable to open a session\n"); + goto shutdown; + } - /* Some environment variables may be set, - * It's up to the server which ones it'll allow though - */ - libssh2_channel_setenv(channel, (char *)"FOO", (char *)"bar"); + /* Some environment variables may be set, + * It's up to the server which ones it'll allow though + */ + libssh2_channel_setenv(channel, (char *)"FOO", (char *)"bar"); - /* Request a terminal with 'vanilla' terminal emulation - * See /etc/termcap for more options - */ - if (libssh2_channel_request_pty(channel, (char *)"vanilla")) { - fprintf(stderr, "Failed requesting pty\n"); - goto skip_shell; - } + /* Request a terminal with 'vanilla' terminal emulation + * See /etc/termcap for more options + */ + if (libssh2_channel_request_pty(channel, (char *)"vanilla")) { + fprintf(stderr, "Failed requesting pty\n"); + goto skip_shell; + } - /* Open a SHELL on that pty */ - if (libssh2_channel_shell(channel)) { - fprintf(stderr, "Unable to request shell on allocated pty\n"); - goto shutdown; - } + /* Open a SHELL on that pty */ + if (libssh2_channel_shell(channel)) { + fprintf(stderr, "Unable to request shell on allocated pty\n"); + goto shutdown; + } - /* At this point the shell can be interacted with using - * libssh2_channel_read() - * libssh2_channel_read_stderr() - * libssh2_channel_write() - * libssh2_channel_write_stderr() - * - * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking() - * If the server send EOF, libssh2_channel_eof() will return non-0 - * To send EOF to the server use: libssh2_channel_send_eof() - * A channel can be closed with: libssh2_channel_close() - * A channel can be freed with: libssh2_channel_free() - */ + /* At this point the shell can be interacted with using + * libssh2_channel_read() + * libssh2_channel_read_stderr() + * libssh2_channel_write() + * libssh2_channel_write_stderr() + * + * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking() + * If the server send EOF, libssh2_channel_eof() will return non-0 + * To send EOF to the server use: libssh2_channel_send_eof() + * A channel can be closed with: libssh2_channel_close() + * A channel can be freed with: libssh2_channel_free() + */ - skip_shell: - if (channel) { - libssh2_channel_free(channel); - channel = NULL; - } + skip_shell: + if (channel) { + libssh2_channel_free(channel); + channel = NULL; + } - /* Other channel types are supported via: - * libssh2_scp_send() - * libssh2_scp_recv() - * libssh2_channel_direct_tcpip() - */ + /* Other channel types are supported via: + * libssh2_scp_send() + * libssh2_scp_recv() + * libssh2_channel_direct_tcpip() + */ - shutdown: + shutdown: - libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); - libssh2_session_free(session); + libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); + libssh2_session_free(session); #ifdef WIN32 - Sleep(1000); - closesocket(sock); + Sleep(1000); + closesocket(sock); #else - sleep(1); - close(sock); + sleep(1); + close(sock); #endif -printf("all done\n"); - return 0; + printf("all done\n"); + return 0; }