#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
//取得位址
//返回IP地址的字符串
//返回:0等於成功,-1等於失?*/
int getlocalip(char* outip)
{
int i=0;
int sockfd;
struct ifconf ifconf;
char buf[512];
struct ifreq *ifreq;
char* ip;
//初始化ifconf
ifconf.ifc_len = 512;
ifconf.ifc_buf = buf;
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)
{
return -1;
}
//獲取所有端口信息
ioctl(sockfd, SIOCGIFCONF, &ifconf);
close(sockfd);
//以下是一個個獲取IP地址
ifreq = (struct ifreq*)buf;
for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--)
{
ip = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr);
//排除本機的判斷
if(strcmp(ip,"127.0.0.1")==0)
{
ifreq++;
continue;
}
strcpy(outip,ip);
return 0;
}
return -1;
}
//獲取地址
//返回MAC地址字串
//返回:0等於成功,-1等於失?*/
int get_mac(char* mac)
{
struct ifreq tmp;
int sock_mac;
char mac_addr[30];
sock_mac = socket(AF_INET, SOCK_STREAM, 0);
if( sock_mac == -1)
{
perror("create socket fail\n");
return -1;
}
memset(&tmp,0,sizeof(tmp));
strncpy(tmp.ifr_name,"eth0",sizeof(tmp.ifr_name)-1 );
if( (ioctl( sock_mac, SIOCGIFHWADDR, &tmp)) < 0 )
{
printf("mac ioctl error\n");
return -1;
}
sprintf(mac_addr, "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)tmp.ifr_hwaddr.sa_data[0],
(unsigned char)tmp.ifr_hwaddr.sa_data[1],
(unsigned char)tmp.ifr_hwaddr.sa_data[2],
(unsigned char)tmp.ifr_hwaddr.sa_data[3],
(unsigned char)tmp.ifr_hwaddr.sa_data[4],
(unsigned char)tmp.ifr_hwaddr.sa_data[5]
);
close(sock_mac);
memcpy(mac,mac_addr,strlen(mac_addr));
return 0;
}
int main(void)
{
char ip[20];
char mac[17];
if ( getlocalip( ip ) == 0 )
{
printf("本機IP是: %s\n", ip );
}
else
{
printf("無法取得本機IP位址");
}
if(get_mac(mac) == 0)
{
printf("本機的MAC地址是: %s\n", mac);
}
else
{
printf("無法取得本機的MAC地址");
}
return 0;
}