Might be a dumb question, but how exactly are you going to collect the
CNAME records? I thought it is not possible to look them up based on
the
primary name of the machine or its IP address only.
It's not necessarily possible! There are many cases where it's not
possible at all. But in the nice cases, especially those where you have
some information about the remote system's DNS, you could.
The way the OP worded the assignment, it sounds like a case of a
clueless teacher sharing his wealth of ignorance with a new
generation.
In the real world, if you've already got a connection, you no longer
care about DNS. It doesn't matter how you addressed the host, once
you've reached it. Maybe there are situations, e.g., security related,
where you want a reverse-lookup to some canonical resolution, but in the
general case, since the mapping is arbitrary and can be private, there's
no really good way to do what the OP wants to do. I would be willing to
give an ear to someone who thinks there's a scenario where this is
necessary, or even workable.
You want *all* the aliases of a host? I doubt your program is able to
discern that I call my workstation "broken piece of crap", or more to
the point, that I have private DNS in my lab for naming hosts that have
totally different names to the public.