
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);
�...

Trending nowThis is a popular solution!

Chapter 12 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Solve this please and fastarrow_forwardPlease original work Background information: Imagine you are a consultant for a fictional company, FutureTech Innovations, which specializes in advanced technology products. The company is interested in improving its decision-making processes by leveraging Business Analytics (BA) and Management Support Systems (MSS). Talk about how Online Analytical Processing (OLAP) could be used to analyze data from multiple perspectives at FutureTech. What dimensions (e.g., time, product, region) would be important, and how would OLAP enhance decision-making? Talk about the role of data visualization in helping FutureTech understand business performance. What types of visualizations (e.g., dashboards, charts) would be useful, and how would they assist in interpreting data? Please cite in text references and add weblinksarrow_forwardDesign a synchronous Up/Down counter to produce the following sequence (4 9 2,0,7,6,3,1,5) using T flip-flop. The counter should count up when Up/Down =1, and down when Up/Down = 0.arrow_forward
- An active-HIGH pulse (HIGH level when asse rted, LOW level when not) is required on an i nverter input.(a) Draw the appropriate logic symbol, using t he distinctive shape and the negation indicat or, for the inverter in this application.(b) Describe the output when a positive-goin g pulse is applied to the input of an inverter.arrow_forwardConvert this question to Subroutines ORG 100 LDA A1 STA T1 LDA B1 STA T2 LDA T1 I STA A1 I ISZ B1 ISZ A1 ISZ ENC BUN L2 //COMPUTER HLT L1, INC A1, HEX 5000 STA T2 1 B1, HEX 6000 ISZ T1 T1, HEX O ISZ T2 T2, HEX 0 ISZ DCD DCD, DEC -8 BUN L1 // DPNQVUFS ENC, DEC -8 LDA B1 I DEC, HEX FF L2, ADD DEC ENDarrow_forwardConvert this question to Subroutines > F = [(A. B) + (A'. B')]” = [(A. B)'. (A'. B')']' ORG 100 LDA A // Load A operand to AC HLT // END Program AND B CMA STA T1 LDA A CMA STA T2 LDA B CMA AND T2 CMA // Store AC result in T1 // Load A operand to AC // Complement result in AC // Store AC [A'] result in T2 // Load B operand to AC // Complement result in AC // AND B' with T2[A'] operand // Complement result in AC AND T1 // AND [(A'.B')'] with T1[(A.B)'] CMA // Complement result in AC STA F // Store AC result in F // AND A operand with B operand B, HEX 01 // Complement result in AC A, HEX 01 T1, HEX O T2, HEX 0 F, HEX 0 ENDarrow_forward
- Convert this question to Subroutines L1, L2, ORG 100 SKI BUN L1 INP OUT STA CTR CLA // Check input operation ready // Loop back to L1 location // Enter counter (-10) to AC // display counter operand // Store counter in CTR // Clear AC STA LOCI // Clear content of location LOC ISZ LOC ISZ CTR BUN L2 // Go to next Address // Check loop duration // Loop back to L2 location // End program HLT CTR, HEX 0 LOC, HEX 0150 ENDarrow_forwardDesign the following Digital circuit in Verilog. IN1 IN2 MUX A Hint: a typical Demultiplexer is defined by a truth table like the following B DEMUX 1t04 demux_1t04 Sel W X Y Z "00" A 000 "01" 0 A 00 Sel sel demux_sel[1:0] "11" "10" 0 0 AO 000Aarrow_forwardhow do I do this in quartus with picture examples pleasearrow_forward
- I need help with this problem and an step by step explanation of the solution from the image described below. (Introduction to Signals and Systems)arrow_forwardx³dx+y³dy=0arrow_forwardI need help with this problem and an step by step explanation of the solution from the image described below. (Introduction to Signals and Systems)arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





