How do I find the MAC address in C on different UNIX platforms?


 
Thread Tools Search this Thread
Top Forums Programming How do I find the MAC address in C on different UNIX platforms?
# 1  
Old 09-13-2010
Power How do I find the MAC address in C on different UNIX platforms?

I need to find the MAC address of the ethernet cards on the host machine from the C language. I have found a way to do this on Linux using socket(), ioctl() and the ifreq structure. But this does not seem to work on AIX, HP/UX and probably the others I need (Solaris, SCO, Alpha etc).

Is there a standard way to do this in UNIX?
Why do the UNIX ifreq structures not have a MAC address field like Linux?
Or and I missing something here? It would be fine with me if I need to to it differently for each platform, if I have to. I just need to know what calls to use for each type of UNIX.

Please help!

Thank you.
# 2  
Old 09-13-2010
On Solaris, there is a struct member ifru_enaddr in ifreq, which holds the 6 byte ethernet address. I think, it is also present in other unix implementations.
# 3  
Old 09-13-2010
On GNU/Linux, use the SIOCGIFHWADDR ioctl.
# 4  
Old 09-17-2010
Thank you both for answering my question. I had to be taken off of this project for a while and now I am back, and able to see what I can do based on your suggestions.

Thanks again for your help.
# 5  
Old 09-28-2010
MySQL

Hi, I had a chance to continue with this project. But I did not have much luck. In case someone has any more advice here is what I found.

On AIX I have a ifreq struct like this:
Code:
struct    ifreq {
#define    IFNAMSIZ    16
    char    ifr_name[IFNAMSIZ];        /* if name, e.g. "en0" */
    union {
        struct    sockaddr ifru_addr;
        struct    sockaddr ifru_dstaddr;
        struct    sockaddr ifru_broadaddr;
        __ulong32_t    ifru_flags;
        int    ifru_metric;
        caddr_t    ifru_data;
        u_short    ifru_site6;
#ifdef    _AIX
        __ulong32_t   ifru_mtu;
        int     ifru_baudrate;
#endif
    } ifr_ifru;

Notice that there is no enaddr or anything similar. I find something very similar on HP/UX and SCO but on Solaris I have:
Code:
struct    ifreq {
#define    IFNAMSIZ    16
    char    ifr_name[IFNAMSIZ];        /* if name, e.g. "en0" */
    union {
        struct    sockaddr ifru_addr;
        struct    sockaddr ifru_dstaddr;
        char    ifru_oname[IFNAMSIZ];    /* other if name */
        struct    sockaddr ifru_broadaddr;
        int    ifru_index;        /* interface index */
        short    ifru_flags;
        int    ifru_metric;
        char    ifru_data[1];        /* interface dependent data */
        char    ifru_enaddr[6];
        int    if_muxid[2];        /* mux id's for arp and ip */

        /* blah blah blah more stuff */
    } ifr_ifru;

The dates on these header files tend to be around 1998 or 1999 too.

Thanks to the comments from hergp I was able to get the solaris version to compile but then at run time it complained about being unable to link to socket.o so I tried linking it to libsocket.a (because we like to have everything statically linked), but then other references in libsocket.a were unfound, and I had no idea where they are located. So, in the end I was still not able to do this. On Linux it all works fine.

Some of these platforms are using GNU but others are using the native C/C++ compiler.

I was hoping there was a standard way to handle this going back to the System V and 1980s but it appears not.

Any suggestions? Any other methods of doing this quickly and accurately.

Thanks!

Last edited by pludi; 09-28-2010 at 06:12 PM..
# 6  
Old 09-29-2010
libsocket.so on Solaris has these dependencies:
Code:
$ ldd libsocket.so
        libnsl.so.1 =>   /lib/libnsl.so.1
        libc.so.1 =>     /lib/libc.so.1
        libmp.so.2 =>    /lib/libmp.so.2
        libmd.so.1 =>    /lib/libmd.so.1
        libscf.so.1 =>   /lib/libscf.so.1
        libdoor.so.1 =>  /lib/libdoor.so.1
        libuutil.so.1 =>         /lib/libuutil.so.1
        libgen.so.1 =>   /lib/libgen.so.1
        libm.so.2 =>     /lib/libm.so.2

Which libraries you really need when you link your program statically depends on your code. But when you try to add the static versions of these libraries one by one, you can find out, which of them you really need to resolve all your unresolved symbols. Or you just add all of those libraries and rely on ld to link just those objects it needs.
# 7  
Old 10-04-2010
Thank you for your help.

I did what you did, but on my platform. I did ldd libsocket.so to see the list in my context. It was different but I continued.

I tried what you said and linked with
/lib/libsocket.a and /lib/libnsl.a
then I had only a few left and they seemed related to lindl so I tried linking to libdl.a too but discovered there was no libdl.a only a lindl.so so I tried linking to this in hopes that it would resolve the static linkage but then find libdl.so and link dynamically at run time. As I mentioned, I normally avoid .so files to avoid run time linkage issues, but I tried it.

The executable linked but when I ran it all the calls to ioctl returned -1 (error). Keep in mind that when I make these same calls (or similar) on Linux they work. I suspect the linkage more than the calling code. All I am doing at the point I get the error is calling ioctl() with
a handle value of 8, SIOCGIFADDR and IfReq.ifr_name set to "eth0"
The handle is from a call to
socket(AF_INET, SOCK_DGRAM, IP_PROTOIP)

Any other ideas?

Can you tell me in a nutshell how the so system works. For example, when I link with a .so file, does it resolve the static linkage issue and the at runtime go out looking for the same .so file to dynamically link to it?
If this is the case, then what are the rules for finding the .so file (in case the location of the .so file on my compiling platform is different that the end users platform)?
Could I have inked with libsocket.so directly? Is this any better? Or is it just a matter of preference?
My concern is of course two things:
- why did it link and the fail to run properly (assuming my code is OK)?
- how can I be sure my code will run reliably using .so files if I do get it to work?

Thank you for your insights.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

MAC Address - Four Interfaces with the same MAC Address

four interfaces with ifconfig all interfaces have the same mac. If is not set for unique. but it still works. what difference does it make to have all macs the same or different? (4 Replies)
Discussion started by: rrodgers
4 Replies

2. Shell Programming and Scripting

Multichecks across all unix platforms

Can somebody refer me following multicheck to perform across most of unix platform like AIX, HP-UX, solaris, Linux. CPU utilization above X% Check IO above X% Swap usage check above X% Memory utilization above X% ... (3 Replies)
Discussion started by: sendtoshailesh
3 Replies

3. IP Networking

Tracing a MAC address to IP address: Solaris

Hi there I lost connectivity to one of our remote systems and when I checked the messages log I found the following: Aug 10 23:42:34 host xntpd: time reset (step) 1.681729 s Aug 16 13:20:51 host ip: WARNING: node "mac address" is using our IP address x.x.x.x on aggr1 Aug 16 13:20:51 host... (9 Replies)
Discussion started by: notreallyhere
9 Replies

4. UNIX for Dummies Questions & Answers

Is there a unix command to find ALL hostnames for an ip address?

I am trying to determine if there are several url/host names for an IP address. Is there a UNIX command to find ALL host names for an IP address? Thank you in advance. (3 Replies)
Discussion started by: rukasu
3 Replies

5. UNIX and Linux Applications

Platforms using Unix

Hi ;) Which hardware platforms/machine types use the Operating System Unix? A list of all would be appreaciated Thx Megadrink :cool: (2 Replies)
Discussion started by: Megadrink
2 Replies

6. Solaris

Get ip address from mac address

I have following message in my messages file on solaris 10 WARNING: e1000g3712000:3 has duplicate address 010.022.196.011 (in use by 00:50:56:85:25:ef); disabled Now is there any way i can find which server has 00:50:56:85:25:ef mac address either IP or Hostname ? (6 Replies)
Discussion started by: fugitive
6 Replies

7. UNIX for Dummies Questions & Answers

Unix Platforms

Hi. Where can i learn, Which Unix platform specific for what? Properties? (1 Reply)
Discussion started by: Enrgy
1 Replies

8. UNIX for Advanced & Expert Users

Find IP Address of a Printer Q in Hp-Unix

Could anyone help me to identify the IP address of a printer queue with its printer queue name information . (1 Reply)
Discussion started by: lenin1985
1 Replies

9. IP Networking

How to Achive IP address through MAC(Ethernet) address

Hi sir, i want to make such programe which takes MAC(Ethernet) address of any host & give me its IP address....... but i'm nt getting that how i can pass the MAC address to Frame........ Please give me an idea for making such program... Thanks & regards Krishna (3 Replies)
Discussion started by: krishnacins
3 Replies

10. IP Networking

Mac address - what is line command to find and active new card

How do you find mac address and active new card. (1 Reply)
Discussion started by: wmetz
1 Replies
Login or Register to Ask a Question