From owner-freebsd-hackers@FreeBSD.ORG Fri Aug 4 16:16:17 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org 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 58AE716A4DE for ; Fri, 4 Aug 2006 16:16:17 +0000 (UTC) (envelope-from mime@traveller.cz) Received: from ss.eunet.cz (ss.eunet.cz [193.85.228.13]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3B1C243D64 for ; Fri, 4 Aug 2006 16:16:08 +0000 (GMT) (envelope-from mime@traveller.cz) Received: from localhost.i.cz (ss.eunet.cz [193.85.228.13]) by ss.eunet.cz (8.13.6/8.13.6) with ESMTP id k74GG4mX042720 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO); Fri, 4 Aug 2006 18:16:05 +0200 (CEST) (envelope-from mime@traveller.cz) From: Michal Mertl To: Mario Lobo In-Reply-To: <200608040759.49390.mario.lobo@ipad.com.br> References: <200608040759.49390.mario.lobo@ipad.com.br> Content-Type: multipart/mixed; boundary="=-tOklOyZQSH+iNYW5Ud0j" Date: Fri, 04 Aug 2006 18:15:47 +0200 Message-Id: <1154708147.3222.15.camel@genius.i.cz> Mime-Version: 1.0 X-Mailer: Evolution 2.6.3 FreeBSD GNOME Team Port X-Content-Filtered-By: Mailman/MimeDel 2.1.5 Cc: freebsd-hackers@freebsd.org Subject: Re: getting interface MAC addr from C X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Aug 2006 16:16:17 -0000 --=-tOklOyZQSH+iNYW5Ud0j Content-Type: text/plain Content-Transfer-Encoding: 7bit Mario Lobo wrote: > Hi; > > Would anyone have a tip on how to get the MAC from a C program? > > I tried: > > struct ifreq ifreq; > unsigned char *hw; > > strcpy(ifreq.ifr_name, "rl0"); > ioctl(fd, SIOCGIFMAC, &ifreq); > hw = ifreq.ifr_ifru.ifru_data; ???? This is probably very far from what you want. MAC stands here for MAC(4) - e.g. security related stuff as opposed to something network (did you read MAC as Media Access Control?). I don't know it that is the easiest/right way to get this information but you can use getifaddrs(3). Attached find a simple program which prints out the interfaces and their MAC addresses using getifaddrs(). The code for getting the printable address is taken from function fmt_sockaddr() from src/usr.bin/netstat/route.c. You can also probably use SIOCGIFADDR with the right socket passed in as fd or with sysctl(3) (int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0}). > but i don't know if this is right or, if it is, where to get the MAC from the > structure. I doesn't issue any error compiling or running though. > > In linux i used: > > struct ifreq ifreq; > unsigned char *hw; > > strcpy(ifreq.ifr_name, "rl0"); > ioctl(fd, SIOCGIFHWADDR, &ifreq); > hw = ifreq.ifr_hwaddr.sa_data; > > Thanks, > > Mario > HTH Michal --=-tOklOyZQSH+iNYW5Ud0j--