diff options
author | 2022-01-18 14:14:44 -0500 | |
---|---|---|
committer | 2022-01-18 14:14:44 -0500 | |
commit | 4dd1e92ba1c0687d3f5860ccc58c80d28c8905b8 (patch) | |
tree | 49fc769149cbc3a343cee46838c6a19c9e4145f3 /src/fnettrace/radix.c | |
parent | gitlab-ci: fix debian_ci build (dh_missing hostnames) (#4865) (diff) | |
download | firejail-4dd1e92ba1c0687d3f5860ccc58c80d28c8905b8.tar.gz firejail-4dd1e92ba1c0687d3f5860ccc58c80d28c8905b8.tar.zst firejail-4dd1e92ba1c0687d3f5860ccc58c80d28c8905b8.zip |
nettrace fixes
Diffstat (limited to 'src/fnettrace/radix.c')
-rw-r--r-- | src/fnettrace/radix.c | 52 |
1 files changed, 38 insertions, 14 deletions
diff --git a/src/fnettrace/radix.c b/src/fnettrace/radix.c index b9104bd39..c9493717d 100644 --- a/src/fnettrace/radix.c +++ b/src/fnettrace/radix.c | |||
@@ -34,18 +34,44 @@ typedef struct rnode_t { | |||
34 | RNode *head = 0; | 34 | RNode *head = 0; |
35 | int radix_nodes = 0; | 35 | int radix_nodes = 0; |
36 | 36 | ||
37 | // get rid of the malloc overhead | ||
38 | #define RNODE_MAX_MALLOC 128 | ||
39 | static RNode *rnode_unused = NULL; | ||
40 | static int rnode_malloc_cnt = 0; | ||
41 | static RNode *rmalloc(void) { | ||
42 | if (rnode_unused == NULL || rnode_malloc_cnt >= RNODE_MAX_MALLOC) { | ||
43 | rnode_unused = malloc(sizeof(RNode) * RNODE_MAX_MALLOC); | ||
44 | if (!rnode_unused) | ||
45 | errExit("malloc"); | ||
46 | memset(rnode_unused, 0, sizeof(RNode) * RNODE_MAX_MALLOC); | ||
47 | rnode_malloc_cnt = 0; | ||
48 | } | ||
49 | |||
50 | rnode_malloc_cnt++; | ||
51 | return rnode_unused + rnode_malloc_cnt - 1; | ||
52 | } | ||
53 | |||
54 | |||
55 | static inline char *duplicate_name(const char *name) { | ||
56 | assert(name); | ||
57 | |||
58 | if (strcmp(name, "United States") == 0) | ||
59 | return "United States"; | ||
60 | else if (strcmp(name, "Amazon") == 0) | ||
61 | return "Amazon"; | ||
62 | return strdup(name); | ||
63 | } | ||
64 | |||
37 | static inline RNode *addOne(RNode *ptr, char *name) { | 65 | static inline RNode *addOne(RNode *ptr, char *name) { |
38 | assert(ptr); | 66 | assert(ptr); |
39 | if (ptr->one) | 67 | if (ptr->one) |
40 | return ptr->one; | 68 | return ptr->one; |
41 | RNode *node = malloc(sizeof(RNode)); | 69 | RNode *node = rmalloc(); |
42 | if (!node) | 70 | assert(node); |
43 | errExit("malloc"); | ||
44 | memset(node, 0, sizeof(RNode)); | ||
45 | if (name) { | 71 | if (name) { |
46 | node->name = strdup(name); | 72 | node->name = duplicate_name(name); |
47 | if (!node->name) | 73 | if (!node->name) |
48 | errExit("strdup"); | 74 | errExit("duplicate name"); |
49 | } | 75 | } |
50 | 76 | ||
51 | ptr->one = node; | 77 | ptr->one = node; |
@@ -56,14 +82,12 @@ static inline RNode *addZero(RNode *ptr, char *name) { | |||
56 | assert(ptr); | 82 | assert(ptr); |
57 | if (ptr->zero) | 83 | if (ptr->zero) |
58 | return ptr->zero; | 84 | return ptr->zero; |
59 | RNode *node = malloc(sizeof(RNode)); | 85 | RNode *node = rmalloc(); |
60 | if (!node) | 86 | assert(node); |
61 | errExit("malloc"); | ||
62 | memset(node, 0, sizeof(RNode)); | ||
63 | if (name) { | 87 | if (name) { |
64 | node->name = strdup(name); | 88 | node->name = duplicate_name(name); |
65 | if (!node->name) | 89 | if (!node->name) |
66 | errExit("strdup"); | 90 | errExit("duplicate name"); |
67 | } | 91 | } |
68 | 92 | ||
69 | ptr->zero = node; | 93 | ptr->zero = node; |
@@ -97,9 +121,9 @@ char *radix_add(uint32_t ip, uint32_t mask, char *name) { | |||
97 | } | 121 | } |
98 | assert(ptr); | 122 | assert(ptr); |
99 | if (!ptr->name) { | 123 | if (!ptr->name) { |
100 | ptr->name = strdup(name); | 124 | ptr->name = duplicate_name(name); |
101 | if (!ptr->name) | 125 | if (!ptr->name) |
102 | errExit("strdup"); | 126 | errExit("duplicate_name"); |
103 | } | 127 | } |
104 | 128 | ||
105 | return ptr->name; | 129 | return ptr->name; |