-
Notifications
You must be signed in to change notification settings - Fork 13
/
proxy.cpp
335 lines (220 loc) · 7 KB
/
proxy.cpp
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
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/wait.h>
#include <netdb.h>
#include "proxy_parse.h"
using namespace std;
char* convert_Request_to_string(struct ParsedRequest *req)
{
/* Set headers */
ParsedHeader_set(req, "Host", req -> host);
ParsedHeader_set(req, "Connection", "close");
int iHeadersLen = ParsedHeader_headersLen(req);
char *headersBuf;
headersBuf = (char*) malloc(iHeadersLen + 1);
if (headersBuf == NULL) {
fprintf (stderr," Error in memory allocation of headersBuffer ! \n");
exit (1);
}
ParsedRequest_unparse_headers(req, headersBuf, iHeadersLen);
headersBuf[iHeadersLen] = '\0';
int request_size = strlen(req->method) + strlen(req->path) + strlen(req->version) + iHeadersLen + 4;
char *serverReq;
serverReq = (char *) malloc(request_size + 1);
if(serverReq == NULL){
fprintf (stderr," Error in memory allocation for serverrequest ! \n");
exit (1);
}
serverReq[0] = '\0';
strcpy(serverReq, req->method);
strcat(serverReq, " ");
strcat(serverReq, req->path);
strcat(serverReq, " ");
strcat(serverReq, req->version);
strcat(serverReq, "\r\n");
strcat(serverReq, headersBuf);
free(headersBuf);
return serverReq;
}
int createserverSocket(char *pcAddress, char *pcPort) {
struct addrinfo ahints;
struct addrinfo *paRes;
int iSockfd;
/* Get address information for stream socket on input port */
memset(&ahints, 0, sizeof(ahints));
ahints.ai_family = AF_UNSPEC;
ahints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(pcAddress, pcPort, &ahints, &paRes) != 0) {
fprintf (stderr," Error in server address format ! \n");
exit (1);
}
/* Create and connect */
if ((iSockfd = socket(paRes->ai_family, paRes->ai_socktype, paRes->ai_protocol)) < 0) {
fprintf (stderr," Error in creating socket to server ! \n");
exit (1);
}
if (connect(iSockfd, paRes->ai_addr, paRes->ai_addrlen) < 0) {
fprintf (stderr," Error in connecting to server ! \n");
exit (1);
}
/* Free paRes, which was dynamically allocated by getaddrinfo */
freeaddrinfo(paRes);
return iSockfd;
}
void writeToserverSocket(const char* buff_to_server,int sockfd,int buff_length)
{
string temp;
temp.append(buff_to_server);
int totalsent = 0;
int senteach;
while (totalsent < buff_length) {
if ((senteach = send(sockfd, (void *) (buff_to_server + totalsent), buff_length - totalsent, 0)) < 0) {
fprintf (stderr," Error in sending to server ! \n");
exit (1);
}
totalsent += senteach;
}
}
void writeToclientSocket(const char* buff_to_server,int sockfd,int buff_length)
{
string temp;
temp.append(buff_to_server);
int totalsent = 0;
int senteach;
while (totalsent < buff_length) {
if ((senteach = send(sockfd, (void *) (buff_to_server + totalsent), buff_length - totalsent, 0)) < 0) {
fprintf (stderr," Error in sending to server ! \n");
exit (1);
}
totalsent += senteach;
}
}
void writeToClient (int Clientfd, int Serverfd) {
int MAX_BUF_SIZE = 5000;
int iRecv;
char buf[MAX_BUF_SIZE];
while ((iRecv = recv(Serverfd, buf, MAX_BUF_SIZE, 0)) > 0) {
writeToclientSocket(buf, Clientfd,iRecv); // writing to client
memset(buf,0,sizeof buf);
}
/* Error handling */
if (iRecv < 0) {
fprintf (stderr,"Yo..!! Error while recieving from server ! \n");
exit (1);
}
}
void* datafromclient(void* sockid)
{
int MAX_BUFFER_SIZE = 5000;
char buf[MAX_BUFFER_SIZE];
int newsockfd = *((int*)sockid);
char *request_message; // Get message from URL
request_message = (char *) malloc(MAX_BUFFER_SIZE);
if (request_message == NULL) {
fprintf (stderr," Error in memory allocation ! \n");
exit (1);
}
request_message[0] = '\0';
int total_recieved_bits = 0;
while (strstr(request_message, "\r\n\r\n") == NULL) { // determines end of request
int recvd = recv(newsockfd, buf, MAX_BUFFER_SIZE, 0) ;
if(recvd < 0 ){
fprintf (stderr," Error while recieving ! \n");
exit (1);
}else if(recvd == 0) {
break;
} else {
total_recieved_bits += recvd;
/* if total message size greater than our string size,double the string size */
buf[recvd] = '\0';
if (total_recieved_bits > MAX_BUFFER_SIZE) {
MAX_BUFFER_SIZE *= 2;
request_message = (char *) realloc(request_message, MAX_BUFFER_SIZE);
if (request_message == NULL) {
fprintf (stderr," Error in memory re-allocation ! \n");
exit (1);
}
}
}
strcat(request_message, buf);
}
struct ParsedRequest *req; // contains parsed request
req = ParsedRequest_create();
if (ParsedRequest_parse(req, request_message, strlen(request_message)) < 0) {
fprintf (stderr,"Error in request message..only http and get with headers are allowed ! \n");
exit(0);
}
if (req->port == NULL) // if port is not mentioned in URL, we take default as 80
req->port = (char *) "80";
/*final request to be sent*/
char* browser_req = convert_Request_to_string(req);
int iServerfd;
iServerfd = createserverSocket(req->host, req->port);
/* To run on IIIT proxy, comment above line and uncomment below line */
//char iiitproxy[] = "172.31.1.4";
//char iiitport[] = "8080";
// iServerfd = createserverSocket(iiitproxy, iiitport);
writeToserverSocket(browser_req, iServerfd, total_recieved_bits);
writeToClient(newsockfd, iServerfd);
ParsedRequest_destroy(req);
close(newsockfd); // close the sockets
close(iServerfd);
// Doesn't make any sense ..as to send something
int y = 3;
int *p = &y;
return p;
}
int main (int argc, char *argv[])
{
int sockfd,newsockfd;
struct sockaddr_in serv_addr;
struct sockaddr cli_addr;
if (argc<2)
{
fprintf (stderr,"SORRY! Provide A Port ! \n");
return 1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0); // create a socket
if (sockfd<0) {
fprintf (stderr,"SORRY! Cannot create a socket ! \n");
return 1;
}
memset(&serv_addr,0,sizeof serv_addr);
int portno = atoi(argv[1]); // argument through terminal
serv_addr.sin_family = AF_INET; // ip4 family
serv_addr.sin_addr.s_addr = INADDR_ANY; // represents for localhost i.e 127.0.0.1
serv_addr.sin_port = htons(portno);
int binded = bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (binded <0 ) {
fprintf (stderr,"Error on binding! \n");
return 1;
}
listen(sockfd, 100); // can have maximum of 100 browser requests
int clilen = sizeof(struct sockaddr);
while(1) {
/* A browser request starts here */
newsockfd = accept(sockfd,&cli_addr, (socklen_t*) &clilen);
if (newsockfd <0){
fprintf(stderr, "ERROR! On Accepting Request ! i.e requests limit crossed \n");
}
int pid = fork();
if(pid == 0){
datafromclient((void*)&newsockfd);
close(newsockfd);
_exit(0);
}else{
close(newsockfd); // pid =1 parent process
}
}
close(sockfd);
return 0;
}