13
#include "libssh2_setup.h"
15
#include <libssh2_sftp.h>
17
#ifdef HAVE_SYS_SOCKET_H
18
#include <sys/socket.h>
23
#ifdef HAVE_NETINET_IN_H
24
#include <netinet/in.h>
26
#ifdef HAVE_ARPA_INET_H
36
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
37
static const char *privkey = "/home/username/.ssh/id_rsa";
38
static const char *username = "username";
39
static const char *password = "password";
40
static const char *loclfile = "sftp_write_nonblock.c";
41
static const char *sftppath = "/tmp/sftp_write_nonblock.c";
43
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
45
struct timeval timeout;
48
fd_set *writefd = NULL;
49
fd_set *readfd = NULL;
58
#pragma GCC diagnostic push
59
#pragma GCC diagnostic ignored "-Wsign-conversion"
61
FD_SET(socket_fd, &fd);
63
#pragma GCC diagnostic pop
67
dir = libssh2_session_block_directions(session);
69
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
72
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
75
rc = select((int)(socket_fd + 1), readfd, writefd, NULL, &timeout);
80
int main(int argc, char *argv[])
83
libssh2_socket_t sock;
85
struct sockaddr_in sin;
86
const char *fingerprint;
88
LIBSSH2_SESSION *session = NULL;
89
LIBSSH2_SFTP *sftp_session;
90
LIBSSH2_SFTP_HANDLE *sftp_handle;
97
libssh2_struct_stat_size total = 0;
103
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
105
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
111
hostaddr = inet_addr(argv[1]);
114
hostaddr = htonl(0x7F000001);
129
rc = libssh2_init(0);
131
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
135
local = fopen(loclfile, "rb");
137
fprintf(stderr, "Cannot open local file %s\n", loclfile);
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);
172
while((rc = libssh2_session_handshake(session, sock)) ==
173
LIBSSH2_ERROR_EAGAIN);
175
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
184
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
185
fprintf(stderr, "Fingerprint: ");
186
for(i = 0; i < 20; i++) {
187
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
189
fprintf(stderr, "\n");
193
while((rc = libssh2_userauth_password(session, username, password)) ==
194
LIBSSH2_ERROR_EAGAIN);
196
fprintf(stderr, "Authentication by password failed.\n");
202
while((rc = libssh2_userauth_publickey_fromfile(session, username,
205
LIBSSH2_ERROR_EAGAIN);
207
fprintf(stderr, "Authentication by public key failed.\n");
212
fprintf(stderr, "libssh2_sftp_init().\n");
214
sftp_session = libssh2_sftp_init(session);
217
libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
218
fprintf(stderr, "Unable to init SFTP session\n");
221
} while(!sftp_session);
223
fprintf(stderr, "libssh2_sftp_open().\n");
226
sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
230
LIBSSH2_SFTP_S_IRUSR |
231
LIBSSH2_SFTP_S_IWUSR |
232
LIBSSH2_SFTP_S_IRGRP |
233
LIBSSH2_SFTP_S_IROTH);
235
libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
236
fprintf(stderr, "Unable to open file with SFTP: %ld\n",
237
libssh2_sftp_last_error(sftp_session));
240
} while(!sftp_handle);
242
fprintf(stderr, "libssh2_sftp_open() is done, now send data.\n");
245
nread = fread(mem, 1, sizeof(mem), local);
252
total += (libssh2_struct_stat_size)nread;
256
while((nwritten = libssh2_sftp_write(sftp_handle, ptr, nread)) ==
257
LIBSSH2_ERROR_EAGAIN) {
258
waitsocket(sock, session);
263
nread -= (size_t)nwritten;
265
} while(nwritten > 0);
267
duration = (int)(time(NULL) - start);
269
fprintf(stderr, "%ld bytes in %d seconds makes %.1f bytes/sec\n",
270
(long)total, duration, (double)total / duration);
273
libssh2_sftp_close(sftp_handle);
274
libssh2_sftp_shutdown(sftp_session);
279
while(libssh2_session_disconnect(session, "Normal Shutdown") ==
280
LIBSSH2_ERROR_EAGAIN);
281
libssh2_session_free(session);
284
if(sock != LIBSSH2_INVALID_SOCKET) {
286
LIBSSH2_SOCKET_CLOSE(sock);
289
fprintf(stderr, "all done\n");