欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux c/c++按规则获取网卡ip

程序员文章站 2022-06-03 08:17:58
...

linux c/c++按规则获取网卡ip

输出项目到云或者输出给外部客户,会遇到服务器多网卡多ip的情形,如果有多个应用都需要这个主机ip,而且多应用需要获取相同的ip,此时可以约定一种规则来获取相同的ip,比如: 获得所有网卡名,然后对网卡名按从小到大排序,查找最小的网卡名对应非local的ip,这样每个应用都使用这种规则,就会获取到相同的ip了,而且这种思路不受多种语言所限制。

详细代码如下:

#include <iostream>
#include <stdio.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

using namespace std;

int compare(const void *a,const void *b)
{
       return strcmp(*(char **)a,*(char **)b);
}

int getSubnetMask(char ip[], int len)
{
    struct sockaddr_in *sin = NULL;
    struct ifaddrs *ifa = NULL, *ifList;
    string name[10];
    if (getifaddrs(&ifList) < 0)
    {
        return -1;
    }
    int i = 0;
    //找到所有网卡,并打印网卡相关信息
    for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next)
    {
        if(ifa->ifa_addr->sa_family == AF_INET)
        {
            string ifname(ifa->ifa_name);
            name[i]=ifname;
            i++;
            printf("interfaceName: %s\n", ifa->ifa_name);

            sin = (struct sockaddr_in *)ifa->ifa_addr;
            printf("ipAddress: %s\n", inet_ntoa(sin->sin_addr));

            sin = (struct sockaddr_in *)ifa->ifa_dstaddr;
            printf("broadcast: %s\n", inet_ntoa(sin->sin_addr));

            sin = (struct sockaddr_in *)ifa->ifa_netmask;
            printf("subnetMask: %s\n\n", inet_ntoa(sin->sin_addr));
        }
    }
    //打印所有非空的网卡名
    for (int i=0; i<sizeof(name)/sizeof(name[0]); i++)
    {
        if(strcmp(name[i].c_str(),""))
        {
            printf("name%d=%s,",i,name[i].c_str());
        }
    }
    printf("\n");
    //从小到大排序
    qsort(name,10,sizeof(name[0]),compare);
    //查找最小网卡对应的ip
    for (int i=0; i<sizeof(name)/sizeof(name[0]); i++)
    {
        if (strcmp(name[i].c_str(),""))
        {
            for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next)
            {
                if(ifa->ifa_addr->sa_family == AF_INET)
                {
                    string ifname(ifa->ifa_name);
                    if (strcmp(name[i].c_str(),ifname.c_str()) == 0)
                    {
                        printf("check name=%s\n",ifname.c_str());
                        sin = (struct sockaddr_in *)ifa->ifa_addr;
                        char *ipAddress = inet_ntoa(sin->sin_addr);
                        //过滤掉local
                        if (strcmp(ipAddress,"127.0.0.1"))
                        {
                            strncpy(ip,ipAddress,len);
                            printf("last use name=%s,ip=%s",name[i].c_str(),ip);
                            freeifaddrs(ifList);
                            return 0;
                        }
                        else
                        {
                            printf("is local, not use");
                        }
                    }
                }
            }
        }
    }

    freeifaddrs(ifList);
    return -1;
}


int main(int argc, char *argv[])
{
    char ip[64];
    if(int ret = getSubnetMask(ip,sizeof(ip)) < 0)
    {
        printf("find ip error\n");
        return -1;
    }
    printf("ip=%s\n",ip);
    return 0;
}