]> git.proxmox.com Git - rustc.git/blob - src/vendor/libssh2-sys/libssh2/example/sftp_nonblock.c
New upstream version 1.19.0+dfsg1
[rustc.git] / src / vendor / libssh2-sys / libssh2 / example / sftp_nonblock.c
1 /*
2 * Sample showing how to do SFTP non-blocking transfers.
3 *
4 * The sample code has default values for host name, user name, password
5 * and path to copy, but you can specify them on the command line like:
6 *
7 * "sftp_nonblock 192.168.0.1 user password /tmp/secrets"
8 */
9
10 #include "libssh2_config.h"
11 #include <libssh2.h>
12 #include <libssh2_sftp.h>
13
14 #ifdef HAVE_WINSOCK2_H
15 # include <winsock2.h>
16 #endif
17 #ifdef HAVE_SYS_SOCKET_H
18 # include <sys/socket.h>
19 #endif
20 #ifdef HAVE_NETINET_IN_H
21 # include <netinet/in.h>
22 #endif
23 #ifdef HAVE_SYS_SELECT_H
24 # include <sys/select.h>
25 #endif
26 # ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #ifdef HAVE_ARPA_INET_H
30 # include <arpa/inet.h>
31 #endif
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
34 #endif
35
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <ctype.h>
41
42 #ifdef HAVE_GETTIMEOFDAY
43 /* diff in ms */
44 static long tvdiff(struct timeval newer, struct timeval older)
45 {
46 return (newer.tv_sec-older.tv_sec)*1000+
47 (newer.tv_usec-older.tv_usec)/1000;
48 }
49 #endif
50
51 static int waitsocket(int socket_fd, LIBSSH2_SESSION *session)
52 {
53 struct timeval timeout;
54 int rc;
55 fd_set fd;
56 fd_set *writefd = NULL;
57 fd_set *readfd = NULL;
58 int dir;
59
60 timeout.tv_sec = 10;
61 timeout.tv_usec = 0;
62
63 FD_ZERO(&fd);
64
65 FD_SET(socket_fd, &fd);
66
67 /* now make sure we wait in the correct direction */
68 dir = libssh2_session_block_directions(session);
69
70 if(dir & LIBSSH2_SESSION_BLOCK_INBOUND)
71 readfd = &fd;
72
73 if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND)
74 writefd = &fd;
75
76 rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
77
78 return rc;
79 }
80
81 int main(int argc, char *argv[])
82 {
83 unsigned long hostaddr;
84 int sock, i, auth_pw = 1;
85 struct sockaddr_in sin;
86 const char *fingerprint;
87 LIBSSH2_SESSION *session;
88 const char *username="username";
89 const char *password="password";
90 const char *sftppath="/tmp/TEST";
91 #ifdef HAVE_GETTIMEOFDAY
92 struct timeval start;
93 struct timeval end;
94 long time_ms;
95 #endif
96 int rc;
97 int total = 0;
98 int spin = 0;
99 LIBSSH2_SFTP *sftp_session;
100 LIBSSH2_SFTP_HANDLE *sftp_handle;
101
102 #ifdef WIN32
103 WSADATA wsadata;
104 int err;
105
106 err = WSAStartup(MAKEWORD(2,0), &wsadata);
107 if (err != 0) {
108 fprintf(stderr, "WSAStartup failed with error: %d\n", err);
109 return 1;
110 }
111 #endif
112
113 if (argc > 1) {
114 hostaddr = inet_addr(argv[1]);
115 } else {
116 hostaddr = htonl(0x7F000001);
117 }
118
119 if (argc > 2) {
120 username = argv[2];
121 }
122 if (argc > 3) {
123 password = argv[3];
124 }
125 if (argc > 4) {
126 sftppath = argv[4];
127 }
128
129 rc = libssh2_init (0);
130 if (rc != 0) {
131 fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
132 return 1;
133 }
134
135 /*
136 * The application code is responsible for creating the socket
137 * and establishing the connection
138 */
139 sock = socket(AF_INET, SOCK_STREAM, 0);
140
141 sin.sin_family = AF_INET;
142 sin.sin_port = htons(22);
143 sin.sin_addr.s_addr = hostaddr;
144 if (connect(sock, (struct sockaddr*)(&sin),
145 sizeof(struct sockaddr_in)) != 0) {
146 fprintf(stderr, "failed to connect!\n");
147 return -1;
148 }
149
150 /* Create a session instance */
151 session = libssh2_session_init();
152 if (!session)
153 return -1;
154
155 /* Since we have set non-blocking, tell libssh2 we are non-blocking */
156 libssh2_session_set_blocking(session, 0);
157
158 #ifdef HAVE_GETTIMEOFDAY
159 gettimeofday(&start, NULL);
160 #endif
161
162 /* ... start it up. This will trade welcome banners, exchange keys,
163 * and setup crypto, compression, and MAC layers
164 */
165 while ((rc = libssh2_session_handshake(session, sock)) ==
166 LIBSSH2_ERROR_EAGAIN);
167 if (rc) {
168 fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
169 return -1;
170 }
171
172 /* At this point we havn't yet authenticated. The first thing to do
173 * is check the hostkey's fingerprint against our known hosts Your app
174 * may have it hard coded, may go to a file, may present it to the
175 * user, that's your call
176 */
177 fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
178 fprintf(stderr, "Fingerprint: ");
179 for(i = 0; i < 20; i++) {
180 fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
181 }
182 fprintf(stderr, "\n");
183
184 if (auth_pw) {
185 /* We could authenticate via password */
186 while ((rc = libssh2_userauth_password(session, username, password))
187 == LIBSSH2_ERROR_EAGAIN);
188 if (rc) {
189 fprintf(stderr, "Authentication by password failed.\n");
190 goto shutdown;
191 }
192 } else {
193 /* Or by public key */
194 while ((rc =
195 libssh2_userauth_publickey_fromfile(session, username,
196 "/home/username/"
197 ".ssh/id_rsa.pub",
198 "/home/username/"
199 ".ssh/id_rsa",
200 password)) ==
201 LIBSSH2_ERROR_EAGAIN);
202 if (rc) {
203 fprintf(stderr, "\tAuthentication by public key failed\n");
204 goto shutdown;
205 }
206 }
207 #if 0
208 libssh2_trace(session, LIBSSH2_TRACE_CONN);
209 #endif
210 fprintf(stderr, "libssh2_sftp_init()!\n");
211 do {
212 sftp_session = libssh2_sftp_init(session);
213
214 if(!sftp_session) {
215 if(libssh2_session_last_errno(session) ==
216 LIBSSH2_ERROR_EAGAIN) {
217 fprintf(stderr, "non-blocking init\n");
218 waitsocket(sock, session); /* now we wait */
219 }
220 else {
221 fprintf(stderr, "Unable to init SFTP session\n");
222 goto shutdown;
223 }
224 }
225 } while (!sftp_session);
226
227 fprintf(stderr, "libssh2_sftp_open()!\n");
228 /* Request a file via SFTP */
229 do {
230 sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
231 LIBSSH2_FXF_READ, 0);
232
233 if (!sftp_handle) {
234 if (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
235 fprintf(stderr, "Unable to open file with SFTP\n");
236 goto shutdown;
237 }
238 else {
239 fprintf(stderr, "non-blocking open\n");
240 waitsocket(sock, session); /* now we wait */
241 }
242 }
243 } while (!sftp_handle);
244
245 fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
246 do {
247 char mem[1024*24];
248
249 /* loop until we fail */
250 while ((rc = libssh2_sftp_read(sftp_handle, mem,
251 sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) {
252 spin++;
253 waitsocket(sock, session); /* now we wait */
254 }
255 if (rc > 0) {
256 total += rc;
257 write(1, mem, rc);
258 } else {
259 break;
260 }
261 } while (1);
262
263 #ifdef HAVE_GETTIMEOFDAY
264 gettimeofday(&end, NULL);
265 time_ms = tvdiff(end, start);
266 fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n", total,
267 time_ms, total/(time_ms/1000.0), spin );
268 #else
269 fprintf(stderr, "Got %d bytes spin: %d\n", total, spin);
270 #endif
271
272 libssh2_sftp_close(sftp_handle);
273 libssh2_sftp_shutdown(sftp_session);
274
275 shutdown:
276
277 fprintf(stderr, "libssh2_session_disconnect\n");
278 while (libssh2_session_disconnect(session,
279 "Normal Shutdown, Thank you") ==
280 LIBSSH2_ERROR_EAGAIN);
281 libssh2_session_free(session);
282
283 #ifdef WIN32
284 closesocket(sock);
285 #else
286 close(sock);
287 #endif
288 fprintf(stderr, "all done\n");
289
290 libssh2_exit();
291
292 return 0;
293 }