-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathpcap-openvizsla.c
208 lines (165 loc) · 4.4 KB
/
pcap-openvizsla.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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "pcap-int.h"
#include "pcap-openvizsla.h"
#include <openvizsla.h>
#define OPENVIZSLA_IFACE "openvizsla"
struct pcap_openvizsla {
enum ov_usb_speed usb_speed;
struct ov_device* ov;
};
struct pcap_openvizsla_read {
pcap_handler callback;
u_char *user;
};
static void openvizsla_read_cb(struct ov_packet* packet, void* data) {
struct pcap_openvizsla_read* r = (struct pcap_openvizsla_read*)data;
struct pcap_pkthdr pkth;
pkth.caplen = ov_to_host_16(packet->size) + sizeof(struct ov_packet);
pkth.len = pkth.caplen;
pkth.ts.tv_sec = 0;
pkth.ts.tv_usec = 0;
r->callback(r->user, &pkth, (const u_char*)packet);
}
static int openvizsla_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
{
struct pcap_openvizsla* handlep = handle->priv;
struct pcap_openvizsla_read r;
r.callback = callback;
r.user = user;
int ret = 0;
if ((ret = ov_capture_start(handlep->ov, handle->buffer, handle->bufsize, openvizsla_read_cb, &r)) < 0) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Can't start capture: %s", ov_get_error_string(handlep->ov));
return PCAP_ERROR;
}
if ((ret = ov_capture_dispatch(handlep->ov, max_packets)) < 0) {
if (handle->break_loop) {
handle->break_loop = 0;
ov_capture_stop(handlep->ov);
return PCAP_ERROR_BREAK;
}
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"An error occured during capturing: %s", ov_get_error_string(handlep->ov));
return PCAP_ERROR;
}
ov_capture_stop(handlep->ov);
return ret;
}
static void openvizsla_cleanup(pcap_t* handle)
{
struct pcap_openvizsla* handlep = handle->priv;
ov_free(handlep->ov);
pcap_cleanup_live_common(handle);
}
static void openvizsla_breakloop(pcap_t* handle)
{
struct pcap_openvizsla* handlep = handle->priv;
ov_capture_breakloop(handlep->ov);
pcap_breakloop_common(handle);
}
static int openvizsla_activate(pcap_t* handle)
{
struct pcap_openvizsla* handlep = handle->priv;
int ret = 0;
if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
handle->snapshot = MAXIMUM_SNAPLEN;
handle->bufsize = handle->snapshot;
handle->offset = 0;
handle->linktype = DLT_OPENVIZSLA;
handle->buffer = malloc(handle->bufsize);
if (!handle->buffer) {
pcap_fmt_errmsg_for_errno(handle->errbuf, PCAP_ERRBUF_SIZE, errno, "malloc");
goto fail_buffer_malloc;
}
handle->cleanup_op = openvizsla_cleanup;
handle->read_op = openvizsla_read;
handle->setfilter_op = install_bpf_program; /* no kernel filtering */
handle->breakloop_op = openvizsla_breakloop;
handlep->ov = ov_new(NULL);
if (!handlep->ov) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Can't create OpenVizsla device handler");
goto fail_ov_init;
}
ret = ov_open(handlep->ov);
if (ret < 0) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Can't open device: %s", ov_get_error_string(handlep->ov));
goto fail_ov_open;
}
ret = ov_set_usb_speed(handlep->ov, handlep->usb_speed);
if (ret < 0) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Can't set USB speed: %s", ov_get_error_string(handlep->ov));
goto fail_ov_set_usb_speed;
}
return 0;
fail_ov_set_usb_speed:
fail_ov_open:
ov_free(handlep->ov);
fail_ov_init:
free(handle->buffer);
handle->buffer = NULL;
fail_buffer_malloc:
return PCAP_ERROR;
}
int openvizsla_findalldevs(pcap_if_list_t *devlistp, char *err_str)
{
/* Always find one device */
return 0;
}
pcap_t *openvizsla_create(const char *device, char *ebuf, int *is_ours)
{
const char *cp;
char *cpend;
long devnum;
pcap_t *p;
struct pcap_openvizsla* handlep;
enum ov_usb_speed usb_speed;
cp = strrchr(device, '/');
if (cp == NULL)
cp = device;
if (strncmp(cp, OPENVIZSLA_IFACE, sizeof (OPENVIZSLA_IFACE - 1)) != 0) {
*is_ours = 0;
return NULL;
}
cp += sizeof OPENVIZSLA_IFACE - 1;
devnum = strtol(cp, &cpend, 10);
if (cpend == cp) {
*is_ours = 0;
return NULL;
}
if (devnum < 0) {
*is_ours = 0;
return NULL;
}
switch (*cpend) {
case '\0': /* Fallback to 'l' */
case 'l': {
usb_speed = OV_LOW_SPEED;
} break;
case 'f': {
usb_speed = OV_FULL_SPEED;
} break;
case 'h': {
usb_speed = OV_HIGH_SPEED;
} break;
default: {
*is_ours = 0;
return NULL;
}
}
*is_ours = 1;
p = PCAP_CREATE_COMMON(ebuf, struct pcap_openvizsla);
if (p == NULL)
return NULL;
p->activate_op = openvizsla_activate;
handlep = p->priv;
handlep->usb_speed = usb_speed;
return p;
}