-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageserver.c
524 lines (492 loc) · 15.3 KB
/
storageserver.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
518
519
520
521
522
523
524
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>
#include "server_functionalities.h"
int CCSSportNo;
int NMSSportNo;
#define MAX_AX_PATHS 100
#define PATH_LENGTH 1024
#define max_clients 2000
#define SIZEOFNAME 128
char HomeDirectory[128] = "";
typedef struct response
{
int responsevalue; // 1 : Success , 0 : Failure , 2 : FileNotFound , 3 : FileFound , 4 : Being Written Onto
} Response;
typedef struct client_Arg
{
int ServerSock;
struct sockaddr_in ServerAddress;
} client_Arg;
typedef struct storage_server_info
{
char ip[15];
int NMSSport;
int SSCLport;
int sizeoffile_paths;
char file_paths[MAX_AX_PATHS][PATH_LENGTH];
} storage_server_info;
typedef struct input
{
char oper[50];
char path1[SIZEOFNAME];
char path2[SIZEOFNAME];
int FileorDirec;
} request;
void *client(void *arg)
{
int client_sock = *(int *)arg;
char buffer[1024];
bzero(buffer, 1024);
request received_request;
recv(client_sock, buffer, BUFFER_SIZE, 0);
memcpy(&received_request, buffer, sizeof(received_request));
char path[128];
strcpy(path, HomeDirectory);
strcat(path, "/");
strcat(path, received_request.path1);
strcpy(received_request.path1, path);
printf("Received struct from client:\n");
printf("Name: %s\n", received_request.oper);
printf("Address1: %s\n", received_request.path1);
printf("Address1: %s\n", received_request.path2);
// create New Thread
// Handle the request
// send response
if (strcmp(received_request.oper, "read") == 0)
{
sc_read(client_sock, received_request.path1);
}
else if (strcmp(received_request.oper, "write") == 0)
{
sc_write(client_sock, received_request.path1);
}
else if (strcmp(received_request.oper, "get") == 0)
{
sc_permission(client_sock, received_request.path1);
}
else if (strcmp(received_request.oper, "replicate") == 0)
{
if (received_request.FileorDirec == 0)
{
printf("%s\n", received_request.path2);
make_directory("R1");
// Open the file to write received data
FILE *filePtr = fopen("a.zip", "wb");
if (filePtr == NULL)
{
perror("File opening failed");
// return -1;
}
ssize_t bytesRead;
char buffer[BUFFER_SIZE];
// bzero(buffer, BUFFER_SIZE);
// Receive file data and write to the file
printf("hannn\n");
while ((bytesRead = recv(client_sock, buffer, BUFFER_SIZE, 0)) > 0)
{
// printf("%s\n",buffer);
// if (bytesRead >= 4 && memcmp(buffer + bytesRead - 4, "END\n", 4) == 0) {
// printf("Received termination signal from sender. Ending receiving.\n");
// printf("yesss\n");
// break; // Terminate the receiving loop
// }
// if (bytesRead == 4 && memcmp(buffer, "END\n", 4) == 0) {
// printf("Received termination signal from sender. Ending receiving.\n");
// printf("yesss\n");
// break;
// }
fwrite(buffer, 1, bytesRead, filePtr);
}
printf("bass\n");
// Close the file and socket
fclose(filePtr);
// close(client_sock);
unzip("a.zip", "R1");
delete_file("a.zip");
}
else
{
char *file_name = zip_curr_dir();
request info;
char fname[SIZEOFNAME];
strcpy(fname, file_name);
char *f = strtok(fname, ".");
strcpy(info.path2, f);
strcpy(info.oper, "replicate");
send(client_sock, &info, BUFFER_SIZE, 0);
FILE *filePtr = fopen(file_name, "rb");
if (filePtr == NULL)
{
perror("File opening failed");
// return -1;
}
char buff[BUFFER_SIZE];
// Read file data and send to client
ssize_t bytesRead;
while ((bytesRead = fread(buff, 1, BUFFER_SIZE, filePtr)) > 0)
{
send(client_sock, buff, bytesRead, 0);
}
// Close file and socket
fclose(filePtr);
// close(client_sock)
delete_file(file_name);
}
}
printf("socket: %d\n", client_sock);
close(client_sock);
}
void *client_handler(void *arg)
{
int socket_for_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socket_for_client < 0)
{
perror("Socket Failed");
exit(1);
}
int client_sock;
struct sockaddr_in server_addr, client_addr;
memset(&server_addr, '\0', sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(CCSSportNo);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
char buffer[1024];
if (bind(socket_for_client, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("Bind Failed");
exit(1);
}
listen(socket_for_client, 5);
printf("Listening for Clients...\n");
pthread_t threads[max_clients];
int i = 0;
while (i < max_clients)
{
int ClientLength = sizeof(client_addr);
bzero((char *)&client_addr, ClientLength);
int client_sock = accept(socket_for_client, (struct sockaddr *)&client_addr, &ClientLength);
printf("[+]SS connected.\n");
if (pthread_create(&threads[i], NULL, client, &client_sock) != 0)
{
fprintf(stderr, "Error creating thread\n");
// return 1;
}
// bzero(buffer, 1024);
// request received_request;
// read(client_sock, buffer, sizeof(received_request));
// memcpy(&received_request, buffer, sizeof(received_request));
// printf("Received struct from client:\n");
// printf("Name: %s\n", received_request.oper);
// printf("Address1: %s\n", received_request.path1);
// printf("Address1: %s\n", received_request.path2);
// create New Thread
// Handle the request
// send response
// close(client_sock);
i++;
}
for (int j = 0; j < max_clients; j++)
{
if (pthread_join(threads[j], NULL) != 0)
{
fprintf(stderr, "Error joining thread\n");
// return ;
}
}
}
void *NaminHandler(void *arg)
{
int client_sock = *(int *)arg;
request Request;
if (recv(client_sock, &Request, sizeof(Request), 0) == -1)
{
perror("recv failed");
exit(1);
}
if (!strcmp(Request.oper, "create"))
{
char path[128];
strcpy(path, HomeDirectory);
strcat(path, "/");
strcat(path, Request.path1);
strcpy(Request.path1, path);
strcat(Request.path1, "/");
strcat(Request.path1, Request.path2);
int answer = -1;
if (Request.FileorDirec == 1)
{
answer = make_file(Request.path1);
}
else if (Request.FileorDirec == 2)
{
answer = make_directory(Request.path1);
}
Response Resp;
Resp.responsevalue = answer;
if (send(client_sock, &Resp, sizeof(Resp), 0) == -1)
{
perror("Sending Response Failed");
exit(1);
}
}
else if (!strcmp(Request.oper, "delete"))
{
char path[128];
strcpy(path, HomeDirectory);
strcat(path, "/");
strcat(path, Request.path1);
strcpy(Request.path1, path);
int answer = -1;
if (Request.FileorDirec == 1)
{
answer = delete_file(Request.path1);
}
else if (Request.FileorDirec == 2)
{
answer = delete_directory(Request.path1);
}
Response Resp;
Resp.responsevalue = answer;
if (send(client_sock, &Resp, sizeof(Resp), 0) == -1)
{
perror("Sending Response Failed");
exit(1);
}
}
else if (!strcmp(Request.oper, "replicate"))
{
printf("inside\n");
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in ss_address;
ss_address.sin_family = AF_INET;
ss_address.sin_port = htons(Request.FileorDirec);
if (inet_pton(AF_INET, Request.path1, &ss_address.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
if (connect(client_socket, (struct sockaddr *)&ss_address, sizeof(ss_address)) == -1)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
request info;
char *file_name = zip_curr_dir();
// printf("%s\n", file_name);
strcpy(info.oper, "replicate");
char fname[SIZEOFNAME];
strcpy(fname, file_name);
char *f = strtok(fname, ".");
strcpy(info.path2, f);
printf("%s\n", info.path2);
info.FileorDirec = 0;
send(client_socket, &info, BUFFER_SIZE, 0);
FILE *filePtr = fopen(file_name, "rb");
if (filePtr == NULL)
{
perror("File opening failed");
// return -1;
}
char buff[BUFFER_SIZE];
// Read file data and send to client
size_t bytesRead;
while ((bytesRead = fread(buff, 1, BUFFER_SIZE, filePtr)) > 0)
{
// printf("%s\n",buff);
send(client_socket, buff, bytesRead, 0);
}
// strcpy(buff,"#52");
// send(client_socket, buff, 3, 0);
// printf("endd\n");
// const char *terminationMsg = "END\n";
// send(client_socket, terminationMsg, strlen(terminationMsg), 0);
printf("endd\n");
// Close file and socket
fclose(filePtr);
close(client_socket);
delete_file(file_name);
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
if (connect(client_socket, (struct sockaddr *)&ss_address, sizeof(ss_address)) == -1)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
info.FileorDirec = 1;
send(client_socket, &info, BUFFER_SIZE, 0);
request info2;
recv(client_socket, buff, BUFFER_SIZE, 0);
memcpy(&info2, buff, sizeof(info2));
printf("%s\n", info2.path2);
make_directory("R2");
// // Open the file to write received data
filePtr = fopen("a.zip", "wb");
if (filePtr == NULL)
{
perror("File opening failed");
// return -1;
}
// ssize_t bytesRead;
char buffer[BUFFER_SIZE];
// bzero(buffer, BUFFER_SIZE);
// Receive file data and write to the file
while ((bytesRead = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0)
{
fwrite(buffer, 1, bytesRead, filePtr);
}
// Close the file and socket
fclose(filePtr);
close(client_socket);
unzip("a.zip", "R2");
delete_file("a.zip");
}
close(client_sock);
return NULL;
}
void *NamingServerListener()
{
int NamingSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (NamingSock < 0)
{
perror("Naming Server Listener : Socket Creation Failed");
exit(1);
}
struct sockaddr_in Storageserver_addr, client_addr;
char buffer[1024];
int n;
memset(&Storageserver_addr, '\0', sizeof(Storageserver_addr));
Storageserver_addr.sin_family = AF_INET;
Storageserver_addr.sin_port = htons(NMSSportNo);
Storageserver_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(NamingSock, (struct sockaddr *)&Storageserver_addr, sizeof(Storageserver_addr)) < 0)
{
perror("[-]Bind error");
exit(1);
}
listen(NamingSock, 5);
printf("Listening...\n");
while (1)
{
int ClientLength = sizeof(client_addr);
bzero((char *)&client_addr, ClientLength);
int client_sock = accept(NamingSock, (struct sockaddr *)&client_addr, &ClientLength);
printf("[+]SS connected. zoya\n");
pthread_t Thread_ID;
if (pthread_create(&Thread_ID, NULL, NaminHandler, &client_sock) != 0)
{
perror("Thread Creation Error");
exit(1);
}
}
return NULL;
}
int main(int argc, char *argv[])
{
CCSSportNo = atoi(argv[1]);
NMSSportNo = atoi(argv[2]);
getcwd(HomeDirectory, 128);
char *ip = "127.0.0.1";
int port = 5566;
int sock;
struct sockaddr_in Naming_Server_Addr;
socklen_t addr_size;
int n;
char **AccessiblePaths = (char **)malloc(sizeof(char *) * 2);
for (int i = 0; i < 2; i++)
{
AccessiblePaths[i] = (char *)malloc(sizeof(char) * 1024);
}
int currentsize = 2;
char path[1024];
int i = 0;
while (1)
{
fgets(path, sizeof(path), stdin);
if (path[0] == '\n')
{
break;
}
else
{
path[strlen(path) - 1] = '\0';
strcpy(AccessiblePaths[i++], path);
if (i >= currentsize)
{
currentsize = 2 * currentsize;
AccessiblePaths = (char **)realloc(AccessiblePaths, sizeof(char *) * currentsize);
for (int j = i; j < currentsize; j++)
{
AccessiblePaths[j] = (char *)malloc(sizeof(char) * 1024);
}
}
}
}
AccessiblePaths = (char **)realloc(AccessiblePaths, sizeof(char *) * i);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0)
{
perror("Socket Error");
exit(1);
}
memset(&Naming_Server_Addr, '\0', sizeof(Naming_Server_Addr));
Naming_Server_Addr.sin_family = AF_INET;
Naming_Server_Addr.sin_port = htons(port);
Naming_Server_Addr.sin_addr.s_addr = inet_addr(ip);
if (connect(sock, (struct sockaddr *)&Naming_Server_Addr, sizeof(Naming_Server_Addr)) == -1)
{
perror("Connect Error");
exit(1);
}
storage_server_info details;
strcpy(details.ip, ip);
details.NMSSport = NMSSportNo;
details.SSCLport = CCSSportNo;
details.sizeoffile_paths = i;
for (int k = 0; k < i; k++)
{
strcpy(details.file_paths[k], AccessiblePaths[k]);
}
if (send(sock, &details, sizeof(details), 0) == -1)
{
perror("Send Error");
exit(1);
}
close(sock);
pthread_t NamingServerThread;
if (pthread_create(&NamingServerThread, NULL, (void *)&NamingServerListener, NULL) != 0)
{
perror("Pthread Error");
exit(1);
}
pthread_t ClientsListeningThread;
if (pthread_create(&ClientsListeningThread, NULL, (void *)client_handler, NULL) != 0)
{
perror("Pthread Error : Client Thread");
exit(1);
}
if (pthread_join(NamingServerThread, NULL) != 0)
{
perror("pthread join");
exit(EXIT_FAILURE);
}
if (pthread_join(ClientsListeningThread, NULL) != 0)
{
perror("pthread join");
exit(EXIT_FAILURE);
}
printf("Disconnected from the naming server.\n");
return 0;
}