Re: Retrieving MAC address from struct ifnet

Martin Husemann <[email protected]>
Newsgroups gmane.os.netbsd.devel.kernel
Message-ID <[email protected]>
On Sun, Jun 08, 2025 at 04:00:22PM +0200, Beesdeckar wrote:
> When I try to cycle through interfaces and I read first member of if_hwdl
> (struct	sockaddr *ifa_addr) the result for ethernet is:
> 
> sa_family: 18, sa_len: 17,
> sa_data: 01 00 06 03 06 00 77 6D 30 F0 DE F1 8F AD EB 00 00

If you just want the string, call getnameinfo(3) on it, like:

	char name[40];
	getnameinfo(ifa, ifa->ifa_addr->sa_len, name, sizeof name,
	    NULL, 0, NI_NUMERICHOST);

or if you want the binary representation:

	#include <net/if_dl.h>

	unsigned char dladdr[ETHER_ADDR_LEN];
	const struct sockaddr_dl *sdl = \
	    (const struct sockaddr_dl*)ifa->ifa_addr;
	memcpy(dladdr, CLLADDR(sdl), ETHER_ADDR_LEN);


Martin
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.