-
Notifications
You must be signed in to change notification settings - Fork 5
/
tshark-capture.c
487 lines (399 loc) · 16.3 KB
/
tshark-capture.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include "config.h"
#include <stddef.h>
#include <stdio.h>
#include <capture_opts.h>
#include "capchild/capture_session.h"
#include "capchild/capture_sync.h"
#include <epan/epan.h>
#include <epan/epan-int.h>
#include <glib.h>
#include <log.h>
#include <file.h>
#include <wsutil/filesystem.h>
#include <wiretap/wtap.h>
#include "ws_dissect.h"
#include "ws_capture.h"
#include "ws_capture-internal.h"
#include "frame_tvbuff.h"
#include <epan/epan_dissect.h>
#include <capture_info.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#define CAPTURE_OF(session) ((ws_capture_t*)((char*)(session)->cf - offsetof(ws_capture_t, cfile)))
//#define CAPTURE_OF(session) container_of(session->cf, ws_capture_t, cfile)
#define CAPTURE_CALLBACK(session, func, ...) do { \
ws_capture_t *_ws = CAPTURE_OF(session); \
if (_ws->dumpcap->cb.func) {_ws->dumpcap->cb.func(_ws, __VA_ARGS__);} \
} while (0)
#define TL_REQUIRES_PROTO_TREE 0
/* XXX racy */
pipe_input_t *pipe_input;
#include "cfile.h"
// FIXME we don't use dfilter
#include <epan/dfilter/dfilter.h>
#define cmdarg_err(...) do { \
fprintf(stderr, __VA_ARGS__); \
putchar('\n'); \
} while (0)
#define cmdarg(...) do { \
printf(__VA_ARGS__); \
putchar("\n"); \
} while (0)
#define cmdarg_err_cont cmdarg_err
#undef SIGINFO
const nstime_t * tshark_get_frame_ts(void *data, guint32 frame_num);
void
capture_input_error_message(capture_session *cap_session, char *error_msg, char *secondary_error_msg)
{
cmdarg_err("%s", error_msg);
cmdarg_err_cont("%s", secondary_error_msg);
CAPTURE_CALLBACK(cap_session, input_error_message, error_msg, secondary_error_msg);
CAPTURE_OF(cap_session)->dumpcap->err = -1;
CAPTURE_OF(cap_session)->dumpcap->err_info = g_strdup_printf("%s\n%s", error_msg, secondary_error_msg);
}
extern epan_t * tshark_epan_new(capture_file *cf);
void
capture_input_cfilter_error_message(capture_session *cap_session, guint i, char *error_message)
{
capture_options *capture_opts = cap_session->capture_opts;
dfilter_t *rfcode = NULL;
interface_options interface_opts;
g_assert(i < capture_opts->ifaces->len);
interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
if (dfilter_compile(interface_opts.cfilter, &rfcode, NULL) && rfcode != NULL) {
cmdarg_err(
"Invalid capture filter \"%s\" for interface '%s'.\n"
"\n"
"That string looks like a valid display filter; however, it isn't a valid\n"
"capture filter (%s).\n"
"\n"
"Note that display filters and capture filters don't have the same syntax,\n"
"so you can't use most display filter expressions as capture filters.\n"
"\n"
"See the User's Guide for a description of the capture filter syntax.",
interface_opts.cfilter, interface_opts.descr, error_message);
dfilter_free(rfcode);
} else {
cmdarg_err(
"Invalid capture filter \"%s\" for interface '%s'.\n"
"\n"
"That string isn't a valid capture filter (%s).\n"
"See the User's Guide for a description of the capture filter syntax.",
interface_opts.cfilter, interface_opts.descr, error_message);
}
CAPTURE_CALLBACK(cap_session, input_cfilter_error_message, i, error_message);
}
/* capture child detected any packet drops? */
void
capture_input_drops(capture_session *cap_session, guint32 dropped)
{
if (dropped != 0) {
/* We're printing packet counts to stderr.
Send a newline so that we move to the line after the packet count. */
fprintf(stderr, "%u packet(s) dropped\n", dropped);
}
CAPTURE_CALLBACK(cap_session, input_drops, dropped);
}
/*
* Capture child closed its side of the pipe, report any error and
* do the required cleanup.
*/
void
capture_input_closed(capture_session *cap_session, gchar *msg)
{
capture_file *cf = (capture_file *) cap_session->cf;
if (msg != NULL)
fprintf(stderr, "tshark: %s\n", msg);
if (cf != NULL && cf->wth != NULL) {
wtap_close(cf->wth);
if (cf->is_tempfile) {
unlink(cf->filename);
}
}
//FIXME!
/*loop_running = FALSE;*/
CAPTURE_CALLBACK(cap_session, input_closed, msg);
}
gboolean
capture_input_new_file(capture_session *cap_session, gchar *new_file)
{
capture_options *capture_opts = cap_session->capture_opts;
capture_file *cf = (capture_file *) cap_session->cf;
gboolean is_tempfile;
int err;
if (cap_session->state == CAPTURE_PREPARING) {
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started.");
}
g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file);
g_assert(cap_session->state == CAPTURE_PREPARING || cap_session->state == CAPTURE_RUNNING);
/* free the old filename */
if (capture_opts->save_file != NULL) {
/* we start a new capture file, close the old one (if we had one before) */
if (cf->state != FILE_CLOSED) {
if (cf->wth != NULL) {
wtap_close(cf->wth);
cf->wth = NULL;
}
cf->state = FILE_CLOSED;
}
g_free(capture_opts->save_file);
is_tempfile = FALSE;
epan_free(cf->epan);
cf->epan = epan_new();
cf->epan->data = cf;
cf->epan->get_frame_ts = tshark_get_frame_ts;
cf->epan->get_interface_name = cap_file_get_interface_name;
cf->epan->get_user_comment = NULL;
} else {
/* we didn't had a save_file before, must be a tempfile */
is_tempfile = TRUE;
}
/* save the new filename */
capture_opts->save_file = g_strdup(new_file);
/* if we are in real-time mode, open the new file now */
/* this is probably unecessary, but better safe than sorry */
((capture_file *)cap_session->cf)->open_type = WTAP_TYPE_AUTO;
/* Attempt to open the capture file and set up to read from it. */
switch(cf_open((capture_file *)cap_session->cf, capture_opts->save_file, WTAP_TYPE_AUTO, is_tempfile, &err)) {
case CF_OK:
break;
case CF_ERROR:
/* Don't unlink (delete) the save file - leave it around,
for debugging purposes. */
g_free(capture_opts->save_file);
capture_opts->save_file = NULL;
return FALSE;
}
cap_session->state = CAPTURE_RUNNING;
CAPTURE_CALLBACK(cap_session, input_new_file, new_file);
CAPTURE_OF(cap_session)->dumpcap->err = 0;
CAPTURE_OF(cap_session)->dumpcap->err_info = NULL;
return TRUE;
}
static const char *
cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
int file_type)
{
const char *errmsg = "";
static char errmsg_errno[1024+1];
if (err < 0) {
/* Wiretap error. */
switch (err) {
case WTAP_ERR_NOT_REGULAR_FILE:
errmsg = "The file \"%s\" is a \"special file\" or socket or other non-regular file.";
break;
case WTAP_ERR_RANDOM_OPEN_PIPE:
/* Seen only when opening a capture file for reading. */
errmsg = "The file \"%s\" is a pipe or FIFO; TShark can't read pipe or FIFO files in two-pass mode.";
break;
case WTAP_ERR_FILE_UNKNOWN_FORMAT:
/* Seen only when opening a capture file for reading. */
errmsg = "The file \"%s\" isn't a capture file in a format TShark understands.";
break;
case WTAP_ERR_UNSUPPORTED:
/* Seen only when opening a capture file for reading. */
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
"The file \"%%s\" contains record data that TShark doesn't support.\n"
"(%s)",
err_info != NULL ? err_info : "no information supplied");
g_free(err_info);
errmsg = errmsg_errno;
break;
case WTAP_ERR_CANT_WRITE_TO_PIPE:
/* Seen only when opening a capture file for writing. */
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
"The file \"%%s\" is a pipe, and \"%s\" capture files can't be "
"written to a pipe.", wtap_file_type_subtype_short_string(file_type));
errmsg = errmsg_errno;
break;
case WTAP_ERR_UNWRITABLE_FILE_TYPE:
/* Seen only when opening a capture file for writing. */
errmsg = "TShark doesn't support writing capture files in that format.";
break;
case WTAP_ERR_UNWRITABLE_ENCAP:
/* Seen only when opening a capture file for writing. */
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
"TShark can't save this capture as a \"%s\" file.",
wtap_file_type_subtype_short_string(file_type));
errmsg = errmsg_errno;
break;
case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
if (for_writing) {
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
"TShark can't save this capture as a \"%s\" file.",
wtap_file_type_subtype_short_string(file_type));
errmsg = errmsg_errno;
} else
errmsg = "The file \"%s\" is a capture for a network type that TShark doesn't support.";
break;
case WTAP_ERR_BAD_FILE:
/* Seen only when opening a capture file for reading. */
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
"The file \"%%s\" appears to be damaged or corrupt.\n"
"(%s)",
err_info != NULL ? err_info : "no information supplied");
g_free(err_info);
errmsg = errmsg_errno;
break;
case WTAP_ERR_CANT_OPEN:
if (for_writing)
errmsg = "The file \"%s\" could not be created for some unknown reason.";
else
errmsg = "The file \"%s\" could not be opened for some unknown reason.";
break;
case WTAP_ERR_SHORT_READ:
errmsg = "The file \"%s\" appears to have been cut short"
" in the middle of a packet or other data.";
break;
case WTAP_ERR_SHORT_WRITE:
errmsg = "A full header couldn't be written to the file \"%s\".";
break;
case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
errmsg = "This file type cannot be written as a compressed file.";
break;
case WTAP_ERR_DECOMPRESS:
/* Seen only when opening a capture file for reading. */
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
"The compressed file \"%%s\" appears to be damaged or corrupt.\n"
"(%s)",
err_info != NULL ? err_info : "no information supplied");
g_free(err_info);
errmsg = errmsg_errno;
break;
default:
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
"The file \"%%s\" could not be %s: %s.",
for_writing ? "created" : "opened",
wtap_strerror(err));
errmsg = errmsg_errno;
break;
}
} else {}
//FIXME
/*errmsg = file_open_error_message(err, for_writing);*/
return errmsg;
}
cf_status_t
cf_open(capture_file *cf, const char *fname, unsigned int type, gboolean is_tempfile, int *err)
{
wtap *wth;
gchar *err_info;
char err_msg[2048+1];
wth = wtap_open_offline(fname, type, err, &err_info, TRUE);
if (wth == NULL)
goto fail;
/* The open succeeded. Fill in the information for this file. */
/* Create new epan session for dissection. */
epan_free(cf->epan);
cf->epan = tshark_epan_new(cf);
cf->wth = wth;
cf->f_datalen = 0; /* not used, but set it anyway */
/* Set the file name because we need it to set the follow stream filter.
XXX - is that still true? We need it for other reasons, though,
in any case. */
cf->filename = g_strdup(fname);
/* Indicate whether it's a permanent or temporary file. */
cf->is_tempfile = is_tempfile;
/* No user changes yet. */
cf->unsaved_changes = FALSE;
cf->cd_t = wtap_file_type_subtype(cf->wth);
cf->open_type = type;
cf->count = 0;
cf->drops_known = FALSE;
cf->drops = 0;
cf->snap = wtap_snapshot_length(cf->wth);
if (cf->snap == 0) {
/* Snapshot length not known. */
cf->snap = WTAP_MAX_PACKET_SIZE_STANDARD;
}
nstime_set_zero(&cf->elapsed_time);
cf->state = FILE_READ_IN_PROGRESS;
#if 0
wtap_set_cb_new_ipv4(cf->wth, add_ipv4_name);
wtap_set_cb_new_ipv6(cf->wth, (wtap_new_ipv6_callback_t) add_ipv6_name);
#endif
return CF_OK;
fail:
g_snprintf(err_msg, sizeof err_msg,
cf_open_error_message(*err, err_info, FALSE, cf->cd_t), fname);
cmdarg_err("%s", err_msg);
return CF_ERROR;
}
void
capture_input_new_packets(capture_session *cap_session, int to_read)
{
CAPTURE_CALLBACK(cap_session, input_new_packets, to_read);
}
#ifdef _WIN32
/* The timer has expired, see if there's stuff to read from the pipe,
if so, do the callback */
static gint
pipe_timer_cb(gpointer data)
{
HANDLE handle;
DWORD avail = 0;
gboolean result;
DWORD childstatus;
pipe_input_t *pipe_input_p = data;
gint iterations = 0;
g_mutex_lock (pipe_input_p->callback_running);
/* try to read data from the pipe only 5 times, to avoid blocking */
while(iterations < 5) {
/*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: new iteration");*/
/* Oddly enough although Named pipes don't work on win9x,
PeekNamedPipe does !!! */
handle = (HANDLE) _get_osfhandle (pipe_input_p->source);
result = PeekNamedPipe(handle, NULL, 0, NULL, &avail, NULL);
/* Get the child process exit status */
GetExitCodeProcess((HANDLE)*(pipe_input_p->child_process),
&childstatus);
/* If the Peek returned an error, or there are bytes to be read
or the childwatcher thread has terminated then call the normal
callback */
if (!result || avail > 0 || childstatus != STILL_ACTIVE) {
/*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: data avail");*/
/* And call the real handler */
if (!pipe_input_p->input_cb(pipe_input_p->source, pipe_input_p->user_data)) {
g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: input pipe closed, iterations: %u", iterations);
/* pipe closed, return false so that the timer is stopped */
g_mutex_unlock (pipe_input_p->callback_running);
return FALSE;
}
}
else {
/*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: no data avail");*/
/* No data, stop now */
break;
}
iterations++;
}
/*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_timer_cb: finished with iterations: %u, new timer", iterations);*/
g_mutex_unlock (pipe_input_p->callback_running);
/* we didn't stopped the timer, so let it run */
return TRUE;
}
#endif
void
pipe_input_set_handler(gint source, gpointer user_data, ws_process_id *child_process, pipe_input_cb_t input_cb)
{
pipe_input->source = source;
pipe_input->child_process = child_process;
pipe_input->user_data = user_data;
pipe_input->input_cb = input_cb;
#ifdef _WIN32
#if GLIB_CHECK_VERSION(2,31,0)
pipe_input->callback_running = g_malloc(sizeof(GMutex));
g_mutex_init(pipe_input->callback_running);
#else
pipe_input->callback_running = g_mutex_new();
#endif
/* Tricky to use pipes in win9x, as no concept of wait. NT can
do this but that doesn't cover all win32 platforms. GTK can do
this but doesn't seem to work over processes. Attempt to do
something similar here, start a timer and check for data on every
timeout. */
/*g_log(NULL, G_LOG_LEVEL_DEBUG, "pipe_input_set_handler: new");*/
pipe_input->pipe_input_id = g_timeout_add(200, pipe_timer_cb, pipe_input);
#endif
}