Skip to content

Commit

Permalink
Add cancellation handling to proxy I/O functions
Browse files Browse the repository at this point in the history
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed May 12, 2012
1 parent 4b74824 commit 033a334
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
49 changes: 46 additions & 3 deletions http.c
Expand Up @@ -899,9 +899,31 @@ static int proxy_write(struct openconnect_info *vpninfo, int fd,
unsigned char *buf, size_t len)
{
size_t count;

for (count = 0; count < len; ) {
int i = write(fd, buf + count, len - count);
fd_set rd_set, wr_set;
int maxfd = fd;
int i;

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;
}

select(maxfd + 1, &rd_set, &wr_set, NULL, NULL);
if (vpninfo->cancel_fd != -1 &&
FD_ISSET(vpninfo->cancel_fd, &rd_set))
return -EINTR;

/* Not that this should ever be able to happen... */
if (!FD_ISSET(fd, &wr_set))
continue;

i = write(fd, buf + count, len - count);
if (i < 0)
return -errno;

Expand All @@ -916,7 +938,28 @@ static int proxy_read(struct openconnect_info *vpninfo, int fd,
size_t count;

for (count = 0; count < len; ) {
int i = read(fd, buf + count, len - count);
fd_set rd_set;
int maxfd = fd;
int i;

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;
}

select(maxfd + 1, &rd_set, NULL, NULL, NULL);
if (vpninfo->cancel_fd != -1 &&
FD_ISSET(vpninfo->cancel_fd, &rd_set))
return -EINTR;

/* Not that this should ever be able to happen... */
if (!FD_ISSET(fd, &rd_set))
continue;

i = read(fd, buf + count, len - count);
if (i < 0)
return -errno;

Expand Down
5 changes: 3 additions & 2 deletions ssl.c
Expand Up @@ -1062,8 +1062,6 @@ int openconnect_open_https(struct openconnect_info *vpninfo)
}
}
fcntl(ssl_sock, F_SETFD, FD_CLOEXEC);
/* Stick it back in blocking mode for now... */
fcntl(ssl_sock, F_SETFL, fcntl(ssl_sock, F_GETFL) & ~O_NONBLOCK);

if (vpninfo->proxy) {
err = process_proxy(vpninfo, ssl_sock);
Expand All @@ -1073,6 +1071,9 @@ int openconnect_open_https(struct openconnect_info *vpninfo)
}
}

/* Stick it back in blocking mode for now... */
fcntl(ssl_sock, F_SETFL, fcntl(ssl_sock, F_GETFL) & ~O_NONBLOCK);

ssl3_method = TLSv1_client_method();
if (!vpninfo->https_ctx) {
vpninfo->https_ctx = SSL_CTX_new(ssl3_method);
Expand Down

0 comments on commit 033a334

Please sign in to comment.