Get MAC address - no SIOCGIFHWADDR available

Hi,

using linux it is quite simple to retrieve the MAC address of an ethernet card: ioctl(s, SIOCGIFHWADDR, &ifr) returns it in ifr. Unfortunately the flag SIOCGIFHWADDR, doesn’T seem to exist in QNX.

So how can I retrieve the MAC address here?

Elmi

getifaddrs() / freeifaddrs()

Regards,

-seanb

I needed to do this. getifaddrs() is the place to start. It returns multiple entries for each item, en0, en1, etc. Also, the mac address is returned in a sockaddr structure, but at an offset. I couldn’t find any documentation on this but it was pretty obvious when you find it.

@maschoen: othermark.livejournal.com/3005.html

i am trying to use getifaddrs to get mac ip but when i compile the code it says error:undefined reference to getifaddrs i have included the header files …can anyone help me out

Amit,

Are you getting these errors at compile time or link time?

If it’s at link time did you remember to add ‘-l socket’ to include the socket library?

Tim

Tim ,

I have included that -l socket option,error is coming in compile time

#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include<netinet/in.h>

int main(int argc, char *argv[]) {

struct ifaddrs *ifaddr;
int family, s;

if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(1);

}
/*collecting the data recived
*/
struct ifaddrs *ifa = ifaddr;

for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr != NULL) {
  int family = ifa->ifa_addr->sa_family;
  if (family == AF_INET || family == AF_INET6) {
    char ip_addr[NI_MAXHOST];
    int s = getnameinfo(ifa->ifa_addr,
                        ((family == AF_INET) ? sizeof(struct sockaddr_in) :
                                               sizeof(struct sockaddr_in6)),
                        ip_addr, sizeof(ip_addr), NULL, 0, NI_NUMERICHOST);
    if (s != 0) {
      printf("getnameinfo() failed: %s\n", gai_strerror(s));
      exit(1);
    } else {
      printf("%-7s: %s\n", ifa->ifa_name, ip_addr);
    }
  }/* else if (family == AF_PACKET) {
    struct rtnl_link_stats *stats = ifa->ifa_data;
    printf("%-7s:\n"
           "\ttx_packets = %12u, rx_packets = %12u\n"
           "\ttx_bytes   = %12u, rx_bytes   = %12u\n",
           ifa->ifa_name,
           stats->tx_packets, stats->rx_packets,
           stats->tx_bytes, stats->rx_bytes);
  } else {
    printf("%-7s: family=%d\n", ifa->ifa_name, family);
  }

*/
}
}

i am getting output:
lo0:123.0.0.1
en0:168.12.12.33

how to get the Mac address from abhove program…plz help

freeifaddrs(ifaddr);
exit(0);
}

Amit,

Do you not have ifaddrs.h in your /usr/include directory? It’s there for me so I am not sure why your compilation isn’t working.

This link might help (This assumes you are running QNX 6.5)

qnx.com/support/knowledgebas … 000000mosy

Tim

Hi,

thanks for the reply,and the link u have send i try it.

But i used the below code to find the mac id.Is there any difference b/w 2,and which one is better

#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include<hw/nicinfo.h>
#include<sys/dcmd_io-net.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>

int main()
{
nic_config_t nicconfig;
int fd,i;
char mac[10];
fd=open("/dev/io-net/en0",O_RDONLY);
devctl(fd, DCMD_IO_NET_GET_CONFIG, &nicconfig,sizeof(nicconfig), NULL);
printf(“Device id=”);
for (i=0;i<8;i++)
{
printf("%x",nicconfig.permanent_address[i]);
}
printf("\n");
return 1;
}