-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
470 lines (463 loc) · 13.4 KB
/
client.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
#include "headers.h"
bool printErrorMessages(errcode errorStatus) // returns true if to close connection
{
if (errorStatus == NETWORK_ERROR)
{
printf("Network error!\n");
return true;
}
else if (errorStatus == WRITER_EXISTS)
{
printf("Some client is writing to the same file!\n");
return false;
}
else if (errorStatus == READER_EXISTS)
{
printf("Some client is reading from the same file!\n");
return false;
}
else if (errorStatus == FILE_NOT_FOUND)
{
printf("File not found!\n");
return false;
}
else if (errorStatus == UNEXPECTED_ERROR)
{
printf("Unexpected error!\n");
return true;
}
else if (errorStatus == FILE_ALREADY_EXISTS)
{
printf("File already exists!\n");
return false;
}
else if (errorStatus == FILE_NOT_WRITABLE)
{
printf("File not writable!\n");
return false;
}
else if (errorStatus == FILE_NOT_READABLE)
{
printf("File not readable!\n");
return false;
}
else if (errorStatus == FILE_SIZE_EXCEEDED)
{
printf("File size exceeded!\n");
return false;
}
else if (errorStatus == FILE_UNABLE_TO_CREATE)
{
printf("File unable to create!\n");
return false;
}
else if (errorStatus == DIRECTORY_UNABLE_TO_CREATE)
{
printf("Directory unable to create!\n");
return false;
}
else if (errorStatus == METADATA_INACESSIBLE)
{
printf("Metadata inaccessible!\n");
return false;
}
else if (errorStatus == DELETE_FAILED)
{
printf("Delete failed!\n");
return false;
}
else if (errorStatus == UNABLE_TO_DELETE)
{
printf("Unable to delete!\n");
return false;
}
else if (errorStatus == UNABLE_TO_COPY)
{
printf("Unable to copy!\n");
return false;
}
else if (errorStatus == INVALID_OPERATION)
{
printf("Invalid operation!\n");
return false;
}
else if (errorStatus == NO_SUCH_PATH)
{
printf("Invalid file path!\n");
return false;
}
else if (errorStatus == PERMISSION_DENIED)
{
printf("Permission denied!\n");
return false;
}
else if (errorStatus == INSUFFICIENT_STORAGE)
{
printf("Insufficient storage!\n");
return false;
}
else if (errorStatus == CLIENT_ALREADY_WRITING)
{
printf("Client already writing!\n");
return false;
}
else if (errorStatus == CONCURRENT_ACCESS_TIMEOUT)
{
printf("Concurrent access timeout!\n");
return false;
}
else if (errorStatus == FILE_IN_USE)
{
printf("File in use!\n");
return false;
}
else if(errorStatus == NO_ERROR)
{
return false;
}
else
{
printf("Unknown error!\n");
return false;
}
}
void printOperationMessage(MessageClient2NM message)
{
switch (message.operation)
{
case CREATE:
if (message.isADirectory)
{
printf("Selected Operation: CREATE - Create a new directory\n");
}
else
{
printf("Selected Operation: CREATE - Create a new file\n");
}
break;
case READ:
printf("Selected Operation: READ - Read the content of a file\n");
break;
case WRITE:
printf("Selected Operation: WRITE - Write data to a file\n");
break;
case DELETE:
if (message.isADirectory)
{
printf("Selected Operation: DELETE - Delete a directory\n");
}
else
{
printf("Selected Operation: DELETE - Delete a file\n");
}
break;
case COPY:
printf("Selected Operation: COPY - Copy from one directory to another\n");
break;
case METADATA:
printf("Selected Operation: METADATA - Get metadata information about a file\n");
break;
case TERMINATE:
printf("Selected Operation: TERMINATE - Terminate Client\n");
break;
default:
printf("Invalid Operation Number\n");
}
}
int printConnectionRequest(int acknowledgement)
{
switch (acknowledgement)
{
case INITIAL_ACK_ACCEPT:
printf("Server has accepted the connection request. Connection established!\n");
return 1;
case INITIAL_ACK_UNSUPPORTED_CLIENT:
printf("Server has rejected the connection request. The client is not supported.\n");
return 0;
case INITIAL_ACK_NO_SS_CONNECTED:
printf("Server has acknowledged the connection, but no Storage Server is currently connected.\n");
return 0;
default:
printf("Unable to infer the server's response. Terminating connection...\n");
}
return 0;
}
void printMetadata(metadata *metadata)
{
printf("I-node number: %lu\n", metadata->inodeNumber);
printf("Mode (permissions: octal): %lo (octal)\n", metadata->mode);
printf("Link count: %lu\n", metadata->linkCount);
printf("Ownership: UID=%lu GID=%lu\n", metadata->uid, metadata->gid);
printf("File size: %ld bytes\n", metadata->fileSize);
printf("Preferred I/O block size: %ld bytes\n", metadata->preferredBlockSize);
printf("Blocks allocated: %ld\n", metadata->blocksAllocated);
printf("Last file access: %s", ctime(&metadata->lastFileAccess));
printf("Last file modification: %s", ctime(&metadata->lastFileModification));
printf("Last status change: %s", ctime(&metadata->lastStatusChange));
return;
}
errcode handleReadCommunication(int socketSS)
{
MessageClient2SS message;
int bytesRead;
while ((bytesRead = recv(socketSS, &message, sizeof(message), 0)) > 0)
{
fwrite(message.msg, 1, message.bytesToRead, stdout);
}
if (bytesRead < 0)
{
closeSocket(socketSS);
return NETWORK_ERROR;
}
return NO_ERROR;
}
errcode handleWriteInput(MessageClient2NM *message)
{
getchar();
printf("Enter text to write into file (terminate by pressing enter twice!)\n");
char input = '\0';
char prevInput = '\0';
int currIndex = 0;
while (1)
{
if (currIndex == SEND_SIZE - 1)
{
fprintf(stderr, "Input limit exceeded\n");
exit(1);
}
input = getchar();
message->msg[currIndex] = input;
if (input == '\n' && prevInput == '\n')
{
message->msg[currIndex] = '\0';
break;
}
currIndex++;
prevInput = input;
}
message->bytesToRead = currIndex;
return NO_ERROR;
}
errcode handleMetadataCommunication(int socketSS)
{
metadata fileInfo;
if (recv(socketSS, &fileInfo, sizeof(fileInfo), 0) < 0)
{
fprintf(stderr, RED "[-]Receive error: %s\n" RESET, strerror(errno)); // ERROR HANDLING
return NETWORK_ERROR;
}
printMetadata(&fileInfo);
return NO_ERROR;
}
errcode receivePriviledgedConfirmation(int socketNM)
{
errcode errorName;
if (recv(socketNM, &errorName, sizeof(errorName), 0) < 0)
{
fprintf(stderr, RED "[-]Receive error: %s\n" RESET, strerror(errno));
return NETWORK_ERROR;
}
return errorName;
}
errcode handleSSCommunication(int socketNM, MessageClient2SS message)
{
int ss_client_port;
if (recv(socketNM, &ss_client_port, sizeof(ss_client_port), 0) < 0)
{
fprintf(stderr, RED "[-]Receive error: %s\n" RESET, strerror(errno));
return NETWORK_ERROR;
}
if (ss_client_port == NO_SUCH_PATH)
{
return FILE_NOT_FOUND;
}
struct sockaddr_in addr;
int socketSS = initSocket();
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = ss_client_port;
addr.sin_addr.s_addr = inet_addr(ip_address);
if (connect(socketSS, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
fprintf(stderr, RED "[-]Connection error: %s\n" RESET, strerror(errno));
return NETWORK_ERROR;
}
printf("Connected to the storage server.\n");
if (message.operation == WRITE)
{
handleWriteInput(&message);
}
if (send(socketSS, &message, sizeof(message), 0) < 0)
{
fprintf(stderr, RED "[-]Send error: %s\n" RESET, strerror(errno));
return NETWORK_ERROR;
}
int err_code;
if (recv(socketSS, &err_code, sizeof(err_code), 0) < 0)
{
fprintf(stderr, RED "[-]Receive error: %s\n" RESET, strerror(errno));
return NETWORK_ERROR;
}
if (message.operation == WRITE)
{
if (send(socketNM, &err_code, sizeof(err_code), 0) < 0)
{
fprintf(stderr, RED "[-]Send error: %s\n" RESET, strerror(errno));
return NETWORK_ERROR;
}
}
if (err_code != NO_ERROR)
{
printf("Error code: %d\n", err_code);
return err_code;
}
else
{
printf("Operation successful\n");
}
switch (message.operation)
{
case READ:
return handleReadCommunication(socketSS);
case WRITE:
return NO_ERROR;
case METADATA:
return handleMetadataCommunication(socketSS);
default:
return UNEXPECTED_ERROR;
}
return NO_ERROR;
}
void askFileOrDirectory(MessageClient2NM *message)
{
char CHAR_BUFF;
getchar();
while (1)
{
printf("Enter F for file and D for directory\n");
scanf("%c", &CHAR_BUFF);
// getchar();
if (CHAR_BUFF == 'F' || CHAR_BUFF == 'D' || CHAR_BUFF == 'f' || CHAR_BUFF == 'd')
{
message->isADirectory = (CHAR_BUFF == 'D' || CHAR_BUFF == 'd') ? true : false;
return;
}
else
{
printf("Invalid request!\n");
}
}
}
int main()
{
struct sockaddr_in addr;
MessageClient2NM message;
errcode errorStatus;
int mySocket = initSocket();
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = nms_client_port;
addr.sin_addr.s_addr = inet_addr(ip_address);
if (connect(mySocket, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
fprintf(stderr, RED "[-]Connection error: %s\n" RESET, strerror(errno));
exit(1);
}
printf("Connected to the server.\n");
// initial request to server
int initialRequest = INITIAL_MESSAGE;
if (send(mySocket, &initialRequest, sizeof(initialRequest), 0) < 0)
{
fprintf(stderr, RED "[-]Send error: %s\n" RESET, strerror(errno));
if (close(mySocket) < 0)
fprintf(stderr, "[-]Error closing socket: %s\n", strerror(errno));
exit(1);
}
// acknowledgment
int initialAck;
if (recv(mySocket, &initialAck, sizeof(initialAck), 0) < 0)
{
fprintf(stderr, "[-]Receive error: %s\n", strerror(errno));
if (close(mySocket) < 0)
fprintf(stderr, "[-]Error closing socket: %s\n", strerror(errno));
exit(1);
}
if (!printConnectionRequest(initialAck))
{
closeSocket(mySocket);
return 0;
}
while (1)
{
// We prompt user for operation number (1 through 6)
printf("\nEnter operation number (1-7):\n");
printf("1. CREATE - Create a new file/folder\n");
printf("2. READ - Read the content of a file\n");
printf("3. WRITE - Write data to a file\n");
printf("4. DELETE - Delete a file/folder\n");
printf("5. COPY - Copy a file/folder\n");
printf("6. METADATA - Get metadata information about a file\n");
printf("7. TERMINATE - Terminate connection\n");
if (scanf("%d", &message.operation) != 1 || message.operation < 1 || message.operation > 7)
{
fprintf(stderr, "[-]Invalid operation number\n");
continue;
}
if (message.operation == CREATE || message.operation == DELETE)
{
askFileOrDirectory(&message);
}
printOperationMessage(message);
if (message.operation == COPY)
{
printf("Enter source path: ");
scanf("%s", message.buffer);
printf("Enter destination path: ");
scanf("%s", message.msg);
}
else if (message.operation != TERMINATE)
{
printf("Enter path: ");
scanf("%s", message.buffer);
}
// Send operation number to the server
if (send(mySocket, &message, sizeof(message), 0) < 0)
{
fprintf(stderr, "[-]Send error: %s\n", strerror(errno));
// close(mySocket);
// exit(1);
}
if (message.operation == TERMINATE)
{
break;
}
// non-priviledged
else if (message.operation == READ || message.operation == WRITE || message.operation == METADATA)
{
errorStatus = handleSSCommunication(mySocket, message);
if (printErrorMessages(errorStatus))
{
break;
}
else
{
continue;
}
}
// priviledged
else if (message.operation == CREATE || message.operation == DELETE || message.operation == COPY)
{
errorStatus = receivePriviledgedConfirmation(mySocket);
if (printErrorMessages(errorStatus))
{
break;
}
else
{
continue;
}
}
}
closeSocket(mySocket);
return 0;
}