diff options
author | 2023-07-06 17:11:59 +0000 | |
---|---|---|
committer | 2023-07-06 17:11:59 +0000 | |
commit | 2ed64f0d89da91ad901099385520e879346f7865 (patch) | |
tree | f520d17586a3ab4a51bb43031e5878b69089a19e /src/fnettrace/radix.c | |
parent | rssguard.profile: add netlink to protocol (diff) | |
parent | Merge pull request #5885 from pirate486743186/lobster (diff) | |
download | firejail-2ed64f0d89da91ad901099385520e879346f7865.tar.gz firejail-2ed64f0d89da91ad901099385520e879346f7865.tar.zst firejail-2ed64f0d89da91ad901099385520e879346f7865.zip |
Merge branch 'netblue30:master' into rssguard
Diffstat (limited to 'src/fnettrace/radix.c')
-rw-r--r-- | src/fnettrace/radix.c | 93 |
1 files changed, 90 insertions, 3 deletions
diff --git a/src/fnettrace/radix.c b/src/fnettrace/radix.c index a1d510a61..f0a22da74 100644 --- a/src/fnettrace/radix.c +++ b/src/fnettrace/radix.c | |||
@@ -55,10 +55,14 @@ static RNode *rmalloc(void) { | |||
55 | static inline char *duplicate_name(const char *name) { | 55 | static inline char *duplicate_name(const char *name) { |
56 | assert(name); | 56 | assert(name); |
57 | 57 | ||
58 | if (strcmp(name, "United States") == 0) | 58 | if (strcmp(name, "Amazon") == 0) |
59 | return "United States"; | ||
60 | else if (strcmp(name, "Amazon") == 0) | ||
61 | return "Amazon"; | 59 | return "Amazon"; |
60 | else if (strcmp(name, "Digital Ocean") == 0) | ||
61 | return "Digital Ocean"; | ||
62 | else if (strcmp(name, "Linode") == 0) | ||
63 | return "Linode"; | ||
64 | else if (strcmp(name, "Google") == 0) | ||
65 | return "Google"; | ||
62 | return strdup(name); | 66 | return strdup(name); |
63 | } | 67 | } |
64 | 68 | ||
@@ -152,3 +156,86 @@ char *radix_longest_prefix_match(uint32_t ip) { | |||
152 | 156 | ||
153 | return (rv)? rv->name: NULL; | 157 | return (rv)? rv->name: NULL; |
154 | } | 158 | } |
159 | |||
160 | static uint32_t sum; | ||
161 | static void print(RNode *ptr, int level) { | ||
162 | if (!ptr) | ||
163 | return; | ||
164 | if (ptr->name) { | ||
165 | printf("%d.%d.%d.%d/%d ", PRINT_IP(sum << (32 - level)), level); | ||
166 | printf("%s\n", ptr->name); | ||
167 | } | ||
168 | |||
169 | if (ptr->zero == NULL && ptr->one == NULL) | ||
170 | return; | ||
171 | |||
172 | level++; | ||
173 | sum <<= 1; | ||
174 | print(ptr->zero, level); | ||
175 | sum++; | ||
176 | print(ptr->one, level); | ||
177 | sum--; | ||
178 | sum >>= 1; | ||
179 | } | ||
180 | |||
181 | void radix_print(void) { | ||
182 | if (!head) | ||
183 | return; | ||
184 | printf("\n"); | ||
185 | sum = 0; | ||
186 | print(head->zero, 1); | ||
187 | assert(sum == 0); | ||
188 | sum = 1; | ||
189 | print(head->one, 1); | ||
190 | assert(sum == 1); | ||
191 | } | ||
192 | |||
193 | static inline int strnullcmp(const char *a, const char *b) { | ||
194 | if (!a || !b) | ||
195 | return -1; | ||
196 | return strcmp(a, b); | ||
197 | } | ||
198 | |||
199 | void squash(RNode *ptr, int level) { | ||
200 | if (!ptr) | ||
201 | return; | ||
202 | |||
203 | if (ptr->name == NULL && | ||
204 | ptr->zero && ptr->one && | ||
205 | strnullcmp(ptr->zero->name, ptr->one->name) == 0 && | ||
206 | !ptr->zero->zero && !ptr->zero->one && | ||
207 | !ptr->one->zero && !ptr->one->one) { | ||
208 | ptr->name = ptr->one->name; | ||
209 | // fprintf(stderr, "squashing %d.%d.%d.%d/%d ", PRINT_IP(sum << (32 - level)), level); | ||
210 | // fprintf(stderr, "%s\n", ptr->name); | ||
211 | ptr->zero = NULL; | ||
212 | ptr->one = NULL; | ||
213 | radix_nodes--; | ||
214 | return; | ||
215 | } | ||
216 | |||
217 | if (ptr->zero == NULL && ptr->one == NULL) | ||
218 | return; | ||
219 | |||
220 | level++; | ||
221 | sum <<= 1; | ||
222 | squash(ptr->zero, level); | ||
223 | sum++; | ||
224 | squash(ptr->one, level); | ||
225 | sum--; | ||
226 | sum >>= 1; | ||
227 | } | ||
228 | |||
229 | // using stderr for printing | ||
230 | void radix_squash(void) { | ||
231 | if (!head) | ||
232 | return; | ||
233 | |||
234 | sum = 0; | ||
235 | squash(head->zero, 1); | ||
236 | assert(sum == 0); | ||
237 | sum = 1; | ||
238 | squash(head->one, 1); | ||
239 | assert(sum == 1); | ||
240 | |||
241 | } | ||