date and time created 92/10/17 13:25:48 by bostic
[unix-history] / usr / src / old / talk / talkd / talkd.c
CommitLineData
6eb7d7ce
KB
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
13#ifndef lint
14static char sccsid[] = "@(#)talkd.c 5.1 (Berkeley) 6/6/85";
15#endif not lint
16
17/*
18 * The top level of the daemon, the format is heavily borrowed
19 * from rwhod.c. Basically: find out who and where you are;
20 * disconnect all descriptors and ttys, and then endless
21 * loop on waiting for and processing requests
22 */
23#include <stdio.h>
24#include <errno.h>
25#include <signal.h>
26
27#include "ctl.h"
28
29struct sockaddr_in sin = { AF_INET };
30
31CTL_MSG request;
32CTL_RESPONSE response;
33
34int sockt;
35int debug = 0;
36FILE *debugout;
37int timeout();
38long lastmsgtime;
39
40char hostname[32];
41
42#define TIMEOUT 30
43#define MAXIDLE 120
44
45main(argc, argv)
46 int argc;
47 char *argv[];
48{
49 struct sockaddr_in from;
50 int fromlen, cc;
51
52 if (debug)
53 debugout = (FILE *)fopen ("/usr/tmp/talkd.msgs", "w");
54
55 if (getuid()) {
56 fprintf(stderr, "Talkd : not super user\n");
57 exit(1);
58 }
59 gethostname(hostname, sizeof (hostname));
60 (void) chdir("/dev");
61 signal(SIGALRM, timeout);
62 alarm(TIMEOUT);
63 for (;;) {
64 extern int errno;
65
66 fromlen = sizeof(from);
67 cc = recvfrom(0, (char *)&request, sizeof (request), 0,
68 &from, &fromlen);
69 if (cc != sizeof(request)) {
70 if (cc < 0 && errno != EINTR)
71 perror("recvfrom");
72 continue;
73 }
74 lastmsgtime = time(0);
75 swapmsg(&request);
76 if (debug) print_request(&request);
77 process_request(&request, &response);
78 /* can block here, is this what I want? */
79 cc = sendto(sockt, (char *) &response,
80 sizeof (response), 0, &request.ctl_addr,
81 sizeof (request.ctl_addr));
82 if (cc != sizeof(response))
83 perror("sendto");
84 }
85}
86
87timeout()
88{
89
90 if (time(0) - lastmsgtime >= MAXIDLE)
91 exit(0);
92 alarm(TIMEOUT);
93}
94
95/*
96 * heuristic to detect if need to swap bytes
97 */
98
99swapmsg(req)
100 CTL_MSG *req;
101{
102 if (req->ctl_addr.sin_family == ntohs(AF_INET)) {
103 req->id_num = ntohl(req->id_num);
104 req->pid = ntohl(req->pid);
105 req->addr.sin_family = ntohs(req->addr.sin_family);
106 req->ctl_addr.sin_family =
107 ntohs(req->ctl_addr.sin_family);
108 }
109}