Try getaddrinfo(3).