----------------------- Lab #02 for CST8165 due January 15, 2007 ----------------------- -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Global weight: 1% of your total mark this term. Due date: before 10h00 AM Monday January 15 The deliverables for this exercise are to be submitted online in the Linux Lab T127 using the "cstsubmit" method described in the exercise description, below. No paper; no email; no FTP. Late-submission date: I will accept without penalty exercises that are submitted late but before 12h00 (noon) on Tuesday, January 16. After that late-submission date, the exercise is worth zero marks. Exercises submitted by the *due date* will be marked online and your marks will be sent to you by email after the late-submission date. Exercise Synopsis: Enhance your Lab #1 Makefile and server code. Test it. Where to work: Submissions must make, compile, and run cleanly in the T127 Linux Lab, though you are free to work on them anywhere you like. Recall that most any program or C function has a manual page: RTFM Note that this lab *adds* targets to your existing Makefile. You should not delete any of the existing targets created in previous labs. 1) Add the following variable near the top of the Makefile from Lab #1: CF2 = $(CFLAGS) -E 2) Find the explanation of what the "-E" option does in the gcc man page. Read it. 3) To the bottom of your Makefile, add a new Makefile target "serverX.c" with dependency "server2.c" that runs gcc with the $(CF2) options to pre-process source file server2.c into output source file "serverX.c". 4) Add serverX.c to the list of files to be cleaned in "make clean". 5) Run "make serverX.c" to run the gcc pre-processor create source file "serverX.c" from server2.c. Confirm that all the CFLAGS options are used, plus the new -E option. Confirm that the created file "serverX.c" is C source code. 6) Look at the bottom 60 lines of the serverX.c source file, where the serv_addr.sin_port and htons() code gives "unreachable" warnings. (You saw this htons() warning in the previous lab - it was the only warning you couldn't get rid of.) Note what a mess the htons() code has expanded into after going through the gcc pre-processor! The resulting "IF" statement is the reason this line generates an unreachable code warning. You would never know this unless you looked at the output of the C pre-processor. Experiment temporarily with gcc options to find out which compiler flag is causing the htons() code to be expanded into the "unreachable code" line. (Hint: Removing one gcc option will cause htons() to be left unexpanded and untouched. Which option?) Do not remove the -Wunreachable-code warning as you experiment. 6b) After you have identified the gcc option that causes htons() to be expanded, restore the gcc options to their Lab 1 standard form. Put back the full set of CFLAGS options to gcc. 7) Add a new Makefile target "serverX" that depends on "serverX.c". Your Makefile will now have these five targets: all server2 clean serverX.c serverX 8) Add serverX to the list of files to be cleaned in "make clean". 9) Run "make serverX" to compile and create binary file "serverX". Run "make all" to create binary file "server2". (Confirm that all the CFLAGS options are used in both cases.) 10) Run "cmp -lc server2 serverX" and note the one-byte difference in the two binary files. Why is that byte different? To help understand the output of "cmp -lc", try this: $ echo "one two three" >a $ echo "one abc three" >b $ cmp -lc a b 11) Create and compile this one-line source file: main(){ error("abc"); } Why doesn't the linker give a link error about the missing function named error()? If you deleted the static function error() from your server2.c source file, would the program still compile and link? Explain. 12) Move the code for the error() function out of server2.c into its own source file named "myerror.c". Rename "error()" to "myerror()" everywhere. Create the appropriate matching "myerror.h" file. 13) Fix the Makefile to rebuild server2 if myerror.c or myerror.h are updated. 14) Fix the Makefile to rebuild serverX if myerror.c or myerror.h are updated. The Tests --------- Here are some tests to run to make sure your Makefile is correct: 15) $ make clean - are all the objects created by the Makefile removed? - anything created by the Makefile should be removed - typing "make clean" twice should not generate any error messages 16) $ make clean all ; make all ; make all - is server2 built just once? 17) $ make clean ; make ; make ; make - is server2 built just once? 18) $ make clean all ; sleep 2 ; touch server2.c ; make all ; make all - is server2 built exactly twice? 19) $ make clean all ; sleep 2 ; touch myerror.c ; make all ; make all - is server2 built exactly twice? 20) $ make clean all ; sleep 2 ; touch myerror.h ; make all ; make all - is server2 built exactly twice? 21) $ make clean serverX.c ; make serverX.c - is serverX.c built just once? 22) $ make clean serverX ; make serverX - is serverX.c built from server2.c? - is serverX built just once? 23) $ make clean serverX ; sleep 2 ; touch server2.c ; make serverX serverX - is serverX built exactly twice? 24) $ make clean serverX ; sleep 2 ; touch myerror.c ; make serverX serverX - is serverX built exactly twice? 25) $ make clean serverX ; sleep 2 ; touch myerror.h ; make serverX serverX - is serverX built exactly twice? Testing ------- 26) Start a "script" session (man script) with output file "testing.txt". (Your output file will not contain the full results of your script session until you exit the subshell started by the script command. When you exit the subshell, script will tell you the name of the output file.) 27) Run the above Makefile tests #15-#25. 28) Test your myerror() code by starting your server2 using a privileged port argument number greater than zero and less than 1024 (e.g. 123). Make sure you see the error message: ERROR on binding: Permission denied 29) Run the same server2 netcat tests that you did for Lab #1. 30) Exit the script session subshell. Script will tell you the name of your script output file. 31) In a new file named "README.txt", summarize briefly the results of your testing. If any of your Makefile tests failed, or if the netcat test did not work properly, document which tests failed. If everything worked, say so. 32) Submit the files (see below). No added comments are needed in these source files (yet). Submission ---------- A. At the top of each and every submitted file, as comments, create an Exterior Assignment Submission label following the directions from last week's lab. B. For material you copy from other sources, credit the author and source, following the directions from last week's lab. C. Submit these files for marking as Exercise 02 using the following *single* cstsubmit command line: $ ~alleni/bin/cstsubmit 02 \ README.txt server2.c myerror.c myerror.h testing.txt Makefile See last week's lab for details on using cstsubmit. All file names must be spelled *exactly* as given above. Incorrect submissions are worth zero marks. P.S. Did you spell all the assignment label fields and file names correctly?