With a lot of research and great help from a few on the list (thanks Luc)..
this is what i finally came up with for find out the IP and MAC address of
an interface in case anyone is interested.
#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>
char *gethwaddr(char *device);
char *getipaddr(char *device);
int main(void){
printf("Hardware: %s\n",gethwaddr("eth0"));
printf("IP: %s\n",getipaddr("eth0"));
}
char *gethwaddr(char *device)
{
int fd,i;
static char ethaddr[8];
struct ifreq ifr;
unsigned char *txt;
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);
txt=(unsigned char *)&ifr.ifr_hwaddr.sa_data;
sprintf(ethaddr, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", txt[0]&0x000000ff,\
txt[1]&0x000000ff,txt[2]&0x000000ff,txt[3]&0x000000ff,\
txt[4]&0x000000ff,txt[5]&0x000000ff);
return ethaddr;
}
char *getipaddr(char *device)
{
int fd,i;
static char ipaddr[14];
struct ifreq ifr;
unsigned long int myip;
char tmp[14]="",str[14]="";
unsigned char *txt;
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));
ifr.ifr_addr.sa_family = AF_INET;
if(ioctl(fd,SIOCGIFADDR,(char *)&ifr)==-1)
{
close(fd);
return 0;
}
close(fd);
txt=(unsigned char *)&ifr.ifr_addr.sa_data;
sprintf(ipaddr, "%d.%d.%d.%d", txt[2],txt[3],txt[4],txt[5]);
return ipaddr;
}
--
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>.