1 #include "ipf.h"
2 #include "ipmon.h"
3
4 static void *file_parse __P((char **));
5 static void file_destroy __P((void *));
6 static int file_send __P((void *, ipmon_msg_t *));
7 static void file_print __P((void *));
8 static int file_match __P((void *, void *));
9 static void *file_dup __P((void *));
10
11 typedef struct file_opts_s {
12 FILE *fp;
13 int raw;
14 char *path;
15 int ref;
16 } file_opts_t;
17
18 ipmon_saver_t filesaver = {
19 "file",
20 file_destroy,
21 file_dup,
22 file_match,
23 file_parse,
24 file_print,
25 file_send
26 };
27
28
29 static void *
file_parse(strings)30 file_parse(strings)
31 char **strings;
32 {
33 file_opts_t *ctx;
34
35 ctx = calloc(1, sizeof(*ctx));
36 if (ctx == NULL)
37 return NULL;
38
39 if (strings[0] != NULL && strings[0][0] != '\0') {
40 ctx->ref = 1;
41 if (!strncmp(strings[0], "raw://", 6)) {
42 ctx->raw = 1;
43 ctx->path = strdup(strings[0] + 6);
44 ctx->fp = fopen(ctx->path, "ab");
45 } else if (!strncmp(strings[0], "file://", 7)) {
46 ctx->path = strdup(strings[0] + 7);
47 ctx->fp = fopen(ctx->path, "a");
48 } else {
49 free(ctx);
50 ctx = NULL;
51 }
52 } else {
53 free(ctx);
54 ctx = NULL;
55 }
56
57 return ctx;
58 }
59
60
61 static int
file_match(ctx1,ctx2)62 file_match(ctx1, ctx2)
63 void *ctx1, *ctx2;
64 {
65 file_opts_t *f1 = ctx1, *f2 = ctx2;
66
67 if (f1->raw != f2->raw)
68 return 1;
69 if (strcmp(f1->path, f2->path))
70 return 1;
71 return 0;
72 }
73
74
75 static void *
file_dup(ctx)76 file_dup(ctx)
77 void *ctx;
78 {
79 file_opts_t *f = ctx;
80
81 f->ref++;
82 return f;
83 }
84
85
86 static void
file_print(ctx)87 file_print(ctx)
88 void *ctx;
89 {
90 file_opts_t *file = ctx;
91
92 if (file->raw)
93 printf("raw://");
94 else
95 printf("file://");
96 printf("%s", file->path);
97 }
98
99
100 static void
file_destroy(ctx)101 file_destroy(ctx)
102 void *ctx;
103 {
104 file_opts_t *file = ctx;
105
106 file->ref--;
107 if (file->ref > 0)
108 return;
109
110 if (file->path != NULL)
111 free(file->path);
112 free(file);
113 }
114
115
116 static int
file_send(ctx,msg)117 file_send(ctx, msg)
118 void *ctx;
119 ipmon_msg_t *msg;
120 {
121 file_opts_t *file = ctx;
122
123 if (file->raw) {
124 fwrite(msg->imm_data, msg->imm_dsize, 1, file->fp);
125 } else {
126 fprintf(file->fp, "%s", msg->imm_msg);
127 }
128 return 0;
129 }
130
131