-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.c
517 lines (455 loc) · 12 KB
/
server.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/******************************************/
/* Adam DeRusha */
/* derusa@rpi.edu */
/* Project 2 */
/* Due: 3/12/2012 */
/******************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#define MAXLINE 4096
#define MAXLISTEN 10
/* HTTP Types */
#define HTTP1_1 1
#define HTTP1_0 0
/* Method Types */
#define BAD_METHOD -1
#define GET 0
#define POST 1
#define HEAD 2
struct request
{
int method; /* GET, POST, HEAD */
int type; /* HTTP/1.1 or HTTP/1.0 */
char *resource;
char *full_resource;
};
/* Linked list node for the headers */
struct headers
{
char *header;
struct headers *next;
};
void serveClient(int connfd, char *client_name, char *blacklist[], int blacklistsize );
char *readResponse( int connfd );
void splitHeaders( char *message, struct headers **output );
void freeHeaders( struct headers *input );
int getRequestInfo( char *message, struct request **info );
int establishConnection( int *sock, char *hostname );
char *findValue( struct headers *head, char *key );
int transfer( int clientfd, int serverfd, char *request_line );
int check_blacklist( char *word, char *blacklist[], int size );
void print_client_request( char *client_name, char *req );
void freeRequestInfo( struct request *r );
int main( int argc, char *argv[] )
{
int listenfd, connfd;
pid_t pid;
short port;
socklen_t len;
struct sockaddr_in servaddr, cliaddr;
struct sigaction sigact;
if( argc < 2 )
{
fprintf(stderr, "Usage: %s portNumber domainsToFilter\n", argv[0]);
return EXIT_FAILURE;
}
port = atoi(argv[1]);
if( port < 1024 )
{
fprintf(stderr, "ERROR! portNumber should be a number and greater than 1023\n");
return EXIT_FAILURE;
}
/* Set up the signal handlers */
sigemptyset( &sigact.sa_mask );
sigact.sa_flags = 0 | SA_NODEFER | SA_RESETHAND;
sigact.sa_handler = SIG_IGN;
sigaction( SIGCHLD, &sigact, NULL); /* To prevent zombie processes */
sigaction( SIGINT, &sigact, NULL); /* To ignore keyboard interupts */
/* Request a server socket */
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
fprintf(stderr, "ERROR! Could not create listener socket\n");
return EXIT_FAILURE;
}
/* Fill out and bind the server socket */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
if( bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 )
{
fprintf(stderr, "ERROR! Could not bind to port %hd\n", port);
return EXIT_FAILURE;
}
if( listen(listenfd, MAXLISTEN) < 0 )
{
fprintf(stderr, "ERROR! Problem in the listen function\n");
return EXIT_FAILURE;
}
/* Start the server process */
while( 1 )
{
/* Get a client */
len = sizeof(cliaddr);
if( (connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &len)) < 0 )
{
if( errno == EINTR )
{
continue;
}
perror("Accept()");
return EXIT_FAILURE;
}
/* Pass the client to a child process */
if( (pid = fork()) == 0 ) /* Child */
{
/* Get the hostname */
struct hostent *hp;
hp = gethostbyaddr( (char *) &cliaddr.sin_addr.s_addr, sizeof(cliaddr.sin_addr.s_addr), AF_INET);
serveClient( connfd, hp->h_name, argv+2, argc-2);
close( connfd );
exit(EXIT_SUCCESS);
}
close( connfd );
}
return EXIT_SUCCESS;
}
void serveClient(int connfd, char *client_name, char *blacklist[], int blacklistsize )
{
char badResponse[] = "HTTP/1.1 403 Forbidden\r\nContent-Length: 22\r\nContent-Type: text/html\r\n\r\n<h1>403 Forbidden</h1>\r\n";
char notImplemented[] = "HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 31\r\nContent-Type: text/html\r\n\r\n<h1>405 Method Not Allowed</h1>\r\n";
char badRequest[] = "HTTP/1.1 400 Bad Request\r\nContent-Length: 24\r\nContent-Type: text/html\r\n\r\n<h1>400 Bad Request</h1>\r\n";
char *input = NULL;
struct headers *headers = NULL;
int sock;
char *hostname = NULL;
struct request *r;
/* Read the request from the client */
if( (input = readResponse(connfd)) == NULL )
{
return;
}
/* Split the headers into a linked list */
splitHeaders(input, &headers);
if( headers == NULL )
{
write( connfd, badRequest, strlen(badRequest) );
print_client_request( client_name, headers->header );
printf("\n");
free(input);
return;
}
/* Split the request line into type, resource, and HTTP version */
getRequestInfo(headers->header, &r);
if( r->method < 0 )
{
write(connfd, notImplemented, strlen(notImplemented));
print_client_request( client_name, headers->header );
printf("\n");
freeRequestInfo(r);
freeHeaders(headers);
free(input);
return;
}
/* Get the hostname (who to connect to) */
if( r->type == HTTP1_1 )
{
free(r->resource);
r->resource = NULL;
hostname = findValue(headers, "Host");
if( hostname == NULL )
{
write( connfd, badRequest, strlen(badRequest) );
print_client_request( client_name, headers->header );
printf("\n");
freeRequestInfo(r);
free(hostname);
freeHeaders(headers);
free(input);
return;
}
}
else if( r->type == HTTP1_0 )
{
hostname = r->resource;
r->resource = NULL;
}
/* Print what the client requested */
print_client_request( client_name, headers->header );
freeRequestInfo(r);
/* Check and make sure the client isn't trying to go somewhere bad... */
if( check_blacklist( hostname, blacklist, blacklistsize ) )
{
printf("[FILTERED]\n");
freeHeaders(headers);
free(input);
free(hostname);
write(connfd, badResponse, strlen(badResponse));
return;
}
printf("\n");
/* If client is ok, establish a connection to the server they want to reach */
if( establishConnection( &sock, hostname ) < 0 )
{
write( connfd, badRequest, strlen(badRequest));
freeHeaders(headers);
free(input);
free(hostname);
return;
}
free(hostname);
/* Transfer the data between client and server */
transfer(connfd, sock, input);
freeHeaders(headers);
free(input);
return;
}
int getRequestInfo( char *message, struct request **info )
{
char *start = strdup(message);
char *piece = NULL;
char *temp = NULL;
while( *start == ' ' ) /* Skip any leading whitespace */
++start;
/* Carve space for the request */
*info = (struct request *) malloc( sizeof(struct request));
(*info)->resource = NULL;
(*info)->full_resource = NULL;
if( (piece = strtok(start, " ")) == NULL )
{
free(start);
return -1;
}
/* Figure out whether we're a POST, GET, HEAD, or other */
if( strncmp(piece, "GET", 3) == 0 )
(*info)->method = GET;
else if( strncmp(piece, "POST", 4) == 0 )
(*info)->method = POST;
else if( strncmp(piece, "HEAD", 4) == 0 )
(*info)->method = HEAD;
else
(*info)->method = BAD_METHOD;
if( (piece = strtok(NULL, " ")) == NULL )
{
free(start);
return -1;
}
/* Get the full resource name and then extract the host name */
(*info)->full_resource = strdup(piece);
if( (temp = strstr(piece, "://")) != NULL )
{
piece = temp+3;
}
if( (temp = strstr(piece, "/")) != NULL )
{
piece[temp-piece] = '\0';
}
(*info)->resource = strdup(piece);
if( (piece = strtok(NULL, " ")) == NULL )
{
free(start);
return -1;
}
/* Figure out whether it's HTTP/1.0 or HTTP/1.1 */
if( strncmp(piece, "HTTP/1.1", 8) == 0 )
{
(*info)->type = HTTP1_1;
}
else if( strncmp(piece, "HTTP/1.0", 8) == 0 )
{
(*info)->type = HTTP1_0;
}
else
{
free(start);
return -1;
}
free(start);
return 0;
}
void splitHeaders( char *message, struct headers **output )
{
struct headers *head;
char *messageCopy = strdup(message);
char *header = strtok(messageCopy, "\r\n");
*output = NULL;
if( header == NULL )
{
return;
}
/* Create a linked list node */
(*output) = (struct headers *) malloc(sizeof(struct headers));
(*output)->header = strdup(header);
(*output)->next = NULL;
head = *output;
/* Now we just keep tokenizing the input and add links to the list */
while( (header = strtok(NULL, "\r\n")) != NULL )
{
(*output)->next = (struct headers *) malloc( sizeof(struct headers) );
*output = (*output)->next;
(*output)->header = strdup(header);
(*output)->next = NULL;
}
free(messageCopy);
*output = head;
}
void freeHeaders( struct headers *input )
{
struct headers *p;
while( input != NULL )
{
p = input;
input = input->next;
if( p->header != NULL )
free(p->header);
free(p);
}
return;
}
int establishConnection( int *sock, char *hostname )
{
struct sockaddr_in server;
struct hostent *hp;
int port = 80;
if( (*sock = socket( PF_INET, SOCK_STREAM, 0 )) < 0 )
{
return -1;
}
server.sin_family = PF_INET;
hp = gethostbyname(hostname);
if( hp == NULL )
{
return -1;
}
bcopy( (char *)hp->h_addr, (char *) &server.sin_addr, hp->h_length );
server.sin_port = htons(port);
if( connect( *sock, (struct sockaddr *) &server, sizeof( server ))< 0)
{
return -1;
}
return 0; /* Successful Connection */
}
/* Searches the linked list to find a key, returns the value */
char *findValue( struct headers *head, char *key )
{
struct headers *p = head;
while( p != NULL )
{
if( strncmp(key, p->header, strlen(key)) == 0 )
{
char *start = strstr(p->header, ":");
if( start == NULL )
return NULL;
++start;
while( *start == ' ' )
++start;
return strdup(start);
}
p = p->next;
}
return NULL;
}
/* Reads in the input from a file descriptor and returns what it read */
char *readResponse( int connfd )
{
char *response = NULL;
int read_count = 0;
char buffer[MAXLINE];
struct headers *p = NULL;
response = (char *) malloc(1);
response[0] = '\0';
while( (read_count = read(connfd, buffer, MAXLINE-1)) > 0 )
{
/* Build new string */
int old_length = strlen(response);
char *temp = (char *) malloc(old_length+read_count+1);
memcpy(temp,response,old_length);
memcpy(temp+old_length, buffer, read_count);
temp[old_length+read_count] = '\0';
free(response);
response = temp;
/* Check for all the headers read */
temp = strstr(response, "\r\n\r\n");
if( temp != NULL )
{
break;
}
}
freeHeaders(p);
return response;
}
/* Reads what a server says and just forwards it to a client */
int transfer( int clientfd, int serverfd, char *request_line )
{
char buffer[MAXLINE];
int count;
write( serverfd, request_line, strlen(request_line) );
while( (count = recv(serverfd, buffer, MAXLINE, MSG_WAITALL)) > 0 )
{
write(clientfd, buffer, count);
if(count < MAXLINE)
{
break;
}
}
return 1;
}
int check_blacklist( char *word, char *blacklist[], int size )
{
int i;
for( i = 0; i < size; ++i )
{
char *temp = NULL;
/* Check the prefix */
if(strncmp(word, blacklist[i], strlen(blacklist[i])) == 0)
{
return 1;
}
/* Check the suffix */
if( (temp = strstr(word,blacklist[i])) != NULL && strcmp(temp, blacklist[i]) == 0 )
{
return 1;
}
}
return 0;
}
/* Prints (most of) the required output */
void print_client_request( char *client_name, char *req )
{
char *t;
printf("%s: ", client_name);
if( (t = strstr(req, "HTTP/")) != NULL )
{
int len = t-req;
char *out = (char *) malloc( len + 1);
memcpy(out, req, len);
out[len] = '\0';
printf("%s ", out);
free(out);
}
else
{
printf("%s ", req);
}
return;
}
void freeRequestInfo( struct request *r )
{
if( r->full_resource != NULL )
free(r->full_resource);
if( r->resource != NULL )
free(r->resource);
free(r);
return;
}