[PATCH/RFC V2 03/26] Introduce new helper functions for cancel_fd checking
Kevin Cernekee
cernekee at gmail.com
Sun Aug 11 21:49:04 EDT 2013
The same code shows up in 12 places, so we'll move it into an inline
function.
Signed-off-by: Kevin Cernekee <cernekee at gmail.com>
---
gnutls.c | 36 ++++++++----------------------------
http.c | 18 ++++--------------
openconnect-internal.h | 29 +++++++++++++++++++++++++++++
openssl.c | 36 ++++++++----------------------------
ssl.c | 8 ++------
5 files changed, 51 insertions(+), 76 deletions(-)
diff --git a/gnutls.c b/gnutls.c
index 2b3b45f..b55c02d 100644
--- a/gnutls.c
+++ b/gnutls.c
@@ -94,14 +94,9 @@ int openconnect_SSL_write(struct openconnect_info *vpninfo, char *buf, size_t le
else
FD_SET(vpninfo->ssl_fd, &rd_set);
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > vpninfo->ssl_fd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL write cancelled\n"));
return -EINTR;
}
@@ -131,14 +126,9 @@ int openconnect_SSL_read(struct openconnect_info *vpninfo, char *buf, size_t len
else
FD_SET(vpninfo->ssl_fd, &rd_set);
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > vpninfo->ssl_fd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
return -EINTR;
}
@@ -189,14 +179,9 @@ int openconnect_SSL_gets(struct openconnect_info *vpninfo, char *buf, size_t len
else
FD_SET(vpninfo->ssl_fd, &rd_set);
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > vpninfo->ssl_fd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
ret = -EINTR;
break;
@@ -1899,14 +1884,9 @@ int openconnect_open_https(struct openconnect_info *vpninfo)
else
FD_SET(ssl_sock, &rd_set);
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > ssl_sock)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL connection cancelled\n"));
gnutls_deinit(vpninfo->https_sess);
vpninfo->https_sess = NULL;
diff --git a/http.c b/http.c
index 5adacaa..aa84773 100644
--- a/http.c
+++ b/http.c
@@ -1295,15 +1295,10 @@ static int proxy_write(struct openconnect_info *vpninfo, int fd,
FD_ZERO(&wr_set);
FD_ZERO(&rd_set);
FD_SET(fd, &wr_set);
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > fd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set))
+ if (cancel_fd_isset(vpninfo, &rd_set))
return -EINTR;
/* Not that this should ever be able to happen... */
@@ -1331,15 +1326,10 @@ static int proxy_read(struct openconnect_info *vpninfo, int fd,
FD_ZERO(&rd_set);
FD_SET(fd, &rd_set);
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > fd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, NULL, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set))
+ if (cancel_fd_isset(vpninfo, &rd_set))
return -EINTR;
/* Not that this should ever be able to happen... */
diff --git a/openconnect-internal.h b/openconnect-internal.h
index ff99cf9..ea59d23 100644
--- a/openconnect-internal.h
+++ b/openconnect-internal.h
@@ -363,6 +363,35 @@ char *openconnect__strcasestr(const char *haystack, const char *needle);
free(__realloc_old); \
} while (0)
+static inline void cancel_fd_set(struct openconnect_info *vpninfo, fd_set *fds, int *maxfd)
+{
+ if (vpninfo->cancel_fd != -1) {
+ FD_SET(vpninfo->cancel_fd, fds);
+ if (vpninfo->cancel_fd > *maxfd)
+ *maxfd = vpninfo->cancel_fd;
+ }
+}
+
+static inline int cancel_fd_isset(struct openconnect_info *vpninfo, fd_set *fds)
+{
+ return vpninfo->cancel_fd != -1 && FD_ISSET(vpninfo->cancel_fd, fds);
+}
+
+static inline int cancel_fd_check(struct openconnect_info *vpninfo)
+{
+ fd_set rd_set;
+ int maxfd = 0;
+ struct timeval tv = { 0, 0 };
+
+ if (vpninfo->cancel_fd == -1)
+ return 0;
+
+ FD_ZERO(&rd_set);
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
+ select(maxfd + 1, &rd_set, NULL, NULL, &tv);
+ return cancel_fd_isset(vpninfo, &rd_set);
+}
+
/****************************************************************************/
/* tun.c */
diff --git a/openssl.c b/openssl.c
index 5e2a94e..ba12bea 100644
--- a/openssl.c
+++ b/openssl.c
@@ -111,14 +111,9 @@ int openconnect_SSL_write(struct openconnect_info *vpninfo, char *buf, size_t le
openconnect_report_ssl_errors(vpninfo);
return -EIO;
}
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > vpninfo->ssl_fd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL write cancelled\n"));
return -EINTR;
}
@@ -148,14 +143,9 @@ int openconnect_SSL_read(struct openconnect_info *vpninfo, char *buf, size_t len
openconnect_report_ssl_errors(vpninfo);
return -EIO;
}
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > vpninfo->ssl_fd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
return -EINTR;
}
@@ -206,14 +196,9 @@ int openconnect_SSL_gets(struct openconnect_info *vpninfo, char *buf, size_t len
ret = -EIO;
break;
}
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > vpninfo->ssl_fd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
ret = -EINTR;
break;
@@ -1403,14 +1388,9 @@ int openconnect_open_https(struct openconnect_info *vpninfo)
return -EINVAL;
}
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > ssl_sock)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 &&
- FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL connection cancelled\n"));
SSL_free(https_ssl);
close(ssl_sock);
diff --git a/ssl.c b/ssl.c
index 7a08609..53d9913 100644
--- a/ssl.c
+++ b/ssl.c
@@ -73,16 +73,12 @@ static int cancellable_connect(struct openconnect_info *vpninfo, int sockfd,
FD_ZERO(&wr_set);
FD_ZERO(&rd_set);
FD_SET(sockfd, &wr_set);
- if (vpninfo->cancel_fd != -1) {
- FD_SET(vpninfo->cancel_fd, &rd_set);
- if (vpninfo->cancel_fd > sockfd)
- maxfd = vpninfo->cancel_fd;
- }
+ cancel_fd_set(vpninfo, &rd_set, &maxfd);
/* Later we'll render this whole exercise non-pointless by
including a 'cancelfd' here too. */
select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
- if (vpninfo->cancel_fd != -1 && FD_ISSET(vpninfo->cancel_fd, &rd_set)) {
+ if (cancel_fd_isset(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("Socket connect cancelled\n"));
errno = EINTR;
return -1;
--
1.7.9.5
More information about the openconnect-devel
mailing list