Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: riepel@Snowflake.Stanford.edu (Rob Riepel)
Subject: Re: Real ethernet address?
Date: 02 Feb 1994 18:00:33 GMT	[thread overview]
Message-ID: <RIEPEL.94Feb2100033@Snowflake.Stanford.edu> (raw)
In-Reply-To: jim@accelr8.com's message of Wed, 2 Feb 1994 16:35:13 GMT


Here's something I picked up long ago.  I don't have an Alpha (yet), and
even if I did, I probably wouldn't be running DECnet on it, so I have no
idea if it works in this brave new world.  If it does, please let us all
know....

/*
 *      getadr.c -- program to acquire ethernet controller addresses
 *              (c) Digital Equipment Corporation, 1989
 *              Author: Jim Lieb, Digital Equipment Corp. suntan::lieb
 *              This program is distributed free as a demonstration
 *              of network access methods.  No warrantee is expressed or
 *              implied.
 *
 *      Application: Use the getadr function to get the hardware address
 *              for the system in applications where the license key is
 *              verified by checking something unique like a system serial
 *              number (not always present on DEC cpus).
 *
 *      NOTE:   There is a difference between the hardware address and the
 *              physical address of Digital manufactured Ethernet controller.
 *              The hardware address is a unique number, typically of the
 *              form:  08-00-2B-xx-xx-xx, as assigned by IEEE authority.  The
 *              xx-xx-xx number is fixed in ROM and is unique for each
 *              controller.  This address is guaranteed fixed and unique.
 *              The physical address is programmable and is MODIFIED by the
 *              DECnet routing layer to encode the 16 bit Phase IV DECnet
 *              address in the least significant 2 octets (byte swapped)
 *              of the source address of packets. This address, typically
 *              of the form aa-00-04-00-yy-yy where yy-yy is the DECnet
 *              address, becomes the address used by the controller for
 *              packet receive and transmit.
 *
 *              March 3, 1990 - Jim Lieb, changed to use internal tables
 *                              and clean up struct usage.  Please note!
 *              May 21, 1990 - John Dustin OSG, use status bits rather than
 *                              string compares to get right interface.
 *                              also test for interface turned on and use
 *                              best guess of none on...
 */

#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <errno.h>                      /* error numbers */
#include <stdio.h>

static char getadrSID[] = "@(#)getadr.c 1.5 6/22/90 demo";
extern int errno;

int getadr();

/* main -- Shell to demonstrate the call to the getadr function.  This
 *      also prints the address contents to stdout.
 */

main(argc, argv, envp)
int argc;
char **argv, **envp;
{
        struct ifdevea ifr;
        char *interface;

        if(argc < 2)
                interface = NULL;
        else
                interface = argv[1];
        if(getadr(interface, &ifr) < 0) {
                fprintf(stderr, "couldn't get network interface\n");
                exit(1);
        }
        printf("ifr_name = '%s'\n", ifr.ifr_name);
        printf(
           "Ethernet hardware address = (%02x-%02x-%02x-%02x-%02x-%02x)\n",
                ifr.default_pa[0], ifr.default_pa[1], ifr.default_pa[2],
                ifr.default_pa[3], ifr.default_pa[4], ifr.default_pa[5]);
        printf(
            "Ethernet physical address = (%02x-%02x-%02x-%02x-%02x-%02x)\n",
                ifr.current_pa[0], ifr.current_pa[1], ifr.current_pa[2],
                ifr.current_pa[3], ifr.current_pa[4], ifr.current_pa[5]);
    }

/*      getadr -- get primary ethernet interface address
 *              read list of net devices via ioctl to ifreqs.  Pass over
 *              loopback device and any serial line interfaces.  Use first
 *              ether device found.  This could cause a problem if there are
 *              multiple ether controllers on the system as it will find the
 *              first device configured.  No panic since most have only one
 *              device.  This function does not require setuid(2) priviledges.
 */

#define IFREQCNT 64     /* need enough room for total number of possible
                         * interfaces, including aliases for LAT and DECnet
                         */
int getadr(name, ifrp)
char *name;
struct ifdevea *ifrp;
{
        int s;
        struct ifreq *ifr;
        struct ifreq ifreqs[IFREQCNT];
        struct ifreq tmp_ifr;
        struct ifconf ifc;
        char *maybe = NULL;     /* a device that might be it */

        bzero(ifrp, sizeof(struct ifdevea));
        if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                perror("socket");
                return( -1);
        }
        if(name != NULL)
                strcpy(ifrp->ifr_name, name);
        else {
                ifc.ifc_req = ifreqs;
                ifc.ifc_len = sizeof(ifreqs);
                if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
                        perror("ioctl: SIOCGIFCONF");
			close(s);
                        return(-1);
                }
                for(ifr = ifreqs; ifr < &ifreqs[IFREQCNT]; ifr++) {
                        if(strlen(ifr->ifr_name) == 0) {
                                if (maybe == NULL) {
                                        fprintf(stderr, "No network devices configured\n");
					close(s);
                                        return(-1);
                                } else
                                        break;  /* check out maybe */
                        } else {
                                (void) strcpy(tmp_ifr.ifr_name, ifr->ifr_name);
                                if (ioctl(s, SIOCGIFFLAGS, &tmp_ifr) < 0) {
                                        perror("ioctl: SIOCGIFFLAGS");
					close(s);
                                        return(-1);
                                }
                                if ((tmp_ifr.ifr_flags & (IFF_POINTOPOINT))
                                         == (IFF_POINTOPOINT)) {
                                        continue; /* skip point to point */
                                }
                                if ((tmp_ifr.ifr_flags
                                    & (IFF_DYNPROTO|IFF_BROADCAST))
                                          != (IFF_DYNPROTO|IFF_BROADCAST)) {
                                        continue;       /* skip CI, etc. */
                                }
                                if ((tmp_ifr.ifr_flags & (IFF_UP|IFF_RUNNING))
                                         != (IFF_UP|IFF_RUNNING)) {
                                        /* save this name, it might be the
                                         * right one but just isn't up now
                                         */
                                        if (maybe == NULL)
                                                maybe = ifr->ifr_name;
                                        continue;   /* not up and running */
                                }
                                (void)strcpy(ifrp->ifr_name, tmp_ifr.ifr_name);
                                maybe = NULL; /* found real one, clear maybe */
                                break;
                        }
                }
        }
        if (maybe != NULL) {
                (void) strcpy(ifrp->ifr_name, maybe);
        }
        if (ioctl(s, SIOCRPHYSADDR, ifrp) < 0) {
                perror("ioctl: SIOCRPHYSADDR");
		close(s);
                return(-1);
        }
	close(s);
        return 0;
}

--
  Rob Riepel                             |     168 Pine Hall
  Systems Programmer                     |     Stanford, CA 94305-4122
  Networking & Communications Systems    |     (415) 725-7577
  Stanford University                    |     riepel@Snowflake.Stanford.EDU
  --------------------------------------------------------------------------
  GE/CS -d+ -p+(---) c++@ !l u++/- e--/* m+ s+ n+ h/--- f+ g+ w+ t+ r+@ y?/+


  reply	other threads:[~1994-02-02 18:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-02-01 21:26 Real ethernet address? Jim Reiss
1994-02-02  1:53 ` Dan Wing
1994-02-02 16:35   ` Jim Reiss
1994-02-02 18:00     ` Rob Riepel [this message]
1994-02-02 15:15 ` Matt Thomas
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox