Commit c58cd771 authored by Kevin Cernekee's avatar Kevin Cernekee

Introduce new helper functions for cancel_fd checking

The same code shows up in 12 places, so we'll move it into a common
location.
Signed-off-by: default avatarKevin Cernekee <cernekee@gmail.com>
parent c7b67214
......@@ -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;
}
cmd_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 (is_cancel_pending(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;
}
cmd_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 (is_cancel_pending(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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
ret = -EINTR;
break;
......@@ -1947,14 +1932,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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL connection cancelled\n"));
gnutls_deinit(vpninfo->https_sess);
vpninfo->https_sess = NULL;
......
......@@ -1298,15 +1298,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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set))
return -EINTR;
/* Not that this should ever be able to happen... */
......@@ -1334,15 +1329,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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set))
return -EINTR;
/* Not that this should ever be able to happen... */
......
......@@ -400,6 +400,8 @@ int openconnect_print_err_cb(const char *str, size_t len, void *ptr);
const char *keystore_strerror(int err);
int keystore_fetch(const char *key, unsigned char **result);
#endif
void cmd_fd_set(struct openconnect_info *vpninfo, fd_set *fds, int *maxfd);
int is_cancel_pending(struct openconnect_info *vpninfo, fd_set *fds);
/* ${SSL_LIBRARY}.c */
int openconnect_SSL_gets(struct openconnect_info *vpninfo, char *buf, size_t len);
......
......@@ -112,14 +112,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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL write cancelled\n"));
return -EINTR;
}
......@@ -149,14 +144,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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
return -EINTR;
}
......@@ -207,14 +197,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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL read cancelled\n"));
ret = -EINTR;
break;
......@@ -1404,14 +1389,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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("SSL connection cancelled\n"));
SSL_free(https_ssl);
close(ssl_sock);
......
......@@ -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;
}
cmd_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 (is_cancel_pending(vpninfo, &rd_set)) {
vpn_progress(vpninfo, PRG_ERR, _("Socket connect cancelled\n"));
errno = EINTR;
return -1;
......@@ -524,3 +520,17 @@ int keystore_fetch(const char *key, unsigned char **result)
return ret;
}
#endif
void cmd_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;
}
}
int is_cancel_pending(struct openconnect_info *vpninfo, fd_set *fds)
{
return vpninfo->cancel_fd != -1 && FD_ISSET(vpninfo->cancel_fd, fds);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment