13
#include "libssh2_setup.h"
17
#define write(f, b, c) write((f), (b), (unsigned int)(c))
20
#ifdef HAVE_SYS_SOCKET_H
21
#include <sys/socket.h>
26
#ifdef HAVE_NETINET_IN_H
27
#include <netinet/in.h>
29
#ifdef HAVE_ARPA_INET_H
38
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
39
static const char *privkey = "/home/username/.ssh/id_rsa";
40
static const char *username = "username";
41
static const char *password = "password";
42
static const char *scppath = "/tmp/TEST";
44
#ifdef HAVE_GETTIMEOFDAY
46
static long tvdiff(struct timeval newer, struct timeval older)
48
return (newer.tv_sec - older.tv_sec) * 1000 +
49
(newer.tv_usec - older.tv_usec) / 1000;
53
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
55
struct timeval timeout;
58
fd_set *writefd = NULL;
59
fd_set *readfd = NULL;
68
#pragma GCC diagnostic push
69
#pragma GCC diagnostic ignored "-Wsign-conversion"
71
FD_SET(socket_fd, &fd);
73
#pragma GCC diagnostic pop
77
dir = libssh2_session_block_directions(session);
79
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
82
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
85
rc = select((int)(socket_fd + 1), readfd, writefd, NULL, &timeout);
90
int main(int argc, char *argv[])
93
libssh2_socket_t sock;
95
struct sockaddr_in sin;
96
const char *fingerprint;
98
LIBSSH2_SESSION *session = NULL;
99
LIBSSH2_CHANNEL *channel;
100
libssh2_struct_stat fileinfo;
101
#ifdef HAVE_GETTIMEOFDAY
102
struct timeval start;
107
libssh2_struct_stat_size got = 0;
108
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);
145
sock = socket(AF_INET, SOCK_STREAM, 0);
146
if(sock == LIBSSH2_INVALID_SOCKET) {
147
fprintf(stderr, "failed to create socket.\n");
151
sin.sin_family = AF_INET;
152
sin.sin_port = htons(22);
153
sin.sin_addr.s_addr = hostaddr;
154
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
155
fprintf(stderr, "failed to connect.\n");
160
session = libssh2_session_init();
162
fprintf(stderr, "Could not initialize SSH session.\n");
167
libssh2_session_set_blocking(session, 0);
169
#ifdef HAVE_GETTIMEOFDAY
170
gettimeofday(&start, NULL);
176
while((rc = libssh2_session_handshake(session, sock)) ==
177
LIBSSH2_ERROR_EAGAIN);
179
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
188
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
189
fprintf(stderr, "Fingerprint: ");
190
for(i = 0; i < 20; i++) {
191
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
193
fprintf(stderr, "\n");
197
while((rc = libssh2_userauth_password(session, username, password)) ==
198
LIBSSH2_ERROR_EAGAIN);
200
fprintf(stderr, "Authentication by password failed.\n");
206
while((rc = libssh2_userauth_publickey_fromfile(session, username,
209
LIBSSH2_ERROR_EAGAIN);
211
fprintf(stderr, "Authentication by public key failed.\n");
217
libssh2_trace(session, LIBSSH2_TRACE_CONN);
221
fprintf(stderr, "libssh2_scp_recv2().\n");
223
channel = libssh2_scp_recv2(session, scppath, &fileinfo);
226
if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
229
libssh2_session_last_error(session, &err_msg, NULL, 0);
230
fprintf(stderr, "%s\n", err_msg);
234
fprintf(stderr, "libssh2_scp_recv2() spin\n");
235
waitsocket(sock, session);
239
fprintf(stderr, "libssh2_scp_recv2() is done, now receive data.\n");
241
while(got < fileinfo.st_size) {
246
int amount = sizeof(mem);
248
if((fileinfo.st_size - got) < amount) {
249
amount = (int)(fileinfo.st_size - got);
253
nread = libssh2_channel_read(channel, mem, (size_t)amount);
255
write(1, mem, (size_t)nread);
261
if(nread == LIBSSH2_ERROR_EAGAIN && got < fileinfo.st_size) {
266
waitsocket(sock, session);
272
#ifdef HAVE_GETTIMEOFDAY
273
gettimeofday(&end, NULL);
275
time_ms = tvdiff(end, start);
276
fprintf(stderr, "Got %ld bytes in %ld ms = %.1f bytes/sec spin: %d\n",
277
(long)total, time_ms,
278
(double)total / ((double)time_ms / 1000.0), spin);
280
fprintf(stderr, "Got %ld bytes spin: %d\n", (long)total, spin);
283
libssh2_channel_free(channel);
289
libssh2_session_disconnect(session, "Normal Shutdown");
290
libssh2_session_free(session);
293
if(sock != LIBSSH2_INVALID_SOCKET) {
295
LIBSSH2_SOCKET_CLOSE(sock);
298
fprintf(stderr, "all done\n");