V22.0480-002 Special Topics: Networks

Homework 1: Create a C program that uses gethostbyname() to get the IP addresses of a given machine and print them out.

The gethostbyname() routine takes a host name as a argument and returns a hostent structure. The hostent structure is defined as follows:

struct hostent {
     char *h_name; /* official name of host */
     char **h_aliases; /* alias list */
     int h_addrtype; /* address type */
     int h_length; /* length of address */
     char **h_addr_list; /* list of addresses from name server */
 };

See the manual page for gethostbyname() for more details.

In this assignment, you must write a C program that takes a machine name and prints out the following:

 1. The official name of the machine
 2. All alias names
 3. The IP addresses of the machine

Example:

 $ getaddr cims.nyu.edu

 Official name: cims.nyu.edu
 Aliases: 

             cims 

             www.cims.nyu.edu 

             ftp.cims.nyu.edu

             gopher.cims.nyu.edu
     courant.nyu.edu
     courant
     timehost
 Addresses:
     128.122.80.107

 

$ getaddr www.irs.gov

 Official name: www.omega.fedworld.gov
 Aliases:
 Addresses:

      208.184.22.40

      208.185.132.74

      192.239.92.40

 

Make sure you print out an appropriate error message is the information cannot be obtained! 

Due date: Monday, October  1

Students with last names that start with A through G will send their programs to Cindy Chung (cc462@cs), students with last names that start with H through M will send their programs to Kong Lei (kl384@cs), and students with last names that start with N through Z to Jie Zhang (jz339@nyu.edu)