]> The Tcpdump Group git mirrors - libpcap/blob - pcap-canusb-linux.c
Constify some variables.
[libpcap] / pcap-canusb-linux.c
1 /*
2 * Copyright (c) 2009 Felix Obenhuber
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Sockettrace sniffing API implementation for Linux platform
31 * By Felix Obenhuber <felix@obenhuber.de>
32 *
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include <libusb-1.0/libusb.h>
40
41 #include "pcap-int.h"
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <string.h>
46
47
48 #define CANUSB_IFACE "canusb"
49
50 #define CANUSB_VID 0x0403
51 #define CANUSB_PID 0x8990
52
53 #define USE_THREAD 1
54
55 #if USE_THREAD == 0
56 #include <signal.h>
57 #endif
58
59
60 /* forward declaration */
61 static int canusb_activate(pcap_t *);
62 static int canusb_read_linux(pcap_t *, int , pcap_handler , u_char *);
63 static int canusb_inject_linux(pcap_t *, const void *, size_t);
64 static int canusb_setfilter_linux(pcap_t *, struct bpf_program *);
65 static int canusb_setdirection_linux(pcap_t *, pcap_direction_t);
66 static int canusb_stats_linux(pcap_t *, struct pcap_stat *);
67
68 struct CAN_Msg
69 {
70 uint32_t timestamp;
71 uint32_t id;
72 uint32_t length;
73 uint8_t data[8];
74 };
75
76 struct canusb_t
77 {
78 libusb_context *ctx;
79 libusb_device_handle *dev;
80 pthread_t worker;
81 int rdpipe, wrpipe;
82 volatile int* loop;
83 };
84
85 static struct canusb_t canusb;
86 static volatile int loop;
87
88 int canusb_findalldevs(pcap_if_t **alldevsp, char *err_str)
89 {
90 libusb_context *fdctx;
91 libusb_device** devs;
92 unsigned char sernum[65];
93 unsigned char buf[96];
94 int cnt, i;
95
96 libusb_init(&fdctx);
97
98 cnt = libusb_get_device_list(fdctx,&devs);
99
100 for(i=0;i<cnt;i++)
101 {
102 int ret;
103 // Check if this device is interesting.
104 struct libusb_device_descriptor desc;
105 libusb_get_device_descriptor(devs[i],&desc);
106
107 if ((desc.idVendor != CANUSB_VID) || (desc.idProduct != CANUSB_PID))
108 continue; //It is not, check next device
109
110 //It is!
111 libusb_device_handle *dh = NULL;
112
113 if (ret = libusb_open(devs[i],&dh) == 0)
114 {
115 char dev_name[30];
116 char dev_descr[50];
117 int n = libusb_get_string_descriptor_ascii(dh,desc.iSerialNumber,sernum,64);
118 sernum[n] = 0;
119
120 snprintf(dev_name, 30, CANUSB_IFACE"%s", sernum);
121 snprintf(dev_descr, 50, "CanUSB [%s]", sernum);
122
123 libusb_close(dh);
124
125 if (pcap_add_if(alldevsp, dev_name, 0, dev_descr, err_str) < 0)
126 {
127 libusb_free_device_list(devs,1);
128 return -1;
129 }
130 }
131 }
132
133 libusb_free_device_list(devs,1);
134 libusb_exit(fdctx);
135 return 0;
136 }
137
138 static libusb_device_handle* canusb_opendevice(struct libusb_context *ctx, char* devserial)
139 {
140 libusb_device_handle* dh;
141 libusb_device** devs;
142 unsigned char serial[65];
143 int cnt,i,n;
144
145 cnt = libusb_get_device_list(ctx,&devs);
146
147 for(i=0;i<cnt;i++)
148 {
149 // Check if this device is interesting.
150 struct libusb_device_descriptor desc;
151 libusb_get_device_descriptor(devs[i],&desc);
152
153 if ((desc.idVendor != CANUSB_VID) || (desc.idProduct != CANUSB_PID))
154 continue;
155
156 //Found one!
157 libusb_device_handle *dh = NULL;
158
159 if (libusb_open(devs[i],&dh) != 0) continue;
160
161 n = libusb_get_string_descriptor_ascii(dh,desc.iSerialNumber,serial,64);
162 serial[n] = 0;
163
164 if ((devserial) && (strcmp(serial,devserial) != 0))
165 {
166 libusb_close(dh);
167 continue;
168 }
169
170 if ((libusb_kernel_driver_active(dh,0)) && (libusb_detach_kernel_driver(dh,0) != 0))
171 {
172 libusb_close(dh);
173 continue;
174 }
175
176 if (libusb_set_configuration(dh,1) != 0)
177 {
178 libusb_close(dh);
179 continue;
180 }
181
182 if (libusb_claim_interface(dh,0) != 0)
183 {
184 libusb_close(dh);
185 continue;
186 }
187
188 //Fount it!
189 libusb_free_device_list(devs,1);
190 return dh;
191 }
192
193 libusb_free_device_list(devs,1);
194 return NULL;
195 }
196
197
198 pcap_t *
199 canusb_create(const char *device, char *ebuf, int *is_ours)
200 {
201 const char *cp, *cpend;
202 long devnum;
203 pcap_t* p;
204
205 libusb_init(&canusb.ctx);
206
207 /* Does this look like a DAG device? */
208 cp = strrchr(device, '/');
209 if (cp == NULL)
210 cp = device;
211 /* Does it begin with "canusb"? */
212 if (strncmp(cp, "canusb", 6) != 0) {
213 /* Nope, doesn't begin with "canusb" */
214 *is_ours = 0;
215 return NULL;
216 }
217 /* Yes - is "canusb" followed by a number? */
218 cp += 6;
219 devnum = strtol(cp, &cpend, 10);
220 if (cpend == cp || *cpend != '\0') {
221 /* Not followed by a number. */
222 *is_ours = 0;
223 return NULL;
224 }
225 if (devnum < 0) {
226 /* Followed by a non-valid number. */
227 *is_ours = 0;
228 return NULL;
229 }
230
231 /* OK, it's probably ours. */
232 *is_ours = 1;
233
234 p = pcap_create_common(device, ebuf);
235 if (p == NULL)
236 return (NULL);
237
238 memset(&canusb, 0x00, sizeof(canusb));
239
240 p->activate_op = canusb_activate;
241
242 return (p);
243 }
244
245
246 static void* canusb_capture_thread(struct canusb_t *canusb)
247 {
248 struct libusb_context *ctx;
249 libusb_device_handle *dev;
250
251 int i, n;
252 struct
253 {
254 uint8_t rxsz, txsz;
255 } status;
256
257 libusb_init(&ctx);
258
259 char *serial = canusb->src + strlen(CANUSB_IFACE);
260 dev = canusb_opendevice(ctx, serial);
261
262 fcntl(canusb->wrpipe, F_SETFL, O_NONBLOCK);
263
264 while(*canusb->loop)
265 {
266 int sz, ret;
267 struct CAN_Msg msg;
268
269 libusb_interrupt_transfer(dev, 0x81, (unsigned char*)&status, sizeof(status), &sz, 100);
270 //HACK!!!!! -> drop buffered data, read new one by reading twice.
271 ret = libusb_interrupt_transfer(dev, 0x81, (unsigned char*)&status, sizeof(status), &sz, 100);
272
273 for(i = 0; i<status.rxsz; i++)
274 {
275 libusb_bulk_transfer(dev, 0x85, (unsigned char*)&msg, sizeof(msg), &sz, 100);
276 n = write(canusb->wrpipe, &msg, sizeof(msg));
277 }
278
279 }
280
281 libusb_close(dev);
282 libusb_exit(ctx);
283
284 return NULL;
285 }
286
287 static int canusb_startcapture(struct canusb_t* this)
288 {
289 int pipefd[2];
290
291 if (pipe(pipefd) == -1) return -1;
292
293 canusb.rdpipe = pipefd[0];
294 canusb.wrpipe = pipefd[1];
295 canusb.loop = &loop;
296
297 loop = 1;
298 pthread_create(&this->worker, NULL, canusb_capture_thread, &canusb);
299
300 return canusb.rdpipe;
301 }
302
303 static void canusb_clearbufs(struct canusb_t* this)
304 {
305 unsigned char cmd[16];
306 int al;
307
308 cmd[0] = 1; //Empty incoming buffer
309 cmd[1] = 1; //Empty outgoing buffer
310 cmd[3] = 0; //Not a write to serial number
311 memset(&cmd[4],0,16-4);
312
313 libusb_interrupt_transfer(this->dev, 0x1,cmd,16,&al,100);
314 }
315
316
317 static void canusb_close(pcap_t* handle)
318 {
319 loop = 0;
320 pthread_join(canusb.worker, NULL);
321
322 if (canusb.dev)
323 {
324 libusb_close(canusb.dev);
325 canusb.dev = NULL;
326 }
327 }
328
329
330
331 static int canusb_activate(pcap_t* handle)
332 {
333 handle->read_op = canusb_read_linux;
334
335 handle->inject_op = canusb_inject_linux;
336 handle->setfilter_op = canusb_setfilter_linux;
337 handle->setdirection_op = canusb_setdirection_linux;
338 handle->getnonblock_op = pcap_getnonblock_fd;
339 handle->setnonblock_op = pcap_setnonblock_fd;
340 handle->stats_op = canusb_stats_linux;
341 handle->cleanup_op = canusb_close;
342
343 /* Initialize some components of the pcap structure. */
344 handle->bufsize = 32;
345 handle->offset = 8;
346 handle->linktype = DLT_CAN_SOCKETCAN;
347 handle->set_datalink_op = NULL;
348
349 char* serial = handle->opt.source + strlen("canusb");
350
351 canusb.dev = canusb_opendevice(canusb.ctx,serial);
352 if (!canusb.dev)
353 {
354 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't open USB Device:");
355 return PCAP_ERROR;
356 }
357
358 canusb_clearbufs(&canusb);
359
360 handle->fd = canusb_startcapture(&canusb);
361 handle->selectable_fd = handle->fd;
362
363 return 0;
364 }
365
366
367
368
369 static int
370 canusb_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
371 {
372 static struct timeval firstpacket = { -1, -1};
373
374 int msgsent = 0;
375 int i = 0;
376 struct CAN_Msg msg;
377 struct pcap_pkthdr pkth;
378
379 while(i < max_packets)
380 {
381 usleep(10 * 1000);
382 int n = read(handle->fd, &msg, sizeof(msg));
383 if (n <= 0) break;
384 pkth.caplen = pkth.len = n;
385 pkth.caplen -= 4;
386 pkth.caplen -= 8 - msg.length;
387
388 if ((firstpacket.tv_sec == -1) && (firstpacket.tv_usec == -1))
389 gettimeofday(&firstpacket, NULL);
390
391 pkth.ts.tv_usec = firstpacket.tv_usec + (msg.timestamp % 100) * 10000;
392 pkth.ts.tv_sec = firstpacket.tv_usec + (msg.timestamp / 100);
393 if (pkth.ts.tv_usec > 1000000)
394 {
395 pkth.ts.tv_usec -= 1000000;
396 pkth.ts.tv_sec++;
397 }
398
399 callback(user, &pkth, (void*)&msg.id);
400 i++;
401 }
402
403 return i;
404 }
405
406
407 static int
408 canusb_inject_linux(pcap_t *handle, const void *buf, size_t size)
409 {
410 /* not yet implemented */
411 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "inject not supported on canusb devices");
412 return (-1);
413 }
414
415
416 static int
417 canusb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
418 {
419 /* not yet implemented */
420 stats->ps_recv = 0; /* number of packets received */
421 stats->ps_drop = 0; /* number of packets dropped */
422 stats->ps_ifdrop = 0; /* drops by interface -- only supported on some platforms */
423 return 0;
424 }
425
426
427 static int
428 canusb_setfilter_linux(pcap_t *p, struct bpf_program *fp)
429 {
430 /* not yet implemented */
431 return 0;
432 }
433
434
435 static int
436 canusb_setdirection_linux(pcap_t *p, pcap_direction_t d)
437 {
438 /* no support for PCAP_D_OUT */
439 if (d == PCAP_D_OUT)
440 {
441 snprintf(p->errbuf, sizeof(p->errbuf),
442 "Setting direction to PCAP_D_OUT is not supported on this interface");
443 return -1;
444 }
445
446 p->direction = d;
447
448 return 0;
449 }
450
451
452 /* eof */