Skip to content

Commit

Permalink
Add set_sock_nonblock() and set_fd_cloexec() compat functions
Browse files Browse the repository at this point in the history
The latter does nothing on Windows, and the former is *only* for sockets.

Which means that it's probably broken where we're using it on pipes, but
we'll deal with that later...

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Feb 6, 2014
1 parent 1f9ed77 commit 441a729
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions dtls.c
Expand Up @@ -547,8 +547,8 @@ int connect_dtls_socket(struct openconnect_info *vpninfo)
return -EINVAL;
}

fcntl(dtls_fd, F_SETFD, FD_CLOEXEC);
fcntl(dtls_fd, F_SETFL, fcntl(dtls_fd, F_GETFL) | O_NONBLOCK);
set_fd_cloexec(dtls_fd);
set_sock_nonblock(dtls_fd);

ret = start_dtls_handshake(vpninfo, dtls_fd);
if (ret) {
Expand Down
3 changes: 1 addition & 2 deletions library.c
Expand Up @@ -379,8 +379,7 @@ int openconnect_setup_cmd_pipe(struct openconnect_info *vpninfo)

if (pipe(pipefd) < 0)
return -EIO;
if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) ||
fcntl(pipefd[1], F_SETFL, O_NONBLOCK)) {
if (set_sock_nonblock(pipefd[0]) || set_sock_nonblock(pipefd[1])) {
close(pipefd[0]);
close(pipefd[1]);
return -EIO;
Expand Down
19 changes: 19 additions & 0 deletions openconnect-internal.h
Expand Up @@ -67,6 +67,7 @@
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#endif

#ifdef LIBPROXY_HDR
Expand Down Expand Up @@ -369,6 +370,24 @@ void openconnect__unsetenv(const char *name);
int openconnect__inet_aton(const char *cp, struct in_addr *addr);
#endif

static inline int set_sock_nonblock(int fd)
{
#ifdef _WIN32
unsigned long mode = 0;
return ioctlsocket(fd, FIONBIO, &mode);
#else
return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
#endif
}
static inline int set_fd_cloexec(int fd)
{
#ifdef _WIN32
return 0; /* Windows has O_INHERIT but... */
#else
return fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
#endif
}

#ifdef _WIN32
#define pipe(fds) _pipe(fds, 4096, O_BINARY)
#define neterrno openconnect__win32_neterrno
Expand Down
6 changes: 3 additions & 3 deletions ssl.c
Expand Up @@ -62,7 +62,7 @@ static int cancellable_connect(struct openconnect_info *vpninfo, int sockfd,
fd_set wr_set, rd_set;
int maxfd = sockfd;

fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK);
set_sock_nonblock(sockfd);
if (vpninfo->protect_socket)
vpninfo->protect_socket(vpninfo->cbdata, sockfd);

Expand Down Expand Up @@ -105,7 +105,7 @@ int connect_https_socket(struct openconnect_info *vpninfo)
ssl_sock = socket(vpninfo->peer_addr->sa_family, SOCK_STREAM, IPPROTO_IP);
if (ssl_sock < 0)
goto reconn_err;
fcntl(ssl_sock, F_SETFD, fcntl(ssl_sock, F_GETFD) | FD_CLOEXEC);
set_fd_cloexec(ssl_sock);
}
if (cancellable_connect(vpninfo, ssl_sock, vpninfo->peer_addr, vpninfo->peer_addrlen)) {
reconn_err:
Expand Down Expand Up @@ -234,7 +234,7 @@ int connect_https_socket(struct openconnect_info *vpninfo)
rp->ai_protocol);
if (ssl_sock < 0)
continue;
fcntl(ssl_sock, F_SETFD, fcntl(ssl_sock, F_GETFD) | FD_CLOEXEC);
set_fd_cloexec(ssl_sock);
if (cancellable_connect(vpninfo, ssl_sock, rp->ai_addr, rp->ai_addrlen) >= 0) {
/* Store the peer address we actually used, so that DTLS can
use it again later */
Expand Down
4 changes: 2 additions & 2 deletions tun.c
Expand Up @@ -633,7 +633,7 @@ static int os_setup_tun(struct openconnect_info *vpninfo)

int openconnect_setup_tun_fd(struct openconnect_info *vpninfo, int tun_fd)
{
fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
set_fd_cloexec(tun_fd);

if (vpninfo->tun_fd != -1)
FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
Expand All @@ -644,7 +644,7 @@ int openconnect_setup_tun_fd(struct openconnect_info *vpninfo, int tun_fd)

FD_SET(tun_fd, &vpninfo->select_rfds);

fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
set_sock_nonblock(tun_fd);

return 0;
}
Expand Down

0 comments on commit 441a729

Please sign in to comment.