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 */
tvdiff(struct timeval newer,struct timeval older)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
waitsocket(int socket_fd,LIBSSH2_SESSION * session)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
main(int argc,char * argv[])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 }
116 else {
117 hostaddr = htonl(0x7F000001);
118 }
119
120 if(argc > 2) {
121 username = argv[2];
122 }
123 if(argc > 3) {
124 password = argv[3];
125 }
126 if(argc > 4) {
127 sftppath = argv[4];
128 }
129
130 rc = libssh2_init(0);
131 if(rc != 0) {
132 fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
133 return 1;
134 }
135
136 /*
137 * The application code is responsible for creating the socket
138 * and establishing the connection
139 */
140 sock = socket(AF_INET, SOCK_STREAM, 0);
141
142 sin.sin_family = AF_INET;
143 sin.sin_port = htons(22);
144 sin.sin_addr.s_addr = hostaddr;
145 if(connect(sock, (struct sockaddr*)(&sin),
146 sizeof(struct sockaddr_in)) != 0) {
147 fprintf(stderr, "failed to connect!\n");
148 return -1;
149 }
150
151 /* Create a session instance */
152 session = libssh2_session_init();
153 if(!session)
154 return -1;
155
156 /* Since we have set non-blocking, tell libssh2 we are non-blocking */
157 libssh2_session_set_blocking(session, 0);
158
159 #ifdef HAVE_GETTIMEOFDAY
160 gettimeofday(&start, NULL);
161 #endif
162
163 /* ... start it up. This will trade welcome banners, exchange keys,
164 * and setup crypto, compression, and MAC layers
165 */
166 while((rc = libssh2_session_handshake(session, sock)) ==
167 LIBSSH2_ERROR_EAGAIN);
168 if(rc) {
169 fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
170 return -1;
171 }
172
173 /* At this point we havn't yet authenticated. The first thing to do
174 * is check the hostkey's fingerprint against our known hosts Your app
175 * may have it hard coded, may go to a file, may present it to the
176 * user, that's your call
177 */
178 fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
179 fprintf(stderr, "Fingerprint: ");
180 for(i = 0; i < 20; i++) {
181 fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
182 }
183 fprintf(stderr, "\n");
184
185 if(auth_pw) {
186 /* We could authenticate via password */
187 while((rc = libssh2_userauth_password(session, username, password))
188 == LIBSSH2_ERROR_EAGAIN);
189 if(rc) {
190 fprintf(stderr, "Authentication by password failed.\n");
191 goto shutdown;
192 }
193 }
194 else {
195 /* Or by public key */
196 while((rc =
197 libssh2_userauth_publickey_fromfile(session, username,
198 "/home/username/"
199 ".ssh/id_rsa.pub",
200 "/home/username/"
201 ".ssh/id_rsa",
202 password)) ==
203 LIBSSH2_ERROR_EAGAIN);
204 if(rc) {
205 fprintf(stderr, "\tAuthentication by public key failed\n");
206 goto shutdown;
207 }
208 }
209 #if 0
210 libssh2_trace(session, LIBSSH2_TRACE_CONN);
211 #endif
212 fprintf(stderr, "libssh2_sftp_init()!\n");
213 do {
214 sftp_session = libssh2_sftp_init(session);
215
216 if(!sftp_session) {
217 if(libssh2_session_last_errno(session) ==
218 LIBSSH2_ERROR_EAGAIN) {
219 fprintf(stderr, "non-blocking init\n");
220 waitsocket(sock, session); /* now we wait */
221 }
222 else {
223 fprintf(stderr, "Unable to init SFTP session\n");
224 goto shutdown;
225 }
226 }
227 } while(!sftp_session);
228
229 fprintf(stderr, "libssh2_sftp_open()!\n");
230 /* Request a file via SFTP */
231 do {
232 sftp_handle = libssh2_sftp_open(sftp_session, sftppath,
233 LIBSSH2_FXF_READ, 0);
234
235 if(!sftp_handle) {
236 if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) {
237 fprintf(stderr, "Unable to open file with SFTP\n");
238 goto shutdown;
239 }
240 else {
241 fprintf(stderr, "non-blocking open\n");
242 waitsocket(sock, session); /* now we wait */
243 }
244 }
245 } while(!sftp_handle);
246
247 fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
248 do {
249 char mem[1024*24];
250
251 /* loop until we fail */
252 while((rc = libssh2_sftp_read(sftp_handle, mem,
253 sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) {
254 spin++;
255 waitsocket(sock, session); /* now we wait */
256 }
257 if(rc > 0) {
258 total += rc;
259 write(1, mem, rc);
260 }
261 else {
262 break;
263 }
264 } while(1);
265
266 #ifdef HAVE_GETTIMEOFDAY
267 gettimeofday(&end, NULL);
268 time_ms = tvdiff(end, start);
269 fprintf(stderr, "Got %d bytes in %ld ms = %.1f bytes/sec spin: %d\n",
270 total,
271 time_ms, total/(time_ms/1000.0), spin);
272 #else
273 fprintf(stderr, "Got %d bytes spin: %d\n", total, spin);
274 #endif
275
276 libssh2_sftp_close(sftp_handle);
277 libssh2_sftp_shutdown(sftp_session);
278
279 shutdown:
280
281 fprintf(stderr, "libssh2_session_disconnect\n");
282 while(libssh2_session_disconnect(session,
283 "Normal Shutdown, Thank you") ==
284 LIBSSH2_ERROR_EAGAIN);
285 libssh2_session_free(session);
286
287 #ifdef WIN32
288 closesocket(sock);
289 #else
290 close(sock);
291 #endif
292 fprintf(stderr, "all done\n");
293
294 libssh2_exit();
295
296 return 0;
297 }
298