Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: Joe Spanicek <joe-x@resptk.bhp.com.au>
To: markwicn@sebb.bel.alcatel.be
Subject: Re: Getting Ethernet (MAC) address on DU 4.0
Date: 1998/09/08	[thread overview]
Message-ID: <35F488F9.486107CD@resptk.bhp.com.au> (raw)
In-Reply-To: 35F3AE4E.413E4BF3@nospam.sebb.bel.alcatel.be

[-- Attachment #1: Type: text/plain, Size: 1613 bytes --]

Niel Markwick wrote:
> 
> I know this is a question that has been asked before, but I am looking
> for some different answers!
> 
> I am looking for a generic way of getting the MAC address from the
> ethernet card from within a C program...
> 
> On the VMS and Solaris platforms, I am sucessfully using the SIOCGARP
> ioctl to get ther ARP entry for the host, but on DEC Unix, this fails...
> It seems that VMS and SOLARIS store an entry in the ARP tables for the
> local host, but Dec Unix doesn't.
> 
> The answer found in Deja News
>         (http://www.dejanews.com/getdoc.xp?AN=333909507)
> was to use the SIOCRPHYSADDR ioctl, but this requires knowledge of the
> name of the network interface that you are interrogating (ie tu#, ln#,
> le#)
> 
> So, is there another way to get the MAC address (I suspect not).
> Alternatively, is there a way to get the names of the network interfaces
> on a system, so that I can then use these with the SIOCRPHYSADDR ioctl
> to get the addresses.
> 
> I currently have a workaround which is to popen the command:
>         /usr/sbin/netstat -i \
>            | awk ' {print $4} ' \
>            | grep '[0-9a-f:]\{17\}'
> and read the output back into my program, but this feels sooooo messy,
> and is subject to the whims of Digital changing the output format of
> netstat!
> 
> All help gratefully recieved.
> 
> Niel.
> 
> --
> Niel Markwick
> Consultant Software Engineer
> Alcatel Bell Telecom, Kontich, Belgium.
> Work Email: markwicn@sebb.bel.alcatel.be


Hi,

I found this bit of C code on the Digital Web Site. Hope it helps.

Joe

PS: The code is an attachment

[-- Attachment #2: ether_minv2.0.c --]
[-- Type: text/plain, Size: 8279 bytes --]

/*
 *      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...
 *              September 28, 1993 - Doug Olson, updated to match change in
 *                              /usr/include/net/if.h (IFF_MULTICAST).
 *              3 November 1994 Chuck Newman.  getadr was changed to return the
 *                              addresses of all ethernet devices.  The
 *                              interface was completely changed.
 */

#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;

    {
    char            *interface;
#define INTERFACE_COUNT 32
	unsigned char hardware[INTERFACE_COUNT][6];
	int temp_int, count;

    if(argc < 2)
        interface = NULL;
    else
        interface = argv[1];

    if ((count = getadr(hardware, INTERFACE_COUNT)) == 0)
        {
        fprintf(stderr, "couldn't get network interface\n");
        exit(1);
        }

    for (temp_int = 0; temp_int < abs(count); temp_int++)
      {
        printf("Ethernet hardware address = (%02.2X-%02.2X-%02.2X-%02.2X-%02.2X-%02.2X)\n",
            hardware[temp_int][0], hardware[temp_int][1],
            hardware[temp_int][2], hardware[temp_int][3],
            hardware[temp_int][4], hardware[temp_int][5]);
      }
    if (count < 0) printf("There are more devices than those listed\nCall getadr with a larger buffer\n");
    }

/*      getadr -- get primary ethernet interface address
 *              read list of net devices via ioctl to ifreqs.  Pass over
 *              loopback device and any serial line interfaces.  Return
 *              all devices.
 * 
 *              This function does not require setuid(2) priviledges.
 */

#if 0

This routine accepts two parameters, and returns the count of addresses found
    Input 1:  Area to receive ethernet addresses
    Input 2:  Number of addresses that can fit into the area specified
              by Input 1.  Each address takes 6 bytes.
   Return
      Value:  Positive -- number of addresses returned
              Negative -- absolute value is number of addresses returned,
                          but there are more addresses
              Zero     -- no addresses, or failure

#endif

#define IFREQCNT 64     /* need enough room for total number of possible
                         * interfaces, including aliases for LAT and DECnet
                         */
int getadr(unsigned char hardware[], int count)

    {
    int             s;
    struct ifreq    ifreqs[IFREQCNT];
    struct ifconf   ifc;
    struct ifdevea  ifrp;
    int    index    = 0;
    int    ndx, ifr;

    /* JOE -  added this for loop - insure picked up errors */
    for(s=0; s<IFREQCNT; s++) {
	bzero(&ifreqs[s], sizeof(struct ifreq));
    }
    bzero(&ifrp, sizeof(struct ifdevea));
    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
        {
        perror("socket");
        return(0);
        }

        ifc.ifc_req = ifreqs;
        ifc.ifc_len = sizeof(ifreqs);
        if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
            {
            perror("ioctl: SIOCGIFCONF");
            return(0);
            }

        for (ifr = 0; ((ifr < IFREQCNT)&&(strlen(ifreqs[ifr].ifr_name) != 0)); ifr++)
            {
                if ((ioctl(s, SIOCGIFFLAGS, &ifreqs[ifr]) < 0)&&(index == 0))
                    {
                    perror("ioctl: SIOCGIFFLAGS");
                    return(0);
                    }

                if ((ifreqs[ifr].ifr_flags & (IFF_POINTOPOINT))
                        == (IFF_POINTOPOINT))
                    {
                    continue;   /* skip point to point */
                    }

                if ((ifreqs[ifr].ifr_flags & (IFF_MULTICAST|IFF_BROADCAST))
                        != (IFF_MULTICAST|IFF_BROADCAST))
                    {
                    continue;   /* skip CI, etc. */
                    }

                strcpy(ifrp.ifr_name, ifreqs[ifr].ifr_name);
                if (ioctl(s, SIOCRPHYSADDR, &ifrp) >= 0)
                 {
                  if (index==0)
                    {
                      if (index == count) return -index;
                      hardware[0+index*6] = ifrp.default_pa[0];
                      hardware[1+index*6] = ifrp.default_pa[1];
                      hardware[2+index*6] = ifrp.default_pa[2];
                      hardware[3+index*6] = ifrp.default_pa[3];
                      hardware[4+index*6] = ifrp.default_pa[4];
                      hardware[5+index*6] = ifrp.default_pa[5];
                      index+=1;
                    }
                  else
                    {
                      for (ndx=0; ; ndx++)
                        {
                          if (ndx == count) return -index;
                          if (ndx == index)
                            {
                              hardware[0+index*6] = ifrp.default_pa[0];
                              hardware[1+index*6] = ifrp.default_pa[1];
                              hardware[2+index*6] = ifrp.default_pa[2];
                              hardware[3+index*6] = ifrp.default_pa[3];
                              hardware[4+index*6] = ifrp.default_pa[4];
                              hardware[5+index*6] = ifrp.default_pa[5];
                              index+=1;
                              break;
                            }
                          if ((ifrp.default_pa[5]==hardware[ndx*6+5])&&
                              (ifrp.default_pa[4]==hardware[ndx*6+4])&&
                              (ifrp.default_pa[3]==hardware[ndx*6+3])&&
                              (ifrp.default_pa[2]==hardware[ndx*6+2])&&
                              (ifrp.default_pa[1]==hardware[ndx*6+1])&&
                              (ifrp.default_pa[0]==hardware[ndx*6+0]))
                            {
                              break;
                            }
                        }
                    }
                 }

            }

    return index;
    }

  reply	other threads:[~1998-09-08  4:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-09-07  4:00 Getting Ethernet (MAC) address on DU 4.0 Niel Markwick
1998-09-08  4:00 ` Joe Spanicek [this message]
1998-09-08  4:00 ` John Kohl
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