-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.c
444 lines (425 loc) · 11.3 KB
/
service.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
#include "service.h"
bool fileInUse = false;
pthread_mutex_t lockFile = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condFile = PTHREAD_COND_INITIALIZER;
bool isSeatBooked[257] = {false};
pthread_mutex_t lockSeat = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condSeat = PTHREAD_COND_INITIALIZER;
void generate_salt(uint8_t *salt) {
int fd = open("/dev/urandom", O_RDONLY);
read(fd, salt, SALT_SIZE);
close(fd);
}
void hash_password(char *password, char *hashed_password) {
uint8_t salt[SALT_SIZE];
generate_salt(salt);
char hash[HASHED_PASSWORD_SIZE];
argon2id_hash_encoded(2, MEMORY_USAGE, 1, password, strlen(password), salt,
SALT_SIZE, HASH_SIZE, hash, HASHED_PASSWORD_SIZE);
strcpy(hashed_password, hash);
}
int validate_password(char *password_to_validate, char *hashed_password) {
if (argon2id_verify(hashed_password, password_to_validate,
strlen(password_to_validate)) == ARGON2_OK) {
return 1;
} else {
return 0;
}
}
void action0(int clientFD, uint32_t userID, int actionID, int seatNumber) {
Res res = {0};
// validate
if (userID != 0 || actionID != 0 || seatNumber != 0) {
res.actionID = 0;
res.code = 1;
} else {
res.actionID = 0;
res.code = 0;
}
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
// terminate the connection
}
void action1(int clientFD, UserQueue *queue, uint32_t userID, char *password) {
// create /tmp/passwords.tsv if doesn't exist, but if exists, open in append
// mode
Res res = {0};
pthread_mutex_lock(&lockFile);
while (fileInUse) {
pthread_cond_wait(&condFile, &lockFile);
}
fileInUse = true;
FILE *fp = fopen("/tmp/passwords.tsv", "a");
if (fp == NULL) {
perror("fopen");
exit(1);
}
FILE *readFile = fopen("/tmp/passwords.tsv", "r");
// read userID from file, and check if userID already exists
uint32_t userIDInFile;
bool userExistsInFile = false;
char hashedPasswordInFile[HASHED_PASSWORD_SIZE + 10];
while (fscanf(readFile, "%u\t%s\n", &userIDInFile, hashedPasswordInFile) !=
EOF) {
// DEBUG
// printf("userIDInFile: %u, hashedPasswordInFile: %s\n", userIDInFile,
// hashedPasswordInFile);
if (userIDInFile == userID) {
userExistsInFile = true;
break;
}
}
fclose(readFile);
if (userLoggedIn(queue, userID)) {
res.actionID = 1;
res.code = 1;
// DEBUG
// printf("User already logged in a client\n");
fclose(fp);
fileInUse = false;
pthread_cond_signal(&condFile);
pthread_mutex_unlock(&lockFile);
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
if (isClientActive(queue, clientFD)) {
res.actionID = 1;
res.code = 2;
// DEBUG
// printf("Client already active\n");
fclose(fp);
fileInUse = false;
pthread_cond_signal(&condFile);
pthread_mutex_unlock(&lockFile);
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
if (!userExistsInFile) {
// treat this as registration
char hashedPassword[HASHED_PASSWORD_SIZE + 10];
hash_password(password, hashedPassword);
// write userID and hashed password to file
// "userID\tpassword"
fprintf(fp, "%u\t%s\n", userID, hashedPassword);
User newUser = {
.userID = userID,
.clientID = clientFD,
.isLoggedIn = true,
.numBookedSeats = 0,
};
putUser(queue, newUser);
// DEBUG
// printf("registration success\n");
fclose(fp);
fileInUse = false;
pthread_cond_signal(&condFile);
pthread_mutex_unlock(&lockFile);
res.actionID = 1;
res.code = 0;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
if (!validate_password(password, hashedPasswordInFile)) {
res.actionID = 1;
res.code = 3;
// DEBUG
// printf("wrong password\n");
fclose(fp);
fileInUse = false;
pthread_cond_signal(&condFile);
pthread_mutex_unlock(&lockFile);
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
// treat this as login
int idx = getIndexOfUser(queue, userID);
// DEBUG
// printf("idx: %d, userID: %d\n", idx, userID);
if (idx < 0) {
// 서버를 껐다 켜면 queue가 비어있을 수 있음
User newUser = {
.userID = userID,
.clientID = clientFD,
.isLoggedIn = true,
.numBookedSeats = 0,
};
putUser(queue, newUser);
} else {
pthread_mutex_lock(&queue->lockQueue);
queue->UserArr[idx].isLoggedIn = true;
queue->UserArr[idx].clientID = clientFD;
pthread_mutex_unlock(&queue->lockQueue);
}
// DEBUG
// printf("right password, login success\n");
fclose(fp);
fileInUse = false;
pthread_cond_signal(&condFile);
pthread_mutex_unlock(&lockFile);
res.actionID = 1;
res.code = 0;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
}
void action2(int clientFD, UserQueue *queue, uint32_t userID, int seatNumber) {
Res res = {0};
// guard
if (!userLoggedIn(queue, userID)) {
// DEBUG
// printf("User not logged in, you cannot booka a seat\n");
res.actionID = 2;
res.code = 1;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
// validate seatNumber
if (seatNumber < 1 || seatNumber > 256) {
// DEBUG
// printf("Invalid seat number\n");
res.actionID = 2;
res.code = 3;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
pthread_mutex_lock(&lockSeat);
if (isSeatBooked[seatNumber]) {
// DEBUG
// printf("Seat already booked\n");
pthread_mutex_unlock(&lockSeat);
res.actionID = 2;
res.code = 2;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
isSeatBooked[seatNumber] = true;
int idx = getIndexOfUser(queue, userID);
pthread_mutex_lock(&queue->lockQueue);
queue->UserArr[idx].bookedSeats[queue->UserArr[idx].numBookedSeats++] =
seatNumber;
res.seats[seatNumber] = true;
pthread_mutex_unlock(&queue->lockQueue);
// DEBUG
// printf("%d Seat booked\n", seatNumber);
pthread_mutex_unlock(&lockSeat);
res.actionID = 2;
res.code = 0;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
}
void action3(int clientFD, UserQueue *queue, uint32_t userID, int seatNumber) {
Res res = {0};
// guard
if (!userLoggedIn(queue, userID)) {
// DEBUG
// printf("User not logged in, you cannot see booked seats\n");
res.actionID = 3;
res.code = 1;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
int idx = getIndexOfUser(queue, userID);
// DEBUG
// printf("Booked seats: ");
if (seatNumber == -1) {
res.actionID = 3;
res.code = 0;
pthread_mutex_lock(&queue->lockQueue);
for (int i = 0; i < queue->UserArr[idx].numBookedSeats; i++) {
res.seats[queue->UserArr[idx].bookedSeats[i]] = true;
}
pthread_mutex_unlock(&queue->lockQueue);
} else if (seatNumber == 0) {
// return the available seats
res.actionID = 3;
res.code = 0;
pthread_mutex_lock(&lockSeat);
for (int i = 1; i <= 256; i++) {
if (!isSeatBooked[i]) {
res.seats[i] = true;
}
}
pthread_mutex_unlock(&lockSeat);
}
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
}
void action4(int clientFD, UserQueue *queue, uint32_t userID, int seatNumber) {
// guard
Res res = {0};
if (!userLoggedIn(queue, userID)) {
// DEBUG
// printf("User not logged in, you cannot cancel a seat\n");
res.actionID = 4;
res.code = 1;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
// validate seatNumber
if (seatNumber < 1 || seatNumber > 256) {
// DEBUG
// printf("Invalid seat number\n");
res.actionID = 4;
res.code = 3;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
pthread_mutex_lock(&lockSeat);
if (!isSeatBooked[seatNumber]) {
// DEBUG
// printf("Seat not booked\n");
pthread_mutex_unlock(&lockSeat);
res.actionID = 4;
res.code = 2;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
// booked by another user
int idx = getIndexOfUser(queue, userID);
bool isBooked = false;
for (int i = 0; i < queue->UserArr[idx].numBookedSeats; i++) {
if (queue->UserArr[idx].bookedSeats[i] == seatNumber) {
isBooked = true;
break;
}
}
if (!isBooked) {
// DEBUG
// printf("Seat not booked by you\n");
pthread_mutex_unlock(&lockSeat);
res.actionID = 4;
res.code = 2;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
isSeatBooked[seatNumber] = false;
pthread_mutex_unlock(&lockSeat);
pthread_mutex_lock(&queue->lockQueue);
for (int i = 0; i < queue->UserArr[idx].numBookedSeats; i++) {
if (queue->UserArr[idx].bookedSeats[i] == seatNumber) {
for (int j = i; j < queue->UserArr[idx].numBookedSeats - 1; j++) {
queue->UserArr[idx].bookedSeats[j] =
queue->UserArr[idx].bookedSeats[j + 1];
}
queue->UserArr[idx].numBookedSeats--;
break;
}
}
// printf("%d Seat cancelled\n", seatNumber);
pthread_mutex_unlock(&queue->lockQueue);
res.actionID = 4;
res.code = 0;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
}
void action5(int clientFD, UserQueue *queue, uint32_t userID, int seatNumber) {
Res res = {0};
// guard
if (!userLoggedIn(queue, userID)) {
// DEBUG
// printf("User not logged in, you cannot logout\n");
res.actionID = 5;
res.code = 1;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
return;
}
int idx = getIndexOfUser(queue, userID);
pthread_mutex_lock(&queue->lockQueue);
queue->UserArr[idx].isLoggedIn = false;
// 오류날 수도 있음
queue->UserArr[idx].clientID = 0;
pthread_mutex_unlock(&queue->lockQueue);
res.actionID = 5;
res.code = 0;
tlv_t resTLV = {
.type = 0,
.length = sizeof(Res),
.value = (uint8_t *)&res,
};
send_tlv(clientFD, &resTLV);
// printf("Logout success\n");
}