-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclientw24.c
More file actions
479 lines (445 loc) · 14.2 KB
/
clientw24.c
File metadata and controls
479 lines (445 loc) · 14.2 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
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <math.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#define PORT 8081
// function to create socket
int create_socket()
{
// Create socket (Same family and socket type as server)
int client_socket;
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
return client_socket;
}
// function to connect to server
void connect_to_server(int client_socket, int portNumber)
{
// Address object
struct sockaddr_in server_address;
// Update server IP in address object
if (inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr) <= 0)
{
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// setting address family and port
server_address.sin_family = AF_INET;
server_address.sin_port = htons(portNumber);
// ARGS (Socket descriptor, address object, size of the address object)
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
}
// function to receive tarfiles from server to client
void receiveFileHelper(int client_socket)
{
char buffer[1024] = { 0 };
// printf("insdie\n");
// Reading size of file
read(client_socket, buffer, 32);
// converting the binary string to int
int fileSize = atoi(buffer);
// printf("Start sequence: %s %d\n", buffer, fileSize);
memset(buffer, 0, sizeof(buffer));
// Creating and opening the file for writing
char path[100];
snprintf(path, sizeof(path), "%s%s", getenv("HOME"), "/w24project");
mkdir(path, 0777);
char outputFilename[500];
snprintf(outputFilename, sizeof(path), "%s%s", getenv("HOME"), "/w24project/temp.tar.gz");
int output_fd = open(outputFilename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (output_fd == -1)
{
perror("Error creating output file");
exit(EXIT_FAILURE);
}
// Writing the file data
for (int i = 0; i < fileSize; i++)
{
read(client_socket, buffer, 1);
// printf("Data: %s %d %d\n", buffer,i,fileSize);
write(output_fd, buffer, 1);
memset(buffer, 0, sizeof(buffer));
}
printf("done\n");
close(output_fd);
}
// function to receive string data from server to client
char* receiveDataHelper(int client_socket)
{
char buffer[1024] = { 0 };
// Reading size of file
read(client_socket, buffer, 32);
// converting the binary string to int
int fileSize = atoi(buffer);
// printf("Start sequence: %s %d\n", buffer, fileSize);
memset(buffer, 0, sizeof(buffer));
// Creating and opening the file for writing
// Writing the file data
char file_data[fileSize + 1];
//
for (int i = 0; i < fileSize; i++)
{
read(client_socket, buffer, 1);
// printf("%s", buffer);
file_data[i] = buffer[0];
memset(buffer, 0, sizeof(buffer));
}
file_data[fileSize] = '\0';
// printf("%s\n", file_data);
return strdup(file_data);
}
// function to addZeros to given number and makes it to 32 bit format
char* addZeros(int num)
{
char* num_str = (char*)malloc(33 * sizeof(char)); // Allocate memory for string, including null terminator
sprintf(num_str, "%032d", num); // Format the integer with leading zeros
return num_str;
}
// Helper method to remove spaces from a string
char* stripSpaces(char* word)
{
// Skip leading spaces
while (isspace(*word))
word++;
if (*word == '\0') // If the string is empty or contains only spaces
return word;
// Trim trailing spaces
char* end = word + strlen(word) - 1;
while (end > word && isspace(*end))
end--;
// Null-terminate the trimmed string
*(end + 1) = '\0';
return word;
}
//function to count spaces
int countSpaces(const char* str) {
int count = 0;
while (*str != '\0') {
if (*str == ' ') {
count++;
}
str++;
}
return count;
}
// checking substring . in a string
int hasDot(const char* filename) {
while (*filename != '\0') {
if (*filename == '.') {
return 1;
}
filename++;
}
return 0;
}
//split string based on a given delimiter
char** splitString(char* str, char* delimenter)
{
int count = 0;
char** tokens = (char**)malloc(10 * sizeof(char*));
if (tokens == NULL)
{
printf("Memory allocation error\n");
return NULL;
}
// tokenize the string
char* token = strtok(str, delimenter);
while (token != NULL)
{
// allocate memory
tokens[count] = (char*)malloc((strlen(token) + 1) * sizeof(char));
if (tokens[count] == NULL)
{
fprintf(stderr, "Memory allocation error\n");
for (int i = 0; i < count; i++)
free(tokens[i]);
free(tokens);
return NULL;
}
// remove any extra spaces
strcpy(tokens[count], stripSpaces(token));
count++;
token = strtok(NULL, delimenter);
}
// add NULL to indicate end
tokens[count] = NULL;
return tokens;
}
// check if date format taken as input is valid or not
int isValidDateFormat(char* input) {
char** date = splitString(input, "-");
if (date[0] == NULL || date[1] == NULL || date[2] == NULL)
{
return 0;
}
if(strlen(date[0])!=4){
return 0;
}
if(strlen(date[1])!=2 || atoi(date[1]) < 0 || atoi(date[1]) > 12 ){
return 0;
}
if(strlen(date[2])!=2 || atoi(date[2]) < 0 || atoi(date[2]) > 31 ){
return 0;
}
return 1; // Format is valid
}
// check if input is an integer or not
int isInteger(const char* input) {
char c;
int num;
return sscanf(input, "%d%c", &num, &c) == 1;
}
// client side validation for all commands before sending it into the server side
int checkCommand(char* command) {
// Tokenize the command to check the first part
char* token = strdup(command);
char** result = splitString(token, " ");
if (strstr(token, "dirlist") != NULL) {
if(result[2]!=NULL){
printf("Usage: dirlist [-a|-t]\n");
return 0;
}
else if (strcmp(result[0], "dirlist") != 0) {
printf("Did you mean dirlist?\n");
}
if (result[1] != NULL && strcmp(result[1], "-a") == 0) {
return 1;
}
else if (result[1] != NULL && strcmp(result[1], "-t") == 0) {
return 1;
}
else {
printf("Usage: dirlist [-a|-t]\n");
return 0;
}
}
else if (strstr(token, "w24fn") != NULL || strstr(token, "w24fz") != NULL || strstr(token, "w24ft") != NULL || strstr(token, "w24fdb") != NULL || strstr(token, "w24fda") != NULL) {
if (strstr(token, "w24fn") != NULL) {
if (strcmp(result[0], "w24fn") != 0) {
printf("Did you mean w24fn?\n");
}
// No file present
if (result[1] == NULL) {
printf("Usage: w24fn [filename]; requires atleast 1 file");
return 0;
}
// More than one file present
if (result[2] != NULL) {
printf("Usage: w24fn [filename]; more than one file provided");
return 0;
}
// More than one file present
if (!hasDot(result[1])) {
printf("Usage: w24fn [filename]; Please Provide Valid Filename");
return 0;
}
}
else if (strstr(token, "w24fz") != NULL) {
if (strcmp(result[0], "w24fz") != 0) {
printf("Did you mean w24fz?\n");
}
// No file present
if (result[1] == NULL) {
printf("Usage: w24fz [size1 size2]; requires atleast 2 sizes");
return 0;
}
// More than one file present
if (result[2] == NULL) {
printf("Usage: w24fz [size1 size2]; requires atleast 2 sizes");
return 0;
}
// More than one file present
if (result[3] != NULL) {
printf("Usage: w24fz [size1 size2]; more than 2 file sizes given");
return 0;
}
if (!isInteger(result[1])) {
printf("Usage: w24fz [size1 size2]; size 1 not in int");
return 0;
}
if (!isInteger(result[2])) {
printf("Usage: w24fz [size1 size2]; size 2 not in int");
return 0;
}
int size1, size2;
size1 = atoi(result[1]);
size2 = atoi(result[2]);
if (size1 < 0) {
printf("Usage: w24fz [size1 size2]; size 1 should be greater than 0");
return 0;
}
if (size1 > size2) {
printf("Usage: w24fz [size1 size2]; size 2 should be greater than size 1");
return 0;
}
}
else if (strstr(token, "w24ft") != NULL) {
if (strcmp(result[0], "w24ft") != 0) {
printf("Did you mean w24ft?\n");
}
if (result[1] == NULL) {
printf("Usage: w24fz [ext 1, ext 2, ext 3]; requires atleast 1 extension");
return 0;
}
// More than 4 extension not allowed
if (result[4] != NULL) {
printf("Usage: w24fz [ext 1, ext 2, ext 3]; support only upto 3 extension");
return 0;
}
}
else if (strstr(token, "w24fdb") != NULL) {
if (strcmp(result[0], "w24fdb") != 0) {
printf("Did you mean w24fdb?\n");
}
// Only 1 allowed
if (result[1] == NULL) {
printf("Usage: w24fdb [date]; No date provided");
return 0;
}
// More than 4 extension not allowed
if (result[2] != NULL) {
printf("Usage: w24fdb [date]; support only upto 3 extension");
return 0;
}
if (!isValidDateFormat(result[1])) {
printf("Usage: w24fdb [date]; enter a valid date in format YYYY-MM-DD");
return 0;
}
}
else if (strstr(token, "w24fda") != NULL) {
if (strcmp(result[0], "w24fda") != 0) {
printf("Did you mean w24fda?\n");
}
// Only 1 allowed
if (result[1] == NULL) {
printf("Usage: w24fda [date]; No date provided");
return 0;
}
// Only one date allowed
if (result[2] != NULL) {
printf("Usage: w24fda [date]; supports only one date");
return 0;
}
if (!isValidDateFormat(result[1])) {
printf("Usage: w24fda [date]; enter a valid date in format YYYY-MM-DD");
return 0;
}
}
}
else if (strcmp(token, "quitc") == 0) {
return 1;
// printf("Valid command: %s\n", command);
}
else {
printf("Usage:\n");
printf("dirlist -a\n");
printf("dirlist -t\n");
printf("w24fn filename\n");
printf("w24fz size1 size2\n");
printf("w24ft <extension list>\n");
printf("w24fdb date\n");
printf("w24fda date\n");
printf("quitc\n");
return 0;
}
return 1;
}
int main()
{
int client_socket = create_socket();
connect_to_server(client_socket, PORT);
send(client_socket, "0", 1, 0);
char* newPortStr = (receiveDataHelper(client_socket));
int newPort = atoi(newPortStr);
close(client_socket);
client_socket = create_socket();
connect_to_server(client_socket, newPort);
char* serverPort;
asprintf(&serverPort, "%d", PORT);
// Only send to server1 to indicate request and not load balancing
if(strcmp(newPortStr, serverPort)==0)
{
send(client_socket, "1", 1, 0);
}
char command[1024];
while (1)
{
printf("\nclient24$ ");
fgets(command, 1024, stdin);
if (checkCommand(command) == 0) {
continue;
}
// printf("user entered %s\n", command);
// Send length of the command to be received
send(client_socket, addZeros(strlen(command)), 32, 0);
// Send command to server
send(client_socket, command, strlen(command), 0);
if (strstr(command, "w24fn") != NULL)
{
printf("%s", receiveDataHelper(client_socket));
}
else if (strstr(command, "dirlist -t") != NULL)
{
printf("%s", receiveDataHelper(client_socket));
}
else if (strstr(command, "dirlist -a") != NULL)
{
printf("%s", receiveDataHelper(client_socket));
}
else if (strstr(command, "quitc") != NULL)
{
close(client_socket);
return 0;
}
else if (strstr(command, "w24ft") != NULL)
{
if (strcmp(receiveDataHelper(client_socket), "no") == 0)
{
printf("No file found\n");
continue;
}
receiveFileHelper(client_socket);
}
else if (strstr(command, "w24fz") != NULL)
{
if (strcmp(receiveDataHelper(client_socket), "no") == 0)
{
printf("No file found\n");
continue;
}
receiveFileHelper(client_socket);
}
else if (strstr(command, "w24fda") != NULL)
{
if (strcmp(receiveDataHelper(client_socket), "no") == 0)
{
printf("No file found\n");
continue;
}
receiveFileHelper(client_socket);
}
else if (strstr(command, "w24fdb") != NULL)
{
if (strcmp(receiveDataHelper(client_socket), "no") == 0)
{
printf("No file found\n");
continue;
}
receiveFileHelper(client_socket);
}
}
}