-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
498 lines (458 loc) · 9.75 KB
/
utilities.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
#include "utilities.h"
void concat(int argn, char** args,char* c,char* concated){
int i;
for(i=1; i< argn; i++){
strcat(concated,args[i]);
if(i!=argn-1){
strcat(concated,c);
}
}
}
void create_service_files(int argn, char** args){
create_directories("./DB");
create_directories("./DB/Services");
int i;
for(i=1; i<argn; i++){
printf("service %s created\n",args[i] );
char name[MAX_STR_SIZE];
clear_buff(name, MAX_STR_SIZE);
strcat(name, "./DB/Services/");
strcat(name,args[i]);
int fd= open_or_create_file(name);
close(fd);
}
}
int open_or_create_file(char* name){
return open(name, O_RDWR | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH |S_IWUSR);
}
void replace_char(char* string,char old_char,char new_char ){
int i=0;
while(string[i]!='\0'){
if(string[i]==old_char)
string[i]=new_char;
i++;
}
}
bool has_access(char* user,char* service,char* mode){
char line [MAX_STR_SIZE];
clear_buff(line,MAX_STR_SIZE);
FILE* file = fopen("./DB/AC.txt","r");
while( fgets(line,MAX_STR_SIZE,file) != NULL){
replace_char(line,'\n','\0');
int input_tokens_num;
char input_tokens[MAX_ARRAY_SIZE][MAX_STR_SIZE];
tokenizer(line, " ", &input_tokens_num, input_tokens);
if(input_tokens_num!=3){
printf("Error in number of token in 'AC.txt' in: %s\n",line );
return false;
}
if(strcmp( input_tokens[0],user) ==0 && strcmp( input_tokens[1],service) ==0 && strcmp( input_tokens[2],mode) ==0){
printf("match hapend in: %s\n",line );
return true;
}
}
fclose(file);
return false;
}
/*
void show_table_dst_port(dst_port table [MAX_ARRAY_SIZE],int index){
int i;
for(i=0; i<index; i++)
printf("%d - ip is: %s \tfd is:%d\n",i,table[i].ip,table[i].fd );
}
void clear_dst_port(dst_port table [MAX_ARRAY_SIZE]){
int i;
for(i=0; i<MAX_ARRAY_SIZE; i++)
{
strcpy( table[i].ip,"");
table[i].fd=-1;
// table[i]=NULL;
}
}
int search_dst_port(dst_port table [MAX_ARRAY_SIZE], char * ip){
int i;
for(i=0; i<MAX_ARRAY_SIZE; i++)
if(table[i].fd !=-1 && mystrcmp(table[i].ip,ip)==0)
return i;
return -1;
}
void delete_dst_port(dst_port table [MAX_ARRAY_SIZE],int index){
table[index].fd=-1;
clear_buff(table[index].ip,MAX_STR_SIZE);
}
void insert_dst_port(dst_port table [MAX_ARRAY_SIZE] ,char* ip,int fd){t
int i;
for(i=0; i<MAX_ARRAY_SIZE; i++)
{
if(table[i].fd == -1){
table[i].fd=fd;
strcpy(table[i].ip,ip);
break;
}
}
}
*/
bool file_exist(char * fname){
if( access( fname, F_OK ) != -1 )
return true;
else
return false;
}
void read_entire_file(char* name,char data [MAX_ARRAY_SIZE]){
printf("name is: %s\n",name );
FILE *f = fopen(name, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
fread(data, fsize, 1, f);
fclose(f);
data[fsize] = 0;
}
void show_table_ip_fd(ip_fd table [MAX_ARRAY_SIZE],int index){
int i;
for(i=0; i<index; i++)
printf("%d - ip is: %s \tfd is:%d\n",i,table[i].ip,table[i].fd );
}
void clear_ip_fd(ip_fd table [MAX_ARRAY_SIZE]){
int i;
for(i=0; i<MAX_ARRAY_SIZE; i++)
{
strcpy( table[i].ip,"");
table[i].fd=-1;
// table[i]=NULL;
}
}
char* crc(char* t){
return "$$$$$$";
}
int search_ip_fd( ip_fd table [MAX_ARRAY_SIZE], char * ip){
int i;
for(i=0; i<MAX_ARRAY_SIZE; i++)
if(table[i].fd !=-1 && mystrcmp(table[i].ip,ip)==0)
return i;
return -1;
}
void initial_ip_fd(ip_fd table [MAX_ARRAY_SIZE]){
int i;
for (i=0; i<MAX_ARRAY_SIZE;i++){
strcpy( table[i].ip,"");
table[i].fd=-1;
}
}
void delete_all_ip_fd( ip_fd table [MAX_ARRAY_SIZE], int fd){
int i;
for(i=0; i<MAX_ARRAY_SIZE; i++)
if(table[i].fd ==fd)
table[i].fd=-1;
}
void delete_ip_fd( ip_fd table [MAX_ARRAY_SIZE],int index){
table[index].fd=-1;
clear_buff(table[index].ip,MAX_STR_SIZE);
}
void insert_ip_fd( ip_fd table [MAX_ARRAY_SIZE] ,char* ip,int fd){
int i;
for(i=0; i<MAX_ARRAY_SIZE; i++)
{
if(table[i].fd == -1){
table[i].fd=fd;
strcpy(table[i].ip,ip);
break;
}
}
}
void extend_to(char* c, int to,char* ress)
{
int size = strlen(c);
int ex = to - size;
int i;
for (i=0; i<ex; i++)
{
strcat(ress, "0");
}
strcat(ress, c);
}
void framing(char* type,char* dstAdd,char* srcAdd,char* data,char* sender_port,char* frame){
strcat(frame, type);
strcat(frame, "&");
//dst
char ress[MAX_STR_SIZE];
clear_buff(ress, MAX_STR_SIZE);
extend_to(dstAdd, 16,ress);
strcat(frame, ress);
strcat(frame,"&");
//src
clear_buff(ress, MAX_STR_SIZE);
extend_to(srcAdd, 16,ress);
strcat(frame, ress);
strcat(frame, "&$$$$&1024&");
//data
strcat(frame, data);
strcat(frame, "&$$$$$$&");
//port
strcat(frame, sender_port);
}
int change_ip_seed(int c)
{
int file_fd = open("./DB/ip_seed.txt",O_TRUNC | O_RDWR);
if(file_fd > 0)
chmod("./DB/ip_seed.txt", S_IRUSR | S_IWUSR );
else
write(STDOUTFD, "Error In Opening File to Write!\n", sizeof("Error In Opening File to Write!\n"));
char res[20];
clear_buff(res, 20);
int_to_str(c, res, 10);
int w_st = write(file_fd, res, strlen(res));
close(file_fd);
return w_st;
}
int read_ip_seed()
{
char line [MAX_STR_SIZE];
clear_buff(line,MAX_STR_SIZE);
FILE* file = fopen("./DB/ip_seed.txt","rt");
fgets(line,MAX_STR_SIZE,file);
int v = atoi(line);
fclose( file);
return v;
}
int create_directories(char path_name[MAX_STR_SIZE])
{
return mkdir(path_name, S_IRUSR | S_IWUSR | S_IXUSR | S_IROTH | S_IWOTH | S_IXOTH);
}
int strlength(char str[MAX_STR_SIZE])
{
int i;
for(i = 0; i < GETMAX(MAX_STR_SIZE, sizeof(str)); i++)
{
if(str[i] == '\0') break;
}
return i;
}
void int_to_str(int val, char res[20], int base)
{
clear_buff(res, 20);
char tmp[20];
clear_buff(tmp, 20);
if(val == 0)
{
strcpy(res, "0");
return;
}
int index = 0;
while (val > 0)
{
char digit = '0';
digit = (char) ((val % base) + (int) digit);
tmp[index] = digit;
val /= base;
index ++;
}
strcat(tmp, "\0");
int i;
int size = strlength(tmp);
for(i = 0; i<size; i++)
{
res[i] = tmp[size-1-i];
}
strcat(res, "\0");
}
void print(char str[MAX_STR_SIZE])
{
write(STDOUTFD, str, strlength(str));
}
void tokenizer(char str[MAX_STR_SIZE], char delim[MAX_STR_SIZE], int* num_of_tokens, char res[MAX_STR_SIZE][MAX_STR_SIZE])
{
char tmp[MAX_STR_SIZE];
int i, tmp_index = 0, res_index = 0;
for(i = 0; i <= strlength(str); i++)
{
int cut = 0;
int j;
for(j = 0; j < strlength(delim); j++)
{
if (str[i] == delim[j] || str[i] == '\0' )
{
cut = 1;
break;
}
}
if(cut == 0)
{
tmp[tmp_index] = str[i];
tmp_index ++;
}
else
{
int k;
for(k = 0; k < tmp_index; k++)
res[res_index][k] = tmp[k];
res[res_index][k] = '\0';
res_index ++;
tmp_index = 0;
}
}
*num_of_tokens = res_index;
}
int clear_buff(char* c, int size)
{
if (size == 0) size = MAX_STR_SIZE;
int i;
for(i = 0; i < size; i++)
{
c[i] = 0;
}
}
int clear_buff_int(int* c, int size)
{
if (size == 0) size = MAX_STR_SIZE;
int i;
for(i = 0; i < size; i++)
{
c[i] = 0;
}
}
int mystrcmp(char const*p, char const *q)
{
int i = 0;
for(i=0; q[i]!='\0'; i++)
{
if(p[i] != q[i])
return -1;
}
return 0;
}
int mystrcmp_to(char const*p, char const *q, int to)
{
int i = 0;
for(i=0; i<to; i++)
{
if(p[i] != q[i])
return -1;
}
return 0;
}
int days_in_month(int month, int year)
{
if(month == 2)
return 28 + (year % 4 == 0);
if(month == 4 || month == 6 || month == 9 || month == 11)
return 30;
return 31;
}
void get_date(char time_str [30])
{
clear_buff(time_str, 30);
time_t my_time = time(NULL);
my_time += 3 * 3600 + 30 * 60;
int second = my_time % 60;
int minute = (my_time % 3600) / 60;
int hour = (my_time % (3600 * 24)) / 3600;
int days = my_time / (3600 * 24);
int month = 1, year = 1970;
while (days > days_in_month(month, year))
{
days -= days_in_month(month, year);
month ++;
if(month == 13)
{
year ++;
month = 1;
}
}
char tmp[20];
char str[20];
clear_buff(tmp, 20);
clear_buff(str, 20);
int_to_str(month, tmp, 10);
if(month < 10)
{
strcpy(str, "0");
strcat(str, tmp);
clear_buff(tmp, 20);
strcpy(tmp, str);
}
strncpy(time_str, tmp, strlength(tmp));
strcat(time_str, "/");//month
clear_buff(tmp, 20);
clear_buff(str, 20);
int_to_str(days, tmp, 10);
if(days < 10)
{
strcpy(str, "0");
strcat(str, tmp);
clear_buff(tmp, 20);
strcpy(tmp, str);
}
strncat(time_str, tmp, strlength(tmp));
strcat(time_str, "/");//day
clear_buff(tmp, 20);
int_to_str(year, tmp, 10);
strncat(time_str, tmp, strlength(tmp));
strcat(time_str, ",");//year
clear_buff(tmp, 20);
clear_buff(str, 20);
int_to_str(hour, tmp, 10);
if(hour < 10)
{
strcpy(str, "0");
strcat(str, tmp);
clear_buff(tmp, 20);
strcpy(tmp, str);
}
strncat(time_str, tmp, strlength(tmp));
strcat(time_str, ":");//hour
clear_buff(tmp, 20);
clear_buff(str, 20);
int_to_str(minute, tmp, 10);
if(minute < 10)
{
strcpy(str, "0");
strcat(str, tmp);
clear_buff(tmp, 20);
strcpy(tmp, str);
}
strncat(time_str, tmp, strlength(tmp));
strcat(time_str, ":");//minute
clear_buff(tmp, 20);
clear_buff(str, 20);
int_to_str(second, tmp, 10);
if(second < 10)
{
strcpy(str, "0");
strcat(str, tmp);
clear_buff(tmp, 20);
strcpy(tmp, str);
}
strncat(time_str, tmp, strlength(tmp));//second
strcat(time_str, "\0");//null at the end of string
}
void list_files(char pathname[MAX_STR_SIZE], char file_names[MAX_ARRAY_SIZE][MAX_STR_SIZE], int* file_num)
{
int fd, nread;
char buf[MAX_STR_SIZE];
struct linux_dirent *d;
int bpos;
char d_type;
fd = open(pathname, O_RDONLY | O_DIRECTORY);
print("\nat 1\n");
int file_num_int = 0;
while(1)
{
nread = syscall(SYS_getdents, fd, buf, MAX_STR_SIZE);
if (nread == 0){
break;
}
for (bpos = 0; bpos < nread;)
{
d = (struct linux_dirent *) (buf + bpos);
print(d->d_name);
print("\n");
clear_buff(file_names[file_num_int], MAX_STR_SIZE);
strcpy(file_names[file_num_int], d->d_name);
bpos += d->d_reclen;
(file_num_int) ++;
}
break;
}
*file_num = file_num_int;
}