日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C/C++获取本地IP(适用于多种操作系统)

發(fā)布時間:2025/3/15 c/c++ 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C/C++获取本地IP(适用于多种操作系统) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

下面列舉多種方法獲取本地IP,這些方法在linux系統(tǒng)下均可運(yùn)行,有些方法亦可以在Windows和Android下運(yùn)行.

?

源碼

/*下列幾種方法均優(yōu)先輸出非回環(huán)IP:127.0.0.1?
inet_ntoa(): 系統(tǒng)函數(shù),將網(wǎng)絡(luò)的二進(jìn)制數(shù)IP轉(zhuǎn)化成點分十進(jìn)制IP
inet_ntop(): 系統(tǒng)函數(shù),新型函數(shù),作用與inet_ntoa()相似

inet_aton(): 系統(tǒng)函數(shù),將點分十進(jìn)制IP轉(zhuǎn)化成網(wǎng)絡(luò)的二進(jìn)制數(shù)IP
inet_pton(): 系統(tǒng)函數(shù),新型函數(shù),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結(jié)構(gòu)體和ioctl函數(shù)時需要用到該頭文件
#include <net/if.h>
#include <sys/ioctl.h>

//使用ifaddrs結(jié)構(gòu)體時需要用到該頭文件
#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服務(wù)器IP) , 而且該服務(wù)器 53 端口是開著的
(也可以用一個能夠connect的遠(yuǎn)程服務(wù)器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上也支持), 此函數(shù)不僅能獲取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系統(tǒng)上沒有該頭文件(可自己實現(xiàn)該頭文件所帶內(nèi)容)
該方法較為強(qiáng)大,可以通過網(wǎng)卡名(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相應(yīng)代碼
? ? ? ? }
? ? ? ? ifAddrStruct=ifAddrStruct->ifa_next;
? ? }
? ? return status;
}

/*使用命令獲取IP,此首要條件是需要系統(tǒng)支持相關(guān)命令,限制性條件較多,下面該方法是獲取本機(jī)的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中提取相應(yīng)的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;
}

結(jié)果:

?

解析惡意代碼的使用方法

#include <stdio.h> #include <stdint.h>typedef uint32_t ipv4_t;ipv4_t LOCAL_ADDR;ipv4_t util_local_addr(void) {int fd;struct sockaddr_in addr;socklen_t addr_len = sizeof (addr);errno = 0;if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){ #ifdef DEBUGprintf("[util] Failed to call socket(), errno = %d\n", errno); #endifreturn 0;}addr.sin_family = AF_INET;addr.sin_addr.s_addr = INET_ADDR(8,8,8,8);addr.sin_port = htons(53);connect(fd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in));getsockname(fd, (struct sockaddr *)&addr, &addr_len);close(fd);return addr.sin_addr.s_addr; }int main() {//結(jié)果是反的,大小端換一下LOCAL_ADDR = util_local_addr();return 0; }

?

新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!

總結(jié)

以上是生活随笔為你收集整理的C/C++获取本地IP(适用于多种操作系统)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。