1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 1994 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28*0Sstevel@tonic-gate /* All Rights Reserved */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate * The Regents of the University of California
33*0Sstevel@tonic-gate * All Rights Reserved
34*0Sstevel@tonic-gate *
35*0Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate * contributors.
38*0Sstevel@tonic-gate */
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #include "talk_ctl.h"
43*0Sstevel@tonic-gate #include <sys/time.h>
44*0Sstevel@tonic-gate #include <signal.h>
45*0Sstevel@tonic-gate #include <setjmp.h>
46*0Sstevel@tonic-gate #include <libintl.h>
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate #ifdef SYSV
49*0Sstevel@tonic-gate #define signal(s, f) sigset(s, f)
50*0Sstevel@tonic-gate #endif /* SYSV */
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate * there wasn't an invitation waiting, so send a request containing
54*0Sstevel@tonic-gate * our sockt address to the remote talk daemon so it can invite
55*0Sstevel@tonic-gate * him
56*0Sstevel@tonic-gate */
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate static int local_id, remote_id;
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate * the msg.id's for the invitations
62*0Sstevel@tonic-gate * on the local and remote machines.
63*0Sstevel@tonic-gate * These are used to delete the invitations.
64*0Sstevel@tonic-gate */
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate static jmp_buf invitebuf;
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate static void re_invite();
69*0Sstevel@tonic-gate static void announce_invite();
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate void
invite_remote()72*0Sstevel@tonic-gate invite_remote()
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate int new_sockt;
75*0Sstevel@tonic-gate struct itimerval itimer;
76*0Sstevel@tonic-gate CTL_RESPONSE response;
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate itimer.it_value.tv_sec = RING_WAIT;
79*0Sstevel@tonic-gate itimer.it_value.tv_usec = 0;
80*0Sstevel@tonic-gate itimer.it_interval = itimer.it_value;
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate if (listen(sockt, 5) != 0) {
83*0Sstevel@tonic-gate p_error(gettext("Error on attempt to listen for caller"));
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate msg.addr = my_addr;
87*0Sstevel@tonic-gate msg.id_num = -1; /* an impossible id_num */
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate invitation_waiting = 1;
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate announce_invite();
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate /*
94*0Sstevel@tonic-gate * shut off the automatic messages for a while,
95*0Sstevel@tonic-gate * so we can use the interupt timer to resend the invitation
96*0Sstevel@tonic-gate */
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate end_msgs();
99*0Sstevel@tonic-gate setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
100*0Sstevel@tonic-gate message(gettext("Waiting for your party to respond"));
101*0Sstevel@tonic-gate signal(SIGALRM, re_invite);
102*0Sstevel@tonic-gate (void) setjmp(invitebuf);
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate while ((new_sockt = accept(sockt, 0, 0)) < 0) {
105*0Sstevel@tonic-gate if (errno != EINTR) {
106*0Sstevel@tonic-gate p_error(gettext("Unable to connect with your party"));
107*0Sstevel@tonic-gate } else {
108*0Sstevel@tonic-gate /* we just returned from a interupt, keep trying */
109*0Sstevel@tonic-gate continue;
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate close(sockt);
114*0Sstevel@tonic-gate sockt = new_sockt;
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate /*
117*0Sstevel@tonic-gate * have the daemons delete the invitations now that we have connected.
118*0Sstevel@tonic-gate */
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate current_state = strdup(gettext("Waiting for your party to respond"));
121*0Sstevel@tonic-gate start_msgs();
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate msg.id_num = local_id;
124*0Sstevel@tonic-gate ctl_transact(my_machine_addr, msg, DELETE, &response);
125*0Sstevel@tonic-gate msg.id_num = remote_id;
126*0Sstevel@tonic-gate ctl_transact(rem_machine_addr, msg, DELETE, &response);
127*0Sstevel@tonic-gate invitation_waiting = 0;
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate /* routine called on interupt to re-invite the callee */
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate static void
re_invite()133*0Sstevel@tonic-gate re_invite()
134*0Sstevel@tonic-gate {
135*0Sstevel@tonic-gate message(gettext("Ringing your party again"));
136*0Sstevel@tonic-gate current_line++;
137*0Sstevel@tonic-gate /* force a re-announce */
138*0Sstevel@tonic-gate msg.id_num = remote_id + 1;
139*0Sstevel@tonic-gate announce_invite();
140*0Sstevel@tonic-gate longjmp(invitebuf, 1);
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate /* transmit the invitation and process the response */
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate static void
announce_invite()146*0Sstevel@tonic-gate announce_invite()
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate CTL_RESPONSE response;
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate current_state =
151*0Sstevel@tonic-gate gettext("Trying to connect to your party's talk daemon");
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate ctl_transact(rem_machine_addr, msg, ANNOUNCE, &response);
154*0Sstevel@tonic-gate remote_id = response.id_num;
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate if (response.answer != SUCCESS) {
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate switch (response.answer) {
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate case NOT_HERE :
161*0Sstevel@tonic-gate message(gettext("Your party is not logged on"));
162*0Sstevel@tonic-gate break;
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate case MACHINE_UNKNOWN :
165*0Sstevel@tonic-gate message(
166*0Sstevel@tonic-gate gettext("Target machine does not recognize us"));
167*0Sstevel@tonic-gate break;
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate case UNKNOWN_REQUEST :
170*0Sstevel@tonic-gate message(
171*0Sstevel@tonic-gate gettext("Target machine can not handle remote talk"));
172*0Sstevel@tonic-gate break;
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate case FAILED :
175*0Sstevel@tonic-gate message(
176*0Sstevel@tonic-gate gettext("Target machine is too confused to talk to us"));
177*0Sstevel@tonic-gate break;
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate case PERMISSION_DENIED :
180*0Sstevel@tonic-gate message(gettext("Your party is refusing messages"));
181*0Sstevel@tonic-gate break;
182*0Sstevel@tonic-gate }
183*0Sstevel@tonic-gate
184*0Sstevel@tonic-gate quit();
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate /* leave the actual invitation on my talk daemon */
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
190*0Sstevel@tonic-gate local_id = response.id_num;
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate void
send_delete()194*0Sstevel@tonic-gate send_delete()
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate /* tell the daemon to remove your invitation */
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate msg.type = DELETE;
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate /*
202*0Sstevel@tonic-gate * this is just a extra clean up, so just send it
203*0Sstevel@tonic-gate * and don't wait for an answer
204*0Sstevel@tonic-gate */
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate msg.id_num = remote_id;
207*0Sstevel@tonic-gate daemon_addr.sin_addr = rem_machine_addr;
208*0Sstevel@tonic-gate if (sendto(ctl_sockt, (char *)&msg, sizeof (CTL_MSG), 0,
209*0Sstevel@tonic-gate (struct sockaddr *)&daemon_addr,
210*0Sstevel@tonic-gate sizeof (daemon_addr)) != sizeof (CTL_MSG)) {
211*0Sstevel@tonic-gate perror(gettext("send_delete remote"));
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate msg.id_num = local_id;
215*0Sstevel@tonic-gate daemon_addr.sin_addr = my_machine_addr;
216*0Sstevel@tonic-gate if (sendto(ctl_sockt, (char *)&msg, sizeof (CTL_MSG), 0,
217*0Sstevel@tonic-gate (struct sockaddr *)&daemon_addr,
218*0Sstevel@tonic-gate sizeof (daemon_addr)) != sizeof (CTL_MSG)) {
219*0Sstevel@tonic-gate perror(gettext("send_delete local"));
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate }
222