mirror of
https://github.com/libssh2/libssh2.git
synced 2025-08-05 20:55:47 +03:00
tidy-up: example, tests
- drop unnecessary `WIN32`-specific branches. - add `static`. - sync header inclusion order. - sync some common code between examples/tests. - fix formatting/indentation. - fix some `checksrc` errors not caught by `checksrc`. Closes #936
This commit is contained in:
@@ -10,6 +10,12 @@
|
||||
#ifdef HAVE_SYS_SOCKET_H
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#ifdef HAVE_SYS_SELECT_H
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#ifdef HAVE_NETINET_IN_H
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
@@ -20,34 +26,28 @@
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <sys/types.h>
|
||||
#ifdef HAVE_SYS_SELECT_H
|
||||
#include <sys/select.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user