[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/3] Add ssh_get_poll_flags()


For integration with an external mainloop, we need to know how to
replicate libssh's internal poll() calls.  We originally through
ssh_get_status() was that API, but it's not really - those flags only
get updated from the *result* of a poll(), where what we really need
is to know how libssh would *start* a poll().
---
 include/libssh/libssh.h |  1 +
 include/libssh/socket.h |  1 +
 src/session.c           | 19 +++++++++++++++++++
 src/socket.c            | 11 +++++++++++
 4 files changed, 32 insertions(+)


From a83cbf638bb595178e23c0983fe66c86ed1e857a Mon Sep 17 00:00:00 2001
From: Colin Walters <walters@xxxxxxxxxx>
Date: Wed, 6 Nov 2013 14:11:52 -0500
Subject: [PATCH 2/3] Add ssh_get_poll_flags()

For integration with an external mainloop, we need to know how to
replicate libssh's internal poll() calls.  We originally through
ssh_get_status() was that API, but it's not really - those flags only
get updated from the *result* of a poll(), where what we really need
is to know how libssh would *start* a poll().
---
 include/libssh/libssh.h |  1 +
 include/libssh/socket.h |  1 +
 src/session.c           | 19 +++++++++++++++++++
 src/socket.c            | 11 +++++++++++
 4 files changed, 32 insertions(+)

diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
index 3833adc..f1b9fd8 100644
--- a/include/libssh/libssh.h
+++ b/include/libssh/libssh.h
@@ -429,6 +429,7 @@ SSH_DEPRECATED LIBSSH_API int ssh_get_pubkey_hash(ssh_session session, unsigned
 LIBSSH_API int ssh_get_random(void *where,int len,int strong);
 LIBSSH_API int ssh_get_version(ssh_session session);
 LIBSSH_API int ssh_get_status(ssh_session session);
+LIBSSH_API int ssh_get_poll_flags(ssh_session session);
 LIBSSH_API int ssh_init(void);
 LIBSSH_API int ssh_is_blocking(ssh_session session);
 LIBSSH_API int ssh_is_connected(ssh_session session);
diff --git a/include/libssh/socket.h b/include/libssh/socket.h
index 285a02e..8e1eac2 100644
--- a/include/libssh/socket.h
+++ b/include/libssh/socket.h
@@ -52,6 +52,7 @@ void ssh_socket_set_write_wontblock(ssh_socket s);
 void ssh_socket_set_read_wontblock(ssh_socket s);
 void ssh_socket_set_except(ssh_socket s);
 int ssh_socket_get_status(ssh_socket s);
+int ssh_socket_get_poll_flags(ssh_socket s);
 int ssh_socket_buffered_write_bytes(ssh_socket s);
 int ssh_socket_data_available(ssh_socket s);
 int ssh_socket_data_writable(ssh_socket s);
diff --git a/src/session.c b/src/session.c
index d4b3643..06e7567 100644
--- a/src/session.c
+++ b/src/session.c
@@ -608,6 +608,25 @@ int ssh_get_status(ssh_session session) {
 }
 
 /**
+ * @brief Get poll flags for an external mainloop
+ *
+ * @param session       The ssh session to use.
+ *
+ * @returns A bitmask including SSH_READ_PENDING or SSH_WRITE_PENDING.
+ *          For SSH_READ_PENDING, your invocation of poll() should include
+ *          POLLIN.  For SSH_WRITE_PENDING, your invocation of poll() should
+ *          include POLLOUT.
+ */
+int ssh_get_poll_flags(ssh_session session)
+{
+  if (session == NULL) {
+    return 0;
+  }
+
+  return ssh_socket_get_poll_flags (session->socket);
+}
+
+/**
  * @brief Get the disconnect message from the server.
  *
  * @param[in] session   The ssh session to use.
diff --git a/src/socket.c b/src/socket.c
index c76ef5a..d7cf539 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -710,6 +710,17 @@ int ssh_socket_get_status(ssh_socket s) {
   return r;
 }
 
+int ssh_socket_get_poll_flags(ssh_socket s) {
+  int r = 0;
+  if (s->poll_in != NULL && (ssh_poll_get_events (s->poll_in) & POLLIN) > 0) {
+    r |= SSH_READ_PENDING;
+  }
+  if (s->poll_out != NULL && (ssh_poll_get_events (s->poll_out) & POLLOUT) > 0) {
+    r |= SSH_WRITE_PENDING;
+  }
+  return r;
+}
+
 #ifdef _WIN32
 int ssh_socket_set_nonblocking(socket_t fd) {
   u_long nonblocking = 1;
-- 
1.8.3.1


Follow-Ups:
Re: [PATCH 2/3] Add ssh_get_poll_flags()Andreas Schneider <asn@xxxxxxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org