xref: /freebsd/usr.bin/talk/invite.c (revision bdcbfde3)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1983, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
32caa64801SMark Murray 
33caa64801SMark Murray 
34caa64801SMark Murray #include <sys/types.h>
35caa64801SMark Murray #include <sys/socket.h>
36caa64801SMark Murray #include <protocols/talkd.h>
379b50d902SRodney W. Grimes 
3820868061SPhilippe Charnier #include <err.h>
399b50d902SRodney W. Grimes #include <errno.h>
409b50d902SRodney W. Grimes #include <setjmp.h>
4120868061SPhilippe Charnier #include <signal.h>
423daadfc8SXin LI #include <unistd.h>
43caa64801SMark Murray 
449b50d902SRodney W. Grimes #include "talk_ctl.h"
459b50d902SRodney W. Grimes #include "talk.h"
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes /*
489b50d902SRodney W. Grimes  * There wasn't an invitation waiting, so send a request containing
499b50d902SRodney W. Grimes  * our sockt address to the remote talk daemon so it can invite
509b50d902SRodney W. Grimes  * him
519b50d902SRodney W. Grimes  */
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes /*
549b50d902SRodney W. Grimes  * The msg.id's for the invitations
559b50d902SRodney W. Grimes  * on the local and remote machines.
569b50d902SRodney W. Grimes  * These are used to delete the
579b50d902SRodney W. Grimes  * invitations.
589b50d902SRodney W. Grimes  */
59b8d48d62SEd Schouten static int	local_id, remote_id;
60b8d48d62SEd Schouten static jmp_buf invitebuf;
619b50d902SRodney W. Grimes 
6228592604SJoerg Wunsch void
invite_remote(void)63b6d9e1f3SXin LI invite_remote(void)
649b50d902SRodney W. Grimes {
65caa64801SMark Murray 	int new_sockt;
669b50d902SRodney W. Grimes 	struct itimerval itimer;
679b50d902SRodney W. Grimes 	CTL_RESPONSE response;
689b50d902SRodney W. Grimes 
699b50d902SRodney W. Grimes 	itimer.it_value.tv_sec = RING_WAIT;
709b50d902SRodney W. Grimes 	itimer.it_value.tv_usec = 0;
719b50d902SRodney W. Grimes 	itimer.it_interval = itimer.it_value;
729b50d902SRodney W. Grimes 	if (listen(sockt, 5) != 0)
739b50d902SRodney W. Grimes 		p_error("Error on attempt to listen for caller");
749b50d902SRodney W. Grimes 	/* copy new style sockaddr to old, swap family (short in old) */
75e4478d7eSBrooks Davis 	msg.addr = *(struct tsockaddr *)&my_addr;
769b50d902SRodney W. Grimes 	msg.addr.sa_family = htons(my_addr.sin_family);
779b50d902SRodney W. Grimes 	msg.id_num = htonl(-1);		/* an impossible id_num */
789b50d902SRodney W. Grimes 	invitation_waiting = 1;
799b50d902SRodney W. Grimes 	announce_invite();
809b50d902SRodney W. Grimes 	/*
819b50d902SRodney W. Grimes 	 * Shut off the automatic messages for a while,
82487ac9acSUlrich Spörlein 	 * so we can use the interrupt timer to resend the invitation
839b50d902SRodney W. Grimes 	 */
849b50d902SRodney W. Grimes 	end_msgs();
859b50d902SRodney W. Grimes 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
869b50d902SRodney W. Grimes 	message("Waiting for your party to respond");
879b50d902SRodney W. Grimes 	signal(SIGALRM, re_invite);
889b50d902SRodney W. Grimes 	(void) setjmp(invitebuf);
899b50d902SRodney W. Grimes 	while ((new_sockt = accept(sockt, 0, 0)) < 0) {
909b50d902SRodney W. Grimes 		if (errno == EINTR)
919b50d902SRodney W. Grimes 			continue;
929b50d902SRodney W. Grimes 		p_error("Unable to connect with your party");
939b50d902SRodney W. Grimes 	}
949b50d902SRodney W. Grimes 	close(sockt);
959b50d902SRodney W. Grimes 	sockt = new_sockt;
969b50d902SRodney W. Grimes 
979b50d902SRodney W. Grimes 	/*
989b50d902SRodney W. Grimes 	 * Have the daemons delete the invitations now that we
999b50d902SRodney W. Grimes 	 * have connected.
1009b50d902SRodney W. Grimes 	 */
1019b50d902SRodney W. Grimes 	current_state = "Waiting for your party to respond";
1029b50d902SRodney W. Grimes 	start_msgs();
1039b50d902SRodney W. Grimes 
1049b50d902SRodney W. Grimes 	msg.id_num = htonl(local_id);
1059b50d902SRodney W. Grimes 	ctl_transact(my_machine_addr, msg, DELETE, &response);
1069b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id);
1079b50d902SRodney W. Grimes 	ctl_transact(his_machine_addr, msg, DELETE, &response);
1089b50d902SRodney W. Grimes 	invitation_waiting = 0;
1099b50d902SRodney W. Grimes }
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes /*
112487ac9acSUlrich Spörlein  * Routine called on interrupt to re-invite the callee
1139b50d902SRodney W. Grimes  */
11428592604SJoerg Wunsch /* ARGSUSED */
1159b50d902SRodney W. Grimes void
re_invite(int signo __unused)116b6d9e1f3SXin LI re_invite(int signo __unused)
1179b50d902SRodney W. Grimes {
1189b50d902SRodney W. Grimes 
1199b50d902SRodney W. Grimes 	message("Ringing your party again");
120b02b5aadSAndrey A. Chernov 	waddch(my_win.x_win, '\n');
121b02b5aadSAndrey A. Chernov 	if (current_line < my_win.x_nlines - 1)
1229b50d902SRodney W. Grimes 		current_line++;
1239b50d902SRodney W. Grimes 	/* force a re-announce */
1249b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id + 1);
1259b50d902SRodney W. Grimes 	announce_invite();
1269b50d902SRodney W. Grimes 	longjmp(invitebuf, 1);
1279b50d902SRodney W. Grimes }
1289b50d902SRodney W. Grimes 
129caa64801SMark Murray static	const char *answers[] = {
1309b50d902SRodney W. Grimes 	"answer #0",					/* SUCCESS */
1319b50d902SRodney W. Grimes 	"Your party is not logged on",			/* NOT_HERE */
1329b50d902SRodney W. Grimes 	"Target machine is too confused to talk to us",	/* FAILED */
1339b50d902SRodney W. Grimes 	"Target machine does not recognize us",		/* MACHINE_UNKNOWN */
1349b50d902SRodney W. Grimes 	"Your party is refusing messages",		/* PERMISSION_REFUSED */
1359b50d902SRodney W. Grimes 	"Target machine can not handle remote talk",	/* UNKNOWN_REQUEST */
1369b50d902SRodney W. Grimes 	"Target machine indicates protocol mismatch",	/* BADVERSION */
1379b50d902SRodney W. Grimes 	"Target machine indicates protocol botch (addr)",/* BADADDR */
1389b50d902SRodney W. Grimes 	"Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
1399b50d902SRodney W. Grimes };
1409b50d902SRodney W. Grimes #define	NANSWERS	(sizeof (answers) / sizeof (answers[0]))
1419b50d902SRodney W. Grimes 
1429b50d902SRodney W. Grimes /*
1439b50d902SRodney W. Grimes  * Transmit the invitation and process the response
1449b50d902SRodney W. Grimes  */
14528592604SJoerg Wunsch void
announce_invite(void)146b6d9e1f3SXin LI announce_invite(void)
1479b50d902SRodney W. Grimes {
1489b50d902SRodney W. Grimes 	CTL_RESPONSE response;
1499b50d902SRodney W. Grimes 
1509b50d902SRodney W. Grimes 	current_state = "Trying to connect to your party's talk daemon";
1519b50d902SRodney W. Grimes 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
1529b50d902SRodney W. Grimes 	remote_id = response.id_num;
1539b50d902SRodney W. Grimes 	if (response.answer != SUCCESS) {
1549b50d902SRodney W. Grimes 		if (response.answer < NANSWERS)
1559b50d902SRodney W. Grimes 			message(answers[response.answer]);
1569b50d902SRodney W. Grimes 		quit();
1579b50d902SRodney W. Grimes 	}
1589b50d902SRodney W. Grimes 	/* leave the actual invitation on my talk daemon */
1599b9c06b0SRuslan Ermilov 	current_state = "Trying to connect to local talk daemon";
1609b50d902SRodney W. Grimes 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
1619b50d902SRodney W. Grimes 	local_id = response.id_num;
1629b50d902SRodney W. Grimes }
1639b50d902SRodney W. Grimes 
1649b50d902SRodney W. Grimes /*
1659b50d902SRodney W. Grimes  * Tell the daemon to remove your invitation
1669b50d902SRodney W. Grimes  */
16728592604SJoerg Wunsch void
send_delete(void)168b6d9e1f3SXin LI send_delete(void)
1699b50d902SRodney W. Grimes {
1709b50d902SRodney W. Grimes 
1719b50d902SRodney W. Grimes 	msg.type = DELETE;
1729b50d902SRodney W. Grimes 	/*
1739d5abbddSJens Schweikhardt 	 * This is just an extra clean up, so just send it
1749b50d902SRodney W. Grimes 	 * and don't wait for an answer
1759b50d902SRodney W. Grimes 	 */
1769b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id);
1779b50d902SRodney W. Grimes 	daemon_addr.sin_addr = his_machine_addr;
1789b50d902SRodney W. Grimes 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
1799b50d902SRodney W. Grimes 	    (struct sockaddr *)&daemon_addr,
1809b50d902SRodney W. Grimes 	    sizeof (daemon_addr)) != sizeof(msg))
18120868061SPhilippe Charnier 		warn("send_delete (remote)");
1829b50d902SRodney W. Grimes 	msg.id_num = htonl(local_id);
1839b50d902SRodney W. Grimes 	daemon_addr.sin_addr = my_machine_addr;
1849b50d902SRodney W. Grimes 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
1859b50d902SRodney W. Grimes 	    (struct sockaddr *)&daemon_addr,
1869b50d902SRodney W. Grimes 	    sizeof (daemon_addr)) != sizeof (msg))
18720868061SPhilippe Charnier 		warn("send_delete (local)");
1889b50d902SRodney W. Grimes }
189