Thanks. I think i'm closer to figuring this out (and understanding ioctl
calls)..

Anyway, what is ethaddr?? How can it be displayed in a human readable form
(i.e. convert the 8 bit char array)? I assume the ETHER_ADDR_LEN is 8.
(that's what my ifconfig.c uses and that's what the size of ethaddr is). I
guess i'm looking for something similar to inet_ntoa but for hardware
address instead of internet address.

My MAC address is: 00:D0:B7:5D:E2:A6

And the return values from gethwaddr of various printf conversions are:
HWADDR AS (%s):
HWADDR AS (%x): 80496e4
HWADDR AS (%d): 134518500

Here is the code i'm using:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#define ETHER_ADDR_LEN 8


char *gethwaddr(char *device);


int main(void){
printf("HWADDR: %s\n",gethwaddr("eth0"));
printf("HWADDR: %x\n",gethwaddr("eth0"));
printf("HWADDR: %i\n",gethwaddr("eth0"));
}


char *gethwaddr(char *device)
{
    int fd;
    static char ethaddr[8];
    struct ifreq ifr;

    if((fd=socket(AF_INET,SOCK_DGRAM,0))<0)
        return 0;
    memset(&ifr,0,sizeof(ifr));
    strncpy(ifr.ifr_name,device,sizeof(ifr.ifr_name));

    if(ioctl(fd,SIOCGIFHWADDR,(char *)&ifr)==-1)
    {
        close(fd);
        return 0;
    }
    close(fd);
    memcpy(ethaddr,&ifr.ifr_hwaddr.sa_data,ETHER_ADDR_LEN);
    return ethaddr;
}

I know i'm missing something obvious here.

Thanks Again,

-Andy


> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf Of Kenneth
> Johansson
> Sent: Wednesday, December 20, 2000 8:46 AM
> To: Andy Angrick
> Cc: Luc Hermans; [EMAIL PROTECTED]
> Subject: Re: Retrieving IP/MAC
>
>
> Andy Angrick wrote:
>
> > Where can i find getHWaddr??
> >
> > -Andy
> >
>
> I don't know I think its a windows thing but maybe this works to
> You should probably change this so it is reentrant.
> ----
>
> char *gethwaddr(char *device)
> {
>     int fd;
>     static char ethaddr[8];
>     struct ifreq ifr;
>
>     if((fd=socket(AF_INET,SOCK_DGRAM,0))<0)
>         return 0;
>     memset(&ifr,0,sizeof(ifr));
>     strncpy(ifr.ifr_name,device,sizeof(ifr.ifr_name));
>     if(ioctl(fd,SIOCGIFHWADDR,(char *)&ifr)==-1)
>     {
>         close(fd);
>         return 0;
>     }
>     close(fd);
>     memcpy(ethaddr,&ifr.ifr_hwaddr.sa_data,ETHER_ADDR_LEN);
>     return ethaddr;
> }
>
>
>
>


--
To unsubscribe from this list, send a message to [EMAIL PROTECTED]
with the command "unsubscribe linux-embedded" in the message body.
For more information, see <http://waste.org/mail/linux-embedded>.

Reply via email to