Lab 5: Network Programming

In this lab, you'll use functions that are common for client-side network programming. Section 4.4 describes many of the functions and data structures that you will need to use. In addition to general I/O functions (such as close(), read(), and write()), you should also consult the C documentation as needed for the following functions:

  • connect(), freeaddrinfo(), getaddrinfo(), inet_ntop(), socket()

This lab also relies on correct use of multiple C structs that are central to socket programming. Consult Chapter 4 of the textbook as needed to distinguish and use the following:

  • struct addrinfo, struct sockaddr, struct sockaddr_in, struct sockaddr_in6

Preliminaries

Set up a bare repository on stu.cs.jmu.edu based on the instructions in the CS 361 Submission Procedures, using the name lab5-net.git.


Implementation requirements: Gathering and interpreting network information

Make the following edits in client.c and main.c. This portion of the lab is focused on providing the basic functionality of the nslookup utility. After completing this implementation, you should compare your output with that of nslookup.

  1. Implementation addr_string() to convert an IP address into the correct format (IPv4 dotted decimal vs. IPv6 colon hexademical). If the server parameter is NULL, return no address information. All returned strings must be dynamically allocated (caller will free() them).
  2. Use addr_string() as part of the implementation of serv_string() to display more information about the passed server. Where addr_string() only formats the address, serv_string() also displays information about the transport and network layer protocols.
  3. Implementat get_server_list() to use DNS to resolve a human-readable hostname. Note that the proto string can be a name (such as http) or a number formatted as a string (such as 80), but the logic of your implementation should not need to examine that parameter explicitly.
  4. Complete the designated portion of main() to support command-line queries of servers. If the protocol is 53 (DNS) or 67 (DHCP), use UDP instead of TCP.
The following steps are extra practice for this lab and are NOT required. They will help prepare you for next week's lab (and the project), however, so you are encouraged to complete them. Completing these steps will pass the EXTRA integration tests.

Implement the web() function in client.c to create a socket and connect to an HTTP server, based on the following requirements:

  1. After opening the socket and connecting, send the message GET foo HTTP/1.0 where foo is the file specified in the arguments. Recall that HTTP requires all lines end in CRLF (\r\n) and the header ends with a blank line.
  2. Read the response, printing the first line (should be the HTTP status code) and the value set in the Content-Type header line. This will require looking through multiple lines (you can use strtok() to split strings). Make sure to strip the CRLF from both the returned result and type strings.
  3. Return NULL if any errors occur.
  4. Make sure to clean up any resources, such as dynamically allocated data or open files (sockets).


James Madison University logo


© 2011-2024 Michael S. Kirkpatrick.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.