Sorry, Its my mistake. I'm sending the program in the atachment. It doesn't include all the parts I've implemented (sftp, work with directories etc for example) but in the terms of this problem it includes everything necessary. Thanks for your patience!
2012/9/23 Peter Stuge <pe...@stuge.se> > Jiri wrote: > > Sample source code (without errors etc) > > ... > > previous code same as direct tcp example > > ... > > You need to send the complete program and not make people guess at > what code you have written. > > > //Peter > _______________________________________________ > libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel > -- S pozdravem Jiří Ševčík
#include "libssh2_config.h" #include <libssh2.h> #include <libssh2_sftp.h> #include "ssh_commands.h" #include <sys/poll.h> #ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> #endif #ifdef HAVE_NETINET_IN_H #include <netinet/in.h> #endif #ifdef HAVE_UNISTD_H #include <unistd.h> #endif #ifdef HAVE_ARPA_INET_H #include <arpa/inet.h> #endif #ifdef HAVE_INTTYPES_H #include <inttypes.h> #endif #include <sys/types.h> #include <fcntl.h> #include <errno.h> #include <stdio.h> #include <ctype.h> #include <sys/select.h> #include <unistd.h> #include <pthread.h> #ifndef INADDR_NONE #define INADDR_NONE (in_addr_t)-1 #endif //structure for connection typedef struct conn { char *hostname; char *username; char *password; int socket; int forwardsocket; int port; int sftp_alive; LIBSSH2_SESSION *session; LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP_HANDLE *sftp_handle; LIBSSH2_CHANNEL *channel; LIBSSH2_LISTENER *listener; LIBSSH2_AGENT *agent; struct libssh2_agent_publickey *identity, *prev_identity; }; int main() { struct conn c; c.hostname = "10.72.6.145"; //Publichost c.username = "root"; c.password = "pass"; c.sftp_session = NULL; c.sftp_alive = 1; struct conn *st = &c; //methods for create socks, session, handshake.. if(init_sock(st) == 1) return -1; init_session(st); handshake(st); fingerprint(st->session); auth_pass(st); //this is it testf(st); //close and "cleanup" if (c.sftp_alive == 0) sftp_close(st); close_session(st); close_sock(st); libssh2_exit(); printf("\nclose all"); //return 0; } int testf(struct conn *con){ init_lib(); struct conn c; const char *local_listenip = "127.0.0.1"; unsigned int local_listenport = 2222; const char *remote_desthost = "10.0.0.1"; // InternalHost unsigned int remote_destport = 22; const char *shost; unsigned int sport; int listensock = -1, forwardsock = -1; struct sockaddr_in sin; socklen_t sinlen; int sockopt; listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); sin.sin_family = AF_INET; sin.sin_port = htons(local_listenport); if (INADDR_NONE == (sin.sin_addr.s_addr = inet_addr(local_listenip))) { perror("inet_addr"); return -1; } sockopt = 1; setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)); sinlen=sizeof(sin); if (-1 == bind(listensock, (struct sockaddr *)&sin, sinlen)) { perror("bind"); return -1; } if (-1 == listen(listensock, 2)) { perror("listen"); return -1; } printf("Waiting for TCP connection on %s:%d...\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port)); set_nonblock(listensock); forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen); if (-1 == forwardsock) { perror("accept"); //return -1; } shost = inet_ntoa(sin.sin_addr); sport = ntohs(sin.sin_port); printf("Forwarding connection from %s:%d here to remote %s:%d\n", shost, sport, remote_desthost, remote_destport); con->channel = libssh2_channel_direct_tcpip_ex(con->session, remote_desthost,remote_destport, shost, sport); if (!con->channel) { fprintf(stderr, "Could not open the direct-tcpip channel!\n"); } libssh2_session_set_blocking(con->session, 0); //make new connection struct conn c1; c1.hostname = "127.0.0.1"; c1.username = "root"; c1.password = "596603142"; c1.port = 2222; c1.sftp_alive = 1; struct conn *st1 = &c1; //create socket init_sock(st1); //create session init_session(st1); int x; x = libssh2_session_handshake(st1->session, st1->socket); printf("handshake %i\n", x); // HANDSHAKE WILL RETURN -37 return 0; } //Some of my implemented methods for creating connections, sockets, sftp etc.. int init_lib(){ if (libssh2_init (0) != 0) { fprintf (stderr, "libssh2 initialization failed\n"); return -1; } return 0; } int init_sock(struct conn *con){ unsigned long hostaddr; hostaddr = inet_addr(con->hostname); struct sockaddr_in sin; //con->socket = socket(AF_INET, SOCK_STREAM, 0); con->socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(con->socket == -1){ perror("Create socket"); return -1; } sin.sin_family = AF_INET; if (INADDR_NONE == (sin.sin_addr.s_addr = hostaddr)) { perror("inet_addr"); return -1; } sin.sin_port = htons(con->port); //sin.sin_addr.s_addr = hostaddr; if (connect(con->socket, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "Cant connect socket\n"); return -1;} else{ printf("ok, %i \n", con->socket); return 0; } } int close_sock(struct conn *con){ if (close(con->socket)){ fprintf(stderr, "Cant close socket\n"); return -1; } return 0; } int init_session(struct conn *con){ con->session = libssh2_session_init(); if (!con->session){ fprintf(stderr, "Cant init session"); return -1; } //libssh2_session_set_blocking(con->session, 0); //printf("Session is in unblocking mode\n"); return 0; } int close_session(struct conn *con){ while (libssh2_session_disconnect(con->session, "Disconnect session") == LIBSSH2_ERROR_EAGAIN); if(libssh2_session_free(con->session) !=0){ perror("Close session"); return -1; } printf("Session is closed"); return 0; } int close_channel(LIBSSH2_CHANNEL *channel){ libssh2_channel_wait_closed(channel); libssh2_channel_free(channel); channel = NULL; printf("Channel closed\n"); return 0; } int handshake(struct conn *con){ int rc; while ((rc = libssh2_session_handshake(con->session, con->socket))== LIBSSH2_ERROR_EAGAIN); if (rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; } return 0; } int fingerprint(LIBSSH2_SESSION *session){ const char *fp; fp = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); if(!fp){ perror("Fingerprint"); return -1; } else{ printf("Fingerprint: "); for(int i= 0; i < 20; i++) { printf("%02X ", (unsigned char)fp[i]); } printf("\n"); return 0; } } int auth_pass(struct conn *con){ int rc; while ((rc = libssh2_userauth_password(con->session, con->username, con->password)) == LIBSSH2_ERROR_EAGAIN); if (rc) { fprintf(stderr, "Authentication by password failed.\n"); return -1; } else{ printf("Authentication by password sucessfull.\n"); return 0; } } int waitsocket(struct conn *con){ struct timeval timeout; int rc; fd_set fd; fd_set *writefd = NULL; fd_set *readfd = NULL; int dir; timeout.tv_sec = 10; timeout.tv_usec = 0; FD_ZERO(&fd); FD_SET(con->socket, &fd); /* now make sure we wait in the correct direction */ dir = libssh2_session_block_directions(con->session); if(dir & LIBSSH2_SESSION_BLOCK_INBOUND) readfd = &fd; if(dir & LIBSSH2_SESSION_BLOCK_OUTBOUND) writefd = &fd; rc = select(con->socket + 1, readfd, writefd, NULL, &timeout); return rc; } int set_nonblock(int sock){ int opts; opts = fcntl(sock,F_GETFL); if (opts < 0) { perror("fcntl(F_GETFL)"); return -1; } opts = (opts | O_NONBLOCK); if (fcntl(sock,F_SETFL,opts) < 0) { perror("fcntl(F_SETFL)"); return -1; } return 0; } int init_sftp(struct conn *con){ if (con->sftp_alive == 1) { do { con->sftp_session = libssh2_sftp_init(con->session); if (!con->sftp_session && (libssh2_session_last_errno(con->session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to init SFTP session\n"); return -1; } } while (!con->sftp_session); con->sftp_alive = 0; printf("SFTP Init ok\n"); return 0; } else{ printf("SFTP Init ok\n"); return 0; } } int open_sftp_r(struct conn *con, char *file_path){ init_sftp(con); do { con->sftp_handle = libssh2_sftp_open(con->sftp_session, file_path, LIBSSH2_FXF_READ, 0); if (!con->sftp_handle) { if (libssh2_session_last_errno(con->session) != LIBSSH2_ERROR_EAGAIN) { fprintf(stderr, "Unable to open file with SFTP\n"); return -1; } else { fprintf(stderr, "non-blocking open\n"); waitsocket(con); /* now we wait */ } } } while (!con->sftp_handle); return 0; } int sftp_read(struct conn *con){ int rc; char mem[1000]; printf("Sftp read start"); do { /* loop until we fail */ while ((rc = libssh2_sftp_read(con->sftp_handle, mem, sizeof(mem))) == LIBSSH2_ERROR_EAGAIN) { waitsocket(con); /* now we wait */ } if (rc > 0) { write(1, mem, rc); //dat zapis do souboru } else { break; } } while (1); printf("Sftp read end"); libssh2_sftp_close_handle(con->sftp_handle); return 0; } int sftp_close(struct conn *con){ int rc; //libssh2_sftp_close(con->sftp_handle); if ((rc = libssh2_sftp_shutdown(con->sftp_session) !=0)){ perror("Sftp shutdown"); return -1; } else{ con->sftp_alive = 1; return 0; } } int exe_com(struct conn *con, char *command){ init_sftp(con); int rc; int bytecount = 0; while( (con->channel = libssh2_channel_open_session(con->session)) == NULL && libssh2_session_last_error(con->session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN ){ waitsocket(con); } if( con->channel == NULL ){ perror("Open channel"); return -1; } while( (rc = libssh2_channel_exec(con->channel, command)) == LIBSSH2_ERROR_EAGAIN ){ waitsocket(con); } if( rc != 0 ){ fprintf(stderr,"Cannot execute command\n"); return -1; } for(;;){ do { char buffer[0x4000]; rc = libssh2_channel_read( con->channel, buffer, sizeof(buffer) ); if ( rc > 0 ) { int i; bytecount += rc; fprintf(stderr, "We read:\n"); for( i=0; i < rc; ++i ) fputc( buffer[i], stderr); fprintf(stderr, "\n"); } else { if( rc != LIBSSH2_ERROR_EAGAIN ) { /* if (rc == 0){ printf("Channel end\n"); return 0; } else{*/ fprintf(stderr, "libssh2_channel_read returned %d\n", rc); return -1; //} } } }while( rc > 0 ); if( rc == LIBSSH2_ERROR_EAGAIN ){ waitsocket(con); } else{ break; } } while( (rc = libssh2_channel_close(con->channel)) == LIBSSH2_ERROR_EAGAIN ) waitsocket(con); return 0; } int mk_dir(struct conn *con, char *folder_path, int mode){ int rc; init_sftp(con); while ((rc = libssh2_sftp_mkdir(con->sftp_session, folder_path, mode)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(con);} if(rc !=0){ fprintf(stderr, "Cannot create directory \n"); return -1; } printf("Folder created\n"); return 0; //sftp_close(con); } int rm_dir(struct conn *con, char *folder_path){ int rc; init_sftp(con); while ((rc = libssh2_sftp_rmdir(con->sftp_session, folder_path)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(con);} if(rc !=0){ fprintf(stderr,"Cannot remove directory\n"); return -1; } printf("Folder deleted\n"); return 0; //sftp_close(con); }
_______________________________________________ libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel