aboutsummaryrefslogtreecommitdiffstats
path: root/src/fnettrace/event.c
blob: f4ccf53605b6b58f28e48f051e9cc42e3313dd7c (plain) (blame)
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
/*
 * Copyright (C) 2014-2023 Firejail Authors
 *
 * This file is part of firejail project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "fnettrace.h"

typedef struct event_t {
	struct event_t *next;
	char *record;
} Event;

static Event *event = NULL;
static Event *last_event = NULL;
int ev_cnt = 0;

void ev_clear(void) {
	ev_cnt = 0;
	Event *ev = event;
	while (ev) {
		Event *next = ev->next;
		free(ev->record);
		free(ev);
		ev = next;
	}
	event = NULL;
}

void ev_add(char *record) {
	assert(record);

	// braking recursivity
	if (*record == '\0')
		return;

	char *ptr = strchr(record, '\n');
	if (ptr)
		*ptr = '\0';

	// filter out duplicates
	if (event && strcmp(event->record, record) == 0)
		return;

	Event *ev = malloc(sizeof(Event));
	if (!ev)
		errExit("malloc");
	memset(ev, 0, sizeof(Event));

	ev->record = strdup(record);
	if (!ev->record)
		errExit("strdup");

	if (event == NULL) {
		event = ev;
		last_event = ev;
	}
	else {
		last_event->next = ev;
		last_event = ev;
	}
	ev_cnt++;

	// recursivity
	if (ptr)
		ev_add(++ptr);
}

void ev_print(FILE *fp) {
	assert(fp);

	Event *ev = event;
	while (ev) {
		fprintf(fp, "   ");
		if (strstr(ev->record, "NXDOMAIN")) {
			if (fp == stdout)
				ansi_red(ev->record);
			else
				fprintf(fp, "%s", ev->record);
		}
		else if (strstr(ev->record, "SSH connection")) {
			if (fp == stdout)
				ansi_red(ev->record);
			else
				fprintf(fp, "%s", ev->record);
		}
		else
			fprintf(fp, "%s", ev->record);
		fprintf(fp, "\n");
		ev = ev->next;
	}
}