Repository navigation
Expand file tree
/
Copy pathhost_database.c
More file actions
476 lines (403 loc) · 10.7 KB
/
Copy pathhost_database.c
File metadata and controls
476 lines (403 loc) · 10.7 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
#include "host_database.h"
#include "debug.h"
#include "defines.h"
#include "stdbool.h"
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
// #define DEBUG(args ...) do { } while (0)
/**
* root node
*
*
*
*/
DB_NODE* g_db_nodes_root = NULL;
/****
* database logic
*
*
*
*
*
*
*/
bool is_same_sig(const SIGNATURE &a, const SIGNATURE &b)
{
// check fct_name_len
if ( a.fct_name_len != b.fct_name_len ) {
return false;
}
// check fct_name
for ( unsigned int i = 0 ; i < a.fct_name_len ; i+= 1 ) {
if ( a.fct_name[i] != b.fct_name[i] ) {
return false;
}
}
// check arg_types_len
if ( a.arg_types_len != b.arg_types_len ) {
return false;
}
// check arg_types
for ( unsigned int i = 0 ; i < a.arg_types_len ; i += 1 ) {
if ( a.arg_types[i] != b.arg_types[i] ) {
return false;
}
}
return true;
}
bool is_same_host(const HOST &a, const HOST &b)
{
// WARNING: this doesn't check for HOST::sock_fd
return (a.ip == b.ip) && (a.port == b.port);
}
bool get_db_node(DB_NODE** node, SIGNATURE sig)
{
DB_NODE* temp = g_db_nodes_root;
while ( temp != NULL ) {
if ( is_same_sig(temp->sig,sig) ) {
*node = temp;
return true;
}
temp = temp->next;
}
return false;
}
/**
* put
*
* returns:
* HOST_DB_PUT_SIGNATURE_SUCCESS
* HOST_DB_PUT_SIGNATURE_DUPLICATE if duplicate
*
*
*
*/
int db_put(const HOST &host, SIGNATURE sig)
{
// DEBUG("db_put");
int ret_code = HOST_DB_PUT_SIGNATURE_SUCCESS;
// find the sig
DB_NODE* found_node = NULL;
if ( get_db_node(&found_node,sig) == true ) {
// found a node
if ( db_delete_host(host,sig) == HOST_DB_DELETE_HOST_SUCCESS ) {
// found sig and removed it
ret_code = HOST_DB_PUT_SIGNATURE_DUPLICATE;
}
} else {
// can't find : make a new node
found_node = (DB_NODE*)malloc(sizeof(DB_NODE));
// copy sig
found_node->sig.fct_name_len = sig.fct_name_len;
found_node->sig.fct_name = (char*)malloc(sig.fct_name_len);
memcpy(found_node->sig.fct_name,sig.fct_name,sig.fct_name_len);
found_node->sig.arg_types_len = sig.arg_types_len;
found_node->sig.arg_types = (int*)malloc(4*sig.arg_types_len);
memcpy(found_node->sig.arg_types,sig.arg_types,4*sig.arg_types_len);
// set hosts_root
found_node->hosts_root = NULL;
// set next
found_node->next = NULL;
// append node in the db
if ( g_db_nodes_root == NULL ) {
g_db_nodes_root = found_node;
} else {
DB_NODE* last_node = g_db_nodes_root;
while ( last_node->next != 0 ) {
last_node = last_node->next;
}
last_node->next = found_node;
}
}
// append host
HOST* new_host = (HOST*)malloc(sizeof(HOST));
new_host->sock_fd = host.sock_fd;
new_host->ip = host.ip;
new_host->port = host.port;
new_host->next = NULL;
if ( found_node->hosts_root == NULL ) {
// if node is empty
found_node->hosts_root = new_host;
} else {
// if node not empty
HOST* last_host = found_node->hosts_root;
while ( last_host->next != 0 ) {
last_host = last_host->next;
}
last_host->next = new_host;
}
return ret_code;
}
/**
* get
*
*
*
*
*
*
*/
int db_get(HOST *host, SIGNATURE sig)
{
// DEBUG("db_get");
// find the sig
DB_NODE* found_node = NULL;
if ( get_db_node(&found_node,sig) == false ) {
return HOST_DB_GET_SIGNATURE_NOT_FOUND;
}
// return the first ip
HOST* first_host = found_node->hosts_root;
if ( first_host == NULL ) {
host->sock_fd = 0;
host->ip = 0;
host->port = 0;
return HOST_DB_GET_SIGNATURE_HAS_NO_HOSTS;
}
host->sock_fd = first_host->sock_fd;
host->ip = first_host->ip;
host->port = first_host->port;
// put head node to end
// HOST* last_host = host;
// while( last_host->next != NULL ) {
// last_host = last_host->next;
// }
// if ( last_host != host ) {
// found_node->hosts_root = found_node->hosts_root->next;
// last_host->next = host;
// host->next = NULL;
// }
// put the HOST of for every sig to the end
DB_NODE* temp_node = g_db_nodes_root;
while ( temp_node != NULL ) {
HOST* temp_host = temp_node->hosts_root;
HOST* prev_host = NULL;
while ( temp_host != NULL ) {
if ( is_same_host(*temp_host,*first_host) ) {
// find last
HOST* temp_last = temp_host;
while ( temp_last->next != NULL ) {
temp_last = temp_last->next;
}
if ( temp_last != temp_host ) {
temp_last->next = temp_host;
if ( prev_host != NULL ) {
prev_host->next = temp_host->next;
} else {
temp_node->hosts_root = temp_host->next;
}
temp_host->next = NULL;
} // if it's last, do nothing
} // if same host
prev_host = temp_host;
temp_host = temp_host->next;
} // while hosts
temp_node = temp_node->next;
} // while sigs
return HOST_DB_GET_SIGNATURE_FOUND;
}
/**
* delete host
*
*
*
*
*
*
*/
int db_delete_host(const HOST &host, SIGNATURE sig)
{
// DEBUG("db_delete_host");
// find the sig
DB_NODE* found_node = NULL;
if ( get_db_node(&found_node,sig) == false ) {
return HOST_DB_DELETE_SIGNATURE_NOT_FOUND;
}
// look for the host
HOST* temp_host = found_node->hosts_root;
while ( temp_host != NULL ) {
if ( is_same_host(*temp_host,host) ) {
// found it
if ( temp_host == found_node->hosts_root ) {
// if it's first
found_node->hosts_root = temp_host->next;
} else {
// find the previous one
HOST* prev = found_node->hosts_root;
while ( prev->next != temp_host ) {
prev = prev->next;
}
prev->next = temp_host->next;
}
free(temp_host);
break;
}
temp_host = temp_host->next;
}
if ( temp_host == NULL ) {
return HOST_DB_DELETE_HOST_NOT_FOUND;
} else {
return HOST_DB_DELETE_HOST_SUCCESS;
}
}
/**
* drop
*
*
*
*
*
*
*/
int db_drop()
{
// DEBUG("db_drop");
DB_NODE* temp_node = g_db_nodes_root;
HOST* temp_host = NULL;
while( temp_node != NULL ) {
g_db_nodes_root = temp_node->next;
free(temp_node->sig.fct_name);
free(temp_node->sig.arg_types);
temp_host = temp_node->hosts_root;
while( temp_host != NULL ) {
temp_node->hosts_root = temp_host->next;
free(temp_host);
temp_host = temp_node->hosts_root;
}
free(temp_node);
temp_node = g_db_nodes_root;
}
return 0;
}
/**
* size
*
*
*
*
*
*
*/
unsigned int db_size()
{
int size = 0 ;
DB_NODE* temp_node = g_db_nodes_root;
while( temp_node != NULL ) {
HOST* temp_host = temp_node->hosts_root;
while ( temp_host != NULL ) {
size += 1;
temp_host = temp_host->next;
}
temp_node = temp_node->next;
}
return size;
}
/**
* print
*
*
*
*
*
*
*/
int db_print()
{
DEBUG("printing db start ====");
DB_NODE* temp_node = g_db_nodes_root;
while ( temp_node != NULL ) {
DEBUG("N:%s",temp_node->sig.fct_name);
for ( unsigned int i = 0 ; i < temp_node->sig.arg_types_len ; i += 1 ) {
DEBUG("Arg:%x %x %x",
(temp_node->sig.arg_types[i] >> 24) & 0xff,
(temp_node->sig.arg_types[i] >> 16) & 0xff,
(temp_node->sig.arg_types[i] >> 0) & 0xffff);
}
HOST* temp_host = temp_node->hosts_root;
while ( temp_host != NULL ) {
DEBUG(" H:%x.%x.%x.%x:%x sock:%d",
(temp_host->ip >> 24 ) & 0xff,
(temp_host->ip >> 16 ) & 0xff,
(temp_host->ip >> 8 ) & 0xff,
(temp_host->ip >> 0 ) & 0xff,
(temp_host->port >> 0 ) & 0xffff,
temp_host->sock_fd
);
temp_host = temp_host->next;
}
temp_node = temp_node->next;
}
DEBUG("printing db end ====");
return 0;
}
/**
* get_all
*
*
*
*
*
*
*/
int db_get_all(unsigned int *hosts_len, unsigned int **ips, unsigned int **ports, SIGNATURE sig)
{
// find the sig
DB_NODE* found_node = NULL;
if ( get_db_node(&found_node,sig) == false ) {
return HOST_DB_GET_SIGNATURE_NOT_FOUND;
}
// calculate the size first
*hosts_len = 0;
HOST* temp_host = found_node->hosts_root;
while ( temp_host != NULL ) {
*hosts_len += 1;
temp_host = temp_host->next;
}
if ( *hosts_len == 0 ) {
return HOST_DB_GET_SIGNATURE_HAS_NO_HOSTS;
}
*ips = (unsigned int*)malloc((*hosts_len)*sizeof(unsigned int));
*ports = (unsigned int*)malloc((*hosts_len)*sizeof(unsigned int));
// copy all hosts to ips and ports
temp_host = found_node->hosts_root;
unsigned int index = 0;
while ( temp_host != NULL ) {
(*ips)[index] = temp_host->ip;
(*ports)[index] = temp_host->port;
index += 1;
temp_host = temp_host->next;
}
return HOST_DB_GET_SIGNATURE_FOUND;
}
// returns # of nodes deleted
int db_delete_all_for_sock(const int sock_fd)
{
int num_deleted = 0;
DB_NODE* temp_node = g_db_nodes_root;
// loop all the sigs
while( temp_node != NULL ) {
HOST* temp_host = temp_node->hosts_root;
HOST host_to_delete;
bool found = false;
// loop all the hosts
while ( temp_host != NULL ) {
if ( temp_host->sock_fd == sock_fd ) {
host_to_delete.sock_fd = temp_host->sock_fd;
host_to_delete.ip = temp_host->ip;
host_to_delete.port = temp_host->port;
found = true;
break;
}
temp_host = temp_host->next;
}
if ( found ) {
if ( db_delete_host(host_to_delete,temp_node->sig) == HOST_DB_DELETE_HOST_SUCCESS ) {
num_deleted += 1;
} else {
DEBUG("couldn't delete host: %x:%d",host_to_delete.ip,host_to_delete.port);
}
}
temp_node = temp_node->next;
}
return num_deleted;
}