【问题标题】:How to programmatically get IPv4 address of a host with getaddrinfo如何使用 getaddrinfo 以编程方式获取主机的 IPv4 地址
【发布时间】:2022-11-22 19:40:52
【问题描述】:

使用手册中的示例,我试图编写一个简单的程序来获取主机的 IPv4 地址列表:

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main() {
    struct addrinfo hints;
    struct addrinfo *result, *rp;

    /* Obtain address(es) matching host/port */

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET;    /* IPv4 */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;
    hints.ai_protocol = 0;          /* Any protocol */

    int res = getaddrinfo("google.com", NULL, &hints, &result);
    if (res != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
        exit(EXIT_FAILURE);
    }

    /* getaddrinfo() returns a list of address structures.
       Try each address until we successfully connect(2).
       If socket(2) (or connect(2)) fails, we (close the socket
       and) try the next address. */

    for (rp = result; rp != NULL; rp = rp->ai_next) {
        puts(rp->ai_addr->sa_data);
    }

    if (rp == NULL) {
        fprintf(stderr, "No address succeeded\n");
        exit(EXIT_FAILURE);
    }

    return 0;
}

我期望从 for 循环中获取 IP 地址列表。但是,不幸的是,程序输出空行。

怎么了?

【问题讨论】:

    标签: c networking


    【解决方案1】:

    getaddrinfo() 函数返回 struct addrinfo 的链表。每个条目都有一个struct_sockaddr *ai_addr 成员,这是表示网络地址的通用结构。您需要将其转换为特定于协议的结构——在本例中为 struct sockaddr_in,如下所示:

    struct sockaddr_in {
       sa_family_t    sin_family; /* address family: AF_INET */
       in_port_t      sin_port;   /* port in network byte order */
       struct in_addr sin_addr;   /* internet address */
    };
    
    struct in_addr {
       uint32_t       s_addr;     /* address in network byte order */
    };
    

    struct sockaddr_in中,ip地址用32位整数表示;要将其转换为常见的点八位字节格式,我们需要使用 inet_ntoa 函数,使您的代码如下所示:

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int main() {
        struct addrinfo hints;
        struct addrinfo *result, *rp;
    
        /* Obtain address(es) matching host/port */
    
        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = AF_INET;    /* IPv4 */
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = 0;
        hints.ai_protocol = 0;          /* Any protocol */
    
        int res = getaddrinfo("google.com", NULL, &hints, &result);
        if (res != 0) {
            fprintf(stderr, "getaddrinfo: %s
    ", gai_strerror(res));
            exit(EXIT_FAILURE);
        }
    
        /* getaddrinfo() returns a list of address structures.
           Try each address until we successfully connect(2).
           If socket(2) (or connect(2)) fails, we (close the socket
           and) try the next address. */
    
        for (rp = result; rp != NULL; rp = rp->ai_next) {
            printf("%s
    ", inet_ntoa(((struct sockaddr_in*)rp->ai_addr)->sin_addr));
        }
    
        return 0;
    }
    

    请注意,我从末尾删除了 if (rp == NULL) 位,因为在 for 循环之后,rp 始终等于 NULL(这是循环的退出条件)。

    运行上面的代码会产生:

    142.250.80.78
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 2010-09-14
      相关资源
      最近更新 更多