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

new options for ifconfig



Hello!

I wrote the following patch for ifconfig. It adds the three new options
-ip, -bc and -nm to print out _only_ an interface's IP address,
broadcast address or netmask. This makes it _much_ easier for shell
scripts etc. to find out these values. I don't know if this is cleanly
integrated into ifconfig, but I think you can recognize the intention.

Regards,
Andreas Krennmair

P.S.: here's the patch:

diff -Nur ifconfig.orig/ifconfig.8 ifconfig/ifconfig.8
--- ifconfig.orig/ifconfig.8	Fri Jul 19 00:26:05 2002
+++ ifconfig/ifconfig.8	Sun Jul 28 00:34:00 2002
@@ -75,6 +75,9 @@
 .Nm ifconfig
 .Op Fl A | Am
 .Op Ar address_family
+.Nm ifconfig
+.Op Fl ip | bc | nm
+.Ar interface
 .Sh DESCRIPTION
 The
 .Nm
@@ -180,6 +183,9 @@
 .It Fl anycast
 (inet6 only)
 Clear the IPv6 anycast address bit.
+.It Fl bc
+(inet only)
+Print the interface's broadcast address if one is configured.
 .It Cm broadcast Ar addr
 (inet only)
 Specify the address to use to represent broadcasts to the
@@ -219,6 +225,9 @@
 .It Cm deletetunnel
 Removes the source and dsetination tunnel addresses,
 configured onto a tunnel interface.
+.It Fl ip
+(inet only)
+Print the interface's IP address if one is configured.
 .It Cm ipdst
 This is used to specify an Internet host who is willing to receive
 ip packets encapsulating NS packets bound for a remote network.
@@ -340,6 +349,9 @@
 Higher metrics have the effect of making a route
 less favorable; metrics are counted as addition hops
 to the destination network or host.
+.It Fl nm
+(inet only)
+Print the interface's netmask if one is configured.
 .It Cm netmask Ar mask
 (inet, inet6 and iso)
 Specify how much of the address to reserve for subdividing
diff -Nur ifconfig.orig/ifconfig.c ifconfig/ifconfig.c
--- ifconfig.orig/ifconfig.c	Fri Jul 19 00:26:05 2002
+++ ifconfig/ifconfig.c	Sun Jul 28 00:35:52 2002
@@ -155,7 +155,7 @@
 int	newaddr = 0;
 int	nsellength = 1;
 int	af = AF_INET;
-int	dflag, mflag, lflag, uflag;
+int	dflag, mflag, lflag, uflag, ipflag, bcflag, nmflag;
 int	reset_if_flags;
 int	explicit_prefix = 0;
 #ifdef INET6
@@ -416,6 +416,27 @@
 		if (argc < 1)
 			usage();
 		(void) strlcpy(name, *argv, sizeof(name));
+        }
+        else if (!strcmp(*argv,"-ip")) {
+                ipflag = 1;
+                argc--, argv++;
+                if (argc < 1 || bcflag || nmflag)
+                        usage();
+                (void) strlcpy(name, *argv, sizeof(name));
+        }
+        else if (!strcmp(*argv,"-bc")) {
+                bcflag = 1;
+                argc--, argv++;
+                if (argc < 1 || ipflag || nmflag)
+                        usage();
+                (void) strlcpy(name, *argv, sizeof(name));
+        }
+        else if (!strcmp(*argv,"-nm")) {
+                nmflag = 1;
+                argc--, argv++;
+                if (argc < 1 || ipflag || bcflag)
+                        usage();
+                (void) strlcpy(name, *argv, sizeof(name));
 	} else
 		(void) strlcpy(name, *argv, sizeof(name));
 	argc--, argv++;
@@ -634,7 +655,8 @@
 			namep = ifa->ifa_name;
 			if (getinfo(ifrp) < 0)
 				continue;
-			status(1, (struct sockaddr_dl *)ifa->ifa_addr);
+                        if (!ipflag && !bcflag && !nmflag)
+			        status(1, (struct sockaddr_dl *)ifa->ifa_addr);
 			count++;
 			noinet = 1;
 			continue;
@@ -1820,6 +1842,50 @@
 	 */
 	memcpy(&sin2, &ifr.ifr_addr, sizeof(sin2));
 
+        if (ipflag || bcflag || nmflag) {
+        /*        struct sockaddr_in * sin;
+                char * inet_ntoa();
+
+                getsock(AF_INET);
+                if (s < 0) {
+                        if (errno == EPROTONOSUPPORT)
+                                return;
+                        err(1, "socket");
+                }
+               
+                (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+                sin = (struct sockaddr_in *)&ifr.ifr_addr;*/
+
+                if (ipflag) {
+                        printf("%s\n",inet_ntoa(sin->sin_addr));
+                } 
+                else if (bcflag) {
+                        if (flags & IFF_BROADCAST) {
+                                /* memcpy(&ifr.ifr_addr,&sin2,sizeof(sin2)); */
+                                if (ioctl(s, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
+                                        if (errno == EADDRNOTAVAIL)
+                                            memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
+                                        else
+                                            warn("SIOCGIFBRDADDR");
+                                } 
+                                (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+                                sin = (struct sockaddr_in *)&ifr.ifr_addr;
+                                if (sin->sin_addr.s_addr != 0)
+                                        printf("%s\n",inet_ntoa(sin->sin_addr));
+                        }         
+                }
+                else if (nmflag) {
+                        if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
+                                if (errno != EADDRNOTAVAIL)
+                                        warn("SIOCGIFNETMASK");
+                                memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
+                        } else
+                                netmask.sin_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
+                        printf("%s\n",inet_ntoa(netmask.sin_addr));
+                }
+                exit(0);
+        }
+
 	printf("\tinet %s ", inet_ntoa(sin->sin_addr));
 	(void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
 	if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
@@ -2589,7 +2655,7 @@
 void
 usage()
 {
-	fprintf(stderr, "usage: ifconfig [ -m ] [ -a ] [ -A ] [ interface ]\n"
+	fprintf(stderr, "usage: ifconfig [ -m ] [ -ip | -nm | -bc ] [ -a ] [ -A ] [ interface ]\n"
 		"\t[ [af] [ address [ dest_addr ] ] [ up ] [ down ] "
 		"[ netmask mask ] ]\n"
 		"\t[ media media_type ] [ mediaopt media_option ]\n"