<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

CS 779/879
Design of Network Protocols
Spring 2005
Midterm Exam
Time 2 & 1/2 hours
Open Book & Notes

 

 

 

Name: 

                            
Unix login:

 

NOTE: In order to make the programs short, you may not write any include, comments or non-essential error statements.

The expected language is C/C++, however you may use any other language such as java if it can do the required task.

 


Question 1: (35  points)

 

1.     Write a client program called TCPattack with the following syntax:

 

TCPattack <host>

 

The program test each TCP port P on <host> and if it can connect to port P, then attacks the host by opening as many TCP connections as it can possibly have to P until it can not make any further connections to P.  The program displays the total number of successful TCP connections to each port P.

 

Solution



Question 2: (30 points)

 

  Consider the following server programs:

1.     TCPServer: create a TCP server a server socket S and bind to a port P=1234

2.     ReuseTCPServer: same as 1 but it call the reusePort function listed below on S.

3.     UDPServer:  same as 1 but uses UDP.

4.     ReuseUDPServer: same as 2 but uses UDP.

 
void reusePort(int s)
{
  int one=1;
  
  if ( setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *) &one,sizeof(one)) == -1 )
  {
    printf("error in setsockopt,SO_REUSEPORT \n");
    exit(-1);
  }
}
 

 

Explain if it is possible to run the programs as specified, assuming port 1234 is not used by any other programs running on isis and cash.

 

 1.      isis % TCPServer &

cash % TCPServer

 

Possible     Not-Possible

Why?

 

 

 2.      isis % TCPServer &

isis % TCPServer

 

Possible     Not-Possible

Why?


 

3.       isis % TCPServer &

isis % ReuseTCPServer

 

Possible     Not-Possible

Why?

 

 

4.       isis % ReuseTCPServer &

isis % TCPServer

 

Possible     Not-Possible

Why?

 

 

5.       isis % TCPServer &

isis % UDPServer

 

Possible     Not-Possible

Why?

 

 

6.       isis % UDServer &

isis % TCPServer

 

Possible     Not-Possible

Why?


 

7.       isis % UDPServer &

isis % UDPServer

 

Possible     Not-Possible

                   Why?

 

 

8.       isis % UDPServer &

isis % ReuseUDPServer

 

Possible     Not-Possible

                   Why?

 

 

9.       isis % ReuseUDPServer &

isis % UDPServer

 

Possible     Not-Possible

                   Why?

 

 

 

10.     isis % ReuseUDPServer &

isis % ReuseUDPServer

 

Possible     Not-Possible

                   Why?


 

Question 3: (35  points)

 

The following is an example of select statement code (explained in our lecture: Multiplexing using select and listed in chapter 6 of textbook).

Modify this code (show all modifications and do not recopy the code) to such that:

1.     Any message received from a client is sent to all other clients.

2.     The program exits if no activity occurs for a period that exceeds 5 minutes.

 

#include       "unp.h"
 
int
main(int argc, char **argv)
{
        int                            i, maxi, maxfd, listenfd, connfd, sockfd;
        int                            nready, client[FD_SETSIZE];
        ssize_t                        n;
        fd_set                         rset, allset;
        char                           line[MAXLINE];
        socklen_t                      clilen;
        struct sockaddr_in             cliaddr, servaddr;
 
 
 
 
 
 

 
 
        listenfd = Socket(AF_INET, SOCK_STREAM, 0);
 
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family      = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port        = htons(SERV_PORT);
 
        Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
 
        Listen(listenfd, LISTENQ);
 
        maxfd = listenfd;                     /* initialize */
        maxi = -1;                            /* index into client[] array */
        for (i = 0; i < FD_SETSIZE; i++)
               client[i] = -1;                /* -1 indicates available entry */
        FD_ZERO(&allset);
        FD_SET(listenfd, &allset);
 
        for ( ; ; ) {
               rset = allset;         /* structure assignment */
 
 
 
 
               nready = Select(maxfd+1, &rset, NULL, NULL, NULL);
 
 
 
 
 
               if (FD_ISSET(listenfd, &rset)) {      /* new client connection */
                       clilen = sizeof(cliaddr);
                       connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
 
                       printf("new client: %s, port %d\n",
                                      Inet_ntop(AF_INET, &cliaddr.sin_addr, 4, NULL),
                                      ntohs(cliaddr.sin_port));
 
 
                       for (i = 0; i < FD_SETSIZE; i++)
                               if (client[i] < 0) {
                                      client[i] = connfd;    /* save descriptor */
                                      break;
                               }
                       if (i == FD_SETSIZE)
                               err_quit("too many clients");
 
                       FD_SET(connfd, &allset);       /* add new descriptor to set */
                       if (connfd > maxfd)
                               maxfd = connfd;        /* for select */
                       if (i > maxi)
                               maxi = i;              /* max index in client[] array */
 
                       if (--nready <= 0)
                               continue;              /* no more readable descriptors */
               }
 
               for (i = 0; i <= maxi; i++) {         /* check all clients for data */
                       if ( (sockfd = client[i]) < 0)
                               continue;
                       if (FD_ISSET(sockfd, &rset)) {
                               if ( (n = Readline(sockfd, line, MAXLINE)) == 0) {
                                                     /*connection closed by client */
                                      Close(sockfd);
                                      FD_CLR(sockfd, &allset);
                                      client[i] = -1;
                               } else
                                      Writen(sockfd, line, n);
 
                               if (--nready <= 0)
                                      break;         /* no more readable descriptors */
                       }
               }
        }
}