The definition of getaddrinfo() is:
int getaddrinfo(const char *node, // e.g. "www.example.com" or IP
const char *service, // e.g. "http" or port number
const struct addrinfo *hints,
struct addrinfo **res);
Per Beej's site it says:
"You give this function three input parameters, and it gives you a pointer to a linked-list, res, of results."
In terms of usage it is:
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
The actual return value "rv" is actually never used. But the results are taken from the fourth input "res" or "servinfo" in this example. I understand that a linked list is involved but it is not clear to me why the functions return does not act in the normal way. Why isn't "rv" an array that is iterated through to get the addresses? Why is the input also returning the results?