From owner-freebsd-hackers@FreeBSD.ORG Mon Jun 28 07:36:41 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 36A8516A4CE for ; Mon, 28 Jun 2004 07:36:41 +0000 (GMT) Received: from gawab.com (v2.gawab.com [204.97.230.42]) by mx1.FreeBSD.org (Postfix) with SMTP id DCFFF43D41 for ; Mon, 28 Jun 2004 07:36:40 +0000 (GMT) (envelope-from wandys@gawab.com) Received: (qmail 24063 invoked by uid 1004); 28 Jun 2004 07:36:31 -0000 Received: from unknown (HELO localhost) (wandys@210.77.10.244) by gawab.com with SMTP; 28 Jun 2004 07:36:31 -0000 Date: Mon, 28 Jun 2004 15:41:01 +0800 From: Alecs King To: hackers@freebsd.org Message-ID: <20040628074101.GA885@localhost> Mail-Followup-To: hackers@freebsd.org References: <40DCC57F.5090209@fer.hr> <20040626104947.T93253@woozle.rinet.ru> <40DD3EC1.9000106@fer.hr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <40DD3EC1.9000106@fer.hr> User-Agent: Mutt/1.5.6i X-Mailman-Approved-At: Tue, 29 Jun 2004 12:29:08 +0000 Subject: Re: Getting MAC address? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 28 Jun 2004 07:36:41 -0000 On Sat, Jun 26, 2004 at 11:15:45AM +0200, Ivan Voras wrote: > > I was looking at it and came across getifaddrs(). This function does not > depend on a open socket (yes, mine is AF_LINK, sockaddr_dl), and > apparently returns a list of all interfaces. Is there really no other > way than to traverse this list? Back on the days of using FreeBSD 4.6-Release, i once wrote a simple program to get MAC address of a specified NIC. I tested it on -current just now. Still works fine. =) /* * getmac.c * * Simple Demo: Get MAC address of a specified NIC on FreeBSD * * To compile: gcc getmac.c -o getmac * * Tested on FreeBSD-4.6 RELEASE & FreeBSD-5.2-current */ #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int mib[6], len; char *buf; unsigned char *ptr; struct if_msghdr *ifm; struct sockaddr_dl *sdl; if (argc != 2) { fprintf(stderr, "Usage: getmac \n"); return 1; } mib[0] = CTL_NET; mib[1] = AF_ROUTE; mib[2] = 0; mib[3] = AF_LINK; mib[4] = NET_RT_IFLIST; if ((mib[5] = if_nametoindex(argv[1])) == 0) { perror("if_nametoindex error"); exit(2); } if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { perror("sysctl 1 error"); exit(3); } if ((buf = malloc(len)) == NULL) { perror("malloc error"); exit(4); } if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { perror("sysctl 2 error"); exit(5); } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm + 1); ptr = (unsigned char *)LLADDR(sdl); printf("%02x:%02x:%02x:%02x:%02x:%02x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)); return 0; } Reference: UNP v1. Section 17.5 Hope this helps. -- Alecs King