Table of Contents

Programming Assignment 1: Mock Name Server (MNS)

Please check the FAQ section for updates on instructions.

Packet Structure

Please use the following packet structure for your programs. You can essentially use the same code to set the fields as you did in PA0. (The figure provided previously in PA0 pdf was incorrect. That is, it did not match the code which we posted in FAQ for setting the Type/R fields. We have updated the figure, which is posted below). For setting/decoding the type field, R bit and other fields, please refer to the FAQ below.

   1                           2                           3                           4
   0   1   2  3  4   5  6   7  0   1  2   3  4  5   6  7   0  1   2  3   4   5  6   7  0  1   2   3  4  5   6   7  
 |                     Protocol Number                    |              Query Sequence / Ack Num               |
 | Unused       |   Type | R|             Unused          |                           Length                    |
 |                            Data: HostName / IP Address (padded to 32 bit boundary)                           |

Testing your program

You should use the client you coded in PA0 for testing your server. At beginning, you should test the basic lookup function. Once this is working, you will test the server for redirection functionality. We can use this client executable which can handle the redirection responses for testing purposes.

Here you can find sample mapping-file and redirect-file

Send email to the TA (shravan [AT][use “CS640:” in subject] if you have any questions regarding the assignment.

Submission Instructions

What to submit:

You will need to submit the source code along with a Makefile, located in a directory called p1/. Compress the directory into a single tar.gz file and name it p1.tar.gz. Do not submit object files, or compiled executables.

The Makefile should have two rules: clean and all.

clean : will delete previous .o files, executables, etc.

all : should produce a single executable called client.

The TA will run the following sequence of operations to execute and test your code:

tar xvfz p1.tar.gz
cd p1/
make clean
make all
./server -p <port> -f <mapping-file-name> -r <redirect-file-name>

Please test to make sure that these commands can execute in sequence without any intervention.

How to submit:

First add the following line to your .cshrc.local file (located in your home directory):

set path = ($path /s/handin/bin)

Then run the following command at the shell prompt:

source ~/.cshrc.local

Finally, to handin your files, enter the following command:

handin -c cs640-1 -a PA1 -d <directory>

where <directory> is the path to the directory where your tar.gz file is located. Note: handin will go through the specified directory and hand in all the necessary files, so make sure the directory contains only p1.tar.gz file.

Frequently Asked Questions

(1) The man pages for some of the functions needed for the assignment are retrieving documentation for the perl functions first. To change this, please enter the following command at the command prompt:

setenv MANPATH /usr/share/man

(2) For running the client, make sure that you provide the integer value of the request type for the -r option.

(3) Constructing the packet – There are many ways to create a packet. memcpy is one of the safest and easiest ways. Here is a small example how you can use memcpy.

 //2 bytes of protocol
char packet[pktsize]; //You will have to compute the size of the packet beforehand. it will be 8 (i.e. header size) + the size of data field.
unsigned short int n_protocol= htons(640);  // make sure that you convert it into network byte order as the server expects
memcpy(&packet[0], (char *) &n_protocol, sizeof(n_protocol));  //see the man page for memcpy
int offset = sizeof(n_protocol);  //This should be 2 (i.e., 2 bytes or 16 bits)
// 2 bytes of sequence number
short int seq=10;
short int n_seq_num =htons(seq); // in network byte order
memcpy(&packet[offset],(char *) &n_seq_num, sizeof(n_seq_num));
offset += sizeof(n_seq_num);

(4) When you put the hostname in the packet, you should also count the \0 in the hostname for the size of packet.

The following is two simple examples for what the payload should look like and what the length should be

1. The host name is The payload of sending message should look like this: tom3.cs.wisc.edu_XXX where _ is the '\0' and X is zero padding and the length of the entire packet should be 8 (header) + 20 = 28

2. the host name is the payload of sending message should look like this foghorn.cs.wisc.edu_ where _ is '\0'and length should be 8 + 20 =28

(5) Machine IP address: Use ping to find the server's IP address such as ping

(6) Endianness issues: There is no ensured endian for your host. You need to use htons, htonl, ntohs, ntohl to ensure the proper endian for network and local host. For this project, the process should be like this

(7) For the reserve bit and operation type, you need to use bit wise operator:

A. Example to create and R = 1 and Type = 1 (hostname to IP address resolution request from the client)

#define R_MASK      0x01
#define TYPE_MASK   0x0e
// Let us say we want to set the reserve bit and set type to 1
reserve_bit =1;
type = 1;
char bit_data;
memset(&bit_data, 0, 1);  // make it all zeros

// R flag
bit_data = reserve_bit; // reserve bit is set

// Type
char type = 1;  
char data_type =  type << 1;
bit_data = bit_data | data_type;

// Copy into the packet
memcpy(&packet[4], &bit_data, 1);  // offset used should be 4 for packet (Refer to Packet Structure)

B. Decoding:

// Copy the data byte from index 4 of the packet
char recv_data;
memcpy(&recv_data, &packet[4], 1); // byte at offset 4
if((recv_data & R_MASK) > 0)
  reserve_bit = true;
// Get the type
type = (recv_data & TYPE_MASK) >> 1;