xref: /csrg-svn/old/implogd/implogd.c (revision 12237)
19198Ssam #ifndef lint
2*12237Ssam static char sccsid[] = "@(#)implogd.c	4.6 (Berkeley) 05/05/83";
39198Ssam #endif
46480Ssam 
56480Ssam #include <time.h>
66480Ssam #include <sgtty.h>
79196Ssam 
89251Ssam #include <sys/param.h>
96480Ssam #include <sys/socket.h>
109251Ssam #include <sys/file.h>
116480Ssam 
129196Ssam #include <netinet/in.h>
139251Ssam #include <netimp/if_imp.h>
149196Ssam 
156480Ssam #define	LOGFILE	"/usr/adm/implog"
166480Ssam 
176480Ssam u_char	request[1024];
186480Ssam int	marktime();
196480Ssam int	options;
206480Ssam extern	int errno;
216480Ssam int	log;
226480Ssam 
236480Ssam /*
246480Ssam  * Socket address, internet style, with
256480Ssam  * unused space taken by timestamp and packet
266480Ssam  * size.
276480Ssam  */
286480Ssam struct sockstamp {
296480Ssam 	short	sin_family;
306480Ssam 	u_short	sin_port;
316480Ssam 	struct	in_addr sin_addr;
326480Ssam 	time_t	sin_time;
339196Ssam 	int	sin_len;
346480Ssam };
356480Ssam 
366480Ssam main(argc, argv)
376480Ssam 	char *argv[];
386480Ssam {
399196Ssam 	int s;
406480Ssam 	time_t t;
419196Ssam 	struct sockstamp from;
426480Ssam 
436480Ssam 	argc--, argv++;
446480Ssam 	if (argc > 0 && !strcmp(argv[0], "-d"))
456480Ssam 		options |= SO_DEBUG;
468457Ssam #ifndef DEBUG
478457Ssam 	if (fork())
488457Ssam 		exit(0);
498457Ssam 	for (s = 0; s < 10; s++)
508457Ssam 		(void) close(t);
518457Ssam 	(void) open("/", 0);
528457Ssam 	(void) dup2(0, 1);
538457Ssam 	(void) dup2(0, 2);
548457Ssam 	{ int tt = open("/dev/tty", 2);
558457Ssam 	  if (tt > 0) {
568457Ssam 		ioctl(tt, TIOCNOTTY, 0);
578457Ssam 		close(tt);
588457Ssam 	  }
596480Ssam 	}
608457Ssam #endif
619251Ssam 	log = open(LOGFILE, FCREATE|FWRONLY|FAPPEND, 0644);
629251Ssam 	if (log < 0) {
639251Ssam 		perror("implogd: open");
646480Ssam 		exit(1);
659251Ssam 	}
666480Ssam 	from.sin_time = time(0);
679196Ssam 	from.sin_len = sizeof (time_t);
689196Ssam 	write(log, (char *)&from, sizeof (from));
699251Ssam 	while ((s = socket(AF_IMPLINK, SOCK_RAW, 0, 0)) < 0) {
709251Ssam 		perror("implogd: socket");
716480Ssam 		sleep(5);
726480Ssam 	}
736480Ssam 	for (;;) {
74*12237Ssam 		int fromlen = sizeof (from), len;
759196Ssam 
76*12237Ssam 		len = recvfrom(s, request, sizeof (request), 0,
77*12237Ssam 			&from, &fromlen);
78*12237Ssam 		if (len < 0) {
799251Ssam 			perror("implogd: recvfrom");
806480Ssam 			continue;
819251Ssam 		}
82*12237Ssam 		if (len == 0 || len > IMPMTU)	/* sanity */
836480Ssam 			continue;
849196Ssam 		from.sin_len = len;
856480Ssam 		from.sin_time = time(0);
869196Ssam 		write(log, (char *)&from, sizeof (from));
879196Ssam 		write(log, request, len);
886480Ssam 	}
896480Ssam 	/*NOTREACHED*/
906480Ssam }
91