-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcapture.c
137 lines (108 loc) · 3.13 KB
/
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
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
#include <WinSock2.h>
#endif
#include "ws_capture.h"
#include <stddef.h>
#include <assert.h>
#include <epan/epan.h>
#include <epan/print.h>
#include <epan/timestamp.h>
#include <epan/epan-int.h>
#include <epan/epan_dissect.h>
#include <epan/disabled_protos.h>
#include <epan/proto.h>
#include <epan/ftypes/ftypes.h>
#include <wsutil/filesystem.h>
#include <epan/asm_utils.h>
#include "caputils/capture_ifinfo.h"
#include <wsutil/privileges.h>
#include <wsutil/plugins.h>
#include <wiretap/wtap.h>
#include "ws_capture-internal.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static WSADATA wsaData;
#endif /* _WIN32 */
int ws_capture_init(void) {
init_process_policies();
wtap_init();
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
/*wtap_register_plugin_types(); [> Types known to libwiretap <]*/
#ifdef _WIN32
/* Start windows sockets */
WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
#endif /* _WIN32 */
#ifdef WITH_ONLINE_CAPTURE
char *progfile = init_progfile_dir(NULL, NULL);
(void)progfile;
#endif
return 0;
}
ws_capture_t *ws_capture_open_offline(const char *path, int flags, int *err, char **err_info) {
assert(flags == 0);
int _err = 0;
char *_err_info = NULL;
Buffer buf;
capture_file cfile;
cap_file_init(&cfile);
cfile.filename = g_strdup(path);
/*if ((flags & WS_CAPTURE_SEQUENTIAL) == WS_CAPTURE_SEQUENTIAL) {*/
ws_buffer_init(&buf, 1500);
gboolean do_random = (strcmp(path, "-") == 0) ? FALSE : TRUE;
cfile.wth = wtap_open_offline(cfile.filename, WTAP_TYPE_AUTO, &_err, &_err_info, do_random);
if (cfile.wth == NULL) {
PROVIDE_ERRORS;
return NULL;
}
cfile.count = 0;
timestamp_set_precision(TS_PREC_AUTO);
cfile.frames = new_frame_data_sequence();
ws_capture_t *cap = g_malloc0(sizeof *cap);
cap->cfile = cfile;
cap->buf = buf;
return cap;
}
void ws_capture_close(ws_capture_t *cap) {
if (!cap) return;
if (cap->cfile.frames) {
// FIXME: crashes in some instances, for now it only leaks memory
/*free_frame_data_sequence(cap->cfile.frames);*/
cap->cfile.frames = NULL;
}
cap->cfile.frames = NULL;
if (cap->cfile.wth)
wtap_close(cap->cfile.wth);
if (cap->is_live)
ws_capture_live_close(cap);
/*if (cf->is_tempfile) ws_unlink(cf->filename);*/
g_free(cap->cfile.filename);
wtap_phdr_cleanup(&cap->cfile.phdr);
ws_buffer_free(&cap->cfile.buf);
cap->cfile.wth = NULL;
dfilter_free(cap->cfile.rfcode);
ws_buffer_free(&cap->buf);
g_free(cap);
}
const char *ws_capture_filename(ws_capture_t *cap)
{
return cap->cfile.filename;
}
uint64_t ws_capture_file_size(ws_capture_t *cap) {
if(!cap) return UINT64_MAX;
int err = 0;
uint64_t size = wtap_file_size(cap->cfile.wth, &err);
if(err != 0) {
return UINT64_MAX;
}
return size;
}
uint64_t ws_capture_read_so_far(ws_capture_t *cap) {
return wtap_read_so_far(cap->cfile.wth);
}
void ws_capture_finalize(void) {
#ifdef _WIN32
WSACleanup();
#endif
}