Getaddrinfo - Error: Success

I am very confused.

I use getaddrinfo to get the address information for this web site.

In this case, I used www.cmu.edu .

My code worked for a while, but then it stopped.

The odd part is the fact that I clearly come up with an error, but when the error code is printed, it says "success."

Here are the relevant bits of code:

 struct addrinfo *res = NULL; struct addrinfo hint; memset(&hint, 0, sizeof(hint)); hint.ai_family = AF_UNSPEC; hint.ai_socktype = SOCK_DGRAM; hint.ai_flags = 0; hint.ai_protocol = 17; if (getaddrinfo(host, portNo, &hint, &res)) { perror("getaddrinfo"); return 0; } 

Host and portNo are strings containing the host (in this case, " www.cmu.edu ") and the port (in this case, "80").

They definitely contain the right thing, no extra spaces or anything like that.

Edit: Thanks everyone! I at least have a corresponding error message, although I still don't know why everything stopped working. Error message:

 Servname not supported for ai_socktype 

I examined the possible causes of this error, and I did not find anything. As I said, this code worked earlier and stopped without changing anything. I realized that this is the port number that I used, but I changed it several times and did not change anything.

Any insight? I am not tied to a port number or anything other than a host. I'm just trying to make it work.

+4
source share
1 answer

For reasons that undoubtedly made sense at the time, 1 getaddrinfo does not report most errors through errno , which means that perror usually useless. You should also check its return value. I crib from wikipedia :

 err = getaddrinfo("www.example.com", NULL, NULL, &result); if (err) { if (err == EAI_SYSTEM) fprintf(stderr, "looking up www.example.com: %s\n", strerror(errno)); else fprintf(stderr, "looking up www.example.com: %s\n", gai_strerror(err)); return -1; } 

By the way, keep in mind that there is no consensus between the realities as to what will happen if you try to find a domain name that does not exist or does not have A or AAAA records. You can get any of EAI_NONAME , EAI_NODATA , EAI_FAIL or EAI_SYSTEM , or you can succeed, but with result set to either NULL or an empty struct addrinfo . Hooray. (See https://sourceware.org/glibc/wiki/NameResolver for more on this.)

1 Many of the new POSIX APIs are trying to move away from errno , which is an abstract good idea, but in practice it turns out to be a headache, because now you have to know which return values โ€‹โ€‹of functions are more complicated than just 0 / success, -1 / error .

+8
source

Source: https://habr.com/ru/post/1493985/


All Articles