Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/ifconfig Add a new -s option to make it easier to test ...



details:   https://anonhg.NetBSD.org/src/rev/d4d10f1ef282
branches:  trunk
changeset: 474326:d4d10f1ef282
user:      sommerfeld <sommerfeld%NetBSD.org@localhost>
date:      Sat Jul 03 17:31:15 1999 +0000

description:
Add a new -s option to make it easier to test the link-level status
(i.e., 10baseT carrier/no carrier) of an interface from scripts

ifconfig -s <interface> will exit with a false status if the interface
reports its unconnected.

-s also works in conjunction with -l and -a, filtering out interfaces
which are reporting down.

Also, add -b (which shows only broadcast interfaces with -l and -a).

I find these options useful in network autoconfig scripts for mobile
systems.

diffstat:

 sbin/ifconfig/ifconfig.8 |  41 +++++++++++++++++++++++++-----
 sbin/ifconfig/ifconfig.c |  65 +++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 93 insertions(+), 13 deletions(-)

diffs (250 lines):

diff -r 8a98578347a4 -r d4d10f1ef282 sbin/ifconfig/ifconfig.8
--- a/sbin/ifconfig/ifconfig.8  Sat Jul 03 15:09:51 1999 +0000
+++ b/sbin/ifconfig/ifconfig.8  Sat Jul 03 17:31:15 1999 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: ifconfig.8,v 1.26 1999/07/02 15:45:46 itojun Exp $
+.\"    $NetBSD: ifconfig.8,v 1.27 1999/07/03 17:31:15 sommerfeld Exp $
 .\"
 .\" Copyright (c) 1983, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -56,13 +56,20 @@
 .Fl a
 .Op Fl A
 .Op Fl mL
+.Op Fl b
 .Op Fl d
 .Op Fl u
+.Op Fl s
 .Op Ar protocol_family
 .Nm ""
 .Fl l
+.Op Fl b
 .Op Fl d
 .Op Fl u
+.Op Fl s
+.Nm ""
+.Fl s
+.Ar interface
 .Sh DESCRIPTION
 .Nm
 is used to assign an address
@@ -334,7 +341,20 @@
 displays the current configuration for a network interface
 when no optional parameters are supplied.
 If a protocol family is specified,
-Ifconfig will report only the details specific to that protocol family.
+Ifconfig will report only the details specific to that protocol
+family.
+.Pp
+If the
+.Fl s
+flag is passed before an interface name,
+.Nm
+will attempt to query the interface for its media status.  If the
+interface supports reporting media status, and it reports that it does
+not appear to be connected to a network,
+.Nm 
+will exit with status of 1 (false); otherwise, it will exit with a
+zero (true) exit status.  Not all interface drivers support media
+status reporting.
 .Pp
 If the
 .Fl m
@@ -352,9 +372,13 @@
 .Nm
 to display information about all interfaces in the system.
 .Fl d
-limits this to interfaces that are down, and
+limits this to interfaces that are down,
 .Fl u
-limits this to interfaces that are up.
+limits this to interfaces that are up,
+.Fl b
+limits this to broadcast interfaces, and
+.Fl s
+omits interfaces which appear not to be connected to a network.
 .Pp
 If the
 .Fl A
@@ -367,10 +391,13 @@
 no other additional information.  Use of this flag is mutually exclusive
 with all other flags and commands, except for
 .Fl d
-(only list interfaces that are down)
-and
+(only list interfaces that are down),
 .Fl u
-(only list interfaces that are up).
+(only list interfaces that are up),
+.Fl s
+(only list interfaces that may be connected),
+.Fl b
+(only list broadcast interfaces).
 .Pp
 Only the super-user may modify the configuration of a network interface.
 .Sh DIAGNOSTICS
diff -r 8a98578347a4 -r d4d10f1ef282 sbin/ifconfig/ifconfig.c
--- a/sbin/ifconfig/ifconfig.c  Sat Jul 03 15:09:51 1999 +0000
+++ b/sbin/ifconfig/ifconfig.c  Sat Jul 03 17:31:15 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ifconfig.c,v 1.53 1999/07/01 13:19:20 itojun Exp $     */
+/*     $NetBSD: ifconfig.c,v 1.54 1999/07/03 17:31:15 sommerfeld Exp $ */
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -80,7 +80,7 @@
 #if 0
 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
 #else
-__RCSID("$NetBSD: ifconfig.c,v 1.53 1999/07/01 13:19:20 itojun Exp $");
+__RCSID("$NetBSD: ifconfig.c,v 1.54 1999/07/03 17:31:15 sommerfeld Exp $");
 #endif
 #endif /* not lint */
 
@@ -93,7 +93,9 @@
 #include <net/if_media.h>
 #include <netinet/in.h>
 #include <netinet/in_var.h>
+#ifdef INET6
 #include <netinet6/nd6.h>
+#endif
 #include <arpa/inet.h>
 
 #include <netatalk/at.h>
@@ -135,13 +137,13 @@
 int    newaddr = -1;
 int    nsellength = 1;
 int    af;
-int    Aflag, aflag, dflag, mflag, lflag, uflag;
+int    Aflag, aflag, bflag, dflag, lflag, mflag, sflag, uflag;
 #ifdef INET6
 int    Lflag;
 #endif
 int    reset_if_flags;
 int    explicit_prefix = 0;
-#ifndef INET_ONLY
+#if !defined(INET_ONLY) && defined(INET6)
 char ntop_buf[INET6_ADDRSTRLEN];       /*inet_ntop()*/
 #endif /* INET_ONLY */
 
@@ -254,6 +256,7 @@
 
 void   adjust_nsellength __P((void));
 int    getinfo __P((struct ifreq *));
+int    carrier __P((void));
 void   getsock __P((int));
 void   printall __P((void));
 void   printalias __P((const char *, int));
@@ -336,7 +339,7 @@
 
        /* Parse command-line options */
        aflag = mflag = 0;
-       while ((ch = getopt(argc, argv, "Aadlmu"
+       while ((ch = getopt(argc, argv, "Aabdlmsu"
 #ifdef INET6
                                        "L"
 #endif
@@ -350,6 +353,10 @@
                        aflag = 1;
                        break;
 
+               case 'b':
+                       bflag = 1;
+                       break;
+                       
                case 'd':
                        dflag = 1;
                        break;
@@ -368,10 +375,15 @@
                        mflag = 1;
                        break;
 
+               case 's':
+                       sflag = 1;
+                       break;
+
                case 'u':
                        uflag = 1;
                        break;
 
+                       
                default:
                        usage();
                        /* NOTREACHED */
@@ -386,8 +398,12 @@
         *
         * -a means "print status of all interfaces".
         */
-       if (lflag && (aflag || mflag || argc))
+       if (lflag && (aflag || mflag || Aflag || argc))
                usage();
+#ifdef INET6
+       if (lflag && Lflag)
+               usage();
+#endif
        if (aflag || lflag) {
                if (argc > 1)
                        usage();
@@ -431,6 +447,13 @@
        if (getinfo(&ifr) < 0)
                exit(1);
 
+       if (sflag) {
+               if (argc != 0)
+                       usage();
+               else
+                       exit(carrier());
+       }
+
        /* No more arguments means interface status. */
        if (argc == 0) {
                status(NULL, 0);
@@ -653,11 +676,15 @@
 
                if (getinfo(&ifreq) < 0)
                        continue;
+               if (bflag && (flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))
+                       continue;
                if (dflag && (flags & IFF_UP) != 0)
                        continue;
                if (uflag && (flags & IFF_UP) == 0)
                        continue;
 
+               if (sflag && carrier())
+                       continue;
                idx++;
                /*
                 * Are we just listing the interfaces?
@@ -1154,6 +1181,32 @@
                printf(" instance %d", IFM_INST(ifmw));
 }
 
+int carrier()
+{
+       struct ifmediareq ifmr;
+
+       (void) memset(&ifmr, 0, sizeof(ifmr));
+       (void) strncpy(ifmr.ifm_name, name, sizeof(ifmr.ifm_name));
+
+       if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
+               /*
+                * Interface doesn't support SIOC{G,S}IFMEDIA;
+                * assume ok.
+                */
+               return 0;
+       }
+       if ((ifmr.ifm_status & IFM_AVALID) == 0) {
+               /*
+                * Interface doesn't report media-valid status.
+                * assume ok.
+                */
+               return 0;
+       }
+       /* otherwise, return ok for active, not-ok if not active. */
+       return !(ifmr.ifm_status & IFM_ACTIVE);
+}
+
+
 #define        IFFBITS \
 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6NOTRAILERS\7RUNNING\10NOARP\
 \11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2\20MULTICAST"



Home | Main Index | Thread Index | Old Index