Linux系统调用:getaddrinfo返回-2

发布于 2024-10-06 22:45:06 字数 1577 浏览 0 评论 0原文

我正在使用系统调用 getaddrinfo,它返回 -2。我尝试知道这个错误是什么,并发现这是“名称或服务未知”。 名称 - 这是我的主机名,我确信它是已知的。但服务的编号在每次运行时都会发生变化。我怎么知道我带来了正确的参数?

我的代码:

int GetSockPeerIPs(int sock, AddressList &addresses, int &error,
                  int family, bool zeroport)
{
    struct sockaddr_storage ss;
    socklen_t salen = sizeof(ss);
    struct sockaddr *sa;
    struct addrinfo hints, *paddr, *paddrp;

    sa = (struct sockaddr *)&ss;

    if (getpeername(sock, sa, &salen) != 0) {
        error = errno;
        return -1;
    }

    char hbuf[NI_MAXHOST];
    char pbuf[NI_MAXSERV];
    if (0 != (error = getnameinfo(sa, salen,
                      hbuf, sizeof(hbuf),
                      pbuf, sizeof(pbuf),
                      0))) {
        return -1;
    }

    memset(&hints, 0, sizeof(hints));
    if (ATNetworkTool::AF_XINETX == family) {
        hints.ai_family = PF_UNSPEC;
    } else {
        hints.ai_family = family;
    }
    hints.ai_socktype = SOCK_STREAM;
    if (0 != (error = getaddrinfo(hbuf, pbuf, &hints, &paddrp))) {
        return -1;
    }
    addresses.clear();
    for (paddr = paddrp; paddr; paddr = paddr->ai_next) {
        if (ATNetworkTool::AF_XINETX == family) {
            if (!ATAddress::saIsInet(paddr->ai_addr)) {
                continue;
            }
        }
        if (zeroport) {
            addresses.insert(ATAddress(paddr->ai_addr, 0));
        } else {
            addresses.insert(paddr->ai_addr);
        }
    }
    freeaddrinfo(paddrp);
    return 0;
}

谢谢! 谷氨酸

I'm using the system call getaddrinfo and it return -2. I try to know what is this error and get that ths is "name or service not known".
the name - it is my host name and I'm sure it is known. but the service is a number that is changed from run to run. how can I know that I am bringing the correct parameter?

my code:

int GetSockPeerIPs(int sock, AddressList &addresses, int &error,
                  int family, bool zeroport)
{
    struct sockaddr_storage ss;
    socklen_t salen = sizeof(ss);
    struct sockaddr *sa;
    struct addrinfo hints, *paddr, *paddrp;

    sa = (struct sockaddr *)&ss;

    if (getpeername(sock, sa, &salen) != 0) {
        error = errno;
        return -1;
    }

    char hbuf[NI_MAXHOST];
    char pbuf[NI_MAXSERV];
    if (0 != (error = getnameinfo(sa, salen,
                      hbuf, sizeof(hbuf),
                      pbuf, sizeof(pbuf),
                      0))) {
        return -1;
    }

    memset(&hints, 0, sizeof(hints));
    if (ATNetworkTool::AF_XINETX == family) {
        hints.ai_family = PF_UNSPEC;
    } else {
        hints.ai_family = family;
    }
    hints.ai_socktype = SOCK_STREAM;
    if (0 != (error = getaddrinfo(hbuf, pbuf, &hints, &paddrp))) {
        return -1;
    }
    addresses.clear();
    for (paddr = paddrp; paddr; paddr = paddr->ai_next) {
        if (ATNetworkTool::AF_XINETX == family) {
            if (!ATAddress::saIsInet(paddr->ai_addr)) {
                continue;
            }
        }
        if (zeroport) {
            addresses.insert(ATAddress(paddr->ai_addr, 0));
        } else {
            addresses.insert(paddr->ai_addr);
        }
    }
    freeaddrinfo(paddrp);
    return 0;
}

thanks!
gln

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情痴 2024-10-13 22:45:06

您有一个错误代码。你有没有想过找出它的含义?这次我已经为你做到了。但这是我所做的,以便您下次可以自己查找。

查看 getaddrinfo() 的手册页,我发现它可以返回许多错误代码例如EAI_AGAIN。数值将在头文件中的某个位置定义,所以我这样做了

cd /usr/include
find . -name "*.h" -exec grep -l EAI_AGAIN {} \;

Thisidentified netdb.h。所以我在 vi 中打开它,它是这样说的:

# define EAI_BADFLAGS     -1    /* Invalid value for `ai_flags' field.  */
# define EAI_NONAME       -2    /* NAME or SERVICE is unknown.  */
# define EAI_AGAIN        -3    /* Temporary failure in name resolution.  */
# define EAI_FAIL         -4    /* Non-recoverable failure in name res.  */
# define EAI_FAMILY       -6    /* `ai_family' not supported.  */
# define EAI_SOCKTYPE     -7    /* `ai_socktype' not supported.  */
# define EAI_SERVICE      -8    /* SERVICE not supported for `ai_socktype'.  */
# define EAI_MEMORY       -10   /* Memory allocation failure.  */
# define EAI_SYSTEM       -11   /* System error returned in `errno'.  */
# define EAI_OVERFLOW     -12   /* Argument buffer overflow.  */

所以基本上, getaddrinfo 不知道您传入的名称或服务。如果我是你,我会检查前两个参数是否合理。

You have an error code. Have you thought about finding out what it means? On this occasion I have done it for you. But here's what I did so you can look it up for yourself next time.

Looking at the man page for getaddrinfo() I found it can return a number of error codes e.g. EAI_AGAIN. The numeric values will be defined in a header file somewhere, so I did

cd /usr/include
find . -name "*.h" -exec grep -l EAI_AGAIN {} \;

This identified netdb.h. So I opened that in vi and this is what it said:

# define EAI_BADFLAGS     -1    /* Invalid value for `ai_flags' field.  */
# define EAI_NONAME       -2    /* NAME or SERVICE is unknown.  */
# define EAI_AGAIN        -3    /* Temporary failure in name resolution.  */
# define EAI_FAIL         -4    /* Non-recoverable failure in name res.  */
# define EAI_FAMILY       -6    /* `ai_family' not supported.  */
# define EAI_SOCKTYPE     -7    /* `ai_socktype' not supported.  */
# define EAI_SERVICE      -8    /* SERVICE not supported for `ai_socktype'.  */
# define EAI_MEMORY       -10   /* Memory allocation failure.  */
# define EAI_SYSTEM       -11   /* System error returned in `errno'.  */
# define EAI_OVERFLOW     -12   /* Argument buffer overflow.  */

So basically, the name or service that you are passing in is unknown to getaddrinfo. I'd check to see whether the first two parameters are reasonable, if I were you.

德意的啸 2024-10-13 22:45:06

getaddrinfo 对数据的格式很挑剔,在解析配置文件时,请确保主机名参数中没有多余的空格,通常尾随空格,否则您将收到此错误,在这种情况下,错误是正确的,只是信息不多。

getaddrinfo is picky about the format of the data, in parsing config files make sure you have no extra spaces normally trailing spaces in the host name parameter or you will get this error, in this instance the error is correct, just not very informative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文