summaryrefslogtreecommitdiffstats
path: root/gnu/usr.sbin/isdn/spy/spy.c
blob: 1f730c07b35022198b5814fae801279eff8930be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
static char     rcsid[] = "@(#)$Id: spy.c,v 1.2 1995/01/25 13:41:44 jkr Exp jkr $";
/*******************************************************************************
 *  II - Version 0.1 $Revision: 1.2 $   $State: Exp $
 *
 * Copyright 1994 Dietmar Friede
 *******************************************************************************
 * Bug reports, patches, comments, suggestions should be sent to:
 *
 *	jkr@saarlink.de or jkrause@guug.de
 *
 *******************************************************************************
 * $Log: spy.c,v $
 *
 ******************************************************************************/

#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/time.h>

#define BSIZE	1024+sizeof(struct timeval)+sizeof(unsigned long)+1
unsigned char   buf[BSIZE];
FILE           *Fout = NULL;

static void
catchsig()
{
	printf("EXIT\n");
	exit(1);
}

main(argc, argv)
	int             argc;
	char          **argv;
{
	int             f, n;
	struct timeval *t = (struct timeval *) & buf[sizeof(unsigned long) + 1];
	char           *b = &buf[sizeof(struct timeval) + sizeof(unsigned long) + 1];
	struct tm      *s;

	if ((f = open("/dev/ispy", O_RDONLY)) < 0)
	{
		perror(argv[1]);
		exit(1);
	}
	argv++;
	if (*argv)
		Fout = fopen(*argv, "w");

	(void) signal(SIGHUP, catchsig);
	(void) signal(SIGTERM, catchsig);
	(void) signal(SIGKILL, catchsig);
	(void) signal(SIGINT, catchsig);
	(void) signal(SIGQUIT, catchsig);

	for (;;)
	{
		n = read(f, buf, BSIZE);
		n -= sizeof(struct timeval) + sizeof(unsigned long) + 1;
		s = localtime(&t->tv_sec);
		if (*buf)
			printf("> ");
		else
			printf("< ");
		printf("%.4d: %.2d:%.2d:%.2d.%.2d len %d:\n", *(unsigned long *) &buf[1],
		   s->tm_hour, s->tm_min, s->tm_sec, t->tv_usec / 10000, n);
		if (Fout != NULL)
		{
			if (*buf)
				fprintf(Fout, "> ");
			else
				fprintf(Fout, "< ");
			fprintf(Fout, "%.4d: %.2d:%.2d:%.2d.%.2d len %d:\n",
			  *(unsigned long *) &buf[1], s->tm_hour, s->tm_min,
				s->tm_sec, t->tv_usec / 10000, n);
		}
		if (n > 0)
		{
			dumpbuf(stdout, n, b);
			if (Fout != NULL)
				dumpbuf(Fout, n, b);
		}
	}
}

dumpbuf(FILE * f, int n, unsigned char *buf)
{
	int             i, j;

	for (i = 0; i < n; i += 16)
	{
		fprintf(f, "%.3x  ", i);
		for (j = 0; j < 16; j++)
			if (i + j < n)
				fprintf(f, "%02x ", buf[i + j]);
			else
				fprintf(f, "   ");
		fprintf(f, "      ");
		for (j = 0; j < 16 && i + j < n; j++)
			if (isprint(buf[i + j]))
				fprintf(f, "%c", buf[i + j]);
			else
				fputc('.', f);
		fputc('\n', f);
	}
	fflush(f);
}
OpenPOWER on IntegriCloud