Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: delamper@brsux0.bro.dec.com (Delamper Lukas)
Subject: Re: reading E-net address on DecStation 3100
Date: Wed, 5 Aug 1992 10:28:10 GMT	[thread overview]
Message-ID: <1992Aug5.102810.18707@decuk.uvo.dec.com> (raw)
In-Reply-To: 1397@sousa.ltn.dec.com


*
 *      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 demostration
 *              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.
 */

#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>

static char getadrSID[] = "@(#)getadr.c 1.3 8/17/89 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 ifreq ifr;
        char *interface;

        if(argc < 2)
                interface = NULL;
        else
                interface = argv[1];
        if(getadr(interface, &ifr) < 0) {
                fprintf(stderr, "getadr failed, exiting\n");
                exit(1);
        }
        printf("ifr_name = '%s'\n", ifr.ifr_name);
        printf("family = %04x\n", 0xffff & ifr.ifr_addr.sa_family);
        printf(
           "Ethernet hardware address = (%02x-%02x-%02x-%02x-%02x-%02x)\n",
                0xff & ifr.ifr_addr.sa_family,
                0xff & (ifr.ifr_addr.sa_family >> 8),
                0xff & ifr.ifr_addr.sa_data[0],
                0xff & ifr.ifr_addr.sa_data[1],
                0xff & ifr.ifr_addr.sa_data[2],
                0xff & ifr.ifr_addr.sa_data[3]);
        printf(
            "Ethernet physical address = (%02x-%02x-%02x-%02x-%02x-%02x)\n",
                0xff & ifr.ifr_addr.sa_data[4],
                0xff & ifr.ifr_addr.sa_data[5],
                0xff & ifr.ifr_addr.sa_data[6],
                0xff & ifr.ifr_addr.sa_data[7],
                0xff & ifr.ifr_addr.sa_data[8],
                0xff & ifr.ifr_addr.sa_data[9]);
    }

/*      getadr -- get primary ethernet interface address
 *              read /etc/rc.local to find primary interface and query that
 *              device.  This function does not require setuid(2) priviledges.
 *              NOTE:  Assumes standard Ultrix rc.local as set up by
 *                      netsetup(8).  Will break if rc.local was hacked...
 */

/* offsets for tokens on rc.local line */

#define IFCONFIG        0
#define INTERFACE       1
#define HOSTNAME        2
#define BROADCAST       3
#define NUMTOKENS       4
  int getadr(name, ifrp)
char *name;
struct ifreq *ifrp;
{
        int stat, i, s;
        char line[256];
        FILE *rcf;
        char *tokens[NUMTOKENS];
        char *cp;

        if((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
                perror("getadr, socket failed");
                return( -1);
        }
        if(name != NULL)
                strcpy(ifrp->ifr_name, name);
        else {
                if((rcf = fopen("/etc/rc.local", "r")) == NULL) {
                        perror("getadr, open failed on rc.local");
                        return -1;
                }
                while((cp = fgets(line, 256, rcf)) != NULL) {
                        for(i = 0; i < NUMTOKENS; i++) {
                                tokens[i] = cp;
                                while(*cp != NULL && *cp != ' ' &&
                                         *cp != '\t') cp++;
                                if(*cp == NULL)
                                        continue;
                                *cp++ = NULL;
                                while(*cp == ' ' || *cp == '\t') cp++;
                        }
                        if(strcmp(tokens[IFCONFIG], "/etc/ifconfig") == 0
                           && strcmp(tokens[HOSTNAME], "`/bin/hostname`") == 0
                           && strcmp(tokens[BROADCAST], "broadcast") == 0)
                                break;
                }
                fclose(rcf);
                strcpy(ifrp->ifr_name, tokens[INTERFACE]);
        }
        if((stat = ioctl(s, SIOCRPHYSADDR, ifrp)) == -1) {
                perror("getadr, ioctl failed");
                return -1;
        }
        return 0;
}


  parent reply	other threads:[~1992-08-05 10:28 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-07-26  5:50 VAX question dwagoner
1992-07-26 21:19 ` Boyd Roberts
1992-07-26 22:39   ` VAX question (P0/P1 space) Eliot Moss
1992-07-27 21:24     ` Paul Dormitzer
1992-07-28 15:51       ` Paul Dormitzer
1992-08-05 10:28       ` Delamper Lukas [this message]
1992-08-05 17:03         ` reading E-net address on DecStation 3100 Gary A. Gaudet
1992-08-07 12:31           ` E.C. Loyd
1992-08-10  5:23             ` Peter Kaiser
1992-07-27 13:40 ` VAX question Dileep Bhandarkar
1992-07-27 16:44   ` CP/M lives!
  -- strict thread matches above, loose matches on Subject: below --
1992-07-27 23:56 reading E-net address on DecStation 3100 Steven Sonnenberg
1992-07-28 13:41 ` E.C. Loyd
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