xref: /csrg-svn/old/implogd/implogd.c (revision 8457)
1*8457Ssam /*	implogd.c	4.2	82/10/10	*/
26480Ssam 
36480Ssam #include <time.h>
46480Ssam #include <sgtty.h>
56480Ssam #include <sys/types.h>
66480Ssam #include <sys/socket.h>
76480Ssam #include <net/in.h>
86480Ssam 
96480Ssam #define	LOGFILE	"/usr/adm/implog"
106480Ssam #define	IMPMTU	((8159 / 8) & ~01)
116480Ssam 
126480Ssam u_char	request[1024];
136480Ssam int	marktime();
146480Ssam int	options;
156480Ssam extern	int errno;
166480Ssam int	log;
176480Ssam 
186480Ssam /*
196480Ssam  * Socket address, internet style, with
206480Ssam  * unused space taken by timestamp and packet
216480Ssam  * size.
226480Ssam  */
236480Ssam struct sockstamp {
246480Ssam 	short	sin_family;
256480Ssam 	u_short	sin_port;
266480Ssam 	struct	in_addr sin_addr;
276480Ssam 	time_t	sin_time;
286480Ssam 	int	sin_cc;
296480Ssam };
306480Ssam 
316480Ssam struct	sockproto improto = { PF_IMPLINK, 0 };
326480Ssam struct	sockstamp from;
336480Ssam 
346480Ssam main(argc, argv)
356480Ssam 	char *argv[];
366480Ssam {
376480Ssam 	int s, cc;
386480Ssam 	time_t t;
396480Ssam 
406480Ssam 	argc--, argv++;
416480Ssam 	if (argc > 0 && !strcmp(argv[0], "-d"))
426480Ssam 		options |= SO_DEBUG;
43*8457Ssam #ifndef DEBUG
44*8457Ssam 	if (fork())
45*8457Ssam 		exit(0);
46*8457Ssam 	for (s = 0; s < 10; s++)
47*8457Ssam 		(void) close(t);
48*8457Ssam 	(void) open("/", 0);
49*8457Ssam 	(void) dup2(0, 1);
50*8457Ssam 	(void) dup2(0, 2);
51*8457Ssam 	{ int tt = open("/dev/tty", 2);
52*8457Ssam 	  if (tt > 0) {
53*8457Ssam 		ioctl(tt, TIOCNOTTY, 0);
54*8457Ssam 		close(tt);
55*8457Ssam 	  }
566480Ssam 	}
57*8457Ssam #endif
586480Ssam 	log = open(LOGFILE, 1);
596480Ssam 	if (log < 0)
606480Ssam 		exit(1);
616480Ssam 	lseek(log, 0L, 2);
626480Ssam 	from.sin_time = time(0);
636480Ssam 	from.sin_cc = sizeof(time_t);
646480Ssam 	write(log, (char *)&from, sizeof(from));
656480Ssam again:
666480Ssam 	errno = 0;
676480Ssam 	if ((s = socket(SOCK_RAW, &improto, 0, options)) < 0) {
686480Ssam 		perror("socket");
696480Ssam 		sleep(5);
706480Ssam 		goto again;
716480Ssam 	}
726480Ssam 	for (;;) {
736480Ssam 		cc = receive(s, &from, request, sizeof(request));
746480Ssam 		if (cc <= 0)
756480Ssam 			continue;
766480Ssam 		if (cc > IMPMTU)	/* sanity */
776480Ssam 			continue;
786480Ssam 		from.sin_cc = cc;
796480Ssam 		from.sin_time = time(0);
806480Ssam 		write(log, (char *)&from, sizeof(from));
816480Ssam 		write(log, request, cc);
826480Ssam 	}
836480Ssam 	/*NOTREACHED*/
846480Ssam }
85