Repository navigation
Expand file tree
/
Copy pathServer.c
More file actions
600 lines (492 loc) · 15.3 KB
/
Copy pathServer.c
File metadata and controls
600 lines (492 loc) · 15.3 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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// Including necessary libraries
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
extern int errno;
typedef struct Message_Struct
{
uint8_t Transmission_Type;
uint16_t Length_of_Name;
uint64_t Size_of_File;
unsigned char* File;
unsigned char* Name_of_File;
}Message_Struct;
uint8_t Get_Transmission_Type(int Socket_File_Descriptor)
{
uint8_t Transmission_Type;
for (int i = 0; i < sizeof(Transmission_Type); i++)
{
int Input_Bytes = read(Socket_File_Descriptor, &Transmission_Type, 1);
if (Input_Bytes < 0)
{
if (errno == EINTR)
{
continue;
}
perror(strerror(errno));
exit(-1);
}
}
return Transmission_Type;
}
uint64_t Get_File_Size(int Socket_File_Descriptor)
{
uint64_t Size_of_File;
int Total_Bytes = sizeof(Size_of_File);
int Input_Bytes = 0;
uint64_t* Buffer = &Size_of_File;
while (Input_Bytes < Total_Bytes)
{
int Get_Byte = read(Socket_File_Descriptor, Buffer + Input_Bytes, Total_Bytes - Input_Bytes);
Input_Bytes += Get_Byte;
}
return ntohl(Size_of_File);
}
unsigned char* Get_File(int Socket_File_Descriptor, uint64_t Size_of_File)
{
unsigned char* File = malloc(Size_of_File + 1);
File[Size_of_File] = '\0';
int Input_Bytes = 0;
unsigned char* Current = File;
while (Input_Bytes < Size_of_File)
{
int Get_Byte = read(Socket_File_Descriptor, Current + Input_Bytes, Size_of_File - Input_Bytes);
Input_Bytes += Get_Byte;
}
return File;
}
uint16_t Get_Length_of_Name(int Socket_File_Descriptor)
{
uint16_t Length_of_Name;
int Input_Bytes = 0;
int Total_Bytes = sizeof(Length_of_Name);
uint16_t* Buffer = &Length_of_Name;
while (Input_Bytes < Total_Bytes)
{
int Get_Byte = read(Socket_File_Descriptor, Buffer + Input_Bytes, Total_Bytes - Input_Bytes);
Input_Bytes += Get_Byte;
}
return ntohs(Length_of_Name);
}
char* Get_Name_of_File(int Socket_File_Descriptor, uint16_t Length_of_Name)
{
char* Name_of_File = malloc(Length_of_Name + 1);
Name_of_File[Length_of_Name] = '\0';
int Input_Bytes = 0;
unsigned char* Current = Name_of_File;
while (Input_Bytes < Length_of_Name)
{
int Get_Byte = read(Socket_File_Descriptor, Current + Input_Bytes, Length_of_Name - Input_Bytes);
Input_Bytes += Get_Byte;
}
return Name_of_File;
}
Message_Struct Get_Message(int File_Descriptor)
{
Message_Struct Message;
Message.Transmission_Type = Get_Transmission_Type(File_Descriptor);
Message.Size_of_File = Get_File_Size(File_Descriptor);
Message.File = Get_File(File_Descriptor, Message.Size_of_File);
Message.Length_of_Name = Get_Length_of_Name(File_Descriptor);
Message.Name_of_File = Get_Name_of_File(File_Descriptor, Message.Length_of_Name);
return Message;
}
int Create_Server(uint16_t Port_Number)
{
int Server_Init = 0;
if ((Server_Init = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
char * Error_Text = strerror(errno);
perror(Error_Text);
exit(-1);
}
struct sockaddr_in Server_Address;
Server_Address.sin_family = AF_INET;
Server_Address.sin_addr.s_addr = htonl(INADDR_ANY);
Server_Address.sin_port = htons(Port_Number);
if (bind(Server_Init, (struct sockaddr *) &Server_Address, sizeof(Server_Address)) < 0)
{
char * Error_Text = strerror(errno);
perror(Error_Text);
exit(-1);
}
return Server_Init;
}
bool Check_Valid_Format(unsigned char Format_Type)
{
return Format_Type == 0 || Format_Type == 1;
}
uint16_t Get_First_Format_Size(uint8_t Count)
{
return sizeof(Count) + Count * 2;
}
uint16_t Get_First_Format_Bytes(unsigned char* Current_Position, unsigned char* File_Stop)
{
uint8_t Count = *Current_Position;
unsigned char* End_of_Line = Current_Position + Get_First_Format_Size(Count);
return (End_of_Line > File_Stop) ? -1 : (End_of_Line - Current_Position);
}
uint32_t Get_INT32_String(unsigned char* Start_Position, unsigned char* End_Position)
{
uint8_t Count_Characters = End_Position - Start_Position;
char* INT32_String = malloc(Count_Characters + 1);
INT32_String[Count_Characters] = '\0';
memcpy(INT32_String, Start_Position, Count_Characters);
uint32_t Number = atoi(INT32_String);
free(INT32_String);
return Number;
}
bool Parse_Number(unsigned char* Start_Position, unsigned char* Current_Position)
{
char c = *Current_Position;
uint64_t Input_Bytes = Current_Position - Start_Position;
return !isdigit(c) || Input_Bytes >= 5;
}
uint8_t Get_Second_Format_Size(unsigned char* Current_Position)
{
unsigned char* Start_Position = Current_Position;
while (!Parse_Number(Start_Position, Current_Position))
{
Current_Position++;
}
return Current_Position - Start_Position;
}
bool Test_Line_End(unsigned char c)
{
return Check_Valid_Format(c) || c == '\n';
}
bool Test_Number_End(unsigned char c)
{
return c == ',';
}
uint16_t Get_Second_Format_Bytes(unsigned char* Current_Position, unsigned char* File_Stop)
{
unsigned char* Line_Position = Current_Position;
uint32_t Count = Get_INT32_String(Line_Position, Line_Position + 3);
Line_Position += 3;
for (int i = 0; i < Count; i++)
{
uint8_t Input_Bytes = Get_Second_Format_Size(Line_Position);
Line_Position += Input_Bytes;
if (Test_Line_End(*Line_Position))
{
break;
}
if (!Test_Number_End(*Line_Position))
{
return -1;
}
Line_Position++;
}
return Line_Position - Current_Position;
}
bool Test_Format(unsigned char* Current_Position, unsigned char* End_Position)
{
while (Current_Position < End_Position)
{
uint8_t Format_Type = *Current_Position++;
if (!Check_Valid_Format(Format_Type))
{
return false;
}
int16_t Input_Bytes = (Format_Type == 0) ?
Get_First_Format_Bytes(Current_Position, End_Position) :
Get_Second_Format_Bytes(Current_Position, End_Position) ;
if (Input_Bytes == -1)
{
return false;
}
Current_Position += Input_Bytes;
}
return Current_Position == End_Position;
}
int Write_Socket(int File_Descriptor, void* Data , int Data_Size)
{
while (Data_Size != 0)
{
int Written_Bytes = write(File_Descriptor, Data, Data_Size);
if (Written_Bytes < 0)
{
if (errno == EINTR)
{
Written_Bytes = 0;
}
perror(strerror(errno));
exit(-1);
}
Data_Size -= Written_Bytes;
Data += Written_Bytes;
}
}
uint8_t Get_Number_Digits(uint8_t Count)
{
uint8_t Digit_Count = 0;
while (Count != 0)
{
Digit_Count++;
Count /= 10;
}
return Digit_Count;
}
unsigned char* Convert_Three_Byte_String(uint8_t Count)
{
uint8_t Number_Digits = Get_Number_Digits(Count);
char* String_Number = malloc(3 + 1);
String_Number[3] = '\0';
memset(String_Number, '0', 3);
int Index = 3 - 1;
for (int i = 0; i < Number_Digits; i++)
{
String_Number[Index] = (Count % 10) + '0';
Count /= 10;
}
return String_Number;
}
uint16_t Read_INT16(unsigned char** File)
{
uint16_t Higher_Bits = *(*File)++;
uint8_t Lower_Bits = *(*File)++;
Higher_Bits <<= 8;
uint16_t Read_Result = Higher_Bits | Lower_Bits;
return Read_Result;
}
void Write_File_Unchanged(unsigned char* Current_Position, unsigned char* File_Stop, FILE* File)
{
while (Current_Position < File_Stop)
{
uint8_t Format_Type = *Current_Position++;
if (Format_Type == 0)
{
uint8_t Count = *Current_Position++;
fprintf(File, "%s ", Convert_Three_Byte_String(Count));
for (int i = 0; i < Count - 1; i++)
{
fprintf(File, "%d ", Read_INT16(&Current_Position));
}
fprintf(File, "%d\n", Read_INT16(&Current_Position));
}
else if (Format_Type == 1)
{
char Count[3 + 1];
Count[3] = '\0';
memcpy(Count, Current_Position, 3);
fprintf(File, "%s", Count);
Current_Position += 3;
while (!Check_Valid_Format(*Current_Position))
{
fprintf(File, "%c", *Current_Position++);
}
fprintf(File, "%c", '\n');
}
}
}
void Write_File_First_Second(unsigned char* Current_Position, unsigned char* File_Stop, FILE* File)
{
while (Current_Position < File_Stop)
{
uint8_t Format_Type = *Current_Position++;
if (Format_Type == 0)
{
uint8_t Count = *Current_Position++;
fprintf(File, "%s ", Convert_Three_Byte_String(Count));
for (int i = 0; i < Count - 1; i++)
{
fprintf(File, "%d,", Read_INT16(&Current_Position));
}
fprintf(File, "%d\n", Read_INT16(&Current_Position));
}
else if (Format_Type == 1)
{
char Count[3 + 1];
Count[3] = '\0';
memcpy(Count, Current_Position, 3);
fprintf(File, "%s ", Count);
Current_Position += 3;
while (!Check_Valid_Format(*Current_Position))
{
fprintf(File, "%c", *Current_Position++);
}
fprintf(File, "%c", '\n');
}
}
}
void Write_File_Second_First(unsigned char* Current_Position, unsigned char* File_Stop, FILE* File)
{
while (Current_Position < File_Stop)
{
uint8_t Format_Type = *Current_Position++;
if (Format_Type == 0)
{
uint8_t Count = *Current_Position++;
fprintf(File, "%s ", Convert_Three_Byte_String(Count));
for (int i = 0; i < Count - 1; i++)
{
fprintf(File, "%d ", Read_INT16(&Current_Position));
}
fprintf(File, "%d\n", Read_INT16(&Current_Position));
}
else if (Format_Type == 1)
{
char Count[3 + 1];
Count[3] = '\0';
memcpy(Count, Current_Position, 3);
fprintf(File, "%s ", Count);
Current_Position += 3;
while (!Check_Valid_Format(*Current_Position))
{
char c = *Current_Position++;
if (c == ',')
{
fprintf(File, "%c", ' ');
}
else
{
fprintf(File, "%c", c);
}
}
fprintf(File, "%c", '\n');
}
}
}
void Write_File_Swap(unsigned char* Current_Position, unsigned char* File_Stop, FILE* File)
{
while (Current_Position < File_Stop)
{
uint8_t Format_Type = *Current_Position++;
if (Format_Type == 0)
{
uint8_t Count = *Current_Position++;
fprintf(File, "%s ", Convert_Three_Byte_String(Count));
for (int i = 0; i < Count - 1; i++)
{
fprintf(File, "%d,", Read_INT16(&Current_Position));
}
fprintf(File, "%d\n", Read_INT16(&Current_Position));
}
else if (Format_Type == 1)
{
char Count[3 + 1];
Count[3] = '\0';
memcpy(Count, Current_Position, 3);
printf("Amount: %s\n", Count);
printf("3\n");
fprintf(File, "%s ", Count);
printf("4\n");
Current_Position += 3;
while (!Check_Valid_Format(*Current_Position))
{
char c = *Current_Position++;
if (c == ',')
{
fprintf(File, "%c", ' ');
}
else
{
fprintf(File, "%c", c);
}
}
fprintf(File, "%c", '\n');
}
}
}
void Write_File(uint8_t Translation_Type, unsigned char* File, uint64_t Size_of_File, unsigned char* Name_of_File)
{
FILE* Output = fopen(Name_of_File, "w");
if (!Output)
{
printf("Error: Cannot open file. \n");
return;
}
unsigned char* File_Stop = File + Size_of_File;
if (Translation_Type == 0)
{
Write_File_Unchanged(File, File_Stop, Output);
}
else if (Translation_Type == 1)
{
Write_File_First_Second(File, File_Stop, Output);
}
else if (Translation_Type == 2)
{
Write_File_Second_First(File, File_Stop, Output);
}
else if (Translation_Type == 3)
{
Write_File_Swap(File, File_Stop, Output);
}
fclose(Output);
}
void Start_Server(int Server_Socket)
{
if (listen(Server_Socket, 3) < 0)
{
char * Error_Text = strerror(errno);
perror(Error_Text);
exit(-1);
}
struct sockaddr_in Client_Address;
int Size_of_Address = sizeof(Client_Address);
int Client_Init = 0;
while (1)
{
if ((Client_Init = accept(Server_Socket, (struct sockaddr*) &Client_Address, &Size_of_Address)) < 0)
{
char * Error_Text = strerror(errno);
perror(Error_Text);
exit(-1);
}
Message_Struct Message = Get_Message(Client_Init);
unsigned char* File_Start = Message.File;
unsigned char* File_Stop = Message.File + Message.Size_of_File;
bool Is_Valid = Test_Format(File_Start, File_Stop);
if (Is_Valid)
{
Write_Socket(Client_Init, "Success!", sizeof("Success!"));
Write_File(Message.Transmission_Type, Message.File, Message.Size_of_File, Message.Name_of_File);
}
else
{
Write_Socket(Client_Init, "Error: Invalid Format", sizeof("Error: Invalid Format"));
}
if (close(Client_Init) < 0)
{
char * Error_Text = strerror(errno);
perror(Error_Text);
exit(-1);
}
}
}
// Main function
int main(int argc, char *argv[])
{
printf("==================================================\n");
printf("CS 450: Data Communication and Network Programming\n");
printf(" Instructor: Dr. Jiang Li \n");
printf(" TCP Programming Project \n");
printf(" By: Shrijanand Chintapatla \n");
printf(" @02778807 \n");
printf("==================================================\n");
printf(" SERVER \n");
printf("==================================================\n");
if (argc < 2)
{
fprintf(stderr, "Error: Kindly check that a port is provided. \n");
exit(1);
}
int Server_Socket = Create_Server(atoi(argv[1]));
printf("\n ... Created Server. Waiting for Client ... \n");
Start_Server(Server_Socket);
if (close(Server_Socket) < 0)
{
printf("Error: Server could not be closed. \n");
}
printf("==================================================\n");
}