summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-12-15 16:33:29 +0100
committerPeter Wu <peter@lekensteyn.nl>2014-12-15 16:34:16 +0100
commitcf6f30827732a82e17649a931400a21d4647e041 (patch)
tree58792345c066d2073fe246a2d07f1fc8c0a78612
parent1b3247bc573314e4b05ce5bc60cf9f14c93228a5 (diff)
downloadltunify-cf6f30827732a82e17649a931400a21d4647e041.tar.gz
read-dev-usbmon: factor out packet processing
Factor out retrieval of a header, time and data from a usbmon event. This allows reuse for pcap processing.
-rw-r--r--read-dev-usbmon.c96
1 files changed, 52 insertions, 44 deletions
diff --git a/read-dev-usbmon.c b/read-dev-usbmon.c
index bd1c1a6..2c246c0 100644
--- a/read-dev-usbmon.c
+++ b/read-dev-usbmon.c
@@ -3,7 +3,7 @@
* Because of limitations of a single output stream, there is currently a hack
* that directly includes hidraw.c.
*
- * Copyright (C) 2013 Peter Wu <lekensteyn@gmail.com>
+ * Copyright (C) 2013-2014 Peter Wu <peter@lekensteyn.nl>
*
* 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
@@ -80,25 +80,24 @@ struct mon_get_arg {
#include "hidraw.c"
#undef NO_MAIN
-void print_time(void) {
- struct timeval tval;
+void print_time(const struct timeval *tval) {
struct tm *tm;
- if (gettimeofday(&tval, NULL)) {
- perror("gettimeofday");
- return;
- }
- tm = localtime(&tval.tv_sec);
+ tm = localtime(&tval->tv_sec);
printf("%02d:%02d:%02d.%03ld ",
tm->tm_hour, tm->tm_min, tm->tm_sec,
- tval.tv_usec / 1000);
+ tval->tv_usec / 1000);
}
+void process_usbpkt(const struct usbmon_packet *hdr, const unsigned char *data,
+ const struct timeval *tval);
+
int main(int argc, char ** argv) {
unsigned char data[1024];
struct usbmon_packet hdr;
struct mon_get_arg event;
int fd, r;
+ struct timeval tval = { 0, 0 };
if (argc < 2) {
fprintf(stderr, "Usage: %s /dev/usbmonX\n", argv[0]);
@@ -129,45 +128,54 @@ int main(int argc, char ** argv) {
break;
}
- // ignore non-data packets
- if (hdr.len_cap) {
- if (getenv("HEX")) {
- unsigned int i;
- printf("Type=%c\n", hdr.type);
- for (i=0; i<hdr.len_cap; i++) {
- printf("%02X%c", data[i],
- i + 1 == hdr.len_cap ? '\n' : ' ');
- }
- } else if (hdr.len_cap > sizeof (struct report)) {
- fprintf(stderr, "Discarding too large packet of length %u!\n", hdr.len_cap);
- } else {
- struct report *report = (struct report *)&data;
- if (hdr.len_cap < 3) {
- fprintf(stderr, "Short data len: %i\n", hdr.len_cap);
- continue;
- }
-#define COLOR(c, cstr) "\033[" c "m" cstr "\033[m"
- print_time();
- if (hdr.type == 'C') {
- printf(COLOR("1;32", "Recv\t"));
- } else if (hdr.type == 'S') {
- printf(COLOR("1;31", "Send\t"));
- } else {
- printf(COLOR("1;35", "Type=%c\t") "\n", hdr.type);
- }
- process_msg(report, hdr.len_cap);
- fflush(NULL);
-#if 0
- if (write(STDOUT_FILENO, &data, hdr.len_cap) < 0) {
- perror("write");
- break;
- }
-#endif
- }
+ if (gettimeofday(&tval, NULL)) {
+ perror("gettimeofday");
}
+ process_usbpkt(&hdr, data, &tval);
}
close(fd);
return 0;
}
+
+void process_usbpkt(const struct usbmon_packet *hdr, const unsigned char *data,
+ const struct timeval *tval) {
+ // ignore non-data packets
+ if (!hdr->len_cap) {
+ return;
+ }
+ if (getenv("HEX")) {
+ unsigned int i;
+ printf("Type=%c\n", hdr->type);
+ for (i=0; i<hdr->len_cap; i++) {
+ printf("%02X%c", data[i],
+ i + 1 == hdr->len_cap ? '\n' : ' ');
+ }
+ } else if (hdr->len_cap > sizeof (struct report)) {
+ fprintf(stderr, "Discarding too large packet of length %u!\n", hdr->len_cap);
+ } else {
+ struct report *report = (struct report *)data;
+ if (hdr->len_cap < 3) {
+ fprintf(stderr, "Short data len: %i\n", hdr->len_cap);
+ return;
+ }
+#define COLOR(c, cstr) "\033[" c "m" cstr "\033[m"
+ print_time(tval);
+ if (hdr->type == 'C') {
+ printf(COLOR("1;32", "Recv\t"));
+ } else if (hdr->type == 'S') {
+ printf(COLOR("1;31", "Send\t"));
+ } else {
+ printf(COLOR("1;35", "Type=%c\t") "\n", hdr->type);
+ }
+ process_msg(report, hdr->len_cap);
+ fflush(NULL);
+#if 0
+ if (write(STDOUT_FILENO, data, hdr->len_cap) < 0) {
+ perror("write");
+ break;
+ }
+#endif
+ }
+}