Everyone:

Next week, we will discuss system calls involving networking. We will explore using sockets to communicate between clients and servers and eventually create our own HTTP client in the form of gwen.

TL;DR

The focus of this reading is to explore system calls related to network sockets in C.

Readings

The readings for this week are:

  1. Beej's Guide to Network Programming

    You can skip the parts about datagram sockets and slightly advanced techniques.

  2. What is a URL?

Optional References

  1. System Programming Wiki

    This has basic coverage of networking, along with some information about remote procedure calls (RPC) that you can skip.

Quiz

This week, the reading is split into two sections: the first part is a dredd quiz, while the second part involves one C program: ncat.c.

To test the C program, you will need to download the Makefile and test scripts:

$ git checkout master                 # Make sure we are in master branch
$ git pull --rebase                   # Make sure we are up-to-date with GitHub

$ git checkout -b reading13           # Create reading13 branch and check it out

$ mkdir reading13                     # Create reading13 folder

$ cd reading13                        # Go into reading13 folder

# Download Reading 13 Makefile
$ curl -LO https://www3.nd.edu/~pbui/teaching/cse.20289.sp24/static/txt/reading13/Makefile

# Download Starter code
$ curl -LO https://www3.nd.edu/~pbui/teaching/cse.20289.sp24/static/txt/reading13/ncat.c

# Download, build, and execute tests
$ make test

Questions

Record the answers to the following Reading 13 Quiz questions in your reading13 branch:

Programs

Given the provided Makefile and ncat.c, you are to do the following:

  1. Modify Makefile to include a rule for the ncat program. Be sure to use the CC and CFLAGS variables in your rule.

    Once you have a working Makefile, you should be able to use the make command to run your recipes:

    $ make clean                                # Remove targets
    rm -f ncat
    
    $ make                                      # Build targets
    gcc -Wall -g -std=gnu99 -o ncat ncat.c
    
  2. Modify ncat.c so that it uses system calls in C to implement the equivalent Python script:

    #!/usr/bin/env python3
    
    import socket
    import sys
    
    # Parse command line options
    try:
        HOST = sys.argv[1]
        PORT = int(sys.argv[2])
    except IndexError:
        print("Usage: {} HOST PORT".format(sys.argv[0]), file=sys.stderr)
        sys.exit(1)
    
    # Create socket and connect to specified HOST and PORT
    try:
        csocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        csocket.connect((HOST, PORT))
        cstream = csocket.makefile('w')
    except socket.error as e:
        print('Socket Error: {}'.format(e))
        sys.exit(1)
    
    # Read from stdin and write to socket
    for line in sys.stdin:
        cstream.write(line)
    
    # Cleanup
    csocket.close()
    

This is basically a partial implementation of the nc command you have used previously: given a HOST and PORT, the program creates a socket, connects to the server, and then sends data from standard input to the remote server via the socket connection.

Fill in the Blanks

The code has a few ____ placeholders. You will need to replace these placeholders with the appropriate functions and variables to complete the program.

Here is a list of some possible functions and variables you may wish to use:

Once you have a working ncat.c, you should be able to build and run it:

$ make                            # Build ncat program
gcc -Wall -g -std=gnu99 -o ncat ncat.c

$ ./ncat                          # Usage
Usage: ./ncat HOST PORT

$ nc -l 9123 &                    # Start netcat server in background
[1] 12532

$ date | ./ncat localhost 9123    # Send message to server
Connected to localhost:9123
Sun Apr 15 16:50:38 EDT 2024
[1]+  Done                    nc -l -p 9123

$ ./ncat fakehost 9999
Could not look up fakehost:9999: Name or service not known

$ ./ncat localhost 0
Unable to connect to localhost:0: Connection refused

$ make clean                      # Cleanup
rm -f ncat

Submission

To submit you work, follow the same process outlined in Reading 01:

#--------------------------------------------------
# BE SURE TO DO THE PREPARATION STEPS ABOVE
#--------------------------------------------------

$ cd reading13                        # Go into reading13 folder

$ $EDITOR answers.json                # Edit your answers.json file

$ ../.scripts/check.py                # Check reading13 quiz
Checking reading13 quiz ...
      Q1 0.25
      Q2 0.75
   Score 1.00 / 1.00
  Status Success

$ git add answers.json                # Add answers.json to staging area
$ git commit -m "Reading 13: Quiz"    # Commit work

$ $EDITOR ncat.c                      # Edit source code

$ make test                           # Build and Run tests
Checking reading13 ncat ...
 ncat (syscalls)                                              ... Success
 ncat (usage, output)                                         ... Success
 ncat (usage, valgrind)                                       ... Success
 ncat (fakehost 9999, client)                                 ... Success
 ncat (fakehost 9999, valgrind)                               ... Success
 ncat (localhost 0, client)                                   ... Success
 ncat (localhost 0, valgrind)                                 ... Success
 ncat (localhost 9770, client)                                ... Success
 ncat (localhost 9770, server)                                ... Success
 ncat (localhost 9770, valgrind)                              ... Success
 ncat (weasel.h4x0r.space 9910, client)                       ... Success
 ncat (weasel.h4x0r.space 9910, server)                       ... Success
 ncat (weasel.h4x0r.space 9910, valgrind)                     ... Success

   Score 3.00 / 3.00
  Status Success

$ git add Makefile                    # Add Makefile to staging area
$ git add ncat.c                      # Add source code to staging area
$ git commit -m "Reading 13: Code"    # Commit work

$ git push -u origin reading13        # Push branch to GitHub

Pull Request

Remember to create a Pull Request and assign the appropriate TA from the Reading 13 TA List.

DO NOT MERGE your own Pull Request. The TAs use open Pull Requests to keep track of which assignments to grade. Closing them yourself will cause a delay in grading and confuse the TAs.