Goals:
Collaboration: You will complete this lab in teams of 1-3 of your choice. You may consult me or your classmates with proper attribution.
netserv.c
and netclient.c
to your account and compile them. Review the programs to be
sure you understand them.netserv.
In another, run netclient.
Briefly explain what you see.nslookup
<hostname>,
where hostname is the name of the computer you are working on, to learn
the IP address of that computer..c,
change the definition of IP_ADDRESS so that it is the address of the
computer you are working on. Recompile netclient.c.
netserv and netclient
again. Explain what you see.netclient when netserv
is not running? Try this and explain what you see.netserv and netclient
on any computer, without having to change the IP address and recompile
the client? The way we will do this is by using the getaddrinfo
system call. This function lets us supply a hostname as a
string, and it will resolve that hostname to a result of type struct
sockaddr. The program my-nslookup.c
illustrates the use of getaddrinfo.
Download this program, compile it, run it a few times
supplying different hostnames as arguments, and review the code to
understand how it works..c
so that it takes the hostname as a command-line argument.
Use code from my-nslookup.c to
resolve this hostname to a result of type struct sockaddr, and then use the result
you obtain to connect to this address rather than the hard-coded IP_ADDRESS.wget
program allows you to fetch the contents of a URL and save it to a new
file. If you've never used wget,
try using it to fetch the
file named by http://www.cs.grinnell.edu/~davisjan/csc/213/2006F/index.html. In
this part of the lab, we will build a simple analog to wget.
Our program will take a URL as a command-line argument and
write the response from the web server to STDOUT.
<protocol>://<hostname>/<path>
or <protocol>://<hostname>:<port>/<path>.
The program parseurl.c
illustrates a simple parser for URLs of this form. Download
the
program, compile it, run it on a few different URLs, and review the
code to
understand what it does.GET url HTTP/1.0↵
HOST: hostname↵
↵
When all of the data has
been read, close the socket. (Note that the data
read from the socket will not
end with a null (\0) character.
You'll need to account for this somehow when you are writing the
contents of the buffer to STDOUT.)http://www.cs.grinnell.edu/.
The HTTP
header is separated from the contents of the file by a
blank line. What information do you see in the header?http://www.cs.grinnell.edu/does-not-exist.
How is the HTTP header different from what you saw in step 12?wwwserv.c
and url_lib.c.
Compile
and run wwwserv.c. Point your web
browser (e.g., Firefox) to the url http://localhost:8000/. Review wwwserv.c to understand what you observed.ROOT
constant to specify the directory in which the web server should look
for files. Does this guarantee that a malicious web client cannot
access files outside of the web server's root directory? If so,
explain why. If not, give an example of a request a client could
make to obtain an "unauthorized" file.netstat -u". You will see a list of all open
Internet domain sockets on the computer, such as the following:Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost.localdom:8000 localhost.localdo:42526 TIME_WAIT
wwwserv
accepts a port number as an optional command-line argument. This
will allow you to easily cycle through a few different server port
numbers (e.g., 8000, 8001, 8002, 8003) as you are testing your server.wwwserv.c
to write the actual contents of the file to the connection socket,
rather than "Content of file goes here." To test the server, you
may wish to use the WWW client program you wrote in Part B.fork strategy we discussed in class. Note that the parent process should not
wait for a child to complete its execution before accepting the next
connection. If it waits for each connection to complete before
starting the next, then it's not concurrent!ps -u <username> --forest". Look for your web server process in the results. What is the problem?SIGCHLD
signal, which a child process sends to its parent when it dies.
(How tragic!) Copy the signal handler into your program and
register it using the signal(...) syscall. Verify that it solves the problem observed in step 19. Review the manpage for waitpid and explain why this code solves our problem.void sig_chld(int signo) {
pid_t pid;
int stat;
while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0 ) {
printf("child %d terminated\n", pid);
}
}Parts A & B: Due Friday, 17 November, 2006
Part C: Due Monday, 20 November, 2006
Janet Davis (davisjan@cs.grinnell.edu)
Created November 6, 2006