1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate *
22*7c478bd9Sstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
23*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate */
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
28*7c478bd9Sstevel@tonic-gate * All Rights Reserved.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
33*7c478bd9Sstevel@tonic-gate * The Regents of the University of California.
34*7c478bd9Sstevel@tonic-gate * All Rights Reserved.
35*7c478bd9Sstevel@tonic-gate *
36*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
37*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
38*7c478bd9Sstevel@tonic-gate * contributors.
39*7c478bd9Sstevel@tonic-gate */
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate * Invoked by the Internet daemon to handle talk requests
43*7c478bd9Sstevel@tonic-gate * Processes talk requests until MAX_LIFE seconds go by with
44*7c478bd9Sstevel@tonic-gate * no action, then dies.
45*7c478bd9Sstevel@tonic-gate */
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate #include <stdio.h>
48*7c478bd9Sstevel@tonic-gate #include <errno.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
53*7c478bd9Sstevel@tonic-gate #include <unistd.h>
54*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
55*7c478bd9Sstevel@tonic-gate #include <string.h>
56*7c478bd9Sstevel@tonic-gate #include "talkd_impl.h"
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate static CTL_MSG request;
59*7c478bd9Sstevel@tonic-gate static CTL_RESPONSE response;
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate char hostname[HOST_NAME_LENGTH];
62*7c478bd9Sstevel@tonic-gate int debug = 0;
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate static CTL_MSG swapmsg(CTL_MSG req);
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate int
main()67*7c478bd9Sstevel@tonic-gate main()
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate struct sockaddr_in from;
70*7c478bd9Sstevel@tonic-gate socklen_t from_size = (socklen_t)sizeof (from);
71*7c478bd9Sstevel@tonic-gate int cc;
72*7c478bd9Sstevel@tonic-gate int name_length = sizeof (hostname);
73*7c478bd9Sstevel@tonic-gate fd_set rfds;
74*7c478bd9Sstevel@tonic-gate struct timeval tv;
75*7c478bd9Sstevel@tonic-gate
76*7c478bd9Sstevel@tonic-gate (void) sysinfo(SI_HOSTNAME, hostname, name_length);
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate for (;;) {
79*7c478bd9Sstevel@tonic-gate tv.tv_sec = MAX_LIFE;
80*7c478bd9Sstevel@tonic-gate tv.tv_usec = 0;
81*7c478bd9Sstevel@tonic-gate FD_ZERO(&rfds);
82*7c478bd9Sstevel@tonic-gate FD_SET(0, &rfds);
83*7c478bd9Sstevel@tonic-gate if (select(1, &rfds, 0, 0, &tv) <= 0)
84*7c478bd9Sstevel@tonic-gate return (0);
85*7c478bd9Sstevel@tonic-gate cc = recvfrom(0, (char *)&request, sizeof (request), 0,
86*7c478bd9Sstevel@tonic-gate (struct sockaddr *)&from, &from_size);
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate if (cc != sizeof (request)) {
89*7c478bd9Sstevel@tonic-gate if (cc < 0 && errno != EINTR) {
90*7c478bd9Sstevel@tonic-gate print_error("receive");
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate } else {
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate if (debug) {
95*7c478bd9Sstevel@tonic-gate (void) printf("Request received : \n");
96*7c478bd9Sstevel@tonic-gate (void) print_request(&request);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate request = swapmsg(request);
100*7c478bd9Sstevel@tonic-gate process_request(&request, &response);
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate if (debug) {
103*7c478bd9Sstevel@tonic-gate (void) printf("Response sent : \n");
104*7c478bd9Sstevel@tonic-gate print_response(&response);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate /*
108*7c478bd9Sstevel@tonic-gate * Can block here, is this what I want?
109*7c478bd9Sstevel@tonic-gate */
110*7c478bd9Sstevel@tonic-gate cc = sendto(0, (char *)&response, sizeof (response), 0,
111*7c478bd9Sstevel@tonic-gate (struct sockaddr *)&request.ctl_addr,
112*7c478bd9Sstevel@tonic-gate (socklen_t)sizeof (request.ctl_addr));
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate if (cc != sizeof (response)) {
115*7c478bd9Sstevel@tonic-gate print_error("Send");
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate void
print_error(char * string)122*7c478bd9Sstevel@tonic-gate print_error(char *string)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate FILE *cons;
125*7c478bd9Sstevel@tonic-gate char *err_dev = "/dev/console";
126*7c478bd9Sstevel@tonic-gate char *sys;
127*7c478bd9Sstevel@tonic-gate pid_t val, pid;
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gate if (debug)
130*7c478bd9Sstevel@tonic-gate err_dev = "/dev/tty";
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate if ((sys = strerror(errno)) == (char *)NULL)
133*7c478bd9Sstevel@tonic-gate sys = "Unknown error";
134*7c478bd9Sstevel@tonic-gate
135*7c478bd9Sstevel@tonic-gate /* don't ever open tty's directly, let a child do it */
136*7c478bd9Sstevel@tonic-gate if ((pid = fork()) == 0) {
137*7c478bd9Sstevel@tonic-gate cons = fopen(err_dev, "a");
138*7c478bd9Sstevel@tonic-gate if (cons != NULL) {
139*7c478bd9Sstevel@tonic-gate (void) fprintf(cons, "Talkd : %s : %s(%d)\n\r", string,
140*7c478bd9Sstevel@tonic-gate sys, errno);
141*7c478bd9Sstevel@tonic-gate (void) fclose(cons);
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate exit(0);
144*7c478bd9Sstevel@tonic-gate } else {
145*7c478bd9Sstevel@tonic-gate /* wait for the child process to return */
146*7c478bd9Sstevel@tonic-gate do {
147*7c478bd9Sstevel@tonic-gate val = wait(0);
148*7c478bd9Sstevel@tonic-gate if (val == (pid_t)-1) {
149*7c478bd9Sstevel@tonic-gate if (errno == EINTR) {
150*7c478bd9Sstevel@tonic-gate continue;
151*7c478bd9Sstevel@tonic-gate } else if (errno == ECHILD) {
152*7c478bd9Sstevel@tonic-gate break;
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate } while (val != pid);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate }
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate #define swapshort(a) (((a << 8) | ((unsigned short) a >> 8)) & 0xffff)
160*7c478bd9Sstevel@tonic-gate #define swaplong(a) ((swapshort(a) << 16) | (swapshort(((unsigned)a >> 16))))
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate /*
163*7c478bd9Sstevel@tonic-gate * Heuristic to detect if need to swap bytes.
164*7c478bd9Sstevel@tonic-gate */
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate static CTL_MSG
swapmsg(CTL_MSG req)167*7c478bd9Sstevel@tonic-gate swapmsg(CTL_MSG req)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate CTL_MSG swapreq;
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate if (req.ctl_addr.sin_family == swapshort(AF_INET)) {
172*7c478bd9Sstevel@tonic-gate swapreq = req;
173*7c478bd9Sstevel@tonic-gate swapreq.id_num = swaplong(req.id_num);
174*7c478bd9Sstevel@tonic-gate swapreq.pid = swaplong(req.pid);
175*7c478bd9Sstevel@tonic-gate swapreq.addr.sin_family = swapshort(req.addr.sin_family);
176*7c478bd9Sstevel@tonic-gate swapreq.ctl_addr.sin_family =
177*7c478bd9Sstevel@tonic-gate swapshort(req.ctl_addr.sin_family);
178*7c478bd9Sstevel@tonic-gate return (swapreq);
179*7c478bd9Sstevel@tonic-gate } else {
180*7c478bd9Sstevel@tonic-gate return (req);
181*7c478bd9Sstevel@tonic-gate }
182*7c478bd9Sstevel@tonic-gate }
183