-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.c
406 lines (360 loc) · 10.3 KB
/
lib.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
#include <cJSON.h>
#include <lib.h>
#include <offline.h>
// global capture file variable
capture_file cf;
static guint hexdump_source_option =
HEXDUMP_SOURCE_MULTI; /* Default - Enable legacy multi-source mode */
static guint hexdump_ascii_option =
HEXDUMP_ASCII_INCLUDE; /* Default - Enable legacy undelimited ASCII dump */
/**
* Init policies、wtap mod、epan mod.
*
* @param filepath the pcap file path
* @return 0 if init correctly
*/
int init_env() {
/**
* Called when the program starts, to enable security features and save
* whatever credential information we'll need later.
*/
init_process_policies();
/**
* Permanently relinquish special privileges. get_credential_info()
* MUST be called before calling this.
*/
relinquish_special_privs_perm();
timestamp_set_type(TS_RELATIVE);
timestamp_set_precision(TS_PREC_AUTO);
timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
/**
* @brief Initialize the Wiretap library.
*
* @param load_wiretap_plugins Load Wiretap plugins when initializing library.
*/
wtap_init(TRUE);
/**
* Init the whole epan module.
*
* Must be called only once in a program.
*
* Returns TRUE on success, FALSE on failure.
*/
if (!epan_init(NULL, NULL, FALSE)) {
return 0;
}
return 1;
}
/**
* Copy from tshark, handle time.
*/
static const nstime_t *tshark_get_frame_ts(struct packet_provider_data *prov,
guint32 frame_num) {
if (prov->ref && prov->ref->num == frame_num)
return &prov->ref->abs_ts;
if (prov->prev_dis && prov->prev_dis->num == frame_num)
return &prov->prev_dis->abs_ts;
if (prov->prev_cap && prov->prev_cap->num == frame_num)
return &prov->prev_cap->abs_ts;
if (prov->frames) {
frame_data *fd = frame_data_sequence_find(prov->frames, frame_num);
return (fd) ? &fd->abs_ts : NULL;
}
return NULL;
}
/**
* Clean the capture file struct and epan mod.
*/
void clean() {
if (cf.provider.frames != NULL) {
/*
* Free a frame_data_sequence and all the frame_data structures in it.
*/
free_frame_data_sequence(cf.provider.frames);
cf.provider.frames = NULL;
}
if (cf.provider.wth != NULL) {
/** Closes any open file handles and frees the memory associated with wth.
*/
wtap_close(cf.provider.wth);
cf.provider.wth = NULL;
}
if (cf.epan != NULL) {
epan_free(cf.epan);
}
/** cleanup the whole epan module, this is used to be called only once in a
* program */
epan_cleanup();
}
/**
* Clean the capture file struct.
*/
void close_cf() {
cf.stop_flag = FALSE;
if (cf.provider.wth) {
wtap_close(cf.provider.wth);
cf.provider.wth = NULL;
}
/* We have no file open... */
if (cf.filename != NULL) {
g_free(cf.filename);
cf.filename = NULL;
}
/* ...which means we have no changes to that file to save. */
cf.unsaved_changes = FALSE;
/* no open_routine type */
cf.open_type = WTAP_TYPE_AUTO;
/* Clean up the record metadata. */
wtap_rec_cleanup(&cf.rec);
cf.rfcode = NULL;
if (cf.provider.frames != NULL) {
free(cf.provider.frames);
cf.provider.frames = NULL;
}
if (cf.provider.frames_modified_blocks) {
g_tree_destroy(cf.provider.frames_modified_blocks);
cf.provider.frames_modified_blocks = NULL;
}
/* No frames, no frame selected, no field in that frame selected. */
cf.count = 0;
cf.current_frame = NULL;
cf.finfo_selected = NULL;
/* No frame link-layer types, either. */
if (cf.linktypes != NULL) {
g_array_free(cf.linktypes, TRUE);
cf.linktypes = NULL;
}
cf.f_datalen = 0;
nstime_set_zero(&cf.elapsed_time);
reset_tap_listeners();
epan_free(cf.epan);
cf.epan = NULL;
/* We have no file open. */
cf.state = FILE_CLOSED;
}
/**
* Init and fill the capture file struct.
*
* @param filepath the pcap file path
* @return 0 if init correctly
*/
int init_cf(char *filepath) {
int err = 0;
gchar *err_info = NULL;
e_prefs *prefs_p;
/* Initialize the capture file struct */
memset(&cf, 0, sizeof(capture_file));
cf.filename = filepath;
cf.provider.wth =
wtap_open_offline(cf.filename, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
if (err != 0 || cf.provider.wth == NULL) {
clean();
return err;
}
cf.count = 0;
cf.provider.frames = new_frame_data_sequence();
static const struct packet_provider_funcs funcs = {
tshark_get_frame_ts,
NULL,
NULL,
NULL,
};
cf.epan = epan_new(&cf.provider, &funcs);
prefs_p = epan_load_settings();
build_column_format_array(&cf.cinfo, prefs_p->num_cols, TRUE);
return 0;
}
/**
* Read each frame.
*
* @param edt_r the epan_dissect_t struct of each frame
* @return TRUE if can dissect frame correctly, FALSE if can not read frame
*/
gboolean read_packet(epan_dissect_t **edt_r) {
epan_dissect_t *edt;
int err;
gchar *err_info = NULL;
guint32 cum_bytes = 0;
gint64 data_offset = 0;
wtap_rec rec;
wtap_rec_init(&rec);
if (wtap_read(cf.provider.wth, &rec, &cf.buf, &err, &err_info,
&data_offset)) {
cf.count++;
frame_data fd;
frame_data_init(&fd, cf.count, &rec, data_offset, cum_bytes);
// data_offset must be correctly set
data_offset = fd.pkt_len;
edt = epan_dissect_new(cf.epan, TRUE, TRUE);
prime_epan_dissect_with_postdissector_wanted_hfids(edt);
frame_data_set_before_dissect(&fd, &cf.elapsed_time, &cf.provider.ref,
cf.provider.prev_dis);
cf.provider.ref = &fd;
tvbuff_t *tvb;
tvb = tvb_new_real_data(cf.buf.data, data_offset, data_offset);
// core dissect process
epan_dissect_run_with_taps(edt, cf.cd_t, &rec, tvb, &fd, &cf.cinfo);
frame_data_set_after_dissect(&fd, &cum_bytes);
cf.provider.prev_cap = cf.provider.prev_dis =
frame_data_sequence_add(cf.provider.frames, &fd);
// free space
frame_data_destroy(&fd);
*edt_r = edt;
return TRUE;
}
return FALSE;
}
/**
* Dissect and print all frames.
*
* @return none, just print dissect result
*/
void print_all_frame() {
epan_dissect_t *edt;
print_stream_t *print_stream = print_stream_text_stdio_new(stdout);
// start reading packets
while (read_packet(&edt)) {
proto_tree_print(print_dissections_expanded, TRUE, edt, NULL, print_stream);
// print hex data
print_hex_data(print_stream, edt,
hexdump_source_option | hexdump_ascii_option);
epan_dissect_free(edt);
edt = NULL;
}
close_cf();
}
/**
* Dissect and print the first frame.
*
* @return none, just print dissect result
*/
void print_first_frame() {
epan_dissect_t *edt;
print_stream_t *print_stream = print_stream_text_stdio_new(stdout);
// start reading packets
if (read_packet(&edt)) {
proto_tree_print(print_dissections_expanded, TRUE, edt, NULL, print_stream);
// print hex data
print_hex_data(print_stream, edt,
hexdump_source_option | hexdump_ascii_option);
epan_dissect_free(edt);
edt = NULL;
}
close_cf();
}
/**
* Dissect and print the first several frames.
*
* @param count the first several frames to dissect and print, count is the num
* @return none, just print dissect result
*/
void print_first_several_frame(int count) {
epan_dissect_t *edt;
print_stream_t *print_stream = print_stream_text_stdio_new(stdout);
// start reading packets
while (read_packet(&edt)) {
// print proto tree
proto_tree_print(print_dissections_expanded, FALSE, edt, NULL,
print_stream);
epan_dissect_free(edt);
edt = NULL;
if (cf.count == count) {
break;
}
}
close_cf();
}
/**
* Dissect and print specific frame.
*
* @param num the index of frame which you want to dissect
* @return int out of bounds:2; correct:0;
*/
int print_specific_frame(int num) {
epan_dissect_t *edt;
print_stream_t *print_stream;
print_stream = print_stream_text_stdio_new(stdout);
// start reading packets
while (read_packet(&edt)) {
if (num == cf.count) {
// print proto tree
proto_tree_print(print_dissections_expanded, TRUE, edt, NULL,
print_stream);
// print hex data
print_hex_data(print_stream, edt,
hexdump_source_option | hexdump_ascii_option);
close_cf();
return 0;
}
epan_dissect_free(edt);
edt = NULL;
}
close_cf();
return 2;
}
/**
* Dissect and get hex data of specific frame.
*
* @param num the index of frame which you want to dissect
* @return char of hex data dissect result, include hex data
*/
char *get_specific_frame_hex_data(int num) {
epan_dissect_t *edt;
// start reading packets
while (read_packet(&edt)) {
if (num != cf.count) {
epan_dissect_free(edt);
edt = NULL;
continue;
}
cJSON *cjson_hex_root = cJSON_CreateObject();
cJSON *cjson_offset = cJSON_CreateArray();
cJSON *cjson_hex = cJSON_CreateArray();
cJSON *cjson_ascii = cJSON_CreateArray();
get_hex_data(edt, cjson_offset, cjson_hex, cjson_ascii);
cJSON_AddItemToObject(cjson_hex_root, "offset", cjson_offset);
cJSON_AddItemToObject(cjson_hex_root, "hex", cjson_hex);
cJSON_AddItemToObject(cjson_hex_root, "ascii", cjson_ascii);
epan_dissect_free(edt);
edt = NULL;
return cJSON_PrintUnformatted(cjson_hex_root);
}
close_cf();
return "";
}
/**
* Transfer proto tree to json format.
*
* @param num the index of frame which you want to dissect
* @return char of protocol tree dissect result, include hex data
*/
char *proto_tree_in_json(int num, int descriptive, int debug) {
epan_dissect_t *edt;
// start reading packets
while (read_packet(&edt)) {
if (num != cf.count) {
epan_dissect_free(edt);
edt = NULL;
continue;
}
// json root node
cJSON *proto_tree_json = cJSON_CreateObject();
get_proto_tree_json(NULL, print_dissections_expanded, TRUE, NULL,
PF_INCLUDE_CHILDREN, edt, &cf.cinfo,
proto_node_group_children_by_unique, proto_tree_json,
descriptive);
char *proto_tree_json_str;
if (debug) {
proto_tree_json_str = cJSON_Print(proto_tree_json);
printf("%s\n", proto_tree_json_str);
} else {
proto_tree_json_str = cJSON_PrintUnformatted(proto_tree_json);
}
cJSON_Delete(proto_tree_json);
epan_dissect_free(edt);
edt = NULL;
return proto_tree_json_str;
}
close_cf();
return "";
}