Skip to content

Commit

Permalink
library: Add new openconnect_setup_cmd_pipe() call
Browse files Browse the repository at this point in the history
Most callers will use a simple nonblocking UNIX pipe as their cmd_fd,
so provide a convenience function for creating it.  Also, libopenconnect
will take care of cleaning up pipes created in this manner when the
library instance is freed.

Pipes created through this function will be able to send new commands,
such as "reconnect".  Pipes passed in via openconnect_set_cancel_fd()
can only be used to cancel the connection, for backward compatibility.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
  • Loading branch information
cernekee committed Jan 15, 2014
1 parent 62eb3b4 commit 5e4048b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libopenconnect.map.in
Expand Up @@ -35,6 +35,11 @@ OPENCONNECT_3.0 {
openconnect_set_token_mode;
};

OPENCONNECT_3.1 {
global:
openconnect_setup_cmd_pipe;
} OPENCONNECT_3.0;

OPENCONNECT_PRIVATE {
global: @SYMVER_TIME@ @SYMVER_ASPRINTF@ @SYMVER_GETLINE@ @SYMVER_PRINT_ERR@
openconnect_SSL_gets;
Expand Down
24 changes: 24 additions & 0 deletions library.c
Expand Up @@ -26,6 +26,8 @@
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#ifdef HAVE_LIBSTOKEN
#include <stoken.h>
Expand Down Expand Up @@ -57,6 +59,7 @@ struct openconnect_info *openconnect_vpninfo_new(char *useragent,
vpninfo->progress = progress;
vpninfo->cbdata = privdata ? : vpninfo;
vpninfo->cmd_fd = -1;
vpninfo->cmd_fd_write = -1;
vpninfo->xmlpost = 1;
openconnect_set_reported_os(vpninfo, NULL);

Expand Down Expand Up @@ -106,6 +109,10 @@ static void free_optlist(struct vpn_option *opt)
void openconnect_vpninfo_free(struct openconnect_info *vpninfo)
{
openconnect_close_https(vpninfo, 1);
if (vpninfo->cmd_fd_write != -1) {
close(vpninfo->cmd_fd);
close(vpninfo->cmd_fd_write);
}
free(vpninfo->peer_addr);
free_optlist(vpninfo->cookies);
free_optlist(vpninfo->cstp_options);
Expand Down Expand Up @@ -282,6 +289,23 @@ void openconnect_set_cancel_fd(struct openconnect_info *vpninfo, int fd)
vpninfo->cmd_fd = fd;
}

int openconnect_setup_cmd_pipe(struct openconnect_info *vpninfo)
{
int pipefd[2];

if (pipe(pipefd) < 0)
return -EIO;
if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) ||
fcntl(pipefd[1], F_SETFL, O_NONBLOCK)) {
close(pipefd[0]);
close(pipefd[1]);
return -EIO;
}
vpninfo->cmd_fd = pipefd[0];
vpninfo->cmd_fd_write = pipefd[1];
return vpninfo->cmd_fd_write;
}

const char *openconnect_get_version(void)
{
return openconnect_version_str;
Expand Down
2 changes: 2 additions & 0 deletions openconnect-internal.h
Expand Up @@ -295,7 +295,9 @@ struct openconnect_info {
int ssl_fd;
int dtls_fd;
int new_dtls_fd;

int cmd_fd;
int cmd_fd_write;

struct pkt *incoming_queue;
struct pkt *outgoing_queue;
Expand Down
6 changes: 6 additions & 0 deletions openconnect.h
Expand Up @@ -246,6 +246,12 @@ void openconnect_set_cert_expiry_warning(struct openconnect_info *vpninfo,
cancellation mechanism inactive. */
void openconnect_set_cancel_fd(struct openconnect_info *vpninfo, int fd);

/* Create a nonblocking pipe used to send cancellations and other commands
to the library. This returns a file descriptor to the write side of
the pipe. Both sides will be closed by openconnect_vpninfo_free().
This replaces openconnect_set_cancel_fd(). */
int openconnect_setup_cmd_pipe(struct openconnect_info *vpninfo);

const char *openconnect_get_version(void);

/* The first (privdata) argument to each of these functions is either
Expand Down

0 comments on commit 5e4048b

Please sign in to comment.