getaddrinfo return -11(EAI_SYSTEM)

getaddrinfo return -11(EAI_SYSTEM)

getaddrinfo 函数总是 return -11(EAI_SYSTEM),即使我在嵌入式 linux 上键入“google.com”也是如此。但是 nslookup 命令运行良好。示例代码没有问题,因为它适用于 Ubuntu。所以我认为这是 linux 系统的问题。我还检查了 errno,它 returns“设备或资源繁忙”。

这是示例代码

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

int
lookup_host (const char *host)
{
  struct addrinfo hints, *res, *result;
  int errcode;
  char addrstr[100];
  void *ptr;

  memset (&hints, 0, sizeof (hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags |= AI_CANONNAME;

  errcode = getaddrinfo (host, NULL, &hints, &result);
  if (errcode != 0)
    {
        fprintf(stderr, "getaddrinfo: %d\n", errcode);
      return -1;
    }
  
  res = result;

  printf ("Host: %s\n", host);
  while (res)
    {
      inet_ntop (res->ai_family, res->ai_addr->sa_data, addrstr, 100);

      switch (res->ai_family)
        {
        case AF_INET:
          ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
          break;
        case AF_INET6:
          ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
          break;
        }
      inet_ntop (res->ai_family, ptr, addrstr, 100);
      printf ("IPv%d address: %s (%s)\n", res->ai_family == PF_INET6 ? 6 : 4,
              addrstr, res->ai_canonname);
      res = res->ai_next;
    }
  
  freeaddrinfo(result);

  return 0;
}

int main (void)
{
  char inbuf[256];
  int len;
  do {
    bzero(inbuf, 256);
    printf("Type domain name:\n");
    fgets(inbuf, 256, stdin);
    len = strlen(inbuf);
    inbuf[len-1] = '[=12=]';
    if(strlen(inbuf) > 0)
      lookup_host (inbuf);
    else
      return EXIT_SUCCESS;
  } while(1);
}

结果

# ./getaddrinfo 
Type domain name:
google.com
getaddrinfo: -11

我想通了。就我而言,我的嵌入式系统中没有关于 nss 的库。放好后就可以了