xref: /freebsd/usr.bin/talk/invite.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro 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 #include <sys/cdefs.h>
33caa64801SMark Murray 
34caa64801SMark Murray __FBSDID("$FreeBSD$");
35caa64801SMark Murray 
369b50d902SRodney W. Grimes #ifndef lint
37caa64801SMark Murray static const char sccsid[] = "@(#)invite.c	8.1 (Berkeley) 6/6/93";
3820868061SPhilippe Charnier #endif
39caa64801SMark Murray 
40caa64801SMark Murray #include <sys/types.h>
41caa64801SMark Murray #include <sys/socket.h>
42caa64801SMark Murray #include <protocols/talkd.h>
439b50d902SRodney W. Grimes 
4420868061SPhilippe Charnier #include <err.h>
459b50d902SRodney W. Grimes #include <errno.h>
469b50d902SRodney W. Grimes #include <setjmp.h>
4720868061SPhilippe Charnier #include <signal.h>
483daadfc8SXin LI #include <unistd.h>
49caa64801SMark Murray 
509b50d902SRodney W. Grimes #include "talk_ctl.h"
519b50d902SRodney W. Grimes #include "talk.h"
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes /*
549b50d902SRodney W. Grimes  * There wasn't an invitation waiting, so send a request containing
559b50d902SRodney W. Grimes  * our sockt address to the remote talk daemon so it can invite
569b50d902SRodney W. Grimes  * him
579b50d902SRodney W. Grimes  */
589b50d902SRodney W. Grimes 
599b50d902SRodney W. Grimes /*
609b50d902SRodney W. Grimes  * The msg.id's for the invitations
619b50d902SRodney W. Grimes  * on the local and remote machines.
629b50d902SRodney W. Grimes  * These are used to delete the
639b50d902SRodney W. Grimes  * invitations.
649b50d902SRodney W. Grimes  */
65b8d48d62SEd Schouten static int	local_id, remote_id;
66b8d48d62SEd Schouten static jmp_buf invitebuf;
679b50d902SRodney W. Grimes 
6828592604SJoerg Wunsch void
69b6d9e1f3SXin LI invite_remote(void)
709b50d902SRodney W. Grimes {
71caa64801SMark Murray 	int new_sockt;
729b50d902SRodney W. Grimes 	struct itimerval itimer;
739b50d902SRodney W. Grimes 	CTL_RESPONSE response;
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes 	itimer.it_value.tv_sec = RING_WAIT;
769b50d902SRodney W. Grimes 	itimer.it_value.tv_usec = 0;
779b50d902SRodney W. Grimes 	itimer.it_interval = itimer.it_value;
789b50d902SRodney W. Grimes 	if (listen(sockt, 5) != 0)
799b50d902SRodney W. Grimes 		p_error("Error on attempt to listen for caller");
809b50d902SRodney W. Grimes #ifdef MSG_EOR
819b50d902SRodney W. Grimes 	/* copy new style sockaddr to old, swap family (short in old) */
829b50d902SRodney W. Grimes 	msg.addr = *(struct osockaddr *)&my_addr;  /* XXX new to old  style*/
839b50d902SRodney W. Grimes 	msg.addr.sa_family = htons(my_addr.sin_family);
849b50d902SRodney W. Grimes #else
859b50d902SRodney W. Grimes 	msg.addr = *(struct sockaddr *)&my_addr;
869b50d902SRodney W. Grimes #endif
879b50d902SRodney W. Grimes 	msg.id_num = htonl(-1);		/* an impossible id_num */
889b50d902SRodney W. Grimes 	invitation_waiting = 1;
899b50d902SRodney W. Grimes 	announce_invite();
909b50d902SRodney W. Grimes 	/*
919b50d902SRodney W. Grimes 	 * Shut off the automatic messages for a while,
92487ac9acSUlrich Spörlein 	 * so we can use the interrupt timer to resend the invitation
939b50d902SRodney W. Grimes 	 */
949b50d902SRodney W. Grimes 	end_msgs();
959b50d902SRodney W. Grimes 	setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
969b50d902SRodney W. Grimes 	message("Waiting for your party to respond");
979b50d902SRodney W. Grimes 	signal(SIGALRM, re_invite);
989b50d902SRodney W. Grimes 	(void) setjmp(invitebuf);
999b50d902SRodney W. Grimes 	while ((new_sockt = accept(sockt, 0, 0)) < 0) {
1009b50d902SRodney W. Grimes 		if (errno == EINTR)
1019b50d902SRodney W. Grimes 			continue;
1029b50d902SRodney W. Grimes 		p_error("Unable to connect with your party");
1039b50d902SRodney W. Grimes 	}
1049b50d902SRodney W. Grimes 	close(sockt);
1059b50d902SRodney W. Grimes 	sockt = new_sockt;
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes 	/*
1089b50d902SRodney W. Grimes 	 * Have the daemons delete the invitations now that we
1099b50d902SRodney W. Grimes 	 * have connected.
1109b50d902SRodney W. Grimes 	 */
1119b50d902SRodney W. Grimes 	current_state = "Waiting for your party to respond";
1129b50d902SRodney W. Grimes 	start_msgs();
1139b50d902SRodney W. Grimes 
1149b50d902SRodney W. Grimes 	msg.id_num = htonl(local_id);
1159b50d902SRodney W. Grimes 	ctl_transact(my_machine_addr, msg, DELETE, &response);
1169b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id);
1179b50d902SRodney W. Grimes 	ctl_transact(his_machine_addr, msg, DELETE, &response);
1189b50d902SRodney W. Grimes 	invitation_waiting = 0;
1199b50d902SRodney W. Grimes }
1209b50d902SRodney W. Grimes 
1219b50d902SRodney W. Grimes /*
122487ac9acSUlrich Spörlein  * Routine called on interrupt to re-invite the callee
1239b50d902SRodney W. Grimes  */
12428592604SJoerg Wunsch /* ARGSUSED */
1259b50d902SRodney W. Grimes void
126b6d9e1f3SXin LI re_invite(int signo __unused)
1279b50d902SRodney W. Grimes {
1289b50d902SRodney W. Grimes 
1299b50d902SRodney W. Grimes 	message("Ringing your party again");
130b02b5aadSAndrey A. Chernov 	waddch(my_win.x_win, '\n');
131b02b5aadSAndrey A. Chernov 	if (current_line < my_win.x_nlines - 1)
1329b50d902SRodney W. Grimes 		current_line++;
1339b50d902SRodney W. Grimes 	/* force a re-announce */
1349b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id + 1);
1359b50d902SRodney W. Grimes 	announce_invite();
1369b50d902SRodney W. Grimes 	longjmp(invitebuf, 1);
1379b50d902SRodney W. Grimes }
1389b50d902SRodney W. Grimes 
139caa64801SMark Murray static	const char *answers[] = {
1409b50d902SRodney W. Grimes 	"answer #0",					/* SUCCESS */
1419b50d902SRodney W. Grimes 	"Your party is not logged on",			/* NOT_HERE */
1429b50d902SRodney W. Grimes 	"Target machine is too confused to talk to us",	/* FAILED */
1439b50d902SRodney W. Grimes 	"Target machine does not recognize us",		/* MACHINE_UNKNOWN */
1449b50d902SRodney W. Grimes 	"Your party is refusing messages",		/* PERMISSION_REFUSED */
1459b50d902SRodney W. Grimes 	"Target machine can not handle remote talk",	/* UNKNOWN_REQUEST */
1469b50d902SRodney W. Grimes 	"Target machine indicates protocol mismatch",	/* BADVERSION */
1479b50d902SRodney W. Grimes 	"Target machine indicates protocol botch (addr)",/* BADADDR */
1489b50d902SRodney W. Grimes 	"Target machine indicates protocol botch (ctl_addr)",/* BADCTLADDR */
1499b50d902SRodney W. Grimes };
1509b50d902SRodney W. Grimes #define	NANSWERS	(sizeof (answers) / sizeof (answers[0]))
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes /*
1539b50d902SRodney W. Grimes  * Transmit the invitation and process the response
1549b50d902SRodney W. Grimes  */
15528592604SJoerg Wunsch void
156b6d9e1f3SXin LI announce_invite(void)
1579b50d902SRodney W. Grimes {
1589b50d902SRodney W. Grimes 	CTL_RESPONSE response;
1599b50d902SRodney W. Grimes 
1609b50d902SRodney W. Grimes 	current_state = "Trying to connect to your party's talk daemon";
1619b50d902SRodney W. Grimes 	ctl_transact(his_machine_addr, msg, ANNOUNCE, &response);
1629b50d902SRodney W. Grimes 	remote_id = response.id_num;
1639b50d902SRodney W. Grimes 	if (response.answer != SUCCESS) {
1649b50d902SRodney W. Grimes 		if (response.answer < NANSWERS)
1659b50d902SRodney W. Grimes 			message(answers[response.answer]);
1669b50d902SRodney W. Grimes 		quit();
1679b50d902SRodney W. Grimes 	}
1689b50d902SRodney W. Grimes 	/* leave the actual invitation on my talk daemon */
1699b9c06b0SRuslan Ermilov 	current_state = "Trying to connect to local talk daemon";
1709b50d902SRodney W. Grimes 	ctl_transact(my_machine_addr, msg, LEAVE_INVITE, &response);
1719b50d902SRodney W. Grimes 	local_id = response.id_num;
1729b50d902SRodney W. Grimes }
1739b50d902SRodney W. Grimes 
1749b50d902SRodney W. Grimes /*
1759b50d902SRodney W. Grimes  * Tell the daemon to remove your invitation
1769b50d902SRodney W. Grimes  */
17728592604SJoerg Wunsch void
178b6d9e1f3SXin LI send_delete(void)
1799b50d902SRodney W. Grimes {
1809b50d902SRodney W. Grimes 
1819b50d902SRodney W. Grimes 	msg.type = DELETE;
1829b50d902SRodney W. Grimes 	/*
1839d5abbddSJens Schweikhardt 	 * This is just an extra clean up, so just send it
1849b50d902SRodney W. Grimes 	 * and don't wait for an answer
1859b50d902SRodney W. Grimes 	 */
1869b50d902SRodney W. Grimes 	msg.id_num = htonl(remote_id);
1879b50d902SRodney W. Grimes 	daemon_addr.sin_addr = his_machine_addr;
1889b50d902SRodney W. Grimes 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
1899b50d902SRodney W. Grimes 	    (struct sockaddr *)&daemon_addr,
1909b50d902SRodney W. Grimes 	    sizeof (daemon_addr)) != sizeof(msg))
19120868061SPhilippe Charnier 		warn("send_delete (remote)");
1929b50d902SRodney W. Grimes 	msg.id_num = htonl(local_id);
1939b50d902SRodney W. Grimes 	daemon_addr.sin_addr = my_machine_addr;
1949b50d902SRodney W. Grimes 	if (sendto(ctl_sockt, &msg, sizeof (msg), 0,
1959b50d902SRodney W. Grimes 	    (struct sockaddr *)&daemon_addr,
1969b50d902SRodney W. Grimes 	    sizeof (daemon_addr)) != sizeof (msg))
19720868061SPhilippe Charnier 		warn("send_delete (local)");
1989b50d902SRodney W. Grimes }
199