Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 6 Nov 2000 14:04:08 -0500 (EST)
From:      Luoqi Chen <luoqi@seven.watermarkgroup.com>
To:        emulation@FreeBSD.ORG, pfeifer@dbai.tuwien.ac.at, ports@FreeBSD.ORG
Subject:   Re: Wine port: How to obtain MAC address?
Message-ID:  <200011061904.OAA09537@seven.watermarkgroup.com>
In-Reply-To: <Pine.BSF.4.30.0011061305430.64453-100000@taygeta.dbai.tuwien.ac.at>

next in thread | previous in thread | raw e-mail | index | archive | help
> For the Wine port I'd need to obtain the MAC address of an ethernet
> interface, alas it seems that this is not easily possible the way it
> is done in Linux/Solaris?
>
> For Linux, the code in Wine looks as follows:
>
>     strcpy(ifInfo.ifr_name, ifName);
>     if (ioctl(sock, SIOCGIFHWADDR, &ifInfo) < 0)
>     {
>          ERR ("Error obtaining MAC Address!\n");
>          close(sock);
>          return (-1);
>     }
>     else
>     {
>          /* FIXME: Is it correct to assume size of 6? */
>          memcpy(IntInfo->if_physaddr, ifInfo.ifr_hwaddr.sa_data, 6);
>         IntInfo->if_physaddrlen=6;
>     }
>
> For FreeBSD I tried the following program, which doesn't really do what
> I want. If I use SIOCSIFPHYADDR, I get errno=22, and SIOCGIFADDR leads
> to a crash later on.
>
> Any hints?  (Most probably I'm tried completely stupid things, but
> unfortunately, that part of the system is not really documented apart
> from the include files, so I had to take a guess&check approach. :-( )
>
There's no easy way (that I know of) to get the mac address in FreeBSD.
(time for a SIOCGIFLLADDR?) You may look at how ifconfig does it, but
I think the following method is a little bit easier:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>

#include <stdio.h>

main()
{
	struct ifconf ifc;
	struct ifreq *ifr;
	struct sockaddr_dl *sdl;
	char buf[4096], *p;
	int s;

	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s < 0)
		err(-1, "socket");

	ifc.ifc_len = sizeof buf;
	ifc.ifc_buf = buf;
	if (ioctl(s, SIOCGIFCONF, &ifc) < 0)
		err(-1, "ioctl");

	close(s);

	for (p = buf; p < buf + ifc.ifc_len; ) {
		ifr = (struct ifreq *)p;
		if (ifr->ifr_addr.sa_family == AF_LINK) {
			sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
			if (sdl->sdl_type == IFT_ETHER) {
				unsigned char *ea = LLADDR(sdl);
				printf("%s\t%x:%x:%x:%x:%x:%x\n", ifr->ifr_name,
				    ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
			}
		}
		p += _SIZEOF_ADDR_IFREQ(*ifr);
	}
}

-lq


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-emulation" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200011061904.OAA09537>