Minor usbdump update:
- Use memcpy() instead of bcopy(). - Replace all asserts() by standard error messages. - Update usage(). MFC after: 7 days
This commit is contained in:
parent
26e8415d1d
commit
955c139ed4
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=221604
@ -40,7 +40,6 @@
|
|||||||
#include <dev/usb/usb.h>
|
#include <dev/usb/usb.h>
|
||||||
#include <dev/usb/usb_pf.h>
|
#include <dev/usb/usb_pf.h>
|
||||||
#include <dev/usb/usbdi.h>
|
#include <dev/usb/usbdi.h>
|
||||||
#include <assert.h>
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@ -49,6 +48,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <sysexits.h>
|
||||||
|
#include <err.h>
|
||||||
|
|
||||||
struct usbcap {
|
struct usbcap {
|
||||||
int fd; /* fd for /dev/usbpf */
|
int fd; /* fd for /dev/usbpf */
|
||||||
@ -408,9 +409,15 @@ write_packets(struct usbcap *p, const uint8_t *data, const int datalen)
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = write(p->wfd, &len, sizeof(int));
|
ret = write(p->wfd, &len, sizeof(int));
|
||||||
assert(ret == sizeof(int));
|
if (ret != sizeof(int)) {
|
||||||
|
err(EXIT_FAILURE, "Could not write length "
|
||||||
|
"field of USB data payload");
|
||||||
|
}
|
||||||
ret = write(p->wfd, data, datalen);
|
ret = write(p->wfd, data, datalen);
|
||||||
assert(ret == datalen);
|
if (ret != datalen) {
|
||||||
|
err(EXIT_FAILURE, "Could not write "
|
||||||
|
"complete USB data payload");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -423,14 +430,16 @@ read_file(struct usbcap *p)
|
|||||||
while ((ret = read(p->rfd, &datalen, sizeof(int))) == sizeof(int)) {
|
while ((ret = read(p->rfd, &datalen, sizeof(int))) == sizeof(int)) {
|
||||||
datalen = le32toh(datalen);
|
datalen = le32toh(datalen);
|
||||||
data = malloc(datalen);
|
data = malloc(datalen);
|
||||||
assert(data != NULL);
|
if (data == NULL)
|
||||||
|
errx(EX_SOFTWARE, "Out of memory.");
|
||||||
ret = read(p->rfd, data, datalen);
|
ret = read(p->rfd, data, datalen);
|
||||||
assert(ret == datalen);
|
if (ret != datalen) {
|
||||||
|
err(EXIT_FAILURE, "Could not read complete "
|
||||||
|
"USB data payload");
|
||||||
|
}
|
||||||
print_packets(data, datalen);
|
print_packets(data, datalen);
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
if (ret == -1)
|
|
||||||
fprintf(stderr, "read: %s\n", strerror(errno));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -466,14 +475,27 @@ init_rfile(struct usbcap *p)
|
|||||||
|
|
||||||
p->rfd = open(r_arg, O_RDONLY);
|
p->rfd = open(r_arg, O_RDONLY);
|
||||||
if (p->rfd < 0) {
|
if (p->rfd < 0) {
|
||||||
fprintf(stderr, "open: %s (%s)\n", r_arg, strerror(errno));
|
err(EXIT_FAILURE, "Could not open "
|
||||||
exit(EXIT_FAILURE);
|
"'%s' for read", r_arg);
|
||||||
}
|
}
|
||||||
ret = read(p->rfd, &uf, sizeof(uf));
|
ret = read(p->rfd, &uf, sizeof(uf));
|
||||||
assert(ret == sizeof(uf));
|
if (ret != sizeof(uf)) {
|
||||||
assert(le32toh(uf.magic) == USBCAP_FILEHDR_MAGIC);
|
err(EXIT_FAILURE, "Could not read USB capture "
|
||||||
assert(uf.major == 0);
|
"file header");
|
||||||
assert(uf.minor == 2);
|
}
|
||||||
|
if (le32toh(uf.magic) != USBCAP_FILEHDR_MAGIC) {
|
||||||
|
errx(EX_SOFTWARE, "Invalid magic field(0x%08x) "
|
||||||
|
"in USB capture file header.",
|
||||||
|
(unsigned int)le32toh(uf.magic));
|
||||||
|
}
|
||||||
|
if (uf.major != 0) {
|
||||||
|
errx(EX_SOFTWARE, "Invalid major version(%d) "
|
||||||
|
"field in USB capture file header.", (int)uf.major);
|
||||||
|
}
|
||||||
|
if (uf.minor != 2) {
|
||||||
|
errx(EX_SOFTWARE, "Invalid minor version(%d) "
|
||||||
|
"field in USB capture file header.", (int)uf.minor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -484,15 +506,18 @@ init_wfile(struct usbcap *p)
|
|||||||
|
|
||||||
p->wfd = open(w_arg, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
|
p->wfd = open(w_arg, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
|
||||||
if (p->wfd < 0) {
|
if (p->wfd < 0) {
|
||||||
fprintf(stderr, "open: %s (%s)\n", w_arg, strerror(errno));
|
err(EXIT_FAILURE, "Could not open "
|
||||||
exit(EXIT_FAILURE);
|
"'%s' for write", r_arg);
|
||||||
}
|
}
|
||||||
bzero(&uf, sizeof(uf));
|
memset(&uf, 0, sizeof(uf));
|
||||||
uf.magic = htole32(USBCAP_FILEHDR_MAGIC);
|
uf.magic = htole32(USBCAP_FILEHDR_MAGIC);
|
||||||
uf.major = 0;
|
uf.major = 0;
|
||||||
uf.minor = 2;
|
uf.minor = 2;
|
||||||
ret = write(p->wfd, (const void *)&uf, sizeof(uf));
|
ret = write(p->wfd, (const void *)&uf, sizeof(uf));
|
||||||
assert(ret == sizeof(uf));
|
if (ret != sizeof(uf)) {
|
||||||
|
err(EXIT_FAILURE, "Could not write "
|
||||||
|
"USB capture header");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -501,13 +526,13 @@ usage(void)
|
|||||||
|
|
||||||
#define FMT " %-14s %s\n"
|
#define FMT " %-14s %s\n"
|
||||||
fprintf(stderr, "usage: usbdump [options]\n");
|
fprintf(stderr, "usage: usbdump [options]\n");
|
||||||
fprintf(stderr, FMT, "-i ifname", "Listen on USB bus interface");
|
fprintf(stderr, FMT, "-i <usbusX>", "Listen on USB bus interface");
|
||||||
fprintf(stderr, FMT, "-r file", "Read the raw packets from file");
|
fprintf(stderr, FMT, "-r <file>", "Read the raw packets from file");
|
||||||
fprintf(stderr, FMT, "-s snaplen", "Snapshot bytes from each packet");
|
fprintf(stderr, FMT, "-s <snaplen>", "Snapshot bytes from each packet");
|
||||||
fprintf(stderr, FMT, "-v", "Increases the verbose level");
|
fprintf(stderr, FMT, "-v", "Increase the verbose level");
|
||||||
fprintf(stderr, FMT, "-w file", "Write the raw packets to file");
|
fprintf(stderr, FMT, "-w <file>", "Write the raw packets to file");
|
||||||
#undef FMT
|
#undef FMT
|
||||||
exit(1);
|
exit(EX_USAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -525,7 +550,7 @@ main(int argc, char *argv[])
|
|||||||
int fd, o;
|
int fd, o;
|
||||||
const char *optstring;
|
const char *optstring;
|
||||||
|
|
||||||
bzero(&uc, sizeof(struct usbcap));
|
memset(&uc, 0, sizeof(struct usbcap));
|
||||||
|
|
||||||
optstring = "i:r:s:vw:";
|
optstring = "i:r:s:vw:";
|
||||||
while ((o = getopt(argc, argv, optstring)) != -1) {
|
while ((o = getopt(argc, argv, optstring)) != -1) {
|
||||||
@ -565,20 +590,15 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
p->fd = fd = open("/dev/bpf", O_RDONLY);
|
p->fd = fd = open("/dev/bpf", O_RDONLY);
|
||||||
if (p->fd < 0) {
|
if (p->fd < 0)
|
||||||
fprintf(stderr, "(no devices found)\n");
|
err(EXIT_FAILURE, "Could not open BPF device");
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0)
|
||||||
|
err(EXIT_FAILURE, "BIOCVERSION ioctl failed");
|
||||||
|
|
||||||
if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
|
|
||||||
fprintf(stderr, "BIOCVERSION: %s\n", strerror(errno));
|
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (bv.bv_major != BPF_MAJOR_VERSION ||
|
if (bv.bv_major != BPF_MAJOR_VERSION ||
|
||||||
bv.bv_minor < BPF_MINOR_VERSION) {
|
bv.bv_minor < BPF_MINOR_VERSION)
|
||||||
fprintf(stderr, "kernel bpf filter out of date");
|
errx(EXIT_FAILURE, "Kernel BPF filter out of date");
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* USB transfers can be greater than 64KByte */
|
/* USB transfers can be greater than 64KByte */
|
||||||
v = 1U << 16;
|
v = 1U << 16;
|
||||||
@ -592,22 +612,16 @@ main(int argc, char *argv[])
|
|||||||
if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
|
if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (v == 0) {
|
if (v == 0)
|
||||||
fprintf(stderr, "BIOCSBLEN: %s: No buffer size worked", i_arg);
|
errx(EXIT_FAILURE, "No buffer size worked.");
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
|
if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0)
|
||||||
fprintf(stderr, "BIOCGBLEN: %s", strerror(errno));
|
err(EXIT_FAILURE, "BIOCGBLEN ioctl failed");
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
p->bufsize = v;
|
p->bufsize = v;
|
||||||
p->buffer = (uint8_t *)malloc(p->bufsize);
|
p->buffer = (uint8_t *)malloc(p->bufsize);
|
||||||
if (p->buffer == NULL) {
|
if (p->buffer == NULL)
|
||||||
fprintf(stderr, "malloc: %s", strerror(errno));
|
errx(EX_SOFTWARE, "Out of memory.");
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* XXX no read filter rules yet so at this moment accept everything */
|
/* XXX no read filter rules yet so at this moment accept everything */
|
||||||
total_insn.code = (u_short)(BPF_RET | BPF_K);
|
total_insn.code = (u_short)(BPF_RET | BPF_K);
|
||||||
@ -617,27 +631,21 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
total_prog.bf_len = 1;
|
total_prog.bf_len = 1;
|
||||||
total_prog.bf_insns = &total_insn;
|
total_prog.bf_insns = &total_insn;
|
||||||
if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
|
if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0)
|
||||||
fprintf(stderr, "BIOCSETF: %s", strerror(errno));
|
err(EXIT_FAILURE, "BIOCSETF ioctl failed");
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 1 second read timeout */
|
/* 1 second read timeout */
|
||||||
tv.tv_sec = 1;
|
tv.tv_sec = 1;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&tv) < 0) {
|
if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&tv) < 0)
|
||||||
fprintf(stderr, "BIOCSRTIMEOUT: %s", strerror(errno));
|
err(EXIT_FAILURE, "BIOCSRTIMEOUT ioctl failed");
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
(void)signal(SIGINT, handle_sigint);
|
(void)signal(SIGINT, handle_sigint);
|
||||||
|
|
||||||
do_loop(p);
|
do_loop(p);
|
||||||
|
|
||||||
if (ioctl(fd, BIOCGSTATS, (caddr_t)&us) < 0) {
|
if (ioctl(fd, BIOCGSTATS, (caddr_t)&us) < 0)
|
||||||
fprintf(stderr, "BIOCGSTATS: %s", strerror(errno));
|
err(EXIT_FAILURE, "BIOCGSTATS ioctl failed");
|
||||||
return (EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* XXX what's difference between pkt_captured and us.us_recv? */
|
/* XXX what's difference between pkt_captured and us.us_recv? */
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user