获取本地 ip 地址,mac,通过域名获取对应的 ip,
是网络编程可能遇到的比较常见的操作了,所以总结如下(封装了3个函数),
直接上代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h> #include <netdb.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h> #define MAC_SIZE 18
#define IP_SIZE 16 // function declare
int get_ip_by_domain(const char *domain, char *ip); // 根据域名获取ip
int get_local_mac(const char *eth_inf, char *mac); // 获取本机mac
int get_local_ip(const char *eth_inf, char *ip); // 获取本机ip /****** main test **********/
int main(void)
{
char ip[IP_SIZE];
char mac[MAC_SIZE];
const char *test_domain = "www.baidu.com";
const char *test_eth = "eth0"; get_ip_by_domain(test_domain, ip);
printf("%s ip: %s\n", test_domain, ip); get_local_mac(test_eth, mac);
printf("local %s mac: %s\n", test_eth, mac); get_local_ip(test_eth, ip);
printf("local %s ip: %s\n", test_eth, ip); return ;
} // 根据域名获取ip
int get_ip_by_domain(const char *domain, char *ip)
{
char **pptr;
struct hostent *hptr; hptr = gethostbyname(domain);
if(NULL == hptr)
{
printf("gethostbyname error for host:%s/n", domain);
return -;
} for(pptr = hptr->h_addr_list ; *pptr != NULL; pptr++)
{
if (NULL != inet_ntop(hptr->h_addrtype, *pptr, ip, IP_SIZE) )
{
return ; // 只获取第一个 ip
}
} return -;
} // 获取本机mac
int get_local_mac(const char *eth_inf, char *mac)
{
struct ifreq ifr;
int sd; bzero(&ifr, sizeof(struct ifreq));
if( (sd = socket(AF_INET, SOCK_STREAM, )) < )
{
printf("get %s mac address socket creat error\n", eth_inf);
return -;
} strncpy(ifr.ifr_name, eth_inf, sizeof(ifr.ifr_name) - ); if(ioctl(sd, SIOCGIFHWADDR, &ifr) < )
{
printf("get %s mac address error\n", eth_inf);
close(sd);
return -;
} snprintf(mac, MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)ifr.ifr_hwaddr.sa_data[],
(unsigned char)ifr.ifr_hwaddr.sa_data[],
(unsigned char)ifr.ifr_hwaddr.sa_data[],
(unsigned char)ifr.ifr_hwaddr.sa_data[],
(unsigned char)ifr.ifr_hwaddr.sa_data[],
(unsigned char)ifr.ifr_hwaddr.sa_data[]); close(sd); return ;
} // 获取本机ip
int get_local_ip(const char *eth_inf, char *ip)
{
int sd;
struct sockaddr_in sin;
struct ifreq ifr; sd = socket(AF_INET, SOCK_DGRAM, );
if (- == sd)
{
printf("socket error: %s\n", strerror(errno));
return -;
} strncpy(ifr.ifr_name, eth_inf, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - ] = ; // if error: No such device
if (ioctl(sd, SIOCGIFADDR, &ifr) < )
{
printf("ioctl error: %s\n", strerror(errno));
close(sd);
return -;
} memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
snprintf(ip, IP_SIZE, "%s", inet_ntoa(sin.sin_addr)); close(sd);
return ;
}
测试运行
结果如下:
ifreq 结构体分析和使用:
struct ifreq
{
#define IFHWADDRLEN 6
union
{
char ifrn_name[IFNAMSIZ];
} ifr_ifrn; union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
struct sockaddr ifru_netmask;
struct sockaddr ifru_hwaddr;
short ifru_flags;
int ifru_ivalue;
int ifru_mtu;
struct ifmap ifru_map;
char ifru_slave[IFNAMSIZ];
char ifru_newname[IFNAMSIZ];
void __user * ifru_data;
struct if_settings ifru_settings;
} ifr_ifru;
}; #define ifr_name ifr_ifrn.ifrn_name
#define ifr_hwaddr ifr_ifru.ifru_hwaddr
#define ifr_addr ifr_ifru.ifru_addr
#define ifr_dstaddr ifr_ifru.ifru_dstaddr
#define ifr_broadaddr ifr_ifru.ifru_broadaddr
#define ifr_netmask ifr_ifru.ifru_netmask
#define ifr_flags ifr_ifru.ifru_flags
#define ifr_metric ifr_ifru.ifru_ivalue
#define ifr_mtu ifr_ifru.ifru_mtu
#define ifr_map ifr_ifru.ifru_map
#define ifr_slave ifr_ifru.ifru_slave
#define ifr_data ifr_ifru.ifru_data
#define ifr_ifindex ifr_ifru.ifru_ivalue
#define ifr_bandwidth ifr_ifru.ifru_ivalue
#define ifr_qlen ifr_ifru.ifru_ivalue
#define ifr_newname ifr_ifru.ifru_newname
#define ifr_settings ifr_ifru.ifru_settings
基本介绍:
ifreq结构定义在/usr/include/net/if.h,用来配置ip地址,激活接口,配置MTU等接口信息的。其中包含了一个接口的名 字和具体内容——(是个共用体,有可能是IP地址,广播地址,子网掩码,MAC号,MTU或其他内容)。ifreq包含在ifconf结构中。而 ifconf结构通常是用来保存所有接口的信息的。
举例说明:
在Linux系统中,ifconfig命令是通过ioctl接口与内核通信,例如,当系统管理员输入如下命令来改变接口eth0的MTU大小:
ifconfig eth0 mtu 1250
ifconfig命令首先打开一个socket,然后通过系统管理员输入的参数初始化一个数据结构,并通过ioctl调用将数据传送到内核。SIOCSIFMTU是命令标识符。
struct ifreq data;
fd = socket(PF_INET, SOCK_DGRAM, 0);
< ... initialize "data" ...>
err = ioctl(fd, SIOCSIFMTU, &data);