diff options
Diffstat (limited to 'src/addr_list.c')
-rw-r--r-- | src/addr_list.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/addr_list.c b/src/addr_list.c new file mode 100644 index 0000000..759907e --- /dev/null +++ b/src/addr_list.c | |||
@@ -0,0 +1,91 @@ | |||
1 | #include "addr_list.h" | ||
2 | |||
3 | #include <stdbool.h> | ||
4 | #include <stddef.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <string.h> | ||
7 | |||
8 | void addr_list_init(struct addr_list* list) { | ||
9 | list->top = NULL; | ||
10 | } | ||
11 | |||
12 | void addr_list_add(struct addr_list* list, unsigned char addr[6]) { | ||
13 | struct addr_list_node* cur = list->top; | ||
14 | struct addr_list_node* prev = NULL; | ||
15 | |||
16 | while (cur != NULL) { | ||
17 | if (!strncmp(cur->value, addr, 6)) { | ||
18 | return; | ||
19 | } | ||
20 | |||
21 | prev = cur; | ||
22 | cur = cur->next; | ||
23 | } | ||
24 | |||
25 | struct addr_list_node* next = (struct addr_list_node*)malloc(sizeof(struct addr_list_node)); | ||
26 | next->next = NULL; | ||
27 | memcpy(next->value, addr, 6); | ||
28 | |||
29 | if (prev == NULL) { | ||
30 | list->top = next; | ||
31 | } else { | ||
32 | prev->next = next; | ||
33 | } | ||
34 | } | ||
35 | |||
36 | bool addr_list_contains(struct addr_list* list, unsigned char addr[6]) { | ||
37 | struct addr_list_node* cur = list->top; | ||
38 | |||
39 | while (cur != NULL) { | ||
40 | if (!strncmp(cur->value, addr, 6)) { | ||
41 | return true; | ||
42 | } | ||
43 | |||
44 | cur = cur->next; | ||
45 | } | ||
46 | |||
47 | return false; | ||
48 | } | ||
49 | |||
50 | void addr_list_remove(struct addr_list* list, unsigned char addr[6]) { | ||
51 | struct addr_list_node* cur = list->top; | ||
52 | struct addr_list_node* prev = NULL; | ||
53 | |||
54 | if (cur == NULL) { | ||
55 | return; | ||
56 | } | ||
57 | |||
58 | while (cur != NULL) { | ||
59 | if (!strncmp(cur->value, addr, 6)) { | ||
60 | break; | ||
61 | } | ||
62 | |||
63 | prev = cur; | ||
64 | cur = cur->next; | ||
65 | |||
66 | if (cur == NULL) { | ||
67 | return; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | if (prev == NULL) { | ||
72 | list->top = cur->next; | ||
73 | } else { | ||
74 | prev->next = cur->next; | ||
75 | } | ||
76 | |||
77 | free(cur); | ||
78 | } | ||
79 | |||
80 | void addr_list_free(struct addr_list* list) { | ||
81 | struct addr_list_node* cur = list->top; | ||
82 | |||
83 | while (cur != NULL) { | ||
84 | struct addr_list_node* next = cur->next; | ||
85 | free(cur); | ||
86 | |||
87 | cur = next; | ||
88 | } | ||
89 | |||
90 | list->top = NULL; | ||
91 | } | ||