I'm having problems with the following piece of code:
#include <errno.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
[...]
struct protoent *tcp;
struct addrinfo hint = {0};
struct addrinfo *addrs;
int status;
tcp = getprotobyname("tcp");
if (!tcp)
return -EINVAL;
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = tcp->p_proto;
// errno is 0
status = getaddrinfo("localhost", "30002", &hint, &addrs);
// errno is 22
if (status)
return -labs(status);
[...]
The problem is that although it works (I'm using this for a TCP communication which indeed is working), it's inconsistent:
man getaddrinfo
says nothing about errno
; instead it returns an error code. Why may it be setting errno
to EINVAL
(22, Invalid argument) internally?