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

前言

下面列举多种方法获取本地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;
}

结果:

解析恶意代码的使用方法

#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 DEBUG
        printf("[util] Failed to call socket(), errno = %d\n", errno);
#endif
        return 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()
{
    //结果是反的,大小端换一下
    LOCAL_ADDR = util_local_addr();

    return 0;
}

C++获取本机IP地址通常需要使用操作系统的特定API或相关库。在Windows系统中,你可以使用Winsock库中的`gethostbyname()`函数,而在类Unix系统中,可以使用C标准库的`gethostname()`和`gethostbyname()`函数来获取。以下是一个简单的例子,展示了如何在类Unix系统中获取本机IP地址: ```cpp #include <iostream> #include <string> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> std::string getLocalIPAddress() { std::string localIP; char hostname[NI_MAXHOST]; if (gethostname(hostname, sizeof hostname) != -1) { struct hostent *host = gethostbyname(hostname); if (host != nullptr) { for (int i = 0; host->h_addr_list[i] != nullptr; i++) { char ip_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, host->h_addr_list[i], ip_str, sizeof ip_str); localIP = ip_str; } } } return localIP; } int main() { std::string ip = getLocalIPAddress(); std::cout << "本机IP地址是: " << ip << std::endl; return 0; } ``` 这个程序首先通过`gethostname()`获取本机的主机名,然后使用`gethostbyname()`根据主机名获取主机信息,最后通过`inet_ntop()`函数将IP地址的网络字节序转换为可读的字符串格式。 请注意,这个例子可能需要链接网络库,例如在gcc编译时使用`-lnsl`和`-lresolv`选项。 如果你使用的是Windows系统,那么可以通过Winsock API获取IP地址,但这通常需要初始化Winsock,创建一个套接字,然后使用相关函数来查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨痕诉清风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值