13
#include "libssh2_setup.h"
15
#include <libssh2_sftp.h>
18
#define write(f, b, c) write((f), (b), (unsigned int)(c))
21
#ifdef HAVE_SYS_SOCKET_H
22
#include <sys/socket.h>
27
#ifdef HAVE_NETINET_IN_H
28
#include <netinet/in.h>
30
#ifdef HAVE_ARPA_INET_H
39
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
40
static const char *privkey = "/home/username/.ssh/id_rsa";
41
static const char *username = "username";
42
static const char *password = "password";
43
static const char *sftppath = "/tmp/TEST";
45
#ifdef HAVE_GETTIMEOFDAY
47
static long tvdiff(struct timeval newer, struct timeval older)
49
return (newer.tv_sec - older.tv_sec) * 1000 +
50
(newer.tv_usec - older.tv_usec) / 1000;
54
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
56
struct timeval timeout;
59
fd_set *writefd = NULL;
60
fd_set *readfd = NULL;
69
#pragma GCC diagnostic push
70
#pragma GCC diagnostic ignored "-Wsign-conversion"
72
FD_SET(socket_fd, &fd);
74
#pragma GCC diagnostic pop
78
dir = libssh2_session_block_directions(session);
80
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
83
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
86
rc = select((int)(socket_fd + 1), readfd, writefd, NULL, &timeout);
91
int main(int argc, char *argv[])
94
libssh2_socket_t sock;
96
struct sockaddr_in sin;
97
const char *fingerprint;
99
LIBSSH2_SESSION *session = NULL;
100
LIBSSH2_SFTP *sftp_session;
101
LIBSSH2_SFTP_HANDLE *sftp_handle;
102
#ifdef HAVE_GETTIMEOFDAY
103
struct timeval start;
107
libssh2_struct_stat_size total = 0;
113
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
115
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
121
hostaddr = inet_addr(argv[1]);
124
hostaddr = htonl(0x7F000001);
136
rc = libssh2_init(0);
138
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
146
sock = socket(AF_INET, SOCK_STREAM, 0);
147
if(sock == LIBSSH2_INVALID_SOCKET) {
148
fprintf(stderr, "failed to create socket.\n");
152
sin.sin_family = AF_INET;
153
sin.sin_port = htons(22);
154
sin.sin_addr.s_addr = hostaddr;
155
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
156
fprintf(stderr, "failed to connect.\n");
161
session = libssh2_session_init();
163
fprintf(stderr, "Could not initialize SSH session.\n");
168
libssh2_session_set_blocking(session, 0);
170
#ifdef HAVE_GETTIMEOFDAY
171
gettimeofday(&start, NULL);
177
while((rc = libssh2_session_handshake(session, sock)) ==
178
LIBSSH2_ERROR_EAGAIN);
180
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
189
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
190
fprintf(stderr, "Fingerprint: ");
191
for(i = 0; i < 20; i++) {
192
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
194
fprintf(stderr, "\n");
198
while((rc = libssh2_userauth_password(session, username, password)) ==
199
LIBSSH2_ERROR_EAGAIN);
201
fprintf(stderr, "Authentication by password failed.\n");
208
libssh2_userauth_publickey_fromfile(session, username,
211
LIBSSH2_ERROR_EAGAIN);
213
fprintf(stderr, "Authentication by public key failed.\n");
218
libssh2_trace(session, LIBSSH2_TRACE_CONN);
220
fprintf(stderr, "libssh2_sftp_init().\n");
222
sftp_session = libssh2_sftp_init(session);
225
if(libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) {
226
fprintf(stderr, "non-blocking init\n");
227
waitsocket(sock, session);
230
fprintf(stderr, "Unable to init SFTP session\n");
234
} while(!sftp_session);
236
fprintf(stderr, "libssh2_sftp_open().\n");
239
sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
240
LIBSSH2_FXF_READ, 0);
242
if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
243
fprintf(stderr, "Unable to open file with SFTP: %ld\n",
244
libssh2_sftp_last_error(sftp_session));
248
fprintf(stderr, "non-blocking open\n");
249
waitsocket(sock, session);
252
} while(!sftp_handle);
254
fprintf(stderr, "libssh2_sftp_open() is done, now receive data.\n");
260
while((nread = libssh2_sftp_read(sftp_handle, mem, sizeof(mem))) ==
261
LIBSSH2_ERROR_EAGAIN) {
263
waitsocket(sock, session);
267
write(1, mem, (size_t)nread);
274
#ifdef HAVE_GETTIMEOFDAY
275
gettimeofday(&end, NULL);
276
time_ms = tvdiff(end, start);
277
fprintf(stderr, "Got %ld bytes in %ld ms = %.1f bytes/sec spin: %d\n",
278
(long)total, time_ms,
279
(double)total / ((double)time_ms / 1000.0), spin);
281
fprintf(stderr, "Got %ld bytes spin: %d\n", (long)total, spin);
284
libssh2_sftp_close(sftp_handle);
285
libssh2_sftp_shutdown(sftp_session);
290
fprintf(stderr, "libssh2_session_disconnect\n");
291
while(libssh2_session_disconnect(session, "Normal Shutdown") ==
292
LIBSSH2_ERROR_EAGAIN);
293
libssh2_session_free(session);
296
if(sock != LIBSSH2_INVALID_SOCKET) {
298
LIBSSH2_SOCKET_CLOSE(sock);
301
fprintf(stderr, "all done\n");