[PATCH 9/9] [RFC] Add setup_tun() callback

Kevin Cernekee cernekee at gmail.com
Sun Feb 7 20:26:06 PST 2016


Library users used to do something like:

  openconnect_obtain_cookie()
  openconnect_make_cstp_connection()
  openconnect_setup_dtls()
  openconnect_get_ip_info()
  # ask the OS to create the tun interface
  openconnect_setup_tun_fd()
  openconnect_mainloop()

But now that MTU is calculated a few seconds after the mainloop starts
up, it is necessary to provide a callback so that the calling application
can create a tun interface with the correct MTU.

(Bonus: Android and Chrome OS currently do not allow the MTU, IP address,
or other parameters to be adjusted after the initial settings were sent
to the OS.)

Signed-off-by: Kevin Cernekee <cernekee at gmail.com>
---
 libopenconnect.map.in  | 1 +
 library.c              | 6 ++++++
 mainloop.c             | 6 ++++++
 openconnect-internal.h | 1 +
 openconnect.h          | 5 +++++
 5 files changed, 19 insertions(+)

diff --git a/libopenconnect.map.in b/libopenconnect.map.in
index 550cbd2..8274682 100644
--- a/libopenconnect.map.in
+++ b/libopenconnect.map.in
@@ -43,6 +43,7 @@ OPENCONNECT_5.0 {
 	openconnect_set_proxy_auth;
 	openconnect_set_reported_os;
 	openconnect_set_reqmtu;
+	openconnect_set_setup_tun_handler;
 	openconnect_set_stats_handler;
 	openconnect_set_stoken_mode;
 	openconnect_set_system_trust;
diff --git a/library.c b/library.c
index 3970ba0..fec293c 100644
--- a/library.c
+++ b/library.c
@@ -771,6 +771,12 @@ void openconnect_override_getaddrinfo(struct openconnect_info *vpninfo, openconn
 	vpninfo->getaddrinfo_override = gai_fn;
 }
 
+void openconnect_set_setup_tun_handler(struct openconnect_info *vpninfo,
+				       openconnect_setup_tun_vfn setup_tun)
+{
+	vpninfo->setup_tun = setup_tun;
+}
+
 void openconnect_set_stats_handler(struct openconnect_info *vpninfo,
 				   openconnect_stats_vfn stats_handler)
 {
diff --git a/mainloop.c b/mainloop.c
index 0363277..b261ddd 100644
--- a/mainloop.c
+++ b/mainloop.c
@@ -110,6 +110,12 @@ static int setup_tun_device(struct openconnect_info *vpninfo)
 {
 	int ret;
 
+	if (vpninfo->setup_tun) {
+		vpninfo->setup_tun(vpninfo->cbdata);
+		if (vpninfo->tun_is_up)
+			return 0;
+	}
+
 #ifndef _WIN32
 	if (vpninfo->use_tun_script) {
 		ret = openconnect_setup_tun_script(vpninfo, vpninfo->vpnc_script);
diff --git a/openconnect-internal.h b/openconnect-internal.h
index fbcbff5..8dc761f 100644
--- a/openconnect-internal.h
+++ b/openconnect-internal.h
@@ -611,6 +611,7 @@ struct openconnect_info {
 	openconnect_progress_vfn progress;
 	openconnect_protect_socket_vfn protect_socket;
 	openconnect_getaddrinfo_vfn getaddrinfo_override;
+	openconnect_setup_tun_vfn setup_tun;
 
 	int (*ssl_read)(struct openconnect_info *vpninfo, char *buf, size_t len);
 	int (*ssl_gets)(struct openconnect_info *vpninfo, char *buf, size_t len);
diff --git a/openconnect.h b/openconnect.h
index 1dd683f..f4ebaba 100644
--- a/openconnect.h
+++ b/openconnect.h
@@ -606,6 +606,11 @@ typedef int (*openconnect_getaddrinfo_vfn) (void *privdata, const char *node, co
 					    const struct addrinfo *hints, struct addrinfo **res);
 void openconnect_override_getaddrinfo(struct openconnect_info *vpninfo, openconnect_getaddrinfo_vfn gai_fn);
 
+/* Callback for configuring the interface after MTU detection finishes. */
+typedef void (*openconnect_setup_tun_vfn) (void *privdata);
+void openconnect_set_setup_tun_handler(struct openconnect_info *vpninfo,
+				       openconnect_setup_tun_vfn setup_tun);
+
 #ifdef __cplusplus
 }
 #endif
-- 
1.9.1




More information about the openconnect-devel mailing list