


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Basic exercises in C and C++ programming languages.
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



In this exercise you are required to understand, modify and write code in C and/or C++. To start this exercise download the starter pack from the following URL:
http://www.cl.cam.ac.uk/teaching/current/CandC++/
This ZIP file should contain the following files:
server.c client.c rfc0791.txt rfc0793.txt message message message message
Important: You should avoid the use of any compiler-specific features in the exercise. Code written for this exercise should conform to ISO9899:1999 (“C99”) or ISO14882:1998 (“C++98”) and use standard libraries. The code should compile and run correctly using gcc or g++ as available on PWF linux. Your code should not generate any warnings, even with all warnings turned on; in other words
gcc -std=c99 -Wall --pedantic sourcefile.c or g++ -std=c++98 -Wall --pedantic sourcefile.cc
should produce no warnings for all source files you submit.
The exercise starter pack contains two computer programs written in C. The first program, server.c transmits data and the second program, client.c receives data. There are a few mistakes in these programs which prevent the code from compiling or functioning correctly. Do the following:
A computer scientist writes a packet inspector to record every bit of data sent by (a corrected version of) server.c on machine A and received by (a corrected version of) client.c on machine B. The recorded data contains all the TCP/IP packets and includes the TCP and IP headers in network byte order together with the actual message payload.
The files rfc0791.txt and rfc0793.txt contain the specification of IP and TCP headers respec- tively. Your attention is drawn in particular to the layout, in bytes, of the contents of the header files as expressed in these documents. For convenience, the layouts are also shown in the Appendix.
The computer scientist invokes the server program on machine A and the client program on machine B once, and generates a log file of all the packets sent between the two machines. The log file contains the raw packet data in the order in which the packets were transmitted on machine A. Therefore the first packet is at the start of the file, and this is immediately followed by data from the second packet, and so on.
An example log file is message1. Your next task is to write a C or C++ program to read in any log file in this format and determine the following facts:
You should save your program into a file called summary.c or summary.cc. Your program should output the above details in the same order as shown above by printing to stdout on a single line, using spaces to separate the fields. For example, if the IP addresses of machines A and B are 192.168.1.1 and 192.168.1.2, the header length field value is 6 , the length of the IP packet is 52 bytes, the value in the TCP header length is 5 , and the total number of packets in the trace is 20 , your program should output:
RFC 791 (http://www.ietf.org/rfc/rfc0791.txt) specifies the layout of packets in the IP Pro- tocol. You may need to read portions of this document in order to complete this exercise. Of particular interest is the position of the packets in the Internet Protocol header:
|Version| IHL |Type of Service| Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
RFC 793 (http://www.ietf.org/rfc/rfc793.html) specifies the layout of packets using the TCP protocol. You may need to read portions of this document in order to complete this exercise. Of particular interest is the position of the key data elements in the Transmission Control Protocol header:
| Source Port | Destination Port | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Sequence Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Acknowledgment Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Data | |U|A|P|R|S|F| | | Offset| Reserved |R|C|S|S|Y|I| Window | | | |G|K|H|T|N|N| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Checksum | Urgent Pointer | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | data | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+