Tutorial 0 - Networking tools

Overview

One learning goal of this course is to introduce you to the basics of writing programs that make use of computer networking. Achieving this requires two or more programs to communicate via a network. This implies some sort of communication connection between the programs (i.e. processes) and the need to exchange data between the two. The rules for the exchange of this data are a protocol. Determining whether or not your program is compliant with a protocol can be challenging and it can be helpful to see exactly what your program is sending and how it responds to the data your program receives. The purpose of this tutorial is to introduce you to a collection of tools that you can use to explore the Internet.

Our first tool is netcat, a tool that you can use to explore how some text-based protocols operate. This command can be called either netcat or nc on your machine.

Anciently, there was another command called telnet that is somewhat similar, but it is insecure and should not used anymore. The Telnet protocol is specified in RFC 854 and tends to work the same on all machines. However, there is no standard implementation of netcat and there are some differences between the versions of netcat on Unix, Macs and Windows, where you may need to install it.

Note: There is no netcat on Windows 10.

Preparation

You will be using netcat so you will need a version of netcat that works on your machine. If you are using Windows 10 there is the option for you to use WSL (Windows Subsystem for Linux) on your machine. You will find instructions for installing WSL online (MS Doc for installing WSL). This takes some time. This is the best way to run netcat under Windows. It is possible to download and install open-source versions for netcat in windows itself, BUT these may not be secure and can trigger your anti-virus software.

On a mac, if you haven’t already, you likely will want to look into home-brew for Unix applications. There should already be an nc (netcat) on your mac, but you can install a nicer version of netcat (also called nc) using home-brew. The home-brew one provides more flexibility and control.

Please make sure you have all the necessary software. If nothing works for you there is also the option of signing into one of the department machines and using netcat on that machine.

The transport protocol you will use for the first assignment is TCP. We will first use netcat to create and play with some TCP connections. TCP first creates a connection before it sends data. A TCP connection allows for two-way connection-oriented communication between two processes.

Exercise 1 – online dictionaries

In the following exercise we are going to connect to a dictionary service and try out a few different commands. The dictionary service is defined in RFC 2229 (https://tools.ietf.org/html/rfc2229). By default the dictionary service runs on port 2628. Try using netcat to connect to host dict.org, port 2628.

netcat dict.org 2628

Use the “help” command to discover what other commands the dictionary server responds to.

How many dictionaries does the server support?

What command would you use to discover the definition of the word “pemican”?

How many different dictionaries contain a definition of the word “protocol”?

What command would you use to find all the words that “sound like” the word “orange”?

Exercise 2 – connecting to the FTP service

The File Transfer Protocol (FTP) was one of the first widely used services on the Internet to transfer or download files from one machine to a client machine. There used to be many so called anonymous FTP servers but they are not as common any more because they can be a security risk. Today sFTP (secure FTP) is often used instead and the service is seldom left open. Nevertheless, there are still some open FTP servers out there (the UBC CS department hosts one of them).

Connect to host ftp.cs.ubc.ca, port 21, and type the following, noting the replies that the server gives you:


USER anonymous
PASS anonymous
PWD
PASV

at this point you will receive a sequence of 6 numbers (a,b,c,d,e,f). Without closing the connection, compute the next connection that you will be making. The host will be: a.b.c.d (based on the received numbers), and to the port will be the result of computing (e*256+f). For example, if you receive (128,10,32,41,10,8), use 128.10.32.41 as the host, and 10*256+8=2568 as the port.

Now open up a new shell and again use netcat to connect to the IP address and port you have calculated from the 6 numbers you received after the “PASV” command. Then return to the original terminal and type:


list
quit

If you see a list of files come out in the other terminal then you have managed to connect to the connection for data from the FTP server.

Exercise 3 – connecting to a web server

Try the following command to connect to a web-server on the standard port 80.


netcat example.com 80

To close a netcat session, simply use Ctrl-D (to close cleanly) or Ctrl-C (to close abruptly). You will probably note that unlike the previous connections, this time there is no immediate response. Unlike the previous two protocols, web servers silently wait for more information from the client before they respond in any way. If you type some input, like the word “hello”, the server will then respond (with an error message, because it isn’t expecting that response). Try it. There is one additional complication about clients and servers. How can a server recognize the end of a line? One simple way, but not the only way, is for the client and server to have a special end-of-line character (or in this case end-of-message) which is sent whenever you hit return and is used by the server on the other side to terminate the message. Unfortunately the Unix world and Windows world never quite agreed on how to terminate messages. In Unix “\n” is used while Windows uses two characters “\r\n”. Most protocols expect “\r\n” but some servers will allow you get away with just “\n”. You are best to always send the “\r\n” (also known as CRLF). On the undergraduate machines netcat does not send a “\r” carriage return by default. Use the “-C” (capital-C) flag to tell nc to send a carriage return along with each newline (“\n”). You will also need to pay attention to the capitalization of the information you send to the server. Connect to the web server again, and then provide the following two lines of input, followed by a blank line:

GET / HTTP/1.0
Host: example.org

Requests to an HTTP server can be (almost) arbitrarily long, so you will need to send two CRLFs in a row (a blank line) in order for the HTTP server to know that the header is finished and it should return data.

Exercise 4 – Other useful network tools

There are a number of other commands that can be used to explore the Internet. Figure out what each of the following commands do, and use the command to discover something about the Internet that you find interesting.

Exercise 5 – Telling a few Knock Knock jokes… Socket programming in Java

Open the Java tutorial on Sockets (https://docs.oracle.com/javase/tutorial/networking/sockets/). Read the entire tutorial, and then download and run the Knock Knock server and client code. Play around with the client, then modify the protocol code to add additional knock-knock jokes (just google knock knock jokes). After doing it, modify the client to always send the correct response to the server, without getting user input (i.e., always send Who’s there and whatever else is needed to keep the connection running). The client should repeat this process 10 times.