diff --git a/example/direct_tcpip.c b/example/direct_tcpip.c index cf19fed3..baa55a72 100644 --- a/example/direct_tcpip.c +++ b/example/direct_tcpip.c @@ -10,6 +10,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif #ifdef HAVE_NETINET_IN_H #include #endif @@ -20,34 +26,28 @@ #include #endif +#include #include #include #include #include -#ifdef HAVE_UNISTD_H -#include -#endif -#include -#ifdef HAVE_SYS_SELECT_H -#include -#endif #ifndef INADDR_NONE -#define INADDR_NONE (in_addr_t)-1 +#define INADDR_NONE (in_addr_t)~0 #endif -const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; -const char *keyfile2 = "/home/username/.ssh/id_rsa"; -const char *username = "username"; -const char *password = ""; +static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; +static const char *privkey = "/home/username/.ssh/id_rsa"; +static const char *username = "username"; +static const char *password = ""; -const char *server_ip = "127.0.0.1"; +static const char *server_ip = "127.0.0.1"; -const char *local_listenip = "127.0.0.1"; -unsigned int local_listenport = 2222; +static const char *local_listenip = "127.0.0.1"; +static unsigned int local_listenport = 2222; -const char *remote_desthost = "localhost"; /* resolved by the server */ -unsigned int remote_destport = 22; +static const char *remote_desthost = "localhost"; /* resolved by the server */ +static unsigned int remote_destport = 22; enum { AUTH_NONE = 0, @@ -112,24 +112,19 @@ int main(int argc, char *argv[]) /* Connect to SSH server */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(sock == LIBSSH2_INVALID_SOCKET) { -#ifdef WIN32 fprintf(stderr, "failed to open socket!\n"); -#else - perror("socket"); -#endif return -1; } sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr(server_ip); if(INADDR_NONE == sin.sin_addr.s_addr) { - perror("inet_addr"); + fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip); return -1; } sin.sin_port = htons(22); - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { + fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr)); return -1; } @@ -184,8 +179,9 @@ int main(int argc, char *argv[]) } } else if(auth & AUTH_PUBLICKEY) { - if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; } @@ -198,11 +194,7 @@ int main(int argc, char *argv[]) listensock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(listensock == LIBSSH2_INVALID_SOCKET) { -#ifdef WIN32 fprintf(stderr, "failed to open listen socket!\n"); -#else - perror("socket"); -#endif return -1; } @@ -231,11 +223,7 @@ int main(int argc, char *argv[]) forwardsock = accept(listensock, (struct sockaddr *)&sin, &sinlen); if(forwardsock == LIBSSH2_INVALID_SOCKET) { -#ifdef WIN32 fprintf(stderr, "failed to accept forward socket!\n"); -#else - perror("accept"); -#endif goto shutdown; } diff --git a/example/scp.c b/example/scp.c index 6dec695a..7a917ba8 100644 --- a/example/scp.c +++ b/example/scp.c @@ -6,17 +6,18 @@ #include #ifdef WIN32 -# define write(f, b, c) write((f), (b), (unsigned int)(c)) +#define write(f, b, c) write((f), (b), (unsigned int)(c)) #endif + #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -39,6 +40,8 @@ int main(int argc, char *argv[]) const char *fingerprint; LIBSSH2_SESSION *session; LIBSSH2_CHANNEL *channel; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *scppath = "/tmp/TEST"; @@ -88,8 +91,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -130,10 +132,8 @@ int main(int argc, char *argv[]) } else { /* Or by public key */ -#define HOME_DIR "/home/username/" if(libssh2_userauth_publickey_fromfile(session, username, - HOME_DIR ".ssh/id_rsa.pub", - HOME_DIR ".ssh/id_rsa", + pubkey, privkey, password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; @@ -174,10 +174,9 @@ int main(int argc, char *argv[]) libssh2_channel_free(channel); channel = NULL; - shutdown: +shutdown: - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/scp_nonblock.c b/example/scp_nonblock.c index 9ee48529..ea07f320 100644 --- a/example/scp_nonblock.c +++ b/example/scp_nonblock.c @@ -11,20 +11,21 @@ #include #ifdef WIN32 -# define write(f, b, c) write((f), (b), (unsigned int)(c)) +#define write(f, b, c) write((f), (b), (unsigned int)(c)) #endif + #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -42,8 +43,8 @@ /* diff in ms */ static long tvdiff(struct timeval newer, struct timeval older) { - return (newer.tv_sec-older.tv_sec)*1000+ - (newer.tv_usec-older.tv_usec)/1000; + return (newer.tv_sec-older.tv_sec)*1000+ + (newer.tv_usec-older.tv_usec)/1000; } #endif @@ -86,6 +87,8 @@ int main(int argc, char *argv[]) const char *fingerprint; LIBSSH2_SESSION *session; LIBSSH2_CHANNEL *channel; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *scppath = "/tmp/TEST"; @@ -193,10 +196,7 @@ int main(int argc, char *argv[]) else { /* Or by public key */ while((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/" - ".ssh/id_rsa.pub", - "/home/username/" - ".ssh/id_rsa", + pubkey, privkey, password)) == LIBSSH2_ERROR_EAGAIN); if(rc) { @@ -277,8 +277,7 @@ int main(int argc, char *argv[]) shutdown: - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/scp_write.c b/example/scp_write.c index 76bbabd3..e95c0bb6 100644 --- a/example/scp_write.c +++ b/example/scp_write.c @@ -8,12 +8,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -36,6 +36,8 @@ int main(int argc, char *argv[]) const char *fingerprint; LIBSSH2_SESSION *session = NULL; LIBSSH2_CHANNEL *channel; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *loclfile = "scp_write.c"; @@ -104,8 +106,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -146,10 +147,8 @@ int main(int argc, char *argv[]) } else { /* Or by public key */ -#define HOME "/home/username/" if(libssh2_userauth_publickey_fromfile(session, username, - HOME ".ssh/id_rsa.pub", - HOME ".ssh/id_rsa", + pubkey, privkey, password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; @@ -206,7 +205,7 @@ int main(int argc, char *argv[]) libssh2_channel_free(channel); channel = NULL; - shutdown: +shutdown: if(session) { libssh2_session_disconnect(session, "Normal Shutdown"); diff --git a/example/scp_write_nonblock.c b/example/scp_write_nonblock.c index 50f9de5a..372e461f 100644 --- a/example/scp_write_nonblock.c +++ b/example/scp_write_nonblock.c @@ -8,15 +8,15 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -70,6 +70,8 @@ int main(int argc, char *argv[]) const char *fingerprint; LIBSSH2_SESSION *session = NULL; LIBSSH2_CHANNEL *channel; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *loclfile = "scp_write.c"; @@ -138,8 +140,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -186,10 +187,8 @@ int main(int argc, char *argv[]) } else { /* Or by public key */ -#define HOME "/home/username/" while((rc = libssh2_userauth_publickey_fromfile(session, username, - HOME ".ssh/id_rsa.pub", - HOME ".ssh/id_rsa", + pubkey, privkey, password)) == LIBSSH2_ERROR_EAGAIN); if(rc) { @@ -265,11 +264,10 @@ int main(int argc, char *argv[]) libssh2_channel_free(channel); channel = NULL; - shutdown: +shutdown: - while(libssh2_session_disconnect(session, - "Normal Shutdown,") == - LIBSSH2_ERROR_EAGAIN); + while(libssh2_session_disconnect(session, "Normal Shutdown") + == LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp.c b/example/sftp.c index 2d2394fc..c3bf835f 100644 --- a/example/sftp.c +++ b/example/sftp.c @@ -12,17 +12,18 @@ #include #ifdef WIN32 -# define write(f, b, c) write((f), (b), (unsigned int)(c)) +#define write(f, b, c) write((f), (b), (unsigned int)(c)) #endif + #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -36,12 +37,11 @@ #include #include -const char *keyfile1 = "~/.ssh/id_rsa.pub"; -const char *keyfile2 = "~/.ssh/id_rsa"; -const char *username = "username"; -const char *password = "password"; -const char *sftppath = "/tmp/TEST"; - +static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; +static const char *privkey = "/home/username/.ssh/id_rsa"; +static const char *username = "username"; +static const char *password = "password"; +static const char *sftppath = "/tmp/TEST"; static void kbd_callback(const char *name, int name_len, const char *instruction, int instruction_len, @@ -91,7 +91,6 @@ static void kbd_callback(const char *name, int name_len, "Done. Sending keyboard-interactive responses to server now.\n"); } - int main(int argc, char *argv[]) { uint32_t hostaddr; @@ -122,7 +121,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -148,8 +146,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -233,8 +230,9 @@ int main(int argc, char *argv[]) } else if(auth_pw & 4) { /* Or by public key */ - if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; } @@ -284,7 +282,7 @@ int main(int argc, char *argv[]) libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session); - shutdown: +shutdown: libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); diff --git a/example/sftp_RW_nonblock.c b/example/sftp_RW_nonblock.c index f6728b09..82914548 100644 --- a/example/sftp_RW_nonblock.c +++ b/example/sftp_RW_nonblock.c @@ -12,20 +12,21 @@ #include #ifdef WIN32 -# define write(f, b, c) write((f), (b), (unsigned int)(c)) +#define write(f, b, c) write((f), (b), (unsigned int)(c)) #endif + #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -80,6 +81,8 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *sftppath = "/tmp/TEST"; /* source path */ @@ -119,8 +122,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = htonl(0x7F000001); - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -186,10 +188,7 @@ int main(int argc, char *argv[]) /* Or by public key */ while((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/" - ".ssh/id_rsa.pub", - "/home/username/" - ".ssh/id_rsa", + pubkey, privkey, password)) == LIBSSH2_ERROR_EAGAIN); if(rc) { @@ -341,7 +340,7 @@ int main(int argc, char *argv[]) libssh2_sftp_shutdown(sftp_session); - shutdown: +shutdown: libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); diff --git a/example/sftp_append.c b/example/sftp_append.c index 63d9e31d..0684ce89 100644 --- a/example/sftp_append.c +++ b/example/sftp_append.c @@ -14,12 +14,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -38,6 +38,8 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *loclfile = "sftp_write.c"; @@ -69,7 +71,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -104,8 +105,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -149,10 +149,8 @@ int main(int argc, char *argv[]) } else { /* Or by public key */ -#define HOME "/home/username/" if(libssh2_userauth_publickey_fromfile(session, username, - HOME ".ssh/id_rsa.pub", - HOME ".ssh/id_rsa", + pubkey, privkey, password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; @@ -218,8 +216,7 @@ int main(int argc, char *argv[]) libssh2_sftp_shutdown(sftp_session); shutdown: - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp_mkdir.c b/example/sftp_mkdir.c index 0ce3307b..ab3a56c6 100644 --- a/example/sftp_mkdir.c +++ b/example/sftp_mkdir.c @@ -14,12 +14,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -38,6 +38,8 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *sftppath = "/tmp/sftp_mkdir"; @@ -61,7 +63,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -87,8 +88,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -130,9 +130,8 @@ int main(int argc, char *argv[]) else { /* Or by public key */ if(libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { + pubkey, privkey, + password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -159,7 +158,7 @@ int main(int argc, char *argv[]) libssh2_sftp_shutdown(sftp_session); - shutdown: +shutdown: libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); diff --git a/example/sftp_mkdir_nonblock.c b/example/sftp_mkdir_nonblock.c index 68a46d9c..6d636cb0 100644 --- a/example/sftp_mkdir_nonblock.c +++ b/example/sftp_mkdir_nonblock.c @@ -14,12 +14,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -38,6 +38,8 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *sftppath = "/tmp/sftp_mkdir_nonblock"; @@ -61,7 +63,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -87,8 +88,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -130,9 +130,8 @@ int main(int argc, char *argv[]) else { /* Or by public key */ if(libssh2_userauth_publickey_fromfile(session, username, - "/home/username/.ssh/id_rsa.pub", - "/home/username/.ssh/id_rsa", - password)) { + pubkey, privkey, + password)) { fprintf(stderr, "\tAuthentication by public key failed\n"); goto shutdown; } @@ -159,7 +158,7 @@ int main(int argc, char *argv[]) libssh2_sftp_shutdown(sftp_session); - shutdown: +shutdown: libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); diff --git a/example/sftp_nonblock.c b/example/sftp_nonblock.c index 658243b7..4fcdaa18 100644 --- a/example/sftp_nonblock.c +++ b/example/sftp_nonblock.c @@ -12,20 +12,21 @@ #include #ifdef WIN32 -# define write(f, b, c) write((f), (b), (unsigned int)(c)) +#define write(f, b, c) write((f), (b), (unsigned int)(c)) #endif + #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -86,6 +87,8 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *sftppath = "/tmp/TEST"; @@ -117,7 +120,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -143,8 +145,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -196,10 +197,7 @@ int main(int argc, char *argv[]) /* Or by public key */ while((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/username/" - ".ssh/id_rsa.pub", - "/home/username/" - ".ssh/id_rsa", + pubkey, privkey, password)) == LIBSSH2_ERROR_EAGAIN); if(rc) { @@ -281,9 +279,8 @@ int main(int argc, char *argv[]) shutdown: fprintf(stderr, "libssh2_session_disconnect\n"); - while(libssh2_session_disconnect(session, - "Normal Shutdown, Thank you") == - LIBSSH2_ERROR_EAGAIN); + while(libssh2_session_disconnect(session, "Normal Shutdown") + == LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp_write.c b/example/sftp_write.c index 85823a2d..dba15e65 100644 --- a/example/sftp_write.c +++ b/example/sftp_write.c @@ -14,12 +14,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -38,6 +38,8 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *loclfile = "sftp_write.c"; @@ -68,7 +70,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -103,8 +104,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -148,8 +148,6 @@ int main(int argc, char *argv[]) } else { /* Or by public key */ - const char *pubkey = "/home/username/.ssh/id_rsa.pub"; - const char *privkey = "/home/username/.ssh/id_rsa.pub"; if(libssh2_userauth_publickey_fromfile(session, username, pubkey, privkey, password)) { @@ -202,8 +200,7 @@ int main(int argc, char *argv[]) libssh2_sftp_shutdown(sftp_session); shutdown: - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp_write_nonblock.c b/example/sftp_write_nonblock.c index b5219b59..79343436 100644 --- a/example/sftp_write_nonblock.c +++ b/example/sftp_write_nonblock.c @@ -14,15 +14,15 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -75,6 +75,8 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *loclfile = "sftp_write_nonblock.c"; @@ -108,7 +110,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -143,8 +144,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -191,8 +191,6 @@ int main(int argc, char *argv[]) } else { /* Or by public key */ - const char *pubkey = "/home/username/.ssh/id_rsa.pub"; - const char *privkey = "/home/username/.ssh/id_rsa"; while((rc = libssh2_userauth_publickey_fromfile(session, username, pubkey, privkey, password)) == @@ -270,7 +268,7 @@ int main(int argc, char *argv[]) shutdown: while(libssh2_session_disconnect(session, "Normal Shutdown") - == LIBSSH2_ERROR_EAGAIN); + == LIBSSH2_ERROR_EAGAIN); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/sftp_write_sliding.c b/example/sftp_write_sliding.c index 8950979e..1e8ffbef 100644 --- a/example/sftp_write_sliding.c +++ b/example/sftp_write_sliding.c @@ -14,15 +14,15 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -75,6 +75,8 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *loclfile = "sftp_write_nonblock.c"; @@ -108,7 +110,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -143,14 +144,13 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } /* Create a session instance - */ + */ session = libssh2_session_init(); if(!session) return -1; @@ -168,10 +168,10 @@ int main(int argc, char *argv[]) return -1; } - /* At this point we have not yet authenticated. 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 + /* At this point we have not yet authenticated. 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 */ fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fprintf(stderr, "Fingerprint: "); @@ -191,10 +191,8 @@ int main(int argc, char *argv[]) } else { /* Or by public key */ -#define PUBKEY "/home/username/.ssh/id_rsa.pub" -#define PRIVKEY "/home/username/.ssh/id_rsa" while((rc = libssh2_userauth_publickey_fromfile(session, username, - PUBKEY, PRIVKEY, + pubkey, privkey, password)) == LIBSSH2_ERROR_EAGAIN); if(rc) { @@ -223,7 +221,6 @@ int main(int argc, char *argv[]) LIBSSH2_FXF_TRUNC, LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR| LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH); - if(!sftp_handle && (libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN)) { fprintf(stderr, "Unable to open file with SFTP\n"); diff --git a/example/sftpdir.c b/example/sftpdir.c index 5e2993f7..94190cdf 100644 --- a/example/sftpdir.c +++ b/example/sftpdir.c @@ -14,12 +14,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -39,10 +39,10 @@ #define __FILESIZE "llu" #endif -const char *keyfile1 = "~/.ssh/id_rsa.pub"; -const char *keyfile2 = "~/.ssh/id_rsa"; -const char *username = "username"; -const char *password = "password"; +static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; +static const char *privkey = "/home/username/.ssh/id_rsa"; +static const char *username = "username"; +static const char *password = "password"; static void kbd_callback(const char *name, int name_len, const char *instruction, int instruction_len, @@ -93,7 +93,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -119,8 +118,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -204,8 +202,9 @@ int main(int argc, char *argv[]) } else if(auth_pw & 4) { /* Or by public key */ - if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; } @@ -277,15 +276,16 @@ int main(int argc, char *argv[]) printf("%s\n", mem); } } - else + else { break; + } } while(1); libssh2_sftp_closedir(sftp_handle); libssh2_sftp_shutdown(sftp_session); - shutdown: +shutdown: libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); diff --git a/example/sftpdir_nonblock.c b/example/sftpdir_nonblock.c index 1bb9eb46..cc8d888b 100644 --- a/example/sftpdir_nonblock.c +++ b/example/sftpdir_nonblock.c @@ -14,12 +14,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -47,11 +47,11 @@ int main(int argc, char *argv[]) struct sockaddr_in sin; const char *fingerprint; LIBSSH2_SESSION *session; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; const char *username = "username"; const char *password = "password"; const char *sftppath = "/tmp/secretdir"; - const char *pubkey = "/home/username/.ssh/id_rsa.pub"; - const char *privkey = "/home/username/.ssh/id_rsa"; int rc; LIBSSH2_SFTP *sftp_session; LIBSSH2_SFTP_HANDLE *sftp_handle; @@ -73,7 +73,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -99,8 +98,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -229,7 +227,7 @@ int main(int argc, char *argv[]) libssh2_sftp_closedir(sftp_handle); libssh2_sftp_shutdown(sftp_session); - shutdown: +shutdown: libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); diff --git a/example/ssh2.c b/example/ssh2.c index 9accc3ec..a762d705 100644 --- a/example/ssh2.c +++ b/example/ssh2.c @@ -18,32 +18,31 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif -#include #include #include #include #include +#include #include #if defined(_MSC_VER) && _MSC_VER < 1900 #define snprintf _snprintf #endif -const char *keyfile1 = ".ssh/id_rsa.pub"; -const char *keyfile2 = ".ssh/id_rsa"; -const char *username = "username"; -const char *password = "password"; - +static const char *pubkey = ".ssh/id_rsa.pub"; +static const char *privkey = ".ssh/id_rsa"; +static const char *username = "username"; +static const char *password = "password"; static void kbd_callback(const char *name, int name_len, const char *instruction, int instruction_len, @@ -62,8 +61,7 @@ static void kbd_callback(const char *name, int name_len, } (void)prompts; (void)abstract; -} /* kbd_callback */ - +} int main(int argc, char *argv[]) { @@ -92,7 +90,6 @@ int main(int argc, char *argv[]) else { hostaddr = htonl(0x7F000001); } - if(argc > 2) { username = argv[2]; } @@ -118,8 +115,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "Connecting to %s:%d as user %s\n", inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), username); - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -146,10 +142,10 @@ int main(int argc, char *argv[]) return -1; } - /* At this point we have not authenticated. 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 + /* At this point we have not yet authenticated. 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 */ fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fprintf(stderr, "Fingerprint: "); @@ -215,8 +211,8 @@ int main(int argc, char *argv[]) char const *h = getenv("HOME"); if(!h || !*h) h = "."; - fn1sz = strlen(h) + strlen(keyfile1) + 2; - fn2sz = strlen(h) + strlen(keyfile2) + 2; + fn1sz = strlen(h) + strlen(pubkey) + 2; + fn2sz = strlen(h) + strlen(privkey) + 2; fn1 = malloc(fn1sz); fn2 = malloc(fn2sz); if(!fn1 || !fn2) { @@ -226,11 +222,12 @@ int main(int argc, char *argv[]) goto shutdown; } /* Using asprintf() here would be much cleaner, but less portable */ - snprintf(fn1, fn1sz, "%s/%s", h, keyfile1); - snprintf(fn2, fn2sz, "%s/%s", h, keyfile2); + snprintf(fn1, fn1sz, "%s/%s", h, pubkey); + snprintf(fn2, fn2sz, "%s/%s", h, privkey); - if(libssh2_userauth_publickey_fromfile(session, username, fn1, - fn2, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, + fn1, fn2, + password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); free(fn2); free(fn1); @@ -284,18 +281,19 @@ int main(int argc, char *argv[]) } #endif -/* At this point the shell can be interacted with using - * libssh2_channel_read() - * libssh2_channel_read_stderr() - * libssh2_channel_write() - * libssh2_channel_write_stderr() - * - * Blocking mode may be (en|dis)abled with: libssh2_channel_set_blocking() - * If the server send EOF, libssh2_channel_eof() will return non-0 - * To send EOF to the server use: libssh2_channel_send_eof() - * A channel can be closed with: libssh2_channel_close() - * A channel can be freed with: libssh2_channel_free() - */ + /* At this point the shell can be interacted with using + * libssh2_channel_read() + * libssh2_channel_read_stderr() + * libssh2_channel_write() + * libssh2_channel_write_stderr() + * + * Blocking mode may be (en|dis)abled with: + * libssh2_channel_set_blocking() + * If the server send EOF, libssh2_channel_eof() will return non-0 + * To send EOF to the server use: libssh2_channel_send_eof() + * A channel can be closed with: libssh2_channel_close() + * A channel can be freed with: libssh2_channel_free() + */ /* Read and display all the data received on stdout (ignoring stderr) * until the channel closes. This will eventually block if the command @@ -331,10 +329,9 @@ int main(int argc, char *argv[]) * libssh2_channel_direct_tcpip() */ - shutdown: +shutdown: - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 @@ -342,7 +339,7 @@ int main(int argc, char *argv[]) #else close(sock); #endif - fprintf(stderr, "all done!\n"); + fprintf(stderr, "all done\n"); libssh2_exit(); diff --git a/example/ssh2_agent.c b/example/ssh2_agent.c index 5d7802da..5e3a82f3 100644 --- a/example/ssh2_agent.c +++ b/example/ssh2_agent.c @@ -13,12 +13,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -27,10 +27,10 @@ #include #include #include -#include #include +#include -const char *username = "username"; +static const char *username = "username"; int main(int argc, char *argv[]) { @@ -86,8 +86,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); goto shutdown; } @@ -101,10 +100,10 @@ int main(int argc, char *argv[]) return 1; } - /* At this point we have not authenticated. 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 + /* At this point we have not yet authenticated. 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 */ fingerprint = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1); fprintf(stderr, "Fingerprint: "); @@ -208,7 +207,7 @@ int main(int argc, char *argv[]) * A channel can be freed with: libssh2_channel_free() */ - skip_shell: +skip_shell: if(channel) { libssh2_channel_free(channel); channel = NULL; @@ -220,7 +219,7 @@ int main(int argc, char *argv[]) * libssh2_channel_direct_tcpip() */ - shutdown: +shutdown: if(agent) { libssh2_agent_disconnect(agent); @@ -228,8 +227,7 @@ int main(int argc, char *argv[]) } if(session) { - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); } @@ -241,7 +239,7 @@ int main(int argc, char *argv[]) #endif } - fprintf(stderr, "all done!\n"); + fprintf(stderr, "all done\n"); libssh2_exit(); diff --git a/example/ssh2_agent_forwarding.c b/example/ssh2_agent_forwarding.c index 5a482efd..0a03113d 100644 --- a/example/ssh2_agent_forwarding.c +++ b/example/ssh2_agent_forwarding.c @@ -19,27 +19,27 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif - #ifdef HAVE_SYS_TIME_H #include #endif + #include -#include #include #include #include +#include #include static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session) @@ -122,8 +122,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -273,8 +272,7 @@ int main(int argc, char *argv[]) shutdown: - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/ssh2_echo.c b/example/ssh2_echo.c index f2b3085e..e4972b8d 100644 --- a/example/ssh2_echo.c +++ b/example/ssh2_echo.c @@ -14,26 +14,27 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif #ifdef HAVE_SYS_TIME_H #include #endif + #include -#include #include #include #include +#include #include static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session) @@ -98,10 +99,9 @@ int main(int argc, char *argv[]) } #endif - if(argc > 1) - /* must be ip address only */ - hostname = argv[1]; - + if(argc > 1) { + hostname = argv[1]; /* must be ip address only */ + } if(argc > 2) { username = argv[2]; } @@ -126,8 +126,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -144,7 +143,7 @@ int main(int argc, char *argv[]) * and setup crypto, compression, and MAC layers */ while((rc = libssh2_session_handshake(session, sock)) == - LIBSSH2_ERROR_EAGAIN); + LIBSSH2_ERROR_EAGAIN); if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; @@ -233,7 +232,7 @@ int main(int argc, char *argv[]) for(i = 0; i < BUFSIZE; i++) buffer[i] = 'A'; - fds = malloc(sizeof (LIBSSH2_POLLFD)); + fds = malloc(sizeof(LIBSSH2_POLLFD)); if(!fds) { fprintf(stderr, "malloc failed\n"); exit(1); @@ -349,8 +348,7 @@ int main(int argc, char *argv[]) } } - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/ssh2_exec.c b/example/ssh2_exec.c index e6a23307..1c4ea4b7 100644 --- a/example/ssh2_exec.c +++ b/example/ssh2_exec.c @@ -16,26 +16,27 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_SYS_SELECT_H #include #endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif #ifdef HAVE_SYS_TIME_H #include #endif + #include -#include #include #include #include +#include #include static int waitsocket(libssh2_socket_t socket_fd, LIBSSH2_SESSION *session) @@ -72,8 +73,10 @@ int main(int argc, char *argv[]) { const char *hostname = "127.0.0.1"; const char *commandline = "uptime"; - const char *username = "user"; - const char *password = "password"; + const char *pubkey = "/home/username/.ssh/id_rsa.pub"; + const char *privkey = "/home/username/.ssh/id_rsa"; + const char *username = "user"; + const char *password = "password"; uint32_t hostaddr; libssh2_socket_t sock; struct sockaddr_in sin; @@ -99,10 +102,9 @@ int main(int argc, char *argv[]) } #endif - if(argc > 1) - /* must be ip address only */ - hostname = argv[1]; - + if(argc > 1) { + hostname = argv[1]; /* must be ip address only */ + } if(argc > 2) { username = argv[2]; } @@ -130,8 +132,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return -1; } @@ -148,7 +149,7 @@ int main(int argc, char *argv[]) * and setup crypto, compression, and MAC layers */ while((rc = libssh2_session_handshake(session, sock)) == - LIBSSH2_ERROR_EAGAIN); + LIBSSH2_ERROR_EAGAIN); if(rc) { fprintf(stderr, "Failure establishing SSH session: %d\n", rc); return -1; @@ -213,11 +214,8 @@ int main(int argc, char *argv[]) else { /* Or by public key */ while((rc = libssh2_userauth_publickey_fromfile(session, username, - "/home/user/" - ".ssh/id_rsa.pub", - "/home/user/" - ".ssh/id_rsa", - password)) == + pubkey, privkey, + password)) == LIBSSH2_ERROR_EAGAIN); if(rc) { fprintf(stderr, "\tAuthentication by public key failed\n"); @@ -240,11 +238,11 @@ int main(int argc, char *argv[]) exit(1); } while((rc = libssh2_channel_exec(channel, commandline)) == - LIBSSH2_ERROR_EAGAIN) { + LIBSSH2_ERROR_EAGAIN) { waitsocket(sock, session); } if(rc != 0) { - fprintf(stderr, "Error\n"); + fprintf(stderr, "exec error\n"); exit(1); } for(;;) { @@ -299,8 +297,7 @@ int main(int argc, char *argv[]) shutdown: - libssh2_session_disconnect(session, - "Normal Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); #ifdef WIN32 diff --git a/example/subsystem_netconf.c b/example/subsystem_netconf.c index 2101cedc..fef9f926 100644 --- a/example/subsystem_netconf.c +++ b/example/subsystem_netconf.c @@ -4,6 +4,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif #ifdef HAVE_NETINET_IN_H #include #endif @@ -14,18 +20,12 @@ #include #endif +#include #include #include #include -#include #include -#ifdef HAVE_UNISTD_H -#include -#endif -#include -#ifdef HAVE_SYS_SELECT_H -#include -#endif +#include #ifndef INADDR_NONE #define INADDR_NONE (in_addr_t)~0 @@ -35,12 +35,12 @@ #define snprintf _snprintf #endif -const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; -const char *keyfile2 = "/home/username/.ssh/id_rsa"; -const char *username = "username"; -const char *password = ""; +static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; +static const char *privkey = "/home/username/.ssh/id_rsa"; +static const char *username = "username"; +static const char *password = ""; -const char *server_ip = "127.0.0.1"; +static const char *server_ip = "127.0.0.1"; enum { AUTH_NONE = 0, @@ -147,11 +147,7 @@ int main(int argc, char *argv[]) /* Connect to SSH server */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(sock == LIBSSH2_INVALID_SOCKET) { -#ifdef WIN32 fprintf(stderr, "failed to open socket!\n"); -#else - perror("socket"); -#endif return -1; } @@ -162,8 +158,7 @@ int main(int argc, char *argv[]) return -1; } sin.sin_port = htons(830); - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr)); return -1; } @@ -219,12 +214,13 @@ int main(int argc, char *argv[]) } } else if(auth & AUTH_PUBLICKEY) { - if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { - fprintf(stderr, "Authentication by public key failed!\n"); + if(libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) { + fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; } - fprintf(stderr, "Authentication by public key succeeded.\n"); + fprintf(stderr, "\tAuthentication by public key succeeded.\n"); } else { fprintf(stderr, "No supported authentication methods found!\n"); diff --git a/example/tcpip-forward.c b/example/tcpip-forward.c index 8dea412f..d802c58e 100644 --- a/example/tcpip-forward.c +++ b/example/tcpip-forward.c @@ -10,6 +10,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif +#ifdef HAVE_SYS_SELECT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif #ifdef HAVE_NETINET_IN_H #include #endif @@ -20,34 +26,30 @@ #include #endif +#include #include #include #include #include -#ifdef HAVE_UNISTD_H -#include -#endif -#include -#ifdef HAVE_SYS_SELECT_H -#include -#endif #ifndef INADDR_NONE -#define INADDR_NONE (in_addr_t)-1 +#define INADDR_NONE (in_addr_t)~0 #endif -const char *keyfile1 = "/home/username/.ssh/id_rsa.pub"; -const char *keyfile2 = "/home/username/.ssh/id_rsa"; -const char *username = "username"; -const char *password = ""; +static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; +static const char *privkey = "/home/username/.ssh/id_rsa"; +static const char *username = "username"; +static const char *password = ""; -const char *server_ip = "127.0.0.1"; +static const char *server_ip = "127.0.0.1"; + +/* resolved by the server */ +static const char *remote_listenhost = "localhost"; -const char *remote_listenhost = "localhost"; /* resolved by the server */ int remote_wantport = 2222; int remote_listenport; -const char *local_destip = "127.0.0.1"; +static const char *local_destip = "127.0.0.1"; int local_destport = 22; enum { @@ -108,24 +110,19 @@ int main(int argc, char *argv[]) /* Connect to SSH server */ sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(sock == LIBSSH2_INVALID_SOCKET) { -#ifdef WIN32 fprintf(stderr, "failed to open socket!\n"); -#else - perror("socket"); -#endif return -1; } sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr(server_ip); if(INADDR_NONE == sin.sin_addr.s_addr) { - perror("inet_addr"); + fprintf(stderr, "inet_addr: Invalid IP address \"%s\"\n", server_ip); return -1; } sin.sin_port = htons(22); - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { - fprintf(stderr, "failed to connect!\n"); + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { + fprintf(stderr, "Failed to connect to %s!\n", inet_ntoa(sin.sin_addr)); return -1; } @@ -180,8 +177,9 @@ int main(int argc, char *argv[]) } } else if(auth & AUTH_PUBLICKEY) { - if(libssh2_userauth_publickey_fromfile(session, username, keyfile1, - keyfile2, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) { fprintf(stderr, "\tAuthentication by public key failed!\n"); goto shutdown; } @@ -221,11 +219,7 @@ int main(int argc, char *argv[]) local_destip, local_destport); forwardsock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(forwardsock == LIBSSH2_INVALID_SOCKET) { -#ifdef WIN32 fprintf(stderr, "failed to open forward socket!\n"); -#else - perror("socket"); -#endif goto shutdown; } diff --git a/example/x11.c b/example/x11.c index c8971252..e5c6815c 100644 --- a/example/x11.c +++ b/example/x11.c @@ -12,7 +12,6 @@ #ifdef HAVE_SYS_UN_H -#include #ifdef HAVE_SYS_IOCTL_H #include #endif @@ -25,20 +24,22 @@ #ifdef HAVE_SYS_SELECT_H #include #endif -#ifdef HAVE_ARPA_INET_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif -#include +#ifdef HAVE_ARPA_INET_H +#include +#endif #ifdef HAVE_SYS_UN_H #include #endif + +#include #include #include -#include #include +#include +#include #include @@ -85,8 +86,7 @@ static void remove_node(struct chan_X11_list *elem) static void session_shutdown(LIBSSH2_SESSION *session) { - libssh2_session_disconnect(session, - "Session Shutdown, Thank you for playing"); + libssh2_session_disconnect(session, "Session Shutdown"); libssh2_session_free(session); } @@ -205,13 +205,12 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) int bufsize = 8192; int rc = 0; int nfds = 1; - LIBSSH2_POLLFD *fds = NULL; + LIBSSH2_POLLFD *fds = NULL; fd_set set; struct timeval timeval_out; timeval_out.tv_sec = 0; timeval_out.tv_usec = 0; - FD_ZERO(&set); FD_SET(sock, &set); @@ -219,7 +218,7 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) if(!buf) return 0; - fds = malloc(sizeof (LIBSSH2_POLLFD)); + fds = malloc(sizeof(LIBSSH2_POLLFD)); if(!fds) { free(buf); return 0; @@ -266,7 +265,7 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) * Main, more than inspired by ssh2.c by Bagder */ int -main (int argc, char *argv[]) +main(int argc, char *argv[]) { uint32_t hostaddr = 0; int rc = 0; @@ -295,7 +294,6 @@ main (int argc, char *argv[]) timeval_out.tv_sec = 0; timeval_out.tv_usec = 10; - if(argc > 3) { hostaddr = inet_addr(argv[1]); username = argv[2]; @@ -328,9 +326,7 @@ main (int argc, char *argv[]) sin.sin_port = htons(22); sin.sin_addr.s_addr = hostaddr; - rc = connect(sock, (struct sockaddr *) &sin, - sizeof(struct sockaddr_in)); - if(rc != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "Failed to established connection!\n"); return -1; } @@ -371,7 +367,6 @@ main (int argc, char *argv[]) return -1; } - /* Request a PTY */ rc = libssh2_channel_request_pty(channel, "xterm"); if(rc != 0) { @@ -430,7 +425,7 @@ main (int argc, char *argv[]) if(buf == NULL) break; - fds = malloc(sizeof (LIBSSH2_POLLFD)); + fds = malloc(sizeof(LIBSSH2_POLLFD)); if(fds == NULL) { free(buf); break; @@ -468,7 +463,6 @@ main (int argc, char *argv[]) current_node = next_node; } - rc = select((int)(fileno(stdin) + 1), &set, NULL, NULL, &timeval_out); if(rc > 0) { ssize_t nread; @@ -482,7 +476,7 @@ main (int argc, char *argv[]) free(fds); free(buf); - if(libssh2_channel_eof (channel) == 1) { + if(libssh2_channel_eof(channel) == 1) { break; } } @@ -501,7 +495,7 @@ main (int argc, char *argv[]) #else int -main (void) +main(void) { printf("Sorry, this platform is not supported."); return 1; diff --git a/tests/openssh_fixture.c b/tests/openssh_fixture.c index 75a40ab1..afbbdaa4 100644 --- a/tests/openssh_fixture.c +++ b/tests/openssh_fixture.c @@ -389,8 +389,8 @@ static libssh2_socket_t open_socket_to_container(char *container_id) sin.sin_addr.s_addr = hostaddr; for(counter = 0; counter < 3; ++counter) { - if(connect(sock, (struct sockaddr *)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), + sizeof(struct sockaddr_in))) { fprintf(stderr, "Connection to %s:%s attempt #%d failed: retrying...\n", ip_address, port_string, counter); diff --git a/tests/simple.c b/tests/simple.c index e97f7d33..450fd568 100644 --- a/tests/simple.c +++ b/tests/simple.c @@ -36,11 +36,11 @@ * OF SUCH DAMAGE. */ +#include + #include #include -#include "libssh2.h" - static int test_libssh2_base64_decode(LIBSSH2_SESSION *session) { char *data; diff --git a/tests/ssh2.c b/tests/ssh2.c index 098230df..84b95ca7 100644 --- a/tests/ssh2.c +++ b/tests/ssh2.c @@ -1,4 +1,4 @@ -/* Self test, based on examples/ssh2.c. */ +/* Self test, based on example/ssh2.c. */ #include "libssh2_setup.h" #include @@ -7,12 +7,12 @@ #ifdef HAVE_SYS_SOCKET_H #include #endif -#ifdef HAVE_NETINET_IN_H -#include -#endif #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_NETINET_IN_H +#include +#endif #ifdef HAVE_ARPA_INET_H #include #endif @@ -34,8 +34,8 @@ int main(int argc, char *argv[]) char *userauthlist; LIBSSH2_SESSION *session; LIBSSH2_CHANNEL *channel; - const char *pubkeyfile = "etc/user.pub"; - const char *privkeyfile = "etc/user"; + const char *pubkey = "etc/user.pub"; + const char *privkey = "etc/user"; const char *username = "username"; const char *password = "password"; int ec = 1; @@ -57,11 +57,11 @@ int main(int argc, char *argv[]) if(getenv("USER")) username = getenv("USER"); - if(getenv ("PRIVKEY")) - privkeyfile = getenv("PRIVKEY"); + if(getenv("PRIVKEY")) + privkey = getenv("PRIVKEY"); if(getenv("PUBKEY")) - pubkeyfile = getenv("PUBKEY"); + pubkey = getenv("PUBKEY"); hostaddr = htonl(0x7F000001); @@ -72,8 +72,7 @@ int main(int argc, char *argv[]) sin.sin_family = AF_INET; sin.sin_port = htons(4711); sin.sin_addr.s_addr = hostaddr; - if(connect(sock, (struct sockaddr*)(&sin), - sizeof(struct sockaddr_in)) != 0) { + if(connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in))) { fprintf(stderr, "failed to connect!\n"); return 1; } @@ -117,8 +116,9 @@ int main(int argc, char *argv[]) if(auth_pw & 4) { /* Authenticate by public key */ - if(libssh2_userauth_publickey_fromfile(session, username, pubkeyfile, - privkeyfile, password)) { + if(libssh2_userauth_publickey_fromfile(session, username, + pubkey, privkey, + password)) { printf("\tAuthentication by public key failed!\n"); goto shutdown; } @@ -159,13 +159,13 @@ int main(int argc, char *argv[]) ec = 0; - skip_shell: +skip_shell: if(channel) { libssh2_channel_free(channel); channel = NULL; } - shutdown: +shutdown: libssh2_session_disconnect(session, "Normal Shutdown"); libssh2_session_free(session); diff --git a/tests/test_keyboard_interactive_auth_info_request.c b/tests/test_keyboard_interactive_auth_info_request.c index d30c0cfc..8fbdc2f2 100644 --- a/tests/test_keyboard_interactive_auth_info_request.c +++ b/tests/test_keyboard_interactive_auth_info_request.c @@ -221,7 +221,7 @@ LIBSSH2_ALLOC_FUNC(test_alloc) { int *threshold_int_ptr = *abstract; alloc_count++; - if (*abstract != NULL && *threshold_int_ptr == alloc_count) { + if(*abstract != NULL && *threshold_int_ptr == alloc_count) { return NULL; } @@ -233,7 +233,7 @@ LIBSSH2_ALLOC_FUNC(test_alloc) static LIBSSH2_FREE_FUNC(test_free) { - (void) abstract; + (void)abstract; free_count++; free(ptr); } @@ -307,10 +307,10 @@ int main(void) int tc = i + TEST_CASES_LEN + 1; int malloc_call_num = 5 + i; test_case(tc, - failed_malloc_test_cases[i].data, - failed_malloc_test_cases[i].data_len, - &malloc_call_num, - failed_malloc_test_cases[i].expected); + failed_malloc_test_cases[i].data, + failed_malloc_test_cases[i].data_len, + &malloc_call_num, + failed_malloc_test_cases[i].expected); } return 0;