aboutsummaryrefslogtreecommitdiffstats
path: root/etc
diff options
context:
space:
mode:
authorFritz Elfert <felfert@to.com>2001-01-31 02:01:16 +0000
committerFritz Elfert <felfert@to.com>2001-01-31 02:01:16 +0000
commit764b9679bb9188a973047982c82f06813d161325 (patch)
tree6b1be08d2d9fdaaaf6f356df40a0f038bd5c67c3 /etc
parentc74c003be18e83d63831cb683dcc7ec2476a6a79 (diff)
downloadplptools-764b9679bb9188a973047982c82f06813d161325.tar.gz
plptools-764b9679bb9188a973047982c82f06813d161325.tar.bz2
plptools-764b9679bb9188a973047982c82f06813d161325.zip
Added tty sniffer.
Diffstat (limited to 'etc')
-rw-r--r--etc/ttytap.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/etc/ttytap.c b/etc/ttytap.c
new file mode 100644
index 0000000..6914e6a
--- /dev/null
+++ b/etc/ttytap.c
@@ -0,0 +1,88 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/fcntl.h>
+#include <sys/time.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <linux/ttytap.h>
+#include <linux/kdev_t.h>
+
+static void usage(void) {
+ fprintf(stderr, "usage: ttytap -q|<ttyDevice>\n");
+ exit(1);
+}
+
+int main(int argc, char **argv) {
+ int fd;
+ tapheader th;
+ int i;
+ int r;
+ int major;
+ int minor;
+ int lastdir = -1;
+ struct stat stbuf;
+ char tbuf[256];
+ unsigned char buf[4096];
+
+
+ if (argc != 2)
+ usage();
+ if (strcmp(argv[1], "-q")) {
+ if (stat(argv[1], &stbuf)) {
+ perror(argv[1]);
+ exit(-1);
+ }
+ if (!S_ISCHR(stbuf.st_mode)) {
+ fprintf(stderr, "%s is not a char device\n", argv[1]);
+ exit(1);
+ }
+ major = MAJOR(stbuf.st_rdev);
+ minor = MINOR(stbuf.st_rdev);
+ } else {
+ minor = 0;
+ major = 0;
+ }
+ fd = open("/proc/ttytap", O_RDONLY);
+ if (fd == -1) {
+ perror("/proc/ttytap");
+ exit(-1);
+ }
+ if (ioctl(fd, TTYTAP_SETDEV, (major << 8) | minor)) {
+ perror("ioctl /proc/ttytap");
+ exit(-1);
+ }
+ if ((minor == 0) && (major == 0)) {
+ printf("Serial tapping switched off\n");
+ exit(0);
+ }
+ if (ioctl(fd, TTYTAP_GETQLEN, &i)) {
+ perror("ioctl /proc/ttytap");
+ exit(-1);
+ }
+ fprintf(stderr, "Queue length is %d\n", i);
+ while (1) {
+ r = read(fd, &th, sizeof(th));
+ if (r != sizeof(th)) {
+ perror("read /proc/ttytap");
+ exit(-1);
+ }
+ strftime(tbuf, 255, "%H:%M:%S", localtime(&th.stamp.tv_sec));
+ printf("%s.%06ld %c (%04d) ", tbuf,
+ (unsigned long)th.stamp.tv_usec,
+ (th.io) ? '<' : '>', th.len);
+ r = read(fd, buf, th.len);
+ if (r != th.len) {
+ perror("read /proc/ttytap");
+ exit(-1);
+ }
+ for (i = 0; i < th.len; i++)
+ printf("%02x ", buf[i] & 255);
+ for (i = 0; i < th.len; i++)
+ printf("%c", isprint(buf[i]) ? buf[i] : '.');
+ printf("\n"); fflush(stdout);
+ }
+ return 0;
+}