在进行 Linux
网络编程时,经常会需要获取本机 IP
地址,除了常规的读取配置文件外,本文罗列几种个人所知的编程常用方法,仅供参考,如有错误请指出。
Linux
下可以使用 ioctl()
函数以及结构体 struct ifreq
和结构体struct ifconf
来获取网络接口的各种信息。具体过程是先通过 ictol
获取本地所有接口的信息保存到 ifconf
结构中,再从其中取出每个 ifreq
表示的接口信息。
如果本机的 IP
地址绑定在第一块网卡上,则只需指定网卡名称,无需获取所有网卡的信息即可获取,见如下函数:
int get_localip(const char * eth_name, char *local_ip_addr)
{
int ret = -1;
register int fd;
struct ifreq ifr;
if (local_ip_addr == NULL || eth_name == NULL)
{
return ret;
}
if ((fd=socket(AF_INET, SOCK_DGRAM, 0)) > 0)
{
strcpy(ifr.ifr_name, eth_name);
if (!(ioctl(fd, SIOCGIFADDR, &ifr)))
{
ret = 0;
strcpy(local_ip_addr, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
}
}
if (fd > 0)
{
close(fd);
}
return ret;
}
如果想通过获取所有网络接口信息,示例代码如下:
#include
#include
#include
#include
#include
#include
#include
int get_localip(const char * eth_name, char *local_ip_addr)
{
int ret = -1;
register int fd, intrface;
struct ifreq ifr[32];
struct ifconf ifc;
if (local_ip_addr == NULL || eth_name == NULL)
{
return ret;
}
if ((fd=socket(AF_INET, SOCK_DGRAM, 0)) > 0)
{
ifc.ifc_len = sizeof ifr;
ifc.ifc_buf = (caddr_t)ifr;
if (!ioctl(fd, SIOCGIFCONF, (char*)&ifc)) //获取所有接口信息
{
intrface = ifc.ifc_len / sizeof(struct ifreq);
while (intrface-- > 0)
{
//Get IP Address
if (!(ioctl(fd, SIOCGIFADDR, (char*)&ifr[intrface])))
{
if(strcmp(eth_name, ifr[intrface].ifr_name) == 0)
{
ret = 0;
sprintf(local_ip_addr, "%s", inet_ntoa(((struct sockaddr_in*)(&ifr[intrface].ifr_addr))->sin_addr));
break;
}
}
}
}
}
if (fd > 0)
{
close(fd);
}
return ret;
}
int main(int argc, const char **argv)
{
int ret;
char local_ip[20] = {0};
ret = get_localip("eth0", local_ip);
if (ret == 0)
{
printf("local ip:%s\n", local_ip);
}
else
{
printf("get local ip failure\n");
}
return 0;
}
getsockname()
用于获取一个已捆绑或已连接套接字的本地地址。若一个套接字与 INADDR_ANY
捆绑,也就是说该套接字可以用任意主机的地址,此时除非调用 connect()
或 accept()
来连接,否则 getsockname()
将不会返回主机 IP
地址的任何信息。
示例代码:
#include
#include
#include
#include
#include
#include
#include
#define PORT 80
#define SERVER_IP "192.168.10.31"
int main(int argc, const char **argv)
{
int ret = -1;
socklen_t len;
char buf[30] = {0};
struct sockaddr_in server_addr, local_addr;
int fd = socket(AF_INET, SOCK_STREAM, 0);
//int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd <= 0)
{
printf("fail to creat socket\n");
return -1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
if(connect(fd, (struct sockaddr*)&server_addr, sizeof(server_addr))<0)
{
printf("connect error!!!\n");
goto end;
}
len = sizeof(local_addr);
memset(&local_addr, 0, sizeof(local_addr));
ret = getsockname(fd, (struct sockaddr*)&local_addr, &len);
if (ret == 0)
{
printf("local ip is %s, local port is %d\n", inet_ntop(AF_INET, &local_addr.sin_addr, buf, sizeof(buf)), ntohs(local_addr.sin_port));
}
else
{
printf("getsockname failed, error=%d\n", errno);
}
end:
if (fd)
{
close(fd);
}
return ret;
}
getaddrinfo()
可以完成网络主机中主机名和服务名到地址的映射,但是一般不能用来获取本地 IP
地址,当它用来获取本地 IP
地址时,返回的一般是 127.0.0.1
本地回环地址,且该函数仅仅支持 IPv4
。
示例代码:
#include
#include
#include
#include
// 获取本地IP时,一般都是127.0.0.1
int main(int argc, const char **argv)
{
int ret;
char host_name[128] = {0};
struct addrinfo *res, *cur;
struct sockaddr_in *addr;
if (gethostname(host_name, sizeof(host_name)) < 0)
{
printf("gethostname error\n");
return -1;
}
ret = getaddrinfo(host_name, NULL, NULL, &res);
if (ret != 0)
{
printf("Error: error in getaddrinfo on hostname: %s\n", gai_strerror(ret));
return -1;
}
for(cur = res; cur != NULL; cur = cur->ai_next)
{
if(cur->ai_family == AF_INET)
{
addr = (struct sockaddr_in*)cur->ai_addr;
printf("local ip:%s\n", inet_ntoa(addr->sin_addr));
}
//char host[1024] = {0};
//ret = getnameinfo(cur->ai_addr, cur->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
//if(ret != 0)
//{
// printf("getnameinfo: %s\n", gai_strerror(ret));
//}
//else
//{
// printf("ip: %s\n", host);
//}
}
freeaddrinfo(res);
return 0;
}
gethostbyname()
和 getaddrinfo()
的功能类似,一般用于通过主机名或者服务名,比如域名来获取主机的 IP
地址。但是要想获取本地 IP
地址的时候,一般获取的是回环地址 127.0.0.1
。
示例代码:
#include
#include
#include
#include
// 获取本地IP时,一般都是127.0.0.1
int main(int argc, const char **argv)
{
int i = 0;
char host_name[128] = {0};
struct hostent *hptr;
if (gethostname(host_name, sizeof(host_name)) < 0)
{
printf("gethostname error\n");
return -1;
}
if ((hptr=gethostbyname(host_name)) == NULL)
{
printf("gethostbyname error\n");
return -1;
}
while(hptr->h_addr_list[i] != NULL)
{
printf("hostname: %s\n", hptr->h_name);
printf(" ip: %s\n", inet_ntoa(*(struct in_addr*)hptr->h_addr_list[i]));
i++;
}
return 0;
}
代码来自StackOverflow
:http://stackoverflow.com/questions/212528/linux-c-get-the-ip-address-of-local-computer
这里解释一下代码中的 INET_ADDRSTRLEN
和 INET6_ADDRSTRLEN
,该宏变量是定义在 netinet/in.h
头文件中:
// FILE: netinet/in.h
#define INET_ADDRSTRLEN 16 /* for IPv4 dotted-decimal */
#define INET6_ADDRSTRLEN 46 /* for IPv6 hex string */
示例代码:
#include
#include
#include
#include
#include
#include
int main (int argc, const char * argv[])
{
struct ifaddrs * ifAddrStruct=NULL;
struct ifaddrs * ifa=NULL;
void * tmpAddrPtr=NULL;
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr)
{
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4
{
// is a valid IP4 Address
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6
{
// is a valid IP6 Address
tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
}
if (ifAddrStruct!=NULL)
{
freeifaddrs(ifAddrStruct);
}
return 0;
}
用popen()
建立一个管道,管道的一端执行命令 ifconfig
,管道的另一端读取收到的数据并进行相应的解析。这种方法需要执行 shell
命令,配合正则表达式,效率较低,一般不采用。而这种方式其实更倾向于配置,原因就是使用简单。
示例代码:
#include
#include
#define ETH_NAME "ens33"
int main(int argc, const char *argv[])
{
FILE *fp;
char buf[256] = {0};
char command[256] = {0};
//char *fmt = "ifconfig %s|sed -n '2p'|sed -n 's#^.*dr:##gp'|sed -n 's#B.*$##gp'";
char *fmt = "ifconfig %s|grep 'inet addr'|awk '{ print $2}' | awk -F: '{print $2}'";
snprintf(command, sizeof(command), fmt, ETH_NAME);
if((fp = popen(command, "r")) == NULL)
{
perror("Fail to popen\n");
return -1;
}
while(fgets(buf, sizeof(buf), fp) != NULL)
{
printf("%s", buf);
}
pclose(fp);
return 0;
}
[1] https://blog.csdn.net/bailyzheng/article/details/7489656
[2] https://blog.csdn.net/k346k346/article/details/48231933
[3] https://blog.csdn.net/zhongmushu/article/details/89944990