c - 具有指定提示的 getaddrinfo(3)->ai_socktype 不返回 IPv6 地址

标签 c linux unix unix-socket getaddrinfo

假设以下代码模拟了 resolveip 实用程序的基本功能:

#define _POSIX_SOURCE     /* getaddrinfo() */
#include <sys/types.h>    /* getaddrinfo(), struct addrinfo, struct sockaddr */
#include <sys/socket.h>   /* getaddrinfo(), struct addrinfo, struct sockaddr, AF_* */
#include <netdb.h>        /* getaddrinfo(), struct addrinfo, struct sockaddr */
#include <arpa/inet.h>    /* inet_ntop() */
#include <stdio.h>        /* fprintf(), printf(), perror(), stderr */
#include <stdlib.h>       /* EXIT_SUCCESS */

int main(int argc, char** argv) {
  for(int i = 1; i < argc; ++i) { /* For each hostname */

    char* hostname = argv[i];    
    struct addrinfo* res;  /* We retrieve the addresses */
    if(getaddrinfo(hostname, NULL, NULL, &res) == -1) {
      perror("getaddrinfo");
      continue;
    }

    for(; res->ai_next; res = res->ai_next) { /* We print the addresses */
      switch(res->ai_addr->sa_family) {
        case AF_INET: {
          struct in_addr addr = ((struct sockaddr_in*)(res->ai_addr))->sin_addr;
          char buffer[17]; printf("%s: %s\n", hostname, inet_ntop(AF_INET, &addr, buffer, 17)); break;
        } case AF_INET6: {
          struct in6_addr addr = ((struct sockaddr_in6*)(res->ai_addr))->sin6_addr;
          char buffer[40]; printf("%s: %s\n", hostname, inet_ntop(AF_INET6, &addr, buffer, 40)); break;
        } default: {
          fprintf(stderr, "%s: Unknown address family\n", hostname);
        }
      }
    }

    freeaddrinfo(res); /* We release the allocated resources */

  } return EXIT_SUCCESS;
}

上面的代码以 google.com 作为第一个也是唯一的参数调用,将输出类似于:

google.com: 173.194.35.87
google.com: 173.194.35.87
google.com: 173.194.35.87
google.com: 173.194.35.95
google.com: 173.194.35.95
google.com: 173.194.35.95
google.com: 173.194.35.88
google.com: 173.194.35.88
google.com: 173.194.35.88
google.com: 2a00:1450:4008:800::101f
google.com: 2a00:1450:4008:800::101f

假设我们要删除重复的条目。因此,让我们创建一个结构,其中包含有关我们希望检索哪种结果的提示。以下修改不会以任何方式影响输出,因为该结构已按照 getaddrinfo(3) 联机帮助页的要求初始化为默认值:

struct addrinfo hints = {
  .ai_family = AF_UNSPEC,
  .ai_socktype = 0,
  .ai_protocol = 0,
  .ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG)
}; if(getaddrinfo(hostname, NULL, &hints, &res) == -1) {
  perror("getaddrinfo");
  continue;
}

现在让我们通过将 ai_socktype 字段指定为任意值来过滤掉重复的条目:

struct addrinfo hints = {
  .ai_family = AF_UNSPEC,
  .ai_socktype = SOCK_DGRAM,
  .ai_protocol = 0,
  .ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG)
};

唉,我们现在丢失了 IPv6 地址:

google.com: 173.194.35.87
google.com: 173.194.35.95
google.com: 173.194.35.88

现在让我们回到原来的无提示版本:

if(getaddrinfo(hostname, NULL, NULL, &res) == -1) {
  perror("getaddrinfo");
  continue;
}

现在让我们改用手动过滤:

for(; res->ai_next; res = res->ai_next) {
  if(res->ai_socktype != SOCK_DGRAM) continue;
  ...
}

现在一切正常:

google.com: 173.194.35.87
google.com: 173.194.35.95
google.com: 173.194.35.88
google.com: 2a00:1450:4008:801::101f

我很好奇将提示传递给 getaddrinfo(3) 函数与手动过滤返回记录之间的差异从何而来。使用 glibc 2.17 在 linux 内核 3.8.0-32 上测试。

最佳答案

您的 for 循环检查是错误的,您总是跳过最后一个条目 - 在您的情况下恰好是 IPv6 地址。

 for(; res->ai_next; res = res->ai_next) {

需要

 for(; res; res = res->ai_next) { 

关于c - 具有指定提示的 getaddrinfo(3)->ai_socktype 不返回 IPv6 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20248254/

相关文章:

c - fgetc() 如何知道下一个字符的地址?如何检索当前字符地址?

c - ASCII 格式的数据大小

linux - 如何使用 NetCat for Windows 将二进制文件发送到 TCP 连接?

unix - 一个文件中的打印行与另一文件中的模式匹配

c - 打印 UTF-16 字符串

java - 从 jenkins 上传文件进行测试

linux - 主机 SMBus Controller 未启用

c - 无法打开 FIFO 进行写入

linux - Unix Bash - 引用内部函数并传递参数

c - 对数组的 int 操作数进行按位异或运算