开发者

Linux system call: getaddrinfo return -2

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜