-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
749 lines (669 loc) · 23.3 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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include <unistd.h>
#include<netdb.h>
#include <arpa/inet.h>
#include<string.h>
#include <ctype.h>
#include <signal.h>
#include <fcntl.h>
#include <termios.h>
#define MAXPW 32
#define MAXLINE 50
int fd;
struct sockaddr_in srv;
char buf[1024],buf_r[1024];
int nbytes,newfd;
void print_customer_menu()
{
system("clear");
printf("┌───────────────────────────────────────────────────┐\n");
printf("│░░░░░░░░░░░░░░░░░ CUSTOMER PORTAL ░░░░░░░░░░░░░░░░░│\n");
printf("├───────────────────────────────────────────────────┤\n");
printf("│1. Balance Enquiry │\n");
printf("│2. Transaction History │\n");
printf("│3. Mini Statment │\n");
printf("│4. Logout │\n");
printf("└───────────────────────────────────────────────────┘\n");
}
void print_police_menu()
{
system("clear");
printf("┌───────────────────────────────────────────────────┐\n");
printf("│░░░░░░░░░░░░░░░░░ POLICE PORTAL ░░░░░░░░░░░░░░░░░░░│\n");
printf("├───────────────────────────────────────────────────┤\n");
printf("│1. Balance Enquiry for all users │\n");
printf("│2. Balance Enquiry for specific user │\n");
printf("│3. Transaction History for specific user │\n");
printf("│4. Mini statement for specific user │\n");
printf("│5. Logout │\n");
printf("└───────────────────────────────────────────────────┘\n");
}
void print_admin_menu()
{
system("clear");
printf("┌───────────────────────────────────────────────────┐\n");
printf("│░░░░░░░░░░░░░░░░░ ADMIN PORTAL ░░░░░░░░░░░░░░░░░░░░│\n");
printf("├───────────────────────────────────────────────────┤\n");
printf("│1. Credit │\n");
printf("│2. Debit │\n");
printf("│3. Transfer │\n");
printf("│4. Add New User │\n");
printf("│5. Logout │\n");
printf("└───────────────────────────────────────────────────┘\n");
}
void ministatment(char user_id[])
{
int data_availability=-1;
if(read(fd,&data_availability,sizeof(data_availability))<0)
{
perror("Error while receiving data availability\n");
}
if(data_availability ==-1)
{
printf("Account number %s does not exist.\n",user_id);
return;
}
else if(data_availability == -2)
{
printf("This option will be available after making transactions.\n");
return;
}
else
{
printf("┌────────────────────────────────────────────────────────────────────┐\n");
printf("│ %14s %8s %10s %4s %10s %3s %10s │\n","Date"," ","Type"," ","Amount"," ","Balance");
printf("├────────────────────────────────────────────────────────────────────┤\n");
}
while(1)
{
// int data_availability=1;
char *buff=(char *)malloc(1024*sizeof(char));
bzero(buff,1024);
int count=read(fd,buff,1024);
if(count<0)
{
perror("Error while receiving mini statement\n");
exit(1);
}
int ack=1;
printf("| %s |\n",buff);
if(write(fd,&ack,sizeof(int))<0)
{
perror("2.error in ack sending ");
}
free(buff);
if(read(fd,&data_availability,sizeof(data_availability))<0)
{
perror("Error while receiving data availability\n");
}
if(data_availability==-1)
break;
}
printf("└────────────────────────────────────────────────────────────────────┘\n");
}
void print_balance(char user_id[],int balance)
{
printf("┌───────────────────────────────────┐\n");
printf("│ USER BALANCE │\n");
printf("├───────────────────────────────────┤\n");
printf("│ %s %15s │\n","Account Number","Balance");
printf("│ %11s %15d │\n",user_id,balance);
printf("└───────────────────────────────────┘\n");
}
void all_balance()
{
printf("┌───────────────────────────────────┐\n");
printf("│ BALANCESHEET │\n");
printf("├───────────────────────────────────┤\n");
printf("│ %s %15s │\n","Account Number","Balance");
while(1)
{
int data_availability=1;
if(read(fd,&data_availability,sizeof(data_availability))<0)
{
perror("Error in recv data availabilty");
}
else{
}
if(data_availability==-1)
break;
char *buff=(char *)malloc(1024*sizeof(char));
bzero(buff,1024);
int count=read(fd,buff,1024);
if(count<0)
{
perror("error in recv ministm");
exit(1);
}
int ack=1;
printf("│%s │\n",buff);
if(write(fd,&ack,sizeof(int))<0)
{
perror("2.error in ack sending ");
}
free(buff);
}
printf("└───────────────────────────────────┘\n");
}
void sigintHandler(int sig_num)
{
printf("\n Exiting the program\n");
exit(1);
}
ssize_t getpasswd (char **pw, size_t sz, int mask, FILE *fp)
{
if (*pw == NULL) { /* reallocate if no address */
void *tmp = realloc (*pw, sz * sizeof **pw);
if (!tmp)
return -1;
memset (tmp, 0, sz); /* initialize memory to 0 */
*pw = (char*) tmp;
}
size_t idx = 0; /* index, number of chars in read */
int c = 0;
struct termios old_kbd_mode; /* orig keyboard settings */
struct termios new_kbd_mode;
if (tcgetattr (0, &old_kbd_mode)) { /* save orig settings */
fprintf (stderr, "%s() error: tcgetattr failed.\n", __func__);
return -1;
} /* copy old to new */
memcpy (&new_kbd_mode, &old_kbd_mode, sizeof(struct termios));
new_kbd_mode.c_lflag &= ~(ICANON | ECHO); /* new kbd flags */
new_kbd_mode.c_cc[VTIME] = 0;
new_kbd_mode.c_cc[VMIN] = 1;
if (tcsetattr (0, TCSANOW, &new_kbd_mode)) {
fprintf (stderr, "%s() error: tcsetattr failed.\n", __func__);
return -1;
}
/* read chars from fp, mask if valid char specified */
while (((c = fgetc (fp)) != '\n' && c != EOF && idx < sz - 1) ||
(idx == sz - 1 && c == 127))
{
if (c != 127) {
if (31 < mask && mask < 127) /* valid ascii char */
fputc (mask, stdout);
(*pw)[idx++] = c;
}
else if (idx > 0) { /* handle backspace (del) */
if (31 < mask && mask < 127) {
fputc (0x8, stdout);
fputc (' ', stdout);
fputc (0x8, stdout);
}
(*pw)[--idx] = 0;
}
}
(*pw)[idx] = 0; /* null-terminate */
/* reset original keyboard */
if (tcsetattr (0, TCSANOW, &old_kbd_mode)) {
fprintf (stderr, "%s() error: tcsetattr failed.\n", __func__);
return -1;
}
if (idx == sz - 1 && c != '\n') /* warn if pw truncated */
fprintf (stderr, " (%s() warning: truncated at %zu chars.)\n",
__func__, sz - 1);
return idx; /* number of chars in passwd */
}
void customer_function(char user_id[])
{
int option;
char choice= 'Y';
do
{
print_customer_menu();
printf("Enter your choice :");
scanf("%d",&option);
getc(stdin);
int count;
count=write(fd,&option,sizeof(int));
if(count<0){
printf("Error in sending option \n");
exit(1);
}
system("clear");
if(option==1)
{
// Balance Enquiry
int balance;
count=read(fd,&balance,sizeof(int));
if(count<0)
{
printf("Error in reciving balance \n");
exit(1);
}
else
{
int i=0;
char user_id_modified[500];
strcpy(user_id_modified,user_id);
while(user_id_modified[i]!='\n')
{
i++;
}
user_id_modified[i]='\0';
print_balance(user_id_modified,balance);
}
}
else if(option==2 || option == 3)
{
ministatment(user_id);
}
else if(option == 4)
{
exit(1);
}
else
printf("Invalid option, please try again...\n");
printf("Do you want to continue?(Y/N): ");
scanf("%c", &choice);
}
while(choice == 'Y' || choice == 'y');
}
void admin_function()
{
int option;
int count;
char choice='N';
do
{
option = 6;
print_admin_menu();
char *user_id=(char*)malloc(500*sizeof(char));
char *user_id_2=(char*)malloc(500*sizeof(char));
bzero(user_id,500);
printf("Enter Option : ");
scanf("%d",&option);
getc(stdin);
if(option > 5 || option < 1)
{
printf("Incorrect option, please try again...\n");
printf("Do you want to continue?(Y/N): ");
scanf("%c", &choice);
continue;
}
int i=0;
count=write(fd,&option,sizeof(option));
if(count<0)
{
perror("Sending option error in admin function\n");
exit(1);
}
if(option == 1 || option == 2)
{
printf("Enter user_id : ");
fgets(user_id,500,stdin);
while(user_id[i]!='\n')
{
i++;
}
user_id[i]='\0';
count=write(fd,user_id,500);
if(count<0)
{
perror("Sending user_id error in admin function\n");
exit(1);
}
int success;
count=read(fd,&success,sizeof(success));
if(success == 0)
{
printf("Account number invalid please check...\n");
printf("Do you want to continue?(Y/N): ");
scanf("%c", &choice);
continue;
}
int amount = -1;
printf("Enter amount : ");
scanf("%d",&amount);
while (amount < 0)
{
print_admin_menu();
printf("Enter Option : %d\n",option);
printf("Enter user_id : %s\n",user_id);
printf("Amount negative please Re-enter amount: ");
scanf("%d",&amount);
}
getc(stdin);
count=write(fd,&amount,sizeof(amount));
if(count<0)
{
perror("Error while sending amount to the admin function\n");
exit(1);
}
count=read(fd,&success,sizeof(success));
if(success == 1)
printf("Transaction successful !!\n");
else if(success==0)printf("Update unsuccessful invalid user id !!\n");
else printf("Account number %s has insufficient balance transaction cannot be made.. \n",user_id);
}
else if(option == 3)
{
printf("Enter user id to send money from : ");
fgets(user_id,500,stdin);
printf("Enter user id to send money to : ");
fgets(user_id_2,500,stdin);
while(user_id[i]!='\n')
{
i++;
}
user_id[i]='\0';
i = 0;
while(user_id_2[i]!='\n')
{
i++;
}
user_id_2[i]='\0';
count=write(fd,user_id,500);
if(count<0)
{
perror("Sending user_id error in admin function\n");
exit(1);
}
count=write(fd,user_id_2,500);
if(count<0)
{
perror("Sending user_id_2 error in admin function\n");
exit(1);
}
int success;
count=read(fd,&success,sizeof(success));
if(success == 0)
{
printf("\nAccount number invalid please check...\n");
continue;
}
int amount = -1;
printf("Enter amount: ");
scanf("%d",&amount);
while (amount < 0)
{
print_admin_menu();
printf("Enter Option : %d\n",option);
printf("Enter user_id : %s\n",user_id);
printf("Amount negative please Re-enter amount: ");
scanf("%d",&amount);
}
getc(stdin);
count=write(fd,&amount,sizeof(amount));
if(count<0)
{
perror("Error while sending amount to the admin function\n");
exit(1);
}
count=read(fd,&success,sizeof(success));
if(success == 1)
printf("Transaction successful !!\n");
else if(success==0)printf("Update unsuccessful invalid user id !!\n");
else printf("Account number %s has insufficient balance transaction cannot be made.. \n",user_id);
}
else if(option == 4)
{
printf("Enter account number of new user : ");
fgets(user_id,500,stdin);
while(user_id[i]!='\n')
{
i++;
}
user_id[i]='\0';
count=write(fd,user_id,500);
if(count<0)
{
perror("Sending account number error in admin function\n");
exit(1);
}
int success;
count=read(fd,&success,sizeof(success));
if(count<0)
{
perror("Sending account number error in admin function\n");
exit(1);
}
if(success == 0)
{
printf("Account already exists please check...\n");
printf("Do you want to continue?(Y/N): ");
scanf("%c", &choice);
continue;
}
else
{
printf("Enter password of new user : ");
char password[500];
fgets(password,500,stdin);
i = 0;
while(password[i]!='\n')
{
i++;
}
password[i]='\0';
int type;
//Change %d to %c after type format change in login file
printf("Enter user type : ");
scanf("%d",&type);
getc(stdin);
count=write(fd,password,500);
count=write(fd,&type,sizeof(type));
count=read(fd,&success,sizeof(success));
if(success == 1)
{
printf("Account Created.\n");
}
else
{
printf("Account creation unsuccessfull.\n");
}
}
}
else
{
exit(1);
}
free(user_id);
printf("Do you want to continue?(Y/N): ");
scanf("%c", &choice);
}
while(choice == 'Y' || choice == 'y');
}
void police_function()
{
int option;
int count;
char choice;
do
{
print_police_menu();
char *user_id=(char*)malloc(500*sizeof(char));
bzero(user_id,500);
printf("Enter Option : ");
scanf("%d",&option);
getc(stdin);
if(option > 5 || option < 1)
{
printf("Incorrect option, please try again...\n");
printf("Do you want to continue?(Y/N): ");
scanf("%c", &choice);
continue;
}
count=write(fd,&option,sizeof(int));
if(count<0)
{
perror("Error while sending selected option.\n");
exit(1);
}
if(option == 2 || option == 3 || option == 4)
{
printf("Enter user_id : ");
fgets(user_id,500,stdin);
int i=0;
while(user_id[i]!='\n')
{
i++;
}
user_id[i]='\0';
count=write(fd,user_id,500);
if(count<0)
{
perror("Sending user_id error in police function\n");
exit(1);
}
}
int balance;
system("clear");
switch(option)
{
case 1:
all_balance();
break;
case 2:
count=recv(fd,&balance,4,0);
if(count<0)
{
perror("\nBalance reciving error in police function ");
}
else
{
if(balance == -1)
{
printf("User doesn't exist.\n");
}
else
{
print_balance(user_id,balance);
}
}
break;
case 3:
ministatment(user_id);
break;
case 4:
ministatment(user_id);
break;
case 5:
exit(1);
break;
default:
printf("Incorrect option, please try again\n");
continue;
}
free(user_id);
printf("Do you want to continue?(Y/N): ");
scanf("%c", &choice);
}
while(choice == 'Y' || choice == 'y');
}
int main(int argc, char *argv[])
{
//For clearing the screen
signal(SIGINT, sigintHandler);
system("clear");
if((fd=socket(AF_INET,SOCK_STREAM, 0))<0)
{
perror("Error in socket creation :\n");
exit(1);
}else
printf("[+] Socket Created :\n");
bzero((char *)&srv,sizeof(srv));
srv.sin_family=AF_INET;
srv.sin_port=htons(atoi(argv[2]));
srv.sin_addr.s_addr= inet_addr(argv[1]);
if(connect(fd,(struct sockaddr*) &srv,sizeof(srv))<0)
{
perror("Failed to connect to server :\n");
exit(1);
}
else
printf("[+] Connected to server.\n");
// login check
char *user_id=(char*)malloc(500*sizeof(char));
char *password=(char*)malloc( 500*sizeof(char));
printf("\n𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣 LOGIN PAGE 𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣𝌣 \n\a");
// printf("\nૐ ૐ ૐ ૐ ૐ ૐ ૐ 𝐋𝐎𝐆𝐈𝐍 𝐏𝐀𝐆𝐄 ૐ ૐ ૐ ૐ ૐ ૐ \n\a");
//printf("\n░░░░░░░░░░░░░░░░░░ LOGIN PAGE ░░░░░░░░░░░░░░░░░░\n\a");
printf("Enter User ID :");
fgets(user_id,500,stdin);
//strcpy(user_id,"214101019\n");
//scanf("%s",user_id);
int count;
if((count=write(fd,user_id,strlen(user_id)))<0)
{
perror("error in sending user name ");
exit(1);
}
else{
int size=sizeof(int);
// printf("%d bytre send user_name\n",count);
int ack=1;
if((count=read(fd,&ack,size))<0)
{
perror("1. error in recv ack :");
}
}
// printf("Type password :");
// fgets(password,MAXLINE,stdin);
char pw[MAXPW] = {0};
char *p = pw;
FILE *fp = stdin;
ssize_t nchr = 0;
printf ( "Enter Password :");
nchr = getpasswd (&p, MAXPW, '*', fp);
strcpy(password,p);
int i=0;
while(password[i]!='\0')
{
i++;
}
password[i]='\n';
//strcpy(password,"00000\n");
if((count=write(fd,password,strlen(password)))<0)
{
perror("Error in sending password ");
exit(1);
}
else
{
int ack=1;
if(read(fd,&ack,sizeof(ack))<0)
{
perror("error in recv ack :");
}
}
int user_type;
if((count=read(fd,&user_type,sizeof(user_type)))<0)
{
perror("Error in recv user type");
exit(1);
}
else{
int ack=1;
if(write(fd,&ack,sizeof(ack))<0)
{
perror("error in sending ack :");
}
//For clearing the screen
system("clear");
if(user_type==0)
{
printf("\n░░░░░░░░░░░░░░░░░░ CLIENT PORTAL ░░░░░░░░░░░░░░░░░░\n\a");
customer_function(user_id);
}
else if(user_type==1)
{
printf("\n░░░░░░░░░░░░░░░░░░ POLICE PORTAL ░░░░░░░░░░░░░░░░░░\n\a");
police_function();
}
else if(user_type==2)
{
printf("\n░░░░░░░░░░░░░░░░░░ ADMIN PORTAL ░░░░░░░░░░░░░░░░░░\n\a");
admin_function();
}
else{
printf("Login Credentials invalid... \n");
exit(0);
}
}
close(fd);
}