Repository navigation
Expand file tree
/
Copy pathlist.c
More file actions
114 lines (98 loc) · 2.13 KB
/
Copy pathlist.c
File metadata and controls
114 lines (98 loc) · 2.13 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "list.h"
/* static */ struct listEntry *listHead;
/* static */ struct listEntry *listTail;
int inet_cmp(uint8_t af, void *addr1, void *addr2)
{
if (af == AF_INET)
{
return memcmp(&(((struct in_addr *)addr1)->s_addr), &(((struct in_addr *)addr2)->s_addr), 4);
}
if (af == AF_INET6)
{
return memcmp(&(((struct in6_addr *)addr1)->s6_addr), &(((struct in6_addr *)addr2)->s6_addr), 16);
}
return 0;
}
struct listEntry *_listSearch(struct listEntry *current, uint8_t protocol, uint8_t af, void *addr)
{
char address[256];
if (current == NULL)
{
return NULL;
}
if (protocol == current->protocol
&& current->af == af
&& inet_cmp(af, addr, &(current->addr)) == 0)
{
return current;
}
if (current->next == NULL)
{
return NULL;
}
return _listSearch(current->next, protocol, af, addr);
}
void listAdd(uint8_t protocol, uint8_t af, void *addr)
{
struct listEntry *listNewItem;
listNewItem = malloc(sizeof(struct listEntry));
listNewItem->protocol = protocol;
if (af == AF_INET)
{
listNewItem->af = AF_INET;
listNewItem->addr.ipv4 = *((struct in_addr *)addr);
}
else if (af == AF_INET6)
{
listNewItem->af = AF_INET6;
listNewItem->addr.ipv6 = *((struct in6_addr *)addr);
}
else
{
listNewItem->af = 0;
}
listNewItem->next = NULL;
if (listHead == NULL || listTail == NULL)
{
listHead = listNewItem;
listTail = listNewItem;
return;
}
else
{
listTail->next = listNewItem;
listTail = listNewItem;
}
}
void _listList(struct listEntry *current)
{
char address[256];
if (current == NULL)
{
return;
}
printf("{\"@\":0x%08x,", current);
printf("\"protocol\":%d", current->protocol);
if (current->af == AF_INET)
{
inet_ntop(AF_INET, &(current->addr.ipv4), address, sizeof(address));
printf(",\"address\":\"%s\"", address);
}
if (current->af == AF_INET6)
{
inet_ntop(AF_INET6, &(current->addr.ipv6), address, sizeof(address));
printf(",\"address\":\"%s\"", address);
}
printf("}\n");
if (current->next == NULL)
{
return;
}
_listList(current->next);
}