2
* $Id: sftp_nonblock.c,v 1.18 2009/04/28 10:35:30 bagder Exp $
4
* Sample showing how to do SFTP non-blocking transfers.
6
* The sample code has default values for host name, user name, password
7
* and path to copy, but you can specify them on the command line like:
9
* "sftp_nonblock 192.168.0.1 user password /tmp/secrets"
12
#include "libssh2_config.h"
14
#include <libssh2_sftp.h>
16
#ifdef HAVE_WINSOCK2_H
17
# include <winsock2.h>
19
#ifdef HAVE_SYS_SOCKET_H
20
# include <sys/socket.h>
22
#ifdef HAVE_NETINET_IN_H
23
# include <netinet/in.h>
25
#ifdef HAVE_SYS_SELECT_H
26
# include <sys/select.h>
31
#ifdef HAVE_ARPA_INET_H
32
# include <arpa/inet.h>
34
#ifdef HAVE_SYS_TIME_H
35
# include <sys/time.h>
38
#include <sys/types.h>
45
static long tvdiff(struct timeval newer, struct timeval older)
47
return (newer.tv_sec-older.tv_sec)*1000+
48
(newer.tv_usec-older.tv_usec)/1000;
51
static int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
53
struct timeval timeout;
56
fd_set *writefd = NULL;
57
fd_set *readfd = NULL;
65
FD_SET(socket_fd, &fd);
67
/* now make sure we wait in the correct direction */
68
dir = libssh2_session_block_directions(session);
70
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
73
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
76
rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
81
int main(int argc, char *argv[])
83
unsigned long hostaddr;
84
int sock, i, auth_pw = 1;
85
struct sockaddr_in sin;
86
const char *fingerprint;
87
LIBSSH2_SESSION *session;
88
const char *username="username";
89
const char *password="password";
90
const char *sftppath="/tmp/TEST";
97
#if defined(HAVE_IOCTLSOCKET)
100
LIBSSH2_SFTP *sftp_session;
101
LIBSSH2_SFTP_HANDLE *sftp_handle;
106
WSAStartup(MAKEWORD(2,0), &wsadata);
110
hostaddr = inet_addr(argv[1]);
112
hostaddr = htonl(0x7F000001);
125
* The application code is responsible for creating the socket
126
* and establishing the connection
128
sock = socket(AF_INET, SOCK_STREAM, 0);
130
sin.sin_family = AF_INET;
131
sin.sin_port = htons(22);
132
sin.sin_addr.s_addr = hostaddr;
133
if (connect(sock, (struct sockaddr*)(&sin),
134
sizeof(struct sockaddr_in)) != 0) {
135
fprintf(stderr, "failed to connect!\n");
139
/* Create a session instance */
140
session = libssh2_session_init();
144
/* Since we have set non-blocking, tell libssh2 we are non-blocking */
145
libssh2_session_set_blocking(session, 0);
147
gettimeofday(&start, NULL);
149
/* ... start it up. This will trade welcome banners, exchange keys,
150
* and setup crypto, compression, and MAC layers
152
while ((rc = libssh2_session_startup(session, sock)) ==
153
LIBSSH2_ERROR_EAGAIN);
155
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
159
/* At this point we havn't yet authenticated. The first thing to do
160
* is check the hostkey's fingerprint against our known hosts Your app
161
* may have it hard coded, may go to a file, may present it to the
162
* user, that's your call
164
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
165
fprintf(stderr, "Fingerprint: ");
166
for(i = 0; i < 16; i++) {
167
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
169
fprintf(stderr, "\n");
172
/* We could authenticate via password */
173
while ((rc = libssh2_userauth_password(session, username, password))
174
== LIBSSH2_ERROR_EAGAIN);
176
fprintf(stderr, "Authentication by password failed.\n");
180
/* Or by public key */
182
libssh2_userauth_publickey_fromfile(session, username,
188
LIBSSH2_ERROR_EAGAIN);
190
fprintf(stderr, "\tAuthentication by public key failed\n");
195
libssh2_trace(session, LIBSSH2_TRACE_CONN);
197
fprintf(stderr, "libssh2_sftp_init()!\n");
199
sftp_session = libssh2_sftp_init(session);
202
if(libssh2_session_last_errno(session) ==
203
LIBSSH2_ERROR_EAGAIN) {
204
fprintf(stderr, "non-blocking init\n");
205
waitsocket(sock, session); /* now we wait */
208
fprintf(stderr, "Unable to init SFTP session\n");
212
} while (!sftp_session);
214
fprintf(stderr, "libssh2_sftp_open()!\n");
215
/* Request a file via SFTP */
217
sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
218
LIBSSH2_FXF_READ, 0);
221
if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
222
fprintf(stderr, "Unable to open file with SFTP\n");
226
fprintf(stderr, "non-blocking open\n");
227
waitsocket(sock, session); /* now we wait */
230
} while (!sftp_handle);
232
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
236
/* loop until we fail */
237
while ((rc = libssh2_sftp_read(sftp_handle, mem,
238
sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) {
240
waitsocket(sock, session); /* now we wait */
250
gettimeofday(&end, NULL);
251
time_ms = tvdiff(end, start);
252
printf("Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total,
253
time_ms, total/(time_ms/1000.0), spin );
255
libssh2_sftp_close(sftp_handle);
256
libssh2_sftp_shutdown(sftp_session);
260
printf("libssh2_session_disconnect\n");
261
while ((rc = libssh2_session_disconnect(session,
262
"Normal Shutdown, Thank you")) ==
263
LIBSSH2_ERROR_EAGAIN);
264
libssh2_session_free(session);
271
fprintf(stderr, "all done\n");