* Re: How to get MAC address
1998-03-13 5:00 How to get MAC address t.schreiner
@ 1998-03-13 5:00 ` Jay Olson
0 siblings, 0 replies; 2+ messages in thread
From: Jay Olson @ 1998-03-13 5:00 UTC (permalink / raw)
[-- 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]);
}
}
^ permalink raw reply [flat|nested] 2+ messages in thread