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