xref: /csrg-svn/old/implogd/implogd.c (revision 9196)
1 /*	implogd.c	4.2	82/10/10	*/
2 
3 #include <time.h>
4 #include <sgtty.h>
5 
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 
9 #include <netinet/in.h>
10 
11 #define	LOGFILE	"/usr/adm/implog"
12 #define	IMPMTU	((8159 / 8) & ~01)
13 
14 u_char	request[1024];
15 int	marktime();
16 int	options;
17 extern	int errno;
18 int	log;
19 
20 /*
21  * Socket address, internet style, with
22  * unused space taken by timestamp and packet
23  * size.
24  */
25 struct sockstamp {
26 	short	sin_family;
27 	u_short	sin_port;
28 	struct	in_addr sin_addr;
29 	time_t	sin_time;
30 	int	sin_len;
31 };
32 
33 main(argc, argv)
34 	char *argv[];
35 {
36 	int s;
37 	time_t t;
38 	struct sockstamp from;
39 
40 	argc--, argv++;
41 	if (argc > 0 && !strcmp(argv[0], "-d"))
42 		options |= SO_DEBUG;
43 #ifndef DEBUG
44 	if (fork())
45 		exit(0);
46 	for (s = 0; s < 10; s++)
47 		(void) close(t);
48 	(void) open("/", 0);
49 	(void) dup2(0, 1);
50 	(void) dup2(0, 2);
51 	{ int tt = open("/dev/tty", 2);
52 	  if (tt > 0) {
53 		ioctl(tt, TIOCNOTTY, 0);
54 		close(tt);
55 	  }
56 	}
57 #endif
58 	log = open(LOGFILE, 1);
59 	if (log < 0)
60 		exit(1);
61 	lseek(log, 0L, 2);
62 	from.sin_time = time(0);
63 	from.sin_len = sizeof (time_t);
64 	write(log, (char *)&from, sizeof (from));
65 again:
66 	s = socket(AF_IMPLINK, SOCK_RAW, 0, 0);
67 	if (s < 0) {
68 		perror("socket");
69 		sleep(5);
70 		goto again;
71 	}
72 	for (;;) {
73 		int len = sizeof (request);
74 
75 		if (recvfrom(s, request, &len, &from, sizeof (from), 0) < 0)
76 			continue;
77 		if (len <= 0 || len > IMPMTU)	/* sanity */
78 			continue;
79 		from.sin_len = len;
80 		from.sin_time = time(0);
81 		write(log, (char *)&from, sizeof (from));
82 		write(log, request, len);
83 	}
84 	/*NOTREACHED*/
85 }
86