1254219Scy#include "ipf.h"
2254219Scy#include "ipmon.h"
3254219Scy
4254219Scystatic void *file_parse __P((char **));
5254219Scystatic void file_destroy __P((void *));
6254219Scystatic int file_send __P((void *, ipmon_msg_t *));
7254219Scystatic void file_print __P((void *));
8254219Scystatic int file_match __P((void *, void *));
9254219Scystatic void *file_dup __P((void *));
10254219Scy
11254219Scytypedef struct file_opts_s {
12254219Scy	FILE	*fp;
13254219Scy	int	raw;
14254219Scy	char	*path;
15254219Scy	int	ref;
16254219Scy} file_opts_t;
17254219Scy
18254219Scyipmon_saver_t filesaver = {
19254219Scy	"file",
20254219Scy	file_destroy,
21254219Scy	file_dup,
22254219Scy	file_match,
23254219Scy	file_parse,
24254219Scy	file_print,
25254219Scy	file_send
26254219Scy};
27254219Scy
28254219Scy
29254219Scystatic void *
30254219Scyfile_parse(strings)
31254219Scy	char **strings;
32254219Scy{
33254219Scy	file_opts_t *ctx;
34254219Scy
35254219Scy	ctx = calloc(1, sizeof(*ctx));
36254219Scy	if (ctx == NULL)
37254219Scy		return NULL;
38254219Scy
39254219Scy	if (strings[0] != NULL && strings[0][0] != '\0') {
40254219Scy		ctx->ref = 1;
41254219Scy		if (!strncmp(strings[0], "raw://", 6)) {
42254219Scy			ctx->raw = 1;
43254219Scy			ctx->path = strdup(strings[0] + 6);
44254219Scy			ctx->fp = fopen(ctx->path, "ab");
45254219Scy		} else if (!strncmp(strings[0], "file://", 7)) {
46254219Scy			ctx->path = strdup(strings[0] + 7);
47254219Scy			ctx->fp = fopen(ctx->path, "a");
48254219Scy		} else {
49254219Scy			free(ctx);
50254219Scy			ctx = NULL;
51254219Scy		}
52254219Scy	} else {
53254219Scy		free(ctx);
54254219Scy		ctx = NULL;
55254219Scy	}
56254219Scy
57254219Scy	return ctx;
58254219Scy}
59254219Scy
60254219Scy
61254219Scystatic int
62254219Scyfile_match(ctx1, ctx2)
63254219Scy	void *ctx1, *ctx2;
64254219Scy{
65254219Scy	file_opts_t *f1 = ctx1, *f2 = ctx2;
66254219Scy
67254219Scy	if (f1->raw != f2->raw)
68254219Scy		return 1;
69254219Scy	if (strcmp(f1->path, f2->path))
70254219Scy		return 1;
71254219Scy	return 0;
72254219Scy}
73254219Scy
74254219Scy
75254219Scystatic void *
76254219Scyfile_dup(ctx)
77254219Scy	void *ctx;
78254219Scy{
79254219Scy	file_opts_t *f = ctx;
80254219Scy
81254219Scy	f->ref++;
82254219Scy	return f;
83254219Scy}
84254219Scy
85254219Scy
86254219Scystatic void
87254219Scyfile_print(ctx)
88254219Scy	void *ctx;
89254219Scy{
90254219Scy	file_opts_t *file = ctx;
91254219Scy
92254219Scy	if (file->raw)
93254219Scy		printf("raw://");
94254219Scy	else
95254219Scy		printf("file://");
96254219Scy	printf("%s", file->path);
97254219Scy}
98254219Scy
99254219Scy
100254219Scystatic void
101254219Scyfile_destroy(ctx)
102254219Scy	void *ctx;
103254219Scy{
104254219Scy	file_opts_t *file = ctx;
105254219Scy
106254219Scy	file->ref--;
107254219Scy	if (file->ref > 0)
108254219Scy		return;
109254219Scy
110254219Scy	if (file->path != NULL)
111254219Scy		free(file->path);
112254219Scy	free(file);
113254219Scy}
114254219Scy
115254219Scy
116254219Scystatic int
117254219Scyfile_send(ctx, msg)
118254219Scy	void *ctx;
119254219Scy	ipmon_msg_t *msg;
120254219Scy{
121254219Scy	file_opts_t *file = ctx;
122254219Scy
123254219Scy	if (file->raw) {
124254219Scy		fwrite(msg->imm_data, msg->imm_dsize, 1, file->fp);
125254219Scy	} else {
126254219Scy		fprintf(file->fp, "%s", msg->imm_msg);
127254219Scy	}
128254219Scy	return 0;
129254219Scy}
130254219Scy
131