Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 454
gln Avatar asked Apr 27 '26 08:04

gln


1 Answers

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.

like image 109
JeremyP Avatar answered Apr 29 '26 20:04

JeremyP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!