From: Jay Olson <jjo@triton.com.no.spam>
Subject: Re: How to get MAC address
Date: 1998/03/13 [thread overview]
Message-ID: <350A10A9.167E@triton.com.no.spam> (raw)
In-Reply-To: 6eb2t7$4eu$1@nnrp1.dejanews.com
[-- Attachment #1: Type: text/plain, Size: 265 bytes --]
t.schreiner@cadenas.de wrote:
>
> Hi,
> does anybody know how to get the MAC address of a workstation within a
> C program?
>
Here is a short program which uses the SIOCRPHYSADDR ioctl to do that.
--
- Jay Olson (jjo @ triton . com)
Triton Software Group Ltd.
[-- Attachment #2: eth_addr.c --]
[-- Type: text/plain, Size: 1410 bytes --]
/*
* eth_addr - print the ethernet address of a given interface
*
* Author: Jay Olson, Triton Software Group Ltd. (15-Aug-1997)
*
* usage: eth_addr interface
*
* $Log$
*/
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <dli/dli_var.h>
#include <sys/ioctl.h>
static char rcsid[] = "$Id$";
int main (int argc, char **argv)
{
int fd;
struct sockaddr_dl out_bind;
struct ifdevea devea;
if (argc <= 1) {
fprintf(stderr, "usage: eth_addr interface\n");
exit(22);
}
/*
* Create a DLI socket.
*/
if ((fd = socket(AF_DLI, SOCK_DGRAM, DLPROTO_DLI)) < 0) {
fprintf(stderr, "eth_addr: can't open DLI socket\n");
perror("eth_addr");
exit(errno);
}
/*
* Print physical address.
*/
strcpy(devea.ifr_name,argv[1]);
if (ioctl(fd,SIOCRPHYSADDR,&devea) < 0) {
fprintf(stderr,"eth_addr: can't determine address for %s\n",
devea.ifr_name);
perror("eth_addr");
}
else {
fprintf(stderr,"eth_addr: address for %s is %0.2X-%0.2X-%0.2X-%0.2X-%0.2X-%0.2X\n",
argv[1],
devea.current_pa[0], devea.current_pa[1],
devea.current_pa[2], devea.current_pa[3],
devea.current_pa[4], devea.current_pa[5]);
}
}
prev parent reply other threads:[~1998-03-13 5:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-03-13 5:00 How to get MAC address t.schreiner
1998-03-13 5:00 ` Jay Olson [this message]
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