From d245c66cc0029e480674394c23e8be1c9410f7ad Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Sat, 1 Apr 2023 01:36:54 +0000 Subject: [PATCH] example: make `x11` exclusion build-tool-agnostic Whether to build the `x11` example or not was decided by each build tool. CMake didn't build it even on supported platforms. GNUMakefile used a specific blocklist for it, while autotools enabled it based on feature-detection. Migrate the enabler logic to an #ifdef in source and build `x11` unconditionally with all build tools. On unsupported platforms (=Windows) this program now displays a short message stating that fact. Also: - fix `x11.c` warnings uncovered after CMake started building it. - use `libssh2_socket_t` type for portability in `x11.c` too. - use detected header guards in `x11.c`. - delete a duplicate reference to `-lws2_32` from `win32/GNUmakefile` while there. Closes #909 --- example/CMakeLists.txt | 3 +- example/Makefile.am | 7 ++--- example/x11.c | 64 +++++++++++++++++++++++++++++++++--------- win32/GNUmakefile | 4 +-- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 1badba3e..66e5deba 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -62,7 +62,8 @@ set(EXAMPLES ssh2_echo ssh2_exec subsystem_netconf - tcpip-forward) + tcpip-forward + x11) foreach(example ${EXAMPLES}) add_executable(example-${example} ${example}.c) diff --git a/example/Makefile.am b/example/Makefile.am index 1fa4c257..495f93eb 100644 --- a/example/Makefile.am +++ b/example/Makefile.am @@ -26,11 +26,8 @@ noinst_PROGRAMS = \ ssh2_echo \ ssh2_exec \ subsystem_netconf \ - tcpip-forward - -if HAVE_SYS_UN_H -noinst_PROGRAMS += x11 -endif + tcpip-forward \ + x11 AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/example -I../src LDADD = $(top_builddir)/src/libssh2.la diff --git a/example/x11.c b/example/x11.c index 80d4f465..ecce8871 100644 --- a/example/x11.c +++ b/example/x11.c @@ -5,23 +5,44 @@ * "ssh2 host user password [DEBUG]" */ +#include +#include "libssh2_config.h" + +#include + +#ifdef HAVE_SYS_UN_H + #include +#ifdef HAVE_SYS_IOCTL_H #include +#endif +#ifdef HAVE_NETINET_IN_H #include +#endif +#ifdef HAVE_SYS_SOCKET_H #include +#endif +#ifdef HAVE_SYS_SELECT_H #include +#endif +#ifdef HAVE_ARPA_INET_H #include +#endif +#ifdef HAVE_UNISTD_H #include +#endif #include +#ifdef HAVE_SYS_UN_H #include +#endif #include #include -#include #include +#ifdef HAVE_STDLIB_H #include -#include +#endif -#include +#include #define _PATH_UNIX_X "/tmp/.X11-unix/X%d" @@ -67,7 +88,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"); + "Session Shutdown, Thank you for playing"); libssh2_session_free(session); } @@ -109,8 +130,8 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, char *ptr = NULL; char *temp_buff = NULL; int display_port = 0; - int sock = 0; int rc = 0; + libssh2_socket_t sock = LIBSSH2_INVALID_SOCKET; struct sockaddr_un addr; struct chan_X11_list *new; struct chan_X11_list *chan_iter; @@ -138,7 +159,7 @@ static void x11_callback(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, free(temp_buff); sock = socket(AF_UNIX, SOCK_STREAM, 0); - if(sock < 0) + if(sock == LIBSSH2_INVALID_SOCKET) return; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; @@ -212,7 +233,7 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) fds[0].revents = LIBSSH2_POLLFD_POLLIN; rc = libssh2_poll(fds, nfds, 0); - if(rc >0) { + if(rc > 0) { ssize_t nread; nread = libssh2_channel_read(channel, buf, bufsize); write(sock, buf, nread); @@ -220,12 +241,14 @@ static int x11_send_receive(LIBSSH2_CHANNEL *channel, int sock) rc = select((int)(sock + 1), &set, NULL, NULL, &timeval_out); if(rc > 0) { + ssize_t nread; + memset((void *)buf, 0, bufsize); /* Data in sock */ - rc = read(sock, buf, bufsize); - if(rc > 0) { - libssh2_channel_write(channel, buf, rc); + nread = read(sock, buf, bufsize); + if(nread > 0) { + libssh2_channel_write(channel, buf, nread); } else { free(buf); @@ -248,8 +271,8 @@ int main (int argc, char *argv[]) { uint32_t hostaddr = 0; - int sock = 0; int rc = 0; + libssh2_socket_t sock = LIBSSH2_INVALID_SOCKET; struct sockaddr_in sin; LIBSSH2_SESSION *session; LIBSSH2_CHANNEL *channel; @@ -298,7 +321,7 @@ main (int argc, char *argv[]) } sock = socket(AF_INET, SOCK_STREAM, 0); - if(sock == -1) { + if(sock == LIBSSH2_INVALID_SOCKET) { perror("socket"); return -1; } @@ -450,9 +473,11 @@ main (int argc, char *argv[]) rc = select((int)(fileno(stdin) + 1), &set, NULL, NULL, &timeval_out); if(rc > 0) { + ssize_t nread; + /* Data in stdin */ - rc = read(fileno(stdin), buf, 1); - if(rc > 0) + nread = read(fileno(stdin), buf, 1); + if(nread > 0) libssh2_channel_write(channel, buf, sizeof(buf)); } @@ -474,3 +499,14 @@ main (int argc, char *argv[]) return 0; } + +#else + +int +main (void) +{ + printf("Sorry, this platform is not supported."); + return 1; +} + +#endif /* HAVE_SYS_UN_H */ diff --git a/win32/GNUmakefile b/win32/GNUmakefile index 2c91eaef..49966276 100644 --- a/win32/GNUmakefile +++ b/win32/GNUmakefile @@ -52,7 +52,7 @@ RCFLAGS += -I$(PROOT)/include # examples, tests LIBSSH2_LDFLAGS_BIN += -L$(PROOT)/win32 -LIBS_BIN := -lssh2 -lws2_32 +LIBS_BIN := -lssh2 ifdef DYN libssh2_DEPENDENCIES := $(PROOT)/win32/libssh2.dll.a @@ -164,7 +164,7 @@ libssh2_dll_LIBRARY := $(TARGET)$(LIBSSH2_DLL_SUFFIX).dll libssh2_dll_a_LIBRARY := $(TARGET).dll.a EXAMPLES := $(PROOT)/example -TARGETS_EXAMPLES := $(filter-out $(EXAMPLES)/x11.exe,$(patsubst %.c,%.exe,$(strip $(wildcard $(EXAMPLES)/*.c)))) +TARGETS_EXAMPLES := $(patsubst %.c,%.exe,$(strip $(wildcard $(EXAMPLES)/*.c))) all: lib dll