19.5 Pass-through Monitoring of Single Connections

Team-FLY

This section describes an implementation of a simple pass-through monitor, passmonitor , similar to the tunnel illustrated in Figure 19.4. The passmonitor program takes its listening port number, the destination web server host name and an optional destination web server port number as command-line arguments. If the last argument is omitted, passmonitor assumes that the destination web server uses port 80. The monitor listens at the specified port for TCP connection requests (using the UICI u_accept function). When it accepts a client connection, passmonitor initiates a TCP connection to the destination server (using u_connect ) and calls the tunnel function described below. After control returns from tunnel , passmonitor resumes listening for another client connection request.

The tunnel function, which handles one session between a client and the origin server, has the following prototype.

 int tunnel(int clientfd, int serverfd); 

Here, clientfd is the open file descriptor returned after acceptance of the client's connection request. The serverfd parameter is an open file descriptor for a TCP connection between the monitor and the destination server. The tunnel function forwards all messages received from clientfd to serverfd , and vice versa. If either the client or the destination server closes a connection ( clientfd or serverfd , respectively), tunnel closes its connections and returns the total number of bytes that were forwarded in both directions.

After control returns from tunnel , passmonitor writes status information to standard error, reporting the total number of bytes written for this communication and the time the communication took. The monitor then resumes listening for another client connection request.

To correctly implement passmonitor , you cannot assume that the client and the server strictly alternate responses. The passmonitor program reads from two sources (the client and the server) and must allow for the possibility that either could send next . Use select or poll as in Program 4.13 to monitor the two file descriptors. A simple implementation of tunnel is given in Example 19.14. Be sure to handle all errors returned by library functions. Under what circumstances should passmonitor exit? What other strategies should passmonitor use when errors occur?

Example 19.14

The tunnel function can easily be implemented in terms of the copy2files function of Program 4.13 on page 111.

 int tunnel(int fd1, int fd2) {    int bytescopied;    bytescopied = copy2files(fd1, fd2, fd2, fd1);    close(fd1);    close(fd2);    return bytescopied; } 

Recall that copy2files returns if either side closes a file descriptor.

Exercise 19.15

Use Program 18.5 on page 629 to test passmonitor by having it connect to web servers through passmonitor . Why doesn't passmonitor have to parse the client's request before forwarding it to the destination server?

Answer:

The passmonitor program uses only the destination server that is passed to it on the command line.

Exercise 19.16

Suppose you start passmonitor on machine os1.cs.utsa.edu with the following command.

 passmonitor 15000 www.usp.cs.utsa.edu 

Start client2 on another machine with the following command.

 client2 os1.cs.utsa.edu 15000 

If you then enter the following request (on client2 ), the passmonitor sends the request to port 80 of www.usp.cs.utsa.edu .

 GET <SP> /usp/simple.html <SP> HTTP/1.0 <CRLF> User-Agent:uiciclient <CRLF> <CRLF> 

How does the reply differ from the one received by having client2 connect directly as in Example 19.4?

Answer:

The replies should be the same in the two cases if passmonitor is correct.

Exercise 19.17

Test passmonitor by using a web browser as the client. Start passmonitor as in Exercise 19.16. To access /usp/simple.html , open the URL as follows .

 http://os1.cs.utsa.edu:15000/usp/simple.html 

Notice that the browser treats the host on which passmonitor is running as the origin server with port number 15000. What happens when you don't specify a port number in the URL?

Answer:

The browser makes the connection to port 80 of the host running passmonitor .

Exercise 19.18

Suppose that you are using a browser and have started passmonitor as in Exercise 19.16. What series of connections are initiated when you open the URL as specified in Exercise 19.17?

Answer:

Your browser makes a connection to port 15000 on os1.cs.utsa.edu and sends a request similar to the one in Example 19.4 on page 660. The passmonitor program receives the request, establishes a connection to port 80 on www.usp.cs.utsa.edu , and forwards the browser's request. The passmonitor program returns www.usp.cs.utsa.edu 's response to the browser and closes the connections.

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net