386BSD 0.1 development
[unix-history] / usr / src / sbin / ifconfig / ifconfig.c
index ffa8c1b..b14fb46 100644 (file)
@@ -2,17 +2,33 @@
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS 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.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -22,16 +38,18 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ifconfig.c 4.26 (Berkeley) %G%";
+static char sccsid[] = "@(#)ifconfig.c 5.1 (Berkeley) 2/28/91";
 #endif /* not lint */
 
 #endif /* not lint */
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 
 #include <net/if.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 
 #include <net/if.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 
 
+#ifdef notdef
 #define        NSIP
 #include <netns/ns.h>
 #include <netns/ns_if.h>
 #define        NSIP
 #include <netns/ns.h>
 #include <netns/ns_if.h>
@@ -39,23 +57,29 @@ static char sccsid[] = "@(#)ifconfig.c      4.26 (Berkeley) %G%";
 #define EON
 #include <netiso/iso.h>
 #include <netiso/iso_var.h>
 #define EON
 #include <netiso/iso.h>
 #include <netiso/iso_var.h>
+#endif
+#include <netdb.h>
 #include <sys/protosw.h>
 
 #include <sys/protosw.h>
 
+#include <unistd.h>
 #include <stdio.h>
 #include <errno.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <errno.h>
 #include <ctype.h>
-#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
 
 
-extern int errno;
 struct ifreq           ifr, ridreq;
 struct ifaliasreq      addreq;
 struct ifreq           ifr, ridreq;
 struct ifaliasreq      addreq;
+#ifdef EON
 struct iso_ifreq       iso_ridreq;
 struct iso_aliasreq    iso_addreq;
 struct iso_ifreq       iso_ridreq;
 struct iso_aliasreq    iso_addreq;
+#endif
 struct sockaddr_in     netmask;
 
 char   name[30];
 int    flags;
 int    metric;
 struct sockaddr_in     netmask;
 
 char   name[30];
 int    flags;
 int    metric;
+int    nsellength = 1;
 int    setaddr;
 int    setipdst;
 int    doalias;
 int    setaddr;
 int    setipdst;
 int    doalias;
@@ -66,7 +90,7 @@ extern        int errno;
 
 int    setifflags(), setifaddr(), setifdstaddr(), setifnetmask();
 int    setifmetric(), setifbroadaddr(), setifipdst();
 
 int    setifflags(), setifaddr(), setifdstaddr(), setifnetmask();
 int    setifmetric(), setifbroadaddr(), setifipdst();
-int    notealias(), setsnpaoffset();
+int    notealias(), setsnpaoffset(), setnsellength();
 
 #define        NEXTARG         0xffffff
 
 
 #define        NEXTARG         0xffffff
 
@@ -93,9 +117,10 @@ struct      cmd {
 #endif
        { "netmask",    NEXTARG,        setifnetmask },
        { "metric",     NEXTARG,        setifmetric },
 #endif
        { "netmask",    NEXTARG,        setifnetmask },
        { "metric",     NEXTARG,        setifmetric },
-       { "snpaoffset", NEXTARG,        setsnpaoffset },
        { "broadcast",  NEXTARG,        setifbroadaddr },
        { "ipdst",      NEXTARG,        setifipdst },
        { "broadcast",  NEXTARG,        setifbroadaddr },
        { "ipdst",      NEXTARG,        setifipdst },
+       { "snpaoffset", NEXTARG,        setsnpaoffset },
+       { "nsellength", NEXTARG,        setnsellength },
        { 0,            0,              setifaddr },
        { 0,            0,              setifdstaddr },
 };
        { 0,            0,              setifaddr },
        { 0,            0,              setifdstaddr },
 };
@@ -106,8 +131,12 @@ struct     cmd {
  * principally by James O'Toole and Chris Torek.
  */
 int    in_status(), in_getaddr();
  * principally by James O'Toole and Chris Torek.
  */
 int    in_status(), in_getaddr();
+#ifdef NSIP
 int    xns_status(), xns_getaddr();
 int    xns_status(), xns_getaddr();
+#endif
+#ifdef EON
 int    iso_status(), iso_getaddr();
 int    iso_status(), iso_getaddr();
+#endif
 
 /* Known address families */
 struct afswtch {
 
 /* Known address families */
 struct afswtch {
@@ -123,26 +152,19 @@ struct afswtch {
 #define C(x) ((caddr_t) &x)
        { "inet", AF_INET, in_status, in_getaddr,
             SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
 #define C(x) ((caddr_t) &x)
        { "inet", AF_INET, in_status, in_getaddr,
             SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
+#ifdef NSIP
        { "ns", AF_NS, xns_status, xns_getaddr,
             SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
        { "ns", AF_NS, xns_status, xns_getaddr,
             SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
+#endif
+#ifdef EON
        { "iso", AF_ISO, iso_status, iso_getaddr,
             SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, C(iso_ridreq), C(iso_addreq) },
        { "iso", AF_ISO, iso_status, iso_getaddr,
             SIOCDIFADDR_ISO, SIOCAIFADDR_ISO, C(iso_ridreq), C(iso_addreq) },
+#endif
        { 0,    0,          0,          0 }
 };
 
 struct afswtch *afp;   /*the address family being set or asked about*/
 
        { 0,    0,          0,          0 }
 };
 
 struct afswtch *afp;   /*the address family being set or asked about*/
 
-int testing = 0;
-Ioctl(a,b,c) {
-       int error = 0;
-       if (testing)
-               printf("would call ioctl with %x, %x, %x\n", a, b, c);
-       else
-               error = ioctl(a, b, c);
-       return error;
-}
-#define ioctl(a, b, c) Ioctl(a,b,c)
-
 main(argc, argv)
        int argc;
        char *argv[];
 main(argc, argv)
        int argc;
        char *argv[];
@@ -208,6 +230,9 @@ main(argc, argv)
                }
                argc--, argv++;
        }
                }
                argc--, argv++;
        }
+       if (af == AF_ISO)
+               adjust_nsellength();
+#ifdef NSIP
        if (setipdst && af==AF_NS) {
                struct nsip_req rq;
                int size = sizeof(rq);
        if (setipdst && af==AF_NS) {
                struct nsip_req rq;
                int size = sizeof(rq);
@@ -218,6 +243,7 @@ main(argc, argv)
                if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
                        Perror("Encapsulation Routing");
        }
                if (setsockopt(s, 0, SO_NSIP_ROUTE, &rq, size) < 0)
                        Perror("Encapsulation Routing");
        }
+#endif
        if (clearaddr) {
                int ret;
                strncpy(rafp->af_ridreq, name, sizeof ifr.ifr_name);
        if (clearaddr) {
                int ret;
                strncpy(rafp->af_ridreq, name, sizeof ifr.ifr_name);
@@ -334,7 +360,9 @@ setifmetric(val)
 setsnpaoffset(val)
        char *val;
 {
 setsnpaoffset(val)
        char *val;
 {
+#ifdef EON
        iso_addreq.ifra_snpaoffset = atoi(val);
        iso_addreq.ifra_snpaoffset = atoi(val);
+#endif
 }
 
 #define        IFFBITS \
 }
 
 #define        IFFBITS \
@@ -415,6 +443,7 @@ in_status(force)
        putchar('\n');
 }
 
        putchar('\n');
 }
 
+#ifdef NSIP
 
 xns_status(force)
        int force;
 
 xns_status(force)
        int force;
@@ -454,6 +483,8 @@ xns_status(force)
        putchar('\n');
 }
 
        putchar('\n');
 }
 
+#endif
+#ifdef EON
 iso_status(force)
        int force;
 {
 iso_status(force)
        int force;
 {
@@ -468,36 +499,41 @@ iso_status(force)
                perror("ifconfig: socket");
                exit(1);
        }
                perror("ifconfig: socket");
                exit(1);
        }
-       if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
+       bzero((caddr_t)&ifr, sizeof(ifr));
+       strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+       if (ioctl(s, SIOCGIFADDR_ISO, (caddr_t)&ifr) < 0) {
                if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
                        if (!force)
                                return;
                        bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
                if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
                        if (!force)
                                return;
                        bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
-               } else
-                       perror("ioctl (SIOCGIFADDR)");
+               } else {
+                       perror("ioctl (SIOCGIFADDR_ISO)");
+                       exit(1);
+               }
        }
        strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
        siso = &ifr.ifr_Addr;
        }
        strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
        siso = &ifr.ifr_Addr;
-       printf("\tiso %s ", iso_ntoa(siso->siso_addr));
-       if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
+       printf("\tiso %s ", iso_ntoa(&siso->siso_addr));
+       if (ioctl(s, SIOCGIFNETMASK_ISO, (caddr_t)&ifr) < 0) {
                if (errno != EADDRNOTAVAIL)
                if (errno != EADDRNOTAVAIL)
-                       perror("ioctl (SIOCGIFNETMASK)");
+                       perror("ioctl (SIOCGIFNETMASK_ISO)");
        } else {
        } else {
-               printf("\n netmask %s ", iso_ntoa(siso->siso_addr));
+               printf(" netmask %s ", iso_ntoa(&siso->siso_addr));
        }
        if (flags & IFF_POINTOPOINT) {
        }
        if (flags & IFF_POINTOPOINT) {
-               if (ioctl(s, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
+               if (ioctl(s, SIOCGIFDSTADDR_ISO, (caddr_t)&ifr) < 0) {
                        if (errno == EADDRNOTAVAIL)
                            bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
                        else
                        if (errno == EADDRNOTAVAIL)
                            bzero((char *)&ifr.ifr_Addr, sizeof(ifr.ifr_Addr));
                        else
-                           Perror("ioctl (SIOCGIFDSTADDR)");
+                           Perror("ioctl (SIOCGIFDSTADDR_ISO)");
                }
                strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
                siso = &ifr.ifr_Addr;
                }
                strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
                siso = &ifr.ifr_Addr;
-               printf("--> %s ", iso_ntoa(siso->siso_addr));
+               printf("--> %s ", iso_ntoa(&siso->siso_addr));
        }
        putchar('\n');
 }
        }
        putchar('\n');
 }
+#endif
 
 Perror(cmd)
        char *cmd;
 
 Perror(cmd)
        char *cmd;
@@ -584,6 +620,7 @@ printb(s, v, bits)
                putchar('>');
        }
 }
                putchar('>');
        }
 }
+#ifdef NSIP
 
 #define SNS(x) ((struct sockaddr_ns *) &(x))
 struct sockaddr_ns *snstab[] = {
 
 #define SNS(x) ((struct sockaddr_ns *) &(x))
 struct sockaddr_ns *snstab[] = {
@@ -603,6 +640,8 @@ char *addr;
                printf("Attempt to set XNS netmask will be ineffectual\n");
 }
 
                printf("Attempt to set XNS netmask will be ineffectual\n");
 }
 
+#endif
+#ifdef EON
 #define SISO(x) ((struct sockaddr_iso *) &(x))
 struct sockaddr_iso *sisotab[] = {
 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
 #define SISO(x) ((struct sockaddr_iso *) &(x))
 struct sockaddr_iso *sisotab[] = {
 SISO(iso_ridreq.ifr_Addr), SISO(iso_addreq.ifra_addr),
@@ -611,14 +650,49 @@ SISO(iso_addreq.ifra_mask), SISO(iso_addreq.ifra_dstaddr)};
 iso_getaddr(addr, which)
 char *addr;
 {
 iso_getaddr(addr, which)
 char *addr;
 {
-       struct sockaddr_iso *siso = sisotab[which];
+       register struct sockaddr_iso *siso = sisotab[which];
        struct iso_addr *iso_addr();
        siso->siso_addr = *iso_addr(addr);
        struct iso_addr *iso_addr();
        siso->siso_addr = *iso_addr(addr);
+
        if (which == MASK) {
                siso->siso_len = TSEL(siso) - (caddr_t)(siso);
                siso->siso_nlen = 0;
        } else {
        if (which == MASK) {
                siso->siso_len = TSEL(siso) - (caddr_t)(siso);
                siso->siso_nlen = 0;
        } else {
-           siso->siso_family = AF_ISO;
-           siso->siso_len =  sizeof(*siso);
+               siso->siso_len = sizeof(*siso);
+               siso->siso_family = AF_ISO;
+       }
+}
+#endif
+
+setnsellength(val)
+       char *val;
+{
+       nsellength = atoi(val);
+       if (nsellength < 0) {
+               fprintf(stderr, "Negative NSEL length is absurd\n");
+               exit (1);
        }
        }
+       if (afp == 0 || afp->af_af != AF_ISO) {
+               fprintf(stderr, "Setting NSEL length valid only for iso\n");
+               exit (1);
+       }
+}
+
+#ifdef notdef
+fixnsel(s)
+register struct sockaddr_iso *s;
+{
+       if (s->siso_family == 0)
+               return;
+       s->siso_tlen = nsellength;
+}
+#endif
+
+adjust_nsellength()
+{
+#ifdef notdef
+       fixnsel(sisotab[RIDADDR]);
+       fixnsel(sisotab[ADDR]);
+       fixnsel(sisotab[DSTADDR]);
+#endif
 }
 }