1
/* Copyright (C) The libssh2 project and its contributors.
3
* The code sends a 'cat' command, and then writes a lot of data to it only to
4
* check that reading the returned data sums up to the same amount.
6
* $ ./ssh2_echo 127.0.0.1 user password
8
* SPDX-License-Identifier: BSD-3-Clause
11
#include "libssh2_setup.h"
14
#ifdef HAVE_SYS_SOCKET_H
15
#include <sys/socket.h>
20
#ifdef HAVE_NETINET_IN_H
21
#include <netinet/in.h>
23
#ifdef HAVE_ARPA_INET_H
31
static const char *hostname = "127.0.0.1";
32
static const char *commandline = "cat";
33
static const char *username = "user";
34
static const char *password = "password";
36
static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session)
38
struct timeval timeout;
41
fd_set *writefd = NULL;
42
fd_set *readfd = NULL;
51
#pragma GCC diagnostic push
52
#pragma GCC diagnostic ignored "-Wsign-conversion"
54
FD_SET(socket_fd, &fd);
56
#pragma GCC diagnostic pop
59
/* now make sure we wait in the correct direction */
60
dir = libssh2_session_block_directions(session);
62
if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
65
if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
68
rc = select((int)(socket_fd + 1), readfd, writefd, NULL, &timeout);
75
int main(int argc, char *argv[])
78
libssh2_socket_t sock;
79
struct sockaddr_in sin;
80
const char *fingerprint;
82
LIBSSH2_SESSION *session = NULL;
83
LIBSSH2_CHANNEL *channel;
85
char *exitsignal = (char *)"none";
87
LIBSSH2_KNOWNHOSTS *nh;
93
rc = WSAStartup(MAKEWORD(2, 0), &wsadata);
95
fprintf(stderr, "WSAStartup failed with error: %d\n", rc);
101
hostname = argv[1]; /* must be ip address only */
110
rc = libssh2_init(0);
112
fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
116
hostaddr = inet_addr(hostname);
118
/* Ultra basic "connect to port 22 on localhost". Your code is
119
* responsible for creating the socket establishing the connection
121
sock = socket(AF_INET, SOCK_STREAM, 0);
122
if(sock == LIBSSH2_INVALID_SOCKET) {
123
fprintf(stderr, "failed to create socket.\n");
127
sin.sin_family = AF_INET;
128
sin.sin_port = htons(22);
129
sin.sin_addr.s_addr = hostaddr;
130
if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) {
131
fprintf(stderr, "failed to connect.\n");
135
/* Create a session instance */
136
session = libssh2_session_init();
138
fprintf(stderr, "Could not initialize SSH session.\n");
142
/* tell libssh2 we want it all done non-blocking */
143
libssh2_session_set_blocking(session, 0);
145
/* ... start it up. This will trade welcome banners, exchange keys,
146
* and setup crypto, compression, and MAC layers
148
while((rc = libssh2_session_handshake(session, sock)) ==
149
LIBSSH2_ERROR_EAGAIN);
151
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
155
nh = libssh2_knownhost_init(session);
157
/* eeek, do cleanup here */
161
/* read all hosts from here */
162
libssh2_knownhost_readfile(nh, "known_hosts",
163
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
165
/* store all known hosts to here */
166
libssh2_knownhost_writefile(nh, "dumpfile",
167
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
169
fingerprint = libssh2_session_hostkey(session, &len, &type);
171
struct libssh2_knownhost *host;
172
int check = libssh2_knownhost_checkp(nh, hostname, 22,
174
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
175
LIBSSH2_KNOWNHOST_KEYENC_RAW,
178
fprintf(stderr, "Host check: %d, key: %s\n", check,
179
(check <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH) ?
180
host->key : "<none>");
183
* At this point, we could verify that 'check' tells us the key is
188
/* eeek, do cleanup here */
191
libssh2_knownhost_free(nh);
193
if(strlen(password) != 0) {
194
/* We could authenticate via password */
195
while((rc = libssh2_userauth_password(session, username, password)) ==
196
LIBSSH2_ERROR_EAGAIN);
198
fprintf(stderr, "Authentication by password failed.\n");
203
libssh2_trace(session, LIBSSH2_TRACE_SOCKET);
205
/* Exec non-blocking on the remote host */
207
channel = libssh2_channel_open_session(session);
209
libssh2_session_last_error(session, NULL, NULL, 0) !=
210
LIBSSH2_ERROR_EAGAIN)
212
waitsocket(sock, session);
215
fprintf(stderr, "Error\n");
218
while((rc = libssh2_channel_exec(channel, commandline)) ==
219
LIBSSH2_ERROR_EAGAIN) {
220
waitsocket(sock, session);
223
fprintf(stderr, "exec error\n");
227
LIBSSH2_POLLFD *fds = NULL;
229
size_t bufsize = BUFSIZE;
230
char buffer[BUFSIZE];
231
size_t totsize = 1500000;
232
size_t totwritten = 0;
238
for(i = 0; i < BUFSIZE; i++)
241
fds = malloc(sizeof(LIBSSH2_POLLFD));
243
fprintf(stderr, "malloc failed\n");
247
fds[0].type = LIBSSH2_POLLFD_CHANNEL;
248
fds[0].fd.channel = channel;
249
fds[0].events = LIBSSH2_POLLFD_POLLIN | LIBSSH2_POLLFD_POLLOUT;
253
rc = libssh2_poll(fds, 1, 10);
258
if(fds[0].revents & LIBSSH2_POLLFD_POLLIN) {
259
ssize_t n = libssh2_channel_read(channel,
260
buffer, sizeof(buffer));
263
if(n == LIBSSH2_ERROR_EAGAIN) {
265
fprintf(stderr, "will read again\n");
268
fprintf(stderr, "read failed\n");
272
totread += (size_t)n;
273
fprintf(stderr, "read %ld bytes (%lu in total)\n",
274
(long)n, (unsigned long)totread);
278
if(fds[0].revents & LIBSSH2_POLLFD_POLLOUT) {
281
if(totwritten < totsize) {
282
/* we have not written all data yet */
283
size_t left = totsize - totwritten;
284
size_t size = (left < bufsize) ? left : bufsize;
285
ssize_t n = libssh2_channel_write_ex(channel, 0,
288
if(n == LIBSSH2_ERROR_EAGAIN) {
290
fprintf(stderr, "will write again\n");
293
fprintf(stderr, "write failed\n");
297
totwritten += (size_t)n;
298
fprintf(stderr, "wrote %ld bytes (%lu in total)",
299
(long)n, (unsigned long)totwritten);
300
if(left >= bufsize && (size_t)n != bufsize) {
301
fprintf(stderr, " PARTIAL");
303
fprintf(stderr, "\n");
307
/* all data written, send EOF */
308
rc = libssh2_channel_send_eof(channel);
310
if(rc == LIBSSH2_ERROR_EAGAIN) {
311
fprintf(stderr, "will send eof again\n");
314
fprintf(stderr, "send eof failed\n");
318
fprintf(stderr, "sent eof\n");
319
/* we're done writing, stop listening for OUT events */
321
~(unsigned long)LIBSSH2_POLLFD_POLLOUT;
326
if(fds[0].revents & LIBSSH2_POLLFD_CHANNEL_CLOSED) {
327
if(!act) /* don't leave loop until we have read all data */
333
while((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
334
waitsocket(sock, session);
337
exitcode = libssh2_channel_get_exit_status(channel);
338
libssh2_channel_get_exit_signal(channel, &exitsignal,
339
NULL, NULL, NULL, NULL, NULL);
343
fprintf(stderr, "\nGot signal: %s\n", exitsignal);
345
libssh2_channel_free(channel);
348
fprintf(stderr, "\nrereads: %d rewrites: %d totwritten %lu\n",
349
rereads, rewrites, (unsigned long)totwritten);
351
if(totwritten != totread) {
352
fprintf(stderr, "\n*** FAIL bytes written: "
353
"%lu bytes read: %lu ***\n",
354
(unsigned long)totwritten, (unsigned long)totread);
362
libssh2_session_disconnect(session, "Normal Shutdown");
363
libssh2_session_free(session);
366
if(sock != LIBSSH2_INVALID_SOCKET) {
368
LIBSSH2_SOCKET_CLOSE(sock);
371
fprintf(stderr, "all done\n");