diff options
Diffstat (limited to 'src/fnettrace/event.c')
-rw-r--r-- | src/fnettrace/event.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/fnettrace/event.c b/src/fnettrace/event.c new file mode 100644 index 000000000..f4ccf5360 --- /dev/null +++ b/src/fnettrace/event.c | |||
@@ -0,0 +1,105 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2023 Firejail Authors | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | #include "fnettrace.h" | ||
21 | |||
22 | typedef struct event_t { | ||
23 | struct event_t *next; | ||
24 | char *record; | ||
25 | } Event; | ||
26 | |||
27 | static Event *event = NULL; | ||
28 | static Event *last_event = NULL; | ||
29 | int ev_cnt = 0; | ||
30 | |||
31 | void ev_clear(void) { | ||
32 | ev_cnt = 0; | ||
33 | Event *ev = event; | ||
34 | while (ev) { | ||
35 | Event *next = ev->next; | ||
36 | free(ev->record); | ||
37 | free(ev); | ||
38 | ev = next; | ||
39 | } | ||
40 | event = NULL; | ||
41 | } | ||
42 | |||
43 | void ev_add(char *record) { | ||
44 | assert(record); | ||
45 | |||
46 | // braking recursivity | ||
47 | if (*record == '\0') | ||
48 | return; | ||
49 | |||
50 | char *ptr = strchr(record, '\n'); | ||
51 | if (ptr) | ||
52 | *ptr = '\0'; | ||
53 | |||
54 | // filter out duplicates | ||
55 | if (event && strcmp(event->record, record) == 0) | ||
56 | return; | ||
57 | |||
58 | Event *ev = malloc(sizeof(Event)); | ||
59 | if (!ev) | ||
60 | errExit("malloc"); | ||
61 | memset(ev, 0, sizeof(Event)); | ||
62 | |||
63 | ev->record = strdup(record); | ||
64 | if (!ev->record) | ||
65 | errExit("strdup"); | ||
66 | |||
67 | if (event == NULL) { | ||
68 | event = ev; | ||
69 | last_event = ev; | ||
70 | } | ||
71 | else { | ||
72 | last_event->next = ev; | ||
73 | last_event = ev; | ||
74 | } | ||
75 | ev_cnt++; | ||
76 | |||
77 | // recursivity | ||
78 | if (ptr) | ||
79 | ev_add(++ptr); | ||
80 | } | ||
81 | |||
82 | void ev_print(FILE *fp) { | ||
83 | assert(fp); | ||
84 | |||
85 | Event *ev = event; | ||
86 | while (ev) { | ||
87 | fprintf(fp, " "); | ||
88 | if (strstr(ev->record, "NXDOMAIN")) { | ||
89 | if (fp == stdout) | ||
90 | ansi_red(ev->record); | ||
91 | else | ||
92 | fprintf(fp, "%s", ev->record); | ||
93 | } | ||
94 | else if (strstr(ev->record, "SSH connection")) { | ||
95 | if (fp == stdout) | ||
96 | ansi_red(ev->record); | ||
97 | else | ||
98 | fprintf(fp, "%s", ev->record); | ||
99 | } | ||
100 | else | ||
101 | fprintf(fp, "%s", ev->record); | ||
102 | fprintf(fp, "\n"); | ||
103 | ev = ev->next; | ||
104 | } | ||
105 | } | ||