aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2008-03-07 01:45:05 +0000
committerAris Adamantiadis <aris@0xbadc0de.be>2008-03-07 01:45:05 +0000
commit29997022c812a5ac47c1c96cbea4d8cee339b532 (patch)
tree9211b4ceac9dc49ddacfe011882af2db32ee2d9f
parent5029fe9d74d0fbc93cc2756d8b48d1bb462e3f39 (diff)
downloadlibssh-29997022c812a5ac47c1c96cbea4d8cee339b532.tar.gz
libssh-29997022c812a5ac47c1c96cbea4d8cee339b532.tar.xz
libssh-29997022c812a5ac47c1c96cbea4d8cee339b532.zip
first part of win32 patches
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@141 7dcaeef0-15fb-0310-b436-a5af3365683c
-rw-r--r--include/libssh/libssh.h14
-rw-r--r--libssh/auth.c1
-rw-r--r--libssh/auth1.c1
-rw-r--r--libssh/channels.c2
-rw-r--r--libssh/channels1.c3
-rw-r--r--libssh/connect.c34
-rw-r--r--libssh/crypt.c2
-rw-r--r--libssh/dh.c4
-rw-r--r--libssh/keys.c1
-rw-r--r--libssh/messages.c7
-rw-r--r--libssh/packet.c2
-rw-r--r--libssh/server.c20
-rw-r--r--libssh/session.c3
-rw-r--r--libssh/sftpserver.c12
-rw-r--r--libssh/socket.c32
-rw-r--r--libssh/string.c2
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 <unistd.h>
+#ifndef _WIN32
#include <sys/select.h> /* for fd_set * */
+#endif
+#ifdef _WIN32
+#include <winsock2.h>
+#endif
#include <inttypes.h>
#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 <stdio.h>
#include <string.h>
#include <netdb.h>
-
/** 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 <string.h>
-#include <netdb.h>
#include <stdlib.h>
/*
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 <string.h>
#include <stdlib.h>
-#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
+#include <netdb.h>
#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 <string.h>
#include <stdlib.h>
-#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
-
+#include <netdb.h>
#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 <netdb.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
-#include <sys/types.h>
+#ifdef _WIN32
+#define _WIN32_WINNT 0x0501 //getaddrinfo, freeaddrinfo, getnameinfo
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#else
+#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
-#include <sys/time.h>
#include <netinet/in.h>
+#endif
#include <fcntl.h>
#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 <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <netdb.h>
#ifdef OPENSSL_CRYPTO
#include <openssl/blowfish.h>
@@ -31,7 +32,6 @@ MA 02111-1307, USA. */
#include <openssl/hmac.h>
#endif
-#include <netdb.h>
#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 <stdio.h>
#include <stdlib.h>
-#include <netdb.h>
#include <string.h>
-
+#include <netdb.h>
+
#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 <stdlib.h>
#include <string.h>
-#include <netinet/in.h>
#ifdef HAVE_LIBCRYPTO
#include <openssl/dsa.h>
#include <openssl/rsa.h>
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 <string.h>
+#include <stdlib.h>
+#include <netdb.h>
#include "libssh/libssh.h"
#include "libssh/priv.h"
#include "libssh/server.h"
#include "libssh/ssh2.h"
-#include <netdb.h>
-#include <string.h>
-#include <stdlib.h>
+
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 <stdio.h>
#include <unistd.h>
#include <string.h>
+#include <netdb.h>
#include "libssh/priv.h"
#include "libssh/ssh2.h"
#include "libssh/ssh1.h"
-#include <netdb.h>
#include <errno.h>
#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 <fcntl.h>
#include <unistd.h>
#include <stdio.h>
-#include <sys/types.h>
+#ifdef _WIN32
+#include <winsock2.h>
+#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 <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
+#define SOCKOPT_TYPE_ARG4 int
+#endif
#include <errno.h>
#include <string.h>
#include <stdlib.h>
@@ -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 "libssh/libssh.h"
-#include "libssh/sftp.h"
-#include "libssh/ssh2.h"
-#include "libssh/priv.h"
+
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
-#include <netdb.h>
#include <stdio.h>
+#include <netdb.h>
+#include "libssh/libssh.h"
+#include "libssh/sftp.h"
+#include "libssh/ssh2.h"
+#include "libssh/priv.h"
+
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 <unistd.h>
#include <errno.h>
-#ifdef _WIN_32
+#ifdef _WIN32
#include <winsock2.h>
-#endif
+#else
#include <sys/types.h>
#include <sys/socket.h>
+#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 <stdlib.h>
-#include <netdb.h>
#include <unistd.h>
#include <string.h>
+#include <netdb.h>
#include "libssh/priv.h"
STRING *string_new(unsigned int size){