-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.c
453 lines (355 loc) · 8.38 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
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
#include <lib.h>
static e_prefs *get_prefs()
{
e_prefs *prefs_p;
char *gpf_path, *pf_path;
int gpf_read_errno, gpf_open_errno;
int pf_open_errno, pf_read_errno;
prefs_p = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
&pf_open_errno, &pf_read_errno, &pf_path);
return prefs_p;
}
int init_cfile(char *filename)
{
int err;
gchar *err_info = NULL;
e_prefs *prefs_p;
cap_file_init(&cfile);
cfile.filename = filename;
cfile.wth = wtap_open_offline(cfile.filename, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
if (cfile.wth == NULL) {
return err;
}
cfile.count = 0;
cfile.epan = epan_new();
cfile.epan->data = &cfile;
cfile.epan->get_frame_ts = tshark_get_frame_ts;
timestamp_set(cfile);
cfile.frames = new_frame_data_sequence();
prefs_p = get_prefs();
build_column_format_array(&cfile.cinfo, prefs_p->num_cols, TRUE);
return 0;
}
int init_pdh(char *savefile)
{
int err;
if (savefile != NULL){
if (!open_output_file(savefile, &err)) {
return err;
}
}
return 0;
}
int init(char *filename, char *savefile)
{
int err = 0;
init_process_policies();
epan_init(register_all_protocols, register_all_protocol_handoffs, NULL, NULL);
err = init_cfile(filename);
if (err != 0)
goto fail;
err = init_pdh(savefile);
if (err != 0)
goto fail;
return 0;
fail:
clean();
return 1;
}
static gboolean read_packet(epan_dissect_t **edt_r)
{
epan_dissect_t *edt;
int err;
gchar *err_info = NULL;
static guint32 cum_bytes = 0;
static gint64 data_offset = 0;
struct wtap_pkthdr *whdr = wtap_phdr(cfile.wth);
guchar *buf = wtap_buf_ptr(cfile.wth);
if (wtap_read(cfile.wth, &err, &err_info, &data_offset)) {
cfile.count++;
frame_data fdlocal;
frame_data_init(&fdlocal, cfile.count, whdr, data_offset, cum_bytes);
edt = epan_dissect_new(cfile.epan, TRUE, TRUE);
frame_data_set_before_dissect(&fdlocal, &cfile.elapsed_time,
&cfile.ref, cfile.prev_dis);
cfile.ref = &fdlocal;
epan_dissect_run(edt, cfile.cd_t, &(cfile.phdr),
frame_tvbuff_new(&fdlocal, buf), &fdlocal, &cfile.cinfo);
frame_data_set_after_dissect(&fdlocal, &cum_bytes);
cfile.prev_cap = cfile.prev_dis = frame_data_sequence_add(cfile.frames, &fdlocal);
//free space
frame_data_destroy(&fdlocal);
*edt_r = edt;
return TRUE;
}
return FALSE;
}
void clean()
{
int err = 0;
if (cfile.frames != NULL) {
free_frame_data_sequence(cfile.frames);
cfile.frames = NULL;
}
if (cfile.wth != NULL) {
wtap_close(cfile.wth);
cfile.wth = NULL;
}
if (pdh != NULL) {
wtap_dump_close(pdh, &err);
}
if (cfile.epan != NULL)
epan_free(cfile.epan);
epan_cleanup();
}
void clean_cfile()
{
if (cfile.frames != NULL) {
free_frame_data_sequence(cfile.frames);
cfile.frames = NULL;
}
if (cfile.wth != NULL) {
wtap_close(cfile.wth);
cfile.wth = NULL;
}
if (cfile.epan != NULL) {
epan_free(cfile.epan);
cfile.epan = NULL;
}
}
void clean_pdh()
{
int err = 0;
if (pdh != NULL) {
wtap_dump_close(pdh, &err);
}
pdh = NULL;
}
static void
timestamp_set(capture_file cfile)
{
switch(wtap_file_tsprecision(cfile.wth)) {
case(WTAP_FILE_TSPREC_SEC):
timestamp_set_precision(TS_PREC_AUTO_SEC);
break;
case(WTAP_FILE_TSPREC_DSEC):
timestamp_set_precision(TS_PREC_AUTO_DSEC);
break;
case(WTAP_FILE_TSPREC_CSEC):
timestamp_set_precision(TS_PREC_AUTO_CSEC);
break;
case(WTAP_FILE_TSPREC_MSEC):
timestamp_set_precision(TS_PREC_AUTO_MSEC);
break;
case(WTAP_FILE_TSPREC_USEC):
timestamp_set_precision(TS_PREC_AUTO_USEC);
break;
case(WTAP_FILE_TSPREC_NSEC):
timestamp_set_precision(TS_PREC_AUTO_NSEC);
break;
default:
g_assert_not_reached();
}
}
static const nstime_t *
tshark_get_frame_ts(void *data, guint32 frame_num)
{
capture_file *cf = (capture_file *) data;
if (cf->ref && cf->ref->num == frame_num)
return &(cf->ref->abs_ts);
if (cf->prev_dis && cf->prev_dis->num == frame_num)
return &(cf->prev_dis->abs_ts);
if (cf->prev_cap && cf->prev_cap->num == frame_num)
return &(cf->prev_cap->abs_ts);
if (cf->frames) {
frame_data *fd = frame_data_sequence_find(cf->frames, frame_num);
return (fd) ? &fd->abs_ts : NULL;
}
return NULL;
}
void
cap_file_init(capture_file *cf)
{
/* Initialize the capture file struct */
memset(cf, 0, sizeof(capture_file));
cf->snap = WTAP_MAX_PACKET_SIZE;
}
struct epan_dissect *next_packet()
{
epan_dissect_t *edt;
if (read_packet(&edt)) {
return edt;
};
return NULL;
}
void
free_packet(struct epan_dissect *edt)
{
epan_dissect_free(edt);
edt = NULL;
}
void
free_string(char *s)
{
free(s);
}
proto_node *
get_field(struct epan_dissect *edt, const char *name)
{
proto_node *node;
if (proto_tree_pre_order(edt->tree, name, &node)) {
return node;
}
return NULL;
}
static gboolean
proto_tree_pre_order(proto_tree *tree, const char *name, proto_node **node)
{
proto_node *pnode = tree;
proto_node *child;
proto_node *current;
field_info *fi = PNODE_FINFO(pnode);
if (fi && fi->hfinfo) {
if (!strcmp(fi->hfinfo->abbrev, name)) {
*node = pnode;
return TRUE;
}
}
child = pnode->first_child;
while (child != NULL) {
current = child;
child = current->next;
if (proto_tree_pre_order((proto_tree *)current, name, node))
return TRUE;
}
return FALSE;
}
char *print_packet(proto_tree *node)
{
char *buf = calloc(sizeof(char), BUFSIZE);
int level = 0;
proto_node_print(node, &level, &buf);
return buf;
}
char *print_node(proto_node *node)
{
char *buf = calloc(sizeof(char), BUFSIZE);
int level = 0;
print_field(node, &level, &buf);
level++;
proto_node_print(node, &level, &buf);
return buf;
}
static void
proto_node_print(proto_tree *tree, int *level, char **buf)
{
proto_node *node = tree;
proto_node *current;
if (!node)
return;
node = node->first_child;
while (node != NULL) {
current = node;
node = current->next;
print_field(current, level, buf);
(*level)++;
proto_node_print(current, level, buf);
(*level)--;
}
}
static void
print_field(proto_node *node, int *level, char **buf)
{
if (node->finfo == NULL)
return;
//reset level when node is proto
if (node->finfo->hfinfo->type == FT_PROTOCOL)
*level = 0;
for (int i = 0; i < *level; i++) {
snprintf(*buf + strlen(*buf), BUFSIZE, "%s", ". ");
}
const char *name = node->finfo->hfinfo->abbrev;
fvalue_t fv = node->finfo->value;
char *value = fvalue_to_string_repr(&fv, FTREPR_DISPLAY, NULL);
if (value == NULL) {
snprintf(*buf + strlen(*buf), BUFSIZE, "[%s]\n", name);
} else {
snprintf(*buf + strlen(*buf), BUFSIZE, "[%s] %s\n", name, value);
}
}
int write_to_file()
{
int err;
if (pdh == NULL) {
return 1;
}
if (!wtap_dump(pdh, wtap_phdr(cfile.wth), wtap_buf_ptr(cfile.wth), &err)) {
return err;
}
return 0;
}
static gboolean
open_output_file(char *savefile, int *err)
{
wtapng_section_t *shb_hdr;
wtapng_iface_descriptions_t *idb_inf;
shb_hdr = wtap_file_get_shb_info(cfile.wth);
idb_inf = wtap_file_get_idb_info(cfile.wth);
guint snapshot_length = wtap_snapshot_length(cfile.wth);
if (snapshot_length == 0) {
snapshot_length = WTAP_MAX_PACKET_SIZE;
}
gint linktype = wtap_file_encap(cfile.wth);
guint out_file_type = WTAP_FILE_TYPE_SUBTYPE_PCAP;
pdh = wtap_dump_open_ng(savefile, out_file_type, linktype,
snapshot_length, FALSE, shb_hdr, idb_inf, err);
g_free(idb_inf);
idb_inf = NULL;
g_free(shb_hdr);
shb_hdr = NULL;
if (pdh == NULL) {
return FALSE;
}
return TRUE;
}
static void
proto_tree_foreach(proto_tree *tree, const char *name, GPtrArray **finfo_array)
{
proto_node *node = tree;
proto_node *current;
field_info *fi;
if (!node)
return;
node = node->first_child;
while (node != NULL) {
current = node;
node = current->next;
fi = PNODE_FINFO(current);
if (fi && fi->hfinfo) {
if (!strcmp(fi->hfinfo->abbrev, name)) {
g_ptr_array_add(*finfo_array, fi);
}
}
proto_tree_foreach(current, name, finfo_array);
}
}
struct _GPtrArray *
get_field_values(struct epan_dissect *edt, const char *name)
{
GPtrArray *finfo_array;
finfo_array = g_ptr_array_new();
proto_tree_foreach(edt->tree, name, &finfo_array);
return finfo_array;
}
char *
finfo_to_value(void *finfo)
{
fvalue_t fv = ((field_info *)finfo)->value;
char *value = fvalue_to_string_repr(&fv, FTREPR_DISPLAY, NULL);
return value;
}
void *
g_ptr_array_data(struct _GPtrArray *array, int index)
{
return ((array)->pdata)[index];
}