1*66683Sbostic /*-
2*66683Sbostic * Copyright (c) 1983, 1985
3*66683Sbostic * The Regents of the University of California. All rights reserved.
4*66683Sbostic *
5*66683Sbostic * %sccs.include.redist.c%
656575Sbostic */
756575Sbostic
856575Sbostic #ifndef lint
956575Sbostic char copyright[] =
10*66683Sbostic "@(#) Copyright (c) 1983, 1985\n\
11*66683Sbostic Regents of the University of California. All rights reserved.\n";
1256575Sbostic #endif not lint
1356575Sbostic
1456575Sbostic #ifndef lint
1556575Sbostic static char sccsid[] = "@(#)talkd.c 5.1 (Berkeley) 6/6/85";
1656575Sbostic #endif not lint
1756575Sbostic
1856575Sbostic /*
1956575Sbostic * The top level of the daemon, the format is heavily borrowed
2056575Sbostic * from rwhod.c. Basically: find out who and where you are;
2156575Sbostic * disconnect all descriptors and ttys, and then endless
2256575Sbostic * loop on waiting for and processing requests
2356575Sbostic */
2456575Sbostic #include <stdio.h>
2556575Sbostic #include <errno.h>
2656575Sbostic #include <signal.h>
2756575Sbostic
2856575Sbostic #include "ctl.h"
2956575Sbostic
3056575Sbostic struct sockaddr_in sin = { AF_INET };
3156575Sbostic
3256575Sbostic CTL_MSG request;
3356575Sbostic CTL_RESPONSE response;
3456575Sbostic
3556575Sbostic int sockt;
3656575Sbostic int debug = 0;
3756575Sbostic FILE *debugout;
3856579Sbostic void timeout();
3956575Sbostic long lastmsgtime;
4056575Sbostic
4156575Sbostic char hostname[32];
4256575Sbostic
4356575Sbostic #define TIMEOUT 30
4456575Sbostic #define MAXIDLE 120
4556575Sbostic
main(argc,argv)4656575Sbostic main(argc, argv)
4756575Sbostic int argc;
4856575Sbostic char *argv[];
4956575Sbostic {
5056575Sbostic struct sockaddr_in from;
5156575Sbostic int fromlen, cc;
5256575Sbostic
5356575Sbostic if (debug)
5456575Sbostic debugout = (FILE *)fopen ("/usr/tmp/talkd.msgs", "w");
5556575Sbostic
5656575Sbostic if (getuid()) {
5756575Sbostic fprintf(stderr, "Talkd : not super user\n");
5856575Sbostic exit(1);
5956575Sbostic }
6056575Sbostic gethostname(hostname, sizeof (hostname));
6156575Sbostic (void) chdir("/dev");
6256575Sbostic signal(SIGALRM, timeout);
6356575Sbostic alarm(TIMEOUT);
6456575Sbostic for (;;) {
6556575Sbostic extern int errno;
6656575Sbostic
6756575Sbostic fromlen = sizeof(from);
6856579Sbostic cc = recvfrom(0, (char *) &request, sizeof (request), 0,
6956579Sbostic (struct sockaddr *)&from, &fromlen);
7056575Sbostic if (cc != sizeof(request)) {
7156575Sbostic if (cc < 0 && errno != EINTR)
7256575Sbostic perror("recvfrom");
7356575Sbostic continue;
7456575Sbostic }
7556575Sbostic lastmsgtime = time(0);
7656575Sbostic swapmsg(&request);
7756575Sbostic if (debug) print_request(&request);
7856575Sbostic process_request(&request, &response);
7956575Sbostic /* can block here, is this what I want? */
8056575Sbostic cc = sendto(sockt, (char *) &response,
8156579Sbostic sizeof (response), 0, (struct sockaddr *)&request.ctl_addr,
8256575Sbostic sizeof (request.ctl_addr));
8356575Sbostic if (cc != sizeof(response))
8456575Sbostic perror("sendto");
8556575Sbostic }
8656575Sbostic }
8756575Sbostic
8856579Sbostic void
timeout()8956575Sbostic timeout()
9056575Sbostic {
9156575Sbostic
9256575Sbostic if (time(0) - lastmsgtime >= MAXIDLE)
9356575Sbostic exit(0);
9456575Sbostic alarm(TIMEOUT);
9556575Sbostic }
9656575Sbostic
9756575Sbostic /*
9856575Sbostic * heuristic to detect if need to swap bytes
9956575Sbostic */
10056575Sbostic
swapmsg(req)10156575Sbostic swapmsg(req)
10256575Sbostic CTL_MSG *req;
10356575Sbostic {
10456575Sbostic if (req->ctl_addr.sin_family == ntohs(AF_INET)) {
10556575Sbostic req->id_num = ntohl(req->id_num);
10656575Sbostic req->pid = ntohl(req->pid);
10756575Sbostic req->addr.sin_family = ntohs(req->addr.sin_family);
10856575Sbostic req->ctl_addr.sin_family =
10956575Sbostic ntohs(req->ctl_addr.sin_family);
11056575Sbostic }
11156575Sbostic }
112