From patchwork Tue Dec 5 17:28:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 136 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director1.mail.ord1d.rsapps.net ([172.27.255.7]) by backend31.mail.ord1d.rsapps.net (Dovecot) with LMTP id 3NGQE7FyJ1r4WgAAgoeIoA for ; Tue, 05 Dec 2017 23:31:45 -0500 Received: from proxy16.mail.iad3a.rsapps.net ([172.27.255.7]) by director1.mail.ord1d.rsapps.net (Dovecot) with LMTP id axjoGbFyJ1rcBQAANGzteQ ; Tue, 05 Dec 2017 23:31:45 -0500 Received: from smtp36.gate.iad3a ([172.27.255.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy16.mail.iad3a.rsapps.net (Dovecot) with LMTP id WqDIFrFyJ1qpNwAADc5QwQ ; Tue, 05 Dec 2017 23:31:45 -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.34.181.88] Authentication-Results: smtp36.gate.iad3a.rsapps.net; iprev=pass policy.iprev="216.34.181.88"; 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; dkim=fail (signature verification failed) header.d=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Classification-ID: 5d691db0-da3e-11e7-b3e3-d4ae527e7642-1-1 Received: from [216.34.181.88] ([216.34.181.88:42563] helo=lists.sourceforge.net) by smtp36.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.1.56364 r(Core:4.2.1.14)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 39/9E-30775-1B2772A5; Tue, 05 Dec 2017 23:31:45 -0500 Received: from localhost ([127.0.0.1] helo=sfs-ml-3.v29.ch3.sourceforge.com) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.89) (envelope-from ) id 1eMRKH-0000uD-T9; Wed, 06 Dec 2017 04:28:53 +0000 Received: from sfi-mx-3.v28.ch3.sourceforge.com ([172.29.28.193] helo=mx.sourceforge.net) by sfs-ml-3.v29.ch3.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1eMRKG-0000u6-QK for openvpn-devel@lists.sourceforge.net; Wed, 06 Dec 2017 04:28:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: MIME-Version:Content-Type:Content-Transfer-Encoding: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=Muk4XBV5Jj80fOsfWUohZVux/kOQX5KTMdLZVn+lVsc=; b=KHCBJTEAbbt12vDtuNi+75slSe QFP4GKtHyStQFrmX3cRBvSsAaPYQyfLSjo5ITI/gdlcAVsrL6icM4pf65Y7R8P4z0aWwf/pZB4LSs q3LQuGcXf/747KAhVNErHL/L0fotszfMCu2CnUTJuhB/hwoaWKkjYBTqc4bTXrHSsYWQ=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version: Content-Type:Content-Transfer-Encoding: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=Muk4XBV5Jj80fOsfWUohZVux/kOQX5KTMdLZVn+lVsc=; b=gEo2+E5VRJRTgKwaTXdbxVDAb1 RRFGQGcR3EgvwP0NeNYqNXM1w6lyz1nXxGELr2/vRdwB2tH7XOSUSOR79gb40/tiHeyYoQsf3Px0P jUwZHj86NJ3pXwPfVa6nqgHAhZxERbBdZivtcm0xlbAzlXF5eXQW0Q2oRht04O5jrCIo=; Received: from mail-io0-f195.google.com ([209.85.223.195]) by sfi-mx-3.v28.ch3.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.89) id 1eMRKF-00028F-OH for openvpn-devel@lists.sourceforge.net; Wed, 06 Dec 2017 04:28:52 +0000 Received: by mail-io0-f195.google.com with SMTP id q15so1196590ioh.2 for ; Tue, 05 Dec 2017 20:28:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Muk4XBV5Jj80fOsfWUohZVux/kOQX5KTMdLZVn+lVsc=; b=l6kjt6l/5Uu3fWX9G7HfjJDkfotAk0B3jcnr/NcAO4I8qMjrNCv560d7hAmIqF79FR U+lWF1rXPheD9U/4H+caWtexCk6hShWQcznfvzmWi2pyBV9/b/Uze0UlgF0IP7tNkacD YzjDzlQmKjLQFO+wBETAsiGgE0nY19hIH0JAAJE/UwoWOybmZwXsdCPRwtO8lQwTXIwz Xgg9kk4Zmg0jXYMiB5EnbKyPpPzvYsOABAnczBOzRN8n2u18p4SaHesifG0oE7BPAfHQ hcqSm228Zvv18PSNBRwd/YvpwF84wDX7gp0Vej8+e097cBqONqVWmd6xZOojQN2+3qj5 unVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Muk4XBV5Jj80fOsfWUohZVux/kOQX5KTMdLZVn+lVsc=; b=WkLNC0WSqosYz0FJeL7XAydDB+/4IFQPuy40bgtRd1IbmMy1w1J1LfM5fVBESSOZ15 li7JxK8ccH/KssQCLlynk3mtg/l6jSCxCoXJAlfAhrSJN3toGRAb1wxEY1ri6gLdw5nc bujQFe198fzv8srbf1rk8lq0c4UyV9HhHTgKkLpxFcJr0Sm7TjhJvBdrjwPHAcMoC4eH mDezrnVWL4dbJcg7+ePOPl9ALi3ODuhedTAGIbaFNFkpGxnlXsfV6YvUUb5cIdtYwthI pnQZpjNr+MZJSQR3H2PduZ0a9+stN/3uz1kARUQvyqmSC+HR8/NMZEm50psGJCYkAsio /rFg== X-Gm-Message-State: AJaThX7Btv3vqaTDMBfMJFOCQkkLwQxFh7LKTKKd1JXBp6vXZOI/8HuQ 9Jaen5QztoAZjWhTA8d25GaB0MuF X-Google-Smtp-Source: AGs4zMZmExViOLWJ19iEdlhBTh7or8j7mlimpHw41x2s5NvbZJ6NM4dAxYzkcZ5q5Z/saowYPOx3bQ== X-Received: by 10.107.164.77 with SMTP id n74mr33705992ioe.45.1512534526141; Tue, 05 Dec 2017 20:28:46 -0800 (PST) Received: from saturn.home.sansel.ca (CPE40167ea0e1c2-CM788df74daaa0.cpe.net.cable.rogers.com. [99.228.215.92]) by smtp.gmail.com with ESMTPSA id z19sm728638iof.53.2017.12.05.20.28.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 05 Dec 2017 20:28:45 -0800 (PST) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Tue, 5 Dec 2017 23:28:41 -0500 Message-Id: <1512534521-14760-1-git-send-email-selva.nair@gmail.com> X-Mailer: git-send-email 2.1.4 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (selva.nair[at]gmail.com) -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [209.85.223.195 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-Headers-End: 1eMRKF-00028F-OH Subject: [Openvpn-devel] [PATCH] Refactor get_interface_metric to return metric and auto flag separately 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: , MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Selva Nair - Instead of returning metric = 0 when automatic metric is in use return the actual metric and flag automatic metric through a parameter. This makes the function reusable elsewhere. - Ensure return value can be correctly cast to int and return -1 on error. Signed-off-by: Selva Nair Acked-by: Simon Rozman --- src/openvpn/block_dns.c | 30 ++++++++++++++++++++---------- src/openvpn/block_dns.h | 14 +++++++------- src/openvpn/win32.c | 15 +++++++-------- src/openvpnserv/interactive.c | 13 +++++++------ 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/openvpn/block_dns.c b/src/openvpn/block_dns.c index d43cbcf..8d5011d 100644 --- a/src/openvpn/block_dns.c +++ b/src/openvpn/block_dns.c @@ -344,33 +344,43 @@ delete_block_dns_filters(HANDLE engine_handle) } /* - * Returns interface metric value for specified interface index. + * Return interface metric value for the specified interface index. * * Arguments: * index : The index of TAP adapter. * family : Address family (AF_INET for IPv4 and AF_INET6 for IPv6). - * Returns positive metric value or zero for automatic metric on success, - * a less then zero error code on failure. + * is_auto : On return set to true if automatic metric is in use. + * Unused if NULL. + * + * Returns positive metric value or -1 on error. */ - int -get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family) +get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family, int *is_auto) { DWORD err = 0; MIB_IPINTERFACE_ROW ipiface; InitializeIpInterfaceEntry(&ipiface); ipiface.Family = family; ipiface.InterfaceIndex = index; + + if (is_auto) + { + *is_auto = 0; + } err = GetIpInterfaceEntry(&ipiface); - if (err == NO_ERROR) + + /* On Windows metric is never > INT_MAX so return value of int is ok. + * But we check for overflow nevertheless. + */ + if (err == NO_ERROR && ipiface.Metric <= INT_MAX) { - if (ipiface.UseAutomaticMetric) + if (is_auto) { - return 0; + *is_auto = ipiface.UseAutomaticMetric; } - return ipiface.Metric; + return (int)ipiface.Metric; } - return -err; + return -1; } /* diff --git a/src/openvpn/block_dns.h b/src/openvpn/block_dns.h index c9a9d70..50b383f 100644 --- a/src/openvpn/block_dns.h +++ b/src/openvpn/block_dns.h @@ -39,17 +39,17 @@ add_block_dns_filters(HANDLE *engine, int iface_index, const WCHAR *exe_path, block_dns_msg_handler_t msg_handler_callback); /** - * Returns interface metric value for specified interface index. + * Return interface metric value for the specified interface index. * - * @param index The index of TAP adapter - * @param family Address family (AF_INET for IPv4 and AF_INET6 for IPv6) + * @param index The index of TAP adapter. + * @param family Address family (AF_INET for IPv4 and AF_INET6 for IPv6). + * @param is_auto On return set to true if automatic metric is in use. + * Unused if NULL. * - * @return positive metric value or zero for automatic metric on success, - * a less then zero error code on failure. + * @return positive interface metric on success or -1 on error */ - int -get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family); +get_interface_metric(const NET_IFINDEX index, const ADDRESS_FAMILY family, int *is_auto); /** * Sets interface metric value for specified interface index. diff --git a/src/openvpn/win32.c b/src/openvpn/win32.c index 95fea5d..4b20298 100644 --- a/src/openvpn/win32.c +++ b/src/openvpn/win32.c @@ -1344,17 +1344,16 @@ win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel) block_dns_msg_handler); if (status == 0) { - tap_metric_v4 = get_interface_metric(index, AF_INET); - tap_metric_v6 = get_interface_metric(index, AF_INET6); - if (tap_metric_v4 < 0) + int is_auto = 0; + tap_metric_v4 = get_interface_metric(index, AF_INET, &is_auto); + if (is_auto) { - /* error, should not restore metric */ - tap_metric_v4 = -1; + tap_metric_v4 = 0; } - if (tap_metric_v6 < 0) + tap_metric_v6 = get_interface_metric(index, AF_INET6, &is_auto); + if (is_auto) { - /* error, should not restore metric */ - tap_metric_v6 = -1; + tap_metric_v6 = 0; } status = set_interface_metric(index, AF_INET, BLOCK_DNS_IFACE_METRIC); if (!status) diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c index aab784e..3907438 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -804,17 +804,18 @@ HandleBlockDNSMessage(const block_dns_message_t *msg, undo_lists_t *lists) } interface_data->engine = engine; interface_data->index = msg->iface.index; + int is_auto = 0; interface_data->metric_v4 = get_interface_metric(msg->iface.index, - AF_INET); - if (interface_data->metric_v4 < 0) + AF_INET, &is_auto); + if (is_auto) { - interface_data->metric_v4 = -1; + interface_data->metric_v4 = 0; } interface_data->metric_v6 = get_interface_metric(msg->iface.index, - AF_INET6); - if (interface_data->metric_v6 < 0) + AF_INET6, &is_auto); + if (is_auto) { - interface_data->metric_v6 = -1; + interface_data->metric_v6 = 0; } err = AddListItem(&(*lists)[block_dns], interface_data); if (!err)