代码之家  ›  专栏  ›  技术社区  ›  leeeroy

获取Linux上接口的IP地址

  •  59
  • leeeroy  · 技术社区  · 16 年前

    我怎么才能拿到 IPv4 从C代码到Linux接口的地址?

    例如,我想得到分配给eth0的IP地址(如果有的话)。

    5 回复  |  直到 7 年前
        1
  •  74
  •   Christian Ternus pachonjcl    12 年前

    试试这个:

    #include <stdio.h>
    #include <unistd.h>
    #include <string.h> /* for strncpy */
    
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/ioctl.h>
    #include <netinet/in.h>
    #include <net/if.h>
    #include <arpa/inet.h>
    
    int
    main()
    {
     int fd;
     struct ifreq ifr;
    
     fd = socket(AF_INET, SOCK_DGRAM, 0);
    
     /* I want to get an IPv4 IP address */
     ifr.ifr_addr.sa_family = AF_INET;
    
     /* I want IP address attached to "eth0" */
     strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
    
     ioctl(fd, SIOCGIFADDR, &ifr);
    
     close(fd);
    
     /* display result */
     printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
    
     return 0;
    }
    

    代码示例取自 here .

        2
  •  38
  •   Duck    16 年前

    除了ioctl()方法,filip演示了您可以使用 getifaddrs() . 在手册页的底部有一个示例程序。

        3
  •  22
  •   Craig McQueen Dr. Watson    12 年前

    如果要查找特定接口的地址(IPv4),请说 WLAN0 然后 尝试使用此代码 getifaddrs() :

    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <ifaddrs.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    int main(int argc, char *argv[])
    {
        struct ifaddrs *ifaddr, *ifa;
        int family, s;
        char host[NI_MAXHOST];
    
        if (getifaddrs(&ifaddr) == -1) 
        {
            perror("getifaddrs");
            exit(EXIT_FAILURE);
        }
    
    
        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) 
        {
            if (ifa->ifa_addr == NULL)
                continue;  
    
            s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
    
            if((strcmp(ifa->ifa_name,"wlan0")==0)&&(ifa->ifa_addr->sa_family==AF_INET))
            {
                if (s != 0)
                {
                    printf("getnameinfo() failed: %s\n", gai_strerror(s));
                    exit(EXIT_FAILURE);
                }
                printf("\tInterface : <%s>\n",ifa->ifa_name );
                printf("\t  Address : <%s>\n", host); 
            }
        }
    
        freeifaddrs(ifaddr);
        exit(EXIT_SUCCESS);
    }
    

    你可以代替 无线局域网 具有 种族歧视 对于以太网和 用于本地环回。

    数据结构的结构和详细解释 可以找到用过的 here .

    要了解有关C中链接列表的更多信息,请执行以下操作: page 将是一个很好的起点。

        4
  •  2
  •   ingconti    9 年前

    我的2分:即使iOS:

    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <ifaddrs.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        showIP();
    }
    
    
    
    void showIP()
    {
        struct ifaddrs *ifaddr, *ifa;
        int family, s;
        char host[NI_MAXHOST];
    
        if (getifaddrs(&ifaddr) == -1)
        {
            perror("getifaddrs");
            exit(EXIT_FAILURE);
        }
    
    
        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
        {
            if (ifa->ifa_addr == NULL)
                continue;
    
            s=getnameinfo(ifa->ifa_addr,sizeof(struct sockaddr_in),host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
    
            if( /*(strcmp(ifa->ifa_name,"wlan0")==0)&&( */ ifa->ifa_addr->sa_family==AF_INET) // )
            {
                if (s != 0)
                {
                    printf("getnameinfo() failed: %s\n", gai_strerror(s));
                    exit(EXIT_FAILURE);
                }
                printf("\tInterface : <%s>\n",ifa->ifa_name );
                printf("\t  Address : <%s>\n", host);
            }
        }
    
        freeifaddrs(ifaddr);
    }
    
    
    @end
    

    我只是删除了对wlan0的测试来查看数据。 PS您可以删除“家庭”

        5
  •  2
  •   eshenhu    8 年前

    如果不介意二进制大小,可以使用iproute2作为库。

    iproute2-as-lib

    赞成的意见:

    • 不需要编写套接字层代码。
    • 可以获得更多关于网络接口的信息。与iproute2工具的功能相同。
    • 简单的API接口。

    欺骗:

    • iproute2作为lib库的大小很大。~500 kb。