-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyClientActiveSocket.c
More file actions
498 lines (420 loc) · 14.2 KB
/
Copy pathproxyClientActiveSocket.c
File metadata and controls
498 lines (420 loc) · 14.2 KB
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
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <ctype.h>
#include "include/buffer.h"
#include "include/selector.h"
#include "include/helpers.h"
#include "include/proxyPassiveSocket.h"
#include "include/proxyClientActiveSocket.h"
#include "include/proxyOriginActiveSocket.h"
#include "include/proxySettings.h"
#define MAX_PORTSTRING_SIZE 10
static fd_handler fd = {
.handle_read = proxy_client_active_socket_read,
.handle_write = proxy_client_active_socket_write,
.handle_block = proxy_client_active_socket_block,
.handle_close = proxy_client_active_socket_close
};
fd_handler * proxy_client_active_socket_fd_handler(void){
return &fd;
}
enum client_states {
/**
* Reads the first line (explicit mode) or looks
* for the Host header (transparent mode) in the first request
*/
READING_HEADER_FIRST_LINE,
/**
* Searchs for host header in html request.
*/
SEARCHING_FOR_HEADER_HOST,
/**
* Waits for the DNS request to resolve
*/
CONNECTING,
/**
* Transfers bits from the client to the origin server
*/
PASSING_CONTENT
};
enum ssl_states {
/**
* Connection doesn't use SSL
*/
NO_SSL,
/**
* Awaiting connection to the origin server
*/
SSL_CONNECTING,
/**
* Succesfully connected to the origin server through SSL
*/
SSL_OK
};
/**
* Contains buffers and connection data
*/
typedef struct proxy_client_active_socket_data {
connection_data * connection_data;
char* hostname;
unsigned short port;
buffer * write_buff;
buffer * read_buff;
enum client_states state;
enum ssl_states ssl;
} proxy_client_active_socket_data;
static void * open_origin_socket(void * client_key);
static void process_ssl (struct selector_key *key);
extern proxy_settings global_settings;
void * proxy_client_active_socket_data_init(fd_selector s, int client_fd) {
proxy_client_active_socket_data * data = malloc(sizeof(proxy_client_active_socket_data));
if (data == NULL)
return NULL;
data->state = READING_HEADER_FIRST_LINE;
data->read_buff = new_buffer();
data->write_buff = new_buffer();
connection_data * conn_data = malloc(sizeof(connection_data));
if (conn_data == NULL) {
free(data);
return NULL;
}
data->connection_data = conn_data;
conn_data->s = s;
conn_data->client_fd = client_fd;
conn_data->client_data = data;
conn_data->origin_fd = -1;
conn_data->origin_data = NULL;
conn_data->origin_transformation_fd = -1;
conn_data->client_transformation_fd = -1;
conn_data->transformation_data = NULL;
conn_data->state = ALIVE;
strcpy(conn_data->client_name, "Unknown");
global_settings.current_connections++;
global_settings.total_connections++;
if (global_settings.current_connections > global_settings.max_connections) {
send_error(500, "Internal server error", (char*) 0, "Proxy is too busy", conn_data);
return NULL;
}
return data;
}
void proxy_client_active_socket_read(struct selector_key *key) {
proxy_client_active_socket_data * data = key->data;
connection_data * connection_data = data->connection_data;
int client_fd = connection_data->client_fd;
int origin_fd = connection_data->origin_fd;
if (data->state == READING_HEADER_FIRST_LINE) {
char aux[1000];
int ret;
while ((ret = recv(client_fd, aux, sizeof(aux), 0)) > 0) {
//TODO: check buffer space
global_settings.bytes_received += ret;
buffer_write_data(data->write_buff, aux, ret);
}
if (ret == 0) {
/* Connection was closed */
kill_client(connection_data);
return;
}
/* Peeks the first line of the request */
ret = buffer_peek_line(data->write_buff, aux, sizeof(aux));
if (ret == -1) {
send_error(400, "Bad Request", (char*) 0, "Can't parse request.", connection_data);
return;
}
if (ret > 0) {
char method[10000], url[10000], protocol[10000], host[10000], path[10000];
unsigned short port;
int iport;
pthread_t tid;
int ssl;
/* Parse it. */
trim(aux);
if (sscanf(aux, "%[^ ] %[^ ] %[^ ]", method, url, protocol ) == 3 && url[0]!='/') {
if (url[0] == '\0') {
send_error(400, "Bad Request", (char*) 0, "Null URL.", connection_data);
return;
}
if (strncasecmp(url, "http://", 7) == 0) {
strncpy(url, "http", 4); /* making sure it's lower case */
if (sscanf(url, "http://%[^:/]:%d%s", host, &iport, path) == 3)
port = (unsigned short) iport;
else if (sscanf(url, "http://%[^/]%s", host, path) == 2)
port = 80;
else if (sscanf(url, "http://%[^:/]:%d", host, &iport) == 2) {
port = (unsigned short) iport;
*path = '\0';
} else if (sscanf(url, "http://%[^/]", host) == 1) {
port = 80;
*path = '\0';
} else {
send_error(400, "Bad Request", (char*) 0, "Can't parse URL.", connection_data);
return;
}
ssl = NO_SSL;
} else if (strcmp(method, "CONNECT") == 0) {
if (sscanf(url, "%[^:]:%d", host, &iport) == 2)
port = (unsigned short) iport;
else if (sscanf(url, "%s", host) == 1)
port = 443;
else {
send_error(400, "Bad Request", (char*) 0, "Can't parse URL.", connection_data);
return;
}
ssl = SSL_CONNECTING;
} else {
send_error(400, "Bad Request", (char*) 0, "Unknown URL type.", connection_data);
return;
}
data->hostname = host;
data->port = port;
data->ssl = ssl;
struct selector_key* k = malloc(sizeof(*key));
if(k == NULL) {
send_error(500, "Internal server error", (char*) 0, "Ran out of memory", connection_data);
return;
}
memcpy(k, key, sizeof(*k));
/* Open socket to the origin server. */
if(-1 == pthread_create(&tid, 0, open_origin_socket, k)) {
send_error(500, "Internal server error", (char*) 0, "Couldn't process request", connection_data);
return;
} else {
selector_set_interest_key(key, OP_NOOP);
}
data->state = CONNECTING;
printDate();
printf(": New request to: %s:%d\n", host, port);
} else {
data->state = SEARCHING_FOR_HEADER_HOST;
data->ssl = NO_SSL;
}
}
}
//TODO: client_fd could be empty if comes from state 'READING_HEADER_FIRST_LINE'
if(data->state == SEARCHING_FOR_HEADER_HOST) {
char aux[1000];
int ret;
while ((ret = recv(client_fd, aux, sizeof(aux), 0)) > 0) {
//TODO: check buffer space
global_settings.bytes_received += ret;
buffer_write_data(data->write_buff, aux, ret);
}
if (ret == 0) {
/* Connection was closed */
kill_client(connection_data);
return;
}
/* Peeks the first line of the request */
ret = buffer_peek_line(data->write_buff, aux, sizeof(aux));
if (ret == -1) {
send_error(400, "Bad Request", (char*) 0, "Can't parse request.", connection_data);
return;
}
if(ret > 0) {
pthread_t tid;
unsigned short port;
int iport;
char host[1000], host_header[1000], path[1000];
trim(aux);
lineToLowerCase(aux, ret);
if(sscanf(aux, "host: %s", host_header)==1) {
if (sscanf(host_header, "%[^:/]:%d%s", host, &iport, path) == 3)
port = (unsigned short) iport;
else if (sscanf(host_header, "%[^/]%s", host, path) == 2)
port = 80;
else if (sscanf(host_header, "%[^:/]:%d", host, &iport) == 2) {
port = (unsigned short) iport;
*path = '\0';
} else if (sscanf(host_header, "%[^/]", host ) == 1) {
port = 80;
*path = '\0';
} else {
send_error(400, "Bad Request", (char*) 0, "Can't parse URL.", connection_data);
return;
}
data->hostname = host;
data->port = port;
struct selector_key* k = malloc(sizeof(*key));
if(k == NULL) {
send_error(500, "Internal server error", (char*) 0, "Ran out of memory", connection_data);
return;
}
memcpy(k, key, sizeof(*k));
/* Open socket to the origin server. */
if(-1 == pthread_create(&tid, 0, open_origin_socket, k)) {
send_error(500, "Internal server error", (char*) 0, "Couldn't process request", connection_data);
return;
} else {
selector_set_interest_key(key, OP_NOOP);
}
data->state = CONNECTING;
printDate();
printf(": New request to: %s:%d\n", host, port);
}
}
}
if (data->state == PASSING_CONTENT) {
if (data->ssl == SSL_CONNECTING) {
process_ssl(key);
return;
}
char aux[1000];
int ret;
int bytes_to_send;
do {
bytes_to_send = sizeof(aux) > buffer_space(data->write_buff) ? buffer_space(data->write_buff) : sizeof(aux);
if (bytes_to_send == 0) {
/* Buffer full. Must wait for the origin server to read it */
selector_set_interest_key(key, OP_NOOP);
break;
}
ret = recv(client_fd, aux, bytes_to_send, 0);
global_settings.bytes_received += ret;
buffer_write_data(data->write_buff, aux, ret);
} while (ret > 0);
if (ret == 0) {
/* Connection was closed */
kill_client(connection_data);
return;
}
selector_set_interest(key->s, origin_fd, OP_WRITE);
}
}
static void process_ssl (struct selector_key *key) {
proxy_client_active_socket_data * data = key->data;
char aux[1000];
int ret;
if (data->ssl == SSL_CONNECTING) {
do {
ret = buffer_peek_line(data->write_buff, aux, sizeof(aux));
buffer_read_data(data->write_buff, aux, ret);
aux[ret] = 0;
} while (ret > 0 && strcmp( aux, "\n" ) != 0 && strcmp( aux, "\r\n" ) != 0 );
if (ret > 0) {
char * str = "HTTP/1.0 200 Connection established\r\n\r\n";
send(key->fd, str, strlen(str), MSG_NOSIGNAL);
buffer_clean(data->write_buff);
data->ssl = SSL_OK;
}
}
}
void proxy_client_active_socket_write(struct selector_key *key) {
proxy_client_active_socket_data * data = key->data;
connection_data * connection_data = data->connection_data;
char aux[1000];
int ret;
while ((ret = buffer_read_data(data->read_buff, aux, sizeof(aux))) > 0) {
//TODO: checkear que se mandaron todos los bytes
global_settings.bytes_sent += ret;
send(key->fd, aux, ret, MSG_NOSIGNAL);
}
if(connection_data->state == CLOSED) {
kill_client(connection_data);
}
selector_set_interest_key(key, OP_READ);
if (connection_data->client_transformation_fd == -1) {
selector_set_interest(key->s, connection_data->origin_fd, OP_READ);
} else {
selector_set_interest(key->s, connection_data->client_transformation_fd, OP_READ);
}
}
static void * open_origin_socket(void * client_key) {
struct selector_key * key = client_key;
proxy_client_active_socket_data * data = key->data;
connection_data * connection_data = data->connection_data;
int sockfd;
char portString[MAX_PORTSTRING_SIZE];
sprintf(portString, "%d", data->port);
/* No need to join the thread */
pthread_detach(pthread_self());
/* Tell the system what kind(s) of address info we want */
struct addrinfo addrCriteria; // Criteria for address match
memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
addrCriteria.ai_family = AF_UNSPEC; // Any address family
addrCriteria.ai_socktype = SOCK_STREAM; // Only stream sockets
addrCriteria.ai_protocol = IPPROTO_TCP; // Only TCP protocol
/* Get address(es) associated with the specified name/service */
struct addrinfo *addrList; // Holder for list of addresses returned
/* Modify servAddr contents to reference linked list of addresses */
int rtnVal = getaddrinfo(data->hostname, portString, &addrCriteria, &addrList);
if (rtnVal != 0) {
send_error(404, "Not Found", (char*) 0, "Unknown host.", connection_data);
return 0;
}
int connectRet = -1;
/* Iterate over returned addresses */
for (struct addrinfo *addr = addrList; addr != NULL; addr = addr->ai_next) {
sockfd = socket( addr->ai_family, addr->ai_socktype, addr->ai_protocol );
if (sockfd < 0) {
continue;
}
if ((connectRet = connect(sockfd, addr->ai_addr, addr->ai_addrlen)) < 0) {
close(sockfd);
continue;
}
/* Connected succesfully! */
break;
}
freeaddrinfo(addrList); // Free addrinfo allocated in getaddrinfo()
if (connectRet < 0) {
send_error(503, "Service Unavailable", (char*) 0, "Connection refused.", connection_data);
return 0;
} else
connection_data->origin_fd = sockfd;
selector_notify_block(key->s, key->fd);
return 0;
}
void proxy_client_active_socket_block(struct selector_key *key) {
proxy_client_active_socket_data * data = key->data;
connection_data * connection_data = data->connection_data;
data->state = PASSING_CONTENT;
selector_set_interest_key(key, OP_READ);
if(selector_fd_set_nio(connection_data->origin_fd) == -1) {
send_error(500, "Internal server error", (char*) 0, "Fatal error", connection_data);
return;
}
connection_data->ssl = data->ssl == NO_SSL ? 0 : 1;
void * origin_data = proxy_origin_active_socket_data_init(connection_data, data->write_buff, data->read_buff);
if (origin_data == NULL) {
send_error(500, "Internal server error", (char*) 0, "Ran out of memory", connection_data);
return;
}
connection_data->origin_data = origin_data;
if (data->ssl == SSL_CONNECTING) {
buffer_reset_peek_line(data->write_buff);
process_ssl(key);
}
if(SELECTOR_SUCCESS != selector_register(key->s, connection_data->origin_fd, proxy_origin_active_socket_fd_handler(),
OP_WRITE, origin_data)) {
send_error(500, "Internal server error", (char*) 0, "Fatal error", connection_data);
}
}
void proxy_client_active_socket_close(struct selector_key *key) {
close(key->fd);
}
void set_client_name(void * client_data, char * client_name) {
proxy_client_active_socket_data * data = client_data;
connection_data * connection_data = data->connection_data;
strncpy(connection_data->client_name, client_name, sizeof(connection_data->client_name));
}
void kill_client(connection_data * connection_data) {
proxy_client_active_socket_data * data = connection_data->client_data;
buffer_free(data->write_buff);
buffer_free(data->read_buff);
kill_origin(connection_data);
selector_unregister_fd(connection_data->s, connection_data->client_fd);
global_settings.current_connections--;
printDate();
printf(": Ending connection with client %s\n", connection_data->client_name);
free(data->connection_data);
free(data);
}