QNX RTOS v4 Knowledge Base
QNX RTOS v4 Knowledge Base
Title |
Getting MAC address from a C program |
Ref. No. |
QNX.000009830 |
Category(ies) |
Network, Development |
Issue |
Is there a way to obtain the Ethernet address of a NIC card programatically from within a C program? If so, how?
|
Solution |
Use the following source code:
/* find my mac address */
#include <stdio.h> #include <malloc.h> #include <sys/kernel.h> #include <sys/net_nq.h> #include <sys/net41msg.h> #include <sys/net_drvr.h> #include <errno.h>
void main() { int ctr; int pid; struct _net_rnodemap rnodemap; char data[1024]; struct _net_rnodemap_reply *rnodemap_reply; int nids[20];
if ((pid=qnx_vc_name_attach(0, 64, "qnx/net"))==-1 { printf ("Could not attach to Net (%d)n", errno); exit(0); } memset (&rnodemap, 0, sizeof(rnodemap)); rnodemap.type=_NET_RNODEMAP; rnodemap.log_nid=getnid();
rnodemap_reply=(struct _net_rnodemap_reply *) data;
if ((Send(pid, &rnodemap, rnodemap_reply, sizeof(rnodemap), 1000))==-1) { printf ("Send errorn"); exit(0); } printf ("MAC: "); // if you have multiple cards you will need a loop here for (ctr=0; ctr<6; ctr++) printf ("%02x", rnodemap_reply->card[0].phys_addr[ctr]);
printf ("n");
}
The sys/net_nq.h, sys/net41msg.h, and sys/net_drvr.h include files can be found in the /qnx/os/net/netraw.v4.tgz in our free software section.
|
|