From owner-svn-src-all@freebsd.org  Mon Oct 16 06:54:27 2017
Return-Path: <owner-svn-src-all@freebsd.org>
Delivered-To: svn-src-all@mailman.ysv.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org
 [IPv6:2001:1900:2254:206a::19:1])
 by mailman.ysv.freebsd.org (Postfix) with ESMTP id D92BEE314D6;
 Mon, 16 Oct 2017 06:54:27 +0000 (UTC)
 (envelope-from avos@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mx1.freebsd.org (Postfix) with ESMTPS id B3A6F6FB44;
 Mon, 16 Oct 2017 06:54:27 +0000 (UTC)
 (envelope-from avos@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v9G6sQ2l062313;
 Mon, 16 Oct 2017 06:54:26 GMT (envelope-from avos@FreeBSD.org)
Received: (from avos@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id v9G6sQmA062311;
 Mon, 16 Oct 2017 06:54:26 GMT (envelope-from avos@FreeBSD.org)
Message-Id: <201710160654.v9G6sQmA062311@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: avos set sender to
 avos@FreeBSD.org using -f
From: Andriy Voskoboinyk <avos@FreeBSD.org>
Date: Mon, 16 Oct 2017 06:54:26 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-all@freebsd.org,
 svn-src-head@freebsd.org
Subject: svn commit: r324656 - head/lib/libifconfig
X-SVN-Group: head
X-SVN-Commit-Author: avos
X-SVN-Commit-Paths: head/lib/libifconfig
X-SVN-Commit-Revision: 324656
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-all@freebsd.org
X-Mailman-Version: 2.1.23
Precedence: list
List-Id: "SVN commit messages for the entire src tree \(except for &quot;
 user&quot; and &quot; projects&quot; \)" <svn-src-all.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-all>,
 <mailto:svn-src-all-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-all/>
List-Post: <mailto:svn-src-all@freebsd.org>
List-Help: <mailto:svn-src-all-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-all>,
 <mailto:svn-src-all-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Mon, 16 Oct 2017 06:54:28 -0000

Author: avos
Date: Mon Oct 16 06:54:26 2017
New Revision: 324656
URL: https://svnweb.freebsd.org/changeset/base/324656

Log:
  libifconfig: allow to get original interface name via ifconfig_get_orig_name()
  
  Uses the same method as in tools/tools/ifinfo/ifinfo.c
  (via net.link.generic sysctl).
  
  Tested with modified wlandebug(8).
  
  Differential Revision:	https://reviews.freebsd.org/D12554

Modified:
  head/lib/libifconfig/libifconfig.c
  head/lib/libifconfig/libifconfig.h

Modified: head/lib/libifconfig/libifconfig.c
==============================================================================
--- head/lib/libifconfig/libifconfig.c	Mon Oct 16 04:46:28 2017	(r324655)
+++ head/lib/libifconfig/libifconfig.c	Mon Oct 16 06:54:26 2017	(r324656)
@@ -61,9 +61,43 @@
  * $FreeBSD$
  */
 
+ /*
+ * Copyright 1996 Massachusetts Institute of Technology
+ *
+ * Permission to use, copy, modify, and distribute this software and
+ * its documentation for any purpose and without fee is hereby
+ * granted, provided that both the above copyright notice and this
+ * permission notice appear in all copies, that both the above
+ * copyright notice and this permission notice appear in all
+ * supporting documentation, and that the name of M.I.T. not be used
+ * in advertising or publicity pertaining to distribution of the
+ * software without specific, written prior permission.  M.I.T. makes
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied
+ * warranty.
+ *
+ * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
+ * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
+ * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/types.h>
 #include <sys/ioctl.h>
+#include <sys/sysctl.h>
 
 #include <net/if.h>
+#include <net/if_mib.h>
 
 #include <err.h>
 #include <errno.h>
@@ -245,6 +279,67 @@ ifconfig_set_name(ifconfig_handle_t *h, const char *na
 
 	free(tmpname);
 	return (0);
+}
+
+int
+ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname,
+    char **orig_name)
+{
+	struct ifmibdata ifmd;
+	size_t len;
+	int name[6];
+	int i, maxifno;
+
+	name[0] = CTL_NET;
+	name[1] = PF_LINK;
+	name[2] = NETLINK_GENERIC;
+	name[3] = IFMIB_SYSTEM;
+	name[4] = IFMIB_IFCOUNT;
+
+	len = sizeof maxifno;
+	if (sysctl(name, 5, &maxifno, &len, 0, 0) < 0) {
+		h->error.errtype = OTHER;
+		h->error.errcode = errno;
+		return (-1);
+	}
+
+	name[3] = IFMIB_IFDATA;
+	name[5] = IFDATA_GENERAL;
+	for (i = 1; i <= maxifno; i++) {
+		len = sizeof ifmd;
+		name[4] = i;
+		if (sysctl(name, 6, &ifmd, &len, 0, 0) < 0) {
+			if (errno == ENOENT)
+				continue;
+
+			goto fail;
+		}
+
+		if (strncmp(ifmd.ifmd_name, ifname, IFNAMSIZ) != 0)
+			continue;
+
+		len = 0;
+		name[5] = IFDATA_DRIVERNAME;
+		if (sysctl(name, 6, NULL, &len, 0, 0) < 0)
+			goto fail;
+
+		*orig_name = malloc(len);
+		if (*orig_name == NULL)
+			goto fail;
+
+		if (sysctl(name, 6, *orig_name, &len, 0, 0) < 0) {
+			free(*orig_name);
+			*orig_name = NULL;
+			goto fail;
+		}
+
+		return (0);
+	}
+
+fail:
+	h->error.errtype = OTHER;
+	h->error.errcode = (i <= maxifno) ? errno : ENOENT;
+	return (-1);
 }
 
 int

Modified: head/lib/libifconfig/libifconfig.h
==============================================================================
--- head/lib/libifconfig/libifconfig.h	Mon Oct 16 04:46:28 2017	(r324655)
+++ head/lib/libifconfig/libifconfig.h	Mon Oct 16 06:54:26 2017	(r324656)
@@ -82,6 +82,8 @@ int ifconfig_set_description(ifconfig_handle_t *h, con
 int ifconfig_unset_description(ifconfig_handle_t *h, const char *name);
 int ifconfig_set_name(ifconfig_handle_t *h, const char *name,
     const char *newname);
+int ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname,
+    char **orig_name);
 int ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu);
 int ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu);
 int ifconfig_set_metric(ifconfig_handle_t *h, const char *name,