8
#include "libssh2_setup.h"
11
#ifdef HAVE_SYS_SOCKET_H
12
#include <sys/socket.h>
17
#ifdef HAVE_NETINET_IN_H
18
#include <netinet/in.h>
20
#ifdef HAVE_ARPA_INET_H
30
static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
31
static const char *privkey = "/home/username/.ssh/id_rsa";
32
static const char *username = "username";
33
static const char *password = "password";
34
static const char *loclfile = "scp_write.c";
35
static const char *scppath = "/tmp/TEST";
37
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
39
struct timeval timeout;
42
fd_set *writefd = NULL;
43
fd_set *readfd = NULL;
52
#pragma GCC diagnostic push
53
#pragma GCC diagnostic ignored "-Wsign-conversion"
55
FD_SET(socket_fd, &fd);
57
#pragma GCC diagnostic pop
61
dir = libssh2_session_block_directions(session);
63
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
66
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
69
rc = select((int)(socket_fd + 1), readfd, writefd, NULL, &timeout);
74
int main(int argc, char *argv[])
77
libssh2_socket_t sock;
79
struct sockaddr_in sin;
80
const char *fingerprint;
82
LIBSSH2_SESSION *session = NULL;
83
LIBSSH2_CHANNEL *channel;
90
libssh2_struct_stat_size total = 0;
97
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
99
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
105
hostaddr = inet_addr(argv[1]);
108
hostaddr = htonl(0x7F000001);
123
rc = libssh2_init(0);
125
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
129
local = fopen(loclfile, "rb");
131
fprintf(stderr, "Cannot open local file %s\n", loclfile);
135
stat(loclfile, &fileinfo);
140
sock = socket(AF_INET, SOCK_STREAM, 0);
141
if(sock == LIBSSH2_INVALID_SOCKET) {
142
fprintf(stderr, "failed to create socket.\n");
146
sin.sin_family = AF_INET;
147
sin.sin_port = htons(22);
148
sin.sin_addr.s_addr = hostaddr;
149
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
150
fprintf(stderr, "failed to connect.\n");
155
session = libssh2_session_init();
157
fprintf(stderr, "Could not initialize SSH session.\n");
162
libssh2_session_set_blocking(session, 0);
167
while((rc = libssh2_session_handshake(session, sock)) ==
168
LIBSSH2_ERROR_EAGAIN);
170
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
179
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
180
fprintf(stderr, "Fingerprint: ");
181
for(i = 0; i < 20; i++) {
182
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
184
fprintf(stderr, "\n");
188
while((rc = libssh2_userauth_password(session, username, password)) ==
189
LIBSSH2_ERROR_EAGAIN);
191
fprintf(stderr, "Authentication by password failed.\n");
197
while((rc = libssh2_userauth_publickey_fromfile(session, username,
200
LIBSSH2_ERROR_EAGAIN);
202
fprintf(stderr, "Authentication by public key failed.\n");
209
channel = libssh2_scp_send(session, scppath, fileinfo.st_mode & 0777,
210
(size_t)fileinfo.st_size);
213
libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
216
libssh2_session_last_error(session, &err_msg, NULL, 0);
217
fprintf(stderr, "%s\n", err_msg);
222
fprintf(stderr, "SCP session waiting to send file\n");
225
nread = fread(mem, 1, sizeof(mem), local);
232
total += (libssh2_struct_stat_size)nread;
237
while((nwritten = libssh2_channel_write(channel, ptr, nread)) ==
238
LIBSSH2_ERROR_EAGAIN) {
239
waitsocket(sock, session);
243
fprintf(stderr, "ERROR %ld total %ld / %lu prev %lu\n",
244
(long)nwritten, (long)total,
245
(unsigned long)nread, (unsigned long)prev);
253
nread -= (size_t)nwritten;
258
duration = (int)(time(NULL) - start);
260
fprintf(stderr, "%ld bytes in %d seconds makes %.1f bytes/sec\n",
261
(long)total, duration, (double)total / duration);
263
fprintf(stderr, "Sending EOF\n");
264
while(libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN);
266
fprintf(stderr, "Waiting for EOF\n");
267
while(libssh2_channel_wait_eof(channel) == LIBSSH2_ERROR_EAGAIN);
269
fprintf(stderr, "Waiting for channel to close\n");
270
while(libssh2_channel_wait_closed(channel) == LIBSSH2_ERROR_EAGAIN);
272
libssh2_channel_free(channel);
278
while(libssh2_session_disconnect(session, "Normal Shutdown") ==
279
LIBSSH2_ERROR_EAGAIN);
280
libssh2_session_free(session);
283
if(sock != LIBSSH2_INVALID_SOCKET) {
285
LIBSSH2_SOCKET_CLOSE(sock);
288
fprintf(stderr, "all done\n");