[PATCH V2 03/11] Convert tun_is_up into an inline function

Kevin Cernekee cernekee at gmail.com
Mon Feb 8 22:34:09 PST 2016


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 at gmail.com>
---
 mainloop.c             | 15 +++++----------
 openconnect-internal.h |  9 ++++++++-
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/mainloop.c b/mainloop.c
index 8b7b9a55fc22..264abc504322 100644
--- a/mainloop.c
+++ b/mainloop.c
@@ -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;
 }
diff --git a/openconnect-internal.h b/openconnect-internal.h
index 7ebf31d5c1d3..c39042778cb2 100644
--- a/openconnect-internal.h
+++ b/openconnect-internal.h
@@ -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)
-- 
2.7.0




More information about the openconnect-devel mailing list