aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <mail@cynapses.org>2010-05-12 15:47:26 +0200
committerAndreas Schneider <mail@cynapses.org>2010-05-12 18:44:09 +0200
commit4b7eba1eda5c069e71807d7f0f5a65ff1462f61f (patch)
tree1402df73c12c467f41fdd52e95a95c2c4a8f8526
parent9970b1fd7daa3ab9174a1822925d5c604fd09afd (diff)
downloadlibssh-4b7eba1eda5c069e71807d7f0f5a65ff1462f61f.tar.gz
libssh-4b7eba1eda5c069e71807d7f0f5a65ff1462f61f.tar.xz
libssh-4b7eba1eda5c069e71807d7f0f5a65ff1462f61f.zip
Added runtime detection of WSAPoll().
Signed-off-by: Andreas Schneider <mail@cynapses.org>
-rw-r--r--include/libssh/poll.h1
-rw-r--r--libssh/poll.c38
-rw-r--r--libssh/socket.c2
3 files changed, 33 insertions, 8 deletions
diff --git a/include/libssh/poll.h b/include/libssh/poll.h
index a43cd660..b9e6494a 100644
--- a/include/libssh/poll.h
+++ b/include/libssh/poll.h
@@ -76,6 +76,7 @@ typedef struct ssh_pollfd_struct {
typedef unsigned long int nfds_t;
#endif /* HAVE_POLL */
+void ssh_poll_init(void);
int ssh_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout);
typedef struct ssh_poll_ctx_struct *ssh_poll_ctx;
typedef struct ssh_poll_handle_struct *ssh_poll_handle;
diff --git a/libssh/poll.c b/libssh/poll.c
index 879d3647..b28d2c4f 100644
--- a/libssh/poll.c
+++ b/libssh/poll.c
@@ -59,6 +59,10 @@ struct ssh_poll_ctx_struct {
#ifdef HAVE_POLL
#include <poll.h>
+void ssh_poll_init(void) {
+ return;
+}
+
int ssh_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
return poll((struct pollfd *) fds, nfds, timeout);
}
@@ -73,7 +77,15 @@ int ssh_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
#endif
#include <time.h>
+#include <windows.h>
#include <winsock2.h>
+
+#define WS2_LIBRARY "ws2_32.dll"
+typedef int (*poll_fn)(ssh_pollfd_t *, nfds_t, int);
+
+static poll_fn win_poll;
+static HINSTANCE hlib;
+
#else
#include <sys/select.h>
#include <sys/socket.h>
@@ -84,7 +96,8 @@ int ssh_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
static int bsd_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
fd_set readfds, writefds, exceptfds;
struct timeval tv, *ptv;
- int max_fd, rc;
+ socket_t max_fd;
+ int rc;
nfds_t i;
if (fds == NULL) {
@@ -185,14 +198,23 @@ static int bsd_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
return rc;
}
-/* TODO Detect if WSAPoll() is available and load it dynamically */
+void ssh_poll_init(void) {
+ poll_fn wsa_poll = NULL;
+
+ hlib = LoadLibrary(WS2_LIBRARY);
+ if (hlib != NULL) {
+ wsa_poll = (poll_fn) GetProcAddress(hlib, "WSAPoll");
+ }
+
+ if (wsa_poll == NULL) {
+ win_poll = bsd_poll;
+ } else {
+ win_poll = wsa_poll;
+ }
+}
+
int ssh_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) {
-#if 0
-#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) */
- return WSAPoll(fds, nfds, timeout);
-#endif
-#endif
- return bsd_poll(fds, nfds, timeout);
+ return win_poll(fds, nfds, timeout);
}
#endif /* HAVE_POLL */
diff --git a/libssh/socket.c b/libssh/socket.c
index eb4c0554..fe477abb 100644
--- a/libssh/socket.c
+++ b/libssh/socket.c
@@ -71,6 +71,8 @@ int ssh_socket_init(void) {
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
return -1;
}
+
+ ssh_poll_init();
#endif
return 0;
}