Commit b153f005 authored by Kevin Cernekee's avatar Kevin Cernekee Committed by David Woodhouse

Convert tun_is_up into an inline function

The tunnel state can be figured out by looking at tun_fd or tun_fh,
so replace the discrete variable with an inline function.

This has the side effect of fixing the case where the tunnel is set
up by a library caller prior to entering the mainloop.
Signed-off-by: default avatarKevin Cernekee <cernekee@gmail.com>
Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 33638c2c
......@@ -50,7 +50,7 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
struct pkt *this;
int work_done = 0;
if (!vpninfo->tun_is_up) {
if (!tun_is_up(vpninfo)) {
/* no tun yet, clear any queued packets */
while ((this = dequeue_packet(&vpninfo->incoming_queue)));
return 0;
......@@ -168,7 +168,6 @@ int openconnect_mainloop(struct openconnect_info *vpninfo,
{
int ret = 0;
vpninfo->tun_is_up = 0;
vpninfo->reconnect_timeout = reconnect_timeout;
vpninfo->reconnect_interval = reconnect_interval;
......@@ -190,7 +189,7 @@ int openconnect_mainloop(struct openconnect_info *vpninfo,
/* If tun is not up, loop more often to detect
* a DTLS timeout (due to a firewall block) as soon. */
if (vpninfo->tun_is_up)
if (tun_is_up(vpninfo))
timeout = INT_MAX;
else
timeout = 1000;
......@@ -200,14 +199,12 @@ int openconnect_mainloop(struct openconnect_info *vpninfo,
* we have a better knowledge of the link MTU. We also
* force the creation if DTLS enters sleeping mode - i.e.,
* we failed to connect on time. */
if (vpninfo->tun_is_up == 0 && (vpninfo->dtls_state == DTLS_CONNECTED ||
if (!tun_is_up(vpninfo) && (vpninfo->dtls_state == DTLS_CONNECTED ||
vpninfo->dtls_state == DTLS_SLEEPING)) {
ret = setup_tun_device(vpninfo);
if (ret) {
break;
}
vpninfo->tun_is_up = 1;
}
ret = vpninfo->proto.udp_mainloop(vpninfo, &timeout);
......@@ -215,13 +212,11 @@ int openconnect_mainloop(struct openconnect_info *vpninfo,
break;
did_work += ret;
} else if (vpninfo->tun_is_up == 0) {
} else if (!tun_is_up(vpninfo)) {
/* No DTLS - setup TUN device unconditionally */
ret = setup_tun_device(vpninfo);
if (ret)
break;
vpninfo->tun_is_up = 1;
}
ret = vpninfo->proto.tcp_mainloop(vpninfo, &timeout);
......@@ -305,7 +300,7 @@ int openconnect_mainloop(struct openconnect_info *vpninfo,
if (vpninfo->quit_reason && vpninfo->proto.vpn_close_session)
vpninfo->proto.vpn_close_session(vpninfo, vpninfo->quit_reason);
if (vpninfo->tun_is_up)
if (tun_is_up(vpninfo))
os_shutdown_tun(vpninfo);
return ret < 0 ? ret : -EIO;
}
......
......@@ -541,7 +541,6 @@ struct openconnect_info {
uid_t uid;
gid_t gid;
#endif
int tun_is_up; /* whether the tun device is setup */
int use_tun_script;
int script_tun;
char *ifname;
......@@ -715,6 +714,14 @@ static inline int set_fd_cloexec(int fd)
return fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
#endif
}
static inline int tun_is_up(struct openconnect_info *vpninfo)
{
#ifdef _WIN32
return vpninfo->tun_fh != NULL;
#else
return vpninfo->tun_fd != -1;
#endif
}
#ifdef _WIN32
#define pipe(fds) _pipe(fds, 4096, O_BINARY)
......
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