~ubuntu-branches/ubuntu/vivid/libssh2/vivid-proposed

« back to all changes in this revision

Viewing changes to example/simple/sftp_nonblock.c

  • Committer: Bazaar Package Importer
  • Author(s): Mikhail Gusarov
  • Date: 2010-02-28 13:11:14 UTC
  • mto: (1.1.6 upstream) (2.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20100228131114-g8d2ps9p1u8i80s3
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

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