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