2
* $Id: sftp_mkdir_nonblock.c,v 1.12 2009/04/28 10:35:30 bagder Exp $
4
* Sample showing how to do SFTP non-blocking mkdir.
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:
9
* "sftp 192.168.0.1 user password /tmp/sftp_write_nonblock.c"
12
#include "libssh2_config.h"
14
#include <libssh2_sftp.h>
16
#ifdef HAVE_WINSOCK2_H
17
# include <winsock2.h>
19
#ifdef HAVE_SYS_SOCKET_H
20
# include <sys/socket.h>
22
#ifdef HAVE_NETINET_IN_H
23
# include <netinet/in.h>
28
#ifdef HAVE_ARPA_INET_H
29
# include <arpa/inet.h>
32
#include <sys/types.h>
38
int main(int argc, char *argv[])
40
unsigned long hostaddr;
41
int sock, i, auth_pw = 1;
42
struct sockaddr_in sin;
43
const char *fingerprint;
44
LIBSSH2_SESSION *session;
45
const char *username="username";
46
const char *password="password";
47
const char *sftppath="/tmp/sftp_mkdir_nonblock";
49
#if defined(HAVE_IOCTLSOCKET)
52
LIBSSH2_SFTP *sftp_session;
57
WSAStartup(MAKEWORD(2,0), &wsadata);
61
hostaddr = inet_addr(argv[1]);
63
hostaddr = htonl(0x7F000001);
77
* The application code is responsible for creating the socket
78
* and establishing the connection
80
sock = socket(AF_INET, SOCK_STREAM, 0);
82
sin.sin_family = AF_INET;
83
sin.sin_port = htons(22);
84
sin.sin_addr.s_addr = hostaddr;
85
if (connect(sock, (struct sockaddr*)(&sin),
86
sizeof(struct sockaddr_in)) != 0) {
87
fprintf(stderr, "failed to connect!\n");
91
/* Create a session instance
93
session = libssh2_session_init();
97
/* ... start it up. This will trade welcome banners, exchange keys,
98
* and setup crypto, compression, and MAC layers
100
rc = libssh2_session_startup(session, sock);
102
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
106
/* At this point we havn't yet authenticated. The first thing to do
107
* is check the hostkey's fingerprint against our known hosts Your app
108
* may have it hard coded, may go to a file, may present it to the
109
* user, that's your call
111
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
112
printf("Fingerprint: ");
113
for(i = 0; i < 20; i++) {
114
printf("%02X ", (unsigned char)fingerprint[i]);
119
/* We could authenticate via password */
120
if (libssh2_userauth_password(session, username, password)) {
121
printf("Authentication by password failed.\n");
125
/* Or by public key */
126
if (libssh2_userauth_publickey_fromfile(session, username,
127
"/home/username/.ssh/id_rsa.pub",
128
"/home/username/.ssh/id_rsa",
130
printf("\tAuthentication by public key failed\n");
135
fprintf(stderr, "libssh2_sftp_init()!\n");
136
sftp_session = libssh2_sftp_init(session);
139
fprintf(stderr, "Unable to init SFTP session\n");
143
/* Since we have set non-blocking, tell libssh2 we are non-blocking */
144
libssh2_session_set_blocking(session, 0);
146
fprintf(stderr, "libssh2_sftp_mkdirnb()!\n");
147
/* Make a directory via SFTP */
148
while ((rc = libssh2_sftp_mkdir(sftp_session, sftppath,
149
LIBSSH2_SFTP_S_IRWXU|
150
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP|
151
LIBSSH2_SFTP_S_IROTH|LIBSSH2_SFTP_S_IXOTH))
152
== LIBSSH2_ERROR_EAGAIN) {
156
libssh2_sftp_shutdown(sftp_session);
160
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
161
libssh2_session_free(session);
168
printf("all done\n");