-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpcap-list.c
342 lines (278 loc) · 6.18 KB
/
pcap-list.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
/* File: pcap-list.c
Copyright (C) 2013 David Hauweele <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <sys/time.h>
#include <err.h>
#include <assert.h>
#include "string-utils.h"
#include "mac-decode.h"
#include "pcap-read.h"
#include "pcap-list.h"
#include "dump.h"
static struct pcap_node *head;
static struct pcap_node *tail;
static struct pcap_node *cursor;
static size_t size;
static unsigned int index;
static bool dirty;
static void (*_ui_warn)(const char *message);
static struct pcap_node * load_frame(void)
{
struct pcap_node *node;
struct timeval tv;
size_t size;
int res;
unsigned char *frame = pcap_read_frame(&size, &tv);
if(!frame)
return NULL;
node = malloc(sizeof(struct pcap_node));
node->prev = tail;
node->next = NULL;
node->size = size;
node->time = tv;
node->data = frame;
node->valid_frame = true;
if(tail)
tail->next = node;
tail = node;
if(!head) {
head = node;
cursor = node;
}
res = mac_decode(&node->frame, frame, true, size);
if(res < 0) {
node->valid_frame = false;
_ui_warn("cannot decode a frame");
}
return node;
}
void pcap_list_init(void (*ui_warn)(const char *))
{
_ui_warn = ui_warn;
dirty = false;
}
void pcap_list_load_from_file(const char *filename)
{
size_t count = 0;
dirty = false;
/* Flush the list if needed. */
if(head)
pcap_list_flush();
open_reading_pcap(filename);
while(1) {
if(!load_frame())
goto EOF;
count++;
}
EOF:
size = count;
close_reading_pcap();
}
/* Free a pcap node's internal structure. */
static void free_pcap_node(struct pcap_node *node)
{
if(node->valid_frame)
free_mac_frame(&node->frame);
free(node->data);
}
void pcap_list_flush(void)
{
struct pcap_node *o = head;
while(o) {
struct pcap_node *t = o;
o = o->next;
free_pcap_node(t);
free(t);
}
head = NULL;
tail = NULL;
cursor = NULL;
size = 0;
index = 0;
dirty = false;
}
size_t pcap_list_size(void)
{
return size;
}
bool pcap_list_dirty(void)
{
return dirty;
}
unsigned int pcap_list_cursor_position(void)
{
return index;
}
static void seek_forward(int offset)
{
while(offset-- && cursor->next) {
cursor = cursor->next;
index++;
}
}
static void seek_backward(int offset)
{
while(offset-- && cursor->prev) {
cursor = cursor->prev;
index--;
}
}
void pcap_list_cursor_seek(int offset, enum cursor_whence whence)
{
if(!cursor)
return;
switch(whence) {
case(CURSOR_SEEK_SET):
cursor = head;
index = 0;
break;
case(CURSOR_SEEK_END):
cursor = tail;
index = size;
offset = -offset;
case(CURSOR_SEEK_CUR):
break;
default:
assert(0);
}
if(offset == 0)
return;
else if(offset > 0)
seek_forward(offset);
else /* offset < 0 */
seek_backward(-offset);
}
void pcap_list_cursor_prev(void)
{
if(!cursor)
return;
if(cursor->prev) {
cursor = cursor->prev;
index--;
}
}
void pcap_list_cursor_next(void)
{
if(!cursor)
return;
if(cursor->next) {
cursor = cursor->next;
index++;
}
}
const struct pcap_node * pcap_list_get_at_cursor(void)
{
return cursor;
}
void pcap_list_delete_at_cursor(void)
{
struct pcap_node *new_cursor;
if(!cursor)
return;
dirty = true;
size--;
if(cursor->next) {
cursor->next->prev = cursor->prev;
new_cursor = cursor->next;
}
else {
index--;
new_cursor = cursor->prev;
}
if(cursor->prev)
cursor->prev->next = cursor->next;
free_pcap_node(cursor);
free(cursor);
cursor = new_cursor;
/* set head/tail accordingly */
if(!cursor->next)
tail = cursor;
if(!cursor->prev)
head = cursor;
}
static void setup_time(struct pcap_node *node)
{
if(node->next)
node->time = node->next->time;
else if(node->prev)
node->time = node->prev->time;
else {
node->time.tv_sec = 0;
node->time.tv_usec = 0;
}
}
void pcap_list_insert_at_cursor(const unsigned char *frame,
size_t size,
const struct timeval *tv,
bool contains_crc)
{
int res;
struct pcap_node *node = malloc(sizeof(struct pcap_node));
size++;
dirty = true;
node->data = memdup(frame, size);
node->prev = cursor;
if(cursor)
node->next = cursor->next;
if(node->next)
node->next->prev = node;
else
tail = node;
if(node->prev)
node->prev->next = node;
else
head = node;
cursor = node;
index++;
/* create the frame */
res = mac_decode(&node->frame, frame, contains_crc, size);
if(res < 0)
node->valid_frame = false;
/* setup the time */
if(!tv)
setup_time(node);
else
node->time = *tv;
}
void pcap_list_replace_at_cursor(const unsigned char *frame,
size_t size,
const struct timeval *tv,
bool contains_crc)
{
int res;
/* we don't want to replace an element in an empty list */
assert(cursor);
dirty = true;
free_pcap_node(cursor);
cursor->data = memdup(frame, size);
cursor->size = size;
res = mac_decode(&cursor->frame, frame, contains_crc, size);
if(res < 0) {
cursor->valid_frame = false;
_ui_warn("Cannot decode the frame");
}
/* setup the time */
if(!tv)
setup_time(cursor);
else
cursor->time = *tv;
}
void pcap_list_for_each(bool (*action)(const struct pcap_node *node,
void *data),
void *data)
{
struct pcap_node *o;
for(o = head ; o ; o = o->next)
if(!action(o, data))
return;
}