converted to 4-level space indents, no trailing whitespace

This commit is contained in:
Daniel Stenberg 2007-07-14 21:24:38 +00:00
parent 84e16944b4
commit accd865aff
14 changed files with 853 additions and 849 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: scp.c,v 1.7 2007/06/06 12:34:08 jehousley Exp $ * $Id: scp.c,v 1.8 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do a simple SCP transfer. * Sample showing how to do a simple SCP transfer.
*/ */
@ -67,7 +67,7 @@ int main(int argc, char *argv[])
if (argc > 4) { if (argc > 4) {
scppath = argv[4]; scppath = argv[4];
} }
/* Ultra basic "connect to port 22 on localhost" /* Ultra basic "connect to port 22 on localhost"
* Your code is responsible for creating the socket establishing the * Your code is responsible for creating the socket establishing the
* connection * connection

View File

@ -1,5 +1,5 @@
/* /*
* $Id: scp_nonblock.c,v 1.6 2007/07/05 15:31:19 jehousley Exp $ * $Id: scp_nonblock.c,v 1.7 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SCP transfers in a non-blocking manner. * Sample showing how to do SCP transfers in a non-blocking manner.
*/ */
@ -46,13 +46,13 @@ int main(int argc, char *argv[])
struct stat fileinfo; struct stat fileinfo;
int rc; int rc;
off_t got=0; off_t got=0;
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
WSAStartup(WINSOCK_VERSION, &wsadata); WSAStartup(WINSOCK_VERSION, &wsadata);
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
@ -67,13 +67,13 @@ int main(int argc, char *argv[])
if (argc > 4) { if (argc > 4) {
scppath = argv[4]; scppath = argv[4];
} }
/* Ultra basic "connect to port 22 on localhost" /* Ultra basic "connect to port 22 on localhost"
* Your code is responsible for creating the socket establishing the * Your code is responsible for creating the socket establishing the
* connection * connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
@ -81,7 +81,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* We set the socket non-blocking. We do it after the connect just to /* We set the socket non-blocking. We do it after the connect just to
simplify the example code. */ simplify the example code. */
#ifdef F_SETFL #ifdef F_SETFL
@ -91,15 +91,15 @@ int main(int argc, char *argv[])
#else #else
#error "add support for setting the socket non-blocking here" #error "add support for setting the socket non-blocking here"
#endif #endif
/* Create a session instance */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if (!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
@ -108,7 +108,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
/* At this point we havn't yet authenticated. The first thing to do /* At this point we havn't yet authenticated. The first thing to do
* is check the hostkey's fingerprint against our known hosts Your app * is check the hostkey's fingerprint against our known hosts Your app
* may have it hard coded, may go to a file, may present it to the * may have it hard coded, may go to a file, may present it to the
@ -120,7 +120,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN);
@ -139,36 +139,36 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
} }
/* Request a file via SCP */ /* Request a file via SCP */
fprintf(stderr, "libssh2_scp_recv()!\n"); fprintf(stderr, "libssh2_scp_recv()!\n");
do { do {
channel = libssh2_scp_recv(session, scppath, &fileinfo); channel = libssh2_scp_recv(session, scppath, &fileinfo);
if ((!channel) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
char *err_msg;
libssh2_session_last_error(session, &err_msg, NULL, 0); if ((!channel) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
char *err_msg;
libssh2_session_last_error(session, &err_msg, NULL, 0);
fprintf(stderr, "%s\n", err_msg); fprintf(stderr, "%s\n", err_msg);
goto shutdown; goto shutdown;
} }
} while (!channel); } while (!channel);
fprintf(stderr, "libssh2_scp_recv() is done, now receive data!\n"); fprintf(stderr, "libssh2_scp_recv() is done, now receive data!\n");
while(got < fileinfo.st_size) { while(got < fileinfo.st_size) {
char mem[1000]; char mem[1000];
struct timeval timeout; struct timeval timeout;
int rc; int rc;
fd_set fd; fd_set fd;
do { do {
int amount=sizeof(mem); int amount=sizeof(mem);
if ((fileinfo.st_size -got) < amount) { if ((fileinfo.st_size -got) < amount) {
amount = fileinfo.st_size - got; amount = fileinfo.st_size - got;
} }
/* loop until we block */ /* loop until we block */
rc = libssh2_channel_read(channel, mem, amount); rc = libssh2_channel_read(channel, mem, amount);
if (rc > 0) { if (rc > 0) {
@ -176,18 +176,18 @@ int main(int argc, char *argv[])
got += rc; got += rc;
} }
} while (rc > 0); } while (rc > 0);
if (rc == LIBSSH2_ERROR_EAGAIN) { if (rc == LIBSSH2_ERROR_EAGAIN) {
/* this is due to blocking that would occur otherwise /* this is due to blocking that would occur otherwise
so we loop on this condition */ so we loop on this condition */
timeout.tv_sec = 10; timeout.tv_sec = 10;
timeout.tv_usec = 0; timeout.tv_usec = 0;
FD_ZERO(&fd); FD_ZERO(&fd);
FD_SET(sock, &fd); FD_SET(sock, &fd);
rc = select(sock+1, &fd, &fd, NULL, &timeout); rc = select(sock+1, &fd, &fd, NULL, &timeout);
if (rc <= 0) { if (rc <= 0) {
/* negative is error /* negative is error
@ -198,15 +198,15 @@ int main(int argc, char *argv[])
} }
break; break;
} }
libssh2_channel_free(channel); libssh2_channel_free(channel);
channel = NULL; channel = NULL;
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
Sleep(1000); Sleep(1000);
closesocket(sock); closesocket(sock);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: scp_write.c,v 1.2 2007/07/14 20:54:47 bagder Exp $ * $Id: scp_write.c,v 1.3 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do a simple SCP transfer. * Sample showing how to do a simple SCP transfer.
*/ */
@ -58,27 +58,27 @@ int main(int argc, char *argv[])
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if (argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if (argc > 3) {
password = argv[3]; password = argv[3];
} }
if(argc > 4) { if(argc > 4) {
loclfile = argv[4]; loclfile = argv[4];
} }
if (argc > 5) { if (argc > 5) {
scppath = argv[5]; scppath = argv[5];
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if (!local) {
fprintf(stderr, "Can't local file %s\n", loclfile); fprintf(stderr, "Can't local file %s\n", loclfile);
goto shutdown; goto shutdown;
} }
stat(loclfile, &fileinfo); stat(loclfile, &fileinfo);
@ -93,24 +93,24 @@ int main(int argc, char *argv[])
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* Create a session instance /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if(!session) if(!session)
return -1; return -1;
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
rc = libssh2_session_startup(session, sock); rc = libssh2_session_startup(session, sock);
if(rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
/* At this point we havn't yet authenticated. The first thing to do /* At this point we havn't yet authenticated. The first thing to do
@ -121,25 +121,25 @@ int main(int argc, char *argv[])
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
fprintf(stderr, "Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 16; i++) { for(i = 0; i < 16; i++) {
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, if (libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
/* Request a file via SCP */ /* Request a file via SCP */
@ -147,25 +147,25 @@ int main(int argc, char *argv[])
(unsigned long)fileinfo.st_size); (unsigned long)fileinfo.st_size);
if (!channel) { if (!channel) {
fprintf(stderr, "Unable to open a session\n"); fprintf(stderr, "Unable to open a session\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "SCP session waiting to send file\n"); fprintf(stderr, "SCP session waiting to send file\n");
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) { if (nread <= 0) {
/* end of file */ /* end of file */
break; break;
} }
ptr = mem; ptr = mem;
do { do {
/* write data in a loop until we block */ /* write data in a loop until we block */
rc = libssh2_channel_write(channel, ptr, nread); rc = libssh2_channel_write(channel, ptr, nread);
ptr += rc; ptr += rc;
nread -= nread; nread -= nread;
} while (rc > 0); } while (rc > 0);
} while (1); } while (1);
fprintf(stderr, "Sending EOF\n"); fprintf(stderr, "Sending EOF\n");

View File

@ -1,5 +1,5 @@
/* /*
* $Id: scp_write_nonblock.c,v 1.4 2007/07/14 20:54:47 bagder Exp $ * $Id: scp_write_nonblock.c,v 1.5 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do a simple SCP transfer. * Sample showing how to do a simple SCP transfer.
*/ */
@ -58,27 +58,27 @@ int main(int argc, char *argv[])
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if (argc > 2) {
username = argv[2]; username = argv[2];
} }
if (argc > 3) { if (argc > 3) {
password = argv[3]; password = argv[3];
} }
if(argc > 4) { if(argc > 4) {
loclfile = argv[4]; loclfile = argv[4];
} }
if (argc > 5) { if (argc > 5) {
scppath = argv[5]; scppath = argv[5];
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if (!local) {
fprintf(stderr, "Can't local file %s\n", loclfile); fprintf(stderr, "Can't local file %s\n", loclfile);
goto shutdown; goto shutdown;
} }
stat(loclfile, &fileinfo); stat(loclfile, &fileinfo);
@ -93,13 +93,13 @@ int main(int argc, char *argv[])
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* We set the socket non-blocking. We do it after the connect just to /* We set the socket non-blocking. We do it after the connect just to
simplify the example code. */ simplify the example code. */
#ifdef F_SETFL #ifdef F_SETFL
/* FIXME: this can/should be done in a more portable manner */ /* FIXME: this can/should be done in a more portable manner */
rc = fcntl(sock, F_GETFL, 0); rc = fcntl(sock, F_GETFL, 0);
@ -112,7 +112,7 @@ int main(int argc, char *argv[])
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if(!session) if(!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
@ -121,10 +121,10 @@ int main(int argc, char *argv[])
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_startup(session, sock)) while ((rc = libssh2_session_startup(session, sock))
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
if(rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
/* At this point we havn't yet authenticated. The first thing to do /* At this point we havn't yet authenticated. The first thing to do
@ -135,64 +135,64 @@ int main(int argc, char *argv[])
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
fprintf(stderr, "Fingerprint: "); fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 16; i++) { for(i = 0; i < 16; i++) {
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == while ((rc = libssh2_userauth_password(session, username, password)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
/* Or by public key */ /* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username, while ((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) == LIBSSH2_ERROR_EAGAIN); password)) == LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
/* Request a file via SCP */ /* Request a file via SCP */
do { do {
channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode, channel = libssh2_scp_send(session, scppath, 0x1FF & fileinfo.st_mode,
(unsigned long)fileinfo.st_size); (unsigned long)fileinfo.st_size);
if ((!channel) && (libssh2_session_last_errno(session) != if ((!channel) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) { LIBSSH2_ERROR_EAGAIN)) {
char *err_msg; char *err_msg;
libssh2_session_last_error(session, &err_msg, NULL, 0); libssh2_session_last_error(session, &err_msg, NULL, 0);
fprintf(stderr, "%s\n", err_msg); fprintf(stderr, "%s\n", err_msg);
goto shutdown; goto shutdown;
} }
} while (!channel); } while (!channel);
fprintf(stderr, "SCP session waiting to send file\n"); fprintf(stderr, "SCP session waiting to send file\n");
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) { if (nread <= 0) {
/* end of file */ /* end of file */
break; break;
} }
ptr = mem; ptr = mem;
do { do {
/* write data in a loop until we block */ /* write data in a loop until we block */
while ((rc = libssh2_channel_write(channel, ptr, nread)) == while ((rc = libssh2_channel_write(channel, ptr, nread)) ==
LIBSSH2_ERROR_EAGAIN); LIBSSH2_ERROR_EAGAIN);
if (rc < 0) { if (rc < 0) {
fprintf(stderr, "ERROR %d\n", rc); fprintf(stderr, "ERROR %d\n", rc);
} }
ptr += rc; ptr += rc;
nread -= rc; nread -= rc;
} while (nread > 0); } while (nread > 0);
} while (1); } while (1);
fprintf(stderr, "Sending EOF\n"); fprintf(stderr, "Sending EOF\n");

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftp.c,v 1.8 2007/06/06 12:34:08 jehousley Exp $ * $Id: sftp.c,v 1.9 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SFTP transfers. * Sample showing how to do SFTP transfers.
* *
@ -40,153 +40,153 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned long hostaddr; unsigned long hostaddr;
int sock, i, auth_pw = 1; int sock, i, auth_pw = 1;
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
char *username=(char *)"username"; char *username=(char *)"username";
char *password=(char *)"password"; char *password=(char *)"password";
char *sftppath=(char *)"/tmp/TEST"; char *sftppath=(char *)"/tmp/TEST";
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_SFTP_HANDLE *sftp_handle;
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
WSAStartup(WINSOCK_VERSION, &wsadata); WSAStartup(WINSOCK_VERSION, &wsadata);
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if(argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if(argc > 4) { if(argc > 4) {
sftppath = argv[4]; sftppath = argv[4];
} }
/* /*
* The application code is responsible for creating the socket * The application code is responsible for creating the socket
* and establishing the connection * and establishing the connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* Create a session instance /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if(!session) if(!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are blocking */ /* Since we have set non-blocking, tell libssh2 we are blocking */
libssh2_session_set_blocking(session, 1); libssh2_session_set_blocking(session, 1);
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
rc = libssh2_session_startup(session, sock);
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
}
/* Since we have not set non-blocking, tell libssh2 we are blocking */ /* ... start it up. This will trade welcome banners, exchange keys,
libssh2_session_set_blocking(session, 1); * and setup crypto, compression, and MAC layers
*/
rc = libssh2_session_startup(session, sock);
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
}
/* At this point we havn't yet authenticated. The first thing to do /* Since we have not set non-blocking, tell libssh2 we are blocking */
* is check the hostkey's fingerprint against our known hosts Your app libssh2_session_set_blocking(session, 1);
* may have it hard coded, may go to a file, may present it to the
* user, that's your call
*/
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
fprintf(stderr, "Fingerprint: ");
for(i = 0; i < 16; i++) {
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
}
fprintf(stderr, "\n");
if (auth_pw) { /* At this point we havn't yet authenticated. The first thing to do
/* We could authenticate via password */ * is check the hostkey's fingerprint against our known hosts Your app
if (libssh2_userauth_password(session, username, password)) { * may have it hard coded, may go to a file, may present it to the
fprintf(stderr, "Authentication by password failed.\n"); * user, that's your call
goto shutdown; */
} fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
} else { fprintf(stderr, "Fingerprint: ");
/* Or by public key */ for(i = 0; i < 16; i++) {
if (libssh2_userauth_publickey_fromfile(session, username, fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
"/home/username/.ssh/id_rsa.pub", }
"/home/username/.ssh/id_rsa", fprintf(stderr, "\n");
password)) {
fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown;
}
}
fprintf(stderr, "libssh2_sftp_init()!\n"); if (auth_pw) {
sftp_session = libssh2_sftp_init(session); /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) {
if (!sftp_session) { fprintf(stderr, "Authentication by password failed.\n");
fprintf(stderr, "Unable to init SFTP session\n"); goto shutdown;
goto shutdown;
}
/* Since we have not set non-blocking, tell libssh2 we are blocking */
libssh2_session_set_blocking(session, 1);
fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */
sftp_handle =
libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0);
if (!sftp_handle) {
fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown;
}
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
do {
char mem[1024];
/* loop until we fail */
fprintf(stderr, "libssh2_sftp_read()!\n");
rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
if (rc > 0) {
write(1, mem, rc);
} else {
break;
} }
} while (1); } else {
/* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa",
password)) {
fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown;
}
}
libssh2_sftp_close(sftp_handle); fprintf(stderr, "libssh2_sftp_init()!\n");
libssh2_sftp_shutdown(sftp_session); sftp_session = libssh2_sftp_init(session);
shutdown: if (!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown;
}
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); /* Since we have not set non-blocking, tell libssh2 we are blocking */
libssh2_session_free(session); libssh2_session_set_blocking(session, 1);
fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */
sftp_handle =
libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0);
if (!sftp_handle) {
fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown;
}
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
do {
char mem[1024];
/* loop until we fail */
fprintf(stderr, "libssh2_sftp_read()!\n");
rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
if (rc > 0) {
write(1, mem, rc);
} else {
break;
}
} while (1);
libssh2_sftp_close(sftp_handle);
libssh2_sftp_shutdown(sftp_session);
shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
Sleep(1000); Sleep(1000);
closesocket(sock); closesocket(sock);
#else #else
sleep(1); sleep(1);
close(sock); close(sock);
#endif #endif
fprintf(stderr, "all done\n"); fprintf(stderr, "all done\n");
return 0; return 0;
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftp_RW_nonblock.c,v 1.7 2007/06/08 13:33:08 jehousley Exp $ * $Id: sftp_RW_nonblock.c,v 1.8 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SFTP transfers in a non-blocking manner. * Sample showing how to do SFTP transfers in a non-blocking manner.
* *
@ -44,251 +44,251 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int sock, i, auth_pw = 1; int sock, i, auth_pw = 1;
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
char *username=(char *)"username"; char *username=(char *)"username";
char *password=(char *)"password"; char *password=(char *)"password";
char *sftppath=(char *)"/tmp/TEST"; /* source path */ char *sftppath=(char *)"/tmp/TEST"; /* source path */
char *dest=(char *)"/tmp/TEST2"; /* destination path */ char *dest=(char *)"/tmp/TEST2"; /* destination path */
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_SFTP_HANDLE *sftp_handle;
FILE *tempstorage; FILE *tempstorage;
char mem[1000]; char mem[1000];
struct timeval timeout; struct timeval timeout;
fd_set fd; fd_set fd;
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
WSAStartup(WINSOCK_VERSION, &wsadata); WSAStartup(WINSOCK_VERSION, &wsadata);
#endif #endif
/* Ultra basic "connect to port 22 on localhost" /* Ultra basic "connect to port 22 on localhost"
* The application is responsible for creating the socket establishing * The application is responsible for creating the socket establishing
* the connection * the connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = htonl(0x7F000001); sin.sin_addr.s_addr = htonl(0x7F000001);
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* We set the socket non-blocking. We do it after the connect just to /* We set the socket non-blocking. We do it after the connect just to
simplify the example code. */ simplify the example code. */
#ifdef F_SETFL #ifdef F_SETFL
/* FIXME: this can/should be done in a more portable manner */ /* FIXME: this can/should be done in a more portable manner */
rc = fcntl(sock, F_GETFL, 0); rc = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, rc | O_NONBLOCK); fcntl(sock, F_SETFL, rc | O_NONBLOCK);
#else #else
#error "add support for setting the socket non-blocking here" #error "add support for setting the socket non-blocking here"
#endif #endif
/* Create a session instance /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if(!session) if(!session)
return -1; return -1;
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
rc = libssh2_session_startup(session, sock); rc = libssh2_session_startup(session, sock);
if(rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
/* At this point we havn't yet authenticated. The first thing to do /* At this point we havn't yet authenticated. The first thing to do
* is check the hostkey's fingerprint against our known hosts Your app * is check the hostkey's fingerprint against our known hosts Your app
* may have it hard coded, may go to a file, may present it to the * may have it hard coded, may go to a file, may present it to the
* user, that's your call * user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
printf("Fingerprint: "); printf("Fingerprint: ");
for(i = 0; i < 16; i++) { for(i = 0; i < 16; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); printf("%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); printf("\n");
if(argc > 1) { if(argc > 1) {
username = argv[1]; username = argv[1];
} }
if(argc > 2) { if(argc > 2) {
password = argv[2]; password = argv[2];
} }
if(argc > 3) { if(argc > 3) {
sftppath = argv[3]; sftppath = argv[3];
} }
if(argc > 4) { if(argc > 4) {
dest = argv[4]; dest = argv[4];
} }
tempstorage = fopen(STORAGE, "wb"); tempstorage = fopen(STORAGE, "wb");
if(!tempstorage) { if(!tempstorage) {
printf("Can't open temp storage file %s\n", STORAGE); printf("Can't open temp storage file %s\n", STORAGE);
goto shutdown; goto shutdown;
}
if (auth_pw) {
/* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) {
printf("Authentication by password failed.\n");
goto shutdown;
} }
} else {
if (auth_pw) { /* Or by public key */
/* We could authenticate via password */ if (libssh2_userauth_publickey_fromfile(session, username,
if (libssh2_userauth_password(session, username, password)) { "/home/username/.ssh/id_rsa.pub",
printf("Authentication by password failed.\n"); "/home/username/.ssh/id_rsa",
goto shutdown; password)) {
} printf("\tAuthentication by public key failed\n");
} else { goto shutdown;
/* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa",
password)) {
printf("\tAuthentication by public key failed\n");
goto shutdown;
}
} }
}
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session) { if (!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
/* Request a file via SFTP */ /* Request a file via SFTP */
sftp_handle = sftp_handle =
libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0);
if (!sftp_handle) { if (!sftp_handle) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
do {
do { do {
do { /* read in a loop until we block */
/* read in a loop until we block */ rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem));
rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); fprintf(stderr, "libssh2_sftp_read returned %d\n",
fprintf(stderr, "libssh2_sftp_read returned %d\n", rc);
rc);
if(rc > 0) { if(rc > 0) {
/* write to stderr */ /* write to stderr */
write(2, mem, rc); write(2, mem, rc);
/* write to temporary storage area */ /* write to temporary storage area */
fwrite(mem, rc, 1, tempstorage); fwrite(mem, rc, 1, tempstorage);
} }
} while (rc > 0); } while (rc > 0);
if(rc != LIBSSH2_ERROR_EAGAIN) { if(rc != LIBSSH2_ERROR_EAGAIN) {
/* error or end of file */ /* error or end of file */
break; break;
} }
timeout.tv_sec = 10; timeout.tv_sec = 10;
timeout.tv_usec = 0; timeout.tv_usec = 0;
FD_ZERO(&fd); FD_ZERO(&fd);
FD_SET(sock, &fd); FD_SET(sock, &fd);
/* wait for readable or writeable */ /* wait for readable or writeable */
rc = select(sock+1, &fd, &fd, NULL, &timeout); rc = select(sock+1, &fd, &fd, NULL, &timeout);
if(rc <= 0) { if(rc <= 0) {
/* negative is error /* negative is error
0 is timeout */ 0 is timeout */
fprintf(stderr, "SFTP download timed out: %d\n", rc); fprintf(stderr, "SFTP download timed out: %d\n", rc);
break; break;
} }
} while (1);
libssh2_sftp_close(sftp_handle);
fclose(tempstorage);
tempstorage = fopen(STORAGE, "rb");
if(!tempstorage) {
/* weird, we can't read the file we just wrote to... */
fprintf(stderr, "can't open %s for reading\n", STORAGE);
goto shutdown;
}
/* we're done downloading, now reverse the process and upload the
temporarily stored data to the destination path */
sftp_handle =
libssh2_sftp_open(sftp_session, dest,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if(sftp_handle) {
size_t nread;
char *ptr;
do {
nread = fread(mem, 1, sizeof(mem), tempstorage);
if(nread <= 0) {
/* end of file */
break;
}
ptr = mem;
do {
/* write data in a loop until we block */
rc = libssh2_sftp_write(sftp_handle, ptr,
nread);
ptr += rc;
nread -= nread;
} while (rc > 0);
if(rc != LIBSSH2_ERROR_EAGAIN) {
/* error or end of file */
break;
}
timeout.tv_sec = 10;
timeout.tv_usec = 0;
FD_ZERO(&fd);
FD_SET(sock, &fd);
/* wait for readable or writeable */
rc = select(sock+1, &fd, &fd, NULL, &timeout);
if(rc <= 0) {
/* negative is error
0 is timeout */
fprintf(stderr, "SFTP upload timed out: %d\n",
rc);
break;
}
} while (1); } while (1);
fprintf(stderr, "SFTP upload done!\n");
}
else {
fprintf(stderr, "SFTP failed to open destination path: %s\n",
dest);
}
libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session);
fclose(tempstorage);
tempstorage = fopen(STORAGE, "rb"); shutdown:
if(!tempstorage) {
/* weird, we can't read the file we just wrote to... */
fprintf(stderr, "can't open %s for reading\n", STORAGE);
goto shutdown;
}
/* we're done downloading, now reverse the process and upload the libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
temporarily stored data to the destination path */ libssh2_session_free(session);
sftp_handle =
libssh2_sftp_open(sftp_session, dest,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if(sftp_handle) {
size_t nread;
char *ptr;
do {
nread = fread(mem, 1, sizeof(mem), tempstorage);
if(nread <= 0) {
/* end of file */
break;
}
ptr = mem;
do {
/* write data in a loop until we block */
rc = libssh2_sftp_write(sftp_handle, ptr,
nread);
ptr += rc;
nread -= nread;
} while (rc > 0);
if(rc != LIBSSH2_ERROR_EAGAIN) {
/* error or end of file */
break;
}
timeout.tv_sec = 10;
timeout.tv_usec = 0;
FD_ZERO(&fd);
FD_SET(sock, &fd);
/* wait for readable or writeable */
rc = select(sock+1, &fd, &fd, NULL, &timeout);
if(rc <= 0) {
/* negative is error
0 is timeout */
fprintf(stderr, "SFTP upload timed out: %d\n",
rc);
break;
}
} while (1);
fprintf(stderr, "SFTP upload done!\n");
}
else {
fprintf(stderr, "SFTP failed to open destination path: %s\n",
dest);
}
libssh2_sftp_shutdown(sftp_session);
shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
Sleep(1000); Sleep(1000);
closesocket(sock); closesocket(sock);
#else #else
sleep(1); sleep(1);
close(sock); close(sock);
#endif #endif
printf("all done\n"); printf("all done\n");
return 0; return 0;
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftp_mkdir.c,v 1.4 2007/06/06 12:34:08 jehousley Exp $ * $Id: sftp_mkdir.c,v 1.5 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SFTP mkdir * Sample showing how to do SFTP mkdir
* *
@ -69,7 +69,7 @@ int main(int argc, char *argv[])
if(argc > 4) { if(argc > 4) {
sftppath = argv[4]; sftppath = argv[4];
} }
/* /*
* The application code is responsible for creating the socket * The application code is responsible for creating the socket
* and establishing the connection * and establishing the connection
@ -79,7 +79,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -139,7 +139,7 @@ int main(int argc, char *argv[])
/* Since we have not set non-blocking, tell libssh2 we are blocking */ /* Since we have not set non-blocking, tell libssh2 we are blocking */
libssh2_session_set_blocking(session, 1); libssh2_session_set_blocking(session, 1);
fprintf(stderr, "libssh2_sftp_mkdir()!\n"); fprintf(stderr, "libssh2_sftp_mkdir()!\n");
/* Make a directory via SFTP */ /* Make a directory via SFTP */
rc = libssh2_sftp_mkdir(sftp_session, sftppath, rc = libssh2_sftp_mkdir(sftp_session, sftppath,

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftp_mkdir_nonblock.c,v 1.6 2007/06/08 13:33:08 jehousley Exp $ * $Id: sftp_mkdir_nonblock.c,v 1.7 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SFTP non-blocking mkdir. * Sample showing how to do SFTP non-blocking mkdir.
* *
@ -69,7 +69,7 @@ int main(int argc, char *argv[])
if(argc > 4) { if(argc > 4) {
sftppath = argv[4]; sftppath = argv[4];
} }
/* /*
* The application code is responsible for creating the socket * The application code is responsible for creating the socket
* and establishing the connection * and establishing the connection
@ -79,7 +79,7 @@ int main(int argc, char *argv[])
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
@ -149,7 +149,7 @@ int main(int argc, char *argv[])
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
fprintf(stderr, "libssh2_sftp_mkdirnb()!\n"); fprintf(stderr, "libssh2_sftp_mkdirnb()!\n");
/* Make a directory via SFTP */ /* Make a directory via SFTP */
while ((rc = libssh2_sftp_mkdir(sftp_session, sftppath, while ((rc = libssh2_sftp_mkdir(sftp_session, sftppath,

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftp_nonblock.c,v 1.10 2007/06/08 13:33:08 jehousley Exp $ * $Id: sftp_nonblock.c,v 1.11 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SFTP non-blocking transfers. * Sample showing how to do SFTP non-blocking transfers.
* *
@ -51,19 +51,19 @@ int main(int argc, char *argv[])
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_SFTP_HANDLE *sftp_handle;
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
WSAStartup(WINSOCK_VERSION, &wsadata); WSAStartup(WINSOCK_VERSION, &wsadata);
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if (argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -78,7 +78,7 @@ int main(int argc, char *argv[])
* and establishing the connection * and establishing the connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
@ -87,7 +87,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* We set the socket non-blocking. We do it after the connect just to /* We set the socket non-blocking. We do it after the connect just to
simplify the example code. */ simplify the example code. */
#ifdef F_SETFL #ifdef F_SETFL
@ -97,15 +97,15 @@ int main(int argc, char *argv[])
#else #else
#error "add support for setting the socket non-blocking here" #error "add support for setting the socket non-blocking here"
#endif #endif
/* Create a session instance */ /* Create a session instance */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if (!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
@ -114,7 +114,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
/* At this point we havn't yet authenticated. The first thing to do /* At this point we havn't yet authenticated. The first thing to do
* is check the hostkey's fingerprint against our known hosts Your app * is check the hostkey's fingerprint against our known hosts Your app
* may have it hard coded, may go to a file, may present it to the * may have it hard coded, may go to a file, may present it to the
@ -126,11 +126,11 @@ int main(int argc, char *argv[])
fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]); fprintf(stderr, "%02X ", (unsigned char)fingerprint[i]);
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
@ -140,12 +140,12 @@ int main(int argc, char *argv[])
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) == LIBSSH2_ERROR_EAGAIN); password)) == LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
do { do {
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
@ -155,22 +155,22 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
} while (!sftp_session); } while (!sftp_session);
fprintf(stderr, "libssh2_sftp_open()!\n"); fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */ /* Request a file via SFTP */
do { do {
sftp_handle = libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0); sftp_handle = libssh2_sftp_open(sftp_session, sftppath, LIBSSH2_FXF_READ, 0);
if ((!sftp_handle) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { if ((!sftp_handle) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_handle); } while (!sftp_handle);
fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now receive data!\n");
do { do {
char mem[1024]; char mem[1024];
/* loop until we fail */ /* loop until we fail */
fprintf(stderr, "libssh2_sftp_readnb()!\n"); fprintf(stderr, "libssh2_sftp_readnb()!\n");
while ((rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) { while ((rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) {
@ -182,15 +182,15 @@ int main(int argc, char *argv[])
break; break;
} }
} while (1); } while (1);
libssh2_sftp_close(sftp_handle); libssh2_sftp_close(sftp_handle);
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
shutdown: shutdown:
while ((rc = libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN); while ((rc = libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
Sleep(1000); Sleep(1000);
closesocket(sock); closesocket(sock);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftp_write.c,v 1.5 2007/07/14 20:54:47 bagder Exp $ * $Id: sftp_write.c,v 1.6 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SFTP write transfers. * Sample showing how to do SFTP write transfers.
* *
@ -61,28 +61,28 @@ int main(int argc, char *argv[])
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if(argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if(argc > 4) { if(argc > 4) {
loclfile = argv[4]; loclfile = argv[4];
} }
if(argc > 5) { if(argc > 5) {
sftppath = argv[5]; sftppath = argv[5];
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if (!local) {
printf("Can't local file %s\n", loclfile); printf("Can't local file %s\n", loclfile);
goto shutdown; goto shutdown;
} }
/* /*
@ -95,16 +95,16 @@ int main(int argc, char *argv[])
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* Create a session instance /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if(!session) if(!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are blocking */ /* Since we have set non-blocking, tell libssh2 we are blocking */
libssh2_session_set_blocking(session, 1); libssh2_session_set_blocking(session, 1);
@ -114,8 +114,8 @@ int main(int argc, char *argv[])
*/ */
rc = libssh2_session_startup(session, sock); rc = libssh2_session_startup(session, sock);
if(rc) { if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
/* At this point we havn't yet authenticated. The first thing to do /* At this point we havn't yet authenticated. The first thing to do
@ -126,33 +126,33 @@ int main(int argc, char *argv[])
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
printf("Fingerprint: "); printf("Fingerprint: ");
for(i = 0; i < 16; i++) { for(i = 0; i < 16; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); printf("%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); printf("\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
printf("Authentication by password failed.\n"); printf("Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, if (libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) { password)) {
printf("\tAuthentication by public key failed\n"); printf("\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if (!sftp_session) { if (!sftp_session) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
/* Since we have not set non-blocking, tell libssh2 we are blocking */ /* Since we have not set non-blocking, tell libssh2 we are blocking */
@ -161,30 +161,30 @@ int main(int argc, char *argv[])
fprintf(stderr, "libssh2_sftp_open()!\n"); fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */ /* Request a file via SFTP */
sftp_handle = sftp_handle =
libssh2_sftp_open(sftp_session, sftppath, libssh2_sftp_open(sftp_session, sftppath,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if (!sftp_handle) { if (!sftp_handle) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) { if (nread <= 0) {
/* end of file */ /* end of file */
break; break;
} }
ptr = mem; ptr = mem;
do { do {
/* write data in a loop until we block */ /* write data in a loop until we block */
rc = libssh2_sftp_write(sftp_handle, ptr, nread); rc = libssh2_sftp_write(sftp_handle, ptr, nread);
ptr += rc; ptr += rc;
nread -= nread; nread -= nread;
} while (rc > 0); } while (rc > 0);
} while (1); } while (1);
fclose(local); fclose(local);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftp_write_nonblock.c,v 1.7 2007/06/08 13:33:08 jehousley Exp $ * $Id: sftp_write_nonblock.c,v 1.8 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SFTP non-blocking write transfers. * Sample showing how to do SFTP non-blocking write transfers.
* *
@ -53,19 +53,19 @@ int main(int argc, char *argv[])
char mem[1024]; char mem[1024];
size_t nread; size_t nread;
char *ptr; char *ptr;
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
WSAStartup(WINSOCK_VERSION, &wsadata); WSAStartup(WINSOCK_VERSION, &wsadata);
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if (argc > 2) { if (argc > 2) {
username = argv[2]; username = argv[2];
} }
@ -78,28 +78,28 @@ int main(int argc, char *argv[])
if (argc > 5) { if (argc > 5) {
sftppath = argv[5]; sftppath = argv[5];
} }
local = fopen(loclfile, "rb"); local = fopen(loclfile, "rb");
if (!local) { if (!local) {
printf("Can't local file %s\n", loclfile); printf("Can't local file %s\n", loclfile);
goto shutdown; goto shutdown;
} }
/* /*
* The application code is responsible for creating the socket * The application code is responsible for creating the socket
* and establishing the connection * and establishing the connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* We set the socket non-blocking. We do it after the connect just to /* We set the socket non-blocking. We do it after the connect just to
simplify the example code. */ simplify the example code. */
#ifdef F_SETFL #ifdef F_SETFL
@ -109,26 +109,26 @@ int main(int argc, char *argv[])
#else #else
#error "add support for setting the socket non-blocking here" #error "add support for setting the socket non-blocking here"
#endif #endif
/* Create a session instance /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if (!session) if (!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
/* ... start it up. This will trade welcome banners, exchange keys, /* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers * and setup crypto, compression, and MAC layers
*/ */
while ((rc = libssh2_session_startup(session, sock)) while ((rc = libssh2_session_startup(session, sock))
== LIBSSH2_ERROR_EAGAIN); == LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1; return -1;
} }
/* At this point we havn't yet authenticated. The first thing to do /* At this point we havn't yet authenticated. The first thing to do
* is check the hostkey's fingerprint against our known hosts Your app * is check the hostkey's fingerprint against our known hosts Your app
* may have it hard coded, may go to a file, may present it to the * may have it hard coded, may go to a file, may present it to the
@ -140,7 +140,7 @@ int main(int argc, char *argv[])
printf("%02X ", (unsigned char)fingerprint[i]); printf("%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); printf("\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN);
@ -159,20 +159,20 @@ int main(int argc, char *argv[])
goto shutdown; goto shutdown;
} }
} }
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
do { do {
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if ((!sftp_session) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { if ((!sftp_session) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to init SFTP session\n"); fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_session); } while (!sftp_session);
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
fprintf(stderr, "libssh2_sftp_open()!\n"); fprintf(stderr, "libssh2_sftp_open()!\n");
/* Request a file via SFTP */ /* Request a file via SFTP */
do { do {
@ -181,13 +181,13 @@ int main(int argc, char *argv[])
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC, LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
if ((!sftp_handle) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { if ((!sftp_handle) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open file with SFTP\n"); fprintf(stderr, "Unable to open file with SFTP\n");
goto shutdown; goto shutdown;
} }
} while (!sftp_handle); } while (!sftp_handle);
fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n"); fprintf(stderr, "libssh2_sftp_open() is done, now send data!\n");
do { do {
nread = fread(mem, 1, sizeof(mem), local); nread = fread(mem, 1, sizeof(mem), local);
@ -196,7 +196,7 @@ int main(int argc, char *argv[])
break; break;
} }
ptr = mem; ptr = mem;
do { do {
/* write data in a loop until we block */ /* write data in a loop until we block */
while ((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) == LIBSSH2_ERROR_EAGAIN) { while ((rc = libssh2_sftp_write(sftp_handle, ptr, nread)) == LIBSSH2_ERROR_EAGAIN) {
@ -206,16 +206,16 @@ int main(int argc, char *argv[])
nread -= nread; nread -= nread;
} while (rc > 0); } while (rc > 0);
} while (1); } while (1);
fclose(local); fclose(local);
libssh2_sftp_close(sftp_handle); libssh2_sftp_close(sftp_handle);
libssh2_sftp_shutdown(sftp_session); libssh2_sftp_shutdown(sftp_session);
shutdown: shutdown:
while ((rc = libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN); while ((rc = libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing")) == LIBSSH2_ERROR_EAGAIN);
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
Sleep(1000); Sleep(1000);
closesocket(sock); closesocket(sock);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftpdir.c,v 1.6 2007/06/15 17:22:49 jehousley Exp $ * $Id: sftpdir.c,v 1.7 2007/07/14 21:24:38 bagder Exp $
* *
* Sample doing an SFTP directory listing. * Sample doing an SFTP directory listing.
* *
@ -139,7 +139,7 @@ int main(int argc, char *argv[])
/* Since we have not set non-blocking, tell libssh2 we are blocking */ /* Since we have not set non-blocking, tell libssh2 we are blocking */
libssh2_session_set_blocking(session, 1); libssh2_session_set_blocking(session, 1);
fprintf(stderr, "libssh2_sftp_opendir()!\n"); fprintf(stderr, "libssh2_sftp_opendir()!\n");
/* Request a dir listing via SFTP */ /* Request a dir listing via SFTP */
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath); sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: sftpdir_nonblock.c,v 1.7 2007/06/08 13:33:08 jehousley Exp $ * $Id: sftpdir_nonblock.c,v 1.8 2007/07/14 21:24:38 bagder Exp $
* *
* Sample doing an SFTP directory listing. * Sample doing an SFTP directory listing.
* *
@ -37,197 +37,201 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned long hostaddr; unsigned long hostaddr;
int sock, i, auth_pw = 1; int sock, i, auth_pw = 1;
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
char *username=(char *)"username"; char *username=(char *)"username";
char *password=(char *)"password"; char *password=(char *)"password";
char *sftppath=(char *)"/tmp/secretdir"; char *sftppath=(char *)"/tmp/secretdir";
int rc; int rc;
LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP *sftp_session;
LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_SFTP_HANDLE *sftp_handle;
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
WSAStartup(WINSOCK_VERSION, &wsadata); WSAStartup(WINSOCK_VERSION, &wsadata);
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if(argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
if(argc > 4) { if(argc > 4) {
sftppath = argv[4]; sftppath = argv[4];
} }
/* /*
* The application code is responsible for creating the socket * The application code is responsible for creating the socket
* and establishing the connection * and establishing the connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* We set the socket non-blocking. We do it after the connect just to /* We set the socket non-blocking. We do it after the connect just to
simplify the example code. */ simplify the example code. */
#ifdef F_SETFL #ifdef F_SETFL
/* FIXME: this can/should be done in a more portable manner */ /* FIXME: this can/should be done in a more portable manner */
rc = fcntl(sock, F_GETFL, 0); rc = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, rc | O_NONBLOCK); fcntl(sock, F_SETFL, rc | O_NONBLOCK);
#else #else
#error "add support for setting the socket non-blocking here" #error "add support for setting the socket non-blocking here"
#endif #endif
/* Create a session instance /* Create a session instance
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if(!session) if(!session)
return -1; return -1;
/* Since we have set non-blocking, tell libssh2 we are non-blocking */ /* Since we have set non-blocking, tell libssh2 we are non-blocking */
libssh2_session_set_blocking(session, 0); libssh2_session_set_blocking(session, 0);
/* ... start it up. This will trade welcome banners, exchange keys,
* and setup crypto, compression, and MAC layers
*/
while ((rc = libssh2_session_startup(session, sock)) == LIBSSH2_ERROR_EAGAIN);
if(rc) {
fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
return -1;
}
/* At this point we havn't yet authenticated. The first thing to do /* ... start it up. This will trade welcome banners, exchange keys,
* is check the hostkey's fingerprint against our known hosts Your app * and setup crypto, compression, and MAC layers
* may have it hard coded, may go to a file, may present it to the */
* user, that's your call while ((rc = libssh2_session_startup(session, sock)) == LIBSSH2_ERROR_EAGAIN);
*/ if(rc) {
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fprintf(stderr, "Failure establishing SSH session: %d\n", rc);
printf("Fingerprint: "); return -1;
for(i = 0; i < 16; i++) { }
printf("%02X ", (unsigned char)fingerprint[i]);
} /* At this point we havn't yet authenticated. The first thing to do
printf("\n"); * is check the hostkey's fingerprint against our known hosts Your app
* may have it hard coded, may go to a file, may present it to the
* user, that's your call
*/
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
printf("Fingerprint: ");
for(i = 0; i < 16; i++) {
printf("%02X ", (unsigned char)fingerprint[i]);
}
printf("\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
while ((rc = libssh2_userauth_password(session, username, password)) == LIBSSH2_ERROR_EAGAIN); while ((rc = libssh2_userauth_password(session, username, password)) ==
if (rc) { LIBSSH2_ERROR_EAGAIN);
if (rc) {
fprintf(stderr, "Authentication by password failed.\n"); fprintf(stderr, "Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
/* Or by public key */ /* Or by public key */
while ((rc = libssh2_userauth_publickey_fromfile(session, username, while ((rc = libssh2_userauth_publickey_fromfile(session, username,
"/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa.pub",
"/home/username/.ssh/id_rsa", "/home/username/.ssh/id_rsa",
password)) == LIBSSH2_ERROR_EAGAIN); password)) == LIBSSH2_ERROR_EAGAIN);
if (rc) { if (rc) {
fprintf(stderr, "\tAuthentication by public key failed\n"); fprintf(stderr, "\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
fprintf(stderr, "libssh2_sftp_init()!\n"); fprintf(stderr, "libssh2_sftp_init()!\n");
do { do {
sftp_session = libssh2_sftp_init(session); sftp_session = libssh2_sftp_init(session);
if ((!sftp_session) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { if ((!sftp_session) && (libssh2_session_last_errno(session) !=
fprintf(stderr, "Unable to init SFTP session\n"); LIBSSH2_ERROR_EAGAIN)) {
goto shutdown; fprintf(stderr, "Unable to init SFTP session\n");
goto shutdown;
}
} while (!sftp_session);
fprintf(stderr, "libssh2_sftp_opendir()!\n");
/* Request a dir listing via SFTP */
do {
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
if ((!sftp_handle) && (libssh2_session_last_errno(session) !=
LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open dir with SFTP\n");
goto shutdown;
}
} while (!sftp_handle);
fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
do {
char mem[512];
LIBSSH2_SFTP_ATTRIBUTES attrs;
/* loop until we fail */
while ((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem),
&attrs)) == LIBSSH2_ERROR_EAGAIN) {
;
}
if(rc > 0) {
/* rc is the length of the file name in the mem
buffer */
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
/* this should check what permissions it
is and print the output accordingly */
printf("--fix----- ");
} else {
printf("---------- ");
} }
} while (!sftp_session);
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) {
fprintf(stderr, "libssh2_sftp_opendir()!\n"); printf("%4ld %4ld ", attrs.uid, attrs.gid);
/* Request a dir listing via SFTP */ } else {
do { printf(" - - ");
sftp_handle = libssh2_sftp_opendir(sftp_session, sftppath);
if ((!sftp_handle) && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) {
fprintf(stderr, "Unable to open dir with SFTP\n");
goto shutdown;
} }
} while (!sftp_handle);
fprintf(stderr, "libssh2_sftp_opendir() is done, now receive listing!\n");
do {
char mem[512];
LIBSSH2_SFTP_ATTRIBUTES attrs;
/* loop until we fail */ if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
while ((rc = libssh2_sftp_readdir(sftp_handle, mem, sizeof(mem), &attrs)) == LIBSSH2_ERROR_EAGAIN) { /* attrs.filesize is an uint64_t according to
; the docs but there is no really good and
} portable 64bit type for C before C99, and
if(rc > 0) { correspondingly there was no good printf()
/* rc is the length of the file name in the mem option for it... */
buffer */
if(attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { printf("%8lld ", attrs.filesize);
/* this should check what permissions it }
is and print the output accordingly */
printf("--fix----- ");
} else {
printf("---------- ");
}
if(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID) { printf("%s\n", mem);
printf("%4ld %4ld ", attrs.uid, attrs.gid); }
} else { else if (rc == LIBSSH2_ERROR_EAGAIN) {
printf(" - - "); /* blocking */
} fprintf(stderr, "Blocking\n");
} else {
break;
}
if(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) { } while (1);
/* attrs.filesize is an uint64_t according to
the docs but there is no really good and
portable 64bit type for C before C99, and
correspondingly there was no good printf()
option for it... */
printf("%8lld ", attrs.filesize); libssh2_sftp_closedir(sftp_handle);
} libssh2_sftp_shutdown(sftp_session);
printf("%s\n", mem); shutdown:
}
else if (rc == LIBSSH2_ERROR_EAGAIN) {
/* blocking */
fprintf(stderr, "Blocking\n");
} else {
break;
}
} while (1); libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
libssh2_sftp_closedir(sftp_handle);
libssh2_sftp_shutdown(sftp_session);
shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
Sleep(1000); Sleep(1000);
closesocket(sock); closesocket(sock);
#else #else
sleep(1); sleep(1);
close(sock); close(sock);
#endif #endif
printf("all done\n"); printf("all done\n");
return 0; return 0;
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: ssh2.c,v 1.4 2007/07/14 20:54:47 bagder Exp $ * $Id: ssh2.c,v 1.5 2007/07/14 21:24:38 bagder Exp $
* *
* Sample showing how to do SSH2 connect. * Sample showing how to do SSH2 connect.
* *
@ -37,145 +37,145 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
unsigned long hostaddr; unsigned long hostaddr;
int sock, i, auth_pw = 1; int sock, i, auth_pw = 1;
struct sockaddr_in sin; struct sockaddr_in sin;
const char *fingerprint; const char *fingerprint;
LIBSSH2_SESSION *session; LIBSSH2_SESSION *session;
LIBSSH2_CHANNEL *channel; LIBSSH2_CHANNEL *channel;
char *username=(char *)"username"; char *username=(char *)"username";
char *password=(char *)"password"; char *password=(char *)"password";
#ifdef WIN32 #ifdef WIN32
WSADATA wsadata; WSADATA wsadata;
WSAStartup(WINSOCK_VERSION, &wsadata); WSAStartup(WINSOCK_VERSION, &wsadata);
#endif #endif
if (argc > 1) { if (argc > 1) {
hostaddr = inet_addr(argv[1]); hostaddr = inet_addr(argv[1]);
} else { } else {
hostaddr = htonl(0x7F000001); hostaddr = htonl(0x7F000001);
} }
if(argc > 2) { if(argc > 2) {
username = argv[2]; username = argv[2];
} }
if(argc > 3) { if(argc > 3) {
password = argv[3]; password = argv[3];
} }
/* Ultra basic "connect to port 22 on localhost" /* Ultra basic "connect to port 22 on localhost"
* Your code is responsible for creating the socket establishing the connection * Your code is responsible for creating the socket establishing the connection
*/ */
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
#ifndef WIN32 #ifndef WIN32
fcntl(sock, F_SETFL, 0); fcntl(sock, F_SETFL, 0);
#endif #endif
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_port = htons(22); sin.sin_port = htons(22);
sin.sin_addr.s_addr = hostaddr; sin.sin_addr.s_addr = hostaddr;
if (connect(sock, (struct sockaddr*)(&sin), if (connect(sock, (struct sockaddr*)(&sin),
sizeof(struct sockaddr_in)) != 0) { sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "failed to connect!\n"); fprintf(stderr, "failed to connect!\n");
return -1; return -1;
} }
/* Create a session instance and start it up /* Create a session instance and start it up
* This will trade welcome banners, exchange keys, and setup crypto, compression, and MAC layers * This will trade welcome banners, exchange keys, and setup crypto, compression, and MAC layers
*/ */
session = libssh2_session_init(); session = libssh2_session_init();
if (libssh2_session_startup(session, sock)) { if (libssh2_session_startup(session, sock)) {
fprintf(stderr, "Failure establishing SSH session\n"); fprintf(stderr, "Failure establishing SSH session\n");
return -1; return -1;
} }
/* At this point we havn't authenticated, /* At this point we havn't authenticated,
* The first thing to do is check the hostkey's fingerprint against our known hosts * The first thing to do is check the hostkey's fingerprint against our known hosts
* Your app may have it hard coded, may go to a file, may present it to the user, that's your call * Your app may have it hard coded, may go to a file, may present it to the user, that's your call
*/ */
fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5); fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
printf("Fingerprint: "); printf("Fingerprint: ");
for(i = 0; i < 16; i++) { for(i = 0; i < 16; i++) {
printf("%02X ", (unsigned char)fingerprint[i]); printf("%02X ", (unsigned char)fingerprint[i]);
} }
printf("\n"); printf("\n");
if (auth_pw) { if (auth_pw) {
/* We could authenticate via password */ /* We could authenticate via password */
if (libssh2_userauth_password(session, username, password)) { if (libssh2_userauth_password(session, username, password)) {
printf("Authentication by password failed.\n"); printf("Authentication by password failed.\n");
goto shutdown; goto shutdown;
} }
} else { } else {
/* Or by public key */ /* Or by public key */
if (libssh2_userauth_publickey_fromfile(session, username, "/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa", password)) { if (libssh2_userauth_publickey_fromfile(session, username, "/home/username/.ssh/id_rsa.pub", "/home/username/.ssh/id_rsa", password)) {
printf("\tAuthentication by public key failed\n"); printf("\tAuthentication by public key failed\n");
goto shutdown; goto shutdown;
} }
} }
/* Request a shell */ /* Request a shell */
if (!(channel = libssh2_channel_open_session(session))) { if (!(channel = libssh2_channel_open_session(session))) {
fprintf(stderr, "Unable to open a session\n"); fprintf(stderr, "Unable to open a session\n");
goto shutdown; goto shutdown;
} }
/* Some environment variables may be set, /* Some environment variables may be set,
* It's up to the server which ones it'll allow though * It's up to the server which ones it'll allow though
*/ */
libssh2_channel_setenv(channel, (char *)"FOO", (char *)"bar"); libssh2_channel_setenv(channel, (char *)"FOO", (char *)"bar");
/* Request a terminal with 'vanilla' terminal emulation /* Request a terminal with 'vanilla' terminal emulation
* See /etc/termcap for more options * See /etc/termcap for more options
*/ */
if (libssh2_channel_request_pty(channel, (char *)"vanilla")) { if (libssh2_channel_request_pty(channel, (char *)"vanilla")) {
fprintf(stderr, "Failed requesting pty\n"); fprintf(stderr, "Failed requesting pty\n");
goto skip_shell; goto skip_shell;
} }
/* Open a SHELL on that pty */ /* Open a SHELL on that pty */
if (libssh2_channel_shell(channel)) { if (libssh2_channel_shell(channel)) {
fprintf(stderr, "Unable to request shell on allocated pty\n"); fprintf(stderr, "Unable to request shell on allocated pty\n");
goto shutdown; goto shutdown;
} }
/* At this point the shell can be interacted with using /* At this point the shell can be interacted with using
* libssh2_channel_read() * libssh2_channel_read()
* libssh2_channel_read_stderr() * libssh2_channel_read_stderr()
* libssh2_channel_write() * libssh2_channel_write()
* libssh2_channel_write_stderr() * libssh2_channel_write_stderr()
* *
* Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking() * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking()
* If the server send EOF, libssh2_channel_eof() will return non-0 * If the server send EOF, libssh2_channel_eof() will return non-0
* To send EOF to the server use: libssh2_channel_send_eof() * To send EOF to the server use: libssh2_channel_send_eof()
* A channel can be closed with: libssh2_channel_close() * A channel can be closed with: libssh2_channel_close()
* A channel can be freed with: libssh2_channel_free() * A channel can be freed with: libssh2_channel_free()
*/ */
skip_shell: skip_shell:
if (channel) { if (channel) {
libssh2_channel_free(channel); libssh2_channel_free(channel);
channel = NULL; channel = NULL;
} }
/* Other channel types are supported via: /* Other channel types are supported via:
* libssh2_scp_send() * libssh2_scp_send()
* libssh2_scp_recv() * libssh2_scp_recv()
* libssh2_channel_direct_tcpip() * libssh2_channel_direct_tcpip()
*/ */
shutdown: shutdown:
libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing"); libssh2_session_disconnect(session, "Normal Shutdown, Thank you for playing");
libssh2_session_free(session); libssh2_session_free(session);
#ifdef WIN32 #ifdef WIN32
Sleep(1000); Sleep(1000);
closesocket(sock); closesocket(sock);
#else #else
sleep(1); sleep(1);
close(sock); close(sock);
#endif #endif
printf("all done\n"); printf("all done\n");
return 0; return 0;
} }