Computer Systems: A Programmer's Perspective (3rd Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
3rd Edition
ISBN: 9780134092669
Author: Bryant, Randal E. Bryant, David R. O'Hallaron, David R., Randal E.; O'Hallaron, Bryant/O'hallaron
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 12, Problem 12.39HW

A.

Explanation of Solution

C code:

//Include Header file

#include <stdio.h>

#include "csapp.h"

//Define the maximum entry for blocked list

#define MAXIMUMSIZE 100

//Function declaration

int divideURI(char *URIName, char *hostName, char *portNumber, char *pName);

void parseBlockList(char *fName, char list[MAXIMUMSIZE][MAXLINE], int limit);

int blockedURIList(char *URIName, char list[MAXIMUMSIZE][MAXLINE]);

//Main function

int main(int argc, char **argv)

{

  //Declare required variable

  int i, listenfd, connfd;

  int clientfd;

  //Create socket

  socklen_t clientlen;

  //Create structure for client address

  struct sockaddr_storage clientaddr;

  //Create rio function

  rio_t cRio, sRio;

  char cbuffer[MAXLINE], sbuffer[MAXLINE];

  ssize_t ssn, ccn;

char methodName[MAXLINE], URIName[MAXLINE], versionNo[MAXLINE];

char hostName[MAXLINE], portNumber[MAXLINE], pName[MAXLINE];

  char block_list[MAXIMUMSIZE][MAXLINE];

  int logFileDes;

  char logFileBuffer[MAXLINE];

  //Check command line arguments

  if (argc != 2)

  {

    //Display message

    fprintf(stderr, "usage: %s <port>\n", argv[0]);

fprintf(stderr, "use default portNumber 5000\n");

    listenfd = Open_listenfd("5000");

  }

  else

  {

    listenfd = Open_listenfd(argv[1]);

  }

  //Open log files

logFileDes = Open("log.list", O_WRONLY | O_APPEND, 0);

  //Set memory for block list

  memset(block_list, '\0', MAXLINE * MAXIMUMSIZE);

  //Call function for parse file

parseBlockList("block.list", block_list, MAXIMUMSIZE);

  //Check condition

  while (1)

  {

    /* wait for connection as a server */

    clientlen = sizeof(struct sockaddr_storage);

    //Call accept method

connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen);

    Rio_readinitb(&sRio, connfd);

    /* Check URI Name full path */

    if (!Rio_readlineb(&sRio, sbuffer, MAXLINE))

    {

      Close(connfd);

      continue;

    }

    //Display the uri path

sscanf(sbuffer, "%s %s %s", methodName, URIName, versionNo);

    /* Check blocked uri */

    if (blockedURIList(URIName, block_list))

    {

      printf("%s is blocked\n", URIName);

      Close(connfd);

      continue;

    }

    //Display the visit uri

sprintf(logFileBuffer, "visit url: %s\n", URIName);

Write(logFileDes, logFileBuffer, strlen(logFileBuffer));

    memset(hostName, '\0', MAXLINE);

    memset(portNumber, '\0', MAXLINE);

    memset(pName, '\0', MAXLINE);

    //Declare variable

    int res;

    /* Check if the given uri is separate */

if ((res = divideURI(URIName, hostName, portNumber, pName)) == -1)

    {

fprintf(stderr, "is not http protocol\n");

      Close(connfd);

      continue;

    }

    else if (res == 0)

    {

fprintf(stderr, "is not a abslute request path\n");

      Close(connfd);

      continue;

    }

    //Connect server as a client

    clientfd = Open_clientfd(hostName, portNumber);

    Rio_readinitb(&cRio, clientfd);

    //Send the first request

sprintf(sbuffer, "%s %s %s\n", methodName, pName, versionNo);

    Rio_writen(clientfd, sbuffer, strlen(sbuffer));

    printf("%s", sbuffer);

    do

    {

      /* Call next http requests */

ssn = Rio_readlineb(&sRio, sbuffer, MAXLINE);

      printf("%s", sbuffer);

      Rio_writen(clientfd, sbuffer, ssn);

    }

    while(strcmp(sbuffer, "\r\n"));

    //For server send reply back

while ((ccn = Rio_readlineb(&cRio, cbuffer, MAXLINE)) != 0)

      Rio_writen(connfd, cbu...

B.

Explanation of Solution

C code:

#include <stdio.h>

#include "csapp.h"

//Define the maximum entry for blocked list

#define MAXIMUMSIZE 100

//Function declaration

int divideURI(char *URIName, char *hostName, char *portName, char *pathName);

void parseBlockList(char *fName, char list[MAXIMUMSIZE][MAXLINE], int limit);

int blockedURIList(char *URIName, char list[MAXIMUMSIZE][MAXLINE]);

void *proxyThreadFunction(void *vargp);

//Declare variable for blocked url list

static char bList[MAXIMUMSIZE][MAXLINE];

//Declare variable for log file fd

static int logFileDesc;

//Main function

int main(int argc, char **argv)

{

  //Declare required variable

  int listenfd;

  //Create socket

  socklen_t clientlen;

  //Create structure for client address

  struct sockaddr_storage clientaddr;

  int *connfdp;

  pthread_t tid;

  //Check command line arguments

  if (argc != 2)

  {

    //Display message

    fprintf(stderr, "usage: %s <port>\n", argv[0]);

    fprintf(stderr, "use default port 5000\n");

    listenfd = Open_listenfd("5000");

  }

  else

  {

    listenfd = Open_listenfd(argv[1]);

  }

  //Open log files

logFileDesc = Open("log.list", O_WRONLY | O_APPEND, 0);

  //Set memory for block list 

  memset(bList, '\0', MAXLINE * MAXIMUMSIZE);

  //Call function for parse file

  parseBlockList("block.list", bList, MAXIMUMSIZE);

  //Check condition

  while (1)

  {

    /* wait for connection as a server */

    clientlen = sizeof(struct sockaddr_storage);

    connfdp = Malloc(sizeof(int));

    //Call accept method

*connfdp = Accept(listenfd, (SA *) &clientaddr, &clientlen);

    //Generate new thread

Pthread_create(&tid, NULL, proxyThreadFunction, connfdp);

  }

  //Close log file descriptor

  Close(logFileDesc);

}

//Function definition for proxy thread

void *proxyThreadFunction(void *vargp)

{

  //Create thread id

  pthread_t tid = Pthread_self();

  Pthread_detach(tid);

  int connfd = *(int*)vargp;

  Free(vargp);

  //Create rio function

  rio_t cRio, sRio;

/* Declare required variable for client and server buffer */

  char cBuffer[MAXLINE], sBuffer[MAXLINE];

ssize_t ssn, ccn;

/* Declare variable for method name, uri, version number, host name, port number and path name*/

char methodName[MAXLINE], URIName[MAXLINE], versionNumber[MAXLINE];

char hostName[MAXLINE], portName[MAXLINE], pathName[MAXLINE];

  char logFileBuffer[MAXLINE];

  int clientfd;

  Rio_readinitb(&sRio, connfd);

  /* Check URI Name full path */

  if (!Rio_readlineb(&sRio, sBuffer, MAXLINE))

  {

    Close(connfd);

    return NULL;

  }

  //Display the uri path

sscanf(sBuffer, "%s %s %s", methodName, URIName, versionNumber);

  /* Check blocked uri */

  if (blockedURIList(URIName, bList))

  {

printf("Thread %ld: %s is blocked\n", tid, URIName);

    Close(connfd);

    return NULL;

  }

  //Print the log visit

sprintf(logFileBuffer, "Thread %ld: visit url: %s\n", tid, URIName);

Write(logFileDesc, logFileBuffer, strlen(logFileBuffer));

  memset(hostName, '\0', MAXLINE);

  memset(portName, '\0', MAXLINE);

  memset(pathName, '\0', MAXLINE);

  int res;

  /* Check if the given uri is separate */

if ((res = divideURI(URIName, hostName, portName, pathName)) == -1)

  {

fprintf(stderr, "tid %ld: not http protocol\n", tid);

    Close(connfd);

    return NULL;

  }

  else if (res == 0)

  {

fprintf(stderr, "tid %ld: not a abslute request path\n", tid);

    Close(connfd);

    return NULL;

  }

  //Connect server as a client

  clientfd = Open_clientfd(hostName, portName);

  Rio_readinitb(&cRio, clientfd);

  //Send the first request

sprintf(sBuffer, "%s %s %s\n", methodName, pathName, versionNumber);

  Rio_writen(clientfd, sBuffer, strlen(sBuffer));

  printf("tid %ld: %s", tid, sBuffer);

  do

  {

    /* Call next http requests */

    ssn = Rio_readlineb(&sRio, sBuffer, MAXLINE);

    printf("Tid %ld: %s", tid, sBuffer);

    Rio_writen(clientfd, sBuffer, ssn);

  } while(strcmp(sBuffer, "\r\n"));

  //For server send reply back

while ((ccn = Rio_readlineb(&cRio, cBuffer, MAXLINE)) != 0)

    Rio_writen(connfd, cBuffer, ccn);

�...

Blurred answer
Students have asked these similar questions
Suppose you buy an electronic device that you operate continuously. The device costs you $300 and carries a one-year warranty. The warranty states that if the device fails during its first year of use, you get a new device for no cost, and this new device carries exactly the same warranty. However, if it fails after the first year of use, the warranty is of no value. You plan to use this device for the next six years. Therefore, any time the device fails outside its warranty period, you will pay $300 for another device of the same kind. (We assume the price does not increase during the six-year period.) The time until failure for a device is gamma distributed with parameters α = 2 and β = 0.5. (This implies a mean of one year.) Use @RISK to simulate the six-year period. Include as outputs (1) your total cost, (2) the number of failures during the warranty period, and (3) the number of devices you own during the six-year period. Your expected total cost to the nearest $100 is _________,…
Which one of the 4 Entities mention in the diagram can have a recursive relationship?    If a new entity Order_Details is introduced, will it be a strong entity or weak entity? If it is a weak entity, then mention its type (ID or Non-ID, also Justify why)?
Please answer the JAVA OOP Programming Assignment scenario below: Patriot Ships is a new cruise line company which has a fleet of 10 cruise ships, each with a capacity of 300 passengers. To manage its operations efficiently, the company is looking for a program that can help track its fleet, manage bookings, and calculate revenue for each cruise. Each cruise is tracked by a Cruise Identifier (must be 5 characters long), cruise route (e.g. Miami to Nassau), and ticket price. The program should also track how many tickets have been sold for each cruise. Create an object-oriented solution with a menu that allows a user to select one of the following options: 1. Create Cruise – This option allows a user to create a new cruise by entering all necessary details (Cruise ID, route, ticket price). If the maximum number of cruises has already been created, display an error message. 2. Search Cruise – This option allows to search a cruise by the user provided cruise ID. 3. Remove Cruise – This op…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
CMPTR
Computer Science
ISBN:9781337681872
Author:PINARD
Publisher:Cengage
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning