summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/lib/save_file.c
blob: b852bd601982e55bbfca8f1c1889fdf6f03b483c (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "ipf.h"
#include "ipmon.h"

static void *file_parse __P((char **));
static void file_destroy __P((void *));
static int file_send __P((void *, ipmon_msg_t *));
static void file_print __P((void *));
static int file_match __P((void *, void *));
static void *file_dup __P((void *));

typedef struct file_opts_s {
	FILE	*fp;
	int	raw;
	char	*path;
	int	ref;
} file_opts_t;

ipmon_saver_t filesaver = {
	"file",
	file_destroy,
	file_dup,
	file_match,
	file_parse,
	file_print,
	file_send
};


static void *
file_parse(strings)
	char **strings;
{
	file_opts_t *ctx;

	ctx = calloc(1, sizeof(*ctx));
	if (ctx == NULL)
		return NULL;

	if (strings[0] != NULL && strings[0][0] != '\0') {
		ctx->ref = 1;
		if (!strncmp(strings[0], "raw://", 6)) {
			ctx->raw = 1;
			ctx->path = strdup(strings[0] + 6);
			ctx->fp = fopen(ctx->path, "ab");
		} else if (!strncmp(strings[0], "file://", 7)) {
			ctx->path = strdup(strings[0] + 7);
			ctx->fp = fopen(ctx->path, "a");
		} else {
			free(ctx);
			ctx = NULL;
		}
	} else {
		free(ctx);
		ctx = NULL;
	}

	return ctx;
}


static int
file_match(ctx1, ctx2)
	void *ctx1, *ctx2;
{
	file_opts_t *f1 = ctx1, *f2 = ctx2;

	if (f1->raw != f2->raw)
		return 1;
	if (strcmp(f1->path, f2->path))
		return 1;
	return 0;
}


static void *
file_dup(ctx)
	void *ctx;
{
	file_opts_t *f = ctx;

	f->ref++;
	return f;
}


static void
file_print(ctx)
	void *ctx;
{
	file_opts_t *file = ctx;

	if (file->raw)
		printf("raw://");
	else
		printf("file://");
	printf("%s", file->path);
}


static void
file_destroy(ctx)
	void *ctx;
{
	file_opts_t *file = ctx;

	file->ref--;
	if (file->ref > 0)
		return;

	if (file->path != NULL)
		free(file->path);
	free(file);
}


static int
file_send(ctx, msg)
	void *ctx;
	ipmon_msg_t *msg;
{
	file_opts_t *file = ctx;

	if (file->raw) {
		fwrite(msg->imm_data, msg->imm_dsize, 1, file->fp);
	} else {
		fprintf(file->fp, "%s", msg->imm_msg);
	}
	return 0;
}

OpenPOWER on IntegriCloud