-
Notifications
You must be signed in to change notification settings - Fork 1
/
pw.c
358 lines (299 loc) · 13.4 KB
/
pw.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
/*
* In the exam, it is required to put a comment on the top portion of the code explaining
* the top-level steps
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netinet/in.h> // Hostname resolution
#include <netdb.h> // Same "
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
//
struct sockaddr_in local, remote, server;
/**
* - The client connects to the proxy using the 's' socket
* - The connection is moved to the 'rs' socket, with a unique port
* for each client
* - The proxy connects to the server requested by the client
* using the 'ss' socket
*
* +--------+ +-------+ +--------+
* | | ==s==> | | | |
* | client |<==rs==>| proxy |<==ss=>| server |
* | | | | | |
* +--------+ +-------+ +--------+
**/
// Create a request/response buffer in the static area so that they are predefined as all zeros
// This should be useful so that the string delimiter is already present (usefulness? not much,
// we change it anyways)
#define buffLen 1024*1024 // 1MB
// Request coming from the client
char request[buffLen];
// Response to be sent back to the client
char response[buffLen];
char hRequest[buffLen];
// Define the header struct in order to save the headers returned in the response
struct Header {
char *n;
char *v;
};
// Create an array of headers to save them
struct Header headers[100];
int main(int argc, char* argv[], char* env[]) {
int s;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("Socket fallita con errno = %d\n", errno);
perror("Socket fallita");
return -1;
}
// Assign the required information to the local address object
local.sin_family = AF_INET;
local.sin_port = htons(21667);
// But this time set the IP address to zero, instead of a remote IP
local.sin_addr.s_addr = 0;
// And do it again with the remote address object
remote.sin_family = AF_INET;
// And let's set these as 0 just for good mesure, it's not really necessary
remote.sin_port = htons(0);
remote.sin_addr.s_addr = 0;
/*
* We also need to set an extra option on the socket for the server:
* the local address can be reused as long as there is no socket actively
* listening to that address. This allowes to open a new server without
* having to wait for the previous one to completely close.
*/
int yes = 1;
if ( setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) {
perror("setsockopt fallita");
return -1;
}
// The bind function binds to a specific socket a certain local address/port pair
if ( bind(s, (struct sockaddr *)&local,sizeof(struct sockaddr_in)) == -1 ) {
perror("Bind Fallita");
return -1;
}
// Finally, before actually starting the server, the socket needs to be passive,
// accepting remote connections
//
// int listen(int sockfd, int backlog);
// Where sockfd: the target socket
// backlog: the maximum number of pending connections to allow
if ( listen(s, 10) == -1) {
perror("Listen fallita");
return -1;
}
// This is the main loop of the server, always waiting for new connections to be
// started from client
int len = sizeof(struct sockaddr);
while (1) {
/*
* The accept call waits for a new connection on the specified 's' socket,
* knowing the information of the peer socket contained at the sockaddr pointer
* and the length of the sockaddr struct.
* The accept call returns a non-negative integer that is the file descriptor
* created for that accepted socket.
*/
int rs = accept(s, (struct sockaddr *) &remote, &len);
// Fork the program to handle multiple requests
if (fork()) continue;
// Let's reset the request headers buffer
bzero(hRequest, buffLen);
// Clean the headers
for (int i=0; i<100; i++) {
headers[i].n = 0;
headers[i].v = 0;
}
int headersCount = 0;
// The request line is a pointer to the request buffer that contains the HTTP request
// And is also
char* reqLine = headers[0].n = hRequest;
// As we do for the client, let's read the request one character at a time until there
// is something to read. When a termination char is reached, the loop will be broken
for (int i=0; read(rs, hRequest+i, 1); i++) {
// When the termination is reached
if (hRequest[i] == '\n' && hRequest[i-1] == '\r') {
// Let's terminate the previous string (which is at i-1 since the CRLF is 2 chars)
hRequest[i-1] = 0;
/*
* We know we reached the end of the headers part of the response when we see
* 2 consecutive CRLF. But the program at this point does not know how to
* differenciate between a header terminator and a headers end.
* Since a CRLF stars a new header, and the value is terminated with a 0 as a
* string terminator, if the previous header's key name is all 0s (=false),
* then the last header was null and that is the indicator that we reached
* the end of the headers section.
*/
if (!headers[headersCount].n[0]) break;
// The value is the string beginning just after the \n char
headers[++headersCount].n = hRequest + i + 1;
}
// When a ":" is reached, that is the signal that the value for the previous key is
// now beign transmitted
// If the current header has something as value, then I don't want to overwrite it
if (hRequest[i] == ':' && !headers[headersCount].v && headersCount > 0) {
// Let's terminate the previous string (the key name)
hRequest[i] = 0;
// And place the pointer for the value string
headers[headersCount].v = hRequest + i + 1;
}
}
// Printing the request line
printf("%s\n", reqLine);
// And then all the recieved headers, starting from index 1 as index 0 contains ony
// the request line
// WARNING: This cycle has been disabled using the 0=false
for (int i=1; i<headersCount && 0; i++)
printf("%s: %s\n", headers[i].n, headers[i].v);
// Now we are going to "pollute" the reuqest line, by adding string termination characters,
// but they are not really a problem as we actually need to understand each single part of
// the request separately in order to be able to serve it
// The request line follows this structure:
// Request-Line = Methodtruct Header headers[100];
// 108 int headersCount = 0;
// SP Request-URI SP HTTP-Version CRLF
char *method, *url, *version;
// Splitting the request line into its individual components: separating url, method and version
method = reqLine;
int p = 0;
for (p=0; reqLine[p] != ' '; p++);
reqLine[p++] = 0;
url = reqLine + p;
for (; reqLine[p] != ' '; p++);
reqLine[p++] = 0;
version = reqLine + p;
for (; reqLine[p] == '\n' && reqLine[p-1] == '\r'; p++);
reqLine[p - 1] = 0;
/*
printf("Method : %s\n", method);
printf("Url : %s\n", url);
printf("Version: %s\n", version);
*/
/*
* When a GET request is recieved, the client is accessing an HTTP website
* The proxy catches this request, makes an equivalent one to the server (without headers, in this
* implementation) and sends the response back to the client.
* The client is aware that a proxy is present and it needs to set it up through the system settings,
* but this technique can also be used to create "invisible" proxy that can see all the traffic going
* through a network node.
*/
if (!strcmp(method, "GET")) {
// Parsing the url to get only the hostname
// http://www.domain.tld:80/page
char *hostname, *filename;
// Cutting the url to remove port numbers
int i = 0;
// Going ahead until I find the first ':'
for (; url[i] != ':' && url[i]; i++);
// Making sure that after the ':', comes '//'
if (url[i+1] != '/' || url[i+2] != '/') {
perror("GET url parsing error");
return(-1);
}
// The hostname starts here
i = i + 3;
hostname = url + i;
// Stop the hostname at the ':' or '/' char
for (; url[i] != ':' && url[i] != '/' && url[i]; i++);
url[i] = 0;
// If the scan stopped because of a ':' char, continue going until a '/'
// is found which denotes the beginning of the filename
if (url[i] == ':') {
for (; url[i] != '/' && url[i]; i++);
}
filename = url + i + 1;
// Resolve the remote hostname in order to get the IP address to use in the socket
struct hostent *remoteIP;
remoteIP = gethostbyname(hostname);
printf("Connecting to: %s [%d.%d.%d.%d] \n",hostname, (unsigned char) remoteIP->h_addr[0],(unsigned char) remoteIP->h_addr[1],(unsigned char) remoteIP->h_addr[2],(unsigned char) remoteIP->h_addr[3]);
// Creating a socket towards the remote server IP that we just resolved
int ss;
if ((ss = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket server failure");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(80);
server.sin_addr.s_addr = *(unsigned int*)(remoteIP->h_addr);
// And using the previously created socket to connect to the remote server
if (connect(ss, (struct sockaddr*) &server, sizeof(struct sockaddr_in)) == -1) {
perror("Connect server failure");
exit(-1);
}
// TODO: vedere perche connection:close
sprintf(request, "GET /%s HTTP/1.1\r\nHost:%s\r\nConnection:close\r\n\r\n", filename, hostname);
write(ss, request, strlen(request));
// And when a response is heard from the server, send it back to the client
for (int t; t = read(ss, response, 2000); ) {
write(rs, response, t);
}
close(ss);
}
/*
* If a CONNECT request is received, the client wants to connect to the specified remote server
* using a tunnelling approach.
* This means that the request coming from the server is directly forwareded to the remote
* server, without having the proxy create a new one.
* This method allows for TLS encrypted connections to work, as the proxy just exposes its IP
* address, but the client is the one dictating what to send.
*/
else if (!strcmp(method, "CONNECT")) {
char *hostname, *port;
// Parsing the hostname and port from the connection string
hostname = url;
int i = 0;
// Cutting the url at the ':' char which devides hostname and port
for(; url[i] != ':' && url[i]; i++);
url[i] = 0;
port = url + i + 1;
/*
printf("Hostna: %s\n", hostname);
printf("Port : %s\n", port);
*/
// Resolving the hostname to an IP address
struct hostent *remoteIP;
remoteIP = gethostbyname(hostname);
printf("Connecting to: %s [%d.%d.%d.%d] \n",hostname, (unsigned char) remoteIP->h_addr[0],(unsigned char) remoteIP->h_addr[1],(unsigned char) remoteIP->h_addr[2],(unsigned char) remoteIP->h_addr[3]);
// Creating the ss socket towards the remote server IP
int ss;
if ((ss = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket server failure");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons((unsigned short)atoi(port));
server.sin_addr.s_addr = *(unsigned int*) remoteIP->h_addr;
// Connecting to the remote IP
if (connect(ss, (struct sockaddr*) &server, sizeof(struct sockaddr_in)) == -1) {
perror("Server connect failure");
exit(-1);
}
// Once the connection has been done, respond with the established command
sprintf(response, "HTTP/1.1 200 Estrablished\r\n\r\n");
write(rs, response, strlen(response));
if (fork()) { // Parent
// The parent keeps listing out for requests coming from the client and for the remote server
// If one is recieved, forward it to the socket connected with the final IP
for (int t; t=read(rs, request, 2000);) {
write(ss, request, t);
// printf("S << C : %s\n", request2);
}
} else { // Child
// Reading from the ss socket (connection between the proxy and the website server) which contains the response
// that the server intends to send the client
for (int t; t=read(ss, response, 2000); ) {
// And forwarding the response to the client, instead of keeping it to us
write(rs, response, t);
// printf("S >> C : %s\n", response2);
}
close(ss);
exit(0);
}
}
close(rs);
}
}