-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.c
More file actions
346 lines (306 loc) · 10.4 KB
/
proxy.c
File metadata and controls
346 lines (306 loc) · 10.4 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
/*
* This is the main program for the proxy, which receives connections for sending and receiving clients
* both in binary and XML format. Many clients can be connected at the same time. The proxy implements
* an event loop.
*
* *** YOU MUST IMPLEMENT THESE FUNCTIONS ***
*
* The parameters and return values of the existing functions must not be changed.
* You can add function, definition etc. as required.
*/
#include "connection.h"
#include "record.h"
#include "recordFromFormat.h"
#include "recordToFormat.h"
#include "xmlfile.h"
#include <arpa/inet.h>
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define BUFSIZE 51200
#define MAXSERVER 32
/* This struct should contain the information that you want
* keep for one connected client.
*/
struct Client {
int index; // the index this client is in
int sock; // FD to this client
char id; // client's ID
char type; // clients type, 'X'(xml) or 'B'(binary)
int bufsize; // size of the stored data in buffer
char *buf; // a buffer of bytes received through the connection
};
typedef struct Client Client;
// Global variables
int server_sock, connected_sockets = 0;
fd_set fdset, ready_fdset;
void usage(char *cmd) {
fprintf(stderr, "Usage: %s <port>\n"
" This is the proxy server. It takes as imput the port where it accepts connections\n"
" from \"xmlSender\", \"binSender\" and \"anyReceiver\" applications.\n"
" <port> - a 16-bit integer in host byte order identifying the proxy server's port\n"
"\n",
cmd);
exit(-1);
}
/*
* This function is called when a new connection is noticed on the server
* socket.
* The proxy accepts a new connection and creates the relevant data structures.
*
* *** The parameters and return values of this functions can be changed. ***
*/
Client *handleNewClient(int client_sock) {
Client *c = malloc(sizeof *c);
c->sock = client_sock;
c->id = 0;
c->bufsize = 0;
c->type = 0;
c->id = 0;
c->buf = malloc(sizeof(c->buf) * BUFSIZE);
return c;
}
/*
* This function is called when a connection is broken by one of the connecting
* clients. Data structures are clean up and resources that are no longer needed
* are released.
*
* *** The parameters and return values of this functions can be changed. ***
*/
void removeClient(Client *c, Client **client_arr) {
client_arr[c->index] = NULL;
free(c->buf);
free(c);
}
/*
* This function is called when the proxy received enough data from a sending
* client to create a Record. The 'dest' field of the Record determines the
* client to which the proxy should send this Record.
*
* If no such client is connected to the proxy, the Record is discarded without
* error. Resources are released as appropriate.
*
* If such a client is connected, this functions find the correct socket for
* sending to that client, and determines if the Record must be converted to
* XML format or to binary format for sendig to that client.
*
* It does then send the converted messages.
* Finally, this function deletes the Record before returning.
*
* *** The parameters and return values of this functions can be changed. ***
*/
void forwardMessage(Record *r, Client **client_arr) {
for (int i = 0; i < MAXSERVER; i++) {
Client *c = client_arr[i];
if (c == NULL)
continue;
// if (!FD_ISSET(c->sock, &fdset))
// continue;
if (r->dest != c->id)
continue;
char *buffer;
int buflen = 0;
if (c->type == 'B')
buffer = recordToBinary(r, &buflen);
else
buffer = recordToXML(r, &buflen);
printf("%s\n", buffer);
if (buffer != NULL) {
write(c->sock, buffer, buflen);
free(buffer);
}
}
}
/*
* This function is called whenever activity is noticed on an connected socket,
* and that socket is associated with a client. This can be sending client
* or a receiving client.
*
* The calling function finds the Client structure for the socket where
* acticity has occurred and calls this function.
*
* If this function receives data that completes a record, it creates an internal
* Record data structure on the heap and calls forwardMessage() with this Record.
*
* If this function notices that a client has disconnected, it calls removeClient()
* to release the resources associated with it.
*
* *** The parameters and return values of this functions can be changed. ***
*/
Record *createDummyRecord() {
Record *r = malloc(sizeof(Record));
r->has_source = 1, r->has_dest = 1;
r->has_username = 1, r->has_id = 1;
r->has_group = 1, r->has_semester = 1;
r->has_grade = 1, r->has_courses = 1;
r->source = 'A', r->dest = 'X';
r->username = strdup("griff"), r->id = 1003;
r->group = 200, r->semester = 27;
r->grade = Grade_PhD, r->courses = Course_IN1020 | Course_IN1060;
return r;
}
Client *find_client_ref(int sock, Client **client_arr) {
printf("looking for sock: %d\n", sock);
for (int i = 0; i < MAXSERVER; i++) {
if (client_arr[i] != NULL && client_arr[i]->sock == sock)
return client_arr[i];
}
return NULL;
}
bool connected(int sock) {
char buf;
size_t err = recv(sock, &buf, 1, MSG_PEEK);
if (err == 0)
return false;
return true;
}
int handleClient(Client *c, Client **client_arr) {
// check if client is still connected
if (!connected(c->sock)) {
printf("client %d disconnected\n", c->sock);
removeClient(c, client_arr);
connected_sockets--;
return 0;
}
int read_bytes = 0;
// get type and than id first
if (!c->id || !c->type) {
char buffer[2];
read_bytes = tcp_read(c->sock, buffer, 1);
if (!c->type)
c->type = buffer[0];
else
c->id = buffer[0];
}
if (read_bytes == -1)
exit(EXIT_FAILURE);
if (read_bytes)
return 1;
read_bytes = tcp_read(c->sock, c->buf + c->bufsize, BUFSIZE - 1);
if (read_bytes == -1)
exit(EXIT_FAILURE);
c->bufsize += read_bytes;
if (c->type == 'X') {
c->buf[c->bufsize] = '\0';
c->bufsize++;
}
// check if a valid record exists in buffer
int bytesread;
Record *r;
if (c->type == 'X') {
while (strstr(c->buf, "<record>") && strstr(c->buf, "</record>")) {
r = XMLtoRecord(c->buf, c->bufsize, &bytesread);
if (!r)
break;
c->bufsize = (int)strlen(c->buf);
forwardMessage(r, client_arr);
deleteRecord(r);
}
} else {
do {
r = BinaryToRecord(c->buf, c->bufsize, &bytesread);
if (r) {
memmove(c->buf, c->buf + bytesread, c->bufsize - bytesread + 1);
c->bufsize -= bytesread;
forwardMessage(r, client_arr);
deleteRecord(r);
}
} while (r != NULL);
}
return 1;
}
void init_client_arr(Client *client_arr[MAXSERVER]) {
for (int i = 0; i < MAXSERVER; i++) {
client_arr[i] = NULL;
}
}
int find_vacant_index(Client **client_arr) {
for (int i = 0; i < MAXSERVER; i++) {
if (client_arr[i] == NULL)
return i;
}
fprintf(stderr, "Exceeded max amount of connected socket which is %d\n", MAXSERVER);
exit(EXIT_FAILURE);
}
void insert_new_client(int client_sock, Client **client_arr) {
int index = find_vacant_index(client_arr);
client_arr[index] = handleNewClient(client_sock);
client_arr[index]->index = index;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
usage(argv[0]);
}
int port = atoi(argv[1]);
server_sock = tcp_create_and_listen(port);
if (server_sock < 0)
exit(-1);
/* add your initialization code */
int max_sock = server_sock;
FD_ZERO(&fdset);
FD_SET(server_sock, &fdset);
// set all client's in this array to be NULL
Client *clients_arr[MAXSERVER];
init_client_arr(clients_arr);
/*
* The following part is the event loop of the proxy. It waits for new connections,
* new data arriving on existing connection, and events that indicate that a client
* has disconnected.
*
* This function uses handleNewClient() when activity is seen on the server socket
* and handleClient() when activity is seen on the socket of an existing connection.
*
* The loops ends when no clients are connected any more.
*/
printf("Proxy has started\n");
int err;
do {
FD_ZERO(&fdset);
FD_SET(server_sock, &fdset);
// add the socket that is still connected, to FDSET
for (int i = 0; i < MAXSERVER; i++) {
if (clients_arr[i] != NULL)
FD_SET(clients_arr[i]->sock, &fdset);
}
err = tcp_wait(&fdset, max_sock + 1);
if (err == -1) {
tcp_close(server_sock);
return EXIT_FAILURE;
}
printf("Activated\n");
for (int sd = 0; sd <= max_sock; sd++) {
if (FD_ISSET(sd, &fdset)) {
if (sd == server_sock) {
int client_sock = tcp_accept(server_sock);
FD_SET(client_sock, &fdset);
insert_new_client(client_sock, clients_arr);
connected_sockets++;
if (client_sock > max_sock)
max_sock = client_sock;
printf("New client has been connected\n");
} else {
// handle the client connection
Client *c = find_client_ref(sd, clients_arr);
// if no socket was found something terrible has happened
if (c == NULL) {
fprintf(stderr, "Could not find the correct client from FD\n");
tcp_close(server_sock);
exit(EXIT_FAILURE);
}
// only clear this soket if its still connected
if (handleClient(c, clients_arr))
FD_CLR(c->sock, &fdset);
}
}
}
} while (connected_sockets > 0);
printf("Proxy server is closing\n");
tcp_close(server_sock);
return 0;
}