From patchwork Wed Nov 21 05:36:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Rozman X-Patchwork-Id: 621 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.27.255.52]) by backend30.mail.ord1d.rsapps.net with LMTP id gI6YHPeJ9Vs+XwAAIUCqbw for ; Wed, 21 Nov 2018 11:38:15 -0500 Received: from proxy4.mail.iad3a.rsapps.net ([172.27.255.52]) by director9.mail.ord1d.rsapps.net with LMTP id sGSGGfeJ9VtJVwAAalYnBA ; Wed, 21 Nov 2018 11:38:15 -0500 Received: from smtp20.gate.iad3a ([172.27.255.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy4.mail.iad3a.rsapps.net with LMTP id yMs2EveJ9Vs/HAAA8Zvu4w ; Wed, 21 Nov 2018 11:38:15 -0500 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp20.gate.iad3a.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=fail (p=none; dis=none) header.from=rozman.si X-Suspicious-Flag: YES X-Classification-ID: d7176b4a-edab-11e8-92a8-525400aab2f3-1-1 Received: from [216.105.38.7] ([216.105.38.7:2405] helo=lists.sourceforge.net) by smtp20.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id F0/99-20357-6F985FB5; Wed, 21 Nov 2018 11:38:14 -0500 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1gPVVG-0005fl-Vu; Wed, 21 Nov 2018 16:37:26 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1gPVVF-0005fb-ML for openvpn-devel@lists.sourceforge.net; Wed, 21 Nov 2018 16:37:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=gvHIMo/PrSpdxxCZLyZ54VcYnIfhoJLsyaKQupCKP/Q=; b=mHXKjGobuwUf9q2Sxx+/CfIAaX 9ArW5kT82xyIrtVn+hhx1Gp8/z74tkRqJb9OoqAv0H5eSbVZK4vNbF2LCwDMCqC6F4czAn3GxzMVe oqaEPdtlnC4lagJalCqxIm7y2YY5hf3yuuop6DKPdEMOpHZZ/xlbhcBN6eleFynrAH0U=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=gvHIMo/PrSpdxxCZLyZ54VcYnIfhoJLsyaKQupCKP/Q=; b=l m0Q7cTUZ4i7cDmIixpGfMmILqglcs4OMXhoMnMP/YoQ6fem++9+vk1rTQFNM1oGSsg0H4uTIhSH3d z7yns5Dyc/iInA6bTR/ie4PY5ATv84U7A0NdRNCwuwLXl5B3eQgX7kG8+PRUdpD5zMTAFAmLzssab tB5U/9xZ+zgvXJLQ=; Received: from pub5.amebis.si ([213.250.55.21]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) id 1gPVUz-0097XM-R7 for openvpn-devel@lists.sourceforge.net; Wed, 21 Nov 2018 16:37:25 +0000 Received: from gw0.selo.doma (unknown [IPv6:2a00:ee2:1206:8101::1]) by pub5.amebis.si (Postfix) with ESMTP id 064932001D; Wed, 21 Nov 2018 17:36:58 +0100 (CET) Received: from s0.selo.doma (s0.selo.doma [IPv6:2a00:ee2:1206:8101::d]) by gw0.selo.doma (Postfix) with ESMTP id CBC4321B9824; Wed, 21 Nov 2018 17:36:57 +0100 (CET) From: Simon Rozman To: openvpn-devel@lists.sourceforge.net Date: Wed, 21 Nov 2018 17:36:53 +0100 Message-Id: <20181121163653.4904-1-simon@rozman.si> X-Mailer: git-send-email 2.19.0.windows.1 MIME-Version: 1.0 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 TIME_LIMIT_EXCEEDED Exceeded time limit / deadline X-Headers-End: 1gPVUz-0097XM-R7 Subject: [Openvpn-devel] [PATCH] Add detection of active VPN connections for MSI packages X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox This patch adds a simple up/down detection for each TAP interface found. All "up" TAP interface IDs are listed as an `ACTIVETAPINTERFACES` MSI property. This allows MSI packages to test for active VPN connections on updates. Acked-by: Gert Doering --- src/openvpnmsica/Makefile.am | 2 +- src/openvpnmsica/openvpnmsica.c | 81 +++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/openvpnmsica/Makefile.am b/src/openvpnmsica/Makefile.am index ecca74bc..f4725a53 100644 --- a/src/openvpnmsica/Makefile.am +++ b/src/openvpnmsica/Makefile.am @@ -41,7 +41,7 @@ libopenvpnmsica_la_CFLAGS = \ -municode -D_UNICODE \ -UNTDDI_VERSION -U_WIN32_WINNT \ -D_WIN32_WINNT=_WIN32_WINNT_VISTA -libopenvpnmsica_la_LDFLAGS = -ladvapi32 -lole32 -lmsi -lsetupapi -lshlwapi -lversion -no-undefined -avoid-version +libopenvpnmsica_la_LDFLAGS = -ladvapi32 -lole32 -lmsi -lsetupapi -liphlpapi -lshlwapi -lversion -no-undefined -avoid-version endif libopenvpnmsica_la_SOURCES = \ diff --git a/src/openvpnmsica/openvpnmsica.c b/src/openvpnmsica/openvpnmsica.c index a2819e62..e8988ac8 100644 --- a/src/openvpnmsica/openvpnmsica.c +++ b/src/openvpnmsica/openvpnmsica.c @@ -22,6 +22,7 @@ #elif defined(_MSC_VER) #include #endif +#include /* Must be included _before_ */ #include "openvpnmsica.h" #include "msica_op.h" @@ -32,6 +33,7 @@ #include "../tapctl/tap.h" #include +#include #include #include #include @@ -41,6 +43,7 @@ #include #ifdef _MSC_VER +#pragma comment(lib, "iphlpapi.lib") #pragma comment(lib, "shlwapi.lib") #pragma comment(lib, "version.lib") #endif @@ -322,6 +325,34 @@ FindTAPInterfaces(_In_ MSIHANDLE hInstall) if (uiResult != ERROR_SUCCESS) goto cleanup_CoInitialize; + /* Get IPv4/v6 info for all network interfaces. Actually, we're interested in link status only: up/down? */ + PIP_ADAPTER_ADDRESSES pAdapterAdresses = NULL; + ULONG ulAdapterAdressesSize = 16*1024; + for (size_t iteration = 0; iteration < 2; iteration++) { + pAdapterAdresses = (PIP_ADAPTER_ADDRESSES)malloc(ulAdapterAdressesSize); + if (pAdapterAdresses == NULL) { + msg(M_NONFATAL, "%s: malloc(%u) failed", __FUNCTION__, ulAdapterAdressesSize); + uiResult = ERROR_OUTOFMEMORY; goto cleanup_tap_list_interfaces; + } + + ULONG ulResult = GetAdaptersAddresses( + AF_UNSPEC, + GAA_FLAG_SKIP_UNICAST | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME | GAA_FLAG_INCLUDE_ALL_INTERFACES, + NULL, + pAdapterAdresses, + &ulAdapterAdressesSize); + + if (ulResult == ERROR_SUCCESS) + break; + + free(pAdapterAdresses); + if (ulResult != ERROR_BUFFER_OVERFLOW) { + SetLastError(ulResult); /* MSDN does not mention GetAdaptersAddresses() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: GetAdaptersAddresses() failed", __FUNCTION__); + uiResult = ulResult; goto cleanup_tap_list_interfaces; + } + } + /* Enumerate interfaces. */ struct interface_node { @@ -363,24 +394,52 @@ FindTAPInterfaces(_In_ MSIHANDLE hInstall) if (interface_count) { - /* Prepare semicolon delimited list of TAP interface ID(s). */ + /* Prepare semicolon delimited list of TAP interface ID(s) and active TAP interface ID(s). */ LPTSTR - szTAPInterfaces = (LPTSTR)malloc(interface_count * (38/*GUID*/ + 1/*separator/terminator*/) * sizeof(TCHAR)), - szTAPInterfacesTail = szTAPInterfaces; + szTAPInterfaces = (LPTSTR)malloc(interface_count * (38/*GUID*/ + 1/*separator/terminator*/) * sizeof(TCHAR)), + szTAPInterfacesTail = szTAPInterfaces, + szTAPInterfacesActive = (LPTSTR)malloc(interface_count * (38/*GUID*/ + 1/*separator/terminator*/) * sizeof(TCHAR)), + szTAPInterfacesActiveTail = szTAPInterfacesActive; while (interfaces_head) { + /* Convert interface GUID to UTF-16 string. (LPOLESTR defaults to LPWSTR) */ LPOLESTR szInterfaceId = NULL; StringFromIID((REFIID)&interfaces_head->iface->guid, &szInterfaceId); + + /* Append to the list of TAP interface ID(s). */ + if (szTAPInterfaces < szTAPInterfacesTail) + *(szTAPInterfacesTail++) = TEXT(';'); memcpy(szTAPInterfacesTail, szInterfaceId, 38 * sizeof(TCHAR)); szTAPInterfacesTail += 38; + + /* If this interface is active (connected), add it to the list of active TAP interface ID(s). */ + for (PIP_ADAPTER_ADDRESSES p = pAdapterAdresses; p; p = p->Next) + { + OLECHAR szId[38 + 1]; + GUID guid; + if (MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, p->AdapterName, -1, szId, _countof(szId)) > 0 && + SUCCEEDED(IIDFromString(szId, &guid)) && + memcmp(&guid, &interfaces_head->iface->guid, sizeof(GUID)) == 0) + { + if (p->OperStatus == IfOperStatusUp) + { + /* This TAP interface is active (connected). */ + if (szTAPInterfacesActive < szTAPInterfacesActiveTail) + *(szTAPInterfacesActiveTail++) = TEXT(';'); + memcpy(szTAPInterfacesActiveTail, szInterfaceId, 38 * sizeof(TCHAR)); + szTAPInterfacesActiveTail += 38; + } + break; + } + } CoTaskMemFree(szInterfaceId); - szTAPInterfacesTail[0] = interfaces_head->next ? TEXT(';') : 0; - szTAPInterfacesTail++; struct interface_node *p = interfaces_head; interfaces_head = interfaces_head->next; free(p); } + szTAPInterfacesTail [0] = 0; + szTAPInterfacesActiveTail[0] = 0; /* Set Installer TAPINTERFACES property. */ uiResult = MsiSetProperty(hInstall, TEXT("TAPINTERFACES"), szTAPInterfaces); @@ -391,12 +450,24 @@ FindTAPInterfaces(_In_ MSIHANDLE hInstall) goto cleanup_szTAPInterfaces; } + /* Set Installer ACTIVETAPINTERFACES property. */ + uiResult = MsiSetProperty(hInstall, TEXT("ACTIVETAPINTERFACES"), szTAPInterfacesActive); + if (uiResult != ERROR_SUCCESS) + { + SetLastError(uiResult); /* MSDN does not mention MsiSetProperty() to set GetLastError(). But we do have an error code. Set last error manually. */ + msg(M_NONFATAL | M_ERRNO, "%s: MsiSetProperty(\"ACTIVETAPINTERFACES\") failed", __FUNCTION__); + goto cleanup_szTAPInterfaces; + } + cleanup_szTAPInterfaces: + free(szTAPInterfacesActive); free(szTAPInterfaces); } else uiResult = ERROR_SUCCESS; + free(pAdapterAdresses); +cleanup_tap_list_interfaces: tap_free_interface_list(pInterfaceList); cleanup_CoInitialize: if (bIsCoInitialized) CoUninitialize();