CSCB09 Summer Week 10 Lab

In this lab, you will learn and use getaddrinfo() for looking up the IPv4 addresses of a domain name aka host name. You will also finish with freeaddrinfo() for freeing the data structure created by getaddrinfo().

getaddrinfo()

getaddrinfo() takes many parameters because it can do many things; the man page can be overwhelming at first sight. On the bright side, it offers to completely compute all parameters you need for socket(), bind(), and connect().

You should read the man page, but here is an orientation:

int getaddrinfo(const char *host,
                const char *service,
                const struct addrinfo *hints,
                struct addrinfo **res);

In hints, the following 4 fields in struct addrinfo are relevant, and the other fields should be set to 0 (if number field) or NULL (if pointer field):

In the struct addrinfo you receive via res:

Task

Your job is to complete getaddr.c to call getaddrinfo() and then use the provided print_inet4_addr() to print out all IPv4 addresses found.

After that, use freeaddrinfo() to free the linked list. I will use valgrind to check this!

Optional: If getaddrinfo() returns a non-zero number, learn and use gai_strerror() for the corresponding error message, print it to stderr, and exit.

Sample runs and outputs:

$ ./a.out utsc.utoronto.ca
142.1.96.30
$ ./a.out google.com | sort
142.250.139.100
142.250.139.101
142.250.139.102
142.250.139.113
142.250.139.138
142.250.139.139

The second example shows a case of multiple answers. Although the order can be different every time, piping to sort should yield a reproducible result, as shown.