Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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: Kevin Cernekee <cernekee@gmail.com>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
cernekee authored and David Woodhouse committed Mar 8, 2016
1 parent 33638c2 commit b153f00
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
15 changes: 5 additions & 10 deletions mainloop.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -200,28 +199,24 @@ 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);
if (vpninfo->quit_reason)
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);
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 8 additions & 1 deletion openconnect-internal.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b153f00

Please sign in to comment.