C語言獲取本地IP(適用於多種作業系統)
阿新 • • 發佈:2019-01-23
前言
下面列舉多種方法獲取本地IP,這些方法在linux系統下均可執行,有些方法亦可以在Windows和Android下執行.
原始碼
/*下列幾種方法均優先輸出非迴環IP:127.0.0.1
inet_ntoa(): 系統函式,將網路的二進位制數IP轉化成點分十進位制IP
inet_ntop(): 系統函式,新型函式,作用與inet_ntoa()相似
inet_aton(): 系統函式,將點分十進位制IP轉化成網路的二進位制數IP
inet_pton(): 系統函式,新型函式,inet_aton()相似
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
//使用 ifconf結構體和ioctl函式時需要用到該標頭檔案
#include <net/if.h>
#include <sys/ioctl.h>
//使用ifaddrs結構體時需要用到該標頭檔案
#include <ifaddrs.h>
#define PJ_MAX_HOSTNAME (128)
#define RUN_SUCCESS 0
#define RUN_FAIL -1
/* 該方法一般取出的ip為 127.0.0.1 ,windows也可以使用此類方法,但是需要略為改動*/
int get_local_ip_using_hostname(char *str_ip)
{
int status = RUN_FAIL;
int i = 0;
char buf[PJ_MAX_HOSTNAME] = {0};
char *local_ip = NULL;
if (gethostname(buf, sizeof(buf)) == 0)
{
struct hostent *temp_he;
temp_he = gethostbyname(buf);
if (temp_he)
{
for(i = 0; temp_he->h_addr_list[i]; i++)
{
local_ip = NULL;
local_ip = inet_ntoa(*(struct in_addr *)(temp_he->h_addr_list[i]));
if(local_ip)
{
strcpy(str_ip, local_ip);
status = RUN_SUCCESS;
if(strcmp("127.0.0.1", str_ip))
{
break;
}
}
}
}
}
return status;
}
/*該方法需要能夠夠ping通 1.1.1.1(DNS伺服器IP) , 而且該伺服器 53 埠是開著的
(也可以用一個能夠connect的遠端伺服器IP代替),windows也可以使用此類方法,但是需要略為改動*/
int get_local_ip_using_create_socket(char *str_ip)
{
int status = RUN_FAIL;
int af = AF_INET;
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in remote_addr;
struct sockaddr_in local_addr;
char *local_ip = NULL;
socklen_t len = 0;
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(53);
remote_addr.sin_addr.s_addr = inet_addr("1.1.1.1");
len = sizeof(struct sockaddr_in);
status = connect(sock_fd, (struct sockaddr*)&remote_addr, len);
if(status != 0 ){
printf("connect err \n");
}
len = sizeof(struct sockaddr_in);
getsockname(sock_fd, (struct sockaddr*)&local_addr, &len);
local_ip = inet_ntoa(local_addr.sin_addr);
if(local_ip)
{
strcpy(str_ip, local_ip);
status = RUN_SUCCESS;
}
return status;
}
/*linux上支援(Android上也支援), 此函式不僅能獲取IP,還可以獲取MAC地址、掩碼和廣播地址等*/
int get_local_ip_using_ifconf(char *str_ip)
{
int sock_fd, intrface;
struct ifreq buf[INET_ADDRSTRLEN];
struct ifconf ifc;
char *local_ip = NULL;
int status = RUN_FAIL;
if ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)
{
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
if (!ioctl(sock_fd, SIOCGIFCONF, (char *)&ifc))
{
intrface = ifc.ifc_len/sizeof(struct ifreq);
while (intrface-- > 0)
{
if (!(ioctl(sock_fd, SIOCGIFADDR, (char *)&buf[intrface])))
{
local_ip = NULL;
local_ip = inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr);
if(local_ip)
{
strcpy(str_ip, local_ip);
status = RUN_SUCCESS;
if(strcmp("127.0.0.1", str_ip))
{
break;
}
}
}
}
}
close(sock_fd);
}
return status;
}
/*可以檢測ip4也可以檢測ip6,但是需要ifaddrs.h,某些Android系統上沒有該標頭檔案(可自己實現該標頭檔案所帶內容)
該方法較為強大,可以通過網絡卡名(ifAddrStruct->ifr_name)獲取IP.
*/
int get_local_ip_using_ifaddrs(char *str_ip)
{
struct ifaddrs * ifAddrStruct=NULL;
void * tmpAddrPtr=NULL;
int status = RUN_FAIL;
getifaddrs(&ifAddrStruct);
while (ifAddrStruct!=NULL)
{
if (ifAddrStruct->ifa_addr->sa_family==AF_INET) // check it is IP4
{
tmpAddrPtr = &((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
if(inet_ntop(AF_INET, tmpAddrPtr, str_ip, INET_ADDRSTRLEN))
{
status = RUN_SUCCESS;
if(strcmp("127.0.0.1",str_ip))
{
break;
}
}
}else if(ifAddrStruct->ifa_addr->sa_family==AF_INET6){
//可以新增IP6相應程式碼
}
ifAddrStruct=ifAddrStruct->ifa_next;
}
return status;
}
/*使用命令獲取IP,此首要條件是需要系統支援相關命令,限制性條件較多,下面該方法是獲取本機的ip地址*/
int get_netway_ip_using_res(char *str_ip)
{
int status = RUN_FAIL;
char do_comment[] = "ifconfig | grep 'inet addr' | awk '{print $2}' | sed 's/.*://g'";
//該命令是從ifconfig中提取相應的IP
FILE *fp = NULL;
fp = popen(do_comment, "r");
if(fp != NULL)
{
status = RUN_SUCCESS;
while( !feof(fp) )
{
fgets(str_ip, 1024, fp);
status = RUN_SUCCESS;
if(strcmp("127.0.0.1", str_ip))
{
break;
}
}
}
fclose(fp);
return status;
}
int main()
{
char local_ip1[INET_ADDRSTRLEN] = {0};
char local_ip2[INET_ADDRSTRLEN] = {0};
char local_ip3[INET_ADDRSTRLEN] = {0};
char local_ip4[INET_ADDRSTRLEN] = {0};
char local_ip5[INET_ADDRSTRLEN] = {0};
if( get_local_ip_using_hostname(local_ip1) == RUN_SUCCESS)
{
printf("get_local_ip_using_hostname() get local ip : %s \n", local_ip1);
}else{
printf("get_local_ip_using_hostname() err \n");
}
if( get_local_ip_using_create_socket(local_ip2) == RUN_SUCCESS)
{
printf("get_local_ip_using_socket() get local ip : %s \n", local_ip2);
}else{
printf("get_local_ip_using_socket() err \n");
}
if( get_local_ip_using_ifconf(local_ip3) == RUN_SUCCESS)
{
printf("get_local_ip_using_ifconf() get local ip : %s \n", local_ip3);
}else{
printf("get_local_ip_using_ifconf() err \n");
}
if( get_local_ip_using_ifaddrs(local_ip4) == RUN_SUCCESS)
{
printf("get_local_ip_using_ifaddrs() get local ip : %s \n", local_ip4);
}else{
printf("get_local_ip_using_ifaddrs() err \n");
}
if( get_netway_ip_using_res(local_ip5) == RUN_SUCCESS)
{
printf("get_netway_ip_using_res() get local ip : %s \n", local_ip5);
}else{
printf("get_netway_ip_using_res() err \n");
}
return 0;
}
執行結果: