From 6922b5fd00efbddf90ddcc5b05dc0161bdbbf6a0 Mon Sep 17 00:00:00 2001
From: Mats Erik Andersson <gnu@gisladisker.se>
Date: Mon, 20 Jul 2015 23:28:19 +0200
Subject: [PATCH 03/60] ifconfig: More info on tunnel interfaces.

All formats display point-to-point destination.
Display tunnel endpoints on BSD systems.
---
 ChangeLog             | 22 ++++++++++++++++++++++
 ifconfig/options.c    |  7 +++++++
 ifconfig/system/bsd.c | 37 +++++++++++++++++++++++++++++++++++--
 ifconfig/system/bsd.h |  8 +++++++-
 4 files changed, 71 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 18a584ba..d0172fa8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2015-07-20  Mats Erik Andersson  <gnu@gisladisker.se>
+
+	ifconfig: More info on tunnel interfaces.
+
+	* ifconfig/options.c (formats) <net-tools format>: Add
+	destination address for point-to-point interfaces.  It is
+	present in some other formats.
+	<unix format>: Display tunnel addresses for p-t-p interfaces.
+	<osf format>: Add destination address for p-t-p interfaces
+	and hardware type and hardware address for any device.
+
+	* ifconfig/system/bsd.c (system_fh_brdaddr_query): Check
+	that IFF_BROADCAST is set and access the relevant member
+	`fp->ifa_broadaddr'.
+	(system_fh_brdaddr): Check for set IFF_BROADCAST.
+	(system_fh_tunnel_query, system_fh_tundst, system_fh_tunsrc):
+	New functions.
+	* ifconfig/system/bsd.h (SYSTEM_FORMAT_HANDLER): Add the
+	keys `tunnel?', `tundst', and `tunsrc'.
+	(system_fh_tunnel_query, system_fh_tundst, system_fh_tunsrc):
+	New prototypes.
+
 2015-07-18  Mats Erik Andersson  <gnu@gisladisker.se>
 
 	ifconfig: Improve the support for GNU/Hurd.
diff --git a/ifconfig/options.c b/ifconfig/options.c
index 3356f0e5..2f20e75f 100644
--- a/ifconfig/options.c
+++ b/ifconfig/options.c
@@ -102,6 +102,7 @@ struct format formats[] = {
    "${name}${exists?}{hwtype?}{${hwtype?}{${tab}{10}Link encap:${hwtype}}"
    "${hwaddr?}{  HWaddr ${hwaddr}}}${\\n}"
    "${addr?}{${tab}{10}inet addr:${addr}"
+   "${dstaddr?}{  P-t-P:${dstaddr}}"
    "${brdaddr?}{  Bcast:${brdaddr}}"
    "${netmask?}{  Mask:${netmask}}"
    "${newline}}"
@@ -164,6 +165,8 @@ struct format formats[] = {
    "${exists?}{hwtype?}{"
      "${hwtype?}{${\\t}${hwtype}${exists?}{hwaddr?}{"
        "${hwaddr?}{ ${hwaddr}}}${\\n}}}"
+   "${exists?}{tunnel?}{"
+     "${tunnel?}{${\\t}tunnel inet ${tunsrc} --> ${tundst}${\\n}}}"
    "${addr?}{${\\t}inet ${addr}"
    "${dstaddr?}{ --> ${dstaddr}}"
    " netmask ${netmask}{0}{%#02x}${netmask}{1}{%02x}"
@@ -180,9 +183,13 @@ struct format formats[] = {
    "${ifdisplay?}{"
    "${name}: flags=${flags}{number}{%x}<${flags}{string}{,}>${\\n}"
    "${addr?}{${\\t}inet ${addr}"
+   "${dstaddr?}{ --> ${dstaddr}}"
    " netmask ${netmask}{0}{%02x}${netmask}{1}{%02x}"
    "${netmask}{2}{%02x}${netmask}{3}{%02x}"
    "${brdaddr?}{ broadcast ${brdaddr}}" "${mtu?}{ ipmtu ${mtu}}${\\n}}"
+   "${exists?}{hwtype?}{"
+     "${hwtype?}{${\\t}${hwtype}${exists?}{hwaddr?}{"
+       "${hwaddr?}{ ${hwaddr}}}${\\n}}}"
    "}"
   },
   {"check",
diff --git a/ifconfig/system/bsd.c b/ifconfig/system/bsd.c
index 47461242..910a75ef 100644
--- a/ifconfig/system/bsd.c
+++ b/ifconfig/system/bsd.c
@@ -217,7 +217,7 @@ system_fh_brdaddr_query (format_data_t form, int argc, char *argv[])
 	      strcmp (fp->ifa_name, form->ifr->ifr_name))
 	    continue;
 
-	  if (fp->ifa_netmask)
+	  if ((fp->ifa_flags & IFF_BROADCAST) && fp->ifa_broadaddr)
 	    missing = 0;
 	  break;
 	}
@@ -242,7 +242,7 @@ system_fh_brdaddr (format_data_t form, int argc, char *argv[])
 	      strcmp (fp->ifa_name, form->ifr->ifr_name))
 	    continue;
 
-	  if (fp->ifa_broadaddr)
+	  if ((fp->ifa_flags & IFF_BROADCAST) && fp->ifa_broadaddr)
 	    {
 	      missing = 0;
 	      put_addr (form, argc, argv, fp->ifa_broadaddr);
@@ -588,3 +588,36 @@ system_fh_status (format_data_t form, int argc, char *argv[])
 #endif /* SIOCGIFMEDIA */
     put_string (form, "(not known)");
 }
+
+void
+system_fh_tunnel_query (format_data_t form, int argc, char *argv[])
+{
+#if defined SIOCGIFPSRCADDR && defined SIOCGIFPDSTADDR
+  if (ioctl (form->sfd, SIOCGIFPSRCADDR, form->ifr) >= 0)
+    select_arg (form, argc, argv, 0);
+  else
+#endif /* SIOCGIFPSRCADDR && SIOCGIFPDSTADDR */
+  select_arg (form, argc, argv, 1);
+}
+
+void
+system_fh_tundst (format_data_t form, int argc, char *argv[])
+{
+#ifdef SIOCGIFPDSTADDR
+  if (ioctl (form->sfd, SIOCGIFPDSTADDR, form->ifr) >= 0)
+    put_addr (form, argc, argv, &form->ifr->ifr_addr);
+  else
+    put_string (form, "(no phydst)");
+#endif /* SIOCGIFPDSTADDR */
+}
+
+void
+system_fh_tunsrc (format_data_t form, int argc, char *argv[])
+{
+#ifdef SIOCGIFPSRCADDR
+  if (ioctl (form->sfd, SIOCGIFPSRCADDR, form->ifr) >= 0)
+    put_addr (form, argc, argv, &form->ifr->ifr_addr);
+  else
+    put_string (form, "(no physrc)");
+#endif /* SIOCGIFPSRCADDR */
+}
diff --git a/ifconfig/system/bsd.h b/ifconfig/system/bsd.h
index 0b802a60..918c1f2c 100644
--- a/ifconfig/system/bsd.h
+++ b/ifconfig/system/bsd.h
@@ -71,7 +71,10 @@ struct system_ifconfig
   {"netmask?", system_fh_netmask_query}, \
   {"netmask", system_fh_netmask},	\
   {"status?", system_fh_status_query}, \
-  {"status", system_fh_status},
+  {"status", system_fh_status}, \
+  {"tunnel?", system_fh_tunnel_query}, \
+  {"tundst", system_fh_tundst}, \
+  {"tunsrc", system_fh_tunsrc},
 
 void system_fh_brdaddr_query (format_data_t form, int argc, char *argv[]);
 void system_fh_brdaddr (format_data_t form, int argc, char *argv[]);
@@ -85,5 +88,8 @@ void system_fh_netmask_query (format_data_t form, int argc, char *argv[]);
 void system_fh_netmask (format_data_t form, int argc, char *argv[]);
 void system_fh_status_query (format_data_t form, int argc, char *argv[]);
 void system_fh_status (format_data_t form, int argc, char *argv[]);
+void system_fh_tunnel_query (format_data_t form, int argc, char *argv[]);
+void system_fh_tundst (format_data_t form, int argc, char *argv[]);
+void system_fh_tunsrc (format_data_t form, int argc, char *argv[]);
 
 #endif /* IFCONFIG_SYSTEM_BSD_H */
-- 
2.26.0.292.g33ef6b2f38

