From 29997022c812a5ac47c1c96cbea4d8cee339b532 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Fri, 7 Mar 2008 01:45:05 +0000 Subject: [PATCH] first part of win32 patches git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@141 7dcaeef0-15fb-0310-b436-a5af3365683c --- include/libssh/libssh.h | 14 +++++++++++++- libssh/auth.c | 1 - libssh/auth1.c | 1 - libssh/channels.c | 2 +- libssh/channels1.c | 3 +-- libssh/connect.c | 34 +++++++++++++++++++++++++--------- libssh/crypt.c | 2 +- libssh/dh.c | 4 ++-- libssh/keys.c | 1 - libssh/messages.c | 7 ++++--- libssh/packet.c | 2 +- libssh/server.c | 20 +++++++++++++++++--- libssh/session.c | 3 ++- libssh/sftpserver.c | 12 +++++++----- libssh/socket.c | 32 +++++++++++++++++++++++--------- libssh/string.c | 2 +- 16 files changed, 98 insertions(+), 42 deletions(-) diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index ff3a60f0..86dc09b1 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -21,7 +21,12 @@ MA 02111-1307, USA. */ #ifndef _LIBSSH_H #define _LIBSSH_H #include +#ifndef _WIN32 #include /* for fd_set * */ +#endif +#ifdef _WIN32 +#include +#endif #include #define LIBSSH_VERSION "libssh-0.2" @@ -45,6 +50,13 @@ typedef uint16_t u16; typedef uint64_t u64; typedef uint8_t u8; +/* Socket type */ +#ifdef _WIN32 +#define socket_t SOCKET +#else +typedef int socket_t; +#endif + /* the offsets of methods */ #define SSH_KEX 0 #define SSH_HOSTKEYS 1 @@ -110,7 +122,7 @@ void ssh_set_verbosity(int num); /* session.c */ SSH_SESSION *ssh_new(); void ssh_set_options(SSH_SESSION *session, SSH_OPTIONS *options); -int ssh_get_fd(SSH_SESSION *session); +socket_t ssh_get_fd(SSH_SESSION *session); void ssh_silent_disconnect(SSH_SESSION *session); int ssh_get_version(SSH_SESSION *session); void ssh_set_fd_toread(SSH_SESSION *session); diff --git a/libssh/auth.c b/libssh/auth.c index f4ea45e7..5ca7934d 100644 --- a/libssh/auth.c +++ b/libssh/auth.c @@ -25,7 +25,6 @@ MA 02111-1307, USA. */ #include #include #include - /** defgroup ssh_auth * \brief functions to authenticate */ diff --git a/libssh/auth1.c b/libssh/auth1.c index 415c42aa..865f2257 100644 --- a/libssh/auth1.c +++ b/libssh/auth1.c @@ -22,7 +22,6 @@ MA 02111-1307, USA. */ #include "libssh/priv.h" #include "libssh/ssh1.h" #include -#include #include /* diff --git a/libssh/channels.c b/libssh/channels.c index 65a2af3a..a961c7a7 100644 --- a/libssh/channels.c +++ b/libssh/channels.c @@ -22,10 +22,10 @@ MA 02111-1307, USA. */ #include #include -#include #include #include #include +#include #include "libssh/priv.h" #include "libssh/ssh2.h" diff --git a/libssh/channels1.c b/libssh/channels1.c index 375f8e68..065bfbd0 100644 --- a/libssh/channels1.c +++ b/libssh/channels1.c @@ -22,10 +22,9 @@ MA 02111-1307, USA. */ #include #include -#include #include #include - +#include #include "libssh/priv.h" #include "libssh/ssh1.h" diff --git a/libssh/connect.c b/libssh/connect.c index ad8a4f30..6470b5a9 100644 --- a/libssh/connect.c +++ b/libssh/connect.c @@ -20,17 +20,21 @@ along with the SSH Library; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include #include #include #include #include #include -#include +#ifdef _WIN32 +#define _WIN32_WINNT 0x0501 //getaddrinfo, freeaddrinfo, getnameinfo +#include +#include +#else +#include #include #include -#include #include +#endif #include #include "libssh/priv.h" @@ -42,12 +46,23 @@ MA 02111-1307, USA. */ #error "Your system must have getaddrinfo()" #endif -static void sock_set_nonblocking(int sock) { +#ifndef _WIN32 +static void sock_set_nonblocking(socket_t sock) { fcntl(sock,F_SETFL,O_NONBLOCK); } -static void sock_set_blocking(int sock){ +static void sock_set_blocking(socket_t sock){ fcntl(sock,F_SETFL,0); } +#else +static void sock_set_nonblocking(socket_t sock) { + u_long nonblocking = 1; + ioctlsocket(sock, FIONBIO, &nonblocking); +} +static void sock_set_blocking(socket_t sock){ + u_long nonblocking = 0; + ioctlsocket(sock, FIONBIO, &nonblocking); +} +#endif static int getai(const char *host, int port, struct addrinfo **ai) { @@ -69,7 +84,7 @@ static int getai(const char *host, int port, struct addrinfo **ai) } int ssh_connect_ai_timeout(SSH_SESSION *session, const char *host, int port, struct addrinfo *ai, - long timeout, long usec,int s) + long timeout, long usec,socket_t s) { struct timeval to; fd_set set; @@ -94,6 +109,7 @@ int ssh_connect_ai_timeout(SSH_SESSION *session, const char *host, int port, str close(s); return -1; } + ret = 0; /* get connect(2) return code. zero means no error */ getsockopt(s,SOL_SOCKET,SO_ERROR,&ret,&len); if (ret!=0){ @@ -111,9 +127,9 @@ int ssh_connect_ai_timeout(SSH_SESSION *session, const char *host, int port, str /* specified by its IP address or hostname. */ /* output is the file descriptor, <0 if failed. */ -int ssh_connect_host(SSH_SESSION *session, const char *host, const char +socket_t ssh_connect_host(SSH_SESSION *session, const char *host, const char *bind_addr, int port,long timeout, long usec){ - int s=-1; + socket_t s=-1; int my_errno; struct addrinfo *ai, *ai2; @@ -239,7 +255,7 @@ int ssh_fd_poll(SSH_SESSION *session, int *write, int *except){ * \warning libssh is not threadsafe. That means that if a signal is caught during the processing * of this function, you cannot call ssh functions on sessions that are busy with ssh_select() */ -int ssh_select(CHANNEL **channels,CHANNEL **outchannels, int maxfd, fd_set *readfds, struct timeval *timeout){ +int ssh_select(CHANNEL **channels,CHANNEL **outchannels, socket_t maxfd, fd_set *readfds, struct timeval *timeout){ struct timeval zerotime; fd_set localset,localset2; int rep; diff --git a/libssh/crypt.c b/libssh/crypt.c index bb3bb4e9..589fc978 100644 --- a/libssh/crypt.c +++ b/libssh/crypt.c @@ -24,6 +24,7 @@ MA 02111-1307, USA. */ #include #include #include +#include #ifdef OPENSSL_CRYPTO #include @@ -31,7 +32,6 @@ MA 02111-1307, USA. */ #include #endif -#include #include "libssh/priv.h" #include "libssh/crypto.h" diff --git a/libssh/dh.c b/libssh/dh.c index 73e9e3aa..9cb6907b 100644 --- a/libssh/dh.c +++ b/libssh/dh.c @@ -37,9 +37,9 @@ MA 02111-1307, USA. */ #include #include -#include #include - +#include + #include "libssh/priv.h" #include "libssh/crypto.h" diff --git a/libssh/keys.c b/libssh/keys.c index 1287701d..d3b81a84 100644 --- a/libssh/keys.c +++ b/libssh/keys.c @@ -22,7 +22,6 @@ the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include -#include #ifdef HAVE_LIBCRYPTO #include #include diff --git a/libssh/messages.c b/libssh/messages.c index d8c16ed1..730aa54b 100644 --- a/libssh/messages.c +++ b/libssh/messages.c @@ -26,13 +26,14 @@ MA 02111-1307, USA. */ * functions to handle it (or use the default handlers if she doesn't know what to * do */ +#include +#include +#include #include "libssh/libssh.h" #include "libssh/priv.h" #include "libssh/server.h" #include "libssh/ssh2.h" -#include -#include -#include + static SSH_MESSAGE *message_new(SSH_SESSION *session){ SSH_MESSAGE *msg=session->ssh_message; diff --git a/libssh/packet.c b/libssh/packet.c index a4d07267..fcb37c54 100644 --- a/libssh/packet.c +++ b/libssh/packet.c @@ -24,10 +24,10 @@ MA 02111-1307, USA. */ #include #include #include +#include #include "libssh/priv.h" #include "libssh/ssh2.h" #include "libssh/ssh1.h" -#include #include #include "libssh/crypto.h" diff --git a/libssh/server.c b/libssh/server.c index 06d5879f..4f28c6a1 100644 --- a/libssh/server.c +++ b/libssh/server.c @@ -25,10 +25,22 @@ MA 02111-1307, USA. */ #include #include #include -#include +#ifdef _WIN32 +#include +#define SOCKOPT_TYPE_ARG4 char + +/* We need to provide hstrerror. Not we can't call the parameter h_errno because it's #defined */ +inline char* hstrerror(int h_errno_val) { + static char text[50]; + snprintf(text,sizeof(text),"gethostbyname error %d\n", h_errno_val); + return text; +} +#else #include #include #include +#define SOCKOPT_TYPE_ARG4 int +#endif #include #include #include @@ -36,10 +48,12 @@ MA 02111-1307, USA. */ #include "libssh/libssh.h" #include "libssh/server.h" #include "libssh/ssh2.h" -static int bind_socket(SSH_BIND *ssh_bind,char *hostname, int port) { + +// TODO: must use getaddrinfo +static socket_t bind_socket(SSH_BIND *ssh_bind,char *hostname, int port) { struct sockaddr_in myaddr; int opt = 1; - int s = socket(PF_INET, SOCK_STREAM, 0); + socket_t s = socket(PF_INET, SOCK_STREAM, 0); struct hostent *hp=NULL; #ifdef HAVE_GETHOSTBYNAME hp=gethostbyname(hostname); diff --git a/libssh/session.c b/libssh/session.c index 06b31ab3..973d4f8d 100644 --- a/libssh/session.c +++ b/libssh/session.c @@ -133,7 +133,8 @@ void ssh_set_blocking(SSH_SESSION *session,int blocking){ * \return file descriptor of the connection, or -1 if it is * not connected */ -int ssh_get_fd(SSH_SESSION *session){ + +socket_t ssh_get_fd(SSH_SESSION *session){ return ssh_socket_get_fd(session->socket); } diff --git a/libssh/sftpserver.c b/libssh/sftpserver.c index 7779512a..23286c82 100644 --- a/libssh/sftpserver.c +++ b/libssh/sftpserver.c @@ -18,15 +18,17 @@ You should have received a copy of the GNU Lesser General Public License along with the SSH Library; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#include +#include +#include +#include +#include #include "libssh/libssh.h" #include "libssh/sftp.h" #include "libssh/ssh2.h" #include "libssh/priv.h" -#include -#include -#include -#include -#include + SFTP_CLIENT_MESSAGE *sftp_get_client_message(SFTP_SESSION *sftp){ SFTP_PACKET *packet=sftp_packet_read(sftp); diff --git a/libssh/socket.c b/libssh/socket.c index c37fcaa9..52a318d6 100644 --- a/libssh/socket.c +++ b/libssh/socket.c @@ -19,20 +19,23 @@ * along with the SSH Library; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -#include "libssh/priv.h" + #include #include -#ifdef _WIN_32 +#ifdef _WIN32 #include -#endif +#else #include #include +#endif +#include "libssh/priv.h" struct socket { - int fd; + socket_t fd; int last_errno; }; + /* * \internal * \brief creates a new Socket object @@ -48,6 +51,7 @@ struct socket *ssh_socket_new(){ * \brief Deletes a socket object */ void ssh_socket_free(struct socket *s){ + ssh_socket_close(s); free(s); } @@ -58,10 +62,11 @@ void ssh_socket_close(struct socket *s){ if(ssh_socket_is_open(s)){ #ifdef _WIN_32 closesocket(s->fd); + s->last_errno=WSAGetLastError(); #else close(s->fd); -#endif s->last_errno=errno; +#endif s->fd=-1; } } @@ -69,14 +74,14 @@ void ssh_socket_close(struct socket *s){ /* \internal * \brief sets the file descriptor of the socket */ -void ssh_socket_set_fd(struct socket *s, int fd){ +void ssh_socket_set_fd(struct socket *s, socket_t fd){ s->fd=fd; } /* \internal * \brief returns the file descriptor of the socket */ -int ssh_socket_get_fd(struct socket *s){ +socket_t ssh_socket_get_fd(struct socket *s){ return s->fd; } @@ -92,7 +97,11 @@ int ssh_socket_is_open(struct socket *s){ */ int ssh_socket_read(struct socket *s, void *buffer, int len){ int r=recv(s->fd,buffer,len,0); - s->last_errno=errno; +#ifndef _WIN32 + s->last_errno=errno; +#else + s->last_errno=WSAGetLastError(); +#endif return r; } @@ -101,7 +110,12 @@ int ssh_socket_read(struct socket *s, void *buffer, int len){ */ int ssh_socket_write(struct socket *s,const void *buffer, int len){ int w=send(s->fd,buffer,len,0); - s->last_errno=errno; +#ifndef _WIN32 + s->last_errno=errno; +#else + s->last_errno=WSAGetLastError(); +#endif + return w; } diff --git a/libssh/string.c b/libssh/string.c index 2e6c3bfd..5f27326c 100644 --- a/libssh/string.c +++ b/libssh/string.c @@ -21,9 +21,9 @@ the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include -#include #include #include +#include #include "libssh/priv.h" STRING *string_new(unsigned int size){