2 * Copyright (c) 2009 Felix Obenhuber
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
30 * Sockettrace sniffing API implementation for Linux platform
31 * By Felix Obenhuber <felix@obenhuber.de>
39 #include <libusb-1.0/libusb.h>
48 #define CANUSB_IFACE "canusb"
50 #define CANUSB_VID 0x0403
51 #define CANUSB_PID 0x8990
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
*);
79 libusb_device_handle
*dev
;
85 static struct canusb_t canusb
;
86 static volatile int loop
;
88 int canusb_findalldevs(pcap_if_t
**alldevsp
, char *err_str
)
90 libusb_context
*fdctx
;
92 unsigned char sernum
[65];
93 unsigned char buf
[96];
98 cnt
= libusb_get_device_list(fdctx
,&devs
);
103 // Check if this device is interesting.
104 struct libusb_device_descriptor desc
;
105 libusb_get_device_descriptor(devs
[i
],&desc
);
107 if ((desc
.idVendor
!= CANUSB_VID
) || (desc
.idProduct
!= CANUSB_PID
))
108 continue; //It is not, check next device
111 libusb_device_handle
*dh
= NULL
;
113 if (ret
= libusb_open(devs
[i
],&dh
) == 0)
117 int n
= libusb_get_string_descriptor_ascii(dh
,desc
.iSerialNumber
,sernum
,64);
120 snprintf(dev_name
, 30, CANUSB_IFACE
"%s", sernum
);
121 snprintf(dev_descr
, 50, "CanUSB [%s]", sernum
);
125 if (pcap_add_if(alldevsp
, dev_name
, 0, dev_descr
, err_str
) < 0)
127 libusb_free_device_list(devs
,1);
133 libusb_free_device_list(devs
,1);
138 static libusb_device_handle
* canusb_opendevice(struct libusb_context
*ctx
, char* devserial
)
140 libusb_device_handle
* dh
;
141 libusb_device
** devs
;
142 unsigned char serial
[65];
145 cnt
= libusb_get_device_list(ctx
,&devs
);
149 // Check if this device is interesting.
150 struct libusb_device_descriptor desc
;
151 libusb_get_device_descriptor(devs
[i
],&desc
);
153 if ((desc
.idVendor
!= CANUSB_VID
) || (desc
.idProduct
!= CANUSB_PID
))
157 libusb_device_handle
*dh
= NULL
;
159 if (libusb_open(devs
[i
],&dh
) != 0) continue;
161 n
= libusb_get_string_descriptor_ascii(dh
,desc
.iSerialNumber
,serial
,64);
164 if ((devserial
) && (strcmp(serial
,devserial
) != 0))
170 if ((libusb_kernel_driver_active(dh
,0)) && (libusb_detach_kernel_driver(dh
,0) != 0))
176 if (libusb_set_configuration(dh
,1) != 0)
182 if (libusb_claim_interface(dh
,0) != 0)
189 libusb_free_device_list(devs
,1);
193 libusb_free_device_list(devs
,1);
199 canusb_create(const char *device
, char *ebuf
, int *is_ours
)
201 const char *cp
, *cpend
;
205 libusb_init(&canusb
.ctx
);
207 /* Does this look like a DAG device? */
208 cp
= strrchr(device
, '/');
211 /* Does it begin with "canusb"? */
212 if (strncmp(cp
, "canusb", 6) != 0) {
213 /* Nope, doesn't begin with "canusb" */
217 /* Yes - is "canusb" followed by a number? */
219 devnum
= strtol(cp
, &cpend
, 10);
220 if (cpend
== cp
|| *cpend
!= '\0') {
221 /* Not followed by a number. */
226 /* Followed by a non-valid number. */
231 /* OK, it's probably ours. */
234 p
= pcap_create_common(device
, ebuf
);
238 memset(&canusb
, 0x00, sizeof(canusb
));
240 p
->activate_op
= canusb_activate
;
246 static void* canusb_capture_thread(struct canusb_t
*canusb
)
248 struct libusb_context
*ctx
;
249 libusb_device_handle
*dev
;
259 char *serial
= canusb
->src
+ strlen(CANUSB_IFACE
);
260 dev
= canusb_opendevice(ctx
, serial
);
262 fcntl(canusb
->wrpipe
, F_SETFL
, O_NONBLOCK
);
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);
273 for(i
= 0; i
<status
.rxsz
; i
++)
275 libusb_bulk_transfer(dev
, 0x85, (unsigned char*)&msg
, sizeof(msg
), &sz
, 100);
276 n
= write(canusb
->wrpipe
, &msg
, sizeof(msg
));
287 static int canusb_startcapture(struct canusb_t
* this)
291 if (pipe(pipefd
) == -1) return -1;
293 canusb
.rdpipe
= pipefd
[0];
294 canusb
.wrpipe
= pipefd
[1];
298 pthread_create(&this->worker
, NULL
, canusb_capture_thread
, &canusb
);
300 return canusb
.rdpipe
;
303 static void canusb_clearbufs(struct canusb_t
* this)
305 unsigned char cmd
[16];
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);
313 libusb_interrupt_transfer(this->dev
, 0x1,cmd
,16,&al
,100);
317 static void canusb_close(pcap_t
* handle
)
320 pthread_join(canusb
.worker
, NULL
);
324 libusb_close(canusb
.dev
);
331 static int canusb_activate(pcap_t
* handle
)
333 handle
->read_op
= canusb_read_linux
;
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
;
343 /* Initialize some components of the pcap structure. */
344 handle
->bufsize
= 32;
346 handle
->linktype
= DLT_CAN_SOCKETCAN
;
347 handle
->set_datalink_op
= NULL
;
349 char* serial
= handle
->opt
.source
+ strlen("canusb");
351 canusb
.dev
= canusb_opendevice(canusb
.ctx
,serial
);
354 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "Can't open USB Device:");
358 canusb_clearbufs(&canusb
);
360 handle
->fd
= canusb_startcapture(&canusb
);
361 handle
->selectable_fd
= handle
->fd
;
370 canusb_read_linux(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
372 static struct timeval firstpacket
= { -1, -1};
377 struct pcap_pkthdr pkth
;
379 while(i
< max_packets
)
382 int n
= read(handle
->fd
, &msg
, sizeof(msg
));
384 pkth
.caplen
= pkth
.len
= n
;
386 pkth
.caplen
-= 8 - msg
.length
;
388 if ((firstpacket
.tv_sec
== -1) && (firstpacket
.tv_usec
== -1))
389 gettimeofday(&firstpacket
, NULL
);
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)
395 pkth
.ts
.tv_usec
-= 1000000;
399 callback(user
, &pkth
, (void*)&msg
.id
);
408 canusb_inject_linux(pcap_t
*handle
, const void *buf
, size_t size
)
410 /* not yet implemented */
411 snprintf(handle
->errbuf
, PCAP_ERRBUF_SIZE
, "inject not supported on canusb devices");
417 canusb_stats_linux(pcap_t
*handle
, struct pcap_stat
*stats
)
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 */
428 canusb_setfilter_linux(pcap_t
*p
, struct bpf_program
*fp
)
430 /* not yet implemented */
436 canusb_setdirection_linux(pcap_t
*p
, pcap_direction_t d
)
438 /* no support for PCAP_D_OUT */
441 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
442 "Setting direction to PCAP_D_OUT is not supported on this interface");