getting interface mac address

getting interface mac address

Post by Brian Fie » Mon, 18 Dec 1995 04:00:00



From C, how do I determine an interface's MAC address?  (If it's machine
dependent, I'm looking for how this is done under OSF and IRIX)

Thanks
Brian

 
 
 

getting interface mac address

Post by David Pete » Tue, 19 Dec 1995 04:00:00



> From C, how do I determine an interface's MAC address?  (If it's machine
> dependent, I'm looking for how this is done under OSF and IRIX)

> Thanks
> Brian

On Irix you can use the SIOCGIFADDR ioctl. This might well work for OSF
as well, or you can use the EIOCDEVP ioctl I think.
--
David P Peter                 | It happens.
Insignia Solutions Inc.       | Sometimes people just explode.

Voice: +44 (0)1494 453351     | http://www.insignia.com/

 
 
 

getting interface mac address

Post by Igor Khasil » Wed, 20 Dec 1995 04:00:00



> From C, how do I determine an interface's MAC address?  (If it's machine
> dependent, I'm looking for how this is done under OSF and IRIX)

Sorry, don't know are they BSDish or not, but under BSD-like systems
you have to use ioctl's. Let me give an example (this is my prog to check
IPX binding, so excuse for it's roughness)

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <netipx/ipx.h>
#include <netinet/in.h>
main(int argc, char **argv)
{
struct  ifreq   ifr, *ifrp;
struct  ifconf  ifconf;
unsigned char   buf[10*sizeof(ifr)];
int             s, sipx,space=sizeof(buf);
int             cmd = SIOCGIFADDR;
        if ( argc > 1 )
        if ( !strcmp("-s", argv[1]) )
                cmd = SIOCSIFADDR;
            else
                cmd = SIOCGIFADDR;
        ifconf.ifc_len=sizeof(buf);
        ifconf.ifc_buf=buf;
        ifrp = buf;
        bzero(buf, sizeof(buf));
        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if ( s == -1 ) {
                perror("socket in");
                exit(0);
        }
        if (ioctl(s, SIOCGIFCONF, &ifconf)) {
                perror("INET: SIOCGIFCONF");
                exit(0);
        }
        ifrp = buf; space=ifconf.ifc_len;
#define family  ifrp->ifr_addr.sa_family
#define sdl     ((struct sockaddr_dl*)&(ifrp->ifr_addr))
#define eth     ((unsigned char *)&(sdl->sdl_data[sdl->sdl_nlen]))
        while(space > 0){
                printf("iface: %s",ifrp->ifr_name, family);
                if ( family == AF_LINK ) {
                        /* this is hardware address */
                        printf("\n hw_addr=%02x:%02x:%02x:%02x:%02x:%02x",
                                (unsigned char)eth[0],
                                (unsigned char)eth[1],
                                (unsigned char)eth[2],
                                (unsigned char)eth[3],
                                (unsigned char)eth[4],
                                (unsigned char)eth[5]);
                }
                /* get inet addr */
                if (ifrp->ifr_addr.sa_family == AF_INET)
#define ifrp_sin ((struct sockaddr_in*)&(ifrp->ifr_addr))
                        printf("\n inetaddr: %s", inet_ntoa(ifrp_sin->sin_addr.s_addr));
#undef  ifrp_sin
                if (ifrp->ifr_addr.sa_family == AF_IPX)
#define ifrp_sipx ((struct sockaddr_ipx*)&(ifrp->ifr_addr))
                        printf("\n ipx_addr: %04x%04x:%04x%04x%04x%",
                                ntohs(ifrp_sipx->sipx_addr.x_net.s_net[0]),
                                ntohs(ifrp_sipx->sipx_addr.x_net.s_net[1]),
                                ntohs(ifrp_sipx->sipx_addr.x_host.s_host[0]),
                                ntohs(ifrp_sipx->sipx_addr.x_host.s_host[1]),
                                ntohs(ifrp_sipx->sipx_addr.x_host.s_host[2])
                                );
#undef  ifrp_sipx

                space-=sdl->sdl_len+16;
                ifrp=((unsigned char *)ifrp+16+sdl->sdl_len);
                printf("\n");
        }

Quote:}

Best regards!

                                Igor

 
 
 

1. Command for assigning IPv6 address and MAC address on the interface

Hello,

iceman a crit :

man ifconfig
man 8 ip

Please can you elaborate ? How do you see it does not work ?

Yes, it works for me. You can also assign an IPv6 address with the 'ip'
command from the iproute/iproute2 package :

ip -6 address add 2001:0db8:0:f101::1/64 dev eth0

Note that the 2001:0db8::/32 range is to be used for documentation
purpose only. If you do not have a global prefix you can create and use
a prefix in the unique local address range (ULA, cf. RFC 4193). A
generator is available at <http://www.kame.net/~suz/gen-ula.html>.

2. Help with FIPS and Win95 (Unknown file system)

3. Change MAC address without down interface

4. Some new daemons started by default in 4.3.3

5. Interface MAC address SunOS 4.1.3

6. Printing Problem - No Print

7. MAC address for each interface

8. Mail collection and delivery

9. How assign MAC address to logical interface hme0:1?

10. changing mac-address (2 hme interfaces)

11. MAC-Address of my interface?

12. Per interface MAC addresses

13. how to get mac address of network interfaces