-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.c
243 lines (204 loc) · 5.37 KB
/
proxy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* proxy.c - CS:APP Web proxy
*
* Student ID: 2013-11394
* Name: Kim Hyung Mo
*
* Proxy acts as server to echo client, and as client to echo server.
*
* 1. To act as server, the proxy opens listenfd to accept echoclients.
* 2. When connection request accepted, the proxy make a worker thread to
* serve echoclient.
* 3. Then the proxy opens clientfd to request to echoserver to act as client.
* 4. The proxy carrys out send/recieve performance, and return it back to
* echoclient.
* 5. At the moment, the proxy saves log for interaction.
*/
#include "csapp.h"
/* The name of the proxy's log file */
#define PROXY_LOG "proxy.log"
/* Undefine this if you don't want debugging output */
#define DEBUG
/* thread argument */
typedef struct{
int logfd;
int connfd;
struct sockaddr_in clientaddr;
} TA;
/*
* Functions to define
*/
void * process_request(void *);
int open_clientfd_ts(char *, int, sem_t *);
ssize_t Rio_readn_w(int, void *, size_t);
ssize_t Rio_readlineb_w(rio_t *, void *, size_t);
void Rio_writen_w(int, void *, size_t);
/*
* Helper functions
*/
int connect_proxy(int, char (*)[], sem_t *);
/* Global static variables */
static sem_t copy_sem;
static sem_t log_sem;
static sem_t conn_sem;
/*
* main - Main routine for the proxy program
*/
int main(int argc, char **argv)
{
TA t_arg;
pthread_t thread;
int port, clientlen, listenfd;
/* check arguments number */
if (argc != 2)
{
fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
exit(0);
}
/* keep proxy from caching */
Signal(SIGPIPE, SIG_IGN);
/* initialize semaphores */
Sem_init(©_sem, 0, 1);
Sem_init(&log_sem, 0, 1);
Sem_init(&conn_sem, 0, 1);
/* initialize as server of echo_client */
port = atoi(argv[1]);
clientlen = sizeof(SA);
listenfd = Open_listenfd(port);
t_arg.logfd = Open(PROXY_LOG, O_CREAT|O_RDWR|O_APPEND, 0677);
/* accept concurrent clients */
while (1)
{
/* aquire lock protecting the clientaddr until copy done */
P(©_sem);
t_arg.connfd = Accept(listenfd, (SA *) &t_arg.clientaddr, &clientlen);
Pthread_create(&thread, NULL, process_request, (void *) &t_arg);
}
Close(t_arg.logfd);
exit(0);
}
/* thread routine */
void * process_request(void * vargp)
{
rio_t rio;
int nbytes;
time_t timer;
char * hostaddrp;
struct tm * time_now;
struct sockaddr_in clientaddr;
int logfd, connfd_c, connfd_s, clientport;
/* local buffers */
char log[MAXLINE];
char logbuf[MAXLINE];
char rdwrbuf[MAXLINE];
/* setup as server of echo_client */
logfd = ((TA *) vargp)->logfd;
connfd_c = ((TA *) vargp)->connfd;
clientaddr = ((TA *) vargp)->clientaddr;
hostaddrp = inet_ntoa(clientaddr.sin_addr);
clientport = ntohs(clientaddr.sin_port);
/* detach thread & release lock */
pthread_detach(Pthread_self());
V(©_sem);
/* work as client of echo_server */
for (nbytes = 0; ;)
{
/* check connection error */
if ((connfd_s = connect_proxy(connfd_c, &rdwrbuf, &conn_sem)) < 0)
{
if (connfd_s == -4)
{
fprintf(stderr, "Disconnect: %d\n", connfd_c);
break;
}
else
{
sprintf(rdwrbuf, "proxy usage: <host> <port> <message>\n");
Rio_writen_w(connfd_c, rdwrbuf, strlen(rdwrbuf));
continue;
}
}
/* read/write with echo_server */
Rio_readinitb(&rio, connfd_s);
Rio_writen_w(connfd_s, rdwrbuf, strlen(rdwrbuf));
nbytes += Rio_readlineb_w(&rio, rdwrbuf, MAXLINE);
/* wite back to echo_client */
Rio_writen_w(connfd_c, rdwrbuf, strlen(rdwrbuf));
Close(connfd_s);
/* save log */
time(&timer);
time_now = localtime(&timer);
strftime(logbuf, MAXLINE, "%a %d %b %Y %H:%M:%S %Z", time_now);
sprintf(log, "%s: %s %d %d %s",
logbuf, hostaddrp, clientport, nbytes, rdwrbuf);
/* write log to proxy.log */
P(&log_sem);
Write(logfd, log, strlen(log));
V(&log_sem);
}
Close(connfd_c);
return NULL;
}
/*
* connect_proxy - Parse input (<host> <port> <message>) and
* make connection with proper echo server.
*/
int connect_proxy(int fd, char (* usrbuf)[MAXLINE], sem_t * mutexp)
{
rio_t rio;
size_t nbytes;
char buf[MAXLINE];
char * host, * args, * port;
Rio_readinitb(&rio, fd);
/* read and check if disconnected */
if (!(nbytes = Rio_readlineb_w(&rio, buf, MAXLINE)))
return -4;
/* parse message */
if (!(host = strtok_r(buf, " ", &args)))
return -1;
if (!(port = strtok_r(NULL, " ", &args)))
return -1;
/* open socket as client of echo_server */
fd = open_clientfd_ts(host, atoi(port), &conn_sem);
strcpy(*usrbuf, args);
return fd;
}
int open_clientfd_ts(char * hostname, int port, sem_t * mutexp)
{
int fd;
/* lock the opening of client fd */
P(mutexp);
fd = open_clientfd(hostname, port);
V(mutexp);
return fd;
}
ssize_t Rio_readn_w(int fd, void * ptr, size_t nbytes)
{
ssize_t ncnt;
if ((ncnt = rio_readn(fd, ptr, nbytes)) < 0)
{
printf("Rio_readn_w error\n");
fflush(stdout);
ncnt = 0;
}
return ncnt;
}
ssize_t Rio_readlineb_w(rio_t * rp, void * usrbuf, size_t maxlen)
{
ssize_t rcnt;
if ((rcnt = rio_readlineb(rp, usrbuf, maxlen)) < 0)
{
printf("Rio_readlineb_w error\n");
fflush(stdout);
rcnt = 0;
}
return rcnt;
}
void Rio_writen_w(int fd, void * usrbuf, size_t n)
{
if (rio_writen(fd, usrbuf, n) != n)
{
printf("Rio_writen_w error\n");
fflush(stdout);
}
}