xref: /freebsd/usr.bin/talk/invite.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 static const char sccsid[] = "@(#)invite.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <protocols/talkd.h>
45 
46 #include <err.h>
47 #include <errno.h>
48 #include <setjmp.h>
49 #include <signal.h>
50 
51 #include "talk_ctl.h"
52 #include "talk.h"
53 
54 /*
55  * There wasn't an invitation waiting, so send a request containing
56  * our sockt address to the remote talk daemon so it can invite
57  * him
58  */
59 
60 /*
61  * The msg.id's for the invitations
62  * on the local and remote machines.
63  * These are used to delete the
64  * invitations.
65  */
66 int	local_id, remote_id;
67 jmp_buf invitebuf;
68 
69 void
70 invite_remote()
71 {
72 	int new_sockt;
73 	struct itimerval itimer;
74 	CTL_RESPONSE response;
75 
76 	itimer.it_value.tv_sec = RING_WAIT;
77 	itimer.it_value.tv_usec = 0;
78 	itimer.it_interval = itimer.it_value;
79 	if (listen(sockt, 5) != 0)
80 		p_error("Error on attempt to listen for caller");
81 #ifdef MSG_EOR
82 	/* copy new style sockaddr to old, swap family (short in old) */
83 	msg.addr = *(struct osockaddr *)&my_addr;  /* XXX new to old  style*/
84 	msg.addr.sa_family = htons(my_addr.sin_family);
85 #else
86 	msg.addr = *(struct sockaddr *)&my_addr;
87 #endif
88 	msg.id_num = htonl(-1);		/* an impossible id_num */
89 	invitation_waiting = 1;
90 	announce_invite();
91 	/*
92 	 * Shut off the automatic messages for a while,
93 	 * so we can use the interupt timer to resend the invitation
94 	 */
95 	end_msgs();
96 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
97 	message("Waiting for your party to respond");
98 	signal(SIGALRM, re_invite);
99 	(void) setjmp(invitebuf);
100 	while ((new_sockt = accept(sockt, 0, 0)) < 0) {
101 		if (errno == EINTR)
102 			continue;
103 		p_error("Unable to connect with your party");
104 	}
105 	close(sockt);
106 	sockt = new_sockt;
107 
108 	/*
109 	 * Have the daemons delete the invitations now that we
110 	 * have connected.
111 	 */
112 	current_state = "Waiting for your party to respond";
113 	start_msgs();
114 
115 	msg.id_num = htonl(local_id);
116 	ctl_transact(my_machine_addr, msg, DELETE, &response);
117 	msg.id_num = htonl(remote_id);
118 	ctl_transact(his_machine_addr, msg, DELETE, &response);
119 	invitation_waiting = 0;
120 }
121 
122 /*
123  * Routine called on interupt to re-invite the callee
124  */
125 /* ARGSUSED */
126 void
127 re_invite(signo)
128 	int signo __unused;
129 {
130 
131 	message("Ringing your party again");
132 	waddch(my_win.x_win, '\n');
133 	if (current_line < my_win.x_nlines - 1)
134 		current_line++;
135 	/* force a re-announce */
136 	msg.id_num = htonl(remote_id + 1);
137 	announce_invite();
138 	longjmp(invitebuf, 1);
139 }
140 
141 static	const char *answers[] = {
142 	"answer #0",					/* SUCCESS */
143 	"Your party is not logged on",			/* NOT_HERE */
144 	"Target machine is too confused to talk to us",	/* FAILED */
145 	"Target machine does not recognize us",		/* MACHINE_UNKNOWN */
146 	"Your party is refusing messages",		/* PERMISSION_REFUSED */
147 	"Target machine can not handle remote talk",	/* UNKNOWN_REQUEST */
148 	"Target machine indicates protocol mismatch",	/* BADVERSION */
149 	"Target machine indicates protocol botch (addr)",/* BADADDR */
150 	"Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
151 };
152 #define	NANSWERS	(sizeof (answers) / sizeof (answers[0]))
153 
154 /*
155  * Transmit the invitation and process the response
156  */
157 void
158 announce_invite()
159 {
160 	CTL_RESPONSE response;
161 
162 	current_state = "Trying to connect to your party's talk daemon";
163 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
164 	remote_id = response.id_num;
165 	if (response.answer != SUCCESS) {
166 		if (response.answer < NANSWERS)
167 			message(answers[response.answer]);
168 		quit();
169 	}
170 	/* leave the actual invitation on my talk daemon */
171 	current_state = "Trying to connect to local talk daemon";
172 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
173 	local_id = response.id_num;
174 }
175 
176 /*
177  * Tell the daemon to remove your invitation
178  */
179 void
180 send_delete()
181 {
182 
183 	msg.type = DELETE;
184 	/*
185 	 * This is just an extra clean up, so just send it
186 	 * and don't wait for an answer
187 	 */
188 	msg.id_num = htonl(remote_id);
189 	daemon_addr.sin_addr = his_machine_addr;
190 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
191 	    (struct sockaddr *)&daemon_addr,
192 	    sizeof (daemon_addr)) != sizeof(msg))
193 		warn("send_delete (remote)");
194 	msg.id_num = htonl(local_id);
195 	daemon_addr.sin_addr = my_machine_addr;
196 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
197 	    (struct sockaddr *)&daemon_addr,
198 	    sizeof (daemon_addr)) != sizeof (msg))
199 		warn("send_delete (local)");
200 }
201