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