[comp.unix.ultrix] SOLUTION: ethernet address or other unique ID for DEC-station and SUN

emmerik@utrcu1.UUCP (Emmerik P.J.L. van) (12/20/90)

Using the information submitted to me on my request for information,
i have written the following two functions:

/*-SUN-solution------------------------------------------------------*/
/*
C// Created   : 900301/PVE 
C// Modified  :
C// Language  : Fortran-77 C
C   Reference : SUN/UNIX specific CE-routine                            
C
C   Function  : Get the unique hardware ID of a
C               SUN a computer.
C
C   Parameters:
C   Type  I/O  Name        Description
C   ----  ---  ----------  -----------
C   C       O  ETHADDR     Hexadecimal number representing the
C                          host ID, no punctuation chars.
C                          Length of ETHADDR must be at least 13 chars
C                          12 for ethernet address + 1 for terminator.
C   I       O  ILEN        The length of the string in ETHADDR,
C                          not including the terminator.
C                           = 0 : Error
C                           =12 : Successful completion.
C
*/

#include <stdio.h>

void ceget_nodeid( ethaddr, ilen )
char ethaddr[];
int *ilen;
{
   long hostid;

   *ilen = 0;

   hostid = gethostid();
   if( hostid <= 0 ) {
      printf( stderr, "unique computer identification not found\n");
      goto lab_error;
      }
   sprintf( ethaddr, "%8.8x\n",hostid );
    *ilen = 8;

lab_error:
return;
}
/*-DECstation-ULTRIX solution----------------------------------------*/
/*
C// Created   : 901219/PVE 
C// Modified  :
C// Language  : Fortran-77 C
C   Reference : ULTRIX specific CE-routine                            
C
C   Function  : Get the unique hardware ethernet address of
C               a computer.
C
C   Parameters:
C   Type  I/O  Name        Description
C   ----  ---  ----------  -----------
C   C       O  ETHADDR     Hexadecimal number representing the
C                          ethernet address, no punctuation chars.
C                          Length of ETHADDR must be at least 13 chars
C                          12 for ethernet address + 1 for terminator.
C   I       O  ILEN        The length of the string in ETHADDR,
C                          not including the terminator.
C                           = 0 : Error
C                           =12 : Successful completion.
C
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void ceget_nodeid( ethaddr, ilen )
char ethaddr[];
int *ilen;
{
   struct ifdevea addr;
   struct ifconf device; 
   struct ifreq ifreq_list[2];
   int s, num_dev;

   *ilen = 0;
/*
cs We have to provide to ioctl for the SIOCGIFCONF funtion
cs a list it can fill in.
*/
   device.ifc_len = sizeof( ifreq_list ); /* provided size of ifc_buf */
   device.ifc_req = ifreq_list;           /* list to hold the device ifo */

/*
cs Create an internet socket
*/
   if ((s=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP ))<0)
   {
      perror("socket");
      goto lab_error;
   }

/*
cs Get the available Ethernet devices.
*/
   if ( ioctl(s, SIOCGIFCONF, &device) < 0 )
   {
      perror("SIOCGIFCONF");
      goto lab_error;
   }
   num_dev = device.ifc_len / sizeof( struct ifreq );
   if ( num_dev < 1 )
   {
      goto lab_error;
   }

/*
cs Get the physical addrs, i.e. the ethernet address.
*/      
   strcpy(addr.ifr_name, device.ifc_ifcu.ifcu_req[0].ifr_name );
   if (ioctl(s, SIOCRPHYSADDR, &addr)<0){
      perror("SIOCRPHYSADDR");
      goto lab_error;
   }

/*
cs Use the default physical address as nodeid (addr.default_pa).
cs Or maybe we should use the current physical address (addr.current_pa).
*/
   sprintf( ethaddr, "%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
            (unsigned char)addr.default_pa[0],
            (unsigned char)addr.default_pa[1],
            (unsigned char)addr.default_pa[2],
            (unsigned char)addr.default_pa[3],
            (unsigned char)addr.default_pa[4],
            (unsigned char)addr.default_pa[5] );
    *ilen = 12;

lab_error:
/*
cs Close the socket.
*/
   close( s );
}
/*-------------------------------------------------------------------*/

If you have any remarks, pleace reply by E-mail !
-- 
Pieter van Emmerik                    | Phone:  +31-74-483059
Hollandse Signaalapparaten b.v.       | FAX:    +31-74-425936
P.O.box 42                            | E-mail: emmerik@utwente.nl
7550 GD  Hengelo, The Netherlands     |         hp4nl!utrcu1!emmerik