-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibpcap-gzip.c
202 lines (168 loc) · 4.02 KB
/
libpcap-gzip.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined(__linux__)
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <zlib.h>
#include <pcap.h>
#if HAVE_FUNOPEN
static int gzip_cookie_read(void *cookie, char *buf, int size)
{
return gzread((gzFile)cookie, (voidp)buf, (unsigned)size);
}
static int gzip_cookie_write(void *cookie, const char *buf, int size)
{
return gzwrite((gzFile)cookie, (voidpc)buf, (unsigned)size);
}
static int gzip_cookie_close(void *cookie)
{
pcap_ioplugin_unregister_fp_cookie(cookie);
return gzclose((gzFile)cookie);
}
#elif HAVE_FOPENCOOKIE
static ssize_t gzip_cookie_read(void *cookie, char *buf, size_t size)
{
return gzread((gzFile)cookie, (voidp)buf, (unsigned)size);
}
static ssize_t gzip_cookie_write(void *cookie, const char *buf, size_t size)
{
return gzwrite((gzFile)cookie, (voidpc)buf, (unsigned)size);
}
static int gzip_cookie_close(void *cookie)
{
pcap_ioplugin_unregister_fp_cookie(cookie);
return gzclose((gzFile)cookie);
}
static cookie_io_functions_t gzip_read_funcs = {
.read = gzip_cookie_read,
.close = gzip_cookie_close
};
static cookie_io_functions_t gzip_write_funcs = {
.write = gzip_cookie_write,
.close = gzip_cookie_close
};
#else
#error "no funopen() or fopencookie() available"
#endif
static FILE *gzip_open_read(const char *fname, char *errbuf)
{
int fd = -1;
gzFile cookie;
FILE *fp;
if (strcmp(fname, "-") == 0) {
/*
* dup so that zlib or libpcap don't close the real stdin.
*
* not sure why, but libpcap refuses to close stdin itself,
* perhaps to avoid a SIGPIPE
*/
fd = dup(fileno(stdin));
if (fd < 0) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: dup: %s",
fname, pcap_strerror(errno));
return NULL;
}
} else {
fd = open(fname, O_RDONLY);
if (fd < 0) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: open: %s",
fname, pcap_strerror(errno));
return NULL;
}
}
cookie = gzdopen(fd, "r");
if (!cookie) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: gzdopen: %s",
fname, pcap_strerror(errno));
goto fail;
}
#if HAVE_FUNOPEN
fp = funopen(cookie, gzip_cookie_read, NULL, NULL, gzip_cookie_close);
if (!fp) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: funopen: %s",
fname, pcap_strerror(errno));
goto fail;
}
#elif HAVE_FOPENCOOKIE
fp = fopencookie(cookie, "r", gzip_read_funcs);
if (!fp) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: fopencookie: %s",
fname, pcap_strerror(errno));
goto fail;
}
#endif
return fp;
fail:
if (cookie) {
gzclose(cookie);
} else if (fd >= 0) {
close(fd);
}
return NULL;
}
static FILE *gzip_open_write(const char *fname, char *errbuf)
{
int fd = -1;
gzFile cookie;
FILE *fp;
if (strcmp(fname, "-") == 0) {
fd = fileno(stdout);
if (fd < 0) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: fileno: %s",
fname, pcap_strerror(errno));
return NULL;
}
} else {
fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: open: %s",
fname, pcap_strerror(errno));
return NULL;
}
}
cookie = gzdopen(fd, "w");
if (!cookie) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: gzdopen: %s",
fname, pcap_strerror(errno));
goto fail;
}
/* TODO: extra option support */
#if HAVE_FUNOPEN
fp = funopen(cookie, NULL, gzip_cookie_write, NULL, gzip_cookie_close);
if (!fp) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: funopen: %s",
fname, pcap_strerror(errno));
goto fail;
}
#elif HAVE_FOPENCOOKIE
fp = fopencookie(cookie, "w", gzip_write_funcs);
if (!fp) {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: fopencookie: %s",
fname, pcap_strerror(errno));
goto fail;
}
#endif
pcap_ioplugin_register_fp_cookie(fp, cookie);
return fp;
fail:
if (cookie) {
gzclose(cookie);
} else if (fd >= 0) {
close(fd);
}
return NULL;
}
const pcap_ioplugin_t* ioplugin_init() {
static pcap_ioplugin_t plugin = {
.open_read = gzip_open_read,
.open_write = gzip_open_write
};
return &plugin;
}