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

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

 

 

 

Name:

 

Login:

 

All questions are of equal weights.

 


Question 1:

 

For a client to connect to a server, the client must know the port number of the server.

Explain how the client in the following program knows the port number of the server?

 

Q1.c

struct sockaddr_in Server;
main(int argc, char *argv[])
{
     int             sd, psd, length;
     struct hostent *hp, *gethostbyname();
     Server.sin_family = AF_INET;
     hp = gethostbyname("localhost");
     bcopy(hp->h_addr, &(Server.sin_addr.s_addr), hp->h_length);
     Server.sin_port = htons(0);
     sd = socket(PF_INET, SOCK_STREAM, 0);

     bind(sd, (SA *) & Server, sizeof(Server));
     length = sizeof(Server);
     getsockname(sd, (SA *) & Server, &length);
     if (fork() == 0) {
          close(sd);
           Clt();
     }
     listen(sd, 0);
     for (;;) {
           psd = accept(sd, 0, 0);
          pause();
     }
}
Clt()
{
     int             csd;
     csd = socket(AF_INET, SOCK_STREAM, 0);
     sleep(1);
     connect(csd, (SA *) & Server, sizeof(Server));
     pause();
}


 

Question 2:

What is the expected output of executing the following program and why?

% Q2Srv

Q2Srv.c

main(int argc, char *argv[])
{
     int             sd, psd;
     struct sockaddr_in Server;
     struct hostent *hp, *gethostbyname();
     int             length;
     char            SrvPort[10];
     char            CltIndex[10];
     int             i;   Server.sin_family = AF_INET;
     hp = gethostbyname("localhost");
     bcopy(hp->h_addr, &(Server.sin_addr.s_addr), hp->h_length);
     Server.sin_port = htons(0);     sd = socket(PF_INET, SOCK_STREAM, 0);

     bind(sd, (SA *) & Server, sizeof(Server));

     length = sizeof(Server);
     getsockname(sd, (SA *) & Server, &length);
     sprintf(SrvPort, "%d", ntohs(Server.sin_port));
     for (i = 0; i <= 1; i++) {
           sprintf(CltIndex, "%d", i);
           if (fork() == 0)
                execl("./Q2Clt", "Q2Clt", CltIndex, SrvPort);
     }

     sleep (1);
     listen(sd, 2);
     for (;;) {
           psd = accept(sd, 0, 0);
     }
}


 

Q2Clt.c

main(int argc, char *argv[])
{
     int             sd;
     struct sockaddr_in server;
     struct hostent *hp, *gethostbyname();

     sd = socket(AF_INET, SOCK_STREAM, 0);

     server.sin_family = AF_INET;
     hp = gethostbyname("localhost");
     bcopy(hp->h_addr, &(server.sin_addr.s_addr), hp->h_length);
     server.sin_port = htons(atoi(argv[2]));
     printf("Clinet %s connecting ... \n", argv[1]);
     sleep(atoi(argv[1]));
     if (connect(sd, (SA *) & server, sizeof(server)) < 0)
           printf("Client %s FAIL to connected\n", argv[1]);
     else
           printf("Client %s IS connected\n", argv[1]);
     pause();
}


Question 3:

Consider the following two programs:

Q3Srv.c

int main(int argc, char *argv[])
{
     int             sd;
     struct sockaddr_in server;
     struct hostent *hp, *gethostbyname();
     char            buf[512];
     int             rc;

     server.sin_family = AF_INET;
     server.sin_port = 1234;
     if (argc == 1) server.sin_addr.s_addr = htonl(INADDR_ANY);
     else if (argc == 2) {
           hp = gethostbyname(argv[1]);
           bcopy(hp->h_addr,&(server.sin_addr.s_addr),hp->h_length);
     }
     sd = socket(AF_INET, SOCK_DGRAM, 0);
     bind(sd, (SA *) & server, sizeof(server));

     for (;;) {
           rc = recv(sd, buf, sizeof(buf), 0);
           buf[rc] = (char) NULL;
           printf("Received: %s\n", buf);
     }
}

Q3Clt.c

main(int argc, char *argv[])
{
     int             sendsock;
     struct sockaddr_in dest;
     char            sendBuf[512];
     int             bytes = 0;
     sendsock = socket(PF_INET, SOCK_DGRAM, 0);
     dest.sin_family = AF_INET;
     dest.sin_port = 1234;
     dest.sin_addr.s_addr = inet_addr(argv[1]);
     while (1) {
      bzero(sendBuf,);
      if ((bytes = read(1, sendBuf, 512)) > 0)
           sendto(sendsock,sendBuf,bytes,0,(SA *)&dest, sizeof(dest));
      else
           exit(0);
     }
}


 

A.

Assume we run the server on host something (128.82.4.210) as follows:

S1: Q3Srv

S2: Q3Srv 127.0.0.1

S3: Q3Srv 128.82.4.210

 

And assume we run the client on the same host something follows:

C1: Q3Clt  127.0.0.1

C2: Q3Clt  128.82.4.210

C3: Q3Clt  224.0.0.1

 

 

Fill the following table with Y or N.

Y: means the server will receive the client messages.

N: means the server will NOT receive the client messages.

 

 

S1

S2

S3

C1

 

 

 

C2

 

 

 

C3

 

 

 

 

B.

Fill the following table with Y or N if the client runs on host somethingels while hile the server runs on something.

 

 

 

 

S1

S2

S3

C1

 

 

 

C2

 

 

 

C3

 

 

 

 

 


 

Question 4:

Consider the following two programs:

Q4Srv.c

main(int argc, char *argv[])
{
      int             sd;
      struct sockaddr_in server;
      struct hostent *hp, *gethostbyname();
      struct sockaddr_in from;
      char            buf[512];
      int             rc;
      int             len;
      server.sin_family = AF_INET;
      server.sin_port = 1234;
      server.sin_addr.s_addr = htonl(INADDR_ANY);
      sd = socket(AF_INET, SOCK_DGRAM, 0);
      reusePort(sd);
      bind(sd, (SA *) & server, sizeof(server));
      while (1) {
            len = sizeof(from);
rc = recvfrom(sd,buf,sizeof(buf),0,(SA *)&from,&len);
            buf[rc] = NULL;
            printf("Received: %s\n", buf);
            sendto(sd, buf, rc, 0, (SA *) & from, sizeof(from));
      }
}
Q4Clt.c

main(int argc, char *argv[])
{
      int             sendsock;
      struct sockaddr_in dest;
      struct sockaddr_in server;
      char            sendBuf[512];
      char            buf[512];
      int             bytes;
      int             rc;
      dest.sin_family = AF_INET;
      dest.sin_port = 1234;
      dest.sin_addr.s_addr = inet_addr(argv[1]);
      server.sin_family = AF_INET;
      server.sin_port = 1234;
      server.sin_addr.s_addr = htonl(INADDR_ANY);
      sendsock = socket(PF_INET, SOCK_DGRAM, 0);
      reusePort(sendsock);
      bind(sendsock, (SA *) & server, sizeof(server));
      sendto(sendsock, “HI”, 2, 0, (SA *) & dest, sizeof(dest));
      rc = recv(sendsock, buf, sizeof(buf), 0);
      buf[rc] = NULL;
      printf("Received Back: %s\n", buf);

}


 

Explain the behavior/outcome of the following scenarios:

A.    Executing both the server and the client at the same host something (128.82.4.210):

1.      Run the server then the client as follows:

something % Q4Srv

something % Q4Clt 128.82.4.210

 

 

 

 

 

 

 

2.      Run the client then the server and follows:

something % Q4Clt 128.82.4.210

something % Q4Srv

 

 

 

 

 

 

 

 

B.     Executing  the server on host somethingelse (128.82.4.120) and the client on host something.

1.      Run the server then the client as follows:

somethingelse  % Q4Srv

something        % Q4Clt 128.82.4.120

 

 

 

 

 

 

 

 

2.      Run the client  then the server  as follows:

something        % Q4Clt 128.82.4.120

somethingelse  % Q4Srv

 


 

Question 5:

A.    How to use tcpdump to:

    1. Capture the IP packets with DF flag  on.

 

 

 

 

 

 

 

 

    1. Capture the TCP segments with RST flag on.

 

 

 

 

 

 

 

 

 

 

    1. Capture the UDP packets with no checksum.

 

 

B.     How to proof without any doubt that a tcp connection has been terminated via simulations close?