-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_functions.h
More file actions
347 lines (338 loc) · 9.22 KB
/
client_functions.h
File metadata and controls
347 lines (338 loc) · 9.22 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
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define PORT 8080
#define START_MENU 0
#define SIGN_UP_OPTIONS 1
#define SIGN_IN_OPTIONS 2
#define USER_OPTIONS 3
#define ADMIN_OPTIONS 4
// possible options
#define SIGN_UP 5
#define SIGN_IN 6
#define SIGN_UP_AS_USER 7
#define SIGN_UP_AS_JOINT 8
#define SIGN_UP_AS_ADMIN 9
#define SIGN_IN_AS_USER 10
#define SIGN_IN_AS_JOINT 11
#define SIGN_IN_AS_ADMIN 12
#define DEPOSIT 13
#define WITHDRAW 14
#define BALANCE 15
#define PASSWORD 16
#define DETAILS 17
#define EXIT 18
#define ADD_USER 19
#define DEL_USER 20
#define MOD_USER 21
#define GET_USER_DETAILS 22
#define INVALID -1
#define BUF_SIZE 1000
//struct user
int global_acc_id = 100;
struct user {
char type[BUF_SIZE];
char password[BUF_SIZE];
char username[BUF_SIZE];
};
//struct details
struct account {
int balance;
int accid;
};
int printOptions(int);
void interface(int);
int get_auth_details(int, int);
void user_interface(int);
void admin_interface();
void interface(int sock)
{
int option, opt_ret;
char *option_string = (char*)malloc(10 * sizeof(char));
char *return_message = (char*)malloc(BUF_SIZE * sizeof(char));
option = printOptions(START_MENU);
switch (option)
{
case SIGN_UP:
opt_ret = get_auth_details(sock, SIGN_UP_OPTIONS);
while (1)
{
switch (opt_ret)
{
case SIGN_UP_AS_USER:
user_interface(sock);
break;
case SIGN_UP_AS_JOINT:
user_interface(sock);
break;
case SIGN_UP_AS_ADMIN:
admin_interface(sock);
break;
}
}
break;
case SIGN_IN:
opt_ret = get_auth_details(sock, SIGN_IN_OPTIONS);
switch (opt_ret)
{
case SIGN_IN_AS_USER:
while (1)
{
user_interface(sock);
}
break;
case SIGN_IN_AS_ADMIN:
while (1)
{
admin_interface(sock);
}
break;
case SIGN_IN_AS_JOINT:
while (1)
{
user_interface(sock);
}
}
break;
default:
printf("Invalid Option\n");
exit(1);
}
}
int get_auth_details(int sock, int option)
{
char *username = (char*)malloc(BUF_SIZE * sizeof(char));
char *password = (char*)malloc(BUF_SIZE * sizeof(char));
char *return_message = (char*)malloc(BUF_SIZE * sizeof(char));
char *option_string = (char*)malloc(10 * sizeof(char));
printf("option = %d\n", option);
int ret_option = printOptions(option);
printf("Enter username : ");
scanf("%s", username);
printf("Enter password : ");
scanf("%s", password);
sprintf(option_string, "%d", ret_option);
send(sock, option_string, sizeof(option_string), 0);
send(sock, username, sizeof(username), 0);
send(sock, password, sizeof(password), 0);
read(sock, return_message, BUF_SIZE * sizeof(char));
printf("%s\n", return_message);
if (!strcmp(return_message, "sign in failed\n"))
exit(1);
return ret_option;
}
void user_interface(int sock)
{
int option = printOptions(USER_OPTIONS), deposit_amt, withdraw_amt;
char *option_string = (char*)malloc(10 * sizeof(char));
char *return_message = (char*)malloc(BUF_SIZE * sizeof(char));
char *amt_string = (char*)malloc(BUF_SIZE * sizeof(char));
char *password = (char*)malloc(BUF_SIZE * sizeof(char));
sprintf(option_string, "%d", option);
send(sock, option_string, sizeof(option_string), 0);
switch (option)
{
case DEPOSIT:
printf("Enter amount to be deposited : ");
scanf("%d", &deposit_amt);
sprintf(amt_string, "%d", deposit_amt);
printf("%s\n", amt_string);
send(sock, amt_string, sizeof(amt_string), 0);
break;
case WITHDRAW:
printf("Enter amount to be withdrawn : ");
scanf("%d", &withdraw_amt);
sprintf(amt_string, "%d", withdraw_amt);
send(sock, amt_string, sizeof(amt_string), 0);
break;
case BALANCE:
break;
case PASSWORD:
printf("Enter new password\n");
scanf("%s", password);
send(sock, password, sizeof(password), 0);
break;
case DETAILS:
break;
case EXIT:
exit(0);
}
read(sock, return_message, BUF_SIZE * sizeof(char));
printf("%s\n", return_message);
}
void admin_interface(int sock)
{
int option = printOptions(ADMIN_OPTIONS), type;
char *option_string = (char*)malloc(10 * sizeof(char));
char *username = (char*)malloc(BUF_SIZE * sizeof(char));
char *new_username = (char*)malloc(BUF_SIZE * sizeof(char));
char *password = (char*)malloc(BUF_SIZE * sizeof(char));
char *return_message = (char*)malloc(BUF_SIZE * sizeof(char));
sprintf(option_string, "%d", option);
send(sock, option_string, sizeof(option_string), 0);
switch (option)
{
case ADD_USER:
printf("Enter User Type\n");
printf("1 : Normal\n");
printf("2 : Joint\n");
scanf("%d", &type);
printf("Enter username : ");
scanf("%s", username);
printf("Enter password : ");
scanf("%s", password);
switch (type)
{
case 1:
send(sock, "1", sizeof("1"), 0);
break;
case 2:
send(sock, "2", sizeof("2"), 0);
break;
default:
printf("Invalid Type\n");
exit(1);
}
printf("username = %s\n", username);
printf("password = %s\n", password);
send(sock, username, sizeof(username), 0);
send(sock, password, sizeof(password), 0);
break;
case DEL_USER:
printf("Enter username : ");
scanf("%s", username);
send(sock, username, sizeof(username), 0);
break;
case MOD_USER:
printf("Enter old username : ");
scanf("%s", username);
send(sock, username, sizeof(username), 0);
printf("Enter new username : ");
scanf("%s", new_username);
send(sock, new_username, sizeof(new_username), 0);
printf("Enter new password : ");
scanf("%s", password);
send(sock, password, sizeof(password), 0);
break;
case GET_USER_DETAILS:
printf("Enter username : ");
scanf("%s", username);
send(sock, username, sizeof(username), 0);
break;
case EXIT:
exit(0);
default:
printf("Invalid input\n");
exit(0);
}
read(sock, return_message, BUF_SIZE * sizeof(char));
printf("%s\n", return_message);
}
int printOptions(int menu)
{
int option;
switch (menu)
{
case START_MENU:
printf("Hello! enter one of the following options...\n");
printf("1 : Sign Up\n");
printf("2 : Sign In\n");
scanf("%d", &option);
switch (option)
{
case 1:
return SIGN_UP;
case 2:
return SIGN_IN;
default:
return INVALID;
}
case SIGN_UP_OPTIONS:
printf("How would you like to sign up?\n");
printf("1 : User\n");
printf("2 : Joint Account User\n");
printf("3 : Administrator\n");
scanf("%d", &option);
switch (option)
{
case 1:
return SIGN_UP_AS_USER;
case 2:
return SIGN_UP_AS_JOINT;
case 3:
return SIGN_UP_AS_ADMIN;
default:
return INVALID;
}
case SIGN_IN_OPTIONS:
printf("How would you like to sign in?\n");
printf("1 : User\n");
printf("2 : Joint Account User\n");
printf("3 : Administrator\n");
scanf("%d", &option);
switch (option)
{
case 1:
return SIGN_IN_AS_USER;
case 2:
return SIGN_IN_AS_JOINT;
case 3:
return SIGN_IN_AS_ADMIN;
default:
return -1;
}
case USER_OPTIONS:
printf("What would you like us to do for you?\n");
printf("1 : Deposit\n");
printf("2 : Withdraw\n");
printf("3 : Check Balance\n");
printf("4 : Change Password\n");
printf("5 : View Details\n");
printf("6 : Exit\n");
scanf("%d", &option);
switch (option)
{
case 1:
return DEPOSIT;
case 2:
return WITHDRAW;
case 3:
return BALANCE;
case 4:
return PASSWORD;
case 5:
return DETAILS;
case 6:
return EXIT;
default:
return -1;
}
case ADMIN_OPTIONS:
printf("1 : Add User\n");
printf("2 : Delete User\n");
printf("3 : Modify User\n");
printf("4 : Search for Account Details\n");
printf("5 : Exit\n");
scanf("%d", &option);
switch (option)
{
case 1:
return ADD_USER;
case 2:
return DEL_USER;
case 3:
return MOD_USER;
case 4:
return GET_USER_DETAILS;
case 5:
return EXIT;
default:
return -1;
}
default:
break;
}
}